2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
// https://github.com/nodeca/pako/blob/master/lib/zlib/constants.js
|
|
|
|
export const Z_NO_FLUSH = 0;
|
|
|
|
export const Z_PARTIAL_FLUSH = 1;
|
|
|
|
export const Z_SYNC_FLUSH = 2;
|
|
|
|
export const Z_FULL_FLUSH = 3;
|
|
|
|
export const Z_FINISH = 4;
|
|
|
|
export const Z_BLOCK = 5;
|
|
|
|
export const Z_TREES = 6;
|
|
|
|
export const Z_OK = 0;
|
|
|
|
export const Z_STREAM_END = 1;
|
|
|
|
export const Z_NEED_DICT = 2;
|
|
|
|
export const Z_ERRNO = -1;
|
|
|
|
export const Z_STREAM_ERROR = -2;
|
|
|
|
export const Z_DATA_ERROR = -3;
|
|
|
|
export const Z_MEM_ERROR = -4;
|
|
|
|
export const Z_BUF_ERROR = -5;
|
|
|
|
export const Z_VERSION_ERROR = -6;
|
|
|
|
export const Z_NO_COMPRESSION = 0;
|
|
|
|
export const Z_BEST_SPEED = 1;
|
|
|
|
export const Z_BEST_COMPRESSION = 9;
|
|
|
|
export const Z_DEFAULT_COMPRESSION = -1;
|
|
|
|
export const Z_FILTERED = 1;
|
|
|
|
export const Z_HUFFMAN_ONLY = 2;
|
|
|
|
export const Z_RLE = 3;
|
|
|
|
export const Z_FIXED = 4;
|
|
|
|
export const Z_DEFAULT_STRATEGY = 0;
|
|
|
|
export const Z_BINARY = 0;
|
|
|
|
export const Z_TEXT = 1;
|
|
|
|
export const Z_UNKNOWN = 2;
|
|
|
|
export const Z_DEFLATED = 8;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
// zlib modes
|
|
|
|
export const NONE = 0;
|
|
|
|
export const DEFLATE = 1;
|
|
|
|
export const INFLATE = 2;
|
|
|
|
export const GZIP = 3;
|
|
|
|
export const GUNZIP = 4;
|
|
|
|
export const DEFLATERAW = 5;
|
|
|
|
export const INFLATERAW = 6;
|
|
|
|
export const UNZIP = 7;
|
|
|
|
|
2024-01-29 08:58:08 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_zlib_close,
|
|
|
|
op_zlib_close_if_pending,
|
|
|
|
op_zlib_init,
|
|
|
|
op_zlib_new,
|
|
|
|
op_zlib_reset,
|
|
|
|
op_zlib_write,
|
2024-01-29 08:58:08 -05:00
|
|
|
} from "ext:core/ops";
|
2024-06-20 01:01:53 -04:00
|
|
|
import process from "node:process";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
const writeResult = new Uint32Array(2);
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
class Zlib {
|
|
|
|
#handle;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
constructor(mode) {
|
2024-01-10 17:37:25 -05:00
|
|
|
this.#handle = op_zlib_new(mode);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
close() {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_zlib_close(this.#handle);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
writeSync(
|
2023-02-14 11:38:45 -05:00
|
|
|
flush,
|
|
|
|
input,
|
|
|
|
in_off,
|
|
|
|
in_len,
|
|
|
|
out,
|
|
|
|
out_off,
|
|
|
|
out_len,
|
|
|
|
) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const err = op_zlib_write(
|
2023-03-27 12:03:07 -04:00
|
|
|
this.#handle,
|
|
|
|
flush,
|
|
|
|
input,
|
|
|
|
in_off,
|
|
|
|
in_len,
|
|
|
|
out,
|
|
|
|
out_off,
|
|
|
|
out_len,
|
|
|
|
writeResult,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.#checkError(err)) {
|
|
|
|
return [writeResult[1], writeResult[0]];
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
#checkError(err) {
|
|
|
|
// Acceptable error states depend on the type of zlib stream.
|
|
|
|
switch (err) {
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
this.#error("unexpected end of file", err);
|
2023-07-08 14:34:08 -04:00
|
|
|
return false;
|
2023-03-27 12:03:07 -04:00
|
|
|
case Z_OK:
|
|
|
|
case Z_STREAM_END:
|
|
|
|
// normal statuses, not fatal
|
|
|
|
break;
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
this.#error("Bad dictionary", err);
|
2023-02-14 11:38:45 -05:00
|
|
|
return false;
|
2023-03-27 12:03:07 -04:00
|
|
|
default:
|
|
|
|
// something else.
|
|
|
|
this.#error("Zlib error", err);
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
return true;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
write(
|
|
|
|
flush,
|
|
|
|
input,
|
|
|
|
in_off,
|
|
|
|
in_len,
|
|
|
|
out,
|
|
|
|
out_off,
|
|
|
|
out_len,
|
|
|
|
) {
|
2024-06-20 01:01:53 -04:00
|
|
|
process.nextTick(() => {
|
|
|
|
const res = this.writeSync(
|
|
|
|
flush ?? Z_NO_FLUSH,
|
|
|
|
input,
|
|
|
|
in_off,
|
|
|
|
in_len,
|
|
|
|
out,
|
|
|
|
out_off,
|
|
|
|
out_len,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
const [availOut, availIn] = res;
|
|
|
|
this.callback(availOut, availIn);
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
init(
|
|
|
|
windowBits,
|
|
|
|
level,
|
|
|
|
memLevel,
|
|
|
|
strategy,
|
2023-07-08 14:34:08 -04:00
|
|
|
dictionary,
|
|
|
|
) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const err = op_zlib_init(
|
2023-03-27 12:03:07 -04:00
|
|
|
this.#handle,
|
|
|
|
level,
|
|
|
|
windowBits,
|
|
|
|
memLevel,
|
|
|
|
strategy,
|
2023-08-11 07:42:35 -04:00
|
|
|
dictionary ?? new Uint8Array(0),
|
2023-03-27 12:03:07 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if (err != Z_OK) {
|
|
|
|
this.#error("Failed to initialize zlib", err);
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
params() {
|
|
|
|
throw new Error("deflateParams Not supported");
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
reset() {
|
2024-01-10 17:37:25 -05:00
|
|
|
const err = op_zlib_reset(this.#handle);
|
2023-03-27 12:03:07 -04:00
|
|
|
if (err != Z_OK) {
|
|
|
|
this.#error("Failed to reset stream", err);
|
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:03:07 -04:00
|
|
|
#error(message, err) {
|
|
|
|
this.onerror(message, err);
|
2024-01-10 17:37:25 -05:00
|
|
|
op_zlib_close_if_pending(this.#handle);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2023-03-27 12:03:07 -04:00
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export { Zlib };
|