tar 7.5.23 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (330) hide show
  1. package/README.md +1705 -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 -37
  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 +135 -89
  119. package/dist/commonjs/unpack.d.ts.map +1 -1
  120. package/dist/commonjs/unpack.js +760 -727
  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 -36
  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 +135 -89
  259. package/dist/esm/unpack.d.ts.map +1 -1
  260. package/dist/esm/unpack.js +756 -721
  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
package/dist/esm/pack.js CHANGED
@@ -1,502 +1,811 @@
1
- // A readable tar stream creator
2
- // Technically, this is a transform stream that you write paths into,
3
- // and tar format comes out of.
4
- // The `add()` method is like `write()` but returns this,
5
- // and end() return `this` as well, so you can
6
- // do `new Pack(opt).add('files').add('dir').end().pipe(output)
7
- // You could also do something like:
8
- // streamOfPaths().pipe(new Pack()).pipe(new fs.WriteStream('out.tar'))
9
- import fs from 'fs';
10
- import { WriteEntry, WriteEntrySync, WriteEntryTar, } from './write-entry.js';
1
+ import { isBlob, isReadableStream, isUint8Array } from './bytes.js';
2
+ import { modeFix } from './mode-fix.js';
3
+ import * as winchars from './winchars.js';
4
+ import { gzip } from './codec.js';
5
+ import { concat } from './concat.js';
6
+ import { countOption, ownOptions, } from './options.js';
7
+ import { isAbsolute, relative, resolve } from './path.js';
8
+ import { stripAbsolutePath } from './strip-absolute-path.js';
9
+ import { toStream } from './to-stream.js';
10
+ import { warn } from './warn.js';
11
+ import { win32IsAbsolute, win32Resolve } from './win32-path.js';
12
+ import { WriteEntry } from './write-entry.js';
13
+ const MAX_READ = 16 * 1024 * 1024;
14
+ const noop = () => { };
15
+ const blockLength = (size) => 512 * Math.ceil(size / 512);
16
+ const slashes = (path) => path.replaceAll('\\', '/');
17
+ // The identity of a file: a key for a `Map`, and a value to compare.
18
+ // The key of two `bigint` values is the key of the same two numbers.
19
+ const idOf = (id) => `${id.dev}:${id.ino}`;
20
+ const BIG = { bigint: true };
21
+ // Node.js gives `dev` and `ino` as numbers. A value that is not a
22
+ // safe integer can be the value of more than one file. On NTFS, most
23
+ // `ino` values are not safe integers.
24
+ const lossy = ({ dev, ino }) => !Number.isSafeInteger(dev) || !Number.isSafeInteger(ino);
25
+ // True if the path is still the object of its stat. The numbers are
26
+ // enough here: one object always gives the same numbers, so numbers
27
+ // that are not exact give no false alarm, and need no second stat.
28
+ const same = (a, b) => a.dev === b.dev && a.ino === b.ino;
29
+ const eof = (message, path) => Object.assign(new Error(message), { path, syscall: 'read', code: 'EOF' });
30
+ // The file does not have the number of bytes that its stat gave.
31
+ const fewer = (path, size) => eof(`encountered unexpected EOF: ${path} has fewer than the ${size} bytes of its stat`, path);
32
+ const more = (path, size) => eof(`did not encounter expected EOF: ${path} has more than the ${size} bytes of its stat`, path);
33
+ const pathOf = (entry) => entry.absolute ?? entry.path;
34
+ const tooLong = (entry) => Object.assign(new Error('writing more data than expected'), {
35
+ path: pathOf(entry),
36
+ });
37
+ // The package has more than one copy of each class: the entry points
38
+ // `tar` and `tar/core` are separate bundles, and each has an ESM and
39
+ // a CommonJS build. `instanceof` is false for an entry of a different
40
+ // copy, so these checks use the shape of the item. A `PackJob` is
41
+ // always of this copy, because only the pack makes it.
42
+ // A source of entries is an iterable. An entry is not.
43
+ const isSource = (item) => Symbol.asyncIterator in item || Symbol.iterator in item;
44
+ // A `WriteEntry` can encode its header. An entry of an archive
45
+ // cannot.
46
+ const isWriteEntry = (item) => typeof item.encode === 'function';
47
+ /** A path in the queue of a pack. */
11
48
  export class PackJob {
12
49
  path;
13
50
  absolute;
14
- entry;
15
- stat;
16
- readdir;
17
- pending = false;
18
- pendingLink = false;
19
- ignore = false;
20
- piped = false;
51
+ /** The directories above it, when the pack follows symbolic links. */
52
+ up;
53
+ /** The target, when the pack writes the file as a hard link. */
54
+ linkpath;
55
+ /** Set when `Pack` starts to read the path. */
56
+ ready;
21
57
  constructor(path, absolute) {
22
- this.path = path || './';
58
+ this.path = path;
23
59
  this.absolute = absolute;
24
60
  }
25
61
  }
26
- import { Minipass } from 'minipass';
27
- import * as zlib from 'minizlib';
28
- import { Yallist } from 'yallist';
29
- import { warnMethod } from './warn-method.js';
30
- const EOF = Buffer.alloc(1024);
31
- const ONSTAT = Symbol('onStat');
32
- const ENDED = Symbol('ended');
33
- const QUEUE = Symbol('queue');
34
- const PENDINGLINKS = Symbol('pendingLinks');
35
- const CURRENT = Symbol('current');
36
- const PROCESS = Symbol('process');
37
- const PROCESSING = Symbol('processing');
38
- const PROCESSJOB = Symbol('processJob');
39
- const JOBS = Symbol('jobs');
40
- const JOBDONE = Symbol('jobDone');
41
- const ADDFSENTRY = Symbol('addFSEntry');
42
- const ADDTARENTRY = Symbol('addTarEntry');
43
- const STAT = Symbol('stat');
44
- const READDIR = Symbol('readdir');
45
- const ONREADDIR = Symbol('onreaddir');
46
- const PIPE = Symbol('pipe');
47
- const ENTRY = Symbol('entry');
48
- const ENTRYOPT = Symbol('entryOpt');
49
- const WRITEENTRYCLASS = Symbol('writeEntryClass');
50
- const WRITE = Symbol('write');
51
- const ONDRAIN = Symbol('ondrain');
52
- import path from 'path';
53
- import { normalizeWindowsPath } from './normalize-windows-path.js';
54
- export class Pack extends Minipass {
55
- sync = false;
62
+ /**
63
+ * The parts of `Pack` and `PackSync` that are the same: the options,
64
+ * the queue, and the rules that make an entry from a path or from an
65
+ * entry of another archive.
66
+ */
67
+ export class PackBase {
56
68
  opt;
69
+ fs;
70
+ /** The normalized form of `opt.cwd`. */
57
71
  cwd;
72
+ win32;
58
73
  maxReadSize;
59
- preservePaths;
60
- strict;
61
- noPax;
62
- prefix;
63
- linkCache;
64
- statCache;
65
- file;
66
- portable;
67
- zip;
68
- readdirCache;
69
- noDirRecurse;
70
- follow;
71
- noMtime;
72
- mtime;
73
- filter;
74
- jobs;
75
- [WRITEENTRYCLASS];
76
- onWriteEntry;
77
- // Note: we actually DO need a linked list here, because we
78
- // shift() to update the head of the list where we start, but still
79
- // while that happens, need to know what the next item in the queue
80
- // will be. Since we do multiple jobs in parallel, it's not as simple
81
- // as just an Array.shift(), since that would lose the information about
82
- // the next job in the list. We could add a .next field on the PackJob
83
- // class, but then we'd have to be tracking the tail of the queue the
84
- // whole time, and Yallist just does that for us anyway.
85
- [QUEUE];
86
- [PENDINGLINKS] = new Map();
87
- [JOBS] = 0;
88
- [PROCESSING] = false;
89
- [ENDED] = false;
90
- constructor(opt = {}) {
91
- super();
92
- this.opt = opt;
93
- this.file = opt.file || '';
94
- this.cwd = opt.cwd || process.cwd();
95
- this.maxReadSize = opt.maxReadSize;
96
- this.preservePaths = !!opt.preservePaths;
97
- this.strict = !!opt.strict;
98
- this.noPax = !!opt.noPax;
99
- this.prefix = normalizeWindowsPath(opt.prefix || '');
100
- this.linkCache = opt.linkCache || new Map();
101
- this.statCache = opt.statCache || new Map();
102
- this.readdirCache = opt.readdirCache || new Map();
103
- this.onWriteEntry = opt.onWriteEntry;
104
- this[WRITEENTRYCLASS] = WriteEntry;
105
- if (typeof opt.onwarn === 'function') {
106
- this.on('warn', opt.onwarn);
107
- }
108
- this.portable = !!opt.portable;
109
- if (opt.gzip || opt.brotli || opt.zstd) {
110
- if ((opt.gzip ? 1 : 0) + (opt.brotli ? 1 : 0) + (opt.zstd ? 1 : 0) >
111
- 1) {
112
- throw new TypeError('gzip, brotli, zstd are mutually exclusive');
113
- }
114
- if (opt.gzip) {
115
- if (typeof opt.gzip !== 'object') {
116
- opt.gzip = {};
117
- }
118
- if (this.portable) {
119
- opt.gzip.portable = true;
120
- }
121
- this.zip = new zlib.Gzip(opt.gzip);
122
- }
123
- if (opt.brotli) {
124
- if (typeof opt.brotli !== 'object') {
125
- opt.brotli = {};
126
- }
127
- this.zip = new zlib.BrotliCompress(opt.brotli);
128
- }
129
- if (opt.zstd) {
130
- if (typeof opt.zstd !== 'object') {
131
- opt.zstd = {};
132
- }
133
- this.zip = new zlib.ZstdCompress(opt.zstd);
74
+ /** The options of `WriteEntry.encode`. */
75
+ header;
76
+ /** The compression of the output. */
77
+ codec;
78
+ codecOptions;
79
+ /** True after `end()`. */
80
+ ended = false;
81
+ head;
82
+ tail;
83
+ wake;
84
+ // The first path of each file that has more than one link.
85
+ #links = new Map();
86
+ constructor(opt, compress) {
87
+ opt = this.opt = ownOptions(opt);
88
+ this.fs = opt.fs;
89
+ this.win32 = opt.platform === 'win32';
90
+ this.maxReadSize = countOption('maxReadSize', opt.maxReadSize, MAX_READ);
91
+ const { prefix, noPax } = opt;
92
+ this.header = {
93
+ prefix: prefix && this.win32 ? slashes(prefix) : prefix,
94
+ noPax,
95
+ };
96
+ const cwd = opt.cwd && (this.win32 ? slashes(opt.cwd) : opt.cwd);
97
+ // This code has no working directory to make a path absolute.
98
+ if (cwd && !(this.win32 ? win32IsAbsolute : isAbsolute)(cwd)) {
99
+ throw new TypeError('cwd must be an absolute path');
100
+ }
101
+ this.cwd = cwd && this.#resolve(cwd, '');
102
+ const set = opt;
103
+ const names = ['gzip', 'brotli', 'zstd', 'compress'].filter(name => set[name]);
104
+ if (names.length > 1) {
105
+ throw new TypeError(`${names.join(', ')} are mutually exclusive`);
106
+ }
107
+ const [name] = names;
108
+ if (name) {
109
+ const custom = opt.compress;
110
+ // The named options use a codec of `codecs`, or the gzip of
111
+ // the runtime. The `compress` option has its own codec.
112
+ const [codec, value] = !custom ?
113
+ [
114
+ [...(opt.codecs ?? []), gzip].find(c => c.name === name),
115
+ set[name],
116
+ ]
117
+ : Array.isArray(custom) ? custom
118
+ : [custom];
119
+ if (!codec?.[compress]) {
120
+ throw new TypeError(`no codec named ${codec?.name ?? name} has a ${compress} function`);
134
121
  }
135
- /* c8 ignore next */
136
- if (!this.zip)
137
- throw new Error('impossible');
138
- const zip = this.zip;
139
- zip.on('data', chunk => super.write(chunk));
140
- zip.on('end', () => super.end());
141
- zip.on('drain', () => this[ONDRAIN]());
142
- this.on('resume', () => zip.resume());
122
+ this.codec = codec;
123
+ this.codecOptions = {
124
+ ...(typeof value === 'object' ? value : undefined),
125
+ ...(codec.name === 'gzip' && opt.portable ?
126
+ { portable: true }
127
+ : undefined),
128
+ };
143
129
  }
144
- else {
145
- this.on('drain', this[ONDRAIN]);
146
- }
147
- this.noDirRecurse = !!opt.noDirRecurse;
148
- this.follow = !!opt.follow;
149
- this.noMtime = !!opt.noMtime;
150
- if (opt.mtime)
151
- this.mtime = opt.mtime;
152
- this.filter =
153
- typeof opt.filter === 'function' ? opt.filter : () => true;
154
- this[QUEUE] = new Yallist();
155
- this[JOBS] = 0;
156
- this.jobs = Number(opt.jobs) || 4;
157
- this[PROCESSING] = false;
158
- this[ENDED] = false;
159
- }
160
- [WRITE](chunk) {
161
- return super.write(chunk);
162
- }
163
- add(path) {
164
- this.write(path);
165
- return this;
166
130
  }
167
- end(path, encoding, cb) {
168
- /* c8 ignore start */
169
- if (typeof path === 'function') {
170
- cb = path;
171
- path = undefined;
172
- }
173
- if (typeof encoding === 'function') {
174
- cb = encoding;
175
- encoding = undefined;
176
- }
177
- /* c8 ignore stop */
178
- if (path) {
179
- this.add(path);
180
- }
181
- this[ENDED] = true;
182
- this[PROCESS]();
183
- /* c8 ignore next */
184
- if (cb)
185
- cb();
186
- return this;
131
+ #resolve(cwd, path) {
132
+ return this.win32 ? win32Resolve(cwd, path) : resolve(cwd, path);
187
133
  }
188
- write(path) {
189
- if (this[ENDED]) {
190
- throw new Error('write after end');
191
- }
192
- if (typeof path === 'string') {
193
- this[ADDFSENTRY](path);
134
+ /** The absolute form of a path that is relative to `cwd`. */
135
+ resolve(path) {
136
+ if (!this.cwd || !this.fs) {
137
+ throw new TypeError('a path needs the cwd and fs options');
194
138
  }
139
+ return this.#resolve(this.cwd, this.win32 ? slashes(path) : path);
140
+ }
141
+ /**
142
+ * Put a path, an entry, or a source of entries at the end of the
143
+ * queue. A directory is added with all that is in it.
144
+ */
145
+ add(item) {
146
+ if (this.ended)
147
+ throw new Error('write after end');
148
+ if (typeof item !== 'string')
149
+ this.push(item);
195
150
  else {
196
- this[ADDTARENTRY](path);
151
+ const path = this.win32 ? slashes(item) : item;
152
+ this.push(new PackJob(path || './', this.resolve(path)));
197
153
  }
198
- return this.flowing;
154
+ return this;
199
155
  }
200
- [ADDTARENTRY](p) {
201
- const absolute = normalizeWindowsPath(path.resolve(this.cwd, p.path));
202
- // in this case, we don't have to wait for the stat
203
- if (!this.filter(p.path, p)) {
204
- p.resume();
205
- }
206
- else {
207
- const job = new PackJob(p.path, absolute);
208
- job.entry = new WriteEntryTar(p, this[ENTRYOPT](job));
209
- job.entry.on('end', () => this[JOBDONE](job));
210
- this[JOBS] += 1;
211
- this[QUEUE].push(job);
212
- }
213
- this[PROCESS]();
214
- }
215
- [ADDFSENTRY](p) {
216
- const absolute = normalizeWindowsPath(path.resolve(this.cwd, p));
217
- this[QUEUE].push(new PackJob(p, absolute));
218
- this[PROCESS]();
219
- }
220
- [STAT](job) {
221
- job.pending = true;
222
- this[JOBS] += 1;
223
- const stat = this.follow ? 'stat' : 'lstat';
224
- fs[stat](job.absolute, (er, stat) => {
225
- job.pending = false;
226
- this[JOBS] -= 1;
227
- if (er) {
228
- this.emit('error', er);
229
- }
230
- else {
231
- this[ONSTAT](job, stat);
232
- }
233
- });
156
+ /** Add no more. The archive ends after the last item of the queue. */
157
+ end() {
158
+ this.ended = true;
159
+ this.wake?.();
160
+ return this;
234
161
  }
235
- [ONSTAT](job, stat) {
236
- this.statCache.set(job.absolute, stat);
237
- job.stat = stat;
238
- // now we have the stat, we can filter it.
239
- if (!this.filter(job.path, stat)) {
240
- job.ignore = true;
241
- }
242
- else if (stat.isFile() &&
243
- stat.nlink > 1 &&
244
- !this.linkCache.get(`${stat.dev}:${stat.ino}`) &&
245
- !this.sync) {
246
- // if it's not filtered, and it's a new File entry, and next anyway
247
- // process right away in case any pending Link entries are about
248
- // to try to link to it.
249
- if (job === this[CURRENT]) {
250
- this[PROCESSJOB](job);
162
+ push(item) {
163
+ const node = { item };
164
+ if (this.tail)
165
+ this.tail.next = node;
166
+ else
167
+ this.head = node;
168
+ this.tail = node;
169
+ this.wake?.();
170
+ }
171
+ shift() {
172
+ this.head = this.head?.next;
173
+ if (!this.head)
174
+ this.tail = undefined;
175
+ }
176
+ /**
177
+ * True if the pack must read the exact `dev` and `ino` of the
178
+ * object with the `bigint` option: the numbers are not exact, and a
179
+ * wrong match loses data. That is a file with more than one link,
180
+ * which a wrong match makes a hard link to a different file, and
181
+ * with `follow` a directory, which a wrong match skips as a
182
+ * directory in itself. Each other object gets no second stat.
183
+ */
184
+ needsExact(stat) {
185
+ return (lossy(stat) &&
186
+ (stat.isDirectory() ?
187
+ !!this.opt.follow
188
+ : stat.isFile() && stat.nlink > 1));
189
+ }
190
+ /**
191
+ * The type of the entry to write for a path, or `undefined` to
192
+ * write no entry. The pack calls it one time for each path, in the
193
+ * order of the queue. `id` is the stat, or the exact `dev` and `ino`
194
+ * of the object.
195
+ */
196
+ plan(job, stat, id) {
197
+ const { filter } = this.opt;
198
+ if (filter && !filter(job.path, stat))
199
+ return;
200
+ if (stat.isDirectory()) {
201
+ for (let a = job.up; a; a = a.up) {
202
+ if (a.key !== idOf(id))
203
+ continue;
204
+ warn(this.opt, 'TAR_ENTRY_ERROR', 'directory is its own ancestor', {
205
+ path: job.path,
206
+ absolute: job.absolute,
207
+ });
208
+ return;
251
209
  }
252
- else {
253
- // if it's NOT the current entry, it needs to be deferred,
254
- // so that the link target can be built first.
255
- const key = `${stat.dev}:${stat.ino}`;
256
- const pending = this[PENDINGLINKS].get(key);
257
- if (pending)
258
- pending.push(job);
259
- else
260
- this[PENDINGLINKS].set(key, [job]);
261
- job.pendingLink = true;
262
- job.pending = true;
210
+ return 'Directory';
211
+ }
212
+ if (stat.isSymbolicLink())
213
+ return 'SymbolicLink';
214
+ if (!stat.isFile()) {
215
+ // A socket, a FIFO, or a device.
216
+ warn(this.opt, 'TAR_ENTRY_UNSUPPORTED', 'unsupported file type', {
217
+ path: job.path,
218
+ absolute: job.absolute,
219
+ });
220
+ return;
221
+ }
222
+ if (stat.nlink > 1) {
223
+ const key = idOf(id);
224
+ const target = this.#links.get(key);
225
+ const cwd = this.cwd;
226
+ // A link is only made to a path in the cwd. A directory beside
227
+ // the cwd can have a name that starts with the name of the cwd.
228
+ // A path that is added two times is a file each time: a link to
229
+ // itself cannot be extracted.
230
+ if (target !== job.absolute &&
231
+ target?.startsWith(cwd.endsWith('/') ? cwd : cwd + '/')) {
232
+ job.linkpath = relative(cwd, target);
233
+ return 'Link';
263
234
  }
235
+ this.#links.set(key, job.absolute);
264
236
  }
265
- this[PROCESS]();
237
+ return 'File';
266
238
  }
267
- [READDIR](job) {
268
- job.pending = true;
269
- this[JOBS] += 1;
270
- fs.readdir(job.absolute, (er, entries) => {
271
- job.pending = false;
272
- this[JOBS] -= 1;
273
- if (er) {
274
- return this.emit('error', er);
275
- }
276
- this[ONREADDIR](job, entries);
239
+ /** The jobs for the names in the directory of `job`. */
240
+ children(job, names, id) {
241
+ const base = job.path === './' ? '' : job.path.replace(/\/*$/, '/');
242
+ const dir = job.absolute.endsWith('/') ? job.absolute : job.absolute + '/';
243
+ // Only a pack that follows symbolic links can find a directory
244
+ // in itself.
245
+ const up = this.opt.follow ? { key: idOf(id), up: job.up } : undefined;
246
+ // Node.js sorts the names of a directory. Bun does not. Sort
247
+ // them, so that each runtime makes the same archive.
248
+ return [...names].sort().map(name => {
249
+ const child = new PackJob(base + name, dir + name);
250
+ child.up = up;
251
+ return child;
252
+ });
253
+ }
254
+ /**
255
+ * Report a path that does not exist, and skip it. Each other error
256
+ * of the stat is thrown.
257
+ */
258
+ missing(job, er) {
259
+ if (er?.code !== 'ENOENT')
260
+ throw er;
261
+ warn(this.opt, 'TAR_ENTRY_ERROR', er, {
262
+ path: job.path,
263
+ absolute: job.absolute,
277
264
  });
278
265
  }
279
- [ONREADDIR](job, entries) {
280
- this.readdirCache.set(job.absolute, entries);
281
- job.readdir = entries;
282
- this[PROCESS]();
266
+ /** Report a path that is a different object after its `lstat`. */
267
+ changed(job, type) {
268
+ warn(this.opt, 'TAR_ENTRY_ERROR', type === 'Directory' ?
269
+ 'directory changed between lstat and readdir'
270
+ : 'file changed between lstat and open', { path: job.path, absolute: job.absolute });
271
+ }
272
+ /** Make the entry for a path. */
273
+ pathEntry(job, stat, type, linkpath, body) {
274
+ const { processUid, processUname = '' } = this.opt;
275
+ const entry = new WriteEntry({
276
+ path: job.path,
277
+ type,
278
+ mode: stat.mode,
279
+ uid: stat.uid,
280
+ gid: stat.gid,
281
+ uname: stat.uid === processUid ? processUname : '',
282
+ size: type === 'File' ? stat.size : 0,
283
+ mtime: stat.mtime,
284
+ atime: stat.atime,
285
+ ctime: stat.ctime,
286
+ linkpath: linkpath && this.win32 ? slashes(linkpath) : linkpath,
287
+ dev: stat.dev,
288
+ ino: stat.ino,
289
+ nlink: stat.nlink,
290
+ }, body);
291
+ entry.absolute = job.absolute;
292
+ return this.#finish(entry, true);
283
293
  }
284
- [PROCESS]() {
285
- if (this[PROCESSING]) {
294
+ /**
295
+ * Make the entry to write for an entry that the caller added.
296
+ * `undefined` if the filter refuses an entry of another archive.
297
+ */
298
+ addedEntry(item) {
299
+ if (isWriteEntry(item))
300
+ return this.#finish(item, false);
301
+ const { filter } = this.opt;
302
+ if (filter && !filter(item.path, item))
286
303
  return;
304
+ return this.#finish(new WriteEntry(item, item.body), false);
305
+ }
306
+ // Apply the options to the fields of the entry.
307
+ #finish(entry, fromPath) {
308
+ const { opt, win32 } = this;
309
+ const dir = entry.type === 'Directory';
310
+ if (win32 && !fromPath) {
311
+ entry.path = slashes(entry.path);
312
+ if (entry.linkpath)
313
+ entry.linkpath = slashes(entry.linkpath);
314
+ }
315
+ const [root, stripped] = opt.preservePaths ? ['', entry.path] : stripAbsolutePath(entry.path);
316
+ entry.path = stripped;
317
+ if (fromPath) {
318
+ if (win32)
319
+ entry.path = winchars.decode(entry.path);
320
+ if (entry.path === '')
321
+ entry.path = './';
322
+ }
323
+ if (root) {
324
+ warn(opt, 'TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
325
+ entry,
326
+ path: root + entry.path,
327
+ });
287
328
  }
288
- this[PROCESSING] = true;
289
- for (let w = this[QUEUE].head; !!w && this[JOBS] < this.jobs; w = w.next) {
290
- this[PROCESSJOB](w.value);
291
- if (w.value.ignore) {
292
- const p = w.next;
293
- this[QUEUE].removeNode(w);
294
- w.next = p;
329
+ if (fromPath && dir && !entry.path.endsWith('/'))
330
+ entry.path += '/';
331
+ if (opt.portable) {
332
+ entry.uid =
333
+ entry.gid =
334
+ entry.dev =
335
+ entry.ino =
336
+ entry.nlink =
337
+ undefined;
338
+ entry.uname = entry.gname = undefined;
339
+ entry.atime = entry.ctime = undefined;
340
+ }
341
+ if (entry.mode !== undefined) {
342
+ entry.mode = modeFix(entry.mode, dir, !!opt.portable);
343
+ }
344
+ entry.mtime =
345
+ opt.noMtime || (opt.portable && dir) ?
346
+ undefined
347
+ : opt.mtime || entry.mtime;
348
+ return entry;
349
+ }
350
+ }
351
+ // Read from the handle into `buf` at `off`, until the buffer is full
352
+ // or the file has no more bytes. `pos` is the place in the file.
353
+ // Returns the new place.
354
+ const fill = async (handle, buf, off, pos, size, path) => {
355
+ while (off < buf.length && pos < size) {
356
+ const { bytesRead } = await handle.read(buf, off, buf.length - off, pos);
357
+ if (bytesRead <= 0)
358
+ throw fewer(path, size);
359
+ if (bytesRead > size - pos)
360
+ throw more(path, size);
361
+ off += bytesRead;
362
+ pos += bytesRead;
363
+ }
364
+ return pos;
365
+ };
366
+ const fillSync = (fs, fd, buf, off, pos, size, path) => {
367
+ while (off < buf.length && pos < size) {
368
+ const bytesRead = fs.readSync(fd, buf, off, buf.length - off, pos);
369
+ if (bytesRead <= 0)
370
+ throw fewer(path, size);
371
+ if (bytesRead > size - pos)
372
+ throw more(path, size);
373
+ off += bytesRead;
374
+ pos += bytesRead;
375
+ }
376
+ return pos;
377
+ };
378
+ // The chunks of a body, from each kind of content.
379
+ async function* chunks(body) {
380
+ if (isUint8Array(body))
381
+ yield body;
382
+ else if (isBlob(body) || isReadableStream(body)) {
383
+ const reader = (isBlob(body) ? body.stream() : body).getReader();
384
+ try {
385
+ for (;;) {
386
+ const res = await reader.read();
387
+ if (res.done)
388
+ break;
389
+ yield res.value;
295
390
  }
296
391
  }
297
- this[PROCESSING] = false;
298
- if (this[ENDED] && this[QUEUE].length === 0 && this[JOBS] === 0) {
299
- if (this.zip) {
300
- this.zip.end(EOF);
301
- }
302
- else {
303
- super.write(EOF);
304
- super.end();
392
+ finally {
393
+ await reader.cancel().catch(noop);
394
+ }
395
+ }
396
+ else if (body)
397
+ yield* body;
398
+ }
399
+ function* chunksSync(body) {
400
+ if (isUint8Array(body))
401
+ yield body;
402
+ else if (body && Symbol.iterator in body)
403
+ yield* body;
404
+ else if (body) {
405
+ throw new TypeError('PackSync needs a body that is a string, a Uint8Array, or an iterable');
406
+ }
407
+ }
408
+ // Count the bytes of a body part. Returns the bytes that are left.
409
+ const count = (entry, remain, chunk) => {
410
+ if (chunk.length > remain)
411
+ throw tooLong(entry);
412
+ return remain - chunk.length;
413
+ };
414
+ // The zeros after a body, or an error if the body was too short.
415
+ const padding = (entry, remain) => {
416
+ if (remain)
417
+ throw eof('encountered unexpected EOF', pathOf(entry));
418
+ return new Uint8Array(blockLength(entry.size) - entry.size);
419
+ };
420
+ // The body of the entry, with the zeros that fill its last block.
421
+ async function* bodyBlocks(entry) {
422
+ if (!entry.size)
423
+ return;
424
+ let remain = entry.size;
425
+ for await (const chunk of chunks(entry.body)) {
426
+ remain = count(entry, remain, chunk);
427
+ yield chunk;
428
+ }
429
+ const pad = padding(entry, remain);
430
+ if (pad.length)
431
+ yield pad;
432
+ }
433
+ function* bodyBlocksSync(entry) {
434
+ if (!entry.size)
435
+ return;
436
+ let remain = entry.size;
437
+ for (const chunk of chunksSync(entry.body)) {
438
+ remain = count(entry, remain, chunk);
439
+ yield chunk;
440
+ }
441
+ const pad = padding(entry, remain);
442
+ if (pad.length)
443
+ yield pad;
444
+ }
445
+ /**
446
+ * Makes an archive. `add()` paths and entries, then `end()`. The
447
+ * pack is the async iterator of the bytes of the archive, compressed
448
+ * if an option selects a codec.
449
+ *
450
+ * It reads the file system only when the consumer asks for bytes.
451
+ * Then it reads the first `jobs` paths of the queue at the same
452
+ * time, and writes them in the order of the queue. The entries of a
453
+ * directory go to the end of the queue.
454
+ *
455
+ * `return()` closes each file that is open.
456
+ */
457
+ export class Pack extends PackBase {
458
+ jobs;
459
+ #out;
460
+ // Settles when the pack called `plan()` for each path that it
461
+ // started to read. It keeps the calls in the order of the queue.
462
+ #planned = Promise.resolve();
463
+ constructor(opt = {}) {
464
+ super(opt, 'compress');
465
+ this.jobs = countOption('jobs', this.opt.jobs, 4);
466
+ }
467
+ async next() {
468
+ const res = await (this.#out ??= this.#output()).next();
469
+ return res.done ? { value: undefined, done: true } : res;
470
+ }
471
+ /** Stop, and close each file that is open. */
472
+ async return() {
473
+ this.end();
474
+ await this.#out?.return();
475
+ return { value: undefined, done: true };
476
+ }
477
+ [Symbol.asyncIterator]() {
478
+ return this;
479
+ }
480
+ #output() {
481
+ const { codec } = this;
482
+ return codec?.compress ?
483
+ this.#compress(codec.compress(this.codecOptions))
484
+ : this.#run();
485
+ }
486
+ async *#compress(stream) {
487
+ const run = this.#run();
488
+ // An error of the pack goes to the consumer, also when the codec
489
+ // does not pass it on: then the cancel ends the read.
490
+ let failure;
491
+ const reader = toStream({
492
+ next: () => run.next().catch(er => {
493
+ failure = { er };
494
+ void reader.cancel().catch(noop);
495
+ throw er;
496
+ }),
497
+ })
498
+ .pipeThrough(stream)
499
+ .getReader();
500
+ try {
501
+ for (;;) {
502
+ const res = await reader.read();
503
+ if (failure)
504
+ throw failure.er;
505
+ if (res.done)
506
+ break;
507
+ yield res.value;
305
508
  }
306
509
  }
510
+ finally {
511
+ await reader.cancel().catch(noop);
512
+ await run.return();
513
+ }
307
514
  }
308
- get [CURRENT]() {
309
- return this[QUEUE] && this[QUEUE].head && this[QUEUE].head.value;
310
- }
311
- [JOBDONE](job) {
312
- this[QUEUE].shift();
313
- this[JOBS] -= 1;
314
- const { stat } = job;
315
- if (stat && stat.isFile() && stat.nlink > 1) {
316
- // might be a file with pending links
317
- const key = `${stat.dev}:${stat.ino}`;
318
- const pending = this[PENDINGLINKS].get(key);
319
- if (pending) {
320
- this[PENDINGLINKS].delete(key);
321
- for (const job of pending) {
322
- job.pending = false;
323
- this[PROCESSJOB](job);
515
+ async *#run() {
516
+ try {
517
+ for (;;) {
518
+ const node = this.head;
519
+ if (!node) {
520
+ if (this.ended)
521
+ break;
522
+ await new Promise(res => (this.wake = res));
523
+ continue;
324
524
  }
525
+ this.#start();
526
+ const { item } = node;
527
+ if (item instanceof PackJob)
528
+ yield* this.#path(item);
529
+ else if (!isSource(item))
530
+ yield* this.#entry(item);
531
+ else
532
+ for await (const entry of item)
533
+ yield* this.#entry(entry);
534
+ this.shift();
535
+ }
536
+ yield new Uint8Array(1024);
537
+ }
538
+ finally {
539
+ // Close the files of the paths that the pack did not write.
540
+ for (let node = this.head; node; node = node.next) {
541
+ const { item } = node;
542
+ if (!(item instanceof PackJob) || !item.ready)
543
+ break;
544
+ const load = await item.ready.catch(noop);
545
+ await load?.handle?.close().catch(noop);
325
546
  }
326
547
  }
327
- this[PROCESS]();
328
548
  }
329
- [PROCESSJOB](job) {
330
- if (job.pending && job.pendingLink && job === this[CURRENT]) {
331
- // At least one of the links to this file are not being included
332
- // in the tarball, so we need to just proceed.
333
- job.pending = false;
334
- job.pendingLink = false;
335
- }
336
- if (job.pending) {
337
- return;
338
- }
339
- if (job.entry) {
340
- if (job === this[CURRENT] && !job.piped) {
341
- this[PIPE](job);
342
- }
343
- return;
549
+ // Start to read the paths at the start of the queue.
550
+ #start() {
551
+ const fs = this.fs;
552
+ let node = this.head;
553
+ for (let n = 0; node && n < this.jobs; n++, node = node.next) {
554
+ const job = node.item;
555
+ // An entry can change what the filter does with a later path.
556
+ if (!(job instanceof PackJob))
557
+ break;
558
+ if (job.ready)
559
+ continue;
560
+ const stat = this.#stat(fs, job.absolute);
561
+ stat.catch(noop);
562
+ const planned = this.#planned
563
+ .then(() => stat)
564
+ .then(([st, id]) => {
565
+ const type = this.plan(job, st, id);
566
+ return type && { load: this.#load(fs, job, st, id, type) };
567
+ }, er => this.missing(job, er));
568
+ this.#planned = planned.catch(noop);
569
+ job.ready = planned.then(p => p?.load);
570
+ job.ready.catch(noop);
344
571
  }
345
- if (!job.stat) {
346
- const sc = this.statCache.get(job.absolute);
347
- if (sc) {
348
- this[ONSTAT](job, sc);
572
+ }
573
+ // The stat of a path, and the identity of the object.
574
+ async #stat(fs, path) {
575
+ const { follow } = this.opt;
576
+ const stat = await (follow ? fs.stat(path) : fs.lstat(path));
577
+ const id = !this.needsExact(stat) ?
578
+ stat
579
+ : await (follow ? fs.stat(path, BIG) : fs.lstat(path, BIG));
580
+ return [stat, id];
581
+ }
582
+ async #load(fs, job, stat, id, type) {
583
+ const { absolute } = job;
584
+ const { follow, noDirRecurse } = this.opt;
585
+ const load = { stat, id, type };
586
+ if (type === 'SymbolicLink') {
587
+ load.linkpath = await fs.readlink(absolute);
588
+ }
589
+ else if (type === 'Directory' && !noDirRecurse) {
590
+ load.names = await fs.readdir(absolute);
591
+ load.changed = !follow && !same(stat, await fs.lstat(absolute));
592
+ }
593
+ else if (type === 'File' && stat.size) {
594
+ const handle = await fs.open(absolute, 'r');
595
+ try {
596
+ const { size } = stat;
597
+ const length = blockLength(size);
598
+ if (!follow && !same(stat, await handle.stat())) {
599
+ load.changed = true;
600
+ }
601
+ else if (length > this.maxReadSize)
602
+ load.handle = handle;
603
+ else {
604
+ load.buf = new Uint8Array(512 + length);
605
+ await fill(handle, load.buf, 512, 0, size, absolute);
606
+ }
349
607
  }
350
- else {
351
- this[STAT](job);
608
+ catch (er) {
609
+ await handle.close().catch(noop);
610
+ throw er;
352
611
  }
612
+ if (!load.handle)
613
+ await handle.close();
353
614
  }
354
- if (!job.stat) {
355
- return;
356
- }
357
- // filtered out!
358
- if (job.ignore) {
615
+ return load;
616
+ }
617
+ async *#path(job) {
618
+ const load = await job.ready;
619
+ if (!load)
359
620
  return;
360
- }
361
- if (!this.noDirRecurse && job.stat.isDirectory() && !job.readdir) {
362
- const rc = this.readdirCache.get(job.absolute);
363
- if (rc) {
364
- this[ONREADDIR](job, rc);
365
- }
366
- else {
367
- this[READDIR](job);
621
+ const { stat, id, type, names, buf, handle } = load;
622
+ if (load.changed)
623
+ return this.changed(job, type);
624
+ let ok = false;
625
+ try {
626
+ const body = buf ?
627
+ buf.subarray(512, 512 + stat.size)
628
+ : handle && this.#read(handle, stat.size, job.absolute);
629
+ const entry = this.pathEntry(job, stat, type, load.linkpath ?? job.linkpath, body);
630
+ if (names) {
631
+ for (const child of this.children(job, names, id)) {
632
+ this.push(child);
633
+ }
368
634
  }
369
- if (!job.readdir) {
370
- return;
635
+ await this.opt.onWriteEntry?.(entry);
636
+ // The header goes in the space before a body that is in `buf`.
637
+ const whole = buf && entry.body === body && entry.size === stat.size;
638
+ yield* entry.encode(this.header, whole ? buf : undefined);
639
+ if (!whole)
640
+ yield* bodyBlocks(entry);
641
+ ok = true;
642
+ }
643
+ finally {
644
+ if (handle) {
645
+ load.handle = undefined;
646
+ const closed = handle.close();
647
+ await (ok ? closed : closed.catch(noop));
371
648
  }
372
649
  }
373
- // we know it doesn't have an entry, because that got checked above
374
- job.entry = this[ENTRY](job);
375
- if (!job.entry) {
376
- job.ignore = true;
377
- return;
378
- }
379
- if (job === this[CURRENT] && !job.piped) {
380
- this[PIPE](job);
650
+ }
651
+ // The chunks of a file that is larger than `maxReadSize`.
652
+ async *#read(handle, size, path) {
653
+ const length = blockLength(size);
654
+ for (let pos = 0; pos < size;) {
655
+ // Ask for the bytes of the last block also, to find a file that
656
+ // is larger now.
657
+ const buf = new Uint8Array(Math.min(length - pos, this.maxReadSize));
658
+ const end = await fill(handle, buf, 0, pos, size, path);
659
+ yield buf.subarray(0, end - pos);
660
+ pos = end;
381
661
  }
382
662
  }
383
- [ENTRYOPT](job) {
384
- return {
385
- onwarn: (code, msg, data) => this.warn(code, msg, data),
386
- noPax: this.noPax,
387
- cwd: this.cwd,
388
- absolute: job.absolute,
389
- preservePaths: this.preservePaths,
390
- maxReadSize: this.maxReadSize,
391
- strict: this.strict,
392
- portable: this.portable,
393
- linkCache: this.linkCache,
394
- statCache: this.statCache,
395
- noMtime: this.noMtime,
396
- mtime: this.mtime,
397
- prefix: this.prefix,
398
- onWriteEntry: this.onWriteEntry,
399
- };
663
+ async *#entry(item) {
664
+ const entry = this.addedEntry(item);
665
+ if (!entry)
666
+ return;
667
+ await this.opt.onWriteEntry?.(entry);
668
+ yield* entry.encode(this.header);
669
+ yield* bodyBlocks(entry);
670
+ }
671
+ }
672
+ /**
673
+ * Makes an archive with no promise. `add()` paths and entries. The
674
+ * pack is the iterator of the bytes of the archive. It reads the
675
+ * file system only when the consumer asks for bytes, one path at a
676
+ * time. The entries of a directory come before the next item that
677
+ * the caller added.
678
+ *
679
+ * The pack ends when the iterator comes to the end of the queue.
680
+ * With compression, the iterator gives all the archive in one chunk.
681
+ */
682
+ export class PackSync extends PackBase {
683
+ #out;
684
+ constructor(opt = {}) {
685
+ super(opt, 'compressSync');
686
+ }
687
+ next() {
688
+ const res = (this.#out ??= this.#output()).next();
689
+ return res.done ? { value: undefined, done: true } : res;
690
+ }
691
+ /** Stop, and close the file that is open. */
692
+ return() {
693
+ this.ended = true;
694
+ this.#out?.return();
695
+ return { value: undefined, done: true };
400
696
  }
401
- [ENTRY](job) {
402
- this[JOBS] += 1;
697
+ [Symbol.iterator]() {
698
+ return this;
699
+ }
700
+ *#output() {
701
+ const { codec } = this;
702
+ if (!codec?.compressSync)
703
+ return yield* this.#run();
704
+ yield codec.compressSync(concat([...this.#run()]), this.codecOptions);
705
+ }
706
+ *#run() {
707
+ for (let node; (node = this.head); this.shift()) {
708
+ const { item } = node;
709
+ if (item instanceof PackJob) {
710
+ const jobs = [item];
711
+ for (const job of jobs)
712
+ yield* this.#path(job, jobs);
713
+ }
714
+ else if (!isSource(item))
715
+ yield* this.#entry(item);
716
+ else
717
+ for (const entry of item)
718
+ yield* this.#entry(entry);
719
+ }
720
+ this.ended = true;
721
+ yield new Uint8Array(1024);
722
+ }
723
+ *#path(job, jobs) {
724
+ const fs = this.fs;
725
+ const { absolute } = job;
726
+ const { follow, noDirRecurse } = this.opt;
727
+ let stat;
403
728
  try {
404
- const e = new this[WRITEENTRYCLASS](job.path, this[ENTRYOPT](job));
405
- return e
406
- .on('end', () => this[JOBDONE](job))
407
- .on('error', er => this.emit('error', er));
729
+ stat = follow ? fs.statSync(absolute) : fs.lstatSync(absolute);
408
730
  }
409
731
  catch (er) {
410
- this.emit('error', er);
411
- }
412
- }
413
- [ONDRAIN]() {
414
- if (this[CURRENT] && this[CURRENT].entry) {
415
- this[CURRENT].entry.resume();
416
- }
417
- }
418
- // like .pipe() but using super, because our write() is special
419
- [PIPE](job) {
420
- job.piped = true;
421
- if (job.readdir) {
422
- job.readdir.forEach(entry => {
423
- const p = job.path;
424
- const base = p === './' ? '' : p.replace(/\/*$/, '/');
425
- this[ADDFSENTRY](base + entry);
426
- });
732
+ return this.missing(job, er);
427
733
  }
428
- const source = job.entry;
429
- const zip = this.zip;
430
- /* c8 ignore start */
431
- if (!source)
432
- throw new Error('cannot pipe without source');
433
- /* c8 ignore stop */
434
- if (zip) {
435
- source.on('data', chunk => {
436
- if (!zip.write(chunk)) {
437
- source.pause();
734
+ const id = !this.needsExact(stat) ? stat
735
+ : follow ? fs.statSync(absolute, BIG)
736
+ : fs.lstatSync(absolute, BIG);
737
+ const type = this.plan(job, stat, id);
738
+ if (!type)
739
+ return;
740
+ let { linkpath } = job;
741
+ let buf;
742
+ let body;
743
+ let fd;
744
+ let ok = false;
745
+ try {
746
+ if (type === 'SymbolicLink')
747
+ linkpath = fs.readlinkSync(absolute);
748
+ else if (type === 'Directory' && !noDirRecurse) {
749
+ const names = fs.readdirSync(absolute);
750
+ if (!follow && !same(stat, fs.lstatSync(absolute))) {
751
+ return this.changed(job, type);
438
752
  }
439
- });
440
- }
441
- else {
442
- source.on('data', chunk => {
443
- if (!super.write(chunk)) {
444
- source.pause();
753
+ jobs.push(...this.children(job, names, id));
754
+ }
755
+ else if (type === 'File' && stat.size) {
756
+ const { size } = stat;
757
+ const length = blockLength(size);
758
+ fd = fs.openSync(absolute, 'r');
759
+ if (!follow && !same(stat, fs.fstatSync(fd))) {
760
+ return this.changed(job, type);
445
761
  }
446
- });
762
+ if (length > this.maxReadSize) {
763
+ body = this.#read(fs, fd, size, absolute);
764
+ }
765
+ else {
766
+ buf = new Uint8Array(512 + length);
767
+ fillSync(fs, fd, buf, 512, 0, size, absolute);
768
+ body = buf.subarray(512, 512 + size);
769
+ }
770
+ }
771
+ const entry = this.pathEntry(job, stat, type, linkpath, body);
772
+ this.opt.onWriteEntry?.(entry);
773
+ // The header goes in the space before a body that is in `buf`.
774
+ const whole = buf && entry.body === body && entry.size === stat.size;
775
+ yield* entry.encode(this.header, whole ? buf : undefined);
776
+ if (!whole)
777
+ yield* bodyBlocksSync(entry);
778
+ ok = true;
779
+ }
780
+ finally {
781
+ // An error of close must not hide the first error.
782
+ if (fd !== undefined && !ok) {
783
+ try {
784
+ fs.closeSync(fd);
785
+ }
786
+ catch { }
787
+ }
447
788
  }
789
+ if (fd !== undefined)
790
+ fs.closeSync(fd);
448
791
  }
449
- pause() {
450
- if (this.zip) {
451
- this.zip.pause();
792
+ // The chunks of a file that is larger than `maxReadSize`.
793
+ *#read(fs, fd, size, path) {
794
+ const length = blockLength(size);
795
+ for (let pos = 0; pos < size;) {
796
+ const buf = new Uint8Array(Math.min(length - pos, this.maxReadSize));
797
+ const end = fillSync(fs, fd, buf, 0, pos, size, path);
798
+ yield buf.subarray(0, end - pos);
799
+ pos = end;
452
800
  }
453
- return super.pause();
454
801
  }
455
- warn(code, message, data = {}) {
456
- warnMethod(this, code, message, data);
457
- }
458
- }
459
- export class PackSync extends Pack {
460
- sync = true;
461
- constructor(opt) {
462
- super(opt);
463
- this[WRITEENTRYCLASS] = WriteEntrySync;
464
- }
465
- // pause/resume are no-ops in sync streams.
466
- pause() { }
467
- resume() { }
468
- [STAT](job) {
469
- const stat = this.follow ? 'statSync' : 'lstatSync';
470
- this[ONSTAT](job, fs[stat](job.absolute));
471
- }
472
- [READDIR](job) {
473
- this[ONREADDIR](job, fs.readdirSync(job.absolute));
474
- }
475
- // gotta get it all in this tick
476
- [PIPE](job) {
477
- const source = job.entry;
478
- const zip = this.zip;
479
- if (job.readdir) {
480
- job.readdir.forEach(entry => {
481
- const p = job.path;
482
- const base = p === './' ? '' : p.replace(/\/*$/, '/');
483
- this[ADDFSENTRY](base + entry);
484
- });
485
- }
486
- /* c8 ignore start */
487
- if (!source)
488
- throw new Error('Cannot pipe without source');
489
- /* c8 ignore stop */
490
- if (zip) {
491
- source.on('data', chunk => {
492
- zip.write(chunk);
493
- });
494
- }
495
- else {
496
- source.on('data', chunk => {
497
- super[WRITE](chunk);
498
- });
499
- }
802
+ *#entry(item) {
803
+ const entry = this.addedEntry(item);
804
+ if (!entry)
805
+ return;
806
+ this.opt.onWriteEntry?.(entry);
807
+ yield* entry.encode(this.header);
808
+ yield* bodyBlocksSync(entry);
500
809
  }
501
810
  }
502
811
  //# sourceMappingURL=pack.js.map