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
11 changes: 7 additions & 4 deletions src/workerd/api/compression.c++
Original file line number Diff line number Diff line change
Expand Up @@ -801,14 +801,17 @@ void ZstdDecoderContext::work() {
JSG_REQUIRE(mode == ZlibMode::ZSTD_DECODE, Error, "Mode should be ZSTD_DECODE"_kj);
JSG_REQUIRE(dctx_.get() != nullptr, Error, "Zstd context should not be null"_kj);

auto inputPosBefore = input_.pos;
lastResult = ZSTD_decompressStream(dctx_.get(), &output_, &input_);

if (ZSTD_isError(lastResult)) {
error_ = ZSTD_getErrorCode(lastResult);
} else if (input_.size > 0) {
// Track whether we're mid-frame: lastResult > 0 means more data needed,
// lastResult == 0 means frame is complete.
frameInProgress_ = (lastResult > 0);
} else if (lastResult == 0) {
frameInProgress_ = false;
} else if (input_.pos > inputPosBefore) {
// A call that consumes no input only drains buffered output, or, after a frame has ended,
// returns the header size hint for a frame that hasn't started. Neither starts a frame.
frameInProgress_ = true;
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/workerd/api/node/tests/zlib-zstd-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,28 @@ export const zstdCompressSyncMaxOutputLengthAtBufferBoundaryTest = {
);
},
};

// The sync output buffer starts at 40 KiB and doubles, so 1x, 2x and 4x fill it exactly.
export const zstdDecompressSyncOutputFillsBufferTest = {
async test() {
const syncFirstBufferSize = 40 * 1024;
for (const multiple of [1, 2, 3, 4]) {
for (const delta of [-1, 0, 1]) {
const size = multiple * syncFirstBufferSize + delta;
const input = xorshiftBytes(size);
const compressed = zlib.zstdCompressSync(input);
assert(
input.equals(zlib.zstdDecompressSync(compressed)),
`size ${size}`
);

const { promise, resolve, reject } = Promise.withResolvers();
zlib.zstdDecompress(compressed, (err, res) => {
if (err) reject(err);
else resolve(res);
});
assert(input.equals(await promise), `size ${size}`);
}
}
},
};
Loading