@mehmetb/rollup-plugin-node-builtins 3.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.
- package/README.md +101 -0
- package/dist/constants.js +474 -0
- package/dist/rollup-plugin-node-builtins.cjs.js +77 -0
- package/dist/rollup-plugin-node-builtins.es6.js +75 -0
- package/package.json +42 -0
- package/src/es6/assert.js +488 -0
- package/src/es6/console.js +13 -0
- package/src/es6/domain.js +100 -0
- package/src/es6/empty.js +1 -0
- package/src/es6/events.js +475 -0
- package/src/es6/http-lib/capability.js +52 -0
- package/src/es6/http-lib/request.js +278 -0
- package/src/es6/http-lib/response.js +185 -0
- package/src/es6/http-lib/to-arraybuffer.js +30 -0
- package/src/es6/http.js +167 -0
- package/src/es6/inherits.js +25 -0
- package/src/es6/os.js +113 -0
- package/src/es6/path.js +234 -0
- package/src/es6/punycode.js +475 -0
- package/src/es6/qs.js +147 -0
- package/src/es6/readable-stream/buffer-list.js +59 -0
- package/src/es6/readable-stream/duplex.js +45 -0
- package/src/es6/readable-stream/passthrough.js +15 -0
- package/src/es6/readable-stream/readable.js +896 -0
- package/src/es6/readable-stream/transform.js +174 -0
- package/src/es6/readable-stream/writable.js +483 -0
- package/src/es6/setimmediate.js +185 -0
- package/src/es6/stream.js +110 -0
- package/src/es6/string-decoder.js +220 -0
- package/src/es6/timers.js +76 -0
- package/src/es6/tty.js +20 -0
- package/src/es6/url.js +745 -0
- package/src/es6/util.js +598 -0
- package/src/es6/vm.js +202 -0
- package/src/es6/zlib-lib/LICENSE +21 -0
- package/src/es6/zlib-lib/adler32.js +31 -0
- package/src/es6/zlib-lib/binding.js +269 -0
- package/src/es6/zlib-lib/crc32.js +40 -0
- package/src/es6/zlib-lib/deflate.js +1862 -0
- package/src/es6/zlib-lib/inffast.js +325 -0
- package/src/es6/zlib-lib/inflate.js +1650 -0
- package/src/es6/zlib-lib/inftrees.js +329 -0
- package/src/es6/zlib-lib/messages.js +11 -0
- package/src/es6/zlib-lib/trees.js +1220 -0
- package/src/es6/zlib-lib/utils.js +73 -0
- package/src/es6/zlib-lib/zstream.js +28 -0
- package/src/es6/zlib.js +635 -0
- package/src/index.js +73 -0
package/src/es6/zlib.js
ADDED
@@ -0,0 +1,635 @@
|
|
1
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
2
|
+
//
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
// copy of this software and associated documentation files (the
|
5
|
+
// "Software"), to deal in the Software without restriction, including
|
6
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
// persons to whom the Software is furnished to do so, subject to the
|
9
|
+
// following conditions:
|
10
|
+
//
|
11
|
+
// The above copyright notice and this permission notice shall be included
|
12
|
+
// in all copies or substantial portions of the Software.
|
13
|
+
//
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
import {Transform} from 'stream';
|
23
|
+
import * as _binding from './zlib-lib/binding';
|
24
|
+
import {inherits} from 'util';
|
25
|
+
function assert (a, msg) {
|
26
|
+
if (!a) {
|
27
|
+
throw new Error(msg);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
var binding = {};
|
31
|
+
Object.keys(_binding).forEach(function (key) {
|
32
|
+
binding[key] = _binding[key];
|
33
|
+
});
|
34
|
+
// zlib doesn't provide these, so kludge them in following the same
|
35
|
+
// const naming scheme zlib uses.
|
36
|
+
binding.Z_MIN_WINDOWBITS = 8;
|
37
|
+
binding.Z_MAX_WINDOWBITS = 15;
|
38
|
+
binding.Z_DEFAULT_WINDOWBITS = 15;
|
39
|
+
|
40
|
+
// fewer than 64 bytes per chunk is stupid.
|
41
|
+
// technically it could work with as few as 8, but even 64 bytes
|
42
|
+
// is absurdly low. Usually a MB or more is best.
|
43
|
+
binding.Z_MIN_CHUNK = 64;
|
44
|
+
binding.Z_MAX_CHUNK = Infinity;
|
45
|
+
binding.Z_DEFAULT_CHUNK = (16 * 1024);
|
46
|
+
|
47
|
+
binding.Z_MIN_MEMLEVEL = 1;
|
48
|
+
binding.Z_MAX_MEMLEVEL = 9;
|
49
|
+
binding.Z_DEFAULT_MEMLEVEL = 8;
|
50
|
+
|
51
|
+
binding.Z_MIN_LEVEL = -1;
|
52
|
+
binding.Z_MAX_LEVEL = 9;
|
53
|
+
binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
|
54
|
+
|
55
|
+
|
56
|
+
// translation table for return codes.
|
57
|
+
export var codes = {
|
58
|
+
Z_OK: binding.Z_OK,
|
59
|
+
Z_STREAM_END: binding.Z_STREAM_END,
|
60
|
+
Z_NEED_DICT: binding.Z_NEED_DICT,
|
61
|
+
Z_ERRNO: binding.Z_ERRNO,
|
62
|
+
Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
|
63
|
+
Z_DATA_ERROR: binding.Z_DATA_ERROR,
|
64
|
+
Z_MEM_ERROR: binding.Z_MEM_ERROR,
|
65
|
+
Z_BUF_ERROR: binding.Z_BUF_ERROR,
|
66
|
+
Z_VERSION_ERROR: binding.Z_VERSION_ERROR
|
67
|
+
};
|
68
|
+
|
69
|
+
Object.keys(codes).forEach(function(k) {
|
70
|
+
codes[codes[k]] = k;
|
71
|
+
});
|
72
|
+
|
73
|
+
export function createDeflate(o) {
|
74
|
+
return new Deflate(o);
|
75
|
+
}
|
76
|
+
|
77
|
+
export function createInflate(o) {
|
78
|
+
return new Inflate(o);
|
79
|
+
}
|
80
|
+
|
81
|
+
export function createDeflateRaw(o) {
|
82
|
+
return new DeflateRaw(o);
|
83
|
+
}
|
84
|
+
|
85
|
+
export function createInflateRaw(o) {
|
86
|
+
return new InflateRaw(o);
|
87
|
+
}
|
88
|
+
|
89
|
+
export function createGzip(o) {
|
90
|
+
return new Gzip(o);
|
91
|
+
}
|
92
|
+
|
93
|
+
export function createGunzip(o) {
|
94
|
+
return new Gunzip(o);
|
95
|
+
}
|
96
|
+
|
97
|
+
export function createUnzip(o) {
|
98
|
+
return new Unzip(o);
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
// Convenience methods.
|
103
|
+
// compress/decompress a string or buffer in one step.
|
104
|
+
export function deflate(buffer, opts, callback) {
|
105
|
+
if (typeof opts === 'function') {
|
106
|
+
callback = opts;
|
107
|
+
opts = {};
|
108
|
+
}
|
109
|
+
return zlibBuffer(new Deflate(opts), buffer, callback);
|
110
|
+
}
|
111
|
+
|
112
|
+
export function deflateSync(buffer, opts) {
|
113
|
+
return zlibBufferSync(new Deflate(opts), buffer);
|
114
|
+
}
|
115
|
+
|
116
|
+
export function gzip(buffer, opts, callback) {
|
117
|
+
if (typeof opts === 'function') {
|
118
|
+
callback = opts;
|
119
|
+
opts = {};
|
120
|
+
}
|
121
|
+
return zlibBuffer(new Gzip(opts), buffer, callback);
|
122
|
+
}
|
123
|
+
|
124
|
+
export function gzipSync(buffer, opts) {
|
125
|
+
return zlibBufferSync(new Gzip(opts), buffer);
|
126
|
+
}
|
127
|
+
|
128
|
+
export function deflateRaw(buffer, opts, callback) {
|
129
|
+
if (typeof opts === 'function') {
|
130
|
+
callback = opts;
|
131
|
+
opts = {};
|
132
|
+
}
|
133
|
+
return zlibBuffer(new DeflateRaw(opts), buffer, callback);
|
134
|
+
}
|
135
|
+
|
136
|
+
export function deflateRawSync(buffer, opts) {
|
137
|
+
return zlibBufferSync(new DeflateRaw(opts), buffer);
|
138
|
+
}
|
139
|
+
|
140
|
+
export function unzip(buffer, opts, callback) {
|
141
|
+
if (typeof opts === 'function') {
|
142
|
+
callback = opts;
|
143
|
+
opts = {};
|
144
|
+
}
|
145
|
+
return zlibBuffer(new Unzip(opts), buffer, callback);
|
146
|
+
}
|
147
|
+
|
148
|
+
export function unzipSync(buffer, opts) {
|
149
|
+
return zlibBufferSync(new Unzip(opts), buffer);
|
150
|
+
}
|
151
|
+
|
152
|
+
export function inflate(buffer, opts, callback) {
|
153
|
+
if (typeof opts === 'function') {
|
154
|
+
callback = opts;
|
155
|
+
opts = {};
|
156
|
+
}
|
157
|
+
return zlibBuffer(new Inflate(opts), buffer, callback);
|
158
|
+
}
|
159
|
+
|
160
|
+
export function inflateSync(buffer, opts) {
|
161
|
+
return zlibBufferSync(new Inflate(opts), buffer);
|
162
|
+
}
|
163
|
+
|
164
|
+
export function gunzip(buffer, opts, callback) {
|
165
|
+
if (typeof opts === 'function') {
|
166
|
+
callback = opts;
|
167
|
+
opts = {};
|
168
|
+
}
|
169
|
+
return zlibBuffer(new Gunzip(opts), buffer, callback);
|
170
|
+
}
|
171
|
+
|
172
|
+
export function gunzipSync(buffer, opts) {
|
173
|
+
return zlibBufferSync(new Gunzip(opts), buffer);
|
174
|
+
}
|
175
|
+
|
176
|
+
export function inflateRaw(buffer, opts, callback) {
|
177
|
+
if (typeof opts === 'function') {
|
178
|
+
callback = opts;
|
179
|
+
opts = {};
|
180
|
+
}
|
181
|
+
return zlibBuffer(new InflateRaw(opts), buffer, callback);
|
182
|
+
}
|
183
|
+
|
184
|
+
export function inflateRawSync(buffer, opts) {
|
185
|
+
return zlibBufferSync(new InflateRaw(opts), buffer);
|
186
|
+
}
|
187
|
+
|
188
|
+
function zlibBuffer(engine, buffer, callback) {
|
189
|
+
var buffers = [];
|
190
|
+
var nread = 0;
|
191
|
+
|
192
|
+
engine.on('error', onError);
|
193
|
+
engine.on('end', onEnd);
|
194
|
+
|
195
|
+
engine.end(buffer);
|
196
|
+
flow();
|
197
|
+
|
198
|
+
function flow() {
|
199
|
+
var chunk;
|
200
|
+
while (null !== (chunk = engine.read())) {
|
201
|
+
buffers.push(chunk);
|
202
|
+
nread += chunk.length;
|
203
|
+
}
|
204
|
+
engine.once('readable', flow);
|
205
|
+
}
|
206
|
+
|
207
|
+
function onError(err) {
|
208
|
+
engine.removeListener('end', onEnd);
|
209
|
+
engine.removeListener('readable', flow);
|
210
|
+
callback(err);
|
211
|
+
}
|
212
|
+
|
213
|
+
function onEnd() {
|
214
|
+
var buf = Buffer.concat(buffers, nread);
|
215
|
+
buffers = [];
|
216
|
+
callback(null, buf);
|
217
|
+
engine.close();
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
221
|
+
function zlibBufferSync(engine, buffer) {
|
222
|
+
if (typeof buffer === 'string')
|
223
|
+
buffer = new Buffer(buffer);
|
224
|
+
if (!Buffer.isBuffer(buffer))
|
225
|
+
throw new TypeError('Not a string or buffer');
|
226
|
+
|
227
|
+
var flushFlag = binding.Z_FINISH;
|
228
|
+
|
229
|
+
return engine._processChunk(buffer, flushFlag);
|
230
|
+
}
|
231
|
+
|
232
|
+
// generic zlib
|
233
|
+
// minimal 2-byte header
|
234
|
+
export function Deflate(opts) {
|
235
|
+
if (!(this instanceof Deflate)) return new Deflate(opts);
|
236
|
+
Zlib.call(this, opts, binding.DEFLATE);
|
237
|
+
}
|
238
|
+
|
239
|
+
export function Inflate(opts) {
|
240
|
+
if (!(this instanceof Inflate)) return new Inflate(opts);
|
241
|
+
Zlib.call(this, opts, binding.INFLATE);
|
242
|
+
}
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
// gzip - bigger header, same deflate compression
|
247
|
+
export function Gzip(opts) {
|
248
|
+
if (!(this instanceof Gzip)) return new Gzip(opts);
|
249
|
+
Zlib.call(this, opts, binding.GZIP);
|
250
|
+
}
|
251
|
+
|
252
|
+
export function Gunzip(opts) {
|
253
|
+
if (!(this instanceof Gunzip)) return new Gunzip(opts);
|
254
|
+
Zlib.call(this, opts, binding.GUNZIP);
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
// raw - no header
|
260
|
+
export function DeflateRaw(opts) {
|
261
|
+
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
|
262
|
+
Zlib.call(this, opts, binding.DEFLATERAW);
|
263
|
+
}
|
264
|
+
|
265
|
+
export function InflateRaw(opts) {
|
266
|
+
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
|
267
|
+
Zlib.call(this, opts, binding.INFLATERAW);
|
268
|
+
}
|
269
|
+
|
270
|
+
|
271
|
+
// auto-detect header.
|
272
|
+
export function Unzip(opts) {
|
273
|
+
if (!(this instanceof Unzip)) return new Unzip(opts);
|
274
|
+
Zlib.call(this, opts, binding.UNZIP);
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
// the Zlib class they all inherit from
|
279
|
+
// This thing manages the queue of requests, and returns
|
280
|
+
// true or false if there is anything in the queue when
|
281
|
+
// you call the .write() method.
|
282
|
+
|
283
|
+
export function Zlib(opts, mode) {
|
284
|
+
this._opts = opts = opts || {};
|
285
|
+
this._chunkSize = opts.chunkSize || binding.Z_DEFAULT_CHUNK;
|
286
|
+
|
287
|
+
Transform.call(this, opts);
|
288
|
+
|
289
|
+
if (opts.flush) {
|
290
|
+
if (opts.flush !== binding.Z_NO_FLUSH &&
|
291
|
+
opts.flush !== binding.Z_PARTIAL_FLUSH &&
|
292
|
+
opts.flush !== binding.Z_SYNC_FLUSH &&
|
293
|
+
opts.flush !== binding.Z_FULL_FLUSH &&
|
294
|
+
opts.flush !== binding.Z_FINISH &&
|
295
|
+
opts.flush !== binding.Z_BLOCK) {
|
296
|
+
throw new Error('Invalid flush flag: ' + opts.flush);
|
297
|
+
}
|
298
|
+
}
|
299
|
+
this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
|
300
|
+
|
301
|
+
if (opts.chunkSize) {
|
302
|
+
if (opts.chunkSize < binding.Z_MIN_CHUNK ||
|
303
|
+
opts.chunkSize > binding.Z_MAX_CHUNK) {
|
304
|
+
throw new Error('Invalid chunk size: ' + opts.chunkSize);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
if (opts.windowBits) {
|
309
|
+
if (opts.windowBits < binding.Z_MIN_WINDOWBITS ||
|
310
|
+
opts.windowBits > binding.Z_MAX_WINDOWBITS) {
|
311
|
+
throw new Error('Invalid windowBits: ' + opts.windowBits);
|
312
|
+
}
|
313
|
+
}
|
314
|
+
|
315
|
+
if (opts.level) {
|
316
|
+
if (opts.level < binding.Z_MIN_LEVEL ||
|
317
|
+
opts.level > binding.Z_MAX_LEVEL) {
|
318
|
+
throw new Error('Invalid compression level: ' + opts.level);
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
if (opts.memLevel) {
|
323
|
+
if (opts.memLevel < binding.Z_MIN_MEMLEVEL ||
|
324
|
+
opts.memLevel > binding.Z_MAX_MEMLEVEL) {
|
325
|
+
throw new Error('Invalid memLevel: ' + opts.memLevel);
|
326
|
+
}
|
327
|
+
}
|
328
|
+
|
329
|
+
if (opts.strategy) {
|
330
|
+
if (opts.strategy != binding.Z_FILTERED &&
|
331
|
+
opts.strategy != binding.Z_HUFFMAN_ONLY &&
|
332
|
+
opts.strategy != binding.Z_RLE &&
|
333
|
+
opts.strategy != binding.Z_FIXED &&
|
334
|
+
opts.strategy != binding.Z_DEFAULT_STRATEGY) {
|
335
|
+
throw new Error('Invalid strategy: ' + opts.strategy);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
if (opts.dictionary) {
|
340
|
+
if (!Buffer.isBuffer(opts.dictionary)) {
|
341
|
+
throw new Error('Invalid dictionary: it should be a Buffer instance');
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
this._binding = new binding.Zlib(mode);
|
346
|
+
|
347
|
+
var self = this;
|
348
|
+
this._hadError = false;
|
349
|
+
this._binding.onerror = function(message, errno) {
|
350
|
+
// there is no way to cleanly recover.
|
351
|
+
// continuing only obscures problems.
|
352
|
+
self._binding = null;
|
353
|
+
self._hadError = true;
|
354
|
+
|
355
|
+
var error = new Error(message);
|
356
|
+
error.errno = errno;
|
357
|
+
error.code = binding.codes[errno];
|
358
|
+
self.emit('error', error);
|
359
|
+
};
|
360
|
+
|
361
|
+
var level = binding.Z_DEFAULT_COMPRESSION;
|
362
|
+
if (typeof opts.level === 'number') level = opts.level;
|
363
|
+
|
364
|
+
var strategy = binding.Z_DEFAULT_STRATEGY;
|
365
|
+
if (typeof opts.strategy === 'number') strategy = opts.strategy;
|
366
|
+
|
367
|
+
this._binding.init(opts.windowBits || binding.Z_DEFAULT_WINDOWBITS,
|
368
|
+
level,
|
369
|
+
opts.memLevel || binding.Z_DEFAULT_MEMLEVEL,
|
370
|
+
strategy,
|
371
|
+
opts.dictionary);
|
372
|
+
|
373
|
+
this._buffer = new Buffer(this._chunkSize);
|
374
|
+
this._offset = 0;
|
375
|
+
this._closed = false;
|
376
|
+
this._level = level;
|
377
|
+
this._strategy = strategy;
|
378
|
+
|
379
|
+
this.once('end', this.close);
|
380
|
+
}
|
381
|
+
|
382
|
+
inherits(Zlib, Transform);
|
383
|
+
|
384
|
+
Zlib.prototype.params = function(level, strategy, callback) {
|
385
|
+
if (level < binding.Z_MIN_LEVEL ||
|
386
|
+
level > binding.Z_MAX_LEVEL) {
|
387
|
+
throw new RangeError('Invalid compression level: ' + level);
|
388
|
+
}
|
389
|
+
if (strategy != binding.Z_FILTERED &&
|
390
|
+
strategy != binding.Z_HUFFMAN_ONLY &&
|
391
|
+
strategy != binding.Z_RLE &&
|
392
|
+
strategy != binding.Z_FIXED &&
|
393
|
+
strategy != binding.Z_DEFAULT_STRATEGY) {
|
394
|
+
throw new TypeError('Invalid strategy: ' + strategy);
|
395
|
+
}
|
396
|
+
|
397
|
+
if (this._level !== level || this._strategy !== strategy) {
|
398
|
+
var self = this;
|
399
|
+
this.flush(binding.Z_SYNC_FLUSH, function() {
|
400
|
+
self._binding.params(level, strategy);
|
401
|
+
if (!self._hadError) {
|
402
|
+
self._level = level;
|
403
|
+
self._strategy = strategy;
|
404
|
+
if (callback) callback();
|
405
|
+
}
|
406
|
+
});
|
407
|
+
} else {
|
408
|
+
process.nextTick(callback);
|
409
|
+
}
|
410
|
+
};
|
411
|
+
|
412
|
+
Zlib.prototype.reset = function() {
|
413
|
+
return this._binding.reset();
|
414
|
+
};
|
415
|
+
|
416
|
+
// This is the _flush function called by the transform class,
|
417
|
+
// internally, when the last chunk has been written.
|
418
|
+
Zlib.prototype._flush = function(callback) {
|
419
|
+
this._transform(new Buffer(0), '', callback);
|
420
|
+
};
|
421
|
+
|
422
|
+
Zlib.prototype.flush = function(kind, callback) {
|
423
|
+
var ws = this._writableState;
|
424
|
+
|
425
|
+
if (typeof kind === 'function' || (kind === void 0 && !callback)) {
|
426
|
+
callback = kind;
|
427
|
+
kind = binding.Z_FULL_FLUSH;
|
428
|
+
}
|
429
|
+
|
430
|
+
if (ws.ended) {
|
431
|
+
if (callback)
|
432
|
+
process.nextTick(callback);
|
433
|
+
} else if (ws.ending) {
|
434
|
+
if (callback)
|
435
|
+
this.once('end', callback);
|
436
|
+
} else if (ws.needDrain) {
|
437
|
+
var self = this;
|
438
|
+
this.once('drain', function() {
|
439
|
+
self.flush(callback);
|
440
|
+
});
|
441
|
+
} else {
|
442
|
+
this._flushFlag = kind;
|
443
|
+
this.write(new Buffer(0), '', callback);
|
444
|
+
}
|
445
|
+
};
|
446
|
+
|
447
|
+
Zlib.prototype.close = function(callback) {
|
448
|
+
if (callback)
|
449
|
+
process.nextTick(callback);
|
450
|
+
|
451
|
+
if (this._closed)
|
452
|
+
return;
|
453
|
+
|
454
|
+
this._closed = true;
|
455
|
+
|
456
|
+
this._binding.close();
|
457
|
+
|
458
|
+
var self = this;
|
459
|
+
process.nextTick(function() {
|
460
|
+
self.emit('close');
|
461
|
+
});
|
462
|
+
};
|
463
|
+
|
464
|
+
Zlib.prototype._transform = function(chunk, encoding, cb) {
|
465
|
+
var flushFlag;
|
466
|
+
var ws = this._writableState;
|
467
|
+
var ending = ws.ending || ws.ended;
|
468
|
+
var last = ending && (!chunk || ws.length === chunk.length);
|
469
|
+
|
470
|
+
if (!chunk === null && !Buffer.isBuffer(chunk))
|
471
|
+
return cb(new Error('invalid input'));
|
472
|
+
|
473
|
+
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
|
474
|
+
// If it's explicitly flushing at some other time, then we use
|
475
|
+
// Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
|
476
|
+
// goodness.
|
477
|
+
if (last)
|
478
|
+
flushFlag = binding.Z_FINISH;
|
479
|
+
else {
|
480
|
+
flushFlag = this._flushFlag;
|
481
|
+
// once we've flushed the last of the queue, stop flushing and
|
482
|
+
// go back to the normal behavior.
|
483
|
+
if (chunk.length >= ws.length) {
|
484
|
+
this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
|
485
|
+
}
|
486
|
+
}
|
487
|
+
|
488
|
+
this._processChunk(chunk, flushFlag, cb);
|
489
|
+
};
|
490
|
+
|
491
|
+
Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
|
492
|
+
var availInBefore = chunk && chunk.length;
|
493
|
+
var availOutBefore = this._chunkSize - this._offset;
|
494
|
+
var inOff = 0;
|
495
|
+
|
496
|
+
var self = this;
|
497
|
+
|
498
|
+
var async = typeof cb === 'function';
|
499
|
+
|
500
|
+
if (!async) {
|
501
|
+
var buffers = [];
|
502
|
+
var nread = 0;
|
503
|
+
|
504
|
+
var error;
|
505
|
+
this.on('error', function(er) {
|
506
|
+
error = er;
|
507
|
+
});
|
508
|
+
|
509
|
+
do {
|
510
|
+
var res = this._binding.writeSync(flushFlag,
|
511
|
+
chunk, // in
|
512
|
+
inOff, // in_off
|
513
|
+
availInBefore, // in_len
|
514
|
+
this._buffer, // out
|
515
|
+
this._offset, //out_off
|
516
|
+
availOutBefore); // out_len
|
517
|
+
} while (!this._hadError && callback(res[0], res[1]));
|
518
|
+
|
519
|
+
if (this._hadError) {
|
520
|
+
throw error;
|
521
|
+
}
|
522
|
+
|
523
|
+
var buf = Buffer.concat(buffers, nread);
|
524
|
+
this.close();
|
525
|
+
|
526
|
+
return buf;
|
527
|
+
}
|
528
|
+
|
529
|
+
var req = this._binding.write(flushFlag,
|
530
|
+
chunk, // in
|
531
|
+
inOff, // in_off
|
532
|
+
availInBefore, // in_len
|
533
|
+
this._buffer, // out
|
534
|
+
this._offset, //out_off
|
535
|
+
availOutBefore); // out_len
|
536
|
+
|
537
|
+
req.buffer = chunk;
|
538
|
+
req.callback = callback;
|
539
|
+
|
540
|
+
function callback(availInAfter, availOutAfter) {
|
541
|
+
if (self._hadError)
|
542
|
+
return;
|
543
|
+
|
544
|
+
var have = availOutBefore - availOutAfter;
|
545
|
+
assert(have >= 0, 'have should not go down');
|
546
|
+
|
547
|
+
if (have > 0) {
|
548
|
+
var out = self._buffer.slice(self._offset, self._offset + have);
|
549
|
+
self._offset += have;
|
550
|
+
// serve some output to the consumer.
|
551
|
+
if (async) {
|
552
|
+
self.push(out);
|
553
|
+
} else {
|
554
|
+
buffers.push(out);
|
555
|
+
nread += out.length;
|
556
|
+
}
|
557
|
+
}
|
558
|
+
|
559
|
+
// exhausted the output buffer, or used all the input create a new one.
|
560
|
+
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
561
|
+
availOutBefore = self._chunkSize;
|
562
|
+
self._offset = 0;
|
563
|
+
self._buffer = new Buffer(self._chunkSize);
|
564
|
+
}
|
565
|
+
|
566
|
+
if (availOutAfter === 0) {
|
567
|
+
// Not actually done. Need to reprocess.
|
568
|
+
// Also, update the availInBefore to the availInAfter value,
|
569
|
+
// so that if we have to hit it a third (fourth, etc.) time,
|
570
|
+
// it'll have the correct byte counts.
|
571
|
+
inOff += (availInBefore - availInAfter);
|
572
|
+
availInBefore = availInAfter;
|
573
|
+
|
574
|
+
if (!async)
|
575
|
+
return true;
|
576
|
+
|
577
|
+
var newReq = self._binding.write(flushFlag,
|
578
|
+
chunk,
|
579
|
+
inOff,
|
580
|
+
availInBefore,
|
581
|
+
self._buffer,
|
582
|
+
self._offset,
|
583
|
+
self._chunkSize);
|
584
|
+
newReq.callback = callback; // this same function
|
585
|
+
newReq.buffer = chunk;
|
586
|
+
return;
|
587
|
+
}
|
588
|
+
|
589
|
+
if (!async)
|
590
|
+
return false;
|
591
|
+
|
592
|
+
// finished with the chunk.
|
593
|
+
cb();
|
594
|
+
}
|
595
|
+
};
|
596
|
+
|
597
|
+
inherits(Deflate, Zlib);
|
598
|
+
inherits(Inflate, Zlib);
|
599
|
+
inherits(Gzip, Zlib);
|
600
|
+
inherits(Gunzip, Zlib);
|
601
|
+
inherits(DeflateRaw, Zlib);
|
602
|
+
inherits(InflateRaw, Zlib);
|
603
|
+
inherits(Unzip, Zlib);
|
604
|
+
export default {
|
605
|
+
codes: codes,
|
606
|
+
createDeflate: createDeflate,
|
607
|
+
createInflate: createInflate,
|
608
|
+
createDeflateRaw: createDeflateRaw,
|
609
|
+
createInflateRaw: createInflateRaw,
|
610
|
+
createGzip: createGzip,
|
611
|
+
createGunzip: createGunzip,
|
612
|
+
createUnzip: createUnzip,
|
613
|
+
deflate: deflate,
|
614
|
+
deflateSync: deflateSync,
|
615
|
+
gzip: gzip,
|
616
|
+
gzipSync: gzipSync,
|
617
|
+
deflateRaw: deflateRaw,
|
618
|
+
deflateRawSync: deflateRawSync,
|
619
|
+
unzip: unzip,
|
620
|
+
unzipSync: unzipSync,
|
621
|
+
inflate: inflate,
|
622
|
+
inflateSync: inflateSync,
|
623
|
+
gunzip: gunzip,
|
624
|
+
gunzipSync: gunzipSync,
|
625
|
+
inflateRaw: inflateRaw,
|
626
|
+
inflateRawSync: inflateRawSync,
|
627
|
+
Deflate: Deflate,
|
628
|
+
Inflate: Inflate,
|
629
|
+
Gzip: Gzip,
|
630
|
+
Gunzip: Gunzip,
|
631
|
+
DeflateRaw: DeflateRaw,
|
632
|
+
InflateRaw: InflateRaw,
|
633
|
+
Unzip: Unzip,
|
634
|
+
Zlib: Zlib
|
635
|
+
};
|
package/src/index.js
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
import {join} from 'path';
|
2
|
+
const libs = new Map();
|
3
|
+
|
4
|
+
// our es6 versions
|
5
|
+
libs.set('process', require.resolve('process-es6'));
|
6
|
+
libs.set('buffer', require.resolve('buffer-es6'));
|
7
|
+
libs.set('util', require.resolve(join('..', 'src', 'es6', 'util')));
|
8
|
+
libs.set('sys', libs.get('util'));
|
9
|
+
libs.set('events', require.resolve(join('..', 'src', 'es6', 'events')));
|
10
|
+
libs.set('stream', require.resolve(join('..', 'src', 'es6', 'stream')));
|
11
|
+
libs.set('path', require.resolve(join('..', 'src', 'es6', 'path')));
|
12
|
+
libs.set('querystring', require.resolve(join('..', 'src', 'es6', 'qs')));
|
13
|
+
libs.set('punycode', require.resolve(join('..', 'src', 'es6', 'punycode')));
|
14
|
+
libs.set('url', require.resolve(join('..', 'src', 'es6', 'url')));
|
15
|
+
libs.set('string_decoder', require.resolve(join('..', 'src', 'es6', 'string-decoder')));
|
16
|
+
libs.set('http', require.resolve(join('..', 'src', 'es6', 'http')));
|
17
|
+
libs.set('https', require.resolve(join('..', 'src', 'es6', 'http')));
|
18
|
+
libs.set('os', require.resolve(join('..', 'src', 'es6', 'os')));
|
19
|
+
libs.set('assert', require.resolve(join('..', 'src', 'es6', 'assert')));
|
20
|
+
libs.set('constants', require.resolve('./constants'));
|
21
|
+
libs.set('_stream_duplex', require.resolve(join('..', 'src', 'es6', 'readable-stream', 'duplex')));
|
22
|
+
libs.set('_stream_passthrough', require.resolve(join('..', 'src', 'es6', 'readable-stream', 'passthrough')));
|
23
|
+
libs.set('_stream_readable', require.resolve(join('..', 'src', 'es6', 'readable-stream', 'readable')));
|
24
|
+
libs.set('_stream_writable', require.resolve(join('..', 'src', 'es6', 'readable-stream', 'writable')));
|
25
|
+
libs.set('_stream_transform', require.resolve(join('..', 'src', 'es6', 'readable-stream', 'transform')));
|
26
|
+
libs.set('timers', require.resolve(join('..', 'src', 'es6', 'timers')));
|
27
|
+
libs.set('console', require.resolve(join('..', 'src', 'es6', 'console')));
|
28
|
+
libs.set('vm', require.resolve(join('..', 'src', 'es6', 'vm')));
|
29
|
+
libs.set('zlib', require.resolve(join('..', 'src', 'es6', 'zlib')));
|
30
|
+
libs.set('tty', require.resolve(join('..', 'src', 'es6', 'tty')));
|
31
|
+
libs.set('domain', require.resolve(join('..', 'src', 'es6', 'domain')));
|
32
|
+
|
33
|
+
const CRYPTO_PATH = require.resolve('crypto-browserify');
|
34
|
+
const FS_PATH = require.resolve('browserify-fs');
|
35
|
+
const EMPTY_PATH = require.resolve(join('..', 'src', 'es6', 'empty'));
|
36
|
+
|
37
|
+
// not shimmed
|
38
|
+
libs.set('dns', EMPTY_PATH);
|
39
|
+
libs.set('dgram', EMPTY_PATH);
|
40
|
+
libs.set('child_process', EMPTY_PATH);
|
41
|
+
libs.set('cluster', EMPTY_PATH);
|
42
|
+
libs.set('module', EMPTY_PATH);
|
43
|
+
libs.set('net', EMPTY_PATH);
|
44
|
+
libs.set('readline', EMPTY_PATH);
|
45
|
+
libs.set('repl', EMPTY_PATH);
|
46
|
+
libs.set('tls', EMPTY_PATH);
|
47
|
+
|
48
|
+
|
49
|
+
export default function (opts) {
|
50
|
+
opts = opts || {};
|
51
|
+
let cryptoPath = EMPTY_PATH;
|
52
|
+
let fsPath = EMPTY_PATH;
|
53
|
+
if (opts.crypto) {
|
54
|
+
cryptoPath = CRYPTO_PATH;
|
55
|
+
}
|
56
|
+
if (opts.fs) {
|
57
|
+
fsPath = FS_PATH;
|
58
|
+
}
|
59
|
+
return {resolveId(importee) {
|
60
|
+
if (importee && importee.slice(-1) === '/') {
|
61
|
+
importee === importee.slice(0, -1);
|
62
|
+
}
|
63
|
+
if (libs.has(importee)) {
|
64
|
+
return libs.get(importee);
|
65
|
+
}
|
66
|
+
if (importee === 'crypto') {
|
67
|
+
return cryptoPath;
|
68
|
+
}
|
69
|
+
if (importee === 'fs') {
|
70
|
+
return fsPath;
|
71
|
+
}
|
72
|
+
}};
|
73
|
+
}
|