Skip to content

Commit 47d2d62

Browse files
committed
typings: add typing for crypto
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64122 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 8cb8312 commit 47d2d62

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
@@ -723,9 +723,19 @@ const validateByteSource = hideStackFrames((val, name) => {
723723
val);
724724
});
725725

726-
// CryptoJob constructors can synchronously throw while running their native
727-
// AdditionalConfig hook. WebCrypto needs those operation-specific setup
728-
// failures to reject with an OperationError.
726+
/**
727+
* @template T
728+
* @typedef {{ run(): Promise<T> }} WebCryptoJob
729+
*/
730+
731+
/**
732+
* CryptoJob constructors can synchronously throw while running their native
733+
* AdditionalConfig hook. WebCrypto needs those operation-specific setup
734+
* failures to reject with an OperationError.
735+
* @template T
736+
* @param {() => WebCryptoJob<T>} getJob
737+
* @returns {Promise<T>}
738+
*/
729739
function jobPromise(getJob) {
730740
try {
731741
return getJob().run();
@@ -736,10 +746,14 @@ function jobPromise(getJob) {
736746
}
737747
}
738748

739-
// Temporarily shadow inherited then accessors on WebCrypto result objects.
740-
// Promise resolution reads "then" synchronously for thenable assimilation.
741-
// Returning an own undefined data property keeps that lookup from reaching
742-
// user-mutated prototypes.
749+
/**
750+
* Temporarily shadow inherited then accessors on WebCrypto result objects.
751+
* Promise resolution reads "then" synchronously for thenable assimilation.
752+
* Returning an own undefined data property keeps that lookup from reaching
753+
* user-mutated prototypes.
754+
* @param {unknown} value
755+
* @returns {boolean}
756+
*/
743757
function prepareWebCryptoResult(value) {
744758
if ((value === null || typeof value !== 'object') &&
745759
typeof value !== 'function') {
@@ -751,12 +765,27 @@ function prepareWebCryptoResult(value) {
751765
return true;
752766
}
753767

754-
// Remove the temporary then property installed by prepareWebCryptoResult().
768+
/**
769+
* Remove the temporary then property installed by prepareWebCryptoResult().
770+
* @param {{ then?: unknown }} value
771+
* @returns {void}
772+
*/
755773
function cleanupWebCryptoResult(value) {
756774
delete value.then;
757775
}
758776

759-
// Resolve a WebCrypto promise while inherited then accessors are shadowed.
777+
/**
778+
* @template T
779+
* @typedef {(value: T | PromiseLike<T>) => void} WebCryptoResolve
780+
*/
781+
782+
/**
783+
* Resolve a WebCrypto promise while inherited then accessors are shadowed.
784+
* @template T
785+
* @param {WebCryptoResolve<T>} resolve
786+
* @param {T | PromiseLike<T>} value
787+
* @returns {void}
788+
*/
760789
function resolveWebCryptoResult(resolve, value) {
761790
const shouldCleanupResult = prepareWebCryptoResult(value);
762791
try {
@@ -767,7 +796,17 @@ function resolveWebCryptoResult(resolve, value) {
767796
}
768797
}
769798

770-
// Run a WebCrypto promise reaction and settle the outer promise.
799+
/**
800+
* Run a WebCrypto promise reaction and settle the outer promise.
801+
* @template T
802+
* @template TResult
803+
* @param {((value: T) => TResult | PromiseLike<TResult>) | undefined} handler
804+
* @param {WebCryptoResolve<T | TResult>} resolve
805+
* @param {(reason?: unknown) => void} reject
806+
* @param {T} value
807+
* @param {boolean} isRejected
808+
* @returns {void}
809+
*/
771810
function settleJobPromise(handler, resolve, reject, value, isRejected) {
772811
try {
773812
if (typeof handler === 'function') {
@@ -782,9 +821,18 @@ function settleJobPromise(handler, resolve, reject, value, isRejected) {
782821
}
783822
}
784823

785-
// Promise.prototype.then gets promise.constructor to determine the result
786-
// promise's species. These promises are internal WebCrypto intermediates, so
787-
// make that lookup stay on the promise itself instead of user-mutated state.
824+
/**
825+
* Promise.prototype.then gets promise.constructor to determine the result
826+
* promise's species. These promises are internal WebCrypto intermediates, so
827+
* make that lookup stay on the promise itself instead of user-mutated state.
828+
* @template T
829+
* @template [TResult1=T]
830+
* @template [TResult2=never]
831+
* @param {Promise<T>} promise
832+
* @param {((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined} [onFulfilled]
833+
* @param {((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined} [onRejected]
834+
* @returns {Promise<TResult1 | TResult2>}
835+
*/
788836
function jobPromiseThen(promise, onFulfilled, onRejected) {
789837
const {
790838
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)