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
@@ -0,0 +1,22 @@
1
+ import type { TarFS, TarFSSync } from './fs.js';
2
+ /**
3
+ * The key of the method of a chunk source that skips input. A
4
+ * `TarReader` calls it with the number of bytes of a body that are
5
+ * after the chunks given so far, when no consumer reads that body.
6
+ * If it returns true, the next chunk starts after those bytes.
7
+ */
8
+ export declare const SKIP: unique symbol;
9
+ /** A chunk source that can skip input. */
10
+ export type Skip = {
11
+ [SKIP](n: number): boolean;
12
+ };
13
+ /**
14
+ * Read a file in chunks of `maxReadSize` bytes at most. The default,
15
+ * and the value for 0, is 16 MiB. Each chunk is a new array. The
16
+ * first chunk is small, and the chunks grow. A reader can skip a
17
+ * large part of the file.
18
+ */
19
+ export declare const readFile: (fs: TarFS, file: string, maxReadSize?: number) => AsyncGenerator<Uint8Array, void, void> & Skip;
20
+ /** The sync form of `readFile`. */
21
+ export declare const readFileSync: (fs: TarFSSync, file: string, maxReadSize?: number) => Generator<Uint8Array, void, void> & Skip;
22
+ //# sourceMappingURL=read-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../../src/read-file.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAS/C;;;;;GAKG;AACH,eAAO,MAAM,IAAI,EAAE,OAAO,MAAwC,CAAA;AAElE,0CAA0C;AAC1C,MAAM,MAAM,IAAI,GAAG;IAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAA;AA4EjD;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GACnB,IAAI,KAAK,EACT,MAAM,MAAM,EACZ,cAAc,MAAM,KACnB,cAAc,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,IAK3C,CAAA;AAED,mCAAmC;AACnC,eAAO,MAAM,YAAY,GACvB,IAAI,SAAS,EACb,MAAM,MAAM,EACZ,cAAc,MAAM,KACnB,SAAS,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,IAKtC,CAAA"}
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readFileSync = exports.readFile = exports.SKIP = void 0;
4
+ const options_js_1 = require("./options.js");
5
+ const MAX_READ = 16 * 1024 * 1024;
6
+ // The size of the first read, and of the read after a skip.
7
+ const FIRST_READ = 64 * 1024;
8
+ // A skip of fewer bytes than this is slower than a read of them.
9
+ const MIN_SKIP = 1024 * 1024;
10
+ /**
11
+ * The key of the method of a chunk source that skips input. A
12
+ * `TarReader` calls it with the number of bytes of a body that are
13
+ * after the chunks given so far, when no consumer reads that body.
14
+ * If it returns true, the next chunk starts after those bytes.
15
+ */
16
+ exports.SKIP = Symbol.for('tar.readFile.skip');
17
+ // The place and the size of the next read of a file.
18
+ class Plan {
19
+ pos = 0;
20
+ /** The size of the file. 0 before the file is open. */
21
+ size = 0;
22
+ #max;
23
+ #want = FIRST_READ;
24
+ constructor(maxReadSize) {
25
+ this.#max = (0, options_js_1.countOption)('maxReadSize', maxReadSize, MAX_READ);
26
+ }
27
+ // The number of bytes to read at `pos`. 0 at the end of the file.
28
+ // Each read is two times the size of the read before it, so that
29
+ // an archive of small entries is read in few reads.
30
+ next() {
31
+ const n = Math.min(this.#want, this.#max, this.size - this.pos);
32
+ this.#want *= 2;
33
+ return n;
34
+ }
35
+ // Skip only inside the file: the parser must see where a truncated
36
+ // file ends. The read after a skip is small, because a header
37
+ // comes next, and the body after it can be skipped also.
38
+ skip(n) {
39
+ if (n < MIN_SKIP || this.pos + n > this.size)
40
+ return false;
41
+ this.pos += n;
42
+ this.#want = FIRST_READ;
43
+ return true;
44
+ }
45
+ }
46
+ async function* chunks(fs, file, plan) {
47
+ const handle = await fs.open(file, 'r');
48
+ try {
49
+ plan.size = (await handle.stat()).size;
50
+ for (let n; (n = plan.next());) {
51
+ const buf = new Uint8Array(n);
52
+ const { bytesRead } = await handle.read(buf, 0, n, plan.pos);
53
+ if (!bytesRead)
54
+ break;
55
+ plan.pos += bytesRead;
56
+ yield bytesRead < n ? buf.subarray(0, bytesRead) : buf;
57
+ }
58
+ }
59
+ finally {
60
+ await handle.close().catch(() => { });
61
+ }
62
+ }
63
+ function* chunksSync(fs, file, plan) {
64
+ const fd = fs.openSync(file, 'r');
65
+ try {
66
+ plan.size = fs.fstatSync(fd).size;
67
+ for (let n; (n = plan.next());) {
68
+ const buf = new Uint8Array(n);
69
+ const bytesRead = fs.readSync(fd, buf, 0, n, plan.pos);
70
+ if (!bytesRead)
71
+ break;
72
+ plan.pos += bytesRead;
73
+ yield bytesRead < n ? buf.subarray(0, bytesRead) : buf;
74
+ }
75
+ }
76
+ finally {
77
+ try {
78
+ fs.closeSync(fd);
79
+ }
80
+ catch { }
81
+ }
82
+ }
83
+ /**
84
+ * Read a file in chunks of `maxReadSize` bytes at most. The default,
85
+ * and the value for 0, is 16 MiB. Each chunk is a new array. The
86
+ * first chunk is small, and the chunks grow. A reader can skip a
87
+ * large part of the file.
88
+ */
89
+ const readFile = (fs, file, maxReadSize) => {
90
+ const plan = new Plan(maxReadSize);
91
+ return Object.assign(chunks(fs, file, plan), {
92
+ [exports.SKIP]: (n) => plan.skip(n),
93
+ });
94
+ };
95
+ exports.readFile = readFile;
96
+ /** The sync form of `readFile`. */
97
+ const readFileSync = (fs, file, maxReadSize) => {
98
+ const plan = new Plan(maxReadSize);
99
+ return Object.assign(chunksSync(fs, file, plan), {
100
+ [exports.SKIP]: (n) => plan.skip(n),
101
+ });
102
+ };
103
+ exports.readFileSync = readFileSync;
104
+ //# sourceMappingURL=read-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-file.js","sourceRoot":"","sources":["../../src/read-file.ts"],"names":[],"mappings":";;;AACA,6CAA0C;AAE1C,MAAM,QAAQ,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAA;AACjC,4DAA4D;AAC5D,MAAM,UAAU,GAAG,EAAE,GAAG,IAAI,CAAA;AAC5B,iEAAiE;AACjE,MAAM,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAA;AAE5B;;;;;GAKG;AACU,QAAA,IAAI,GAAkB,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;AAKlE,qDAAqD;AACrD,MAAM,IAAI;IACR,GAAG,GAAW,CAAC,CAAA;IACf,uDAAuD;IACvD,IAAI,GAAW,CAAC,CAAA;IAChB,IAAI,CAAQ;IACZ,KAAK,GAAW,UAAU,CAAA;IAE1B,YAAY,WAA+B;QACzC,IAAI,CAAC,IAAI,GAAG,IAAA,wBAAW,EAAC,aAAa,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAA;IAC/D,CAAC;IAED,kEAAkE;IAClE,iEAAiE;IACjE,oDAAoD;IACpD,IAAI;QACF,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAA;QAC/D,IAAI,CAAC,KAAK,IAAI,CAAC,CAAA;QACf,OAAO,CAAC,CAAA;IACV,CAAC;IAED,mEAAmE;IACnE,8DAA8D;IAC9D,yDAAyD;IACzD,IAAI,CAAC,CAAS;QACZ,IAAI,CAAC,GAAG,QAAQ,IAAI,IAAI,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAA;QAC1D,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;QACb,IAAI,CAAC,KAAK,GAAG,UAAU,CAAA;QACvB,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,KAAK,SAAS,CAAC,CAAC,MAAM,CACpB,EAAS,EACT,IAAY,EACZ,IAAU;IAEV,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACvC,IAAI,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAA;QACtC,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAI,CAAC;YAChC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;YAC7B,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;YAC5D,IAAI,CAAC,SAAS;gBAAE,MAAK;YACrB,IAAI,CAAC,GAAG,IAAI,SAAS,CAAA;YACrB,MAAM,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;QACxD,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACtC,CAAC;AACH,CAAC;AAED,QAAQ,CAAC,CAAC,UAAU,CAClB,EAAa,EACb,IAAY,EACZ,IAAU;IAEV,MAAM,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACjC,IAAI,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,IAAI,CAAA;QACjC,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,GAAI,CAAC;YAChC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;YAC7B,MAAM,SAAS,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;YACtD,IAAI,CAAC,SAAS;gBAAE,MAAK;YACrB,IAAI,CAAC,GAAG,IAAI,SAAS,CAAA;YACrB,MAAM,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;QACxD,CAAC;IACH,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;QAClB,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACI,MAAM,QAAQ,GAAG,CACtB,EAAS,EACT,IAAY,EACZ,WAAoB,EAC2B,EAAE;IACjD,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,CAAA;IAClC,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE;QAC3C,CAAC,YAAI,CAAC,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;KACpC,CAAC,CAAA;AACJ,CAAC,CAAA;AATY,QAAA,QAAQ,YASpB;AAED,mCAAmC;AAC5B,MAAM,YAAY,GAAG,CAC1B,EAAa,EACb,IAAY,EACZ,WAAoB,EACsB,EAAE;IAC5C,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,CAAA;IAClC,OAAO,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE;QAC/C,CAAC,YAAI,CAAC,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;KACpC,CAAC,CAAA;AACJ,CAAC,CAAA;AATY,QAAA,YAAY,gBASxB","sourcesContent":["import type { TarFS, TarFSSync } from './fs.js'\nimport { countOption } from './options.js'\n\nconst MAX_READ = 16 * 1024 * 1024\n// The size of the first read, and of the read after a skip.\nconst FIRST_READ = 64 * 1024\n// A skip of fewer bytes than this is slower than a read of them.\nconst MIN_SKIP = 1024 * 1024\n\n/**\n * The key of the method of a chunk source that skips input. A\n * `TarReader` calls it with the number of bytes of a body that are\n * after the chunks given so far, when no consumer reads that body.\n * If it returns true, the next chunk starts after those bytes.\n */\nexport const SKIP: unique symbol = Symbol.for('tar.readFile.skip')\n\n/** A chunk source that can skip input. */\nexport type Skip = { [SKIP](n: number): boolean }\n\n// The place and the size of the next read of a file.\nclass Plan {\n pos: number = 0\n /** The size of the file. 0 before the file is open. */\n size: number = 0\n #max: number\n #want: number = FIRST_READ\n\n constructor(maxReadSize: number | undefined) {\n this.#max = countOption('maxReadSize', maxReadSize, MAX_READ)\n }\n\n // The number of bytes to read at `pos`. 0 at the end of the file.\n // Each read is two times the size of the read before it, so that\n // an archive of small entries is read in few reads.\n next(): number {\n const n = Math.min(this.#want, this.#max, this.size - this.pos)\n this.#want *= 2\n return n\n }\n\n // Skip only inside the file: the parser must see where a truncated\n // file ends. The read after a skip is small, because a header\n // comes next, and the body after it can be skipped also.\n skip(n: number): boolean {\n if (n < MIN_SKIP || this.pos + n > this.size) return false\n this.pos += n\n this.#want = FIRST_READ\n return true\n }\n}\n\nasync function* chunks(\n fs: TarFS,\n file: string,\n plan: Plan,\n): AsyncGenerator<Uint8Array, void, void> {\n const handle = await fs.open(file, 'r')\n try {\n plan.size = (await handle.stat()).size\n for (let n; (n = plan.next()); ) {\n const buf = new Uint8Array(n)\n const { bytesRead } = await handle.read(buf, 0, n, plan.pos)\n if (!bytesRead) break\n plan.pos += bytesRead\n yield bytesRead < n ? buf.subarray(0, bytesRead) : buf\n }\n } finally {\n await handle.close().catch(() => {})\n }\n}\n\nfunction* chunksSync(\n fs: TarFSSync,\n file: string,\n plan: Plan,\n): Generator<Uint8Array, void, void> {\n const fd = fs.openSync(file, 'r')\n try {\n plan.size = fs.fstatSync(fd).size\n for (let n; (n = plan.next()); ) {\n const buf = new Uint8Array(n)\n const bytesRead = fs.readSync(fd, buf, 0, n, plan.pos)\n if (!bytesRead) break\n plan.pos += bytesRead\n yield bytesRead < n ? buf.subarray(0, bytesRead) : buf\n }\n } finally {\n try {\n fs.closeSync(fd)\n } catch {}\n }\n}\n\n/**\n * Read a file in chunks of `maxReadSize` bytes at most. The default,\n * and the value for 0, is 16 MiB. Each chunk is a new array. The\n * first chunk is small, and the chunks grow. A reader can skip a\n * large part of the file.\n */\nexport const readFile = (\n fs: TarFS,\n file: string,\n maxReadSize?: number,\n): AsyncGenerator<Uint8Array, void, void> & Skip => {\n const plan = new Plan(maxReadSize)\n return Object.assign(chunks(fs, file, plan), {\n [SKIP]: (n: number) => plan.skip(n),\n })\n}\n\n/** The sync form of `readFile`. */\nexport const readFileSync = (\n fs: TarFSSync,\n file: string,\n maxReadSize?: number,\n): Generator<Uint8Array, void, void> & Skip => {\n const plan = new Plan(maxReadSize)\n return Object.assign(chunksSync(fs, file, plan), {\n [SKIP]: (n: number) => plan.skip(n),\n })\n}\n"]}
@@ -1,2 +1,46 @@
1
- export declare const replace: import("./make-command.js").TarCommand<never, never>;
1
+ import { type Codec } from './codec.js';
2
+ import { type WithAliases } from './dealias.js';
3
+ import type { ReplaceOptions, ReplaceOptionsSync } from './options.js';
4
+ import type { PackInput, PackInputSync } from './pack.js';
5
+ /**
6
+ * Finds the place in an archive file where new entries go: the end
7
+ * of the last complete entry before the first block that is not a
8
+ * valid header. A PAX or GNU header with no entry after it is not a
9
+ * complete entry.
10
+ *
11
+ * Read the file into `buf` at `pos`, and call `push()`, until it
12
+ * returns false. The bodies of the entries are not read.
13
+ */
14
+ export declare class AppendScan {
15
+ #private;
16
+ buf: Uint8Array;
17
+ /** The place in the file of the next read. */
18
+ pos: number;
19
+ /**
20
+ * @param size the size of the file
21
+ * @param mtimes gets the path and the `mtime` of each complete entry
22
+ */
23
+ constructor(size: number, codecs?: Codec[], mtimes?: Map<string, Date>);
24
+ /** The place in the file where new entries go. */
25
+ get position(): number;
26
+ /** The number of bytes to read into `buf` at `pos`. */
27
+ get want(): number;
28
+ /**
29
+ * Parse the first `length` bytes of `buf`. False if the place is
30
+ * found. Throws if the file is compressed.
31
+ */
32
+ push(length: number): boolean;
33
+ }
34
+ /**
35
+ * Add the files to the end of an archive file that is not
36
+ * compressed. The files are the same as for `create`. If the archive
37
+ * does not exist, it is made.
38
+ *
39
+ * The new entries go after the last complete entry of the archive.
40
+ * A reader of the archive gets the old entry and then the new entry
41
+ * of a path that is in it two times.
42
+ */
43
+ export declare const replace: (options: WithAliases<ReplaceOptions>, files: PackInput[]) => Promise<void>;
44
+ /** The sync form of `replace`. */
45
+ export declare const replaceSync: (options: WithAliases<ReplaceOptionsSync>, files: PackInputSync[]) => void;
2
46
  //# sourceMappingURL=replace.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"replace.d.ts","sourceRoot":"","sources":["../../src/replace.ts"],"names":[],"mappings":"AAuPA,eAAO,MAAM,OAAO,sDA8BnB,CAAA"}
1
+ {"version":3,"file":"replace.d.ts","sourceRoot":"","sources":["../../src/replace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,KAAK,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAW,KAAK,WAAW,EAAE,MAAM,cAAc,CAAA;AAOxD,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AA+BzD;;;;;;;;GAQG;AACH,qBAAa,UAAU;;IACrB,GAAG,EAAE,UAAU,CAAwB;IACvC,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAI;IAMf;;;OAGG;gBAED,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,KAAK,EAAO,EACpB,MAAM,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC;IAe5B,kDAAkD;IAClD,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,uDAAuD;IACvD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;OAGG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;CA6B9B;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO,GAClB,SAAS,WAAW,CAAC,cAAc,CAAC,EACpC,OAAO,SAAS,EAAE,KACjB,OAAO,CAAC,IAAI,CAyBd,CAAA;AAED,kCAAkC;AAClC,eAAO,MAAM,WAAW,GACtB,SAAS,WAAW,CAAC,kBAAkB,CAAC,EACxC,OAAO,aAAa,EAAE,KACrB,IAyBF,CAAA"}
@@ -1,227 +1,181 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.replace = void 0;
7
- // tar -r
8
- const fs_minipass_1 = require("@isaacs/fs-minipass");
9
- const node_fs_1 = __importDefault(require("node:fs"));
10
- const node_path_1 = __importDefault(require("node:path"));
11
- const header_js_1 = require("./header.js");
12
- const list_js_1 = require("./list.js");
13
- const make_command_js_1 = require("./make-command.js");
14
- const options_js_1 = require("./options.js");
15
- const pack_js_1 = require("./pack.js");
16
- // starting at the head of the file, read a Header
17
- // If the checksum is invalid, that's our position to start writing
18
- // If it is, jump forward by the specified size (round up to 512)
19
- // and try again.
20
- // Write the new Pack stream starting there.
21
- const replaceSync = (opt, files) => {
22
- const p = new pack_js_1.PackSync(opt);
23
- let threw = true;
24
- let fd;
25
- let position;
26
- try {
27
- try {
28
- fd = node_fs_1.default.openSync(opt.file, 'r+');
29
- }
30
- catch (er) {
31
- if (er?.code === 'ENOENT') {
32
- fd = node_fs_1.default.openSync(opt.file, 'w+');
33
- }
34
- else {
35
- throw er;
36
- }
37
- }
38
- const st = node_fs_1.default.fstatSync(fd);
39
- const headBuf = Buffer.alloc(512);
40
- POSITION: for (position = 0; position < st.size; position += 512) {
41
- for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {
42
- bytes = node_fs_1.default.readSync(fd, headBuf, bufPos, headBuf.length - bufPos, position + bufPos);
43
- if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {
44
- throw new Error('cannot append to compressed archives');
45
- }
46
- if (!bytes) {
47
- break POSITION;
48
- }
49
- }
50
- const h = new header_js_1.Header(headBuf);
51
- if (!h.cksumValid) {
52
- break;
53
- }
54
- const entryBlockSize = 512 * Math.ceil((h.size || 0) / 512);
55
- if (position + entryBlockSize + 512 > st.size) {
56
- break;
57
- }
58
- // the 512 for the header we just parsed will be added as well
59
- // also jump ahead all the blocks for the body
60
- position += entryBlockSize;
61
- if (opt.mtimeCache && h.mtime) {
62
- opt.mtimeCache.set(String(h.path), h.mtime);
63
- }
64
- }
65
- threw = false;
66
- streamSync(opt, p, position, fd, files);
3
+ exports.replaceSync = exports.replace = exports.AppendScan = void 0;
4
+ const codec_js_1 = require("./codec.js");
5
+ const dealias_js_1 = require("./dealias.js");
6
+ const create_js_1 = require("./create.js");
7
+ const parser_js_1 = require("./parser.js");
8
+ // The most bytes to read from the archive at one time.
9
+ const CHUNK = 64 * 1024;
10
+ // Thrown in the parser to stop at the first block that is not valid.
11
+ const STOP = {};
12
+ const noop = () => { };
13
+ const compressed = () => new Error('cannot append to compressed archives');
14
+ const check = (opt, files) => {
15
+ const { file } = opt;
16
+ if (!file)
17
+ throw new TypeError('file is required');
18
+ if (opt.gzip ||
19
+ opt.brotli ||
20
+ opt.zstd ||
21
+ file.endsWith('.br') ||
22
+ file.endsWith('.tbr')) {
23
+ throw new TypeError(compressed().message);
67
24
  }
68
- finally {
69
- if (threw) {
70
- try {
71
- node_fs_1.default.closeSync(fd);
72
- }
73
- catch { }
74
- }
25
+ if (!files?.length) {
26
+ throw new TypeError('no paths specified to add/replace');
75
27
  }
76
28
  };
77
- const streamSync = (opt, p, position, fd, files) => {
78
- const stream = new fs_minipass_1.WriteStreamSync(opt.file, {
79
- fd: fd,
80
- start: position,
81
- });
82
- p.pipe(stream);
83
- addFilesSync(p, files);
84
- };
85
- const replaceAsync = (opt, files) => {
86
- files = Array.from(files);
87
- const p = new pack_js_1.Pack(opt);
88
- const getPos = (fd, size, cb_) => {
89
- const cb = (er, pos) => {
90
- if (er) {
91
- node_fs_1.default.close(fd, _ => cb_(er));
92
- }
93
- else {
94
- cb_(null, pos);
95
- }
96
- };
97
- let position = 0;
98
- if (size === 0) {
99
- return cb(null, 0);
29
+ /**
30
+ * Finds the place in an archive file where new entries go: the end
31
+ * of the last complete entry before the first block that is not a
32
+ * valid header. A PAX or GNU header with no entry after it is not a
33
+ * complete entry.
34
+ *
35
+ * Read the file into `buf` at `pos`, and call `push()`, until it
36
+ * returns false. The bodies of the entries are not read.
37
+ */
38
+ class AppendScan {
39
+ buf = new Uint8Array(CHUNK);
40
+ /** The place in the file of the next read. */
41
+ pos = 0;
42
+ #size;
43
+ #parser;
44
+ #codecs;
45
+ #mtimes;
46
+ /**
47
+ * @param size the size of the file
48
+ * @param mtimes gets the path and the `mtime` of each complete entry
49
+ */
50
+ constructor(size, codecs = [], mtimes) {
51
+ this.#size = size;
52
+ this.#codecs = [...codecs, codec_js_1.gzip];
53
+ this.#mtimes = mtimes;
54
+ this.#parser = new parser_js_1.Parser({
55
+ // The parser must not give the bodies.
56
+ filter: () => false,
57
+ onwarn: (_code, _message, data) => {
58
+ // An error of the header code goes to the caller.
59
+ throw data instanceof Error ? data : STOP;
60
+ },
61
+ });
62
+ }
63
+ /** The place in the file where new entries go. */
64
+ get position() {
65
+ return this.#parser.position;
66
+ }
67
+ /** The number of bytes to read into `buf` at `pos`. */
68
+ get want() {
69
+ return Math.min(CHUNK, this.#size - this.pos);
70
+ }
71
+ /**
72
+ * Parse the first `length` bytes of `buf`. False if the place is
73
+ * found. Throws if the file is compressed.
74
+ */
75
+ push(length) {
76
+ const chunk = this.buf.subarray(0, length);
77
+ if (this.pos === 0 && this.#codecs.some(c => c.detect(chunk))) {
78
+ throw compressed();
100
79
  }
101
- let bufPos = 0;
102
- const headBuf = Buffer.alloc(512);
103
- const onread = (er, bytes) => {
104
- if (er || bytes === undefined) {
105
- return cb(er);
106
- }
107
- bufPos += bytes;
108
- if (bufPos < 512 && bytes) {
109
- return node_fs_1.default.read(fd, headBuf, bufPos, headBuf.length - bufPos, position + bufPos, onread);
110
- }
111
- if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {
112
- return cb(new Error('cannot append to compressed archives'));
113
- }
114
- // truncated header
115
- if (bufPos < 512) {
116
- return cb(null, position);
117
- }
118
- const h = new header_js_1.Header(headBuf);
119
- if (!h.cksumValid) {
120
- return cb(null, position);
121
- }
122
- /* c8 ignore next */
123
- const entryBlockSize = 512 * Math.ceil((h.size ?? 0) / 512);
124
- if (position + entryBlockSize + 512 > size) {
125
- return cb(null, position);
126
- }
127
- position += entryBlockSize + 512;
128
- if (position >= size) {
129
- return cb(null, position);
130
- }
131
- if (opt.mtimeCache && h.mtime) {
132
- opt.mtimeCache.set(String(h.path), h.mtime);
133
- }
134
- bufPos = 0;
135
- node_fs_1.default.read(fd, headBuf, 0, 512, position, onread);
136
- };
137
- node_fs_1.default.read(fd, headBuf, 0, 512, position, onread);
138
- };
139
- const promise = new Promise((resolve, reject) => {
140
- p.on('error', reject);
141
- let flag = 'r+';
142
- const onopen = (er, fd) => {
143
- if (er && er.code === 'ENOENT' && flag === 'r+') {
144
- flag = 'w+';
145
- return node_fs_1.default.open(opt.file, flag, onopen);
146
- }
147
- if (er || !fd) {
148
- return reject(er);
80
+ const parser = this.#parser;
81
+ this.pos += length;
82
+ // the end of the body of the last entry in the chunk
83
+ let end = 0;
84
+ parser.push(chunk);
85
+ try {
86
+ for (let ev; (ev = parser.next());) {
87
+ if (ev.type !== 'entry')
88
+ return false;
89
+ const { entry } = ev;
90
+ end = parser.offset + 512 * Math.ceil(entry.size / 512);
91
+ if (end > this.#size)
92
+ return false;
93
+ if (entry.mtime)
94
+ this.#mtimes?.set(entry.path, entry.mtime);
95
+ }
96
+ // The rest of the body of the last entry is not read.
97
+ if (this.pos < end) {
98
+ parser.skipBody();
99
+ this.pos = end;
149
100
  }
150
- node_fs_1.default.fstat(fd, (er, st) => {
151
- if (er) {
152
- return node_fs_1.default.close(fd, () => reject(er));
153
- }
154
- getPos(fd, st.size, (er, position) => {
155
- if (er) {
156
- return reject(er);
157
- }
158
- const stream = new fs_minipass_1.WriteStream(opt.file, {
159
- fd: fd,
160
- start: position,
161
- });
162
- p.pipe(stream);
163
- stream.on('error', reject);
164
- stream.on('close', resolve);
165
- addFilesAsync(p, files);
166
- });
167
- });
168
- };
169
- node_fs_1.default.open(opt.file, flag, onopen);
170
- });
171
- return promise;
172
- };
173
- const addFilesSync = (p, files) => {
174
- files.forEach(file => {
175
- if (file.charAt(0) === '@') {
176
- (0, list_js_1.list)({
177
- file: node_path_1.default.resolve(p.cwd, file.slice(1)),
178
- sync: true,
179
- noResume: true,
180
- onReadEntry: entry => p.add(entry),
181
- });
182
101
  }
183
- else {
184
- p.add(file);
102
+ catch (er) {
103
+ if (er !== STOP)
104
+ throw er;
105
+ return false;
185
106
  }
107
+ return length > 0 && this.pos < this.#size;
108
+ }
109
+ }
110
+ exports.AppendScan = AppendScan;
111
+ /**
112
+ * Add the files to the end of an archive file that is not
113
+ * compressed. The files are the same as for `create`. If the archive
114
+ * does not exist, it is made.
115
+ *
116
+ * The new entries go after the last complete entry of the archive.
117
+ * A reader of the archive gets the old entry and then the new entry
118
+ * of a path that is in it two times.
119
+ */
120
+ const replace = async (options, files) => {
121
+ const opt = (0, dealias_js_1.dealias)(options);
122
+ check(opt, files);
123
+ const { fs, file } = opt;
124
+ const pack = (0, create_js_1.packFiles)(opt, files);
125
+ if (pack.codec)
126
+ throw new TypeError(compressed().message);
127
+ const handle = await fs.open(file, 'r+').catch(er => {
128
+ if (er?.code !== 'ENOENT')
129
+ throw er;
130
+ return fs.open(file, 'w+', opt.mode || 0o666);
186
131
  });
187
- p.end();
188
- };
189
- const addFilesAsync = async (p, files) => {
190
- for (const file of files) {
191
- if (file.charAt(0) === '@') {
192
- await (0, list_js_1.list)({
193
- file: node_path_1.default.resolve(String(p.cwd), file.slice(1)),
194
- noResume: true,
195
- onReadEntry: entry => p.add(entry),
196
- });
197
- }
198
- else {
199
- p.add(file);
200
- }
132
+ let ok = false;
133
+ try {
134
+ const { size } = await handle.stat();
135
+ const scan = new AppendScan(size, opt.codecs, opt.mtimeCache);
136
+ while (scan.push((await handle.read(scan.buf, 0, scan.want, scan.pos)).bytesRead))
137
+ ;
138
+ await (0, create_js_1.writeChunks)(handle, pack, scan.position);
139
+ ok = true;
140
+ }
141
+ finally {
142
+ const closed = handle.close();
143
+ await (ok ? closed : closed.catch(noop));
201
144
  }
202
- p.end();
203
145
  };
204
- exports.replace = (0, make_command_js_1.makeCommand)(replaceSync, replaceAsync,
205
- /* c8 ignore start */
206
- () => {
207
- throw new TypeError('file is required');
208
- }, () => {
209
- throw new TypeError('file is required');
210
- },
211
- /* c8 ignore stop */
212
- (opt, entries) => {
213
- if (!(0, options_js_1.isFile)(opt)) {
214
- throw new TypeError('file is required');
146
+ exports.replace = replace;
147
+ /** The sync form of `replace`. */
148
+ const replaceSync = (options, files) => {
149
+ const opt = (0, dealias_js_1.dealias)(options);
150
+ check(opt, files);
151
+ const { fs, file } = opt;
152
+ const pack = (0, create_js_1.packFilesSync)(opt, files);
153
+ if (pack.codec)
154
+ throw new TypeError(compressed().message);
155
+ let fd;
156
+ try {
157
+ fd = fs.openSync(file, 'r+');
215
158
  }
216
- if (opt.gzip ||
217
- opt.brotli ||
218
- opt.zstd ||
219
- opt.file.endsWith('.br') ||
220
- opt.file.endsWith('.tbr')) {
221
- throw new TypeError('cannot append to compressed archives');
159
+ catch (er) {
160
+ if (er?.code !== 'ENOENT')
161
+ throw er;
162
+ fd = fs.openSync(file, 'w+', opt.mode || 0o666);
222
163
  }
223
- if (!entries?.length) {
224
- throw new TypeError('no paths specified to add/replace');
164
+ try {
165
+ const { size } = fs.fstatSync(fd);
166
+ const scan = new AppendScan(size, opt.codecs, opt.mtimeCache);
167
+ while (scan.push(fs.readSync(fd, scan.buf, 0, scan.want, scan.pos)))
168
+ ;
169
+ (0, create_js_1.writeChunksSync)(fs, fd, pack, scan.position);
225
170
  }
226
- });
171
+ catch (er) {
172
+ try {
173
+ fs.closeSync(fd);
174
+ }
175
+ catch { }
176
+ throw er;
177
+ }
178
+ fs.closeSync(fd);
179
+ };
180
+ exports.replaceSync = replaceSync;
227
181
  //# sourceMappingURL=replace.js.map