@netlify/edge-bundler 1.13.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/deno/bundle.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { writeStage2 } from 'https://62f5f45fbc76ed0009624267--edge.netlify.com/bundler/mod.ts'
1
+ import { writeStage2 } from './lib/stage2.ts'
2
2
 
3
3
  const [payload] = Deno.args
4
4
  const { basePath, destPath, functions, imports } = JSON.parse(payload)
@@ -0,0 +1,47 @@
1
+ import { load } from "https://deno.land/x/eszip@v0.18.0/loader.ts";
2
+ import { LoadResponse } from "https://deno.land/x/eszip@v0.18.0/mod.ts";
3
+ import * as path from "https://deno.land/std@0.127.0/path/mod.ts";
4
+ import { retryAsync } from "https://deno.land/x/retry@v2.0.0/mod.ts";
5
+
6
+ const inlineModule = (
7
+ specifier: string,
8
+ content: string,
9
+ ): LoadResponse => {
10
+ return {
11
+ content,
12
+ headers: {
13
+ "content-type": "application/typescript",
14
+ },
15
+ kind: "module",
16
+ specifier,
17
+ };
18
+ };
19
+
20
+ const loadFromVirtualRoot = async (
21
+ specifier: string,
22
+ virtualRoot: string,
23
+ basePath: string,
24
+ ) => {
25
+ const basePathURL = path.toFileUrl(basePath).toString();
26
+ const filePath = specifier.replace(virtualRoot.slice(0, -1), basePathURL);
27
+ const file = await load(filePath);
28
+
29
+ if (file === undefined) {
30
+ throw new Error(`Could not find file: ${filePath}`);
31
+ }
32
+
33
+ return { ...file, specifier };
34
+ };
35
+
36
+ const loadWithRetry = (specifier: string, delay = 1000, maxTry = 3) => {
37
+ if (!specifier.startsWith("https://")) {
38
+ return load(specifier);
39
+ }
40
+
41
+ return retryAsync(() => load(specifier), {
42
+ delay,
43
+ maxTry,
44
+ });
45
+ };
46
+
47
+ export { inlineModule, loadFromVirtualRoot, loadWithRetry };
@@ -0,0 +1,4 @@
1
+ export const PUBLIC_SPECIFIER = "netlify:edge";
2
+ export const STAGE1_SPECIFIER = "netlify:bootstrap-stage1";
3
+ export const STAGE2_SPECIFIER = "netlify:bootstrap-stage2";
4
+ export const virtualRoot = "file:///root/";
@@ -0,0 +1,81 @@
1
+ import { build, LoadResponse } from 'https://deno.land/x/eszip@v0.18.0/mod.ts'
2
+
3
+ import * as path from 'https://deno.land/std@0.127.0/path/mod.ts'
4
+
5
+ import { PUBLIC_SPECIFIER, STAGE2_SPECIFIER, virtualRoot } from './consts.ts'
6
+ import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts'
7
+
8
+ interface InputFunction {
9
+ name: string
10
+ path: string
11
+ }
12
+
13
+ interface WriteStage2Options {
14
+ basePath: string
15
+ destPath: string
16
+ functions: InputFunction[]
17
+ imports?: Record<string, string>
18
+ }
19
+
20
+ const getFunctionReference = (basePath: string, func: InputFunction, index: number) => {
21
+ const importName = `func${index}`
22
+ const exportLine = `"${func.name}": ${importName}`
23
+ const url = getVirtualPath(basePath, func.path)
24
+
25
+ return {
26
+ exportLine,
27
+ importLine: `import ${importName} from "${url}";`,
28
+ }
29
+ }
30
+
31
+ const getStage2Entry = (basePath: string, functions: InputFunction[]) => {
32
+ const lines = functions.map((func, index) => getFunctionReference(basePath, func, index))
33
+ const importLines = lines.map(({ importLine }) => importLine).join('\n')
34
+ const exportLines = lines.map(({ exportLine }) => exportLine).join(', ')
35
+ const exportDeclaration = `export const functions = {${exportLines}};`
36
+
37
+ return [importLines, exportDeclaration].join('\n\n')
38
+ }
39
+
40
+ const getVirtualPath = (basePath: string, filePath: string) => {
41
+ const relativePath = path.relative(basePath, filePath)
42
+ const url = new URL(relativePath, virtualRoot)
43
+
44
+ return url
45
+ }
46
+
47
+ const stage2Loader = (basePath: string, functions: InputFunction[], imports: Record<string, string> = {}) => {
48
+ return async (specifier: string): Promise<LoadResponse | undefined> => {
49
+ if (specifier === STAGE2_SPECIFIER) {
50
+ const stage2Entry = getStage2Entry(basePath, functions)
51
+
52
+ return inlineModule(specifier, stage2Entry)
53
+ }
54
+
55
+ if (specifier === PUBLIC_SPECIFIER) {
56
+ return {
57
+ kind: 'external',
58
+ specifier,
59
+ }
60
+ }
61
+
62
+ if (imports[specifier] !== undefined) {
63
+ return await loadWithRetry(imports[specifier])
64
+ }
65
+
66
+ if (specifier.startsWith(virtualRoot)) {
67
+ return loadFromVirtualRoot(specifier, virtualRoot, basePath)
68
+ }
69
+
70
+ return await loadWithRetry(specifier)
71
+ }
72
+ }
73
+
74
+ const writeStage2 = async ({ basePath, destPath, functions, imports }: WriteStage2Options) => {
75
+ const loader = stage2Loader(basePath, functions, imports)
76
+ const bytes = await build([STAGE2_SPECIFIER], loader)
77
+
78
+ return await Deno.writeFile(destPath, bytes)
79
+ }
80
+
81
+ export { writeStage2 }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",