Skip to content

Commit

Permalink
馃 Merge PR #68186 [@types/node] Add crypto to globalThis by @g12i
Browse files Browse the repository at this point in the history
* Add webcrypto to globalThis

* Add 4.8 files

* Do not put variable in NodeJS namespace
  • Loading branch information
g12i committed Jan 27, 2024
1 parent 0cd509f commit 75fea78
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions types/node/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4473,6 +4473,13 @@ declare module "crypto" {
): Promise<ArrayBuffer>;
}
}

global {
var crypto: typeof globalThis extends {
crypto: infer T;
} ? T
: webcrypto.Crypto;
}
}
declare module "node:crypto" {
export * from "crypto";
Expand Down
14 changes: 14 additions & 0 deletions types/node/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
const f: File = {} as any;
f.name; // $ExpectType string
}

{
crypto.randomUUID(); // $ExpectType `${string}-${string}-${string}-${string}-${string}` || string
crypto.getRandomValues(Buffer.alloc(8)); // $ExpectType Buffer
crypto.getRandomValues(new BigInt64Array(4)); // $ExpectType BigInt64Array

crypto.subtle.generateKey({ name: "HMAC", hash: "SHA-1" }, true, ["sign", "decrypt", "deriveBits"]).then(
(out) => {
out.algorithm; // $ExpectType KeyAlgorithm
out.extractable; // $ExpectType boolean
out.usages; // $ExpectType KeyUsage[]
},
);
}
4 changes: 4 additions & 0 deletions types/node/ts4.8/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4472,6 +4472,10 @@ declare module "crypto" {
): Promise<ArrayBuffer>;
}
}

global {
var crypto: webcrypto.Crypto;
}
}
declare module "node:crypto" {
export * from "crypto";
Expand Down
14 changes: 14 additions & 0 deletions types/node/ts4.8/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
const f: File = {} as any;
f.name; // $ExpectType string
}

{
crypto.randomUUID(); // $ExpectType string
crypto.getRandomValues(Buffer.alloc(8)); // $ExpectType Buffer
crypto.getRandomValues(new BigInt64Array(4)); // $ExpectType BigInt64Array

crypto.subtle.generateKey({ name: "HMAC", hash: "SHA-1" }, true, ["sign", "decrypt", "deriveBits"]).then(
(out) => {
out.algorithm; // $ExpectType KeyAlgorithm
out.extractable; // $ExpectType boolean
out.usages; // $ExpectType KeyUsage[]
},
);
}

0 comments on commit 75fea78

Please sign in to comment.