Skip to content

Commit

Permalink
Merge pull request #2645 from cloudflare/yagiz/simplify-zlib-internal
Browse files Browse the repository at this point in the history
simplify zlib internals
  • Loading branch information
anonrig authored Sep 3, 2024
2 parents 6d652db + a7e57af commit b852ab9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 71 deletions.
75 changes: 7 additions & 68 deletions src/node/internal/internal_zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,19 @@ function normalizeArgs(
function wrapCallback(
callback: CompressCallback<Error, Buffer>
): CompressCallback<string, ArrayBuffer> {
return (error: string | undefined, result: ArrayBuffer | undefined) => {
return (error: string | null, result: ArrayBuffer | undefined) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
queueMicrotask(() => {
callback(
error ? new Error(error) : undefined,
result ? Buffer.from(result) : undefined
);
if (error) {
callback(new Error(error));
} else {
// To avoid having a runtime assertion, let's use type assertion.
callback(null, Buffer.from(result as ArrayBuffer));
}
});
};
}

export function inflate(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function inflate(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function inflate(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -135,15 +128,6 @@ export function inflate(
zlibUtil.zlib(data, options, zlibUtil.CONST_INFLATE, wrapCallback(callback));
}

export function unzip(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function unzip(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function unzip(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -156,15 +140,6 @@ export function unzip(
zlibUtil.zlib(data, options, zlibUtil.CONST_UNZIP, wrapCallback(callback));
}

export function inflateRaw(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function inflateRaw(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function inflateRaw(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -182,15 +157,6 @@ export function inflateRaw(
);
}

export function gunzip(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function gunzip(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function gunzip(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -203,15 +169,6 @@ export function gunzip(
zlibUtil.zlib(data, options, zlibUtil.CONST_GUNZIP, wrapCallback(callback));
}

export function deflate(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function deflate(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function deflate(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -224,15 +181,6 @@ export function deflate(
zlibUtil.zlib(data, options, zlibUtil.CONST_DEFLATE, wrapCallback(callback));
}

export function deflateRaw(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function deflateRaw(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function deflateRaw(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand All @@ -250,15 +198,6 @@ export function deflateRaw(
);
}

export function gzip(
data: ArrayBufferView | string,
callback: CompressCallback<Error, Buffer>
): void;
export function gzip(
data: ArrayBufferView | string,
options: ZlibOptions,
callback: CompressCallback<Error, Buffer>
): void;
export function gzip(
data: ArrayBufferView | string,
optionsOrCallback: ZlibOptions | CompressCallback<Error, Buffer>,
Expand Down
4 changes: 2 additions & 2 deletions src/node/internal/internal_zlib_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class ZlibBase extends Transform {
public _defaultFlushFlag: number;
public _finishFlushFlag: number;
public _defaultFullFlushFlag: number;
public _info: unknown;
public _info: boolean;
public _handle: ZlibHandleType | null = null;
public _writeState = new Uint32Array(2);

Expand Down Expand Up @@ -430,7 +430,7 @@ export class ZlibBase extends Transform {
this._defaultFlushFlag = flush;
this._finishFlushFlag = finishFlush;
this._defaultFullFlushFlag = fullFlush;
this._info = opts && opts.info;
this._info = Boolean(opts?.info);
this._maxOutputLength = maxOutputLength;
}

Expand Down
2 changes: 1 addition & 1 deletion src/node/internal/zlib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { owner_symbol, type Zlib } from 'node-internal:internal_zlib_base';
export function crc32(data: ArrayBufferView, value: number): number;

export type CompressCallback<ErrT, BufT> = (
error?: ErrT,
error: ErrT | null,
result?: BufT
) => void;

Expand Down

0 comments on commit b852ab9

Please sign in to comment.