Skip to content

Commit

Permalink
feat: load bootstrap from deploy URL (#7)
Browse files Browse the repository at this point in the history
* feat: load bootstrap from deploy URL

* feat: support loading boot URL from environment variable
  • Loading branch information
eduardoboucas committed Mar 18, 2022
1 parent 17972fb commit 01f1285
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@ module.exports = {
rules: {
'node/no-missing-import': 'off',
},
overrides: [...overrides],
overrides: [
...overrides,
{
files: ['test/**/*.ts'],
rules: {
'max-statements': 'off',
'no-magic-numbers': 'off',
},
},
],
}
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@commitlint/config-conventional": "^16.0.0",
"@netlify/eslint-config-node": "^4.1.7",
"@types/glob-to-regexp": "^0.4.1",
"@types/node": "^17.0.10",
"@types/node": "^17.0.21",
"@types/semver": "^7.3.9",
"@types/sinon": "^10.0.8",
"@types/uuid": "^8.3.4",
Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { env } from 'process'

const BOOTSTRAP_LATEST =
'https://dinosaurs:are-the-future!@62337d29c8edd9000870ec20--edge-bootstrap.netlify.app/index.ts'

const getBootstrapURL = () => env.NETLIFY_EDGE_BOOTSTRAP ?? BOOTSTRAP_LATEST

export const getBootstrapImport = () => `import { boot } from "${getBootstrapURL()}";`
3 changes: 2 additions & 1 deletion src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pathToFileURL } from 'url'

import { v4 as uuidv4 } from 'uuid'

import { getBootstrapImport } from './bootstrap.js'
import { DenoBridge, LifecycleHook } from './bridge.js'
import type { BundleAlternate } from './bundle_alternate.js'
import type { Declaration } from './declaration.js'
Expand Down Expand Up @@ -122,7 +123,7 @@ const createFinalBundles = async (bundleOps: Promise<string>[], distDirectory: s

const generateEntrypoint = (handlers: Handler[], distDirectory: string) => {
const lines = handlers.map((handler, index) => generateHandlerReference(handler, index, distDirectory))
const bootImport = 'import { boot } from "https://dinosaurs:are-the-future!@edge-bootstrap.netlify.app/index.ts";'
const bootImport = getBootstrapImport()
const importLines = lines.map(({ importLine }) => importLine).join('\n')
const exportLines = lines.map(({ exportLine }) => exportLine).join(', ')
const exportDeclaration = `const handlers = {${exportLines}};`
Expand Down
63 changes: 63 additions & 0 deletions test/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Buffer } from 'buffer'
import { env } from 'process'

import test from 'ava'
import fetch, { Headers } from 'node-fetch'

import { getBootstrapImport } from '../src/bootstrap.js'

const importURLRegex = /^import { boot } from "(.*)";$/m

test.serial('Imports the bootstrap layer from a valid URL', async (t) => {
const importLine = getBootstrapImport()
const match = importLine.match(importURLRegex)
const url = match?.[1]

if (url === undefined) {
t.fail('Import expression is invalid')

return
}

const importURL = new URL(url)
const headers = new Headers()

// `node-fetch` doesn't let us send credentials as part of the URL, so we
// have to transform them into an `Authorization` header.
if (importURL.username) {
const auth = Buffer.from(`${importURL.username}:${importURL.password}`)

importURL.username = ''
importURL.password = ''

headers.set('Authorization', `Basic ${auth.toString('base64')}`)
}

const canonicalURL = importURL.toString()
const { status } = await fetch(canonicalURL, { headers })

t.is(status, 200)
})

test.serial(
'Imports the bootstrap layer from the URL present in the `NETLIFY_EDGE_BOOTSTRAP` environment variable, if present',
(t) => {
const mockURL = 'https://example.com/boot.ts'

env.NETLIFY_EDGE_BOOTSTRAP = mockURL

const importLine = getBootstrapImport()
const match = importLine.match(importURLRegex)
const url = match?.[1]

env.NETLIFY_EDGE_BOOTSTRAP = undefined

if (url === undefined) {
t.fail('Import expression is invalid')

return
}

t.is(url, mockURL)
},
)

0 comments on commit 01f1285

Please sign in to comment.