Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): implement zlib #377

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elide/runtime/js/modules/buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function isAscii(buffer: Buffer | Uint8Array): boolean {
* For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed
* using Buffer.from(str, 'base64') and buf.toString('base64').
*/
export const btoa = node_buffer.btoa;
export const btoa = globalThis['btoa'];

/**
* Decodes a string of Base64-encoded data into bytes, and encodes those bytes into a string using Latin-1 (ISO-8859-1).
Expand All @@ -140,7 +140,7 @@ export const btoa = node_buffer.btoa;
* code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed
* using Buffer.from(str, 'base64') and buf.toString('base64').
*/
export const atob = node_buffer.atob;
export const atob = globalThis['atob'];

// `Buffer` is assigned as the default export.
export default Buffer;
2 changes: 1 addition & 1 deletion elide/runtime/js/modules/os/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function intrinsic(): any {
/**
* Resolves operating system constants for the current platform.
*/
export const constants = {};
export const constants = intrinsic().constants;

/**
* Returns the operating system's end-of-line value.
Expand Down
281 changes: 280 additions & 1 deletion elide/runtime/js/modules/zlib/zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,283 @@
* Provides access to the Zlib compression and decompression library.
*/

export {};
const { node_zlib } = primordials;
if (!node_zlib) {
throw new Error(`The 'zlib' module failed to load the intrinsic API.`);
}

function intrinsic(): any {
return node_zlib;
}

/**
* Provides constants defined by the `zlib` module.
*/
export const constants = intrinsic().constants;

/**
* Calculates a CRC32 checksum for the given data.
*
* @param data The data to checksum
* @param value The initial value for the checksum (optional)
* @returns The CRC32 checksum for the given data
*/
export function crc32(data: string | Buffer | DataView | any, value?: number): number {
return intrinsic().crc32(data, value);
}

export type Deflate = any;

export type Inflate = any;

export type BrotliCompress = any;

export type BrotliDecompress = any;

export type BufferLike = any;

/**
* Creates a writable stream which compresses data using the DEFLATE algorithm.
*
* @param options Zlib options to apply to this stream. Optional.
* @returns Deflation stream
*/
export function createDeflate(options?: any): Deflate {
return intrinsic().createDeflate(options);
}

/**
* Creates a readable stream which decompresses data using the DEFLATE algorithm.
*
* @param options Zlib options to apply to this stream. Optional.
* @returns Inflation stream
*/
export function createInflate(options?: any): Inflate {
return intrinsic().createInflate(options);
}

/**
* Creates a readable stream which decompresses data using the zip algorithm.
*
* @param options Zlib options to apply to this stream. Optional.
* @returns Inflation stream
*/
export function createUnzip(options?: any): Inflate {
return intrinsic().createUnzip(options);
}

/**
* Creates a writable stream which compresses data using the Brotli algorithm.
*
* @param options Brotli options to apply to this stream. Optional.
* @returns Deflation stream
*/
export function createBrotliCompress(options?: any): Deflate {
return intrinsic().createBrotliCompress(options);
}

/**
* Creates a readable stream which decompresses data using the Brotli algorithm.
*
* @param options Brotli options to apply to this stream. Optional.
* @returns Inflation stream
*/
export function createBrotliDecompress(options?: any): Inflate {
return intrinsic().createBrotliDecompress(options);
}

/**
* Asynchronously compresses the given data using the DEFLATE algorithm.
*
* @param data The data to compress
* @param options Zlib options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function deflate(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().deflate(data, opts, callback);
}

/**
* Synchronously compresses the given data using the DEFLATE algorithm.
*
* @param data The data to compress
* @param options Zlib options to apply to this compression. Optional.
* @returns The compressed data
*/
export function deflateSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().deflateSync(data, options);
}

/**
* Asynchronously decompresses the given data using the DEFLATE algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function inflate(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().inflate(data, opts, callback);
}

/**
* Synchronously decompresses the given data using the DEFLATE algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this decompression. Optional.
* @returns The decompressed data
*/
export function inflateSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().inflateSync(data, options);
}

/**
* Asynchronously compresses the given data using the gzip algorithm.
*
* @param data The data to compress
* @param options Zlib options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function gzip(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().gzip(data, opts, callback);
}

/**
* Synchronously compresses the given data using the gzip algorithm.
*
* @param data The data to compress
* @param options Zlib options to apply to this compression. Optional.
* @returns The compressed data
*/
export function gzipSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().gzipSync(data, options);
}

/**
* Asynchronously decompresses the given data using the gzip algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function gunzip(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().gunzip(data, opts, callback);
}

/**
* Synchronously decompresses the given data using the gzip algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this decompression. Optional.
* @returns The decompressed data
*/
export function gunzipSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().gunzipSync(data, options);
}

/**
* Asynchronously decompresses the given data using the zip algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function unzip(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().unzip(data, opts, callback);
}

/**
* Synchronously decompresses the given data using the zip algorithm.
*
* @param data The data to decompress
* @param options Zlib options to apply to this decompression. Optional.
* @returns The decompressed data
*/
export function unzipSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().unzipSync(data, options);
}

/**
* Asynchronously compresses the given data using the Brotli algorithm.
*
* @param data The data to compress
* @param options Brotli options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function brotliCompress(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().brotliCompress(data, opts, callback);
}

/**
* Synchronously compresses the given data using the Brotli algorithm.
*
* @param data The data to compress
* @param options Brotli options to apply to this compression. Optional.
* @returns The compressed data
*/
export function brotliCompressSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().brotliCompressSync(data, options);
}

/**
* Asynchronously decompresses the given data using the Brotli algorithm.
*
* @param data The data to decompress
* @param options Brotli options to apply to this compression. Optional.
* @param cbk Callback to invoke when the operation completes, and which receives the resulting data.
*/
export function brotliDecompress(
data: string | Buffer | DataView | any,
optionsOrCbk?: any,
cbk?: (errOrResult: Error | BufferLike) => void,
): Buffer {
const callback = cbk || optionsOrCbk;
const opts = cbk ? optionsOrCbk : undefined;
return intrinsic().brotliDecompress(data, opts, callback);
}

/**
* Synchronously decompresses the given data using the Brotli algorithm.
*
* @param data The data to decompress
* @param options Brotli options to apply to this decompression. Optional.
* @returns The decompressed data
*/
export function brotliDecompressSync(data: string | Buffer | DataView | any, options?: any): Buffer {
return intrinsic().brotliDecompressSync(data, options);
}