@atlaspack/utils 2.14.5-canary.49 → 2.14.5-canary.490

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 (197) hide show
  1. package/CHANGELOG.md +655 -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 +137 -0
  13. package/dist/countLines.js +12 -0
  14. package/dist/debounce.js +15 -0
  15. package/dist/debug-tools.js +40 -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/getTimeId.js +13 -0
  25. package/dist/glob.js +129 -0
  26. package/dist/hash.js +45 -0
  27. package/dist/http-server.js +55 -0
  28. package/dist/index.js +148 -0
  29. package/dist/is-url.js +15 -0
  30. package/dist/isDirectoryInside.js +11 -0
  31. package/dist/objectHash.js +20 -0
  32. package/dist/openInBrowser.js +61 -0
  33. package/dist/parseCSSImport.js +14 -0
  34. package/dist/path.js +36 -0
  35. package/dist/prettifyTime.js +6 -0
  36. package/dist/prettyDiagnostic.js +108 -0
  37. package/dist/progress-message.js +37 -0
  38. package/dist/relativeBundlePath.js +13 -0
  39. package/dist/relativeUrl.js +11 -0
  40. package/dist/replaceBundleReferences.js +131 -0
  41. package/dist/schema.js +404 -0
  42. package/dist/shared-buffer.js +24 -0
  43. package/dist/sourcemap.js +121 -0
  44. package/dist/stream.js +69 -0
  45. package/dist/throttle.js +12 -0
  46. package/dist/urlJoin.js +22 -0
  47. package/lib/DefaultMap.js +42 -0
  48. package/lib/Deferred.js +30 -0
  49. package/lib/PromiseQueue.js +112 -0
  50. package/lib/TapStream.js +34 -0
  51. package/lib/alternatives.js +116 -0
  52. package/lib/ansi-html.js +20 -0
  53. package/lib/blob.js +40 -0
  54. package/lib/bundle-url.js +34 -0
  55. package/lib/collection.js +111 -0
  56. package/lib/config.js +173 -0
  57. package/lib/countLines.js +15 -0
  58. package/lib/debounce.js +18 -0
  59. package/lib/debug-tools.js +41 -0
  60. package/lib/dependency-location.js +21 -0
  61. package/lib/escape-html.js +22 -0
  62. package/lib/generateBuildMetrics.js +121 -0
  63. package/lib/generateCertificate.js +129 -0
  64. package/lib/getCertificate.js +18 -0
  65. package/lib/getExisting.js +25 -0
  66. package/lib/getModuleParts.js +30 -0
  67. package/lib/getRootDir.js +52 -0
  68. package/lib/getTimeId.js +10 -0
  69. package/lib/glob.js +109 -0
  70. package/lib/hash.js +50 -0
  71. package/lib/http-server.js +85 -0
  72. package/lib/index.js +642 -37310
  73. package/lib/is-url.js +24 -0
  74. package/lib/isDirectoryInside.js +18 -0
  75. package/lib/objectHash.js +26 -0
  76. package/lib/openInBrowser.js +76 -0
  77. package/lib/parseCSSImport.js +15 -0
  78. package/lib/path.js +39 -0
  79. package/lib/prettifyTime.js +9 -0
  80. package/lib/prettyDiagnostic.js +155 -0
  81. package/lib/progress-message.js +46 -0
  82. package/lib/relativeBundlePath.js +22 -0
  83. package/lib/relativeUrl.js +24 -0
  84. package/lib/replaceBundleReferences.js +182 -0
  85. package/lib/schema.js +352 -0
  86. package/lib/shared-buffer.js +31 -0
  87. package/lib/sourcemap.js +137 -0
  88. package/lib/stream.js +76 -0
  89. package/lib/throttle.js +15 -0
  90. package/lib/types/DefaultMap.d.ts +13 -0
  91. package/lib/types/Deferred.d.ts +8 -0
  92. package/lib/types/PromiseQueue.d.ts +25 -0
  93. package/lib/types/TapStream.d.ts +6 -0
  94. package/lib/types/alternatives.d.ts +3 -0
  95. package/lib/types/ansi-html.d.ts +1 -0
  96. package/lib/types/blob.d.ts +4 -0
  97. package/lib/types/bundle-url.d.ts +4 -0
  98. package/lib/types/collection.d.ts +33 -0
  99. package/lib/types/config.d.ts +17 -0
  100. package/lib/types/countLines.d.ts +1 -0
  101. package/lib/types/debounce.d.ts +1 -0
  102. package/lib/types/debug-tools.d.ts +9 -0
  103. package/lib/types/dependency-location.d.ts +14 -0
  104. package/lib/types/escape-html.d.ts +1 -0
  105. package/lib/types/generateBuildMetrics.d.ts +18 -0
  106. package/lib/types/generateCertificate.d.ts +5 -0
  107. package/lib/types/getCertificate.d.ts +5 -0
  108. package/lib/types/getExisting.d.ts +8 -0
  109. package/lib/types/getModuleParts.d.ts +4 -0
  110. package/lib/types/getRootDir.d.ts +2 -0
  111. package/lib/types/getTimeId.d.ts +1 -0
  112. package/lib/types/glob.d.ts +9 -0
  113. package/lib/types/hash.d.ts +7 -0
  114. package/lib/types/http-server.d.ts +19 -0
  115. package/lib/types/index.d.ts +49 -0
  116. package/lib/types/is-url.d.ts +1 -0
  117. package/lib/types/isDirectoryInside.d.ts +2 -0
  118. package/lib/types/objectHash.d.ts +3 -0
  119. package/lib/types/openInBrowser.d.ts +1 -0
  120. package/lib/types/parseCSSImport.d.ts +1 -0
  121. package/lib/types/path.d.ts +8 -0
  122. package/lib/types/prettifyTime.d.ts +1 -0
  123. package/lib/types/prettyDiagnostic.d.ts +18 -0
  124. package/lib/types/progress-message.d.ts +3 -0
  125. package/lib/types/relativeBundlePath.d.ts +4 -0
  126. package/lib/types/relativeUrl.d.ts +1 -0
  127. package/lib/types/replaceBundleReferences.d.ts +39 -0
  128. package/lib/types/schema.d.ts +107 -0
  129. package/lib/types/shared-buffer.d.ts +2 -0
  130. package/lib/types/sourcemap.d.ts +15 -0
  131. package/lib/types/stream.d.ts +8 -0
  132. package/lib/types/throttle.d.ts +1 -0
  133. package/lib/types/urlJoin.d.ts +5 -0
  134. package/lib/urlJoin.js +35 -0
  135. package/package.json +30 -21
  136. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  137. package/src/Deferred.ts +26 -0
  138. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  139. package/src/{TapStream.js → TapStream.ts} +8 -7
  140. package/src/{alternatives.js → alternatives.ts} +15 -16
  141. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  142. package/src/{blob.js → blob.ts} +3 -5
  143. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  144. package/src/{collection.js → collection.ts} +23 -20
  145. package/src/{config.js → config.ts} +21 -23
  146. package/src/{countLines.js → countLines.ts} +0 -2
  147. package/src/{debounce.js → debounce.ts} +3 -5
  148. package/src/debug-tools.ts +50 -0
  149. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  150. package/src/{escape-html.js → escape-html.ts} +5 -3
  151. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +19 -21
  152. package/src/{generateCertificate.js → generateCertificate.ts} +8 -6
  153. package/src/{getCertificate.js → getCertificate.ts} +6 -5
  154. package/src/{getExisting.js → getExisting.ts} +4 -3
  155. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  156. package/src/{getRootDir.js → getRootDir.ts} +1 -3
  157. package/src/getTimeId.ts +12 -0
  158. package/src/{glob.js → glob.ts} +13 -14
  159. package/src/{hash.js → hash.ts} +23 -18
  160. package/src/{http-server.js → http-server.ts} +34 -40
  161. package/src/{index.js → index.ts} +14 -10
  162. package/src/{is-url.js → is-url.ts} +1 -2
  163. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +1 -2
  164. package/src/{objectHash.js → objectHash.ts} +1 -4
  165. package/src/{openInBrowser.js → openInBrowser.ts} +3 -5
  166. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  167. package/src/{path.js → path.ts} +2 -4
  168. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  169. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +48 -25
  170. package/src/progress-message.ts +50 -0
  171. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +4 -4
  172. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  173. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +67 -51
  174. package/src/schema.ts +552 -0
  175. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  176. package/src/{sourcemap.js → sourcemap.ts} +28 -10
  177. package/src/{stream.js → stream.ts} +30 -22
  178. package/src/throttle.ts +13 -0
  179. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  180. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  181. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  182. package/test/{collection.test.js → collection.test.ts} +0 -2
  183. package/test/{config.test.js → config.test.ts} +0 -3
  184. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  185. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  186. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  187. package/test/schema.test.ts +748 -0
  188. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  189. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  190. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  191. package/tsconfig.json +36 -0
  192. package/tsconfig.tsbuildinfo +1 -0
  193. package/lib/index.js.map +0 -1
  194. package/src/Deferred.js +0 -23
  195. package/src/progress-message.js +0 -22
  196. package/src/schema.js +0 -504
  197. package/src/throttle.js +0 -15
package/lib/schema.js ADDED
@@ -0,0 +1,352 @@
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(e) { return e && e.__esModule ? e : { default: e }; }
30
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
31
+ function validateSchema(schema, data) {
32
+ function walk(schemaAncestors, dataNode, dataPath) {
33
+ let [schemaNode] = schemaAncestors;
34
+ if ('type' in schemaNode && schemaNode.type) {
35
+ let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
36
+ if (schemaNode.type !== type) {
37
+ return {
38
+ type: 'type',
39
+ dataType: 'value',
40
+ dataPath,
41
+ expectedTypes: [schemaNode.type],
42
+ ancestors: schemaAncestors,
43
+ prettyType: '__type' in schemaNode ? schemaNode.__type : undefined
44
+ };
45
+ } else {
46
+ switch (schemaNode.type) {
47
+ case 'array':
48
+ {
49
+ if ('items' in schemaNode && schemaNode.items && Array.isArray(dataNode)) {
50
+ let results = [];
51
+ for (let i = 0; i < dataNode.length; i++) {
52
+ let result = walk([schemaNode.items].concat(schemaAncestors), dataNode[i], dataPath + '/' + i);
53
+ if (result) results.push(result);
54
+ }
55
+ if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
56
+ }
57
+ break;
58
+ }
59
+ case 'string':
60
+ {
61
+ if (typeof dataNode === 'string') {
62
+ let value = dataNode;
63
+ if ('enum' in schemaNode && schemaNode.enum) {
64
+ if (!schemaNode.enum.includes(value)) {
65
+ return {
66
+ type: 'enum',
67
+ dataType: 'value',
68
+ dataPath,
69
+ expectedValues: schemaNode.enum,
70
+ actualValue: value,
71
+ ancestors: schemaAncestors
72
+ };
73
+ }
74
+ } else if ('__validate' in schemaNode && schemaNode.__validate) {
75
+ let validationError = schemaNode.__validate(value);
76
+ if (typeof validationError == 'string') {
77
+ return {
78
+ type: 'other',
79
+ dataType: 'value',
80
+ dataPath,
81
+ message: validationError,
82
+ actualValue: value,
83
+ ancestors: schemaAncestors
84
+ };
85
+ }
86
+ }
87
+ }
88
+ break;
89
+ }
90
+ case 'number':
91
+ {
92
+ if (typeof dataNode === 'number') {
93
+ let value = dataNode;
94
+ if ('enum' in schemaNode && schemaNode.enum) {
95
+ if (!schemaNode.enum.includes(value)) {
96
+ return {
97
+ type: 'enum',
98
+ dataType: 'value',
99
+ dataPath,
100
+ expectedValues: schemaNode.enum,
101
+ actualValue: value,
102
+ ancestors: schemaAncestors
103
+ };
104
+ }
105
+ }
106
+ }
107
+ break;
108
+ }
109
+ case 'object':
110
+ {
111
+ if (typeof dataNode === 'object' && dataNode !== null && !Array.isArray(dataNode)) {
112
+ let results = [];
113
+ let invalidProps;
114
+ if ('__forbiddenProperties' in schemaNode && schemaNode.__forbiddenProperties) {
115
+ let keys = Object.keys(dataNode);
116
+ invalidProps = schemaNode.__forbiddenProperties.filter(val => keys.includes(val));
117
+ results.push(...invalidProps.map(k => ({
118
+ type: 'forbidden-prop',
119
+ dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
120
+ dataType: 'key',
121
+ prop: k,
122
+ expectedProps: Object.keys(schemaNode.properties),
123
+ actualProps: keys,
124
+ ancestors: schemaAncestors
125
+ })));
126
+ }
127
+ if ('required' in schemaNode && schemaNode.required) {
128
+ let keys = Object.keys(dataNode);
129
+ let missingKeys = schemaNode.required.filter(val => !keys.includes(val));
130
+ results.push(...missingKeys.map(k => ({
131
+ type: 'missing-prop',
132
+ dataPath,
133
+ dataType: 'value',
134
+ prop: k,
135
+ expectedProps: schemaNode.required,
136
+ actualProps: keys,
137
+ ancestors: schemaAncestors
138
+ })));
139
+ }
140
+ if ('properties' in schemaNode && schemaNode.properties) {
141
+ let {
142
+ additionalProperties = true
143
+ } = schemaNode;
144
+ for (let k in dataNode) {
145
+ if (invalidProps && invalidProps.includes(k)) {
146
+ // Don't check type on forbidden props
147
+ continue;
148
+ } else if (k in schemaNode.properties) {
149
+ let result = walk([schemaNode.properties[k]].concat(schemaAncestors), dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
150
+ if (result) results.push(result);
151
+ } else {
152
+ if (typeof additionalProperties === 'boolean') {
153
+ if (!additionalProperties) {
154
+ results.push({
155
+ type: 'enum',
156
+ dataType: 'key',
157
+ dataPath: dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k),
158
+ expectedValues: Object.keys(schemaNode.properties).filter(p => !(p in dataNode)),
159
+ actualValue: k,
160
+ ancestors: schemaAncestors,
161
+ prettyType: schemaNode.__type
162
+ });
163
+ }
164
+ } else {
165
+ let result = walk([additionalProperties].concat(schemaAncestors), dataNode[k], dataPath + '/' + (0, _diagnostic().encodeJSONKeyComponent)(k));
166
+ if (result) results.push(result);
167
+ }
168
+ }
169
+ }
170
+ }
171
+ if (results.length) return results.reduce((acc, v) => acc.concat(v), []);
172
+ }
173
+ break;
174
+ }
175
+ case 'boolean':
176
+ // NOOP, type was checked already
177
+ break;
178
+ default:
179
+ throw new Error(`Unimplemented schema type ${type}?`);
180
+ }
181
+ }
182
+ } else {
183
+ if ('enum' in schemaNode && schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
184
+ return {
185
+ type: 'enum',
186
+ dataType: 'value',
187
+ dataPath: dataPath,
188
+ expectedValues: schemaNode.enum,
189
+ actualValue: schemaNode,
190
+ ancestors: schemaAncestors
191
+ };
192
+ }
193
+ if ('oneOf' in schemaNode || 'allOf' in schemaNode) {
194
+ let list = 'oneOf' in schemaNode ? schemaNode.oneOf : 'allOf' in schemaNode ? schemaNode.allOf : [];
195
+ let results = [];
196
+ for (let f of list) {
197
+ let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
198
+ if (result) results.push(result);
199
+ }
200
+ if ('oneOf' in schemaNode ? results.length == schemaNode.oneOf.length : results.length > 0) {
201
+ // return the result with more values / longer key
202
+ 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);
203
+ return results[0];
204
+ }
205
+ } else if ('not' in schemaNode && schemaNode.not) {
206
+ let result = walk([schemaNode.not].concat(schemaAncestors), dataNode, dataPath);
207
+ if (!result || Array.isArray(result) && result.length == 0) {
208
+ return {
209
+ type: 'other',
210
+ dataPath,
211
+ dataType: null,
212
+ message: schemaNode.__message,
213
+ actualValue: dataNode,
214
+ ancestors: schemaAncestors
215
+ };
216
+ }
217
+ }
218
+ }
219
+ return undefined;
220
+ }
221
+ let result = walk([schema], data, '');
222
+ return Array.isArray(result) ? result : result ? [result] : [];
223
+ }
224
+ var _default = exports.default = validateSchema;
225
+ function fuzzySearch(expectedValues, actualValue) {
226
+ let result = expectedValues.map(exp => [exp, levenshtein().distance(exp, actualValue)]).filter(
227
+ // Remove if more than half of the string would need to be changed
228
+ ([, d]) => d * 2 < actualValue.length);
229
+ result.sort(([, a], [, b]) => a - b);
230
+ return result.map(([v]) => v);
231
+ }
232
+ validateSchema.diagnostic = function (schema, data, origin, message) {
233
+ if (!('map' in data) && !('source' in data || 'data' in data)) {
234
+ throw new Error('At least one of data.source, data.data, or data.map must be defined!');
235
+ }
236
+ let loadedSource;
237
+ function loadSource(loader) {
238
+ if (loadedSource !== undefined) {
239
+ return loadedSource;
240
+ } else if (typeof loader === 'function') {
241
+ loadedSource = loader();
242
+ return loadedSource;
243
+ } else if (typeof loader === 'string') {
244
+ loadedSource = loader;
245
+ return loadedSource;
246
+ }
247
+ return loadedSource;
248
+ }
249
+ let object;
250
+ if ('map' in data && data.map) {
251
+ object = data.map.data;
252
+ } else if ('data' in data && data.data !== undefined) {
253
+ object = data.data;
254
+ } else if ('source' in data && data.source) {
255
+ object = JSON.parse(loadSource(data.source) || '');
256
+ } else {
257
+ throw new Error('Unable to get object from data');
258
+ }
259
+ let errors = validateSchema(schema, object);
260
+ if (errors.length) {
261
+ let keys = errors.map(e => {
262
+ let message;
263
+ if (e.type === 'enum') {
264
+ let {
265
+ actualValue
266
+ } = e;
267
+ let expectedValues = e.expectedValues.map(String);
268
+ let likely = actualValue != null ? fuzzySearch(expectedValues, String(actualValue)) : [];
269
+ if (likely.length > 0) {
270
+ message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
271
+ } else if (expectedValues.length > 0) {
272
+ message = `Possible values: ${expectedValues.map(v => JSON.stringify(v)).join(', ')}`;
273
+ } else {
274
+ message = 'Unexpected value';
275
+ }
276
+ } else if (e.type === 'forbidden-prop') {
277
+ let {
278
+ prop,
279
+ expectedProps,
280
+ actualProps
281
+ } = e;
282
+ let likely = fuzzySearch(expectedProps, prop).filter(v => !actualProps.includes(v));
283
+ if (likely.length > 0) {
284
+ message = `Did you mean ${likely.map(v => JSON.stringify(v)).join(', ')}?`;
285
+ } else {
286
+ message = 'Unexpected property';
287
+ }
288
+ } else if (e.type === 'missing-prop') {
289
+ let {
290
+ prop,
291
+ actualProps
292
+ } = e;
293
+ let likely = fuzzySearch(actualProps, prop);
294
+ if (likely.length > 0) {
295
+ message = `Did you mean ${JSON.stringify(prop)}?`;
296
+ e.dataPath += '/' + likely[0];
297
+ e.dataType = 'key';
298
+ } else {
299
+ message = `Missing property ${prop}`;
300
+ }
301
+ } else if (e.type === 'type') {
302
+ if (e.prettyType != null) {
303
+ message = `Expected ${e.prettyType}`;
304
+ } else {
305
+ message = `Expected type ${e.expectedTypes.join(', ')}`;
306
+ }
307
+ } else {
308
+ message = e.message;
309
+ }
310
+ return {
311
+ key: e.dataPath,
312
+ type: e.dataType,
313
+ message
314
+ };
315
+ });
316
+ let map, code;
317
+ if ('map' in data && data.map) {
318
+ map = data.map;
319
+ code = loadSource(data.source) ?? '';
320
+ } else {
321
+ if ('source' in data && data.source) {
322
+ map = loadSource(data.source) ?? '';
323
+ } else if ('data' in data && data.data !== undefined) {
324
+ map = JSON.stringify((0, _nullthrows().default)(data.data), null, '\t');
325
+ } else {
326
+ map = '';
327
+ }
328
+ code = map;
329
+ }
330
+ let codeFrames = [{
331
+ filePath: data.filePath ?? undefined,
332
+ language: 'json',
333
+ code: code ?? '',
334
+ codeHighlights: (0, _diagnostic().generateJSONCodeHighlights)(map, keys.map(({
335
+ key,
336
+ type,
337
+ message
338
+ }) => ({
339
+ key: (data.prependKey ?? '') + key,
340
+ type: type,
341
+ message: message != null ? (0, _diagnostic().escapeMarkdown)(message) : message
342
+ })))
343
+ }];
344
+ throw new (_diagnostic().default)({
345
+ diagnostic: {
346
+ message: message,
347
+ origin,
348
+ codeFrames
349
+ }
350
+ });
351
+ }
352
+ };
@@ -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,137 @@
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("@atlaspack/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(e) { return e && e.__esModule ? e : { default: e }; }
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
+ if (end !== null && end !== void 0 && end.original) {
107
+ ({
108
+ line: endLine,
109
+ column: endCol
110
+ } = end.original);
111
+ endCol++; // source map columns are 0-based
112
+
113
+ if (endLine < startLine) {
114
+ endLine = startLine;
115
+ endCol = startCol;
116
+ } else if (endLine === startLine && endCol < startCol && lineDiff === 0) {
117
+ endCol = startCol + colDiff;
118
+ } else if (endLine === startLine && startCol === endCol && lineDiff === 0) {
119
+ // Prevent 0-length ranges
120
+ endCol = startCol + 1;
121
+ }
122
+ } else {
123
+ endLine = startLine;
124
+ endCol = startCol;
125
+ }
126
+ return {
127
+ filePath,
128
+ start: {
129
+ line: startLine,
130
+ column: startCol
131
+ },
132
+ end: {
133
+ line: endLine,
134
+ column: endCol
135
+ }
136
+ };
137
+ }
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;
@@ -0,0 +1,4 @@
1
+ import type { Blob } from '@atlaspack/types-internal';
2
+ import { Buffer } from 'buffer';
3
+ export declare function blobToBuffer(blob: Blob): Promise<Buffer>;
4
+ export declare function blobToString(blob: Blob): Promise<string>;
@@ -0,0 +1,4 @@
1
+ declare function getBundleURLCached(): string;
2
+ export declare function getBaseURL(url?: string | null): string;
3
+ export declare const getBundleURL: typeof getBundleURLCached;
4
+ export {};