tar 8.0.0 → 8.0.2

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 (36) hide show
  1. package/README.md +8 -1
  2. package/dist/commonjs/header.d.ts.map +1 -1
  3. package/dist/commonjs/header.js +7 -7
  4. package/dist/commonjs/header.js.map +1 -1
  5. package/dist/commonjs/index.min.js +2 -2
  6. package/dist/commonjs/index.min.js.map +3 -3
  7. package/dist/commonjs/normalize-unicode.d.ts.map +1 -1
  8. package/dist/commonjs/normalize-unicode.js +9 -0
  9. package/dist/commonjs/normalize-unicode.js.map +1 -1
  10. package/dist/commonjs/path-reservations.d.ts.map +1 -1
  11. package/dist/commonjs/path-reservations.js +47 -54
  12. package/dist/commonjs/path-reservations.js.map +1 -1
  13. package/dist/commonjs/server/index.min.js +2 -2
  14. package/dist/commonjs/server/index.min.js.map +3 -3
  15. package/dist/commonjs/unpack.d.ts +8 -0
  16. package/dist/commonjs/unpack.d.ts.map +1 -1
  17. package/dist/commonjs/unpack.js +38 -14
  18. package/dist/commonjs/unpack.js.map +1 -1
  19. package/dist/esm/header.d.ts.map +1 -1
  20. package/dist/esm/header.js +7 -7
  21. package/dist/esm/header.js.map +1 -1
  22. package/dist/esm/index.min.js +2 -2
  23. package/dist/esm/index.min.js.map +3 -3
  24. package/dist/esm/normalize-unicode.d.ts.map +1 -1
  25. package/dist/esm/normalize-unicode.js +9 -0
  26. package/dist/esm/normalize-unicode.js.map +1 -1
  27. package/dist/esm/path-reservations.d.ts.map +1 -1
  28. package/dist/esm/path-reservations.js +47 -54
  29. package/dist/esm/path-reservations.js.map +1 -1
  30. package/dist/esm/server/index.min.js +2 -2
  31. package/dist/esm/server/index.min.js.map +3 -3
  32. package/dist/esm/unpack.d.ts +8 -0
  33. package/dist/esm/unpack.d.ts.map +1 -1
  34. package/dist/esm/unpack.js +38 -14
  35. package/dist/esm/unpack.js.map +1 -1
  36. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"normalize-unicode.d.ts","sourceRoot":"","sources":["../../src/normalize-unicode.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,gBAAgB,GAAI,GAAG,MAAM,KAAG,MAyB5C,CAAA"}
1
+ {"version":3,"file":"normalize-unicode.d.ts","sourceRoot":"","sources":["../../src/normalize-unicode.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,gBAAgB,GAAI,GAAG,MAAM,KAAG,MA+B5C,CAAA"}
@@ -9,7 +9,16 @@ const normalizeCache = Object.create(null);
9
9
  // Limit the size of this. Very low-sophistication LRU cache
10
10
  const MAX = 10000;
11
11
  const cache = new Set();
12
+ // A longer string is not cached. Long paths do not repeat, and the
13
+ // cache would keep them for the life of the process.
14
+ const MAX_LENGTH = 256;
12
15
  const normalizeUnicode = (s) => {
16
+ if (s.length > MAX_LENGTH) {
17
+ return s
18
+ .normalize('NFD')
19
+ .toLocaleLowerCase('en')
20
+ .toLocaleUpperCase('en');
21
+ }
13
22
  if (!cache.has(s)) {
14
23
  // shake out identical accents and ligatures
15
24
  normalizeCache[s] = s
@@ -1 +1 @@
1
- {"version":3,"file":"normalize-unicode.js","sourceRoot":"","sources":["../../src/normalize-unicode.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AACpC,+CAA+C;AAC/C,6CAA6C;AAC7C,4CAA4C;AAC5C,MAAM,cAAc,GAA2B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AAElE,4DAA4D;AAC5D,MAAM,GAAG,GAAG,KAAK,CAAA;AACjB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAA;AACxB,MAAM,gBAAgB,GAAG,CAAC,CAAS,EAAU,EAAE;IACpD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAClB,4CAA4C;QAC5C,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC;aAClB,SAAS,CAAC,KAAK,CAAC;aAChB,iBAAiB,CAAC,IAAI,CAAC;aACvB,iBAAiB,CAAC,IAAI,CAAC,CAAA;IAC5B,CAAC;SAAM,CAAC;QACN,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IACjB,CAAC;IACD,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;IAEZ,MAAM,GAAG,GAAG,cAAc,CAAC,CAAC,CAAW,CAAA;IAEvC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,GAAG,GAAG,CAAA;IACxB,yCAAyC;IACzC,IAAI,CAAC,GAAG,GAAG,GAAG,EAAE,EAAE,CAAC;QACjB,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;YACf,OAAO,cAAc,CAAC,CAAC,CAAC,CAAA;YACxB,IAAI,EAAE,CAAC,IAAI,CAAC;gBAAE,MAAK;QACrB,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAzBY,QAAA,gBAAgB,oBAyB5B","sourcesContent":["// warning: extremely hot code path.\n// This has been meticulously optimized for use\n// within npm install on large package trees.\n// Do not edit without careful benchmarking.\nconst normalizeCache: Record<string, string> = Object.create(null)\n\n// Limit the size of this. Very low-sophistication LRU cache\nconst MAX = 10000\nconst cache = new Set<string>()\nexport const normalizeUnicode = (s: string): string => {\n if (!cache.has(s)) {\n // shake out identical accents and ligatures\n normalizeCache[s] = s\n .normalize('NFD')\n .toLocaleLowerCase('en')\n .toLocaleUpperCase('en')\n } else {\n cache.delete(s)\n }\n cache.add(s)\n\n const ret = normalizeCache[s] as string\n\n let i = cache.size - MAX\n // only prune when we're 10% over the max\n if (i > MAX / 10) {\n for (const s of cache) {\n cache.delete(s)\n delete normalizeCache[s]\n if (--i <= 0) break\n }\n }\n\n return ret\n}\n"]}
1
+ {"version":3,"file":"normalize-unicode.js","sourceRoot":"","sources":["../../src/normalize-unicode.ts"],"names":[],"mappings":";;;AAAA,oCAAoC;AACpC,+CAA+C;A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,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AA/BY,QAAA,gBAAgB,oBA+B5B","sourcesContent":["// warning: extremely hot code path.\n// This has been meticulously optimized for use\n// within npm install on large package trees.\n// Do not edit without careful benchmarking.\nconst normalizeCache: Record<string, string> = Object.create(null)\n\n// Limit the size of this. Very low-sophistication LRU cache\nconst MAX = 10000\nconst cache = new Set<string>()\n// A longer string is not cached. Long paths do not repeat, and the\n// cache would keep them for the life of the process.\nconst MAX_LENGTH = 256\nexport const normalizeUnicode = (s: string): string => {\n if (s.length > MAX_LENGTH) {\n return s\n .normalize('NFD')\n .toLocaleLowerCase('en')\n .toLocaleUpperCase('en')\n }\n if (!cache.has(s)) {\n // shake out identical accents and ligatures\n normalizeCache[s] = s\n .normalize('NFD')\n .toLocaleLowerCase('en')\n .toLocaleUpperCase('en')\n } else {\n cache.delete(s)\n }\n cache.add(s)\n\n const ret = normalizeCache[s] as string\n\n let i = cache.size - MAX\n // only prune when we're 10% over the max\n if (i > MAX / 10) {\n for (const s of cache) {\n cache.delete(s)\n delete normalizeCache[s]\n if (--i <= 0) break\n }\n }\n\n return ret\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"path-reservations.d.ts","sourceRoot":"","sources":["../../src/path-reservations.ts"],"names":[],"mappings":"AAsBA;;;;;;;;GAQG;AACH,qBAAa,gBAAgB;;IAS3B,0CAA0C;gBAC9B,KAAK,GAAE,OAAe;IAIlC;;;;OAIG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAgGjE"}
1
+ {"version":3,"file":"path-reservations.d.ts","sourceRoot":"","sources":["../../src/path-reservations.ts"],"names":[],"mappings":"AAsBA;;;;;;;;GAQG;AACH,qBAAa,gBAAgB;;IAM3B,0CAA0C;gBAC9B,KAAK,GAAE,OAAe;IAIlC;;;;OAIG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAqGjE"}
@@ -7,15 +7,6 @@ exports.PathReservations = void 0;
7
7
  const normalize_unicode_js_1 = require("./normalize-unicode.js");
8
8
  const strip_trailing_slashes_js_1 = require("./strip-trailing-slashes.js");
9
9
  const path_js_1 = require("./path.js");
10
- // The parent directories of a normalized path.
11
- // '/a/b/c' gives ['/', '/a', '/a/b'].
12
- const getDirs = (path) => {
13
- const dirs = [];
14
- for (let i = path.indexOf('/'); i !== -1; i = path.indexOf('/', i + 1)) {
15
- dirs.push(path.slice(0, i) || '/');
16
- }
17
- return dirs;
18
- };
19
10
  /**
20
11
  * Gives each task the exclusive use of its paths, and the shared use
21
12
  * of their parent directories. A task starts when each task before
@@ -26,10 +17,7 @@ const getDirs = (path) => {
26
17
  * path, because they can be the same file on disk.
27
18
  */
28
19
  class PathReservations {
29
- // For each path: the tasks that wait for it, in order. A function
30
- // has the path for itself. A Set of functions shares the path as a
31
- // parent directory.
32
- #queues = new Map();
20
+ #root = { queue: [], name: '' };
33
21
  #reservations = new Map();
34
22
  #running = new Set();
35
23
  #win32;
@@ -56,35 +44,39 @@ class PathReservations {
56
44
  this.#add(paths, handler);
57
45
  });
58
46
  }
59
- #add(paths, fn) {
60
- paths =
61
- this.#win32 ?
62
- ['win32 parallelization disabled']
63
- : [
64
- ...new Set(paths.map(p => (0, strip_trailing_slashes_js_1.stripTrailingSlashes)((0, path_js_1.join)((0, normalize_unicode_js_1.normalizeUnicode)(p))))),
65
- ];
66
- const dirs = new Set(paths.flatMap(getDirs));
47
+ #add(names, fn) {
48
+ const root = this.#root;
49
+ const paths = new Set();
50
+ const dirs = new Set();
51
+ for (const name of this.#win32 ?
52
+ ['win32 parallelization disabled']
53
+ : names) {
54
+ const path = this.#win32 ? name : ((0, strip_trailing_slashes_js_1.stripTrailingSlashes)((0, path_js_1.join)((0, normalize_unicode_js_1.normalizeUnicode)(name))));
55
+ let node = root;
56
+ for (const part of path.split('/')) {
57
+ if (node !== root)
58
+ dirs.add(node);
59
+ const kids = (node.kids ??= new Map());
60
+ let kid = kids.get(part);
61
+ if (!kid)
62
+ kids.set(part, (kid = { queue: [], name: part, up: node }));
63
+ node = kid;
64
+ }
65
+ paths.add(node);
66
+ }
67
67
  // A path of the task is exclusive, so it also covers the use of
68
68
  // that path as a parent directory of another path.
69
69
  for (const p of paths)
70
70
  dirs.delete(p);
71
71
  this.#reservations.set(fn, { dirs, paths });
72
- for (const p of paths) {
73
- const q = this.#queues.get(p);
74
- if (!q)
75
- this.#queues.set(p, [fn]);
76
- else
77
- q.push(fn);
78
- }
79
- for (const dir of dirs) {
80
- const q = this.#queues.get(dir);
81
- const last = q?.[q.length - 1];
82
- if (!q)
83
- this.#queues.set(dir, [new Set([fn])]);
84
- else if (last instanceof Set)
72
+ for (const p of paths)
73
+ p.queue.push(fn);
74
+ for (const { queue } of dirs) {
75
+ const last = queue[queue.length - 1];
76
+ if (last instanceof Set)
85
77
  last.add(fn);
86
78
  else
87
- q.push(new Set([fn]));
79
+ queue.push(new Set([fn]));
88
80
  }
89
81
  this.#run(fn);
90
82
  }
@@ -94,11 +86,11 @@ class PathReservations {
94
86
  #run(fn) {
95
87
  const { paths, dirs } = this.#reservations.get(fn);
96
88
  for (const p of paths) {
97
- if (this.#queues.get(p)?.[0] !== fn)
89
+ if (p.queue[0] !== fn)
98
90
  return;
99
91
  }
100
- for (const dir of dirs) {
101
- const first = this.#queues.get(dir)?.[0];
92
+ for (const { queue } of dirs) {
93
+ const first = queue[0];
102
94
  if (!(first instanceof Set) || !first.has(fn))
103
95
  return;
104
96
  }
@@ -108,33 +100,34 @@ class PathReservations {
108
100
  #clear(fn) {
109
101
  const { paths, dirs } = this.#reservations.get(fn);
110
102
  const next = new Set();
111
- for (const path of paths) {
103
+ for (const { queue } of paths) {
112
104
  // A running task is first in the queue of each of its paths.
113
- const q = this.#queues.get(path);
114
- const after = q[1];
115
- if (!after) {
116
- this.#queues.delete(path);
117
- continue;
118
- }
119
- q.shift();
105
+ queue.shift();
106
+ const after = queue[0];
120
107
  if (typeof after === 'function')
121
108
  next.add(after);
122
- else
109
+ else if (after)
123
110
  for (const f of after)
124
111
  next.add(f);
125
112
  }
126
- for (const dir of dirs) {
113
+ for (const { queue } of dirs) {
127
114
  // A running task is in the first set of each of its directories.
128
- const q = this.#queues.get(dir);
129
- const first = q[0];
115
+ const first = queue[0];
130
116
  if (first.size > 1)
131
117
  first.delete(fn);
132
- else if (q.length === 1)
133
- this.#queues.delete(dir);
134
118
  else {
135
- q.shift();
119
+ queue.shift();
136
120
  // Two sets in a row would be one set, so this is a function.
137
- next.add(q[0]);
121
+ if (queue.length)
122
+ next.add(queue[0]);
123
+ }
124
+ }
125
+ // Remove each node that no task uses. Each directory of the task
126
+ // is above one of its paths.
127
+ for (let node of paths) {
128
+ while (node.up && !node.queue.length && !node.kids?.size) {
129
+ node.up.kids?.delete(node.name);
130
+ node = node.up;
138
131
  }
139
132
  }
140
133
  this.#running.delete(fn);
@@ -1 +1 @@
1
- {"version":3,"file":"path-reservations.js","sourceRoot":"","sources":["../../src/path-reservations.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,+DAA+D;AAC/D,kEAAkE;;;AAElE,iEAAyD;AACzD,2EAAkE;AAClE,uCAAgC;AAMhC,+CAA+C;AAC/C,sCAAsC;AACtC,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE;IAC/B,MAAM,IAAI,GAAa,EAAE,CAAA;IACzB,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACvE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,E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ACT,IAAI,OAAO,KAAK,KAAK,UAAU;gBAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;;gBAC3C,KAAK,MAAM,CAAC,IAAI,KAAK;oBAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QACzC,CAAC;QACD,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,iEAAiE;YACjE,MAAM,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAU,CAAA;YACxC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,CAAiB,CAAA;YAClC,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC;gBAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;iBAC/B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC;gBAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;iBAC5C,CAAC;gBACJ,CAAC,CAAC,KAAK,EAAE,CAAA;gBACT,6DAA6D;gBAC7D,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAY,CAAC,CAAA;YAC3B,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QACxB,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAC7B,KAAK,MAAM,CAAC,IAAI,IAAI;YAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACpC,CAAC;CACF;AAnHD,4CAmHC","sourcesContent":["// The async extraction does several entries at one time. Two entries\n// must not work on the same path at one time: a link needs its\n// target, and an entry can replace an object of a different type.\n\nimport { normalizeUnicode } from './normalize-unicode.js'\nimport { stripTrailingSlashes } from './strip-trailing-slashes.js'\nimport { join } from './path.js'\n\ntype Handler = () => void\ntype Queue = (Handler | Set<Handler>)[]\ntype Reservation = { paths: string[]; dirs: Set<string> }\n\n// The parent directories of a normalized path.\n// '/a/b/c' gives ['/', '/a', '/a/b'].\nconst getDirs = (path: string) => {\n const dirs: string[] = []\n for (let i = path.indexOf('/'); i !== -1; i = path.indexOf('/', i + 1)) {\n dirs.push(path.slice(0, i) || '/')\n }\n return dirs\n}\n\n/**\n * Gives each task the exclusive use of its paths, and the shared use\n * of their parent directories. A task starts when each task before\n * it that uses one of its paths, or a directory above or below one,\n * is done.\n *\n * Paths that differ only in case or in Unicode form are the same\n * path, because they can be the same file on disk.\n */\nexport class PathReservations {\n // For each path: the tasks that wait for it, in order. A function\n // has the path for itself. A Set of functions shares the path as a\n // parent directory.\n #queues = new Map<string, Queue>()\n #reservations = new Map<Handler, Reservation>()\n #running = new Set<Handler>()\n #win32: boolean\n\n /** @param win32 run one task at a time */\n constructor(win32: boolean = false) {\n this.#win32 = win32\n }\n\n /**\n * Call `fn` when it is first in line for each of its paths. The\n * paths are free again when the promise of `fn` settles. Returns a\n * promise with the result of `fn`.\n */\n reserve(paths: string[], fn: () => Promise<void>): Promise<void> {\n return new Promise<void>((res, rej) => {\n const handler: Handler = () => {\n fn().then(\n () => {\n this.#clear(handler)\n res()\n },\n er => {\n this.#clear(handler)\n rej(er)\n },\n )\n }\n this.#add(paths, handler)\n })\n }\n\n #add(paths: string[], fn: Handler) {\n paths =\n this.#win32 ?\n ['win32 parallelization disabled']\n : [\n ...new Set(\n paths.map(p =>\n stripTrailingSlashes(join(normalizeUnicode(p))),\n ),\n ),\n ]\n const dirs = new Set<string>(paths.flatMap(getDirs))\n // A path of the task is exclusive, so it also covers the use of\n // that path as a parent directory of another path.\n for (const p of paths) dirs.delete(p)\n this.#reservations.set(fn, { dirs, paths })\n for (const p of paths) {\n const q = this.#queues.get(p)\n if (!q) this.#queues.set(p, [fn])\n else q.push(fn)\n }\n for (const dir of dirs) {\n const q = this.#queues.get(dir)\n const last = q?.[q.length - 1]\n if (!q) this.#queues.set(dir, [new Set([fn])])\n else if (last instanceof Set) last.add(fn)\n else q.push(new Set([fn]))\n }\n this.#run(fn)\n }\n\n // Start the task if it is first in line for each path, and in the\n // first set for each directory. A task that waits is never first in\n // line for each of them, so a task starts one time only.\n #run(fn: Handler) {\n const { paths, dirs } = this.#reservations.get(fn) as Reservation\n for (const p of paths) {\n if (this.#queues.get(p)?.[0] !== fn) return\n }\n for (const dir of dirs) {\n const first = this.#queues.get(dir)?.[0]\n if (!(first instanceof Set) || !first.has(fn)) return\n }\n this.#running.add(fn)\n fn()\n }\n\n #clear(fn: Handler) {\n const { paths, dirs } = this.#reservations.get(fn) as Reservation\n const next = new Set<Handler>()\n for (const path of paths) {\n // A running task is first in the queue of each of its paths.\n const q = this.#queues.get(path) as Queue\n const after = q[1]\n if (!after) {\n this.#queues.delete(path)\n continue\n }\n q.shift()\n if (typeof after === 'function') next.add(after)\n else for (const f of after) next.add(f)\n }\n for (const dir of dirs) {\n // A running task is in the first set of each of its directories.\n const q = this.#queues.get(dir) as Queue\n const first = q[0] as Set<Handler>\n if (first.size > 1) first.delete(fn)\n else if (q.length === 1) this.#queues.delete(dir)\n else {\n q.shift()\n // Two sets in a row would be one set, so this is a function.\n next.add(q[0] as Handler)\n }\n }\n this.#running.delete(fn)\n this.#reservations.delete(fn)\n for (const f of next) this.#run(f)\n }\n}\n"]}
1
+ {"version":3,"file":"path-reservations.js","sourceRoot":"","sources":["../../src/path-reservations.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,+DAA+D;AAC/D,kEAAkE;;;AAElE,iEAAyD;AACzD,2EAAkE;AAClE,uCAAgC;AAgBhC;;;;;;;;GAQG;AACH,MAAa,gBAAgB;IAC3B,KAAK,GAAS,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAA;IACrC,aAAa,GAAG,IAAI,GAAG,EAAwB,CAAA;IAC/C,QAAQ,GAAG,IAAI,GAAG,EAAW,CAAA;IAC7B,MAAM,CAAS;IAEf,0CAA0C;IAC1C,YAAY,QAAiB,KAAK;QAChC,IAAI,CAAC,MAAM,G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AAC,IAAI,GAAG,CAAC;gBAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;iBAC/B,CAAC;gBACJ,KAAK,CAAC,KAAK,EAAE,CAAA;gBACb,6DAA6D;gBAC7D,IAAI,KAAK,CAAC,MAAM;oBAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAY,CAAC,CAAA;YACjD,CAAC;QACH,CAAC;QACD,iEAAiE;QACjE,6BAA6B;QAC7B,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE,CAAC;YACvB,OAAO,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC;gBACzD,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBAC/B,IAAI,GAAG,IAAI,CAAC,EAAE,CAAA;YAChB,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QACxB,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAC7B,KAAK,MAAM,CAAC,IAAI,IAAI;YAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACpC,CAAC;CACF;AArHD,4CAqHC","sourcesContent":["// The async extraction does several entries at one time. Two entries\n// must not work on the same path at one time: a link needs its\n// target, and an entry can replace an object of a different type.\n\nimport { normalizeUnicode } from './normalize-unicode.js'\nimport { stripTrailingSlashes } from './strip-trailing-slashes.js'\nimport { join } from './path.js'\n\ntype Handler = () => void\ntype Queue = (Handler | Set<Handler>)[]\n// One part of a path. `queue` has the tasks that wait for the path,\n// in order. A function has the path for itself. A Set of functions\n// shares the path as a parent directory. So a path uses memory for\n// its own parts only, and not for each of its parent paths.\ntype Node = {\n queue: Queue\n name: string\n up?: Node\n kids?: Map<string, Node>\n}\ntype Reservation = { paths: Set<Node>; dirs: Set<Node> }\n\n/**\n * Gives each task the exclusive use of its paths, and the shared use\n * of their parent directories. A task starts when each task before\n * it that uses one of its paths, or a directory above or below one,\n * is done.\n *\n * Paths that differ only in case or in Unicode form are the same\n * path, because they can be the same file on disk.\n */\nexport class PathReservations {\n #root: Node = { queue: [], name: '' }\n #reservations = new Map<Handler, Reservation>()\n #running = new Set<Handler>()\n #win32: boolean\n\n /** @param win32 run one task at a time */\n constructor(win32: boolean = false) {\n this.#win32 = win32\n }\n\n /**\n * Call `fn` when it is first in line for each of its paths. The\n * paths are free again when the promise of `fn` settles. Returns a\n * promise with the result of `fn`.\n */\n reserve(paths: string[], fn: () => Promise<void>): Promise<void> {\n return new Promise<void>((res, rej) => {\n const handler: Handler = () => {\n fn().then(\n () => {\n this.#clear(handler)\n res()\n },\n er => {\n this.#clear(handler)\n rej(er)\n },\n )\n }\n this.#add(paths, handler)\n })\n }\n\n #add(names: string[], fn: Handler) {\n const root = this.#root\n const paths = new Set<Node>()\n const dirs = new Set<Node>()\n for (const name of this.#win32 ?\n ['win32 parallelization disabled']\n : names) {\n const path =\n this.#win32 ? name : (\n stripTrailingSlashes(join(normalizeUnicode(name)))\n )\n let node = root\n for (const part of path.split('/')) {\n if (node !== root) dirs.add(node)\n const kids = (node.kids ??= new Map<string, Node>())\n let kid = kids.get(part)\n if (!kid)\n kids.set(part, (kid = { queue: [], name: part, up: node }))\n node = kid\n }\n paths.add(node)\n }\n // A path of the task is exclusive, so it also covers the use of\n // that path as a parent directory of another path.\n for (const p of paths) dirs.delete(p)\n this.#reservations.set(fn, { dirs, paths })\n for (const p of paths) p.queue.push(fn)\n for (const { queue } of dirs) {\n const last = queue[queue.length - 1]\n if (last instanceof Set) last.add(fn)\n else queue.push(new Set([fn]))\n }\n this.#run(fn)\n }\n\n // Start the task if it is first in line for each path, and in the\n // first set for each directory. A task that waits is never first in\n // line for each of them, so a task starts one time only.\n #run(fn: Handler) {\n const { paths, dirs } = this.#reservations.get(fn) as Reservation\n for (const p of paths) {\n if (p.queue[0] !== fn) return\n }\n for (const { queue } of dirs) {\n const first = queue[0]\n if (!(first instanceof Set) || !first.has(fn)) return\n }\n this.#running.add(fn)\n fn()\n }\n\n #clear(fn: Handler) {\n const { paths, dirs } = this.#reservations.get(fn) as Reservation\n const next = new Set<Handler>()\n for (const { queue } of paths) {\n // A running task is first in the queue of each of its paths.\n queue.shift()\n const after = queue[0]\n if (typeof after === 'function') next.add(after)\n else if (after) for (const f of after) next.add(f)\n }\n for (const { queue } of dirs) {\n // A running task is in the first set of each of its directories.\n const first = queue[0] as Set<Handler>\n if (first.size > 1) first.delete(fn)\n else {\n queue.shift()\n // Two sets in a row would be one set, so this is a function.\n if (queue.length) next.add(queue[0] as Handler)\n }\n }\n // Remove each node that no task uses. Each directory of the task\n // is above one of its paths.\n for (let node of paths) {\n while (node.up && !node.queue.length && !node.kids?.size) {\n node.up.kids?.delete(node.name)\n node = node.up\n }\n }\n this.#running.delete(fn)\n this.#reservations.delete(fn)\n for (const f of next) this.#run(f)\n }\n}\n"]}