1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file
// 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;
// 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;
import {
op_zlib_close,
op_zlib_close_if_pending,
op_zlib_init,
op_zlib_new,
op_zlib_reset,
op_zlib_write,
} from "ext:core/ops";
import process from "node:process";
const writeResult = new Uint32Array(2);
class Zlib {
#handle;
constructor(mode) {
this.#handle = op_zlib_new(mode);
}
close() {
op_zlib_close(this.#handle);
}
writeSync(
flush,
input,
in_off,
in_len,
out,
out_off,
out_len,
) {
const err = op_zlib_write(
this.#handle,
flush,
input,
in_off,
in_len,
out,
out_off,
out_len,
writeResult,
);
if (this.#checkError(err)) {
return [writeResult[1], writeResult[0]];
}
return;
}
#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);
return false;
case Z_OK:
case Z_STREAM_END:
// normal statuses, not fatal
break;
case Z_NEED_DICT:
this.#error("Bad dictionary", err);
return false;
default:
// something else.
this.#error("Zlib error", err);
return false;
}
return true;
}
write(
flush,
input,
in_off,
in_len,
out,
out_off,
out_len,
) {
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);
}
});
return this;
}
init(
windowBits,
level,
memLevel,
strategy,
dictionary,
) {
const err = op_zlib_init(
this.#handle,
level,
windowBits,
memLevel,
strategy,
dictionary ?? new Uint8Array(0),
);
if (err != Z_OK) {
this.#error("Failed to initialize zlib", err);
}
}
params() {
throw new Error("deflateParams Not supported");
}
reset() {
const err = op_zlib_reset(this.#handle);
if (err != Z_OK) {
this.#error("Failed to reset stream", err);
}
}
#error(message, err) {
this.onerror(message, err);
op_zlib_close_if_pending(this.#handle);
}
}
export { Zlib };
|