-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathuncompress_stream.js
More file actions
144 lines (120 loc) · 4.62 KB
/
uncompress_stream.js
File metadata and controls
144 lines (120 loc) · 4.62 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
// https://github.com/thejoshwolfe/yauzl#no-streaming-unzip-api
const debug = require('util').debuglog('compressing/zip/uncompress_stream');
const yauzl = require('@eggjs/yauzl');
const stream = require('stream');
const UncompressBaseStream = require('../base_write_stream');
const utils = require('../utils');
// lazy load iconv-lite
let iconv;
const YAUZL_CALLBACK = Symbol('ZipUncompressStream#yauzlCallback');
const STRIP_NAME = Symbol('ZipUncompressStream#stripName');
// don't decodeStrings on yauzl, we should handle fileName by ourself
// see validateFileName on https://github.com/thejoshwolfe/yauzl/blob/51010ce4e8c7e6345efe195e1b4150518f37b393/index.js#L607
// - support "absolute path"
const DEFAULTS = { lazyEntries: true, decodeStrings: false };
// from: https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/node/zip.ts#L51
function modeFromEntry(entry) {
const attr = entry.externalFileAttributes >> 16 || 33188;
return [ 448 /* S_IRWXU */, 56 /* S_IRWXG */, 7 /* S_IRWXO */ ]
.map(mask => attr & mask)
.reduce((a, b) => a + b, attr & 61440 /* S_IFMT */);
}
class ZipUncompressStream extends UncompressBaseStream {
constructor(opts) {
opts = opts || {};
super(opts);
this._chunks = [];
this._strip = Number(opts.strip) || 0;
this._zipFileNameEncoding = opts.zipFileNameEncoding || 'utf8';
if (this._zipFileNameEncoding === 'utf-8') {
this._zipFileNameEncoding = 'utf8';
}
this._finalCallback = err => {
if (err) {
debug('finalCallback, error: %j', err);
return this.emit('error', err);
}
this.emit('finish');
};
this[YAUZL_CALLBACK] = this[YAUZL_CALLBACK].bind(this);
const sourceType = utils.sourceType(opts.source);
const yauzlOpts = this._yauzlOpts = Object.assign({}, DEFAULTS, opts.yauzl);
debug('sourceType: %s, yauzlOpts: %j', sourceType, yauzlOpts);
if (sourceType === 'file') {
yauzl.open(opts.source, yauzlOpts, this[YAUZL_CALLBACK]);
return;
}
if (sourceType === 'buffer') {
yauzl.fromBuffer(opts.source, yauzlOpts, this[YAUZL_CALLBACK]);
return;
}
if (sourceType === 'stream') {
utils.streamToBuffer(opts.source)
.then(buf => yauzl.fromBuffer(buf, yauzlOpts, this[YAUZL_CALLBACK]))
.catch(e => this.emit('error', e));
return;
}
}
_write(chunk, _encoding, callback) {
this._chunks.push(chunk);
debug('write size: %d, chunks: %d', chunk.length, this._chunks.length);
callback();
}
_final(callback) {
const buf = Buffer.concat(this._chunks);
debug('final, buf size: %d, chunks: %d', buf.length, this._chunks.length);
this._finalCallback = callback;
yauzl.fromBuffer(buf, this._yauzlOpts, this[YAUZL_CALLBACK]);
}
[YAUZL_CALLBACK](err, zipFile) {
if (err) {
debug('yauzl error', err);
return this._finalCallback(err);
}
zipFile.readEntry();
zipFile
.on('entry', entry => {
const mode = modeFromEntry(entry);
// fileName is buffer by default because decodeStrings = false
if (Buffer.isBuffer(entry.fileName)) {
if (this._zipFileNameEncoding === 'utf8') {
entry.fileName = entry.fileName.toString();
} else {
if (!iconv) {
iconv = require('iconv-lite');
}
entry.fileName = iconv.decode(entry.fileName, this._zipFileNameEncoding);
}
}
// directory file names end with '/' (for Linux and macOS) or '\' (for Windows)
const type = /[\\\/]$/.test(entry.fileName) ? 'directory' : 'file';
const name = entry.fileName = this[STRIP_NAME](entry.fileName, type);
const header = { name, type, yauzl: entry, mode };
if (type === 'file') {
zipFile.openReadStream(entry, (err, readStream) => {
if (err) {
debug('file, error: %j', err);
return this._finalCallback(err);
}
debug('file, header: %j', header);
this.emit('entry', header, readStream, next);
});
} else { // directory
const placeholder = new stream.Readable({ read() {} });
debug('directory, header: %j', header);
this.emit('entry', header, placeholder, next);
setImmediate(() => placeholder.emit('end'));
}
})
.on('end', () => this._finalCallback())
.on('error', err => this._finalCallback(err));
function next() {
zipFile.readEntry();
}
}
[STRIP_NAME](fileName, type) {
return utils.stripFileName(this._strip, fileName, type);
}
}
module.exports = ZipUncompressStream;