Skip to content

Commit 58d9685

Browse files
panvarichardlau
authored andcommitted
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 bbdd764 commit 58d9685

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
@@ -715,9 +715,19 @@ const validateByteSource = hideStackFrames((val, name) => {
715715
val);
716716
});
717717

718-
// CryptoJob constructors can synchronously throw while running their native
719-
// AdditionalConfig hook. WebCrypto needs those operation-specific setup
720-
// failures to reject with an OperationError.
718+
/**
719+
* @template T
720+
* @typedef {{ run(): Promise<T> }} WebCryptoJob
721+
*/
722+
723+
/**
724+
* CryptoJob constructors can synchronously throw while running their native
725+
* AdditionalConfig hook. WebCrypto needs those operation-specific setup
726+
* failures to reject with an OperationError.
727+
* @template T
728+
* @param {() => WebCryptoJob<T>} getJob
729+
* @returns {Promise<T>}
730+
*/
721731
function jobPromise(getJob) {
722732
try {
723733
return getJob().run();
@@ -728,10 +738,14 @@ function jobPromise(getJob) {
728738
}
729739
}
730740

731-
// Temporarily shadow inherited then accessors on WebCrypto result objects.
732-
// Promise resolution reads "then" synchronously for thenable assimilation.
733-
// Returning an own undefined data property keeps that lookup from reaching
734-
// user-mutated prototypes.
741+
/**
742+
* Temporarily shadow inherited then accessors on WebCrypto result objects.
743+
* Promise resolution reads "then" synchronously for thenable assimilation.
744+
* Returning an own undefined data property keeps that lookup from reaching
745+
* user-mutated prototypes.
746+
* @param {unknown} value
747+
* @returns {boolean}
748+
*/
735749
function prepareWebCryptoResult(value) {
736750
if ((value === null || typeof value !== 'object') &&
737751
typeof value !== 'function') {
@@ -743,12 +757,27 @@ function prepareWebCryptoResult(value) {
743757
return true;
744758
}
745759

746-
// Remove the temporary then property installed by prepareWebCryptoResult().
760+
/**
761+
* Remove the temporary then property installed by prepareWebCryptoResult().
762+
* @param {{ then?: unknown }} value
763+
* @returns {void}
764+
*/
747765
function cleanupWebCryptoResult(value) {
748766
delete value.then;
749767
}
750768

751-
// Resolve a WebCrypto promise while inherited then accessors are shadowed.
769+
/**
770+
* @template T
771+
* @typedef {(value: T | PromiseLike<T>) => void} WebCryptoResolve
772+
*/
773+
774+
/**
775+
* Resolve a WebCrypto promise while inherited then accessors are shadowed.
776+
* @template T
777+
* @param {WebCryptoResolve<T>} resolve
778+
* @param {T | PromiseLike<T>} value
779+
* @returns {void}
780+
*/
752781
function resolveWebCryptoResult(resolve, value) {
753782
const shouldCleanupResult = prepareWebCryptoResult(value);
754783
try {
@@ -759,7 +788,17 @@ function resolveWebCryptoResult(resolve, value) {
759788
}
760789
}
761790

762-
// Run a WebCrypto promise reaction and settle the outer promise.
791+
/**
792+
* Run a WebCrypto promise reaction and settle the outer promise.
793+
* @template T
794+
* @template TResult
795+
* @param {((value: T) => TResult | PromiseLike<TResult>) | undefined} handler
796+
* @param {WebCryptoResolve<T | TResult>} resolve
797+
* @param {(reason?: unknown) => void} reject
798+
* @param {T} value
799+
* @param {boolean} isRejected
800+
* @returns {void}
801+
*/
763802
function settleJobPromise(handler, resolve, reject, value, isRejected) {
764803
try {
765804
if (typeof handler === 'function') {
@@ -774,9 +813,18 @@ function settleJobPromise(handler, resolve, reject, value, isRejected) {
774813
}
775814
}
776815

777-
// Promise.prototype.then gets promise.constructor to determine the result
778-
// promise's species. These promises are internal WebCrypto intermediates, so
779-
// make that lookup stay on the promise itself instead of user-mutated state.
816+
/**
817+
* Promise.prototype.then gets promise.constructor to determine the result
818+
* promise's species. These promises are internal WebCrypto intermediates, so
819+
* make that lookup stay on the promise itself instead of user-mutated state.
820+
* @template T
821+
* @template [TResult1=T]
822+
* @template [TResult2=never]
823+
* @param {Promise<T>} promise
824+
* @param {((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined} [onFulfilled]
825+
* @param {((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined} [onRejected]
826+
* @returns {Promise<TResult1 | TResult2>}
827+
*/
780828
function jobPromiseThen(promise, onFulfilled, onRejected) {
781829
const {
782830
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)