@netlify/build 28.0.0-beta → 28.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +5 -0
- package/lib/core/bin.js +66 -0
- package/lib/core/build.js +355 -0
- package/lib/core/config.js +121 -0
- package/lib/core/constants.js +116 -0
- package/lib/core/dev.js +27 -0
- package/lib/core/dry.js +21 -0
- package/lib/core/feature_flags.js +19 -0
- package/lib/core/flags.js +201 -0
- package/lib/core/lingering.js +68 -0
- package/lib/core/main.js +123 -0
- package/lib/core/missing_side_file.js +17 -0
- package/lib/core/normalize_flags.js +59 -0
- package/lib/core/severity.js +20 -0
- package/lib/core/user_node_version.js +32 -0
- package/lib/env/changes.js +43 -0
- package/lib/env/main.js +14 -0
- package/lib/env/metadata.js +68 -0
- package/lib/error/api.js +37 -0
- package/lib/error/build.js +36 -0
- package/{src → lib}/error/cancel.js +5 -6
- package/lib/error/colors.js +9 -0
- package/lib/error/handle.js +46 -0
- package/lib/error/info.js +37 -0
- package/lib/error/monitor/location.js +16 -0
- package/lib/error/monitor/normalize.js +86 -0
- package/lib/error/monitor/print.js +20 -0
- package/lib/error/monitor/report.js +120 -0
- package/lib/error/monitor/start.js +58 -0
- package/lib/error/parse/clean_stack.js +70 -0
- package/lib/error/parse/location.js +50 -0
- package/lib/error/parse/normalize.js +24 -0
- package/lib/error/parse/parse.js +67 -0
- package/lib/error/parse/plugin.js +55 -0
- package/lib/error/parse/properties.js +16 -0
- package/lib/error/parse/serialize_log.js +34 -0
- package/lib/error/parse/serialize_status.js +18 -0
- package/lib/error/parse/stack.js +34 -0
- package/lib/error/type.js +171 -0
- package/lib/install/functions.js +20 -0
- package/lib/install/local.js +45 -0
- package/lib/install/main.js +67 -0
- package/lib/install/missing.js +54 -0
- package/{src → lib}/log/colors.js +15 -22
- package/lib/log/description.js +21 -0
- package/lib/log/header.js +14 -0
- package/lib/log/header_func.js +13 -0
- package/lib/log/logger.js +130 -0
- package/lib/log/messages/compatibility.js +120 -0
- package/lib/log/messages/config.js +91 -0
- package/lib/log/messages/core.js +50 -0
- package/lib/log/messages/core_steps.js +75 -0
- package/lib/log/messages/dry.js +41 -0
- package/lib/log/messages/install.js +25 -0
- package/lib/log/messages/ipc.js +29 -0
- package/lib/log/messages/mutations.js +62 -0
- package/{src → lib}/log/messages/plugins.js +18 -32
- package/lib/log/messages/status.js +14 -0
- package/lib/log/messages/steps.js +18 -0
- package/lib/log/old_version.js +32 -0
- package/lib/log/serialize.js +10 -0
- package/lib/log/stream.js +68 -0
- package/lib/log/theme.js +25 -0
- package/lib/plugins/child/diff.js +46 -0
- package/lib/plugins/child/error.js +26 -0
- package/lib/plugins/child/lazy.js +15 -0
- package/lib/plugins/child/load.js +22 -0
- package/lib/plugins/child/logic.js +57 -0
- package/lib/plugins/child/main.js +37 -0
- package/lib/plugins/child/run.js +19 -0
- package/lib/plugins/child/status.js +63 -0
- package/lib/plugins/child/typescript.js +28 -0
- package/lib/plugins/child/utils.js +42 -0
- package/lib/plugins/child/validate.js +31 -0
- package/lib/plugins/compatibility.js +104 -0
- package/{src → lib}/plugins/error.js +31 -35
- package/{src → lib}/plugins/events.js +7 -12
- package/lib/plugins/expected_version.js +81 -0
- package/lib/plugins/ipc.js +120 -0
- package/lib/plugins/list.js +73 -0
- package/lib/plugins/load.js +50 -0
- package/lib/plugins/manifest/check.js +85 -0
- package/lib/plugins/manifest/load.js +38 -0
- package/lib/plugins/manifest/main.js +17 -0
- package/lib/plugins/manifest/path.js +24 -0
- package/lib/plugins/manifest/validate.js +91 -0
- package/lib/plugins/node_version.js +30 -0
- package/lib/plugins/options.js +55 -0
- package/lib/plugins/pinned_version.js +83 -0
- package/lib/plugins/resolve.js +110 -0
- package/lib/plugins/spawn.js +54 -0
- package/lib/plugins_core/add.js +35 -0
- package/lib/plugins_core/build_command.js +50 -0
- package/lib/plugins_core/deploy/buildbot_client.js +87 -0
- package/lib/plugins_core/deploy/index.js +49 -0
- package/{src → lib}/plugins_core/deploy/manifest.yml +0 -0
- package/lib/plugins_core/edge_functions/index.js +79 -0
- package/lib/plugins_core/edge_functions/lib/error.js +17 -0
- package/lib/plugins_core/edge_functions/lib/internal_manifest.js +50 -0
- package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +75 -0
- package/lib/plugins_core/functions/error.js +123 -0
- package/lib/plugins_core/functions/feature_flags.js +6 -0
- package/lib/plugins_core/functions/index.js +114 -0
- package/lib/plugins_core/functions/utils.js +45 -0
- package/lib/plugins_core/functions/zisi.js +39 -0
- package/{src → lib}/plugins_core/functions_install/index.js +8 -11
- package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
- package/lib/plugins_core/list.js +20 -0
- package/lib/status/add.js +30 -0
- package/lib/status/colors.js +18 -0
- package/lib/status/load_error.js +10 -0
- package/lib/status/report.js +83 -0
- package/lib/status/success.js +14 -0
- package/lib/steps/core_step.js +57 -0
- package/lib/steps/error.js +65 -0
- package/lib/steps/get.js +43 -0
- package/lib/steps/plugin.js +55 -0
- package/lib/steps/return.js +25 -0
- package/lib/steps/run_core_steps.js +147 -0
- package/lib/steps/run_step.js +188 -0
- package/lib/steps/run_steps.js +96 -0
- package/lib/steps/update_config.js +66 -0
- package/lib/telemetry/main.js +97 -0
- package/lib/time/aggregate.js +120 -0
- package/lib/time/main.js +37 -0
- package/lib/time/measure.js +18 -0
- package/lib/time/report.js +47 -0
- package/lib/utils/errors.js +13 -0
- package/lib/utils/json.js +15 -0
- package/{src → lib}/utils/omit.js +3 -4
- package/lib/utils/package.js +22 -0
- package/lib/utils/remove_falsy.js +8 -0
- package/lib/utils/resolve.js +41 -0
- package/lib/utils/runtime.js +5 -0
- package/lib/utils/semver.js +28 -0
- package/package.json +39 -20
- package/types/config/netlify_config.d.ts +4 -4
- package/types/netlify_plugin_constants.d.ts +8 -8
- package/src/core/bin.js +0 -83
- package/src/core/config.js +0 -186
- package/src/core/constants.js +0 -156
- package/src/core/dry.js +0 -39
- package/src/core/feature_flags.js +0 -21
- package/src/core/flags.js +0 -194
- package/src/core/lingering.js +0 -85
- package/src/core/main.js +0 -692
- package/src/core/missing_side_file.js +0 -29
- package/src/core/normalize_flags.js +0 -69
- package/src/core/severity.js +0 -22
- package/src/core/user_node_version.js +0 -41
- package/src/env/changes.js +0 -52
- package/src/env/main.js +0 -19
- package/src/env/metadata.js +0 -81
- package/src/error/api.js +0 -46
- package/src/error/build.js +0 -50
- package/src/error/colors.js +0 -11
- package/src/error/handle.js +0 -57
- package/src/error/info.js +0 -46
- package/src/error/monitor/location.js +0 -21
- package/src/error/monitor/normalize.js +0 -77
- package/src/error/monitor/print.js +0 -42
- package/src/error/monitor/report.js +0 -133
- package/src/error/monitor/start.js +0 -69
- package/src/error/parse/clean_stack.js +0 -87
- package/src/error/parse/location.js +0 -58
- package/src/error/parse/normalize.js +0 -29
- package/src/error/parse/parse.js +0 -97
- package/src/error/parse/plugin.js +0 -70
- package/src/error/parse/properties.js +0 -23
- package/src/error/parse/serialize_log.js +0 -42
- package/src/error/parse/serialize_status.js +0 -23
- package/src/error/parse/stack.js +0 -43
- package/src/error/type.js +0 -182
- package/src/install/functions.js +0 -28
- package/src/install/local.js +0 -62
- package/src/install/main.js +0 -81
- package/src/install/missing.js +0 -67
- package/src/log/description.js +0 -26
- package/src/log/header.js +0 -16
- package/src/log/header_func.js +0 -17
- package/src/log/logger.js +0 -107
- package/src/log/messages/compatibility.js +0 -164
- package/src/log/messages/config.js +0 -105
- package/src/log/messages/core.js +0 -70
- package/src/log/messages/core_steps.js +0 -104
- package/src/log/messages/dry.js +0 -63
- package/src/log/messages/install.js +0 -20
- package/src/log/messages/ipc.js +0 -38
- package/src/log/messages/mutations.js +0 -82
- package/src/log/messages/status.js +0 -16
- package/src/log/messages/steps.js +0 -22
- package/src/log/old_version.js +0 -41
- package/src/log/serialize.js +0 -13
- package/src/log/stream.js +0 -85
- package/src/log/theme.js +0 -26
- package/src/plugins/child/diff.js +0 -55
- package/src/plugins/child/error.js +0 -32
- package/src/plugins/child/lazy.js +0 -18
- package/src/plugins/child/load.js +0 -29
- package/src/plugins/child/logic.js +0 -57
- package/src/plugins/child/main.js +0 -51
- package/src/plugins/child/run.js +0 -28
- package/src/plugins/child/status.js +0 -67
- package/src/plugins/child/typescript.js +0 -45
- package/src/plugins/child/utils.js +0 -56
- package/src/plugins/child/validate.js +0 -34
- package/src/plugins/compatibility.js +0 -128
- package/src/plugins/expected_version.js +0 -119
- package/src/plugins/ipc.js +0 -145
- package/src/plugins/list.js +0 -86
- package/src/plugins/load.js +0 -70
- package/src/plugins/manifest/check.js +0 -106
- package/src/plugins/manifest/load.js +0 -41
- package/src/plugins/manifest/main.js +0 -22
- package/src/plugins/manifest/path.js +0 -31
- package/src/plugins/manifest/validate.js +0 -108
- package/src/plugins/node_version.js +0 -50
- package/src/plugins/options.js +0 -88
- package/src/plugins/pinned_version.js +0 -131
- package/src/plugins/resolve.js +0 -152
- package/src/plugins/spawn.js +0 -66
- package/src/plugins_core/add.js +0 -49
- package/src/plugins_core/build_command.js +0 -75
- package/src/plugins_core/deploy/buildbot_client.js +0 -102
- package/src/plugins_core/deploy/index.js +0 -73
- package/src/plugins_core/edge_functions/index.js +0 -107
- package/src/plugins_core/edge_functions/lib/internal_manifest.js +0 -54
- package/src/plugins_core/functions/error.js +0 -163
- package/src/plugins_core/functions/feature_flags.js +0 -6
- package/src/plugins_core/functions/index.js +0 -160
- package/src/plugins_core/functions/utils.js +0 -66
- package/src/plugins_core/functions/zisi.js +0 -53
- package/src/plugins_core/list.js +0 -27
- package/src/status/add.js +0 -36
- package/src/status/colors.js +0 -23
- package/src/status/load_error.js +0 -11
- package/src/status/report.js +0 -128
- package/src/status/success.js +0 -18
- package/src/steps/core_step.js +0 -90
- package/src/steps/error.js +0 -102
- package/src/steps/get.js +0 -32
- package/src/steps/plugin.js +0 -85
- package/src/steps/return.js +0 -52
- package/src/steps/run_core_steps.js +0 -194
- package/src/steps/run_step.js +0 -300
- package/src/steps/run_steps.js +0 -179
- package/src/steps/update_config.js +0 -93
- package/src/telemetry/main.js +0 -136
- package/src/time/aggregate.js +0 -146
- package/src/time/main.js +0 -48
- package/src/time/measure.js +0 -22
- package/src/time/report.js +0 -59
- package/src/utils/errors.js +0 -12
- package/src/utils/json.js +0 -19
- package/src/utils/package.js +0 -23
- package/src/utils/remove_falsy.js +0 -10
- package/src/utils/resolve.js +0 -46
- package/src/utils/semver.js +0 -34
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { addMutableConstants } from '../core/constants.js';
|
|
2
|
+
import { logStepStart } from '../log/messages/steps.js';
|
|
3
|
+
import { runsAlsoOnBuildFailure, runsOnlyOnBuildFailure } from '../plugins/events.js';
|
|
4
|
+
import { measureDuration, normalizeTimerName } from '../time/main.js';
|
|
5
|
+
import { fireCoreStep } from './core_step.js';
|
|
6
|
+
import { firePluginStep } from './plugin.js';
|
|
7
|
+
import { getStepReturn } from './return.js';
|
|
8
|
+
// Run a step (core, build command or plugin)
|
|
9
|
+
export const runStep = async function ({ event, childProcess, packageName, coreStep, coreStepId, coreStepName, coreStepDescription, pluginPackageJson, loadedFrom, origin, condition, configPath, buildDir, repositoryRoot, nodePath, index, childEnv, context, branch, envChanges, constants, steps, buildbotServerSocket, events, mode, api, errorMonitor, deployId, errorParams, error, failedPlugins, configOpts, netlifyConfig, configMutations, headersPath, redirectsPath, logs, debug, systemLog, verbose, saveConfig, timers, testOpts, featureFlags, }) {
|
|
10
|
+
const constantsA = await addMutableConstants({ constants, buildDir, netlifyConfig });
|
|
11
|
+
if (!(await shouldRunStep({
|
|
12
|
+
event,
|
|
13
|
+
packageName,
|
|
14
|
+
error,
|
|
15
|
+
failedPlugins,
|
|
16
|
+
netlifyConfig,
|
|
17
|
+
condition,
|
|
18
|
+
constants: constantsA,
|
|
19
|
+
buildbotServerSocket,
|
|
20
|
+
buildDir,
|
|
21
|
+
}))) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
logStepStart({ logs, event, packageName, coreStepDescription, index, error, netlifyConfig });
|
|
25
|
+
const fireStep = getFireStep(packageName, coreStepId, event);
|
|
26
|
+
const { newEnvChanges, netlifyConfig: netlifyConfigA = netlifyConfig, configMutations: configMutationsA = configMutations, headersPath: headersPathA = headersPath, redirectsPath: redirectsPathA = redirectsPath, newError, newStatus, timers: timersA, durationNs, } = await fireStep({
|
|
27
|
+
event,
|
|
28
|
+
childProcess,
|
|
29
|
+
packageName,
|
|
30
|
+
pluginPackageJson,
|
|
31
|
+
loadedFrom,
|
|
32
|
+
origin,
|
|
33
|
+
coreStep,
|
|
34
|
+
coreStepName,
|
|
35
|
+
configPath,
|
|
36
|
+
buildDir,
|
|
37
|
+
repositoryRoot,
|
|
38
|
+
nodePath,
|
|
39
|
+
childEnv,
|
|
40
|
+
context,
|
|
41
|
+
branch,
|
|
42
|
+
envChanges,
|
|
43
|
+
constants: constantsA,
|
|
44
|
+
steps,
|
|
45
|
+
buildbotServerSocket,
|
|
46
|
+
events,
|
|
47
|
+
error,
|
|
48
|
+
logs,
|
|
49
|
+
debug,
|
|
50
|
+
systemLog,
|
|
51
|
+
verbose,
|
|
52
|
+
saveConfig,
|
|
53
|
+
timers,
|
|
54
|
+
errorParams,
|
|
55
|
+
configOpts,
|
|
56
|
+
netlifyConfig,
|
|
57
|
+
configMutations,
|
|
58
|
+
headersPath,
|
|
59
|
+
redirectsPath,
|
|
60
|
+
featureFlags,
|
|
61
|
+
});
|
|
62
|
+
const newValues = await getStepReturn({
|
|
63
|
+
event,
|
|
64
|
+
packageName,
|
|
65
|
+
newError,
|
|
66
|
+
newEnvChanges,
|
|
67
|
+
newStatus,
|
|
68
|
+
coreStep,
|
|
69
|
+
coreStepName,
|
|
70
|
+
childEnv,
|
|
71
|
+
mode,
|
|
72
|
+
api,
|
|
73
|
+
errorMonitor,
|
|
74
|
+
deployId,
|
|
75
|
+
netlifyConfig: netlifyConfigA,
|
|
76
|
+
configMutations: configMutationsA,
|
|
77
|
+
headersPath: headersPathA,
|
|
78
|
+
redirectsPath: redirectsPathA,
|
|
79
|
+
logs,
|
|
80
|
+
debug,
|
|
81
|
+
systemLog,
|
|
82
|
+
timers: timersA,
|
|
83
|
+
durationNs,
|
|
84
|
+
testOpts,
|
|
85
|
+
});
|
|
86
|
+
return { ...newValues, newIndex: index + 1 };
|
|
87
|
+
};
|
|
88
|
+
// A plugin fails _without making the build fail_ when either:
|
|
89
|
+
// - using `utils.build.failPlugin()`
|
|
90
|
+
// - the failure happens after deploy. This means: inside any `onError`,
|
|
91
|
+
// `onSuccess` or `onEnd` event handler.
|
|
92
|
+
//
|
|
93
|
+
// When that happens, no other events for that specific plugin is run.
|
|
94
|
+
// Not even `onError` and `onEnd`.
|
|
95
|
+
// Plugins should use `try`/`catch`/`finally` blocks to handle exceptions,
|
|
96
|
+
// not `onError` nor `onEnd`.
|
|
97
|
+
//
|
|
98
|
+
// Otherwise, most failures will make the build fail. This includes:
|
|
99
|
+
// - the build command failed
|
|
100
|
+
// - Functions or Edge Functions bundling failed
|
|
101
|
+
// - the deploy failed (deploying files to our CDN)
|
|
102
|
+
// - a plugin `onPreBuild`, `onBuild` or `onPostBuild` event handler failed.
|
|
103
|
+
// This includes uncaught exceptions and using `utils.build.failBuild()`
|
|
104
|
+
// or `utils.build.cancelBuild()`.
|
|
105
|
+
//
|
|
106
|
+
// `onError` is triggered when the build failed.
|
|
107
|
+
// It means "on build failure" not "on plugin failure".
|
|
108
|
+
// `onSuccess` is the opposite. It is triggered after the build succeeded.
|
|
109
|
+
// `onEnd` is triggered after the build either failed or succeeded.
|
|
110
|
+
// It is useful for resources cleanup.
|
|
111
|
+
//
|
|
112
|
+
// Finally, some plugins (only core plugins for the moment) might be enabled or
|
|
113
|
+
// not depending on whether a specific action is happening during the build,
|
|
114
|
+
// such as creating a file. For example, the Functions and Edge Functions core
|
|
115
|
+
// plugins are disabled if no Functions or Edge Functions directory is specified
|
|
116
|
+
// or available. However, one might be created by a build plugin, in which case,
|
|
117
|
+
// those core plugins should be triggered. We use a dynamic `condition()` to
|
|
118
|
+
// model this behavior.
|
|
119
|
+
const shouldRunStep = async function ({ event, packageName, error, failedPlugins, netlifyConfig, condition, constants, buildbotServerSocket, buildDir, }) {
|
|
120
|
+
if (failedPlugins.includes(packageName) ||
|
|
121
|
+
(condition !== undefined && !(await condition({ buildDir, constants, buildbotServerSocket, netlifyConfig })))) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (error !== undefined) {
|
|
125
|
+
return runsAlsoOnBuildFailure(event);
|
|
126
|
+
}
|
|
127
|
+
return !runsOnlyOnBuildFailure(event);
|
|
128
|
+
};
|
|
129
|
+
// Wrap step function to measure its time
|
|
130
|
+
const getFireStep = function (packageName, coreStepId, event) {
|
|
131
|
+
if (coreStepId !== undefined) {
|
|
132
|
+
return measureDuration(tFireStep, coreStepId);
|
|
133
|
+
}
|
|
134
|
+
const parentTag = normalizeTimerName(packageName);
|
|
135
|
+
return measureDuration(tFireStep, event, { parentTag, category: 'pluginEvent' });
|
|
136
|
+
};
|
|
137
|
+
const tFireStep = function ({ event, childProcess, packageName, pluginPackageJson, loadedFrom, origin, coreStep, coreStepName, configPath, buildDir, repositoryRoot, nodePath, childEnv, context, branch, envChanges, constants, steps, buildbotServerSocket, events, error, logs, debug, systemLog, verbose, saveConfig, errorParams, configOpts, netlifyConfig, configMutations, headersPath, redirectsPath, featureFlags, }) {
|
|
138
|
+
if (coreStep !== undefined) {
|
|
139
|
+
return fireCoreStep({
|
|
140
|
+
coreStep,
|
|
141
|
+
coreStepName,
|
|
142
|
+
configPath,
|
|
143
|
+
buildDir,
|
|
144
|
+
repositoryRoot,
|
|
145
|
+
constants,
|
|
146
|
+
buildbotServerSocket,
|
|
147
|
+
events,
|
|
148
|
+
logs,
|
|
149
|
+
nodePath,
|
|
150
|
+
childEnv,
|
|
151
|
+
context,
|
|
152
|
+
branch,
|
|
153
|
+
envChanges,
|
|
154
|
+
errorParams,
|
|
155
|
+
configOpts,
|
|
156
|
+
netlifyConfig,
|
|
157
|
+
configMutations,
|
|
158
|
+
headersPath,
|
|
159
|
+
redirectsPath,
|
|
160
|
+
featureFlags,
|
|
161
|
+
debug,
|
|
162
|
+
systemLog,
|
|
163
|
+
saveConfig,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return firePluginStep({
|
|
167
|
+
event,
|
|
168
|
+
childProcess,
|
|
169
|
+
packageName,
|
|
170
|
+
pluginPackageJson,
|
|
171
|
+
loadedFrom,
|
|
172
|
+
origin,
|
|
173
|
+
envChanges,
|
|
174
|
+
errorParams,
|
|
175
|
+
configOpts,
|
|
176
|
+
netlifyConfig,
|
|
177
|
+
configMutations,
|
|
178
|
+
headersPath,
|
|
179
|
+
redirectsPath,
|
|
180
|
+
constants,
|
|
181
|
+
steps,
|
|
182
|
+
error,
|
|
183
|
+
logs,
|
|
184
|
+
debug,
|
|
185
|
+
systemLog,
|
|
186
|
+
verbose,
|
|
187
|
+
});
|
|
188
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import pReduce from 'p-reduce';
|
|
2
|
+
import { addErrorInfo } from '../error/info.js';
|
|
3
|
+
import { addStatus } from '../status/add.js';
|
|
4
|
+
import { runStep } from './run_step.js';
|
|
5
|
+
// Run all steps.
|
|
6
|
+
// Each step can change some state: last `error`, environment variables changes,
|
|
7
|
+
// list of `failedPlugins` (that ran `utils.build.failPlugin()`).
|
|
8
|
+
// If an error arises, runs `onError` events.
|
|
9
|
+
// Runs `onEnd` events at the end, whether an error was thrown or not.
|
|
10
|
+
export const runSteps = async function ({ steps, buildbotServerSocket, events, configPath, headersPath, redirectsPath, buildDir, repositoryRoot, nodePath, childEnv, context, branch, constants, mode, api, errorMonitor, deployId, errorParams, netlifyConfig, configOpts, logs, debug, systemLog, verbose, saveConfig, timers, testOpts, featureFlags, }) {
|
|
11
|
+
const { index: stepsCount, error: errorA, netlifyConfig: netlifyConfigC, statuses: statusesB, failedPlugins: failedPluginsA, timers: timersC, configMutations: configMutationsB, } = await pReduce(steps, async ({ index, error, failedPlugins, envChanges, netlifyConfig: netlifyConfigA, configMutations, headersPath: headersPathA, redirectsPath: redirectsPathA, statuses, timers: timersA, }, { event, childProcess, packageName, coreStep, coreStepId, coreStepName, coreStepDescription, pluginPackageJson, loadedFrom, origin, condition, }) => {
|
|
12
|
+
const { newIndex = index, newError = error, failedPlugin = [], newEnvChanges = {}, netlifyConfig: netlifyConfigB = netlifyConfigA, configMutations: configMutationsA = configMutations, headersPath: headersPathB = headersPathA, redirectsPath: redirectsPathB = redirectsPathA, newStatus, timers: timersB = timersA, } = await runStep({
|
|
13
|
+
event,
|
|
14
|
+
childProcess,
|
|
15
|
+
packageName,
|
|
16
|
+
coreStep,
|
|
17
|
+
coreStepId,
|
|
18
|
+
coreStepName,
|
|
19
|
+
coreStepDescription,
|
|
20
|
+
pluginPackageJson,
|
|
21
|
+
loadedFrom,
|
|
22
|
+
origin,
|
|
23
|
+
condition,
|
|
24
|
+
configPath,
|
|
25
|
+
buildDir,
|
|
26
|
+
repositoryRoot,
|
|
27
|
+
nodePath,
|
|
28
|
+
index,
|
|
29
|
+
childEnv,
|
|
30
|
+
context,
|
|
31
|
+
branch,
|
|
32
|
+
envChanges,
|
|
33
|
+
constants,
|
|
34
|
+
steps,
|
|
35
|
+
buildbotServerSocket,
|
|
36
|
+
events,
|
|
37
|
+
mode,
|
|
38
|
+
api,
|
|
39
|
+
errorMonitor,
|
|
40
|
+
deployId,
|
|
41
|
+
errorParams,
|
|
42
|
+
error,
|
|
43
|
+
failedPlugins,
|
|
44
|
+
configOpts,
|
|
45
|
+
netlifyConfig: netlifyConfigA,
|
|
46
|
+
configMutations,
|
|
47
|
+
headersPath: headersPathA,
|
|
48
|
+
redirectsPath: redirectsPathA,
|
|
49
|
+
logs,
|
|
50
|
+
debug,
|
|
51
|
+
systemLog,
|
|
52
|
+
verbose,
|
|
53
|
+
saveConfig,
|
|
54
|
+
timers: timersA,
|
|
55
|
+
testOpts,
|
|
56
|
+
featureFlags,
|
|
57
|
+
});
|
|
58
|
+
const statusesA = addStatus({ newStatus, statuses, event, packageName, pluginPackageJson });
|
|
59
|
+
return {
|
|
60
|
+
index: newIndex,
|
|
61
|
+
error: newError,
|
|
62
|
+
failedPlugins: [...failedPlugins, ...failedPlugin],
|
|
63
|
+
envChanges: { ...envChanges, ...newEnvChanges },
|
|
64
|
+
netlifyConfig: netlifyConfigB,
|
|
65
|
+
configMutations: configMutationsA,
|
|
66
|
+
headersPath: headersPathB,
|
|
67
|
+
redirectsPath: redirectsPathB,
|
|
68
|
+
statuses: statusesA,
|
|
69
|
+
timers: timersB,
|
|
70
|
+
};
|
|
71
|
+
}, {
|
|
72
|
+
index: 0,
|
|
73
|
+
failedPlugins: [],
|
|
74
|
+
envChanges: {},
|
|
75
|
+
netlifyConfig,
|
|
76
|
+
configMutations: [],
|
|
77
|
+
headersPath,
|
|
78
|
+
redirectsPath,
|
|
79
|
+
statuses: [],
|
|
80
|
+
timers,
|
|
81
|
+
});
|
|
82
|
+
// Instead of throwing any build failure right away, we wait for `onError`,
|
|
83
|
+
// etc. to complete. This is why we are throwing only now.
|
|
84
|
+
if (errorA !== undefined) {
|
|
85
|
+
addErrorInfo(errorA, { statuses: statusesB });
|
|
86
|
+
throw errorA;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
stepsCount,
|
|
90
|
+
netlifyConfig: netlifyConfigC,
|
|
91
|
+
statuses: statusesB,
|
|
92
|
+
failedPlugins: failedPluginsA,
|
|
93
|
+
timers: timersC,
|
|
94
|
+
configMutations: configMutationsB,
|
|
95
|
+
};
|
|
96
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'util';
|
|
2
|
+
import pFilter from 'p-filter';
|
|
3
|
+
import { pathExists } from 'path-exists';
|
|
4
|
+
import { resolveUpdatedConfig } from '../core/config.js';
|
|
5
|
+
import { addErrorInfo } from '../error/info.js';
|
|
6
|
+
import { logConfigOnUpdate } from '../log/messages/config.js';
|
|
7
|
+
import { logConfigMutations } from '../log/messages/mutations.js';
|
|
8
|
+
// If `netlifyConfig` was updated or `_redirects` was created, the configuration
|
|
9
|
+
// is updated by calling `@netlify/config` again.
|
|
10
|
+
export const updateNetlifyConfig = async function ({ configOpts, netlifyConfig, headersPath, redirectsPath, configMutations, newConfigMutations, configSideFiles, errorParams, logs, debug, }) {
|
|
11
|
+
if (!(await shouldUpdateConfig({ newConfigMutations, configSideFiles, headersPath, redirectsPath }))) {
|
|
12
|
+
return { netlifyConfig, configMutations };
|
|
13
|
+
}
|
|
14
|
+
validateConfigMutations(newConfigMutations);
|
|
15
|
+
logConfigMutations(logs, newConfigMutations, debug);
|
|
16
|
+
const configMutationsA = [...configMutations, ...newConfigMutations];
|
|
17
|
+
const { config: netlifyConfigA, headersPath: headersPathA, redirectsPath: redirectsPathA, } = await resolveUpdatedConfig(configOpts, configMutationsA);
|
|
18
|
+
logConfigOnUpdate({ logs, netlifyConfig: netlifyConfigA, debug });
|
|
19
|
+
errorParams.netlifyConfig = netlifyConfigA;
|
|
20
|
+
return {
|
|
21
|
+
netlifyConfig: netlifyConfigA,
|
|
22
|
+
configMutations: configMutationsA,
|
|
23
|
+
headersPath: headersPathA,
|
|
24
|
+
redirectsPath: redirectsPathA,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const shouldUpdateConfig = async function ({ newConfigMutations, configSideFiles, headersPath, redirectsPath }) {
|
|
28
|
+
return (newConfigMutations.length !== 0 || (await haveConfigSideFilesChanged(configSideFiles, headersPath, redirectsPath)));
|
|
29
|
+
};
|
|
30
|
+
// The configuration mostly depends on `netlify.toml` and UI build settings.
|
|
31
|
+
// However, it also uses some additional optional side files like `_redirects`.
|
|
32
|
+
// Those are often created by the build command. When those are created, we need
|
|
33
|
+
// to update the configuration. We detect this by checking for file existence
|
|
34
|
+
// before and after running plugins and the build command.
|
|
35
|
+
const haveConfigSideFilesChanged = async function (configSideFiles, headersPath, redirectsPath) {
|
|
36
|
+
const newSideFiles = await listConfigSideFiles([headersPath, redirectsPath]);
|
|
37
|
+
return !isDeepStrictEqual(newSideFiles, configSideFiles);
|
|
38
|
+
};
|
|
39
|
+
// List all the files used for configuration besides `netlify.toml`.
|
|
40
|
+
// This is useful when applying configuration mutations since those files
|
|
41
|
+
// sometimes have higher priority and should therefore be deleted in order to
|
|
42
|
+
// apply any configuration update on `netlify.toml`.
|
|
43
|
+
export const listConfigSideFiles = async function (sideFiles) {
|
|
44
|
+
const configSideFiles = await pFilter(sideFiles, pathExists);
|
|
45
|
+
return configSideFiles.sort();
|
|
46
|
+
};
|
|
47
|
+
// Validate each new configuration change
|
|
48
|
+
const validateConfigMutations = function (newConfigMutations) {
|
|
49
|
+
try {
|
|
50
|
+
newConfigMutations.forEach(validateConfigMutation);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
addErrorInfo(error, { type: 'pluginValidation' });
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
// Triggered when calling `netlifyConfig.{key} = undefined | null`
|
|
58
|
+
// We do not allow this because the back-end only receives mutations as a
|
|
59
|
+
// `netlify.toml`, i.e. cannot apply property deletions since `undefined` is
|
|
60
|
+
// not serializable in TOML.
|
|
61
|
+
const validateConfigMutation = function ({ value, keysString }) {
|
|
62
|
+
if (value === undefined || value === null) {
|
|
63
|
+
throw new Error(`Setting "netlifyConfig.${keysString}" to ${value} is not allowed.
|
|
64
|
+
Please set this property to a specific value instead.`);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { platform } from 'process';
|
|
2
|
+
import got from 'got';
|
|
3
|
+
import osName from 'os-name';
|
|
4
|
+
import { addErrorInfo } from '../error/info.js';
|
|
5
|
+
import { roundTimerToMillisecs } from '../time/measure.js';
|
|
6
|
+
import { ROOT_PACKAGE_JSON } from '../utils/json.js';
|
|
7
|
+
const DEFAULT_TELEMETRY_TIMEOUT = 1200;
|
|
8
|
+
const DEFAULT_TELEMETRY_CONFIG = {
|
|
9
|
+
origin: 'https://api.segment.io/v1',
|
|
10
|
+
writeKey: 'dWhlM1lYSlpNd1k5Uk9rcjFra2JSOEoybnRjZjl0YTI6',
|
|
11
|
+
timeout: DEFAULT_TELEMETRY_TIMEOUT,
|
|
12
|
+
};
|
|
13
|
+
// Send telemetry request when build completes
|
|
14
|
+
export const trackBuildComplete = async function ({ deployId, buildId, status, stepsCount, pluginsOptions, durationNs, siteInfo, telemetry, userNodeVersion, framework, testOpts: { telemetryOrigin = DEFAULT_TELEMETRY_CONFIG.origin, telemetryTimeout = DEFAULT_TELEMETRY_CONFIG.timeout }, }) {
|
|
15
|
+
if (!telemetry) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const payload = getPayload({
|
|
20
|
+
deployId,
|
|
21
|
+
buildId,
|
|
22
|
+
status,
|
|
23
|
+
stepsCount,
|
|
24
|
+
pluginsOptions,
|
|
25
|
+
durationNs,
|
|
26
|
+
siteInfo,
|
|
27
|
+
userNodeVersion,
|
|
28
|
+
framework,
|
|
29
|
+
});
|
|
30
|
+
await track({
|
|
31
|
+
payload,
|
|
32
|
+
config: { ...DEFAULT_TELEMETRY_CONFIG, origin: telemetryOrigin, timeout: telemetryTimeout },
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
addErrorInfo(error, { type: 'telemetry' });
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
// Send track HTTP request to telemetry.
|
|
41
|
+
const track = async function ({ payload, config: { origin, writeKey, timeout } }) {
|
|
42
|
+
const url = `${origin}/track`;
|
|
43
|
+
await got.post(url, {
|
|
44
|
+
json: payload,
|
|
45
|
+
timeout,
|
|
46
|
+
retry: 0,
|
|
47
|
+
headers: { Authorization: `Basic ${writeKey}` },
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
// Retrieve telemetry information
|
|
51
|
+
// siteInfo can be empty if the build fails during the get config step
|
|
52
|
+
const getPayload = function ({ deployId, buildId, status, stepsCount, pluginsOptions, durationNs, userNodeVersion, siteInfo = {}, framework, }) {
|
|
53
|
+
return {
|
|
54
|
+
userId: 'buildbot_user',
|
|
55
|
+
event: 'build:ci_build_process_completed',
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
properties: {
|
|
58
|
+
deployId,
|
|
59
|
+
buildId,
|
|
60
|
+
status,
|
|
61
|
+
steps: stepsCount,
|
|
62
|
+
buildVersion: ROOT_PACKAGE_JSON.version,
|
|
63
|
+
// We're passing the node version set by the buildbot/user which will run the `build.command` and
|
|
64
|
+
// the `package.json`/locally defined plugins
|
|
65
|
+
nodeVersion: userNodeVersion,
|
|
66
|
+
osPlatform: OS_TYPES[platform],
|
|
67
|
+
osName: osName(),
|
|
68
|
+
framework,
|
|
69
|
+
siteId: siteInfo.id,
|
|
70
|
+
...(pluginsOptions !== undefined && {
|
|
71
|
+
plugins: pluginsOptions.map(getPlugin),
|
|
72
|
+
pluginCount: pluginsOptions.length,
|
|
73
|
+
}),
|
|
74
|
+
...(durationNs !== undefined && { duration: roundTimerToMillisecs(durationNs) }),
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
const OS_TYPES = {
|
|
79
|
+
linux: 'Linux',
|
|
80
|
+
darwin: 'MacOS',
|
|
81
|
+
win32: 'Windows',
|
|
82
|
+
android: 'Android',
|
|
83
|
+
sunos: 'SunOS',
|
|
84
|
+
aix: 'AIX',
|
|
85
|
+
freebsd: 'FreeBSD',
|
|
86
|
+
openbsd: 'OpenBSD',
|
|
87
|
+
};
|
|
88
|
+
const getPlugin = function ({ packageName, origin, loadedFrom, nodeVersion, pinnedVersion, pluginPackageJson: { version } = {}, }) {
|
|
89
|
+
const installType = getInstallType(origin, loadedFrom);
|
|
90
|
+
return { name: packageName, installType, nodeVersion, pinnedVersion, version };
|
|
91
|
+
};
|
|
92
|
+
const getInstallType = function (origin, loadedFrom) {
|
|
93
|
+
if (loadedFrom === 'auto_install') {
|
|
94
|
+
return origin === 'ui' ? 'ui' : 'netlify_toml';
|
|
95
|
+
}
|
|
96
|
+
return loadedFrom;
|
|
97
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { createTimer, TOP_PARENT_TAG } from './main.js';
|
|
2
|
+
// Some timers are computed based on others:
|
|
3
|
+
// - `others` is `total` minus the other timers
|
|
4
|
+
// - `run_plugins` is the sum of all plugins
|
|
5
|
+
// - `run_netlify_build_per_type` aggregates timers but per system/plugin/user
|
|
6
|
+
// - each plugin timer is the sum of its event handlers
|
|
7
|
+
export const addAggregatedTimers = function (timers) {
|
|
8
|
+
const timersA = addRunPluginsTimer(timers);
|
|
9
|
+
const timersB = addPluginTimers(timersA);
|
|
10
|
+
const timersC = addOthersTimers(timersB);
|
|
11
|
+
const timersD = addTypeTimers(timersC);
|
|
12
|
+
return timersD;
|
|
13
|
+
};
|
|
14
|
+
// Having a `total` timer is redundant since the buildbot already measures this.
|
|
15
|
+
// The buildbot measurement is better since it includes the time to load Node.
|
|
16
|
+
// Instead, we only use `total` to measure what did not get `measureDuration()`.
|
|
17
|
+
const addOthersTimers = function (timers) {
|
|
18
|
+
const totalTimer = timers.find(isTotalTimer);
|
|
19
|
+
const timersA = timers.filter((timer) => !isTotalTimer(timer));
|
|
20
|
+
const topTimers = timersA.filter(isTopTimer);
|
|
21
|
+
const othersTimer = createOthersTimer(topTimers, totalTimer);
|
|
22
|
+
return [...timersA, othersTimer];
|
|
23
|
+
};
|
|
24
|
+
const isTotalTimer = function ({ stageTag, parentTag }) {
|
|
25
|
+
return stageTag === 'total' && parentTag === 'build_site';
|
|
26
|
+
};
|
|
27
|
+
const createOthersTimer = function (topTimers, { durationNs: totalTimerDurationNs }) {
|
|
28
|
+
const topTimersDurationNs = computeTimersDuration(topTimers);
|
|
29
|
+
const otherTimersDurationNs = Math.max(0, totalTimerDurationNs - topTimersDurationNs);
|
|
30
|
+
const othersTimer = createTimer(OTHERS_STAGE_TAG, otherTimersDurationNs);
|
|
31
|
+
return othersTimer;
|
|
32
|
+
};
|
|
33
|
+
const OTHERS_STAGE_TAG = 'others';
|
|
34
|
+
// Measure the total time running plugins
|
|
35
|
+
const addRunPluginsTimer = function (timers) {
|
|
36
|
+
const pluginsTimers = timers.filter(isPluginTimer);
|
|
37
|
+
return pluginsTimers.length === 0 ? timers : [...timers, createSumTimer(pluginsTimers, RUN_PLUGINS_STAGE_TAG)];
|
|
38
|
+
};
|
|
39
|
+
const RUN_PLUGINS_STAGE_TAG = 'run_plugins';
|
|
40
|
+
// Retrieve one timer for each plugin, summing all its individual timers
|
|
41
|
+
// (one per event handler)
|
|
42
|
+
const addPluginTimers = function (timers) {
|
|
43
|
+
const timersA = timers.filter((timer) => !isCommunityPluginTimer(timer));
|
|
44
|
+
const pluginsTimers = timersA.filter(isPluginTimer);
|
|
45
|
+
if (pluginsTimers.length === 0) {
|
|
46
|
+
return timersA;
|
|
47
|
+
}
|
|
48
|
+
const pluginPackages = getPluginPackages(pluginsTimers);
|
|
49
|
+
const wholePluginsTimers = pluginPackages.map((pluginPackage) => getWholePluginTimer(pluginPackage, pluginsTimers));
|
|
50
|
+
return [...timersA, ...wholePluginsTimers];
|
|
51
|
+
};
|
|
52
|
+
// We only measure plugins maintained by us, not community
|
|
53
|
+
const isCommunityPluginTimer = function (timer) {
|
|
54
|
+
return isPluginTimer(timer) && !isNetlifyMaintainedPlugin(getPluginTimerPackage(timer));
|
|
55
|
+
};
|
|
56
|
+
const isNetlifyMaintainedPlugin = function (pluginPackage) {
|
|
57
|
+
return NETLIFY_MAINTAINED_PLUGINS.has(pluginPackage);
|
|
58
|
+
};
|
|
59
|
+
const NETLIFY_MAINTAINED_PLUGINS = new Set([
|
|
60
|
+
'netlify_plugin_gatsby_cache',
|
|
61
|
+
'netlify_plugin_sitemap',
|
|
62
|
+
'netlify_plugin_debug_cache',
|
|
63
|
+
'netlify_plugin_is_website_vulnerable',
|
|
64
|
+
'netlify_plugin_lighthouse',
|
|
65
|
+
'netlify_plugin_nextjs',
|
|
66
|
+
'netlify_plugin_gatsby',
|
|
67
|
+
]);
|
|
68
|
+
const getPluginPackages = function (pluginsTimers) {
|
|
69
|
+
const pluginPackages = pluginsTimers.map(getPluginTimerPackage);
|
|
70
|
+
return [...new Set(pluginPackages)];
|
|
71
|
+
};
|
|
72
|
+
const getWholePluginTimer = function (pluginPackage, pluginsTimers) {
|
|
73
|
+
const pluginTimers = pluginsTimers.filter((pluginTimer) => getPluginTimerPackage(pluginTimer) === pluginPackage);
|
|
74
|
+
const wholePluginsTimer = createSumTimer(pluginTimers, pluginPackage, RUN_PLUGINS_STAGE_TAG);
|
|
75
|
+
return wholePluginsTimer;
|
|
76
|
+
};
|
|
77
|
+
const getPluginTimerPackage = function ({ parentTag }) {
|
|
78
|
+
return parentTag;
|
|
79
|
+
};
|
|
80
|
+
// Check if a timer relates to a Build plugin
|
|
81
|
+
const isPluginTimer = function ({ category }) {
|
|
82
|
+
return category === 'pluginEvent';
|
|
83
|
+
};
|
|
84
|
+
// Reports total time depending on whether it was system, plugin or user
|
|
85
|
+
const addTypeTimers = function (timers) {
|
|
86
|
+
const topTimers = timers.filter(isTopTimer);
|
|
87
|
+
const typeTimers = TYPE_TIMERS.map(({ name, stages }) => getTypeTimer(name, stages, topTimers)).filter(Boolean);
|
|
88
|
+
return [...timers, ...typeTimers];
|
|
89
|
+
};
|
|
90
|
+
const TYPE_TIMERS = [
|
|
91
|
+
{ name: 'system', stages: ['resolve_config', 'get_plugins_options', 'start_plugins', 'others'] },
|
|
92
|
+
{ name: 'plugin', stages: ['load_plugins', 'run_plugins'] },
|
|
93
|
+
{ name: 'user', stages: ['build_command', 'functions_bundling', 'deploy_site'] },
|
|
94
|
+
];
|
|
95
|
+
const getTypeTimer = function (name, stages, topTimers) {
|
|
96
|
+
const topTimersA = topTimers.filter(({ stageTag }) => stages.includes(stageTag));
|
|
97
|
+
if (topTimersA.length === 0) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const typeTimer = createSumTimer(topTimersA, name, 'run_netlify_build_per_type');
|
|
101
|
+
return typeTimer;
|
|
102
|
+
};
|
|
103
|
+
const isTopTimer = function ({ parentTag }) {
|
|
104
|
+
return parentTag === TOP_PARENT_TAG;
|
|
105
|
+
};
|
|
106
|
+
// Creates a timer that sums up the duration of several others
|
|
107
|
+
const createSumTimer = function (timers, stageTag, parentTag) {
|
|
108
|
+
const durationNs = computeTimersDuration(timers);
|
|
109
|
+
const timer = createTimer(stageTag, durationNs, { parentTag });
|
|
110
|
+
return timer;
|
|
111
|
+
};
|
|
112
|
+
const computeTimersDuration = function (timers) {
|
|
113
|
+
return timers.map(getTimerDuration).reduce(reduceSum, 0);
|
|
114
|
+
};
|
|
115
|
+
const getTimerDuration = function ({ durationNs }) {
|
|
116
|
+
return durationNs;
|
|
117
|
+
};
|
|
118
|
+
const reduceSum = function (sum, number) {
|
|
119
|
+
return sum + number;
|
|
120
|
+
};
|
package/lib/time/main.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import slugify from '@sindresorhus/slugify';
|
|
2
|
+
import keepFuncProps from 'keep-func-props';
|
|
3
|
+
import { startTimer, endTimer } from './measure.js';
|
|
4
|
+
// Initialize the `timers` array
|
|
5
|
+
export const initTimers = function () {
|
|
6
|
+
return [];
|
|
7
|
+
};
|
|
8
|
+
// Wrap an async function to measure how long it takes.
|
|
9
|
+
// The function must:
|
|
10
|
+
// - take a plain object as first argument. This must contain a `timers`.
|
|
11
|
+
// - return a plain object. This may or may not contain a modified `timers`.
|
|
12
|
+
// The `durationNs` will be returned by the function. A new `timers` with the
|
|
13
|
+
// additional duration timer will be returned as well.
|
|
14
|
+
const kMeasureDuration = function (func, stageTag, { parentTag, category } = {}) {
|
|
15
|
+
return async function measuredFunc({ timers, ...opts }, ...args) {
|
|
16
|
+
const timerNs = startTimer();
|
|
17
|
+
const { timers: timersA = timers, ...returnObject } = await func({ timers, ...opts }, ...args);
|
|
18
|
+
const { tags = {} } = returnObject;
|
|
19
|
+
const durationNs = endTimer(timerNs);
|
|
20
|
+
const timer = createTimer(stageTag, durationNs, { parentTag, category, tags });
|
|
21
|
+
const timersB = [...timersA, timer];
|
|
22
|
+
return { ...returnObject, timers: timersB, durationNs };
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
// Ensure the wrapped function `name` is not `anonymous` in stack traces
|
|
26
|
+
export const measureDuration = keepFuncProps(kMeasureDuration);
|
|
27
|
+
// Create a new object representing a completed timer
|
|
28
|
+
export const createTimer = function (stageTag, durationNs, { metricName = DEFAULT_METRIC_NAME, parentTag = TOP_PARENT_TAG, category, tags } = {}) {
|
|
29
|
+
return { metricName, stageTag, parentTag, durationNs, category, tags };
|
|
30
|
+
};
|
|
31
|
+
const DEFAULT_METRIC_NAME = 'buildbot.build.stage.duration';
|
|
32
|
+
export const TOP_PARENT_TAG = 'run_netlify_build';
|
|
33
|
+
// Make sure the timer name does not include special characters.
|
|
34
|
+
// For example, the `packageName` of local plugins includes dots.
|
|
35
|
+
export const normalizeTimerName = function (name) {
|
|
36
|
+
return slugify(name, { separator: '_' });
|
|
37
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { hrtime } from 'process';
|
|
2
|
+
// Starts a timer
|
|
3
|
+
export const startTimer = function () {
|
|
4
|
+
return hrtime();
|
|
5
|
+
};
|
|
6
|
+
// Stops a timer
|
|
7
|
+
export const endTimer = function ([startSecs, startNsecs]) {
|
|
8
|
+
const [endSecs, endNsecs] = hrtime();
|
|
9
|
+
const durationNs = (endSecs - startSecs) * NANOSECS_TO_SECS + endNsecs - startNsecs;
|
|
10
|
+
return durationNs;
|
|
11
|
+
};
|
|
12
|
+
// statsd expects milliseconds integers.
|
|
13
|
+
// To prevent double rounding errors, rounding should only be applied once.
|
|
14
|
+
export const roundTimerToMillisecs = function (durationNs) {
|
|
15
|
+
return Math.round(durationNs / NANOSECS_TO_MSECS);
|
|
16
|
+
};
|
|
17
|
+
const NANOSECS_TO_SECS = 1e9;
|
|
18
|
+
const NANOSECS_TO_MSECS = 1e6;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import StatsdClient from 'statsd-client';
|
|
3
|
+
import { addAggregatedTimers } from './aggregate.js';
|
|
4
|
+
import { roundTimerToMillisecs } from './measure.js';
|
|
5
|
+
// TODO: replace with `timers/promises` after dropping Node < 15.0.0
|
|
6
|
+
const pSetTimeout = promisify(setTimeout);
|
|
7
|
+
// Record the duration of a build phase, for monitoring.
|
|
8
|
+
// Sends to statsd daemon.
|
|
9
|
+
export const reportTimers = async function ({ timers, statsdOpts: { host, port }, framework }) {
|
|
10
|
+
if (host === undefined) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const timersA = addAggregatedTimers(timers);
|
|
14
|
+
await sendTimers({ timers: timersA, host, port, framework });
|
|
15
|
+
};
|
|
16
|
+
const sendTimers = async function ({ timers, host, port, framework }) {
|
|
17
|
+
const client = await startClient(host, port);
|
|
18
|
+
timers.forEach((timer) => {
|
|
19
|
+
sendTimer({ timer, client, framework });
|
|
20
|
+
});
|
|
21
|
+
await closeClient(client);
|
|
22
|
+
};
|
|
23
|
+
// The socket creation is delayed until the first packet is sent. In our
|
|
24
|
+
// case, this happens just before `client.close()` is called, which is too
|
|
25
|
+
// late and make it not send anything. We need to manually create it using
|
|
26
|
+
// the internal API.
|
|
27
|
+
const startClient = async function (host, port) {
|
|
28
|
+
const client = new StatsdClient({ host, port, socketTimeout: 0 });
|
|
29
|
+
await promisify(client._socket._createSocket.bind(client._socket))();
|
|
30
|
+
return client;
|
|
31
|
+
};
|
|
32
|
+
const sendTimer = function ({ timer: { metricName, stageTag, parentTag, durationNs, tags }, client, framework }) {
|
|
33
|
+
const durationMs = roundTimerToMillisecs(durationNs);
|
|
34
|
+
const frameworkTag = framework === undefined ? {} : { framework };
|
|
35
|
+
client.distribution(metricName, durationMs, { stage: stageTag, parent: parentTag, ...tags, ...frameworkTag });
|
|
36
|
+
};
|
|
37
|
+
// UDP packets are buffered and flushed at regular intervals by statsd-client.
|
|
38
|
+
// Closing force flushing all of them.
|
|
39
|
+
const closeClient = async function (client) {
|
|
40
|
+
client.close();
|
|
41
|
+
// statsd-clent does not provide with a way of knowing when the socket is done
|
|
42
|
+
// closing, so we need to use the following hack.
|
|
43
|
+
await pSetTimeout(CLOSE_TIMEOUT);
|
|
44
|
+
await pSetTimeout(CLOSE_TIMEOUT);
|
|
45
|
+
};
|
|
46
|
+
// See https://github.com/msiebuhr/node-statsd-client/blob/45a93ee4c94ca72f244a40b06cb542d4bd7c3766/lib/EphemeralSocket.js#L81
|
|
47
|
+
const CLOSE_TIMEOUT = 11;
|