@netlify/zip-it-and-ship-it 4.23.3 → 4.23.5-setup-typescript-4

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.
Files changed (136) hide show
  1. package/dist/archive.d.ts +8 -0
  2. package/dist/archive.js +39 -0
  3. package/dist/bin.d.ts +2 -0
  4. package/dist/bin.js +67 -0
  5. package/dist/config.d.ts +4 -0
  6. package/dist/config.js +26 -0
  7. package/dist/feature_flags.d.ts +12 -0
  8. package/dist/feature_flags.js +10 -0
  9. package/dist/main.d.ts +15 -0
  10. package/dist/main.js +80 -0
  11. package/dist/manifest.d.ts +4 -0
  12. package/dist/manifest.js +25 -0
  13. package/dist/node_dependencies/index.d.ts +15 -0
  14. package/dist/node_dependencies/index.js +132 -0
  15. package/dist/node_dependencies/module.d.ts +1 -0
  16. package/dist/node_dependencies/module.js +10 -0
  17. package/dist/node_dependencies/nested.d.ts +10 -0
  18. package/{src → dist}/node_dependencies/nested.js +39 -60
  19. package/dist/node_dependencies/package_json.d.ts +1 -0
  20. package/dist/node_dependencies/package_json.js +29 -0
  21. package/dist/node_dependencies/published.d.ts +1 -0
  22. package/dist/node_dependencies/published.js +42 -0
  23. package/dist/node_dependencies/resolve.d.ts +2 -0
  24. package/dist/node_dependencies/resolve.js +114 -0
  25. package/dist/node_dependencies/side_files.d.ts +1 -0
  26. package/dist/node_dependencies/side_files.js +14 -0
  27. package/dist/node_dependencies/special_cases.d.ts +6 -0
  28. package/dist/node_dependencies/special_cases.js +44 -0
  29. package/dist/node_dependencies/traverse.d.ts +39 -0
  30. package/dist/node_dependencies/traverse.js +120 -0
  31. package/dist/node_dependencies/tree_files.d.ts +3 -0
  32. package/dist/node_dependencies/tree_files.js +32 -0
  33. package/dist/node_dependencies/tree_shake.d.ts +1 -0
  34. package/{src → dist}/node_dependencies/tree_shake.js +8 -12
  35. package/dist/runtimes/detect_runtime.d.ts +4 -0
  36. package/dist/runtimes/detect_runtime.js +27 -0
  37. package/dist/runtimes/go/builder.d.ts +12 -0
  38. package/dist/runtimes/go/builder.js +39 -0
  39. package/dist/runtimes/go/index.d.ts +18 -0
  40. package/dist/runtimes/go/index.js +78 -0
  41. package/dist/runtimes/index.d.ts +9 -0
  42. package/dist/runtimes/index.js +87 -0
  43. package/dist/runtimes/node/base_path.d.ts +1 -0
  44. package/dist/runtimes/node/base_path.js +8 -0
  45. package/dist/runtimes/node/bundler.d.ts +20 -0
  46. package/dist/runtimes/node/bundler.js +135 -0
  47. package/dist/runtimes/node/bundler_target.d.ts +1 -0
  48. package/dist/runtimes/node/bundler_target.js +16 -0
  49. package/dist/runtimes/node/detect_es_module.d.ts +3 -0
  50. package/dist/runtimes/node/detect_es_module.js +28 -0
  51. package/dist/runtimes/node/dynamic_imports/parser.d.ts +8 -0
  52. package/dist/runtimes/node/dynamic_imports/parser.js +135 -0
  53. package/dist/runtimes/node/dynamic_imports/plugin.d.ts +10 -0
  54. package/dist/runtimes/node/dynamic_imports/plugin.js +102 -0
  55. package/dist/runtimes/node/finder.d.ts +12 -0
  56. package/dist/runtimes/node/finder.js +82 -0
  57. package/dist/runtimes/node/index.d.ts +16 -0
  58. package/dist/runtimes/node/index.js +118 -0
  59. package/dist/runtimes/node/list_imports.d.ts +4 -0
  60. package/dist/runtimes/node/list_imports.js +68 -0
  61. package/dist/runtimes/node/native_modules/detector.d.ts +7 -0
  62. package/dist/runtimes/node/native_modules/detector.js +14 -0
  63. package/dist/runtimes/node/native_modules/plugin.d.ts +4 -0
  64. package/dist/runtimes/node/native_modules/plugin.js +65 -0
  65. package/dist/runtimes/node/src_files.d.ts +20 -0
  66. package/dist/runtimes/node/src_files.js +84 -0
  67. package/dist/runtimes/node/zip_esbuild.d.ts +22 -0
  68. package/dist/runtimes/node/zip_esbuild.js +95 -0
  69. package/dist/runtimes/node/zip_zisi.d.ts +20 -0
  70. package/dist/runtimes/node/zip_zisi.js +42 -0
  71. package/dist/runtimes/rust/builder.d.ts +9 -0
  72. package/dist/runtimes/rust/builder.js +100 -0
  73. package/dist/runtimes/rust/constants.d.ts +2 -0
  74. package/dist/runtimes/rust/constants.js +3 -0
  75. package/dist/runtimes/rust/index.d.ts +20 -0
  76. package/dist/runtimes/rust/index.js +101 -0
  77. package/dist/utils/consts.d.ts +8 -0
  78. package/dist/utils/consts.js +10 -0
  79. package/dist/utils/fs.d.ts +14 -0
  80. package/dist/utils/fs.js +86 -0
  81. package/dist/utils/polyfills.d.ts +1 -0
  82. package/{src → dist}/utils/polyfills.js +1 -1
  83. package/dist/utils/remove_falsy.d.ts +1 -0
  84. package/dist/utils/remove_falsy.js +9 -0
  85. package/dist/utils/shell.d.ts +2 -0
  86. package/dist/utils/shell.js +2 -0
  87. package/dist/zip.d.ts +15 -0
  88. package/dist/zip.js +116 -0
  89. package/dist/zip_binary.d.ts +7 -0
  90. package/dist/zip_binary.js +20 -0
  91. package/dist/zip_node.d.ts +4 -0
  92. package/dist/zip_node.js +143 -0
  93. package/package.json +19 -13
  94. package/src/archive.js +0 -36
  95. package/src/bin.js +0 -53
  96. package/src/config.js +0 -32
  97. package/src/feature_flags.js +0 -20
  98. package/src/main.js +0 -91
  99. package/src/manifest.js +0 -22
  100. package/src/node_dependencies/index.js +0 -161
  101. package/src/node_dependencies/module.js +0 -13
  102. package/src/node_dependencies/package_json.js +0 -21
  103. package/src/node_dependencies/published.js +0 -37
  104. package/src/node_dependencies/resolve.js +0 -108
  105. package/src/node_dependencies/side_files.js +0 -18
  106. package/src/node_dependencies/special_cases.js +0 -42
  107. package/src/node_dependencies/traverse.js +0 -147
  108. package/src/node_dependencies/tree_files.js +0 -20
  109. package/src/runtimes/detect_runtime.js +0 -20
  110. package/src/runtimes/go/builder.js +0 -37
  111. package/src/runtimes/go/index.js +0 -89
  112. package/src/runtimes/index.js +0 -96
  113. package/src/runtimes/node/base_path.js +0 -11
  114. package/src/runtimes/node/bundler.js +0 -151
  115. package/src/runtimes/node/bundler_target.js +0 -22
  116. package/src/runtimes/node/detect_es_module.js +0 -24
  117. package/src/runtimes/node/dynamic_imports/parser.js +0 -162
  118. package/src/runtimes/node/dynamic_imports/plugin.js +0 -116
  119. package/src/runtimes/node/finder.js +0 -93
  120. package/src/runtimes/node/index.js +0 -122
  121. package/src/runtimes/node/list_imports.js +0 -59
  122. package/src/runtimes/node/native_modules/detector.js +0 -21
  123. package/src/runtimes/node/native_modules/plugin.js +0 -77
  124. package/src/runtimes/node/src_files.js +0 -99
  125. package/src/runtimes/node/zip_esbuild.js +0 -119
  126. package/src/runtimes/node/zip_zisi.js +0 -50
  127. package/src/runtimes/rust/builder.js +0 -114
  128. package/src/runtimes/rust/constants.js +0 -4
  129. package/src/runtimes/rust/index.js +0 -112
  130. package/src/utils/consts.js +0 -10
  131. package/src/utils/fs.js +0 -88
  132. package/src/utils/remove_falsy.js +0 -12
  133. package/src/utils/shell.js +0 -3
  134. package/src/zip.js +0 -161
  135. package/src/zip_binary.js +0 -11
  136. package/src/zip_node.js +0 -179
@@ -0,0 +1,100 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { readFile } = require('fs');
11
+ const { basename, join } = require('path');
12
+ const { promisify } = require('util');
13
+ const pReadFile = promisify(readFile);
14
+ const makeDir = require('make-dir');
15
+ const tmp = require('tmp-promise');
16
+ const toml = require('toml');
17
+ const { RUNTIME_RUST } = require('../../utils/consts');
18
+ const { lstat } = require('../../utils/fs');
19
+ const { runCommand } = require('../../utils/shell');
20
+ const { BUILD_TARGET, MANIFEST_NAME } = require('./constants');
21
+ const build = ({ config, name, srcDir }) => __awaiter(this, void 0, void 0, function* () {
22
+ const functionName = basename(srcDir);
23
+ try {
24
+ yield installBuildTarget();
25
+ }
26
+ catch (error) {
27
+ error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } };
28
+ throw error;
29
+ }
30
+ const targetDirectory = yield getTargetDirectory({ config, name });
31
+ yield cargoBuild({ functionName, srcDir, targetDirectory });
32
+ // By default, the binary will have the same name as the crate and there's no
33
+ // way to override it (https://github.com/rust-lang/cargo/issues/1706). We
34
+ // must extract the crate name from the manifest and use it to form the path
35
+ // to the binary.
36
+ const manifest = yield pReadFile(join(srcDir, MANIFEST_NAME));
37
+ const { package } = toml.parse(manifest);
38
+ const binaryPath = join(targetDirectory, BUILD_TARGET, 'release', package.name);
39
+ const stat = yield lstat(binaryPath);
40
+ return {
41
+ path: binaryPath,
42
+ stat,
43
+ };
44
+ });
45
+ const cargoBuild = ({ functionName, srcDir, targetDirectory }) => __awaiter(this, void 0, void 0, function* () {
46
+ try {
47
+ yield runCommand('cargo', ['build', '--target', BUILD_TARGET, '--release'], {
48
+ cwd: srcDir,
49
+ env: {
50
+ CARGO_TARGET_DIR: targetDirectory,
51
+ },
52
+ });
53
+ }
54
+ catch (error) {
55
+ const hasToolchain = yield checkRustToolchain();
56
+ if (hasToolchain) {
57
+ console.error(`Could not compile Rust function ${functionName}:\n`);
58
+ }
59
+ else {
60
+ error.message =
61
+ 'There is no Rust toolchain installed. Visit https://ntl.fyi/missing-rust-toolchain for more information.';
62
+ }
63
+ error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } };
64
+ throw error;
65
+ }
66
+ });
67
+ const checkRustToolchain = () => __awaiter(this, void 0, void 0, function* () {
68
+ try {
69
+ yield runCommand('cargo', ['-V']);
70
+ return true;
71
+ }
72
+ catch (_) {
73
+ return false;
74
+ }
75
+ });
76
+ // Returns the path of the Cargo target directory.
77
+ const getTargetDirectory = ({ config, name }) => __awaiter(this, void 0, void 0, function* () {
78
+ const { rustTargetDirectory } = config;
79
+ // If the config includes a `rustTargetDirectory` path, we'll use that.
80
+ if (rustTargetDirectory) {
81
+ // We replace the [name] placeholder with the name of the function.
82
+ const path = rustTargetDirectory.replace(/\[name]/g, name);
83
+ yield makeDir(path);
84
+ return path;
85
+ }
86
+ // If the directory hasn't been configured, we'll use a temporary directory.
87
+ const { path } = yield tmp.dir();
88
+ return path;
89
+ });
90
+ let buildTargetInstallation;
91
+ // Installs the build target defined in `BUILD_TARGET`. The Promise is saved to
92
+ // `buildTargetInstallation` so that we run the command just once for multiple
93
+ // Rust functions.
94
+ const installBuildTarget = () => {
95
+ if (buildTargetInstallation === undefined) {
96
+ buildTargetInstallation = runCommand('rustup', ['target', 'add', BUILD_TARGET]);
97
+ }
98
+ return buildTargetInstallation;
99
+ };
100
+ module.exports = { build };
@@ -0,0 +1,2 @@
1
+ export const BUILD_TARGET: "x86_64-unknown-linux-musl";
2
+ export const MANIFEST_NAME: "Cargo.toml";
@@ -0,0 +1,3 @@
1
+ const BUILD_TARGET = 'x86_64-unknown-linux-musl';
2
+ const MANIFEST_NAME = 'Cargo.toml';
3
+ module.exports = { BUILD_TARGET, MANIFEST_NAME };
@@ -0,0 +1,20 @@
1
+ export function findFunctionsInPaths({ featureFlags, fsCache, paths }: {
2
+ featureFlags: any;
3
+ fsCache: any;
4
+ paths: any;
5
+ }): Promise<any[]>;
6
+ import { RUNTIME_RUST } from "../../utils/consts";
7
+ export function zipFunction({ config, destFolder, filename, mainFile, runtime, srcDir, srcPath, stat }: {
8
+ config: any;
9
+ destFolder: any;
10
+ filename: any;
11
+ mainFile: any;
12
+ runtime: any;
13
+ srcDir: any;
14
+ srcPath: any;
15
+ stat: any;
16
+ }): Promise<{
17
+ config: any;
18
+ path: string;
19
+ }>;
20
+ export { RUNTIME_RUST as name };
@@ -0,0 +1,101 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { join, extname, dirname, basename } = require('path');
11
+ const { RUNTIME_RUST } = require('../../utils/consts');
12
+ const { cachedLstat, cachedReaddir } = require('../../utils/fs');
13
+ const { zipBinary } = require('../../zip_binary');
14
+ const { detectBinaryRuntime } = require('../detect_runtime');
15
+ const { build } = require('./builder');
16
+ const { MANIFEST_NAME } = require('./constants');
17
+ const detectRustFunction = ({ fsCache, path }) => __awaiter(this, void 0, void 0, function* () {
18
+ const stat = yield cachedLstat(fsCache, path);
19
+ if (!stat.isDirectory()) {
20
+ return;
21
+ }
22
+ const files = yield cachedReaddir(fsCache, path);
23
+ const hasCargoManifest = files.includes(MANIFEST_NAME);
24
+ if (!hasCargoManifest) {
25
+ return;
26
+ }
27
+ const mainFilePath = join(path, 'src', 'main.rs');
28
+ try {
29
+ const mainFile = yield cachedLstat(fsCache, mainFilePath);
30
+ if (mainFile.isFile()) {
31
+ return mainFilePath;
32
+ }
33
+ }
34
+ catch (_) {
35
+ // no-op
36
+ }
37
+ });
38
+ const findFunctionsInPaths = function ({ featureFlags, fsCache, paths }) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const functions = yield Promise.all(paths.map((path) => __awaiter(this, void 0, void 0, function* () {
41
+ const runtime = yield detectBinaryRuntime({ fsCache, path });
42
+ if (runtime === RUNTIME_RUST) {
43
+ return processBinary({ fsCache, path });
44
+ }
45
+ if (featureFlags.buildRustSource !== true) {
46
+ return;
47
+ }
48
+ const rustSourceFile = yield detectRustFunction({ fsCache, path });
49
+ if (rustSourceFile) {
50
+ return processSource({ fsCache, mainFile: rustSourceFile, path });
51
+ }
52
+ })));
53
+ return functions.filter(Boolean);
54
+ });
55
+ };
56
+ const processBinary = ({ fsCache, path }) => __awaiter(this, void 0, void 0, function* () {
57
+ const stat = yield cachedLstat(fsCache, path);
58
+ const name = basename(path, extname(path));
59
+ return {
60
+ mainFile: path,
61
+ name,
62
+ srcDir: dirname(path),
63
+ srcPath: path,
64
+ stat,
65
+ };
66
+ });
67
+ const processSource = ({ mainFile, path }) => {
68
+ const functionName = basename(path);
69
+ return {
70
+ mainFile,
71
+ name: functionName,
72
+ srcDir: path,
73
+ srcPath: path,
74
+ };
75
+ };
76
+ // The name of the binary inside the zip file must always be `bootstrap`
77
+ // because they include the Lambda runtime, and that's the name that AWS
78
+ // expects for those kind of functions.
79
+ const zipFunction = function ({ config, destFolder, filename, mainFile, runtime, srcDir, srcPath, stat }) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const destPath = join(destFolder, `${filename}.zip`);
82
+ const isSource = extname(mainFile) === '.rs';
83
+ const zipOptions = {
84
+ destPath,
85
+ filename: 'bootstrap',
86
+ runtime,
87
+ };
88
+ // If we're building from source, we first need to build the source and zip
89
+ // the resulting binary. Otherwise, we're dealing with a binary so we zip it
90
+ // directly.
91
+ if (isSource) {
92
+ const { path: binaryPath, stat: binaryStat } = yield build({ config, name: filename, srcDir });
93
+ yield zipBinary(Object.assign(Object.assign({}, zipOptions), { srcPath: binaryPath, stat: binaryStat }));
94
+ }
95
+ else {
96
+ yield zipBinary(Object.assign(Object.assign({}, zipOptions), { srcPath, stat }));
97
+ }
98
+ return { config, path: destPath };
99
+ });
100
+ };
101
+ module.exports = { findFunctionsInPaths, name: RUNTIME_RUST, zipFunction };
@@ -0,0 +1,8 @@
1
+ export const ARCHIVE_FORMAT_NONE: string;
2
+ export const ARCHIVE_FORMAT_ZIP: string;
3
+ export const JS_BUNDLER_ESBUILD: string;
4
+ export const JS_BUNDLER_ESBUILD_ZISI: string;
5
+ export const JS_BUNDLER_ZISI: string;
6
+ export const RUNTIME_GO: string;
7
+ export const RUNTIME_JS: string;
8
+ export const RUNTIME_RUST: string;
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ ARCHIVE_FORMAT_NONE: 'none',
3
+ ARCHIVE_FORMAT_ZIP: 'zip',
4
+ JS_BUNDLER_ESBUILD: 'esbuild',
5
+ JS_BUNDLER_ESBUILD_ZISI: 'esbuild_zisi',
6
+ JS_BUNDLER_ZISI: 'zisi',
7
+ RUNTIME_GO: 'go',
8
+ RUNTIME_JS: 'js',
9
+ RUNTIME_RUST: 'rs',
10
+ };
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ export function cachedLstat(...args: any[]): any;
3
+ export function cachedReaddir(...args: any[]): any;
4
+ export function cachedReadFile(...args: any[]): any;
5
+ declare const pLstat: typeof lstat.__promisify__;
6
+ export function getPathWithExtension(path: any, extension: any): string;
7
+ export function listFunctionsDirectories(srcFolders: any): Promise<any[]>;
8
+ export function listFunctionsDirectory(srcFolder: any): Promise<string[]>;
9
+ export function resolveFunctionsDirectories(input: any): string[];
10
+ export function safeUnlink(path: any): Promise<void>;
11
+ declare const pWriteFile: typeof writeFile.__promisify__;
12
+ import { lstat } from "fs";
13
+ import { writeFile } from "fs";
14
+ export { pLstat as lstat, pWriteFile as writeFile };
@@ -0,0 +1,86 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { lstat, readdir, readFile, unlink, writeFile } = require('fs');
11
+ const { format, join, parse, resolve } = require('path');
12
+ const { promisify } = require('util');
13
+ const pLstat = promisify(lstat);
14
+ const pReaddir = promisify(readdir);
15
+ const pReadFile = promisify(readFile);
16
+ const pUnlink = promisify(unlink);
17
+ const pWriteFile = promisify(writeFile);
18
+ // This caches multiple FS calls to the same path. It creates a cache key with
19
+ // the name of the function and the path (e.g. "readdir:/some/directory").
20
+ const cachedIOFunction = (func, cache, path, ...args) => {
21
+ const key = `${func.name}:${path}`;
22
+ if (cache[key] === undefined) {
23
+ // eslint-disable-next-line no-param-reassign
24
+ cache[key] = func(path, ...args);
25
+ }
26
+ return cache[key];
27
+ };
28
+ const cachedLstat = (...args) => cachedIOFunction(pLstat, ...args);
29
+ const cachedReaddir = (...args) => cachedIOFunction(pReaddir, ...args);
30
+ const cachedReadFile = (...args) => cachedIOFunction(pReadFile, ...args);
31
+ const getPathWithExtension = (path, extension) => format(Object.assign(Object.assign({}, parse(path)), { base: undefined, ext: extension }));
32
+ const safeUnlink = (path) => __awaiter(this, void 0, void 0, function* () {
33
+ try {
34
+ yield pUnlink(path);
35
+ }
36
+ catch (_) { }
37
+ });
38
+ // Takes a list of absolute paths and returns an array containing all the
39
+ // filenames within those directories, if at least one of the directories
40
+ // exists. If not, an error is thrown.
41
+ const listFunctionsDirectories = function (srcFolders) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const filenamesByDirectory = yield Promise.all(srcFolders.map((srcFolder) => __awaiter(this, void 0, void 0, function* () {
44
+ try {
45
+ const filenames = yield listFunctionsDirectory(srcFolder);
46
+ return filenames;
47
+ }
48
+ catch (error) {
49
+ return null;
50
+ }
51
+ })));
52
+ const validDirectories = filenamesByDirectory.filter(Boolean);
53
+ if (validDirectories.length === 0) {
54
+ throw new Error(`Functions folder does not exist: ${srcFolders.join(', ')}`);
55
+ }
56
+ return validDirectories.flat();
57
+ });
58
+ };
59
+ const listFunctionsDirectory = function (srcFolder) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ try {
62
+ const filenames = yield pReaddir(srcFolder);
63
+ return filenames.map((name) => join(srcFolder, name));
64
+ }
65
+ catch (error) {
66
+ throw new Error(`Functions folder does not exist: ${srcFolder}`);
67
+ }
68
+ });
69
+ };
70
+ const resolveFunctionsDirectories = (input) => {
71
+ const directories = Array.isArray(input) ? input : [input];
72
+ const absoluteDirectories = directories.map((srcFolder) => resolve(srcFolder));
73
+ return absoluteDirectories;
74
+ };
75
+ module.exports = {
76
+ cachedLstat,
77
+ cachedReaddir,
78
+ cachedReadFile,
79
+ lstat: pLstat,
80
+ getPathWithExtension,
81
+ listFunctionsDirectories,
82
+ listFunctionsDirectory,
83
+ resolveFunctionsDirectories,
84
+ safeUnlink,
85
+ writeFile: pWriteFile,
86
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,3 @@
1
1
  // Patches `Array.flat()` and `Array.flatMap()`
2
2
  // TODO: remove after dropping Node <12 support
3
- require('array-flat-polyfill')
3
+ require('array-flat-polyfill');
@@ -0,0 +1 @@
1
+ export function removeFalsy(obj: any): Partial<any>;
@@ -0,0 +1,9 @@
1
+ const filterObj = require('filter-obj');
2
+ // Remove falsy values from object
3
+ const removeFalsy = function (obj) {
4
+ return filterObj(obj, isDefined);
5
+ };
6
+ const isDefined = function (key, value) {
7
+ return value !== undefined && value !== '';
8
+ };
9
+ module.exports = { removeFalsy };
@@ -0,0 +1,2 @@
1
+ import execa = require("execa");
2
+ export { execa as runCommand };
@@ -0,0 +1,2 @@
1
+ const execa = require('execa');
2
+ module.exports = { runCommand: execa };
package/dist/zip.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export function zipFunction(relativeSrcPath: any, destFolder: any, { archiveFormat, basePath, config: inputConfig, featureFlags: inputFeatureFlags, pluginsModulesPath: defaultModulesPath, }?: {
2
+ archiveFormat?: string;
3
+ basePath: any;
4
+ config?: {};
5
+ featureFlags: any;
6
+ pluginsModulesPath: any;
7
+ }): Promise<Partial<any>>;
8
+ export function zipFunctions(relativeSrcFolders: any, destFolder: any, { archiveFormat, basePath, config, featureFlags: inputFeatureFlags, manifest, parallelLimit, }?: {
9
+ archiveFormat?: string;
10
+ basePath: any;
11
+ config?: {};
12
+ featureFlags: any;
13
+ manifest: any;
14
+ parallelLimit?: number;
15
+ }): Promise<Partial<any>[]>;
package/dist/zip.js ADDED
@@ -0,0 +1,116 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { resolve } = require('path');
11
+ const makeDir = require('make-dir');
12
+ const pMap = require('p-map');
13
+ const { getFlags } = require('./feature_flags');
14
+ const { createManifest } = require('./manifest');
15
+ const { getPluginsModulesPath } = require('./node_dependencies');
16
+ const { getFunctionsFromPaths } = require('./runtimes');
17
+ const { ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP } = require('./utils/consts');
18
+ const { listFunctionsDirectories, resolveFunctionsDirectories } = require('./utils/fs');
19
+ const { removeFalsy } = require('./utils/remove_falsy');
20
+ const DEFAULT_PARALLEL_LIMIT = 5;
21
+ const validateArchiveFormat = (archiveFormat) => {
22
+ if (![ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP].includes(archiveFormat)) {
23
+ throw new Error(`Invalid archive format: ${archiveFormat}`);
24
+ }
25
+ };
26
+ // Takes the result of zipping a function and formats it for output.
27
+ const formatZipResult = (result) => {
28
+ const { bundler, bundlerErrors, bundlerWarnings, config = {}, inputs, mainFile, name, nativeNodeModules, nodeModulesWithDynamicImports, path, runtime, } = result;
29
+ return removeFalsy({
30
+ bundler,
31
+ bundlerErrors,
32
+ bundlerWarnings,
33
+ config,
34
+ inputs,
35
+ mainFile,
36
+ name,
37
+ nativeNodeModules,
38
+ nodeModulesWithDynamicImports,
39
+ path,
40
+ runtime: runtime.name,
41
+ });
42
+ };
43
+ // Zip `srcFolder/*` (Node.js or Go files) to `destFolder/*.zip` so it can be
44
+ // used by AWS Lambda
45
+ const zipFunctions = function (relativeSrcFolders, destFolder, { archiveFormat = ARCHIVE_FORMAT_ZIP, basePath, config = {}, featureFlags: inputFeatureFlags, manifest, parallelLimit = DEFAULT_PARALLEL_LIMIT, } = {}) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ validateArchiveFormat(archiveFormat);
48
+ const featureFlags = getFlags(inputFeatureFlags);
49
+ const srcFolders = resolveFunctionsDirectories(relativeSrcFolders);
50
+ const [paths] = yield Promise.all([listFunctionsDirectories(srcFolders), makeDir(destFolder)]);
51
+ const [functions, pluginsModulesPath] = yield Promise.all([
52
+ getFunctionsFromPaths(paths, { config, dedupe: true, featureFlags }),
53
+ // All function directories should be part of the same project root, so we
54
+ // can get away with computing the plugins modules path from any of the
55
+ // source directories.
56
+ getPluginsModulesPath(srcFolders[0]),
57
+ ]);
58
+ const results = yield pMap(functions.values(), (func) => __awaiter(this, void 0, void 0, function* () {
59
+ const zipResult = yield func.runtime.zipFunction({
60
+ archiveFormat,
61
+ basePath,
62
+ config: func.config,
63
+ destFolder,
64
+ extension: func.extension,
65
+ filename: func.filename,
66
+ mainFile: func.mainFile,
67
+ name: func.name,
68
+ pluginsModulesPath,
69
+ runtime: func.runtime,
70
+ srcDir: func.srcDir,
71
+ srcPath: func.srcPath,
72
+ stat: func.stat,
73
+ featureFlags,
74
+ });
75
+ return Object.assign(Object.assign({}, zipResult), { mainFile: func.mainFile, name: func.name, runtime: func.runtime });
76
+ }), {
77
+ concurrency: parallelLimit,
78
+ });
79
+ const formattedResults = results.filter(Boolean).map(formatZipResult);
80
+ if (manifest !== undefined) {
81
+ yield createManifest({ functions: formattedResults, path: resolve(manifest) });
82
+ }
83
+ return formattedResults;
84
+ });
85
+ };
86
+ const zipFunction = function (relativeSrcPath, destFolder, { archiveFormat = ARCHIVE_FORMAT_ZIP, basePath, config: inputConfig = {}, featureFlags: inputFeatureFlags, pluginsModulesPath: defaultModulesPath, } = {}) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ validateArchiveFormat(archiveFormat);
89
+ const featureFlags = getFlags(inputFeatureFlags);
90
+ const srcPath = resolve(relativeSrcPath);
91
+ const functions = yield getFunctionsFromPaths([srcPath], { config: inputConfig, dedupe: true, featureFlags });
92
+ if (functions.size === 0) {
93
+ return;
94
+ }
95
+ const { config, extension, filename, mainFile, name, runtime, srcDir, stat } = functions.values().next().value;
96
+ const pluginsModulesPath = defaultModulesPath === undefined ? yield getPluginsModulesPath(srcPath) : defaultModulesPath;
97
+ yield makeDir(destFolder);
98
+ const zipResult = yield runtime.zipFunction({
99
+ archiveFormat,
100
+ basePath,
101
+ config,
102
+ srcPath,
103
+ destFolder,
104
+ mainFile,
105
+ filename,
106
+ extension,
107
+ srcDir,
108
+ stat,
109
+ runtime,
110
+ pluginsModulesPath,
111
+ featureFlags,
112
+ });
113
+ return formatZipResult(Object.assign(Object.assign({}, zipResult), { mainFile, name, runtime }));
114
+ });
115
+ };
116
+ module.exports = { zipFunction, zipFunctions };
@@ -0,0 +1,7 @@
1
+ export function zipBinary({ srcPath, destPath, filename, stat, runtime }: {
2
+ srcPath: any;
3
+ destPath: any;
4
+ filename: any;
5
+ stat: any;
6
+ runtime: any;
7
+ }): Promise<void>;
@@ -0,0 +1,20 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { startZip, addZipFile, addZipContent, endZip } = require('./archive');
11
+ // Zip a binary function file
12
+ const zipBinary = function ({ srcPath, destPath, filename, stat, runtime }) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const { archive, output } = startZip(destPath);
15
+ addZipFile(archive, srcPath, filename, stat);
16
+ addZipContent(archive, JSON.stringify({ runtime: runtime.name }), 'netlify-toolchain');
17
+ yield endZip(archive, output);
18
+ });
19
+ };
20
+ module.exports = { zipBinary };
@@ -0,0 +1,4 @@
1
+ export function zipNodeJs({ archiveFormat, ...options }: {
2
+ [x: string]: any;
3
+ archiveFormat: any;
4
+ }): Promise<string>;