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,4 @@
1
+ declare const _default: {
2
+ getPbkdf(password: ArrayBuffer | ArrayBufferView, salt: ArrayBuffer | ArrayBufferView, iterations: number, length: number, digest: string): Uint8Array<ArrayBufferLike>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,26 @@
1
+ import { inputBytes, validateCryptoLength } from "./validation.js";
2
+ import { nativePbkdf2 } from "./native-crypto.js";
3
+ import { aliases } from "./hash-handle.js";
4
+ const sizes = {
5
+ md4: 16,
6
+ md5: 16,
7
+ sha1: 20,
8
+ sha224: 28,
9
+ sha256: 32,
10
+ sha384: 48,
11
+ sha512: 64,
12
+ "md5-sha1": 36,
13
+ };
14
+ export default {
15
+ getPbkdf(password, salt, iterations, length, digest) {
16
+ const passwordBytes = inputBytes(password, "getPbkdf", 1), saltBytes = inputBytes(salt, "getPbkdf", 2);
17
+ const normalized = aliases[digest.split("\0")[0].toLowerCase()];
18
+ if (!normalized)
19
+ throw new TypeError("Invalid Pbkdf2 digest: " + digest + ".");
20
+ validateCryptoLength(passwordBytes.byteLength, "Pbkdf2 failed: password is too large");
21
+ validateCryptoLength(saltBytes.byteLength, "Pbkdf2 failed: salt is too large");
22
+ if (length > 255 * sizes[normalized])
23
+ throw new RangeError("Pbkdf2 failed: derived key length exceeds maximum for this hash");
24
+ return nativePbkdf2(passwordBytes, saltBytes, iterations, length, normalized);
25
+ },
26
+ };
@@ -0,0 +1,5 @@
1
+ import { Buffer } from "node:buffer";
2
+ export type ArrayLike = string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
3
+ export declare function pbkdf2Sync(password: ArrayLike, salt: ArrayLike, iterations: number, keylen: number, digest: string): Buffer;
4
+ export type Pbkdf2Callback = (err?: Error | null, result?: Buffer) => void;
5
+ export declare function pbkdf2(password: ArrayLike, salt: ArrayLike, iterations: number, keylen: number, digest: string, callback: Pbkdf2Callback): void;
@@ -0,0 +1,60 @@
1
+ // @ts-nocheck
2
+ // Copyright (c) 2017-2023 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
+ import cryptoImpl from "./pbkdf2-handle.js";
27
+ import { Buffer } from "node:buffer";
28
+ import { validateInt32, validateFunction, validateString, } from "../net/validators.js";
29
+ import { arrayBufferOrView as getArrayBufferOrView } from "./validation.js";
30
+ export function pbkdf2Sync(password, salt, iterations, keylen, digest) {
31
+ ({ password, salt, iterations, keylen, digest } = check(password, salt, iterations, keylen, digest));
32
+ const result = cryptoImpl.getPbkdf(password, salt, iterations, keylen, digest);
33
+ return Buffer.from(result);
34
+ }
35
+ export function pbkdf2(password, salt, iterations, keylen, digest, callback) {
36
+ if (typeof digest === "function") {
37
+ // Appease node test cases
38
+ validateString(undefined, "digest");
39
+ }
40
+ validateFunction(callback, "callback");
41
+ ({ password, salt, iterations, keylen, digest } = check(password, salt, iterations, keylen, digest));
42
+ new Promise((res, rej) => {
43
+ try {
44
+ res(cryptoImpl.getPbkdf(password, salt, iterations, keylen, digest));
45
+ }
46
+ catch (err) {
47
+ rej(err);
48
+ }
49
+ }).then((val) => callback(null, Buffer.from(val)), (err) => callback(err));
50
+ }
51
+ function check(password, salt, iterations, keylen, digest) {
52
+ validateString(digest, "digest");
53
+ password = getArrayBufferOrView(password, "password");
54
+ salt = getArrayBufferOrView(salt, "salt");
55
+ // OpenSSL uses a signed int to represent these values, so we are restricted
56
+ // to the 31-bit range here (which is plenty).
57
+ validateInt32(iterations, "iterations", 1);
58
+ validateInt32(keylen, "keylen", 0);
59
+ return { password, salt, iterations, keylen, digest };
60
+ }
@@ -0,0 +1,19 @@
1
+ import { Buffer } from "node:buffer";
2
+ export type PrimeNum = ArrayBuffer | ArrayBufferView | Buffer | bigint;
3
+ export interface GeneratePrimeOptions {
4
+ add?: PrimeNum;
5
+ rem?: PrimeNum;
6
+ safe?: boolean;
7
+ bigint?: boolean;
8
+ }
9
+ export interface CheckPrimeOptions {
10
+ checks?: number;
11
+ }
12
+ export type GeneratePrimeCallback = (err: Error | null, prime?: bigint | ArrayBuffer) => void;
13
+ export type CheckPrimeCallback = (err: Error | null, prime?: boolean) => void;
14
+ export declare function generatePrimeSync(size: number, options?: GeneratePrimeOptions): bigint | ArrayBuffer;
15
+ export declare function generatePrime(size: number, options: GeneratePrimeOptions, callback: GeneratePrimeCallback): void;
16
+ export declare function generatePrime(size: number, callback: GeneratePrimeCallback): void;
17
+ export declare function checkPrimeSync(candidate: PrimeNum, options?: CheckPrimeOptions): boolean;
18
+ export declare function checkPrime(candidate: PrimeNum, options: CheckPrimeOptions, callback: CheckPrimeCallback): void;
19
+ export declare function checkPrime(candidate: PrimeNum, callback: CheckPrimeCallback): void;
@@ -0,0 +1,146 @@
1
+ // Adapted from pinned workerd crypto_random.ts (Apache-2.0 / Node MIT).
2
+ import { bufferBytes, viewBytes } from "../../shared/buffer-source.js";
3
+ import { Buffer } from "node:buffer";
4
+ import { validateObject, validateFunction, validateBoolean, validateInt32, } from "../net/validators.js";
5
+ import { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
6
+ import { isAnyBuffer as isAnyArrayBuffer } from "./validation.js";
7
+ const isArrayBufferView = ArrayBuffer.isView;
8
+ import { nativePrimeCheck, nativePrimeGenerate } from "./native-crypto.js";
9
+ function view(value) {
10
+ return ArrayBuffer.isView(value) ? viewBytes(value) : bufferBytes(value);
11
+ }
12
+ function arrayBufferToUnsignedBigInt(value) {
13
+ const hex = Buffer.from(view(value)).toString("hex");
14
+ return hex ? BigInt("0x" + hex) : 0n;
15
+ }
16
+ const cryptoImpl = {
17
+ checkPrimeSync(candidate, checks) {
18
+ const data = view(candidate);
19
+ if (checks > 64)
20
+ throw new RangeError("Invalid number of checks");
21
+ if (data.length > 1024)
22
+ throw new RangeError("checkPrime candidate exceeds maximum size");
23
+ return nativePrimeCheck(data, checks);
24
+ },
25
+ randomPrime(bits, safe, add, rem) {
26
+ if (bits > 8192)
27
+ throw new RangeError("generatePrime size exceeds maximum (8192 bits)");
28
+ if (add !== undefined) {
29
+ const a = arrayBufferToUnsignedBigInt(add), r = rem === undefined ? undefined : arrayBufferToUnsignedBigInt(rem);
30
+ if (!((a === 12n && r === 11n) ||
31
+ (a === 24n && r === 23n) ||
32
+ (a === 60n && r === 59n)))
33
+ throw new RangeError("Invalid values for add and rem");
34
+ if (a.toString(2).length > bits)
35
+ throw new RangeError("options.add must not be bigger than size of the requested prime");
36
+ }
37
+ return nativePrimeGenerate(bits, safe, add === undefined ? undefined : view(add), rem === undefined ? undefined : view(rem)).buffer;
38
+ },
39
+ };
40
+ function processGeneratePrimeOptions(options) {
41
+ validateObject(options, "options");
42
+ const { safe = false, bigint = false } = options;
43
+ let { add, rem } = options;
44
+ validateBoolean(safe, "options.safe");
45
+ validateBoolean(bigint, "options.bigint");
46
+ if (add !== undefined) {
47
+ if (typeof add === "bigint") {
48
+ add = unsignedBigIntToBuffer(add, "options.add");
49
+ }
50
+ else if (!isAnyArrayBuffer(add) && !isArrayBufferView(add)) {
51
+ throw new ERR_INVALID_ARG_TYPE("options.add", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], add);
52
+ }
53
+ }
54
+ if (rem !== undefined) {
55
+ if (typeof rem === "bigint") {
56
+ rem = unsignedBigIntToBuffer(rem, "options.rem");
57
+ }
58
+ else if (!isAnyArrayBuffer(rem) && !isArrayBufferView(rem)) {
59
+ throw new ERR_INVALID_ARG_TYPE("options.rem", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], rem);
60
+ }
61
+ }
62
+ return {
63
+ safe,
64
+ bigint,
65
+ add: add,
66
+ rem: rem,
67
+ };
68
+ }
69
+ export function generatePrimeSync(size, options = {}) {
70
+ validateInt32(size, "size", 1);
71
+ const { safe, bigint, add, rem } = processGeneratePrimeOptions(options);
72
+ const primeBuf = cryptoImpl.randomPrime(size, safe, add, rem);
73
+ return bigint ? arrayBufferToUnsignedBigInt(primeBuf) : primeBuf;
74
+ }
75
+ export function generatePrime(size, options, callback) {
76
+ validateInt32(size, "size", 1);
77
+ if (typeof options === "function") {
78
+ callback = options;
79
+ options = {};
80
+ }
81
+ validateFunction(callback, "callback");
82
+ const { safe, bigint, add, rem } = processGeneratePrimeOptions(options);
83
+ new Promise((res, rej) => {
84
+ try {
85
+ const primeBuf = cryptoImpl.randomPrime(size, safe, add, rem);
86
+ res(bigint ? arrayBufferToUnsignedBigInt(primeBuf) : primeBuf);
87
+ }
88
+ catch (err) {
89
+ rej(err);
90
+ }
91
+ }).then((val) => {
92
+ callback(null, val);
93
+ }, (err) => {
94
+ callback(err);
95
+ });
96
+ }
97
+ function unsignedBigIntToBuffer(bigint, name) {
98
+ if (bigint < 0) {
99
+ throw new ERR_OUT_OF_RANGE(name, ">= 0", bigint);
100
+ }
101
+ const hex = bigint.toString(16);
102
+ const padded = hex.padStart(hex.length + (hex.length % 2), "0");
103
+ return Buffer.from(padded, "hex");
104
+ }
105
+ function validateCandidate(candidate) {
106
+ if (typeof candidate === "bigint")
107
+ candidate = unsignedBigIntToBuffer(candidate, "candidate");
108
+ if (!isAnyArrayBuffer(candidate) && !isArrayBufferView(candidate)) {
109
+ throw new ERR_INVALID_ARG_TYPE("candidate", ["ArrayBuffer", "TypedArray", "Buffer", "DataView", "bigint"], candidate);
110
+ }
111
+ return candidate;
112
+ }
113
+ function validateChecks(options) {
114
+ const { checks = 0 } = options;
115
+ // The checks option is unsigned but must fit into a signed 32-bit integer for OpenSSL.
116
+ validateInt32(checks, "options.checks", 0);
117
+ return checks;
118
+ }
119
+ export function checkPrimeSync(candidate, options = {}) {
120
+ candidate = validateCandidate(candidate);
121
+ validateObject(options, "options");
122
+ const checks = validateChecks(options);
123
+ return cryptoImpl.checkPrimeSync(candidate, checks);
124
+ }
125
+ export function checkPrime(candidate, options, callback) {
126
+ candidate = validateCandidate(candidate);
127
+ if (typeof options === "function") {
128
+ callback = options;
129
+ options = {};
130
+ }
131
+ validateObject(options, "options");
132
+ validateFunction(callback, "callback");
133
+ const checks = validateChecks(options);
134
+ new Promise((res, rej) => {
135
+ try {
136
+ res(cryptoImpl.checkPrimeSync(candidate, checks));
137
+ }
138
+ catch (err) {
139
+ rej(err);
140
+ }
141
+ }).then((val) => {
142
+ callback(null, val);
143
+ }, (err) => {
144
+ callback(err);
145
+ });
146
+ }
@@ -0,0 +1,18 @@
1
+ import { Buffer } from "node:buffer";
2
+ import type { RandomUUIDOptions } from "node:crypto";
3
+ export type RandomBytesCallback = (err: Error | null, buffer: Buffer) => void;
4
+ export declare function randomBytes(size: number, callback: RandomBytesCallback): void;
5
+ export declare function randomBytes(size: number): Buffer;
6
+ export type RandomFillBuffer = NodeJS.ArrayBufferView | ArrayBuffer | SharedArrayBuffer;
7
+ export type RandomFillResult<T extends RandomFillBuffer> = T extends ArrayBuffer | SharedArrayBuffer ? Buffer : T extends DataView ? Uint8Array : T;
8
+ export declare function randomFillSync<T extends RandomFillBuffer>(buffer: T, offset?: number, size?: number): RandomFillResult<T>;
9
+ export type RandomFillCallback<T extends RandomFillBuffer = RandomFillBuffer> = (err: Error | null, buf?: T) => void;
10
+ export declare function randomFill<T extends RandomFillBuffer>(buffer: T, callback: RandomFillCallback<T>): void;
11
+ export declare function randomFill<T extends RandomFillBuffer>(buffer: T, offset: number, callback: RandomFillCallback<T>): void;
12
+ export declare function randomFill<T extends RandomFillBuffer>(buffer: T, offset: number, size: number, callback: RandomFillCallback<T>): void;
13
+ export type RandomIntCallback = (err: Error | null, n?: number) => void;
14
+ export declare function randomInt(max: number): number;
15
+ export declare function randomInt(min: number, max: number): number;
16
+ export declare function randomInt(max: number, callback: RandomIntCallback): void;
17
+ export declare function randomInt(min: number, max: number, callback: RandomIntCallback): void;
18
+ export declare function randomUUID(options?: RandomUUIDOptions): string;
@@ -0,0 +1,203 @@
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 OR
23
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+ // Adapted from workerd@5358e3d337336eaf696a48bc1563d808b10feba5.
26
+ import { Buffer } from "node:buffer";
27
+ import { validateInteger, isAnyBuffer as isAnyArrayBuffer, } from "./validation.js";
28
+ import { validateBoolean, validateObject, validateFunction, } from "../net/validators.js";
29
+ const isArrayBufferView = ArrayBuffer.isView;
30
+ const dataViewBuffer = Object.getOwnPropertyDescriptor(DataView.prototype, "buffer").get;
31
+ function isDataView(value) {
32
+ try {
33
+ dataViewBuffer.call(value);
34
+ return true;
35
+ }
36
+ catch {
37
+ return false;
38
+ }
39
+ }
40
+ import { kMaxLength } from "../buffer/internal-buffer.js";
41
+ import { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
42
+ export function randomBytes(size, callback) {
43
+ validateInteger(size, "size", 0, kMaxLength);
44
+ const buf = Buffer.alloc(size);
45
+ if (callback !== undefined) {
46
+ randomFill(buf, callback);
47
+ return undefined;
48
+ }
49
+ else {
50
+ randomFillSync(buf);
51
+ return buf;
52
+ }
53
+ }
54
+ export function randomFillSync(buffer, offset, size) {
55
+ if (!isAnyArrayBuffer(buffer) && !isArrayBufferView(buffer)) {
56
+ throw new ERR_INVALID_ARG_TYPE("buffer", ["TypedArray", "DataView", "ArrayBuffer", "SharedArrayBuffer"], buffer);
57
+ }
58
+ // Use byteLength, not length — DataView has no .length property and
59
+ // TypedArray .length is element count, not bytes.
60
+ const maxLength = buffer.byteLength;
61
+ if (offset !== undefined) {
62
+ validateInteger(offset, "offset", 0, kMaxLength);
63
+ }
64
+ else
65
+ offset = 0;
66
+ if (size !== undefined) {
67
+ validateInteger(size, "size", 0, maxLength - offset);
68
+ }
69
+ else
70
+ size = maxLength - offset;
71
+ if (isAnyArrayBuffer(buffer)) {
72
+ buffer = Buffer.from(buffer);
73
+ }
74
+ else if (isDataView(buffer)) {
75
+ buffer = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
76
+ }
77
+ buffer = buffer.subarray(offset, offset + size);
78
+ return crypto.getRandomValues(buffer);
79
+ }
80
+ export function randomFill(buffer, offsetOrCallback, sizeOrCallback, callback) {
81
+ if (!isAnyArrayBuffer(buffer) && !isArrayBufferView(buffer)) {
82
+ throw new ERR_INVALID_ARG_TYPE("buffer", ["TypedArray", "DataView", "ArrayBuffer", "SharedArrayBuffer"], buffer);
83
+ }
84
+ let offset = 0;
85
+ let size = 0;
86
+ // Use byteLength, not length — DataView has no .length property and
87
+ // TypedArray .length is element count, not bytes.
88
+ const maxLength = buffer.byteLength;
89
+ if (typeof callback === "function") {
90
+ validateInteger(offsetOrCallback, "offset", 0, maxLength);
91
+ offset = offsetOrCallback;
92
+ validateInteger(sizeOrCallback, "size", 0, maxLength - offset);
93
+ size = sizeOrCallback;
94
+ }
95
+ else if (typeof sizeOrCallback === "function") {
96
+ validateInteger(offsetOrCallback, "offset", 0, maxLength);
97
+ offset = offsetOrCallback;
98
+ size = maxLength - offset;
99
+ callback = sizeOrCallback;
100
+ }
101
+ else if (typeof offsetOrCallback === "function") {
102
+ offset = 0;
103
+ size = maxLength;
104
+ callback = offsetOrCallback;
105
+ }
106
+ validateFunction(callback, "callback");
107
+ // We're currently not actually implementing the fill itself asynchronously,
108
+ // so we defer to randomFillSync here, but we invoke the callback asynchronously.
109
+ new Promise((res) => {
110
+ randomFillSync(buffer, offset, size);
111
+ res();
112
+ }).then(() => callback(null, buffer), (err) => callback(err));
113
+ }
114
+ const RAND_MAX = 0xffff_ffff_ffff;
115
+ // Cache random data to use in randomInt. The cache size must be evenly
116
+ // divisible by 6 because each attempt to obtain a random int uses 6 bytes.
117
+ const randomCache = Buffer.alloc(6 * 1024);
118
+ let randomCacheOffset = 0;
119
+ let initialized = false;
120
+ function getRandomInt(min, max) {
121
+ if (!initialized) {
122
+ randomFillSync(randomCache);
123
+ initialized = true;
124
+ }
125
+ // First we generate a random int between [0..range)
126
+ const range = max - min;
127
+ if (!(range <= RAND_MAX)) {
128
+ throw new ERR_OUT_OF_RANGE(`max${max ? "" : " - min"}`, `<= ${RAND_MAX}`, range);
129
+ }
130
+ // For (x % range) to produce an unbiased value greater than or equal to 0 and
131
+ // less than range, x must be drawn randomly from the set of integers greater
132
+ // than or equal to 0 and less than randLimit.
133
+ const randLimit = RAND_MAX - (RAND_MAX % range);
134
+ // If we don't have a callback, or if there is still data in the cache, we can
135
+ // do this synchronously, which is super fast.
136
+ while (randomCacheOffset <= randomCache.length) {
137
+ if (randomCacheOffset === randomCache.length) {
138
+ // This might block the thread for a bit, but we are in sync mode.
139
+ randomFillSync(randomCache);
140
+ randomCacheOffset = 0;
141
+ }
142
+ const x = randomCache.readUIntBE(randomCacheOffset, 6);
143
+ randomCacheOffset += 6;
144
+ if (x < randLimit) {
145
+ return (x % range) + min;
146
+ }
147
+ }
148
+ return 0; // Should be unreachable.
149
+ }
150
+ export function randomInt(minOrMax, maxOrCallback, callback) {
151
+ let min = 0;
152
+ let max = 0;
153
+ if (typeof callback === "function") {
154
+ validateInteger(minOrMax, "min");
155
+ validateInteger(maxOrCallback, "max");
156
+ min = minOrMax;
157
+ max = maxOrCallback;
158
+ }
159
+ else if (typeof maxOrCallback === "function") {
160
+ min = 0;
161
+ validateInteger(minOrMax, "max");
162
+ max = minOrMax;
163
+ callback = maxOrCallback;
164
+ }
165
+ else if (arguments.length === 2) {
166
+ validateInteger(minOrMax, "min");
167
+ validateInteger(maxOrCallback, "max");
168
+ min = minOrMax;
169
+ max = maxOrCallback;
170
+ }
171
+ else {
172
+ min = 0;
173
+ validateInteger(minOrMax, "max");
174
+ max = minOrMax;
175
+ }
176
+ if (min >= max) {
177
+ throw new ERR_OUT_OF_RANGE("min", "min < max", min);
178
+ }
179
+ if (callback != null) {
180
+ new Promise((res) => {
181
+ res(getRandomInt(min, max));
182
+ }).then((n) => {
183
+ callback(null, n);
184
+ }, (err) => {
185
+ callback(err);
186
+ });
187
+ }
188
+ else {
189
+ return getRandomInt(min, max);
190
+ }
191
+ return undefined;
192
+ }
193
+ export function randomUUID(options) {
194
+ // While we do not actually use the entropy cache, we go ahead and validate
195
+ // the input parameters as Node.js does.
196
+ if (options !== undefined) {
197
+ validateObject(options, "options");
198
+ if (options.disableEntropyCache !== undefined) {
199
+ validateBoolean(options.disableEntropyCache, "options.disableEntropyCache");
200
+ }
201
+ }
202
+ return crypto.randomUUID();
203
+ }
@@ -0,0 +1,21 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { KeyObject } from "./keys.js";
3
+ export type KeyData = string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | KeyObject | CryptoKey;
4
+ interface HashOptions {
5
+ padding?: number;
6
+ oaepHash?: string;
7
+ oaepLabel?: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
8
+ encoding?: BufferEncoding;
9
+ }
10
+ export interface RsaKeyOptions extends HashOptions {
11
+ key: KeyData;
12
+ format?: "pem" | "der" | "jwk";
13
+ type?: "pkcs1" | "pkcs8" | "sec1" | "spki";
14
+ passphrase?: string | ArrayBuffer | ArrayBufferView;
15
+ }
16
+ type CreateAsymmetricKeyOptions = RsaKeyOptions;
17
+ export declare function privateEncrypt(privateKey: CreateAsymmetricKeyOptions | KeyData | undefined, buffer: string | ArrayBufferView | ArrayBuffer | undefined): Buffer;
18
+ export declare function privateDecrypt(privateKey: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
19
+ export declare function publicEncrypt(key: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
20
+ export declare function publicDecrypt(key: CreateAsymmetricKeyOptions | KeyData, buffer: string | ArrayBufferView | ArrayBuffer): Buffer;
21
+ export {};