Skip to content

Commit dbcea87

Browse files
committed
fix: use stable edge functions bootstrap package for globalThis.Netlify
1 parent 2ea5a35 commit dbcea87

File tree

8 files changed

+653
-91
lines changed

8 files changed

+653
-91
lines changed

deno.lock

Lines changed: 536 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 80 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/edge-bundler/deno/config.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
// this needs to be updated whenever there's a change to globalThis.Netlify in bootstrap
2-
import { Netlify } from "https://64e8753eae24930008fac6d9--edge.netlify.app/bootstrap/index-combined.ts"
3-
4-
const [functionURL, collectorURL, rawExitCodes] = Deno.args
1+
const [functionURL, collectorURL, bootstrapURL, rawExitCodes] = Deno.args
52
const exitCodes = JSON.parse(rawExitCodes)
63

7-
globalThis.Netlify = Netlify
8-
94
let func
105

116
try {
7+
const {Netlify} = await import(new URL("/bootstrap/globals/implementation.ts", bootstrapURL).toString())
8+
globalThis.Netlify = Netlify
129
func = await import(functionURL)
1310
} catch (error) {
1411
console.error(error)

packages/edge-bundler/node/bundler.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import { writeManifest } from './manifest.js'
2222
import { vendorNPMSpecifiers } from './npm_dependencies.js'
2323
import { ensureLatestTypes } from './types.js'
2424
import { nonNullable } from './utils/non_nullable.js'
25+
// @ts-expect-error TypeScript is complaining about the values for the `module`
26+
// and `moduleResolution` configuration properties, but changing those to more
27+
// modern values causes other packages to fail. Leaving this for now, but we
28+
// should have a proper fix for this.
29+
import { getURL as getBootstrapURL } from '@netlify/edge-functions-bootstrap/version'
2530

2631
export interface BundleOptions {
2732
basePath?: string
@@ -60,8 +65,12 @@ export const bundle = async (
6065
userLogger,
6166
systemLogger,
6267
vendorDirectory,
68+
bootstrapURL,
6369
}: BundleOptions = {},
6470
) => {
71+
if (!bootstrapURL) {
72+
bootstrapURL = await getBootstrapURL()
73+
}
6574
const logger = getLogger(systemLogger, userLogger, debug)
6675
const featureFlags = getFlags(inputFeatureFlags)
6776
const options: DenoOptions = {
@@ -161,10 +170,10 @@ export const bundle = async (
161170
// Retrieving a configuration object for each function.
162171
// Run `getFunctionConfig` in parallel as it is a non-trivial operation and spins up deno
163172
const internalConfigPromises = internalFunctions.map(
164-
async (func) => [func.name, await getFunctionConfig({ func, importMap, deno, log: logger })] as const,
173+
async (func) => [func.name, await getFunctionConfig({ func, importMap, deno, log: logger, bootstrapURL })] as const,
165174
)
166175
const userConfigPromises = userFunctions.map(
167-
async (func) => [func.name, await getFunctionConfig({ func, importMap, deno, log: logger })] as const,
176+
async (func) => [func.name, await getFunctionConfig({ func, importMap, deno, log: logger, bootstrapURL })] as const,
168177
)
169178

170179
// Creating a hash of function names to configuration objects.

packages/edge-bundler/node/config.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import { FunctionConfig, getFunctionConfig } from './config.js'
1414
import type { Declaration } from './declaration.js'
1515
import { ImportMap } from './import_map.js'
1616
import { RateLimitAction, RateLimitAggregator } from './rate_limit.js'
17+
// @ts-expect-error TypeScript is complaining about the values for the `module`
18+
// and `moduleResolution` configuration properties, but changing those to more
19+
// modern values causes other packages to fail. Leaving this for now, but we
20+
// should have a proper fix for this.
21+
import { getURL as getBootstrapURL } from '@netlify/edge-functions-bootstrap/version'
1722

1823
const importMapFile = {
1924
baseURL: new URL('file:///some/path/import-map.json'),
@@ -193,6 +198,8 @@ describe('`getFunctionConfig` extracts configuration properties from function fi
193198

194199
await fs.writeFile(path, func.source)
195200

201+
const bootstrapURL = await getBootstrapURL()
202+
196203
const funcCall = () =>
197204
getFunctionConfig({
198205
func: {
@@ -202,6 +209,7 @@ describe('`getFunctionConfig` extracts configuration properties from function fi
202209
importMap: new ImportMap([importMapFile]),
203210
deno,
204211
log: logger,
212+
bootstrapURL,
205213
})
206214

207215
if (func.error) {
@@ -393,6 +401,7 @@ test('Passes validation if default export exists and is a function', async () =>
393401
importMap: new ImportMap([importMapFile]),
394402
deno,
395403
log: logger,
404+
bootstrapURL: await getBootstrapURL(),
396405
}),
397406
).resolves.not.toThrow()
398407

@@ -429,6 +438,7 @@ test('Fails validation if default export is not function', async () => {
429438
importMap: new ImportMap([importMapFile]),
430439
deno,
431440
log: logger,
441+
bootstrapURL: await getBootstrapURL(),
432442
})
433443

434444
await expect(config).rejects.toThrowError(invalidDefaultExportErr(path))
@@ -465,6 +475,7 @@ test('Fails validation if default export is not present', async () => {
465475
importMap: new ImportMap([importMapFile]),
466476
deno,
467477
log: logger,
478+
bootstrapURL: await getBootstrapURL(),
468479
})
469480

470481
await expect(config).rejects.toThrowError(invalidDefaultExportErr(path))

packages/edge-bundler/node/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import { ImportMap } from './import_map.js'
1212
import { Logger } from './logger.js'
1313
import { getPackagePath } from './package_json.js'
1414
import { RateLimit } from './rate_limit.js'
15+
// @ts-expect-error TypeScript is complaining about the values for the `module`
16+
// and `moduleResolution` configuration properties, but changing those to more
17+
// modern values causes other packages to fail. Leaving this for now, but we
18+
// should have a proper fix for this.
19+
import { getURL as getBootstrapURL } from '@netlify/edge-functions-bootstrap/version'
1520

1621
enum ConfigExitCode {
1722
Success = 0,
@@ -78,13 +83,17 @@ export const getFunctionConfig = async ({
7883
func,
7984
importMap,
8085
deno,
86+
bootstrapURL,
8187
log,
8288
}: {
8389
func: EdgeFunction
8490
importMap: ImportMap
8591
deno: DenoBridge
92+
bootstrapURL?: string
8693
log: Logger
8794
}) => {
95+
const concreteBootstrapURL = bootstrapURL || (await getBootstrapURL())
96+
8897
// The extractor is a Deno script that will import the function and run its
8998
// `config` export, if one exists.
9099
const extractorPath = getConfigExtractor()
@@ -117,6 +126,7 @@ export const getFunctionConfig = async ({
117126
extractorPath,
118127
pathToFileURL(func.path).href,
119128
pathToFileURL(collector.path).href,
129+
concreteBootstrapURL,
120130
JSON.stringify(ConfigExitCode),
121131
].filter(Boolean),
122132
{ rejectOnExitCode: false },

packages/edge-bundler/node/server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const prepareServer = ({
148148

149149
if (options.getFunctionsConfig) {
150150
functionsConfig = await Promise.all(
151-
functions.map((func) => getFunctionConfig({ func, importMap, deno, log: logger })),
151+
functions.map((func) => getFunctionConfig({ func, importMap, deno, bootstrapURL, log: logger })),
152152
)
153153
}
154154

packages/edge-bundler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"test": "test/node"
4343
},
4444
"devDependencies": {
45-
"@netlify/edge-functions-bootstrap": "^2.14.0",
4645
"@types/node": "^18.19.111",
4746
"@types/semver": "^7.3.9",
4847
"@types/uuid": "^10.0.0",
@@ -59,6 +58,7 @@
5958
"node": ">=18.14.0"
6059
},
6160
"dependencies": {
61+
"@netlify/edge-functions-bootstrap": "^2.14.0",
6262
"@import-maps/resolve": "^2.0.0",
6363
"ajv": "^8.11.2",
6464
"ajv-errors": "^3.0.0",

0 commit comments

Comments
 (0)