Skip to content

Commit a8dd7b5

Browse files
committed
fix: extract ESZIP correctly
1 parent b08cf32 commit a8dd7b5

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
// CLI utility to build/list/extract/run ESZIPs
22

33
import { build, Parser } from "./mod.ts";
4-
import { dirname, join } from "https://deno.land/[email protected]/path/mod.ts";
4+
import { dirname, extname, join } from "https://deno.land/[email protected]/path/mod.ts";
5+
6+
const extensions = new Set([
7+
".ts",
8+
".tsx",
9+
".js",
10+
".jsx",
11+
".mjs",
12+
".mts",
13+
".json",
14+
".wasm"
15+
]);
516

617
function hasV2Header(bytes: Uint8Array) {
718
const magicV2 = new TextDecoder().decode(bytes.slice(0, 8));
@@ -109,9 +120,13 @@ export async function loadESZIP(filename: string): Promise<ESZIP> {
109120

110121
function url2path(urlString: string) {
111122
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);
123+
let path = join(url.hostname, ...url.pathname.split("/").filter(Boolean));
124+
125+
if (!extensions.has(extname(path))) {
126+
path = join(path, ".index.ts");
127+
}
128+
129+
return path;
115130
}
116131

117132
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)