diff --git a/packages/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip.ts b/packages/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip.ts index 3ed2bb0286..23a1935fcb 100644 --- a/packages/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip.ts +++ b/packages/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip.ts @@ -81,6 +81,7 @@ class V2 { const imports: Record = {}; for (const specifier of this.specifiers) { + if (new URL(specifier).protocol !== "file:") continue const module = await this.parser.getModuleSource(specifier); await write(join(dest, "source", url2path(specifier)), module); // Track import @@ -107,11 +108,8 @@ export async function loadESZIP(filename: string): Promise { return await V1.load(bytes); } -function url2path(urlString: string) { - const url = new URL(urlString); - const tail = url.pathname.split("/").filter(Boolean); - const relativePath = tail.length === 0 ? [".root"] : tail; - return join(url.hostname, ...relativePath); +function url2path(url: string) { + return join(...(new URL(url).pathname.split("/").filter(Boolean))); } async function write(path: string, content: string) { diff --git a/packages/edge-bundler/node/bundler.ts b/packages/edge-bundler/node/bundler.ts index d9fbf5eea3..52ce09334a 100644 --- a/packages/edge-bundler/node/bundler.ts +++ b/packages/edge-bundler/node/bundler.ts @@ -22,6 +22,7 @@ import { writeManifest } from './manifest.js' import { vendorNPMSpecifiers } from './npm_dependencies.js' import { ensureLatestTypes } from './types.js' import { nonNullable } from './utils/non_nullable.js' +import { BundleError } from './bundle_error.js' export interface BundleOptions { basePath?: string @@ -249,26 +250,35 @@ const getFunctionConfigs = async ({ throw err } - // We failed to extract the configuration because there is an import assert - // in the function code, a deprecated feature that we used to support with - // Deno 1.x. To avoid a breaking change, we treat this error as a special - // case, using the generated ESZIP to extract the configuration. This works - // because import asserts are transpiled to import attributes. - const extractedESZIP = await extractESZIP(deno, eszipPath) - const configs = await Promise.all( - [...internalFunctions, ...userFunctions].map(async (func) => { - const relativePath = relative(basePath, func.path) - const functionPath = join(extractedESZIP.path, relativePath) - - return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const - }), - ) + try { + // We failed to extract the configuration because there is an import assert + // in the function code, a deprecated feature that we used to support with + // Deno 1.x. To avoid a breaking change, we treat this error as a special + // case, using the generated ESZIP to extract the configuration. This works + // because import asserts are transpiled to import attributes. + const extractedESZIP = await extractESZIP(deno, eszipPath) + const configs = await Promise.all( + [...internalFunctions, ...userFunctions].map(async (func) => { + const relativePath = relative(basePath, func.path) + const functionPath = join(extractedESZIP.path, relativePath) - await extractedESZIP.cleanup() + return [func.name, await getFunctionConfig({ functionPath, importMap, deno, log })] as const + }), + ) - return { - internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)), - userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)), + await extractedESZIP.cleanup() + + return { + internalFunctions: Object.fromEntries(configs.slice(0, internalFunctions.length)), + userFunctions: Object.fromEntries(configs.slice(internalFunctions.length)), + } + } catch (err) { + throw new BundleError( + new Error( + 'An error occurred while building an edge function that uses an import assertion. Refer to https://ntl.fyi/import-assert for more information.', + ), + { cause: err }, + ) } } }