@netlify/build 27.18.5-rc → 27.18.5

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 (135) hide show
  1. package/package.json +8 -9
  2. package/src/core/bin.js +83 -0
  3. package/src/core/build.js +559 -0
  4. package/src/core/config.js +186 -0
  5. package/src/core/constants.js +156 -0
  6. package/src/core/dev.js +31 -0
  7. package/src/core/dry.js +39 -0
  8. package/src/core/feature_flags.js +22 -0
  9. package/src/core/flags.js +204 -0
  10. package/src/core/lingering.js +85 -0
  11. package/src/core/main.js +165 -0
  12. package/src/core/missing_side_file.js +29 -0
  13. package/src/core/normalize_flags.js +70 -0
  14. package/src/core/severity.js +22 -0
  15. package/src/core/user_node_version.js +41 -0
  16. package/src/env/changes.js +52 -0
  17. package/src/env/main.js +19 -0
  18. package/src/env/metadata.js +81 -0
  19. package/src/error/api.js +46 -0
  20. package/src/error/build.js +50 -0
  21. package/src/error/cancel.js +8 -0
  22. package/src/error/colors.js +11 -0
  23. package/src/error/handle.js +57 -0
  24. package/src/error/info.js +46 -0
  25. package/src/error/monitor/location.js +21 -0
  26. package/src/error/monitor/normalize.js +96 -0
  27. package/src/error/monitor/print.js +42 -0
  28. package/src/error/monitor/report.js +138 -0
  29. package/src/error/monitor/start.js +69 -0
  30. package/src/error/parse/clean_stack.js +87 -0
  31. package/src/error/parse/location.js +62 -0
  32. package/src/error/parse/normalize.js +29 -0
  33. package/src/error/parse/parse.js +97 -0
  34. package/src/error/parse/plugin.js +70 -0
  35. package/src/error/parse/properties.js +23 -0
  36. package/src/error/parse/serialize_log.js +42 -0
  37. package/src/error/parse/serialize_status.js +23 -0
  38. package/src/error/parse/stack.js +43 -0
  39. package/src/error/type.js +189 -0
  40. package/src/install/functions.js +28 -0
  41. package/src/install/local.js +62 -0
  42. package/src/install/main.js +81 -0
  43. package/src/install/missing.js +67 -0
  44. package/src/log/colors.js +34 -0
  45. package/src/log/description.js +26 -0
  46. package/src/log/header.js +16 -0
  47. package/src/log/header_func.js +17 -0
  48. package/src/log/logger.js +161 -0
  49. package/src/log/messages/compatibility.js +178 -0
  50. package/src/log/messages/config.js +107 -0
  51. package/src/log/messages/core.js +70 -0
  52. package/src/log/messages/core_steps.js +104 -0
  53. package/src/log/messages/dry.js +63 -0
  54. package/src/log/messages/install.js +32 -0
  55. package/src/log/messages/ipc.js +38 -0
  56. package/src/log/messages/mutations.js +82 -0
  57. package/src/log/messages/plugins.js +39 -0
  58. package/src/log/messages/status.js +16 -0
  59. package/src/log/messages/steps.js +22 -0
  60. package/src/log/old_version.js +41 -0
  61. package/src/log/serialize.js +13 -0
  62. package/src/log/stream.js +85 -0
  63. package/src/log/theme.js +26 -0
  64. package/src/plugins/child/diff.js +55 -0
  65. package/src/plugins/child/error.js +32 -0
  66. package/src/plugins/child/lazy.js +18 -0
  67. package/src/plugins/child/load.js +29 -0
  68. package/src/plugins/child/logic.js +65 -0
  69. package/src/plugins/child/main.js +51 -0
  70. package/src/plugins/child/run.js +28 -0
  71. package/src/plugins/child/status.js +74 -0
  72. package/src/plugins/child/typescript.js +45 -0
  73. package/src/plugins/child/utils.js +56 -0
  74. package/src/plugins/child/validate.js +34 -0
  75. package/src/plugins/compatibility.js +132 -0
  76. package/src/plugins/error.js +50 -0
  77. package/src/plugins/events.js +17 -0
  78. package/src/plugins/expected_version.js +119 -0
  79. package/src/plugins/ipc.js +145 -0
  80. package/src/plugins/list.js +86 -0
  81. package/src/plugins/load.js +70 -0
  82. package/src/plugins/manifest/check.js +106 -0
  83. package/src/plugins/manifest/load.js +41 -0
  84. package/src/plugins/manifest/main.js +22 -0
  85. package/src/plugins/manifest/path.js +31 -0
  86. package/src/plugins/manifest/validate.js +108 -0
  87. package/src/plugins/node_version.js +50 -0
  88. package/src/plugins/options.js +88 -0
  89. package/src/plugins/pinned_version.js +131 -0
  90. package/src/plugins/resolve.js +152 -0
  91. package/src/plugins/spawn.js +72 -0
  92. package/src/plugins_core/add.js +49 -0
  93. package/src/plugins_core/build_command.js +75 -0
  94. package/src/plugins_core/deploy/buildbot_client.js +113 -0
  95. package/src/plugins_core/deploy/index.js +73 -0
  96. package/src/plugins_core/deploy/manifest.yml +1 -0
  97. package/src/plugins_core/edge_functions/index.js +123 -0
  98. package/src/plugins_core/edge_functions/lib/error.js +21 -0
  99. package/src/plugins_core/edge_functions/lib/internal_manifest.js +60 -0
  100. package/src/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +89 -0
  101. package/src/plugins_core/functions/error.js +163 -0
  102. package/src/plugins_core/functions/feature_flags.js +6 -0
  103. package/src/plugins_core/functions/index.js +161 -0
  104. package/src/plugins_core/functions/utils.js +66 -0
  105. package/src/plugins_core/functions/zisi.js +56 -0
  106. package/src/plugins_core/functions_install/index.js +13 -0
  107. package/src/plugins_core/functions_install/manifest.yml +1 -0
  108. package/src/plugins_core/list.js +27 -0
  109. package/src/status/add.js +36 -0
  110. package/src/status/colors.js +23 -0
  111. package/src/status/load_error.js +11 -0
  112. package/src/status/report.js +137 -0
  113. package/src/status/success.js +18 -0
  114. package/src/steps/core_step.js +92 -0
  115. package/src/steps/error.js +102 -0
  116. package/src/steps/get.js +51 -0
  117. package/src/steps/plugin.js +85 -0
  118. package/src/steps/return.js +52 -0
  119. package/src/steps/run_core_steps.js +200 -0
  120. package/src/steps/run_step.js +304 -0
  121. package/src/steps/run_steps.js +179 -0
  122. package/src/steps/update_config.js +93 -0
  123. package/src/telemetry/main.js +136 -0
  124. package/src/time/aggregate.js +146 -0
  125. package/src/time/main.js +48 -0
  126. package/src/time/measure.js +22 -0
  127. package/src/time/report.js +59 -0
  128. package/src/utils/errors.js +12 -0
  129. package/src/utils/json.js +19 -0
  130. package/src/utils/omit.js +6 -0
  131. package/src/utils/package.js +23 -0
  132. package/src/utils/remove_falsy.js +10 -0
  133. package/src/utils/resolve.js +46 -0
  134. package/src/utils/runtime.js +5 -0
  135. package/src/utils/semver.js +34 -0
@@ -0,0 +1,93 @@
1
+ import { isDeepStrictEqual } from 'util'
2
+
3
+ import pFilter from 'p-filter'
4
+ import { pathExists } from 'path-exists'
5
+
6
+ import { resolveUpdatedConfig } from '../core/config.js'
7
+ import { addErrorInfo } from '../error/info.js'
8
+ import { logConfigOnUpdate } from '../log/messages/config.js'
9
+ import { logConfigMutations } from '../log/messages/mutations.js'
10
+
11
+ // If `netlifyConfig` was updated or `_redirects` was created, the configuration
12
+ // is updated by calling `@netlify/config` again.
13
+ export const updateNetlifyConfig = async function ({
14
+ configOpts,
15
+ netlifyConfig,
16
+ headersPath,
17
+ redirectsPath,
18
+ configMutations,
19
+ newConfigMutations,
20
+ configSideFiles,
21
+ errorParams,
22
+ logs,
23
+ debug,
24
+ }) {
25
+ if (!(await shouldUpdateConfig({ newConfigMutations, configSideFiles, headersPath, redirectsPath }))) {
26
+ return { netlifyConfig, configMutations }
27
+ }
28
+
29
+ validateConfigMutations(newConfigMutations)
30
+ logConfigMutations(logs, newConfigMutations, debug)
31
+ const configMutationsA = [...configMutations, ...newConfigMutations]
32
+ const {
33
+ config: netlifyConfigA,
34
+ headersPath: headersPathA,
35
+ redirectsPath: redirectsPathA,
36
+ } = await resolveUpdatedConfig(configOpts, configMutationsA)
37
+ logConfigOnUpdate({ logs, netlifyConfig: netlifyConfigA, debug })
38
+ // eslint-disable-next-line fp/no-mutation,no-param-reassign
39
+ errorParams.netlifyConfig = netlifyConfigA
40
+ return {
41
+ netlifyConfig: netlifyConfigA,
42
+ configMutations: configMutationsA,
43
+ headersPath: headersPathA,
44
+ redirectsPath: redirectsPathA,
45
+ }
46
+ }
47
+
48
+ const shouldUpdateConfig = async function ({ newConfigMutations, configSideFiles, headersPath, redirectsPath }) {
49
+ return (
50
+ newConfigMutations.length !== 0 || (await haveConfigSideFilesChanged(configSideFiles, headersPath, redirectsPath))
51
+ )
52
+ }
53
+
54
+ // The configuration mostly depends on `netlify.toml` and UI build settings.
55
+ // However, it also uses some additional optional side files like `_redirects`.
56
+ // Those are often created by the build command. When those are created, we need
57
+ // to update the configuration. We detect this by checking for file existence
58
+ // before and after running plugins and the build command.
59
+ const haveConfigSideFilesChanged = async function (configSideFiles, headersPath, redirectsPath) {
60
+ const newSideFiles = await listConfigSideFiles([headersPath, redirectsPath])
61
+ return !isDeepStrictEqual(newSideFiles, configSideFiles)
62
+ }
63
+
64
+ // List all the files used for configuration besides `netlify.toml`.
65
+ // This is useful when applying configuration mutations since those files
66
+ // sometimes have higher priority and should therefore be deleted in order to
67
+ // apply any configuration update on `netlify.toml`.
68
+ export const listConfigSideFiles = async function (sideFiles) {
69
+ const configSideFiles = await pFilter(sideFiles, pathExists)
70
+ // eslint-disable-next-line fp/no-mutating-methods
71
+ return configSideFiles.sort()
72
+ }
73
+
74
+ // Validate each new configuration change
75
+ const validateConfigMutations = function (newConfigMutations) {
76
+ try {
77
+ newConfigMutations.forEach(validateConfigMutation)
78
+ } catch (error) {
79
+ addErrorInfo(error, { type: 'pluginValidation' })
80
+ throw error
81
+ }
82
+ }
83
+
84
+ // Triggered when calling `netlifyConfig.{key} = undefined | null`
85
+ // We do not allow this because the back-end only receives mutations as a
86
+ // `netlify.toml`, i.e. cannot apply property deletions since `undefined` is
87
+ // not serializable in TOML.
88
+ const validateConfigMutation = function ({ value, keysString }) {
89
+ if (value === undefined || value === null) {
90
+ throw new Error(`Setting "netlifyConfig.${keysString}" to ${value} is not allowed.
91
+ Please set this property to a specific value instead.`)
92
+ }
93
+ }
@@ -0,0 +1,136 @@
1
+ import { platform } from 'process'
2
+
3
+ import got from 'got'
4
+ import osName from 'os-name'
5
+
6
+ import { addErrorInfo } from '../error/info.js'
7
+ import { roundTimerToMillisecs } from '../time/measure.js'
8
+ import { ROOT_PACKAGE_JSON } from '../utils/json.js'
9
+
10
+ const DEFAULT_TELEMETRY_TIMEOUT = 1200
11
+ const DEFAULT_TELEMETRY_CONFIG = {
12
+ origin: 'https://api.segment.io/v1',
13
+ writeKey: 'dWhlM1lYSlpNd1k5Uk9rcjFra2JSOEoybnRjZjl0YTI6',
14
+ timeout: DEFAULT_TELEMETRY_TIMEOUT,
15
+ }
16
+
17
+ // Send telemetry request when build completes
18
+ export const trackBuildComplete = async function ({
19
+ deployId,
20
+ buildId,
21
+ status,
22
+ stepsCount,
23
+ pluginsOptions,
24
+ durationNs,
25
+ siteInfo,
26
+ telemetry,
27
+ userNodeVersion,
28
+ framework,
29
+ testOpts: { telemetryOrigin = DEFAULT_TELEMETRY_CONFIG.origin, telemetryTimeout = DEFAULT_TELEMETRY_CONFIG.timeout },
30
+ }) {
31
+ if (!telemetry) {
32
+ return
33
+ }
34
+
35
+ try {
36
+ const payload = getPayload({
37
+ deployId,
38
+ buildId,
39
+ status,
40
+ stepsCount,
41
+ pluginsOptions,
42
+ durationNs,
43
+ siteInfo,
44
+ userNodeVersion,
45
+ framework,
46
+ })
47
+ await track({
48
+ payload,
49
+ config: { ...DEFAULT_TELEMETRY_CONFIG, origin: telemetryOrigin, timeout: telemetryTimeout },
50
+ })
51
+ } catch (error) {
52
+ addErrorInfo(error, { type: 'telemetry' })
53
+ throw error
54
+ }
55
+ }
56
+
57
+ // Send track HTTP request to telemetry.
58
+ const track = async function ({ payload, config: { origin, writeKey, timeout } }) {
59
+ const url = `${origin}/track`
60
+ await got.post(url, {
61
+ json: payload,
62
+ timeout,
63
+ retry: 0,
64
+ headers: { Authorization: `Basic ${writeKey}` },
65
+ })
66
+ }
67
+
68
+ // Retrieve telemetry information
69
+ // siteInfo can be empty if the build fails during the get config step
70
+ const getPayload = function ({
71
+ deployId,
72
+ buildId,
73
+ status,
74
+ stepsCount,
75
+ pluginsOptions,
76
+ durationNs,
77
+ userNodeVersion,
78
+ siteInfo = {},
79
+ framework,
80
+ }) {
81
+ return {
82
+ userId: 'buildbot_user',
83
+ event: 'build:ci_build_process_completed',
84
+ timestamp: Date.now(),
85
+ properties: {
86
+ deployId,
87
+ buildId,
88
+ status,
89
+ steps: stepsCount,
90
+ buildVersion: ROOT_PACKAGE_JSON.version,
91
+ // We're passing the node version set by the buildbot/user which will run the `build.command` and
92
+ // the `package.json`/locally defined plugins
93
+ nodeVersion: userNodeVersion,
94
+ osPlatform: OS_TYPES[platform],
95
+ osName: osName(),
96
+ framework,
97
+ siteId: siteInfo.id,
98
+ ...(pluginsOptions !== undefined && {
99
+ plugins: pluginsOptions.map(getPlugin),
100
+ pluginCount: pluginsOptions.length,
101
+ }),
102
+ ...(durationNs !== undefined && { duration: roundTimerToMillisecs(durationNs) }),
103
+ },
104
+ }
105
+ }
106
+
107
+ const OS_TYPES = {
108
+ linux: 'Linux',
109
+ darwin: 'MacOS',
110
+ win32: 'Windows',
111
+ android: 'Android',
112
+ sunos: 'SunOS',
113
+ aix: 'AIX',
114
+ freebsd: 'FreeBSD',
115
+ openbsd: 'OpenBSD',
116
+ }
117
+
118
+ const getPlugin = function ({
119
+ packageName,
120
+ origin,
121
+ loadedFrom,
122
+ nodeVersion,
123
+ pinnedVersion,
124
+ pluginPackageJson: { version } = {},
125
+ }) {
126
+ const installType = getInstallType(origin, loadedFrom)
127
+ return { name: packageName, installType, nodeVersion, pinnedVersion, version }
128
+ }
129
+
130
+ const getInstallType = function (origin, loadedFrom) {
131
+ if (loadedFrom === 'auto_install') {
132
+ return origin === 'ui' ? 'ui' : 'netlify_toml'
133
+ }
134
+
135
+ return loadedFrom
136
+ }
@@ -0,0 +1,146 @@
1
+ import { createTimer, TOP_PARENT_TAG } from './main.js'
2
+
3
+ // Some timers are computed based on others:
4
+ // - `others` is `total` minus the other timers
5
+ // - `run_plugins` is the sum of all plugins
6
+ // - `run_netlify_build_per_type` aggregates timers but per system/plugin/user
7
+ // - each plugin timer is the sum of its event handlers
8
+ export const addAggregatedTimers = function (timers) {
9
+ const timersA = addRunPluginsTimer(timers)
10
+ const timersB = addPluginTimers(timersA)
11
+ const timersC = addOthersTimers(timersB)
12
+ const timersD = addTypeTimers(timersC)
13
+ return timersD
14
+ }
15
+
16
+ // Having a `total` timer is redundant since the buildbot already measures this.
17
+ // The buildbot measurement is better since it includes the time to load Node.
18
+ // Instead, we only use `total` to measure what did not get `measureDuration()`.
19
+ const addOthersTimers = function (timers) {
20
+ const totalTimer = timers.find(isTotalTimer)
21
+ const timersA = timers.filter((timer) => !isTotalTimer(timer))
22
+ const topTimers = timersA.filter(isTopTimer)
23
+ const othersTimer = createOthersTimer(topTimers, totalTimer)
24
+ return [...timersA, othersTimer]
25
+ }
26
+
27
+ const isTotalTimer = function ({ stageTag, parentTag }) {
28
+ return stageTag === 'total' && parentTag === 'build_site'
29
+ }
30
+
31
+ const createOthersTimer = function (topTimers, { durationNs: totalTimerDurationNs }) {
32
+ const topTimersDurationNs = computeTimersDuration(topTimers)
33
+ const otherTimersDurationNs = Math.max(0, totalTimerDurationNs - topTimersDurationNs)
34
+ const othersTimer = createTimer(OTHERS_STAGE_TAG, otherTimersDurationNs)
35
+ return othersTimer
36
+ }
37
+
38
+ const OTHERS_STAGE_TAG = 'others'
39
+
40
+ // Measure the total time running plugins
41
+ const addRunPluginsTimer = function (timers) {
42
+ const pluginsTimers = timers.filter(isPluginTimer)
43
+ return pluginsTimers.length === 0 ? timers : [...timers, createSumTimer(pluginsTimers, RUN_PLUGINS_STAGE_TAG)]
44
+ }
45
+
46
+ const RUN_PLUGINS_STAGE_TAG = 'run_plugins'
47
+
48
+ // Retrieve one timer for each plugin, summing all its individual timers
49
+ // (one per event handler)
50
+ const addPluginTimers = function (timers) {
51
+ const timersA = timers.filter((timer) => !isCommunityPluginTimer(timer))
52
+ const pluginsTimers = timersA.filter(isPluginTimer)
53
+ if (pluginsTimers.length === 0) {
54
+ return timersA
55
+ }
56
+
57
+ const pluginPackages = getPluginPackages(pluginsTimers)
58
+ const wholePluginsTimers = pluginPackages.map((pluginPackage) => getWholePluginTimer(pluginPackage, pluginsTimers))
59
+ return [...timersA, ...wholePluginsTimers]
60
+ }
61
+
62
+ // We only measure plugins maintained by us, not community
63
+ const isCommunityPluginTimer = function (timer) {
64
+ return isPluginTimer(timer) && !isNetlifyMaintainedPlugin(getPluginTimerPackage(timer))
65
+ }
66
+
67
+ const isNetlifyMaintainedPlugin = function (pluginPackage) {
68
+ return NETLIFY_MAINTAINED_PLUGINS.has(pluginPackage)
69
+ }
70
+
71
+ const NETLIFY_MAINTAINED_PLUGINS = new Set([
72
+ 'netlify_plugin_gatsby_cache',
73
+ 'netlify_plugin_sitemap',
74
+ 'netlify_plugin_debug_cache',
75
+ 'netlify_plugin_is_website_vulnerable',
76
+ 'netlify_plugin_lighthouse',
77
+ 'netlify_plugin_nextjs',
78
+ 'netlify_plugin_gatsby',
79
+ ])
80
+
81
+ const getPluginPackages = function (pluginsTimers) {
82
+ const pluginPackages = pluginsTimers.map(getPluginTimerPackage)
83
+ return [...new Set(pluginPackages)]
84
+ }
85
+
86
+ const getWholePluginTimer = function (pluginPackage, pluginsTimers) {
87
+ const pluginTimers = pluginsTimers.filter((pluginTimer) => getPluginTimerPackage(pluginTimer) === pluginPackage)
88
+ const wholePluginsTimer = createSumTimer(pluginTimers, pluginPackage, RUN_PLUGINS_STAGE_TAG)
89
+ return wholePluginsTimer
90
+ }
91
+
92
+ const getPluginTimerPackage = function ({ parentTag }) {
93
+ return parentTag
94
+ }
95
+
96
+ // Check if a timer relates to a Build plugin
97
+ const isPluginTimer = function ({ category }) {
98
+ return category === 'pluginEvent'
99
+ }
100
+
101
+ // Reports total time depending on whether it was system, plugin or user
102
+ const addTypeTimers = function (timers) {
103
+ const topTimers = timers.filter(isTopTimer)
104
+ const typeTimers = TYPE_TIMERS.map(({ name, stages }) => getTypeTimer(name, stages, topTimers)).filter(Boolean)
105
+ return [...timers, ...typeTimers]
106
+ }
107
+
108
+ const TYPE_TIMERS = [
109
+ { name: 'system', stages: ['resolve_config', 'get_plugins_options', 'start_plugins', 'others'] },
110
+ { name: 'plugin', stages: ['load_plugins', 'run_plugins'] },
111
+ { name: 'user', stages: ['build_command', 'functions_bundling', 'deploy_site'] },
112
+ ]
113
+
114
+ const getTypeTimer = function (name, stages, topTimers) {
115
+ const topTimersA = topTimers.filter(({ stageTag }) => stages.includes(stageTag))
116
+
117
+ if (topTimersA.length === 0) {
118
+ return
119
+ }
120
+
121
+ const typeTimer = createSumTimer(topTimersA, name, 'run_netlify_build_per_type')
122
+ return typeTimer
123
+ }
124
+
125
+ const isTopTimer = function ({ parentTag }) {
126
+ return parentTag === TOP_PARENT_TAG
127
+ }
128
+
129
+ // Creates a timer that sums up the duration of several others
130
+ const createSumTimer = function (timers, stageTag, parentTag) {
131
+ const durationNs = computeTimersDuration(timers)
132
+ const timer = createTimer(stageTag, durationNs, { parentTag })
133
+ return timer
134
+ }
135
+
136
+ const computeTimersDuration = function (timers) {
137
+ return timers.map(getTimerDuration).reduce(reduceSum, 0)
138
+ }
139
+
140
+ const getTimerDuration = function ({ durationNs }) {
141
+ return durationNs
142
+ }
143
+
144
+ const reduceSum = function (sum, number) {
145
+ return sum + number
146
+ }
@@ -0,0 +1,48 @@
1
+ import slugify from '@sindresorhus/slugify'
2
+ import keepFuncProps from 'keep-func-props'
3
+
4
+ import { startTimer, endTimer } from './measure.js'
5
+
6
+ // Initialize the `timers` array
7
+ export const initTimers = function () {
8
+ return []
9
+ }
10
+
11
+ // Wrap an async function to measure how long it takes.
12
+ // The function must:
13
+ // - take a plain object as first argument. This must contain a `timers`.
14
+ // - return a plain object. This may or may not contain a modified `timers`.
15
+ // The `durationNs` will be returned by the function. A new `timers` with the
16
+ // additional duration timer will be returned as well.
17
+ const kMeasureDuration = function (func, stageTag, { parentTag, category } = {}) {
18
+ return async function measuredFunc({ timers, ...opts }, ...args) {
19
+ const timerNs = startTimer()
20
+ const { timers: timersA = timers, ...returnObject } = await func({ timers, ...opts }, ...args)
21
+ const { tags = {} } = returnObject
22
+ const durationNs = endTimer(timerNs)
23
+ const timer = createTimer(stageTag, durationNs, { parentTag, category, tags })
24
+ const timersB = [...timersA, timer]
25
+ return { ...returnObject, timers: timersB, durationNs }
26
+ }
27
+ }
28
+
29
+ // Ensure the wrapped function `name` is not `anonymous` in stack traces
30
+ export const measureDuration = keepFuncProps(kMeasureDuration)
31
+
32
+ // Create a new object representing a completed timer
33
+ export const createTimer = function (
34
+ stageTag,
35
+ durationNs,
36
+ { metricName = DEFAULT_METRIC_NAME, parentTag = TOP_PARENT_TAG, category, tags } = {},
37
+ ) {
38
+ return { metricName, stageTag, parentTag, durationNs, category, tags }
39
+ }
40
+
41
+ const DEFAULT_METRIC_NAME = 'buildbot.build.stage.duration'
42
+ export const TOP_PARENT_TAG = 'run_netlify_build'
43
+
44
+ // Make sure the timer name does not include special characters.
45
+ // For example, the `packageName` of local plugins includes dots.
46
+ export const normalizeTimerName = function (name) {
47
+ return slugify(name, { separator: '_' })
48
+ }
@@ -0,0 +1,22 @@
1
+ import { hrtime } from 'process'
2
+
3
+ // Starts a timer
4
+ export const startTimer = function () {
5
+ return hrtime()
6
+ }
7
+
8
+ // Stops a timer
9
+ export const endTimer = function ([startSecs, startNsecs]) {
10
+ const [endSecs, endNsecs] = hrtime()
11
+ const durationNs = (endSecs - startSecs) * NANOSECS_TO_SECS + endNsecs - startNsecs
12
+ return durationNs
13
+ }
14
+
15
+ // statsd expects milliseconds integers.
16
+ // To prevent double rounding errors, rounding should only be applied once.
17
+ export const roundTimerToMillisecs = function (durationNs) {
18
+ return Math.round(durationNs / NANOSECS_TO_MSECS)
19
+ }
20
+
21
+ const NANOSECS_TO_SECS = 1e9
22
+ const NANOSECS_TO_MSECS = 1e6
@@ -0,0 +1,59 @@
1
+ import { promisify } from 'util'
2
+
3
+ import StatsdClient from 'statsd-client'
4
+
5
+ import { addAggregatedTimers } from './aggregate.js'
6
+ import { roundTimerToMillisecs } from './measure.js'
7
+
8
+ // TODO: replace with `timers/promises` after dropping Node < 15.0.0
9
+ const pSetTimeout = promisify(setTimeout)
10
+
11
+ // Record the duration of a build phase, for monitoring.
12
+ // Sends to statsd daemon.
13
+ export const reportTimers = async function ({ timers, statsdOpts: { host, port }, framework }) {
14
+ if (host === undefined) {
15
+ return
16
+ }
17
+
18
+ const timersA = addAggregatedTimers(timers)
19
+ await sendTimers({ timers: timersA, host, port, framework })
20
+ }
21
+
22
+ const sendTimers = async function ({ timers, host, port, framework }) {
23
+ const client = await startClient(host, port)
24
+ timers.forEach((timer) => {
25
+ sendTimer({ timer, client, framework })
26
+ })
27
+ await closeClient(client)
28
+ }
29
+
30
+ // The socket creation is delayed until the first packet is sent. In our
31
+ // case, this happens just before `client.close()` is called, which is too
32
+ // late and make it not send anything. We need to manually create it using
33
+ // the internal API.
34
+ const startClient = async function (host, port) {
35
+ const client = new StatsdClient({ host, port, socketTimeout: 0 })
36
+ // eslint-disable-next-line no-underscore-dangle
37
+ await promisify(client._socket._createSocket.bind(client._socket))()
38
+ return client
39
+ }
40
+
41
+ const sendTimer = function ({ timer: { metricName, stageTag, parentTag, durationNs, tags }, client, framework }) {
42
+ const durationMs = roundTimerToMillisecs(durationNs)
43
+ const frameworkTag = framework === undefined ? {} : { framework }
44
+ client.distribution(metricName, durationMs, { stage: stageTag, parent: parentTag, ...tags, ...frameworkTag })
45
+ }
46
+
47
+ // UDP packets are buffered and flushed at regular intervals by statsd-client.
48
+ // Closing force flushing all of them.
49
+ const closeClient = async function (client) {
50
+ client.close()
51
+
52
+ // statsd-clent does not provide with a way of knowing when the socket is done
53
+ // closing, so we need to use the following hack.
54
+ await pSetTimeout(CLOSE_TIMEOUT)
55
+ await pSetTimeout(CLOSE_TIMEOUT)
56
+ }
57
+
58
+ // See https://github.com/msiebuhr/node-statsd-client/blob/45a93ee4c94ca72f244a40b06cb542d4bd7c3766/lib/EphemeralSocket.js#L81
59
+ const CLOSE_TIMEOUT = 11
@@ -0,0 +1,12 @@
1
+ // Wrap an async function so it prepends an error message on exceptions.
2
+ // This helps locate errors.
3
+ export const addAsyncErrorMessage = function (asyncFunc, message) {
4
+ return async (...args) => {
5
+ try {
6
+ return await asyncFunc(...args)
7
+ } catch (error) {
8
+ error.stack = `${message}: ${error.stack}`
9
+ throw error
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,19 @@
1
+ import { promises as fs, readFileSync } from 'fs'
2
+ import { fileURLToPath } from 'url'
3
+
4
+ const ROOT_PACKAGE_JSON_PATH = fileURLToPath(new URL('../../package.json', import.meta.url))
5
+
6
+ // TODO: Replace with dynamic `import()` once it is supported without
7
+ // experimental flags
8
+ export const importJsonFile = async function (filePath) {
9
+ const fileContents = await fs.readFile(filePath)
10
+ return JSON.parse(fileContents)
11
+ }
12
+
13
+ const importJsonFileSync = function (filePath) {
14
+ // Use sync I/O so it is easier to migrate to `import()` later on
15
+ const fileContents = readFileSync(filePath, 'utf8')
16
+ return JSON.parse(fileContents)
17
+ }
18
+
19
+ export const ROOT_PACKAGE_JSON = importJsonFileSync(ROOT_PACKAGE_JSON_PATH)
@@ -0,0 +1,6 @@
1
+ import filterObj from 'filter-obj'
2
+
3
+ // lodash.omit is 1400 lines of codes. filter-obj is much smaller and simpler.
4
+ export const omit = function (obj, keys) {
5
+ return filterObj(obj, (key) => !keys.includes(key))
6
+ }
@@ -0,0 +1,23 @@
1
+ import { dirname } from 'path'
2
+
3
+ import { readPackageUp } from 'read-pkg-up'
4
+
5
+ // Retrieve `package.json` from a specific directory
6
+ export const getPackageJson = async function (cwd, { normalize } = {}) {
7
+ const packageObj = await getPackageObj({ cwd, normalize })
8
+ if (packageObj === undefined) {
9
+ return { packageJson: {} }
10
+ }
11
+
12
+ const { path, packageJson } = packageObj
13
+ const packageDir = dirname(path)
14
+ return { packageDir, packageJson }
15
+ }
16
+
17
+ const getPackageObj = async function ({ cwd, normalize = true }) {
18
+ try {
19
+ return await readPackageUp({ cwd, normalize })
20
+ // If the `package.json` is invalid and `normalize` is `true`, an error is
21
+ // thrown. We return `undefined` then.
22
+ } catch {}
23
+ }
@@ -0,0 +1,10 @@
1
+ import filterObj from 'filter-obj'
2
+
3
+ // Remove falsy values from object
4
+ export const removeFalsy = function (obj) {
5
+ return filterObj(obj, isDefined)
6
+ }
7
+
8
+ const isDefined = function (key, value) {
9
+ return value !== undefined && value !== ''
10
+ }
@@ -0,0 +1,46 @@
1
+ import { createRequire } from 'module'
2
+
3
+ import { async as resolveLib } from 'resolve'
4
+
5
+ // TODO: use `import.resolve()` once it is available without any experimental
6
+ // flags
7
+ const require = createRequire(import.meta.url)
8
+
9
+ // Like `resolvePath()` but does not throw
10
+ export const tryResolvePath = async function (path, basedir) {
11
+ try {
12
+ const resolvedPath = await resolvePath(path, basedir)
13
+ return { path: resolvedPath }
14
+ } catch (error) {
15
+ return { error }
16
+ }
17
+ }
18
+
19
+ // This throws if the package cannot be found
20
+ export const resolvePath = async function (path, basedir) {
21
+ try {
22
+ return await resolvePathWithBasedir(path, basedir)
23
+ // Fallback.
24
+ // `resolve` sometimes gives unhelpful error messages.
25
+ // https://github.com/browserify/resolve/issues/223
26
+ } catch {
27
+ return require.resolve(path, { paths: [basedir] })
28
+ }
29
+ }
30
+
31
+ // Like `require.resolve()` but as an external library.
32
+ // We need to use `new Promise()` due to a bug with `utils.promisify()` on
33
+ // `resolve`:
34
+ // https://github.com/browserify/resolve/issues/151#issuecomment-368210310
35
+ const resolvePathWithBasedir = function (path, basedir) {
36
+ return new Promise((resolve, reject) => {
37
+ // eslint-disable-next-line promise/prefer-await-to-callbacks
38
+ resolveLib(path, { basedir }, (error, resolvedPath) => {
39
+ if (error) {
40
+ return reject(error)
41
+ }
42
+
43
+ resolve(resolvedPath)
44
+ })
45
+ })
46
+ }
@@ -0,0 +1,5 @@
1
+ export const isRuntime = function (pluginOption) {
2
+ const { packageName } = pluginOption
3
+ // Make this a bit more robust in the future
4
+ return ['@netlify/next-runtime', '@netlify/plugin-nextjs'].includes(packageName)
5
+ }
@@ -0,0 +1,34 @@
1
+ import semver from 'semver'
2
+
3
+ // Compare two versions by their major versions.
4
+ // Takes into account the special rules for `0.*.*` and `0.0.*` versions.
5
+ export const isPreviousMajor = function (versionA, versionB) {
6
+ return semver.lt(getMajor(versionA), getMajor(versionB))
7
+ }
8
+
9
+ // Remove minor/patch numbers
10
+ const getMajor = function (version) {
11
+ return semver.minVersion(getMajorVersion(version)).version
12
+ }
13
+
14
+ // According to semver, the second number is the major release number for
15
+ // `0.*.*` versions and the third for `0.0.*`. This is how `^` behaves with the
16
+ // `semver` module which is used by `npm`.
17
+ export const getMajorVersion = function (version) {
18
+ if (!version || semver.clean(version) === null) {
19
+ return
20
+ }
21
+
22
+ const majorVersion = semver.major(version)
23
+ if (majorVersion !== 0) {
24
+ return `${majorVersion}`
25
+ }
26
+
27
+ const minorVersion = semver.minor(version)
28
+ if (minorVersion !== 0) {
29
+ return `${majorVersion}.${minorVersion}`
30
+ }
31
+
32
+ const patchVersion = semver.patch(version)
33
+ return `${majorVersion}.${minorVersion}.${patchVersion}`
34
+ }