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