archiver-node 8.0.2 → 8.0.4
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.
- package/cjs/Archiver.js +771 -668
- package/cjs/archivers/JsonArchive.js +35 -42
- package/cjs/archivers/TarArchive.js +35 -42
- package/cjs/archivers/ZipArchive.js +35 -42
- package/cjs/error.js +10 -38
- package/cjs/lazystream.js +97 -51
- package/cjs/plugins/json.js +106 -102
- package/cjs/plugins/tar.js +128 -131
- package/cjs/plugins/zip.js +90 -95
- package/cjs/readdir-glob.js +382 -0
- package/cjs/utils.js +27 -55
- package/lib/Archiver.js +6 -1
- package/lib/readdir-glob.js +216 -0
- package/package.json +6 -3
package/cjs/Archiver.js
CHANGED
|
@@ -1,172 +1,184 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var Archiver_exports = {};
|
|
19
|
-
__export(Archiver_exports, {
|
|
20
|
-
default: () => Archiver
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
21
5
|
});
|
|
22
|
-
|
|
23
|
-
var
|
|
24
|
-
var
|
|
25
|
-
var
|
|
26
|
-
var
|
|
27
|
-
var
|
|
28
|
-
var
|
|
29
|
-
var
|
|
30
|
-
var
|
|
31
|
-
var
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
var _fs = require("fs");
|
|
8
|
+
var _isStream = require("is-stream");
|
|
9
|
+
var _readdirGlob = require("./readdir-glob.js");
|
|
10
|
+
var _lazystream = require("./lazystream.js");
|
|
11
|
+
var _async = require("async");
|
|
12
|
+
var _path = require("path");
|
|
13
|
+
var _error = require("./error.js");
|
|
14
|
+
var _readableStream = require("readable-stream");
|
|
15
|
+
var _utils = require("./utils.js");
|
|
16
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
17
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
18
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
19
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
20
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
21
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
22
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
23
|
+
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
|
24
|
+
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
|
25
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
26
|
+
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
|
27
|
+
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
28
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
29
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
30
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
31
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // `readdir-glob` has different exports in `esm` and `cjs`:
|
|
32
|
+
// in `esm` it's a named export but in `cjs` it's a default export.
|
|
33
|
+
// https://github.com/Yqnn/node-readdir-glob/issues/28
|
|
34
|
+
// As a result, this package's code can't be transpiled properly.
|
|
35
|
+
// Copy-pasted `readdir-glob` code here to fix that.
|
|
36
|
+
var ReaddirGlob = _readdirGlob.readdirGlob.ReaddirGlob;
|
|
37
|
+
var win32 = process.platform === "win32";
|
|
38
|
+
var Archiver = exports["default"] = /*#__PURE__*/function (_Transform) {
|
|
39
|
+
/**
|
|
40
|
+
* @constructor
|
|
41
|
+
* @param {String} format The archive format to use.
|
|
42
|
+
* @param {(CoreOptions|TransformOptions)} options See also {@link ZipOptions} and {@link TarOptions}.
|
|
41
43
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
function Archiver(options) {
|
|
45
|
+
var _this;
|
|
46
|
+
_classCallCheck(this, Archiver);
|
|
47
|
+
options = _objectSpread({
|
|
44
48
|
highWaterMark: 1024 * 1024,
|
|
45
|
-
statConcurrency: 4
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
this._statQueue.drain(this._onQueueDrain.bind(this));
|
|
65
|
-
this._state = {
|
|
49
|
+
statConcurrency: 4
|
|
50
|
+
}, options);
|
|
51
|
+
_this = _callSuper(this, Archiver, [options]);
|
|
52
|
+
_defineProperty(_this, "_supportsDirectory", false);
|
|
53
|
+
_defineProperty(_this, "_supportsSymlink", false);
|
|
54
|
+
_this.options = options;
|
|
55
|
+
_this._format = false;
|
|
56
|
+
_this._module = false;
|
|
57
|
+
_this._pending = 0;
|
|
58
|
+
_this._pointer = 0;
|
|
59
|
+
_this._entriesCount = 0;
|
|
60
|
+
_this._entriesProcessedCount = 0;
|
|
61
|
+
_this._fsEntriesTotalBytes = 0;
|
|
62
|
+
_this._fsEntriesProcessedBytes = 0;
|
|
63
|
+
_this._queue = (0, _async.queue)(_this._onQueueTask.bind(_this), 1);
|
|
64
|
+
_this._queue.drain(_this._onQueueDrain.bind(_this));
|
|
65
|
+
_this._statQueue = (0, _async.queue)(_this._onStatQueueTask.bind(_this), options.statConcurrency);
|
|
66
|
+
_this._statQueue.drain(_this._onQueueDrain.bind(_this));
|
|
67
|
+
_this._state = {
|
|
66
68
|
aborted: false,
|
|
67
69
|
finalize: false,
|
|
68
70
|
finalizing: false,
|
|
69
71
|
finalized: false,
|
|
70
72
|
modulePiped: false
|
|
71
73
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Internal logic for `abort`.
|
|
76
|
-
*
|
|
77
|
-
* @private
|
|
78
|
-
* @return void
|
|
79
|
-
*/
|
|
80
|
-
_abort() {
|
|
81
|
-
this._state.aborted = true;
|
|
82
|
-
this._queue.kill();
|
|
83
|
-
this._statQueue.kill();
|
|
84
|
-
if (this._queue.idle()) {
|
|
85
|
-
this._shutdown();
|
|
86
|
-
}
|
|
74
|
+
_this._streams = [];
|
|
75
|
+
return _this;
|
|
87
76
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
93
|
-
* @
|
|
94
|
-
* @return void
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Internal logic for `abort`.
|
|
80
|
+
*
|
|
81
|
+
* @private
|
|
82
|
+
* @return void
|
|
95
83
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
84
|
+
_inherits(Archiver, _Transform);
|
|
85
|
+
return _createClass(Archiver, [{
|
|
86
|
+
key: "_abort",
|
|
87
|
+
value: function _abort() {
|
|
88
|
+
this._state.aborted = true;
|
|
89
|
+
this._queue.kill();
|
|
90
|
+
this._statQueue.kill();
|
|
91
|
+
if (this._queue.idle()) {
|
|
92
|
+
this._shutdown();
|
|
93
|
+
}
|
|
104
94
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Internal helper for appending files.
|
|
97
|
+
*
|
|
98
|
+
* @private
|
|
99
|
+
* @param {String} filepath The source filepath.
|
|
100
|
+
* @param {EntryData} data The entry data.
|
|
101
|
+
* @return void
|
|
102
|
+
*/
|
|
103
|
+
}, {
|
|
104
|
+
key: "_append",
|
|
105
|
+
value: function _append(filepath, data) {
|
|
106
|
+
data = data || {};
|
|
107
|
+
var task = {
|
|
108
|
+
source: null,
|
|
109
|
+
filepath: filepath
|
|
110
|
+
};
|
|
111
|
+
if (!data.name) {
|
|
112
|
+
data.name = filepath;
|
|
113
|
+
}
|
|
114
|
+
data.sourcePath = filepath;
|
|
115
|
+
task.data = data;
|
|
116
|
+
this._entriesCount++;
|
|
117
|
+
if (data.stats && data.stats instanceof _fs.Stats) {
|
|
118
|
+
task = this._updateQueueTaskWithStats(task, data.stats);
|
|
119
|
+
if (task) {
|
|
120
|
+
if (data.stats.size) {
|
|
121
|
+
this._fsEntriesTotalBytes += data.stats.size;
|
|
122
|
+
}
|
|
123
|
+
this._queue.push(task);
|
|
113
124
|
}
|
|
114
|
-
|
|
125
|
+
} else {
|
|
126
|
+
this._statQueue.push(task);
|
|
115
127
|
}
|
|
116
|
-
} else {
|
|
117
|
-
this._statQueue.push(task);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Internal logic for `finalize`.
|
|
122
|
-
*
|
|
123
|
-
* @private
|
|
124
|
-
* @return void
|
|
125
|
-
*/
|
|
126
|
-
_finalize() {
|
|
127
|
-
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
128
|
-
return;
|
|
129
128
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Internal logic for `finalize`.
|
|
131
|
+
*
|
|
132
|
+
* @private
|
|
133
|
+
* @return void
|
|
134
|
+
*/
|
|
135
|
+
}, {
|
|
136
|
+
key: "_finalize",
|
|
137
|
+
value: function _finalize() {
|
|
138
|
+
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
this._state.finalizing = true;
|
|
142
|
+
this._moduleFinalize();
|
|
143
|
+
this._state.finalizing = false;
|
|
144
|
+
this._state.finalized = true;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Checks the various state variables to determine if we can `finalize`.
|
|
148
|
+
*
|
|
149
|
+
* @private
|
|
150
|
+
* @return {Boolean}
|
|
151
|
+
*/
|
|
152
|
+
}, {
|
|
153
|
+
key: "_maybeFinalize",
|
|
154
|
+
value: function _maybeFinalize() {
|
|
155
|
+
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (this._state.finalize && this._pending === 0 && this._queue.idle() && this._statQueue.idle()) {
|
|
159
|
+
this._finalize();
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
143
162
|
return false;
|
|
144
163
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
callback();
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
this._module.append(
|
|
167
|
-
source,
|
|
168
|
-
data,
|
|
169
|
-
function(err) {
|
|
164
|
+
/**
|
|
165
|
+
* Appends an entry to the module.
|
|
166
|
+
*
|
|
167
|
+
* @private
|
|
168
|
+
* @fires Archiver#entry
|
|
169
|
+
* @param {(Buffer|Stream)} source
|
|
170
|
+
* @param {EntryData} data
|
|
171
|
+
* @param {Function} callback
|
|
172
|
+
* @return void
|
|
173
|
+
*/
|
|
174
|
+
}, {
|
|
175
|
+
key: "_moduleAppend",
|
|
176
|
+
value: function _moduleAppend(source, data, callback) {
|
|
177
|
+
if (this._state.aborted) {
|
|
178
|
+
callback();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this._module.append(source, data, function (err) {
|
|
170
182
|
this._task = null;
|
|
171
183
|
if (this._state.aborted) {
|
|
172
184
|
this._shutdown();
|
|
@@ -177,11 +189,21 @@ class Archiver extends import_readable_stream.Transform {
|
|
|
177
189
|
setImmediate(callback);
|
|
178
190
|
return;
|
|
179
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Fires when the entry's input has been processed and appended to the archive.
|
|
194
|
+
*
|
|
195
|
+
* @event Archiver#entry
|
|
196
|
+
* @type {EntryData}
|
|
197
|
+
*/
|
|
180
198
|
this.emit("entry", data);
|
|
181
199
|
this._entriesProcessedCount++;
|
|
182
200
|
if (data.stats && data.stats.size) {
|
|
183
201
|
this._fsEntriesProcessedBytes += data.stats.size;
|
|
184
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* @event Archiver#progress
|
|
205
|
+
* @type {ProgressData}
|
|
206
|
+
*/
|
|
185
207
|
this.emit("progress", {
|
|
186
208
|
entries: {
|
|
187
209
|
total: this._entriesCount,
|
|
@@ -193,175 +215,197 @@ class Archiver extends import_readable_stream.Transform {
|
|
|
193
215
|
}
|
|
194
216
|
});
|
|
195
217
|
setImmediate(callback);
|
|
196
|
-
}.bind(this)
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
this._module.finalize
|
|
208
|
-
|
|
209
|
-
this._module.end
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Pipes the module to our internal stream with error bubbling.
|
|
216
|
-
*
|
|
217
|
-
* @private
|
|
218
|
-
* @return void
|
|
219
|
-
*/
|
|
220
|
-
_modulePipe() {
|
|
221
|
-
this._module.on("error", this._onModuleError.bind(this));
|
|
222
|
-
this._module.pipe(this);
|
|
223
|
-
this._state.modulePiped = true;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Unpipes the module from our internal stream.
|
|
227
|
-
*
|
|
228
|
-
* @private
|
|
229
|
-
* @return void
|
|
230
|
-
*/
|
|
231
|
-
_moduleUnpipe() {
|
|
232
|
-
this._module.unpipe(this);
|
|
233
|
-
this._state.modulePiped = false;
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Normalizes entry data with fallbacks for key properties.
|
|
237
|
-
*
|
|
238
|
-
* @private
|
|
239
|
-
* @param {Object} data
|
|
240
|
-
* @param {fs.Stats} stats
|
|
241
|
-
* @return {Object}
|
|
242
|
-
*/
|
|
243
|
-
_normalizeEntryData(data, stats) {
|
|
244
|
-
data = {
|
|
245
|
-
type: "file",
|
|
246
|
-
name: null,
|
|
247
|
-
date: null,
|
|
248
|
-
mode: null,
|
|
249
|
-
prefix: null,
|
|
250
|
-
sourcePath: null,
|
|
251
|
-
stats: false,
|
|
252
|
-
...data
|
|
253
|
-
};
|
|
254
|
-
if (stats && data.stats === false) {
|
|
255
|
-
data.stats = stats;
|
|
256
|
-
}
|
|
257
|
-
let isDir = data.type === "directory";
|
|
258
|
-
if (data.name) {
|
|
259
|
-
if (typeof data.prefix === "string" && "" !== data.prefix) {
|
|
260
|
-
data.name = data.prefix + "/" + data.name;
|
|
261
|
-
data.prefix = null;
|
|
262
|
-
}
|
|
263
|
-
data.name = (0, import_utils.sanitizePath)(data.name);
|
|
264
|
-
if (data.type !== "symlink" && data.name.slice(-1) === "/") {
|
|
265
|
-
isDir = true;
|
|
266
|
-
data.type = "directory";
|
|
267
|
-
} else if (isDir) {
|
|
268
|
-
data.name += "/";
|
|
218
|
+
}.bind(this));
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Finalizes the module.
|
|
222
|
+
*
|
|
223
|
+
* @private
|
|
224
|
+
* @return void
|
|
225
|
+
*/
|
|
226
|
+
}, {
|
|
227
|
+
key: "_moduleFinalize",
|
|
228
|
+
value: function _moduleFinalize() {
|
|
229
|
+
if (typeof this._module.finalize === "function") {
|
|
230
|
+
this._module.finalize();
|
|
231
|
+
} else if (typeof this._module.end === "function") {
|
|
232
|
+
this._module.end();
|
|
233
|
+
} else {
|
|
234
|
+
this.emit("error", new _error.ArchiverError("NOENDMETHOD"));
|
|
269
235
|
}
|
|
270
236
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
237
|
+
/**
|
|
238
|
+
* Pipes the module to our internal stream with error bubbling.
|
|
239
|
+
*
|
|
240
|
+
* @private
|
|
241
|
+
* @return void
|
|
242
|
+
*/
|
|
243
|
+
}, {
|
|
244
|
+
key: "_modulePipe",
|
|
245
|
+
value: function _modulePipe() {
|
|
246
|
+
this._module.on("error", this._onModuleError.bind(this));
|
|
247
|
+
this._module.pipe(this);
|
|
248
|
+
this._state.modulePiped = true;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Unpipes the module from our internal stream.
|
|
252
|
+
*
|
|
253
|
+
* @private
|
|
254
|
+
* @return void
|
|
255
|
+
*/
|
|
256
|
+
}, {
|
|
257
|
+
key: "_moduleUnpipe",
|
|
258
|
+
value: function _moduleUnpipe() {
|
|
259
|
+
this._module.unpipe(this);
|
|
260
|
+
this._state.modulePiped = false;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Normalizes entry data with fallbacks for key properties.
|
|
264
|
+
*
|
|
265
|
+
* @private
|
|
266
|
+
* @param {Object} data
|
|
267
|
+
* @param {fs.Stats} stats
|
|
268
|
+
* @return {Object}
|
|
269
|
+
*/
|
|
270
|
+
}, {
|
|
271
|
+
key: "_normalizeEntryData",
|
|
272
|
+
value: function _normalizeEntryData(data, stats) {
|
|
273
|
+
data = _objectSpread({
|
|
274
|
+
type: "file",
|
|
275
|
+
name: null,
|
|
276
|
+
date: null,
|
|
277
|
+
mode: null,
|
|
278
|
+
prefix: null,
|
|
279
|
+
sourcePath: null,
|
|
280
|
+
stats: false
|
|
281
|
+
}, data);
|
|
282
|
+
if (stats && data.stats === false) {
|
|
283
|
+
data.stats = stats;
|
|
276
284
|
}
|
|
277
|
-
|
|
278
|
-
if (
|
|
279
|
-
data.
|
|
280
|
-
|
|
281
|
-
|
|
285
|
+
var isDir = data.type === "directory";
|
|
286
|
+
if (data.name) {
|
|
287
|
+
if (typeof data.prefix === "string" && "" !== data.prefix) {
|
|
288
|
+
data.name = data.prefix + "/" + data.name;
|
|
289
|
+
data.prefix = null;
|
|
290
|
+
}
|
|
291
|
+
data.name = (0, _utils.sanitizePath)(data.name);
|
|
292
|
+
if (data.type !== "symlink" && data.name.slice(-1) === "/") {
|
|
293
|
+
isDir = true;
|
|
294
|
+
data.type = "directory";
|
|
295
|
+
} else if (isDir) {
|
|
296
|
+
data.name += "/";
|
|
297
|
+
}
|
|
282
298
|
}
|
|
283
|
-
|
|
284
|
-
|
|
299
|
+
// 511 === 0777; 493 === 0755; 438 === 0666; 420 === 0644
|
|
300
|
+
if (typeof data.mode === "number") {
|
|
301
|
+
if (win32) {
|
|
302
|
+
data.mode &= 511;
|
|
303
|
+
} else {
|
|
304
|
+
data.mode &= 4095;
|
|
305
|
+
}
|
|
306
|
+
} else if (data.stats && data.mode === null) {
|
|
307
|
+
if (win32) {
|
|
308
|
+
data.mode = data.stats.mode & 511;
|
|
309
|
+
} else {
|
|
310
|
+
data.mode = data.stats.mode & 4095;
|
|
311
|
+
}
|
|
312
|
+
// stat isn't reliable on windows; force 0755 for dir
|
|
313
|
+
if (win32 && isDir) {
|
|
314
|
+
data.mode = 493;
|
|
315
|
+
}
|
|
316
|
+
} else if (data.mode === null) {
|
|
317
|
+
data.mode = isDir ? 493 : 420;
|
|
285
318
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
data.date = data.stats.mtime;
|
|
291
|
-
} else {
|
|
292
|
-
data.date = (0, import_utils.dateify)(data.date);
|
|
293
|
-
}
|
|
294
|
-
return data;
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Error listener that re-emits error on to our internal stream.
|
|
298
|
-
*
|
|
299
|
-
* @private
|
|
300
|
-
* @param {Error} err
|
|
301
|
-
* @return void
|
|
302
|
-
*/
|
|
303
|
-
_onModuleError(err) {
|
|
304
|
-
this.emit("error", err);
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Checks the various state variables after queue has drained to determine if
|
|
308
|
-
* we need to `finalize`.
|
|
309
|
-
*
|
|
310
|
-
* @private
|
|
311
|
-
* @return void
|
|
312
|
-
*/
|
|
313
|
-
_onQueueDrain() {
|
|
314
|
-
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
if (this._state.finalize && this._pending === 0 && this._queue.idle() && this._statQueue.idle()) {
|
|
318
|
-
this._finalize();
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Appends each queue task to the module.
|
|
323
|
-
*
|
|
324
|
-
* @private
|
|
325
|
-
* @param {Object} task
|
|
326
|
-
* @param {Function} callback
|
|
327
|
-
* @return void
|
|
328
|
-
*/
|
|
329
|
-
_onQueueTask(task, callback) {
|
|
330
|
-
const fullCallback = () => {
|
|
331
|
-
if (task.data.callback) {
|
|
332
|
-
task.data.callback();
|
|
319
|
+
if (data.stats && data.date === null) {
|
|
320
|
+
data.date = data.stats.mtime;
|
|
321
|
+
} else {
|
|
322
|
+
data.date = (0, _utils.dateify)(data.date);
|
|
333
323
|
}
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
324
|
+
return data;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Error listener that re-emits error on to our internal stream.
|
|
328
|
+
*
|
|
329
|
+
* @private
|
|
330
|
+
* @param {Error} err
|
|
331
|
+
* @return void
|
|
332
|
+
*/
|
|
333
|
+
}, {
|
|
334
|
+
key: "_onModuleError",
|
|
335
|
+
value: function _onModuleError(err) {
|
|
336
|
+
/**
|
|
337
|
+
* @event Archiver#error
|
|
338
|
+
* @type {ErrorData}
|
|
339
|
+
*/
|
|
340
|
+
this.emit("error", err);
|
|
339
341
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Checks the various state variables after queue has drained to determine if
|
|
344
|
+
* we need to `finalize`.
|
|
345
|
+
*
|
|
346
|
+
* @private
|
|
347
|
+
* @return void
|
|
348
|
+
*/
|
|
349
|
+
}, {
|
|
350
|
+
key: "_onQueueDrain",
|
|
351
|
+
value: function _onQueueDrain() {
|
|
352
|
+
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (this._state.finalize && this._pending === 0 && this._queue.idle() && this._statQueue.idle()) {
|
|
356
|
+
this._finalize();
|
|
357
|
+
}
|
|
355
358
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
+
/**
|
|
360
|
+
* Appends each queue task to the module.
|
|
361
|
+
*
|
|
362
|
+
* @private
|
|
363
|
+
* @param {Object} task
|
|
364
|
+
* @param {Function} callback
|
|
365
|
+
* @return void
|
|
366
|
+
*/
|
|
367
|
+
}, {
|
|
368
|
+
key: "_onQueueTask",
|
|
369
|
+
value: function _onQueueTask(task, callback) {
|
|
370
|
+
var fullCallback = function fullCallback() {
|
|
371
|
+
if (task.data.callback) {
|
|
372
|
+
task.data.callback();
|
|
373
|
+
}
|
|
374
|
+
callback();
|
|
375
|
+
};
|
|
376
|
+
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
377
|
+
fullCallback();
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
this._task = task;
|
|
381
|
+
this._moduleAppend(task.source, task.data, fullCallback);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Performs a file stat and reinjects the task back into the queue.
|
|
385
|
+
*
|
|
386
|
+
* @private
|
|
387
|
+
* @param {Object} task
|
|
388
|
+
* @param {Function} callback
|
|
389
|
+
* @return void
|
|
390
|
+
*/
|
|
391
|
+
}, {
|
|
392
|
+
key: "_onStatQueueTask",
|
|
393
|
+
value: function _onStatQueueTask(task, callback) {
|
|
394
|
+
if (this._state.finalizing || this._state.finalized || this._state.aborted) {
|
|
395
|
+
callback();
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
(0, _fs.lstat)(task.filepath, function (err, stats) {
|
|
359
399
|
if (this._state.aborted) {
|
|
360
400
|
setImmediate(callback);
|
|
361
401
|
return;
|
|
362
402
|
}
|
|
363
403
|
if (err) {
|
|
364
404
|
this._entriesCount--;
|
|
405
|
+
/**
|
|
406
|
+
* @event Archiver#warning
|
|
407
|
+
* @type {ErrorData}
|
|
408
|
+
*/
|
|
365
409
|
this.emit("warning", err);
|
|
366
410
|
setImmediate(callback);
|
|
367
411
|
return;
|
|
@@ -374,382 +418,441 @@ class Archiver extends import_readable_stream.Transform {
|
|
|
374
418
|
this._queue.push(task);
|
|
375
419
|
}
|
|
376
420
|
setImmediate(callback);
|
|
377
|
-
}.bind(this)
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
(0,
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
this.emit(
|
|
439
|
-
"warning",
|
|
440
|
-
new import_error.ArchiverError("DIRECTORYNOTSUPPORTED", task.data)
|
|
441
|
-
);
|
|
442
|
-
} else if (stats.isSymbolicLink()) {
|
|
443
|
-
this.emit(
|
|
444
|
-
"warning",
|
|
445
|
-
new import_error.ArchiverError("SYMLINKNOTSUPPORTED", task.data)
|
|
446
|
-
);
|
|
421
|
+
}.bind(this));
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Unpipes the module and ends our internal stream.
|
|
425
|
+
*
|
|
426
|
+
* @private
|
|
427
|
+
* @return void
|
|
428
|
+
*/
|
|
429
|
+
}, {
|
|
430
|
+
key: "_shutdown",
|
|
431
|
+
value: function _shutdown() {
|
|
432
|
+
this._moduleUnpipe();
|
|
433
|
+
this.end();
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Tracks the bytes emitted by our internal stream.
|
|
437
|
+
*
|
|
438
|
+
* @private
|
|
439
|
+
* @param {Buffer} chunk
|
|
440
|
+
* @param {String} encoding
|
|
441
|
+
* @param {Function} callback
|
|
442
|
+
* @return void
|
|
443
|
+
*/
|
|
444
|
+
}, {
|
|
445
|
+
key: "_transform",
|
|
446
|
+
value: function _transform(chunk, encoding, callback) {
|
|
447
|
+
if (chunk) {
|
|
448
|
+
this._pointer += chunk.length;
|
|
449
|
+
}
|
|
450
|
+
callback(null, chunk);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Updates and normalizes a queue task using stats data.
|
|
454
|
+
*
|
|
455
|
+
* @private
|
|
456
|
+
* @param {Object} task
|
|
457
|
+
* @param {Stats} stats
|
|
458
|
+
* @return {Object}
|
|
459
|
+
*/
|
|
460
|
+
}, {
|
|
461
|
+
key: "_updateQueueTaskWithStats",
|
|
462
|
+
value: function _updateQueueTaskWithStats(task, stats) {
|
|
463
|
+
if (stats.isFile()) {
|
|
464
|
+
task.data.type = "file";
|
|
465
|
+
task.data.sourceType = "stream";
|
|
466
|
+
task.source = new _lazystream.Readable(function () {
|
|
467
|
+
return (0, _fs.createReadStream)(task.filepath);
|
|
468
|
+
});
|
|
469
|
+
} else if (stats.isDirectory() && this._supportsDirectory) {
|
|
470
|
+
task.data.name = (0, _utils.trailingSlashIt)(task.data.name);
|
|
471
|
+
task.data.type = "directory";
|
|
472
|
+
task.data.sourcePath = (0, _utils.trailingSlashIt)(task.filepath);
|
|
473
|
+
task.data.sourceType = "buffer";
|
|
474
|
+
task.source = Buffer.concat([]);
|
|
475
|
+
} else if (stats.isSymbolicLink() && this._supportsSymlink) {
|
|
476
|
+
var linkPath = (0, _fs.readlinkSync)(task.filepath);
|
|
477
|
+
var dirName = (0, _path.dirname)(task.filepath);
|
|
478
|
+
task.data.type = "symlink";
|
|
479
|
+
task.data.linkname = (0, _path.relative)(dirName, (0, _path.resolve)(dirName, linkPath));
|
|
480
|
+
task.data.sourceType = "buffer";
|
|
481
|
+
task.source = Buffer.concat([]);
|
|
447
482
|
} else {
|
|
448
|
-
|
|
483
|
+
if (stats.isDirectory()) {
|
|
484
|
+
this.emit("warning", new _error.ArchiverError("DIRECTORYNOTSUPPORTED", task.data));
|
|
485
|
+
} else if (stats.isSymbolicLink()) {
|
|
486
|
+
this.emit("warning", new _error.ArchiverError("SYMLINKNOTSUPPORTED", task.data));
|
|
487
|
+
} else {
|
|
488
|
+
this.emit("warning", new _error.ArchiverError("ENTRYNOTSUPPORTED", task.data));
|
|
489
|
+
}
|
|
490
|
+
return null;
|
|
449
491
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
return this;
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* Appends an input source (text string, buffer, or stream) to the instance.
|
|
476
|
-
*
|
|
477
|
-
* When the instance has received, processed, and emitted the input, the `entry`
|
|
478
|
-
* event is fired.
|
|
479
|
-
*
|
|
480
|
-
* @fires Archiver#entry
|
|
481
|
-
* @param {(Buffer|Stream|String)} source The input source.
|
|
482
|
-
* @param {EntryData} data See also {@link ZipEntryData} and {@link TarEntryData}.
|
|
483
|
-
* @return {this}
|
|
484
|
-
*/
|
|
485
|
-
append(source, data) {
|
|
486
|
-
if (this._state.finalize || this._state.aborted) {
|
|
487
|
-
this.emit("error", new import_error.ArchiverError("QUEUECLOSED"));
|
|
488
|
-
return this;
|
|
489
|
-
}
|
|
490
|
-
data = this._normalizeEntryData(data);
|
|
491
|
-
if (typeof data.name !== "string" || data.name.length === 0) {
|
|
492
|
-
this.emit("error", new import_error.ArchiverError("ENTRYNAMEREQUIRED"));
|
|
493
|
-
return this;
|
|
494
|
-
}
|
|
495
|
-
if (data.type === "directory" && !this._supportsDirectory) {
|
|
496
|
-
this.emit(
|
|
497
|
-
"error",
|
|
498
|
-
new import_error.ArchiverError("DIRECTORYNOTSUPPORTED", { name: data.name })
|
|
499
|
-
);
|
|
500
|
-
return this;
|
|
501
|
-
}
|
|
502
|
-
source = (0, import_utils.normalizeInputSource)(source);
|
|
503
|
-
if (Buffer.isBuffer(source)) {
|
|
504
|
-
data.sourceType = "buffer";
|
|
505
|
-
} else if ((0, import_is_stream.isStream)(source)) {
|
|
506
|
-
data.sourceType = "stream";
|
|
507
|
-
} else {
|
|
508
|
-
this.emit(
|
|
509
|
-
"error",
|
|
510
|
-
new import_error.ArchiverError("INPUTSTEAMBUFFERREQUIRED", { name: data.name })
|
|
511
|
-
);
|
|
512
|
-
return this;
|
|
513
|
-
}
|
|
514
|
-
this._entriesCount++;
|
|
515
|
-
this._queue.push({
|
|
516
|
-
data,
|
|
517
|
-
source
|
|
518
|
-
});
|
|
519
|
-
return this;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Appends a directory and its files, recursively, given its dirpath.
|
|
523
|
-
*
|
|
524
|
-
* @param {String} dirpath The source directory path.
|
|
525
|
-
* @param {String} destpath The destination path within the archive.
|
|
526
|
-
* @param {(EntryData|Function)} data See also [ZipEntryData]{@link ZipEntryData} and
|
|
527
|
-
* [TarEntryData]{@link TarEntryData}.
|
|
528
|
-
* @return {this}
|
|
529
|
-
*/
|
|
530
|
-
directory(dirpath, destpath, data) {
|
|
531
|
-
if (this._state.finalize || this._state.aborted) {
|
|
532
|
-
this.emit("error", new import_error.ArchiverError("QUEUECLOSED"));
|
|
492
|
+
task.data = this._normalizeEntryData(task.data, stats);
|
|
493
|
+
return task;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Aborts the archiving process, taking a best-effort approach, by:
|
|
497
|
+
*
|
|
498
|
+
* - removing any pending queue tasks
|
|
499
|
+
* - allowing any active queue workers to finish
|
|
500
|
+
* - detaching internal module pipes
|
|
501
|
+
* - ending both sides of the Transform stream
|
|
502
|
+
*
|
|
503
|
+
* It will NOT drain any remaining sources.
|
|
504
|
+
*
|
|
505
|
+
* @return {this}
|
|
506
|
+
*/
|
|
507
|
+
}, {
|
|
508
|
+
key: "abort",
|
|
509
|
+
value: function abort() {
|
|
510
|
+
if (this._state.aborted || this._state.finalized) {
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
this._abort();
|
|
533
514
|
return this;
|
|
534
515
|
}
|
|
535
|
-
|
|
536
|
-
|
|
516
|
+
/**
|
|
517
|
+
* Appends an input source (text string, buffer, or stream) to the instance.
|
|
518
|
+
*
|
|
519
|
+
* When the instance has received, processed, and emitted the input, the `entry`
|
|
520
|
+
* event is fired.
|
|
521
|
+
*
|
|
522
|
+
* @fires Archiver#entry
|
|
523
|
+
* @param {(Buffer|Stream|String)} source The input source.
|
|
524
|
+
* @param {EntryData} data See also {@link ZipEntryData} and {@link TarEntryData}.
|
|
525
|
+
* @return {this}
|
|
526
|
+
*/
|
|
527
|
+
}, {
|
|
528
|
+
key: "append",
|
|
529
|
+
value: function append(source, data) {
|
|
530
|
+
if (this._state.finalize || this._state.aborted) {
|
|
531
|
+
this.emit("error", new _error.ArchiverError("QUEUECLOSED"));
|
|
532
|
+
return this;
|
|
533
|
+
}
|
|
534
|
+
data = this._normalizeEntryData(data);
|
|
535
|
+
if (typeof data.name !== "string" || data.name.length === 0) {
|
|
536
|
+
this.emit("error", new _error.ArchiverError("ENTRYNAMEREQUIRED"));
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
if (data.type === "directory" && !this._supportsDirectory) {
|
|
540
|
+
this.emit("error", new _error.ArchiverError("DIRECTORYNOTSUPPORTED", {
|
|
541
|
+
name: data.name
|
|
542
|
+
}));
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
source = (0, _utils.normalizeInputSource)(source);
|
|
546
|
+
if (Buffer.isBuffer(source)) {
|
|
547
|
+
data.sourceType = "buffer";
|
|
548
|
+
} else if ((0, _isStream.isStream)(source)) {
|
|
549
|
+
data.sourceType = "stream";
|
|
550
|
+
} else {
|
|
551
|
+
this.emit("error", new _error.ArchiverError("INPUTSTEAMBUFFERREQUIRED", {
|
|
552
|
+
name: data.name
|
|
553
|
+
}));
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
556
|
+
this._entriesCount++;
|
|
557
|
+
this._queue.push({
|
|
558
|
+
data: data,
|
|
559
|
+
source: source
|
|
560
|
+
});
|
|
537
561
|
return this;
|
|
538
562
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
563
|
+
/**
|
|
564
|
+
* Appends a directory and its files, recursively, given its dirpath.
|
|
565
|
+
*
|
|
566
|
+
* @param {String} dirpath The source directory path.
|
|
567
|
+
* @param {String} destpath The destination path within the archive.
|
|
568
|
+
* @param {(EntryData|Function)} data See also [ZipEntryData]{@link ZipEntryData} and
|
|
569
|
+
* [TarEntryData]{@link TarEntryData}.
|
|
570
|
+
* @return {this}
|
|
571
|
+
*/
|
|
572
|
+
}, {
|
|
573
|
+
key: "directory",
|
|
574
|
+
value: function directory(dirpath, destpath, data) {
|
|
575
|
+
if (this._state.finalize || this._state.aborted) {
|
|
576
|
+
this.emit("error", new _error.ArchiverError("QUEUECLOSED"));
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
579
|
+
if (typeof dirpath !== "string" || dirpath.length === 0) {
|
|
580
|
+
this.emit("error", new _error.ArchiverError("DIRECTORYDIRPATHREQUIRED"));
|
|
581
|
+
return this;
|
|
582
|
+
}
|
|
583
|
+
this._pending++;
|
|
584
|
+
if (destpath === false) {
|
|
585
|
+
destpath = "";
|
|
586
|
+
} else if (typeof destpath !== "string") {
|
|
587
|
+
destpath = dirpath;
|
|
588
|
+
}
|
|
589
|
+
var dataFunction = false;
|
|
590
|
+
if (typeof data === "function") {
|
|
591
|
+
dataFunction = data;
|
|
592
|
+
data = {};
|
|
593
|
+
} else if (_typeof(data) !== "object") {
|
|
594
|
+
data = {};
|
|
595
|
+
}
|
|
596
|
+
var globOptions = {
|
|
597
|
+
stat: true,
|
|
598
|
+
dot: true
|
|
599
|
+
};
|
|
600
|
+
function onGlobEnd() {
|
|
601
|
+
this._pending--;
|
|
602
|
+
this._maybeFinalize();
|
|
603
|
+
}
|
|
604
|
+
function onGlobError(err) {
|
|
605
|
+
this.emit("error", err);
|
|
606
|
+
}
|
|
607
|
+
function onGlobMatch(match) {
|
|
608
|
+
globber.pause();
|
|
609
|
+
var ignoreMatch = false;
|
|
610
|
+
var entryData = Object.assign({}, data);
|
|
611
|
+
entryData.name = match.relative;
|
|
612
|
+
entryData.prefix = destpath;
|
|
613
|
+
entryData.stats = match.stat;
|
|
614
|
+
entryData.callback = globber.resume.bind(globber);
|
|
615
|
+
try {
|
|
616
|
+
if (dataFunction) {
|
|
617
|
+
entryData = dataFunction(entryData);
|
|
618
|
+
if (entryData === false) {
|
|
619
|
+
ignoreMatch = true;
|
|
620
|
+
} else if (_typeof(entryData) !== "object") {
|
|
621
|
+
throw new _error.ArchiverError("DIRECTORYFUNCTIONINVALIDDATA", {
|
|
622
|
+
dirpath: dirpath
|
|
623
|
+
});
|
|
624
|
+
}
|
|
580
625
|
}
|
|
626
|
+
} catch (e) {
|
|
627
|
+
this.emit("error", e);
|
|
628
|
+
return;
|
|
581
629
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
globber.resume();
|
|
588
|
-
return;
|
|
630
|
+
if (ignoreMatch) {
|
|
631
|
+
globber.resume();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
this._append(match.absolute, entryData);
|
|
589
635
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
globber.on("match", onGlobMatch.bind(this));
|
|
595
|
-
globber.on("end", onGlobEnd.bind(this));
|
|
596
|
-
return this;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Appends a file given its filepath using a
|
|
600
|
-
* [lazystream]{@link https://github.com/jpommerening/node-lazystream} wrapper to
|
|
601
|
-
* prevent issues with open file limits.
|
|
602
|
-
*
|
|
603
|
-
* When the instance has received, processed, and emitted the file, the `entry`
|
|
604
|
-
* event is fired.
|
|
605
|
-
*
|
|
606
|
-
* @param {String} filepath The source filepath.
|
|
607
|
-
* @param {EntryData} data See also [ZipEntryData]{@link ZipEntryData} and
|
|
608
|
-
* [TarEntryData]{@link TarEntryData}.
|
|
609
|
-
* @return {this}
|
|
610
|
-
*/
|
|
611
|
-
file(filepath, data) {
|
|
612
|
-
if (this._state.finalize || this._state.aborted) {
|
|
613
|
-
this.emit("error", new import_error.ArchiverError("QUEUECLOSED"));
|
|
614
|
-
return this;
|
|
615
|
-
}
|
|
616
|
-
if (typeof filepath !== "string" || filepath.length === 0) {
|
|
617
|
-
this.emit("error", new import_error.ArchiverError("FILEFILEPATHREQUIRED"));
|
|
636
|
+
var globber = (0, _readdirGlob.readdirGlob)(dirpath, globOptions);
|
|
637
|
+
globber.on("error", onGlobError.bind(this));
|
|
638
|
+
globber.on("match", onGlobMatch.bind(this));
|
|
639
|
+
globber.on("end", onGlobEnd.bind(this));
|
|
618
640
|
return this;
|
|
619
641
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
this.
|
|
645
|
-
}
|
|
646
|
-
function onGlobMatch(match) {
|
|
647
|
-
globber.pause();
|
|
648
|
-
const entryData = Object.assign({}, data);
|
|
649
|
-
entryData.callback = globber.resume.bind(globber);
|
|
650
|
-
entryData.stats = match.stat;
|
|
651
|
-
entryData.name = match.relative;
|
|
652
|
-
this._append(match.absolute, entryData);
|
|
653
|
-
}
|
|
654
|
-
const globber = new ReaddirGlob(options.cwd || ".", options);
|
|
655
|
-
globber.on("error", onGlobError.bind(this));
|
|
656
|
-
globber.on("match", onGlobMatch.bind(this));
|
|
657
|
-
globber.on("end", onGlobEnd.bind(this));
|
|
658
|
-
return this;
|
|
659
|
-
}
|
|
660
|
-
/**
|
|
661
|
-
* Finalizes the instance and prevents further appending to the archive
|
|
662
|
-
* structure (queue will continue til drained).
|
|
663
|
-
*
|
|
664
|
-
* The `end`, `close` or `finish` events on the destination stream may fire
|
|
665
|
-
* right after calling this method so you should set listeners beforehand to
|
|
666
|
-
* properly detect stream completion.
|
|
667
|
-
*
|
|
668
|
-
* @return {Promise}
|
|
669
|
-
*/
|
|
670
|
-
finalize() {
|
|
671
|
-
if (this._state.aborted) {
|
|
672
|
-
var abortedError = new import_error.ArchiverError("ABORTED");
|
|
673
|
-
this.emit("error", abortedError);
|
|
674
|
-
return Promise.reject(abortedError);
|
|
675
|
-
}
|
|
676
|
-
if (this._state.finalize) {
|
|
677
|
-
var finalizingError = new import_error.ArchiverError("FINALIZING");
|
|
678
|
-
this.emit("error", finalizingError);
|
|
679
|
-
return Promise.reject(finalizingError);
|
|
680
|
-
}
|
|
681
|
-
this._state.finalize = true;
|
|
682
|
-
if (this._pending === 0 && this._queue.idle() && this._statQueue.idle()) {
|
|
683
|
-
this._finalize();
|
|
684
|
-
}
|
|
685
|
-
var self = this;
|
|
686
|
-
return new Promise(function(resolve, reject) {
|
|
687
|
-
var errored;
|
|
688
|
-
self._module.on("end", function() {
|
|
689
|
-
if (!errored) {
|
|
690
|
-
resolve();
|
|
691
|
-
}
|
|
692
|
-
});
|
|
693
|
-
self._module.on("error", function(err) {
|
|
694
|
-
errored = true;
|
|
695
|
-
reject(err);
|
|
696
|
-
});
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
/**
|
|
700
|
-
* Appends a symlink to the instance.
|
|
701
|
-
*
|
|
702
|
-
* This does NOT interact with filesystem and is used for programmatically creating symlinks.
|
|
703
|
-
*
|
|
704
|
-
* @param {String} filepath The symlink path (within archive).
|
|
705
|
-
* @param {String} target The target path (within archive).
|
|
706
|
-
* @param {Number} mode Sets the entry permissions.
|
|
707
|
-
* @return {this}
|
|
708
|
-
*/
|
|
709
|
-
symlink(filepath, target, mode) {
|
|
710
|
-
if (this._state.finalize || this._state.aborted) {
|
|
711
|
-
this.emit("error", new import_error.ArchiverError("QUEUECLOSED"));
|
|
642
|
+
/**
|
|
643
|
+
* Appends a file given its filepath using a
|
|
644
|
+
* [lazystream]{@link https://github.com/jpommerening/node-lazystream} wrapper to
|
|
645
|
+
* prevent issues with open file limits.
|
|
646
|
+
*
|
|
647
|
+
* When the instance has received, processed, and emitted the file, the `entry`
|
|
648
|
+
* event is fired.
|
|
649
|
+
*
|
|
650
|
+
* @param {String} filepath The source filepath.
|
|
651
|
+
* @param {EntryData} data See also [ZipEntryData]{@link ZipEntryData} and
|
|
652
|
+
* [TarEntryData]{@link TarEntryData}.
|
|
653
|
+
* @return {this}
|
|
654
|
+
*/
|
|
655
|
+
}, {
|
|
656
|
+
key: "file",
|
|
657
|
+
value: function file(filepath, data) {
|
|
658
|
+
if (this._state.finalize || this._state.aborted) {
|
|
659
|
+
this.emit("error", new _error.ArchiverError("QUEUECLOSED"));
|
|
660
|
+
return this;
|
|
661
|
+
}
|
|
662
|
+
if (typeof filepath !== "string" || filepath.length === 0) {
|
|
663
|
+
this.emit("error", new _error.ArchiverError("FILEFILEPATHREQUIRED"));
|
|
664
|
+
return this;
|
|
665
|
+
}
|
|
666
|
+
this._append(filepath, data);
|
|
712
667
|
return this;
|
|
713
668
|
}
|
|
714
|
-
|
|
715
|
-
|
|
669
|
+
/**
|
|
670
|
+
* Appends multiple files that match a glob pattern.
|
|
671
|
+
*
|
|
672
|
+
* @param {String} pattern The [glob pattern]{@link https://github.com/isaacs/minimatch} to match.
|
|
673
|
+
* @param {Object} options See [node-readdir-glob]{@link https://github.com/yqnn/node-readdir-glob#options}.
|
|
674
|
+
* @param {EntryData} data See also [ZipEntryData]{@link ZipEntryData} and
|
|
675
|
+
* [TarEntryData]{@link TarEntryData}.
|
|
676
|
+
* @return {this}
|
|
677
|
+
*/
|
|
678
|
+
}, {
|
|
679
|
+
key: "glob",
|
|
680
|
+
value: function glob(pattern, options, data) {
|
|
681
|
+
this._pending++;
|
|
682
|
+
options = _objectSpread({
|
|
683
|
+
stat: true,
|
|
684
|
+
pattern: pattern
|
|
685
|
+
}, options);
|
|
686
|
+
function onGlobEnd() {
|
|
687
|
+
this._pending--;
|
|
688
|
+
this._maybeFinalize();
|
|
689
|
+
}
|
|
690
|
+
function onGlobError(err) {
|
|
691
|
+
this.emit("error", err);
|
|
692
|
+
}
|
|
693
|
+
function onGlobMatch(match) {
|
|
694
|
+
globber.pause();
|
|
695
|
+
var entryData = Object.assign({}, data);
|
|
696
|
+
entryData.callback = globber.resume.bind(globber);
|
|
697
|
+
entryData.stats = match.stat;
|
|
698
|
+
entryData.name = match.relative;
|
|
699
|
+
this._append(match.absolute, entryData);
|
|
700
|
+
}
|
|
701
|
+
var globber = new ReaddirGlob(options.cwd || ".", options);
|
|
702
|
+
globber.on("error", onGlobError.bind(this));
|
|
703
|
+
globber.on("match", onGlobMatch.bind(this));
|
|
704
|
+
globber.on("end", onGlobEnd.bind(this));
|
|
716
705
|
return this;
|
|
717
706
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
707
|
+
/**
|
|
708
|
+
* Finalizes the instance and prevents further appending to the archive
|
|
709
|
+
* structure (queue will continue til drained).
|
|
710
|
+
*
|
|
711
|
+
* The `end`, `close` or `finish` events on the destination stream may fire
|
|
712
|
+
* right after calling this method so you should set listeners beforehand to
|
|
713
|
+
* properly detect stream completion.
|
|
714
|
+
*
|
|
715
|
+
* @return {Promise}
|
|
716
|
+
*/
|
|
717
|
+
}, {
|
|
718
|
+
key: "finalize",
|
|
719
|
+
value: function finalize() {
|
|
720
|
+
if (this._state.aborted) {
|
|
721
|
+
var abortedError = new _error.ArchiverError("ABORTED");
|
|
722
|
+
this.emit("error", abortedError);
|
|
723
|
+
return Promise.reject(abortedError);
|
|
724
|
+
}
|
|
725
|
+
if (this._state.finalize) {
|
|
726
|
+
var finalizingError = new _error.ArchiverError("FINALIZING");
|
|
727
|
+
this.emit("error", finalizingError);
|
|
728
|
+
return Promise.reject(finalizingError);
|
|
729
|
+
}
|
|
730
|
+
this._state.finalize = true;
|
|
731
|
+
if (this._pending === 0 && this._queue.idle() && this._statQueue.idle()) {
|
|
732
|
+
this._finalize();
|
|
733
|
+
}
|
|
734
|
+
var self = this;
|
|
735
|
+
return new Promise(function (resolve, reject) {
|
|
736
|
+
var errored;
|
|
737
|
+
self._module.on("end", function () {
|
|
738
|
+
if (!errored) {
|
|
739
|
+
resolve();
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
self._module.on("error", function (err) {
|
|
743
|
+
errored = true;
|
|
744
|
+
reject(err);
|
|
745
|
+
});
|
|
746
|
+
});
|
|
724
747
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
748
|
+
/**
|
|
749
|
+
* Appends a symlink to the instance.
|
|
750
|
+
*
|
|
751
|
+
* This does NOT interact with filesystem and is used for programmatically creating symlinks.
|
|
752
|
+
*
|
|
753
|
+
* @param {String} filepath The symlink path (within archive).
|
|
754
|
+
* @param {String} target The target path (within archive).
|
|
755
|
+
* @param {Number} mode Sets the entry permissions.
|
|
756
|
+
* @return {this}
|
|
757
|
+
*/
|
|
758
|
+
}, {
|
|
759
|
+
key: "symlink",
|
|
760
|
+
value: function symlink(filepath, target, mode) {
|
|
761
|
+
if (this._state.finalize || this._state.aborted) {
|
|
762
|
+
this.emit("error", new _error.ArchiverError("QUEUECLOSED"));
|
|
763
|
+
return this;
|
|
764
|
+
}
|
|
765
|
+
if (typeof filepath !== "string" || filepath.length === 0) {
|
|
766
|
+
this.emit("error", new _error.ArchiverError("SYMLINKFILEPATHREQUIRED"));
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
if (typeof target !== "string" || target.length === 0) {
|
|
770
|
+
this.emit("error", new _error.ArchiverError("SYMLINKTARGETREQUIRED", {
|
|
771
|
+
filepath: filepath
|
|
772
|
+
}));
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
if (!this._supportsSymlink) {
|
|
776
|
+
this.emit("error", new _error.ArchiverError("SYMLINKNOTSUPPORTED", {
|
|
777
|
+
filepath: filepath
|
|
778
|
+
}));
|
|
779
|
+
return this;
|
|
780
|
+
}
|
|
781
|
+
var data = {};
|
|
782
|
+
data.type = "symlink";
|
|
783
|
+
data.name = filepath.replace(/\\/g, "/");
|
|
784
|
+
data.linkname = target.replace(/\\/g, "/");
|
|
785
|
+
data.sourceType = "buffer";
|
|
786
|
+
if (typeof mode === "number") {
|
|
787
|
+
data.mode = mode;
|
|
788
|
+
}
|
|
789
|
+
this._entriesCount++;
|
|
790
|
+
this._queue.push({
|
|
791
|
+
data: data,
|
|
792
|
+
source: Buffer.concat([])
|
|
793
|
+
});
|
|
730
794
|
return this;
|
|
731
795
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
}
|
|
796
|
+
/**
|
|
797
|
+
* Returns the current length (in bytes) that has been emitted.
|
|
798
|
+
*
|
|
799
|
+
* @return {Number}
|
|
800
|
+
*/
|
|
801
|
+
}, {
|
|
802
|
+
key: "pointer",
|
|
803
|
+
value: function pointer() {
|
|
804
|
+
return this._pointer;
|
|
805
|
+
}
|
|
806
|
+
}]);
|
|
807
|
+
}(_readableStream.Transform);
|
|
808
|
+
/**
|
|
809
|
+
* @typedef {Object} CoreOptions
|
|
810
|
+
* @global
|
|
811
|
+
* @property {Number} [statConcurrency=4] Sets the number of workers used to
|
|
812
|
+
* process the internal fs stat queue.
|
|
813
|
+
*/
|
|
814
|
+
/**
|
|
815
|
+
* @typedef {Object} TransformOptions
|
|
816
|
+
* @property {Boolean} [allowHalfOpen=true] If set to false, then the stream
|
|
817
|
+
* will automatically end the readable side when the writable side ends and vice
|
|
818
|
+
* versa.
|
|
819
|
+
* @property {Boolean} [readableObjectMode=false] Sets objectMode for readable
|
|
820
|
+
* side of the stream. Has no effect if objectMode is true.
|
|
821
|
+
* @property {Boolean} [writableObjectMode=false] Sets objectMode for writable
|
|
822
|
+
* side of the stream. Has no effect if objectMode is true.
|
|
823
|
+
* @property {Boolean} [decodeStrings=true] Whether or not to decode strings
|
|
824
|
+
* into Buffers before passing them to _write(). `Writable`
|
|
825
|
+
* @property {String} [encoding=NULL] If specified, then buffers will be decoded
|
|
826
|
+
* to strings using the specified encoding. `Readable`
|
|
827
|
+
* @property {Number} [highWaterMark=16kb] The maximum number of bytes to store
|
|
828
|
+
* in the internal buffer before ceasing to read from the underlying resource.
|
|
829
|
+
* `Readable` `Writable`
|
|
830
|
+
* @property {Boolean} [objectMode=false] Whether this stream should behave as a
|
|
831
|
+
* stream of objects. Meaning that stream.read(n) returns a single value instead
|
|
832
|
+
* of a Buffer of size n. `Readable` `Writable`
|
|
833
|
+
*/
|
|
834
|
+
/**
|
|
835
|
+
* @typedef {Object} EntryData
|
|
836
|
+
* @property {String} name Sets the entry name including internal path.
|
|
837
|
+
* @property {(String|Date)} [date=NOW()] Sets the entry date.
|
|
838
|
+
* @property {Number} [mode=D:0755/F:0644] Sets the entry permissions.
|
|
839
|
+
* @property {String} [prefix] Sets a path prefix for the entry name. Useful
|
|
840
|
+
* when working with methods like `directory` or `glob`.
|
|
841
|
+
* @property {fs.Stats} [stats] Sets the fs stat data for this entry allowing
|
|
842
|
+
* for reduction of fs stat calls when stat data is already known.
|
|
843
|
+
*/
|
|
844
|
+
/**
|
|
845
|
+
* @typedef {Object} ErrorData
|
|
846
|
+
* @property {String} message The message of the error.
|
|
847
|
+
* @property {String} code The error code assigned to this error.
|
|
848
|
+
* @property {String} data Additional data provided for reporting or debugging (where available).
|
|
849
|
+
*/
|
|
850
|
+
/**
|
|
851
|
+
* @typedef {Object} ProgressData
|
|
852
|
+
* @property {Object} entries
|
|
853
|
+
* @property {Number} entries.total Number of entries that have been appended.
|
|
854
|
+
* @property {Number} entries.processed Number of entries that have been processed.
|
|
855
|
+
* @property {Object} fs
|
|
856
|
+
* @property {Number} fs.totalBytes Number of bytes that have been appended. Calculated asynchronously and might not be accurate: it growth while entries are added. (based on fs.Stats)
|
|
857
|
+
* @property {Number} fs.processedBytes Number of bytes that have been processed. (based on fs.Stats)
|
|
858
|
+
*/
|