-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathtar.js
118 lines (117 loc) · 2.68 KB
/
tar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import zlib from "node:zlib";
import engine from "tar-stream";
import { collectStream } from "../utils.js";
/**
* TAR Format Plugin
*
* @module plugins/tar
* @license [MIT]{@link https://github.com/archiverjs/node-archiver/blob/master/LICENSE}
* @copyright (c) 2012-2014 Chris Talkington, contributors.
*/
export default class Tar {
/**
* @constructor
* @param {TarOptions} options
*/
constructor(options) {
options = this.options = {
gzip: false,
...options,
};
if (typeof options.gzipOptions !== "object") {
options.gzipOptions = {};
}
this.engine = engine.pack(options);
this.compressor = false;
if (options.gzip) {
this.compressor = zlib.createGzip(options.gzipOptions);
this.compressor.on("error", this._onCompressorError.bind(this));
}
}
/**
* [_onCompressorError description]
*
* @private
* @param {Error} err
* @return void
*/
_onCompressorError(err) {
this.engine.emit("error", err);
}
/**
* [append description]
*
* @param {(Buffer|Stream)} source
* @param {TarEntryData} data
* @param {Function} callback
* @return void
*/
append(source, data, callback) {
var self = this;
data.mtime = data.date;
function append(err, sourceBuffer) {
if (err) {
callback(err);
return;
}
self.engine.entry(data, sourceBuffer, function (err) {
callback(err, data);
});
}
if (data.sourceType === "buffer") {
append(null, source);
} else if (data.sourceType === "stream" && data.stats) {
data.size = data.stats.size;
var entry = self.engine.entry(data, function (err) {
callback(err, data);
});
source.pipe(entry);
} else if (data.sourceType === "stream") {
collectStream(source, append);
}
}
/**
* [finalize description]
*
* @return void
*/
finalize() {
this.engine.finalize();
}
/**
* [on description]
*
* @return this.engine
*/
on() {
return this.engine.on.apply(this.engine, arguments);
}
/**
* [pipe description]
*
* @param {String} destination
* @param {Object} options
* @return this.engine
*/
pipe(destination, options) {
if (this.compressor) {
return this.engine.pipe
.apply(this.engine, [this.compressor])
.pipe(destination, options);
} else {
return this.engine.pipe.apply(this.engine, arguments);
}
}
/**
* [unpipe description]
*
* @return this.engine
*/
unpipe() {
if (this.compressor) {
return this.compressor.unpipe.apply(this.compressor, arguments);
} else {
return this.engine.unpipe.apply(this.engine, arguments);
}
}
}