tar 6.2.1 → 7.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 (253) hide show
  1. package/README.md +30 -16
  2. package/dist/commonjs/create.d.ts +8 -0
  3. package/dist/commonjs/create.d.ts.map +1 -0
  4. package/dist/commonjs/create.js +106 -0
  5. package/dist/commonjs/create.js.map +1 -0
  6. package/dist/commonjs/cwd-error.d.ts +8 -0
  7. package/dist/commonjs/cwd-error.d.ts.map +1 -0
  8. package/dist/commonjs/cwd-error.js +18 -0
  9. package/dist/commonjs/cwd-error.js.map +1 -0
  10. package/dist/commonjs/extract.d.ts +8 -0
  11. package/dist/commonjs/extract.d.ts.map +1 -0
  12. package/dist/commonjs/extract.js +141 -0
  13. package/dist/commonjs/extract.js.map +1 -0
  14. package/dist/commonjs/get-write-flag.d.ts +2 -0
  15. package/dist/commonjs/get-write-flag.d.ts.map +1 -0
  16. package/dist/commonjs/get-write-flag.js +29 -0
  17. package/dist/commonjs/get-write-flag.js.map +1 -0
  18. package/dist/commonjs/header.d.ts +54 -0
  19. package/dist/commonjs/header.d.ts.map +1 -0
  20. package/dist/commonjs/header.js +311 -0
  21. package/dist/commonjs/header.js.map +1 -0
  22. package/dist/commonjs/index.d.ts +19 -0
  23. package/dist/commonjs/index.d.ts.map +1 -0
  24. package/dist/commonjs/index.js +54 -0
  25. package/dist/commonjs/index.js.map +1 -0
  26. package/dist/commonjs/large-numbers.d.ts +4 -0
  27. package/dist/commonjs/large-numbers.d.ts.map +1 -0
  28. package/dist/commonjs/large-numbers.js +99 -0
  29. package/dist/commonjs/large-numbers.js.map +1 -0
  30. package/dist/commonjs/list.d.ts +8 -0
  31. package/dist/commonjs/list.d.ts.map +1 -0
  32. package/dist/commonjs/list.js +167 -0
  33. package/dist/commonjs/list.js.map +1 -0
  34. package/dist/commonjs/mkdir.d.ts +27 -0
  35. package/dist/commonjs/mkdir.d.ts.map +1 -0
  36. package/dist/commonjs/mkdir.js +209 -0
  37. package/dist/commonjs/mkdir.js.map +1 -0
  38. package/dist/commonjs/mode-fix.d.ts +2 -0
  39. package/dist/commonjs/mode-fix.d.ts.map +1 -0
  40. package/dist/commonjs/mode-fix.js +29 -0
  41. package/dist/commonjs/mode-fix.js.map +1 -0
  42. package/dist/commonjs/normalize-unicode.d.ts +2 -0
  43. package/dist/commonjs/normalize-unicode.d.ts.map +1 -0
  44. package/dist/commonjs/normalize-unicode.js +17 -0
  45. package/dist/commonjs/normalize-unicode.js.map +1 -0
  46. package/dist/commonjs/normalize-windows-path.d.ts +2 -0
  47. package/dist/commonjs/normalize-windows-path.d.ts.map +1 -0
  48. package/dist/commonjs/normalize-windows-path.js +12 -0
  49. package/dist/commonjs/normalize-windows-path.js.map +1 -0
  50. package/dist/commonjs/options.d.ts +545 -0
  51. package/dist/commonjs/options.d.ts.map +1 -0
  52. package/dist/commonjs/options.js +55 -0
  53. package/dist/commonjs/options.js.map +1 -0
  54. package/dist/commonjs/pack.d.ts +98 -0
  55. package/dist/commonjs/pack.d.ts.map +1 -0
  56. package/dist/commonjs/pack.js +461 -0
  57. package/dist/commonjs/pack.js.map +1 -0
  58. package/dist/commonjs/package.json +3 -0
  59. package/dist/commonjs/parse.d.ts +84 -0
  60. package/dist/commonjs/parse.d.ts.map +1 -0
  61. package/dist/commonjs/parse.js +571 -0
  62. package/dist/commonjs/parse.js.map +1 -0
  63. package/dist/commonjs/path-reservations.d.ts +11 -0
  64. package/dist/commonjs/path-reservations.d.ts.map +1 -0
  65. package/dist/commonjs/path-reservations.js +169 -0
  66. package/dist/commonjs/path-reservations.js.map +1 -0
  67. package/dist/commonjs/pax.d.ts +27 -0
  68. package/dist/commonjs/pax.d.ts.map +1 -0
  69. package/dist/commonjs/pax.js +158 -0
  70. package/dist/commonjs/pax.js.map +1 -0
  71. package/dist/commonjs/read-entry.d.ts +37 -0
  72. package/dist/commonjs/read-entry.d.ts.map +1 -0
  73. package/dist/commonjs/read-entry.js +139 -0
  74. package/dist/commonjs/read-entry.js.map +1 -0
  75. package/dist/commonjs/replace.d.ts +5 -0
  76. package/dist/commonjs/replace.d.ts.map +1 -0
  77. package/dist/commonjs/replace.js +223 -0
  78. package/dist/commonjs/replace.js.map +1 -0
  79. package/dist/commonjs/strip-absolute-path.d.ts +2 -0
  80. package/dist/commonjs/strip-absolute-path.d.ts.map +1 -0
  81. package/dist/commonjs/strip-absolute-path.js +29 -0
  82. package/dist/commonjs/strip-absolute-path.js.map +1 -0
  83. package/dist/commonjs/strip-trailing-slashes.d.ts +2 -0
  84. package/dist/commonjs/strip-trailing-slashes.d.ts.map +1 -0
  85. package/dist/commonjs/strip-trailing-slashes.js +18 -0
  86. package/dist/commonjs/strip-trailing-slashes.js.map +1 -0
  87. package/dist/commonjs/symlink-error.d.ts +9 -0
  88. package/dist/commonjs/symlink-error.d.ts.map +1 -0
  89. package/dist/commonjs/symlink-error.js +19 -0
  90. package/dist/commonjs/symlink-error.js.map +1 -0
  91. package/dist/commonjs/types.d.ts +7 -0
  92. package/dist/commonjs/types.d.ts.map +1 -0
  93. package/dist/commonjs/types.js +50 -0
  94. package/dist/commonjs/types.js.map +1 -0
  95. package/dist/commonjs/unpack.d.ts +98 -0
  96. package/dist/commonjs/unpack.d.ts.map +1 -0
  97. package/dist/commonjs/unpack.js +921 -0
  98. package/dist/commonjs/unpack.js.map +1 -0
  99. package/dist/commonjs/update.d.ts +3 -0
  100. package/dist/commonjs/update.d.ts.map +1 -0
  101. package/dist/commonjs/update.js +47 -0
  102. package/dist/commonjs/update.js.map +1 -0
  103. package/dist/commonjs/warn-method.d.ts +20 -0
  104. package/dist/commonjs/warn-method.d.ts.map +1 -0
  105. package/dist/commonjs/warn-method.js +31 -0
  106. package/dist/commonjs/warn-method.js.map +1 -0
  107. package/dist/commonjs/winchars.d.ts +3 -0
  108. package/dist/commonjs/winchars.d.ts.map +1 -0
  109. package/dist/commonjs/winchars.js +14 -0
  110. package/dist/commonjs/winchars.js.map +1 -0
  111. package/dist/commonjs/write-entry.d.ts +125 -0
  112. package/dist/commonjs/write-entry.d.ts.map +1 -0
  113. package/dist/commonjs/write-entry.js +657 -0
  114. package/dist/commonjs/write-entry.js.map +1 -0
  115. package/dist/esm/create.d.ts +8 -0
  116. package/dist/esm/create.d.ts.map +1 -0
  117. package/dist/esm/create.js +99 -0
  118. package/dist/esm/create.js.map +1 -0
  119. package/dist/esm/cwd-error.d.ts +8 -0
  120. package/dist/esm/cwd-error.d.ts.map +1 -0
  121. package/dist/esm/cwd-error.js +14 -0
  122. package/dist/esm/cwd-error.js.map +1 -0
  123. package/dist/esm/extract.d.ts +8 -0
  124. package/dist/esm/extract.d.ts.map +1 -0
  125. package/dist/esm/extract.js +111 -0
  126. package/dist/esm/extract.js.map +1 -0
  127. package/dist/esm/get-write-flag.d.ts +2 -0
  128. package/dist/esm/get-write-flag.d.ts.map +1 -0
  129. package/dist/esm/get-write-flag.js +23 -0
  130. package/dist/esm/get-write-flag.js.map +1 -0
  131. package/dist/esm/header.d.ts +54 -0
  132. package/dist/esm/header.d.ts.map +1 -0
  133. package/dist/esm/header.js +284 -0
  134. package/dist/esm/header.js.map +1 -0
  135. package/dist/esm/index.d.ts +19 -0
  136. package/dist/esm/index.d.ts.map +1 -0
  137. package/dist/esm/index.js +20 -0
  138. package/dist/esm/index.js.map +1 -0
  139. package/dist/esm/large-numbers.d.ts +4 -0
  140. package/dist/esm/large-numbers.d.ts.map +1 -0
  141. package/dist/esm/large-numbers.js +94 -0
  142. package/dist/esm/large-numbers.js.map +1 -0
  143. package/dist/esm/list.d.ts +8 -0
  144. package/dist/esm/list.d.ts.map +1 -0
  145. package/dist/esm/list.js +137 -0
  146. package/dist/esm/list.js.map +1 -0
  147. package/dist/esm/mkdir.d.ts +27 -0
  148. package/dist/esm/mkdir.d.ts.map +1 -0
  149. package/dist/esm/mkdir.js +201 -0
  150. package/dist/esm/mkdir.js.map +1 -0
  151. package/dist/esm/mode-fix.d.ts +2 -0
  152. package/dist/esm/mode-fix.d.ts.map +1 -0
  153. package/dist/esm/mode-fix.js +25 -0
  154. package/dist/esm/mode-fix.js.map +1 -0
  155. package/dist/esm/normalize-unicode.d.ts +2 -0
  156. package/dist/esm/normalize-unicode.d.ts.map +1 -0
  157. package/dist/esm/normalize-unicode.js +13 -0
  158. package/dist/esm/normalize-unicode.js.map +1 -0
  159. package/dist/esm/normalize-windows-path.d.ts +2 -0
  160. package/dist/esm/normalize-windows-path.d.ts.map +1 -0
  161. package/{lib → dist/esm}/normalize-windows-path.js +5 -4
  162. package/dist/esm/normalize-windows-path.js.map +1 -0
  163. package/dist/esm/options.d.ts +545 -0
  164. package/dist/esm/options.d.ts.map +1 -0
  165. package/dist/esm/options.js +48 -0
  166. package/dist/esm/options.js.map +1 -0
  167. package/dist/esm/pack.d.ts +98 -0
  168. package/dist/esm/pack.d.ts.map +1 -0
  169. package/dist/esm/pack.js +429 -0
  170. package/dist/esm/pack.js.map +1 -0
  171. package/dist/esm/package.json +3 -0
  172. package/dist/esm/parse.d.ts +84 -0
  173. package/dist/esm/parse.d.ts.map +1 -0
  174. package/dist/esm/parse.js +567 -0
  175. package/dist/esm/parse.js.map +1 -0
  176. package/dist/esm/path-reservations.d.ts +11 -0
  177. package/dist/esm/path-reservations.d.ts.map +1 -0
  178. package/dist/esm/path-reservations.js +165 -0
  179. package/dist/esm/path-reservations.js.map +1 -0
  180. package/dist/esm/pax.d.ts +27 -0
  181. package/dist/esm/pax.d.ts.map +1 -0
  182. package/dist/esm/pax.js +154 -0
  183. package/dist/esm/pax.js.map +1 -0
  184. package/dist/esm/read-entry.d.ts +37 -0
  185. package/dist/esm/read-entry.d.ts.map +1 -0
  186. package/dist/esm/read-entry.js +135 -0
  187. package/dist/esm/read-entry.js.map +1 -0
  188. package/dist/esm/replace.d.ts +5 -0
  189. package/dist/esm/replace.d.ts.map +1 -0
  190. package/dist/esm/replace.js +216 -0
  191. package/dist/esm/replace.js.map +1 -0
  192. package/dist/esm/strip-absolute-path.d.ts +2 -0
  193. package/dist/esm/strip-absolute-path.d.ts.map +1 -0
  194. package/dist/esm/strip-absolute-path.js +25 -0
  195. package/dist/esm/strip-absolute-path.js.map +1 -0
  196. package/dist/esm/strip-trailing-slashes.d.ts +2 -0
  197. package/dist/esm/strip-trailing-slashes.d.ts.map +1 -0
  198. package/dist/esm/strip-trailing-slashes.js +14 -0
  199. package/dist/esm/strip-trailing-slashes.js.map +1 -0
  200. package/dist/esm/symlink-error.d.ts +9 -0
  201. package/dist/esm/symlink-error.d.ts.map +1 -0
  202. package/dist/esm/symlink-error.js +15 -0
  203. package/dist/esm/symlink-error.js.map +1 -0
  204. package/dist/esm/types.d.ts +7 -0
  205. package/dist/esm/types.d.ts.map +1 -0
  206. package/dist/esm/types.js +45 -0
  207. package/dist/esm/types.js.map +1 -0
  208. package/dist/esm/unpack.d.ts +98 -0
  209. package/dist/esm/unpack.d.ts.map +1 -0
  210. package/dist/esm/unpack.js +890 -0
  211. package/dist/esm/unpack.js.map +1 -0
  212. package/dist/esm/update.d.ts +3 -0
  213. package/dist/esm/update.d.ts.map +1 -0
  214. package/dist/esm/update.js +43 -0
  215. package/dist/esm/update.js.map +1 -0
  216. package/dist/esm/warn-method.d.ts +20 -0
  217. package/dist/esm/warn-method.d.ts.map +1 -0
  218. package/dist/esm/warn-method.js +27 -0
  219. package/dist/esm/warn-method.js.map +1 -0
  220. package/dist/esm/winchars.d.ts +3 -0
  221. package/dist/esm/winchars.d.ts.map +1 -0
  222. package/dist/esm/winchars.js +9 -0
  223. package/dist/esm/winchars.js.map +1 -0
  224. package/dist/esm/write-entry.d.ts +125 -0
  225. package/dist/esm/write-entry.d.ts.map +1 -0
  226. package/dist/esm/write-entry.js +625 -0
  227. package/dist/esm/write-entry.js.map +1 -0
  228. package/package.json +255 -40
  229. package/index.js +0 -18
  230. package/lib/create.js +0 -111
  231. package/lib/extract.js +0 -113
  232. package/lib/get-write-flag.js +0 -20
  233. package/lib/header.js +0 -304
  234. package/lib/high-level-opt.js +0 -29
  235. package/lib/large-numbers.js +0 -104
  236. package/lib/list.js +0 -139
  237. package/lib/mkdir.js +0 -229
  238. package/lib/mode-fix.js +0 -27
  239. package/lib/normalize-unicode.js +0 -12
  240. package/lib/pack.js +0 -432
  241. package/lib/parse.js +0 -552
  242. package/lib/path-reservations.js +0 -156
  243. package/lib/pax.js +0 -150
  244. package/lib/read-entry.js +0 -107
  245. package/lib/replace.js +0 -246
  246. package/lib/strip-absolute-path.js +0 -24
  247. package/lib/strip-trailing-slashes.js +0 -13
  248. package/lib/types.js +0 -44
  249. package/lib/unpack.js +0 -923
  250. package/lib/update.js +0 -40
  251. package/lib/warn-mixin.js +0 -24
  252. package/lib/winchars.js +0 -23
  253. package/lib/write-entry.js +0 -546
@@ -0,0 +1,625 @@
1
+ import fs from 'fs';
2
+ import { Minipass } from 'minipass';
3
+ import path from 'path';
4
+ import { Header } from './header.js';
5
+ import { modeFix } from './mode-fix.js';
6
+ import { normalizeWindowsPath } from './normalize-windows-path.js';
7
+ import { dealias, } from './options.js';
8
+ import { Pax } from './pax.js';
9
+ import { stripAbsolutePath } from './strip-absolute-path.js';
10
+ import { stripTrailingSlashes } from './strip-trailing-slashes.js';
11
+ import { warnMethod } from './warn-method.js';
12
+ import * as winchars from './winchars.js';
13
+ const prefixPath = (path, prefix) => {
14
+ if (!prefix) {
15
+ return normalizeWindowsPath(path);
16
+ }
17
+ path = normalizeWindowsPath(path).replace(/^\.(\/|$)/, '');
18
+ return stripTrailingSlashes(prefix) + '/' + path;
19
+ };
20
+ const maxReadSize = 16 * 1024 * 1024;
21
+ const PROCESS = Symbol('process');
22
+ const FILE = Symbol('file');
23
+ const DIRECTORY = Symbol('directory');
24
+ const SYMLINK = Symbol('symlink');
25
+ const HARDLINK = Symbol('hardlink');
26
+ const HEADER = Symbol('header');
27
+ const READ = Symbol('read');
28
+ const LSTAT = Symbol('lstat');
29
+ const ONLSTAT = Symbol('onlstat');
30
+ const ONREAD = Symbol('onread');
31
+ const ONREADLINK = Symbol('onreadlink');
32
+ const OPENFILE = Symbol('openfile');
33
+ const ONOPENFILE = Symbol('onopenfile');
34
+ const CLOSE = Symbol('close');
35
+ const MODE = Symbol('mode');
36
+ const AWAITDRAIN = Symbol('awaitDrain');
37
+ const ONDRAIN = Symbol('ondrain');
38
+ const PREFIX = Symbol('prefix');
39
+ export class WriteEntry extends Minipass {
40
+ path;
41
+ portable;
42
+ myuid = (process.getuid && process.getuid()) || 0;
43
+ // until node has builtin pwnam functions, this'll have to do
44
+ myuser = process.env.USER || '';
45
+ maxReadSize;
46
+ linkCache;
47
+ statCache;
48
+ preservePaths;
49
+ cwd;
50
+ strict;
51
+ mtime;
52
+ noPax;
53
+ noMtime;
54
+ prefix;
55
+ fd;
56
+ blockLen = 0;
57
+ blockRemain = 0;
58
+ buf;
59
+ pos = 0;
60
+ remain = 0;
61
+ length = 0;
62
+ offset = 0;
63
+ win32;
64
+ absolute;
65
+ header;
66
+ type;
67
+ linkpath;
68
+ stat;
69
+ /* c8 ignore start */
70
+ #hadError = false;
71
+ constructor(p, opt_ = {}) {
72
+ const opt = dealias(opt_);
73
+ super();
74
+ this.path = normalizeWindowsPath(p);
75
+ // suppress atime, ctime, uid, gid, uname, gname
76
+ this.portable = !!opt.portable;
77
+ this.maxReadSize = opt.maxReadSize || maxReadSize;
78
+ this.linkCache = opt.linkCache || new Map();
79
+ this.statCache = opt.statCache || new Map();
80
+ this.preservePaths = !!opt.preservePaths;
81
+ this.cwd = normalizeWindowsPath(opt.cwd || process.cwd());
82
+ this.strict = !!opt.strict;
83
+ this.noPax = !!opt.noPax;
84
+ this.noMtime = !!opt.noMtime;
85
+ this.mtime = opt.mtime;
86
+ this.prefix = opt.prefix
87
+ ? normalizeWindowsPath(opt.prefix)
88
+ : undefined;
89
+ if (typeof opt.onwarn === 'function') {
90
+ this.on('warn', opt.onwarn);
91
+ }
92
+ let pathWarn = false;
93
+ if (!this.preservePaths) {
94
+ const [root, stripped] = stripAbsolutePath(this.path);
95
+ if (root && typeof stripped === 'string') {
96
+ this.path = stripped;
97
+ pathWarn = root;
98
+ }
99
+ }
100
+ this.win32 = !!opt.win32 || process.platform === 'win32';
101
+ if (this.win32) {
102
+ // force the \ to / normalization, since we might not *actually*
103
+ // be on windows, but want \ to be considered a path separator.
104
+ this.path = winchars.decode(this.path.replace(/\\/g, '/'));
105
+ p = p.replace(/\\/g, '/');
106
+ }
107
+ this.absolute = normalizeWindowsPath(opt.absolute || path.resolve(this.cwd, p));
108
+ if (this.path === '') {
109
+ this.path = './';
110
+ }
111
+ if (pathWarn) {
112
+ this.warn('TAR_ENTRY_INFO', `stripping ${pathWarn} from absolute path`, {
113
+ entry: this,
114
+ path: pathWarn + this.path,
115
+ });
116
+ }
117
+ const cs = this.statCache.get(this.absolute);
118
+ if (cs) {
119
+ this[ONLSTAT](cs);
120
+ }
121
+ else {
122
+ this[LSTAT]();
123
+ }
124
+ }
125
+ warn(code, message, data = {}) {
126
+ return warnMethod(this, code, message, data);
127
+ }
128
+ emit(ev, ...data) {
129
+ if (ev === 'error') {
130
+ this.#hadError = true;
131
+ }
132
+ return super.emit(ev, ...data);
133
+ }
134
+ [LSTAT]() {
135
+ fs.lstat(this.absolute, (er, stat) => {
136
+ if (er) {
137
+ return this.emit('error', er);
138
+ }
139
+ this[ONLSTAT](stat);
140
+ });
141
+ }
142
+ [ONLSTAT](stat) {
143
+ this.statCache.set(this.absolute, stat);
144
+ this.stat = stat;
145
+ if (!stat.isFile()) {
146
+ stat.size = 0;
147
+ }
148
+ this.type = getType(stat);
149
+ this.emit('stat', stat);
150
+ this[PROCESS]();
151
+ }
152
+ [PROCESS]() {
153
+ switch (this.type) {
154
+ case 'File':
155
+ return this[FILE]();
156
+ case 'Directory':
157
+ return this[DIRECTORY]();
158
+ case 'SymbolicLink':
159
+ return this[SYMLINK]();
160
+ // unsupported types are ignored.
161
+ default:
162
+ return this.end();
163
+ }
164
+ }
165
+ [MODE](mode) {
166
+ return modeFix(mode, this.type === 'Directory', this.portable);
167
+ }
168
+ [PREFIX](path) {
169
+ return prefixPath(path, this.prefix);
170
+ }
171
+ [HEADER]() {
172
+ /* c8 ignore start */
173
+ if (!this.stat) {
174
+ throw new Error('cannot write header before stat');
175
+ }
176
+ /* c8 ignore stop */
177
+ if (this.type === 'Directory' && this.portable) {
178
+ this.noMtime = true;
179
+ }
180
+ this.header = new Header({
181
+ path: this[PREFIX](this.path),
182
+ // only apply the prefix to hard links.
183
+ linkpath: this.type === 'Link' && this.linkpath !== undefined
184
+ ? this[PREFIX](this.linkpath)
185
+ : this.linkpath,
186
+ // only the permissions and setuid/setgid/sticky bitflags
187
+ // not the higher-order bits that specify file type
188
+ mode: this[MODE](this.stat.mode),
189
+ uid: this.portable ? undefined : this.stat.uid,
190
+ gid: this.portable ? undefined : this.stat.gid,
191
+ size: this.stat.size,
192
+ mtime: this.noMtime ? undefined : this.mtime || this.stat.mtime,
193
+ /* c8 ignore next */
194
+ type: this.type === 'Unsupported' ? undefined : this.type,
195
+ uname: this.portable
196
+ ? undefined
197
+ : this.stat.uid === this.myuid
198
+ ? this.myuser
199
+ : '',
200
+ atime: this.portable ? undefined : this.stat.atime,
201
+ ctime: this.portable ? undefined : this.stat.ctime,
202
+ });
203
+ if (this.header.encode() && !this.noPax) {
204
+ super.write(new Pax({
205
+ atime: this.portable ? undefined : this.header.atime,
206
+ ctime: this.portable ? undefined : this.header.ctime,
207
+ gid: this.portable ? undefined : this.header.gid,
208
+ mtime: this.noMtime
209
+ ? undefined
210
+ : this.mtime || this.header.mtime,
211
+ path: this[PREFIX](this.path),
212
+ linkpath: this.type === 'Link' && this.linkpath !== undefined
213
+ ? this[PREFIX](this.linkpath)
214
+ : this.linkpath,
215
+ size: this.header.size,
216
+ uid: this.portable ? undefined : this.header.uid,
217
+ uname: this.portable ? undefined : this.header.uname,
218
+ dev: this.portable ? undefined : this.stat.dev,
219
+ ino: this.portable ? undefined : this.stat.ino,
220
+ nlink: this.portable ? undefined : this.stat.nlink,
221
+ }).encode());
222
+ }
223
+ const block = this.header?.block;
224
+ /* c8 ignore start */
225
+ if (!block) {
226
+ throw new Error('failed to encode header');
227
+ }
228
+ /* c8 ignore stop */
229
+ super.write(block);
230
+ }
231
+ [DIRECTORY]() {
232
+ /* c8 ignore start */
233
+ if (!this.stat) {
234
+ throw new Error('cannot create directory entry without stat');
235
+ }
236
+ /* c8 ignore stop */
237
+ if (this.path.slice(-1) !== '/') {
238
+ this.path += '/';
239
+ }
240
+ this.stat.size = 0;
241
+ this[HEADER]();
242
+ this.end();
243
+ }
244
+ [SYMLINK]() {
245
+ fs.readlink(this.absolute, (er, linkpath) => {
246
+ if (er) {
247
+ return this.emit('error', er);
248
+ }
249
+ this[ONREADLINK](linkpath);
250
+ });
251
+ }
252
+ [ONREADLINK](linkpath) {
253
+ this.linkpath = normalizeWindowsPath(linkpath);
254
+ this[HEADER]();
255
+ this.end();
256
+ }
257
+ [HARDLINK](linkpath) {
258
+ /* c8 ignore start */
259
+ if (!this.stat) {
260
+ throw new Error('cannot create link entry without stat');
261
+ }
262
+ /* c8 ignore stop */
263
+ this.type = 'Link';
264
+ this.linkpath = normalizeWindowsPath(path.relative(this.cwd, linkpath));
265
+ this.stat.size = 0;
266
+ this[HEADER]();
267
+ this.end();
268
+ }
269
+ [FILE]() {
270
+ /* c8 ignore start */
271
+ if (!this.stat) {
272
+ throw new Error('cannot create file entry without stat');
273
+ }
274
+ /* c8 ignore stop */
275
+ if (this.stat.nlink > 1) {
276
+ const linkKey = `${this.stat.dev}:${this.stat.ino}`;
277
+ const linkpath = this.linkCache.get(linkKey);
278
+ if (linkpath?.indexOf(this.cwd) === 0) {
279
+ return this[HARDLINK](linkpath);
280
+ }
281
+ this.linkCache.set(linkKey, this.absolute);
282
+ }
283
+ this[HEADER]();
284
+ if (this.stat.size === 0) {
285
+ return this.end();
286
+ }
287
+ this[OPENFILE]();
288
+ }
289
+ [OPENFILE]() {
290
+ fs.open(this.absolute, 'r', (er, fd) => {
291
+ if (er) {
292
+ return this.emit('error', er);
293
+ }
294
+ this[ONOPENFILE](fd);
295
+ });
296
+ }
297
+ [ONOPENFILE](fd) {
298
+ this.fd = fd;
299
+ if (this.#hadError) {
300
+ return this[CLOSE]();
301
+ }
302
+ /* c8 ignore start */
303
+ if (!this.stat) {
304
+ throw new Error('should stat before calling onopenfile');
305
+ }
306
+ /* c8 ignore start */
307
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512);
308
+ this.blockRemain = this.blockLen;
309
+ const bufLen = Math.min(this.blockLen, this.maxReadSize);
310
+ this.buf = Buffer.allocUnsafe(bufLen);
311
+ this.offset = 0;
312
+ this.pos = 0;
313
+ this.remain = this.stat.size;
314
+ this.length = this.buf.length;
315
+ this[READ]();
316
+ }
317
+ [READ]() {
318
+ const { fd, buf, offset, length, pos } = this;
319
+ if (fd === undefined || buf === undefined) {
320
+ throw new Error('cannot read file without first opening');
321
+ }
322
+ fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
323
+ if (er) {
324
+ // ignoring the error from close(2) is a bad practice, but at
325
+ // this point we already have an error, don't need another one
326
+ return this[CLOSE](() => this.emit('error', er));
327
+ }
328
+ this[ONREAD](bytesRead);
329
+ });
330
+ }
331
+ /* c8 ignore start */
332
+ [CLOSE](cb = () => { }) {
333
+ /* c8 ignore stop */
334
+ if (this.fd !== undefined)
335
+ fs.close(this.fd, cb);
336
+ }
337
+ [ONREAD](bytesRead) {
338
+ if (bytesRead <= 0 && this.remain > 0) {
339
+ const er = Object.assign(new Error('encountered unexpected EOF'), {
340
+ path: this.absolute,
341
+ syscall: 'read',
342
+ code: 'EOF',
343
+ });
344
+ return this[CLOSE](() => this.emit('error', er));
345
+ }
346
+ if (bytesRead > this.remain) {
347
+ const er = Object.assign(new Error('did not encounter expected EOF'), {
348
+ path: this.absolute,
349
+ syscall: 'read',
350
+ code: 'EOF',
351
+ });
352
+ return this[CLOSE](() => this.emit('error', er));
353
+ }
354
+ /* c8 ignore start */
355
+ if (!this.buf) {
356
+ throw new Error('should have created buffer prior to reading');
357
+ }
358
+ /* c8 ignore stop */
359
+ // null out the rest of the buffer, if we could fit the block padding
360
+ // at the end of this loop, we've incremented bytesRead and this.remain
361
+ // to be incremented up to the blockRemain level, as if we had expected
362
+ // to get a null-padded file, and read it until the end. then we will
363
+ // decrement both remain and blockRemain by bytesRead, and know that we
364
+ // reached the expected EOF, without any null buffer to append.
365
+ if (bytesRead === this.remain) {
366
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
367
+ this.buf[i + this.offset] = 0;
368
+ bytesRead++;
369
+ this.remain++;
370
+ }
371
+ }
372
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length
373
+ ? this.buf
374
+ : this.buf.subarray(this.offset, this.offset + bytesRead);
375
+ const flushed = this.write(writeBuf);
376
+ if (!flushed) {
377
+ this[AWAITDRAIN](() => this[ONDRAIN]());
378
+ }
379
+ else {
380
+ this[ONDRAIN]();
381
+ }
382
+ }
383
+ [AWAITDRAIN](cb) {
384
+ this.once('drain', cb);
385
+ }
386
+ write(writeBuf) {
387
+ if (this.blockRemain < writeBuf.length) {
388
+ const er = Object.assign(new Error('writing more data than expected'), {
389
+ path: this.absolute,
390
+ });
391
+ return this.emit('error', er);
392
+ }
393
+ this.remain -= writeBuf.length;
394
+ this.blockRemain -= writeBuf.length;
395
+ this.pos += writeBuf.length;
396
+ this.offset += writeBuf.length;
397
+ return super.write(writeBuf);
398
+ }
399
+ [ONDRAIN]() {
400
+ if (!this.remain) {
401
+ if (this.blockRemain) {
402
+ super.write(Buffer.alloc(this.blockRemain));
403
+ }
404
+ return this[CLOSE](er => er ? this.emit('error', er) : this.end());
405
+ }
406
+ /* c8 ignore start */
407
+ if (!this.buf) {
408
+ throw new Error('buffer lost somehow in ONDRAIN');
409
+ }
410
+ /* c8 ignore stop */
411
+ if (this.offset >= this.length) {
412
+ // if we only have a smaller bit left to read, alloc a smaller buffer
413
+ // otherwise, keep it the same length it was before.
414
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length));
415
+ this.offset = 0;
416
+ }
417
+ this.length = this.buf.length - this.offset;
418
+ this[READ]();
419
+ }
420
+ }
421
+ export class WriteEntrySync extends WriteEntry {
422
+ [LSTAT]() {
423
+ this[ONLSTAT](fs.lstatSync(this.absolute));
424
+ }
425
+ [SYMLINK]() {
426
+ this[ONREADLINK](fs.readlinkSync(this.absolute));
427
+ }
428
+ [OPENFILE]() {
429
+ this[ONOPENFILE](fs.openSync(this.absolute, 'r'));
430
+ }
431
+ [READ]() {
432
+ let threw = true;
433
+ try {
434
+ const { fd, buf, offset, length, pos } = this;
435
+ /* c8 ignore start */
436
+ if (fd === undefined || buf === undefined) {
437
+ throw new Error('fd and buf must be set in READ method');
438
+ }
439
+ /* c8 ignore stop */
440
+ const bytesRead = fs.readSync(fd, buf, offset, length, pos);
441
+ this[ONREAD](bytesRead);
442
+ threw = false;
443
+ }
444
+ finally {
445
+ // ignoring the error from close(2) is a bad practice, but at
446
+ // this point we already have an error, don't need another one
447
+ if (threw) {
448
+ try {
449
+ this[CLOSE](() => { });
450
+ }
451
+ catch (er) { }
452
+ }
453
+ }
454
+ }
455
+ [AWAITDRAIN](cb) {
456
+ cb();
457
+ }
458
+ /* c8 ignore start */
459
+ [CLOSE](cb = () => { }) {
460
+ /* c8 ignore stop */
461
+ if (this.fd !== undefined)
462
+ fs.closeSync(this.fd);
463
+ cb();
464
+ }
465
+ }
466
+ export class WriteEntryTar extends Minipass {
467
+ blockLen = 0;
468
+ blockRemain = 0;
469
+ buf = 0;
470
+ pos = 0;
471
+ remain = 0;
472
+ length = 0;
473
+ preservePaths;
474
+ portable;
475
+ strict;
476
+ noPax;
477
+ noMtime;
478
+ readEntry;
479
+ type;
480
+ prefix;
481
+ path;
482
+ mode;
483
+ uid;
484
+ gid;
485
+ uname;
486
+ gname;
487
+ header;
488
+ mtime;
489
+ atime;
490
+ ctime;
491
+ linkpath;
492
+ size;
493
+ warn(code, message, data = {}) {
494
+ return warnMethod(this, code, message, data);
495
+ }
496
+ constructor(readEntry, opt_ = {}) {
497
+ const opt = dealias(opt_);
498
+ super();
499
+ this.preservePaths = !!opt.preservePaths;
500
+ this.portable = !!opt.portable;
501
+ this.strict = !!opt.strict;
502
+ this.noPax = !!opt.noPax;
503
+ this.noMtime = !!opt.noMtime;
504
+ this.readEntry = readEntry;
505
+ const { type } = readEntry;
506
+ /* c8 ignore start */
507
+ if (type === 'Unsupported') {
508
+ throw new Error('writing entry that should be ignored');
509
+ }
510
+ /* c8 ignore stop */
511
+ this.type = type;
512
+ if (this.type === 'Directory' && this.portable) {
513
+ this.noMtime = true;
514
+ }
515
+ this.prefix = opt.prefix;
516
+ this.path = normalizeWindowsPath(readEntry.path);
517
+ this.mode =
518
+ readEntry.mode !== undefined
519
+ ? this[MODE](readEntry.mode)
520
+ : undefined;
521
+ this.uid = this.portable ? undefined : readEntry.uid;
522
+ this.gid = this.portable ? undefined : readEntry.gid;
523
+ this.uname = this.portable ? undefined : readEntry.uname;
524
+ this.gname = this.portable ? undefined : readEntry.gname;
525
+ this.size = readEntry.size;
526
+ this.mtime = this.noMtime
527
+ ? undefined
528
+ : opt.mtime || readEntry.mtime;
529
+ this.atime = this.portable ? undefined : readEntry.atime;
530
+ this.ctime = this.portable ? undefined : readEntry.ctime;
531
+ this.linkpath =
532
+ readEntry.linkpath !== undefined
533
+ ? normalizeWindowsPath(readEntry.linkpath)
534
+ : undefined;
535
+ if (typeof opt.onwarn === 'function') {
536
+ this.on('warn', opt.onwarn);
537
+ }
538
+ let pathWarn = false;
539
+ if (!this.preservePaths) {
540
+ const [root, stripped] = stripAbsolutePath(this.path);
541
+ if (root && typeof stripped === 'string') {
542
+ this.path = stripped;
543
+ pathWarn = root;
544
+ }
545
+ }
546
+ this.remain = readEntry.size;
547
+ this.blockRemain = readEntry.startBlockSize;
548
+ this.header = new Header({
549
+ path: this[PREFIX](this.path),
550
+ linkpath: this.type === 'Link' && this.linkpath !== undefined
551
+ ? this[PREFIX](this.linkpath)
552
+ : this.linkpath,
553
+ // only the permissions and setuid/setgid/sticky bitflags
554
+ // not the higher-order bits that specify file type
555
+ mode: this.mode,
556
+ uid: this.portable ? undefined : this.uid,
557
+ gid: this.portable ? undefined : this.gid,
558
+ size: this.size,
559
+ mtime: this.noMtime ? undefined : this.mtime,
560
+ type: this.type,
561
+ uname: this.portable ? undefined : this.uname,
562
+ atime: this.portable ? undefined : this.atime,
563
+ ctime: this.portable ? undefined : this.ctime,
564
+ });
565
+ if (pathWarn) {
566
+ this.warn('TAR_ENTRY_INFO', `stripping ${pathWarn} from absolute path`, {
567
+ entry: this,
568
+ path: pathWarn + this.path,
569
+ });
570
+ }
571
+ if (this.header.encode() && !this.noPax) {
572
+ super.write(new Pax({
573
+ atime: this.portable ? undefined : this.atime,
574
+ ctime: this.portable ? undefined : this.ctime,
575
+ gid: this.portable ? undefined : this.gid,
576
+ mtime: this.noMtime ? undefined : this.mtime,
577
+ path: this[PREFIX](this.path),
578
+ linkpath: this.type === 'Link' && this.linkpath !== undefined
579
+ ? this[PREFIX](this.linkpath)
580
+ : this.linkpath,
581
+ size: this.size,
582
+ uid: this.portable ? undefined : this.uid,
583
+ uname: this.portable ? undefined : this.uname,
584
+ dev: this.portable ? undefined : this.readEntry.dev,
585
+ ino: this.portable ? undefined : this.readEntry.ino,
586
+ nlink: this.portable ? undefined : this.readEntry.nlink,
587
+ }).encode());
588
+ }
589
+ const b = this.header?.block;
590
+ /* c8 ignore start */
591
+ if (!b)
592
+ throw new Error('failed to encode header');
593
+ /* c8 ignore stop */
594
+ super.write(b);
595
+ readEntry.pipe(this);
596
+ }
597
+ [PREFIX](path) {
598
+ return prefixPath(path, this.prefix);
599
+ }
600
+ [MODE](mode) {
601
+ return modeFix(mode, this.type === 'Directory', this.portable);
602
+ }
603
+ write(data) {
604
+ const writeLen = data.length;
605
+ if (writeLen > this.blockRemain) {
606
+ throw new Error('writing more to entry than is appropriate');
607
+ }
608
+ this.blockRemain -= writeLen;
609
+ return super.write(data);
610
+ }
611
+ end() {
612
+ if (this.blockRemain) {
613
+ super.write(Buffer.alloc(this.blockRemain));
614
+ }
615
+ return super.end();
616
+ }
617
+ }
618
+ const getType = (stat) => stat.isFile()
619
+ ? 'File'
620
+ : stat.isDirectory()
621
+ ? 'Directory'
622
+ : stat.isSymbolicLink()
623
+ ? 'SymbolicLink'
624
+ : 'Unsupported';
625
+ //# sourceMappingURL=write-entry.js.map