cf-workers-native 0.1.4 → 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 (296) hide show
  1. package/README.md +2 -2
  2. package/dist/browser/documents/page-source.js +1 -1
  3. package/dist/browser/fetch/host-call.js +6 -1
  4. package/dist/browser/fetch/native-network.d.ts +1 -1
  5. package/dist/browser/fetch/relay.js +2 -0
  6. package/dist/browser/headers/index.d.ts +2 -0
  7. package/dist/browser/headers/index.js +5 -0
  8. package/dist/browser/isolated/transport.js +3 -2
  9. package/dist/browser/request/index.js +3 -2
  10. package/dist/browser/response/index.d.ts +2 -0
  11. package/dist/browser/response/index.js +11 -3
  12. package/dist/browser/structured-clone/index.d.ts +3 -0
  13. package/dist/browser/structured-clone/index.js +75 -7
  14. package/dist/browser/websocket/attachment-size.d.ts +3 -0
  15. package/dist/browser/websocket/attachment-size.js +336 -0
  16. package/dist/browser/websocket/brand.d.ts +4 -0
  17. package/dist/browser/websocket/brand.js +16 -0
  18. package/dist/browser/websocket/close-event.d.ts +7 -0
  19. package/dist/browser/websocket/close-event.js +30 -0
  20. package/dist/browser/websocket/compression.d.ts +5 -0
  21. package/dist/browser/websocket/compression.js +124 -0
  22. package/dist/browser/websocket/conversion.d.ts +2 -0
  23. package/dist/browser/websocket/conversion.js +13 -0
  24. package/dist/browser/websocket/endpoint.d.ts +39 -0
  25. package/dist/browser/websocket/endpoint.js +379 -0
  26. package/dist/browser/websocket/frames.d.ts +21 -0
  27. package/dist/browser/websocket/frames.js +121 -0
  28. package/dist/browser/websocket/native.d.ts +14 -0
  29. package/dist/browser/websocket/native.js +226 -0
  30. package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
  31. package/dist/cloudflare/workers/worker-runtime.js +2 -1
  32. package/dist/compat/console/index.d.ts +35 -0
  33. package/dist/compat/console/index.js +92 -0
  34. package/dist/compat/crypto/LICENSE-3RD-PARTY.md +39 -0
  35. package/dist/compat/crypto/LICENSE-APACHE +177 -0
  36. package/dist/compat/crypto/LICENSE.boringssl +238 -0
  37. package/dist/compat/crypto/LICENSE.emscripten +102 -0
  38. package/dist/compat/crypto/LICENSE.musl +193 -0
  39. package/dist/compat/crypto/LICENSE.ncrypto +21 -0
  40. package/dist/compat/crypto/LICENSE.openssl +177 -0
  41. package/dist/compat/crypto/asymmetric.d.ts +13 -0
  42. package/dist/compat/crypto/asymmetric.js +312 -0
  43. package/dist/compat/crypto/brorand.cjs +74 -0
  44. package/dist/compat/crypto/brorand.d.cts +6 -0
  45. package/dist/compat/crypto/certificate.d.ts +14 -0
  46. package/dist/compat/crypto/certificate.js +66 -0
  47. package/dist/compat/crypto/cipher-handle.d.ts +27 -0
  48. package/dist/compat/crypto/cipher-handle.js +239 -0
  49. package/dist/compat/crypto/cipher-info.d.ts +12 -0
  50. package/dist/compat/crypto/cipher-info.js +324 -0
  51. package/dist/compat/crypto/constants.d.ts +62 -0
  52. package/dist/compat/crypto/constants.js +68 -0
  53. package/dist/compat/crypto/dh-handle.d.ts +17 -0
  54. package/dist/compat/crypto/dh-handle.js +180 -0
  55. package/dist/compat/crypto/dh.d.ts +53 -0
  56. package/dist/compat/crypto/dh.js +138 -0
  57. package/dist/compat/crypto/digest-stream.d.ts +15 -0
  58. package/dist/compat/crypto/digest-stream.js +213 -0
  59. package/dist/compat/crypto/digests.d.ts +3 -0
  60. package/dist/compat/crypto/digests.js +32 -0
  61. package/dist/compat/crypto/ecdh.d.ts +25 -0
  62. package/dist/compat/crypto/ecdh.js +109 -0
  63. package/dist/compat/crypto/hash-handle.d.ts +23 -0
  64. package/dist/compat/crypto/hash-handle.js +70 -0
  65. package/dist/compat/crypto/hash.d.ts +37 -0
  66. package/dist/compat/crypto/hash.js +218 -0
  67. package/dist/compat/crypto/hkdf.d.ts +2 -0
  68. package/dist/compat/crypto/hkdf.js +78 -0
  69. package/dist/compat/crypto/index.cjs +2 -0
  70. package/dist/compat/crypto/index.d.cts +169 -0
  71. package/dist/compat/crypto/index.d.ts +213 -0
  72. package/dist/compat/crypto/index.js +96 -0
  73. package/dist/compat/crypto/key-errors.d.ts +10 -0
  74. package/dist/compat/crypto/key-errors.js +17 -0
  75. package/dist/compat/crypto/key-export.d.ts +8 -0
  76. package/dist/compat/crypto/key-export.js +49 -0
  77. package/dist/compat/crypto/key-formats.d.ts +7 -0
  78. package/dist/compat/crypto/key-formats.js +251 -0
  79. package/dist/compat/crypto/key-options.d.ts +1 -0
  80. package/dist/compat/crypto/key-options.js +74 -0
  81. package/dist/compat/crypto/key-state.d.ts +10 -0
  82. package/dist/compat/crypto/key-state.js +4 -0
  83. package/dist/compat/crypto/keys.d.ts +58 -0
  84. package/dist/compat/crypto/keys.js +278 -0
  85. package/dist/compat/crypto/metadata.d.ts +13 -0
  86. package/dist/compat/crypto/metadata.js +109 -0
  87. package/dist/compat/crypto/native-cipher.d.ts +17 -0
  88. package/dist/compat/crypto/native-cipher.js +192 -0
  89. package/dist/compat/crypto/native-crypto.d.ts +81 -0
  90. package/dist/compat/crypto/native-crypto.js +681 -0
  91. package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
  92. package/dist/compat/crypto/pbkdf2-handle.js +26 -0
  93. package/dist/compat/crypto/pbkdf2.d.ts +5 -0
  94. package/dist/compat/crypto/pbkdf2.js +60 -0
  95. package/dist/compat/crypto/prime.d.ts +19 -0
  96. package/dist/compat/crypto/prime.js +146 -0
  97. package/dist/compat/crypto/random.d.ts +18 -0
  98. package/dist/compat/crypto/random.js +203 -0
  99. package/dist/compat/crypto/rsa.d.ts +21 -0
  100. package/dist/compat/crypto/rsa.js +177 -0
  101. package/dist/compat/crypto/scrypt.d.ts +25 -0
  102. package/dist/compat/crypto/scrypt.js +152 -0
  103. package/dist/compat/crypto/sign-errors.d.ts +8 -0
  104. package/dist/compat/crypto/sign-errors.js +13 -0
  105. package/dist/compat/crypto/sign-handle.d.ts +22 -0
  106. package/dist/compat/crypto/sign-handle.js +149 -0
  107. package/dist/compat/crypto/signatures.d.ts +34 -0
  108. package/dist/compat/crypto/signatures.js +252 -0
  109. package/dist/compat/crypto/subtle-digest.d.ts +1 -0
  110. package/dist/compat/crypto/subtle-digest.js +56 -0
  111. package/dist/compat/crypto/symmetric.d.ts +45 -0
  112. package/dist/compat/crypto/symmetric.js +239 -0
  113. package/dist/compat/crypto/validation.d.ts +7 -0
  114. package/dist/compat/crypto/validation.js +46 -0
  115. package/dist/compat/crypto/webcrypto.d.ts +13 -0
  116. package/dist/compat/crypto/webcrypto.js +51 -0
  117. package/dist/compat/crypto/x509.d.ts +30 -0
  118. package/dist/compat/crypto/x509.js +407 -0
  119. package/dist/compat/events/errors.d.ts +8 -0
  120. package/dist/compat/events/errors.js +39 -0
  121. package/dist/compat/events/index.cjs +2 -0
  122. package/dist/compat/events/index.d.cts +2 -0
  123. package/dist/compat/events/index.d.ts +64 -0
  124. package/dist/compat/events/index.js +792 -0
  125. package/dist/compat/events/intrinsics.d.ts +1 -0
  126. package/dist/compat/events/intrinsics.js +2 -0
  127. package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
  128. package/dist/compat/fs/LICENSE-APACHE +177 -0
  129. package/dist/compat/fs/directory-backend.d.ts +6 -0
  130. package/dist/compat/fs/directory-backend.js +51 -0
  131. package/dist/compat/fs/directory.d.ts +37 -0
  132. package/dist/compat/fs/directory.js +137 -0
  133. package/dist/compat/fs/embedded.d.ts +85 -0
  134. package/dist/compat/fs/embedded.js +260 -0
  135. package/dist/compat/fs/file-handle.d.ts +75 -0
  136. package/dist/compat/fs/file-handle.js +226 -0
  137. package/dist/compat/fs/glob-internal.d.ts +15 -0
  138. package/dist/compat/fs/glob-internal.js +417 -0
  139. package/dist/compat/fs/glob.d.ts +10 -0
  140. package/dist/compat/fs/glob.js +71 -0
  141. package/dist/compat/fs/index.d.ts +8 -82
  142. package/dist/compat/fs/index.js +736 -238
  143. package/dist/compat/fs/io-validation.d.ts +14 -0
  144. package/dist/compat/fs/io-validation.js +130 -0
  145. package/dist/compat/fs/io.d.ts +1 -0
  146. package/dist/compat/fs/io.js +282 -0
  147. package/dist/compat/fs/mode.d.ts +1 -0
  148. package/dist/compat/fs/mode.js +13 -0
  149. package/dist/compat/fs/promises/index.d.ts +3 -1
  150. package/dist/compat/fs/promises/index.js +1 -1
  151. package/dist/compat/fs/public-types.d.ts +120 -0
  152. package/dist/compat/fs/public-types.js +1 -0
  153. package/dist/compat/fs/stream-errors.d.ts +6 -0
  154. package/dist/compat/fs/stream-errors.js +8 -0
  155. package/dist/compat/fs/stream-utils.d.ts +16 -0
  156. package/dist/compat/fs/stream-utils.js +29 -0
  157. package/dist/compat/fs/streams.d.ts +104 -0
  158. package/dist/compat/fs/streams.js +794 -0
  159. package/dist/compat/fs/writable.d.ts +14 -0
  160. package/dist/compat/fs/writable.js +532 -0
  161. package/dist/compat/http/index.d.ts +11 -28
  162. package/dist/compat/http/index.js +81 -125
  163. package/dist/compat/http/server-options.d.ts +1 -0
  164. package/dist/compat/http/server-options.js +87 -0
  165. package/dist/compat/http/symbols.d.ts +5 -0
  166. package/dist/compat/http/symbols.js +9 -0
  167. package/dist/compat/http/unique-headers.d.ts +1 -0
  168. package/dist/compat/http/unique-headers.js +21 -0
  169. package/dist/compat/http-common/index.d.ts +37 -0
  170. package/dist/compat/http-common/index.js +43 -0
  171. package/dist/compat/http-outgoing/index.d.ts +14 -0
  172. package/dist/compat/http-outgoing/index.js +17 -0
  173. package/dist/compat/http-server/index.d.ts +18 -0
  174. package/dist/compat/http-server/index.js +26 -0
  175. package/dist/compat/https/index.d.ts +4 -2
  176. package/dist/compat/module/builtins.d.ts +1 -0
  177. package/dist/compat/module/builtins.js +101 -0
  178. package/dist/compat/module/index.d.ts +4 -2
  179. package/dist/compat/module/index.js +22 -17
  180. package/dist/compat/module/metadata.d.ts +44 -0
  181. package/dist/compat/module/metadata.js +127 -0
  182. package/dist/compat/net/internal-net.js +4 -0
  183. package/dist/compat/net/sockets.d.ts +5 -0
  184. package/dist/compat/net/sockets.js +153 -14
  185. package/dist/compat/net/validators.d.ts +1 -0
  186. package/dist/compat/net/validators.js +8 -0
  187. package/dist/compat/process/configuration.d.ts +113 -0
  188. package/dist/compat/process/configuration.js +115 -0
  189. package/dist/compat/process/directory.d.ts +4 -0
  190. package/dist/compat/process/directory.js +28 -0
  191. package/dist/compat/process/files.d.ts +3 -0
  192. package/dist/compat/process/files.js +10 -0
  193. package/dist/compat/process/index.cjs +2 -0
  194. package/dist/compat/process/index.d.cts +4 -0
  195. package/dist/compat/process/index.d.ts +20 -0
  196. package/dist/compat/process/index.js +113 -0
  197. package/dist/compat/process/metadata.d.ts +135 -0
  198. package/dist/compat/process/metadata.js +251 -0
  199. package/dist/compat/process/scheduling.d.ts +2 -0
  200. package/dist/compat/process/scheduling.js +60 -0
  201. package/dist/compat/process/state.d.ts +4 -0
  202. package/dist/compat/process/state.js +5 -0
  203. package/dist/compat/process/stdio.d.ts +18 -0
  204. package/dist/compat/process/stdio.js +66 -0
  205. package/dist/compat/stream/destroy.d.ts +1 -0
  206. package/dist/compat/stream/destroy.js +21 -1
  207. package/dist/compat/stream/readable.js +10 -2
  208. package/dist/compat/stream-web/LICENSE +22 -0
  209. package/dist/compat/stream-web/ponyfill.d.ts +1 -1
  210. package/dist/compat/stream-web/ponyfill.js +2369 -1775
  211. package/dist/compat/stream-web/runtime.d.ts +1 -1
  212. package/dist/compat/stream-web/runtime.js +3 -2
  213. package/dist/compat/stream-wrap/LICENSE-3RD-PARTY.md +11 -0
  214. package/dist/compat/stream-wrap/LICENSE-APACHE +177 -0
  215. package/dist/compat/stream-wrap/index.d.ts +1 -0
  216. package/dist/compat/stream-wrap/index.js +1 -0
  217. package/dist/compat/tls/LICENSE-3RD-PARTY.md +11 -0
  218. package/dist/compat/tls/LICENSE-APACHE +177 -0
  219. package/dist/compat/tls/constants.d.ts +7 -0
  220. package/dist/compat/tls/constants.js +14 -0
  221. package/dist/compat/tls/errors.d.ts +35 -0
  222. package/dist/compat/tls/errors.js +68 -0
  223. package/dist/compat/tls/helpers.d.ts +9 -0
  224. package/dist/compat/tls/helpers.js +207 -0
  225. package/dist/compat/tls/index.d.ts +26 -0
  226. package/dist/compat/tls/index.js +50 -0
  227. package/dist/compat/tls/jsstream.d.ts +19 -0
  228. package/dist/compat/tls/jsstream.js +196 -0
  229. package/dist/compat/tls/native-session.d.ts +3 -0
  230. package/dist/compat/tls/native-session.js +187 -0
  231. package/dist/compat/tls/wrap.d.ts +65 -0
  232. package/dist/compat/tls/wrap.js +541 -0
  233. package/dist/compat/tls-wrap/LICENSE-3RD-PARTY.md +11 -0
  234. package/dist/compat/tls-wrap/LICENSE-APACHE +177 -0
  235. package/dist/compat/tls-wrap/index.d.ts +7 -0
  236. package/dist/compat/tls-wrap/index.js +3 -0
  237. package/dist/compat/util/index.js +43 -1
  238. package/dist/compat/util/inspect.js +5 -1
  239. package/dist/compat/util/types/index.d.ts +3 -0
  240. package/dist/compat/util/types/index.js +5 -0
  241. package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
  242. package/dist/compat/zlib/LICENSE-APACHE +177 -0
  243. package/dist/compat/zlib/LICENSE.brotli +19 -0
  244. package/dist/compat/zlib/LICENSE.emscripten +102 -0
  245. package/dist/compat/zlib/LICENSE.musl +193 -0
  246. package/dist/compat/zlib/LICENSE.zlib +22 -0
  247. package/dist/compat/zlib/LICENSE.zstd +30 -0
  248. package/dist/compat/zlib/classic-base.cjs +600 -0
  249. package/dist/compat/zlib/classic-base.d.cts +39 -0
  250. package/dist/compat/zlib/classic-binding.d.ts +28 -0
  251. package/dist/compat/zlib/classic-binding.js +105 -0
  252. package/dist/compat/zlib/classic.d.ts +44 -0
  253. package/dist/compat/zlib/classic.js +199 -0
  254. package/dist/compat/zlib/codec-binding.d.ts +33 -0
  255. package/dist/compat/zlib/codec-binding.js +141 -0
  256. package/dist/compat/zlib/compression.d.ts +26 -0
  257. package/dist/compat/zlib/compression.js +170 -0
  258. package/dist/compat/zlib/constants.d.ts +172 -0
  259. package/dist/compat/zlib/constants.js +173 -0
  260. package/dist/compat/zlib/errors.d.ts +21 -0
  261. package/dist/compat/zlib/errors.js +32 -0
  262. package/dist/compat/zlib/index.d.ts +544 -0
  263. package/dist/compat/zlib/index.js +194 -0
  264. package/dist/compat/zlib/input.d.ts +16 -0
  265. package/dist/compat/zlib/input.js +132 -0
  266. package/dist/compat/zlib/native-codecs.d.ts +24 -0
  267. package/dist/compat/zlib/native-codecs.js +180 -0
  268. package/dist/compat/zlib/native-zlib.d.ts +5 -0
  269. package/dist/compat/zlib/native-zlib.js +52 -0
  270. package/dist/compat/zlib/public-types.d.ts +48 -0
  271. package/dist/compat/zlib/public-types.js +1 -0
  272. package/dist/compat/zlib/validators.d.ts +3 -0
  273. package/dist/compat/zlib/validators.js +18 -0
  274. package/dist/shared/buffer-source.d.ts +5 -0
  275. package/dist/shared/buffer-source.js +70 -0
  276. package/dist/shared/globals/inject.d.ts +2 -1
  277. package/dist/shared/globals/inject.js +2 -1
  278. package/dist/shared/globals/platform.d.ts +8 -0
  279. package/dist/shared/globals/platform.js +1 -0
  280. package/dist/shared/globals/runtime.d.ts +1 -2
  281. package/dist/shared/globals/runtime.js +1 -2
  282. package/dist/shared/globals/web.js +5 -0
  283. package/dist/shared/request-context.d.ts +2 -0
  284. package/dist/shared/request-context.js +11 -0
  285. package/dist/shared/request-scope.d.ts +4 -0
  286. package/dist/shared/request-scope.js +14 -0
  287. package/dist/tooling/async.js +36 -14
  288. package/dist/tooling/shared.d.ts +1 -0
  289. package/dist/tooling/shared.js +26 -1
  290. package/dist/tooling/vite.js +8 -3
  291. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +92 -13
  292. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +4 -2
  293. package/native/ios/NetworkBridge.swift +11 -1
  294. package/native/ios/SocketBridge.swift +37 -2
  295. package/native/ios/WorkersCapacitorPlugin.swift +4 -0
  296. package/package.json +4 -2
@@ -0,0 +1,109 @@
1
+ // @ts-nocheck
2
+ // Ported from pinned workerd crypto_dh.ts; see LICENSE-APACHE and LICENSE-3RD-PARTY.md.
3
+ import { Buffer } from "node:buffer";
4
+ import { NativeECDH } from "./native-crypto.js";
5
+ import { validateString, validateOneOf } from "../net/validators.js";
6
+ import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
7
+ import { isArrayBufferView } from "../util/types/index.js";
8
+ const cryptoImpl = { ECDHHandle: NativeECDH };
9
+ const kHandle = Symbol("kHandle");
10
+ export const ECDH = function (curveName) {
11
+ if (!(this instanceof ECDH)) {
12
+ return new ECDH(curveName);
13
+ }
14
+ validateString(curveName, "curveName");
15
+ this[kHandle] = new cryptoImpl.ECDHHandle(curveName);
16
+ return this;
17
+ };
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
19
+ ECDH.prototype.computeSecret = function (otherPublicKey, inputEncoding, outputEncoding) {
20
+ if (typeof otherPublicKey === "string") {
21
+ otherPublicKey = Buffer.from(otherPublicKey, inputEncoding);
22
+ }
23
+ if (!isArrayBufferView(otherPublicKey)) {
24
+ throw new ERR_INVALID_ARG_TYPE("otherPublicKey", ["string", "Buffer", "TypedArray", "DataView"], otherPublicKey);
25
+ }
26
+ if (inputEncoding != null) {
27
+ validateString(inputEncoding, "inputEncoding");
28
+ }
29
+ if (outputEncoding != null) {
30
+ validateString(outputEncoding, "outputEncoding");
31
+ }
32
+ const ret = this[kHandle].computeSecret(otherPublicKey);
33
+ if (typeof outputEncoding === "string") {
34
+ return Buffer.from(ret).toString(outputEncoding);
35
+ }
36
+ return Buffer.from(ret);
37
+ };
38
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
39
+ ECDH.prototype.generateKeys = function (encoding, format) {
40
+ if (encoding != null) {
41
+ validateString(encoding, "encoding");
42
+ }
43
+ if (format != null) {
44
+ validateOneOf(format, "format", ["compressed", "uncompressed", "hybrid"]);
45
+ }
46
+ this[kHandle].generateKeys();
47
+ return this.getPublicKey(encoding, format);
48
+ };
49
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
50
+ ECDH.prototype.getPrivateKey = function (encoding) {
51
+ if (encoding != null) {
52
+ validateString(encoding, "encoding");
53
+ }
54
+ const pvt = this[kHandle].getPrivateKey();
55
+ if (typeof encoding === "string") {
56
+ return Buffer.from(pvt).toString(encoding);
57
+ }
58
+ return Buffer.from(pvt);
59
+ };
60
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
61
+ ECDH.prototype.getPublicKey = function (encoding, format = "uncompressed") {
62
+ if (encoding != null) {
63
+ validateString(encoding, "encoding");
64
+ }
65
+ validateOneOf(format, "format", ["compressed", "uncompressed", "hybrid"]);
66
+ const pub = this[kHandle].getPublicKey(format);
67
+ if (typeof encoding === "string") {
68
+ return Buffer.from(pub).toString(encoding);
69
+ }
70
+ return Buffer.from(pub);
71
+ };
72
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
73
+ ECDH.prototype.setPrivateKey = function (key, encoding) {
74
+ if (encoding != null) {
75
+ validateString(encoding, "encoding");
76
+ }
77
+ if (typeof key === "string") {
78
+ key = Buffer.from(key, encoding);
79
+ }
80
+ if (!isArrayBufferView(key)) {
81
+ throw new ERR_INVALID_ARG_TYPE("key", ["string", "Buffer", "TypedArray", "DataView"], key);
82
+ }
83
+ this[kHandle].setPrivateKey(key);
84
+ };
85
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access
86
+ ECDH.convertKey = function (key, curve, inputEncoding, outputEncoding, format = "uncompressed") {
87
+ if (typeof key === "string") {
88
+ key = Buffer.from(key, inputEncoding);
89
+ }
90
+ if (!isArrayBufferView(key)) {
91
+ throw new ERR_INVALID_ARG_TYPE("key", ["string", "Buffer", "TypedArray", "DataView"], key);
92
+ }
93
+ validateString(curve, "curve");
94
+ if (inputEncoding != null) {
95
+ validateString(inputEncoding, "inputEncoding");
96
+ }
97
+ if (outputEncoding != null) {
98
+ validateString(outputEncoding, "outputEncoding");
99
+ }
100
+ validateOneOf(format, "format", ["compressed", "uncompressed", "hybrid"]);
101
+ const ret = cryptoImpl.ECDHHandle.convertKey(key, curve, format);
102
+ if (typeof outputEncoding === "string") {
103
+ return Buffer.from(ret).toString(outputEncoding);
104
+ }
105
+ return Buffer.from(ret);
106
+ };
107
+ export function createECDH(curveName) {
108
+ return new ECDH(curveName);
109
+ }
@@ -0,0 +1,23 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { NativeDigest } from "./native-crypto.js";
3
+ export { hashNames, aliases } from "./digests.js";
4
+ declare class HashHandle {
5
+ private hash;
6
+ private cached?;
7
+ constructor(algorithm: string | NativeDigest, length?: number);
8
+ update(data: ArrayBufferView): number;
9
+ digest(): Buffer<ArrayBuffer>;
10
+ copy(length?: number): HashHandle;
11
+ }
12
+ declare class HmacHandle {
13
+ private hmac;
14
+ private cached?;
15
+ constructor(algorithm: string, key: any);
16
+ update(data: ArrayBufferView): number;
17
+ digest(): Buffer<ArrayBuffer>;
18
+ }
19
+ declare const _default: {
20
+ HashHandle: typeof HashHandle;
21
+ HmacHandle: typeof HmacHandle;
22
+ };
23
+ export default _default;
@@ -0,0 +1,70 @@
1
+ import { bufferBytes, viewBytes } from "../../shared/buffer-source.js";
2
+ import { validateCryptoLength } from "./validation.js";
3
+ import { secretMaterial, KeyObject } from "./keys.js";
4
+ import { Buffer } from "node:buffer";
5
+ import { NativeDigest, NativeHmac } from "./native-crypto.js";
6
+ import { aliases } from "./digests.js";
7
+ export { hashNames, aliases } from "./digests.js";
8
+ class HashHandle {
9
+ hash;
10
+ cached;
11
+ constructor(algorithm, length) {
12
+ if (algorithm instanceof NativeDigest)
13
+ this.hash = algorithm;
14
+ else {
15
+ const normalized = aliases[algorithm.split("\0")[0].toLowerCase()];
16
+ if (!normalized)
17
+ throw new Error("Digest method not supported");
18
+ this.hash = new NativeDigest(normalized, "Hash");
19
+ }
20
+ if (length !== undefined && length !== this.hash.size) {
21
+ this.hash.dispose();
22
+ throw new Error("invalid digest size");
23
+ }
24
+ }
25
+ update(data) {
26
+ if (this.cached)
27
+ throw new DOMException("Hash context has already been finalized.", "OperationError");
28
+ const input = viewBytes(data);
29
+ validateCryptoLength(input.byteLength, "data is too long");
30
+ this.hash.update(input);
31
+ return 1;
32
+ }
33
+ digest() {
34
+ this.cached ??= Buffer.from(this.hash.final());
35
+ return Buffer.from(this.cached);
36
+ }
37
+ copy(length) {
38
+ if (this.cached)
39
+ throw new DOMException("Hash context has already been finalized.", "OperationError");
40
+ return new HashHandle(this.hash.copy(), length);
41
+ }
42
+ }
43
+ class HmacHandle {
44
+ hmac;
45
+ cached;
46
+ constructor(algorithm, key) {
47
+ if (typeof CryptoKey !== "undefined" && key instanceof CryptoKey)
48
+ key = KeyObject.from(key);
49
+ key = secretMaterial(key);
50
+ const data = ArrayBuffer.isView(key) ? viewBytes(key) : bufferBytes(key);
51
+ validateCryptoLength(data.byteLength, "key is too long");
52
+ const normalized = aliases[algorithm.split("\0")[0].toLowerCase()];
53
+ if (!normalized)
54
+ throw new Error("Digest method not supported");
55
+ this.hmac = new NativeHmac(normalized, data);
56
+ }
57
+ update(data) {
58
+ if (this.cached)
59
+ throw new DOMException("HMAC context has already been finalized.", "OperationError");
60
+ const input = viewBytes(data);
61
+ validateCryptoLength(input.byteLength, "data is too long");
62
+ this.hmac.update(input);
63
+ return 1;
64
+ }
65
+ digest() {
66
+ this.cached ??= Buffer.from(this.hmac.final());
67
+ return Buffer.from(this.cached);
68
+ }
69
+ }
70
+ export default { HashHandle, HmacHandle };
@@ -0,0 +1,37 @@
1
+ import cryptoImpl from "./hash-handle.js";
2
+ import { Buffer } from "node:buffer";
3
+ import type { Transform as NodeTransform, TransformOptions } from "node:stream";
4
+ declare const kHandle: unique symbol, kState: unique symbol, kFinalized: unique symbol;
5
+ export interface HashOptions extends TransformOptions {
6
+ outputLength?: number;
7
+ }
8
+ interface _kState {
9
+ [kFinalized]: boolean;
10
+ }
11
+ declare class Hash extends NodeTransform {
12
+ [kHandle]: InstanceType<typeof cryptoImpl.HashHandle>;
13
+ [kState]: _kState;
14
+ constructor(algorithm: string | InstanceType<typeof cryptoImpl.HashHandle>, options?: HashOptions);
15
+ copy(options?: HashOptions): Hash;
16
+ update(data: string | Buffer | ArrayBufferView, encoding?: string): this;
17
+ digest(): Buffer;
18
+ digest(outputEncoding: "buffer"): Buffer;
19
+ digest(outputEncoding: BufferEncoding): string;
20
+ digest(outputEncoding?: string): Buffer | string;
21
+ }
22
+ export declare function createHash(algorithm: string, options?: HashOptions): Hash;
23
+ declare function Hash(this: unknown, algorithm: string | InstanceType<typeof cryptoImpl.HashHandle>, options?: HashOptions): Hash;
24
+ export declare function hash(algorithm: string, data: string | ArrayBufferView, outputEncoding?: string): string | Buffer;
25
+ declare class Hmac extends NodeTransform {
26
+ [kHandle]: InstanceType<typeof cryptoImpl.HmacHandle>;
27
+ [kState]: _kState;
28
+ constructor(hmac: string, key: unknown, options?: TransformOptions);
29
+ update(data: string | Buffer | ArrayBufferView, encoding?: string): this;
30
+ digest(): Buffer;
31
+ digest(outputEncoding: "buffer"): Buffer;
32
+ digest(outputEncoding: BufferEncoding): string;
33
+ digest(outputEncoding?: string): Buffer | string;
34
+ }
35
+ export declare function createHmac(hmac: string, key: unknown, options?: TransformOptions): Hmac;
36
+ declare function Hmac(this: unknown, hmac: string, key: unknown, options?: TransformOptions): Hmac;
37
+ export { Hash, Hmac };
@@ -0,0 +1,218 @@
1
+ // @ts-nocheck
2
+ // Copyright (c) 2017-2022 Cloudflare, Inc.
3
+ // Licensed under the Apache 2.0 license found in the LICENSE file or at:
4
+ // https://opensource.org/licenses/Apache-2.0
5
+ //
6
+ // Copyright Joyent, Inc. and other Node contributors.
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a
9
+ // copy of this software and associated documentation files (the
10
+ // "Software"), to deal in the Software without restriction, including
11
+ // without limitation the rights to use, copy, modify, merge, publish,
12
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ // persons to whom the Software is furnished to do so, subject to the
14
+ // following conditions:
15
+ //
16
+ // The above copyright notice and this permission notice shall be included
17
+ // in all copies or substantial portions of the Software.
18
+ //
19
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ // Ported from workerd@5358e3d337336eaf696a48bc1563d808b10feba5 crypto_hash.ts.
27
+ import cryptoImpl from "./hash-handle.js";
28
+ import { Buffer } from "node:buffer";
29
+ import { KeyObject } from "./keys.js";
30
+ import { ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE } from "./sign-errors.js";
31
+ const isCryptoKey = (value) => typeof CryptoKey !== "undefined" && value instanceof CryptoKey;
32
+ import { isAnyBuffer as isAnyArrayBuffer } from "./validation.js";
33
+ function getStringOption(options, key) {
34
+ let value;
35
+ if (options && (value = options[key]) != null)
36
+ validateString(value, `options.${key}`);
37
+ return value;
38
+ }
39
+ function getArrayBufferOrView(value, name, encoding) {
40
+ return typeof value === "string"
41
+ ? Buffer.from(value, encoding === "buffer" ? undefined : encoding)
42
+ : value;
43
+ }
44
+ import { Transform } from "../stream/transform.js";
45
+ import { validateString, validateUint32 } from "../net/validators.js";
46
+ import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
47
+ import { NodeError } from "../net/error-base.js";
48
+ const kHandle = Symbol("kHandle"), kState = Symbol("kState"), kFinalized = Symbol("kFinalized");
49
+ const isArrayBufferView = ArrayBuffer.isView;
50
+ class ERR_CRYPTO_HASH_FINALIZED extends NodeError {
51
+ constructor() {
52
+ super("ERR_CRYPTO_HASH_FINALIZED", "Digest already called");
53
+ }
54
+ }
55
+ class ERR_CRYPTO_HASH_UPDATE_FAILED extends NodeError {
56
+ constructor() {
57
+ super("ERR_CRYPTO_HASH_UPDATE_FAILED", "Hash update failed");
58
+ }
59
+ }
60
+ // These helper functions are needed because the constructors can
61
+ // use new, in which case V8 cannot inline the recursive constructor call
62
+ export function createHash(algorithm, options) {
63
+ return new Hash(algorithm, options);
64
+ }
65
+ function Hash(algorithm, options) {
66
+ if (!(this instanceof Hash)) {
67
+ return new Hash(algorithm, options);
68
+ }
69
+ const xofLen = typeof options === "object" ? options.outputLength : undefined;
70
+ if (xofLen !== undefined)
71
+ validateUint32(xofLen, "options.outputLength");
72
+ if (algorithm instanceof cryptoImpl.HashHandle) {
73
+ this[kHandle] = algorithm.copy(xofLen);
74
+ }
75
+ else {
76
+ validateString(algorithm, "algorithm");
77
+ this[kHandle] = new cryptoImpl.HashHandle(algorithm, xofLen);
78
+ }
79
+ this[kState] = {
80
+ [kFinalized]: false,
81
+ };
82
+ Transform.call(this, options);
83
+ return this;
84
+ }
85
+ Object.setPrototypeOf(Hash.prototype, Transform.prototype);
86
+ Object.setPrototypeOf(Hash, Transform);
87
+ Hash.prototype.copy = function (options) {
88
+ const state = this[kState];
89
+ if (state[kFinalized])
90
+ throw new ERR_CRYPTO_HASH_FINALIZED();
91
+ return new Hash(this[kHandle], options);
92
+ };
93
+ Hash.prototype._transform = function (chunk, encoding, callback) {
94
+ if (typeof chunk === "string") {
95
+ chunk = Buffer.from(chunk, encoding);
96
+ }
97
+ this[kHandle].update(chunk);
98
+ callback();
99
+ };
100
+ Hash.prototype._flush = function (callback) {
101
+ this.push(Buffer.from(this[kHandle].digest()));
102
+ callback();
103
+ };
104
+ Hash.prototype.update = function (data, encoding) {
105
+ encoding ??= "utf8";
106
+ if (encoding === "buffer") {
107
+ encoding = undefined;
108
+ }
109
+ const state = this[kState];
110
+ if (state[kFinalized])
111
+ throw new ERR_CRYPTO_HASH_FINALIZED();
112
+ if (typeof data === "string") {
113
+ data = Buffer.from(data, encoding);
114
+ }
115
+ else if (!isArrayBufferView(data)) {
116
+ throw new ERR_INVALID_ARG_TYPE("data", ["string", "Buffer", "TypedArray", "DataView"], data);
117
+ }
118
+ if (!this[kHandle].update(data))
119
+ throw new ERR_CRYPTO_HASH_UPDATE_FAILED();
120
+ return this;
121
+ };
122
+ Hash.prototype.digest = function (outputEncoding) {
123
+ const state = this[kState];
124
+ if (state[kFinalized])
125
+ throw new ERR_CRYPTO_HASH_FINALIZED();
126
+ // Explicit conversion for backward compatibility.
127
+ const ret = Buffer.from(this[kHandle].digest());
128
+ state[kFinalized] = true;
129
+ if (outputEncoding !== undefined && outputEncoding !== "buffer") {
130
+ return ret.toString(outputEncoding);
131
+ }
132
+ else {
133
+ return ret;
134
+ }
135
+ };
136
+ export function hash(algorithm, data, outputEncoding = "hex") {
137
+ validateString(algorithm, "algorithm");
138
+ validateString(outputEncoding, "outputEncoding");
139
+ if (typeof data === "string") {
140
+ const hash = createHash(algorithm);
141
+ hash.update(data, "utf8");
142
+ return hash.digest(outputEncoding);
143
+ }
144
+ else if (isArrayBufferView(data)) {
145
+ const hash = createHash(algorithm);
146
+ hash.update(data, "utf8");
147
+ return hash.digest(outputEncoding);
148
+ }
149
+ throw new ERR_INVALID_ARG_TYPE("data", ["string", "Buffer", "TypedArray", "DataView"], data);
150
+ }
151
+ export function createHmac(hmac, key, options) {
152
+ return new Hmac(hmac, key, options);
153
+ }
154
+ function Hmac(hmac, key, options) {
155
+ if (!(this instanceof Hmac)) {
156
+ return new Hmac(hmac, key, options);
157
+ }
158
+ validateString(hmac, "hmac");
159
+ const encoding = getStringOption(options, "encoding");
160
+ if (key instanceof KeyObject) {
161
+ if (key.type !== "secret") {
162
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "secret");
163
+ }
164
+ this[kHandle] = new cryptoImpl.HmacHandle(hmac, key);
165
+ }
166
+ else if (isCryptoKey(key)) {
167
+ if (key.type !== "secret") {
168
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "secret");
169
+ }
170
+ this[kHandle] = new cryptoImpl.HmacHandle(hmac, key);
171
+ }
172
+ else if (typeof key !== "string" &&
173
+ !isArrayBufferView(key) &&
174
+ !isAnyArrayBuffer(key)) {
175
+ throw new ERR_INVALID_ARG_TYPE("key", [
176
+ "ArrayBuffer",
177
+ "Buffer",
178
+ "ArrayBufferView",
179
+ "string",
180
+ "KeyObject",
181
+ "CryptoKey",
182
+ ], key);
183
+ }
184
+ else {
185
+ this[kHandle] = new cryptoImpl.HmacHandle(hmac, getArrayBufferOrView(key, "key", encoding));
186
+ }
187
+ this[kState] = {
188
+ [kFinalized]: false,
189
+ };
190
+ Transform.call(this, options);
191
+ return this;
192
+ }
193
+ Object.setPrototypeOf(Hmac.prototype, Transform.prototype);
194
+ Object.setPrototypeOf(Hmac, Transform);
195
+ // eslint-disable-next-line @typescript-eslint/unbound-method
196
+ Hmac.prototype.update = Hash.prototype.update;
197
+ Hmac.prototype.digest = function (outputEncoding) {
198
+ const state = this[kState];
199
+ if (state[kFinalized]) {
200
+ return !outputEncoding || outputEncoding === "buffer"
201
+ ? Buffer.from("")
202
+ : "";
203
+ }
204
+ // Explicit conversion for backward compatibility.
205
+ const ret = Buffer.from(this[kHandle].digest());
206
+ state[kFinalized] = true;
207
+ if (outputEncoding !== undefined && outputEncoding !== "buffer") {
208
+ return ret.toString(outputEncoding);
209
+ }
210
+ else {
211
+ return ret;
212
+ }
213
+ };
214
+ // eslint-disable-next-line @typescript-eslint/unbound-method
215
+ Hmac.prototype._flush = Hash.prototype._flush;
216
+ // eslint-disable-next-line @typescript-eslint/unbound-method
217
+ Hmac.prototype._transform = Hash.prototype._transform;
218
+ export { Hash, Hmac };
@@ -0,0 +1,2 @@
1
+ export declare function hkdfSync(digest: string, key: unknown, salt: unknown, info: unknown, length: number): ArrayBuffer;
2
+ export declare function hkdf(digest: string, key: unknown, salt: unknown, info: unknown, length: number, callback: (error: unknown, value?: ArrayBuffer) => void): void;
@@ -0,0 +1,78 @@
1
+ import { KeyObject } from "./keys.js";
2
+ import { createHmac } from "./hash.js";
3
+ import { aliases } from "./hash-handle.js";
4
+ // RFC 5869 extract-and-expand, using the existing HMAC implementation.
5
+ import { Buffer } from "node:buffer";
6
+ import { arrayBufferOrView, inputBytes, validateInteger, validateCryptoLength, } from "./validation.js";
7
+ import { validateString, validateFunction } from "../net/validators.js";
8
+ import { ERR_OUT_OF_RANGE } from "../net/errors.js";
9
+ function parameters(digest, key, salt, info, length) {
10
+ if (key instanceof KeyObject)
11
+ key = key.export();
12
+ validateString(digest, "digest");
13
+ const ikm = arrayBufferOrView(key, "ikm", [
14
+ "string",
15
+ "SecretKeyObject",
16
+ "ArrayBuffer",
17
+ "TypedArray",
18
+ "DataView",
19
+ "Buffer",
20
+ ]), s = arrayBufferOrView(salt, "salt", [
21
+ "string",
22
+ "ArrayBuffer",
23
+ "TypedArray",
24
+ "DataView",
25
+ "Buffer",
26
+ ]), i = arrayBufferOrView(info, "info", [
27
+ "string",
28
+ "ArrayBuffer",
29
+ "TypedArray",
30
+ "DataView",
31
+ "Buffer",
32
+ ]);
33
+ validateInteger(length, "length", 0, 2147483647);
34
+ if (i.byteLength > 1024)
35
+ throw new ERR_OUT_OF_RANGE("info", "must not contain more than 1024 bytes", i.byteLength);
36
+ return { digest, ikm, s, i, length };
37
+ }
38
+ function derive({ digest, ikm, s, i, length, }) {
39
+ const keyBytes = inputBytes(ikm, "getHkdf", 2), saltBytes = inputBytes(s, "getHkdf", 3), infoBytes = inputBytes(i, "getHkdf", 4);
40
+ if (!aliases[digest.split("\0")[0].toLowerCase()])
41
+ throw new TypeError("Invalid Hkdf digest: " + digest);
42
+ validateCryptoLength(infoBytes.byteLength, "Hkdf failed: info is too large");
43
+ validateCryptoLength(saltBytes.byteLength, "Hkdf failed: salt is too large");
44
+ validateCryptoLength(keyBytes.byteLength, "Hkdf failed: key is too large");
45
+ const prk = createHmac(digest, saltBytes).update(keyBytes).digest();
46
+ let previous = Buffer.alloc(0);
47
+ try {
48
+ if (length > 255 * prk.length)
49
+ throw new RangeError("Invalid Hkdf key length");
50
+ const output = Buffer.alloc(length);
51
+ for (let offset = 0, counter = 1; offset < length; counter++) {
52
+ const next = createHmac(digest, prk)
53
+ .update(previous)
54
+ .update(infoBytes)
55
+ .update(Buffer.from([counter]))
56
+ .digest();
57
+ previous.fill(0);
58
+ previous = next;
59
+ const size = Math.min(previous.length, length - offset);
60
+ output.set(previous.subarray(0, size), offset);
61
+ offset += size;
62
+ }
63
+ return new Uint8Array(output).buffer;
64
+ }
65
+ finally {
66
+ prk.fill(0);
67
+ previous.fill(0);
68
+ }
69
+ }
70
+ export function hkdfSync(digest, key, salt, info, length) {
71
+ return derive(parameters(digest, key, salt, info, length));
72
+ }
73
+ export function hkdf(digest, key, salt, info, length, callback) {
74
+ const args = parameters(digest, key, salt, info, length);
75
+ validateFunction(callback, "callback");
76
+ const result = new Promise((resolve) => resolve(derive(args)));
77
+ result.then((value) => callback(null, value), (error) => callback(error));
78
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ module.exports = require("./index.js").default;