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,177 @@
1
+ // @ts-nocheck
2
+ // Pinned workerd crypto_cipher.ts; LICENSE-APACHE and LICENSE-3RD-PARTY.md.
3
+ import { Buffer } from "node:buffer";
4
+ import { validateNumber, validateString } from "../net/validators.js";
5
+ import { ERR_INVALID_ARG_TYPE, ERR_MISSING_ARGS } from "../net/errors.js";
6
+ import { ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE } from "./sign-errors.js";
7
+ import { isAnyBuffer as isAnyArrayBuffer, bytes } from "./validation.js";
8
+ const isArrayBufferView = ArrayBuffer.isView;
9
+ import { isKeyObject, getKeyObjectHandle } from "./keys.js";
10
+ import { createPrivateKey, createPublicKey } from "./asymmetric.js";
11
+ import { keyStates } from "./key-state.js";
12
+ const kRsaPkcs1Padding = 1;
13
+ function operate(operation, key, data, options) {
14
+ const handle = keyStates.get(key)?.asymmetric;
15
+ if (!handle)
16
+ throw Object.assign(new Error("Synchronous access to opaque host key material is unavailable"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
17
+ return handle.rsa(operation, bytes(data, "buffer"), {
18
+ ...options,
19
+ oaepLabel: options.oaepLabel === undefined
20
+ ? undefined
21
+ : bytes(options.oaepLabel, "oaepLabel"),
22
+ });
23
+ }
24
+ const cryptoImpl = {
25
+ publicEncrypt: (...args) => operate(0, ...args),
26
+ privateDecrypt: (...args) => operate(1, ...args),
27
+ privateEncrypt: (...args) => operate(2, ...args),
28
+ publicDecrypt: (...args) => operate(3, ...args),
29
+ };
30
+ function getPaddingAndHash(options) {
31
+ const { padding = kRsaPkcs1Padding, oaepHash = "sha256", oaepLabel, encoding = "utf8", } = options;
32
+ validateNumber(padding, "options.padding");
33
+ validateString(oaepHash, "options.oaepHash");
34
+ let label;
35
+ if (oaepLabel !== undefined) {
36
+ if (typeof oaepLabel === "string") {
37
+ label = Buffer.from(oaepLabel, encoding);
38
+ }
39
+ else if (isAnyArrayBuffer(oaepLabel) || isArrayBufferView(oaepLabel)) {
40
+ label = oaepLabel;
41
+ }
42
+ else {
43
+ throw new ERR_INVALID_ARG_TYPE("options.oaepLabel", ["string", "Buffer", "TypedArray", "DataView", "ArrayBuffer"], oaepLabel);
44
+ }
45
+ }
46
+ return { padding, oaepHash, oaepLabel: label };
47
+ }
48
+ export function privateEncrypt(privateKey, buffer) {
49
+ if (privateKey === undefined) {
50
+ throw new ERR_MISSING_ARGS("privateKey");
51
+ }
52
+ if (buffer === undefined) {
53
+ throw new ERR_MISSING_ARGS("buffer");
54
+ }
55
+ const key = (() => {
56
+ if (privateKey instanceof CryptoKey) {
57
+ if (privateKey.type !== "private") {
58
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
59
+ }
60
+ return privateKey;
61
+ }
62
+ else if (isKeyObject(privateKey)) {
63
+ if (privateKey.type !== "private") {
64
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
65
+ }
66
+ return getKeyObjectHandle(privateKey);
67
+ }
68
+ else {
69
+ const pvtKey = createPrivateKey(privateKey);
70
+ return getKeyObjectHandle(pvtKey);
71
+ }
72
+ })();
73
+ if (typeof buffer === "string") {
74
+ const { encoding = "utf8" } = privateKey;
75
+ buffer = Buffer.from(buffer, encoding);
76
+ }
77
+ return Buffer.from(cryptoImpl.privateEncrypt(key, buffer, getPaddingAndHash(privateKey)));
78
+ }
79
+ export function privateDecrypt(privateKey, buffer) {
80
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
81
+ if (privateKey === undefined) {
82
+ throw new ERR_MISSING_ARGS("privateKey");
83
+ }
84
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
85
+ if (buffer === undefined) {
86
+ throw new ERR_MISSING_ARGS("buffer");
87
+ }
88
+ const key = (() => {
89
+ if (privateKey instanceof CryptoKey) {
90
+ if (privateKey.type !== "private") {
91
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
92
+ }
93
+ return privateKey;
94
+ }
95
+ else if (isKeyObject(privateKey)) {
96
+ if (privateKey.type !== "private") {
97
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, "private");
98
+ }
99
+ return getKeyObjectHandle(privateKey);
100
+ }
101
+ else {
102
+ const pvtKey = createPrivateKey(privateKey);
103
+ return getKeyObjectHandle(pvtKey);
104
+ }
105
+ })();
106
+ if (typeof buffer === "string") {
107
+ const { encoding = "utf8" } = privateKey;
108
+ buffer = Buffer.from(buffer, encoding);
109
+ }
110
+ return Buffer.from(cryptoImpl.privateDecrypt(key, buffer, getPaddingAndHash(privateKey)));
111
+ }
112
+ export function publicEncrypt(key, buffer) {
113
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
114
+ if (key === undefined) {
115
+ throw new ERR_MISSING_ARGS("key");
116
+ }
117
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
118
+ if (buffer === undefined) {
119
+ throw new ERR_MISSING_ARGS("buffer");
120
+ }
121
+ const pkey = (() => {
122
+ if (key instanceof CryptoKey) {
123
+ if (key.type !== "public") {
124
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
125
+ }
126
+ return key;
127
+ }
128
+ else if (isKeyObject(key)) {
129
+ if (key.type !== "public") {
130
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
131
+ }
132
+ return getKeyObjectHandle(key);
133
+ }
134
+ else {
135
+ const pubKey = createPublicKey(key);
136
+ return getKeyObjectHandle(pubKey);
137
+ }
138
+ })();
139
+ if (typeof buffer === "string") {
140
+ const { encoding = "utf8" } = key;
141
+ buffer = Buffer.from(buffer, encoding);
142
+ }
143
+ return Buffer.from(cryptoImpl.publicEncrypt(pkey, buffer, getPaddingAndHash(key)));
144
+ }
145
+ export function publicDecrypt(key, buffer) {
146
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
147
+ if (key === undefined) {
148
+ throw new ERR_MISSING_ARGS("key");
149
+ }
150
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
151
+ if (buffer === undefined) {
152
+ throw new ERR_MISSING_ARGS("buffer");
153
+ }
154
+ const pkey = (() => {
155
+ if (key instanceof CryptoKey) {
156
+ if (key.type !== "public") {
157
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
158
+ }
159
+ return key;
160
+ }
161
+ else if (isKeyObject(key)) {
162
+ if (key.type !== "public") {
163
+ throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, "public");
164
+ }
165
+ return getKeyObjectHandle(key);
166
+ }
167
+ else {
168
+ const pubKey = createPublicKey(key);
169
+ return getKeyObjectHandle(pubKey);
170
+ }
171
+ })();
172
+ if (typeof buffer === "string") {
173
+ const { encoding = "utf8" } = key;
174
+ buffer = Buffer.from(buffer, encoding);
175
+ }
176
+ return Buffer.from(cryptoImpl.publicDecrypt(pkey, buffer, getPaddingAndHash(key)));
177
+ }
@@ -0,0 +1,25 @@
1
+ import { Buffer } from "node:buffer";
2
+ export type ArrayLike = ArrayBuffer | string | Buffer | ArrayBufferView;
3
+ export interface ValidatedScryptOptions {
4
+ password: ArrayBuffer | ArrayBufferView;
5
+ salt: ArrayBuffer | ArrayBufferView;
6
+ keylen: number;
7
+ N: number;
8
+ r: number;
9
+ p: number;
10
+ maxmem: number;
11
+ }
12
+ export interface ScryptOptions {
13
+ N?: number;
14
+ r?: number;
15
+ p?: number;
16
+ maxmem?: number;
17
+ cost?: number;
18
+ blockSize?: number;
19
+ parallelization?: number;
20
+ }
21
+ type Callback = (err: Error | null, derivedKey?: Buffer) => void;
22
+ type OptionsOrCallback = ScryptOptions | Callback;
23
+ export declare function scrypt(password: ArrayLike, salt: ArrayLike, keylen: number, options: OptionsOrCallback, callback?: OptionsOrCallback): void;
24
+ export declare function scryptSync(password: ArrayLike, salt: ArrayLike, keylen: number, options?: ScryptOptions): Buffer;
25
+ export {};
@@ -0,0 +1,152 @@
1
+ // Copyright (c) 2017-2023 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
+ // Validation/callback behavior from pinned workerd; algorithm from scrypt-js.
26
+ import { syncScrypt } from "scrypt-js";
27
+ import { Buffer } from "node:buffer";
28
+ import { validateInteger, arrayBufferOrView as getArrayBufferOrView, inputBytes, } from "./validation.js";
29
+ import { validateFunction, validateUint32 } from "../net/validators.js";
30
+ import { ERR_INVALID_ARG_VALUE } from "../net/errors.js";
31
+ const kMaxLength = 2147483647;
32
+ const cryptoImpl = {
33
+ getScrypt(password, salt, N, r, p, maxmem, length) {
34
+ const passwordBytes = inputBytes(password, "getScrypt", 1), saltBytes = inputBytes(salt, "getScrypt", 2);
35
+ // JSG validates the uint32 binding argument before entering the backend.
36
+ if (maxmem > 0xffffffff)
37
+ throw new TypeError("Value out of range. Must be less than or equal to 4294967295.");
38
+ // Pinned workerd IsolateLimitEnforcer::checkScryptCost (2^20).
39
+ if (N > 1048576 || r > 1048576 || p > 1048576 || N * r * p > 1048576)
40
+ throw new RangeError("Scrypt failed: cost exceeds maximum (1048576).");
41
+ if (N < 2 ||
42
+ (N & (N - 1)) !== 0 ||
43
+ N >= 2 ** (16 * r) ||
44
+ r * p >= 2 ** 30 ||
45
+ r < 1 ||
46
+ p < 1 ||
47
+ 128 * N * r + 128 * r * p + 256 * r > maxmem) {
48
+ throw new Error("Scrypt failed");
49
+ }
50
+ return new Uint8Array(syncScrypt(passwordBytes, saltBytes, N, r, p, length))
51
+ .buffer;
52
+ },
53
+ };
54
+ const defaults = {
55
+ N: 16384,
56
+ r: 8,
57
+ p: 1,
58
+ maxmem: 32 << 20,
59
+ };
60
+ function validateParameters(password, salt, keylen, options) {
61
+ // TODO(soon): Add support for KeyObject input.
62
+ const passwordBytes = getArrayBufferOrView(password, "password");
63
+ const saltBytes = getArrayBufferOrView(salt, "salt");
64
+ if (passwordBytes.byteLength >= kMaxLength) {
65
+ throw new ERR_INVALID_ARG_VALUE("password", passwordBytes, "is too big");
66
+ }
67
+ if (saltBytes.byteLength >= kMaxLength) {
68
+ throw new ERR_INVALID_ARG_VALUE("salt", saltBytes, "is too big");
69
+ }
70
+ validateInteger(keylen, "keylen", 0, kMaxLength);
71
+ let { N, r, p, maxmem } = defaults;
72
+ if (options && options !== defaults) {
73
+ const has_N = options.N !== undefined;
74
+ if (has_N) {
75
+ N = options.N;
76
+ validateUint32(N, "N");
77
+ }
78
+ if (options.cost !== undefined) {
79
+ if (has_N)
80
+ throw new ERR_INVALID_ARG_VALUE("cost", options.cost);
81
+ N = options.cost;
82
+ validateUint32(N, "cost");
83
+ }
84
+ const has_r = options.r !== undefined;
85
+ if (has_r) {
86
+ r = options.r;
87
+ validateUint32(r, "r");
88
+ }
89
+ if (options.blockSize !== undefined) {
90
+ if (has_r)
91
+ throw new ERR_INVALID_ARG_VALUE("blockSize", options.blockSize);
92
+ r = options.blockSize;
93
+ validateUint32(r, "blockSize");
94
+ }
95
+ const has_p = options.p !== undefined;
96
+ if (has_p) {
97
+ p = options.p;
98
+ validateUint32(p, "p");
99
+ }
100
+ if (options.parallelization !== undefined) {
101
+ if (has_p)
102
+ throw new ERR_INVALID_ARG_VALUE("parallelization", options.parallelization);
103
+ p = options.parallelization;
104
+ validateUint32(p, "parallelization");
105
+ }
106
+ if (options.maxmem !== undefined) {
107
+ maxmem = options.maxmem;
108
+ validateInteger(maxmem, "maxmem", 0);
109
+ }
110
+ if (N === 0)
111
+ N = defaults.N;
112
+ if (r === 0)
113
+ r = defaults.r;
114
+ if (p === 0)
115
+ p = defaults.p;
116
+ if (maxmem === 0)
117
+ maxmem = defaults.maxmem;
118
+ }
119
+ return {
120
+ password: passwordBytes,
121
+ salt: saltBytes,
122
+ keylen,
123
+ N: N,
124
+ r: r,
125
+ p: p,
126
+ maxmem: maxmem,
127
+ };
128
+ }
129
+ export function scrypt(password, salt, keylen, options, callback = defaults) {
130
+ if (callback === defaults) {
131
+ callback = options;
132
+ options = defaults;
133
+ }
134
+ const { password: passwordBytes, salt: saltBytes, keylen: outputLength, N, r, p, maxmem, } = validateParameters(password, salt, keylen, options);
135
+ validateFunction(callback, "callback");
136
+ new Promise((res, rej) => {
137
+ try {
138
+ res(cryptoImpl.getScrypt(passwordBytes, saltBytes, N, r, p, maxmem, outputLength));
139
+ }
140
+ catch (err) {
141
+ rej(err);
142
+ }
143
+ }).then((val) => {
144
+ callback(null, Buffer.from(val));
145
+ }, (err) => {
146
+ callback(err);
147
+ });
148
+ }
149
+ export function scryptSync(password, salt, keylen, options) {
150
+ const { password: passwordBytes, salt: saltBytes, keylen: outputLength, N, r, p, maxmem, } = validateParameters(password, salt, keylen, options);
151
+ return Buffer.from(cryptoImpl.getScrypt(passwordBytes, saltBytes, N, r, p, maxmem, outputLength));
152
+ }
@@ -0,0 +1,8 @@
1
+ import { NodeError } from "../net/error-base.js";
2
+ export { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE } from "../net/errors.js";
3
+ export declare class ERR_CRYPTO_SIGN_KEY_REQUIRED extends NodeError {
4
+ constructor();
5
+ }
6
+ export declare class ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE extends NodeError {
7
+ constructor(actual: string, expected: string);
8
+ }
@@ -0,0 +1,13 @@
1
+ // Adapted from workerd internal_errors.ts (Apache-2.0 / Node MIT).
2
+ import { NodeError } from "../net/error-base.js";
3
+ export { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE } from "../net/errors.js";
4
+ export class ERR_CRYPTO_SIGN_KEY_REQUIRED extends NodeError {
5
+ constructor() {
6
+ super("ERR_CRYPTO_SIGN_KEY_REQUIRED", "No key provided to sign");
7
+ }
8
+ }
9
+ export class ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE extends NodeError {
10
+ constructor(actual, expected) {
11
+ super("ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE", `Invalid key object type ${actual}, expected ${expected}.`);
12
+ }
13
+ }
@@ -0,0 +1,22 @@
1
+ import { Buffer } from "node:buffer";
2
+ export declare class SignHandle {
3
+ private algorithm;
4
+ private hash;
5
+ constructor(algorithm: string);
6
+ update(data: Uint8Array): void;
7
+ sign(value: any, padding?: number, saltLength?: number, encoding?: number): Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>;
8
+ }
9
+ export declare class VerifyHandle {
10
+ private algorithm;
11
+ private hash;
12
+ constructor(algorithm: string);
13
+ update(data: Uint8Array): void;
14
+ verify(value: any, signature: ArrayBuffer | ArrayBufferView, padding?: number, saltLength?: number, encoding?: number): boolean;
15
+ }
16
+ declare const _default: {
17
+ SignHandle: typeof SignHandle;
18
+ VerifyHandle: typeof VerifyHandle;
19
+ signOneShot(key: any, algorithm: any, data: any, padding?: number, saltLength?: number, encoding?: number): any;
20
+ verifyOneShot(key: any, algorithm: any, data: any, signature: any, padding?: number, saltLength?: number, encoding?: number): any;
21
+ };
22
+ export default _default;
@@ -0,0 +1,149 @@
1
+ import { bufferBytes, viewBytes } from "../../shared/buffer-source.js";
2
+ import { isAnyBuffer } from "./validation.js";
3
+ import { signatureDigest } from "./digests.js";
4
+ import { Buffer } from "node:buffer";
5
+ import { NativeDigest } from "./native-crypto.js";
6
+ import { keyStates } from "./key-state.js";
7
+ import { KeyObject } from "./keys.js";
8
+ import { sign, verify } from "./asymmetric.js";
9
+ import { der, integer } from "./key-formats.js";
10
+ function key(value) {
11
+ if (value instanceof globalThis.CryptoKey)
12
+ value = KeyObject.from(value);
13
+ const result = keyStates.get(value)?.asymmetric;
14
+ if (!result)
15
+ throw Object.assign(new Error("Synchronous access to opaque host key material is unavailable"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
16
+ return result;
17
+ }
18
+ function rawSignature(data, width) {
19
+ let offset = 0;
20
+ const item = (tag) => {
21
+ if (data[offset++] !== tag)
22
+ throw new Error("Invalid DER signature");
23
+ let length = data[offset++];
24
+ if (length & 128) {
25
+ const count = length & 127;
26
+ length = 0;
27
+ for (let i = 0; i < count; i++)
28
+ length = length * 256 + data[offset++];
29
+ }
30
+ if (offset + length > data.length)
31
+ throw new Error("Invalid DER signature");
32
+ return length;
33
+ };
34
+ const length = item(48);
35
+ if (offset + length !== data.length)
36
+ throw new Error("Invalid DER signature");
37
+ const output = Buffer.alloc(width * 2);
38
+ for (let part = 0; part < 2; part++) {
39
+ let length = item(2);
40
+ while (length > width && data[offset] === 0) {
41
+ offset++;
42
+ length--;
43
+ }
44
+ if (length > width)
45
+ throw new Error("Invalid DER signature");
46
+ output.set(data.subarray(offset, offset + length), part * width + width - length);
47
+ offset += length;
48
+ }
49
+ return output;
50
+ }
51
+ function rsaParameters(type, padding, saltLength, verifying) {
52
+ if (type !== "rsa" && type !== "rsa-pss")
53
+ return;
54
+ const mode = padding ?? (type === "rsa-pss" ? 6 : 1);
55
+ if (![1, 3, 6].includes(mode) ||
56
+ (mode === 6 && saltLength !== undefined && saltLength < -2))
57
+ throw new Error("Failed to set RSA parameters for signature");
58
+ if (mode === 3)
59
+ throw new Error(verifying
60
+ ? "Failed to set digest context for signature verification"
61
+ : "Failed to set signature digest");
62
+ }
63
+ export class SignHandle {
64
+ algorithm;
65
+ hash;
66
+ constructor(algorithm) {
67
+ this.algorithm = algorithm;
68
+ this.algorithm = signatureDigest(algorithm);
69
+ this.hash = new NativeDigest(this.algorithm);
70
+ }
71
+ update(data) {
72
+ this.hash.update(viewBytes(data));
73
+ }
74
+ sign(value, padding, saltLength, encoding) {
75
+ this.hash.assertActive();
76
+ const native = key(value), hash = this.hash.final();
77
+ if (native.type === "dsa")
78
+ throw new Error("Signing with DSA keys is not currently supported");
79
+ if (native.type === "ed25519")
80
+ throw new Error("Failed to set signature digest");
81
+ if (native.type === "x25519")
82
+ throw new Error("Failed to initialize signing context");
83
+ rsaParameters(native.type, padding, saltLength, false);
84
+ let signed;
85
+ try {
86
+ signed = native.signHash(this.algorithm, hash, native.type.startsWith("rsa") ? padding : undefined, saltLength ?? -1);
87
+ }
88
+ catch {
89
+ throw new Error("Failed to generate signature");
90
+ }
91
+ return encoding === 1 && native.type === "ec"
92
+ ? rawSignature(signed, Math.ceil(native.bits / 8))
93
+ : signed;
94
+ }
95
+ }
96
+ export class VerifyHandle {
97
+ algorithm;
98
+ hash;
99
+ constructor(algorithm) {
100
+ this.algorithm = algorithm;
101
+ this.algorithm = signatureDigest(algorithm);
102
+ this.hash = new NativeDigest(this.algorithm, "Verification");
103
+ }
104
+ update(data) {
105
+ this.hash.update(viewBytes(data));
106
+ }
107
+ verify(value, signature, padding, saltLength, encoding) {
108
+ if (!isAnyBuffer(signature) && !ArrayBuffer.isView(signature))
109
+ throw new TypeError("Failed to execute 'verify' on 'VerifyHandle': parameter 2 is not of type 'BufferSource'.");
110
+ this.hash.assertActive();
111
+ const native = key(value);
112
+ if (native.type === "ed25519" || native.type === "ed448")
113
+ throw new Error("Unsupported operation for this key");
114
+ let signatureBytes = Buffer.from(isAnyBuffer(signature) ? bufferBytes(signature) : viewBytes(signature));
115
+ const hash = this.hash.final();
116
+ if (native.type === "dsa")
117
+ throw new Error("Verifying with DSA keys is not currently supported");
118
+ if (native.type === "x25519")
119
+ throw new Error("Failed to initialize key for verification");
120
+ rsaParameters(native.type, padding, saltLength, true);
121
+ if (encoding === 1 && native.type === "ec") {
122
+ const width = Math.ceil(native.bits / 8);
123
+ if (signatureBytes.length !== width * 2)
124
+ return false;
125
+ signatureBytes = der(48, integer(signatureBytes.subarray(0, width)), integer(signatureBytes.subarray(width)));
126
+ }
127
+ return native.verifyHash(this.algorithm, hash, signatureBytes, native.type.startsWith("rsa") ? padding : undefined, saltLength ?? -1);
128
+ }
129
+ }
130
+ export default {
131
+ SignHandle,
132
+ VerifyHandle,
133
+ signOneShot(key, algorithm, data, padding, saltLength, encoding) {
134
+ return sign(algorithm, data, {
135
+ key,
136
+ padding,
137
+ saltLength,
138
+ dsaEncoding: encoding === 1 ? "ieee-p1363" : "der",
139
+ });
140
+ },
141
+ verifyOneShot(key, algorithm, data, signature, padding, saltLength, encoding) {
142
+ return verify(algorithm, data, {
143
+ key,
144
+ padding,
145
+ saltLength,
146
+ dsaEncoding: encoding === 1 ? "ieee-p1363" : "der",
147
+ }, signature);
148
+ },
149
+ };
@@ -0,0 +1,34 @@
1
+ import { type SignHandle, type VerifyHandle } from "./sign-handle.js";
2
+ import { Buffer } from "node:buffer";
3
+ import type { Writable as WritableType, WritableOptions } from "node:stream";
4
+ export interface SignOptions extends WritableOptions {
5
+ }
6
+ export interface Sign extends WritableType {
7
+ [kHandle]: SignHandle;
8
+ update(data: string | ArrayBufferView, encoding?: BufferEncoding): this;
9
+ sign(privateKey: any, encoding?: undefined | "buffer"): Buffer;
10
+ sign(privateKey: any, encoding: BufferEncoding): string;
11
+ }
12
+ export interface Verify extends WritableType {
13
+ [kHandle]: VerifyHandle;
14
+ update(data: string | ArrayBufferView, encoding?: BufferEncoding): this;
15
+ verify(publicKey: any, signature: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView, encoding?: BufferEncoding): boolean;
16
+ }
17
+ declare const kHandle: unique symbol;
18
+ export declare const Sign: {
19
+ new (algorithm: string, options?: SignOptions): Sign;
20
+ (algorithm: string, options?: SignOptions): Sign;
21
+ };
22
+ export declare const Verify: {
23
+ new (algorithm: string, options?: SignOptions): Verify;
24
+ (algorithm: string, options?: SignOptions): Verify;
25
+ };
26
+ export declare function createSign(algorithm: string, options?: SignOptions): Sign;
27
+ export declare function createVerify(algorithm: string, options?: SignOptions): Verify;
28
+ type SignCallback = (err: any, signature?: Buffer) => void;
29
+ type VerifyCallback = (err: any, valid?: boolean) => void;
30
+ export declare function sign(algorithm: string | null | undefined, data: ArrayBufferView, options: any): Buffer;
31
+ export declare function sign(algorithm: string | null | undefined, data: ArrayBufferView, options: any, callback: SignCallback): void;
32
+ export declare function verify(algorithm: string | null | undefined, data: ArrayBufferView, options: any, signature: ArrayBufferView): boolean;
33
+ export declare function verify(algorithm: string | null | undefined, data: ArrayBufferView, options: any, signature: ArrayBufferView, callback: VerifyCallback): void;
34
+ export {};