@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
@@ -1,59 +0,0 @@
1
- const esbuild = require('@netlify/esbuild')
2
- const isBuiltinModule = require('is-builtin-module')
3
- const { tmpName } = require('tmp-promise')
4
-
5
- const { safeUnlink } = require('../../utils/fs')
6
-
7
- // Maximum number of log messages that an esbuild instance will produce. This
8
- // limit is important to avoid out-of-memory errors due to too much data being
9
- // sent in the Go<>Node IPC channel.
10
- const ESBUILD_LOG_LIMIT = 10
11
-
12
- const getListImportsPlugin = ({ imports, path }) => ({
13
- name: 'list-imports',
14
- setup(build) {
15
- build.onResolve({ filter: /.*/ }, (args) => {
16
- const isEntryPoint = args.path === path
17
- const isImport = !isEntryPoint && !isBuiltinModule(args.path)
18
-
19
- if (isImport) {
20
- imports.add(args.path)
21
- }
22
-
23
- return {
24
- namespace: 'list-imports',
25
- external: isImport,
26
- }
27
- })
28
- },
29
- })
30
-
31
- const listImports = async ({ path }) => {
32
- // We're not interested in the output that esbuild generates, we're just
33
- // using it for its parsing capabilities in order to find import/require
34
- // statements. However, if we don't give esbuild a path in `outfile`, it
35
- // will pipe the output to stdout, which we also don't want. So we create
36
- // a temporary file to serve as the esbuild output and then get rid of it
37
- // when we're done.
38
- const targetPath = await tmpName()
39
- const imports = new Set()
40
-
41
- try {
42
- await esbuild.build({
43
- bundle: true,
44
- entryPoints: [path],
45
- logLevel: 'error',
46
- logLimit: ESBUILD_LOG_LIMIT,
47
- outfile: targetPath,
48
- platform: 'node',
49
- plugins: [getListImportsPlugin({ imports, path })],
50
- target: 'esnext',
51
- })
52
- } finally {
53
- await safeUnlink(targetPath)
54
- }
55
-
56
- return [...imports]
57
- }
58
-
59
- module.exports = { listImports }
@@ -1,21 +0,0 @@
1
- const { extname } = require('path')
2
-
3
- const markerModules = ['bindings', 'nan', 'node-gyp', 'node-gyp-build', 'node-pre-gyp', 'prebuild']
4
-
5
- const isNativeModule = ({ binary, dependencies = {}, devDependencies = {}, files = [], gypfile }) => {
6
- if (binary || gypfile) {
7
- return true
8
- }
9
-
10
- const hasMarkerModule = markerModules.some((marker) => dependencies[marker] || devDependencies[marker])
11
-
12
- if (hasMarkerModule) {
13
- return true
14
- }
15
-
16
- const hasBinaryFile = files.some((path) => !path.startsWith('!') && extname(path) === '.node')
17
-
18
- return hasBinaryFile
19
- }
20
-
21
- module.exports = { isNativeModule }
@@ -1,77 +0,0 @@
1
- const path = require('path')
2
-
3
- const readPackageJson = require('read-package-json-fast')
4
-
5
- const { isNativeModule } = require('./detector')
6
-
7
- // Filters out relative or absolute file paths.
8
- const packageFilter = /^([^./]*)$/
9
-
10
- // Filters valid package names and extracts the base directory.
11
- const packageName = /^([^@][^/]*|@[^/]*\/[^/]+)(?:\/|$)/
12
-
13
- const findNativeModule = (packageJsonPath, cache) => {
14
- if (cache[packageJsonPath] === undefined) {
15
- // eslint-disable-next-line no-param-reassign, promise/prefer-await-to-then
16
- cache[packageJsonPath] = readPackageJson(packageJsonPath).then(
17
- (data) => [Boolean(isNativeModule(data), data), data],
18
- () => [],
19
- )
20
- }
21
-
22
- return cache[packageJsonPath]
23
- }
24
-
25
- const getNativeModulesPlugin = (externalizedModules) => ({
26
- name: 'external-native-modules',
27
- setup(build) {
28
- const cache = {}
29
-
30
- // eslint-disable-next-line complexity, max-statements
31
- build.onResolve({ filter: packageFilter }, async (args) => {
32
- const package = packageName.exec(args.path)
33
-
34
- if (!package) return
35
-
36
- let directory = args.resolveDir
37
-
38
- while (true) {
39
- if (path.basename(directory) !== 'node_modules') {
40
- const modulePath = path.join(directory, 'node_modules', package[1])
41
- const packageJsonPath = path.join(modulePath, 'package.json')
42
- // eslint-disable-next-line no-await-in-loop
43
- const [isNative, packageJsonData] = await findNativeModule(packageJsonPath, cache)
44
-
45
- // eslint-disable-next-line max-depth
46
- if (isNative === true) {
47
- // eslint-disable-next-line max-depth
48
- if (externalizedModules[args.path] === undefined) {
49
- // eslint-disable-next-line no-param-reassign
50
- externalizedModules[args.path] = {}
51
- }
52
-
53
- // eslint-disable-next-line no-param-reassign
54
- externalizedModules[args.path][modulePath] = packageJsonData.version
55
-
56
- return { path: args.path, external: true }
57
- }
58
-
59
- // eslint-disable-next-line max-depth
60
- if (isNative === false) {
61
- return
62
- }
63
- }
64
-
65
- const parentDirectory = path.dirname(directory)
66
-
67
- if (parentDirectory === directory) {
68
- break
69
- }
70
-
71
- directory = parentDirectory
72
- }
73
- })
74
- },
75
- })
76
-
77
- module.exports = { getNativeModulesPlugin }
@@ -1,99 +0,0 @@
1
- const { normalize } = require('path')
2
- const { promisify } = require('util')
3
-
4
- const glob = require('glob')
5
-
6
- const pGlob = promisify(glob)
7
-
8
- const { getDependencyNamesAndPathsForDependencies, listFilesUsingLegacyBundler } = require('../../node_dependencies')
9
- const { JS_BUNDLER_ZISI } = require('../../utils/consts')
10
-
11
- const getPathsOfIncludedFiles = async (includedFiles, basePath) => {
12
- // Some of the globs in `includedFiles` might be exclusion patterns, which
13
- // means paths that should NOT be included in the bundle. We need to treat
14
- // these differently, so we iterate on the array and put those paths in a
15
- // `exclude` array and the rest of the paths in an `include` array.
16
- const { include, exclude } = includedFiles.reduce(
17
- (acc, path) => {
18
- if (path.startsWith('!')) {
19
- return {
20
- ...acc,
21
- exclude: [...acc.exclude, path.slice(1)],
22
- }
23
- }
24
-
25
- return {
26
- ...acc,
27
- include: [...acc.include, path],
28
- }
29
- },
30
- { include: [], exclude: [] },
31
- )
32
- const pathGroups = await Promise.all(
33
- include.map((expression) => pGlob(expression, { absolute: true, cwd: basePath, ignore: exclude, nodir: true })),
34
- )
35
-
36
- // `pathGroups` is an array containing the paths for each expression in the
37
- // `include` array. We flatten it into a single dimension.
38
- const paths = pathGroups.flat()
39
- const normalizedPaths = paths.map(normalize)
40
-
41
- return [...new Set(normalizedPaths)]
42
- }
43
-
44
- const getSrcFiles = async function ({ config, ...parameters }) {
45
- const { paths } = await getSrcFilesAndExternalModules({
46
- ...parameters,
47
- externalNodeModules: config.externalNodeModules,
48
- })
49
-
50
- return paths
51
- }
52
-
53
- const getSrcFilesAndExternalModules = async function ({
54
- bundler,
55
- externalNodeModules = [],
56
- featureFlags,
57
- includedFiles = [],
58
- includedFilesBasePath,
59
- mainFile,
60
- pluginsModulesPath,
61
- srcDir,
62
- srcPath,
63
- stat,
64
- }) {
65
- const includedFilePaths = await getPathsOfIncludedFiles(includedFiles, includedFilesBasePath)
66
-
67
- if (bundler === JS_BUNDLER_ZISI) {
68
- const paths = await listFilesUsingLegacyBundler({
69
- featureFlags,
70
- srcPath,
71
- mainFile,
72
- srcDir,
73
- stat,
74
- pluginsModulesPath,
75
- })
76
-
77
- return {
78
- moduleNames: [],
79
- paths: [...paths, ...includedFilePaths],
80
- }
81
- }
82
-
83
- if (externalNodeModules.length !== 0) {
84
- const { moduleNames, paths } = await getDependencyNamesAndPathsForDependencies({
85
- dependencies: externalNodeModules,
86
- basedir: srcDir,
87
- pluginsModulesPath,
88
- })
89
-
90
- return { moduleNames, paths: [...paths, ...includedFilePaths, mainFile] }
91
- }
92
-
93
- return {
94
- moduleNames: externalNodeModules,
95
- paths: [mainFile, ...includedFilePaths],
96
- }
97
- }
98
-
99
- module.exports = { getSrcFiles, getSrcFilesAndExternalModules }
@@ -1,119 +0,0 @@
1
- const { dirname, normalize } = require('path')
2
-
3
- const { getExternalAndIgnoredModulesFromSpecialCases } = require('../../node_dependencies')
4
- const { JS_BUNDLER_ESBUILD } = require('../../utils/consts')
5
- const { getPathWithExtension } = require('../../utils/fs')
6
- const { zipNodeJs } = require('../../zip_node')
7
-
8
- const { getBasePath } = require('./base_path')
9
- const { bundleJsFile } = require('./bundler')
10
- const { getSrcFilesAndExternalModules } = require('./src_files')
11
-
12
- const getFunctionBasePath = ({ basePathFromConfig, mainFile, supportingSrcFiles }) => {
13
- // If there is a base path defined in the config, we use that.
14
- if (basePathFromConfig !== undefined) {
15
- return basePathFromConfig
16
- }
17
-
18
- // If not, the base path is the common path prefix between all the supporting
19
- // files and the main file.
20
- const dirnames = [...supportingSrcFiles, mainFile].map((filePath) => normalize(dirname(filePath)))
21
-
22
- return getBasePath(dirnames)
23
- }
24
-
25
- // Convenience method for retrieving external and ignored modules from
26
- // different places and merging them together.
27
- const getExternalAndIgnoredModules = async ({ config, srcDir }) => {
28
- const { externalNodeModules: externalModulesFromConfig = [], ignoredNodeModules: ignoredModulesFromConfig = [] } =
29
- config
30
- const { externalModules: externalModulesFromSpecialCases, ignoredModules: ignoredModulesFromSpecialCases } =
31
- await getExternalAndIgnoredModulesFromSpecialCases({ srcDir })
32
- const externalModules = [...new Set([...externalModulesFromConfig, ...externalModulesFromSpecialCases])]
33
- const ignoredModules = [...ignoredModulesFromConfig, ...ignoredModulesFromSpecialCases]
34
-
35
- return { externalModules, ignoredModules }
36
- }
37
-
38
- const zipEsbuild = async ({
39
- archiveFormat,
40
- basePath,
41
- config = {},
42
- destFolder,
43
- extension,
44
- filename,
45
- mainFile,
46
- name,
47
- pluginsModulesPath,
48
- srcDir,
49
- srcPath,
50
- stat,
51
- }) => {
52
- const { externalModules, ignoredModules } = await getExternalAndIgnoredModules({ config, srcDir })
53
- const {
54
- additionalPaths,
55
- bundlePaths,
56
- cleanTempFiles,
57
- inputs,
58
- nativeNodeModules = {},
59
- nodeModulesWithDynamicImports,
60
- warnings,
61
- } = await bundleJsFile({
62
- additionalModulePaths: pluginsModulesPath ? [pluginsModulesPath] : [],
63
- basePath,
64
- config,
65
- destFilename: filename,
66
- externalModules,
67
- ignoredModules,
68
- name,
69
- srcDir,
70
- srcFile: mainFile,
71
- })
72
- const bundlerWarnings = warnings.length === 0 ? undefined : warnings
73
- const { paths: srcFiles } = await getSrcFilesAndExternalModules({
74
- externalNodeModules: [...externalModules, ...Object.keys(nativeNodeModules)],
75
- bundler: JS_BUNDLER_ESBUILD,
76
- includedFiles: [...(config.includedFiles || []), ...additionalPaths],
77
- includedFilesBasePath: config.includedFilesBasePath || basePath,
78
- mainFile,
79
- srcPath,
80
- srcDir,
81
- pluginsModulesPath,
82
- stat,
83
- })
84
-
85
- // We want to remove `mainFile` from `srcFiles` because it represents the
86
- // path of the original, pre-bundling function file. We'll add the actual
87
- // bundled file further below.
88
- const supportingSrcFiles = srcFiles.filter((path) => path !== mainFile)
89
- const normalizedMainFile = getPathWithExtension(mainFile, '.js')
90
- const functionBasePath = getFunctionBasePath({ basePathFromConfig: basePath, mainFile, supportingSrcFiles })
91
-
92
- try {
93
- const path = await zipNodeJs({
94
- aliases: bundlePaths,
95
- archiveFormat,
96
- basePath: functionBasePath,
97
- destFolder,
98
- extension,
99
- filename,
100
- mainFile: normalizedMainFile,
101
- pluginsModulesPath,
102
- srcFiles: [...supportingSrcFiles, ...bundlePaths.keys()],
103
- })
104
-
105
- return {
106
- bundler: JS_BUNDLER_ESBUILD,
107
- bundlerWarnings,
108
- config,
109
- inputs,
110
- nativeNodeModules,
111
- nodeModulesWithDynamicImports,
112
- path,
113
- }
114
- } finally {
115
- await cleanTempFiles()
116
- }
117
- }
118
-
119
- module.exports = { zipEsbuild }
@@ -1,50 +0,0 @@
1
- const { dirname, normalize } = require('path')
2
-
3
- const { JS_BUNDLER_ZISI } = require('../../utils/consts')
4
- const { zipNodeJs } = require('../../zip_node')
5
-
6
- const { getBasePath } = require('./base_path')
7
- const { getSrcFilesAndExternalModules } = require('./src_files')
8
-
9
- const zipZisi = async ({
10
- archiveFormat,
11
- basePath,
12
- config,
13
- destFolder,
14
- extension,
15
- featureFlags,
16
- filename,
17
- mainFile,
18
- pluginsModulesPath,
19
- srcDir,
20
- srcPath,
21
- stat,
22
- }) => {
23
- const { paths: srcFiles } = await getSrcFilesAndExternalModules({
24
- bundler: JS_BUNDLER_ZISI,
25
- extension,
26
- featureFlags,
27
- includedFiles: config.includedFiles,
28
- includedFilesBasePath: config.includedFilesBasePath || basePath,
29
- mainFile,
30
- pluginsModulesPath,
31
- srcDir,
32
- srcPath,
33
- stat,
34
- })
35
- const dirnames = srcFiles.map((filePath) => normalize(dirname(filePath)))
36
- const path = await zipNodeJs({
37
- archiveFormat,
38
- basePath: getBasePath(dirnames),
39
- destFolder,
40
- extension,
41
- filename,
42
- mainFile,
43
- pluginsModulesPath,
44
- srcFiles,
45
- })
46
-
47
- return { bundler: JS_BUNDLER_ZISI, config, inputs: srcFiles, path }
48
- }
49
-
50
- module.exports = { zipZisi }
@@ -1,114 +0,0 @@
1
- const { readFile } = require('fs')
2
- const { basename, join } = require('path')
3
- const { promisify } = require('util')
4
-
5
- const pReadFile = promisify(readFile)
6
-
7
- const makeDir = require('make-dir')
8
- const tmp = require('tmp-promise')
9
- const toml = require('toml')
10
-
11
- const { RUNTIME_RUST } = require('../../utils/consts')
12
- const { lstat } = require('../../utils/fs')
13
- const { runCommand } = require('../../utils/shell')
14
-
15
- const { BUILD_TARGET, MANIFEST_NAME } = require('./constants')
16
-
17
- const build = async ({ config, name, srcDir }) => {
18
- const functionName = basename(srcDir)
19
-
20
- try {
21
- await installBuildTarget()
22
- } catch (error) {
23
- error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } }
24
-
25
- throw error
26
- }
27
-
28
- const targetDirectory = await getTargetDirectory({ config, name })
29
-
30
- await cargoBuild({ functionName, srcDir, targetDirectory })
31
-
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 = await pReadFile(join(srcDir, MANIFEST_NAME))
37
- const { package } = toml.parse(manifest)
38
- const binaryPath = join(targetDirectory, BUILD_TARGET, 'release', package.name)
39
- const stat = await lstat(binaryPath)
40
-
41
- return {
42
- path: binaryPath,
43
- stat,
44
- }
45
- }
46
-
47
- const cargoBuild = async ({ functionName, srcDir, targetDirectory }) => {
48
- try {
49
- await runCommand('cargo', ['build', '--target', BUILD_TARGET, '--release'], {
50
- cwd: srcDir,
51
- env: {
52
- CARGO_TARGET_DIR: targetDirectory,
53
- },
54
- })
55
- } catch (error) {
56
- const hasToolchain = await checkRustToolchain()
57
-
58
- if (hasToolchain) {
59
- console.error(`Could not compile Rust function ${functionName}:\n`)
60
- } else {
61
- error.message =
62
- 'There is no Rust toolchain installed. Visit https://ntl.fyi/missing-rust-toolchain for more information.'
63
- }
64
-
65
- error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } }
66
-
67
- throw error
68
- }
69
- }
70
-
71
- const checkRustToolchain = async () => {
72
- try {
73
- await runCommand('cargo', ['-V'])
74
-
75
- return true
76
- } catch (_) {
77
- return false
78
- }
79
- }
80
-
81
- // Returns the path of the Cargo target directory.
82
- const getTargetDirectory = async ({ config, name }) => {
83
- const { rustTargetDirectory } = config
84
-
85
- // If the config includes a `rustTargetDirectory` path, we'll use that.
86
- if (rustTargetDirectory) {
87
- // We replace the [name] placeholder with the name of the function.
88
- const path = rustTargetDirectory.replace(/\[name]/g, name)
89
-
90
- await makeDir(path)
91
-
92
- return path
93
- }
94
-
95
- // If the directory hasn't been configured, we'll use a temporary directory.
96
- const { path } = await tmp.dir()
97
-
98
- return path
99
- }
100
-
101
- let buildTargetInstallation
102
-
103
- // Installs the build target defined in `BUILD_TARGET`. The Promise is saved to
104
- // `buildTargetInstallation` so that we run the command just once for multiple
105
- // Rust functions.
106
- const installBuildTarget = () => {
107
- if (buildTargetInstallation === undefined) {
108
- buildTargetInstallation = runCommand('rustup', ['target', 'add', BUILD_TARGET])
109
- }
110
-
111
- return buildTargetInstallation
112
- }
113
-
114
- module.exports = { build }
@@ -1,4 +0,0 @@
1
- const BUILD_TARGET = 'x86_64-unknown-linux-musl'
2
- const MANIFEST_NAME = 'Cargo.toml'
3
-
4
- module.exports = { BUILD_TARGET, MANIFEST_NAME }
@@ -1,112 +0,0 @@
1
- const { join, extname, dirname, basename } = require('path')
2
-
3
- const { RUNTIME_RUST } = require('../../utils/consts')
4
- const { cachedLstat, cachedReaddir } = require('../../utils/fs')
5
- const { zipBinary } = require('../../zip_binary')
6
- const { detectBinaryRuntime } = require('../detect_runtime')
7
-
8
- const { build } = require('./builder')
9
- const { MANIFEST_NAME } = require('./constants')
10
-
11
- const detectRustFunction = async ({ fsCache, path }) => {
12
- const stat = await cachedLstat(fsCache, path)
13
-
14
- if (!stat.isDirectory()) {
15
- return
16
- }
17
-
18
- const files = await cachedReaddir(fsCache, path)
19
- const hasCargoManifest = files.includes(MANIFEST_NAME)
20
-
21
- if (!hasCargoManifest) {
22
- return
23
- }
24
-
25
- const mainFilePath = join(path, 'src', 'main.rs')
26
-
27
- try {
28
- const mainFile = await cachedLstat(fsCache, mainFilePath)
29
-
30
- if (mainFile.isFile()) {
31
- return mainFilePath
32
- }
33
- } catch (_) {
34
- // no-op
35
- }
36
- }
37
-
38
- const findFunctionsInPaths = async function ({ featureFlags, fsCache, paths }) {
39
- const functions = await Promise.all(
40
- paths.map(async (path) => {
41
- const runtime = await detectBinaryRuntime({ fsCache, path })
42
-
43
- if (runtime === RUNTIME_RUST) {
44
- return processBinary({ fsCache, path })
45
- }
46
-
47
- if (featureFlags.buildRustSource !== true) {
48
- return
49
- }
50
-
51
- const rustSourceFile = await detectRustFunction({ fsCache, path })
52
-
53
- if (rustSourceFile) {
54
- return processSource({ fsCache, mainFile: rustSourceFile, path })
55
- }
56
- }),
57
- )
58
-
59
- return functions.filter(Boolean)
60
- }
61
-
62
- const processBinary = async ({ fsCache, path }) => {
63
- const stat = await cachedLstat(fsCache, path)
64
- const name = basename(path, extname(path))
65
-
66
- return {
67
- mainFile: path,
68
- name,
69
- srcDir: dirname(path),
70
- srcPath: path,
71
- stat,
72
- }
73
- }
74
-
75
- const processSource = ({ mainFile, path }) => {
76
- const functionName = basename(path)
77
-
78
- return {
79
- mainFile,
80
- name: functionName,
81
- srcDir: path,
82
- srcPath: path,
83
- }
84
- }
85
-
86
- // The name of the binary inside the zip file must always be `bootstrap`
87
- // because they include the Lambda runtime, and that's the name that AWS
88
- // expects for those kind of functions.
89
- const zipFunction = async function ({ config, destFolder, filename, mainFile, runtime, srcDir, srcPath, stat }) {
90
- const destPath = join(destFolder, `${filename}.zip`)
91
- const isSource = extname(mainFile) === '.rs'
92
- const zipOptions = {
93
- destPath,
94
- filename: 'bootstrap',
95
- runtime,
96
- }
97
-
98
- // If we're building from source, we first need to build the source and zip
99
- // the resulting binary. Otherwise, we're dealing with a binary so we zip it
100
- // directly.
101
- if (isSource) {
102
- const { path: binaryPath, stat: binaryStat } = await build({ config, name: filename, srcDir })
103
-
104
- await zipBinary({ ...zipOptions, srcPath: binaryPath, stat: binaryStat })
105
- } else {
106
- await zipBinary({ ...zipOptions, srcPath, stat })
107
- }
108
-
109
- return { config, path: destPath }
110
- }
111
-
112
- module.exports = { findFunctionsInPaths, name: RUNTIME_RUST, zipFunction }
@@ -1,10 +0,0 @@
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
- }