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,74 @@
1
+ // Copyright (c) 2017-2022 Cloudflare, Inc.
2
+ // Licensed under the Apache 2.0 license found in the LICENSE file or at:
3
+ // https://opensource.org/licenses/Apache-2.0
4
+ //
5
+ // Copyright Joyent, Inc. and other Node contributors.
6
+ //
7
+ // Permission is hereby granted, free of charge, to any person obtaining a
8
+ // copy of this software and associated documentation files (the
9
+ // "Software"), to deal in the Software without restriction, including
10
+ // without limitation the rights to use, copy, modify, merge, publish,
11
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
12
+ // persons to whom the Software is furnished to do so, subject to the
13
+ // following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included
16
+ // in all copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
21
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT ORs
23
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ import { Buffer } from "node:buffer";
26
+ import { validateObject, validateString } from "../net/validators.js";
27
+ import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
28
+ import { ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS } from "./key-errors.js";
29
+ import { isUint8Array } from "../util/types/index.js";
30
+ export function validateExportOptions(options, type, name = "options", asymmetricKeyType) {
31
+ validateObject(options, name);
32
+ // Yes, converting to any is a bit of a cheat, but it allows us to check
33
+ // each option individually without having to do a bunch of type guards.
34
+ const opts = options;
35
+ if (opts.format !== undefined) {
36
+ validateString(opts.format, `${name}.format`);
37
+ }
38
+ else {
39
+ options.format = "buffer";
40
+ }
41
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
42
+ if ("type" in opts && opts.type !== undefined) {
43
+ validateString(opts.type, `${name}.type`);
44
+ }
45
+ if (type === "private") {
46
+ if ("type" in opts &&
47
+ opts.type === "pkcs1" &&
48
+ asymmetricKeyType !== undefined &&
49
+ asymmetricKeyType !== "rsa") {
50
+ throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "can only be used for RSA keys");
51
+ }
52
+ if ("type" in opts &&
53
+ opts.type === "sec1" &&
54
+ asymmetricKeyType !== undefined &&
55
+ asymmetricKeyType !== "ec" &&
56
+ asymmetricKeyType !== "ecdh") {
57
+ throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "can only be used for EC keys");
58
+ }
59
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
60
+ if ("cipher" in opts && opts.cipher !== undefined) {
61
+ validateString(opts.cipher, `${name}.cipher`);
62
+ if (opts.format === "der" &&
63
+ (opts.type === "pkcs1" || opts.type === "sec1")) {
64
+ throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "does not support encryption");
65
+ }
66
+ if (typeof opts.passphrase === "string") {
67
+ opts.passphrase = Buffer.from(opts.passphrase, opts.encoding);
68
+ }
69
+ if (!isUint8Array(opts.passphrase)) {
70
+ throw new ERR_INVALID_ARG_TYPE(`${name}.passphrase`, ["string", "Uint8Array"], opts.passphrase);
71
+ }
72
+ }
73
+ }
74
+ }
@@ -4,6 +4,7 @@ export interface KeyState {
4
4
  bytes?: Uint8Array;
5
5
  asymmetric?: NativeKey;
6
6
  native?: CryptoKey;
7
+ nativeAlgorithm?: KeyAlgorithm;
7
8
  }
8
9
  export declare const keyStates: WeakMap<object, KeyState>;
9
10
  export declare function isKeyObject(value: unknown): boolean;
@@ -4,39 +4,47 @@ export declare abstract class KeyObject {
4
4
  constructor();
5
5
  static from(key: CryptoKey): KeyObject;
6
6
  abstract get type(): KeyState["type"];
7
- get [Symbol.toStringTag](): string;
8
7
  export(options?: any): any;
9
8
  equals(other: KeyObject): boolean;
9
+ get [Symbol.toStringTag](): string;
10
10
  }
11
11
  export declare class SecretKeyObject extends KeyObject {
12
- get type(): "secret";
13
- export(options?: any): any;
14
12
  get symmetricKeySize(): number;
13
+ export(options?: any): any;
14
+ get type(): "secret";
15
15
  }
16
16
  declare abstract class AsymmetricKeyObject extends KeyObject {
17
- get asymmetricKeyType(): string;
18
17
  get asymmetricKeyDetails(): {
19
18
  modulusLength: number;
20
19
  publicExponent: bigint;
21
- namedCurve?: undefined;
20
+ divisorLength: undefined;
21
+ namedCurve: undefined;
22
22
  } | {
23
- modulusLength?: undefined;
24
- publicExponent?: undefined;
25
23
  namedCurve: string;
24
+ modulusLength: undefined;
25
+ publicExponent: undefined;
26
+ divisorLength: undefined;
26
27
  } | {
27
- modulusLength?: undefined;
28
- publicExponent?: undefined;
29
- namedCurve?: undefined;
28
+ modulusLength: undefined;
29
+ publicExponent: undefined;
30
+ divisorLength: undefined;
31
+ namedCurve: undefined;
32
+ } | {
33
+ modulusLength: number;
34
+ divisorLength: number;
35
+ publicExponent: undefined;
36
+ namedCurve: undefined;
30
37
  };
38
+ get asymmetricKeyType(): string;
31
39
  toCryptoKey(): void;
32
40
  }
33
41
  export declare class PublicKeyObject extends AsymmetricKeyObject {
34
- get type(): "public";
35
42
  export(options?: any): any;
43
+ get type(): "public";
36
44
  }
37
45
  export declare class PrivateKeyObject extends AsymmetricKeyObject {
38
- get type(): "private";
39
46
  export(options?: any): any;
47
+ get type(): "private";
40
48
  }
41
49
  export declare function createSecretKey(value: unknown, encoding?: BufferEncoding): SecretKeyObject;
42
50
  export declare function secretMaterial(value: unknown): any;
@@ -1,11 +1,14 @@
1
+ import { inspect } from "../util/inspect.js";
2
+ import { exportOptions, exportAsymmetric } from "./key-export.js";
3
+ import { validateExportOptions } from "./key-options.js";
1
4
  import { exportJwk } from "./key-formats.js";
2
5
  // API behavior follows workerd@5358e3d337336eaf696a48bc1563d808b10feba5
3
6
  // crypto_keys.ts (Apache-2.0 and Node MIT). Host CryptoKey export is an explicit
4
7
  // synchronous-engine boundary; adapter-created secret keys own copied bytes.
5
8
  import { Buffer } from "node:buffer";
6
9
  import { keyStates } from "./key-state.js";
7
- import { bytes, validateInteger } from "./validation.js";
8
- import { validateObject, validateString, validateOneOf, } from "../net/validators.js";
10
+ import { isAnyBuffer, validateInteger } from "./validation.js";
11
+ import { validateObject, validateOneOf } from "../net/validators.js";
9
12
  import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
10
13
  function state(value) {
11
14
  const result = keyStates.get(value);
@@ -16,6 +19,18 @@ function state(value) {
16
19
  function boundary() {
17
20
  throw Object.assign(new Error("Synchronous export of a host CryptoKey is unavailable in this WebView runtime"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
18
21
  }
22
+ const nativeAlgorithm = Object.getOwnPropertyDescriptor(globalThis.CryptoKey.prototype, "algorithm").get;
23
+ const nativeType = Object.getOwnPropertyDescriptor(globalThis.CryptoKey.prototype, "type").get;
24
+ function cloneAlgorithm(value) {
25
+ if (ArrayBuffer.isView(value))
26
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength).slice();
27
+ if (value && typeof value === "object")
28
+ return Object.fromEntries(Object.entries(value).map(([name, entry]) => [
29
+ name,
30
+ cloneAlgorithm(entry),
31
+ ]));
32
+ return value;
33
+ }
19
34
  export function create(type, value) {
20
35
  const Class = type === "secret"
21
36
  ? SecretKeyObject
@@ -33,32 +48,22 @@ export class KeyObject {
33
48
  static from(key) {
34
49
  if (!(key instanceof globalThis.CryptoKey))
35
50
  throw new ERR_INVALID_ARG_TYPE("key", "CryptoKey", key);
36
- return create(key.type, { native: key });
37
- }
38
- get [Symbol.toStringTag]() {
39
- return "KeyObject";
51
+ return create(nativeType.call(key), {
52
+ native: key,
53
+ nativeAlgorithm: cloneAlgorithm(nativeAlgorithm.call(key)),
54
+ });
40
55
  }
41
56
  export(options = {}) {
42
57
  validateObject(options, "options");
43
- if (options.format === undefined)
44
- options.format = "buffer";
45
- validateString(options.format, "options.format");
46
- if (options.type !== undefined)
47
- validateString(options.type, "options.type");
48
58
  const current = state(this);
59
+ validateExportOptions(options, current.type, "options", current.asymmetric?.type);
60
+ options = exportOptions(options);
61
+ if (options.format === undefined)
62
+ throw new TypeError("Missing format option");
49
63
  if (current.asymmetric) {
50
64
  if (options.format === "jwk")
51
65
  return exportJwk(current.asymmetric, current.type === "private");
52
- validateOneOf(options.format, "options.format", ["pem", "der"]);
53
- validateOneOf(options.type, "options.type", current.type === "private"
54
- ? ["pkcs1", "pkcs8", "sec1"]
55
- : ["pkcs1", "spki"]);
56
- if (options.cipher !== undefined)
57
- validateString(options.cipher, "options.cipher");
58
- const passphrase = options.passphrase === undefined
59
- ? undefined
60
- : bytes(options.passphrase, "options.passphrase");
61
- const result = current.asymmetric.export(current.type === "private", options.format === "pem", options.type, options.cipher, passphrase);
66
+ const result = exportAsymmetric(current.asymmetric, current.type === "private", options);
62
67
  return options.format === "pem"
63
68
  ? new TextDecoder().decode(result)
64
69
  : Buffer.from(result);
@@ -101,75 +106,139 @@ export class KeyObject {
101
106
  difference |= a.bytes[i] ^ b.bytes[i];
102
107
  return difference === 0;
103
108
  }
109
+ get [Symbol.toStringTag]() {
110
+ return "KeyObject";
111
+ }
104
112
  }
105
113
  export class SecretKeyObject extends KeyObject {
106
- get type() {
107
- return "secret";
114
+ get symmetricKeySize() {
115
+ const key = state(this);
116
+ return ((key.bytes?.length ?? key.native?.algorithm?.length ?? 0) | 0);
108
117
  }
109
118
  export(options) {
110
119
  return super.export(options);
111
120
  }
112
- get symmetricKeySize() {
113
- const key = state(this);
114
- return (key.bytes?.length ?? (key.native?.algorithm?.length ?? 0) | 0);
121
+ get type() {
122
+ return "secret";
115
123
  }
116
- [Symbol.for("nodejs.util.inspect.custom")]() {
117
- return `SecretKeyObject { size: ${this.symmetricKeySize} }`;
124
+ [Symbol.for("nodejs.util.inspect.custom")](depth, options) {
125
+ if (depth < 0)
126
+ return this;
127
+ const opts = {
128
+ ...options,
129
+ depth: options.depth == null ? null : options.depth - 1,
130
+ };
131
+ return `${this.constructor.name} ${inspect({ size: this.symmetricKeySize }, opts)}`;
118
132
  }
119
133
  }
120
134
  class AsymmetricKeyObject extends KeyObject {
135
+ get asymmetricKeyDetails() {
136
+ const details = (() => {
137
+ const current = state(this), key = current.asymmetric;
138
+ if (!key) {
139
+ const algorithm = current.nativeAlgorithm;
140
+ if (!algorithm)
141
+ return boundary();
142
+ if ("modulusLength" in algorithm) {
143
+ let publicExponent = 0n;
144
+ for (const value of algorithm.publicExponent)
145
+ publicExponent = (publicExponent << 8n) | BigInt(value);
146
+ return { modulusLength: algorithm.modulusLength, publicExponent };
147
+ }
148
+ if ("namedCurve" in algorithm)
149
+ return {
150
+ namedCurve: {
151
+ "P-256": "prime256v1",
152
+ "P-384": "secp384r1",
153
+ "P-521": "secp521r1",
154
+ }[algorithm.namedCurve] ?? algorithm.namedCurve,
155
+ };
156
+ return {};
157
+ }
158
+ if (key.type === "rsa")
159
+ return {
160
+ modulusLength: key.bits,
161
+ publicExponent: BigInt("0x" + Buffer.from(key.parameter("e")).toString("hex")),
162
+ };
163
+ if (key.type === "ec")
164
+ return { namedCurve: key.group };
165
+ if (key.type === "dsa") {
166
+ const q = key.parameter("q");
167
+ return {
168
+ modulusLength: key.bits,
169
+ divisorLength: q.length
170
+ ? (q.length - 1) * 8 + 32 - Math.clz32(q[0])
171
+ : 0,
172
+ };
173
+ }
174
+ return {};
175
+ })();
176
+ return {
177
+ modulusLength: undefined,
178
+ publicExponent: undefined,
179
+ divisorLength: undefined,
180
+ namedCurve: undefined,
181
+ ...details,
182
+ };
183
+ }
121
184
  get asymmetricKeyType() {
122
- const key = state(this).asymmetric;
123
- if (!key)
124
- return boundary();
185
+ const current = state(this), key = current.asymmetric;
186
+ if (!key) {
187
+ const name = current.nativeAlgorithm?.name;
188
+ if (name === undefined)
189
+ return boundary();
190
+ return ({
191
+ "RSASSA-PKCS1-v1_5": "rsa",
192
+ "RSA-PSS": "rsa",
193
+ "RSA-OAEP": "rsa",
194
+ ECDSA: "ec",
195
+ ECDH: "ecdh",
196
+ Ed25519: "ed25519",
197
+ "NODE-ED25519": "ed25519",
198
+ X25519: "x25519",
199
+ }[name] ?? name);
200
+ }
125
201
  return key.type;
126
202
  }
127
- get asymmetricKeyDetails() {
128
- const key = state(this).asymmetric;
129
- if (!key)
130
- return boundary();
131
- if (key.type === "rsa")
132
- return {
133
- modulusLength: key.bits,
134
- publicExponent: BigInt("0x" + Buffer.from(key.parameter("e")).toString("hex")),
135
- };
136
- if (key.type === "ec")
137
- return { namedCurve: key.group };
138
- return {};
139
- }
140
203
  toCryptoKey() {
141
204
  throw Object.assign(new Error("The toCryptoKey method is not implemented"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
142
205
  }
206
+ [Symbol.for("nodejs.util.inspect.custom")](depth, options) {
207
+ if (depth < 0)
208
+ return this;
209
+ const opts = {
210
+ ...options,
211
+ depth: options.depth == null ? null : options.depth - 1,
212
+ };
213
+ return `${this.constructor.name} ${inspect({ type: this.asymmetricKeyType, details: this.asymmetricKeyDetails }, opts)}`;
214
+ }
143
215
  }
144
216
  export class PublicKeyObject extends AsymmetricKeyObject {
217
+ export(options) {
218
+ return super.export(options);
219
+ }
145
220
  get type() {
146
221
  return "public";
147
222
  }
223
+ }
224
+ export class PrivateKeyObject extends AsymmetricKeyObject {
148
225
  export(options) {
149
226
  return super.export(options);
150
227
  }
151
- }
152
- export class PrivateKeyObject extends AsymmetricKeyObject {
153
228
  get type() {
154
229
  return "private";
155
230
  }
156
- export(options) {
157
- return super.export(options);
158
- }
159
231
  }
160
232
  export function createSecretKey(value, encoding) {
161
233
  if (typeof value !== "string" &&
162
- !(value instanceof ArrayBuffer) &&
163
- !ArrayBuffer.isView(value) &&
164
- !(typeof SharedArrayBuffer !== "undefined" &&
165
- value instanceof SharedArrayBuffer))
234
+ !isAnyBuffer(value) &&
235
+ !ArrayBuffer.isView(value))
166
236
  throw new ERR_INVALID_ARG_TYPE("key", ["string", "ArrayBuffer", "TypedArray", "DataView"], value);
167
237
  const data = typeof value === "string"
168
238
  ? Buffer.from(value, encoding)
169
- : typeof SharedArrayBuffer !== "undefined" &&
170
- value instanceof SharedArrayBuffer
239
+ : isAnyBuffer(value)
171
240
  ? new Uint8Array(value)
172
- : bytes(value, "key");
241
+ : new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
173
242
  return create("secret", { bytes: new Uint8Array(data) });
174
243
  }
175
244
  export function secretMaterial(value) {
@@ -1,4 +1,13 @@
1
+ export declare function getHashes(): string[];
1
2
  export declare function getCurves(): string[];
2
3
  export declare function secureHeapUsed(): Record<string, number>;
3
4
  export declare function setEngine(_engine: string, _flags?: number): never;
4
5
  export declare function setFips(_enabled: boolean): never;
6
+ export declare const fips = false;
7
+ export declare function getFips(): boolean;
8
+ export { getCipherInfo } from "./cipher-info.js";
9
+ export declare function getCiphers(): string[];
10
+ export declare function Cipher(): never;
11
+ export declare function Decipher(): never;
12
+ export declare function createCipher(): never;
13
+ export declare function createDecipher(): never;
@@ -1,3 +1,7 @@
1
+ import { hashNames } from "./hash-handle.js";
2
+ export function getHashes() {
3
+ return [...hashNames];
4
+ }
1
5
  // Copyright (c) 2017-2022 Cloudflare, Inc.
2
6
  // Licensed under Apache-2.0: https://opensource.org/licenses/Apache-2.0
3
7
  // Adapted from workerd@5358e3d337336eaf696a48bc1563d808b10feba5.
@@ -18,5 +22,88 @@ export function setEngine(_engine, _flags) {
18
22
  export function setFips(_enabled) {
19
23
  return unavailable("setFips");
20
24
  }
21
- // Do not copy Workers' fips=true: this adapter uses JavaScript algorithms,
22
- // not Workers' FIPS backend. That capability remains an explicit boundary.
25
+ // The owned backends do not run in a validated FIPS mode. Report the actual
26
+ // capability rather than copying Workers' true value.
27
+ export const fips = false;
28
+ export function getFips() {
29
+ return fips;
30
+ }
31
+ export { getCipherInfo } from "./cipher-info.js";
32
+ // Includes the duplicate GCM aliases exposed by the pinned backend.
33
+ const cipherNames = [
34
+ "AES-128-CBC",
35
+ "AES-192-CBC",
36
+ "AES-256-CBC",
37
+ "AES-128-CTR",
38
+ "AES-192-CTR",
39
+ "AES-256-CTR",
40
+ "AES-128-ECB",
41
+ "AES-192-ECB",
42
+ "AES-256-ECB",
43
+ "AES-128-OFB",
44
+ "AES-192-OFB",
45
+ "AES-256-OFB",
46
+ "AES-128-GCM",
47
+ "AES-192-GCM",
48
+ "AES-256-GCM",
49
+ "DES-CBC",
50
+ "DES-ECB",
51
+ "DES-EDE",
52
+ "DES-EDE-CBC",
53
+ "DES-EDE3-CBC",
54
+ "RC2-CBC",
55
+ "RC4",
56
+ "aes-128-cbc",
57
+ "aes-192-cbc",
58
+ "aes-256-cbc",
59
+ "aes-128-ctr",
60
+ "aes-192-ctr",
61
+ "aes-256-ctr",
62
+ "aes-128-ecb",
63
+ "aes-192-ecb",
64
+ "aes-256-ecb",
65
+ "aes-128-ofb",
66
+ "aes-192-ofb",
67
+ "aes-256-ofb",
68
+ "aes-128-gcm",
69
+ "aes-192-gcm",
70
+ "aes-256-gcm",
71
+ "des-cbc",
72
+ "des-ecb",
73
+ "des-ede",
74
+ "des-ede-cbc",
75
+ "des-ede3-cbc",
76
+ "rc2-cbc",
77
+ "rc4",
78
+ "aes-256-eax",
79
+ "aes-128-ccm-matter",
80
+ "aes-128-ccm-bluetooth-8",
81
+ "aes-128-gcm-randnonce",
82
+ "aes-256-gcm-randnonce",
83
+ "aes-128-gcm-siv",
84
+ "aes-128-ccm-bluetooth",
85
+ "aes-256-ctr-hmac-sha256",
86
+ "aes-128-ctr-hmac-sha256",
87
+ "xchacha20-poly1305",
88
+ "aes-256-gcm-siv",
89
+ "aes-128-eax",
90
+ "chacha20-poly1305",
91
+ "aes-192-gcm",
92
+ "aes-256-gcm",
93
+ "aes-128-gcm",
94
+ ];
95
+ export function getCiphers() {
96
+ return [...cipherNames];
97
+ }
98
+ export function Cipher() {
99
+ return unavailable("Cipher");
100
+ }
101
+ export function Decipher() {
102
+ return unavailable("Decipher");
103
+ }
104
+ export function createCipher() {
105
+ return unavailable("createCipher");
106
+ }
107
+ export function createDecipher() {
108
+ return unavailable("createDecipher");
109
+ }
@@ -0,0 +1,17 @@
1
+ type API = Record<string, (...args: any[]) => any> & {
2
+ memory: WebAssembly.Memory;
3
+ };
4
+ export declare function library(): API;
5
+ export declare function inputs<T>(values: (string | Uint8Array | number)[], operation: (api: API, pointers: number[]) => T): T;
6
+ export declare function output(api: API, pointer: number, length: number): Uint8Array;
7
+ export declare function size(api: API, pointer: number): number;
8
+ export declare function validateEcPrivate(name: string, value: Uint8Array): void;
9
+ export declare function decodeKey(data: Uint8Array, pem: boolean, privateKey: boolean, type: string | undefined, password?: Uint8Array): {
10
+ data: Uint8Array;
11
+ privateKey: boolean;
12
+ };
13
+ export declare function curveName(name: string): string;
14
+ export declare function generateDh(prime: Uint8Array, generator: number): Uint8Array;
15
+ export declare function deriveDh(prime: Uint8Array, generator: Uint8Array, privateKey: Uint8Array, publicKey: Uint8Array): Uint8Array;
16
+ export declare function encodeKey(data: Uint8Array, privateKey: boolean, pem: boolean, type: string, cipher?: string, password?: Uint8Array, ecEncoding?: number): Uint8Array;
17
+ export {};