Skip to content

Commit

Permalink
WIP: zlib one-shot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
npaun committed Aug 22, 2024
1 parent 3114d6d commit 9b128cd
Show file tree
Hide file tree
Showing 6 changed files with 623 additions and 36 deletions.
163 changes: 147 additions & 16 deletions src/node/internal/internal_zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// https://opensource.org/licenses/Apache-2.0
// Copyright Joyent and Node contributors. All rights reserved. MIT license.

import { default as zlibUtil, type ZlibOptions } from 'node-internal:zlib';
import { default as zlibUtil, ZlibOptions } from 'node-internal:zlib';
import { Buffer } from 'node-internal:internal_buffer';
import { validateUint32 } from 'node-internal:validators';
import { ERR_INVALID_ARG_TYPE } from 'node-internal:internal_errors';
import { isArrayBufferView } from 'node-internal:internal_types';
import { Zlib } from 'node-internal:internal_zlib_base';
export const { ZLIB_OS_CODE } = zlibUtil;

type CompressCallback = (error: Error | null, result: Buffer | null) => void;

const {
CONST_DEFLATE,
Expand All @@ -20,6 +21,149 @@ const {
CONST_UNZIP,
} = zlibUtil;

export function crc32(
data: ArrayBufferView | string,
value: number = 0
): number {
validateUint32(value, 'value');
return zlibUtil.crc32(data, value);
}

export function inflateSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(zlibUtil.zlibSync(data, options, zlibUtil.CONST_INFLATE));
}

export function deflateSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(zlibUtil.zlibSync(data, options, zlibUtil.CONST_DEFLATE));
}

export function gunzipSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(zlibUtil.zlibSync(data, options, zlibUtil.CONST_GUNZIP));
}

export function gzipSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(zlibUtil.zlibSync(data, options, zlibUtil.CONST_GZIP));
}

export function inflateRawSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(
zlibUtil.zlibSync(data, options, zlibUtil.CONST_INFLATERAW)
);
}

export function deflateRawSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
return Buffer.from(
zlibUtil.zlibSync(data, options, zlibUtil.CONST_DEFLATERAW)
);
}

export function unzipSync(
data: ArrayBufferView | string,
options: ZlibOptions = {}
): Buffer {
data;
options;
return Buffer.alloc(0);
// FIXME: not actually implemented
}

export function inflate(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(data, options, zlibUtil.CONST_INFLATE, (error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

export function unzip(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
data;
options;
callback;
// FIXME: not actually implemented
}

export function inflateRaw(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(
data,
options,
zlibUtil.CONST_INFLATERAW,
(error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

export function gunzip(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(data, options, zlibUtil.CONST_GUNZIP, (error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

export function deflate(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(data, options, zlibUtil.CONST_DEFLATE, (error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

export function deflateRaw(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(
data,
options,
zlibUtil.CONST_DEFLATERAW,
(error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

export function gzip(
data: ArrayBufferView | string,
options: ZlibOptions = {},
callback: CompressCallback
): void {
zlibUtil.zlibWithCb(data, options, zlibUtil.CONST_GZIP, (error, result) =>
callback(error ? Error(error) : null, result ? Buffer.from(result) : null)
);
}

const constPrefix = 'CONST_';
export const constants: Record<string, number> = {};

Expand All @@ -41,19 +185,6 @@ Object.defineProperties(
)
);

export function crc32(
data: ArrayBufferView | string,
value: number = 0
): number {
if (typeof data === 'string') {
data = Buffer.from(data);
} else if (!isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE('data', 'ArrayBufferView', typeof data);
}
validateUint32(value, 'value');
return zlibUtil.crc32(data, value);
}

export class Gzip extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_GZIP);
Expand Down
20 changes: 20 additions & 0 deletions src/node/internal/zlib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import { owner_symbol, type Zlib } from 'node-internal:internal_zlib_base';

export function crc32(data: ArrayBufferView, value: number): number;

type CompressCallback = (
error: string | null,
result: ArrayBuffer | null
) => void;

export function crc32(data: ArrayBufferView | string, value: number): number;
export function zlibSync(
data: ArrayBufferView | string,
options: ZlibOptions,
mode: number
): ArrayBuffer;
export function zlibWithCb(
data: ArrayBufferView | string,
options: ZlibOptions,
mode: number,
cb: CompressCallback
): ArrayBuffer;

// zlib.constants (part of the API contract for node:zlib)
export const CONST_Z_NO_FLUSH: number;
export const CONST_Z_PARTIAL_FLUSH: number;
Expand Down Expand Up @@ -54,6 +72,8 @@ export const CONST_Z_MIN_LEVEL: number;
export const CONST_Z_MAX_LEVEL: number;
export const CONST_Z_DEFAULT_LEVEL: number;

export const ZLIB_OS_CODE: number;

export const CONST_BROTLI_OPERATION_PROCESS: number;
export const CONST_BROTLI_OPERATION_FLUSH: number;
export const CONST_BROTLI_OPERATION_FINISH: number;
Expand Down
55 changes: 54 additions & 1 deletion src/node/zlib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as zlib from 'node-internal:internal_zlib';
import { crc32, constants } from 'node-internal:internal_zlib';
import { crc32, constants, ZLIB_OS_CODE } from 'node-internal:internal_zlib';
import { default as compatFlags } from 'workerd:compatibility-flags';

const { nodeJsZlib } = compatFlags;
Expand Down Expand Up @@ -29,6 +29,24 @@ const createInflate = protectMethod(zlib.createInflate);
const createInflateRaw = protectMethod(zlib.createInflateRaw);
const createUnzip = protectMethod(zlib.createUnzip);

const inflate = protectMethod(zlib.inflate);
const inflateSync = protectMethod(zlib.inflateSync);
const deflate = protectMethod(zlib.deflate);
const deflateSync = protectMethod(zlib.deflateSync);

const inflateRaw = protectMethod(zlib.inflateRaw);
const inflateRawSync = protectMethod(zlib.inflateRawSync);
const deflateRaw = protectMethod(zlib.deflateRaw);
const deflateRawSync = protectMethod(zlib.deflateRawSync);

const gzip = protectMethod(zlib.gzip);
const gzipSync = protectMethod(zlib.gzipSync);
const gunzip = protectMethod(zlib.gunzip);
const gunzipSync = protectMethod(zlib.gunzipSync);

const unzip = protectMethod(zlib.unzip);
const unzipSync = protectMethod(zlib.unzipSync);

export {
crc32,
constants,
Expand All @@ -50,6 +68,22 @@ export {
createInflate,
createInflateRaw,
createUnzip,

// One-shot methods
inflate,
inflateSync,
deflate,
deflateSync,
inflateRaw,
inflateRawSync,
deflateRaw,
deflateRawSync,
gzip,
gzipSync,
gunzip,
gunzipSync,
unzip,
unzipSync,
};

export default {
Expand All @@ -73,4 +107,23 @@ export default {
createInflate,
createInflateRaw,
createUnzip,

// One-shot methods
inflate,
inflateSync,
deflate,
deflateSync,
inflateRaw,
inflateRawSync,
deflateRaw,
deflateRawSync,
gzip,
gzipSync,
gunzip,
gunzipSync,
unzip,
unzipSync,

// For unit tests only
ZLIB_OS_CODE,
};
Loading

0 comments on commit 9b128cd

Please sign in to comment.