Skip to content

Commit 3f342bd

Browse files
fix: only target file: specifiers when extracting ESZIP (#6722)
1 parent b08cf32 commit 3f342bd

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

packages/edge-bundler/deno/vendor/deno.land/x/[email protected]/eszip.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class V2 {
8181
const imports: Record<string, string> = {};
8282

8383
for (const specifier of this.specifiers) {
84+
if (new URL(specifier).protocol !== "file:") continue
8485
const module = await this.parser.getModuleSource(specifier);
8586
await write(join(dest, "source", url2path(specifier)), module);
8687
// Track import
@@ -107,11 +108,8 @@ export async function loadESZIP(filename: string): Promise<ESZIP> {
107108
return await V1.load(bytes);
108109
}
109110

110-
function url2path(urlString: string) {
111-
const url = new URL(urlString);
112-
const tail = url.pathname.split("/").filter(Boolean);
113-
const relativePath = tail.length === 0 ? [".root"] : tail;
114-
return join(url.hostname, ...relativePath);
111+
function url2path(url: string) {
112+
return join(...(new URL(url).pathname.split("/").filter(Boolean)));
115113
}
116114

117115
async function write(path: string, content: string) {

packages/edge-bundler/node/bundler.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ 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+
import { BundleError } from './bundle_error.js'
2526

2627
export interface BundleOptions {
2728
basePath?: string
@@ -249,26 +250,35 @@ const getFunctionConfigs = async ({
249250
throw err
250251
}
251252

252-
// We failed to extract the configuration because there is an import assert
253-
// in the function code, a deprecated feature that we used to support with
254-
// Deno 1.x. To avoid a breaking change, we treat this error as a special
255-
// case, using the generated ESZIP to extract the configuration. This works
256-
// because import asserts are transpiled to import attributes.
257-
const extractedESZIP = await extractESZIP(deno, eszipPath)
258-
const configs = await Promise.all(
259-
[...internalFunctions, ...userFunctions].map(async (func) => {
260-
const relativePath = relative(basePath, func.path)
261-
const functionPath = join(extractedESZIP.path, relativePath)
262-
263-
return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const
264-
}),
265-
)
253+
try {
254+
// We failed to extract the configuration because there is an import assert
255+
// in the function code, a deprecated feature that we used to support with
256+
// Deno 1.x. To avoid a breaking change, we treat this error as a special
257+
// case, using the generated ESZIP to extract the configuration. This works
258+
// because import asserts are transpiled to import attributes.
259+
const extractedESZIP = await extractESZIP(deno, eszipPath)
260+
const configs = await Promise.all(
261+
[...internalFunctions, ...userFunctions].map(async (func) => {
262+
const relativePath = relative(basePath, func.path)
263+
const functionPath = join(extractedESZIP.path, relativePath)
266264

267-
await extractedESZIP.cleanup()
265+
return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const
266+
}),
267+
)
268268

269-
return {
270-
internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)),
271-
userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)),
269+
await extractedESZIP.cleanup()
270+
271+
return {
272+
internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)),
273+
userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)),
274+
}
275+
} catch (err) {
276+
throw new BundleError(
277+
new Error(
278+
'An error occurred while building an edge function that uses an import assertion. Refer to https://ntl.fyi/import-assert for more information.',
279+
),
280+
{ cause: err },
281+
)
272282
}
273283
}
274284
}

0 commit comments

Comments
 (0)