Skip to content

Commit

Permalink
Make the function signature overloads of SubtleCrypto#exportKey mor…
Browse files Browse the repository at this point in the history
…e flexible (#1593)

Co-authored-by: saschanaz <[email protected]>
  • Loading branch information
steveluscher and saschanaz authored Jul 13, 2023
1 parent 9e884a8 commit 13115c8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21839,6 +21839,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
Expand Down
1 change: 1 addition & 0 deletions baselines/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5016,6 +5016,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
Expand Down
1 change: 1 addition & 0 deletions baselines/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4827,6 +4827,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
Expand Down
1 change: 1 addition & 0 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5322,6 +5322,7 @@ interface SubtleCrypto {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
Expand Down
7 changes: 4 additions & 3 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1572,17 +1572,18 @@
},
"exportKey": {
"additionalSignatures": [
"exportKey(format: \"jwk\", key: CryptoKey): Promise<JsonWebKey>"
"exportKey(format: \"jwk\", key: CryptoKey): Promise<JsonWebKey>",
"exportKey(format: Exclude<KeyFormat, \"jwk\">, key: CryptoKey): Promise<ArrayBuffer>"
],
"signature": {
"0": {
"param": [
{
"name": "format",
"overrideType": "Exclude<KeyFormat, \"jwk\">"
"overrideType": "KeyFormat"
}
],
"overrideType": "Promise<ArrayBuffer>"
"overrideType": "Promise<ArrayBuffer | JsonWebKey>"
}
}
},
Expand Down
61 changes: 46 additions & 15 deletions unittests/files/keyusage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
const usageInline = crypto.subtle.generateKey({
name: "AES-GCM",
length: 256,
}, true, ['encrypt', 'decrypt'])

const usageConst = crypto.subtle.generateKey( {
name: "AES-GCM",
length: 256,
}, true, ['encrypt', 'decrypt'] as const)

const keyUsage: ReadonlyArray<KeyUsage> = ['encrypt', 'decrypt']
const usageAsReadonly = crypto.subtle.generateKey( {
name: "AES-GCM",
length: 256,
}, true, keyUsage)
function assertType<T>(_x: T) {}

const mockKey = {} as CryptoKey;

assertType<Promise<JsonWebKey>>(crypto.subtle.exportKey("jwk", mockKey));
assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey("pkcs8", mockKey));
assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey("raw", mockKey));
assertType<Promise<ArrayBuffer>>(crypto.subtle.exportKey("spki", mockKey));

assertType<Promise<ArrayBuffer | JsonWebKey>>(
crypto.subtle
.exportKey("" as KeyFormat, mockKey)
.then((ambiguousExportedKeyData) =>
ambiguousExportedKeyData instanceof ArrayBuffer
? (ambiguousExportedKeyData satisfies ArrayBuffer)
: (ambiguousExportedKeyData satisfies JsonWebKey)
)
);

const usageInline = crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"]
);

const usageConst = crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"] as const
);

const keyUsage: ReadonlyArray<KeyUsage> = ["encrypt", "decrypt"];
const usageAsReadonly = crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
keyUsage
);

0 comments on commit 13115c8

Please sign in to comment.