Skip to content

Commit fb4d8db

Browse files
panvaaduh95
authored andcommitted
typings: add typing for crypto
Signed-off-by: Filip Skokan <[email protected]> PR-URL: #64122 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent c40b615 commit fb4d8db

3 files changed

Lines changed: 1028 additions & 13 deletions

File tree

lib/internal/crypto/util.js

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,19 @@ const validateByteSource = hideStackFrames((val, name) => {
668668
val);
669669
});
670670

671-
// CryptoJob constructors can synchronously throw while running their native
672-
// AdditionalConfig hook. WebCrypto needs those operation-specific setup
673-
// failures to reject with an OperationError.
671+
/**
672+
* @template T
673+
* @typedef {{ run(): Promise<T> }} WebCryptoJob
674+
*/
675+
676+
/**
677+
* CryptoJob constructors can synchronously throw while running their native
678+
* AdditionalConfig hook. WebCrypto needs those operation-specific setup
679+
* failures to reject with an OperationError.
680+
* @template T
681+
* @param {() => WebCryptoJob<T>} getJob
682+
* @returns {Promise<T>}
683+
*/
674684
function jobPromise(getJob) {
675685
try {
676686
return getJob().run();
@@ -681,10 +691,14 @@ function jobPromise(getJob) {
681691
}
682692
}
683693

684-
// Temporarily shadow inherited then accessors on WebCrypto result objects.
685-
// Promise resolution reads "then" synchronously for thenable assimilation.
686-
// Returning an own undefined data property keeps that lookup from reaching
687-
// user-mutated prototypes.
694+
/**
695+
* Temporarily shadow inherited then accessors on WebCrypto result objects.
696+
* Promise resolution reads "then" synchronously for thenable assimilation.
697+
* Returning an own undefined data property keeps that lookup from reaching
698+
* user-mutated prototypes.
699+
* @param {unknown} value
700+
* @returns {boolean}
701+
*/
688702
function prepareWebCryptoResult(value) {
689703
if ((value === null || typeof value !== 'object') &&
690704
typeof value !== 'function') {
@@ -696,12 +710,27 @@ function prepareWebCryptoResult(value) {
696710
return true;
697711
}
698712

699-
// Remove the temporary then property installed by prepareWebCryptoResult().
713+
/**
714+
* Remove the temporary then property installed by prepareWebCryptoResult().
715+
* @param {{ then?: unknown }} value
716+
* @returns {void}
717+
*/
700718
function cleanupWebCryptoResult(value) {
701719
delete value.then;
702720
}
703721

704-
// Resolve a WebCrypto promise while inherited then accessors are shadowed.
722+
/**
723+
* @template T
724+
* @typedef {(value: T | PromiseLike<T>) => void} WebCryptoResolve
725+
*/
726+
727+
/**
728+
* Resolve a WebCrypto promise while inherited then accessors are shadowed.
729+
* @template T
730+
* @param {WebCryptoResolve<T>} resolve
731+
* @param {T | PromiseLike<T>} value
732+
* @returns {void}
733+
*/
705734
function resolveWebCryptoResult(resolve, value) {
706735
const shouldCleanupResult = prepareWebCryptoResult(value);
707736
try {
@@ -712,7 +741,17 @@ function resolveWebCryptoResult(resolve, value) {
712741
}
713742
}
714743

715-
// Run a WebCrypto promise reaction and settle the outer promise.
744+
/**
745+
* Run a WebCrypto promise reaction and settle the outer promise.
746+
* @template T
747+
* @template TResult
748+
* @param {((value: T) => TResult | PromiseLike<TResult>) | undefined} handler
749+
* @param {WebCryptoResolve<T | TResult>} resolve
750+
* @param {(reason?: unknown) => void} reject
751+
* @param {T} value
752+
* @param {boolean} isRejected
753+
* @returns {void}
754+
*/
716755
function settleJobPromise(handler, resolve, reject, value, isRejected) {
717756
try {
718757
if (typeof handler === 'function') {
@@ -727,9 +766,18 @@ function settleJobPromise(handler, resolve, reject, value, isRejected) {
727766
}
728767
}
729768

730-
// Promise.prototype.then gets promise.constructor to determine the result
731-
// promise's species. These promises are internal WebCrypto intermediates, so
732-
// make that lookup stay on the promise itself instead of user-mutated state.
769+
/**
770+
* Promise.prototype.then gets promise.constructor to determine the result
771+
* promise's species. These promises are internal WebCrypto intermediates, so
772+
* make that lookup stay on the promise itself instead of user-mutated state.
773+
* @template T
774+
* @template [TResult1=T]
775+
* @template [TResult2=never]
776+
* @param {Promise<T>} promise
777+
* @param {((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined} [onFulfilled]
778+
* @param {((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined} [onRejected]
779+
* @returns {Promise<TResult1 | TResult2>}
780+
*/
733781
function jobPromiseThen(promise, onFulfilled, onRejected) {
734782
const {
735783
promise: resultPromise,

typings/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BufferBinding } from './internalBinding/buffer';
55
import { CJSLexerBinding } from './internalBinding/cjs_lexer';
66
import { ConfigBinding } from './internalBinding/config';
77
import { ConstantsBinding } from './internalBinding/constants';
8+
import { CryptoBinding } from './internalBinding/crypto';
89
import { DebugBinding } from './internalBinding/debug';
910
import { EncodingBinding } from './internalBinding/encoding_binding';
1011
import { HttpParserBinding } from './internalBinding/http_parser';
@@ -42,6 +43,7 @@ interface InternalBindingMap {
4243
cjs_lexer: CJSLexerBinding;
4344
config: ConfigBinding;
4445
constants: ConstantsBinding;
46+
crypto: CryptoBinding;
4547
debug: DebugBinding;
4648
encoding_binding: EncodingBinding;
4749
fs: FsBinding;

0 commit comments

Comments
 (0)