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
@@ -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,99 @@
1
+ import { countOption } from './options.js';
2
+ const MAX_READ = 16 * 1024 * 1024;
3
+ // The size of the first read, and of the read after a skip.
4
+ const FIRST_READ = 64 * 1024;
5
+ // A skip of fewer bytes than this is slower than a read of them.
6
+ const MIN_SKIP = 1024 * 1024;
7
+ /**
8
+ * The key of the method of a chunk source that skips input. A
9
+ * `TarReader` calls it with the number of bytes of a body that are
10
+ * after the chunks given so far, when no consumer reads that body.
11
+ * If it returns true, the next chunk starts after those bytes.
12
+ */
13
+ export const SKIP = Symbol.for('tar.readFile.skip');
14
+ // The place and the size of the next read of a file.
15
+ class Plan {
16
+ pos = 0;
17
+ /** The size of the file. 0 before the file is open. */
18
+ size = 0;
19
+ #max;
20
+ #want = FIRST_READ;
21
+ constructor(maxReadSize) {
22
+ this.#max = countOption('maxReadSize', maxReadSize, MAX_READ);
23
+ }
24
+ // The number of bytes to read at `pos`. 0 at the end of the file.
25
+ // Each read is two times the size of the read before it, so that
26
+ // an archive of small entries is read in few reads.
27
+ next() {
28
+ const n = Math.min(this.#want, this.#max, this.size - this.pos);
29
+ this.#want *= 2;
30
+ return n;
31
+ }
32
+ // Skip only inside the file: the parser must see where a truncated
33
+ // file ends. The read after a skip is small, because a header
34
+ // comes next, and the body after it can be skipped also.
35
+ skip(n) {
36
+ if (n < MIN_SKIP || this.pos + n > this.size)
37
+ return false;
38
+ this.pos += n;
39
+ this.#want = FIRST_READ;
40
+ return true;
41
+ }
42
+ }
43
+ async function* chunks(fs, file, plan) {
44
+ const handle = await fs.open(file, 'r');
45
+ try {
46
+ plan.size = (await handle.stat()).size;
47
+ for (let n; (n = plan.next());) {
48
+ const buf = new Uint8Array(n);
49
+ const { bytesRead } = await handle.read(buf, 0, n, plan.pos);
50
+ if (!bytesRead)
51
+ break;
52
+ plan.pos += bytesRead;
53
+ yield bytesRead < n ? buf.subarray(0, bytesRead) : buf;
54
+ }
55
+ }
56
+ finally {
57
+ await handle.close().catch(() => { });
58
+ }
59
+ }
60
+ function* chunksSync(fs, file, plan) {
61
+ const fd = fs.openSync(file, 'r');
62
+ try {
63
+ plan.size = fs.fstatSync(fd).size;
64
+ for (let n; (n = plan.next());) {
65
+ const buf = new Uint8Array(n);
66
+ const bytesRead = fs.readSync(fd, buf, 0, n, plan.pos);
67
+ if (!bytesRead)
68
+ break;
69
+ plan.pos += bytesRead;
70
+ yield bytesRead < n ? buf.subarray(0, bytesRead) : buf;
71
+ }
72
+ }
73
+ finally {
74
+ try {
75
+ fs.closeSync(fd);
76
+ }
77
+ catch { }
78
+ }
79
+ }
80
+ /**
81
+ * Read a file in chunks of `maxReadSize` bytes at most. The default,
82
+ * and the value for 0, is 16 MiB. Each chunk is a new array. The
83
+ * first chunk is small, and the chunks grow. A reader can skip a
84
+ * large part of the file.
85
+ */
86
+ export const readFile = (fs, file, maxReadSize) => {
87
+ const plan = new Plan(maxReadSize);
88
+ return Object.assign(chunks(fs, file, plan), {
89
+ [SKIP]: (n) => plan.skip(n),
90
+ });
91
+ };
92
+ /** The sync form of `readFile`. */
93
+ export const readFileSync = (fs, file, maxReadSize) => {
94
+ const plan = new Plan(maxReadSize);
95
+ return Object.assign(chunksSync(fs, file, plan), {
96
+ [SKIP]: (n) => plan.skip(n),
97
+ });
98
+ };
99
+ //# sourceMappingURL=read-file.js.map
@@ -0,0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C,EAAE;QAC/C,CAAC,IAAI,CAAC,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;KACpC,CAAC,CAAA;AACJ,CAAC,CAAA","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,221 +1,175 @@
1
- // tar -r
2
- import { WriteStream, WriteStreamSync } from '@isaacs/fs-minipass';
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { Header } from './header.js';
6
- import { list } from './list.js';
7
- import { makeCommand } from './make-command.js';
8
- import { isFile } from './options.js';
9
- import { Pack, PackSync } from './pack.js';
10
- // starting at the head of the file, read a Header
11
- // If the checksum is invalid, that's our position to start writing
12
- // If it is, jump forward by the specified size (round up to 512)
13
- // and try again.
14
- // Write the new Pack stream starting there.
15
- const replaceSync = (opt, files) => {
16
- const p = new PackSync(opt);
17
- let threw = true;
18
- let fd;
19
- let position;
20
- try {
21
- try {
22
- fd = fs.openSync(opt.file, 'r+');
23
- }
24
- catch (er) {
25
- if (er?.code === 'ENOENT') {
26
- fd = fs.openSync(opt.file, 'w+');
27
- }
28
- else {
29
- throw er;
30
- }
31
- }
32
- const st = fs.fstatSync(fd);
33
- const headBuf = Buffer.alloc(512);
34
- POSITION: for (position = 0; position < st.size; position += 512) {
35
- for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {
36
- bytes = fs.readSync(fd, headBuf, bufPos, headBuf.length - bufPos, position + bufPos);
37
- if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {
38
- throw new Error('cannot append to compressed archives');
39
- }
40
- if (!bytes) {
41
- break POSITION;
42
- }
43
- }
44
- const h = new Header(headBuf);
45
- if (!h.cksumValid) {
46
- break;
47
- }
48
- const entryBlockSize = 512 * Math.ceil((h.size || 0) / 512);
49
- if (position + entryBlockSize + 512 > st.size) {
50
- break;
51
- }
52
- // the 512 for the header we just parsed will be added as well
53
- // also jump ahead all the blocks for the body
54
- position += entryBlockSize;
55
- if (opt.mtimeCache && h.mtime) {
56
- opt.mtimeCache.set(String(h.path), h.mtime);
57
- }
58
- }
59
- threw = false;
60
- streamSync(opt, p, position, fd, files);
1
+ import { gzip } from './codec.js';
2
+ import { dealias } from './dealias.js';
3
+ import { packFiles, packFilesSync, writeChunks, writeChunksSync, } from './create.js';
4
+ import { Parser } from './parser.js';
5
+ // The most bytes to read from the archive at one time.
6
+ const CHUNK = 64 * 1024;
7
+ // Thrown in the parser to stop at the first block that is not valid.
8
+ const STOP = {};
9
+ const noop = () => { };
10
+ const compressed = () => new Error('cannot append to compressed archives');
11
+ const check = (opt, files) => {
12
+ const { file } = opt;
13
+ if (!file)
14
+ throw new TypeError('file is required');
15
+ if (opt.gzip ||
16
+ opt.brotli ||
17
+ opt.zstd ||
18
+ file.endsWith('.br') ||
19
+ file.endsWith('.tbr')) {
20
+ throw new TypeError(compressed().message);
61
21
  }
62
- finally {
63
- if (threw) {
64
- try {
65
- fs.closeSync(fd);
66
- }
67
- catch { }
68
- }
22
+ if (!files?.length) {
23
+ throw new TypeError('no paths specified to add/replace');
69
24
  }
70
25
  };
71
- const streamSync = (opt, p, position, fd, files) => {
72
- const stream = new WriteStreamSync(opt.file, {
73
- fd: fd,
74
- start: position,
75
- });
76
- p.pipe(stream);
77
- addFilesSync(p, files);
78
- };
79
- const replaceAsync = (opt, files) => {
80
- files = Array.from(files);
81
- const p = new Pack(opt);
82
- const getPos = (fd, size, cb_) => {
83
- const cb = (er, pos) => {
84
- if (er) {
85
- fs.close(fd, _ => cb_(er));
86
- }
87
- else {
88
- cb_(null, pos);
89
- }
90
- };
91
- let position = 0;
92
- if (size === 0) {
93
- return cb(null, 0);
26
+ /**
27
+ * Finds the place in an archive file where new entries go: the end
28
+ * of the last complete entry before the first block that is not a
29
+ * valid header. A PAX or GNU header with no entry after it is not a
30
+ * complete entry.
31
+ *
32
+ * Read the file into `buf` at `pos`, and call `push()`, until it
33
+ * returns false. The bodies of the entries are not read.
34
+ */
35
+ export class AppendScan {
36
+ buf = new Uint8Array(CHUNK);
37
+ /** The place in the file of the next read. */
38
+ pos = 0;
39
+ #size;
40
+ #parser;
41
+ #codecs;
42
+ #mtimes;
43
+ /**
44
+ * @param size the size of the file
45
+ * @param mtimes gets the path and the `mtime` of each complete entry
46
+ */
47
+ constructor(size, codecs = [], mtimes) {
48
+ this.#size = size;
49
+ this.#codecs = [...codecs, gzip];
50
+ this.#mtimes = mtimes;
51
+ this.#parser = new Parser({
52
+ // The parser must not give the bodies.
53
+ filter: () => false,
54
+ onwarn: (_code, _message, data) => {
55
+ // An error of the header code goes to the caller.
56
+ throw data instanceof Error ? data : STOP;
57
+ },
58
+ });
59
+ }
60
+ /** The place in the file where new entries go. */
61
+ get position() {
62
+ return this.#parser.position;
63
+ }
64
+ /** The number of bytes to read into `buf` at `pos`. */
65
+ get want() {
66
+ return Math.min(CHUNK, this.#size - this.pos);
67
+ }
68
+ /**
69
+ * Parse the first `length` bytes of `buf`. False if the place is
70
+ * found. Throws if the file is compressed.
71
+ */
72
+ push(length) {
73
+ const chunk = this.buf.subarray(0, length);
74
+ if (this.pos === 0 && this.#codecs.some(c => c.detect(chunk))) {
75
+ throw compressed();
94
76
  }
95
- let bufPos = 0;
96
- const headBuf = Buffer.alloc(512);
97
- const onread = (er, bytes) => {
98
- if (er || bytes === undefined) {
99
- return cb(er);
100
- }
101
- bufPos += bytes;
102
- if (bufPos < 512 && bytes) {
103
- return fs.read(fd, headBuf, bufPos, headBuf.length - bufPos, position + bufPos, onread);
104
- }
105
- if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {
106
- return cb(new Error('cannot append to compressed archives'));
107
- }
108
- // truncated header
109
- if (bufPos < 512) {
110
- return cb(null, position);
111
- }
112
- const h = new Header(headBuf);
113
- if (!h.cksumValid) {
114
- return cb(null, position);
115
- }
116
- /* c8 ignore next */
117
- const entryBlockSize = 512 * Math.ceil((h.size ?? 0) / 512);
118
- if (position + entryBlockSize + 512 > size) {
119
- return cb(null, position);
120
- }
121
- position += entryBlockSize + 512;
122
- if (position >= size) {
123
- return cb(null, position);
124
- }
125
- if (opt.mtimeCache && h.mtime) {
126
- opt.mtimeCache.set(String(h.path), h.mtime);
127
- }
128
- bufPos = 0;
129
- fs.read(fd, headBuf, 0, 512, position, onread);
130
- };
131
- fs.read(fd, headBuf, 0, 512, position, onread);
132
- };
133
- const promise = new Promise((resolve, reject) => {
134
- p.on('error', reject);
135
- let flag = 'r+';
136
- const onopen = (er, fd) => {
137
- if (er && er.code === 'ENOENT' && flag === 'r+') {
138
- flag = 'w+';
139
- return fs.open(opt.file, flag, onopen);
140
- }
141
- if (er || !fd) {
142
- return reject(er);
77
+ const parser = this.#parser;
78
+ this.pos += length;
79
+ // the end of the body of the last entry in the chunk
80
+ let end = 0;
81
+ parser.push(chunk);
82
+ try {
83
+ for (let ev; (ev = parser.next());) {
84
+ if (ev.type !== 'entry')
85
+ return false;
86
+ const { entry } = ev;
87
+ end = parser.offset + 512 * Math.ceil(entry.size / 512);
88
+ if (end > this.#size)
89
+ return false;
90
+ if (entry.mtime)
91
+ this.#mtimes?.set(entry.path, entry.mtime);
92
+ }
93
+ // The rest of the body of the last entry is not read.
94
+ if (this.pos < end) {
95
+ parser.skipBody();
96
+ this.pos = end;
143
97
  }
144
- fs.fstat(fd, (er, st) => {
145
- if (er) {
146
- return fs.close(fd, () => reject(er));
147
- }
148
- getPos(fd, st.size, (er, position) => {
149
- if (er) {
150
- return reject(er);
151
- }
152
- const stream = new WriteStream(opt.file, {
153
- fd: fd,
154
- start: position,
155
- });
156
- p.pipe(stream);
157
- stream.on('error', reject);
158
- stream.on('close', resolve);
159
- addFilesAsync(p, files);
160
- });
161
- });
162
- };
163
- fs.open(opt.file, flag, onopen);
164
- });
165
- return promise;
166
- };
167
- const addFilesSync = (p, files) => {
168
- files.forEach(file => {
169
- if (file.charAt(0) === '@') {
170
- list({
171
- file: path.resolve(p.cwd, file.slice(1)),
172
- sync: true,
173
- noResume: true,
174
- onReadEntry: entry => p.add(entry),
175
- });
176
98
  }
177
- else {
178
- p.add(file);
99
+ catch (er) {
100
+ if (er !== STOP)
101
+ throw er;
102
+ return false;
179
103
  }
104
+ return length > 0 && this.pos < this.#size;
105
+ }
106
+ }
107
+ /**
108
+ * Add the files to the end of an archive file that is not
109
+ * compressed. The files are the same as for `create`. If the archive
110
+ * does not exist, it is made.
111
+ *
112
+ * The new entries go after the last complete entry of the archive.
113
+ * A reader of the archive gets the old entry and then the new entry
114
+ * of a path that is in it two times.
115
+ */
116
+ export const replace = async (options, files) => {
117
+ const opt = dealias(options);
118
+ check(opt, files);
119
+ const { fs, file } = opt;
120
+ const pack = packFiles(opt, files);
121
+ if (pack.codec)
122
+ throw new TypeError(compressed().message);
123
+ const handle = await fs.open(file, 'r+').catch(er => {
124
+ if (er?.code !== 'ENOENT')
125
+ throw er;
126
+ return fs.open(file, 'w+', opt.mode || 0o666);
180
127
  });
181
- p.end();
182
- };
183
- const addFilesAsync = async (p, files) => {
184
- for (const file of files) {
185
- if (file.charAt(0) === '@') {
186
- await list({
187
- file: path.resolve(String(p.cwd), file.slice(1)),
188
- noResume: true,
189
- onReadEntry: entry => p.add(entry),
190
- });
191
- }
192
- else {
193
- p.add(file);
194
- }
128
+ let ok = false;
129
+ try {
130
+ const { size } = await handle.stat();
131
+ const scan = new AppendScan(size, opt.codecs, opt.mtimeCache);
132
+ while (scan.push((await handle.read(scan.buf, 0, scan.want, scan.pos)).bytesRead))
133
+ ;
134
+ await writeChunks(handle, pack, scan.position);
135
+ ok = true;
136
+ }
137
+ finally {
138
+ const closed = handle.close();
139
+ await (ok ? closed : closed.catch(noop));
195
140
  }
196
- p.end();
197
141
  };
198
- export const replace = makeCommand(replaceSync, replaceAsync,
199
- /* c8 ignore start */
200
- () => {
201
- throw new TypeError('file is required');
202
- }, () => {
203
- throw new TypeError('file is required');
204
- },
205
- /* c8 ignore stop */
206
- (opt, entries) => {
207
- if (!isFile(opt)) {
208
- throw new TypeError('file is required');
142
+ /** The sync form of `replace`. */
143
+ export const replaceSync = (options, files) => {
144
+ const opt = dealias(options);
145
+ check(opt, files);
146
+ const { fs, file } = opt;
147
+ const pack = packFilesSync(opt, files);
148
+ if (pack.codec)
149
+ throw new TypeError(compressed().message);
150
+ let fd;
151
+ try {
152
+ fd = fs.openSync(file, 'r+');
209
153
  }
210
- if (opt.gzip ||
211
- opt.brotli ||
212
- opt.zstd ||
213
- opt.file.endsWith('.br') ||
214
- opt.file.endsWith('.tbr')) {
215
- throw new TypeError('cannot append to compressed archives');
154
+ catch (er) {
155
+ if (er?.code !== 'ENOENT')
156
+ throw er;
157
+ fd = fs.openSync(file, 'w+', opt.mode || 0o666);
216
158
  }
217
- if (!entries?.length) {
218
- throw new TypeError('no paths specified to add/replace');
159
+ try {
160
+ const { size } = fs.fstatSync(fd);
161
+ const scan = new AppendScan(size, opt.codecs, opt.mtimeCache);
162
+ while (scan.push(fs.readSync(fd, scan.buf, 0, scan.want, scan.pos)))
163
+ ;
164
+ writeChunksSync(fs, fd, pack, scan.position);
219
165
  }
220
- });
166
+ catch (er) {
167
+ try {
168
+ fs.closeSync(fd);
169
+ }
170
+ catch { }
171
+ throw er;
172
+ }
173
+ fs.closeSync(fd);
174
+ };
221
175
  //# sourceMappingURL=replace.js.map
@@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AC,MAAM,OAAO,GAAG,WAAW,CAChC,WAAW,EACX,YAAY;AACZ,qBAAqB;AACrB,GAAU,EAAE;IACV,MAAM,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAA;AACzC,CAAC,EACD,GAAU,EAAE;IACV,MAAM,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAA;AACzC,CAAC;AACD,oBAAoB;AACpB,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;IACf,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QACjB,MAAM,IAAI,SAAS,CAAC,kBAAkB,CAAC,CAAA;IACzC,CAAC;IAED,IACE,GAAG,CAAC,IAAI;QACR,GAAG,CAAC,MAAM;QACV,GAAG,CAAC,IAAI;QACR,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QACxB,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EACzB,CAAC;QACD,MAAM,IAAI,SAAS,CAAC,sCAAsC,CAAC,CAAA;IAC7D,CAAC;IAED,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;QACrB,MAAM,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAA;IAC1D,CAAC;AACH,CAAC,CACF,CAAA","sourcesContent":["// tar -r\nimport { WriteStream, WriteStreamSync } from '@isaacs/fs-minipass'\nimport type { Minipass } from 'minipass'\nimport fs from 'node:fs'\nimport path from 'node:path'\nimport { Header } from './header.js'\nimport { list } from './list.js'\nimport { makeCommand } from './make-command.js'\nimport type { TarOptionsFile, TarOptionsSyncFile } from './options.js'\nimport { isFile } from './options.js'\nimport { Pack, PackSync } from './pack.js'\n\n// starting at the head of the file, read a Header\n// If the checksum is invalid, that's our position to start writing\n// If it is, jump forward by the specified size (round up to 512)\n// and try again.\n// Write the new Pack stream starting there.\n\nconst replaceSync = (opt: TarOptionsSyncFile, files: string[]) => {\n const p = new PackSync(opt)\n\n let threw = true\n let fd\n let position\n\n try {\n try {\n fd = fs.openSync(opt.file, 'r+')\n } catch (er) {\n if ((er as NodeJS.ErrnoException)?.code === 'ENOENT') {\n fd = fs.openSync(opt.file, 'w+')\n } else {\n throw er\n }\n }\n\n const st = fs.fstatSync(fd)\n const headBuf = Buffer.alloc(512)\n\n POSITION: for (position = 0; position < st.size; position += 512) {\n for (let bufPos = 0, bytes = 0; bufPos < 512; bufPos += bytes) {\n bytes = fs.readSync(\n fd,\n headBuf,\n bufPos,\n headBuf.length - bufPos,\n position + bufPos,\n )\n\n if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {\n throw new Error('cannot append to compressed archives')\n }\n\n if (!bytes) {\n break POSITION\n }\n }\n\n const h = new Header(headBuf)\n if (!h.cksumValid) {\n break\n }\n const entryBlockSize = 512 * Math.ceil((h.size || 0) / 512)\n if (position + entryBlockSize + 512 > st.size) {\n break\n }\n // the 512 for the header we just parsed will be added as well\n // also jump ahead all the blocks for the body\n position += entryBlockSize\n if (opt.mtimeCache && h.mtime) {\n opt.mtimeCache.set(String(h.path), h.mtime)\n }\n }\n threw = false\n\n streamSync(opt, p, position, fd, files)\n } finally {\n if (threw) {\n try {\n fs.closeSync(fd as number)\n } catch {}\n }\n }\n}\n\nconst streamSync = (\n opt: TarOptionsSyncFile,\n p: Pack,\n position: number,\n fd: number,\n files: string[],\n) => {\n const stream = new WriteStreamSync(opt.file, {\n fd: fd,\n start: position,\n })\n p.pipe(stream as unknown as Minipass.Writable)\n addFilesSync(p, files)\n}\n\nconst replaceAsync = (\n opt: TarOptionsFile,\n files: string[],\n): Promise<void> => {\n files = Array.from(files)\n const p = new Pack(opt)\n\n const getPos = (\n fd: number,\n size: number,\n cb_: (er?: null | Error, pos?: number) => void,\n ) => {\n const cb = (er?: Error | null, pos?: number) => {\n if (er) {\n fs.close(fd, _ => cb_(er))\n } else {\n cb_(null, pos)\n }\n }\n\n let position = 0\n if (size === 0) {\n return cb(null, 0)\n }\n\n let bufPos = 0\n const headBuf = Buffer.alloc(512)\n const onread = (er?: null | Error, bytes?: number): void => {\n if (er || bytes === undefined) {\n return cb(er)\n }\n bufPos += bytes\n if (bufPos < 512 && bytes) {\n return fs.read(\n fd,\n headBuf,\n bufPos,\n headBuf.length - bufPos,\n position + bufPos,\n onread,\n )\n }\n\n if (position === 0 && headBuf[0] === 0x1f && headBuf[1] === 0x8b) {\n return cb(new Error('cannot append to compressed archives'))\n }\n\n // truncated header\n if (bufPos < 512) {\n return cb(null, position)\n }\n\n const h = new Header(headBuf)\n if (!h.cksumValid) {\n return cb(null, position)\n }\n\n /* c8 ignore next */\n const entryBlockSize = 512 * Math.ceil((h.size ?? 0) / 512)\n if (position + entryBlockSize + 512 > size) {\n return cb(null, position)\n }\n\n position += entryBlockSize + 512\n if (position >= size) {\n return cb(null, position)\n }\n\n if (opt.mtimeCache && h.mtime) {\n opt.mtimeCache.set(String(h.path), h.mtime)\n }\n bufPos = 0\n fs.read(fd, headBuf, 0, 512, position, onread)\n }\n fs.read(fd, headBuf, 0, 512, position, onread)\n }\n\n const promise = new Promise<void>((resolve, reject) => {\n p.on('error', reject)\n let flag = 'r+'\n const onopen = (er?: NodeJS.ErrnoException | null, fd?: number) => {\n if (er && er.code === 'ENOENT' && flag === 'r+') {\n flag = 'w+'\n return fs.open(opt.file, flag, onopen)\n }\n\n if (er || !fd) {\n return reject(er)\n }\n\n fs.fstat(fd, (er, st) => {\n if (er) {\n return fs.close(fd, () => reject(er))\n }\n\n getPos(fd, st.size, (er, position) => {\n if (er) {\n return reject(er)\n }\n const stream = new WriteStream(opt.file, {\n fd: fd,\n start: position,\n })\n p.pipe(stream as unknown as Minipass.Writable)\n stream.on('error', reject)\n stream.on('close', resolve)\n addFilesAsync(p, files)\n })\n })\n }\n fs.open(opt.file, flag, onopen)\n })\n\n return promise\n}\n\nconst addFilesSync = (p: Pack, files: string[]) => {\n files.forEach(file => {\n if (file.charAt(0) === '@') {\n list({\n file: path.resolve(p.cwd, file.slice(1)),\n sync: true,\n noResume: true,\n onReadEntry: entry => p.add(entry),\n })\n } else {\n p.add(file)\n }\n })\n p.end()\n}\n\nconst addFilesAsync = async (p: Pack, files: string[]): Promise<void> => {\n for (const file of files) {\n if (file.charAt(0) === '@') {\n await list({\n file: path.resolve(String(p.cwd), file.slice(1)),\n noResume: true,\n onReadEntry: entry => p.add(entry),\n })\n } else {\n p.add(file)\n }\n }\n p.end()\n}\n\nexport const replace = makeCommand(\n replaceSync,\n replaceAsync,\n /* c8 ignore start */\n (): never => {\n throw new TypeError('file is required')\n },\n (): never => {\n throw new TypeError('file is required')\n },\n /* c8 ignore stop */\n (opt, entries) => {\n if (!isFile(opt)) {\n throw new TypeError('file is required')\n }\n\n if (\n opt.gzip ||\n opt.brotli ||\n opt.zstd ||\n opt.file.endsWith('.br') ||\n opt.file.endsWith('.tbr')\n ) {\n throw new TypeError('cannot append to compressed archives')\n }\n\n if (!entries?.length) {\n throw new TypeError('no paths specified to add/replace')\n }\n },\n)\n"]}
1
+ {"version":3,"file":"replace.js","sourceRoot":"","sources":["../../src/replace.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAc,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAoB,MAAM,cAAc,CAAA;AACxD,OAAO,EACL,SAAS,EACT,aAAa,EACb,WAAW,EACX,eAAe,GAChB,MAAM,aAAa,CAAA;AAGpB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,uDAAuD;AACvD,MAAM,KAAK,GAAG,EAAE,GAAG,IAAI,CAAA;AACvB,qEAAqE;AACrE,MAAM,IAAI,GAAG,EAAE,CAAA;AACf,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AAErB,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;AAE1E,MAAM,KAAK,GAAG,CACZ,GAAwC,EACxC,KAAgB,EAChB,EAAE;IACF,MAAM,EAAE,IAAI,EAAE,GAAG,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I,CAAC,CAAA;IAC9B,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACZ,IAAK,EAAwB,EAAE,IAAI,KAAK,QAAQ;YAAE,MAAM,EAAE,CAAA;QAC1D,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,IAAI,KAAK,CAAC,CAAA;IACjD,CAAC;IACD,IAAI,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;QACjC,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC,CAAA;QAC7D,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YAAC,CAAC;QACrE,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC9C,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;QAClB,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QACV,MAAM,EAAE,CAAA;IACV,CAAC;IACD,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;AAClB,CAAC,CAAA","sourcesContent":["import { gzip, type Codec } from './codec.js'\nimport { dealias, type WithAliases } from './dealias.js'\nimport {\n packFiles,\n packFilesSync,\n writeChunks,\n writeChunksSync,\n} from './create.js'\nimport type { ReplaceOptions, ReplaceOptionsSync } from './options.js'\nimport type { PackInput, PackInputSync } from './pack.js'\nimport { Parser } from './parser.js'\n\n// The most bytes to read from the archive at one time.\nconst CHUNK = 64 * 1024\n// Thrown in the parser to stop at the first block that is not valid.\nconst STOP = {}\nconst noop = () => {}\n\nconst compressed = () => new Error('cannot append to compressed archives')\n\nconst check = (\n opt: ReplaceOptions | ReplaceOptionsSync,\n files: unknown[],\n) => {\n const { file } = opt\n if (!file) throw new TypeError('file is required')\n if (\n opt.gzip ||\n opt.brotli ||\n opt.zstd ||\n file.endsWith('.br') ||\n file.endsWith('.tbr')\n ) {\n throw new TypeError(compressed().message)\n }\n if (!files?.length) {\n throw new TypeError('no paths specified to add/replace')\n }\n}\n\n/**\n * Finds the place in an archive file where new entries go: the end\n * of the last complete entry before the first block that is not a\n * valid header. A PAX or GNU header with no entry after it is not a\n * complete entry.\n *\n * Read the file into `buf` at `pos`, and call `push()`, until it\n * returns false. The bodies of the entries are not read.\n */\nexport class AppendScan {\n buf: Uint8Array = new Uint8Array(CHUNK)\n /** The place in the file of the next read. */\n pos: number = 0\n #size: number\n #parser: Parser\n #codecs: Codec[]\n #mtimes?: Map<string, Date>\n\n /**\n * @param size the size of the file\n * @param mtimes gets the path and the `mtime` of each complete entry\n */\n constructor(\n size: number,\n codecs: Codec[] = [],\n mtimes?: Map<string, Date>,\n ) {\n this.#size = size\n this.#codecs = [...codecs, gzip]\n this.#mtimes = mtimes\n this.#parser = new Parser({\n // The parser must not give the bodies.\n filter: () => false,\n onwarn: (_code, _message, data) => {\n // An error of the header code goes to the caller.\n throw data instanceof Error ? data : STOP\n },\n })\n }\n\n /** The place in the file where new entries go. */\n get position(): number {\n return this.#parser.position\n }\n\n /** The number of bytes to read into `buf` at `pos`. */\n get want(): number {\n return Math.min(CHUNK, this.#size - this.pos)\n }\n\n /**\n * Parse the first `length` bytes of `buf`. False if the place is\n * found. Throws if the file is compressed.\n */\n push(length: number): boolean {\n const chunk = this.buf.subarray(0, length)\n if (this.pos === 0 && this.#codecs.some(c => c.detect(chunk))) {\n throw compressed()\n }\n const parser = this.#parser\n this.pos += length\n // the end of the body of the last entry in the chunk\n let end = 0\n parser.push(chunk)\n try {\n for (let ev; (ev = parser.next()); ) {\n if (ev.type !== 'entry') return false\n const { entry } = ev\n end = parser.offset + 512 * Math.ceil(entry.size / 512)\n if (end > this.#size) return false\n if (entry.mtime) this.#mtimes?.set(entry.path, entry.mtime)\n }\n // The rest of the body of the last entry is not read.\n if (this.pos < end) {\n parser.skipBody()\n this.pos = end\n }\n } catch (er) {\n if (er !== STOP) throw er\n return false\n }\n return length > 0 && this.pos < this.#size\n }\n}\n\n/**\n * Add the files to the end of an archive file that is not\n * compressed. The files are the same as for `create`. If the archive\n * does not exist, it is made.\n *\n * The new entries go after the last complete entry of the archive.\n * A reader of the archive gets the old entry and then the new entry\n * of a path that is in it two times.\n */\nexport const replace = async (\n options: WithAliases<ReplaceOptions>,\n files: PackInput[],\n): Promise<void> => {\n const opt = dealias<ReplaceOptions>(options)\n check(opt, files)\n const { fs, file } = opt\n const pack = packFiles(opt, files)\n if (pack.codec) throw new TypeError(compressed().message)\n const handle = await fs.open(file, 'r+').catch(er => {\n if ((er as { code?: string })?.code !== 'ENOENT') throw er\n return fs.open(file, 'w+', opt.mode || 0o666)\n })\n let ok = false\n try {\n const { size } = await handle.stat()\n const scan = new AppendScan(size, opt.codecs, opt.mtimeCache)\n while (\n scan.push(\n (await handle.read(scan.buf, 0, scan.want, scan.pos)).bytesRead,\n )\n );\n await writeChunks(handle, pack, scan.position)\n ok = true\n } finally {\n const closed = handle.close()\n await (ok ? closed : closed.catch(noop))\n }\n}\n\n/** The sync form of `replace`. */\nexport const replaceSync = (\n options: WithAliases<ReplaceOptionsSync>,\n files: PackInputSync[],\n): void => {\n const opt = dealias<ReplaceOptionsSync>(options)\n check(opt, files)\n const { fs, file } = opt\n const pack = packFilesSync(opt, files)\n if (pack.codec) throw new TypeError(compressed().message)\n let fd: number\n try {\n fd = fs.openSync(file, 'r+')\n } catch (er) {\n if ((er as { code?: string })?.code !== 'ENOENT') throw er\n fd = fs.openSync(file, 'w+', opt.mode || 0o666)\n }\n try {\n const { size } = fs.fstatSync(fd)\n const scan = new AppendScan(size, opt.codecs, opt.mtimeCache)\n while (scan.push(fs.readSync(fd, scan.buf, 0, scan.want, scan.pos)));\n writeChunksSync(fs, fd, pack, scan.position)\n } catch (er) {\n try {\n fs.closeSync(fd)\n } catch {}\n throw er\n }\n fs.closeSync(fd)\n}\n"]}