Skip to content

Commit

Permalink
Merge pull request #2646 from cloudflare/yagiz/fix-zlib-flush
Browse files Browse the repository at this point in the history
fix node:zlib flush tests
  • Loading branch information
anonrig authored Sep 3, 2024
2 parents b852ab9 + 109a00c commit 4ba4b48
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
11 changes: 5 additions & 6 deletions src/node/internal/internal_zlib_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ function processCallback(this: ZlibHandleType): void {
handle.availInBefore, // in_len
self._outBuffer, // out
self._outOffset, // out_off
self._chunkSize
); // out_len
self._chunkSize // out_len
);
self._read(n);
};
}
Expand Down Expand Up @@ -470,9 +470,9 @@ export class ZlibBase extends Transform {
kind?: number | (() => void),
callback: (() => void) | undefined = undefined
): void {
if (typeof kind === 'function' || (kind == null && !callback)) {
if (typeof kind === 'function' || (kind === undefined && !callback)) {
callback = kind as (() => void) | undefined;
kind = this._defaultFlushFlag;
kind = this._defaultFullFlushFlag;
}

if (this.writableFinished) {
Expand Down Expand Up @@ -650,7 +650,6 @@ export class Zlib extends ZlibBase {
dictionary
);
super(options ?? {}, mode, handle);
handle[owner_symbol] = this;
this._level = level;
this._strategy = strategy;
this._handle = handle;
Expand Down Expand Up @@ -678,7 +677,7 @@ export class Zlib extends ZlibBase {
);
} else {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-call */
queueMicrotask(() => callback());
queueMicrotask(callback);
}
}

Expand Down
67 changes: 33 additions & 34 deletions src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,40 +1521,39 @@ export const zlibEmptyBuffer = {

// Test taken from:
// https://github.com/nodejs/node/blob/d75e253c506310ea6728329981beb3284fa431b5/test/parallel/test-zlib-flush.js
// TODO(soon): Enable this test once the bug is fixed
// export const zlibFlush = {
// async test() {
// const opts = { level: 0 };
// const deflater = zlib.createDeflate(opts);

// const chunk = Buffer.from('/9j/4AAQSkZJRgABAQEASA==', 'base64');
// const expectedNone = Buffer.from([0x78, 0x01]);
// const blkhdr = Buffer.from([0x00, 0x10, 0x00, 0xef, 0xff]);
// const adler32 = Buffer.from([0x00, 0x00, 0x00, 0xff, 0xff]);
// const expectedFull = Buffer.concat([blkhdr, chunk, adler32]);
// let actualNone;
// let actualFull;

// const { promise, resolve } = Promise.withResolvers();
// deflater.write(chunk, function () {
// deflater.flush(zlib.constants.Z_NO_FLUSH, function () {
// actualNone = deflater.read();
// deflater.flush(function () {
// const bufs = [];
// let buf;
// while ((buf = deflater.read()) !== null) bufs.push(buf);
// actualFull = Buffer.concat(bufs);

// resolve();
// });
// });
// });

// await promise;
// assert.deepStrictEqual(actualNone, expectedNone);
// assert.deepStrictEqual(actualFull, expectedFull);
// },
// };
export const zlibFlush = {
async test() {
const opts = { level: 0 };
const deflater = zlib.createDeflate(opts);

const chunk = Buffer.from('/9j/4AAQSkZJRgABAQEASA==', 'base64');
const expectedNone = Buffer.from([0x78, 0x01]);
const blkhdr = Buffer.from([0x00, 0x10, 0x00, 0xef, 0xff]);
const adler32 = Buffer.from([0x00, 0x00, 0x00, 0xff, 0xff]);
const expectedFull = Buffer.concat([blkhdr, chunk, adler32]);
let actualNone;
let actualFull;

const { promise, resolve } = Promise.withResolvers();
deflater.write(chunk, function () {
deflater.flush(zlib.constants.Z_NO_FLUSH, function () {
actualNone = deflater.read();
deflater.flush(function () {
const bufs = [];
let buf;
while ((buf = deflater.read()) !== null) bufs.push(buf);
actualFull = Buffer.concat(bufs);

resolve();
});
});
});

await promise;
assert.deepStrictEqual(actualNone, expectedNone);
assert.deepStrictEqual(actualFull, expectedFull);
},
};

// Test taken from:
// https://github.com/nodejs/node/blob/9bdf2ee1d184e7ec5c690319e068894ed324b595/test/parallel/test-zlib-dictionary.js
Expand Down

0 comments on commit 4ba4b48

Please sign in to comment.