@eagleoutice/flowr-dev
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
122 lines • 6.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZstdCodec = exports.BrotliCodec = exports.CompressedExtPattern = exports.CompressedExts = exports.DefaultSigDbCodec = void 0;
exports.zstdSupported = zstdSupported;
exports.isCompressedExt = isCompressedExt;
exports.compressedExtOf = compressedExtOf;
exports.stripCompressedExt = stripCompressedExt;
exports.codecFor = codecFor;
exports.writeCodecs = writeCodecs;
exports.readableExtsPreferred = readableExtsPreferred;
exports.createDecompressFor = createDecompressFor;
exports.decompressSyncFor = decompressSyncFor;
/**
* The on-disk compression codec for sigdb bundles. Brotli (`.br`) is always written as a universal fallback
* (every Node ships it), and zstd (`.zst`, smaller + faster to decompress) is written additionally whenever the
* running Node exposes it (`node:zlib` zstd, Node 22.15+ / 23.8+). Readers pick the better available variant at
* runtime -- `.zst` when this Node can decompress it, else the `.br` fallback -- so bundles read on any Node,
* including the older Node bundled in the VS Code extension host. Only the outer compression changes -- the
* ranged reads and `.idx` seek logic operate on the decompressed plain NDJSON and are codec-agnostic.
*/
const zlib_1 = __importDefault(require("zlib"));
const zstd = zlib_1.default;
/** whether the running Node exposes the `node:zlib` zstd API (Node 22.15+ / 23.8+) */
function zstdSupported() {
return typeof zstd.zstdCompressSync === 'function' && typeof zstd.createZstdDecompress === 'function';
}
function requireZstd() {
if (!zstdSupported()) {
throw new Error('zstd sigdb bundles need Node >= 22.15 (node:zlib zstd support); upgrade Node or use the brotli codec');
}
return zstd;
}
/** the richer codec, preferred by readers when this Node supports it (writers always additionally emit `.br`) */
exports.DefaultSigDbCodec = 'zstd';
/** compressed extensions a reader understands (outer compression only); `.gz` is read-only legacy */
exports.CompressedExts = ['.br', '.zst', '.gz'];
/** whether a bundle path is a compressed source that must be decompressed to be seekable */
function isCompressedExt(file) {
return exports.CompressedExts.some(e => file.endsWith(e));
}
/** the compressed extension of a path, or `undefined` if it is a plain (uncompressed) source */
function compressedExtOf(file) {
return exports.CompressedExts.find(e => file.endsWith(e));
}
/** strip a trailing compressed extension (`.br`/`.zst`/`.gz`) from a filename, if present */
function stripCompressedExt(name) {
const ext = compressedExtOf(name);
return ext ? name.slice(0, -ext.length) : name;
}
/** regex alternation matching an optional trailing compressed extension, e.g. `(?:\.br|\.zst|\.gz)?` */
exports.CompressedExtPattern = `(?:${exports.CompressedExts.map(e => e.replace(/\./g, '\\.')).join('|')})?`;
// reading large-window brotli requires opting in; every `.br` flowR writes uses large windows, so this is always safe
const BrotliDecodeParams = { params: { [zlib_1.default.constants.BROTLI_DECODER_PARAM_LARGE_WINDOW]: 1 } };
function brotliParams(opts) {
const lgwin = opts.lgwin ?? 30;
const params = {
[zlib_1.default.constants.BROTLI_PARAM_QUALITY]: opts.level ?? 11,
[zlib_1.default.constants.BROTLI_PARAM_LGWIN]: lgwin,
[zlib_1.default.constants.BROTLI_PARAM_LARGE_WINDOW]: lgwin > 24 ? 1 : 0
};
if (opts.sizeHint !== undefined) {
params[zlib_1.default.constants.BROTLI_PARAM_SIZE_HINT] = opts.sizeHint;
}
return params;
}
function zstdParams(opts) {
return { [zstd.constants.ZSTD_c_compressionLevel]: opts.level ?? 19 };
}
exports.BrotliCodec = {
codec: 'brotli',
ext: '.br',
compressSync: (buf, opts = {}) => zlib_1.default.brotliCompressSync(buf, { params: brotliParams(opts) }),
createCompress: (opts = {}) => zlib_1.default.createBrotliCompress({ params: brotliParams(opts) })
};
exports.ZstdCodec = {
codec: 'zstd',
ext: '.zst',
compressSync: (buf, opts = {}) => requireZstd().zstdCompressSync(buf, { params: zstdParams(opts) }),
createCompress: (opts = {}) => requireZstd().createZstdCompress({ params: zstdParams(opts) })
};
/** the codec spec for a codec name (used when writing) */
function codecFor(codec) {
return codec === 'zstd' ? exports.ZstdCodec : exports.BrotliCodec;
}
/**
* The codec specs a writer emits: brotli `.br` always (the universal fallback -- every Node ships it), plus
* zstd `.zst` when this Node can produce/read it. So a `.br` fallback is guaranteed next to every `.zst`.
*/
function writeCodecs() {
return zstdSupported() ? [exports.BrotliCodec, exports.ZstdCodec] : [exports.BrotliCodec];
}
/**
* Compressed extensions this runtime can actually decompress, most-preferred first (zstd before brotli when
* supported). `.zst` is omitted entirely when this Node lacks zstd, so a reader never picks a variant it cannot read.
*/
function readableExtsPreferred() {
return zstdSupported() ? ['.zst', '.br', '.gz'] : ['.br', '.gz'];
}
/** a streaming decompressor for a compressed source, chosen by its extension (`.zst`/`.gz`/`.br`) */
function createDecompressFor(file) {
if (file.endsWith('.zst')) {
return requireZstd().createZstdDecompress();
}
if (file.endsWith('.gz')) {
return zlib_1.default.createGunzip();
}
return zlib_1.default.createBrotliDecompress(BrotliDecodeParams);
}
/** synchronously decompress a compressed buffer, choosing the codec by the source's extension */
function decompressSyncFor(file, raw) {
if (file.endsWith('.zst')) {
return requireZstd().zstdDecompressSync(raw);
}
if (file.endsWith('.gz')) {
return zlib_1.default.gunzipSync(raw);
}
return zlib_1.default.brotliDecompressSync(raw, BrotliDecodeParams);
}
//# sourceMappingURL=codec.js.map