@atlaspack/utils 2.14.5-canary.18 → 2.14.5-canary.181

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 (147) hide show
  1. package/CHANGELOG.md +238 -0
  2. package/benchmark.js +23 -0
  3. package/lib/DefaultMap.js +42 -0
  4. package/lib/Deferred.js +30 -0
  5. package/lib/PromiseQueue.js +112 -0
  6. package/lib/TapStream.js +34 -0
  7. package/lib/alternatives.js +116 -0
  8. package/lib/ansi-html.js +20 -0
  9. package/lib/blob.js +40 -0
  10. package/lib/bundle-url.js +34 -0
  11. package/lib/collection.js +111 -0
  12. package/lib/config.js +174 -0
  13. package/lib/countLines.js +15 -0
  14. package/lib/debounce.js +18 -0
  15. package/lib/debug-tools.js +38 -0
  16. package/lib/dependency-location.js +21 -0
  17. package/lib/escape-html.js +22 -0
  18. package/lib/generateBuildMetrics.js +121 -0
  19. package/lib/generateCertificate.js +129 -0
  20. package/lib/getCertificate.js +18 -0
  21. package/lib/getExisting.js +25 -0
  22. package/lib/getModuleParts.js +30 -0
  23. package/lib/getRootDir.js +52 -0
  24. package/lib/glob.js +118 -0
  25. package/lib/hash.js +50 -0
  26. package/lib/http-server.js +85 -0
  27. package/lib/index.js +635 -37310
  28. package/lib/is-url.js +24 -0
  29. package/lib/isDirectoryInside.js +18 -0
  30. package/lib/objectHash.js +26 -0
  31. package/lib/openInBrowser.js +74 -0
  32. package/lib/parseCSSImport.js +15 -0
  33. package/lib/path.js +39 -0
  34. package/lib/prettifyTime.js +9 -0
  35. package/lib/prettyDiagnostic.js +134 -0
  36. package/lib/progress-message.js +39 -0
  37. package/lib/relativeBundlePath.js +22 -0
  38. package/lib/relativeUrl.js +24 -0
  39. package/lib/replaceBundleReferences.js +199 -0
  40. package/lib/schema.js +355 -0
  41. package/lib/shared-buffer.js +31 -0
  42. package/lib/sourcemap.js +127 -0
  43. package/lib/stream.js +76 -0
  44. package/lib/throttle.js +15 -0
  45. package/lib/types/DefaultMap.d.ts +13 -0
  46. package/lib/types/Deferred.d.ts +8 -0
  47. package/lib/types/PromiseQueue.d.ts +25 -0
  48. package/lib/types/TapStream.d.ts +6 -0
  49. package/lib/types/alternatives.d.ts +3 -0
  50. package/lib/types/ansi-html.d.ts +1 -0
  51. package/lib/types/blob.d.ts +4 -0
  52. package/lib/types/bundle-url.d.ts +4 -0
  53. package/lib/types/collection.d.ts +33 -0
  54. package/lib/types/config.d.ts +17 -0
  55. package/lib/types/countLines.d.ts +1 -0
  56. package/lib/types/debounce.d.ts +1 -0
  57. package/lib/types/debug-tools.d.ts +7 -0
  58. package/lib/types/dependency-location.d.ts +14 -0
  59. package/lib/types/escape-html.d.ts +1 -0
  60. package/lib/types/generateBuildMetrics.d.ts +18 -0
  61. package/lib/types/generateCertificate.d.ts +5 -0
  62. package/lib/types/getCertificate.d.ts +5 -0
  63. package/lib/types/getExisting.d.ts +8 -0
  64. package/lib/types/getModuleParts.d.ts +4 -0
  65. package/lib/types/getRootDir.d.ts +2 -0
  66. package/lib/types/glob.d.ts +10 -0
  67. package/lib/types/hash.d.ts +7 -0
  68. package/lib/types/http-server.d.ts +19 -0
  69. package/lib/types/index.d.ts +48 -0
  70. package/lib/types/is-url.d.ts +1 -0
  71. package/lib/types/isDirectoryInside.d.ts +2 -0
  72. package/lib/types/objectHash.d.ts +3 -0
  73. package/lib/types/openInBrowser.d.ts +1 -0
  74. package/lib/types/parseCSSImport.d.ts +1 -0
  75. package/lib/types/path.d.ts +8 -0
  76. package/lib/types/prettifyTime.d.ts +1 -0
  77. package/lib/types/prettyDiagnostic.d.ts +17 -0
  78. package/lib/types/progress-message.d.ts +3 -0
  79. package/lib/types/relativeBundlePath.d.ts +4 -0
  80. package/lib/types/relativeUrl.d.ts +1 -0
  81. package/lib/types/replaceBundleReferences.d.ts +39 -0
  82. package/lib/types/schema.d.ts +107 -0
  83. package/lib/types/shared-buffer.d.ts +2 -0
  84. package/lib/types/sourcemap.d.ts +15 -0
  85. package/lib/types/stream.d.ts +8 -0
  86. package/lib/types/throttle.d.ts +1 -0
  87. package/lib/types/urlJoin.d.ts +5 -0
  88. package/lib/urlJoin.js +35 -0
  89. package/package.json +25 -16
  90. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  91. package/src/Deferred.ts +26 -0
  92. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  93. package/src/{TapStream.js → TapStream.ts} +8 -7
  94. package/src/{alternatives.js → alternatives.ts} +15 -16
  95. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  96. package/src/{blob.js → blob.ts} +3 -5
  97. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  98. package/src/{collection.js → collection.ts} +23 -20
  99. package/src/{config.js → config.ts} +20 -21
  100. package/src/{countLines.js → countLines.ts} +0 -2
  101. package/src/{debounce.js → debounce.ts} +3 -5
  102. package/src/debug-tools.ts +45 -0
  103. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  104. package/src/{escape-html.js → escape-html.ts} +5 -3
  105. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +18 -20
  106. package/src/{generateCertificate.js → generateCertificate.ts} +8 -6
  107. package/src/{getCertificate.js → getCertificate.ts} +6 -5
  108. package/src/{getExisting.js → getExisting.ts} +4 -3
  109. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  110. package/src/{getRootDir.js → getRootDir.ts} +1 -3
  111. package/src/{glob.js → glob.ts} +16 -12
  112. package/src/{hash.js → hash.ts} +23 -18
  113. package/src/{http-server.js → http-server.ts} +34 -40
  114. package/src/{index.js → index.ts} +13 -9
  115. package/src/{is-url.js → is-url.ts} +1 -2
  116. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +1 -2
  117. package/src/{objectHash.js → objectHash.ts} +1 -4
  118. package/src/{openInBrowser.js → openInBrowser.ts} +2 -4
  119. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  120. package/src/{path.js → path.ts} +2 -4
  121. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  122. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +23 -21
  123. package/src/progress-message.ts +43 -0
  124. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +4 -4
  125. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  126. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +63 -39
  127. package/src/{schema.js → schema.ts} +158 -141
  128. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  129. package/src/{sourcemap.js → sourcemap.ts} +17 -8
  130. package/src/{stream.js → stream.ts} +30 -22
  131. package/src/throttle.ts +13 -0
  132. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  133. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  134. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  135. package/test/{collection.test.js → collection.test.ts} +0 -2
  136. package/test/{config.test.js → config.test.ts} +0 -3
  137. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  138. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  139. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  140. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  141. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  142. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  143. package/tsconfig.json +4 -0
  144. package/lib/index.js.map +0 -1
  145. package/src/Deferred.js +0 -23
  146. package/src/progress-message.js +0 -22
  147. package/src/throttle.js +0 -15
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getExisting;
7
+ function _fs() {
8
+ const data = _interopRequireDefault(require("fs"));
9
+ _fs = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ /**
16
+ * Creates an object that contains both source and minified (using the source as a fallback).
17
+ * e.g. builtins.min.js and builtins.js.
18
+ */
19
+ function getExisting(minifiedPath, sourcePath) {
20
+ let source = _fs().default.readFileSync(sourcePath, 'utf8').trim();
21
+ return {
22
+ source,
23
+ minified: _fs().default.existsSync(minifiedPath) ? _fs().default.readFileSync(minifiedPath, 'utf8').trim().replace(/;$/, '') : source
24
+ };
25
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getModuleParts;
7
+ function _path() {
8
+ const data = _interopRequireDefault(require("path"));
9
+ _path = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ var _path2 = require("./path");
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+ /**
17
+ * Returns the package name and the optional subpath
18
+ */
19
+ function getModuleParts(_name) {
20
+ let name = _path().default.normalize(_name);
21
+ let splitOn = name.indexOf(_path().default.sep);
22
+ if (name.charAt(0) === '@') {
23
+ splitOn = name.indexOf(_path().default.sep, splitOn + 1);
24
+ }
25
+ if (splitOn < 0) {
26
+ return [(0, _path2.normalizeSeparators)(name), undefined];
27
+ } else {
28
+ return [(0, _path2.normalizeSeparators)(name.substring(0, splitOn)), name.substring(splitOn + 1) || undefined];
29
+ }
30
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getRootDir;
7
+ var _glob = require("./glob");
8
+ function _path() {
9
+ const data = _interopRequireDefault(require("path"));
10
+ _path = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+ function getRootDir(files) {
17
+ let cur = null;
18
+ for (let file of files) {
19
+ let parsed = _path().default.parse(file);
20
+ parsed.dir = findGlobRoot(parsed.dir);
21
+ if (!cur) {
22
+ cur = parsed;
23
+ } else if (parsed.root !== cur.root) {
24
+ // bail out. there is no common root.
25
+ // this can happen on windows, e.g. C:\foo\bar vs. D:\foo\bar
26
+ return process.cwd();
27
+ } else {
28
+ // find the common path parts.
29
+ let curParts = cur.dir.split(_path().default.sep);
30
+ let newParts = parsed.dir.split(_path().default.sep);
31
+ let len = Math.min(curParts.length, newParts.length);
32
+ let i = 0;
33
+ while (i < len && curParts[i] === newParts[i]) {
34
+ i++;
35
+ }
36
+ cur.dir = i > 1 ? curParts.slice(0, i).join(_path().default.sep) : cur.root;
37
+ }
38
+ }
39
+ return cur ? cur.dir : process.cwd();
40
+ }
41
+
42
+ // Transforms a path like `packages/*/src/index.js` to the root of the glob, `packages/`
43
+ function findGlobRoot(dir) {
44
+ let parts = dir.split(_path().default.sep);
45
+ let last = parts.length;
46
+ for (let i = parts.length - 1; i >= 0; i--) {
47
+ if ((0, _glob.isGlob)(parts[i])) {
48
+ last = i;
49
+ }
50
+ }
51
+ return parts.slice(0, last).join(_path().default.sep);
52
+ }
package/lib/glob.js ADDED
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.glob = glob;
7
+ exports.globMatch = globMatch;
8
+ exports.globSync = globSync;
9
+ exports.globToRegex = globToRegex;
10
+ exports.isGlob = isGlob;
11
+ exports.isGlobMatch = isGlobMatch;
12
+ function _isGlob2() {
13
+ const data = _interopRequireDefault(require("is-glob"));
14
+ _isGlob2 = function () {
15
+ return data;
16
+ };
17
+ return data;
18
+ }
19
+ function _fastGlob() {
20
+ const data = _interopRequireDefault(require("fast-glob"));
21
+ _fastGlob = function () {
22
+ return data;
23
+ };
24
+ return data;
25
+ }
26
+ function _micromatch() {
27
+ const data = _interopRequireWildcard(require("micromatch"));
28
+ _micromatch = function () {
29
+ return data;
30
+ };
31
+ return data;
32
+ }
33
+ var _path = require("./path");
34
+ 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); }
35
+ 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; }
36
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
+ // @ts-expect-error TS7016
38
+
39
+ // @ts-expect-error TS2305
40
+
41
+ function isGlob(p) {
42
+ return (0, _isGlob2().default)((0, _path.normalizeSeparators)(p));
43
+ }
44
+ function isGlobMatch(filePath, glob, opts) {
45
+ glob = Array.isArray(glob) ? glob.map(_path.normalizeSeparators) : (0, _path.normalizeSeparators)(glob);
46
+ return (0, _micromatch().isMatch)(filePath, glob, opts);
47
+ }
48
+ function globMatch(values, glob, opts) {
49
+ glob = Array.isArray(glob) ? glob.map(_path.normalizeSeparators) : (0, _path.normalizeSeparators)(glob);
50
+ return (0, _micromatch().default)(values, glob, opts);
51
+ }
52
+ function globToRegex(glob, opts) {
53
+ return (0, _micromatch().makeRe)(glob, opts);
54
+ }
55
+ function globSync(p, fs, options) {
56
+ options = {
57
+ ...options,
58
+ fs: {
59
+ // @ts-expect-error TS7006
60
+ statSync: p => {
61
+ return fs.statSync(p);
62
+ },
63
+ // @ts-expect-error TS7006
64
+ lstatSync: p => {
65
+ // Our FileSystem interface doesn't have lstat support at the moment,
66
+ // but this is fine for our purposes since we follow symlinks by default.
67
+ return fs.statSync(p);
68
+ },
69
+ // @ts-expect-error TS7006
70
+ readdirSync: (p, opts) => {
71
+ return fs.readdirSync(p, opts);
72
+ }
73
+ }
74
+ };
75
+
76
+ // @ts-expect-error TS2322
77
+ return _fastGlob().default.sync((0, _path.normalizeSeparators)(p), options);
78
+ }
79
+ function glob(p, fs, options) {
80
+ options = {
81
+ ...options,
82
+ fs: {
83
+ // @ts-expect-error TS7006
84
+ stat: async (p, cb) => {
85
+ try {
86
+ cb(null, await fs.stat(p));
87
+ } catch (err) {
88
+ cb(err);
89
+ }
90
+ },
91
+ // @ts-expect-error TS7006
92
+ lstat: async (p, cb) => {
93
+ // Our FileSystem interface doesn't have lstat support at the moment,
94
+ // but this is fine for our purposes since we follow symlinks by default.
95
+ try {
96
+ cb(null, await fs.stat(p));
97
+ } catch (err) {
98
+ cb(err);
99
+ }
100
+ },
101
+ // @ts-expect-error TS7006
102
+ readdir: async (p, opts, cb) => {
103
+ if (typeof opts === 'function') {
104
+ cb = opts;
105
+ opts = null;
106
+ }
107
+ try {
108
+ cb(null, await fs.readdir(p, opts));
109
+ } catch (err) {
110
+ cb(err);
111
+ }
112
+ }
113
+ }
114
+ };
115
+
116
+ // @ts-expect-error TS2322
117
+ return (0, _fastGlob().default)((0, _path.normalizeSeparators)(p), options);
118
+ }
package/lib/hash.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hashFile = hashFile;
7
+ exports.hashObject = hashObject;
8
+ exports.hashStream = hashStream;
9
+ var _collection = require("./collection");
10
+ function _rust() {
11
+ const data = require("@atlaspack/rust");
12
+ _rust = function () {
13
+ return data;
14
+ };
15
+ return data;
16
+ }
17
+ function hashStream(stream) {
18
+ let hash = new (_rust().Hash)();
19
+ return new Promise((resolve, reject) => {
20
+ stream.on('error', err => {
21
+ reject(err);
22
+ });
23
+ stream.on('data', chunk => {
24
+ hash.writeBuffer(chunk);
25
+ }).on('end', function () {
26
+ resolve(hash.finish());
27
+ }).on('error', err => {
28
+ reject(err);
29
+ });
30
+ });
31
+ }
32
+ function hashObject(obj) {
33
+ return (0, _rust().hashString)(JSON.stringify((0, _collection.objectSortedEntriesDeep)(obj)));
34
+ }
35
+ let testCache = {
36
+ /*:: ...null */
37
+ };
38
+ function hashFile(fs, filePath) {
39
+ if (process.env.ATLASPACK_BUILD_ENV === 'test') {
40
+ // Development builds of these native modules are especially big and slow to hash.
41
+ if (/parcel-swc\.[^\\/]+\.node$|lightningcss.[^\\/]+.node$/.test(filePath)) {
42
+ let cacheEntry = testCache[filePath];
43
+ if (cacheEntry) return cacheEntry;
44
+ let v = hashStream(fs.createReadStream(filePath));
45
+ testCache[filePath] = v;
46
+ return v;
47
+ }
48
+ }
49
+ return hashStream(fs.createReadStream(filePath));
50
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createHTTPServer = createHTTPServer;
7
+ function _http() {
8
+ const data = _interopRequireDefault(require("http"));
9
+ _http = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _https() {
15
+ const data = _interopRequireDefault(require("https"));
16
+ _https = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _nullthrows() {
22
+ const data = _interopRequireDefault(require("nullthrows"));
23
+ _nullthrows = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ var _ = require("./");
29
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
+ // Creates either an http or https server with an awaitable dispose
31
+ // that closes any connections
32
+ async function createHTTPServer(options) {
33
+ let server;
34
+ if (!options.https) {
35
+ server = _http().default.createServer(options.listener);
36
+ } else if (options.https === true) {
37
+ let {
38
+ cert,
39
+ key
40
+ } = await (0, _.generateCertificate)(options.outputFS, options.cacheDir, options.host);
41
+ server = _https().default.createServer({
42
+ cert,
43
+ key
44
+ }, options.listener);
45
+ } else {
46
+ let {
47
+ cert,
48
+ key
49
+ } = await (0, _.getCertificate)(options.inputFS, options.https);
50
+ server = _https().default.createServer({
51
+ cert,
52
+ key
53
+ }, options.listener);
54
+ }
55
+
56
+ // HTTPServer#close only stops accepting new connections, and does not close existing ones.
57
+ // Before closing, destroy any active connections through their sockets. Additionally, remove sockets when they close:
58
+ // https://stackoverflow.com/questions/18874689/force-close-all-connections-in-a-node-js-http-server
59
+ // https://stackoverflow.com/questions/14626636/how-do-i-shutdown-a-node-js-https-server-immediately/14636625#14636625
60
+ let sockets = new Set();
61
+ server.on('connection', socket => {
62
+ (0, _nullthrows().default)(sockets).add(socket);
63
+ socket.on('close', () => {
64
+ (0, _nullthrows().default)(sockets).delete(socket);
65
+ });
66
+ });
67
+ return {
68
+ server,
69
+ stop() {
70
+ return new Promise((resolve, reject) => {
71
+ for (let socket of (0, _nullthrows().default)(sockets)) {
72
+ socket.destroy();
73
+ }
74
+ sockets = new Set();
75
+ server.close(err => {
76
+ if (err != null) {
77
+ reject(err);
78
+ return;
79
+ }
80
+ resolve();
81
+ });
82
+ });
83
+ }
84
+ };
85
+ }