@jsonlines/core
Version:
parse and stringify jsonlines files through streams
94 lines (79 loc) • 3.24 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
require('core-js/modules/es.array.iterator');
require('core-js/modules/es.promise');
var lineSep = require('./_chunks/line-sep-69d200dd.js');
var stream = require('stream');
var duplexBase = require('./_chunks/duplex-base-8f9de019.js');
function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
}
var _stringify = new WeakMap();
class JsonLinesStringifyStream extends stream.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 = lineSep.getLineSepString((_options$lineSep = options === null || options === void 0 ? void 0 : options.lineSep) !== null && _options$lineSep !== void 0 ? _options$lineSep : "lf");
lineSep._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 lineSep._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 duplexBase.AsyncDuplexBase {
constructor(options) {
const gzip = options === null || options === void 0 ? void 0 : options.gzip;
const stringifyStream = new JsonLinesStringifyStream(options);
super({
readable: gzip ? () => Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('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);
}
}
exports.JsonLinesStringifyStream = JsonLinesStringifyStream;
exports.JsonLinesStringifyStreamWithGzip = JsonLinesStringifyStreamWithGzip;
exports.stringify = stringify;
//# sourceMappingURL=stringify.js.map