@netlify/edge-bundler 1.8.0 → 1.11.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/dist/bridge.d.ts CHANGED
@@ -1,10 +1,13 @@
1
+ /// <reference types="node" />
1
2
  import { ExecaChildProcess } from 'execa';
3
+ import { Logger } from './logger.js';
2
4
  declare type OnBeforeDownloadHook = () => void | Promise<void>;
3
5
  declare type OnAfterDownloadHook = (error?: Error) => void | Promise<void>;
4
6
  interface DenoOptions {
5
7
  cacheDirectory?: string;
6
8
  debug?: boolean;
7
9
  denoDir?: string;
10
+ logger?: Logger;
8
11
  onAfterDownload?: OnAfterDownloadHook;
9
12
  onBeforeDownload?: OnBeforeDownloadHook;
10
13
  useGlobal?: boolean;
@@ -15,19 +18,22 @@ interface ProcessRef {
15
18
  }
16
19
  interface RunOptions {
17
20
  pipeOutput?: boolean;
21
+ env?: NodeJS.ProcessEnv;
22
+ extendEnv?: boolean;
18
23
  }
19
24
  declare class DenoBridge {
20
25
  cacheDirectory: string;
21
26
  currentDownload?: ReturnType<DenoBridge['downloadBinary']>;
22
27
  debug: boolean;
23
28
  denoDir?: string;
29
+ logger: Logger;
24
30
  onAfterDownload?: OnAfterDownloadHook;
25
31
  onBeforeDownload?: OnBeforeDownloadHook;
26
32
  useGlobal: boolean;
27
33
  versionRange: string;
28
- constructor(options?: DenoOptions);
34
+ constructor(options: DenoOptions);
29
35
  private downloadBinary;
30
- static getBinaryVersion(binaryPath: string): Promise<string | undefined>;
36
+ private getBinaryVersion;
31
37
  private getCachedBinary;
32
38
  private getGlobalBinary;
33
39
  private getRemoteBinary;
@@ -38,10 +44,9 @@ declare class DenoBridge {
38
44
  global: boolean;
39
45
  path: string;
40
46
  }>;
41
- getEnvironmentVariables(): Record<string, string>;
42
- log(...data: unknown[]): void;
43
- run(args: string[], { pipeOutput }?: RunOptions): Promise<import("execa").ExecaReturnValue<string>>;
44
- runInBackground(args: string[], pipeOutput?: boolean, ref?: ProcessRef): Promise<void>;
47
+ getEnvironmentVariables(inputEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
48
+ run(args: string[], { pipeOutput, env: inputEnv, extendEnv }?: RunOptions): Promise<import("execa").ExecaReturnValue<string>>;
49
+ runInBackground(args: string[], ref?: ProcessRef, { pipeOutput, env: inputEnv, extendEnv }?: RunOptions): Promise<void>;
45
50
  }
46
51
  export { DenoBridge };
47
52
  export type { DenoOptions, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef };
package/dist/bridge.js CHANGED
@@ -5,27 +5,29 @@ import { execa } from 'execa';
5
5
  import semver from 'semver';
6
6
  import { download } from './downloader.js';
7
7
  import { getPathInHome } from './home_path.js';
8
+ import { getLogger } from './logger.js';
8
9
  import { getBinaryExtension } from './platform.js';
9
10
  const DENO_VERSION_FILE = 'version.txt';
10
11
  const DENO_VERSION_RANGE = '^1.20.3';
11
12
  class DenoBridge {
12
- constructor(options = {}) {
13
- var _a, _b, _c, _d;
13
+ constructor(options) {
14
+ var _a, _b, _c, _d, _e;
14
15
  this.cacheDirectory = (_a = options.cacheDirectory) !== null && _a !== void 0 ? _a : getPathInHome('deno-cli');
15
16
  this.debug = (_b = options.debug) !== null && _b !== void 0 ? _b : false;
16
17
  this.denoDir = options.denoDir;
18
+ this.logger = (_c = options.logger) !== null && _c !== void 0 ? _c : getLogger(undefined, options.debug);
17
19
  this.onAfterDownload = options.onAfterDownload;
18
20
  this.onBeforeDownload = options.onBeforeDownload;
19
- this.useGlobal = (_c = options.useGlobal) !== null && _c !== void 0 ? _c : true;
20
- this.versionRange = (_d = options.versionRange) !== null && _d !== void 0 ? _d : DENO_VERSION_RANGE;
21
+ this.useGlobal = (_d = options.useGlobal) !== null && _d !== void 0 ? _d : true;
22
+ this.versionRange = (_e = options.versionRange) !== null && _e !== void 0 ? _e : DENO_VERSION_RANGE;
21
23
  }
22
24
  async downloadBinary() {
23
25
  var _a, _b, _c;
24
26
  await ((_a = this.onBeforeDownload) === null || _a === void 0 ? void 0 : _a.call(this));
25
27
  await this.ensureCacheDirectory();
26
- this.log(`Downloading Deno CLI to ${this.cacheDirectory}...`);
28
+ this.logger.system(`Downloading Deno CLI to ${this.cacheDirectory}`);
27
29
  const binaryPath = await download(this.cacheDirectory, this.versionRange);
28
- const downloadedVersion = await DenoBridge.getBinaryVersion(binaryPath);
30
+ const downloadedVersion = await this.getBinaryVersion(binaryPath);
29
31
  // We should never get here, because it means that `DENO_VERSION_RANGE` is
30
32
  // a malformed semver range. If this does happen, let's throw an error so
31
33
  // that the tests catch it.
@@ -38,7 +40,7 @@ class DenoBridge {
38
40
  await ((_c = this.onAfterDownload) === null || _c === void 0 ? void 0 : _c.call(this));
39
41
  return binaryPath;
40
42
  }
41
- static async getBinaryVersion(binaryPath) {
43
+ async getBinaryVersion(binaryPath) {
42
44
  try {
43
45
  const { stdout } = await execa(binaryPath, ['--version']);
44
46
  const version = stdout.match(/^deno ([\d.]+)/);
@@ -47,8 +49,8 @@ class DenoBridge {
47
49
  }
48
50
  return version[1];
49
51
  }
50
- catch {
51
- // no-op
52
+ catch (error) {
53
+ this.logger.system('Error checking Deno binary version', error);
52
54
  }
53
55
  }
54
56
  async getCachedBinary() {
@@ -71,7 +73,7 @@ class DenoBridge {
71
73
  return;
72
74
  }
73
75
  const globalBinaryName = 'deno';
74
- const globalVersion = await DenoBridge.getBinaryVersion(globalBinaryName);
76
+ const globalVersion = await this.getBinaryVersion(globalBinaryName);
75
77
  if (globalVersion === undefined || !semver.satisfies(globalVersion, this.versionRange)) {
76
78
  return;
77
79
  }
@@ -103,44 +105,38 @@ class DenoBridge {
103
105
  async getBinaryPath() {
104
106
  const globalPath = await this.getGlobalBinary();
105
107
  if (globalPath !== undefined) {
106
- this.log('Using global installation of Deno CLI');
108
+ this.logger.system('Using global installation of Deno CLI');
107
109
  return { global: true, path: globalPath };
108
110
  }
109
111
  const cachedPath = await this.getCachedBinary();
110
112
  if (cachedPath !== undefined) {
111
- this.log('Using cached Deno CLI from', cachedPath);
113
+ this.logger.system('Using cached Deno CLI from', cachedPath);
112
114
  return { global: false, path: cachedPath };
113
115
  }
114
116
  const downloadedPath = await this.getRemoteBinary();
115
117
  return { global: false, path: downloadedPath };
116
118
  }
117
- getEnvironmentVariables() {
118
- const env = {};
119
+ getEnvironmentVariables(inputEnv = {}) {
120
+ const env = { ...inputEnv };
119
121
  if (this.denoDir !== undefined) {
120
122
  env.DENO_DIR = this.denoDir;
121
123
  }
122
124
  return env;
123
125
  }
124
- log(...data) {
125
- if (!this.debug) {
126
- return;
127
- }
128
- console.log(...data);
129
- }
130
126
  // Runs the Deno CLI in the background and returns a reference to the child
131
127
  // process, awaiting its execution.
132
- async run(args, { pipeOutput } = {}) {
128
+ async run(args, { pipeOutput, env: inputEnv, extendEnv = true } = {}) {
133
129
  const { path: binaryPath } = await this.getBinaryPath();
134
- const env = this.getEnvironmentVariables();
135
- const options = { env };
130
+ const env = this.getEnvironmentVariables(inputEnv);
131
+ const options = { env, extendEnv };
136
132
  return DenoBridge.runWithBinary(binaryPath, args, options, pipeOutput);
137
133
  }
138
134
  // Runs the Deno CLI in the background, assigning a reference of the child
139
135
  // process to a `ps` property in the `ref` argument, if one is supplied.
140
- async runInBackground(args, pipeOutput, ref) {
136
+ async runInBackground(args, ref, { pipeOutput, env: inputEnv, extendEnv = true } = {}) {
141
137
  const { path: binaryPath } = await this.getBinaryPath();
142
- const env = this.getEnvironmentVariables();
143
- const options = { env };
138
+ const env = this.getEnvironmentVariables(inputEnv);
139
+ const options = { env, extendEnv };
144
140
  const ps = DenoBridge.runWithBinary(binaryPath, args, options, pipeOutput);
145
141
  if (ref !== undefined) {
146
142
  // eslint-disable-next-line no-param-reassign
package/dist/bundler.d.ts CHANGED
@@ -3,6 +3,7 @@ import type { Declaration } from './declaration.js';
3
3
  import { EdgeFunction } from './edge_function.js';
4
4
  import { FeatureFlags } from './feature_flags.js';
5
5
  import { ImportMapFile } from './import_map.js';
6
+ import { LogFunction } from './logger.js';
6
7
  interface BundleOptions {
7
8
  basePath?: string;
8
9
  cacheDirectory?: string;
@@ -12,8 +13,9 @@ interface BundleOptions {
12
13
  importMaps?: ImportMapFile[];
13
14
  onAfterDownload?: OnAfterDownloadHook;
14
15
  onBeforeDownload?: OnBeforeDownloadHook;
16
+ systemLogger?: LogFunction;
15
17
  }
16
- declare const bundle: (sourceDirectories: string[], distDirectory: string, declarations?: Declaration[], { basePath: inputBasePath, cacheDirectory, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMaps, onAfterDownload, onBeforeDownload, }?: BundleOptions) => Promise<{
18
+ declare const bundle: (sourceDirectories: string[], distDirectory: string, declarations?: Declaration[], { basePath: inputBasePath, cacheDirectory, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMaps, onAfterDownload, onBeforeDownload, systemLogger, }?: BundleOptions) => Promise<{
17
19
  functions: EdgeFunction[];
18
20
  manifest: import("./manifest.js").Manifest;
19
21
  }>;
package/dist/bundler.js CHANGED
@@ -8,6 +8,7 @@ import { findFunctions } from './finder.js';
8
8
  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
+ import { getLogger } from './logger.js';
11
12
  import { writeManifest } from './manifest.js';
12
13
  import { ensureLatestTypes } from './types.js';
13
14
  const createBundleOps = ({ basePath, buildID, debug, deno, distDirectory, functions, importMap, featureFlags, }) => {
@@ -35,11 +36,13 @@ const createBundleOps = ({ basePath, buildID, debug, deno, distDirectory, functi
35
36
  }
36
37
  return bundleOps;
37
38
  };
38
- const bundle = async (sourceDirectories, distDirectory, declarations = [], { basePath: inputBasePath, cacheDirectory, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMaps, onAfterDownload, onBeforeDownload, } = {}) => {
39
+ const bundle = async (sourceDirectories, distDirectory, declarations = [], { basePath: inputBasePath, cacheDirectory, debug, distImportMapPath, featureFlags: inputFeatureFlags, importMaps, onAfterDownload, onBeforeDownload, systemLogger, } = {}) => {
40
+ const logger = getLogger(systemLogger, debug);
39
41
  const featureFlags = getFlags(inputFeatureFlags);
40
42
  const options = {
41
43
  debug,
42
44
  cacheDirectory,
45
+ logger,
43
46
  onAfterDownload,
44
47
  onBeforeDownload,
45
48
  };
@@ -48,7 +51,7 @@ const bundle = async (sourceDirectories, distDirectory, declarations = [], { bas
48
51
  }
49
52
  const deno = new DenoBridge(options);
50
53
  const basePath = getBasePath(sourceDirectories, inputBasePath);
51
- await ensureLatestTypes(deno);
54
+ await ensureLatestTypes(deno, logger);
52
55
  // The name of the bundle will be the hash of its contents, which we can't
53
56
  // compute until we run the bundle process. For now, we'll use a random ID
54
57
  // to create the bundle artifacts and rename them later.
@@ -0,0 +1,8 @@
1
+ declare type LogFunction = (...args: unknown[]) => void;
2
+ interface Logger {
3
+ system: LogFunction;
4
+ user: LogFunction;
5
+ }
6
+ declare const getLogger: (systemLogger?: LogFunction, debug?: boolean) => Logger;
7
+ export { getLogger };
8
+ export type { LogFunction, Logger };
package/dist/logger.js ADDED
@@ -0,0 +1,14 @@
1
+ const noopLogger = () => {
2
+ // no-op
3
+ };
4
+ const getLogger = (systemLogger, debug = false) => {
5
+ // If there is a system logger configured, we'll use that. If there isn't,
6
+ // we'll pipe system logs to stdout if `debug` is enabled and swallow them
7
+ // otherwise.
8
+ const system = systemLogger !== null && systemLogger !== void 0 ? systemLogger : (debug ? console.log : noopLogger);
9
+ return {
10
+ system,
11
+ user: console.log,
12
+ };
13
+ };
14
+ export { getLogger };
@@ -1,6 +1,11 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
1
5
  import { OnAfterDownloadHook, OnBeforeDownloadHook } from '../bridge.js';
2
6
  import type { EdgeFunction } from '../edge_function.js';
3
7
  import { ImportMapFile } from '../import_map.js';
8
+ import { LogFunction } from '../logger.js';
4
9
  declare type FormatFunction = (name: string) => string;
5
10
  interface InspectSettings {
6
11
  enabled: boolean;
@@ -18,8 +23,9 @@ interface ServeOptions {
18
23
  formatExportTypeError?: FormatFunction;
19
24
  formatImportError?: FormatFunction;
20
25
  port: number;
26
+ systemLogger?: LogFunction;
21
27
  }
22
- declare const serve: ({ certificatePath, debug, distImportMapPath, inspectSettings, formatExportTypeError, formatImportError, importMaps, onAfterDownload, onBeforeDownload, port, }: ServeOptions) => Promise<(newFunctions: EdgeFunction[]) => Promise<{
28
+ declare const serve: ({ certificatePath, debug, distImportMapPath, inspectSettings, formatExportTypeError, formatImportError, importMaps, onAfterDownload, onBeforeDownload, port, systemLogger, }: ServeOptions) => Promise<(newFunctions: EdgeFunction[], env?: NodeJS.ProcessEnv) => Promise<{
23
29
  graph: any;
24
30
  success: boolean;
25
31
  }>>;
@@ -2,11 +2,12 @@ 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 { getLogger } from '../logger.js';
5
6
  import { ensureLatestTypes } from '../types.js';
6
7
  import { killProcess, waitForServer } from './util.js';
7
8
  const prepareServer = ({ deno, distDirectory, flags: denoFlags, formatExportTypeError, formatImportError, port, }) => {
8
9
  const processRef = {};
9
- const startIsolate = async (newFunctions) => {
10
+ const startIsolate = async (newFunctions, env = {}) => {
10
11
  if ((processRef === null || processRef === void 0 ? void 0 : processRef.ps) !== undefined) {
11
12
  await killProcess(processRef.ps);
12
13
  }
@@ -31,7 +32,14 @@ const prepareServer = ({ deno, distDirectory, flags: denoFlags, formatExportType
31
32
  // no-op
32
33
  }
33
34
  const bootstrapFlags = ['--port', port.toString()];
34
- await deno.runInBackground(['run', ...denoFlags, stage2Path, ...bootstrapFlags], true, processRef);
35
+ // We set `extendEnv: false` to avoid polluting the edge function context
36
+ // with variables from the user's system, since those will not be available
37
+ // in the production environment.
38
+ await deno.runInBackground(['run', ...denoFlags, stage2Path, ...bootstrapFlags], processRef, {
39
+ pipeOutput: true,
40
+ env,
41
+ extendEnv: false,
42
+ });
35
43
  const success = await waitForServer(port, processRef.ps);
36
44
  return {
37
45
  graph,
@@ -40,9 +48,11 @@ const prepareServer = ({ deno, distDirectory, flags: denoFlags, formatExportType
40
48
  };
41
49
  return startIsolate;
42
50
  };
43
- const serve = async ({ certificatePath, debug, distImportMapPath, inspectSettings, formatExportTypeError, formatImportError, importMaps, onAfterDownload, onBeforeDownload, port, }) => {
51
+ const serve = async ({ certificatePath, debug, distImportMapPath, inspectSettings, formatExportTypeError, formatImportError, importMaps, onAfterDownload, onBeforeDownload, port, systemLogger, }) => {
52
+ const logger = getLogger(systemLogger, debug);
44
53
  const deno = new DenoBridge({
45
54
  debug,
55
+ logger,
46
56
  onAfterDownload,
47
57
  onBeforeDownload,
48
58
  });
@@ -52,7 +62,7 @@ const serve = async ({ certificatePath, debug, distImportMapPath, inspectSetting
52
62
  // Wait for the binary to be downloaded if needed.
53
63
  await deno.getBinaryPath();
54
64
  // Downloading latest types if needed.
55
- await ensureLatestTypes(deno);
65
+ await ensureLatestTypes(deno, logger);
56
66
  // Creating an ImportMap instance with any import maps supplied by the user,
57
67
  // if any.
58
68
  const importMap = new ImportMap(importMaps);
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { DenoBridge } from './bridge.js';
2
- declare const ensureLatestTypes: (deno: DenoBridge, customTypesURL?: string) => Promise<void>;
2
+ import type { Logger } from './logger.js';
3
+ declare const ensureLatestTypes: (deno: DenoBridge, logger: Logger, customTypesURL?: string) => Promise<void>;
3
4
  export { ensureLatestTypes };
package/dist/types.js CHANGED
@@ -2,26 +2,26 @@ import { promises as fs } from 'fs';
2
2
  import { join } from 'path';
3
3
  import fetch from 'node-fetch';
4
4
  const TYPES_URL = 'https://edge.netlify.com';
5
- const ensureLatestTypes = async (deno, customTypesURL) => {
5
+ const ensureLatestTypes = async (deno, logger, customTypesURL) => {
6
6
  const typesURL = customTypesURL !== null && customTypesURL !== void 0 ? customTypesURL : TYPES_URL;
7
7
  let [localVersion, remoteVersion] = [await getLocalVersion(deno), ''];
8
8
  try {
9
9
  remoteVersion = await getRemoteVersion(typesURL);
10
10
  }
11
11
  catch (error) {
12
- deno.log('Could not check latest version of types:', error);
12
+ logger.system('Could not check latest version of types:', error);
13
13
  return;
14
14
  }
15
15
  if (localVersion === remoteVersion) {
16
- deno.log('Local version of types is up-to-date:', localVersion);
16
+ logger.system('Local version of types is up-to-date:', localVersion);
17
17
  return;
18
18
  }
19
- deno.log('Local version of types is outdated, updating:', localVersion);
19
+ logger.system('Local version of types is outdated, updating:', localVersion);
20
20
  try {
21
21
  await deno.run(['cache', '-r', typesURL]);
22
22
  }
23
23
  catch (error) {
24
- deno.log('Could not download latest types:', error);
24
+ logger.system('Could not download latest types:', error);
25
25
  return;
26
26
  }
27
27
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "1.8.0",
3
+ "version": "1.11.0",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",