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