Skip to content

Commit

Permalink
add several node:zlib classes
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 20, 2024
1 parent 249464f commit 03d8834
Show file tree
Hide file tree
Showing 11 changed files with 1,812 additions and 61 deletions.
4 changes: 3 additions & 1 deletion src/node/internal/internal_assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ function assert(actual: unknown, message?: string | Error): asserts actual {
} as AssertionErrorConstructorOptions );
}
}
export const ok = assert;

type Assert = (actual: unknown, message?: string | Error) => asserts actual;
export const ok: Assert = assert;

export function throws(
fn: () => void,
Expand Down
6 changes: 6 additions & 0 deletions src/node/internal/internal_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ export class ERR_STREAM_UNSHIFT_AFTER_END_EVENT extends NodeError {
}
}

export class ERR_BUFFER_TOO_LARGE extends NodeRangeError {
constructor(value: number) {
super("ERR_BUFFER_TOO_LARGE", `Cannot create a Buffer larger than ${value} bytes`);
}
}

export function aggregateTwoErrors(innerError: any, outerError: any) {
if (innerError && outerError && innerError !== outerError) {
if (Array.isArray(outerError.errors)) {
Expand Down
117 changes: 100 additions & 17 deletions src/node/internal/internal_zlib.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { default as zlibUtil } from 'node-internal:zlib';
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// 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 { Buffer } from 'node-internal:internal_buffer';
import { validateUint32 } from 'node-internal:validators';
import { isArrayBufferView } from 'node-internal:internal_types';
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';

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);
}

const {
CONST_DEFLATE,
CONST_DEFLATERAW,
CONST_INFLATE,
CONST_INFLATERAW,
CONST_GUNZIP,
CONST_GZIP,
CONST_UNZIP,
} = zlibUtil;

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

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.defineProperties(constants, Object.fromEntries(Object.entries(Object.getPrototypeOf(zlibUtil))
Expand All @@ -29,7 +34,85 @@ Object.defineProperties(constants, Object.fromEntries(Object.entries(Object.getP
}])
));

export {
crc32,
constants,
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);
}
}

export class Gunzip extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_GUNZIP);
}
}

export class Deflate extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_DEFLATE);
}
}

export class DeflateRaw extends Zlib {
public constructor(options?: ZlibOptions) {
if (options?.windowBits === 8) {
options.windowBits = 9
}
super(options, CONST_DEFLATERAW);
}
}

export class Inflate extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_INFLATE);
}
}

export class InflateRaw extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_INFLATERAW);
}
}

export class Unzip extends Zlib {
public constructor(options: ZlibOptions) {
super(options, CONST_UNZIP);
}
}

export function createGzip(options: ZlibOptions): Gzip {
return new Gzip(options);
}

export function createGunzip(options: ZlibOptions): Gunzip {
return new Gunzip(options);
}

export function createDeflate(options: ZlibOptions): Deflate {
return new Deflate(options);
}

export function createDeflateRaw(options: ZlibOptions): DeflateRaw {
return new DeflateRaw(options);
}

export function createInflate(options: ZlibOptions): Inflate {
return new Inflate(options);
}

export function createInflateRaw(options: ZlibOptions): InflateRaw {
return new InflateRaw(options);
}

export function createUnzip(options: ZlibOptions): Unzip {
return new Unzip(options);
}
Loading

0 comments on commit 03d8834

Please sign in to comment.