Skip to content

Commit

Permalink
Include dynamic route discoverability and compilation (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
uditdc authored Aug 18, 2023
1 parent 6caa82b commit b04d9c5
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 102 deletions.
14 changes: 10 additions & 4 deletions src/commands/sites/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ export const run = async (options: {

// check for and store unmodified wasm file name to change later
const buildConfig = !debug ? build_release : build
const deployConfig = deployment
const buildDir = resolve(path, buildConfig.dir || '.bls')
const buildName = buildConfig.entry ? buildConfig.entry.replace('.wasm', '') : name
const wasmName = buildConfig.entry || `${name}.wasm`
const wasmArchive = `${buildName}.tar.gz`

// Rebuild function if requested
if (!fs.existsSync(resolve(buildDir, wasmName)) || rebuild) {
buildSiteWasm(wasmName, buildDir, path, buildConfig, debug)
} else if (fs.existsSync(resolve(buildDir, wasmName)) && !rebuild) {
// Bail if file is present and rebuild is not requested
if (fs.existsSync(resolve(buildDir, wasmName)) && !rebuild) {
return
}

Expand All @@ -48,6 +47,9 @@ export const run = async (options: {
content_type
)

// Build site WASM
await buildSiteWasm(wasmName, buildDir, path, buildConfig, debug)

// Create a WASM archive
const archive = createWasmArchive(buildDir, wasmArchive, wasmName)
const checksum = generateChecksum(archive)
Expand All @@ -60,6 +62,10 @@ export const run = async (options: {
result_type: "string",
})

if (deployConfig) {
wasmManifest.permissions = deployConfig.permissions || []
}

// Store manifest
fs.writeFileSync(`${buildDir}/manifest.json`, JSON.stringify(wasmManifest))

Expand Down
13 changes: 6 additions & 7 deletions src/commands/sites/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ export function sitesCli(yargs: Argv) {
type: 'string',
default: undefined
})
.option('debug', {
alias: 'd',
description: 'Add a debug flag to the function build',
type: 'boolean',
default: false
})
.group(['debug'], 'Options:')
},
(argv) => {
runBuild(argv as any)
Expand All @@ -89,6 +82,12 @@ export function sitesCli(yargs: Argv) {
type: 'string',
default: undefined
})
.option('rebuild', {
description: 'Rebuild the funciton',
type: 'boolean',
default: true
})
.group(['rebuild'], 'Options:')
},
(argv) => {
runPreview(argv)
Expand Down
52 changes: 51 additions & 1 deletion src/commands/sites/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const run = async (options: any) => {
const { build, build_release } = parseBlsConfig()

// Execute the build command
runBuild({ path, debug, rebuild })
await runBuild({ path, debug, rebuild })

// check for and store unmodified wasm file name to change later
const buildConfig = !debug ? build_release : build
Expand Down Expand Up @@ -94,6 +94,56 @@ export const run = async (options: any) => {
})

fastify.get("*", async (request, reply) => {
let qs = ''
let headerString = ''
let requestPath = decodeURIComponent(request.url.trim())

if (requestPath.includes('?')) {
qs = requestPath.split('?')[1]
requestPath = requestPath.split('?')[0]
}

if (request.headers) {
headerString = Object.entries(request.headers)
.map(([key, value]) => `${key}=${value}`)
.join('&')
}

let envString = ''
let envVars = [] as string[]
let envVarsKeys = [] as string[]

if (!!options.env) {
// Validate environment variables
const vars = typeof options.env === 'string' ? [options.env] : options.env
vars.map((v: string) => {
const split = v.split('=')
if (split.length !== 2) return

envVars.push(v)
envVarsKeys.push(split[0])
})
}

envVars.push(`BLS_REQUEST_PATH="${requestPath}"`)
envVars.push(`BLS_REQUEST_QUERY="${qs}"`)
envVars.push(`BLS_REQUEST_METHOD="${request.method}"`)
envVars.push(`BLS_REQUEST_HEADERS="${headerString}"`)
envVarsKeys.push('BLS_REQUEST_PATH')
envVarsKeys.push('BLS_REQUEST_QUERY')
envVarsKeys.push('BLS_REQUEST_METHOD')
envVarsKeys.push('BLS_REQUEST_HEADERS')

if (request.body) {
envVars.push(`BLS_REQUEST_BODY="${JSON.stringify(request.body)}"`)
envVarsKeys.push('BLS_REQUEST_BODY')
}

// Include environment string if there are variables
if (envVars.length > 0) {
envString = `env ${envVars.join(' ')} BLS_LIST_VARS=\"${envVarsKeys.join(';')}\"`
}

const result = execSync(`echo "${decodeURIComponent(request.url.trim())}" | ${envString} ${runtimePath} ${manifestPath}`, {
cwd: path,
maxBuffer: (10000 * 1024)
Expand Down
Loading

0 comments on commit b04d9c5

Please sign in to comment.