UNPKG

@jsonlines/core

Version:

parse and stringify jsonlines files through streams

67 lines (55 loc) 2.4 kB
import 'core-js/modules/es.array.iterator'; import 'core-js/modules/es.promise'; import { b as getLineSepString, _ as _classPrivateFieldSet, a as _classPrivateFieldGet } from './_chunks/line-sep-6e4e0e79.js'; import { Transform } from 'stream'; import { A as AsyncDuplexBase } from './_chunks/duplex-base-32ed1b65.js'; var _stringify = new WeakMap(); class JsonLinesStringifyStream extends Transform { constructor(options) { var _options$lineSep, _options$stringify; super({ writableObjectMode: true }); _stringify.set(this, { writable: true, value: void 0 }); this.encoding = options === null || options === void 0 ? void 0 : options.encoding; this.lineSep = getLineSepString((_options$lineSep = options === null || options === void 0 ? void 0 : options.lineSep) !== null && _options$lineSep !== void 0 ? _options$lineSep : "lf"); _classPrivateFieldSet(this, _stringify, (_options$stringify = options === null || options === void 0 ? void 0 : options.stringify) !== null && _options$stringify !== void 0 ? _options$stringify : JSON.stringify); } async _transformAsync(chunk) { const str = await _classPrivateFieldGet(this, _stringify).call(this, chunk); const buf = Buffer.from(str + this.lineSep, this.encoding); this.push(buf); } _transform(chunk, encoding, callback) { this._transformAsync(chunk).then(() => callback()).catch(err => callback(err)); } } class JsonLinesStringifyStreamWithGzip extends AsyncDuplexBase { constructor(options) { const gzip = options === null || options === void 0 ? void 0 : options.gzip; const stringifyStream = new JsonLinesStringifyStream(options); super({ readable: gzip ? () => import('zlib').then(zlib => { const gzipStream = zlib.createGzip(gzip === true ? undefined : gzip); stringifyStream.pipe(gzipStream); return gzipStream; }) : stringifyStream, writable: stringifyStream }, { readableObjectMode: false, writableObjectMode: true }); } } function stringify(options) { if (options === null || options === void 0 ? void 0 : options.gzip) { return new JsonLinesStringifyStreamWithGzip(options); } else { return new JsonLinesStringifyStream(options); } } export { JsonLinesStringifyStream, JsonLinesStringifyStreamWithGzip, stringify }; //# sourceMappingURL=stringify.js.map