Skip to content

Commit

Permalink
feat(node): add missing Brotli APIs to the zlib module
Browse files Browse the repository at this point in the history
Summary:
https://nodejs.org/api/zlib.html

Also, narrow the type of the second argument to the `asyncFn`'s
callbacks from `any` to `Buffer`, and expand the type of the first
argument to all functions to include more `Buffer`-like types, as
specified in the node documentation.

<!--
  If this is a change to library defintions, please include links to relevant documentation.
  If this is a documentation change, please prefix the title with [DOCS].

  If this is neither, ensure you opened a discussion issue and link it in the PR description.
-->

Pull Request resolved: #8463

Reviewed By: nmote

Differential Revision: D26605398

Pulled By: jobrk

fbshipit-source-id: b13eb4c1cbcab601743bf84177904508f3dd21ef
  • Loading branch information
isker authored and facebook-github-bot committed Mar 9, 2021
1 parent 98b5329 commit 8fc3a08
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 36 deletions.
101 changes: 98 additions & 3 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2402,15 +2402,38 @@ type zlib$options = {
...
};

type zlib$brotliOptions = {
flush?: number,
finishFlush?: number,
chunkSize?: number,
params?: {
[number]: boolean | number,
...
},
maxOutputLength?: number,
...
}

type zlib$syncFn = (
buffer: string | Buffer,
buffer: Buffer | $TypedArray | DataView | ArrayBuffer | string,
options?: zlib$options
) => Buffer;

type zlib$asyncFn = (
buffer: string | Buffer,
buffer: Buffer | $TypedArray | DataView | ArrayBuffer | string,
options?: zlib$options,
callback?: ((error: ?Error, result: any) => void)
callback?: (error: ?Error, result: Buffer) => void
) => void;

type zlib$brotliSyncFn = (
buffer: Buffer | $TypedArray | DataView | ArrayBuffer | string,
options?: zlib$brotliOptions
) => Buffer;

type zlib$brotliAsyncFn = (
buffer: Buffer | $TypedArray | DataView | ArrayBuffer | string,
options?: zlib$brotliOptions,
callback?: (error: ?Error, result: Buffer) => void
) => void;

// Accessing the constants directly from the module is currently still
Expand Down Expand Up @@ -2504,6 +2527,70 @@ declare module "zlib" {
Z_MIN_LEVEL: number,
Z_MIN_MEMLEVEL: number,
Z_MIN_WINDOWBITS: number,

BROTLI_DECODE: number,
BROTLI_ENCODE: number,
BROTLI_OPERATION_PROCESS: number,
BROTLI_OPERATION_FLUSH: number,
BROTLI_OPERATION_FINISH: number,
BROTLI_OPERATION_EMIT_METADATA: number,
BROTLI_PARAM_MODE: number,
BROTLI_MODE_GENERIC: number,
BROTLI_MODE_TEXT: number,
BROTLI_MODE_FONT: number,
BROTLI_DEFAULT_MODE: number,
BROTLI_PARAM_QUALITY: number,
BROTLI_MIN_QUALITY: number,
BROTLI_MAX_QUALITY: number,
BROTLI_DEFAULT_QUALITY: number,
BROTLI_PARAM_LGWIN: number,
BROTLI_MIN_WINDOW_BITS: number,
BROTLI_MAX_WINDOW_BITS: number,
BROTLI_LARGE_MAX_WINDOW_BITS: number,
BROTLI_DEFAULT_WINDOW: number,
BROTLI_PARAM_LGBLOCK: number,
BROTLI_MIN_INPUT_BLOCK_BITS: number,
BROTLI_MAX_INPUT_BLOCK_BITS: number,
BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING: number,
BROTLI_PARAM_SIZE_HINT: number,
BROTLI_PARAM_LARGE_WINDOW: number,
BROTLI_PARAM_NPOSTFIX: number,
BROTLI_PARAM_NDIRECT: number,
BROTLI_DECODER_RESULT_ERROR: number,
BROTLI_DECODER_RESULT_SUCCESS: number,
BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: number,
BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: number,
BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: number,
BROTLI_DECODER_PARAM_LARGE_WINDOW: number,
BROTLI_DECODER_NO_ERROR: number,
BROTLI_DECODER_SUCCESS: number,
BROTLI_DECODER_NEEDS_MORE_INPUT: number,
BROTLI_DECODER_NEEDS_MORE_OUTPUT: number,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE: number,
BROTLI_DECODER_ERROR_FORMAT_RESERVED: number,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE: number,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET: number,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME: number,
BROTLI_DECODER_ERROR_FORMAT_CL_SPACE: number,
BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE: number,
BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT: number,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1: number,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2: number,
BROTLI_DECODER_ERROR_FORMAT_TRANSFORM: number,
BROTLI_DECODER_ERROR_FORMAT_DICTIONARY: number,
BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS: number,
BROTLI_DECODER_ERROR_FORMAT_PADDING_1: number,
BROTLI_DECODER_ERROR_FORMAT_PADDING_2: number,
BROTLI_DECODER_ERROR_FORMAT_DISTANCE: number,
BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET: number,
BROTLI_DECODER_ERROR_INVALID_ARGUMENTS: number,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES: number,
BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS: number,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP: number,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1: number,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2: number,
BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES: number,
BROTLI_DECODER_ERROR_UNREACHABL: number,
...
};
declare var codes: {
Expand All @@ -2521,20 +2608,28 @@ declare module "zlib" {
declare class Zlib extends stream$Duplex {
// TODO
}
declare class BrotliCompress extends Zlib {}
declare class BrotliDecompress extends Zlib {}
declare class Deflate extends Zlib {}
declare class Inflate extends Zlib {}
declare class Gzip extends Zlib {}
declare class Gunzip extends Zlib {}
declare class DeflateRaw extends Zlib {}
declare class InflateRaw extends Zlib {}
declare class Unzip extends Zlib {}
declare function createBrotliCompress(options?: zlib$brotliOptions): BrotliCompress;
declare function createBrotliDecompress(options?: zlib$brotliOptions): BrotliDecompress;
declare function createDeflate(options?: zlib$options): Deflate;
declare function createInflate(options?: zlib$options): Inflate;
declare function createDeflateRaw(options?: zlib$options): DeflateRaw;
declare function createInflateRaw(options?: zlib$options): InflateRaw;
declare function createGzip(options?: zlib$options): Gzip;
declare function createGunzip(options?: zlib$options): Gunzip;
declare function createUnzip(options?: zlib$options): Unzip;
declare var brotliCompress: zlib$brotliAsyncFn;
declare var brotliCompressSync: zlib$brotliSyncFn;
declare var brotliDeompress: zlib$brotliAsyncFn;
declare var brotliDecompressSync: zlib$brotliSyncFn;
declare var deflate: zlib$asyncFn;
declare var deflateSync: zlib$syncFn;
declare var gzip: zlib$asyncFn;
Expand Down
66 changes: 33 additions & 33 deletions tests/node_tests/node_tests.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,26 +1636,26 @@ References:
process/emitWarning.js:10:1
10| process.emitWarning(); // error
^^^^^^^^^^^^^^^^^^^^^ [1]
<BUILTINS>/node.js:2676:24
2676| emitWarning(warning: string | Error): void;
<BUILTINS>/node.js:2771:24
2771| emitWarning(warning: string | Error): void;
^^^^^^ [2]
<BUILTINS>/node.js:2676:33
2676| emitWarning(warning: string | Error): void;
<BUILTINS>/node.js:2771:33
2771| emitWarning(warning: string | Error): void;
^^^^^ [3]
<BUILTINS>/node.js:2677:3
2677| emitWarning(warning: string, typeOrCtor: string | (...empty) => mixed): void;
<BUILTINS>/node.js:2772:3
2772| emitWarning(warning: string, typeOrCtor: string | (...empty) => mixed): void;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [4]
<BUILTINS>/node.js:2678:3
2678| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
<BUILTINS>/node.js:2773:3
2773| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [5]
<BUILTINS>/node.js:2679:3
<BUILTINS>/node.js:2774:3
v-----------
2679| emitWarning(
2680| warning: string,
2681| type: string,
2682| code: string,
2683| ctor?: (...empty) => mixed
2684| ): void;
2774| emitWarning(
2775| warning: string,
2776| type: string,
2777| code: string,
2778| ctor?: (...empty) => mixed
2779| ): void;
------^ [6]


Expand All @@ -1674,14 +1674,14 @@ References:
process/emitWarning.js:11:21
11| process.emitWarning(42); // error
^^ [1]
<BUILTINS>/node.js:2677:24
2677| emitWarning(warning: string, typeOrCtor: string | (...empty) => mixed): void;
<BUILTINS>/node.js:2772:24
2772| emitWarning(warning: string, typeOrCtor: string | (...empty) => mixed): void;
^^^^^^ [2]
<BUILTINS>/node.js:2678:24
2678| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
<BUILTINS>/node.js:2773:24
2773| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
^^^^^^ [3]
<BUILTINS>/node.js:2680:14
2680| warning: string,
<BUILTINS>/node.js:2775:14
2775| warning: string,
^^^^^^ [4]


Expand All @@ -1699,11 +1699,11 @@ References:
process/emitWarning.js:12:29
12| process.emitWarning("blah", 42); // error
^^ [1]
<BUILTINS>/node.js:2678:38
2678| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
<BUILTINS>/node.js:2773:38
2773| emitWarning(warning: string, type: string, codeOrCtor: string | (...empty) => mixed): void;
^^^^^^ [2]
<BUILTINS>/node.js:2681:11
2681| type: string,
<BUILTINS>/node.js:2776:11
2776| type: string,
^^^^^^ [3]


Expand All @@ -1719,8 +1719,8 @@ References:
process/emitWarning.js:13:37
13| process.emitWarning("blah", "blah", 42); // error
^^ [1]
<BUILTINS>/node.js:2682:11
2682| code: string,
<BUILTINS>/node.js:2777:11
2777| code: string,
^^^^^^ [2]


Expand All @@ -1734,8 +1734,8 @@ Cannot cast `process.emitWarning(...)` to string because undefined [1] is incomp
^^^^^^^^^^^^^^^^^^^^^^^^^^^

References:
<BUILTINS>/node.js:2676:41
2676| emitWarning(warning: string | Error): void;
<BUILTINS>/node.js:2771:41
2771| emitWarning(warning: string | Error): void;
^^^^ [1]
process/emitWarning.js:14:31
14| (process.emitWarning("blah"): string); // error
Expand Down Expand Up @@ -1800,8 +1800,8 @@ References:
process/nextTick.js:27:3
27| (a: string, b: number, c: boolean) => {} // Error: too few arguments
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]
<BUILTINS>/node.js:2705:21
2705| nextTick: <T>(cb: (...T) => mixed, ...T) => void;
<BUILTINS>/node.js:2800:21
2800| nextTick: <T>(cb: (...T) => mixed, ...T) => void;
^^^^^^^^^^^^^^^ [2]


Expand All @@ -1815,8 +1815,8 @@ Cannot cast `process.allowedNodeEnvironmentFlags` to string because `Set` [1] is
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

References:
<BUILTINS>/node.js:2665:32
2665| allowedNodeEnvironmentFlags: Set<string>;
<BUILTINS>/node.js:2760:32
2760| allowedNodeEnvironmentFlags: Set<string>;
^^^^^^^^^^^ [1]
process/process.js:5:39
5| (process.allowedNodeEnvironmentFlags: string); // error
Expand Down

0 comments on commit 8fc3a08

Please sign in to comment.