GeezerVison is showing github.com — zlib: add typings for better dx · nodejs/node@8385958
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/zlib.js +17-12Lines changed: 17 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -294,9 +294,12 @@ ObjectDefineProperty(ZlibBase.prototype, 'bytesRead', { | 294 | 294 | 'This feature will be removed in the future.', 'DEP0108'), | 295 | 295 | }); | 296 | 296 |
| | 297 | +/** | | 298 | + * @this ZlibBase | | 299 | + * @returns {void} | | 300 | + */ | 297 | 301 | ZlibBase.prototype.reset = function() { | 298 | | - if (!this._handle) | 299 | | - assert(false, 'zlib binding closed'); | | 302 | + assert(this._handle, 'zlib binding closed'); | 300 | 303 | return this._handle.reset(); | 301 | 304 | }; | 302 | 305 |
| @@ -358,6 +361,10 @@ ZlibBase.prototype.flush = function(kind, callback) { | 358 | 361 | } | 359 | 362 | }; | 360 | 363 |
| | 364 | +/** | | 365 | + * @this import('stream').Transform | | 366 | + * @param {(err?: Error) => any} [callback] | | 367 | + */ | 361 | 368 | ZlibBase.prototype.close = function(callback) { | 362 | 369 | if (callback) finished(this, callback); | 363 | 370 | this.destroy(); | @@ -398,7 +405,7 @@ function processChunkSync(self, chunk, flushFlag) { | 398 | 405 | let availOutAfter; | 399 | 406 | let availInAfter; | 400 | 407 |
| 401 | | - let buffers = null; | | 408 | + const buffers = []; | 402 | 409 | let nread = 0; | 403 | 410 | let inputRead = 0; | 404 | 411 | const state = self._writeState; | @@ -435,10 +442,7 @@ function processChunkSync(self, chunk, flushFlag) { | 435 | 442 | if (have > 0) { | 436 | 443 | const out = buffer.slice(offset, offset + have); | 437 | 444 | offset += have; | 438 | | - if (!buffers) | 439 | | - buffers = [out]; | 440 | | - else | 441 | | - ArrayPrototypePush(buffers, out); | | 445 | + ArrayPrototypePush(buffers, out); | 442 | 446 | nread += out.byteLength; | 443 | 447 |
| 444 | 448 | if (nread > self._maxOutputLength) { | @@ -589,12 +593,13 @@ function processCallback() { | 589 | 593 | this.cb(); | 590 | 594 | } | 591 | 595 |
| | 596 | +/** | | 597 | + * @param {ZlibBase} engine | | 598 | + * @private | | 599 | + */ | 592 | 600 | 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(); | 598 | 603 | engine._handle = null; | 599 | 604 | } | 600 | 605 |
| |
Collapse file Copy file name to clipboardExpand all lines: typings/globals.d.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import { UtilBinding } from './internalBinding/util'; | 18 | 18 | import { WASIBinding } from './internalBinding/wasi'; | 19 | 19 | import { WorkerBinding } from './internalBinding/worker'; | 20 | 20 | import { ModulesBinding } from './internalBinding/modules'; | | 21 | +import { ZlibBinding } from './internalBinding/zlib'; | 21 | 22 |
| 22 | 23 | interface InternalBindingMap { | 23 | 24 | async_wrap: AsyncWrapBinding; | @@ -40,6 +41,7 @@ interface InternalBindingMap { | 40 | 41 | util: UtilBinding; | 41 | 42 | wasi: WASIBinding; | 42 | 43 | worker: WorkerBinding; | | 44 | + zlib: ZlibBinding; | 43 | 45 | } | 44 | 46 |
| 45 | 47 | type InternalBindingKeys = keyof InternalBindingMap; | |
Collapse file Copy file name to clipboard +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|