@netlify/build 28.0.1-beta → 28.0.1

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 (259) hide show
  1. package/bin.js +5 -0
  2. package/lib/core/bin.js +66 -0
  3. package/lib/core/build.js +355 -0
  4. package/lib/core/config.js +121 -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 +19 -0
  9. package/lib/core/flags.js +201 -0
  10. package/lib/core/lingering.js +68 -0
  11. package/lib/core/main.js +105 -0
  12. package/lib/core/missing_side_file.js +17 -0
  13. package/lib/core/normalize_flags.js +59 -0
  14. package/lib/core/severity.js +21 -0
  15. package/lib/core/types.js +8 -0
  16. package/lib/core/user_node_version.js +32 -0
  17. package/lib/env/changes.js +43 -0
  18. package/lib/env/main.js +14 -0
  19. package/lib/env/metadata.js +68 -0
  20. package/lib/error/api.js +37 -0
  21. package/lib/error/build.js +36 -0
  22. package/{src → lib}/error/cancel.js +5 -6
  23. package/lib/error/colors.js +9 -0
  24. package/lib/error/handle.js +46 -0
  25. package/lib/error/info.js +37 -0
  26. package/lib/error/monitor/location.js +16 -0
  27. package/lib/error/monitor/normalize.js +86 -0
  28. package/lib/error/monitor/print.js +20 -0
  29. package/lib/error/monitor/report.js +120 -0
  30. package/lib/error/monitor/start.js +61 -0
  31. package/lib/error/parse/clean_stack.js +70 -0
  32. package/lib/error/parse/location.js +50 -0
  33. package/lib/error/parse/normalize.js +24 -0
  34. package/lib/error/parse/parse.js +67 -0
  35. package/lib/error/parse/plugin.js +55 -0
  36. package/lib/error/parse/properties.js +16 -0
  37. package/lib/error/parse/serialize_log.js +34 -0
  38. package/lib/error/parse/serialize_status.js +18 -0
  39. package/lib/error/parse/stack.js +34 -0
  40. package/lib/error/type.js +171 -0
  41. package/lib/install/functions.js +20 -0
  42. package/lib/install/local.js +45 -0
  43. package/lib/install/main.js +67 -0
  44. package/lib/install/missing.js +54 -0
  45. package/{src → lib}/log/colors.js +15 -22
  46. package/lib/log/description.js +21 -0
  47. package/lib/log/header.js +14 -0
  48. package/lib/log/header_func.js +13 -0
  49. package/lib/log/logger.js +140 -0
  50. package/lib/log/messages/compatibility.js +120 -0
  51. package/lib/log/messages/config.js +91 -0
  52. package/lib/log/messages/core.js +50 -0
  53. package/lib/log/messages/core_steps.js +75 -0
  54. package/lib/log/messages/dry.js +41 -0
  55. package/lib/log/messages/install.js +25 -0
  56. package/lib/log/messages/ipc.js +29 -0
  57. package/lib/log/messages/mutations.js +62 -0
  58. package/{src → lib}/log/messages/plugins.js +18 -32
  59. package/lib/log/messages/status.js +14 -0
  60. package/lib/log/messages/steps.js +18 -0
  61. package/lib/log/old_version.js +32 -0
  62. package/lib/log/serialize.js +10 -0
  63. package/lib/log/stream.js +68 -0
  64. package/lib/log/theme.js +25 -0
  65. package/lib/plugins/child/diff.js +46 -0
  66. package/lib/plugins/child/error.js +26 -0
  67. package/lib/plugins/child/lazy.js +15 -0
  68. package/lib/plugins/child/load.js +22 -0
  69. package/lib/plugins/child/logic.js +57 -0
  70. package/lib/plugins/child/main.js +37 -0
  71. package/lib/plugins/child/run.js +19 -0
  72. package/lib/plugins/child/status.js +63 -0
  73. package/lib/plugins/child/typescript.js +28 -0
  74. package/lib/plugins/child/utils.js +42 -0
  75. package/lib/plugins/child/validate.js +31 -0
  76. package/lib/plugins/compatibility.js +104 -0
  77. package/{src → lib}/plugins/error.js +31 -35
  78. package/{src → lib}/plugins/events.js +7 -12
  79. package/lib/plugins/expected_version.js +81 -0
  80. package/lib/plugins/ipc.js +120 -0
  81. package/lib/plugins/list.js +73 -0
  82. package/lib/plugins/load.js +50 -0
  83. package/lib/plugins/manifest/check.js +85 -0
  84. package/lib/plugins/manifest/load.js +38 -0
  85. package/lib/plugins/manifest/main.js +17 -0
  86. package/lib/plugins/manifest/path.js +24 -0
  87. package/lib/plugins/manifest/validate.js +91 -0
  88. package/lib/plugins/node_version.js +30 -0
  89. package/lib/plugins/options.js +55 -0
  90. package/lib/plugins/pinned_version.js +83 -0
  91. package/lib/plugins/resolve.js +110 -0
  92. package/lib/plugins/spawn.js +54 -0
  93. package/lib/plugins_core/add.js +35 -0
  94. package/lib/plugins_core/build_command.js +50 -0
  95. package/lib/plugins_core/deploy/buildbot_client.js +87 -0
  96. package/lib/plugins_core/deploy/index.js +49 -0
  97. package/{src → lib}/plugins_core/deploy/manifest.yml +0 -0
  98. package/lib/plugins_core/edge_functions/index.js +79 -0
  99. package/lib/plugins_core/edge_functions/lib/error.js +17 -0
  100. package/lib/plugins_core/edge_functions/lib/internal_manifest.js +50 -0
  101. package/lib/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js +75 -0
  102. package/lib/plugins_core/functions/error.js +123 -0
  103. package/lib/plugins_core/functions/feature_flags.js +6 -0
  104. package/lib/plugins_core/functions/index.js +114 -0
  105. package/lib/plugins_core/functions/utils.js +45 -0
  106. package/lib/plugins_core/functions/zisi.js +39 -0
  107. package/{src → lib}/plugins_core/functions_install/index.js +8 -11
  108. package/{src → lib}/plugins_core/functions_install/manifest.yml +0 -0
  109. package/lib/plugins_core/list.js +20 -0
  110. package/lib/status/add.js +30 -0
  111. package/lib/status/colors.js +18 -0
  112. package/lib/status/load_error.js +10 -0
  113. package/lib/status/report.js +83 -0
  114. package/lib/status/success.js +14 -0
  115. package/lib/steps/core_step.js +57 -0
  116. package/lib/steps/error.js +65 -0
  117. package/lib/steps/get.js +43 -0
  118. package/lib/steps/plugin.js +55 -0
  119. package/lib/steps/return.js +25 -0
  120. package/lib/steps/run_core_steps.js +126 -0
  121. package/lib/steps/run_step.js +188 -0
  122. package/lib/steps/run_steps.js +96 -0
  123. package/lib/steps/update_config.js +66 -0
  124. package/lib/telemetry/main.js +97 -0
  125. package/lib/time/aggregate.js +120 -0
  126. package/lib/time/main.js +37 -0
  127. package/lib/time/measure.js +18 -0
  128. package/lib/time/report.js +47 -0
  129. package/lib/utils/errors.js +13 -0
  130. package/lib/utils/json.js +15 -0
  131. package/{src → lib}/utils/omit.js +3 -4
  132. package/lib/utils/package.js +22 -0
  133. package/lib/utils/remove_falsy.js +8 -0
  134. package/lib/utils/resolve.js +41 -0
  135. package/lib/utils/runtime.js +5 -0
  136. package/lib/utils/semver.js +28 -0
  137. package/package.json +40 -20
  138. package/types/config/netlify_config.d.ts +4 -4
  139. package/types/netlify_plugin_constants.d.ts +8 -8
  140. package/src/core/bin.js +0 -83
  141. package/src/core/config.js +0 -186
  142. package/src/core/constants.js +0 -156
  143. package/src/core/dry.js +0 -39
  144. package/src/core/feature_flags.js +0 -21
  145. package/src/core/flags.js +0 -194
  146. package/src/core/lingering.js +0 -85
  147. package/src/core/main.js +0 -692
  148. package/src/core/missing_side_file.js +0 -29
  149. package/src/core/normalize_flags.js +0 -69
  150. package/src/core/severity.js +0 -22
  151. package/src/core/user_node_version.js +0 -41
  152. package/src/env/changes.js +0 -52
  153. package/src/env/main.js +0 -19
  154. package/src/env/metadata.js +0 -81
  155. package/src/error/api.js +0 -46
  156. package/src/error/build.js +0 -50
  157. package/src/error/colors.js +0 -11
  158. package/src/error/handle.js +0 -57
  159. package/src/error/info.js +0 -46
  160. package/src/error/monitor/location.js +0 -21
  161. package/src/error/monitor/normalize.js +0 -77
  162. package/src/error/monitor/print.js +0 -42
  163. package/src/error/monitor/report.js +0 -133
  164. package/src/error/monitor/start.js +0 -69
  165. package/src/error/parse/clean_stack.js +0 -87
  166. package/src/error/parse/location.js +0 -58
  167. package/src/error/parse/normalize.js +0 -29
  168. package/src/error/parse/parse.js +0 -97
  169. package/src/error/parse/plugin.js +0 -70
  170. package/src/error/parse/properties.js +0 -23
  171. package/src/error/parse/serialize_log.js +0 -42
  172. package/src/error/parse/serialize_status.js +0 -23
  173. package/src/error/parse/stack.js +0 -43
  174. package/src/error/type.js +0 -182
  175. package/src/install/functions.js +0 -28
  176. package/src/install/local.js +0 -62
  177. package/src/install/main.js +0 -81
  178. package/src/install/missing.js +0 -67
  179. package/src/log/description.js +0 -26
  180. package/src/log/header.js +0 -16
  181. package/src/log/header_func.js +0 -17
  182. package/src/log/logger.js +0 -107
  183. package/src/log/messages/compatibility.js +0 -164
  184. package/src/log/messages/config.js +0 -105
  185. package/src/log/messages/core.js +0 -70
  186. package/src/log/messages/core_steps.js +0 -104
  187. package/src/log/messages/dry.js +0 -63
  188. package/src/log/messages/install.js +0 -20
  189. package/src/log/messages/ipc.js +0 -38
  190. package/src/log/messages/mutations.js +0 -82
  191. package/src/log/messages/status.js +0 -16
  192. package/src/log/messages/steps.js +0 -22
  193. package/src/log/old_version.js +0 -41
  194. package/src/log/serialize.js +0 -13
  195. package/src/log/stream.js +0 -85
  196. package/src/log/theme.js +0 -26
  197. package/src/plugins/child/diff.js +0 -55
  198. package/src/plugins/child/error.js +0 -32
  199. package/src/plugins/child/lazy.js +0 -18
  200. package/src/plugins/child/load.js +0 -29
  201. package/src/plugins/child/logic.js +0 -57
  202. package/src/plugins/child/main.js +0 -51
  203. package/src/plugins/child/run.js +0 -28
  204. package/src/plugins/child/status.js +0 -67
  205. package/src/plugins/child/typescript.js +0 -45
  206. package/src/plugins/child/utils.js +0 -56
  207. package/src/plugins/child/validate.js +0 -34
  208. package/src/plugins/compatibility.js +0 -128
  209. package/src/plugins/expected_version.js +0 -119
  210. package/src/plugins/ipc.js +0 -145
  211. package/src/plugins/list.js +0 -86
  212. package/src/plugins/load.js +0 -70
  213. package/src/plugins/manifest/check.js +0 -106
  214. package/src/plugins/manifest/load.js +0 -41
  215. package/src/plugins/manifest/main.js +0 -22
  216. package/src/plugins/manifest/path.js +0 -31
  217. package/src/plugins/manifest/validate.js +0 -108
  218. package/src/plugins/node_version.js +0 -50
  219. package/src/plugins/options.js +0 -88
  220. package/src/plugins/pinned_version.js +0 -131
  221. package/src/plugins/resolve.js +0 -152
  222. package/src/plugins/spawn.js +0 -66
  223. package/src/plugins_core/add.js +0 -49
  224. package/src/plugins_core/build_command.js +0 -75
  225. package/src/plugins_core/deploy/buildbot_client.js +0 -102
  226. package/src/plugins_core/deploy/index.js +0 -73
  227. package/src/plugins_core/edge_functions/index.js +0 -107
  228. package/src/plugins_core/edge_functions/lib/internal_manifest.js +0 -54
  229. package/src/plugins_core/functions/error.js +0 -163
  230. package/src/plugins_core/functions/feature_flags.js +0 -6
  231. package/src/plugins_core/functions/index.js +0 -160
  232. package/src/plugins_core/functions/utils.js +0 -66
  233. package/src/plugins_core/functions/zisi.js +0 -53
  234. package/src/plugins_core/list.js +0 -27
  235. package/src/status/add.js +0 -36
  236. package/src/status/colors.js +0 -23
  237. package/src/status/load_error.js +0 -11
  238. package/src/status/report.js +0 -128
  239. package/src/status/success.js +0 -18
  240. package/src/steps/core_step.js +0 -90
  241. package/src/steps/error.js +0 -102
  242. package/src/steps/get.js +0 -32
  243. package/src/steps/plugin.js +0 -85
  244. package/src/steps/return.js +0 -52
  245. package/src/steps/run_core_steps.js +0 -194
  246. package/src/steps/run_step.js +0 -300
  247. package/src/steps/run_steps.js +0 -179
  248. package/src/steps/update_config.js +0 -93
  249. package/src/telemetry/main.js +0 -136
  250. package/src/time/aggregate.js +0 -146
  251. package/src/time/main.js +0 -48
  252. package/src/time/measure.js +0 -22
  253. package/src/time/report.js +0 -59
  254. package/src/utils/errors.js +0 -12
  255. package/src/utils/json.js +0 -19
  256. package/src/utils/package.js +0 -23
  257. package/src/utils/remove_falsy.js +0 -10
  258. package/src/utils/resolve.js +0 -46
  259. package/src/utils/semver.js +0 -34
@@ -1,146 +0,0 @@
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
- }
package/src/time/main.js DELETED
@@ -1,48 +0,0 @@
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
- }
@@ -1,22 +0,0 @@
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
@@ -1,59 +0,0 @@
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
@@ -1,12 +0,0 @@
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
- }
package/src/utils/json.js DELETED
@@ -1,19 +0,0 @@
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)
@@ -1,23 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,46 +0,0 @@
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
- }
@@ -1,34 +0,0 @@
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
- }