@atlaspack/utils 2.17.3 → 2.17.4-typescript-5ad950d33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/LICENSE +201 -0
  2. package/lib/DefaultMap.d.ts +13 -0
  3. package/lib/DefaultMap.js +0 -4
  4. package/lib/Deferred.d.ts +8 -0
  5. package/lib/PromiseQueue.d.ts +25 -0
  6. package/lib/TapStream.d.ts +6 -0
  7. package/lib/alternatives.d.ts +3 -0
  8. package/lib/ansi-html.d.ts +1 -0
  9. package/lib/ansi-html.js +2 -0
  10. package/lib/blob.d.ts +4 -0
  11. package/lib/bundle-url.d.ts +4 -0
  12. package/lib/collection.d.ts +33 -0
  13. package/lib/config.d.ts +17 -0
  14. package/lib/config.js +3 -1
  15. package/lib/countLines.d.ts +1 -0
  16. package/lib/debounce.d.ts +1 -0
  17. package/lib/debug-tools.d.ts +6 -0
  18. package/lib/debug-tools.js +1 -0
  19. package/lib/dependency-location.d.ts +14 -0
  20. package/lib/escape-html.d.ts +1 -0
  21. package/lib/generateBuildMetrics.d.ts +18 -0
  22. package/lib/generateCertificate.d.ts +5 -0
  23. package/lib/getCertificate.d.ts +6 -0
  24. package/lib/getExisting.d.ts +8 -0
  25. package/lib/getModuleParts.d.ts +4 -0
  26. package/lib/getRootDir.d.ts +2 -0
  27. package/lib/glob.d.ts +10 -0
  28. package/lib/glob.js +12 -4
  29. package/lib/hash.d.ts +7 -0
  30. package/lib/http-server.d.ts +19 -0
  31. package/lib/index.d.ts +48 -0
  32. package/lib/index.js +164 -9
  33. package/lib/is-url.d.ts +1 -0
  34. package/lib/is-url.js +2 -0
  35. package/lib/isDirectoryInside.d.ts +2 -0
  36. package/lib/objectHash.d.ts +3 -0
  37. package/lib/objectHash.js +0 -1
  38. package/lib/openInBrowser.d.ts +1 -0
  39. package/lib/parseCSSImport.d.ts +1 -0
  40. package/lib/path.d.ts +8 -0
  41. package/lib/prettifyTime.d.ts +1 -0
  42. package/lib/prettyDiagnostic.d.ts +17 -0
  43. package/lib/prettyDiagnostic.js +3 -5
  44. package/lib/progress-message.d.ts +3 -0
  45. package/lib/relativeBundlePath.d.ts +4 -0
  46. package/lib/relativeUrl.d.ts +1 -0
  47. package/lib/replaceBundleReferences.d.ts +39 -0
  48. package/lib/schema.d.ts +107 -0
  49. package/lib/schema.js +38 -19
  50. package/lib/shared-buffer.d.ts +2 -0
  51. package/lib/shared-buffer.js +5 -1
  52. package/lib/sourcemap.d.ts +16 -0
  53. package/lib/stream.d.ts +8 -0
  54. package/lib/throttle.d.ts +1 -0
  55. package/lib/urlJoin.d.ts +5 -0
  56. package/lib/urlJoin.js +1 -1
  57. package/package.json +22 -15
  58. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  59. package/src/Deferred.ts +26 -0
  60. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  61. package/src/{TapStream.js → TapStream.ts} +8 -7
  62. package/src/{alternatives.js → alternatives.ts} +14 -15
  63. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  64. package/src/{blob.js → blob.ts} +2 -4
  65. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  66. package/src/{collection.js → collection.ts} +14 -21
  67. package/src/{config.js → config.ts} +18 -19
  68. package/src/{countLines.js → countLines.ts} +0 -2
  69. package/src/{debounce.js → debounce.ts} +3 -5
  70. package/src/{debug-tools.js → debug-tools.ts} +6 -8
  71. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  72. package/src/{escape-html.js → escape-html.ts} +5 -3
  73. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +16 -18
  74. package/src/{generateCertificate.js → generateCertificate.ts} +7 -5
  75. package/src/{getCertificate.js → getCertificate.ts} +5 -3
  76. package/src/{getExisting.js → getExisting.ts} +4 -3
  77. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  78. package/src/{getRootDir.js → getRootDir.ts} +0 -2
  79. package/src/{glob.js → glob.ts} +14 -10
  80. package/src/{hash.js → hash.ts} +22 -17
  81. package/src/{http-server.js → http-server.ts} +32 -38
  82. package/src/{index.js → index.ts} +7 -8
  83. package/src/{is-url.js → is-url.ts} +1 -2
  84. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +0 -1
  85. package/src/{objectHash.js → objectHash.ts} +1 -4
  86. package/src/{openInBrowser.js → openInBrowser.ts} +2 -4
  87. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  88. package/src/{path.js → path.ts} +1 -3
  89. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  90. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +22 -20
  91. package/src/{progress-message.js → progress-message.ts} +3 -2
  92. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +3 -3
  93. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  94. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +54 -36
  95. package/src/{schema.js → schema.ts} +158 -141
  96. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  97. package/src/{sourcemap.js → sourcemap.ts} +16 -6
  98. package/src/{stream.js → stream.ts} +29 -21
  99. package/src/throttle.ts +13 -0
  100. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  101. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  102. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  103. package/test/{collection.test.js → collection.test.ts} +0 -2
  104. package/test/{config.test.js → config.test.ts} +0 -3
  105. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  106. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  107. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  108. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  109. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  110. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  111. package/tsconfig.json +4 -0
  112. package/src/Deferred.js +0 -23
  113. package/src/throttle.js +0 -15
@@ -1,4 +1,3 @@
1
- // @flow strict-local
2
1
  import ThrowableDiagnostic, {
3
2
  generateJSONCodeHighlights,
4
3
  escapeMarkdown,
@@ -19,105 +18,107 @@ export type SchemaEntity =
19
18
  | SchemaAllOf
20
19
  | SchemaNot
21
20
  | SchemaAny;
22
- export type SchemaArray = {|
23
- type: 'array',
24
- items?: SchemaEntity,
25
- __type?: string,
26
- |};
27
- export type SchemaBoolean = {|
28
- type: 'boolean',
29
- __type?: string,
30
- |};
31
- export type SchemaOneOf = {|
32
- oneOf: Array<SchemaEntity>,
33
- |};
34
- export type SchemaAllOf = {|
35
- allOf: Array<SchemaEntity>,
36
- |};
37
- export type SchemaNot = {|
38
- not: SchemaEntity,
39
- __message: string,
40
- |};
41
- export type SchemaString = {|
42
- type: 'string',
43
- enum?: Array<string>,
44
- __validate?: (val: string) => ?string,
45
- __type?: string,
46
- |};
47
- export type SchemaNumber = {|
48
- type: 'number',
49
- enum?: Array<number>,
50
- __type?: string,
51
- |};
52
- export type SchemaEnum = {|
53
- enum: Array<mixed>,
54
- |};
55
- export type SchemaObject = {|
56
- type: 'object',
57
- properties: {[string]: SchemaEntity, ...},
58
- additionalProperties?: boolean | SchemaEntity,
59
- required?: Array<string>,
60
- __forbiddenProperties?: Array<string>,
61
- __type?: string,
62
- |};
63
- export type SchemaAny = {||};
21
+ export type SchemaArray = {
22
+ type: 'array';
23
+ items?: SchemaEntity;
24
+ __type?: string;
25
+ };
26
+ export type SchemaBoolean = {
27
+ type: 'boolean';
28
+ __type?: string;
29
+ };
30
+ export type SchemaOneOf = {
31
+ oneOf: Array<SchemaEntity>;
32
+ };
33
+ export type SchemaAllOf = {
34
+ allOf: Array<SchemaEntity>;
35
+ };
36
+ export type SchemaNot = {
37
+ not: SchemaEntity;
38
+ __message: string;
39
+ };
40
+ export type SchemaString = {
41
+ type: 'string';
42
+ enum?: Array<string>;
43
+ __validate?: (val: string) => string | null | undefined;
44
+ __type?: string;
45
+ };
46
+ export type SchemaNumber = {
47
+ type: 'number';
48
+ enum?: Array<number>;
49
+ __type?: string;
50
+ };
51
+ export type SchemaEnum = {
52
+ enum: Array<unknown>;
53
+ };
54
+ export type SchemaObject = {
55
+ type: 'object';
56
+ properties: {
57
+ [key: string]: SchemaEntity;
58
+ };
59
+ additionalProperties?: boolean | SchemaEntity;
60
+ required?: Array<string>;
61
+ __forbiddenProperties?: Array<string>;
62
+ __type?: string;
63
+ };
64
+ export type SchemaAny = Record<any, any>;
64
65
  export type SchemaError =
65
- | {|
66
- type: 'type',
67
- expectedTypes: Array<string>,
68
- dataType: ?'key' | 'value',
69
-
70
- dataPath: string,
71
- ancestors: Array<SchemaEntity>,
72
- prettyType?: string,
73
- |}
74
- | {|
75
- type: 'enum',
76
- expectedValues: Array<mixed>,
77
- dataType: 'key' | 'value',
78
- actualValue: mixed,
79
-
80
- dataPath: string,
81
- ancestors: Array<SchemaEntity>,
82
- prettyType?: string,
83
- |}
84
- | {|
85
- type: 'forbidden-prop',
86
- prop: string,
87
- expectedProps: Array<string>,
88
- actualProps: Array<string>,
89
- dataType: 'key',
90
-
91
- dataPath: string,
92
- ancestors: Array<SchemaEntity>,
93
- prettyType?: string,
94
- |}
95
- | {|
96
- type: 'missing-prop',
97
- prop: string,
98
- expectedProps: Array<string>,
99
- actualProps: Array<string>,
100
- dataType: 'key' | 'value',
101
-
102
- dataPath: string,
103
- ancestors: Array<SchemaEntity>,
104
- prettyType?: string,
105
- |}
106
- | {|
107
- type: 'other',
108
- actualValue: mixed,
109
- dataType: ?'key' | 'value',
110
- message?: string,
111
- dataPath: string,
112
- ancestors: Array<SchemaEntity>,
113
- |};
66
+ | {
67
+ type: 'type';
68
+ expectedTypes: Array<string>;
69
+ dataType: 'key' | null | undefined | 'value';
70
+ dataPath: string;
71
+ ancestors: Array<SchemaEntity>;
72
+ prettyType?: string;
73
+ }
74
+ | {
75
+ type: 'enum';
76
+ expectedValues: Array<unknown>;
77
+ dataType: 'key' | 'value';
78
+ actualValue: unknown;
79
+ dataPath: string;
80
+ ancestors: Array<SchemaEntity>;
81
+ prettyType?: string;
82
+ }
83
+ | {
84
+ type: 'forbidden-prop';
85
+ prop: string;
86
+ expectedProps: Array<string>;
87
+ actualProps: Array<string>;
88
+ dataType: 'key';
89
+ dataPath: string;
90
+ ancestors: Array<SchemaEntity>;
91
+ prettyType?: string;
92
+ }
93
+ | {
94
+ type: 'missing-prop';
95
+ prop: string;
96
+ expectedProps: Array<string>;
97
+ actualProps: Array<string>;
98
+ dataType: 'key' | 'value';
99
+ dataPath: string;
100
+ ancestors: Array<SchemaEntity>;
101
+ prettyType?: string;
102
+ }
103
+ | {
104
+ type: 'other';
105
+ actualValue: unknown;
106
+ dataType: 'key' | null | undefined | 'value';
107
+ message?: string;
108
+ dataPath: string;
109
+ ancestors: Array<SchemaEntity>;
110
+ };
114
111
 
115
- function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
112
+ function validateSchema(
113
+ schema: SchemaEntity,
114
+ data: unknown,
115
+ ): Array<SchemaError> {
116
116
  function walk(
117
+ // @ts-expect-error TS7006
117
118
  schemaAncestors,
118
- dataNode,
119
- dataPath,
120
- ): ?SchemaError | Array<SchemaError> {
119
+ dataNode: unknown,
120
+ dataPath: string,
121
+ ): SchemaError | null | undefined | Array<SchemaError> {
121
122
  let [schemaNode] = schemaAncestors;
122
123
 
123
124
  if (schemaNode.type) {
@@ -136,23 +137,26 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
136
137
  case 'array': {
137
138
  if (schemaNode.items) {
138
139
  let results: Array<SchemaError | Array<SchemaError>> = [];
139
- // $FlowFixMe type was already checked
140
+ // @ts-expect-error TS18046
140
141
  for (let i = 0; i < dataNode.length; i++) {
141
142
  let result = walk(
142
143
  [schemaNode.items].concat(schemaAncestors),
143
- // $FlowFixMe type was already checked
144
+ // @ts-expect-error TS18046
144
145
  dataNode[i],
145
146
  dataPath + '/' + i,
146
147
  );
147
148
  if (result) results.push(result);
148
149
  }
149
150
  if (results.length)
150
- return results.reduce((acc, v) => acc.concat(v), []);
151
+ return results.reduce<Array<any>>(
152
+ (acc, v) => acc.concat(v),
153
+ [],
154
+ );
151
155
  }
152
156
  break;
153
157
  }
154
158
  case 'string': {
155
- // $FlowFixMe type was already checked
159
+ // @ts-expect-error TS2322
156
160
  let value: string = dataNode;
157
161
  if (schemaNode.enum) {
158
162
  if (!schemaNode.enum.includes(value)) {
@@ -181,7 +185,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
181
185
  break;
182
186
  }
183
187
  case 'number': {
184
- // $FlowFixMe type was already checked
188
+ // @ts-expect-error TS2322
185
189
  let value: number = dataNode;
186
190
  if (schemaNode.enum) {
187
191
  if (!schemaNode.enum.includes(value)) {
@@ -201,13 +205,15 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
201
205
  let results: Array<Array<SchemaError> | SchemaError> = [];
202
206
  let invalidProps;
203
207
  if (schemaNode.__forbiddenProperties) {
204
- // $FlowFixMe type was already checked
208
+ // @ts-expect-error TS2769
205
209
  let keys = Object.keys(dataNode);
210
+ // @ts-expect-error TS7006
206
211
  invalidProps = schemaNode.__forbiddenProperties.filter((val) =>
207
212
  keys.includes(val),
208
213
  );
209
214
  results.push(
210
215
  ...invalidProps.map(
216
+ // @ts-expect-error TS7006
211
217
  (k) =>
212
218
  ({
213
219
  type: 'forbidden-prop',
@@ -217,18 +223,20 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
217
223
  expectedProps: Object.keys(schemaNode.properties),
218
224
  actualProps: keys,
219
225
  ancestors: schemaAncestors,
220
- }: SchemaError),
226
+ }) as SchemaError,
221
227
  ),
222
228
  );
223
229
  }
224
230
  if (schemaNode.required) {
225
- // $FlowFixMe type was already checked
231
+ // @ts-expect-error TS2769
226
232
  let keys = Object.keys(dataNode);
227
233
  let missingKeys = schemaNode.required.filter(
234
+ // @ts-expect-error TS7006
228
235
  (val) => !keys.includes(val),
229
236
  );
230
237
  results.push(
231
238
  ...missingKeys.map(
239
+ // @ts-expect-error TS7006
232
240
  (k) =>
233
241
  ({
234
242
  type: 'missing-prop',
@@ -238,13 +246,13 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
238
246
  expectedProps: schemaNode.required,
239
247
  actualProps: keys,
240
248
  ancestors: schemaAncestors,
241
- }: SchemaError),
249
+ }) as SchemaError,
242
250
  ),
243
251
  );
244
252
  }
245
253
  if (schemaNode.properties) {
246
254
  let {additionalProperties = true} = schemaNode;
247
- // $FlowFixMe type was already checked
255
+ // @ts-expect-error TS2407
248
256
  for (let k in dataNode) {
249
257
  if (invalidProps && invalidProps.includes(k)) {
250
258
  // Don't check type on forbidden props
@@ -252,7 +260,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
252
260
  } else if (k in schemaNode.properties) {
253
261
  let result = walk(
254
262
  [schemaNode.properties[k]].concat(schemaAncestors),
255
- // $FlowFixMe type was already checked
263
+ // @ts-expect-error TS18046
256
264
  dataNode[k],
257
265
  dataPath + '/' + encodeJSONKeyComponent(k),
258
266
  );
@@ -266,10 +274,8 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
266
274
  dataPath: dataPath + '/' + encodeJSONKeyComponent(k),
267
275
  expectedValues: Object.keys(
268
276
  schemaNode.properties,
269
- ).filter(
270
- // $FlowFixMe type was already checked
271
- (p) => !(p in dataNode),
272
- ),
277
+ // @ts-expect-error TS18046
278
+ ).filter((p) => !(p in dataNode)),
273
279
  actualValue: k,
274
280
  ancestors: schemaAncestors,
275
281
  prettyType: schemaNode.__type,
@@ -278,7 +284,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
278
284
  } else {
279
285
  let result = walk(
280
286
  [additionalProperties].concat(schemaAncestors),
281
- // $FlowFixMe type was already checked
287
+ // @ts-expect-error TS18046
282
288
  dataNode[k],
283
289
  dataPath + '/' + encodeJSONKeyComponent(k),
284
290
  );
@@ -288,7 +294,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
288
294
  }
289
295
  }
290
296
  if (results.length)
291
- return results.reduce((acc, v) => acc.concat(v), []);
297
+ return results.reduce<Array<any>>((acc, v) => acc.concat(v), []);
292
298
  break;
293
299
  }
294
300
  case 'boolean':
@@ -328,10 +334,10 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
328
334
  ? Array.isArray(a) && !Array.isArray(b)
329
335
  ? -1
330
336
  : !Array.isArray(a) && Array.isArray(b)
331
- ? 1
332
- : Array.isArray(a) && Array.isArray(b)
333
- ? b.length - a.length
334
- : 0
337
+ ? 1
338
+ : Array.isArray(a) && Array.isArray(b)
339
+ ? b.length - a.length
340
+ : 0
335
341
  : b.dataPath.length - a.dataPath.length,
336
342
  );
337
343
  return results[0];
@@ -342,6 +348,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
342
348
  dataNode,
343
349
  dataPath,
344
350
  );
351
+ // @ts-expect-error TS2339
345
352
  if (!result || result.length == 0) {
346
353
  return {
347
354
  type: 'other',
@@ -371,33 +378,38 @@ export function fuzzySearch(
371
378
  .map((exp) => [exp, levenshtein.distance(exp, actualValue)])
372
379
  .filter(
373
380
  // Remove if more than half of the string would need to be changed
374
- ([, d]) => d * 2 < actualValue.length,
381
+ // @ts-expect-error TS2769
382
+ ([, d]: [any, any]) => d * 2 < actualValue.length,
375
383
  );
376
- result.sort(([, a], [, b]) => a - b);
377
- return result.map(([v]) => v);
384
+ // @ts-expect-error TS2345
385
+ result.sort(([, a]: [any, any], [, b]: [any, any]) => a - b);
386
+ // @ts-expect-error TS2345
387
+ return result.map(([v]: [any]) => v);
378
388
  }
379
389
 
380
390
  validateSchema.diagnostic = function (
381
391
  schema: SchemaEntity,
382
- data: {|
383
- ...
384
- | {|
385
- source?: ?string,
386
- data?: mixed,
387
- |}
388
- | {|
389
- source: string,
390
- map: {|
391
- data: mixed,
392
- pointers: {|[key: string]: Mapping|},
393
- |},
394
- |},
395
- filePath?: ?string,
396
- prependKey?: ?string,
397
- |},
392
+ data: (
393
+ | {
394
+ source?: string | null | undefined;
395
+ data?: unknown;
396
+ }
397
+ | {
398
+ source: string;
399
+ map: {
400
+ data: unknown;
401
+ pointers: {
402
+ [key: string]: Mapping;
403
+ };
404
+ };
405
+ }
406
+ ) & {
407
+ filePath?: string | null | undefined;
408
+ prependKey?: string | null | undefined;
409
+ },
398
410
  origin: string,
399
411
  message: string,
400
- ): void {
412
+ ): undefined {
401
413
  if (
402
414
  'source' in data &&
403
415
  'data' in data &&
@@ -408,10 +420,12 @@ validateSchema.diagnostic = function (
408
420
  'At least one of data.source and data.data must be defined!',
409
421
  );
410
422
  }
423
+ // @ts-expect-error TS2339
411
424
  let object = data.map
412
- ? data.map.data
413
- : // $FlowFixMe we can assume it's a JSON object
414
- data.data ?? JSON.parse(data.source);
425
+ ? // @ts-expect-error TS2339
426
+ data.map.data
427
+ : // @ts-expect-error TS2339
428
+ (data.data ?? JSON.parse(data.source));
415
429
  let errors = validateSchema(schema, object);
416
430
  if (errors.length) {
417
431
  let keys = errors.map((e) => {
@@ -469,11 +483,13 @@ validateSchema.diagnostic = function (
469
483
  return {key: e.dataPath, type: e.dataType, message};
470
484
  });
471
485
  let map, code;
486
+ // @ts-expect-error TS2339
472
487
  if (data.map) {
488
+ // @ts-expect-error TS2339
473
489
  map = data.map;
474
490
  code = data.source;
475
491
  } else {
476
- // $FlowFixMe we can assume that data is valid JSON
492
+ // @ts-expect-error TS2339
477
493
  map = data.source ?? JSON.stringify(nullthrows(data.data), 0, '\t');
478
494
  code = map;
479
495
  }
@@ -497,6 +513,7 @@ validateSchema.diagnostic = function (
497
513
  diagnostic: {
498
514
  message: message,
499
515
  origin,
516
+ // @ts-expect-error TS2322
500
517
  codeFrames,
501
518
  },
502
519
  });
@@ -1,8 +1,10 @@
1
- // @flow
1
+ // flow-to-ts helpers
2
+ export type Class<T> = new (...args: any[]) => T;
3
+ // /flow-to-ts helpers
2
4
 
3
5
  export let SharedBuffer: Class<ArrayBuffer> | Class<SharedArrayBuffer>;
4
6
 
5
- // $FlowFixMe[prop-missing]
7
+ // @ts-expect-error process.browser is a browser-specific property
6
8
  if (process.browser) {
7
9
  SharedBuffer = ArrayBuffer;
8
10
  // Safari has removed the constructor
@@ -12,7 +14,7 @@ if (process.browser) {
12
14
  // Firefox might throw when sending the Buffer over a MessagePort
13
15
  channel.port1.postMessage(new SharedArrayBuffer(0));
14
16
  SharedBuffer = SharedArrayBuffer;
15
- } catch (_) {
17
+ } catch (_: any) {
16
18
  // NOOP
17
19
  }
18
20
  channel.port1.close();
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  import type {SourceLocation} from '@atlaspack/types';
3
2
  import type {FileSystem} from '@atlaspack/fs';
4
3
  import SourceMap from '@parcel/source-map';
@@ -21,7 +20,7 @@ export const SOURCEMAP_EXTENSIONS: Set<string> = new Set<string>([
21
20
 
22
21
  export function matchSourceMappingURL(
23
22
  contents: string,
24
- ): null | RegExp$matchResult {
23
+ ): RegExpMatchArray | null {
25
24
  return contents.match(SOURCEMAP_RE);
26
25
  }
27
26
 
@@ -29,7 +28,15 @@ export async function loadSourceMapUrl(
29
28
  fs: FileSystem,
30
29
  filename: string,
31
30
  contents: string,
32
- ): Promise<?{|filename: string, map: any, url: string|}> {
31
+ ): Promise<
32
+ | {
33
+ filename: string;
34
+ map: any;
35
+ url: string;
36
+ }
37
+ | null
38
+ | undefined
39
+ > {
33
40
  let match = matchSourceMappingURL(contents);
34
41
  if (match) {
35
42
  let url = match[1].trim();
@@ -60,8 +67,11 @@ export async function loadSourceMapUrl(
60
67
  export async function loadSourceMap(
61
68
  filename: string,
62
69
  contents: string,
63
- options: {fs: FileSystem, projectRoot: string, ...},
64
- ): Promise<?SourceMap> {
70
+ options: {
71
+ fs: FileSystem;
72
+ projectRoot: string;
73
+ },
74
+ ): Promise<SourceMap | null | undefined> {
65
75
  let foundMap = await loadSourceMapUrl(options.fs, filename, contents);
66
76
  if (foundMap) {
67
77
  let mapSourceRoot = path.dirname(filename);
@@ -75,7 +85,7 @@ export async function loadSourceMap(
75
85
  let sourcemapInstance = new SourceMap(options.projectRoot);
76
86
  sourcemapInstance.addVLQMap({
77
87
  ...foundMap.map,
78
- sources: foundMap.map.sources.map((s) => {
88
+ sources: foundMap.map.sources.map((s: string) => {
79
89
  return path.join(mapSourceRoot, s);
80
90
  }),
81
91
  });
@@ -1,17 +1,20 @@
1
- // @flow strict-local
2
-
3
1
  import {Readable, PassThrough} from 'stream';
4
2
  import type {Blob} from '@atlaspack/types';
5
3
 
6
4
  export function measureStreamLength(stream: Readable): Promise<number> {
7
- return new Promise((resolve, reject) => {
8
- let length = 0;
9
- stream.on('data', (chunk) => {
10
- length += chunk;
11
- });
12
- stream.on('end', () => resolve(length));
13
- stream.on('error', reject);
14
- });
5
+ return new Promise(
6
+ (
7
+ resolve: (result: Promise<number> | number) => void,
8
+ reject: (error?: any) => void,
9
+ ) => {
10
+ let length = 0;
11
+ stream.on('data', (chunk) => {
12
+ length += chunk;
13
+ });
14
+ stream.on('end', () => resolve(length));
15
+ stream.on('error', reject);
16
+ },
17
+ );
15
18
  }
16
19
 
17
20
  export function readableFromStringOrBuffer(str: string | Buffer): Readable {
@@ -23,16 +26,21 @@ export function readableFromStringOrBuffer(str: string | Buffer): Readable {
23
26
  }
24
27
 
25
28
  export function bufferStream(stream: Readable): Promise<Buffer> {
26
- return new Promise((resolve, reject) => {
27
- let buf = Buffer.from([]);
28
- stream.on('data', (data) => {
29
- buf = Buffer.concat([buf, data]);
30
- });
31
- stream.on('end', () => {
32
- resolve(buf);
33
- });
34
- stream.on('error', reject);
35
- });
29
+ return new Promise(
30
+ (
31
+ resolve: (result: Promise<Buffer> | Buffer) => void,
32
+ reject: (error?: any) => void,
33
+ ) => {
34
+ let buf = Buffer.from([]);
35
+ stream.on('data', (data) => {
36
+ buf = Buffer.concat([buf, data]);
37
+ });
38
+ stream.on('end', () => {
39
+ resolve(buf);
40
+ });
41
+ stream.on('error', reject);
42
+ },
43
+ );
36
44
  }
37
45
 
38
46
  export function blobToStream(blob: Blob): Readable {
@@ -62,7 +70,7 @@ export function fallbackStream(
62
70
  ): Readable {
63
71
  const res = new PassThrough();
64
72
  stream.on('error', (err) => {
65
- if (err.code === 'ENOENT') {
73
+ if ((err as any).code === 'ENOENT') {
66
74
  fallback().pipe(res);
67
75
  } else {
68
76
  res.emit('error', err);
@@ -0,0 +1,13 @@
1
+ export default function throttle<TArgs extends Array<unknown>>(
2
+ fn: (...args: TArgs) => unknown,
3
+ delay: number,
4
+ ): (...args: TArgs) => void {
5
+ let lastCalled: number | null | undefined;
6
+
7
+ return function throttled(this: any, ...args: TArgs) {
8
+ if (lastCalled == null || lastCalled + delay <= Date.now()) {
9
+ fn.call(this, ...args);
10
+ lastCalled = Date.now();
11
+ }
12
+ };
13
+ }
@@ -1,5 +1,3 @@
1
- // @flow strict-local
2
-
3
1
  import URL from 'url';
4
2
  import path from 'path';
5
3
 
@@ -12,7 +10,7 @@ export default function urlJoin(publicURL: string, assetPath: string): string {
12
10
  // Leading / ensures that paths with colons are not parsed as a protocol.
13
11
  let p = assetPath.startsWith('/') ? assetPath : '/' + assetPath;
14
12
  const assetUrl = URL.parse(p);
15
- url.pathname = path.posix.join(url.pathname, assetUrl.pathname);
13
+ url.pathname = path.posix.join(url.pathname ?? '', assetUrl.pathname ?? '');
16
14
  url.search = assetUrl.search;
17
15
  url.hash = assetUrl.hash;
18
16
  return URL.format(url);
@@ -1,12 +1,10 @@
1
- // @flow strict-local
2
-
3
1
  import assert from 'assert';
4
2
  import {DefaultMap} from '../src/DefaultMap';
5
3
 
6
4
  describe('DefaultMap', () => {
7
5
  it('constructs with entries just like Map', () => {
8
6
  let map = new DefaultMap(
9
- (k) => k,
7
+ (k: any) => k,
10
8
  [
11
9
  [1, 3],
12
10
  [2, 27],
@@ -20,18 +18,18 @@ describe('DefaultMap', () => {
20
18
  });
21
19
 
22
20
  it("returns a default value based on a key if it doesn't exist", () => {
23
- let map = new DefaultMap((k) => k);
21
+ let map = new DefaultMap((k: any) => k);
24
22
  assert.equal(map.get(3), 3);
25
23
  });
26
24
 
27
25
  it("sets a default value based on a key if it doesn't exist", () => {
28
- let map = new DefaultMap((k) => k);
26
+ let map = new DefaultMap((k: any) => k);
29
27
  map.get(3);
30
28
  assert.deepEqual(Array.from(map.entries()), [[3, 3]]);
31
29
  });
32
30
 
33
31
  it('respects undefined/null if it already existed in the map', () => {
34
- let map = new DefaultMap<number, number | void | null>((k) => k);
32
+ let map = new DefaultMap<number, number | undefined | null>((k: any) => k);
35
33
  map.set(3, undefined);
36
34
  assert.equal(map.get(3), undefined);
37
35