-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load bootstrap from deploy URL (#7)
* feat: load bootstrap from deploy URL * feat: support loading boot URL from environment variable
- Loading branch information
1 parent
17972fb
commit 01f1285
Showing
6 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}";` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
) |