GeezerVison is showing github.com — crypto: expose Web Crypto API on the global scope · nodejs/node@88cbceb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -338,5 +338,9 @@ module.exports = { | 338 | 338 | Headers: 'readable', | 339 | 339 | Request: 'readable', | 340 | 340 | Response: 'readable', | | 341 | + crypto: 'readable', | | 342 | + Crypto: 'readable', | | 343 | + CryptoKey: 'readable', | | 344 | + SubtleCrypto: 'readable', | 341 | 345 | }, | 342 | 346 | }; |
Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -288,6 +288,14 @@ added: v17.5.0 | 288 | 288 |
| 289 | 289 | Enable experimental support for the [Fetch API][]. | 290 | 290 |
| | 291 | +### `--experimental-global-webcrypto` | | 292 | + | | 293 | +<!-- YAML | | 294 | +added: REPLACEME | | 295 | +--> | | 296 | + | | 297 | +Expose the [Web Crypto API][] on the global scope. | | 298 | + | 291 | 299 | ### `--experimental-import-meta-resolve` | 292 | 300 |
| 293 | 301 | <!-- YAML | @@ -1578,6 +1586,7 @@ Node.js options that are allowed are: | 1578 | 1586 | * `--enable-source-maps` | 1579 | 1587 | * `--experimental-abortcontroller` | 1580 | 1588 | * `--experimental-fetch` | | 1589 | +* `--experimental-global-webcrypto` | 1581 | 1590 | * `--experimental-import-meta-resolve` | 1582 | 1591 | * `--experimental-json-modules` | 1583 | 1592 | * `--experimental-loader` | @@ -1980,6 +1989,7 @@ $ node --max-old-space-size=1536 index.js | 1980 | 1989 | [Source Map]: https://sourcemaps.info/spec.html | 1981 | 1990 | [Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity | 1982 | 1991 | [V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html | | 1992 | +[Web Crypto API]: webcrypto.md | 1983 | 1993 | [`"type"`]: packages.md#type | 1984 | 1994 | [`--cpu-prof-dir`]: #--cpu-prof-dir | 1985 | 1995 | [`--diagnostic-dir`]: #--diagnostic-dirdirectory | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/crypto.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,8 @@ calling `require('crypto')` will result in an error being thrown. | 41 | 41 |
| 42 | 42 | When using CommonJS, the error thrown can be caught using try/catch: | 43 | 43 |
| | 44 | +<!-- eslint-skip --> | | 45 | + | 44 | 46 | ```cjs | 45 | 47 | let crypto; | 46 | 48 | try { | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/globals.md +52Lines changed: 52 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -281,6 +281,43 @@ added: v0.1.100 | 281 | 281 |
| 282 | 282 | Used to print to stdout and stderr. See the [`console`][] section. | 283 | 283 |
| | 284 | +## `Crypto` | | 285 | + | | 286 | +<!-- YAML | | 287 | +added: REPLACEME | | 288 | +--> | | 289 | + | | 290 | +> Stability: 1 - Experimental. Enable this API with the | | 291 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 292 | +
| | 293 | +A browser-compatible implementation of {Crypto}. This global is available | | 294 | +only if the Node.js binary was compiled with including support for the | | 295 | +`crypto` module. | | 296 | + | | 297 | +## `crypto` | | 298 | + | | 299 | +<!-- YAML | | 300 | +added: REPLACEME | | 301 | +--> | | 302 | + | | 303 | +> Stability: 1 - Experimental. Enable this API with the | | 304 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 305 | +
| | 306 | +A browser-compatible implementation of the [Web Crypto API][]. | | 307 | + | | 308 | +## `CryptoKey` | | 309 | + | | 310 | +<!-- YAML | | 311 | +added: REPLACEME | | 312 | +--> | | 313 | + | | 314 | +> Stability: 1 - Experimental. Enable this API with the | | 315 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 316 | +
| | 317 | +A browser-compatible implementation of {CryptoKey}. This global is available | | 318 | +only if the Node.js binary was compiled with including support for the | | 319 | +`crypto` module. | | 320 | + | 284 | 321 | ## `Event` | 285 | 322 |
| 286 | 323 | <!-- YAML | @@ -508,6 +545,19 @@ added: v17.0.0 | 508 | 545 |
| 509 | 546 | The WHATWG [`structuredClone`][] method. | 510 | 547 |
| | 548 | +## `SubtleCrypto` | | 549 | + | | 550 | +<!-- YAML | | 551 | +added: REPLACEME | | 552 | +--> | | 553 | + | | 554 | +> Stability: 1 - Experimental. Enable this API with the | | 555 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 556 | +
| | 557 | +A browser-compatible implementation of {SubtleCrypto}. This global is available | | 558 | +only if the Node.js binary was compiled with including support for the | | 559 | +`crypto` module. | | 560 | + | 511 | 561 | ## `DOMException` | 512 | 562 |
| 513 | 563 | <!-- YAML | @@ -572,7 +622,9 @@ The object that acts as the namespace for all W3C | 572 | 622 | [WebAssembly][webassembly-org] related functionality. See the | 573 | 623 | [Mozilla Developer Network][webassembly-mdn] for usage and compatibility. | 574 | 624 |
| | 625 | +[Web Crypto API]: webcrypto.md | 575 | 626 | [`--experimental-fetch`]: cli.md#--experimental-fetch | | 627 | +[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto | 576 | 628 | [`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController | 577 | 629 | [`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException | 578 | 630 | [`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -142,6 +142,9 @@ Enable Source Map V3 support for stack traces. | 142 | 142 | .It Fl -experimental-fetch | 143 | 143 | Enable experimental support for the Fetch API. | 144 | 144 | . | | 145 | +.It Fl -experimental-global-webcrypto | | 146 | +Expose the Web Crypto API on the global scope. | | 147 | +. | 145 | 148 | .It Fl -experimental-import-meta-resolve | 146 | 149 | Enable experimental ES modules support for import.meta.resolve(). | 147 | 150 | . | |
Collapse file Copy file name to clipboardExpand all lines: lib/.eslintrc.yaml +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,6 +75,12 @@ rules: | 75 | 75 | message: "Use `const { atob } = require('buffer');` instead of the global." | 76 | 76 | - name: btoa | 77 | 77 | message: "Use `const { btoa } = require('buffer');` instead of the global." | | 78 | + - name: crypto | | 79 | + message: "Use `const { crypto } = require('internal/crypto/webcrypto');` instead of the global." | | 80 | + - name: Crypto | | 81 | + message: "Use `const { Crypto } = require('internal/crypto/webcrypto');` instead of the global." | | 82 | + - name: CryptoKey | | 83 | + message: "Use `const { CryptoKey } = require('internal/crypto/webcrypto');` instead of the global." | 78 | 84 | - name: global | 79 | 85 | message: "Use `const { globalThis } = primordials;` instead of `global`." | 80 | 86 | - name: globalThis | | 85 | 91 | message: "Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global." | 86 | 92 | - name: structuredClone | 87 | 93 | message: "Use `const { structuredClone } = require('internal/structured_clone');` instead of the global." | | 94 | + - name: SubtleCrypto | | 95 | + message: "Use `const { SubtleCrypto } = require('internal/crypto/webcrypto');` instead of the global." | 88 | 96 | # Custom rules in tools/eslint-rules | 89 | 97 | node-core/lowercase-name-for-primitive: error | 90 | 98 | node-core/non-ascii-character: error | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | NumberParseInt, | 5 | 5 | ObjectDefineProperty, | | 6 | + ObjectGetOwnPropertyDescriptor, | 6 | 7 | SafeMap, | 7 | 8 | SafeWeakMap, | 8 | 9 | StringPrototypeStartsWith, | @@ -34,6 +35,7 @@ function prepareMainThreadExecution(expandArgv1 = false) { | 34 | 35 | setupInspectorHooks(); | 35 | 36 | setupWarningHandler(); | 36 | 37 | setupFetch(); | | 38 | + setupWebCrypto(); | 37 | 39 |
| 38 | 40 | // Resolve the coverage directory to an absolute path, and | 39 | 41 | // overwrite process.env so that the original path gets passed | @@ -180,6 +182,44 @@ function setupFetch() { | 180 | 182 | }); | 181 | 183 | } | 182 | 184 |
| | 185 | +// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is | | 186 | +// removed. | | 187 | +function setupWebCrypto() { | | 188 | + if (!getOptionValue('--experimental-global-webcrypto')) { | | 189 | + return; | | 190 | + } | | 191 | + | | 192 | + let webcrypto; | | 193 | + ObjectDefineProperty(globalThis, 'crypto', | | 194 | + ObjectGetOwnPropertyDescriptor({ | | 195 | + get crypto() { | | 196 | + webcrypto ??= require('internal/crypto/webcrypto'); | | 197 | + return webcrypto.crypto; | | 198 | + } | | 199 | + }, 'crypto')); | | 200 | + if (internalBinding('config').hasOpenSSL) { | | 201 | + webcrypto ??= require('internal/crypto/webcrypto'); | | 202 | + ObjectDefineProperty(globalThis, 'Crypto', { | | 203 | + writable: true, | | 204 | + enumerable: false, | | 205 | + configurable: true, | | 206 | + value: webcrypto.Crypto | | 207 | + }); | | 208 | + ObjectDefineProperty(globalThis, 'CryptoKey', { | | 209 | + writable: true, | | 210 | + enumerable: false, | | 211 | + configurable: true, | | 212 | + value: webcrypto.CryptoKey | | 213 | + }); | | 214 | + ObjectDefineProperty(globalThis, 'SubtleCrypto', { | | 215 | + writable: true, | | 216 | + enumerable: false, | | 217 | + configurable: true, | | 218 | + value: webcrypto.SubtleCrypto | | 219 | + }); | | 220 | + } | | 221 | +} | | 222 | + | 183 | 223 | // Setup User-facing NODE_V8_COVERAGE environment variable that writes | 184 | 224 | // ScriptCoverage to a specified file. | 185 | 225 | function setupCoverageHooks(dir) { | @@ -521,6 +561,7 @@ module.exports = { | 521 | 561 | setupCoverageHooks, | 522 | 562 | setupWarningHandler, | 523 | 563 | setupFetch, | | 564 | + setupWebCrypto, | 524 | 565 | setupDebugEnv, | 525 | 566 | setupPerfHooks, | 526 | 567 | prepareMainThreadExecution, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -808,6 +808,7 @@ ObjectDefineProperties( | 808 | 808 |
| 809 | 809 | module.exports = { | 810 | 810 | Crypto, | | 811 | + CryptoKey, | 811 | 812 | SubtleCrypto, | 812 | 813 | crypto, | 813 | 814 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/worker_thread.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ const { | 18 | 18 | setupInspectorHooks, | 19 | 19 | setupWarningHandler, | 20 | 20 | setupFetch, | | 21 | + setupWebCrypto, | 21 | 22 | setupDebugEnv, | 22 | 23 | setupPerfHooks, | 23 | 24 | initializeDeprecations, | @@ -69,6 +70,7 @@ setupDebugEnv(); | 69 | 70 |
| 70 | 71 | setupWarningHandler(); | 71 | 72 | setupFetch(); | | 73 | +setupWebCrypto(); | 72 | 74 | initializeSourceMapsHandlers(); | 73 | 75 |
| 74 | 76 | // Since worker threads cannot switch cwd, we do not need to | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -319,6 +319,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 319 | 319 | "experimental Fetch API", | 320 | 320 | &EnvironmentOptions::experimental_fetch, | 321 | 321 | kAllowedInEnvironment); | | 322 | + AddOption("--experimental-global-webcrypto", | | 323 | + "expose experimental Web Crypto API on the global scope", | | 324 | + &EnvironmentOptions::experimental_global_web_crypto, | | 325 | + kAllowedInEnvironment); | 322 | 326 | AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment); | 323 | 327 | AddOption("--experimental-loader", | 324 | 328 | "use the specified module as a custom loader", | |
|