Skip to content

Commit 8385958

Browse files
anonrigRafaelGSS
authored andcommitted
zlib: add typings for better dx
PR-URL: #54699 Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 323d9da commit 8385958

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

lib/zlib.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,12 @@ ObjectDefineProperty(ZlibBase.prototype, 'bytesRead', {
294294
'This feature will be removed in the future.', 'DEP0108'),
295295
});
296296

297+
/**
298+
* @this ZlibBase
299+
* @returns {void}
300+
*/
297301
ZlibBase.prototype.reset = function() {
298-
if (!this._handle)
299-
assert(false, 'zlib binding closed');
302+
assert(this._handle, 'zlib binding closed');
300303
return this._handle.reset();
301304
};
302305

@@ -358,6 +361,10 @@ ZlibBase.prototype.flush = function(kind, callback) {
358361
}
359362
};
360363

364+
/**
365+
* @this import('stream').Transform
366+
* @param {(err?: Error) => any} [callback]
367+
*/
361368
ZlibBase.prototype.close = function(callback) {
362369
if (callback) finished(this, callback);
363370
this.destroy();
@@ -398,7 +405,7 @@ function processChunkSync(self, chunk, flushFlag) {
398405
let availOutAfter;
399406
let availInAfter;
400407

401-
let buffers = null;
408+
const buffers = [];
402409
let nread = 0;
403410
let inputRead = 0;
404411
const state = self._writeState;
@@ -435,10 +442,7 @@ function processChunkSync(self, chunk, flushFlag) {
435442
if (have > 0) {
436443
const out = buffer.slice(offset, offset + have);
437444
offset += have;
438-
if (!buffers)
439-
buffers = [out];
440-
else
441-
ArrayPrototypePush(buffers, out);
445+
ArrayPrototypePush(buffers, out);
442446
nread += out.byteLength;
443447

444448
if (nread > self._maxOutputLength) {
@@ -589,12 +593,13 @@ function processCallback() {
589593
this.cb();
590594
}
591595

596+
/**
597+
* @param {ZlibBase} engine
598+
* @private
599+
*/
592600
function _close(engine) {
593-
// Caller may invoke .close after a zlib error (which will null _handle).
594-
if (!engine._handle)
595-
return;
596-
597-
engine._handle.close();
601+
// Caller may invoke .close after a zlib error (which will null _handle)
602+
engine._handle?.close();
598603
engine._handle = null;
599604
}
600605

typings/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { UtilBinding } from './internalBinding/util';
1818
import { WASIBinding } from './internalBinding/wasi';
1919
import { WorkerBinding } from './internalBinding/worker';
2020
import { ModulesBinding } from './internalBinding/modules';
21+
import { ZlibBinding } from './internalBinding/zlib';
2122

2223
interface InternalBindingMap {
2324
async_wrap: AsyncWrapBinding;
@@ -40,6 +41,7 @@ interface InternalBindingMap {
4041
util: UtilBinding;
4142
wasi: WASIBinding;
4243
worker: WorkerBinding;
44+
zlib: ZlibBinding;
4345
}
4446

4547
type InternalBindingKeys = keyof InternalBindingMap;

typings/internalBinding/zlib.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { TypedArray } from '../globals';
2+
3+
declare namespace InternalZlibBinding {
4+
class ZlibBase {
5+
// These attributes are not used by the C++ binding, but declared on JS side.
6+
buffer?: TypedArray;
7+
cb?: VoidFunction;
8+
availOutBefore?: number;
9+
availInBefore?: number;
10+
inOff?: number;
11+
flushFlag?: number;
12+
13+
reset(): void;
14+
close(): void;
15+
params(level: number, strategy: number): void;
16+
write(flushFlag: number, input: TypedArray, inputOff: number, inputLen: number, out: TypedArray, outOff: number, outLen: number): void;
17+
writeSync(flushFlag: number, input: TypedArray, inputOff: number, inputLen: number, out: TypedArray, outOff: number, outLen: number): void;
18+
}
19+
20+
class Zlib extends ZlibBase{
21+
constructor(mode: number)
22+
init(windowBits: number, level: number, memLevel: number, strategy: number, writeState: Uint32Array, callback: VoidFunction, dictionary: Uint32Array): number;
23+
}
24+
25+
class BrotliDecoder extends ZlibBase {
26+
constructor(mode: number);
27+
init(initParamsArray: Uint32Array, writeState: Uint32Array, callback: VoidFunction): boolean;
28+
}
29+
30+
class BrotliEncoder extends ZlibBase {
31+
constructor(mode: number);
32+
init(initParamsArray: Uint32Array, writeState: Uint32Array, callback: VoidFunction): boolean;
33+
}
34+
}
35+
36+
export interface ZlibBinding {
37+
BrotliDecoder: typeof InternalZlibBinding.BrotliDecoder;
38+
BrotliEncoder: typeof InternalZlibBinding.BrotliEncoder;
39+
Zlib: typeof InternalZlibBinding.Zlib;
40+
}

0 commit comments

Comments
 (0)