@netlify/edge-bundler 1.4.2 → 1.6.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,6 +1,6 @@
1
- import { writeStage2 } from 'https://62bae4994570970008142f1e--edge-bootstrap.netlify.app/bundler/mod.ts'
1
+ import { writeStage2 } from 'https://62d144a15553b50009af7ac6--edge.netlify.com/bundler/mod.ts'
2
2
 
3
3
  const [payload] = Deno.args
4
- const { basePath, destPath, functions } = JSON.parse(payload)
4
+ const { basePath, destPath, functions, imports } = JSON.parse(payload)
5
5
 
6
- await writeStage2({ basePath, functions, destPath })
6
+ await writeStage2({ basePath, destPath, functions, imports })
package/dist/bridge.d.ts CHANGED
@@ -29,13 +29,14 @@ declare class DenoBridge {
29
29
  private getCachedBinary;
30
30
  private getGlobalBinary;
31
31
  private getRemoteBinary;
32
- private log;
33
32
  private static runWithBinary;
34
33
  private writeVersionFile;
34
+ ensureCacheDirectory(): Promise<void>;
35
35
  getBinaryPath(): Promise<{
36
36
  global: boolean;
37
37
  path: string;
38
38
  }>;
39
+ log(...data: unknown[]): void;
39
40
  run(args: string[], { pipeOutput }?: RunOptions): Promise<import("execa").ExecaReturnValue<string>>;
40
41
  runInBackground(args: string[], pipeOutput?: boolean, ref?: ProcessRef): Promise<void>;
41
42
  }
package/dist/bridge.js CHANGED
@@ -21,7 +21,7 @@ class DenoBridge {
21
21
  async downloadBinary() {
22
22
  var _a, _b, _c;
23
23
  await ((_a = this.onBeforeDownload) === null || _a === void 0 ? void 0 : _a.call(this));
24
- await fs.mkdir(this.cacheDirectory, { recursive: true });
24
+ await this.ensureCacheDirectory();
25
25
  this.log(`Downloading Deno CLI to ${this.cacheDirectory}...`);
26
26
  const binaryPath = await download(this.cacheDirectory, this.versionRange);
27
27
  const downloadedVersion = await DenoBridge.getBinaryVersion(binaryPath);
@@ -82,12 +82,6 @@ class DenoBridge {
82
82
  }
83
83
  return this.currentDownload;
84
84
  }
85
- log(...data) {
86
- if (!this.debug) {
87
- return;
88
- }
89
- console.log(...data);
90
- }
91
85
  static runWithBinary(binaryPath, args, pipeOutput) {
92
86
  var _a, _b;
93
87
  const runDeno = execa(binaryPath, args);
@@ -98,9 +92,13 @@ class DenoBridge {
98
92
  return runDeno;
99
93
  }
100
94
  async writeVersionFile(version) {
95
+ await this.ensureCacheDirectory();
101
96
  const versionFilePath = path.join(this.cacheDirectory, DENO_VERSION_FILE);
102
97
  await fs.writeFile(versionFilePath, version);
103
98
  }
99
+ async ensureCacheDirectory() {
100
+ await fs.mkdir(this.cacheDirectory, { recursive: true });
101
+ }
104
102
  async getBinaryPath() {
105
103
  const globalPath = await this.getGlobalBinary();
106
104
  if (globalPath !== undefined) {
@@ -115,6 +113,12 @@ class DenoBridge {
115
113
  const downloadedPath = await this.getRemoteBinary();
116
114
  return { global: false, path: downloadedPath };
117
115
  }
116
+ log(...data) {
117
+ if (!this.debug) {
118
+ return;
119
+ }
120
+ console.log(...data);
121
+ }
118
122
  // Runs the Deno CLI in the background and returns a reference to the child
119
123
  // process, awaiting its execution.
120
124
  async run(args, { pipeOutput } = {}) {
package/dist/bundler.js CHANGED
@@ -9,6 +9,7 @@ import { bundle as bundleESZIP } from './formats/eszip.js';
9
9
  import { bundle as bundleJS } from './formats/javascript.js';
10
10
  import { ImportMap } from './import_map.js';
11
11
  import { writeManifest } from './manifest.js';
12
+ import { ensureLatestTypes } from './types.js';
12
13
  const createBundleOps = ({ basePath, buildID, debug, deno, distDirectory, functions, importMap, featureFlags, }) => {
13
14
  const bundleOps = [];
14
15
  if (featureFlags.edge_functions_produce_eszip) {
@@ -19,6 +20,7 @@ const createBundleOps = ({ basePath, buildID, debug, deno, distDirectory, functi
19
20
  deno,
20
21
  distDirectory,
21
22
  functions,
23
+ importMap,
22
24
  }));
23
25
  }
24
26
  else {
@@ -42,6 +44,7 @@ const bundle = async (sourceDirectories, distDirectory, declarations = [], { cac
42
44
  onBeforeDownload,
43
45
  });
44
46
  const basePath = getBasePath(sourceDirectories);
47
+ await ensureLatestTypes(deno);
45
48
  // The name of the bundle will be the hash of its contents, which we can't
46
49
  // compute until we run the bundle process. For now, we'll use a random ID
47
50
  // to create the bundle artifacts and rename them later.
@@ -1,6 +1,7 @@
1
1
  import { DenoBridge } from '../bridge.js';
2
2
  import type { Bundle } from '../bundle.js';
3
3
  import { EdgeFunction } from '../edge_function.js';
4
+ import { ImportMap } from '../import_map.js';
4
5
  interface BundleESZIPOptions {
5
6
  basePath: string;
6
7
  buildID: string;
@@ -8,6 +9,7 @@ interface BundleESZIPOptions {
8
9
  deno: DenoBridge;
9
10
  distDirectory: string;
10
11
  functions: EdgeFunction[];
12
+ importMap: ImportMap;
11
13
  }
12
- declare const bundle: (options: BundleESZIPOptions) => Promise<Bundle>;
13
- export { bundle };
14
+ declare const bundleESZIP: ({ basePath, buildID, debug, deno, distDirectory, functions, importMap, }: BundleESZIPOptions) => Promise<Bundle>;
15
+ export { bundleESZIP as bundle };
@@ -2,15 +2,7 @@ import { join, resolve } from 'path';
2
2
  import { fileURLToPath } from 'url';
3
3
  import { wrapBundleError } from '../bundle_error.js';
4
4
  import { getFileHash } from '../utils/sha256.js';
5
- const bundle = async (options) => {
6
- try {
7
- return await bundleESZIP(options);
8
- }
9
- catch (error) {
10
- throw wrapBundleError(error, { format: 'eszip' });
11
- }
12
- };
13
- const bundleESZIP = async ({ basePath, buildID, debug, deno, distDirectory, functions, }) => {
5
+ const bundleESZIP = async ({ basePath, buildID, debug, deno, distDirectory, functions, importMap, }) => {
14
6
  const extension = '.eszip';
15
7
  const destPath = join(distDirectory, `${buildID}${extension}`);
16
8
  const bundler = getESZIPBundler();
@@ -18,12 +10,18 @@ const bundleESZIP = async ({ basePath, buildID, debug, deno, distDirectory, func
18
10
  basePath,
19
11
  destPath,
20
12
  functions,
13
+ imports: importMap.imports,
21
14
  };
22
15
  const flags = ['--allow-all'];
23
16
  if (!debug) {
24
17
  flags.push('--quiet');
25
18
  }
26
- await deno.run(['run', ...flags, bundler, JSON.stringify(payload)], { pipeOutput: true });
19
+ try {
20
+ await deno.run(['run', ...flags, bundler, JSON.stringify(payload)], { pipeOutput: true });
21
+ }
22
+ catch (error) {
23
+ throw wrapBundleError(error, { format: 'eszip' });
24
+ }
27
25
  const hash = await getFileHash(destPath);
28
26
  return { extension, format: 'eszip2', hash };
29
27
  };
@@ -33,4 +31,4 @@ const getESZIPBundler = () => {
33
31
  const bundlerPath = resolve(pathname, '../../../deno/bundle.ts');
34
32
  return bundlerPath;
35
33
  };
36
- export { bundle };
34
+ export { bundleESZIP as bundle };
@@ -11,7 +11,7 @@ interface BundleJSOptions {
11
11
  functions: EdgeFunction[];
12
12
  importMap: ImportMap;
13
13
  }
14
- declare const bundle: (options: BundleJSOptions) => Promise<Bundle>;
14
+ declare const bundleJS: ({ buildID, debug, deno, distDirectory, functions, importMap, }: BundleJSOptions) => Promise<Bundle>;
15
15
  interface GenerateStage2Options {
16
16
  distDirectory: string;
17
17
  fileName: string;
@@ -22,4 +22,4 @@ interface GenerateStage2Options {
22
22
  }
23
23
  declare const generateStage2: ({ distDirectory, fileName, formatExportTypeError, formatImportError, functions, type, }: GenerateStage2Options) => Promise<string>;
24
24
  declare const getBootstrapURL: () => string;
25
- export { bundle, generateStage2, getBootstrapURL };
25
+ export { bundleJS as bundle, generateStage2, getBootstrapURL };
@@ -5,15 +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://62bae4994570970008142f1e--edge-bootstrap.netlify.app/bootstrap/index-combined.ts';
9
- const bundle = async (options) => {
10
- try {
11
- return await bundleJS(options);
12
- }
13
- catch (error) {
14
- throw wrapBundleError(error, { format: 'javascript' });
15
- }
16
- };
8
+ const BOOTSTRAP_LATEST = 'https://62d144a15553b50009af7ac6--edge.netlify.com/bootstrap/index-combined.ts';
17
9
  const bundleJS = async ({ buildID, debug, deno, distDirectory, functions, importMap, }) => {
18
10
  const stage2Path = await generateStage2({ distDirectory, functions, fileName: `${buildID}-pre.js` });
19
11
  const extension = '.js';
@@ -22,7 +14,12 @@ const bundleJS = async ({ buildID, debug, deno, distDirectory, functions, import
22
14
  if (!debug) {
23
15
  flags.push('--quiet');
24
16
  }
25
- await deno.run(['bundle', ...flags, stage2Path, jsBundlePath], { pipeOutput: true });
17
+ try {
18
+ await deno.run(['bundle', ...flags, stage2Path, jsBundlePath], { pipeOutput: true });
19
+ }
20
+ catch (error) {
21
+ throw wrapBundleError(error, { format: 'javascript' });
22
+ }
26
23
  await fs.unlink(stage2Path);
27
24
  const hash = await getFileHash(jsBundlePath);
28
25
  return { extension, format: 'js', hash };
@@ -80,4 +77,4 @@ const getProductionEntryPoint = (functions) => {
80
77
  const defaultExport = 'boot(functions);';
81
78
  return [bootImport, importLines, exportDeclaration, defaultExport].join('\n\n');
82
79
  };
83
- export { bundle, generateStage2, getBootstrapURL };
80
+ export { bundleJS as bundle, generateStage2, getBootstrapURL };
@@ -2,7 +2,7 @@ import { Buffer } from 'buffer';
2
2
  import { promises as fs } from 'fs';
3
3
  import { dirname } from 'path';
4
4
  const INTERNAL_IMPORTS = {
5
- 'netlify:edge': 'https://edge-bootstrap.netlify.app/v1/index.ts',
5
+ 'netlify:edge': 'https://edge.netlify.com/v1/index.ts',
6
6
  };
7
7
  class ImportMap {
8
8
  constructor(input = []) {
@@ -2,6 +2,7 @@ import { tmpName } from 'tmp-promise';
2
2
  import { DenoBridge } from '../bridge.js';
3
3
  import { generateStage2 } from '../formats/javascript.js';
4
4
  import { ImportMap } from '../import_map.js';
5
+ import { ensureLatestTypes } from '../types.js';
5
6
  import { killProcess, waitForServer } from './util.js';
6
7
  const prepareServer = ({ deno, distDirectory, flags: denoFlags, formatExportTypeError, formatImportError, port, }) => {
7
8
  const processRef = {};
@@ -39,7 +40,6 @@ const prepareServer = ({ deno, distDirectory, flags: denoFlags, formatExportType
39
40
  };
40
41
  return startIsolate;
41
42
  };
42
- // eslint-disable-next-line complexity, max-statements
43
43
  const serve = async ({ certificatePath, debug, distImportMapPath, inspectSettings, formatExportTypeError, formatImportError, importMaps, onAfterDownload, onBeforeDownload, port, }) => {
44
44
  const deno = new DenoBridge({
45
45
  debug,
@@ -51,6 +51,8 @@ const serve = async ({ certificatePath, debug, distImportMapPath, inspectSetting
51
51
  const distDirectory = await tmpName();
52
52
  // Wait for the binary to be downloaded if needed.
53
53
  await deno.getBinaryPath();
54
+ // Downloading latest types if needed.
55
+ await ensureLatestTypes(deno);
54
56
  // Creating an ImportMap instance with any import maps supplied by the user,
55
57
  // if any.
56
58
  const importMap = new ImportMap(importMaps);
@@ -0,0 +1,3 @@
1
+ import type { DenoBridge } from './bridge.js';
2
+ declare const ensureLatestTypes: (deno: DenoBridge, customTypesURL?: string) => Promise<void>;
3
+ export { ensureLatestTypes };
package/dist/types.js ADDED
@@ -0,0 +1,58 @@
1
+ import { promises as fs } from 'fs';
2
+ import { join } from 'path';
3
+ import fetch from 'node-fetch';
4
+ const TYPES_URL = 'https://edge.netlify.com';
5
+ const ensureLatestTypes = async (deno, customTypesURL) => {
6
+ const typesURL = customTypesURL !== null && customTypesURL !== void 0 ? customTypesURL : TYPES_URL;
7
+ let [localVersion, remoteVersion] = [await getLocalVersion(deno), ''];
8
+ try {
9
+ remoteVersion = await getRemoteVersion(typesURL);
10
+ }
11
+ catch (error) {
12
+ deno.log('Could not check latest version of types:', error);
13
+ return;
14
+ }
15
+ if (localVersion === remoteVersion) {
16
+ deno.log('Local version of types is up-to-date:', localVersion);
17
+ return;
18
+ }
19
+ deno.log('Local version of types is outdated, updating:', localVersion);
20
+ try {
21
+ await deno.run(['cache', '-r', typesURL]);
22
+ }
23
+ catch (error) {
24
+ deno.log('Could not download latest types:', error);
25
+ return;
26
+ }
27
+ try {
28
+ await writeVersionFile(deno, remoteVersion);
29
+ }
30
+ catch {
31
+ // no-op
32
+ }
33
+ };
34
+ const getLocalVersion = async (deno) => {
35
+ const versionFilePath = join(deno.cacheDirectory, 'types-version.txt');
36
+ try {
37
+ const version = await fs.readFile(versionFilePath, 'utf8');
38
+ return version;
39
+ }
40
+ catch {
41
+ // no-op
42
+ }
43
+ };
44
+ const getRemoteVersion = async (typesURL) => {
45
+ const versionURL = new URL('/version.txt', typesURL);
46
+ const res = await fetch(versionURL.toString());
47
+ if (res.status !== 200) {
48
+ throw new Error('Unexpected status code from version endpoint');
49
+ }
50
+ const version = await res.text();
51
+ return version;
52
+ };
53
+ const writeVersionFile = async (deno, version) => {
54
+ await deno.ensureCacheDirectory();
55
+ const versionFilePath = join(deno.cacheDirectory, 'types-version.txt');
56
+ await fs.writeFile(versionFilePath, version);
57
+ };
58
+ export { ensureLatestTypes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "1.4.2",
3
+ "version": "1.6.0",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",