Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
index.js64 linesDownload Raw Back to esm
1import { Minipass } from 'minipass';
2const isBufferEncoding = (enc) => typeof enc === 'string';
3export class SizeError extends Error {
4    expect;
5    found;
6    code = 'EBADSIZE';
7    constructor(found, expect, from) {
8        super(`Bad data size: expected ${expect} bytes, but got ${found}`);
9        this.expect = expect;
10        this.found = found;
11        Error.captureStackTrace(this, from ?? this.constructor);
12    }
13    get name() {
14        return 'SizeError';
15    }
16}
17export class MinipassSized extends Minipass {
18    found = 0;
19    expect;
20    constructor(options) {
21        const size = options?.size;
22        if (typeof size !== 'number' ||
23            size > Number.MAX_SAFE_INTEGER ||
24            isNaN(size) ||
25            size < 0 ||
26            !isFinite(size) ||
27            size !== Math.floor(size)) {
28            throw new Error('invalid expected size: ' + size);
29        }
30        //@ts-ignore
31        super(options);
32        if (options.objectMode) {
33            throw new TypeError(`${this.constructor.name} streams only work with string and buffer data`);
34        }
35        this.expect = size;
36    }
37    write(chunk, encoding, cb) {
38        const buffer = Buffer.isBuffer(chunk) ? chunk
39            : typeof chunk === 'string' ?
40                Buffer.from(chunk, isBufferEncoding(encoding) ? encoding : 'utf8')
41                : chunk;
42        if (typeof encoding === 'function') {
43            cb = encoding;
44            encoding = null;
45        }
46        if (!Buffer.isBuffer(buffer)) {
47            this.emit('error', new TypeError(`${this.constructor.name} streams only work with string and buffer data`));
48            return false;
49        }
50        this.found += buffer.length;
51        if (this.found > this.expect)
52            this.emit('error', new SizeError(this.found, this.expect));
53        return super.write(chunk, encoding, cb);
54    }
55    emit(ev, ...args) {
56        if (ev === 'end') {
57            if (this.found !== this.expect) {
58                this.emit('error', new SizeError(this.found, this.expect, this.emit));
59            }
60        }
61        return super.emit(ev, ...args);
62    }
63}
64//# sourceMappingURL=index.js.map
codekingpro/portable-devtools · Team Ai