Skip to content

Commit

Permalink
feat: load edge functions bootstrap from module (#6496)
Browse files Browse the repository at this point in the history
* feat: load edge functions bootstrap from module

* chore: add test

* refactor: update error logging

* chore: update `@netlify/edge-functions`
  • Loading branch information
eduardoboucas committed Apr 8, 2024
1 parent a6f0f45 commit bb2279f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@netlify/build-info": "7.13.2",
"@netlify/config": "20.12.1",
"@netlify/edge-bundler": "11.3.0",
"@netlify/edge-functions": "2.5.1",
"@netlify/local-functions-proxy": "1.1.1",
"@netlify/zip-it-and-ship-it": "9.31.1",
"@octokit/rest": "19.0.13",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ const bundleEdgeFunctions = async (options, command: BaseCommand) => {
packagePath: command.workspacePackage,
buffer: true,
featureFlags: edgeFunctionsFeatureFlags,
edgeFunctionsBootstrapURL: getBootstrapURL(),
edgeFunctionsBootstrapURL: await getBootstrapURL(),
})

if (!success) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { featureFlags as edgeFunctionsFeatureFlags } from './edge-functions/cons
* @param {*} config.deployHandler
* @returns {BuildConfig}
*/
export const getBuildOptions = ({
export const getBuildOptions = async ({
// @ts-expect-error TS(7031) FIXME: Binding element 'cachedConfig' implicitly has an '... Remove this comment to see the full error message
cachedConfig,
// @ts-expect-error TS(7031) FIXME: Binding element 'currentDir' implicitly has an 'an... Remove this comment to see the full error message
Expand Down Expand Up @@ -89,7 +89,7 @@ export const getBuildOptions = ({
functionsBundlingManifest: true,
},
eventHandlers,
edgeFunctionsBootstrapURL: getBootstrapURL(),
edgeFunctionsBootstrapURL: await getBootstrapURL(),
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/lib/edge-functions/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { env } from 'process'

const latestBootstrapURL = 'https://65f9b38dc160de0008d35515--edge.netlify.com/bootstrap/index-combined.ts'
import { getURL } from '@netlify/edge-functions/version'

export const getBootstrapURL = () => env.NETLIFY_EDGE_BOOTSTRAP || latestBootstrapURL
import { warn } from '../../utils/command-helpers.js'

export const FALLBACK_BOOTSTRAP_URL = 'https://edge.netlify.com/bootstrap/index-combined.ts'

export const getBootstrapURL = async () => {
if (env.NETLIFY_EDGE_BOOTSTRAP) {
return env.NETLIFY_EDGE_BOOTSTRAP
}

try {
return await getURL()
} catch (error) {
warn(`Could not load latest version of Edge Functions environment: ${(error as NodeJS.ErrnoException)?.message}`)

// If there was an error getting the bootstrap URL from the module, let's
// use the latest version of the bootstrap. This is not ideal, but better
// than failing to serve requests with edge functions.
return FALLBACK_BOOTSTRAP_URL
}
}
2 changes: 1 addition & 1 deletion src/lib/edge-functions/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const prepareServer = async ({
const runIsolate = await bundler.serve({
...getDownloadUpdateFunctions(),
basePath: projectDir,
bootstrapURL: getBootstrapURL(),
bootstrapURL: await getBootstrapURL(),
debug,
distImportMapPath: join(projectDir, distImportMapPath),
featureFlags,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/run-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const runNetlifyBuild = async ({
cwd: cachedConfig.buildDir,
quiet: options.quiet,
saveConfig: options.saveConfig,
edgeFunctionsBootstrapURL: getBootstrapURL(),
edgeFunctionsBootstrapURL: await getBootstrapURL(),
}

const devCommand = async (settingsOverrides = {}) => {
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/lib/edge-functions/bootstrap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { env } from 'process'

import { describe, expect, test } from 'vitest'

import { getBootstrapURL, FALLBACK_BOOTSTRAP_URL } from '../../../../dist/lib/edge-functions/bootstrap.js'

describe('`getBootstrapURL()`', () => {
test('Returns the URL in the `NETLIFY_EDGE_BOOTSTRAP` URL, if set', async () => {
const mockBootstrapURL = 'https://edge.netlify/bootstrap.ts'

env.NETLIFY_EDGE_BOOTSTRAP = mockBootstrapURL

const bootstrapURL = await getBootstrapURL()

delete env.NETLIFY_EDGE_BOOTSTRAP

expect(bootstrapURL).toEqual(mockBootstrapURL)
})

test('Returns a publicly accessible URL', async () => {
const bootstrapURL = await getBootstrapURL()

// We shouldn't get the fallback URL, because that means we couldn't get
// the URL from the `@netlify/edge-functions` module.
expect(bootstrapURL).not.toBe(FALLBACK_BOOTSTRAP_URL)

const res = await fetch(bootstrapURL)

expect(res.status).toBe(200)
expect(res.headers.get('content-type').startsWith('application/typescript')).toBe(true)
})
})

2 comments on commit bb2279f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,313
  • Package size: 294 MB
  • Number of ts-expect-error directives: 1,008

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,313
  • Package size: 294 MB
  • Number of ts-expect-error directives: 1,008

Please sign in to comment.