tar 7.5.24 → 8.0.1

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 (330) hide show
  1. package/README.md +1712 -1147
  2. package/dist/commonjs/bytes.d.ts +21 -0
  3. package/dist/commonjs/bytes.d.ts.map +1 -0
  4. package/dist/commonjs/bytes.js +83 -0
  5. package/dist/commonjs/bytes.js.map +1 -0
  6. package/dist/commonjs/codec.d.ts +53 -0
  7. package/dist/commonjs/codec.d.ts.map +1 -0
  8. package/dist/commonjs/codec.js +35 -0
  9. package/dist/commonjs/codec.js.map +1 -0
  10. package/dist/commonjs/concat.d.ts +3 -0
  11. package/dist/commonjs/concat.d.ts.map +1 -0
  12. package/dist/commonjs/concat.js +20 -0
  13. package/dist/commonjs/concat.js.map +1 -0
  14. package/dist/commonjs/create.d.ts +39 -2
  15. package/dist/commonjs/create.d.ts.map +1 -1
  16. package/dist/commonjs/create.js +150 -64
  17. package/dist/commonjs/create.js.map +1 -1
  18. package/dist/commonjs/dealias.d.ts +79 -0
  19. package/dist/commonjs/dealias.d.ts.map +1 -0
  20. package/dist/commonjs/dealias.js +50 -0
  21. package/dist/commonjs/dealias.js.map +1 -0
  22. package/dist/commonjs/delete.d.ts +19 -0
  23. package/dist/commonjs/delete.d.ts.map +1 -0
  24. package/dist/commonjs/delete.js +121 -0
  25. package/dist/commonjs/delete.js.map +1 -0
  26. package/dist/commonjs/entries.d.ts +82 -0
  27. package/dist/commonjs/entries.d.ts.map +1 -0
  28. package/dist/commonjs/entries.js +398 -0
  29. package/dist/commonjs/entries.js.map +1 -0
  30. package/dist/commonjs/extract.d.ts +14 -2
  31. package/dist/commonjs/extract.d.ts.map +1 -1
  32. package/dist/commonjs/extract.js +40 -81
  33. package/dist/commonjs/extract.js.map +1 -1
  34. package/dist/commonjs/fs.d.ts +201 -0
  35. package/dist/commonjs/fs.d.ts.map +1 -0
  36. package/dist/commonjs/fs.js +5 -0
  37. package/dist/commonjs/fs.js.map +1 -0
  38. package/dist/commonjs/header.d.ts +4 -4
  39. package/dist/commonjs/header.d.ts.map +1 -1
  40. package/dist/commonjs/header.js +38 -42
  41. package/dist/commonjs/header.js.map +1 -1
  42. package/dist/commonjs/index.d.ts +25 -18
  43. package/dist/commonjs/index.d.ts.map +1 -1
  44. package/dist/commonjs/index.js +46 -18
  45. package/dist/commonjs/index.js.map +1 -1
  46. package/dist/commonjs/index.min.js +2 -3
  47. package/dist/commonjs/index.min.js.map +4 -4
  48. package/dist/commonjs/large-numbers.d.ts +2 -2
  49. package/dist/commonjs/large-numbers.d.ts.map +1 -1
  50. package/dist/commonjs/large-numbers.js +4 -4
  51. package/dist/commonjs/large-numbers.js.map +1 -1
  52. package/dist/commonjs/list.d.ts +23 -6
  53. package/dist/commonjs/list.d.ts.map +1 -1
  54. package/dist/commonjs/list.js +99 -138
  55. package/dist/commonjs/list.js.map +1 -1
  56. package/dist/commonjs/mkdir.d.ts +16 -11
  57. package/dist/commonjs/mkdir.d.ts.map +1 -1
  58. package/dist/commonjs/mkdir.js +121 -159
  59. package/dist/commonjs/mkdir.js.map +1 -1
  60. package/dist/commonjs/options.d.ts +284 -513
  61. package/dist/commonjs/options.d.ts.map +1 -1
  62. package/dist/commonjs/options.js +20 -61
  63. package/dist/commonjs/options.js.map +1 -1
  64. package/dist/commonjs/pack.d.ts +150 -96
  65. package/dist/commonjs/pack.d.ts.map +1 -1
  66. package/dist/commonjs/pack.js +752 -445
  67. package/dist/commonjs/pack.js.map +1 -1
  68. package/dist/commonjs/parser.d.ts +94 -0
  69. package/dist/commonjs/parser.d.ts.map +1 -0
  70. package/dist/commonjs/parser.js +381 -0
  71. package/dist/commonjs/parser.js.map +1 -0
  72. package/dist/commonjs/path-reservations.d.ts +17 -7
  73. package/dist/commonjs/path-reservations.d.ts.map +1 -1
  74. package/dist/commonjs/path-reservations.js +96 -119
  75. package/dist/commonjs/path-reservations.js.map +1 -1
  76. package/dist/commonjs/path.d.ts +19 -0
  77. package/dist/commonjs/path.d.ts.map +1 -0
  78. package/dist/commonjs/path.js +90 -0
  79. package/dist/commonjs/path.js.map +1 -0
  80. package/dist/commonjs/pax.d.ts +3 -1
  81. package/dist/commonjs/pax.d.ts.map +1 -1
  82. package/dist/commonjs/pax.js +53 -43
  83. package/dist/commonjs/pax.js.map +1 -1
  84. package/dist/commonjs/read-entry.d.ts +97 -11
  85. package/dist/commonjs/read-entry.d.ts.map +1 -1
  86. package/dist/commonjs/read-entry.js +248 -97
  87. package/dist/commonjs/read-entry.js.map +1 -1
  88. package/dist/commonjs/read-file.d.ts +22 -0
  89. package/dist/commonjs/read-file.d.ts.map +1 -0
  90. package/dist/commonjs/read-file.js +104 -0
  91. package/dist/commonjs/read-file.js.map +1 -0
  92. package/dist/commonjs/replace.d.ts +45 -1
  93. package/dist/commonjs/replace.d.ts.map +1 -1
  94. package/dist/commonjs/replace.js +165 -211
  95. package/dist/commonjs/replace.js.map +1 -1
  96. package/dist/commonjs/server/codecs.d.ts +38 -0
  97. package/dist/commonjs/server/codecs.d.ts.map +1 -0
  98. package/dist/commonjs/server/codecs.js +252 -0
  99. package/dist/commonjs/server/codecs.js.map +1 -0
  100. package/dist/commonjs/server/index.d.ts +115 -0
  101. package/dist/commonjs/server/index.d.ts.map +1 -0
  102. package/dist/commonjs/server/index.js +166 -0
  103. package/dist/commonjs/server/index.js.map +1 -0
  104. package/dist/commonjs/server/index.min.js +3 -0
  105. package/dist/commonjs/server/index.min.js.map +7 -0
  106. package/dist/commonjs/strip-absolute-path.d.ts +8 -0
  107. package/dist/commonjs/strip-absolute-path.d.ts.map +1 -1
  108. package/dist/commonjs/strip-absolute-path.js +17 -20
  109. package/dist/commonjs/strip-absolute-path.js.map +1 -1
  110. package/dist/commonjs/to-stream.d.ts +7 -0
  111. package/dist/commonjs/to-stream.d.ts.map +1 -0
  112. package/dist/commonjs/to-stream.js +22 -0
  113. package/dist/commonjs/to-stream.js.map +1 -0
  114. package/dist/commonjs/types.d.ts +4 -3
  115. package/dist/commonjs/types.d.ts.map +1 -1
  116. package/dist/commonjs/types.js +4 -3
  117. package/dist/commonjs/types.js.map +1 -1
  118. package/dist/commonjs/unpack.d.ts +143 -89
  119. package/dist/commonjs/unpack.d.ts.map +1 -1
  120. package/dist/commonjs/unpack.js +782 -725
  121. package/dist/commonjs/unpack.js.map +1 -1
  122. package/dist/commonjs/update.d.ts +11 -1
  123. package/dist/commonjs/update.d.ts.map +1 -1
  124. package/dist/commonjs/update.js +37 -28
  125. package/dist/commonjs/update.js.map +1 -1
  126. package/dist/commonjs/warn.d.ts +27 -0
  127. package/dist/commonjs/warn.d.ts.map +1 -0
  128. package/dist/commonjs/warn.js +36 -0
  129. package/dist/commonjs/warn.js.map +1 -0
  130. package/dist/commonjs/win32-path.d.ts +16 -0
  131. package/dist/commonjs/win32-path.d.ts.map +1 -0
  132. package/dist/commonjs/win32-path.js +67 -0
  133. package/dist/commonjs/win32-path.js.map +1 -0
  134. package/dist/commonjs/write-entry.d.ts +64 -111
  135. package/dist/commonjs/write-entry.d.ts.map +1 -1
  136. package/dist/commonjs/write-entry.js +108 -672
  137. package/dist/commonjs/write-entry.js.map +1 -1
  138. package/dist/commonjs/write-flag.d.ts +18 -0
  139. package/dist/commonjs/write-flag.d.ts.map +1 -0
  140. package/dist/commonjs/write-flag.js +29 -0
  141. package/dist/commonjs/write-flag.js.map +1 -0
  142. package/dist/esm/bytes.d.ts +21 -0
  143. package/dist/esm/bytes.d.ts.map +1 -0
  144. package/dist/esm/bytes.js +72 -0
  145. package/dist/esm/bytes.js.map +1 -0
  146. package/dist/esm/codec.d.ts +53 -0
  147. package/dist/esm/codec.d.ts.map +1 -0
  148. package/dist/esm/codec.js +32 -0
  149. package/dist/esm/codec.js.map +1 -0
  150. package/dist/esm/concat.d.ts +3 -0
  151. package/dist/esm/concat.d.ts.map +1 -0
  152. package/dist/esm/concat.js +16 -0
  153. package/dist/esm/concat.js.map +1 -0
  154. package/dist/esm/create.d.ts +39 -2
  155. package/dist/esm/create.d.ts.map +1 -1
  156. package/dist/esm/create.js +144 -61
  157. package/dist/esm/create.js.map +1 -1
  158. package/dist/esm/dealias.d.ts +79 -0
  159. package/dist/esm/dealias.d.ts.map +1 -0
  160. package/dist/esm/dealias.js +46 -0
  161. package/dist/esm/dealias.js.map +1 -0
  162. package/dist/esm/delete.d.ts +19 -0
  163. package/dist/esm/delete.d.ts.map +1 -0
  164. package/dist/esm/delete.js +117 -0
  165. package/dist/esm/delete.js.map +1 -0
  166. package/dist/esm/entries.d.ts +82 -0
  167. package/dist/esm/entries.d.ts.map +1 -0
  168. package/dist/esm/entries.js +388 -0
  169. package/dist/esm/entries.js.map +1 -0
  170. package/dist/esm/extract.d.ts +14 -2
  171. package/dist/esm/extract.d.ts.map +1 -1
  172. package/dist/esm/extract.js +37 -44
  173. package/dist/esm/extract.js.map +1 -1
  174. package/dist/esm/fs.d.ts +201 -0
  175. package/dist/esm/fs.d.ts.map +1 -0
  176. package/dist/esm/fs.js +4 -0
  177. package/dist/esm/fs.js.map +1 -0
  178. package/dist/esm/header.d.ts +4 -4
  179. package/dist/esm/header.d.ts.map +1 -1
  180. package/dist/esm/header.js +38 -42
  181. package/dist/esm/header.js.map +1 -1
  182. package/dist/esm/index.d.ts +25 -18
  183. package/dist/esm/index.d.ts.map +1 -1
  184. package/dist/esm/index.js +23 -18
  185. package/dist/esm/index.js.map +1 -1
  186. package/dist/esm/index.min.js +2 -3
  187. package/dist/esm/index.min.js.map +4 -4
  188. package/dist/esm/large-numbers.d.ts +2 -2
  189. package/dist/esm/large-numbers.d.ts.map +1 -1
  190. package/dist/esm/large-numbers.js +4 -4
  191. package/dist/esm/large-numbers.js.map +1 -1
  192. package/dist/esm/list.d.ts +23 -6
  193. package/dist/esm/list.d.ts.map +1 -1
  194. package/dist/esm/list.js +96 -101
  195. package/dist/esm/list.js.map +1 -1
  196. package/dist/esm/mkdir.d.ts +16 -11
  197. package/dist/esm/mkdir.d.ts.map +1 -1
  198. package/dist/esm/mkdir.js +119 -154
  199. package/dist/esm/mkdir.js.map +1 -1
  200. package/dist/esm/options.d.ts +284 -513
  201. package/dist/esm/options.d.ts.map +1 -1
  202. package/dist/esm/options.js +17 -51
  203. package/dist/esm/options.js.map +1 -1
  204. package/dist/esm/pack.d.ts +150 -96
  205. package/dist/esm/pack.d.ts.map +1 -1
  206. package/dist/esm/pack.js +750 -441
  207. package/dist/esm/pack.js.map +1 -1
  208. package/dist/esm/parser.d.ts +94 -0
  209. package/dist/esm/parser.d.ts.map +1 -0
  210. package/dist/esm/parser.js +377 -0
  211. package/dist/esm/parser.js.map +1 -0
  212. package/dist/esm/path-reservations.d.ts +17 -7
  213. package/dist/esm/path-reservations.d.ts.map +1 -1
  214. package/dist/esm/path-reservations.js +96 -119
  215. package/dist/esm/path-reservations.js.map +1 -1
  216. package/dist/esm/path.d.ts +19 -0
  217. package/dist/esm/path.d.ts.map +1 -0
  218. package/dist/esm/path.js +80 -0
  219. package/dist/esm/path.js.map +1 -0
  220. package/dist/esm/pax.d.ts +3 -1
  221. package/dist/esm/pax.d.ts.map +1 -1
  222. package/dist/esm/pax.js +52 -42
  223. package/dist/esm/pax.js.map +1 -1
  224. package/dist/esm/read-entry.d.ts +97 -11
  225. package/dist/esm/read-entry.d.ts.map +1 -1
  226. package/dist/esm/read-entry.js +245 -96
  227. package/dist/esm/read-entry.js.map +1 -1
  228. package/dist/esm/read-file.d.ts +22 -0
  229. package/dist/esm/read-file.d.ts.map +1 -0
  230. package/dist/esm/read-file.js +99 -0
  231. package/dist/esm/read-file.js.map +1 -0
  232. package/dist/esm/replace.d.ts +45 -1
  233. package/dist/esm/replace.d.ts.map +1 -1
  234. package/dist/esm/replace.js +161 -207
  235. package/dist/esm/replace.js.map +1 -1
  236. package/dist/esm/server/codecs.d.ts +38 -0
  237. package/dist/esm/server/codecs.d.ts.map +1 -0
  238. package/dist/esm/server/codecs.js +214 -0
  239. package/dist/esm/server/codecs.js.map +1 -0
  240. package/dist/esm/server/index.d.ts +115 -0
  241. package/dist/esm/server/index.d.ts.map +1 -0
  242. package/dist/esm/server/index.js +102 -0
  243. package/dist/esm/server/index.js.map +1 -0
  244. package/dist/esm/server/index.min.js +3 -0
  245. package/dist/esm/server/index.min.js.map +7 -0
  246. package/dist/esm/strip-absolute-path.d.ts +8 -0
  247. package/dist/esm/strip-absolute-path.d.ts.map +1 -1
  248. package/dist/esm/strip-absolute-path.js +17 -20
  249. package/dist/esm/strip-absolute-path.js.map +1 -1
  250. package/dist/esm/to-stream.d.ts +7 -0
  251. package/dist/esm/to-stream.d.ts.map +1 -0
  252. package/dist/esm/to-stream.js +18 -0
  253. package/dist/esm/to-stream.js.map +1 -0
  254. package/dist/esm/types.d.ts +4 -3
  255. package/dist/esm/types.d.ts.map +1 -1
  256. package/dist/esm/types.js +4 -3
  257. package/dist/esm/types.js.map +1 -1
  258. package/dist/esm/unpack.d.ts +143 -89
  259. package/dist/esm/unpack.d.ts.map +1 -1
  260. package/dist/esm/unpack.js +778 -719
  261. package/dist/esm/unpack.js.map +1 -1
  262. package/dist/esm/update.d.ts +11 -1
  263. package/dist/esm/update.d.ts.map +1 -1
  264. package/dist/esm/update.js +35 -28
  265. package/dist/esm/update.js.map +1 -1
  266. package/dist/esm/warn.d.ts +27 -0
  267. package/dist/esm/warn.d.ts.map +1 -0
  268. package/dist/esm/warn.js +31 -0
  269. package/dist/esm/warn.js.map +1 -0
  270. package/dist/esm/win32-path.d.ts +16 -0
  271. package/dist/esm/win32-path.d.ts.map +1 -0
  272. package/dist/esm/win32-path.js +60 -0
  273. package/dist/esm/win32-path.js.map +1 -0
  274. package/dist/esm/write-entry.d.ts +64 -111
  275. package/dist/esm/write-entry.d.ts.map +1 -1
  276. package/dist/esm/write-entry.js +107 -633
  277. package/dist/esm/write-entry.js.map +1 -1
  278. package/dist/esm/write-flag.d.ts +18 -0
  279. package/dist/esm/write-flag.d.ts.map +1 -0
  280. package/dist/esm/write-flag.js +25 -0
  281. package/dist/esm/write-flag.js.map +1 -0
  282. package/package.json +68 -224
  283. package/dist/commonjs/get-write-flag.d.ts +0 -2
  284. package/dist/commonjs/get-write-flag.d.ts.map +0 -1
  285. package/dist/commonjs/get-write-flag.js +0 -32
  286. package/dist/commonjs/get-write-flag.js.map +0 -1
  287. package/dist/commonjs/make-command.d.ts +0 -49
  288. package/dist/commonjs/make-command.d.ts.map +0 -1
  289. package/dist/commonjs/make-command.js +0 -52
  290. package/dist/commonjs/make-command.js.map +0 -1
  291. package/dist/commonjs/normalize-windows-path.d.ts +0 -2
  292. package/dist/commonjs/normalize-windows-path.d.ts.map +0 -1
  293. package/dist/commonjs/normalize-windows-path.js +0 -12
  294. package/dist/commonjs/normalize-windows-path.js.map +0 -1
  295. package/dist/commonjs/parse.d.ts +0 -92
  296. package/dist/commonjs/parse.d.ts.map +0 -1
  297. package/dist/commonjs/parse.js +0 -655
  298. package/dist/commonjs/parse.js.map +0 -1
  299. package/dist/commonjs/process-umask.d.ts +0 -2
  300. package/dist/commonjs/process-umask.d.ts.map +0 -1
  301. package/dist/commonjs/process-umask.js +0 -7
  302. package/dist/commonjs/process-umask.js.map +0 -1
  303. package/dist/commonjs/warn-method.d.ts +0 -24
  304. package/dist/commonjs/warn-method.d.ts.map +0 -1
  305. package/dist/commonjs/warn-method.js +0 -31
  306. package/dist/commonjs/warn-method.js.map +0 -1
  307. package/dist/esm/get-write-flag.d.ts +0 -2
  308. package/dist/esm/get-write-flag.d.ts.map +0 -1
  309. package/dist/esm/get-write-flag.js +0 -26
  310. package/dist/esm/get-write-flag.js.map +0 -1
  311. package/dist/esm/make-command.d.ts +0 -49
  312. package/dist/esm/make-command.d.ts.map +0 -1
  313. package/dist/esm/make-command.js +0 -48
  314. package/dist/esm/make-command.js.map +0 -1
  315. package/dist/esm/normalize-windows-path.d.ts +0 -2
  316. package/dist/esm/normalize-windows-path.d.ts.map +0 -1
  317. package/dist/esm/normalize-windows-path.js +0 -9
  318. package/dist/esm/normalize-windows-path.js.map +0 -1
  319. package/dist/esm/parse.d.ts +0 -92
  320. package/dist/esm/parse.d.ts.map +0 -1
  321. package/dist/esm/parse.js +0 -651
  322. package/dist/esm/parse.js.map +0 -1
  323. package/dist/esm/process-umask.d.ts +0 -2
  324. package/dist/esm/process-umask.d.ts.map +0 -1
  325. package/dist/esm/process-umask.js +0 -3
  326. package/dist/esm/process-umask.js.map +0 -1
  327. package/dist/esm/warn-method.d.ts +0 -24
  328. package/dist/esm/warn-method.d.ts.map +0 -1
  329. package/dist/esm/warn-method.js +0 -27
  330. package/dist/esm/warn-method.js.map +0 -1
@@ -1,166 +1,143 @@
1
- // A path exclusive reservation system
2
- // reserve([list, of, paths], fn)
3
- // When the fn is first in line for all its paths, it
4
- // is called with a cb that clears the reservation.
5
- //
6
- // Used by async unpack to avoid clobbering paths in use,
7
- // while still allowing maximal safe parallelization.
8
- import { join } from 'node:path';
1
+ // The async extraction does several entries at one time. Two entries
2
+ // must not work on the same path at one time: a link needs its
3
+ // target, and an entry can replace an object of a different type.
9
4
  import { normalizeUnicode } from './normalize-unicode.js';
10
5
  import { stripTrailingSlashes } from './strip-trailing-slashes.js';
11
- const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform;
12
- const isWindows = platform === 'win32';
13
- // return a set of parent dirs for a given path
14
- // '/a/b/c/d' -> ['/', '/a', '/a/b', '/a/b/c', '/a/b/c/d']
6
+ import { join } from './path.js';
7
+ // The parent directories of a normalized path.
8
+ // '/a/b/c' gives ['/', '/a', '/a/b'].
15
9
  const getDirs = (path) => {
16
- const dirs = path
17
- .split('/')
18
- .slice(0, -1)
19
- .reduce((set, path) => {
20
- const s = set.at(-1);
21
- if (s !== undefined) {
22
- path = join(s, path);
23
- }
24
- set.push(path || '/');
25
- return set;
26
- }, []);
10
+ const dirs = [];
11
+ for (let i = path.indexOf('/'); i !== -1; i = path.indexOf('/', i + 1)) {
12
+ dirs.push(path.slice(0, i) || '/');
13
+ }
27
14
  return dirs;
28
15
  };
16
+ /**
17
+ * Gives each task the exclusive use of its paths, and the shared use
18
+ * of their parent directories. A task starts when each task before
19
+ * it that uses one of its paths, or a directory above or below one,
20
+ * is done.
21
+ *
22
+ * Paths that differ only in case or in Unicode form are the same
23
+ * path, because they can be the same file on disk.
24
+ */
29
25
  export class PathReservations {
30
- // path => [function or Set]
31
- // A Set object means a directory reservation
32
- // A fn is a direct reservation on that path
26
+ // For each path: the tasks that wait for it, in order. A function
27
+ // has the path for itself. A Set of functions shares the path as a
28
+ // parent directory.
33
29
  #queues = new Map();
34
- // fn => {paths:[path,...], dirs:[path, ...]}
35
30
  #reservations = new Map();
36
- // functions currently running
37
31
  #running = new Set();
32
+ #win32;
33
+ /** @param win32 run one task at a time */
34
+ constructor(win32 = false) {
35
+ this.#win32 = win32;
36
+ }
37
+ /**
38
+ * Call `fn` when it is first in line for each of its paths. The
39
+ * paths are free again when the promise of `fn` settles. Returns a
40
+ * promise with the result of `fn`.
41
+ */
38
42
  reserve(paths, fn) {
43
+ return new Promise((res, rej) => {
44
+ const handler = () => {
45
+ fn().then(() => {
46
+ this.#clear(handler);
47
+ res();
48
+ }, er => {
49
+ this.#clear(handler);
50
+ rej(er);
51
+ });
52
+ };
53
+ this.#add(paths, handler);
54
+ });
55
+ }
56
+ #add(paths, fn) {
39
57
  paths =
40
- isWindows ?
58
+ this.#win32 ?
41
59
  ['win32 parallelization disabled']
42
- : paths.map(p => {
43
- // don't need normPath, because we skip this entirely for windows
44
- return stripTrailingSlashes(join(normalizeUnicode(p)));
45
- });
46
- const dirs = new Set(paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b)));
60
+ : [
61
+ ...new Set(paths.map(p => stripTrailingSlashes(join(normalizeUnicode(p))))),
62
+ ];
63
+ const dirs = new Set(paths.flatMap(getDirs));
64
+ // A path of the task is exclusive, so it also covers the use of
65
+ // that path as a parent directory of another path.
66
+ for (const p of paths)
67
+ dirs.delete(p);
47
68
  this.#reservations.set(fn, { dirs, paths });
48
69
  for (const p of paths) {
49
70
  const q = this.#queues.get(p);
50
- if (!q) {
71
+ if (!q)
51
72
  this.#queues.set(p, [fn]);
52
- }
53
- else {
73
+ else
54
74
  q.push(fn);
55
- }
56
75
  }
57
76
  for (const dir of dirs) {
58
77
  const q = this.#queues.get(dir);
59
- if (!q) {
78
+ const last = q?.[q.length - 1];
79
+ if (!q)
60
80
  this.#queues.set(dir, [new Set([fn])]);
61
- }
62
- else {
63
- const l = q.at(-1);
64
- if (l instanceof Set) {
65
- l.add(fn);
66
- }
67
- else {
68
- q.push(new Set([fn]));
69
- }
70
- }
81
+ else if (last instanceof Set)
82
+ last.add(fn);
83
+ else
84
+ q.push(new Set([fn]));
71
85
  }
72
- return this.#run(fn);
86
+ this.#run(fn);
73
87
  }
74
- // return the queues for each path the function cares about
75
- // fn => {paths, dirs}
76
- #getQueues(fn) {
77
- const res = this.#reservations.get(fn);
78
- /* c8 ignore start */
79
- if (!res) {
80
- throw new Error('function does not have any path reservations');
81
- }
82
- /* c8 ignore stop */
83
- return {
84
- paths: res.paths.map((path) => this.#queues.get(path)),
85
- dirs: [...res.dirs].map(path => this.#queues.get(path)),
86
- };
87
- }
88
- // check if fn is first in line for all its paths, and is
89
- // included in the first set for all its dir queues
90
- check(fn) {
91
- const { paths, dirs } = this.#getQueues(fn);
92
- return (paths.every(q => q && q[0] === fn) &&
93
- dirs.every(q => q && q[0] instanceof Set && q[0].has(fn)));
94
- }
95
- // run the function if it's first in line and not already running
88
+ // Start the task if it is first in line for each path, and in the
89
+ // first set for each directory. A task that waits is never first in
90
+ // line for each of them, so a task starts one time only.
96
91
  #run(fn) {
97
- if (this.#running.has(fn) || !this.check(fn)) {
98
- return false;
92
+ const { paths, dirs } = this.#reservations.get(fn);
93
+ for (const p of paths) {
94
+ if (this.#queues.get(p)?.[0] !== fn)
95
+ return;
96
+ }
97
+ for (const dir of dirs) {
98
+ const first = this.#queues.get(dir)?.[0];
99
+ if (!(first instanceof Set) || !first.has(fn))
100
+ return;
99
101
  }
100
102
  this.#running.add(fn);
101
- fn(() => this.#clear(fn));
102
- return true;
103
+ fn();
103
104
  }
104
105
  #clear(fn) {
105
- if (!this.#running.has(fn)) {
106
- return false;
107
- }
108
- const res = this.#reservations.get(fn);
109
- /* c8 ignore start */
110
- if (!res) {
111
- throw new Error('invalid reservation');
112
- }
113
- /* c8 ignore stop */
114
- const { paths, dirs } = res;
106
+ const { paths, dirs } = this.#reservations.get(fn);
115
107
  const next = new Set();
116
108
  for (const path of paths) {
109
+ // A running task is first in the queue of each of its paths.
117
110
  const q = this.#queues.get(path);
118
- /* c8 ignore start */
119
- if (!q || q?.[0] !== fn) {
120
- continue;
121
- }
122
- /* c8 ignore stop */
123
- const q0 = q[1];
124
- if (!q0) {
111
+ const after = q[1];
112
+ if (!after) {
125
113
  this.#queues.delete(path);
126
114
  continue;
127
115
  }
128
116
  q.shift();
129
- if (typeof q0 === 'function') {
130
- next.add(q0);
131
- }
132
- else {
133
- for (const f of q0) {
117
+ if (typeof after === 'function')
118
+ next.add(after);
119
+ else
120
+ for (const f of after)
134
121
  next.add(f);
135
- }
136
- }
137
122
  }
138
123
  for (const dir of dirs) {
124
+ // A running task is in the first set of each of its directories.
139
125
  const q = this.#queues.get(dir);
140
- const q0 = q?.[0];
141
- /* c8 ignore next - type safety only */
142
- if (!q || !(q0 instanceof Set))
143
- continue;
144
- if (q0.size === 1 && q.length === 1) {
126
+ const first = q[0];
127
+ if (first.size > 1)
128
+ first.delete(fn);
129
+ else if (q.length === 1)
145
130
  this.#queues.delete(dir);
146
- continue;
147
- }
148
- else if (q0.size === 1) {
149
- q.shift();
150
- // next one must be a function,
151
- // or else the Set would've been reused
152
- const n = q[0];
153
- if (typeof n === 'function') {
154
- next.add(n);
155
- }
156
- }
157
131
  else {
158
- q0.delete(fn);
132
+ q.shift();
133
+ // Two sets in a row would be one set, so this is a function.
134
+ next.add(q[0]);
159
135
  }
160
136
  }
161
137
  this.#running.delete(fn);
162
- next.forEach(fn => this.#run(fn));
163
- return true;
138
+ this.#reservations.delete(fn);
139
+ for (const f of next)
140
+ this.#run(f);
164
141
  }
165
142
  }
166
143
  //# sourceMappingURL=path-reservations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"path-reservations.js","sourceRoot":"","sources":["../../src/path-reservations.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AACjC,qDAAqD;AACrD,mDAAmD;AACnD,EAAE;AACF,yDAAyD;AACzD,qDAAqD;AAErD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAElE,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,yBAAyB,IAAI,OAAO,CAAC,QAAQ,CAAA;AAC1E,MAAM,SAAS,GAAG,QAAQ,KAAK,OAAO,CAAA;AAStC,+CAA+C;AAC/C,0DAA0D;AAC1D,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,EAAE;IAC/B,MAAM,IAAI,GAAG,IAAI;SACd,KAAK,CAAC,GAAG,CAAC;SACV,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SACZ,MAAM,CAAC,CAAC,GAAa,EAAE,IAAI,EAAE,EAAE;QAC9B,MAAM,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;QACpB,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YACpB,IAAI,GAAG,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;QACtB,CAAC;QACD,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,CAAA;QACrB,OAAO,GAAG,CAAA;IACZ,CAAC,EAAE,EAAE,CAAC,CAAA;IACR,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAED,MAAM,OAAO,gBAAgB;IAC3B,4BAA4B;IAC5B,6CAA6C;IAC7C,4CAA4C;IAC5C,OAAO,GAAG,IAAI,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+B;gBAC/B,uCAAuC;gBACvC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;gBACd,IAAI,OAAO,CAAC,KAAK,UAAU,EAAE,CAAC;oBAC5B,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;gBACb,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;YACf,CAAC;QACH,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QACxB,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAA;QACjC,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["// A path exclusive reservation system\n// reserve([list, of, paths], fn)\n// When the fn is first in line for all its paths, it\n// is called with a cb that clears the reservation.\n//\n// Used by async unpack to avoid clobbering paths in use,\n// while still allowing maximal safe parallelization.\n\nimport { join } from 'node:path'\nimport { normalizeUnicode } from './normalize-unicode.js'\nimport { stripTrailingSlashes } from './strip-trailing-slashes.js'\n\nconst platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform\nconst isWindows = platform === 'win32'\n\nexport type Reservation = {\n paths: string[]\n dirs: Set<string>\n}\n\nexport type Handler = (clear: () => void) => void\n\n// return a set of parent dirs for a given path\n// '/a/b/c/d' -> ['/', '/a', '/a/b', '/a/b/c', '/a/b/c/d']\nconst getDirs = (path: string) => {\n const dirs = path\n .split('/')\n .slice(0, -1)\n .reduce((set: string[], path) => {\n const s = set.at(-1)\n if (s !== undefined) {\n path = join(s, path)\n }\n set.push(path || '/')\n return set\n }, [])\n return dirs\n}\n\nexport class PathReservations {\n // path => [function or Set]\n // A Set object means a directory reservation\n // A fn is a direct reservation on that path\n #queues = new Map<string, (Handler | Set<Handler>)[]>()\n\n // fn => {paths:[path,...], dirs:[path, ...]}\n #reservations = new Map<Handler, Reservation>()\n\n // functions currently running\n #running = new Set<Handler>()\n\n reserve(paths: string[], fn: Handler) {\n paths =\n isWindows ?\n ['win32 parallelization disabled']\n : paths.map(p => {\n // don't need normPath, because we skip this entirely for windows\n return stripTrailingSlashes(join(normalizeUnicode(p)))\n })\n\n const dirs = new Set<string>(\n paths.map(path => getDirs(path)).reduce((a, b) => a.concat(b)),\n )\n this.#reservations.set(fn, { dirs, paths })\n for (const p of paths) {\n const q = this.#queues.get(p)\n if (!q) {\n this.#queues.set(p, [fn])\n } else {\n q.push(fn)\n }\n }\n for (const dir of dirs) {\n const q = this.#queues.get(dir)\n if (!q) {\n this.#queues.set(dir, [new Set([fn])])\n } else {\n const l = q.at(-1)\n if (l instanceof Set) {\n l.add(fn)\n } else {\n q.push(new Set([fn]))\n }\n }\n }\n return this.#run(fn)\n }\n\n // return the queues for each path the function cares about\n // fn => {paths, dirs}\n #getQueues(fn: Handler): {\n paths: Handler[][]\n dirs: (Handler | Set<Handler>)[][]\n } {\n const res = this.#reservations.get(fn)\n /* c8 ignore start */\n if (!res) {\n throw new Error('function does not have any path reservations')\n }\n /* c8 ignore stop */\n return {\n paths: res.paths.map((path: string) =>\n this.#queues.get(path),\n ) as Handler[][],\n dirs: [...res.dirs].map(path => this.#queues.get(path)) as (\n | Handler\n | Set<Handler>\n )[][],\n }\n }\n\n // check if fn is first in line for all its paths, and is\n // included in the first set for all its dir queues\n check(fn: Handler) {\n const { paths, dirs } = this.#getQueues(fn)\n return (\n paths.every(q => q && q[0] === fn) &&\n dirs.every(q => q && q[0] instanceof Set && q[0].has(fn))\n )\n }\n\n // run the function if it's first in line and not already running\n #run(fn: Handler) {\n if (this.#running.has(fn) || !this.check(fn)) {\n return false\n }\n this.#running.add(fn)\n fn(() => this.#clear(fn))\n return true\n }\n\n #clear(fn: Handler) {\n if (!this.#running.has(fn)) {\n return false\n }\n const res = this.#reservations.get(fn)\n /* c8 ignore start */\n if (!res) {\n throw new Error('invalid reservation')\n }\n /* c8 ignore stop */\n const { paths, dirs } = res\n\n const next = new Set<Handler>()\n for (const path of paths) {\n const q = this.#queues.get(path)\n /* c8 ignore start */\n if (!q || q?.[0] !== fn) {\n continue\n }\n /* c8 ignore stop */\n const q0 = q[1]\n if (!q0) {\n this.#queues.delete(path)\n continue\n }\n q.shift()\n if (typeof q0 === 'function') {\n next.add(q0)\n } else {\n for (const f of q0) {\n next.add(f)\n }\n }\n }\n\n for (const dir of dirs) {\n const q = this.#queues.get(dir)\n const q0 = q?.[0]\n /* c8 ignore next - type safety only */\n if (!q || !(q0 instanceof Set)) continue\n if (q0.size === 1 && q.length === 1) {\n this.#queues.delete(dir)\n continue\n } else if (q0.size === 1) {\n q.shift()\n // next one must be a function,\n // or else the Set would've been reused\n const n = q[0]\n if (typeof n === 'function') {\n next.add(n)\n }\n } else {\n q0.delete(fn)\n }\n }\n\n this.#running.delete(fn)\n next.forEach(fn => this.#run(fn))\n return true\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,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;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,EAAE,CAAC,CAAC,IAA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;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","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"]}
@@ -0,0 +1,19 @@
1
+ /** Resolve `.` and `..` parts, and remove repeated slashes. */
2
+ export declare const normalize: (path: string) => string;
3
+ /** Join the parts with a slash, then normalize the result. */
4
+ export declare const join: (...parts: string[]) => string;
5
+ /** The path without its last part. */
6
+ export declare const dirname: (path: string) => string;
7
+ /** The last part of the path. */
8
+ export declare const basename: (path: string) => string;
9
+ /** True if the path starts with a slash. */
10
+ export declare const isAbsolute: (path: string) => boolean;
11
+ /**
12
+ * Resolve the parts from right to left, up to the first absolute
13
+ * part. There is no working directory here: if no part is absolute,
14
+ * the result is a relative path.
15
+ */
16
+ export declare const resolve: (...parts: string[]) => string;
17
+ /** The relative path from `from` to `to`. */
18
+ export declare const relative: (from: string, to: string) => string;
19
+ //# sourceMappingURL=path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":"AAGA,+DAA+D;AAC/D,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,MAaxC,CAAA;AAED,8DAA8D;AAC9D,eAAO,MAAM,IAAI,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,MACC,CAAA;AAE3C,sCAAsC;AACtC,eAAO,MAAM,OAAO,GAAI,MAAM,MAAM,KAAG,MAQtC,CAAA;AAED,iCAAiC;AACjC,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,KAAG,MAIvC,CAAA;AAED,4CAA4C;AAC5C,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,KAAG,OACf,CAAA;AAE3B;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAAI,GAAG,OAAO,MAAM,EAAE,KAAG,MAQ5C,CAAA;AAED,6CAA6C;AAC7C,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,IAAI,MAAM,KAAG,MAYnD,CAAA"}
@@ -0,0 +1,80 @@
1
+ // The POSIX path functions that tar paths need. Each one gives the
2
+ // same result as the function of the same name in `path.posix`.
3
+ /** Resolve `.` and `..` parts, and remove repeated slashes. */
4
+ export const normalize = (path) => {
5
+ const absolute = path.charCodeAt(0) === 47;
6
+ const out = [];
7
+ for (const part of path.split('/')) {
8
+ if (part === '' || part === '.')
9
+ continue;
10
+ if (part !== '..')
11
+ out.push(part);
12
+ else if (out.length && out[out.length - 1] !== '..')
13
+ out.pop();
14
+ else if (!absolute)
15
+ out.push(part);
16
+ }
17
+ const trail = out.length && path.charCodeAt(path.length - 1) === 47;
18
+ const res = out.join('/') + (trail ? '/' : '');
19
+ if (absolute)
20
+ return '/' + res;
21
+ return res || (path.endsWith('/') ? './' : '.');
22
+ };
23
+ /** Join the parts with a slash, then normalize the result. */
24
+ export const join = (...parts) => normalize(parts.filter(p => p).join('/'));
25
+ /** The path without its last part. */
26
+ export const dirname = (path) => {
27
+ let end = path.length;
28
+ while (end > 1 && path.charCodeAt(end - 1) === 47)
29
+ end--;
30
+ const slash = path.lastIndexOf('/', end - 1);
31
+ if (slash === -1)
32
+ return '.';
33
+ if (slash === 0)
34
+ return '/';
35
+ if (slash === 1 && path.charCodeAt(0) === 47)
36
+ return '//';
37
+ return path.slice(0, slash);
38
+ };
39
+ /** The last part of the path. */
40
+ export const basename = (path) => {
41
+ let end = path.length;
42
+ while (end > 0 && path.charCodeAt(end - 1) === 47)
43
+ end--;
44
+ return path.slice(path.lastIndexOf('/', end - 1) + 1, end);
45
+ };
46
+ /** True if the path starts with a slash. */
47
+ export const isAbsolute = (path) => path.charCodeAt(0) === 47;
48
+ /**
49
+ * Resolve the parts from right to left, up to the first absolute
50
+ * part. There is no working directory here: if no part is absolute,
51
+ * the result is a relative path.
52
+ */
53
+ export const resolve = (...parts) => {
54
+ let path = '';
55
+ for (let i = parts.length - 1; i >= 0 && !isAbsolute(path); i--) {
56
+ const part = parts[i];
57
+ if (part)
58
+ path = path ? part + '/' + path : part;
59
+ }
60
+ const res = normalize(path);
61
+ return res.length > 1 && res.endsWith('/') ? res.slice(0, -1) : res;
62
+ };
63
+ /** The relative path from `from` to `to`. */
64
+ export const relative = (from, to) => {
65
+ const f = resolve(from).split('/');
66
+ const t = resolve(to).split('/');
67
+ // The root gives an empty last part.
68
+ if (!f[f.length - 1])
69
+ f.pop();
70
+ if (!t[t.length - 1])
71
+ t.pop();
72
+ let same = 0;
73
+ while (same < f.length && same < t.length && f[same] === t[same])
74
+ same++;
75
+ return [
76
+ ...Array.from({ length: f.length - same }, () => '..'),
77
+ ...t.slice(same),
78
+ ].join('/');
79
+ };
80
+ //# sourceMappingURL=path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,gEAAgE;AAEhE,+DAA+D;AAC/D,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAU,E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G,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAAE,IAAI,EAAE,CAAA;IACxE,OAAO;QACL,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC;QACtD,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;KACjB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACb,CAAC,CAAA","sourcesContent":["// The POSIX path functions that tar paths need. Each one gives the\n// same result as the function of the same name in `path.posix`.\n\n/** Resolve `.` and `..` parts, and remove repeated slashes. */\nexport const normalize = (path: string): string => {\n const absolute = path.charCodeAt(0) === 47\n const out: string[] = []\n for (const part of path.split('/')) {\n if (part === '' || part === '.') continue\n if (part !== '..') out.push(part)\n else if (out.length && out[out.length - 1] !== '..') out.pop()\n else if (!absolute) out.push(part)\n }\n const trail = out.length && path.charCodeAt(path.length - 1) === 47\n const res = out.join('/') + (trail ? '/' : '')\n if (absolute) return '/' + res\n return res || (path.endsWith('/') ? './' : '.')\n}\n\n/** Join the parts with a slash, then normalize the result. */\nexport const join = (...parts: string[]): string =>\n normalize(parts.filter(p => p).join('/'))\n\n/** The path without its last part. */\nexport const dirname = (path: string): string => {\n let end = path.length\n while (end > 1 && path.charCodeAt(end - 1) === 47) end--\n const slash = path.lastIndexOf('/', end - 1)\n if (slash === -1) return '.'\n if (slash === 0) return '/'\n if (slash === 1 && path.charCodeAt(0) === 47) return '//'\n return path.slice(0, slash)\n}\n\n/** The last part of the path. */\nexport const basename = (path: string): string => {\n let end = path.length\n while (end > 0 && path.charCodeAt(end - 1) === 47) end--\n return path.slice(path.lastIndexOf('/', end - 1) + 1, end)\n}\n\n/** True if the path starts with a slash. */\nexport const isAbsolute = (path: string): boolean =>\n path.charCodeAt(0) === 47\n\n/**\n * Resolve the parts from right to left, up to the first absolute\n * part. There is no working directory here: if no part is absolute,\n * the result is a relative path.\n */\nexport const resolve = (...parts: string[]): string => {\n let path = ''\n for (let i = parts.length - 1; i >= 0 && !isAbsolute(path); i--) {\n const part = parts[i]\n if (part) path = path ? part + '/' + path : part\n }\n const res = normalize(path)\n return res.length > 1 && res.endsWith('/') ? res.slice(0, -1) : res\n}\n\n/** The relative path from `from` to `to`. */\nexport const relative = (from: string, to: string): string => {\n const f = resolve(from).split('/')\n const t = resolve(to).split('/')\n // The root gives an empty last part.\n if (!f[f.length - 1]) f.pop()\n if (!t[t.length - 1]) t.pop()\n let same = 0\n while (same < f.length && same < t.length && f[same] === t[same]) same++\n return [\n ...Array.from({ length: f.length - same }, () => '..'),\n ...t.slice(same),\n ].join('/')\n}\n"]}
package/dist/esm/pax.d.ts CHANGED
@@ -18,9 +18,11 @@ export declare class Pax implements HeaderData {
18
18
  mode?: number;
19
19
  global: boolean;
20
20
  constructor(obj: HeaderData, global?: boolean);
21
- encode(): Buffer<ArrayBuffer>;
21
+ encode(): Uint8Array<ArrayBuffer>;
22
22
  encodeBody(): string;
23
23
  encodeField(field: keyof Pax): string;
24
24
  static parse(str: string, ex?: HeaderData, g?: boolean): Pax;
25
+ /** Parse the body of a PAX header entry. */
26
+ static parseBytes(buf: Uint8Array, ex?: HeaderData, g?: boolean): Pax;
25
27
  }
26
28
  //# sourceMappingURL=pax.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pax.d.ts","sourceRoot":"","sources":["../../src/pax.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAK7C,qBAAa,GAAI,YAAW,UAAU;IACpC,KAAK,CAAC,EAAE,IAAI,CAAA;IACZ,KAAK,CAAC,EAAE,IAAI,CAAA;IACZ,KAAK,CAAC,EAAE,IAAI,CAAA;IAEZ,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB,GAAG,CAAC,EAAE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CAAA;IACZ,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb,MAAM,EAAE,OAAO,CAAA;gBAEH,GAAG,EAAE,UAAU,EAAE,MAAM,GAAE,OAAe;IAmBpD,MAAM;IAqCN,UAAU;IAoBV,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,MAAM;IA2BrC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,GAAE,OAAe;IAI7D,4CAA4C;IAC5C,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,GAAE,OAAe;CAGvE"}
package/dist/esm/pax.js CHANGED
@@ -1,6 +1,7 @@
1
- import { basename } from 'node:path';
2
1
  import { Header } from './header.js';
3
- const SIMPLE_NUMBER = /^(?:0|[1-9]\d*)$/;
2
+ import { decodeUtf8, encodeUtf8Into, utf8Length } from './bytes.js';
3
+ import { basename } from './path.js';
4
+ const SIMPLE_NUMBER = /^\d+$/;
4
5
  export class Pax {
5
6
  atime;
6
7
  mtime;
@@ -40,24 +41,18 @@ export class Pax {
40
41
  encode() {
41
42
  const body = this.encodeBody();
42
43
  if (body === '') {
43
- return Buffer.allocUnsafe(0);
44
+ return new Uint8Array(0);
44
45
  }
45
- const bodyLen = Buffer.byteLength(body);
46
+ const bodyLen = utf8Length(body);
46
47
  // round up to 512 bytes
47
48
  // add 512 for header
48
49
  const bufLen = 512 * Math.ceil(1 + bodyLen / 512);
49
- const buf = Buffer.allocUnsafe(bufLen);
50
- // 0-fill the header section, it might not hit every field
51
- for (let i = 0; i < 512; i++) {
52
- buf[i] = 0;
53
- }
50
+ const buf = new Uint8Array(bufLen);
54
51
  new Header({
55
52
  // XXX split the path
56
53
  // then the path should be PaxHeader + basename, but less than 99,
57
54
  // prepend with the dirname
58
- /* c8 ignore start */
59
55
  path: ('PaxHeader/' + basename(this.path ?? '')).slice(0, 99),
60
- /* c8 ignore stop */
61
56
  mode: this.mode || 0o644,
62
57
  uid: this.uid,
63
58
  gid: this.gid,
@@ -72,11 +67,7 @@ export class Pax {
72
67
  atime: this.atime,
73
68
  ctime: this.ctime,
74
69
  }).encode(buf);
75
- buf.write(body, 512, bodyLen, 'utf8');
76
- // null pad after the body
77
- for (let i = bodyLen + 512; i < buf.length; i++) {
78
- buf[i] = 0;
79
- }
70
+ encodeUtf8Into(body, buf, 512, bodyLen);
80
71
  return buf;
81
72
  }
82
73
  encodeBody() {
@@ -101,7 +92,7 @@ export class Pax {
101
92
  return '';
102
93
  }
103
94
  const r = this[field];
104
- const v = r instanceof Date ? r.getTime() / 1000 : r;
95
+ const v = typeof r === 'object' ? r.getTime() / 1000 : r;
105
96
  const s = ' ' +
106
97
  (field === 'dev' || field === 'ino' || field === 'nlink' ?
107
98
  'SCHILY.'
@@ -110,7 +101,7 @@ export class Pax {
110
101
  '=' +
111
102
  v +
112
103
  '\n';
113
- const byteLen = Buffer.byteLength(s);
104
+ const byteLen = utf8Length(s);
114
105
  // the digits includes the length of the digits in ascii base-10
115
106
  // so if it's 9 characters, then adding 1 for the 9 makes it 10
116
107
  // which makes it 11 chars.
@@ -122,29 +113,49 @@ export class Pax {
122
113
  return len + s;
123
114
  }
124
115
  static parse(str, ex, g = false) {
125
- return new Pax(merge(parseKV(str), ex), g);
116
+ return Pax.parseBytes(new TextEncoder().encode(str), ex, g);
117
+ }
118
+ /** Parse the body of a PAX header entry. */
119
+ static parseBytes(buf, ex, g = false) {
120
+ return new Pax(merge(parseKV(buf), ex), g);
126
121
  }
127
122
  }
128
123
  const merge = (a, b) => b ? Object.assign({}, b, a) : a;
129
- const parseKV = (str) => str
130
- .replace(/\n$/, '')
131
- .split('\n')
132
- .reduce(parseKVLine, Object.create(null));
133
- const parseKVLine = (set, line) => {
134
- const n = parseInt(line, 10);
135
- // XXX Values with \n in them will fail this.
136
- // Refactor to not be a naive line-by-line parse.
137
- if (n !== Buffer.byteLength(line) + 1) {
138
- return set;
124
+ // Each record is "<len> <key>=<value>\n". <len> is the byte length of
125
+ // the whole record. A value can contain a newline, so use the length
126
+ // to find the end of the record. Stop at the first malformed record.
127
+ const parseKV = (buf) => {
128
+ const set = Object.create(null);
129
+ let pos = 0;
130
+ while (pos < buf.length) {
131
+ let sp = pos;
132
+ while (sp < buf.length &&
133
+ buf[sp] >= 0x30 &&
134
+ buf[sp] <= 0x39) {
135
+ sp++;
136
+ }
137
+ if (sp === pos || buf[sp] !== 0x20)
138
+ break;
139
+ const n = Number(decodeUtf8(buf, pos, sp));
140
+ const end = pos + n;
141
+ if (end <= sp + 1 || end > buf.length || buf[end - 1] !== 0x0a)
142
+ break;
143
+ parseKVLine(set, decodeUtf8(buf, sp + 1, end - 1));
144
+ pos = end;
139
145
  }
140
- line = line.slice((n + ' ').length);
146
+ return set;
147
+ };
148
+ const parseKVLine = (set, line) => {
141
149
  const kv = line.split('=');
142
150
  const r = kv.shift();
143
151
  if (!r) {
144
152
  return set;
145
153
  }
146
154
  const k = r.replace(/^SCHILY\.(dev|ino|nlink)/, '$1');
147
- const v = kv.join('=').replace(/\0.*/, '');
155
+ // The value ends at the first NUL.
156
+ const joined = kv.join('=');
157
+ const nul = joined.indexOf('\0');
158
+ const v = nul === -1 ? joined : joined.slice(0, nul);
148
159
  switch (k) {
149
160
  case 'path':
150
161
  case 'linkpath':
@@ -158,25 +169,24 @@ const parseKVLine = (set, line) => {
158
169
  case 'ctime':
159
170
  case 'atime':
160
171
  case 'mtime':
161
- set[k] = new Date(Number(v) * 1000);
172
+ const d = new Date(Number(v) * 1000);
173
+ if (!isNaN(d.getTime()))
174
+ set[k] = d;
162
175
  break;
163
176
  case 'size':
164
- // value must be a string of digits with no negative sign
165
- if (!SIMPLE_NUMBER.test(v.trim()))
166
- break;
167
- const s = +v;
168
- // must be a finite, non-negative integer, like the binary header path
169
- if (!Number.isSafeInteger(s))
170
- break;
171
- set[k] = s;
172
- break;
173
177
  case 'gid':
174
178
  case 'uid':
175
179
  case 'dev':
176
180
  case 'ino':
177
181
  case 'nlink':
178
182
  case 'mode':
179
- set[k] = +v;
183
+ // value must be a string of decimal digits
184
+ if (!SIMPLE_NUMBER.test(v.trim()))
185
+ break;
186
+ const s = +v;
187
+ if (!Number.isSafeInteger(s))
188
+ break;
189
+ set[k] = s;
180
190
  break;
181
191
  }
182
192
  return set;