Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: add typings for better dx #54699

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 17 additions & 12 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,12 @@ ObjectDefineProperty(ZlibBase.prototype, 'bytesRead', {
'This feature will be removed in the future.', 'DEP0108'),
});

/**
* @this ZlibBase
* @returns {void}
*/
ZlibBase.prototype.reset = function() {
if (!this._handle)
assert(false, 'zlib binding closed');
assert(this._handle, 'zlib binding closed');
return this._handle.reset();
};

Expand Down Expand Up @@ -358,6 +361,10 @@ ZlibBase.prototype.flush = function(kind, callback) {
}
};

/**
* @this import('stream').Transform
* @param {(err?: Error) => any} [callback]
*/
ZlibBase.prototype.close = function(callback) {
if (callback) finished(this, callback);
this.destroy();
Expand Down Expand Up @@ -398,7 +405,7 @@ function processChunkSync(self, chunk, flushFlag) {
let availOutAfter;
let availInAfter;

let buffers = null;
const buffers = [];
let nread = 0;
let inputRead = 0;
const state = self._writeState;
Expand Down Expand Up @@ -435,10 +442,7 @@ function processChunkSync(self, chunk, flushFlag) {
if (have > 0) {
const out = buffer.slice(offset, offset + have);
offset += have;
if (!buffers)
buffers = [out];
else
ArrayPrototypePush(buffers, out);
ArrayPrototypePush(buffers, out);
nread += out.byteLength;

if (nread > self._maxOutputLength) {
Expand Down Expand Up @@ -589,12 +593,13 @@ function processCallback() {
this.cb();
}

/**
* @param {ZlibBase} engine
* @private
*/
function _close(engine) {
// Caller may invoke .close after a zlib error (which will null _handle).
if (!engine._handle)
return;

engine._handle.close();
// Caller may invoke .close after a zlib error (which will null _handle)
engine._handle?.close();
engine._handle = null;
}

Expand Down
2 changes: 2 additions & 0 deletions typings/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { UtilBinding } from './internalBinding/util';
import { WASIBinding } from './internalBinding/wasi';
import { WorkerBinding } from './internalBinding/worker';
import { ModulesBinding } from './internalBinding/modules';
import { ZlibBinding } from './internalBinding/zlib';

interface InternalBindingMap {
async_wrap: AsyncWrapBinding;
Expand All @@ -40,6 +41,7 @@ interface InternalBindingMap {
util: UtilBinding;
wasi: WASIBinding;
worker: WorkerBinding;
zlib: ZlibBinding;
}

type InternalBindingKeys = keyof InternalBindingMap;
Expand Down
40 changes: 40 additions & 0 deletions typings/internalBinding/zlib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { TypedArray } from '../globals';

declare namespace InternalZlibBinding {
class ZlibBase {
// These attributes are not used by the C++ binding, but declared on JS side.
buffer?: TypedArray;
cb?: VoidFunction;
availOutBefore?: number;
availInBefore?: number;
inOff?: number;
flushFlag?: number;

reset(): void;
close(): void;
params(level: number, strategy: number): void;
write(flushFlag: number, input: TypedArray, inputOff: number, inputLen: number, out: TypedArray, outOff: number, outLen: number): void;
writeSync(flushFlag: number, input: TypedArray, inputOff: number, inputLen: number, out: TypedArray, outOff: number, outLen: number): void;
}

class Zlib extends ZlibBase{
constructor(mode: number)
init(windowBits: number, level: number, memLevel: number, strategy: number, writeState: Uint32Array, callback: VoidFunction, dictionary: Uint32Array): number;
}

class BrotliDecoder extends ZlibBase {
constructor(mode: number);
init(initParamsArray: Uint32Array, writeState: Uint32Array, callback: VoidFunction): boolean;
}

class BrotliEncoder extends ZlibBase {
constructor(mode: number);
init(initParamsArray: Uint32Array, writeState: Uint32Array, callback: VoidFunction): boolean;
}
}

export interface ZlibBinding {
BrotliDecoder: typeof InternalZlibBinding.BrotliDecoder;
BrotliEncoder: typeof InternalZlibBinding.BrotliEncoder;
Zlib: typeof InternalZlibBinding.Zlib;
}
Loading