From 13115c88ced2476fd8f7bbfe99059222d0220828 Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Thu, 13 Jul 2023 10:20:19 -0700 Subject: [PATCH] Make the function signature overloads of `SubtleCrypto#exportKey` more flexible (#1593) Co-authored-by: saschanaz --- baselines/dom.generated.d.ts | 1 + baselines/serviceworker.generated.d.ts | 1 + baselines/sharedworker.generated.d.ts | 1 + baselines/webworker.generated.d.ts | 1 + inputfiles/overridingTypes.jsonc | 7 +-- unittests/files/keyusage.ts | 61 +++++++++++++++++++------- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 39d99ba51..3666a2eb3 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -21839,6 +21839,7 @@ interface SubtleCrypto { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */ exportKey(format: "jwk", key: CryptoKey): Promise; exportKey(format: Exclude, key: CryptoKey): Promise; + exportKey(format: KeyFormat, key: CryptoKey): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */ generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise; generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray): Promise; diff --git a/baselines/serviceworker.generated.d.ts b/baselines/serviceworker.generated.d.ts index 7c60baeb3..50e5a6b4a 100644 --- a/baselines/serviceworker.generated.d.ts +++ b/baselines/serviceworker.generated.d.ts @@ -5016,6 +5016,7 @@ interface SubtleCrypto { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */ exportKey(format: "jwk", key: CryptoKey): Promise; exportKey(format: Exclude, key: CryptoKey): Promise; + exportKey(format: KeyFormat, key: CryptoKey): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */ generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise; generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray): Promise; diff --git a/baselines/sharedworker.generated.d.ts b/baselines/sharedworker.generated.d.ts index ece37067b..56f1d33a9 100644 --- a/baselines/sharedworker.generated.d.ts +++ b/baselines/sharedworker.generated.d.ts @@ -4827,6 +4827,7 @@ interface SubtleCrypto { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */ exportKey(format: "jwk", key: CryptoKey): Promise; exportKey(format: Exclude, key: CryptoKey): Promise; + exportKey(format: KeyFormat, key: CryptoKey): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */ generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise; generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray): Promise; diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index 217032859..584d440f7 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -5322,6 +5322,7 @@ interface SubtleCrypto { /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */ exportKey(format: "jwk", key: CryptoKey): Promise; exportKey(format: Exclude, key: CryptoKey): Promise; + exportKey(format: KeyFormat, key: CryptoKey): Promise; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */ generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise; generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray): Promise; diff --git a/inputfiles/overridingTypes.jsonc b/inputfiles/overridingTypes.jsonc index 218462b48..38b4d59f9 100644 --- a/inputfiles/overridingTypes.jsonc +++ b/inputfiles/overridingTypes.jsonc @@ -1572,17 +1572,18 @@ }, "exportKey": { "additionalSignatures": [ - "exportKey(format: \"jwk\", key: CryptoKey): Promise" + "exportKey(format: \"jwk\", key: CryptoKey): Promise", + "exportKey(format: Exclude, key: CryptoKey): Promise" ], "signature": { "0": { "param": [ { "name": "format", - "overrideType": "Exclude" + "overrideType": "KeyFormat" } ], - "overrideType": "Promise" + "overrideType": "Promise" } } }, diff --git a/unittests/files/keyusage.ts b/unittests/files/keyusage.ts index fab5b4e94..7e037c4e6 100644 --- a/unittests/files/keyusage.ts +++ b/unittests/files/keyusage.ts @@ -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 = ['encrypt', 'decrypt'] -const usageAsReadonly = crypto.subtle.generateKey( { - name: "AES-GCM", - length: 256, -}, true, keyUsage) \ No newline at end of file +function assertType(_x: T) {} + +const mockKey = {} as CryptoKey; + +assertType>(crypto.subtle.exportKey("jwk", mockKey)); +assertType>(crypto.subtle.exportKey("pkcs8", mockKey)); +assertType>(crypto.subtle.exportKey("raw", mockKey)); +assertType>(crypto.subtle.exportKey("spki", mockKey)); + +assertType>( + 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 = ["encrypt", "decrypt"]; +const usageAsReadonly = crypto.subtle.generateKey( + { + name: "AES-GCM", + length: 256, + }, + true, + keyUsage +);