GeezerVison is showing github.com — crypto: expose Web Crypto API on the global scope · hernanc/node@849991c
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 |
|---|
@@ -340,5 +340,9 @@ module.exports = { | 340 | 340 | Headers: 'readable', | 341 | 341 | Request: 'readable', | 342 | 342 | Response: 'readable', | | 343 | + crypto: 'readable', | | 344 | + Crypto: 'readable', | | 345 | + CryptoKey: 'readable', | | 346 | + SubtleCrypto: 'readable', | 343 | 347 | }, | 344 | 348 | }; |
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 | @@ -1580,6 +1588,7 @@ Node.js options that are allowed are: | 1580 | 1588 | * `--enable-source-maps` | 1581 | 1589 | * `--experimental-abortcontroller` | 1582 | 1590 | * `--experimental-fetch` | | 1591 | +* `--experimental-global-webcrypto` | 1583 | 1592 | * `--experimental-import-meta-resolve` | 1584 | 1593 | * `--experimental-json-modules` | 1585 | 1594 | * `--experimental-loader` | @@ -1982,6 +1991,7 @@ $ node --max-old-space-size=1536 index.js | 1982 | 1991 | [Source Map]: https://sourcemaps.info/spec.html | 1983 | 1992 | [Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity | 1984 | 1993 | [V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html | | 1994 | +[Web Crypto API]: webcrypto.md | 1985 | 1995 | [`"type"`]: packages.md#type | 1986 | 1996 | [`--cpu-prof-dir`]: #--cpu-prof-dir | 1987 | 1997 | [`--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 |
|---|
@@ -307,6 +307,43 @@ added: v0.1.100 | 307 | 307 |
| 308 | 308 | Used to print to stdout and stderr. See the [`console`][] section. | 309 | 309 |
| | 310 | +## `Crypto` | | 311 | + | | 312 | +<!-- YAML | | 313 | +added: REPLACEME | | 314 | +--> | | 315 | + | | 316 | +> Stability: 1 - Experimental. Enable this API with the | | 317 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 318 | +
| | 319 | +A browser-compatible implementation of {Crypto}. This global is available | | 320 | +only if the Node.js binary was compiled with including support for the | | 321 | +`crypto` module. | | 322 | + | | 323 | +## `crypto` | | 324 | + | | 325 | +<!-- YAML | | 326 | +added: REPLACEME | | 327 | +--> | | 328 | + | | 329 | +> Stability: 1 - Experimental. Enable this API with the | | 330 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 331 | +
| | 332 | +A browser-compatible implementation of the [Web Crypto API][]. | | 333 | + | | 334 | +## `CryptoKey` | | 335 | + | | 336 | +<!-- YAML | | 337 | +added: REPLACEME | | 338 | +--> | | 339 | + | | 340 | +> Stability: 1 - Experimental. Enable this API with the | | 341 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 342 | +
| | 343 | +A browser-compatible implementation of {CryptoKey}. This global is available | | 344 | +only if the Node.js binary was compiled with including support for the | | 345 | +`crypto` module. | | 346 | + | 310 | 347 | ## `Event` | 311 | 348 |
| 312 | 349 | <!-- YAML | @@ -534,6 +571,19 @@ added: v17.0.0 | 534 | 571 |
| 535 | 572 | The WHATWG [`structuredClone`][] method. | 536 | 573 |
| | 574 | +## `SubtleCrypto` | | 575 | + | | 576 | +<!-- YAML | | 577 | +added: REPLACEME | | 578 | +--> | | 579 | + | | 580 | +> Stability: 1 - Experimental. Enable this API with the | | 581 | +> [`--experimental-global-webcrypto`][] CLI flag. | | 582 | +
| | 583 | +A browser-compatible implementation of {SubtleCrypto}. This global is available | | 584 | +only if the Node.js binary was compiled with including support for the | | 585 | +`crypto` module. | | 586 | + | 537 | 587 | ## `DOMException` | 538 | 588 |
| 539 | 589 | <!-- YAML | @@ -598,7 +648,9 @@ The object that acts as the namespace for all W3C | 598 | 648 | [WebAssembly][webassembly-org] related functionality. See the | 599 | 649 | [Mozilla Developer Network][webassembly-mdn] for usage and compatibility. | 600 | 650 |
| | 651 | +[Web Crypto API]: webcrypto.md | 601 | 652 | [`--experimental-fetch`]: cli.md#--experimental-fetch | | 653 | +[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto | 602 | 654 | [`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController | 603 | 655 | [`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException | 604 | 656 | [`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 |
|---|
@@ -79,6 +79,12 @@ rules: | 79 | 79 | message: "Use `const { atob } = require('buffer');` instead of the global." | 80 | 80 | - name: btoa | 81 | 81 | message: "Use `const { btoa } = require('buffer');` instead of the global." | | 82 | + - name: crypto | | 83 | + message: "Use `const { crypto } = require('internal/crypto/webcrypto');` instead of the global." | | 84 | + - name: Crypto | | 85 | + message: "Use `const { Crypto } = require('internal/crypto/webcrypto');` instead of the global." | | 86 | + - name: CryptoKey | | 87 | + message: "Use `const { CryptoKey } = require('internal/crypto/webcrypto');` instead of the global." | 82 | 88 | - name: global | 83 | 89 | message: "Use `const { globalThis } = primordials;` instead of `global`." | 84 | 90 | - name: globalThis | | 89 | 95 | message: "Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global." | 90 | 96 | - name: structuredClone | 91 | 97 | message: "Use `const { structuredClone } = require('internal/structured_clone');` instead of the global." | | 98 | + - name: SubtleCrypto | | 99 | + message: "Use `const { SubtleCrypto } = require('internal/crypto/webcrypto');` instead of the global." | 92 | 100 | # Custom rules in tools/eslint-rules | 93 | 101 | node-core/lowercase-name-for-primitive: error | 94 | 102 | node-core/non-ascii-character: error | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js +26Lines changed: 26 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, | @@ -36,6 +37,7 @@ function prepareMainThreadExecution(expandArgv1 = false) { | 36 | 37 | setupInspectorHooks(); | 37 | 38 | setupWarningHandler(); | 38 | 39 | setupFetch(); | | 40 | + setupWebCrypto(); | 39 | 41 |
| 40 | 42 | // Resolve the coverage directory to an absolute path, and | 41 | 43 | // overwrite process.env so that the original path gets passed | @@ -162,6 +164,29 @@ function setupFetch() { | 162 | 164 | exposeInterface(globalThis, 'Response', undici.Response); | 163 | 165 | } | 164 | 166 |
| | 167 | +// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is | | 168 | +// removed. | | 169 | +function setupWebCrypto() { | | 170 | + if (!getOptionValue('--experimental-global-webcrypto')) { | | 171 | + return; | | 172 | + } | | 173 | + | | 174 | + let webcrypto; | | 175 | + ObjectDefineProperty(globalThis, 'crypto', | | 176 | + ObjectGetOwnPropertyDescriptor({ | | 177 | + get crypto() { | | 178 | + webcrypto ??= require('internal/crypto/webcrypto'); | | 179 | + return webcrypto.crypto; | | 180 | + } | | 181 | + }, 'crypto')); | | 182 | + if (internalBinding('config').hasOpenSSL) { | | 183 | + webcrypto ??= require('internal/crypto/webcrypto'); | | 184 | + exposeInterface(globalThis, 'Crypto', webcrypto.Crypto); | | 185 | + exposeInterface(globalThis, 'CryptoKey', webcrypto.CryptoKey); | | 186 | + exposeInterface(globalThis, 'SubtleCrypto', webcrypto.SubtleCrypto); | | 187 | + } | | 188 | +} | | 189 | + | 165 | 190 | // Setup User-facing NODE_V8_COVERAGE environment variable that writes | 166 | 191 | // ScriptCoverage to a specified file. | 167 | 192 | function setupCoverageHooks(dir) { | @@ -503,6 +528,7 @@ module.exports = { | 503 | 528 | setupCoverageHooks, | 504 | 529 | setupWarningHandler, | 505 | 530 | setupFetch, | | 531 | + setupWebCrypto, | 506 | 532 | setupDebugEnv, | 507 | 533 | setupPerfHooks, | 508 | 534 | 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", | |
|