cf-workers-native 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/README.md +2 -2
  2. package/dist/browser/documents/page-source.js +1 -1
  3. package/dist/browser/headers/index.d.ts +2 -0
  4. package/dist/browser/headers/index.js +5 -0
  5. package/dist/browser/response/index.d.ts +2 -0
  6. package/dist/browser/response/index.js +11 -3
  7. package/dist/browser/structured-clone/index.d.ts +3 -0
  8. package/dist/browser/structured-clone/index.js +75 -7
  9. package/dist/browser/websocket/attachment-size.d.ts +3 -0
  10. package/dist/browser/websocket/attachment-size.js +336 -0
  11. package/dist/browser/websocket/brand.d.ts +4 -0
  12. package/dist/browser/websocket/brand.js +16 -0
  13. package/dist/browser/websocket/close-event.d.ts +7 -0
  14. package/dist/browser/websocket/close-event.js +30 -0
  15. package/dist/browser/websocket/compression.d.ts +5 -0
  16. package/dist/browser/websocket/compression.js +124 -0
  17. package/dist/browser/websocket/conversion.d.ts +2 -0
  18. package/dist/browser/websocket/conversion.js +13 -0
  19. package/dist/browser/websocket/endpoint.d.ts +39 -0
  20. package/dist/browser/websocket/endpoint.js +379 -0
  21. package/dist/browser/websocket/frames.d.ts +21 -0
  22. package/dist/browser/websocket/frames.js +121 -0
  23. package/dist/browser/websocket/native.d.ts +14 -0
  24. package/dist/browser/websocket/native.js +226 -0
  25. package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
  26. package/dist/compat/crypto/LICENSE-3RD-PARTY.md +20 -0
  27. package/dist/compat/crypto/LICENSE.boringssl +238 -0
  28. package/dist/compat/crypto/LICENSE.ncrypto +21 -0
  29. package/dist/compat/crypto/asymmetric.js +141 -68
  30. package/dist/compat/crypto/cipher-handle.d.ts +27 -0
  31. package/dist/compat/crypto/cipher-handle.js +239 -0
  32. package/dist/compat/crypto/cipher-info.d.ts +12 -0
  33. package/dist/compat/crypto/cipher-info.js +324 -0
  34. package/dist/compat/crypto/constants.d.ts +62 -0
  35. package/dist/compat/crypto/constants.js +68 -0
  36. package/dist/compat/crypto/dh-handle.d.ts +17 -0
  37. package/dist/compat/crypto/dh-handle.js +180 -0
  38. package/dist/compat/crypto/dh.d.ts +53 -0
  39. package/dist/compat/crypto/dh.js +138 -0
  40. package/dist/compat/crypto/digest-stream.d.ts +15 -0
  41. package/dist/compat/crypto/digest-stream.js +213 -0
  42. package/dist/compat/crypto/digests.d.ts +3 -0
  43. package/dist/compat/crypto/digests.js +32 -0
  44. package/dist/compat/crypto/ecdh.d.ts +16 -6
  45. package/dist/compat/crypto/hash-handle.d.ts +23 -0
  46. package/dist/compat/crypto/hash-handle.js +70 -0
  47. package/dist/compat/crypto/hash.d.ts +37 -0
  48. package/dist/compat/crypto/hash.js +218 -0
  49. package/dist/compat/crypto/hkdf.js +40 -14
  50. package/dist/compat/crypto/index.d.cts +168 -1
  51. package/dist/compat/crypto/index.d.ts +191 -8
  52. package/dist/compat/crypto/index.js +36 -23
  53. package/dist/compat/crypto/key-errors.d.ts +10 -0
  54. package/dist/compat/crypto/key-errors.js +17 -0
  55. package/dist/compat/crypto/key-export.d.ts +8 -0
  56. package/dist/compat/crypto/key-export.js +49 -0
  57. package/dist/compat/crypto/key-formats.d.ts +2 -1
  58. package/dist/compat/crypto/key-formats.js +162 -31
  59. package/dist/compat/crypto/key-options.d.ts +1 -0
  60. package/dist/compat/crypto/key-options.js +74 -0
  61. package/dist/compat/crypto/key-state.d.ts +1 -0
  62. package/dist/compat/crypto/keys.d.ts +20 -12
  63. package/dist/compat/crypto/keys.js +129 -59
  64. package/dist/compat/crypto/metadata.d.ts +9 -0
  65. package/dist/compat/crypto/metadata.js +89 -2
  66. package/dist/compat/crypto/native-cipher.d.ts +17 -0
  67. package/dist/compat/crypto/native-cipher.js +192 -0
  68. package/dist/compat/crypto/native-crypto.d.ts +14 -1
  69. package/dist/compat/crypto/native-crypto.js +182 -6
  70. package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
  71. package/dist/compat/crypto/pbkdf2-handle.js +26 -0
  72. package/dist/compat/crypto/pbkdf2.d.ts +5 -0
  73. package/dist/compat/crypto/pbkdf2.js +60 -0
  74. package/dist/compat/crypto/prime.js +4 -4
  75. package/dist/compat/crypto/random.d.ts +11 -0
  76. package/dist/compat/crypto/random.js +86 -6
  77. package/dist/compat/crypto/rsa.d.ts +17 -1
  78. package/dist/compat/crypto/rsa.js +8 -4
  79. package/dist/compat/crypto/scrypt.d.ts +3 -3
  80. package/dist/compat/crypto/scrypt.js +25 -23
  81. package/dist/compat/crypto/sign-handle.d.ts +3 -3
  82. package/dist/compat/crypto/sign-handle.js +55 -11
  83. package/dist/compat/crypto/signatures.d.ts +17 -11
  84. package/dist/compat/crypto/subtle-digest.d.ts +1 -0
  85. package/dist/compat/crypto/subtle-digest.js +56 -0
  86. package/dist/compat/crypto/symmetric.d.ts +40 -24
  87. package/dist/compat/crypto/symmetric.js +229 -94
  88. package/dist/compat/crypto/validation.d.ts +4 -0
  89. package/dist/compat/crypto/validation.js +29 -2
  90. package/dist/compat/crypto/webcrypto.d.ts +13 -0
  91. package/dist/compat/crypto/webcrypto.js +51 -0
  92. package/dist/compat/crypto/x509.js +2 -1
  93. package/dist/compat/events/index.js +1 -1
  94. package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
  95. package/dist/compat/fs/LICENSE-APACHE +177 -0
  96. package/dist/compat/fs/directory-backend.d.ts +6 -0
  97. package/dist/compat/fs/directory-backend.js +51 -0
  98. package/dist/compat/fs/directory.d.ts +37 -0
  99. package/dist/compat/fs/directory.js +137 -0
  100. package/dist/compat/fs/embedded.d.ts +1 -1
  101. package/dist/compat/fs/embedded.js +7 -2
  102. package/dist/compat/fs/file-handle.d.ts +75 -0
  103. package/dist/compat/fs/file-handle.js +249 -0
  104. package/dist/compat/fs/glob-internal.d.ts +15 -0
  105. package/dist/compat/fs/glob-internal.js +417 -0
  106. package/dist/compat/fs/glob.d.ts +10 -0
  107. package/dist/compat/fs/glob.js +71 -0
  108. package/dist/compat/fs/index.d.ts +6 -3
  109. package/dist/compat/fs/index.js +596 -50
  110. package/dist/compat/fs/io-validation.d.ts +14 -0
  111. package/dist/compat/fs/io-validation.js +130 -0
  112. package/dist/compat/fs/io.d.ts +1 -0
  113. package/dist/compat/fs/io.js +283 -0
  114. package/dist/compat/fs/promises/index.d.ts +3 -1
  115. package/dist/compat/fs/public-types.d.ts +120 -0
  116. package/dist/compat/fs/public-types.js +1 -0
  117. package/dist/compat/fs/stream-errors.d.ts +6 -0
  118. package/dist/compat/fs/stream-errors.js +8 -0
  119. package/dist/compat/fs/stream-utils.d.ts +16 -0
  120. package/dist/compat/fs/stream-utils.js +29 -0
  121. package/dist/compat/fs/streams.d.ts +104 -0
  122. package/dist/compat/fs/streams.js +794 -0
  123. package/dist/compat/fs/writable.d.ts +8 -0
  124. package/dist/compat/fs/writable.js +412 -22
  125. package/dist/compat/http/index.d.ts +11 -28
  126. package/dist/compat/http/index.js +81 -125
  127. package/dist/compat/http/server-options.d.ts +1 -0
  128. package/dist/compat/http/server-options.js +87 -0
  129. package/dist/compat/http/unique-headers.d.ts +1 -0
  130. package/dist/compat/http/unique-headers.js +21 -0
  131. package/dist/compat/http-outgoing/index.d.ts +2 -1
  132. package/dist/compat/http-outgoing/index.js +2 -17
  133. package/dist/compat/http-server/index.d.ts +2 -2
  134. package/dist/compat/http-server/index.js +4 -85
  135. package/dist/compat/https/index.d.ts +4 -2
  136. package/dist/compat/net/sockets.js +98 -20
  137. package/dist/compat/net/validators.d.ts +1 -0
  138. package/dist/compat/net/validators.js +8 -0
  139. package/dist/compat/process/configuration.d.ts +113 -0
  140. package/dist/compat/process/configuration.js +115 -0
  141. package/dist/compat/process/directory.d.ts +4 -0
  142. package/dist/compat/process/directory.js +28 -0
  143. package/dist/compat/process/files.d.ts +3 -0
  144. package/dist/compat/process/files.js +10 -0
  145. package/dist/compat/process/index.d.ts +15 -13
  146. package/dist/compat/process/index.js +47 -74
  147. package/dist/compat/process/scheduling.d.ts +2 -0
  148. package/dist/compat/process/scheduling.js +60 -0
  149. package/dist/compat/process/state.js +1 -0
  150. package/dist/compat/process/stdio.d.ts +18 -0
  151. package/dist/compat/process/stdio.js +66 -0
  152. package/dist/compat/stream/destroy.d.ts +1 -0
  153. package/dist/compat/stream/destroy.js +21 -1
  154. package/dist/compat/stream/readable.js +10 -2
  155. package/dist/compat/tls/jsstream.js +16 -6
  156. package/dist/compat/tls/wrap.d.ts +1 -1
  157. package/dist/compat/tls/wrap.js +0 -3
  158. package/dist/compat/util/inspect.js +5 -1
  159. package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
  160. package/dist/compat/zlib/LICENSE-APACHE +177 -0
  161. package/dist/compat/zlib/LICENSE.zlib +22 -0
  162. package/dist/compat/zlib/classic-base.cjs +600 -0
  163. package/dist/compat/zlib/classic-base.d.cts +39 -0
  164. package/dist/compat/zlib/classic-binding.d.ts +28 -0
  165. package/dist/compat/zlib/classic-binding.js +105 -0
  166. package/dist/compat/zlib/classic.d.ts +44 -0
  167. package/dist/compat/zlib/classic.js +199 -0
  168. package/dist/compat/zlib/codec-binding.d.ts +33 -0
  169. package/dist/compat/zlib/codec-binding.js +141 -0
  170. package/dist/compat/zlib/compression.d.ts +22 -51
  171. package/dist/compat/zlib/compression.js +129 -218
  172. package/dist/compat/zlib/errors.d.ts +21 -0
  173. package/dist/compat/zlib/errors.js +32 -0
  174. package/dist/compat/zlib/index.d.ts +367 -3
  175. package/dist/compat/zlib/index.js +28 -11
  176. package/dist/compat/zlib/input.d.ts +16 -0
  177. package/dist/compat/zlib/input.js +132 -0
  178. package/dist/compat/zlib/native-codecs.d.ts +7 -0
  179. package/dist/compat/zlib/native-codecs.js +22 -10
  180. package/dist/compat/zlib/native-zlib.d.ts +5 -0
  181. package/dist/compat/zlib/native-zlib.js +52 -0
  182. package/dist/compat/zlib/public-types.d.ts +48 -0
  183. package/dist/compat/zlib/public-types.js +1 -0
  184. package/dist/compat/zlib/validators.d.ts +3 -0
  185. package/dist/compat/zlib/validators.js +18 -0
  186. package/dist/shared/buffer-source.d.ts +5 -0
  187. package/dist/shared/buffer-source.js +70 -0
  188. package/dist/shared/globals/platform.d.ts +8 -0
  189. package/dist/shared/globals/platform.js +1 -0
  190. package/dist/shared/globals/web.js +5 -0
  191. package/dist/shared/request-context.d.ts +2 -0
  192. package/dist/shared/request-context.js +11 -0
  193. package/dist/shared/request-scope.js +4 -1
  194. package/dist/tooling/shared.js +6 -0
  195. package/dist/tooling/vite.js +4 -0
  196. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +38 -7
  197. package/package.json +2 -2
@@ -1,259 +1,170 @@
1
+ import { nativeNumber, codecBytes, assertCodecInput, nativeInt32, optionInteger as integer, } from "./input.js";
2
+ import { isAnyBuffer } from "../../shared/buffer-source.js";
1
3
  import { Buffer } from "node:buffer";
2
- import { Transform } from "node:stream";
3
4
  import { NativeCodec } from "./native-codecs.js";
4
- import { validateFunction, validateObject } from "../net/validators.js";
5
- import { validateInteger } from "../crypto/validation.js";
6
- import { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
7
- const flushMarker = Symbol("codec flush");
8
- function bytes(value) {
9
- if (typeof value === "string")
10
- return Buffer.from(value);
11
- if (ArrayBuffer.isView(value))
12
- return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
13
- if (value instanceof ArrayBuffer)
14
- return new Uint8Array(value);
15
- throw new ERR_INVALID_ARG_TYPE("buffer", ["string", "Buffer", "TypedArray", "DataView", "ArrayBuffer"], value);
16
- }
17
- // Pinned workerd validators: NaN selects defaults; finite fractional values
18
- // are accepted here and converted by the underlying buffer/codec operation.
19
- function finite(value, name) {
20
- if (value === undefined || Number.isNaN(value))
21
- return false;
22
- if (typeof value !== "number")
23
- throw new ERR_INVALID_ARG_TYPE(name, "number", value);
24
- if (!Number.isFinite(value))
25
- throw new ERR_OUT_OF_RANGE(name, "a finite number", value);
26
- return true;
27
- }
28
- function range(value, name, low, high, fallback) {
29
- if (!finite(value, name))
30
- return fallback;
31
- if (value < low || value > high)
32
- throw new ERR_OUT_OF_RANGE(name, `>= ${low} and <= ${high}`, value);
33
- return value;
34
- }
35
- function settings(kind, encode, value = {}) {
36
- if (value == null)
37
- value = {};
38
- validateObject(value, "options");
39
- const chunkSize = finite(value.chunkSize, "options.chunkSize")
40
- ? value.chunkSize
41
- : 16384;
42
- if (chunkSize < 64)
43
- throw new ERR_OUT_OF_RANGE("options.chunkSize", ">= 64", chunkSize);
44
- const maxOutputLength = range(value.maxOutputLength, "options.maxOutputLength", 1, 2147483647, 2147483647);
45
- const params = value.params ?? {};
46
- validateObject(params, "options.params");
47
- const maximum = kind === "brotli" ? 8 : encode ? 402 : 100;
48
- const seen = new Set();
49
- for (const [key, val] of Object.entries(params)) {
50
- const number = Number(key);
51
- if (!Number.isInteger(number) ||
52
- number < 0 ||
53
- number > maximum ||
54
- seen.has(number))
55
- throw Object.assign(new RangeError(`${key} is not a valid ${kind === "brotli" ? "Brotli" : "Zstd"} parameter`), {
56
- code: kind === "brotli"
57
- ? "ERR_BROTLI_INVALID_PARAM"
58
- : "ERR_ZSTD_INVALID_PARAM",
59
- });
60
- seen.add(number);
61
- if (typeof val !== "number" && typeof val !== "boolean")
62
- throw new ERR_INVALID_ARG_TYPE("options.params[key]", "number", val);
63
- }
64
- const flush = range(value.flush, "options.flush", 0, kind === "brotli" ? 3 : 2, 0);
65
- const finishFlush = range(value.finishFlush, "options.finishFlush", 0, kind === "brotli" ? 3 : 2, 2);
66
- if (value.pledgedSrcSize !== undefined)
67
- validateInteger(value.pledgedSrcSize, "options.pledgedSrcSize", 0, 0xffffffff);
68
- return {
69
- chunkSize,
70
- maxOutputLength,
71
- params,
72
- flush,
73
- finishFlush,
74
- pledged: value.pledgedSrcSize,
75
- info: !!value.info,
76
- };
77
- }
78
- class CodecTransform extends Transform {
79
- codec;
80
- config;
81
- bytesWritten = 0;
82
- kind;
83
- encode;
84
- constructor(kind, encode, options) {
85
- const config = settings(kind, encode, options);
86
- super(options ?? {});
87
- this.kind = kind;
88
- this.encode = encode;
89
- this.config = config;
90
- this.codec = new NativeCodec(kind, encode, config.chunkSize, config.params, config.pledged);
91
- }
92
- _transform(input, _encoding, callback) {
93
- try {
94
- const operation = input[flushMarker] ?? this.config.flush;
95
- this.codec.process(bytes(input), operation, (output) => this.push(Buffer.from(output)));
96
- this.bytesWritten = this.codec.consumed;
97
- callback();
98
- }
99
- catch (error) {
100
- callback(error);
101
- }
102
- }
103
- _flush(callback) {
104
- try {
105
- this.codec.process(new Uint8Array(), this.config.finishFlush, (output) => this.push(Buffer.from(output)));
106
- this.bytesWritten = this.codec.consumed;
107
- callback();
108
- }
109
- catch (error) {
110
- callback(error);
111
- }
112
- }
113
- _destroy(error, callback) {
114
- this.codec.close();
115
- callback(error);
116
- }
117
- flush(kind, callback) {
118
- if (typeof kind === "function") {
119
- callback = kind;
120
- kind = 1;
121
- }
122
- kind ??= 1;
123
- validateInteger(kind, "kind", 0, this.kind === "brotli" ? 3 : 2);
124
- if (callback !== undefined)
125
- validateFunction(callback, "callback");
126
- if (this.writableFinished) {
127
- if (callback)
128
- queueMicrotask(callback);
129
- return;
130
- }
131
- const marker = Buffer.alloc(0);
132
- marker[flushMarker] = kind;
133
- this.write(marker, callback);
134
- }
135
- reset() {
136
- if (this.destroyed)
137
- throw new Error("Codec is closed");
138
- this.codec.close();
139
- this.codec = new NativeCodec(this.kind, this.encode, this.config.chunkSize, this.config.params, this.config.pledged);
140
- this.bytesWritten = 0;
141
- }
142
- close(callback) {
143
- if (callback) {
144
- validateFunction(callback, "callback");
145
- this.once("close", callback);
146
- }
147
- this.destroy();
148
- }
149
- _processChunk(input, operation = 2) {
150
- const chunks = [];
151
- let length = 0;
152
- try {
153
- this.codec.process(bytes(input), operation, (output) => {
154
- length += output.length;
155
- if (length > this.config.maxOutputLength)
156
- throw Object.assign(new RangeError("Cannot create a Buffer larger than maxOutputLength"), { code: "ERR_BUFFER_TOO_LARGE" });
157
- chunks.push(output);
158
- });
159
- this.bytesWritten = this.codec.consumed;
160
- return Buffer.concat(chunks, length);
161
- }
162
- finally {
163
- this.codec.close();
164
- }
165
- }
166
- }
167
- export class BrotliCompress extends CodecTransform {
5
+ import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
6
+ import { Brotli, Zstd, zstdInitCParamsArray, zstdInitDParamsArray, kMaxZstdCParam, kMaxZstdDParam, } from "./classic-base.cjs";
7
+ export class BrotliCompress extends Brotli {
168
8
  constructor(options) {
169
- super("brotli", true, options);
9
+ super(options, 9);
170
10
  }
171
11
  }
172
- export class BrotliDecompress extends CodecTransform {
12
+ export class BrotliDecompress extends Brotli {
173
13
  constructor(options) {
174
- super("brotli", false, options);
14
+ super(options, 8);
175
15
  }
176
16
  }
177
- export class ZstdCompress extends CodecTransform {
17
+ export class ZstdCompress extends Zstd {
178
18
  constructor(options) {
179
- super("zstd", true, options);
19
+ super(options, 10, zstdInitCParamsArray, kMaxZstdCParam);
180
20
  }
181
21
  }
182
- export class ZstdDecompress extends CodecTransform {
22
+ export class ZstdDecompress extends Zstd {
183
23
  constructor(options) {
184
- super("zstd", false, options);
24
+ super(options, 11, zstdInitDParamsArray, kMaxZstdDParam);
185
25
  }
186
26
  }
187
27
  export const createBrotliCompress = (options) => new BrotliCompress(options);
188
28
  export const createBrotliDecompress = (options) => new BrotliDecompress(options);
189
29
  export const createZstdCompress = (options) => new ZstdCompress(options);
190
30
  export const createZstdDecompress = (options) => new ZstdDecompress(options);
191
- function sync(kind, encode, input, options) {
192
- const data = bytes(input), config = settings(kind, encode, options);
193
- if (config.info) {
194
- const Constructor = kind === "brotli"
195
- ? encode
196
- ? BrotliCompress
197
- : BrotliDecompress
198
- : encode
199
- ? ZstdCompress
200
- : ZstdDecompress;
201
- const engine = new Constructor(options);
202
- return { buffer: engine._processChunk(data, config.finishFlush), engine };
203
- }
204
- const codec = new NativeCodec(kind, encode, config.chunkSize, config.params, config.pledged ?? (encode && kind === "zstd" ? data.length : undefined));
31
+ function bytes(value, method) {
32
+ if (typeof value === "string")
33
+ return Buffer.from(value);
34
+ if (ArrayBuffer.isView(value) || isAnyBuffer(value))
35
+ return codecBytes(value);
36
+ throw new TypeError(`Failed to execute '${method}' on 'ZlibUtil': parameter 1 is not of type 'string or ArrayBuffer or ArrayBufferView'.`);
37
+ }
38
+ function pledged(value) {
39
+ if (value === undefined)
40
+ return undefined;
41
+ const number = nativeNumber(value);
42
+ if (!Number.isFinite(number) || number < 0 || number >= 18446744073709551616)
43
+ throw new TypeError("The value cannot be converted because it is not an integer.");
44
+ return Math.trunc(number);
45
+ }
46
+ function convert(options, kind) {
47
+ if (!options || typeof options !== "object")
48
+ throw new TypeError("Options must be an object");
49
+ const flush = integer(options.flush, 0, false);
50
+ const finishFlush = integer(options.finishFlush, 2, false);
51
+ const chunkSize = integer(options.chunkSize, 16384);
52
+ const parameters = options.params;
53
+ if (parameters !== undefined &&
54
+ (!parameters || typeof parameters !== "object"))
55
+ throw new TypeError("Incorrect type for the 'params' field on 'Options': the provided value is not of type 'object'.");
56
+ const params = Object.create(null);
57
+ for (const key of Object.keys(parameters ?? {}))
58
+ params[key] = nativeInt32(parameters[key]);
59
+ const maxOutputLength = integer(options.maxOutputLength, 134217728);
60
+ return {
61
+ flush,
62
+ finishFlush,
63
+ chunkSize,
64
+ params,
65
+ maxOutputLength,
66
+ pledged: kind === "zstd" ? pledged(options.pledgedSrcSize) : undefined,
67
+ };
68
+ }
69
+ function bounded(value, name, min, max) {
70
+ if (value < min || value > max)
71
+ throw new RangeError(`The value of "options.${name}" is out of range. It must be >= ${min} and <= ${max}. Received ${value}`);
72
+ }
73
+ function fast(kind, encode, data, config) {
74
+ bounded(config.chunkSize, "chunkSize", 64, 134217728);
75
+ bounded(config.maxOutputLength, "maxOutputLength", 1, 134217728);
76
+ for (const key of Object.keys(config.params))
77
+ if (!/^[+-]?\d+$/.test(key))
78
+ throw new Error("Invalid parameter name");
79
+ const codec = new NativeCodec(kind, encode, Math.min(16384, config.maxOutputLength), config.params, config.pledged ?? (encode && kind === "zstd" ? data.length : undefined));
205
80
  const chunks = [];
206
81
  let length = 0;
207
82
  try {
83
+ bounded(config.flush, "flush", 0, kind === "brotli" ? 3 : 2);
84
+ bounded(config.finishFlush, "finishFlush", 0, kind === "brotli" ? 3 : 2);
85
+ assertCodecInput(data);
208
86
  codec.process(data, config.finishFlush, (output) => {
209
87
  length += output.length;
210
88
  if (length > config.maxOutputLength)
211
- throw new RangeError("Memory limit exceeded");
89
+ throw kind === "zstd" && encode
90
+ ? new Error("Unexpected end of file")
91
+ : new RangeError("Memory limit exceeded");
212
92
  chunks.push(output);
213
93
  });
214
94
  return Buffer.concat(chunks, length);
215
95
  }
216
96
  catch (error) {
217
- if (error instanceof RangeError ||
218
- (error instanceof Error && error.message === "Unexpected end of file"))
97
+ if (error instanceof TypeError ||
98
+ error instanceof RangeError ||
99
+ (error instanceof Error &&
100
+ ["Unexpected end of file", "unexpected end of file"].includes(error.message)))
219
101
  throw error;
102
+ if (kind === "zstd" &&
103
+ !encode &&
104
+ error?.code === "ZSTD_ERROR")
105
+ throw new Error(`Zstd decompression failed: ${error.message}`);
220
106
  throw new Error("Compression failed");
221
107
  }
222
108
  finally {
223
109
  codec.close();
224
110
  }
225
111
  }
112
+ function engine(kind, encode, options) {
113
+ return new (kind === "brotli"
114
+ ? encode
115
+ ? BrotliCompress
116
+ : BrotliDecompress
117
+ : encode
118
+ ? ZstdCompress
119
+ : ZstdDecompress)(options);
120
+ }
121
+ function sync(kind, encode, input, options = {}) {
122
+ if (options.info) {
123
+ const instance = engine(kind, encode, options);
124
+ return {
125
+ buffer: instance._processChunk(typeof input === "string"
126
+ ? Buffer.from(input)
127
+ : input, instance._finishFlushFlag),
128
+ engine: instance,
129
+ };
130
+ }
131
+ return fast(kind, encode, bytes(input, `${kind}${encode ? "Compress" : "Decompress"}Sync`), convert(options, kind));
132
+ }
226
133
  function asyncOperation(kind, encode, input, options, callback) {
227
134
  if (typeof options === "function") {
228
135
  callback = options;
229
- options = undefined;
136
+ options = {};
137
+ }
138
+ if (typeof callback !== "function")
139
+ throw new ERR_INVALID_ARG_TYPE("callback", "Function", callback);
140
+ let run;
141
+ if (options.info) {
142
+ const instance = engine(kind, encode, options);
143
+ run = () => ({
144
+ buffer: instance._processChunk(typeof input === "string"
145
+ ? Buffer.from(input)
146
+ : input, instance._finishFlushFlag),
147
+ engine: instance,
148
+ });
230
149
  }
231
- validateFunction(callback, "callback");
232
- const data = bytes(input);
233
- settings(kind, encode, options);
234
- new Promise((resolve) => resolve(sync(kind, encode, data, options))).then((result) => callback(null, result), (error) => callback(error));
235
- }
236
- export function brotliCompressSync(input, options) {
237
- return sync("brotli", true, input, options);
238
- }
239
- export function brotliDecompressSync(input, options) {
240
- return sync("brotli", false, input, options);
241
- }
242
- export function zstdCompressSync(input, options) {
243
- return sync("zstd", true, input, options);
244
- }
245
- export function zstdDecompressSync(input, options) {
246
- return sync("zstd", false, input, options);
247
- }
248
- export function brotliCompress(input, options, callback) {
249
- asyncOperation("brotli", true, input, options, callback);
250
- }
251
- export function brotliDecompress(input, options, callback) {
252
- asyncOperation("brotli", false, input, options, callback);
253
- }
254
- export function zstdCompress(input, options, callback) {
255
- asyncOperation("zstd", true, input, options, callback);
256
- }
257
- export function zstdDecompress(input, options, callback) {
258
- asyncOperation("zstd", false, input, options, callback);
150
+ else {
151
+ const data = bytes(input, `${kind}${encode ? "Compress" : "Decompress"}`), config = convert(options, kind);
152
+ run = () => fast(kind, encode, data, config);
153
+ }
154
+ let result, error;
155
+ try {
156
+ result = run();
157
+ }
158
+ catch (failure) {
159
+ error = failure;
160
+ }
161
+ queueMicrotask(() => (error ? callback(error) : callback(null, result)));
259
162
  }
163
+ export const brotliCompressSync = ((input, options) => sync("brotli", true, input, options));
164
+ export const brotliDecompressSync = ((input, options) => sync("brotli", false, input, options));
165
+ export const zstdCompressSync = ((input, options) => sync("zstd", true, input, options));
166
+ export const zstdDecompressSync = ((input, options) => sync("zstd", false, input, options));
167
+ export const brotliCompress = ((input, options, callback) => asyncOperation("brotli", true, input, options, callback));
168
+ export const brotliDecompress = ((input, options, callback) => asyncOperation("brotli", false, input, options, callback));
169
+ export const zstdCompress = ((input, options, callback) => asyncOperation("zstd", true, input, options, callback));
170
+ export const zstdDecompress = ((input, options, callback) => asyncOperation("zstd", false, input, options, callback));
@@ -0,0 +1,21 @@
1
+ export { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
2
+ export declare class NodeError extends Error {
3
+ readonly code: string;
4
+ constructor(code: string, message: string);
5
+ }
6
+ export declare class ERR_BUFFER_TOO_LARGE extends RangeError {
7
+ readonly code = "ERR_BUFFER_TOO_LARGE";
8
+ constructor(size: number);
9
+ }
10
+ export declare class ERR_BROTLI_INVALID_PARAM extends RangeError {
11
+ readonly code = "ERR_BROTLI_INVALID_PARAM";
12
+ constructor(key: string);
13
+ }
14
+ export declare class ERR_ZSTD_INVALID_PARAM extends RangeError {
15
+ readonly code = "ERR_ZSTD_INVALID_PARAM";
16
+ constructor(key: string);
17
+ }
18
+ export declare class ERR_ZLIB_INITIALIZATION_FAILED extends Error {
19
+ readonly code = "ERR_ZLIB_INITIALIZATION_FAILED";
20
+ constructor();
21
+ }
@@ -0,0 +1,32 @@
1
+ export { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
2
+ export class NodeError extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.code = code;
7
+ }
8
+ }
9
+ export class ERR_BUFFER_TOO_LARGE extends RangeError {
10
+ code = "ERR_BUFFER_TOO_LARGE";
11
+ constructor(size) {
12
+ super(`Cannot create a Buffer larger than ${size} bytes`);
13
+ }
14
+ }
15
+ export class ERR_BROTLI_INVALID_PARAM extends RangeError {
16
+ code = "ERR_BROTLI_INVALID_PARAM";
17
+ constructor(key) {
18
+ super(`${key} is not a valid Brotli parameter`);
19
+ }
20
+ }
21
+ export class ERR_ZSTD_INVALID_PARAM extends RangeError {
22
+ code = "ERR_ZSTD_INVALID_PARAM";
23
+ constructor(key) {
24
+ super(`${key} is not a valid Zstd parameter`);
25
+ }
26
+ }
27
+ export class ERR_ZLIB_INITIALIZATION_FAILED extends Error {
28
+ code = "ERR_ZLIB_INITIALIZATION_FAILED";
29
+ constructor() {
30
+ super("Initialization failed");
31
+ }
32
+ }