cf-workers-native 0.1.5 → 0.1.6

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 +125 -56
  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 +118 -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 +226 -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 +582 -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 +282 -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
@@ -0,0 +1,105 @@
1
+ import { viewBytes } from "../../shared/buffer-source.js";
2
+ import { bindingBuffers } from "./input.js";
3
+ import * as codecs from "./codec-binding.js";
4
+ import { Buffer } from "node:buffer";
5
+ import { library, message } from "./native-zlib.js";
6
+ import * as constants from "./constants.js";
7
+ const codes = {
8
+ 0: "Z_OK",
9
+ 1: "Z_STREAM_END",
10
+ 2: "Z_NEED_DICT",
11
+ [-1]: "Z_ERRNO",
12
+ [-2]: "Z_STREAM_ERROR",
13
+ [-3]: "Z_DATA_ERROR",
14
+ [-4]: "Z_MEM_ERROR",
15
+ [-5]: "Z_BUF_ERROR",
16
+ [-6]: "Z_VERSION_ERROR",
17
+ };
18
+ export class ZlibStream {
19
+ mode;
20
+ api = library();
21
+ context = 0;
22
+ dictionarySize = 0;
23
+ state;
24
+ callback;
25
+ onerror;
26
+ constructor(mode) {
27
+ this.mode = mode;
28
+ }
29
+ initialize(windowBits, level, memLevel, strategy, state, callback, dictionary) {
30
+ this.state = state;
31
+ this.callback = callback;
32
+ const bytes = dictionary && Buffer.from(viewBytes(dictionary));
33
+ this.dictionarySize = bytes?.length ?? 0;
34
+ const pointer = bytes ? this.api.malloc(bytes.length || 1) : 0;
35
+ try {
36
+ if (bytes) {
37
+ if (!pointer)
38
+ throw new RangeError("Memory limit exceeded");
39
+ new Uint8Array(this.api.memory.buffer).set(bytes, pointer);
40
+ }
41
+ this.context = this.api.cz_create(this.mode, windowBits, Math.trunc(level), Math.trunc(memLevel), Math.trunc(strategy), pointer, bytes?.length ?? 0);
42
+ if (!this.context)
43
+ throw new Error("Zlib error");
44
+ }
45
+ finally {
46
+ if (pointer)
47
+ this.api.free(pointer);
48
+ }
49
+ }
50
+ setErrorHandler(handler) {
51
+ this.onerror = handler;
52
+ }
53
+ writeSync(flush, input, inputOffset, inputLength, output, outputOffset, outputLength) {
54
+ const api = this.api;
55
+ const bounds = bindingBuffers("ZlibContext", input, inputOffset, inputLength, output, outputOffset, outputLength);
56
+ const buffer = bounds.source;
57
+ ({ inputOffset, inputLength, outputOffset, outputLength } = bounds);
58
+ const pointer = api.malloc(Math.max(1, inputLength) + outputLength + 8);
59
+ if (!pointer)
60
+ throw new RangeError("Memory limit exceeded");
61
+ const out = pointer + Math.max(1, inputLength), state = out + outputLength;
62
+ try {
63
+ new Uint8Array(api.memory.buffer).set(buffer.subarray(inputOffset, inputOffset + inputLength), pointer);
64
+ const result = api.cz_process(this.context, pointer, inputLength, out, outputLength, flush, state);
65
+ const view = new DataView(api.memory.buffer);
66
+ this.state[0] = view.getUint32(state, true);
67
+ this.state[1] = view.getUint32(state + 4, true);
68
+ bounds.target.set(new Uint8Array(api.memory.buffer, out, outputLength - this.state[0]), outputOffset);
69
+ if (result !== 0 &&
70
+ result !== 1 &&
71
+ (result !== -5 || (flush === 4 && this.state[0] !== 0)))
72
+ this.onerror?.(result, codes[result], result === -5
73
+ ? "unexpected end of file"
74
+ : result === 2
75
+ ? this.dictionarySize === 0
76
+ ? "Missing dictionary"
77
+ : "Bad dictionary"
78
+ : message(api, api.cz_message(this.context)));
79
+ }
80
+ finally {
81
+ api.free(pointer);
82
+ }
83
+ }
84
+ write(...args) {
85
+ this.writeSync(...args);
86
+ this.callback();
87
+ }
88
+ reset() {
89
+ const result = this.api.cz_reset(this.context);
90
+ if (result !== 0)
91
+ this.onerror?.(result, codes[result], "Failed to reset stream");
92
+ }
93
+ params(level, strategy) {
94
+ const result = this.api.cz_params(this.context, level, strategy);
95
+ if (result !== 0)
96
+ this.onerror?.(result, codes[result], "Failed to set parameters");
97
+ }
98
+ close() {
99
+ if (this.context) {
100
+ this.api.cz_free(this.context);
101
+ this.context = 0;
102
+ }
103
+ }
104
+ }
105
+ export default Object.assign({ ZlibStream, ...codecs }, Object.fromEntries(Object.entries(constants).map(([name, value]) => ["CONST_" + name, value])));
@@ -0,0 +1,44 @@
1
+ import type { ZlibOptions, SyncCodec, AsyncCodec } from "./public-types.js";
2
+ import { Zlib } from "./classic-base.cjs";
3
+ export declare class Deflate extends Zlib {
4
+ constructor(options?: ZlibOptions);
5
+ }
6
+ export declare class Inflate extends Zlib {
7
+ constructor(options?: ZlibOptions);
8
+ }
9
+ export declare class Gzip extends Zlib {
10
+ constructor(options?: ZlibOptions);
11
+ }
12
+ export declare class Gunzip extends Zlib {
13
+ constructor(options?: ZlibOptions);
14
+ }
15
+ export declare class DeflateRaw extends Zlib {
16
+ constructor(options?: ZlibOptions);
17
+ }
18
+ export declare class InflateRaw extends Zlib {
19
+ constructor(options?: ZlibOptions);
20
+ }
21
+ export declare class Unzip extends Zlib {
22
+ constructor(options?: ZlibOptions);
23
+ }
24
+ export declare const createDeflate: (options?: ZlibOptions) => Deflate;
25
+ export declare const createInflate: (options?: ZlibOptions) => Inflate;
26
+ export declare const createGzip: (options?: ZlibOptions) => Gzip;
27
+ export declare const createGunzip: (options?: ZlibOptions) => Gunzip;
28
+ export declare const createDeflateRaw: (options?: ZlibOptions) => DeflateRaw;
29
+ export declare const createInflateRaw: (options?: ZlibOptions) => InflateRaw;
30
+ export declare const createUnzip: (options?: ZlibOptions) => Unzip;
31
+ export declare const deflateSync: SyncCodec<ZlibOptions, Deflate>;
32
+ export declare const inflateSync: SyncCodec<ZlibOptions, Inflate>;
33
+ export declare const gzipSync: SyncCodec<ZlibOptions, Gzip>;
34
+ export declare const gunzipSync: SyncCodec<ZlibOptions, Gunzip>;
35
+ export declare const deflateRawSync: SyncCodec<ZlibOptions, DeflateRaw>;
36
+ export declare const inflateRawSync: SyncCodec<ZlibOptions, InflateRaw>;
37
+ export declare const unzipSync: SyncCodec<ZlibOptions, Unzip>;
38
+ export declare const deflate: AsyncCodec<ZlibOptions, Deflate>;
39
+ export declare const inflate: AsyncCodec<ZlibOptions, Inflate>;
40
+ export declare const gzip: AsyncCodec<ZlibOptions, Gzip>;
41
+ export declare const gunzip: AsyncCodec<ZlibOptions, Gunzip>;
42
+ export declare const deflateRaw: AsyncCodec<ZlibOptions, DeflateRaw>;
43
+ export declare const inflateRaw: AsyncCodec<ZlibOptions, InflateRaw>;
44
+ export declare const unzip: AsyncCodec<ZlibOptions, Unzip>;
@@ -0,0 +1,199 @@
1
+ import { optionInteger as integer, codecBytes, assertCodecInput, } from "./input.js";
2
+ import { isAnyBuffer } from "../../shared/buffer-source.js";
3
+ import { Buffer } from "node:buffer";
4
+ import { Zlib } from "./classic-base.cjs";
5
+ import { ZlibStream } from "./classic-binding.js";
6
+ import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
7
+ export class Deflate extends Zlib {
8
+ constructor(options) {
9
+ super(options, 1);
10
+ }
11
+ }
12
+ export class Inflate extends Zlib {
13
+ constructor(options) {
14
+ super(options, 2);
15
+ }
16
+ }
17
+ export class Gzip extends Zlib {
18
+ constructor(options) {
19
+ super(options, 3);
20
+ }
21
+ }
22
+ export class Gunzip extends Zlib {
23
+ constructor(options) {
24
+ super(options, 4);
25
+ }
26
+ }
27
+ export class DeflateRaw extends Zlib {
28
+ constructor(options) {
29
+ if (options?.windowBits === 8)
30
+ options.windowBits = 9;
31
+ super(options, 5);
32
+ }
33
+ }
34
+ export class InflateRaw extends Zlib {
35
+ constructor(options) {
36
+ super(options, 6);
37
+ }
38
+ }
39
+ export class Unzip extends Zlib {
40
+ constructor(options) {
41
+ super(options, 7);
42
+ }
43
+ }
44
+ const constructors = [
45
+ Deflate,
46
+ Inflate,
47
+ Gzip,
48
+ Gunzip,
49
+ DeflateRaw,
50
+ InflateRaw,
51
+ Unzip,
52
+ ];
53
+ export const createDeflate = (options) => new Deflate(options);
54
+ export const createInflate = (options) => new Inflate(options);
55
+ export const createGzip = (options) => new Gzip(options);
56
+ export const createGunzip = (options) => new Gunzip(options);
57
+ export const createDeflateRaw = (options) => new DeflateRaw(options);
58
+ export const createInflateRaw = (options) => new InflateRaw(options);
59
+ export const createUnzip = (options) => new Unzip(options);
60
+ const maxChunk = 134217728;
61
+ function bytes(input, parameter = 1, method = "zlibSync") {
62
+ if (typeof input === "string")
63
+ return Buffer.from(input);
64
+ if (ArrayBuffer.isView(input) || isAnyBuffer(input))
65
+ return codecBytes(input);
66
+ throw new TypeError(`Failed to execute '${method}' on 'ZlibUtil': parameter ${parameter} is not of type 'string or ArrayBuffer or ArrayBufferView'.`);
67
+ }
68
+ function bounded(value, name, min, max) {
69
+ if (value < min || value > max)
70
+ throw new RangeError(`The value of "options.${name}" is out of range. It must be >= ${min} and <= ${max}. Received ${value}`);
71
+ return value;
72
+ }
73
+ function dictionary(value) {
74
+ if (value === undefined)
75
+ return undefined;
76
+ if (ArrayBuffer.isView(value) || isAnyBuffer(value))
77
+ return codecBytes(value);
78
+ throw new TypeError("Incorrect type for the 'dictionary' field on 'Options': the provided value is not of type 'ArrayBuffer or ArrayBufferView'.");
79
+ }
80
+ function prepare(input, options, method = "zlibSync") {
81
+ const data = bytes(input, 1, method);
82
+ if (typeof options !== "object" || options === null)
83
+ throw new TypeError(`Failed to execute '${method}' on 'ZlibUtil': parameter 2 is not of type 'Options'.`);
84
+ // Conversion precedes native validation, including otherwise unused flush.
85
+ const opts = {
86
+ flush: integer(options.flush, 0, false),
87
+ finishFlush: integer(options.finishFlush, 4, false),
88
+ chunkSize: integer(options.chunkSize, 16384),
89
+ windowBits: integer(options.windowBits, 15),
90
+ level: integer(options.level, -1, false),
91
+ memLevel: integer(options.memLevel, 8),
92
+ strategy: integer(options.strategy, 0),
93
+ dictionary: dictionary(options.dictionary),
94
+ maxOutputLength: integer(options.maxOutputLength, maxChunk),
95
+ };
96
+ return { data, opts };
97
+ }
98
+ function fast(mode, data, opts) {
99
+ bounded(opts.chunkSize, "chunkSize", 64, maxChunk);
100
+ bounded(opts.maxOutputLength, "maxOutputLength", 1, maxChunk);
101
+ if (!(opts.windowBits === 0 && [2, 4, 7].includes(mode)))
102
+ bounded(opts.windowBits | 0, "windowBits", 8, 15);
103
+ bounded(opts.level, "level", -1, 9);
104
+ bounded(opts.memLevel | 0, "memLevel", 1, 9);
105
+ if (opts.strategy > 4)
106
+ throw new Error("invalid strategy");
107
+ if (mode === 3 && opts.windowBits === 8)
108
+ throw new Error("Zlib error");
109
+ bounded(opts.flush, "flush", 0, 6);
110
+ if (opts.finishFlush < 0 || opts.finishFlush > 6)
111
+ throw new RangeError(`The value of "options.finishFlush" is out of range. It must be >= 0 and <= 6. Received ${opts.flush}`);
112
+ assertCodecInput(data);
113
+ assertCodecInput(opts.dictionary);
114
+ const state = new Uint32Array(2), handle = new ZlibStream(mode), chunks = [];
115
+ let error, offset = 0, length = 0;
116
+ handle.initialize(opts.windowBits, opts.level, opts.memLevel, opts.strategy, state, () => { }, opts.dictionary);
117
+ handle.setErrorHandler((_errno, _code, message) => {
118
+ error = new Error(message);
119
+ });
120
+ try {
121
+ for (;;) {
122
+ const size = Math.min(16384, opts.maxOutputLength - length + 1);
123
+ const output = Buffer.allocUnsafe(size);
124
+ handle.writeSync(opts.finishFlush, data, offset, data.length - offset, output, 0, size);
125
+ if (error)
126
+ throw error;
127
+ offset = data.length - state[1];
128
+ const produced = size - state[0];
129
+ length += produced;
130
+ if (length > opts.maxOutputLength)
131
+ throw new RangeError("Memory limit exceeded");
132
+ if (produced)
133
+ chunks.push(output.subarray(0, produced));
134
+ if (state[0] > 0)
135
+ break;
136
+ }
137
+ return Buffer.concat(chunks, length);
138
+ }
139
+ finally {
140
+ handle.close();
141
+ }
142
+ }
143
+ function sync(mode, input, options = {}) {
144
+ if (options.info) {
145
+ const engine = new constructors[mode - 1](options);
146
+ return {
147
+ buffer: engine._processChunk(typeof input === "string" ? Buffer.from(input) : input, engine._finishFlushFlag),
148
+ engine,
149
+ };
150
+ }
151
+ const { data, opts } = prepare(input, options);
152
+ return fast(mode, data, opts);
153
+ }
154
+ function callbackOperation(mode, input, options, callback) {
155
+ if (typeof options === "function") {
156
+ callback = options;
157
+ options = {};
158
+ }
159
+ if (typeof callback !== "function")
160
+ throw new ERR_INVALID_ARG_TYPE("callback", "Function", callback);
161
+ if (options.info) {
162
+ const engine = new constructors[mode - 1](options);
163
+ let result, error;
164
+ try {
165
+ result = {
166
+ buffer: engine._processChunk(typeof input === "string" ? Buffer.from(input) : input, engine._finishFlushFlag),
167
+ engine,
168
+ };
169
+ }
170
+ catch (e) {
171
+ error = e;
172
+ }
173
+ queueMicrotask(() => (error ? callback(error) : callback(null, result)));
174
+ return;
175
+ }
176
+ const { data, opts } = prepare(input, options, "zlib");
177
+ let result, error;
178
+ try {
179
+ result = fast(mode, data, opts);
180
+ }
181
+ catch (e) {
182
+ error = e;
183
+ }
184
+ queueMicrotask(() => (error ? callback(error) : callback(null, result)));
185
+ }
186
+ export const deflateSync = ((input, options) => sync(1, input, options));
187
+ export const inflateSync = ((input, options) => sync(2, input, options));
188
+ export const gzipSync = ((input, options) => sync(3, input, options));
189
+ export const gunzipSync = ((input, options) => sync(4, input, options));
190
+ export const deflateRawSync = ((input, options) => sync(5, input, options));
191
+ export const inflateRawSync = ((input, options) => sync(6, input, options));
192
+ export const unzipSync = ((input, options) => sync(7, input, options));
193
+ export const deflate = ((input, options, callback) => callbackOperation(1, input, options, callback));
194
+ export const inflate = ((input, options, callback) => callbackOperation(2, input, options, callback));
195
+ export const gzip = ((input, options, callback) => callbackOperation(3, input, options, callback));
196
+ export const gunzip = ((input, options, callback) => callbackOperation(4, input, options, callback));
197
+ export const deflateRaw = ((input, options, callback) => callbackOperation(5, input, options, callback));
198
+ export const inflateRaw = ((input, options, callback) => callbackOperation(6, input, options, callback));
199
+ export const unzip = ((input, options, callback) => callbackOperation(7, input, options, callback));
@@ -0,0 +1,33 @@
1
+ declare class CodecBinding {
2
+ private readonly kind;
3
+ private readonly encode;
4
+ private api;
5
+ private context;
6
+ private state;
7
+ private callback;
8
+ private onerror?;
9
+ private params;
10
+ private pledged?;
11
+ private finished;
12
+ private frameInProgress;
13
+ constructor(kind: "brotli" | "zstd", encode: boolean);
14
+ initialize(params: Uint32Array | Int32Array, state: Uint32Array, callback: () => void, pledged?: number): boolean;
15
+ setErrorHandler(handler: (errno: number, code: string, message: string) => void): void;
16
+ writeSync(flush: number, input: Uint8Array, inputOffset: number, inputLength: number, output: Uint8Array, outputOffset: number, outputLength: number): void;
17
+ write(...args: Parameters<CodecBinding["writeSync"]>): void;
18
+ reset(): void;
19
+ close(): void;
20
+ }
21
+ export declare class BrotliEncoder extends CodecBinding {
22
+ constructor();
23
+ }
24
+ export declare class BrotliDecoder extends CodecBinding {
25
+ constructor();
26
+ }
27
+ export declare class ZstdEncoder extends CodecBinding {
28
+ constructor();
29
+ }
30
+ export declare class ZstdDecoder extends CodecBinding {
31
+ constructor();
32
+ }
33
+ export {};
@@ -0,0 +1,141 @@
1
+ import { bindingBuffers } from "./input.js";
2
+ import { library, string } from "./native-codecs.js";
3
+ class CodecBinding {
4
+ kind;
5
+ encode;
6
+ api = library();
7
+ context = 0;
8
+ state;
9
+ callback;
10
+ onerror;
11
+ params;
12
+ pledged;
13
+ finished = false;
14
+ frameInProgress = false;
15
+ constructor(kind, encode) {
16
+ this.kind = kind;
17
+ this.encode = encode;
18
+ }
19
+ initialize(params, state, callback, pledged) {
20
+ this.finished = false;
21
+ this.frameInProgress = false;
22
+ this.params = params.slice();
23
+ this.state = state;
24
+ this.callback = callback;
25
+ this.pledged = pledged;
26
+ const api = this.api;
27
+ this.context = (this.kind === "brotli" ? api.b_create : api.z_create)(+this.encode);
28
+ if (!this.context)
29
+ return false;
30
+ for (let key = 0; key < params.length; key++)
31
+ if ((params[key] | 0) !== -1) {
32
+ const result = (this.kind === "brotli" ? api.b_param : api.z_param)(this.context, +this.encode, key, params[key]);
33
+ if (this.kind === "brotli" ? !result : api.z_is_error(result)) {
34
+ this.close();
35
+ return false;
36
+ }
37
+ }
38
+ if (pledged !== undefined && this.kind === "zstd" && this.encode) {
39
+ if (typeof pledged !== "number" ||
40
+ !Number.isFinite(pledged) ||
41
+ pledged < 0 ||
42
+ pledged >= 18446744073709551616) {
43
+ this.close();
44
+ throw new TypeError("The value cannot be converted because it is not an integer.");
45
+ }
46
+ if (api.z_is_error(api.z_size64(this.context, pledged >>> 0, Math.floor(pledged / 4294967296) >>> 0))) {
47
+ this.close();
48
+ return false;
49
+ }
50
+ }
51
+ return true;
52
+ }
53
+ setErrorHandler(handler) {
54
+ this.onerror = handler;
55
+ }
56
+ writeSync(flush, input, inputOffset, inputLength, output, outputOffset, outputLength) {
57
+ const bounds = bindingBuffers(`${this.kind === "brotli" ? "Brotli" : "Zstd"}${this.encode ? "Encoder" : "Decoder"}Context`, input, inputOffset, inputLength, output, outputOffset, outputLength);
58
+ ({ inputOffset, inputLength, outputOffset, outputLength } = bounds);
59
+ if (this.finished &&
60
+ !this.encode &&
61
+ (inputLength === 0 || this.kind === "brotli")) {
62
+ this.state[0] = outputLength;
63
+ this.state[1] = inputLength;
64
+ return;
65
+ }
66
+ const api = this.api, buffer = bounds.source;
67
+ const pointer = api.malloc(Math.max(1, inputLength) + outputLength + 8);
68
+ if (!pointer)
69
+ throw new RangeError("Memory limit exceeded");
70
+ const out = pointer + Math.max(1, inputLength), state = out + outputLength;
71
+ try {
72
+ new Uint8Array(api.memory.buffer).set(buffer.subarray(inputOffset, inputOffset + inputLength), pointer);
73
+ const result = (this.kind === "brotli" ? api.b_process : api.z_process)(this.context, +this.encode, pointer, inputLength, out, outputLength, flush, state);
74
+ this.finished = this.kind === "brotli" ? result === 1 : result === 0;
75
+ if (this.kind === "zstd" && !this.encode && inputLength > 0)
76
+ this.frameInProgress = result > 0;
77
+ const view = new DataView(api.memory.buffer);
78
+ const consumed = view.getUint32(state, true), produced = view.getUint32(state + 4, true);
79
+ this.state[0] = outputLength - produced;
80
+ this.state[1] = inputLength - consumed;
81
+ bounds.target.set(new Uint8Array(api.memory.buffer, out, produced), outputOffset);
82
+ if (this.kind === "brotli" && !result) {
83
+ const errno = this.encode ? 0 : api.b_error(this.context);
84
+ this.onerror?.(errno, this.encode
85
+ ? "ERR_BROTLI_COMPRESSION_FAILED"
86
+ : "ERR_" + string(api, api.b_error_string(errno)), "Compression failed");
87
+ }
88
+ else if (this.kind === "zstd" && api.z_is_error(result))
89
+ this.onerror?.(-1, this.encode
90
+ ? "ERR_ZSTD_COMPRESSION_FAILED"
91
+ : "ERR_ZSTD_DECOMPRESSION_FAILED", `Zstd ${this.encode ? "compression" : "decompression"} failed: ${string(api, api.z_error(result))}`);
92
+ else if (!this.encode &&
93
+ flush === 2 &&
94
+ produced < outputLength &&
95
+ (this.kind === "brotli" ? result !== 1 : this.frameInProgress)) {
96
+ if (this.kind === "zstd")
97
+ this.onerror?.(-1, "ERR_ZSTD_DECOMPRESSION_FAILED", "unexpected end of file");
98
+ else
99
+ this.onerror?.(-5, "Z_BUF_ERROR", "Unexpected end of file");
100
+ }
101
+ }
102
+ finally {
103
+ api.free(pointer);
104
+ }
105
+ }
106
+ write(...args) {
107
+ this.writeSync(...args);
108
+ this.callback();
109
+ }
110
+ reset() {
111
+ this.close();
112
+ if (!this.initialize(this.params, this.state, this.callback, this.pledged))
113
+ throw new Error("Initialization failed");
114
+ }
115
+ close() {
116
+ if (this.context) {
117
+ (this.kind === "brotli" ? this.api.b_free : this.api.z_free)(this.context, +this.encode);
118
+ this.context = 0;
119
+ }
120
+ }
121
+ }
122
+ export class BrotliEncoder extends CodecBinding {
123
+ constructor() {
124
+ super("brotli", true);
125
+ }
126
+ }
127
+ export class BrotliDecoder extends CodecBinding {
128
+ constructor() {
129
+ super("brotli", false);
130
+ }
131
+ }
132
+ export class ZstdEncoder extends CodecBinding {
133
+ constructor() {
134
+ super("zstd", true);
135
+ }
136
+ }
137
+ export class ZstdDecoder extends CodecBinding {
138
+ constructor() {
139
+ super("zstd", false);
140
+ }
141
+ }
@@ -1,55 +1,26 @@
1
- import { Buffer } from "node:buffer";
2
- import { Transform } from "node:stream";
3
- import { type Kind } from "./native-codecs.js";
4
- declare class CodecTransform extends Transform {
5
- private codec;
6
- private readonly config;
7
- bytesWritten: number;
8
- private readonly kind;
9
- private readonly encode;
10
- constructor(kind: Kind, encode: boolean, options?: any);
11
- _transform(input: any, _encoding: any, callback: any): void;
12
- _flush(callback: any): void;
13
- _destroy(error: any, callback: any): void;
14
- flush(kind?: number | (() => void), callback?: () => void): void;
15
- reset(): void;
16
- close(callback?: () => void): void;
17
- _processChunk(input: unknown, operation?: number): Buffer<ArrayBuffer>;
1
+ import type { BrotliOptions, ZstdOptions, SyncCodec, AsyncCodec } from "./public-types.js";
2
+ import { Brotli, Zstd } from "./classic-base.cjs";
3
+ export declare class BrotliCompress extends Brotli {
4
+ constructor(options?: BrotliOptions);
18
5
  }
19
- export declare class BrotliCompress extends CodecTransform {
20
- constructor(options?: any);
6
+ export declare class BrotliDecompress extends Brotli {
7
+ constructor(options?: BrotliOptions);
21
8
  }
22
- export declare class BrotliDecompress extends CodecTransform {
23
- constructor(options?: any);
9
+ export declare class ZstdCompress extends Zstd {
10
+ constructor(options?: ZstdOptions);
24
11
  }
25
- export declare class ZstdCompress extends CodecTransform {
26
- constructor(options?: any);
12
+ export declare class ZstdDecompress extends Zstd {
13
+ constructor(options?: ZstdOptions);
27
14
  }
28
- export declare class ZstdDecompress extends CodecTransform {
29
- constructor(options?: any);
30
- }
31
- export declare const createBrotliCompress: (options?: any) => BrotliCompress;
32
- export declare const createBrotliDecompress: (options?: any) => BrotliDecompress;
33
- export declare const createZstdCompress: (options?: any) => ZstdCompress;
34
- export declare const createZstdDecompress: (options?: any) => ZstdDecompress;
35
- export declare function brotliCompressSync(input: unknown, options?: any): Buffer<ArrayBuffer> | {
36
- buffer: Buffer<ArrayBuffer>;
37
- engine: BrotliCompress;
38
- };
39
- export declare function brotliDecompressSync(input: unknown, options?: any): Buffer<ArrayBuffer> | {
40
- buffer: Buffer<ArrayBuffer>;
41
- engine: BrotliCompress;
42
- };
43
- export declare function zstdCompressSync(input: unknown, options?: any): Buffer<ArrayBuffer> | {
44
- buffer: Buffer<ArrayBuffer>;
45
- engine: BrotliCompress;
46
- };
47
- export declare function zstdDecompressSync(input: unknown, options?: any): Buffer<ArrayBuffer> | {
48
- buffer: Buffer<ArrayBuffer>;
49
- engine: BrotliCompress;
50
- };
51
- export declare function brotliCompress(input: unknown, options?: any, callback?: any): void;
52
- export declare function brotliDecompress(input: unknown, options?: any, callback?: any): void;
53
- export declare function zstdCompress(input: unknown, options?: any, callback?: any): void;
54
- export declare function zstdDecompress(input: unknown, options?: any, callback?: any): void;
55
- export {};
15
+ export declare const createBrotliCompress: (options?: BrotliOptions) => BrotliCompress;
16
+ export declare const createBrotliDecompress: (options?: BrotliOptions) => BrotliDecompress;
17
+ export declare const createZstdCompress: (options?: ZstdOptions) => ZstdCompress;
18
+ export declare const createZstdDecompress: (options?: ZstdOptions) => ZstdDecompress;
19
+ export declare const brotliCompressSync: SyncCodec<BrotliOptions, BrotliCompress>;
20
+ export declare const brotliDecompressSync: SyncCodec<BrotliOptions, BrotliDecompress>;
21
+ export declare const zstdCompressSync: SyncCodec<ZstdOptions, ZstdCompress>;
22
+ export declare const zstdDecompressSync: SyncCodec<ZstdOptions, ZstdDecompress>;
23
+ export declare const brotliCompress: AsyncCodec<BrotliOptions, BrotliCompress>;
24
+ export declare const brotliDecompress: AsyncCodec<BrotliOptions, BrotliDecompress>;
25
+ export declare const zstdCompress: AsyncCodec<ZstdOptions, ZstdCompress>;
26
+ export declare const zstdDecompress: AsyncCodec<ZstdOptions, ZstdDecompress>;