@netlify/build 0.4.48 → 1.0.0-dl-test

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 (243) hide show
  1. package/bin.js +5 -0
  2. package/lib/core/bin.js +66 -0
  3. package/lib/core/build.js +392 -0
  4. package/lib/core/config.js +124 -0
  5. package/lib/core/constants.js +116 -0
  6. package/lib/core/dev.js +27 -0
  7. package/lib/core/dry.js +21 -0
  8. package/lib/core/feature_flags.js +17 -0
  9. package/lib/core/flags.js +206 -0
  10. package/lib/core/lingering.js +68 -0
  11. package/lib/core/main.js +114 -0
  12. package/lib/core/missing_side_file.js +17 -0
  13. package/lib/core/normalize_flags.js +62 -0
  14. package/lib/core/report_metrics.js +17 -0
  15. package/lib/core/severity.js +21 -0
  16. package/lib/core/types.js +8 -0
  17. package/lib/core/user_node_version.js +32 -0
  18. package/lib/env/changes.js +43 -0
  19. package/lib/env/main.js +14 -0
  20. package/lib/env/metadata.js +68 -0
  21. package/lib/error/api.js +37 -0
  22. package/lib/error/build.js +36 -0
  23. package/lib/error/cancel.js +7 -0
  24. package/lib/error/colors.js +9 -0
  25. package/lib/error/handle.js +44 -0
  26. package/lib/error/info.js +37 -0
  27. package/lib/error/monitor/location.js +16 -0
  28. package/lib/error/monitor/normalize.js +86 -0
  29. package/lib/error/monitor/print.js +20 -0
  30. package/lib/error/monitor/report.js +120 -0
  31. package/lib/error/monitor/start.js +61 -0
  32. package/lib/error/parse/clean_stack.js +70 -0
  33. package/lib/error/parse/location.js +50 -0
  34. package/lib/error/parse/normalize.js +24 -0
  35. package/lib/error/parse/parse.js +67 -0
  36. package/lib/error/parse/plugin.js +55 -0
  37. package/lib/error/parse/properties.js +16 -0
  38. package/lib/error/parse/serialize_log.js +34 -0
  39. package/lib/error/parse/serialize_status.js +18 -0
  40. package/lib/error/parse/stack.js +34 -0
  41. package/lib/error/report.js +29 -0
  42. package/lib/error/type.js +177 -0
  43. package/lib/install/functions.js +20 -0
  44. package/lib/install/local.js +45 -0
  45. package/lib/install/main.js +67 -0
  46. package/lib/install/missing.js +54 -0
  47. package/lib/log/colors.js +28 -0
  48. package/lib/log/description.js +21 -0
  49. package/lib/log/header.js +12 -0
  50. package/lib/log/header_func.js +13 -0
  51. package/lib/log/logger.js +140 -0
  52. package/lib/log/messages/compatibility.js +120 -0
  53. package/lib/log/messages/config.js +91 -0
  54. package/lib/log/messages/core.js +49 -0
  55. package/lib/log/messages/core_steps.js +75 -0
  56. package/lib/log/messages/dry.js +41 -0
  57. package/lib/log/messages/install.js +25 -0
  58. package/lib/log/messages/ipc.js +29 -0
  59. package/lib/log/messages/mutations.js +62 -0
  60. package/lib/log/messages/plugins.js +25 -0
  61. package/lib/log/messages/status.js +14 -0
  62. package/lib/log/messages/steps.js +18 -0
  63. package/lib/log/serialize.js +10 -0
  64. package/lib/log/stream.js +68 -0
  65. package/lib/log/theme.js +27 -0
  66. package/lib/plugins/child/diff.js +46 -0
  67. package/lib/plugins/child/error.js +26 -0
  68. package/lib/plugins/child/lazy.js +15 -0
  69. package/lib/plugins/child/load.js +22 -0
  70. package/lib/plugins/child/logic.js +57 -0
  71. package/lib/plugins/child/main.js +37 -0
  72. package/lib/plugins/child/run.js +19 -0
  73. package/lib/plugins/child/status.js +63 -0
  74. package/lib/plugins/child/typescript.js +28 -0
  75. package/lib/plugins/child/utils.js +42 -0
  76. package/lib/plugins/child/validate.js +31 -0
  77. package/lib/plugins/compatibility.js +104 -0
  78. package/lib/plugins/error.js +46 -0
  79. package/lib/plugins/events.js +12 -0
  80. package/lib/plugins/expected_version.js +81 -0
  81. package/lib/plugins/internal.js +10 -0
  82. package/lib/plugins/ipc.js +120 -0
  83. package/lib/plugins/list.js +73 -0
  84. package/lib/plugins/load.js +50 -0
  85. package/lib/plugins/manifest/check.js +85 -0
  86. package/lib/plugins/manifest/load.js +38 -0
  87. package/lib/plugins/manifest/main.js +19 -0
  88. package/lib/plugins/manifest/path.js +24 -0
  89. package/lib/plugins/manifest/validate.js +91 -0
  90. package/lib/plugins/node_version.js +35 -0
  91. package/lib/plugins/options.js +70 -0
  92. package/lib/plugins/pinned_version.js +83 -0
  93. package/lib/plugins/resolve.js +110 -0
  94. package/lib/plugins/spawn.js +55 -0
  95. package/lib/plugins_core/add.js +35 -0
  96. package/lib/plugins_core/build_command.js +50 -0
  97. package/lib/plugins_core/deploy/buildbot_client.js +87 -0
  98. package/lib/plugins_core/deploy/index.js +49 -0
  99. package/lib/plugins_core/deploy/manifest.yml +1 -0
  100. package/lib/plugins_core/edge_functions/index.js +106 -0
  101. package/lib/plugins_core/edge_functions/lib/error.js +17 -0
  102. package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +14 -0
  103. package/lib/plugins_core/functions/error.js +123 -0
  104. package/lib/plugins_core/functions/feature_flags.js +5 -0
  105. package/lib/plugins_core/functions/index.js +137 -0
  106. package/lib/plugins_core/functions/utils.js +45 -0
  107. package/lib/plugins_core/functions/zisi.js +64 -0
  108. package/lib/plugins_core/functions_install/index.js +10 -0
  109. package/lib/plugins_core/list.js +20 -0
  110. package/lib/plugins_core/save_artifacts/index.js +33 -0
  111. package/lib/report/statsd.js +56 -0
  112. package/lib/status/add.js +30 -0
  113. package/lib/status/colors.js +18 -0
  114. package/lib/status/load_error.js +10 -0
  115. package/lib/status/report.js +83 -0
  116. package/lib/status/success.js +14 -0
  117. package/lib/steps/core_step.js +62 -0
  118. package/lib/steps/error.js +65 -0
  119. package/lib/steps/get.js +44 -0
  120. package/lib/steps/plugin.js +55 -0
  121. package/lib/steps/return.js +27 -0
  122. package/lib/steps/run_core_steps.js +117 -0
  123. package/lib/steps/run_step.js +200 -0
  124. package/lib/steps/run_steps.js +102 -0
  125. package/lib/steps/update_config.js +66 -0
  126. package/lib/telemetry/main.js +94 -0
  127. package/lib/time/aggregate.js +109 -0
  128. package/lib/time/main.js +31 -0
  129. package/lib/time/measure.js +16 -0
  130. package/lib/time/report.js +30 -0
  131. package/lib/utils/errors.js +13 -0
  132. package/lib/utils/json.js +10 -0
  133. package/lib/utils/omit.js +3 -0
  134. package/lib/utils/package.js +24 -0
  135. package/lib/utils/remove_falsy.js +8 -0
  136. package/lib/utils/resolve.js +41 -0
  137. package/lib/utils/runtime.js +5 -0
  138. package/lib/utils/semver.js +28 -0
  139. package/package.json +119 -56
  140. package/types/config/build.d.ts +52 -0
  141. package/types/config/functions.d.ts +36 -0
  142. package/types/config/inputs.d.ts +7 -0
  143. package/types/config/netlify_config.d.ts +58 -0
  144. package/types/index.d.ts +7 -0
  145. package/types/netlify_event_handler.d.ts +29 -0
  146. package/types/netlify_plugin.d.ts +29 -0
  147. package/types/netlify_plugin_constants.d.ts +51 -0
  148. package/types/netlify_plugin_options.d.ts +23 -0
  149. package/types/options/index.d.ts +1 -0
  150. package/types/options/netlify_plugin_build_util.d.ts +7 -0
  151. package/types/options/netlify_plugin_cache_util.d.ts +39 -0
  152. package/types/options/netlify_plugin_functions_util.d.ts +32 -0
  153. package/types/options/netlify_plugin_git_util.d.ts +41 -0
  154. package/types/options/netlify_plugin_run_util.d.ts +24 -0
  155. package/types/options/netlify_plugin_status_util.d.ts +24 -0
  156. package/types/options/netlify_plugin_utils.d.ts +15 -0
  157. package/types/utils/json_value.d.ts +1 -0
  158. package/types/utils/many.d.ts +6 -0
  159. package/README.md +0 -3
  160. package/src/core/bin.js +0 -139
  161. package/src/core/commands.js +0 -304
  162. package/src/core/config.js +0 -130
  163. package/src/core/constants.js +0 -88
  164. package/src/core/dry.js +0 -23
  165. package/src/core/main.js +0 -196
  166. package/src/env/changes.js +0 -49
  167. package/src/env/git.js +0 -27
  168. package/src/env/main.js +0 -97
  169. package/src/env/metadata.js +0 -81
  170. package/src/error/api.js +0 -32
  171. package/src/error/build.js +0 -32
  172. package/src/error/cancel.js +0 -22
  173. package/src/error/colors.js +0 -13
  174. package/src/error/info.js +0 -12
  175. package/src/error/monitor/normalize.js +0 -50
  176. package/src/error/monitor/print.js +0 -43
  177. package/src/error/monitor/report.js +0 -140
  178. package/src/error/monitor/start.js +0 -50
  179. package/src/error/parse/clean_stack.js +0 -86
  180. package/src/error/parse/location.js +0 -50
  181. package/src/error/parse/parse.js +0 -87
  182. package/src/error/parse/plugin.js +0 -68
  183. package/src/error/parse/properties.js +0 -20
  184. package/src/error/parse/serialize_log.js +0 -46
  185. package/src/error/parse/serialize_status.js +0 -28
  186. package/src/error/parse/stack.js +0 -45
  187. package/src/error/process.js +0 -13
  188. package/src/error/type.js +0 -143
  189. package/src/install/functions.js +0 -52
  190. package/src/install/local.js +0 -65
  191. package/src/install/main.js +0 -103
  192. package/src/install/missing.js +0 -86
  193. package/src/log/colors.js +0 -59
  194. package/src/log/description.js +0 -38
  195. package/src/log/header.js +0 -19
  196. package/src/log/logger.js +0 -55
  197. package/src/log/main.js +0 -348
  198. package/src/log/old_version.js +0 -45
  199. package/src/log/serialize.js +0 -15
  200. package/src/log/stream.js +0 -15
  201. package/src/log/theme.js +0 -32
  202. package/src/log/timer.js +0 -28
  203. package/src/plugins/child/api.js +0 -59
  204. package/src/plugins/child/error.js +0 -39
  205. package/src/plugins/child/load.js +0 -40
  206. package/src/plugins/child/logic.js +0 -31
  207. package/src/plugins/child/main.js +0 -48
  208. package/src/plugins/child/normalize.js +0 -20
  209. package/src/plugins/child/run.js +0 -34
  210. package/src/plugins/child/status.js +0 -53
  211. package/src/plugins/child/utils.js +0 -43
  212. package/src/plugins/child/validate.js +0 -46
  213. package/src/plugins/error.js +0 -44
  214. package/src/plugins/events.js +0 -77
  215. package/src/plugins/ipc.js +0 -136
  216. package/src/plugins/load.js +0 -66
  217. package/src/plugins/manifest/check.js +0 -80
  218. package/src/plugins/manifest/load.js +0 -47
  219. package/src/plugins/manifest/main.js +0 -28
  220. package/src/plugins/manifest/path.js +0 -12
  221. package/src/plugins/manifest/validate.js +0 -136
  222. package/src/plugins/node_version.js +0 -74
  223. package/src/plugins/options.js +0 -78
  224. package/src/plugins/package.js +0 -17
  225. package/src/plugins/resolve.js +0 -159
  226. package/src/plugins/spawn.js +0 -82
  227. package/src/plugins_core/cache/manifest.yml +0 -1
  228. package/src/plugins_core/cache/plugin.js +0 -75
  229. package/src/plugins_core/functions/manifest.yml +0 -1
  230. package/src/plugins_core/functions/plugin.js +0 -37
  231. package/src/plugins_core/functions_install/plugin.js +0 -14
  232. package/src/plugins_core/main.js +0 -38
  233. package/src/status/add.js +0 -56
  234. package/src/status/colors.js +0 -25
  235. package/src/status/report.js +0 -57
  236. package/src/telemetry/complete.js +0 -49
  237. package/src/telemetry/request.js +0 -28
  238. package/src/telemetry/track.js +0 -33
  239. package/src/utils/omit.js +0 -8
  240. package/src/utils/polyfills.js +0 -3
  241. package/src/utils/remove_falsy.js +0 -12
  242. package/src/utils/resolve.js +0 -39
  243. /package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
@@ -0,0 +1,35 @@
1
+ import { listCorePlugins, isCorePlugin } from './list.js';
2
+ // Add core plugins and user plugins together.
3
+ // Do not allow user override of core plugins.
4
+ export const addCorePlugins = function ({ netlifyConfig: { plugins }, constants }) {
5
+ const corePlugins = listCorePlugins(constants);
6
+ const allCorePlugins = corePlugins
7
+ .map((corePlugin) => addCoreProperties(corePlugin, plugins))
8
+ .filter((corePlugin) => !isOptionalCore(corePlugin, plugins));
9
+ const userPlugins = plugins.filter(isUserPlugin);
10
+ const allPlugins = [...userPlugins, ...allCorePlugins];
11
+ const pluginsOptions = allPlugins.map(normalizePluginOptions);
12
+ return pluginsOptions;
13
+ };
14
+ const addCoreProperties = function (corePlugin, plugins) {
15
+ const inputs = getCorePluginInputs(corePlugin, plugins);
16
+ return { ...corePlugin, inputs, loadedFrom: 'core', origin: 'core' };
17
+ };
18
+ // Core plugins can get inputs too
19
+ const getCorePluginInputs = function (corePlugin, plugins) {
20
+ const configuredCorePlugin = plugins.find((plugin) => plugin.package === corePlugin.package);
21
+ if (configuredCorePlugin === undefined) {
22
+ return {};
23
+ }
24
+ return configuredCorePlugin.inputs;
25
+ };
26
+ // Optional core plugins requires user opt-in
27
+ const isOptionalCore = function (pluginA, plugins) {
28
+ return pluginA.optional && plugins.every((pluginB) => pluginB.package !== pluginA.package);
29
+ };
30
+ const isUserPlugin = function (plugin) {
31
+ return !isCorePlugin(plugin.package);
32
+ };
33
+ const normalizePluginOptions = function ({ package: packageName, pluginPath, pinned_version: pinnedVersion, loadedFrom, origin, inputs, }) {
34
+ return { packageName, pluginPath, pinnedVersion, loadedFrom, origin, inputs };
35
+ };
@@ -0,0 +1,50 @@
1
+ import { platform } from 'process';
2
+ import { execa } from 'execa';
3
+ import { addErrorInfo } from '../error/info.js';
4
+ import { getBuildCommandDescription } from '../log/description.js';
5
+ import { logBuildCommandStart } from '../log/messages/steps.js';
6
+ import { getBuildCommandStdio, handleBuildCommandOutput } from '../log/stream.js';
7
+ // Fire `build.command`
8
+ const coreStep = async function ({ configPath, buildDir, nodePath, childEnv, logs, netlifyConfig: { build: { command: buildCommand, commandOrigin: buildCommandOrigin }, }, }) {
9
+ logBuildCommandStart(logs, buildCommand);
10
+ const stdio = getBuildCommandStdio(logs);
11
+ const childProcess = execa(buildCommand, {
12
+ shell: SHELL,
13
+ cwd: buildDir,
14
+ preferLocal: true,
15
+ execPath: nodePath,
16
+ env: childEnv,
17
+ extendEnv: false,
18
+ stdio,
19
+ });
20
+ try {
21
+ const buildCommandOutput = await childProcess;
22
+ handleBuildCommandOutput(buildCommandOutput, logs);
23
+ return {};
24
+ }
25
+ catch (error) {
26
+ // In our test environment we use `stdio: 'pipe'` on the build command, meaning our `stdout/stderr` output are
27
+ // buffered and consequently added to `error.message`. To avoid this and end up with duplicated output in our
28
+ // logs/snapshots we need to rely on `error.shortMessage`.
29
+ error.message = error.shortMessage;
30
+ handleBuildCommandOutput(error, logs);
31
+ addErrorInfo(error, { type: 'buildCommand', location: { buildCommand, buildCommandOrigin, configPath } });
32
+ throw error;
33
+ }
34
+ };
35
+ // We use Bash on Unix and `cmd.exe` on Windows
36
+ const SHELL = platform === 'win32' ? true : 'bash';
37
+ const coreStepDescription = function ({ netlifyConfig: { build: { commandOrigin: buildCommandOrigin }, }, }) {
38
+ return getBuildCommandDescription(buildCommandOrigin);
39
+ };
40
+ const hasBuildCommand = function ({ netlifyConfig: { build: { command: buildCommand }, }, }) {
41
+ return buildCommand !== undefined && buildCommand !== '';
42
+ };
43
+ export const buildCommandCore = {
44
+ event: 'onBuild',
45
+ coreStep,
46
+ coreStepId: 'build_command',
47
+ coreStepName: 'build.command',
48
+ coreStepDescription,
49
+ condition: hasBuildCommand,
50
+ };
@@ -0,0 +1,87 @@
1
+ import net from 'net';
2
+ import { normalize, resolve, relative } from 'path';
3
+ import { promisify } from 'util';
4
+ import { pEvent } from 'p-event';
5
+ import { addErrorInfo } from '../../error/info.js';
6
+ import { runsAfterDeploy } from '../../plugins/events.js';
7
+ import { addAsyncErrorMessage } from '../../utils/errors.js';
8
+ export const createBuildbotClient = function (buildbotServerSocket) {
9
+ const connectionOpts = getConnectionOpts(buildbotServerSocket);
10
+ const client = net.createConnection(connectionOpts);
11
+ return client;
12
+ };
13
+ // Windows does not support Unix sockets well, so we also support `host:port`
14
+ const getConnectionOpts = function (buildbotServerSocket) {
15
+ if (!buildbotServerSocket.includes(':')) {
16
+ return { path: buildbotServerSocket };
17
+ }
18
+ const [host, port] = buildbotServerSocket.split(':');
19
+ return { host, port };
20
+ };
21
+ const eConnectBuildbotClient = async function (client) {
22
+ await pEvent(client, 'connect');
23
+ };
24
+ export const connectBuildbotClient = addAsyncErrorMessage(eConnectBuildbotClient, 'Could not connect to buildbot');
25
+ export const closeBuildbotClient = async function (client) {
26
+ if (client.destroyed) {
27
+ return;
28
+ }
29
+ await promisify(client.end.bind(client))();
30
+ };
31
+ const cWritePayload = async function (buildbotClient, payload) {
32
+ await promisify(buildbotClient.write.bind(buildbotClient))(JSON.stringify(payload));
33
+ };
34
+ const writePayload = addAsyncErrorMessage(cWritePayload, 'Could not send payload to buildbot');
35
+ const cGetNextParsedResponsePromise = async function (buildbotClient) {
36
+ const data = await pEvent(buildbotClient, 'data');
37
+ return JSON.parse(data);
38
+ };
39
+ const getNextParsedResponsePromise = addAsyncErrorMessage(cGetNextParsedResponsePromise, 'Invalid response from buildbot');
40
+ export const deploySiteWithBuildbotClient = async function ({ client, events, buildDir, repositoryRoot, constants }) {
41
+ const action = shouldWaitForPostProcessing(events) ? 'deploySiteAndAwaitLive' : 'deploySite';
42
+ const deployDir = getDeployDir({ buildDir, repositoryRoot, constants });
43
+ const payload = { action, deployDir };
44
+ const [{ succeeded, values: { error, error_type: errorType } = {} }] = await Promise.all([
45
+ getNextParsedResponsePromise(client),
46
+ writePayload(client, payload),
47
+ ]);
48
+ if (!succeeded) {
49
+ return handleDeployError(error, errorType);
50
+ }
51
+ };
52
+ // The file paths in the buildbot are relative to the repository root.
53
+ // However, the file paths in Build plugins, including `constants.PUBLISH_DIR`
54
+ // are relative to the build directory, which is different when there is a
55
+ // base directory. This converts it.
56
+ // We need to call `normalize()` in case the publish directory is the
57
+ // repository root, so `deployDir` is "." not ""
58
+ const getDeployDir = function ({ buildDir, repositoryRoot, constants: { PUBLISH_DIR } }) {
59
+ const absolutePublishDir = resolve(buildDir, PUBLISH_DIR);
60
+ const relativePublishDir = relative(repositoryRoot, absolutePublishDir);
61
+ const deployDir = normalize(relativePublishDir);
62
+ return deployDir;
63
+ };
64
+ // We distinguish between user errors and system errors during deploys
65
+ const handleDeployError = function (error, errorType) {
66
+ const errorIs422 = error !== undefined && error.code === '422';
67
+ const errMsg = errorIs422
68
+ ? `
69
+ File upload failed because of mismatched SHAs.
70
+ This can happen when files are changed after the deployment process has started but before they are uploaded.
71
+
72
+ Error: ${error}
73
+ `
74
+ : `Deploy did not succeed: ${error}`;
75
+ const errorA = new Error(errMsg);
76
+ const errorInfo = errorType === 'user' ? { type: 'resolveConfig' } : { type: 'coreStep', location: { coreStepName: 'Deploy site' } };
77
+ addErrorInfo(errorA, errorInfo);
78
+ throw errorA;
79
+ };
80
+ // We only wait for post-processing (last stage before site deploy) if the build
81
+ // has some plugins that do post-deploy logic
82
+ const shouldWaitForPostProcessing = function (events) {
83
+ return events.some(hasPostDeployLogic);
84
+ };
85
+ const hasPostDeployLogic = function (event) {
86
+ return runsAfterDeploy(event);
87
+ };
@@ -0,0 +1,49 @@
1
+ import { saveUpdatedConfig, restoreUpdatedConfig } from '../../core/config.js';
2
+ import { logDeploySuccess } from '../../log/messages/plugins.js';
3
+ import { createBuildbotClient, connectBuildbotClient, closeBuildbotClient, deploySiteWithBuildbotClient, } from './buildbot_client.js';
4
+ const coreStep = async function ({ buildDir, configPath, repositoryRoot, constants, buildbotServerSocket, events, logs, featureFlags, context, branch, configMutations, headersPath, redirectsPath, debug, saveConfig, }) {
5
+ const client = createBuildbotClient(buildbotServerSocket);
6
+ try {
7
+ await connectBuildbotClient(client);
8
+ await saveUpdatedConfig({
9
+ configMutations,
10
+ buildDir,
11
+ repositoryRoot,
12
+ configPath,
13
+ headersPath,
14
+ redirectsPath,
15
+ logs,
16
+ featureFlags,
17
+ context,
18
+ branch,
19
+ debug,
20
+ saveConfig,
21
+ });
22
+ await deploySiteWithBuildbotClient({ client, events, buildDir, repositoryRoot, constants });
23
+ await restoreUpdatedConfig({
24
+ configMutations,
25
+ buildDir,
26
+ repositoryRoot,
27
+ configPath,
28
+ headersPath,
29
+ redirectsPath,
30
+ saveConfig,
31
+ });
32
+ logDeploySuccess(logs);
33
+ return {};
34
+ }
35
+ finally {
36
+ await closeBuildbotClient(client);
37
+ }
38
+ };
39
+ export const shouldDeploy = function ({ buildbotServerSocket }) {
40
+ return buildbotServerSocket !== undefined;
41
+ };
42
+ export const deploySite = {
43
+ event: 'onPostBuild',
44
+ coreStep,
45
+ coreStepId: 'deploy_site',
46
+ coreStepName: 'Deploy site',
47
+ coreStepDescription: () => 'Deploy site',
48
+ condition: shouldDeploy,
49
+ };
@@ -0,0 +1 @@
1
+ name: '@netlify/plugin-deploy-core'
@@ -0,0 +1,106 @@
1
+ import { promises as fs } from 'fs';
2
+ import { dirname, join, resolve } from 'path';
3
+ import { bundle, find } from '@netlify/edge-bundler';
4
+ import { pathExists } from 'path-exists';
5
+ import { logFunctionsToBundle } from '../../log/messages/core_steps.js';
6
+ import { tagBundlingError } from './lib/error.js';
7
+ import { validateEdgeFunctionsManifest } from './validate_manifest/validate_edge_functions_manifest.js';
8
+ // TODO: Replace this with a custom cache directory.
9
+ const DENO_CLI_CACHE_DIRECTORY = '.netlify/plugins/deno-cli';
10
+ const IMPORT_MAP_FILENAME = 'edge-functions-import-map.json';
11
+ const coreStep = async function ({ buildDir, constants: { EDGE_FUNCTIONS_DIST: distDirectory, EDGE_FUNCTIONS_SRC: srcDirectory, INTERNAL_EDGE_FUNCTIONS_SRC: internalSrcDirectory, IS_LOCAL: isRunningLocally, }, debug, systemLog, featureFlags, logs, netlifyConfig, }) {
12
+ const { edge_functions: declarations = [] } = netlifyConfig;
13
+ const { deno_import_map: userDefinedImportMap } = netlifyConfig.functions['*'];
14
+ const distPath = resolve(buildDir, distDirectory);
15
+ const internalSrcPath = resolve(buildDir, internalSrcDirectory);
16
+ const distImportMapPath = join(dirname(internalSrcPath), IMPORT_MAP_FILENAME);
17
+ const srcPath = srcDirectory ? resolve(buildDir, srcDirectory) : undefined;
18
+ const sourcePaths = [internalSrcPath, srcPath].filter(Boolean);
19
+ logFunctions({ internalSrcDirectory, internalSrcPath, logs, srcDirectory, srcPath });
20
+ // If we're running in buildbot and the feature flag is enabled, we set the
21
+ // Deno cache dir to a directory that is persisted between builds.
22
+ const cacheDirectory = !isRunningLocally && featureFlags.edge_functions_cache_cli ? resolve(buildDir, DENO_CLI_CACHE_DIRECTORY) : undefined;
23
+ // Cleaning up the dist directory, in case it has any artifacts from previous
24
+ // builds.
25
+ try {
26
+ await fs.rm(distPath, { recursive: true });
27
+ }
28
+ catch {
29
+ // no-op
30
+ }
31
+ // Ensuring the dist directory actually exists before letting Edge Bundler
32
+ // write to it.
33
+ await fs.mkdir(distPath, { recursive: true });
34
+ try {
35
+ const { manifest } = await bundle(sourcePaths, distPath, declarations, {
36
+ basePath: buildDir,
37
+ cacheDirectory,
38
+ configPath: join(internalSrcPath, 'manifest.json'),
39
+ debug,
40
+ distImportMapPath,
41
+ featureFlags,
42
+ importMapPaths: [userDefinedImportMap],
43
+ systemLogger: featureFlags.edge_functions_system_logger ? systemLog : undefined,
44
+ internalSrcFolder: internalSrcPath,
45
+ });
46
+ const metrics = getMetrics(manifest);
47
+ systemLog('Edge Functions manifest:', manifest);
48
+ await validateEdgeFunctionsManifest(manifest, featureFlags);
49
+ return { metrics };
50
+ }
51
+ catch (error) {
52
+ tagBundlingError(error);
53
+ throw error;
54
+ }
55
+ };
56
+ const getMetrics = (manifest) => {
57
+ const numGenEfs = Object.values(manifest.function_config).filter((config) => config.generator).length;
58
+ const allRoutes = [...manifest.routes, ...manifest.post_cache_routes];
59
+ const totalEfs = [];
60
+ allRoutes.forEach((route) => {
61
+ if (!totalEfs.some((func) => func === route.function)) {
62
+ totalEfs.push(route.function);
63
+ }
64
+ });
65
+ const numUserEfs = totalEfs.length - numGenEfs;
66
+ return [
67
+ { type: 'increment', name: 'buildbot.build.functions', value: numGenEfs, tags: { type: 'edge:generated' } },
68
+ { type: 'increment', name: 'buildbot.build.functions', value: numUserEfs, tags: { type: 'edge:user' } },
69
+ ];
70
+ };
71
+ // We run this core step if at least one of the functions directories (the
72
+ // one configured by the user or the internal one) exists. We use a dynamic
73
+ // `condition` because the directories might be created by the build command
74
+ // or plugins.
75
+ const hasEdgeFunctionsDirectories = async function ({ buildDir, constants: { INTERNAL_EDGE_FUNCTIONS_SRC, EDGE_FUNCTIONS_SRC }, }) {
76
+ const hasFunctionsSrc = EDGE_FUNCTIONS_SRC !== undefined && EDGE_FUNCTIONS_SRC !== '';
77
+ if (hasFunctionsSrc) {
78
+ return true;
79
+ }
80
+ const internalFunctionsSrc = resolve(buildDir, INTERNAL_EDGE_FUNCTIONS_SRC);
81
+ return await pathExists(internalFunctionsSrc);
82
+ };
83
+ const logFunctions = async ({ internalSrcDirectory, internalSrcPath, logs, srcDirectory: userFunctionsSrc, srcPath, }) => {
84
+ const [userFunctionsSrcExists, userFunctions, internalFunctions] = await Promise.all([
85
+ srcPath ? pathExists(srcPath) : Promise.resolve(false),
86
+ srcPath ? find([srcPath]) : Promise.resolve([]),
87
+ find([internalSrcPath]),
88
+ ]);
89
+ logFunctionsToBundle({
90
+ logs,
91
+ userFunctions: userFunctions.map(({ name }) => name),
92
+ userFunctionsSrc,
93
+ userFunctionsSrcExists,
94
+ internalFunctions: internalFunctions.map(({ name }) => name),
95
+ internalFunctionsSrc: internalSrcDirectory,
96
+ type: 'Edge Functions',
97
+ });
98
+ };
99
+ export const bundleEdgeFunctions = {
100
+ event: 'onBuild',
101
+ coreStep,
102
+ coreStepId: 'edge_functions_bundling',
103
+ coreStepName: 'Edge Functions bundling',
104
+ coreStepDescription: () => 'Edge Functions bundling',
105
+ condition: hasEdgeFunctionsDirectories,
106
+ };
@@ -0,0 +1,17 @@
1
+ import { CUSTOM_ERROR_KEY, getErrorInfo, isBuildError } from '../../../error/info.js';
2
+ // If we have a custom error tagged with `functionsBundling` (which happens if
3
+ // there is an issue with user code), we tag it as coming from an edge function
4
+ // so that we can adjust the downstream error messages accordingly.
5
+ export const tagBundlingError = (error) => {
6
+ if (!isBuildError(error)) {
7
+ return;
8
+ }
9
+ const [errorInfo = {}] = getErrorInfo(error);
10
+ if (errorInfo.type !== 'functionsBundling') {
11
+ return;
12
+ }
13
+ error[CUSTOM_ERROR_KEY].location = {
14
+ ...error[CUSTOM_ERROR_KEY].location,
15
+ functionType: 'edge',
16
+ };
17
+ };
@@ -0,0 +1,14 @@
1
+ import { ManifestValidationError, validateManifest } from '@netlify/edge-bundler';
2
+ import { tagBundlingError } from '../lib/error.js';
3
+ export const validateEdgeFunctionsManifest = async function (manifest, featureFlags) {
4
+ try {
5
+ validateManifest(manifest, featureFlags);
6
+ }
7
+ catch (error) {
8
+ if (error instanceof ManifestValidationError) {
9
+ tagBundlingError(error);
10
+ }
11
+ throw error;
12
+ }
13
+ return {};
14
+ };
@@ -0,0 +1,123 @@
1
+ import readdirp from 'readdirp';
2
+ import { addErrorInfo } from '../../error/info.js';
3
+ const MODULE_NOT_FOUND_CODE = 'MODULE_NOT_FOUND';
4
+ const MODULE_NOT_FOUND_ESBUILD_REGEXP = /^Could not resolve ['"]([^'"]+)/;
5
+ const MODULE_NOT_FOUND_REGEXP = /Cannot find module ['"]([^'"]+)/;
6
+ // Handle errors coming from zip-it-and-ship-it
7
+ export const getZipError = async function (error, functionsSrc) {
8
+ const moduleNotFoundError = await getModuleNotFoundError(error, functionsSrc);
9
+ if (moduleNotFoundError) {
10
+ return moduleNotFoundError;
11
+ }
12
+ if (isPackageJsonError(error)) {
13
+ return getPackageJsonError(error);
14
+ }
15
+ return error;
16
+ };
17
+ const getModuleNotFoundError = async function (error, functionsSrc) {
18
+ const errorFromZisi = await getModuleNotFoundErrorFromZISI(error, functionsSrc);
19
+ if (errorFromZisi) {
20
+ return errorFromZisi;
21
+ }
22
+ const errorFromEsbuild = await getModuleNotFoundErrorFromEsbuild(error, functionsSrc);
23
+ if (errorFromEsbuild) {
24
+ return errorFromEsbuild;
25
+ }
26
+ };
27
+ const getModuleNotFoundErrorObject = async ({ error, functionsSrc, moduleNames }) => {
28
+ const message = await getModuleNotFoundMessage(functionsSrc, moduleNames);
29
+ error.message = `${message}\n\n${error.message}`;
30
+ addErrorInfo(error, { type: 'dependencies' });
31
+ return error;
32
+ };
33
+ const getModuleNotFoundMessage = async function (functionsSrc, moduleNames) {
34
+ if (moduleNames.length === 0 || !(await lacksNodeModules(functionsSrc))) {
35
+ return MODULE_NOT_FOUND_MESSAGE;
36
+ }
37
+ if (moduleNames.filter(Boolean).some(isLocalPath)) {
38
+ return PATH_NOT_FOUND_MESSAGE;
39
+ }
40
+ return getLocalInstallMessage(moduleNames);
41
+ };
42
+ const isLocalPath = function (moduleName) {
43
+ return moduleName.startsWith('.') || moduleName.startsWith('/');
44
+ };
45
+ const getModuleNotFoundErrorFromEsbuild = function (error, functionsSrc) {
46
+ const { errors = [] } = error;
47
+ const modulesNotFound = errors.reduce((modules, errorObject) => {
48
+ const match = errorObject.text.match(MODULE_NOT_FOUND_ESBUILD_REGEXP);
49
+ if (!match) {
50
+ return modules;
51
+ }
52
+ return [...modules, match[1]];
53
+ }, []);
54
+ if (modulesNotFound.length === 0) {
55
+ return;
56
+ }
57
+ return getModuleNotFoundErrorObject({ error, functionsSrc, moduleNames: modulesNotFound });
58
+ };
59
+ const getModuleNotFoundErrorFromZISI = function (error, functionsSrc) {
60
+ if (!(error instanceof Error && error.code === MODULE_NOT_FOUND_CODE)) {
61
+ return;
62
+ }
63
+ const moduleName = getModuleNameFromZISIError(error);
64
+ return getModuleNotFoundErrorObject({ error, functionsSrc, moduleNames: moduleName ? [moduleName] : [] });
65
+ };
66
+ // This error message always include the same words
67
+ const getModuleNameFromZISIError = function (error) {
68
+ if (typeof error.message !== 'string') {
69
+ return;
70
+ }
71
+ const result = MODULE_NOT_FOUND_REGEXP.exec(error.message);
72
+ if (result === null) {
73
+ return;
74
+ }
75
+ return result[1];
76
+ };
77
+ // Netlify Functions has a `package.json` but no `node_modules`
78
+ const lacksNodeModules = async function (functionsSrc) {
79
+ return (functionsSrc !== undefined &&
80
+ (await hasFunctionRootFile('package.json', functionsSrc)) &&
81
+ !(await hasFunctionRootFile('node_modules', functionsSrc)));
82
+ };
83
+ // Functions can be either files or directories, so we need to check on two
84
+ // depth levels
85
+ const hasFunctionRootFile = async function (filename, functionsSrc) {
86
+ const files = await readdirp.promise(functionsSrc, { depth: 1, fileFilter: filename });
87
+ return files.length !== 0;
88
+ };
89
+ const MODULE_NOT_FOUND_MESSAGE = `A Netlify Function failed to require one of its dependencies.
90
+ Please make sure it is present in the site's top-level "package.json".`;
91
+ const PATH_NOT_FOUND_MESSAGE = `A Netlify Function failed to require a local file.
92
+ Please make sure the file exists and its path is correctly spelled.`;
93
+ // A common mistake is to assume Netlify Functions dependencies are
94
+ // automatically installed. This checks for this pattern.
95
+ const getLocalInstallMessage = function (modules) {
96
+ const genericMessage = `
97
+
98
+ By default, dependencies inside a Netlify Function's "package.json" are not automatically installed.
99
+ There are several ways to fix this problem:
100
+ - Removing your Function's "package.json" and adding the dependencies to the project's top-level "package.json" instead. This is the fastest and safest solution.
101
+ - Running "npm install" or "yarn" inside your Netlify Function in your build command.
102
+ - Adding the following plugin to your "netlify.toml":
103
+
104
+ [[plugins]]
105
+ package = "@netlify/plugin-functions-install-core"
106
+ `;
107
+ if (modules.length === 1) {
108
+ return `A Netlify Function is using "${modules[0]}" but that dependency has not been installed yet.${genericMessage}`;
109
+ }
110
+ const moduleNames = modules.map((name) => `"${name}"`).join(', ');
111
+ return `A Netlify Function is using dependencies that have not been installed yet: ${moduleNames}${genericMessage}`;
112
+ };
113
+ // We need to load the site's `package.json` when bundling Functions. This is
114
+ // because `optionalDependencies` can make `import()` fail, but we don't want
115
+ // to error then. However, if the `package.json` is invalid, we fail the build.
116
+ const isPackageJsonError = function (error) {
117
+ return PACKAGE_JSON_ORIGINAL_MESSAGES.some((msg) => error.message.includes(msg));
118
+ };
119
+ const PACKAGE_JSON_ORIGINAL_MESSAGES = ['is invalid JSON', 'in JSON at position'];
120
+ const getPackageJsonError = function (error) {
121
+ addErrorInfo(error, { type: 'resolveConfig' });
122
+ return error;
123
+ };
@@ -0,0 +1,5 @@
1
+ export const getZisiFeatureFlags = (featureFlags) => ({
2
+ ...featureFlags,
3
+ parseWithEsbuild: featureFlags.buildbot_zisi_esbuild_parser,
4
+ traceWithNft: featureFlags.buildbot_zisi_trace_nft,
5
+ });
@@ -0,0 +1,137 @@
1
+ import { resolve } from 'path';
2
+ import { RUNTIME, zipFunctions } from '@netlify/zip-it-and-ship-it';
3
+ import { pathExists } from 'path-exists';
4
+ import { log } from '../../log/logger.js';
5
+ import { logBundleResults, logFunctionsNonExistingDir, logFunctionsToBundle } from '../../log/messages/core_steps.js';
6
+ import { getZipError } from './error.js';
7
+ import { getUserAndInternalFunctions, validateFunctionsSrc } from './utils.js';
8
+ import { getZisiParameters } from './zisi.js';
9
+ // Get a list of all unique bundlers in this run
10
+ const getBundlers = (results = []) =>
11
+ // using a Set to filter duplicates
12
+ new Set(results
13
+ .map((bundle) => (bundle.runtime === RUNTIME.JAVASCRIPT ? bundle.bundler : null))
14
+ .filter(Boolean));
15
+ const zipFunctionsAndLogResults = async ({ buildDir, childEnv, featureFlags, functionsConfig, functionsDist, functionsSrc, internalFunctionsSrc, isRunningLocally, logs, repositoryRoot, userNodeVersion, }) => {
16
+ const zisiParameters = getZisiParameters({
17
+ buildDir,
18
+ childEnv,
19
+ featureFlags,
20
+ functionsConfig,
21
+ functionsDist,
22
+ internalFunctionsSrc,
23
+ isRunningLocally,
24
+ repositoryRoot,
25
+ userNodeVersion,
26
+ });
27
+ try {
28
+ // Printing an empty line before bundling output.
29
+ log(logs, '');
30
+ const sourceDirectories = [internalFunctionsSrc, functionsSrc].filter(Boolean);
31
+ const results = await zipItAndShipIt.zipFunctions(sourceDirectories, functionsDist, zisiParameters);
32
+ const bundlers = Array.from(getBundlers(results));
33
+ logBundleResults({ logs, results });
34
+ return { bundlers };
35
+ }
36
+ catch (error) {
37
+ throw await getZipError(error, functionsSrc);
38
+ }
39
+ };
40
+ // Plugin to package Netlify functions with @netlify/zip-it-and-ship-it
41
+ const coreStep = async function ({ childEnv, constants: { INTERNAL_FUNCTIONS_SRC: relativeInternalFunctionsSrc, IS_LOCAL: isRunningLocally, FUNCTIONS_SRC: relativeFunctionsSrc, FUNCTIONS_DIST: relativeFunctionsDist, }, buildDir, logs, netlifyConfig, featureFlags, repositoryRoot, userNodeVersion, }) {
42
+ const functionsSrc = relativeFunctionsSrc === undefined ? undefined : resolve(buildDir, relativeFunctionsSrc);
43
+ const functionsDist = resolve(buildDir, relativeFunctionsDist);
44
+ const internalFunctionsSrc = resolve(buildDir, relativeInternalFunctionsSrc);
45
+ const internalFunctionsSrcExists = await pathExists(internalFunctionsSrc);
46
+ const functionsSrcExists = await validateFunctionsSrc({ functionsSrc, relativeFunctionsSrc });
47
+ const [userFunctions = [], internalFunctions = []] = await getUserAndInternalFunctions({
48
+ featureFlags,
49
+ functionsSrc,
50
+ functionsSrcExists,
51
+ internalFunctionsSrc,
52
+ internalFunctionsSrcExists,
53
+ });
54
+ if (functionsSrc && !functionsSrcExists) {
55
+ logFunctionsNonExistingDir(logs, relativeFunctionsSrc);
56
+ if (internalFunctions.length !== 0) {
57
+ log(logs, '');
58
+ }
59
+ }
60
+ logFunctionsToBundle({
61
+ logs,
62
+ userFunctions,
63
+ userFunctionsSrc: relativeFunctionsSrc,
64
+ userFunctionsSrcExists: functionsSrcExists,
65
+ internalFunctions,
66
+ internalFunctionsSrc: relativeInternalFunctionsSrc,
67
+ });
68
+ if (userFunctions.length === 0 && internalFunctions.length === 0) {
69
+ return {};
70
+ }
71
+ const { bundlers } = await zipFunctionsAndLogResults({
72
+ buildDir,
73
+ childEnv,
74
+ featureFlags,
75
+ functionsConfig: netlifyConfig.functions,
76
+ functionsDist,
77
+ functionsSrc,
78
+ internalFunctionsSrc,
79
+ isRunningLocally,
80
+ logs,
81
+ repositoryRoot,
82
+ userNodeVersion,
83
+ });
84
+ const metrics = getMetrics(internalFunctions, userFunctions);
85
+ return {
86
+ tags: {
87
+ bundler: bundlers,
88
+ },
89
+ metrics,
90
+ };
91
+ };
92
+ // We run this core step if at least one of the functions directories (the
93
+ // one configured by the user or the internal one) exists. We use a dynamic
94
+ // `condition` because the directories might be created by the build command
95
+ // or plugins.
96
+ const hasFunctionsDirectories = async function ({ buildDir, constants: { INTERNAL_FUNCTIONS_SRC, FUNCTIONS_SRC } }) {
97
+ const hasFunctionsSrc = FUNCTIONS_SRC !== undefined && FUNCTIONS_SRC !== '';
98
+ if (hasFunctionsSrc) {
99
+ return true;
100
+ }
101
+ const internalFunctionsSrc = resolve(buildDir, INTERNAL_FUNCTIONS_SRC);
102
+ return await pathExists(internalFunctionsSrc);
103
+ };
104
+ export const bundleFunctions = {
105
+ event: 'onBuild',
106
+ coreStep,
107
+ coreStepId: 'functions_bundling',
108
+ coreStepName: 'Functions bundling',
109
+ coreStepDescription: () => 'Functions bundling',
110
+ condition: hasFunctionsDirectories,
111
+ };
112
+ // Named imports with ES modules cannot be mocked (unlike CommonJS) because
113
+ // they are bound at load time.
114
+ // However, some of our tests are asserting which arguments are passed to
115
+ // `zip-it-and-ship-it` methods. Therefore, we need to use an intermediary
116
+ // function and export them so tests can use it.
117
+ export const zipItAndShipIt = {
118
+ async zipFunctions(...args) {
119
+ return await zipFunctions(...args);
120
+ },
121
+ };
122
+ const getMetrics = (internalFunctions, userFunctions) => {
123
+ return [
124
+ {
125
+ type: 'increment',
126
+ name: 'buildbot.build.functions',
127
+ value: internalFunctions.length,
128
+ tags: { type: 'lambda:generated' },
129
+ },
130
+ {
131
+ type: 'increment',
132
+ name: 'buildbot.build.functions',
133
+ value: userFunctions.length,
134
+ tags: { type: 'lambda:user' },
135
+ },
136
+ ];
137
+ };