@atlaspack/utils 2.14.5-canary.26 → 2.14.5-canary.261

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 (192) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/benchmark.js +23 -0
  3. package/dist/DefaultMap.js +41 -0
  4. package/dist/Deferred.js +16 -0
  5. package/dist/PromiseQueue.js +107 -0
  6. package/dist/TapStream.js +23 -0
  7. package/dist/alternatives.js +97 -0
  8. package/dist/ansi-html.js +12 -0
  9. package/dist/blob.js +29 -0
  10. package/dist/bundle-url.js +32 -0
  11. package/dist/collection.js +106 -0
  12. package/dist/config.js +138 -0
  13. package/dist/countLines.js +12 -0
  14. package/dist/debounce.js +15 -0
  15. package/dist/debug-tools.js +39 -0
  16. package/dist/dependency-location.js +22 -0
  17. package/dist/escape-html.js +19 -0
  18. package/dist/generateBuildMetrics.js +111 -0
  19. package/dist/generateCertificate.js +124 -0
  20. package/dist/getCertificate.js +13 -0
  21. package/dist/getExisting.js +20 -0
  22. package/dist/getModuleParts.js +27 -0
  23. package/dist/getRootDir.js +46 -0
  24. package/dist/glob.js +129 -0
  25. package/dist/hash.js +45 -0
  26. package/dist/http-server.js +55 -0
  27. package/dist/index.js +146 -0
  28. package/dist/is-url.js +15 -0
  29. package/dist/isDirectoryInside.js +11 -0
  30. package/dist/objectHash.js +20 -0
  31. package/dist/openInBrowser.js +61 -0
  32. package/dist/parseCSSImport.js +14 -0
  33. package/dist/path.js +36 -0
  34. package/dist/prettifyTime.js +6 -0
  35. package/dist/prettyDiagnostic.js +104 -0
  36. package/dist/progress-message.js +31 -0
  37. package/dist/relativeBundlePath.js +13 -0
  38. package/dist/relativeUrl.js +11 -0
  39. package/dist/replaceBundleReferences.js +131 -0
  40. package/dist/schema.js +403 -0
  41. package/dist/shared-buffer.js +24 -0
  42. package/dist/sourcemap.js +121 -0
  43. package/dist/stream.js +69 -0
  44. package/dist/throttle.js +12 -0
  45. package/dist/urlJoin.js +22 -0
  46. package/lib/DefaultMap.js +42 -0
  47. package/lib/Deferred.js +30 -0
  48. package/lib/PromiseQueue.js +112 -0
  49. package/lib/TapStream.js +34 -0
  50. package/lib/alternatives.js +116 -0
  51. package/lib/ansi-html.js +20 -0
  52. package/lib/blob.js +40 -0
  53. package/lib/bundle-url.js +34 -0
  54. package/lib/collection.js +111 -0
  55. package/lib/config.js +174 -0
  56. package/lib/countLines.js +15 -0
  57. package/lib/debounce.js +18 -0
  58. package/lib/debug-tools.js +40 -0
  59. package/lib/dependency-location.js +21 -0
  60. package/lib/escape-html.js +22 -0
  61. package/lib/generateBuildMetrics.js +121 -0
  62. package/lib/generateCertificate.js +129 -0
  63. package/lib/getCertificate.js +18 -0
  64. package/lib/getExisting.js +25 -0
  65. package/lib/getModuleParts.js +30 -0
  66. package/lib/getRootDir.js +52 -0
  67. package/lib/glob.js +110 -0
  68. package/lib/hash.js +50 -0
  69. package/lib/http-server.js +85 -0
  70. package/lib/index.js +635 -37310
  71. package/lib/is-url.js +24 -0
  72. package/lib/isDirectoryInside.js +18 -0
  73. package/lib/objectHash.js +26 -0
  74. package/lib/openInBrowser.js +74 -0
  75. package/lib/parseCSSImport.js +15 -0
  76. package/lib/path.js +39 -0
  77. package/lib/prettifyTime.js +9 -0
  78. package/lib/prettyDiagnostic.js +150 -0
  79. package/lib/progress-message.js +39 -0
  80. package/lib/relativeBundlePath.js +22 -0
  81. package/lib/relativeUrl.js +24 -0
  82. package/lib/replaceBundleReferences.js +182 -0
  83. package/lib/schema.js +369 -0
  84. package/lib/shared-buffer.js +31 -0
  85. package/lib/sourcemap.js +138 -0
  86. package/lib/stream.js +76 -0
  87. package/lib/throttle.js +15 -0
  88. package/lib/types/DefaultMap.d.ts +13 -0
  89. package/lib/types/Deferred.d.ts +8 -0
  90. package/lib/types/PromiseQueue.d.ts +25 -0
  91. package/lib/types/TapStream.d.ts +6 -0
  92. package/lib/types/alternatives.d.ts +3 -0
  93. package/lib/types/ansi-html.d.ts +1 -0
  94. package/lib/types/blob.d.ts +4 -0
  95. package/lib/types/bundle-url.d.ts +4 -0
  96. package/lib/types/collection.d.ts +33 -0
  97. package/lib/types/config.d.ts +17 -0
  98. package/lib/types/countLines.d.ts +1 -0
  99. package/lib/types/debounce.d.ts +1 -0
  100. package/lib/types/debug-tools.d.ts +8 -0
  101. package/lib/types/dependency-location.d.ts +14 -0
  102. package/lib/types/escape-html.d.ts +1 -0
  103. package/lib/types/generateBuildMetrics.d.ts +18 -0
  104. package/lib/types/generateCertificate.d.ts +5 -0
  105. package/lib/types/getCertificate.d.ts +5 -0
  106. package/lib/types/getExisting.d.ts +8 -0
  107. package/lib/types/getModuleParts.d.ts +4 -0
  108. package/lib/types/getRootDir.d.ts +2 -0
  109. package/lib/types/glob.d.ts +9 -0
  110. package/lib/types/hash.d.ts +7 -0
  111. package/lib/types/http-server.d.ts +19 -0
  112. package/lib/types/index.d.ts +48 -0
  113. package/lib/types/is-url.d.ts +1 -0
  114. package/lib/types/isDirectoryInside.d.ts +2 -0
  115. package/lib/types/objectHash.d.ts +3 -0
  116. package/lib/types/openInBrowser.d.ts +1 -0
  117. package/lib/types/parseCSSImport.d.ts +1 -0
  118. package/lib/types/path.d.ts +8 -0
  119. package/lib/types/prettifyTime.d.ts +1 -0
  120. package/lib/types/prettyDiagnostic.d.ts +17 -0
  121. package/lib/types/progress-message.d.ts +3 -0
  122. package/lib/types/relativeBundlePath.d.ts +4 -0
  123. package/lib/types/relativeUrl.d.ts +1 -0
  124. package/lib/types/replaceBundleReferences.d.ts +39 -0
  125. package/lib/types/schema.d.ts +107 -0
  126. package/lib/types/shared-buffer.d.ts +2 -0
  127. package/lib/types/sourcemap.d.ts +15 -0
  128. package/lib/types/stream.d.ts +8 -0
  129. package/lib/types/throttle.d.ts +1 -0
  130. package/lib/types/urlJoin.d.ts +5 -0
  131. package/lib/urlJoin.js +35 -0
  132. package/package.json +26 -17
  133. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  134. package/src/Deferred.ts +26 -0
  135. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  136. package/src/{TapStream.js → TapStream.ts} +8 -7
  137. package/src/{alternatives.js → alternatives.ts} +15 -16
  138. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  139. package/src/{blob.js → blob.ts} +3 -5
  140. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  141. package/src/{collection.js → collection.ts} +23 -20
  142. package/src/{config.js → config.ts} +20 -21
  143. package/src/{countLines.js → countLines.ts} +0 -2
  144. package/src/{debounce.js → debounce.ts} +3 -5
  145. package/src/debug-tools.ts +48 -0
  146. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  147. package/src/{escape-html.js → escape-html.ts} +5 -3
  148. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +18 -20
  149. package/src/{generateCertificate.js → generateCertificate.ts} +8 -6
  150. package/src/{getCertificate.js → getCertificate.ts} +6 -5
  151. package/src/{getExisting.js → getExisting.ts} +4 -3
  152. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  153. package/src/{getRootDir.js → getRootDir.ts} +1 -3
  154. package/src/{glob.js → glob.ts} +13 -14
  155. package/src/{hash.js → hash.ts} +23 -18
  156. package/src/{http-server.js → http-server.ts} +34 -40
  157. package/src/{index.js → index.ts} +13 -9
  158. package/src/{is-url.js → is-url.ts} +1 -2
  159. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +1 -2
  160. package/src/{objectHash.js → objectHash.ts} +1 -4
  161. package/src/{openInBrowser.js → openInBrowser.ts} +2 -4
  162. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  163. package/src/{path.js → path.ts} +2 -4
  164. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  165. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +34 -22
  166. package/src/progress-message.ts +43 -0
  167. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +4 -4
  168. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  169. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +66 -50
  170. package/src/{schema.js → schema.ts} +179 -150
  171. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  172. package/src/{sourcemap.js → sourcemap.ts} +27 -9
  173. package/src/{stream.js → stream.ts} +30 -22
  174. package/src/throttle.ts +13 -0
  175. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  176. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  177. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  178. package/test/{collection.test.js → collection.test.ts} +0 -2
  179. package/test/{config.test.js → config.test.ts} +0 -3
  180. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  181. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  182. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  183. package/test/schema.test.ts +748 -0
  184. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  185. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  186. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  187. package/tsconfig.json +33 -0
  188. package/tsconfig.tsbuildinfo +1 -0
  189. package/lib/index.js.map +0 -1
  190. package/src/Deferred.js +0 -23
  191. package/src/progress-message.js +0 -22
  192. package/src/throttle.js +0 -15
package/lib/schema.js ADDED
@@ -0,0 +1,369 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ exports.fuzzySearch = fuzzySearch;
8
+ function _diagnostic() {
9
+ const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
10
+ _diagnostic = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _nullthrows() {
16
+ const data = _interopRequireDefault(require("nullthrows"));
17
+ _nullthrows = function () {
18
+ return data;
19
+ };
20
+ return data;
21
+ }
22
+ function levenshtein() {
23
+ const data = _interopRequireWildcard(require("fastest-levenshtein"));
24
+ levenshtein = function () {
25
+ return data;
26
+ };
27
+ return data;
28
+ }
29
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
31
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
32
+ function validateSchema(schema, data) {
33
+ function walk(
34
+ // @ts-expect-error TS7006
35
+ schemaAncestors, dataNode, dataPath) {
36
+ let [schemaNode] = schemaAncestors;
37
+ if (schemaNode.type) {
38
+ let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
39
+ if (schemaNode.type !== type) {
40
+ return {
41
+ type: 'type',
42
+ dataType: 'value',
43
+ dataPath,
44
+ expectedTypes: [schemaNode.type],
45
+ ancestors: schemaAncestors,
46
+ prettyType: schemaNode.__type
47
+ };
48
+ } else {
49
+ switch (schemaNode.type) {
50
+ case 'array':
51
+ {
52
+ if (schemaNode.items) {
53
+ let results = [];
54
+ // @ts-expect-error TS18046
55
+ for (let i = 0; i < dataNode.length; i++) {
56
+ let result = walk([schemaNode.items].concat(schemaAncestors),
57
+ // @ts-expect-error TS18046
58
+ dataNode[i], dataPath + '/' + i);
59
+ if (result) results.push(result);
60
+ }
61
+ if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
62
+ }
63
+ break;
64
+ }
65
+ case 'string':
66
+ {
67
+ // @ts-expect-error TS2322
68
+ let value = dataNode;
69
+ if (schemaNode.enum) {
70
+ if (!schemaNode.enum.includes(value)) {
71
+ return {
72
+ type: 'enum',
73
+ dataType: 'value',
74
+ dataPath,
75
+ expectedValues: schemaNode.enum,
76
+ actualValue: value,
77
+ ancestors: schemaAncestors
78
+ };
79
+ }
80
+ } else if (schemaNode.__validate) {
81
+ let validationError = schemaNode.__validate(value);
82
+ if (typeof validationError == 'string') {
83
+ return {
84
+ type: 'other',
85
+ dataType: 'value',
86
+ dataPath,
87
+ message: validationError,
88
+ actualValue: value,
89
+ ancestors: schemaAncestors
90
+ };
91
+ }
92
+ }
93
+ break;
94
+ }
95
+ case 'number':
96
+ {
97
+ // @ts-expect-error TS2322
98
+ let value = dataNode;
99
+ if (schemaNode.enum) {
100
+ if (!schemaNode.enum.includes(value)) {
101
+ return {
102
+ type: 'enum',
103
+ dataType: 'value',
104
+ dataPath,
105
+ expectedValues: schemaNode.enum,
106
+ actualValue: value,
107
+ ancestors: schemaAncestors
108
+ };
109
+ }
110
+ }
111
+ break;
112
+ }
113
+ case 'object':
114
+ {
115
+ let results = [];
116
+ let invalidProps;
117
+ if (schemaNode.__forbiddenProperties) {
118
+ // @ts-expect-error TS2769
119
+ let keys = Object.keys(dataNode);
120
+ // @ts-expect-error TS7006
121
+ invalidProps = schemaNode.__forbiddenProperties.filter(val => keys.includes(val));
122
+ results.push(...invalidProps.map(
123
+ // @ts-expect-error TS7006
124
+ k => ({
125
+ type: 'forbidden-prop',
126
+ dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
127
+ dataType: 'key',
128
+ prop: k,
129
+ expectedProps: Object.keys(schemaNode.properties),
130
+ actualProps: keys,
131
+ ancestors: schemaAncestors
132
+ })));
133
+ }
134
+ if (schemaNode.required) {
135
+ // @ts-expect-error TS2769
136
+ let keys = Object.keys(dataNode);
137
+ let missingKeys = schemaNode.required.filter(
138
+ // @ts-expect-error TS7006
139
+ val => !keys.includes(val));
140
+ results.push(...missingKeys.map(
141
+ // @ts-expect-error TS7006
142
+ k => ({
143
+ type: 'missing-prop',
144
+ dataPath,
145
+ dataType: 'value',
146
+ prop: k,
147
+ expectedProps: schemaNode.required,
148
+ actualProps: keys,
149
+ ancestors: schemaAncestors
150
+ })));
151
+ }
152
+ if (schemaNode.properties) {
153
+ let {
154
+ additionalProperties = true
155
+ } = schemaNode;
156
+ // @ts-expect-error TS2407
157
+ for (let k in dataNode) {
158
+ if (invalidProps && invalidProps.includes(k)) {
159
+ // Don't check type on forbidden props
160
+ continue;
161
+ } else if (k in schemaNode.properties) {
162
+ let result = walk([schemaNode.properties[k]].concat(schemaAncestors),
163
+ // @ts-expect-error TS18046
164
+ dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
165
+ if (result) results.push(result);
166
+ } else {
167
+ if (typeof additionalProperties === 'boolean') {
168
+ if (!additionalProperties) {
169
+ results.push({
170
+ type: 'enum',
171
+ dataType: 'key',
172
+ dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
173
+ expectedValues: Object.keys(schemaNode.properties
174
+ // @ts-expect-error TS18046
175
+ ).filter(p => !(p in dataNode)),
176
+ actualValue: k,
177
+ ancestors: schemaAncestors,
178
+ prettyType: schemaNode.__type
179
+ });
180
+ }
181
+ } else {
182
+ let result = walk([additionalProperties].concat(schemaAncestors),
183
+ // @ts-expect-error TS18046
184
+ dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
185
+ if (result) results.push(result);
186
+ }
187
+ }
188
+ }
189
+ }
190
+ if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
191
+ break;
192
+ }
193
+ case 'boolean':
194
+ // NOOP, type was checked already
195
+ break;
196
+ default:
197
+ throw new Error(`Unimplemented schema type ${type}?`);
198
+ }
199
+ }
200
+ } else {
201
+ if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
202
+ return {
203
+ type: 'enum',
204
+ dataType: 'value',
205
+ dataPath: dataPath,
206
+ expectedValues: schemaNode.enum,
207
+ actualValue: schemaNode,
208
+ ancestors: schemaAncestors
209
+ };
210
+ }
211
+ if (schemaNode.oneOf || schemaNode.allOf) {
212
+ let list = schemaNode.oneOf || schemaNode.allOf;
213
+ let results = [];
214
+ for (let f of list) {
215
+ let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
216
+ if (result) results.push(result);
217
+ }
218
+ if (schemaNode.oneOf ? results.length == schemaNode.oneOf.length : results.length > 0) {
219
+ // return the result with more values / longer key
220
+ results.sort((a, b) => Array.isArray(a) || Array.isArray(b) ? Array.isArray(a) && !Array.isArray(b) ? -1 : !Array.isArray(a) && Array.isArray(b) ? 1 : Array.isArray(a) && Array.isArray(b) ? b.length - a.length : 0 : b.dataPath.length - a.dataPath.length);
221
+ return results[0];
222
+ }
223
+ } else if (schemaNode.not) {
224
+ let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
225
+ // @ts-expect-error TS2339
226
+ if (!result || result.length == 0) {
227
+ return {
228
+ type: 'other',
229
+ dataPath,
230
+ dataType: null,
231
+ message: schemaNode.__message,
232
+ actualValue: dataNode,
233
+ ancestors: schemaAncestors
234
+ };
235
+ }
236
+ }
237
+ }
238
+ return undefined;
239
+ }
240
+ let result = walk([schema], data, '');
241
+ return Array.isArray(result) ? result : result ? [result] : [];
242
+ }
243
+ var _default = exports.default = validateSchema;
244
+ function fuzzySearch(expectedValues, actualValue) {
245
+ let result = expectedValues.map(exp => [exp, levenshtein().distance(exp, actualValue)]).filter(
246
+ // Remove if more than half of the string would need to be changed
247
+ // @ts-expect-error TS2769
248
+ ([, d]) => d * 2 < actualValue.length);
249
+ // @ts-expect-error TS2345
250
+ result.sort(([, a], [, b]) => a - b);
251
+ // @ts-expect-error TS2345
252
+ return result.map(([v]) => v);
253
+ }
254
+ validateSchema.diagnostic = function (schema, data, origin, message) {
255
+ if (!('map' in data) && !('source' in data || 'data' in data)) {
256
+ throw new Error('At least one of data.source, data.data, or data.map must be defined!');
257
+ }
258
+ let loadedSource;
259
+ function loadSource(loader) {
260
+ if (loadedSource !== undefined) {
261
+ return loadedSource;
262
+ } else if (typeof loader === 'function') {
263
+ loadedSource = loader();
264
+ return loadedSource;
265
+ } else if (typeof loader === 'string') {
266
+ loadedSource = loader;
267
+ return loadedSource;
268
+ }
269
+ return loadedSource;
270
+ }
271
+ // @ts-expect-error TS2339
272
+ let object = data.map ?
273
+ // @ts-expect-error TS2339
274
+ data.map.data :
275
+ // @ts-expect-error TS2339
276
+ data.data ?? JSON.parse(loadSource(data.source));
277
+ let errors = validateSchema(schema, object);
278
+ if (errors.length) {
279
+ let keys = errors.map(e => {
280
+ let message;
281
+ if (e.type === 'enum') {
282
+ let {
283
+ actualValue
284
+ } = e;
285
+ let expectedValues = e.expectedValues.map(String);
286
+ let likely = actualValue != null ? fuzzySearch(expectedValues, String(actualValue)) : [];
287
+ if (likely.length > 0) {
288
+ message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
289
+ } else if (expectedValues.length > 0) {
290
+ message = `Possible values: ${expectedValues.map(v => JSON.stringify(v)).join(', ')}`;
291
+ } else {
292
+ message = 'Unexpected value';
293
+ }
294
+ } else if (e.type === 'forbidden-prop') {
295
+ let {
296
+ prop,
297
+ expectedProps,
298
+ actualProps
299
+ } = e;
300
+ let likely = fuzzySearch(expectedProps, prop).filter(v => !actualProps.includes(v));
301
+ if (likely.length > 0) {
302
+ message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
303
+ } else {
304
+ message = 'Unexpected property';
305
+ }
306
+ } else if (e.type === 'missing-prop') {
307
+ let {
308
+ prop,
309
+ actualProps
310
+ } = e;
311
+ let likely = fuzzySearch(actualProps, prop);
312
+ if (likely.length > 0) {
313
+ message = `Did you mean ${JSON.stringify(prop)}?`;
314
+ e.dataPath += '/' + likely[0];
315
+ e.dataType = 'key';
316
+ } else {
317
+ message = `Missing property ${prop}`;
318
+ }
319
+ } else if (e.type === 'type') {
320
+ if (e.prettyType != null) {
321
+ message = `Expected ${e.prettyType}`;
322
+ } else {
323
+ message = `Expected type ${e.expectedTypes.join(', ')}`;
324
+ }
325
+ } else {
326
+ message = e.message;
327
+ }
328
+ return {
329
+ key: e.dataPath,
330
+ type: e.dataType,
331
+ message
332
+ };
333
+ });
334
+ let map, code;
335
+ // @ts-expect-error TS2339
336
+ if (data.map) {
337
+ // @ts-expect-error TS2339
338
+ map = data.map;
339
+ code = loadSource(data.source);
340
+ } else {
341
+ map = loadSource(data.source) ??
342
+ // @ts-expect-error TS2339
343
+ JSON.stringify((0, _nullthrows().default)(data.data), 0, '\t');
344
+ code = map;
345
+ }
346
+ let codeFrames = [{
347
+ filePath: data.filePath ?? undefined,
348
+ language: 'json',
349
+ code,
350
+ codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(map, keys.map(({
351
+ key,
352
+ type,
353
+ message
354
+ }) => ({
355
+ key: (data.prependKey ?? '') + key,
356
+ type: type,
357
+ message: message != null ? (0, _diagnostic().escapeMarkdown)(message) : message
358
+ })))
359
+ }];
360
+ throw new (_diagnostic().default)({
361
+ diagnostic: {
362
+ message: message,
363
+ origin,
364
+ // @ts-expect-error TS2322
365
+ codeFrames
366
+ }
367
+ });
368
+ }
369
+ };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.SharedBuffer = void 0;
7
+ // flow-to-ts helpers
8
+
9
+ // /flow-to-ts helpers
10
+
11
+ let SharedBuffer = exports.SharedBuffer = void 0;
12
+
13
+ // @ts-expect-error process.browser is a browser-specific property
14
+ if (process.browser) {
15
+ exports.SharedBuffer = SharedBuffer = ArrayBuffer;
16
+ // Safari has removed the constructor
17
+ if (typeof SharedArrayBuffer !== 'undefined') {
18
+ let channel = new MessageChannel();
19
+ try {
20
+ // Firefox might throw when sending the Buffer over a MessagePort
21
+ channel.port1.postMessage(new SharedArrayBuffer(0));
22
+ exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
23
+ } catch (_) {
24
+ // NOOP
25
+ }
26
+ channel.port1.close();
27
+ channel.port2.close();
28
+ }
29
+ } else {
30
+ exports.SharedBuffer = SharedBuffer = SharedArrayBuffer;
31
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.SOURCEMAP_RE = exports.SOURCEMAP_EXTENSIONS = void 0;
7
+ exports.loadSourceMap = loadSourceMap;
8
+ exports.loadSourceMapUrl = loadSourceMapUrl;
9
+ exports.matchSourceMappingURL = matchSourceMappingURL;
10
+ exports.remapSourceLocation = remapSourceLocation;
11
+ function _featureFlags() {
12
+ const data = require("@atlaspack/feature-flags");
13
+ _featureFlags = function () {
14
+ return data;
15
+ };
16
+ return data;
17
+ }
18
+ function _sourceMap() {
19
+ const data = _interopRequireDefault(require("@parcel/source-map"));
20
+ _sourceMap = function () {
21
+ return data;
22
+ };
23
+ return data;
24
+ }
25
+ function _path() {
26
+ const data = _interopRequireDefault(require("path"));
27
+ _path = function () {
28
+ return data;
29
+ };
30
+ return data;
31
+ }
32
+ var _path2 = require("./path");
33
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
+ const SOURCEMAP_RE = exports.SOURCEMAP_RE = /(?:\/\*|\/\/)\s*[@#]\s*sourceMappingURL\s*=\s*([^\s*]+)(?:\s*\*\/)?\s*$/;
35
+ const DATA_URL_RE = /^data:[^;]+(?:;charset=[^;]+)?;base64,(.*)/;
36
+ const SOURCEMAP_EXTENSIONS = exports.SOURCEMAP_EXTENSIONS = new Set(['css', 'es', 'es6', 'js', 'jsx', 'mjs', 'ts', 'tsx']);
37
+ function matchSourceMappingURL(contents) {
38
+ return contents.match(SOURCEMAP_RE);
39
+ }
40
+ async function loadSourceMapUrl(fs, filename, contents) {
41
+ let match = matchSourceMappingURL(contents);
42
+ if (match) {
43
+ let url = match[1].trim();
44
+ let dataURLMatch = url.match(DATA_URL_RE);
45
+ let mapFilePath;
46
+ if (dataURLMatch) {
47
+ mapFilePath = filename;
48
+ } else {
49
+ mapFilePath = url.replace(/^file:\/\//, '');
50
+ mapFilePath = (0, _path2.isAbsolute)(mapFilePath) ? mapFilePath : _path().default.join(_path().default.dirname(filename), mapFilePath);
51
+ }
52
+ return {
53
+ url,
54
+ filename: mapFilePath,
55
+ map: JSON.parse(dataURLMatch ? Buffer.from(dataURLMatch[1], 'base64').toString() : await fs.readFile(mapFilePath, 'utf8'))
56
+ };
57
+ }
58
+ }
59
+ async function loadSourceMap(filename, contents, options) {
60
+ let foundMap = await loadSourceMapUrl(options.fs, filename, contents);
61
+ if (foundMap) {
62
+ let mapSourceRoot = _path().default.dirname(filename);
63
+ if (foundMap.map.sourceRoot && !(0, _path2.normalizeSeparators)(foundMap.map.sourceRoot).startsWith('/')) {
64
+ mapSourceRoot = _path().default.join(mapSourceRoot, foundMap.map.sourceRoot);
65
+ }
66
+ let sourcemapInstance = new (_sourceMap().default)(options.projectRoot);
67
+ sourcemapInstance.addVLQMap({
68
+ ...foundMap.map,
69
+ sources: foundMap.map.sources.map(s => {
70
+ return _path().default.join(mapSourceRoot, s);
71
+ })
72
+ });
73
+ return sourcemapInstance;
74
+ }
75
+ }
76
+ function remapSourceLocation(loc, originalMap, projectRoot) {
77
+ let {
78
+ filePath,
79
+ start: {
80
+ line: startLine,
81
+ column: startCol
82
+ },
83
+ end: {
84
+ line: endLine,
85
+ column: endCol
86
+ }
87
+ } = loc;
88
+ let lineDiff = endLine - startLine;
89
+ let colDiff = endCol - startCol;
90
+ let start = originalMap.findClosestMapping(startLine, startCol - 1);
91
+ let end = originalMap.findClosestMapping(endLine, endCol - 1);
92
+ if (start !== null && start !== void 0 && start.original) {
93
+ if (start.source) {
94
+ if ((0, _featureFlags().getFeatureFlag)('symbolLocationFix') && !_path().default.isAbsolute(start.source)) {
95
+ filePath = _path().default.join(projectRoot, start.source);
96
+ } else {
97
+ filePath = start.source;
98
+ }
99
+ }
100
+ ({
101
+ line: startLine,
102
+ column: startCol
103
+ } = start.original);
104
+ startCol++; // source map columns are 0-based
105
+ }
106
+
107
+ if (end !== null && end !== void 0 && end.original) {
108
+ ({
109
+ line: endLine,
110
+ column: endCol
111
+ } = end.original);
112
+ endCol++; // source map columns are 0-based
113
+
114
+ if (endLine < startLine) {
115
+ endLine = startLine;
116
+ endCol = startCol;
117
+ } else if (endLine === startLine && endCol < startCol && lineDiff === 0) {
118
+ endCol = startCol + colDiff;
119
+ } else if (endLine === startLine && startCol === endCol && lineDiff === 0) {
120
+ // Prevent 0-length ranges
121
+ endCol = startCol + 1;
122
+ }
123
+ } else {
124
+ endLine = startLine;
125
+ endCol = startCol;
126
+ }
127
+ return {
128
+ filePath,
129
+ start: {
130
+ line: startLine,
131
+ column: startCol
132
+ },
133
+ end: {
134
+ line: endLine,
135
+ column: endCol
136
+ }
137
+ };
138
+ }
package/lib/stream.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.blobToStream = blobToStream;
7
+ exports.bufferStream = bufferStream;
8
+ exports.fallbackStream = fallbackStream;
9
+ exports.measureStreamLength = measureStreamLength;
10
+ exports.readableFromStringOrBuffer = readableFromStringOrBuffer;
11
+ exports.streamFromPromise = streamFromPromise;
12
+ function _stream() {
13
+ const data = require("stream");
14
+ _stream = function () {
15
+ return data;
16
+ };
17
+ return data;
18
+ }
19
+ function measureStreamLength(stream) {
20
+ return new Promise((resolve, reject) => {
21
+ let length = 0;
22
+ stream.on('data', chunk => {
23
+ length += chunk;
24
+ });
25
+ stream.on('end', () => resolve(length));
26
+ stream.on('error', reject);
27
+ });
28
+ }
29
+ function readableFromStringOrBuffer(str) {
30
+ // https://stackoverflow.com/questions/12755997/how-to-create-streams-from-string-in-node-js
31
+ const stream = new (_stream().Readable)();
32
+ stream.push(str);
33
+ stream.push(null);
34
+ return stream;
35
+ }
36
+ function bufferStream(stream) {
37
+ return new Promise((resolve, reject) => {
38
+ let buf = Buffer.from([]);
39
+ stream.on('data', data => {
40
+ buf = Buffer.concat([buf, data]);
41
+ });
42
+ stream.on('end', () => {
43
+ resolve(buf);
44
+ });
45
+ stream.on('error', reject);
46
+ });
47
+ }
48
+ function blobToStream(blob) {
49
+ if (blob instanceof _stream().Readable) {
50
+ return blob;
51
+ }
52
+ return readableFromStringOrBuffer(blob);
53
+ }
54
+ function streamFromPromise(promise) {
55
+ const stream = new (_stream().PassThrough)();
56
+ promise.then(blob => {
57
+ if (blob instanceof _stream().Readable) {
58
+ blob.pipe(stream);
59
+ } else {
60
+ stream.end(blob);
61
+ }
62
+ });
63
+ return stream;
64
+ }
65
+ function fallbackStream(stream, fallback) {
66
+ const res = new (_stream().PassThrough)();
67
+ stream.on('error', err => {
68
+ if (err.code === 'ENOENT') {
69
+ fallback().pipe(res);
70
+ } else {
71
+ res.emit('error', err);
72
+ }
73
+ });
74
+ stream.pipe(res);
75
+ return res;
76
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = throttle;
7
+ function throttle(fn, delay) {
8
+ let lastCalled;
9
+ return function throttled(...args) {
10
+ if (lastCalled == null || lastCalled + delay <= Date.now()) {
11
+ fn.call(this, ...args);
12
+ lastCalled = Date.now();
13
+ }
14
+ };
15
+ }
@@ -0,0 +1,13 @@
1
+ export declare class DefaultMap<K, V> extends Map<K, V> {
2
+ _getDefault: (arg1: K) => V;
3
+ constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>);
4
+ get(key: K): V;
5
+ }
6
+ interface Key {
7
+ }
8
+ export declare class DefaultWeakMap<K extends Key, V> extends WeakMap<K, V> {
9
+ _getDefault: (arg1: K) => V;
10
+ constructor(getDefault: (arg1: K) => V, entries?: Iterable<[K, V]>);
11
+ get(key: K): V;
12
+ }
13
+ export {};
@@ -0,0 +1,8 @@
1
+ export type Deferred<T> = {
2
+ resolve(arg1: T): void;
3
+ reject(arg1: unknown): void;
4
+ };
5
+ export declare function makeDeferredWithPromise<T>(): {
6
+ deferred: Deferred<T>;
7
+ promise: Promise<T>;
8
+ };
@@ -0,0 +1,25 @@
1
+ import { type Deferred } from './Deferred';
2
+ type PromiseQueueOpts = {
3
+ maxConcurrent: number;
4
+ };
5
+ export default class PromiseQueue<T> {
6
+ _deferred: Deferred<Array<T>> | null | undefined;
7
+ _maxConcurrent: number;
8
+ _numRunning: number;
9
+ _queue: Array<() => Promise<void>>;
10
+ _runPromise: Promise<Array<T>> | null | undefined;
11
+ _error: unknown;
12
+ _count: number;
13
+ _results: Array<T>;
14
+ _addSubscriptions: Set<() => void>;
15
+ constructor(opts?: PromiseQueueOpts);
16
+ getNumWaiting(): number;
17
+ add(fn: () => Promise<T>): void;
18
+ subscribeToAdd(fn: () => void): () => void;
19
+ run(): Promise<Array<T>>;
20
+ _next(): Promise<void>;
21
+ _runFn(fn: () => unknown): Promise<void>;
22
+ _resetState(): void;
23
+ _done(): void;
24
+ }
25
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Transform } from 'stream';
2
+ export default class TapStream extends Transform {
3
+ _tap: (arg1: Buffer) => unknown;
4
+ constructor(tap: (arg1: Buffer) => unknown, options: unknown);
5
+ _transform(chunk: Buffer | string, encoding: string, callback: (err?: Error | null | undefined, chunk?: Buffer | string) => unknown): void;
6
+ }
@@ -0,0 +1,3 @@
1
+ import type { FileSystem } from '@atlaspack/types-internal';
2
+ export declare function findAlternativeNodeModules(fs: FileSystem, moduleName: string, dir: string): Promise<Array<string>>;
3
+ export declare function findAlternativeFiles(fs: FileSystem, fileSpecifier: string, dir: string, projectRoot: string, leadingDotSlash?: boolean, includeDirectories?: boolean, includeExtension?: boolean): Promise<Array<string>>;
@@ -0,0 +1 @@
1
+ export declare function ansiHtml(ansi: string): string;