Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 235 additions & 36 deletions pkgs/cold-store-reaper/worker/dist/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ../../../../../code/pkgs/cold-store-reaper/worker/node_modules/fzstd/esm/index.mjs
// node_modules/fzstd/esm/index.mjs
var ab = ArrayBuffer;
var u8 = Uint8Array;
var u16 = Uint16Array;
Expand Down Expand Up @@ -582,46 +582,235 @@ var cct = function(bufs, ol) {
}
return buf;
};
function decompress(dat, buf) {
var bufs = [], nb = +!buf;
var bt = 0, ol = 0;
for (; dat.length; ) {
var st = rzfh(dat, nb || buf);
if (typeof st == "object") {
if (nb) {
buf = null;
if (st.w.length == st.u) {
bufs.push(buf = st.w);
ol += st.u;
var Decompress = /* @__PURE__ */ (function() {
function Decompress2(ondata) {
this.ondata = ondata;
this.c = [];
this.l = 0;
this.z = 0;
}
Decompress2.prototype.push = function(chunk, final) {
if (typeof this.s == "number") {
var sub = Math.min(chunk.length, this.s);
chunk = chunk.subarray(sub);
this.s -= sub;
}
var sl = chunk.length;
var ncs = sl + this.l;
if (!this.s) {
if (final) {
if (!ncs) {
this.ondata(new u8(0), true);
return;
}
} else {
bufs.push(buf);
st.e = 0;
if (ncs < 5)
err(5);
} else if (ncs < 18) {
this.c.push(chunk);
this.l = ncs;
return;
}
if (this.l) {
this.c.push(chunk);
chunk = cct(this.c, ncs);
this.c = [];
this.l = 0;
}
for (; !st.l; ) {
var blk = rzb(dat, st, buf);
if (!blk)
if (typeof (this.s = rzfh(chunk)) == "number")
return this.push(chunk, final);
}
if (typeof this.s != "number") {
if (ncs < (this.z || 3)) {
if (final)
err(5);
if (buf)
st.e = st.y;
else {
bufs.push(blk);
ol += blk.length;
cpw(st.w, 0, blk.length);
st.w.set(blk, st.w.length - blk.length);
this.c.push(chunk);
this.l = ncs;
return;
}
if (this.l) {
this.c.push(chunk);
chunk = cct(this.c, ncs);
this.c = [];
this.l = 0;
}
if (!this.z && ncs < (this.z = chunk[this.s.b] & 2 ? 4 : 3 + (chunk[this.s.b] >> 3 | chunk[this.s.b + 1] << 5 | chunk[this.s.b + 2] << 13))) {
if (final)
err(5);
this.c.push(chunk);
this.l = ncs;
return;
} else
this.z = 0;
for (; ; ) {
var blk = rzb(chunk, this.s);
if (!blk) {
if (final)
err(5);
var adc = chunk.subarray(this.s.b);
this.s.b = 0;
this.c.push(adc), this.l += adc.length;
return;
} else {
this.ondata(blk, false);
cpw(this.s.w, 0, blk.length);
this.s.w.set(blk, this.s.w.length - blk.length);
}
if (this.s.l) {
var rest = chunk.subarray(this.s.b);
this.s = this.s.c * 4;
this.push(rest, final);
return;
}
}
bt = st.b + st.c * 4;
} else
bt = st;
dat = dat.subarray(bt);
}
return cct(bufs, ol);
}
} else if (final)
err(5);
};
return Decompress2;
})();

// src/decoder.js
function decompressFrame(frameBytes) {
return decompress(frameBytes);
var ExpansionLimitError = class extends Error {
constructor(limit, produced, message) {
super(
message ?? `zstd frame expands past the expected size: limit ${limit} bytes, already produced ${produced} bytes \u2014 refusing to return unverified bytes`
);
this.name = "ExpansionLimitError";
this.limit = limit;
}
};
var INPUT_SLICE_BYTES = 16 * 1024;
var MIN_WINDOW_ALLOWANCE_BYTES = 8 * 1024 * 1024;
var ZSTD_MAGIC = 4247762216;
var SKIPPABLE_MAGIC_MIN = 407710288;
var SKIPPABLE_MAGIC_MAX = 407710303;
function parseZstdFrameHeader(bytes) {
let off = 0;
for (; ; ) {
if (bytes.length - off < 5) return null;
const view2 = new DataView(bytes.buffer, bytes.byteOffset + off);
const magic = view2.getUint32(0, true);
if (magic >= SKIPPABLE_MAGIC_MIN && magic <= SKIPPABLE_MAGIC_MAX) {
if (bytes.length - off < 8) return null;
off += 8 + view2.getUint32(4, true);
continue;
}
if (magic !== ZSTD_MAGIC) return null;
break;
}
const head = off + 4;
const descriptor = bytes[head];
const fcsCode = descriptor >> 6;
const singleSegment = (descriptor & 32) !== 0;
const checksum = (descriptor & 4) !== 0;
const dictIdCode = descriptor & 3;
const dictIdBytes = [0, 1, 2, 4][dictIdCode];
const fcsBytes = singleSegment ? [1, 2, 4, 8][fcsCode] : [0, 2, 4, 8][fcsCode];
const headerLength = 1 + (singleSegment ? 0 : 1) + dictIdBytes + fcsBytes;
if (bytes.length - head < headerLength) return null;
const view = new DataView(bytes.buffer, bytes.byteOffset + head);
let pos = 1;
let windowSize;
if (singleSegment) {
windowSize = null;
} else {
const wd = view.getUint8(pos);
pos += 1;
const windowLog = 10 + (wd >> 3);
const windowBase = 2 ** windowLog;
windowSize = windowBase + windowBase / 8 * (wd & 7);
}
pos += dictIdBytes;
let contentSize = null;
if (fcsBytes > 0) {
if (fcsBytes === 8) {
const v = view.getBigUint64(pos, true);
contentSize = v > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(v);
} else {
let v = 0;
for (let i = fcsBytes - 1; i >= 0; i--) v = v * 256 + view.getUint8(pos + i);
contentSize = fcsBytes === 2 ? v + 256 : v;
}
}
if (singleSegment) windowSize = contentSize;
return { contentSize, windowSize, frameStart: off, headerLength, checksum };
}
function zstdFrameEnd(bytes, info) {
let pos = info.frameStart + 4 + info.headerLength;
for (; ; ) {
if (pos + 3 > bytes.length) return null;
const bh = bytes[pos] | bytes[pos + 1] << 8 | bytes[pos + 2] << 16;
pos += 3;
const last = (bh & 1) !== 0;
const type = bh >> 1 & 3;
const size = bh >> 3;
if (type === 3) return null;
pos += type === 1 ? 1 : size;
if (pos > bytes.length) return null;
if (last) break;
}
if (info.checksum) pos += 4;
return pos <= bytes.length ? pos : null;
}
function decompressFrame(frameBytes, maxPlainBytes) {
if (!Number.isSafeInteger(maxPlainBytes) || maxPlainBytes < 0) {
throw new TypeError(
`decompressFrame requires a non-negative integer byte limit, got ${maxPlainBytes}`
);
}
const header = parseZstdFrameHeader(frameBytes);
if (header !== null) {
if (header.contentSize !== null && header.contentSize > maxPlainBytes) {
throw new ExpansionLimitError(
maxPlainBytes,
header.contentSize,
`zstd frame declares a content size of ${header.contentSize} bytes, past the expected size (limit ${maxPlainBytes} bytes) \u2014 refusing to return unverified bytes`
);
}
const windowCap = Math.max(maxPlainBytes, MIN_WINDOW_ALLOWANCE_BYTES);
if (header.windowSize !== null && header.windowSize > windowCap) {
throw new ExpansionLimitError(
maxPlainBytes,
header.windowSize,
`zstd frame declares a ${header.windowSize}-byte window, over the ${windowCap}-byte bound for an expected size of ${maxPlainBytes} bytes \u2014 refusing to return unverified bytes`
);
}
const end = zstdFrameEnd(frameBytes, header);
if (end !== null && end < frameBytes.length) {
throw new Error(
`expected exactly one zstd frame, but ${frameBytes.length - end} trailing byte(s) follow it \u2014 refusing to decode concatenated frames`
);
}
}
const chunks = [];
let total = 0;
let exceeded = false;
const stream = new Decompress((data) => {
if (exceeded) return;
total += data.length;
if (total > maxPlainBytes) {
exceeded = true;
chunks.length = 0;
return;
}
chunks.push(data);
});
if (frameBytes.length === 0) {
stream.push(new Uint8Array(0), true);
}
for (let off = 0; off < frameBytes.length && !exceeded; off += INPUT_SLICE_BYTES) {
const end = Math.min(frameBytes.length, off + INPUT_SLICE_BYTES);
stream.push(frameBytes.subarray(off, end), end === frameBytes.length);
}
if (exceeded) {
throw new ExpansionLimitError(maxPlainBytes, total);
}
const out = new Uint8Array(total);
let pos = 0;
for (const chunk of chunks) {
out.set(chunk, pos);
pos += chunk.length;
}
return out;
}

// src/index.js
Expand Down Expand Up @@ -749,6 +938,13 @@ var BundleFlight = class {
}
async rehydrate(job) {
const { drive_file_id, offset, frame_length, sha256, bundle_id } = job;
const expectedSize = typeof job.size === "string" && job.size.trim() !== "" ? Number(job.size) : job.size;
if (!Number.isSafeInteger(expectedSize) || expectedSize < 0) {
throw new HttpError(
502,
"manifest entry has no usable expected size \u2014 refusing unbounded decompression"
);
}
const token = await this.accessToken();
const range = `bytes=${offset}-${offset + frame_length - 1}`;
const resp = await fetch(`${DRIVE_FILES_URL}/${drive_file_id}?alt=media`, {
Expand All @@ -771,8 +967,11 @@ var BundleFlight = class {
}
let plain;
try {
plain = decompressFrame(frame);
} catch {
plain = decompressFrame(frame, expectedSize);
} catch (err2) {
if (err2 instanceof ExpansionLimitError) {
throw new HttpError(502, err2.message);
}
throw new HttpError(502, "zstd frame decompression failed");
}
const digest = await sha256Hex(plain);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cold-store-reaper/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"private": true,
"scripts": {
"build": "esbuild src/index.js --bundle --format=esm --platform=neutral --target=es2022 --outfile=dist/worker.js",
"test": "node test/run-local-proof.mjs && node test/range-ignored.test.mjs"
"test": "node test/run-local-proof.mjs && node test/range-ignored.test.mjs && node test/bounded-decompress.test.mjs"
},
"type": "module",
"version": "1.0.0"
Expand Down
Loading
Loading