@atlaspack/utils 2.14.5-canary.24 → 2.14.5-canary.240

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 (191) hide show
  1. package/CHANGELOG.md +294 -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 +389 -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 +355 -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} +158 -141
  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/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  184. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  185. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  186. package/tsconfig.json +33 -0
  187. package/tsconfig.tsbuildinfo +1 -0
  188. package/lib/index.js.map +0 -1
  189. package/src/Deferred.js +0 -23
  190. package/src/progress-message.js +0 -22
  191. package/src/throttle.js +0 -15
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedBuffer = void 0;
4
+ // @ts-expect-error process.browser is a browser-specific property
5
+ if (process.browser) {
6
+ exports.SharedBuffer = ArrayBuffer;
7
+ // Safari has removed the constructor
8
+ if (typeof SharedArrayBuffer !== 'undefined') {
9
+ let channel = new MessageChannel();
10
+ try {
11
+ // Firefox might throw when sending the Buffer over a MessagePort
12
+ channel.port1.postMessage(new SharedArrayBuffer(0));
13
+ exports.SharedBuffer = SharedArrayBuffer;
14
+ }
15
+ catch (_) {
16
+ // NOOP
17
+ }
18
+ channel.port1.close();
19
+ channel.port2.close();
20
+ }
21
+ }
22
+ else {
23
+ exports.SharedBuffer = SharedArrayBuffer;
24
+ }
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SOURCEMAP_EXTENSIONS = exports.SOURCEMAP_RE = void 0;
7
+ exports.matchSourceMappingURL = matchSourceMappingURL;
8
+ exports.loadSourceMapUrl = loadSourceMapUrl;
9
+ exports.loadSourceMap = loadSourceMap;
10
+ exports.remapSourceLocation = remapSourceLocation;
11
+ const feature_flags_1 = require("@atlaspack/feature-flags");
12
+ const source_map_1 = __importDefault(require("@parcel/source-map"));
13
+ const path_1 = __importDefault(require("path"));
14
+ const path_2 = require("./path");
15
+ exports.SOURCEMAP_RE = /(?:\/\*|\/\/)\s*[@#]\s*sourceMappingURL\s*=\s*([^\s*]+)(?:\s*\*\/)?\s*$/;
16
+ const DATA_URL_RE = /^data:[^;]+(?:;charset=[^;]+)?;base64,(.*)/;
17
+ exports.SOURCEMAP_EXTENSIONS = new Set([
18
+ 'css',
19
+ 'es',
20
+ 'es6',
21
+ 'js',
22
+ 'jsx',
23
+ 'mjs',
24
+ 'ts',
25
+ 'tsx',
26
+ ]);
27
+ function matchSourceMappingURL(contents) {
28
+ return contents.match(exports.SOURCEMAP_RE);
29
+ }
30
+ async function loadSourceMapUrl(fs, filename, contents) {
31
+ let match = matchSourceMappingURL(contents);
32
+ if (match) {
33
+ let url = match[1].trim();
34
+ let dataURLMatch = url.match(DATA_URL_RE);
35
+ let mapFilePath;
36
+ if (dataURLMatch) {
37
+ mapFilePath = filename;
38
+ }
39
+ else {
40
+ mapFilePath = url.replace(/^file:\/\//, '');
41
+ mapFilePath = (0, path_2.isAbsolute)(mapFilePath)
42
+ ? mapFilePath
43
+ : path_1.default.join(path_1.default.dirname(filename), mapFilePath);
44
+ }
45
+ return {
46
+ url,
47
+ filename: mapFilePath,
48
+ map: JSON.parse(dataURLMatch
49
+ ? Buffer.from(dataURLMatch[1], 'base64').toString()
50
+ : await fs.readFile(mapFilePath, 'utf8')),
51
+ };
52
+ }
53
+ }
54
+ async function loadSourceMap(filename, contents, options) {
55
+ let foundMap = await loadSourceMapUrl(options.fs, filename, contents);
56
+ if (foundMap) {
57
+ let mapSourceRoot = path_1.default.dirname(filename);
58
+ if (foundMap.map.sourceRoot &&
59
+ !(0, path_2.normalizeSeparators)(foundMap.map.sourceRoot).startsWith('/')) {
60
+ mapSourceRoot = path_1.default.join(mapSourceRoot, foundMap.map.sourceRoot);
61
+ }
62
+ let sourcemapInstance = new source_map_1.default(options.projectRoot);
63
+ sourcemapInstance.addVLQMap({
64
+ ...foundMap.map,
65
+ sources: foundMap.map.sources.map((s) => {
66
+ return path_1.default.join(mapSourceRoot, s);
67
+ }),
68
+ });
69
+ return sourcemapInstance;
70
+ }
71
+ }
72
+ function remapSourceLocation(loc, originalMap, projectRoot) {
73
+ let { filePath, start: { line: startLine, column: startCol }, end: { line: endLine, column: endCol }, } = loc;
74
+ let lineDiff = endLine - startLine;
75
+ let colDiff = endCol - startCol;
76
+ let start = originalMap.findClosestMapping(startLine, startCol - 1);
77
+ let end = originalMap.findClosestMapping(endLine, endCol - 1);
78
+ if (start?.original) {
79
+ if (start.source) {
80
+ if ((0, feature_flags_1.getFeatureFlag)('symbolLocationFix') &&
81
+ !path_1.default.isAbsolute(start.source)) {
82
+ filePath = path_1.default.join(projectRoot, start.source);
83
+ }
84
+ else {
85
+ filePath = start.source;
86
+ }
87
+ }
88
+ ({ line: startLine, column: startCol } = start.original);
89
+ startCol++; // source map columns are 0-based
90
+ }
91
+ if (end?.original) {
92
+ ({ line: endLine, column: endCol } = end.original);
93
+ endCol++; // source map columns are 0-based
94
+ if (endLine < startLine) {
95
+ endLine = startLine;
96
+ endCol = startCol;
97
+ }
98
+ else if (endLine === startLine && endCol < startCol && lineDiff === 0) {
99
+ endCol = startCol + colDiff;
100
+ }
101
+ else if (endLine === startLine && startCol === endCol && lineDiff === 0) {
102
+ // Prevent 0-length ranges
103
+ endCol = startCol + 1;
104
+ }
105
+ }
106
+ else {
107
+ endLine = startLine;
108
+ endCol = startCol;
109
+ }
110
+ return {
111
+ filePath,
112
+ start: {
113
+ line: startLine,
114
+ column: startCol,
115
+ },
116
+ end: {
117
+ line: endLine,
118
+ column: endCol,
119
+ },
120
+ };
121
+ }
package/dist/stream.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.measureStreamLength = measureStreamLength;
4
+ exports.readableFromStringOrBuffer = readableFromStringOrBuffer;
5
+ exports.bufferStream = bufferStream;
6
+ exports.blobToStream = blobToStream;
7
+ exports.streamFromPromise = streamFromPromise;
8
+ exports.fallbackStream = fallbackStream;
9
+ const stream_1 = require("stream");
10
+ function measureStreamLength(stream) {
11
+ return new Promise((resolve, reject) => {
12
+ let length = 0;
13
+ stream.on('data', (chunk) => {
14
+ length += chunk;
15
+ });
16
+ stream.on('end', () => resolve(length));
17
+ stream.on('error', reject);
18
+ });
19
+ }
20
+ function readableFromStringOrBuffer(str) {
21
+ // https://stackoverflow.com/questions/12755997/how-to-create-streams-from-string-in-node-js
22
+ const stream = new stream_1.Readable();
23
+ stream.push(str);
24
+ stream.push(null);
25
+ return stream;
26
+ }
27
+ function bufferStream(stream) {
28
+ return new Promise((resolve, reject) => {
29
+ let buf = Buffer.from([]);
30
+ stream.on('data', (data) => {
31
+ buf = Buffer.concat([buf, data]);
32
+ });
33
+ stream.on('end', () => {
34
+ resolve(buf);
35
+ });
36
+ stream.on('error', reject);
37
+ });
38
+ }
39
+ function blobToStream(blob) {
40
+ if (blob instanceof stream_1.Readable) {
41
+ return blob;
42
+ }
43
+ return readableFromStringOrBuffer(blob);
44
+ }
45
+ function streamFromPromise(promise) {
46
+ const stream = new stream_1.PassThrough();
47
+ promise.then((blob) => {
48
+ if (blob instanceof stream_1.Readable) {
49
+ blob.pipe(stream);
50
+ }
51
+ else {
52
+ stream.end(blob);
53
+ }
54
+ });
55
+ return stream;
56
+ }
57
+ function fallbackStream(stream, fallback) {
58
+ const res = new stream_1.PassThrough();
59
+ stream.on('error', (err) => {
60
+ if (err.code === 'ENOENT') {
61
+ fallback().pipe(res);
62
+ }
63
+ else {
64
+ res.emit('error', err);
65
+ }
66
+ });
67
+ stream.pipe(res);
68
+ return res;
69
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = throttle;
4
+ function throttle(fn, delay) {
5
+ let lastCalled;
6
+ return function throttled(...args) {
7
+ if (lastCalled == null || lastCalled + delay <= Date.now()) {
8
+ fn.call(this, ...args);
9
+ lastCalled = Date.now();
10
+ }
11
+ };
12
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = urlJoin;
7
+ const url_1 = __importDefault(require("url"));
8
+ const path_1 = __importDefault(require("path"));
9
+ /**
10
+ * Joins a path onto a URL, and normalizes Windows paths
11
+ * e.g. from \path\to\res.js to /path/to/res.js.
12
+ */
13
+ function urlJoin(publicURL, assetPath) {
14
+ const url = url_1.default.parse(publicURL, false, true);
15
+ // Leading / ensures that paths with colons are not parsed as a protocol.
16
+ let p = assetPath.startsWith('/') ? assetPath : '/' + assetPath;
17
+ const assetUrl = url_1.default.parse(p);
18
+ url.pathname = path_1.default.posix.join(url.pathname ?? '', assetUrl.pathname ?? '');
19
+ url.search = assetUrl.search;
20
+ url.hash = assetUrl.hash;
21
+ return url_1.default.format(url);
22
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DefaultWeakMap = exports.DefaultMap = void 0;
7
+ class DefaultMap extends Map {
8
+ constructor(getDefault, entries) {
9
+ super(entries);
10
+ this._getDefault = getDefault;
11
+ }
12
+ get(key) {
13
+ let ret;
14
+ if (this.has(key)) {
15
+ ret = super.get(key);
16
+ } else {
17
+ ret = this._getDefault(key);
18
+ this.set(key, ret);
19
+ }
20
+ return ret;
21
+ }
22
+ }
23
+ exports.DefaultMap = DefaultMap;
24
+ // Duplicated from DefaultMap implementation for Flow
25
+ // Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
26
+ class DefaultWeakMap extends WeakMap {
27
+ constructor(getDefault, entries) {
28
+ super(entries);
29
+ this._getDefault = getDefault;
30
+ }
31
+ get(key) {
32
+ let ret;
33
+ if (this.has(key)) {
34
+ ret = super.get(key);
35
+ } else {
36
+ ret = this._getDefault(key);
37
+ this.set(key, ret);
38
+ }
39
+ return ret;
40
+ }
41
+ }
42
+ exports.DefaultWeakMap = DefaultWeakMap;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.makeDeferredWithPromise = makeDeferredWithPromise;
7
+ function _assert() {
8
+ const data = _interopRequireDefault(require("assert"));
9
+ _assert = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ function makeDeferredWithPromise() {
16
+ let deferred;
17
+ let promise = new Promise((resolve, reject) => {
18
+ deferred = {
19
+ resolve,
20
+ reject
21
+ };
22
+ });
23
+
24
+ // Promise constructor callback executes synchronously, so this is defined
25
+ (0, _assert().default)(deferred != null);
26
+ return {
27
+ deferred,
28
+ promise
29
+ };
30
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _Deferred = require("./Deferred");
8
+ class PromiseQueue {
9
+ _numRunning = 0;
10
+ _queue = [];
11
+ _runPromise = null;
12
+ _count = 0;
13
+ _results = [];
14
+ _addSubscriptions = new Set();
15
+ constructor(opts = {
16
+ maxConcurrent: Infinity
17
+ }) {
18
+ if (opts.maxConcurrent <= 0) {
19
+ throw new TypeError('maxConcurrent must be a positive, non-zero value');
20
+ }
21
+ this._maxConcurrent = opts.maxConcurrent;
22
+ }
23
+ getNumWaiting() {
24
+ return this._queue.length;
25
+ }
26
+ add(fn) {
27
+ new Promise((resolve, reject) => {
28
+ let i = this._count++;
29
+ let wrapped = () => fn().then(result => {
30
+ this._results[i] = result;
31
+ resolve(result);
32
+ }, err => {
33
+ reject(err);
34
+ throw err;
35
+ });
36
+ this._queue.push(wrapped);
37
+ for (const addFn of this._addSubscriptions) {
38
+ addFn();
39
+ }
40
+ if (this._numRunning > 0 && this._numRunning < this._maxConcurrent) {
41
+ this._next();
42
+ }
43
+ }).catch(() => {});
44
+ }
45
+ subscribeToAdd(fn) {
46
+ this._addSubscriptions.add(fn);
47
+ return () => {
48
+ this._addSubscriptions.delete(fn);
49
+ };
50
+ }
51
+ run() {
52
+ if (this._runPromise != null) {
53
+ return this._runPromise;
54
+ }
55
+ if (this._queue.length === 0) {
56
+ return Promise.resolve([]);
57
+ }
58
+ let {
59
+ deferred,
60
+ promise
61
+ } = (0, _Deferred.makeDeferredWithPromise)();
62
+ this._deferred = deferred;
63
+ this._runPromise = promise;
64
+ while (this._queue.length && this._numRunning < this._maxConcurrent) {
65
+ this._next();
66
+ }
67
+ return promise;
68
+ }
69
+ async _next() {
70
+ let fn = this._queue.shift();
71
+ await this._runFn(fn);
72
+ if (this._queue.length) {
73
+ this._next();
74
+ } else if (this._numRunning === 0) {
75
+ this._done();
76
+ }
77
+ }
78
+ async _runFn(fn) {
79
+ this._numRunning++;
80
+ try {
81
+ await fn();
82
+ } catch (e) {
83
+ // Only store the first error that occurs.
84
+ // We don't reject immediately so that any other concurrent
85
+ // requests have time to complete.
86
+ if (this._error == null) {
87
+ this._error = e;
88
+ }
89
+ } finally {
90
+ this._numRunning--;
91
+ }
92
+ }
93
+ _resetState() {
94
+ this._queue = [];
95
+ this._count = 0;
96
+ this._results = [];
97
+ this._runPromise = null;
98
+ this._numRunning = 0;
99
+ this._deferred = null;
100
+ }
101
+ _done() {
102
+ if (this._deferred != null) {
103
+ if (this._error != null) {
104
+ this._deferred.reject(this._error);
105
+ } else {
106
+ this._deferred.resolve(this._results);
107
+ }
108
+ }
109
+ this._resetState();
110
+ }
111
+ }
112
+ exports.default = PromiseQueue;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _stream() {
8
+ const data = require("stream");
9
+ _stream = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ /*
15
+ * "Taps" into the contents of a flowing stream, yielding chunks to the passed
16
+ * callback. Continues to pass data chunks down the stream.
17
+ */
18
+ class TapStream extends _stream().Transform {
19
+ constructor(tap, options) {
20
+ super({
21
+ ...options
22
+ });
23
+ this._tap = tap;
24
+ }
25
+ _transform(chunk, encoding, callback) {
26
+ try {
27
+ this._tap(Buffer.from(chunk));
28
+ callback(null, chunk);
29
+ } catch (err) {
30
+ callback(err);
31
+ }
32
+ }
33
+ }
34
+ exports.default = TapStream;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findAlternativeFiles = findAlternativeFiles;
7
+ exports.findAlternativeNodeModules = findAlternativeNodeModules;
8
+ function _path() {
9
+ const data = _interopRequireDefault(require("path"));
10
+ _path = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ var _schema = require("./schema");
16
+ var _path2 = require("./path");
17
+ var _config = require("./config");
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+ async function findAlternativeNodeModules(fs, moduleName, dir) {
20
+ let potentialModules = [];
21
+ let root = _path().default.parse(dir).root;
22
+ let isOrganisationModule = moduleName.startsWith('@');
23
+ while (dir !== root) {
24
+ // Skip node_modules directories
25
+ if (_path().default.basename(dir) === 'node_modules') {
26
+ dir = _path().default.dirname(dir);
27
+ }
28
+ try {
29
+ let modulesDir = _path().default.join(dir, 'node_modules');
30
+ let stats = await fs.stat(modulesDir);
31
+ if (stats.isDirectory()) {
32
+ let dirContent = (await fs.readdir(modulesDir)).sort();
33
+
34
+ // Filter out the modules that interest us
35
+ let modules = dirContent.filter(i => isOrganisationModule ? i.startsWith('@') : !i.startsWith('@'));
36
+
37
+ // If it's an organisation module, loop through all the modules of that organisation
38
+ if (isOrganisationModule) {
39
+ await Promise.all(modules.map(async item => {
40
+ let orgDirPath = _path().default.join(modulesDir, item);
41
+ let orgDirContent = (await fs.readdir(orgDirPath)).sort();
42
+
43
+ // Add all org packages
44
+ potentialModules.push(...orgDirContent.map(i => `${item}/${i}`));
45
+ }));
46
+ } else {
47
+ potentialModules.push(...modules);
48
+ }
49
+ }
50
+ } catch (err) {
51
+ // ignore
52
+ }
53
+
54
+ // Move up a directory
55
+ dir = _path().default.dirname(dir);
56
+ }
57
+ return (0, _schema.fuzzySearch)(potentialModules.sort(), moduleName).slice(0, 2);
58
+ }
59
+ async function findAllFilesUp({
60
+ fs,
61
+ dir,
62
+ root,
63
+ basedir,
64
+ maxlength,
65
+ collected,
66
+ leadingDotSlash = true,
67
+ includeDirectories = true
68
+ }) {
69
+ let dirContent = (await fs.readdir(dir)).sort();
70
+ return Promise.all(dirContent.map(async item => {
71
+ let fullPath = _path().default.join(dir, item);
72
+ let relativeFilePath = (0, _path2.relativePath)(basedir, fullPath, leadingDotSlash);
73
+ if (relativeFilePath.length < maxlength) {
74
+ let stats = await fs.stat(fullPath);
75
+ let isDir = stats.isDirectory();
76
+ if (isDir && includeDirectories || stats.isFile()) {
77
+ collected.push(relativeFilePath);
78
+ }
79
+
80
+ // If it's a directory, run over each item within said directory...
81
+ if (isDir) {
82
+ return findAllFilesUp({
83
+ fs,
84
+ dir: fullPath,
85
+ root,
86
+ basedir,
87
+ maxlength,
88
+ collected
89
+ });
90
+ }
91
+ }
92
+ }));
93
+ }
94
+ async function findAlternativeFiles(fs, fileSpecifier, dir, projectRoot, leadingDotSlash = true, includeDirectories = true, includeExtension = false) {
95
+ let potentialFiles = [];
96
+ // Find our root, we won't recommend files above the package root as that's bad practise
97
+ let pkg = await (0, _config.resolveConfig)(fs, _path().default.join(dir, 'index'), ['package.json'], projectRoot);
98
+ let pkgRoot = pkg ? _path().default.dirname(pkg) : projectRoot;
99
+ await findAllFilesUp({
100
+ fs,
101
+ dir: pkgRoot,
102
+ root: pkgRoot,
103
+ basedir: dir,
104
+ maxlength: fileSpecifier.length + 10,
105
+ collected: potentialFiles,
106
+ leadingDotSlash,
107
+ includeDirectories
108
+ });
109
+ if (_path().default.extname(fileSpecifier) === '' && !includeExtension) {
110
+ potentialFiles = potentialFiles.map(p => {
111
+ let ext = _path().default.extname(p);
112
+ return ext.length > 0 ? p.slice(0, -ext.length) : p;
113
+ });
114
+ }
115
+ return (0, _schema.fuzzySearch)(potentialFiles.sort(), fileSpecifier).slice(0, 2);
116
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ansiHtml = ansiHtml;
7
+ function _ansiHtmlCommunity() {
8
+ const data = _interopRequireDefault(require("ansi-html-community"));
9
+ _ansiHtmlCommunity = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ var _escapeHtml = require("./escape-html");
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+ // @ts-expect-error ansi-html-community is not typed
17
+
18
+ function ansiHtml(ansi) {
19
+ return (0, _ansiHtmlCommunity().default)((0, _escapeHtml.escapeHTML)(ansi));
20
+ }
package/lib/blob.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.blobToBuffer = blobToBuffer;
7
+ exports.blobToString = blobToString;
8
+ function _buffer() {
9
+ const data = require("buffer");
10
+ _buffer = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ var _ = require("./");
16
+ function _stream() {
17
+ const data = require("stream");
18
+ _stream = function () {
19
+ return data;
20
+ };
21
+ return data;
22
+ }
23
+ function blobToBuffer(blob) {
24
+ if (blob instanceof _stream().Readable) {
25
+ return (0, _.bufferStream)(blob);
26
+ } else if (blob instanceof _buffer().Buffer) {
27
+ return Promise.resolve(_buffer().Buffer.from(blob));
28
+ } else {
29
+ return Promise.resolve(_buffer().Buffer.from(blob, 'utf8'));
30
+ }
31
+ }
32
+ async function blobToString(blob) {
33
+ if (blob instanceof _stream().Readable) {
34
+ return (await (0, _.bufferStream)(blob)).toString();
35
+ } else if (blob instanceof _buffer().Buffer) {
36
+ return blob.toString();
37
+ } else {
38
+ return blob;
39
+ }
40
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getBaseURL = getBaseURL;
7
+ exports.getBundleURL = void 0;
8
+ let bundleURL = null;
9
+ function getBundleURLCached() {
10
+ if (bundleURL == null) {
11
+ bundleURL = _getBundleURL();
12
+ }
13
+ return bundleURL;
14
+ }
15
+ function _getBundleURL() {
16
+ // Attempt to find the URL of the current script and use that as the base URL
17
+ try {
18
+ throw new Error();
19
+ } catch (err) {
20
+ let stack = typeof err.stack === 'string' ? err.stack : '';
21
+ let matches = stack.match(/(https?|file|ftp):\/\/[^)\n]+/g);
22
+ if (matches) {
23
+ return getBaseURL(matches[0]);
24
+ }
25
+ }
26
+ return '/';
27
+ }
28
+ function getBaseURL(url) {
29
+ if (url == null) {
30
+ return '/';
31
+ }
32
+ return url.replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
33
+ }
34
+ const getBundleURL = exports.getBundleURL = getBundleURLCached;