@netlify/edge-bundler 1.12.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://62ea8d05a6858300091547ed--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/dist/bridge.js CHANGED
@@ -2,6 +2,7 @@ import { promises as fs } from 'fs';
2
2
  import path from 'path';
3
3
  import process from 'process';
4
4
  import { execa } from 'execa';
5
+ import pathKey from 'path-key';
5
6
  import semver from 'semver';
6
7
  import { download } from './downloader.js';
7
8
  import { getPathInHome } from './home_path.js';
@@ -26,7 +27,7 @@ class DenoBridge {
26
27
  await ((_a = this.onBeforeDownload) === null || _a === void 0 ? void 0 : _a.call(this));
27
28
  await this.ensureCacheDirectory();
28
29
  this.logger.system(`Downloading Deno CLI to ${this.cacheDirectory}`);
29
- const binaryPath = await download(this.cacheDirectory, this.versionRange);
30
+ const binaryPath = await download(this.cacheDirectory, this.versionRange, this.logger);
30
31
  const downloadedVersion = await this.getBinaryVersion(binaryPath);
31
32
  // We should never get here, because it means that `DENO_VERSION_RANGE` is
32
33
  // a malformed semver range. If this does happen, let's throw an error so
@@ -34,6 +35,7 @@ class DenoBridge {
34
35
  if (downloadedVersion === undefined) {
35
36
  const error = new Error('There was a problem setting up the Edge Functions environment. To try a manual installation, visit https://ntl.fyi/install-deno.');
36
37
  await ((_b = this.onAfterDownload) === null || _b === void 0 ? void 0 : _b.call(this, error));
38
+ this.logger.system('Could not run downloaded Deno CLI', error);
37
39
  throw error;
38
40
  }
39
41
  await this.writeVersionFile(downloadedVersion);
@@ -45,13 +47,12 @@ class DenoBridge {
45
47
  const { stdout } = await execa(binaryPath, ['--version']);
46
48
  const version = stdout.match(/^deno ([\d.]+)/);
47
49
  if (!version) {
50
+ this.logger.system(`getBinaryVersion no version found. binaryPath ${binaryPath}`);
48
51
  return;
49
52
  }
50
53
  return version[1];
51
54
  }
52
- catch (error) {
53
- this.logger.system('Error checking Deno binary version', error);
54
- }
55
+ catch { }
55
56
  }
56
57
  async getCachedBinary() {
57
58
  const versionFilePath = path.join(this.cacheDirectory, DENO_VERSION_FILE);
@@ -59,10 +60,12 @@ class DenoBridge {
59
60
  try {
60
61
  cachedVersion = await fs.readFile(versionFilePath, 'utf8');
61
62
  }
62
- catch {
63
+ catch (error) {
64
+ this.logger.system('Error getting cached binary', error);
63
65
  return;
64
66
  }
65
67
  if (!semver.satisfies(cachedVersion, this.versionRange)) {
68
+ this.logger.system(`semver not satisfied. cachedVersion: ${cachedVersion}, versionRange: ${this.versionRange}`);
66
69
  return;
67
70
  }
68
71
  const binaryName = `deno${getBinaryExtension()}`;
@@ -75,6 +78,7 @@ class DenoBridge {
75
78
  const globalBinaryName = 'deno';
76
79
  const globalVersion = await this.getBinaryVersion(globalBinaryName);
77
80
  if (globalVersion === undefined || !semver.satisfies(globalVersion, this.versionRange)) {
81
+ this.logger.system(`No globalVersion or semver not satisfied. globalVersion: ${globalVersion}, versionRange: ${this.versionRange}`);
78
82
  return;
79
83
  }
80
84
  return globalBinaryName;
@@ -121,6 +125,8 @@ class DenoBridge {
121
125
  if (this.denoDir !== undefined) {
122
126
  env.DENO_DIR = this.denoDir;
123
127
  }
128
+ // Ensure PATH is always set as otherwise we are not able to find the global deno binary
129
+ env[pathKey()] = inputEnv[pathKey({ env: inputEnv })] || process.env[pathKey()];
124
130
  return env;
125
131
  }
126
132
  // Runs the Deno CLI in the background and returns a reference to the child
@@ -1,2 +1,3 @@
1
- declare const download: (targetDirectory: string, versionRange: string) => Promise<string>;
1
+ import { Logger } from './logger.js';
2
+ declare const download: (targetDirectory: string, versionRange: string, logger: Logger) => Promise<string>;
2
3
  export { download };
@@ -2,11 +2,12 @@ import fs from 'fs';
2
2
  import path from 'path';
3
3
  import fetch from 'node-fetch';
4
4
  import StreamZip from 'node-stream-zip';
5
+ import pRetry from 'p-retry';
5
6
  import semver from 'semver';
6
7
  import { getBinaryExtension, getPlatformTarget } from './platform.js';
7
- const download = async (targetDirectory, versionRange) => {
8
+ const download = async (targetDirectory, versionRange, logger) => {
8
9
  const zipPath = path.join(targetDirectory, 'deno-cli-latest.zip');
9
- const data = await downloadVersion(versionRange);
10
+ const data = await downloadVersionWithRetry(versionRange, logger);
10
11
  const binaryName = `deno${getBinaryExtension()}`;
11
12
  const binaryPath = path.join(targetDirectory, binaryName);
12
13
  const file = fs.createWriteStream(zipPath);
@@ -33,6 +34,12 @@ const downloadVersion = async (versionRange) => {
33
34
  }
34
35
  return res.body;
35
36
  };
37
+ const downloadVersionWithRetry = async (versionRange, logger) => await pRetry(async () => await downloadVersion(versionRange), {
38
+ retries: 3,
39
+ onFailedAttempt: (error) => {
40
+ logger.system('Deno CLI download retry attempt error', error);
41
+ },
42
+ });
36
43
  const extractBinaryFromZip = async (zipPath, binaryPath, binaryName) => {
37
44
  const { async: StreamZipAsync } = StreamZip;
38
45
  const zip = new StreamZipAsync({ file: zipPath });
@@ -5,7 +5,7 @@ import { pathToFileURL } from 'url';
5
5
  import del from 'del';
6
6
  import { wrapBundleError } from '../bundle_error.js';
7
7
  import { getFileHash } from '../utils/sha256.js';
8
- const BOOTSTRAP_LATEST = 'https://62ea8d05a6858300091547ed--edge.netlify.com/bootstrap/index-combined.ts';
8
+ const BOOTSTRAP_LATEST = 'https://62f5f45fbc76ed0009624267--edge.netlify.com/bootstrap/index-combined.ts';
9
9
  const bundleJS = async ({ buildID, debug, deno, distDirectory, functions, importMap, }) => {
10
10
  const stage2Path = await generateStage2({ distDirectory, functions, fileName: `${buildID}-pre.js` });
11
11
  const extension = '.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "1.12.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",
@@ -86,7 +86,9 @@
86
86
  "glob-to-regexp": "^0.4.1",
87
87
  "node-fetch": "^3.1.1",
88
88
  "node-stream-zip": "^1.15.0",
89
+ "p-retry": "^5.1.1",
89
90
  "p-wait-for": "^4.1.0",
91
+ "path-key": "^4.0.0",
90
92
  "semver": "^7.3.5",
91
93
  "tmp-promise": "^3.0.3",
92
94
  "uuid": "^8.3.2"