@netlify/edge-bundler 14.6.0 → 14.7.1

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.
@@ -107,8 +107,11 @@ export async function loadESZIP(filename: string): Promise<ESZIP> {
107
107
  return await V1.load(bytes);
108
108
  }
109
109
 
110
- function url2path(url: string) {
111
- return join(...(new URL(url).pathname.split("/").filter(Boolean)));
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);
112
115
  }
113
116
 
114
117
  async function write(path: string, content: string) {
@@ -2,7 +2,7 @@ import { type WriteStream } from 'fs';
2
2
  import { type ExecaChildProcess } from 'execa';
3
3
  import { FeatureFlags } from './feature_flags.js';
4
4
  import { Logger } from './logger.js';
5
- export declare const DENO_VERSION_RANGE = "^2.4.2";
5
+ export declare const DENO_VERSION_RANGE = "1.39.0 - 2.2.4";
6
6
  export type OnBeforeDownloadHook = () => void | Promise<void>;
7
7
  export type OnAfterDownloadHook = (error?: Error) => void | Promise<void>;
8
8
  export interface DenoOptions {
@@ -40,7 +40,13 @@ export declare class DenoBridge {
40
40
  versionRange: string;
41
41
  constructor(options: DenoOptions);
42
42
  private downloadBinary;
43
- getBinaryVersion(binaryPath: string): Promise<string | undefined>;
43
+ getBinaryVersion(binaryPath: string): Promise<{
44
+ version: string;
45
+ error?: undefined;
46
+ } | {
47
+ version?: undefined;
48
+ error: Error;
49
+ }>;
44
50
  private getCachedBinary;
45
51
  private getGlobalBinary;
46
52
  private getRemoteBinary;
@@ -12,7 +12,8 @@ const DENO_VERSION_FILE = 'version.txt';
12
12
  // When updating DENO_VERSION_RANGE, ensure that the deno version
13
13
  // on the netlify/buildbot build image satisfies this range!
14
14
  // https://github.com/netlify/buildbot/blob/f9c03c9dcb091d6570e9d0778381560d469e78ad/build-image/noble/Dockerfile#L410
15
- export const DENO_VERSION_RANGE = '^2.4.2';
15
+ export const DENO_VERSION_RANGE = '1.39.0 - 2.2.4';
16
+ const NEXT_DENO_VERSION_RANGE = '^2.4.2';
16
17
  export class DenoBridge {
17
18
  cacheDirectory;
18
19
  currentDownload;
@@ -31,24 +32,35 @@ export class DenoBridge {
31
32
  this.onAfterDownload = options.onAfterDownload;
32
33
  this.onBeforeDownload = options.onBeforeDownload;
33
34
  this.useGlobal = options.useGlobal ?? true;
34
- this.versionRange = options.versionRange ?? DENO_VERSION_RANGE;
35
+ const useNextDeno = options.featureFlags?.edge_bundler_generate_tarball || options.featureFlags?.edge_bundler_deno_v2;
36
+ this.versionRange = options.versionRange ?? (useNextDeno ? NEXT_DENO_VERSION_RANGE : DENO_VERSION_RANGE);
35
37
  }
36
38
  async downloadBinary() {
37
39
  await this.onBeforeDownload?.();
38
40
  await this.ensureCacheDirectory();
39
41
  this.logger.system(`Downloading Deno CLI to ${this.cacheDirectory}`);
40
42
  const binaryPath = await download(this.cacheDirectory, this.versionRange, this.logger);
41
- const downloadedVersion = await this.getBinaryVersion(binaryPath);
42
- // We should never get here, because it means that `DENO_VERSION_RANGE` is
43
- // a malformed semver range. If this does happen, let's throw an error so
44
- // that the tests catch it.
45
- if (downloadedVersion === undefined) {
46
- 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.');
43
+ const result = await this.getBinaryVersion(binaryPath);
44
+ // If we can't execute the downloaded binary, provide actionable info to diagnose and self-heal if possible
45
+ if (result.error) {
46
+ const error = new Error(`Failed to set up Deno for Edge Functions.
47
+ Error: ${result.error.message}
48
+ Downloaded to: ${binaryPath}
49
+ Platform: ${process.platform}/${process.arch}
50
+
51
+ This may be caused by permissions, antivirus software, or platform incompatibility.
52
+
53
+ Try clearing the Deno cache directory and retrying:
54
+ ${this.cacheDirectory}
55
+
56
+ Supported Deno versions: ${this.versionRange}
57
+
58
+ To install Deno manually: https://ntl.fyi/install-deno`);
47
59
  await this.onAfterDownload?.(error);
48
60
  this.logger.system('Could not run downloaded Deno CLI', error);
49
61
  throw error;
50
62
  }
51
- await this.writeVersionFile(downloadedVersion);
63
+ await this.writeVersionFile(result.version);
52
64
  await this.onAfterDownload?.();
53
65
  return binaryPath;
54
66
  }
@@ -58,12 +70,13 @@ export class DenoBridge {
58
70
  const version = stdout.match(/^deno ([\d.]+)/);
59
71
  if (!version) {
60
72
  this.logger.system(`getBinaryVersion no version found. binaryPath ${binaryPath}`);
61
- return;
73
+ return { error: new Error('Could not parse Deno version from output') };
62
74
  }
63
- return version[1];
75
+ return { version: version[1] };
64
76
  }
65
77
  catch (error) {
66
78
  this.logger.system('getBinaryVersion failed', error);
79
+ return { error: error instanceof Error ? error : new Error(String(error)) };
67
80
  }
68
81
  }
69
82
  async getCachedBinary() {
@@ -88,9 +101,9 @@ export class DenoBridge {
88
101
  return;
89
102
  }
90
103
  const globalBinaryName = 'deno';
91
- const globalVersion = await this.getBinaryVersion(globalBinaryName);
92
- if (globalVersion === undefined || !semver.satisfies(globalVersion, this.versionRange)) {
93
- this.logger.system(`No globalVersion or semver not satisfied. globalVersion: ${globalVersion}, versionRange: ${this.versionRange}`);
104
+ const result = await this.getBinaryVersion(globalBinaryName);
105
+ if (result.error || !semver.satisfies(result.version, this.versionRange)) {
106
+ this.logger.system(`No globalVersion or semver not satisfied. globalVersion: ${result.version}, versionRange: ${this.versionRange}`);
94
107
  return;
95
108
  }
96
109
  return globalBinaryName;
@@ -101,3 +101,41 @@ test('Does inherit environment variables if `extendEnv` is not set', async () =>
101
101
  expect(environmentVariables).toEqual(['LULU=LALA', 'TADA=TUDU']);
102
102
  await rm(tmpDir.path, { force: true, recursive: true, maxRetries: 10 });
103
103
  });
104
+ test('Provides actionable error message when downloaded binary cannot be executed', async () => {
105
+ const tmpDir = await tmp.dir();
106
+ const latestVersion = semver.minVersion(DENO_VERSION_RANGE)?.version ?? '';
107
+ const data = new PassThrough();
108
+ const archive = archiver('zip', { zlib: { level: 9 } });
109
+ archive.pipe(data);
110
+ // Create a binary that will fail to execute (invalid content)
111
+ archive.append(Buffer.from('invalid binary content'), {
112
+ name: platform === 'win32' ? 'deno.exe' : 'deno',
113
+ });
114
+ archive.finalize();
115
+ const target = getPlatformTarget();
116
+ nock('https://dl.deno.land').get('/release-latest.txt').reply(200, `v${latestVersion}`);
117
+ nock('https://dl.deno.land')
118
+ .get(`/release/v${latestVersion}/deno-${target}.zip`)
119
+ .reply(200, () => data);
120
+ const deno = new DenoBridge({
121
+ cacheDirectory: tmpDir.path,
122
+ useGlobal: false,
123
+ });
124
+ try {
125
+ await deno.getBinaryPath();
126
+ expect.fail('Should have thrown an error');
127
+ }
128
+ catch (error) {
129
+ expect(error).toBeInstanceOf(Error);
130
+ const errorMessage = error.message;
131
+ expect(errorMessage).toContain('Failed to set up Deno for Edge Functions');
132
+ expect(errorMessage).toMatch(/Error:/);
133
+ expect(errorMessage).toMatch(/Downloaded to: .+deno(\.exe)?/);
134
+ expect(errorMessage).toContain(tmpDir.path);
135
+ expect(errorMessage).toMatch(/Platform: (darwin|linux|win32)\/(x64|arm64|ia32)/);
136
+ expect(errorMessage).toContain('This may be caused by permissions, antivirus software, or platform incompatibility');
137
+ expect(errorMessage).toContain('Try clearing the Deno cache directory and retrying');
138
+ expect(errorMessage).toContain('https://ntl.fyi/install-deno');
139
+ }
140
+ await rm(tmpDir.path, { force: true, recursive: true, maxRetries: 10 });
141
+ });
@@ -98,6 +98,7 @@ export const bundle = async (sourceDirectories, distDirectory, tomlDeclarations
98
98
  basePath,
99
99
  deno,
100
100
  eszipPath,
101
+ featureFlags,
101
102
  importMap,
102
103
  internalFunctions,
103
104
  log: logger,
@@ -127,7 +128,7 @@ export const bundle = async (sourceDirectories, distDirectory, tomlDeclarations
127
128
  }
128
129
  return { functions, manifest };
129
130
  };
130
- const getFunctionConfigs = async ({ basePath, deno, eszipPath, importMap, log, internalFunctions, userFunctions, }) => {
131
+ const getFunctionConfigs = async ({ basePath, deno, eszipPath, featureFlags, importMap, log, internalFunctions, userFunctions, }) => {
131
132
  try {
132
133
  const internalConfigPromises = internalFunctions.map(async (func) => [func.name, await getFunctionConfig({ functionPath: func.path, importMap, deno, log })]);
133
134
  const userConfigPromises = userFunctions.map(async (func) => [func.name, await getFunctionConfig({ functionPath: func.path, importMap, deno, log })]);
@@ -140,7 +141,7 @@ const getFunctionConfigs = async ({ basePath, deno, eszipPath, importMap, log, i
140
141
  };
141
142
  }
142
143
  catch (err) {
143
- if (!(err instanceof Error && err.cause === 'IMPORT_ASSERT') || !eszipPath) {
144
+ if (!(err instanceof Error && err.cause === 'IMPORT_ASSERT') || !eszipPath || !featureFlags?.edge_bundler_deno_v2) {
144
145
  throw err;
145
146
  }
146
147
  // We failed to extract the configuration because there is an import assert
@@ -505,6 +505,9 @@ test('Emits a system log when import assertions are used', async () => {
505
505
  const systemLogger = vi.fn();
506
506
  await bundle([sourceDirectory], distPath, [], {
507
507
  basePath,
508
+ featureFlags: {
509
+ edge_bundler_deno_v2: true,
510
+ },
508
511
  systemLogger,
509
512
  vendorDirectory: vendorDirectory.path,
510
513
  });
@@ -38,7 +38,8 @@ export const getFunctionConfig = async ({ deno, functionPath, importMap, log, })
38
38
  // with the extractor.
39
39
  const collector = await tmp.file();
40
40
  // Retrieving the version of Deno.
41
- const version = new SemVer((await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path)) || '');
41
+ const result = await deno.getBinaryVersion((await deno.getBinaryPath({ silent: true })).path);
42
+ const version = new SemVer(result.version || '');
42
43
  // The extractor will use its exit code to signal different error scenarios,
43
44
  // based on the list of exit codes we send as an argument. We then capture
44
45
  // the exit code to know exactly what happened and guide people accordingly.
@@ -1,10 +1,12 @@
1
1
  declare const defaultFlags: {
2
2
  edge_bundler_generate_tarball: boolean;
3
+ edge_bundler_deno_v2: boolean;
3
4
  };
4
5
  type FeatureFlag = keyof typeof defaultFlags;
5
6
  type FeatureFlags = Partial<Record<FeatureFlag, boolean>>;
6
7
  declare const getFlags: (input?: Record<string, boolean>, flags?: {
7
8
  edge_bundler_generate_tarball: boolean;
9
+ edge_bundler_deno_v2: boolean;
8
10
  }) => FeatureFlags;
9
11
  export { defaultFlags, getFlags };
10
12
  export type { FeatureFlag, FeatureFlags };
@@ -1,5 +1,6 @@
1
1
  const defaultFlags = {
2
2
  edge_bundler_generate_tarball: false,
3
+ edge_bundler_deno_v2: false,
3
4
  };
4
5
  const getFlags = (input = {}, flags = defaultFlags) => Object.entries(flags).reduce((result, [key, defaultValue]) => ({
5
6
  ...result,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "14.6.0",
3
+ "version": "14.7.1",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.js",
@@ -80,5 +80,5 @@
80
80
  "urlpattern-polyfill": "8.0.2",
81
81
  "uuid": "^11.0.0"
82
82
  },
83
- "gitHead": "3dfa7bd62ac0c8fe4b9b61128b8c2315d1272b4f"
83
+ "gitHead": "b08cf327fc8a631e3520c5f3326ace1d40e22c85"
84
84
  }