Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
index.js416 linesDownload Raw Back to commonjs
1"use strict";
2var __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}));
13var __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});
18var __importStar = (this && this.__importStar) || (function () {
19    var ownKeys = function(o) {
20        ownKeys = Object.getOwnPropertyNames || function (o) {
21            var ar = [];
22            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23            return ar;
24        };
25        return ownKeys(o);
26    };
27    return function (mod) {
28        if (mod && mod.__esModule) return mod;
29        var result = {};
30        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31        __setModuleDefault(result, mod);
32        return result;
33    };
34})();
35var __importDefault = (this && this.__importDefault) || function (mod) {
36    return (mod && mod.__esModule) ? mod : { "default": mod };
37};
38Object.defineProperty(exports, "__esModule", { value: true });
39exports.ZstdDecompress = exports.ZstdCompress = exports.BrotliDecompress = exports.BrotliCompress = exports.Unzip = exports.InflateRaw = exports.DeflateRaw = exports.Gunzip = exports.Gzip = exports.Inflate = exports.Deflate = exports.Zlib = exports.ZlibError = exports.constants = void 0;
40const assert_1 = __importDefault(require("assert"));
41const buffer_1 = require("buffer");
42const minipass_1 = require("minipass");
43const realZlib = __importStar(require("zlib"));
44const constants_js_1 = require("./constants.js");
45var constants_js_2 = require("./constants.js");
46Object.defineProperty(exports, "constants", { enumerable: true, get: function () { return constants_js_2.constants; } });
47const OriginalBufferConcat = buffer_1.Buffer.concat;
48const desc = Object.getOwnPropertyDescriptor(buffer_1.Buffer, 'concat');
49const noop = (args) => args;
50const passthroughBufferConcat = desc?.writable === true || desc?.set !== undefined
51    ? (makeNoOp) => {
52        buffer_1.Buffer.concat = makeNoOp ? noop : OriginalBufferConcat;
53    }
54    : (_) => { };
55const _superWrite = Symbol('_superWrite');
56class ZlibError extends Error {
57    code;
58    errno;
59    constructor(err, origin) {
60        super('zlib: ' + err.message, { cause: err });
61        this.code = err.code;
62        this.errno = err.errno;
63        /* c8 ignore next */
64        if (!this.code)
65            this.code = 'ZLIB_ERROR';
66        this.message = 'zlib: ' + err.message;
67        Error.captureStackTrace(this, origin ?? this.constructor);
68    }
69    get name() {
70        return 'ZlibError';
71    }
72}
73exports.ZlibError = ZlibError;
74// the Zlib class they all inherit from
75// This thing manages the queue of requests, and returns
76// true or false if there is anything in the queue when
77// you call the .write() method.
78const _flushFlag = Symbol('flushFlag');
79class ZlibBase extends minipass_1.Minipass {
80    #sawError = false;
81    #ended = false;
82    #flushFlag;
83    #finishFlushFlag;
84    #fullFlushFlag;
85    #handle;
86    #onError;
87    get sawError() {
88        return this.#sawError;
89    }
90    get handle() {
91        return this.#handle;
92    }
93    /* c8 ignore start */
94    get flushFlag() {
95        return this.#flushFlag;
96    }
97    /* c8 ignore stop */
98    constructor(opts, mode) {
99        if (!opts || typeof opts !== 'object')
100            throw new TypeError('invalid options for ZlibBase constructor');
101        //@ts-ignore
102        super(opts);
103        /* c8 ignore start */
104        this.#flushFlag = opts.flush ?? 0;
105        this.#finishFlushFlag = opts.finishFlush ?? 0;
106        this.#fullFlushFlag = opts.fullFlushFlag ?? 0;
107        /* c8 ignore stop */
108        //@ts-ignore
109        if (typeof realZlib[mode] !== 'function') {
110            throw new TypeError('Compression method not supported: ' + mode);
111        }
112        // this will throw if any options are invalid for the class selected
113        try {
114            // @types/node doesn't know that it exports the classes, but they're there
115            //@ts-ignore
116            this.#handle = new realZlib[mode](opts);
117        }
118        catch (er) {
119            // make sure that all errors get decorated properly
120            throw new ZlibError(er, this.constructor);
121        }
122        this.#onError = err => {
123            // no sense raising multiple errors, since we abort on the first one.
124            if (this.#sawError)
125                return;
126            this.#sawError = true;
127            // there is no way to cleanly recover.
128            // continuing only obscures problems.
129            this.close();
130            this.emit('error', err);
131        };
132        this.#handle?.on('error', er => this.#onError(new ZlibError(er)));
133        this.once('end', () => this.close);
134    }
135    close() {
136        if (this.#handle) {
137            this.#handle.close();
138            this.#handle = undefined;
139            this.emit('close');
140        }
141    }
142    reset() {
143        if (!this.#sawError) {
144            (0, assert_1.default)(this.#handle, 'zlib binding closed');
145            //@ts-ignore
146            return this.#handle.reset?.();
147        }
148    }
149    flush(flushFlag) {
150        if (this.ended)
151            return;
152        if (typeof flushFlag !== 'number')
153            flushFlag = this.#fullFlushFlag;
154        this.write(Object.assign(buffer_1.Buffer.alloc(0), { [_flushFlag]: flushFlag }));
155    }
156    end(chunk, encoding, cb) {
157        /* c8 ignore start */
158        if (typeof chunk === 'function') {
159            cb = chunk;
160            encoding = undefined;
161            chunk = undefined;
162        }
163        if (typeof encoding === 'function') {
164            cb = encoding;
165            encoding = undefined;
166        }
167        /* c8 ignore stop */
168        if (chunk) {
169            if (encoding)
170                this.write(chunk, encoding);
171            else
172                this.write(chunk);
173        }
174        this.flush(this.#finishFlushFlag);
175        this.#ended = true;
176        return super.end(cb);
177    }
178    get ended() {
179        return this.#ended;
180    }
181    // overridden in the gzip classes to do portable writes
182    [_superWrite](data) {
183        return super.write(data);
184    }
185    write(chunk, encoding, cb) {
186        // process the chunk using the sync process
187        // then super.write() all the outputted chunks
188        if (typeof encoding === 'function')
189            (cb = encoding), (encoding = 'utf8');
190        if (typeof chunk === 'string')
191            chunk = buffer_1.Buffer.from(chunk, encoding);
192        if (this.#sawError)
193            return;
194        (0, assert_1.default)(this.#handle, 'zlib binding closed');
195        // _processChunk tries to .close() the native handle after it's done, so we
196        // intercept that by temporarily making it a no-op.
197        // diving into the node:zlib internals a bit here
198        const nativeHandle = this.#handle
199            ._handle;
200        const originalNativeClose = nativeHandle.close;
201        nativeHandle.close = () => { };
202        const originalClose = this.#handle.close;
203        this.#handle.close = () => { };
204        // It also calls `Buffer.concat()` at the end, which may be convenient
205        // for some, but which we are not interested in as it slows us down.
206        passthroughBufferConcat(true);
207        let result = undefined;
208        try {
209            const flushFlag = typeof chunk[_flushFlag] === 'number'
210                ? chunk[_flushFlag]
211                : this.#flushFlag;
212            result = this.#handle._processChunk(chunk, flushFlag);
213            // if we don't throw, reset it back how it was
214            passthroughBufferConcat(false);
215        }
216        catch (err) {
217            // or if we do, put Buffer.concat() back before we emit error
218            // Error events call into user code, which may call Buffer.concat()
219            passthroughBufferConcat(false);
220            this.#onError(new ZlibError(err, this.write));
221        }
222        finally {
223            if (this.#handle) {
224                // Core zlib resets `_handle` to null after attempting to close the
225                // native handle. Our no-op handler prevented actual closure, but we
226                // need to restore the `._handle` property.
227                ;
228                this.#handle._handle =
229                    nativeHandle;
230                nativeHandle.close = originalNativeClose;
231                this.#handle.close = originalClose;
232                // `_processChunk()` adds an 'error' listener. If we don't remove it
233                // after each call, these handlers start piling up.
234                this.#handle.removeAllListeners('error');
235                // make sure OUR error listener is still attached tho
236            }
237        }
238        if (this.#handle)
239            this.#handle.on('error', er => this.#onError(new ZlibError(er, this.write)));
240        let writeReturn;
241        if (result) {
242            if (Array.isArray(result) && result.length > 0) {
243                const r = result[0];
244                // The first buffer is always `handle._outBuffer`, which would be
245                // re-used for later invocations; so, we always have to copy that one.
246                writeReturn = this[_superWrite](buffer_1.Buffer.from(r));
247                for (let i = 1; i < result.length; i++) {
248                    writeReturn = this[_superWrite](result[i]);
249                }
250            }
251            else {
252                // either a single Buffer or an empty array
253                writeReturn = this[_superWrite](buffer_1.Buffer.from(result));
254            }
255        }
256        if (cb)
257            cb();
258        return writeReturn;
259    }
260}
261class Zlib extends ZlibBase {
262    #level;
263    #strategy;
264    constructor(opts, mode) {
265        opts = opts || {};
266        opts.flush = opts.flush || constants_js_1.constants.Z_NO_FLUSH;
267        opts.finishFlush = opts.finishFlush || constants_js_1.constants.Z_FINISH;
268        opts.fullFlushFlag = constants_js_1.constants.Z_FULL_FLUSH;
269        super(opts, mode);
270        this.#level = opts.level;
271        this.#strategy = opts.strategy;
272    }
273    params(level, strategy) {
274        if (this.sawError)
275            return;
276        if (!this.handle)
277            throw new Error('cannot switch params when binding is closed');
278        // no way to test this without also not supporting params at all
279        /* c8 ignore start */
280        if (!this.handle.params)
281            throw new Error('not supported in this implementation');
282        /* c8 ignore stop */
283        if (this.#level !== level || this.#strategy !== strategy) {
284            this.flush(constants_js_1.constants.Z_SYNC_FLUSH);
285            (0, assert_1.default)(this.handle, 'zlib binding closed');
286            // .params() calls .flush(), but the latter is always async in the
287            // core zlib. We override .flush() temporarily to intercept that and
288            // flush synchronously.
289            const origFlush = this.handle.flush;
290            this.handle.flush = (flushFlag, cb) => {
291                /* c8 ignore start */
292                if (typeof flushFlag === 'function') {
293                    cb = flushFlag;
294                    flushFlag = this.flushFlag;
295                }
296                /* c8 ignore stop */
297                this.flush(flushFlag);
298                cb?.();
299            };
300            try {
301                ;
302                this.handle.params(level, strategy);
303            }
304            finally {
305                this.handle.flush = origFlush;
306            }
307            /* c8 ignore start */
308            if (this.handle) {
309                this.#level = level;
310                this.#strategy = strategy;
311            }
312            /* c8 ignore stop */
313        }
314    }
315}
316exports.Zlib = Zlib;
317// minimal 2-byte header
318class Deflate extends Zlib {
319    constructor(opts) {
320        super(opts, 'Deflate');
321    }
322}
323exports.Deflate = Deflate;
324class Inflate extends Zlib {
325    constructor(opts) {
326        super(opts, 'Inflate');
327    }
328}
329exports.Inflate = Inflate;
330class Gzip extends Zlib {
331    #portable;
332    constructor(opts) {
333        super(opts, 'Gzip');
334        this.#portable = opts && !!opts.portable;
335    }
336    [_superWrite](data) {
337        if (!this.#portable)
338            return super[_superWrite](data);
339        // we'll always get the header emitted in one first chunk
340        // overwrite the OS indicator byte with 0xFF
341        this.#portable = false;
342        data[9] = 255;
343        return super[_superWrite](data);
344    }
345}
346exports.Gzip = Gzip;
347class Gunzip extends Zlib {
348    constructor(opts) {
349        super(opts, 'Gunzip');
350    }
351}
352exports.Gunzip = Gunzip;
353// raw - no header
354class DeflateRaw extends Zlib {
355    constructor(opts) {
356        super(opts, 'DeflateRaw');
357    }
358}
359exports.DeflateRaw = DeflateRaw;
360class InflateRaw extends Zlib {
361    constructor(opts) {
362        super(opts, 'InflateRaw');
363    }
364}
365exports.InflateRaw = InflateRaw;
366// auto-detect header.
367class Unzip extends Zlib {
368    constructor(opts) {
369        super(opts, 'Unzip');
370    }
371}
372exports.Unzip = Unzip;
373class Brotli extends ZlibBase {
374    constructor(opts, mode) {
375        opts = opts || {};
376        opts.flush = opts.flush || constants_js_1.constants.BROTLI_OPERATION_PROCESS;
377        opts.finishFlush =
378            opts.finishFlush || constants_js_1.constants.BROTLI_OPERATION_FINISH;
379        opts.fullFlushFlag = constants_js_1.constants.BROTLI_OPERATION_FLUSH;
380        super(opts, mode);
381    }
382}
383class BrotliCompress extends Brotli {
384    constructor(opts) {
385        super(opts, 'BrotliCompress');
386    }
387}
388exports.BrotliCompress = BrotliCompress;
389class BrotliDecompress extends Brotli {
390    constructor(opts) {
391        super(opts, 'BrotliDecompress');
392    }
393}
394exports.BrotliDecompress = BrotliDecompress;
395class Zstd extends ZlibBase {
396    constructor(opts, mode) {
397        opts = opts || {};
398        opts.flush = opts.flush || constants_js_1.constants.ZSTD_e_continue;
399        opts.finishFlush = opts.finishFlush || constants_js_1.constants.ZSTD_e_end;
400        opts.fullFlushFlag = constants_js_1.constants.ZSTD_e_flush;
401        super(opts, mode);
402    }
403}
404class ZstdCompress extends Zstd {
405    constructor(opts) {
406        super(opts, 'ZstdCompress');
407    }
408}
409exports.ZstdCompress = ZstdCompress;
410class ZstdDecompress extends Zstd {
411    constructor(opts) {
412        super(opts, 'ZstdDecompress');
413    }
414}
415exports.ZstdDecompress = ZstdDecompress;
416//# sourceMappingURL=index.js.map
codekingpro/portable-devtools · Team Ai