Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the function signature overloads of SubtleCrypto#exportKey more flexible #1593

Merged
merged 11 commits into from
Jul 13, 2023
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: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
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: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
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: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
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: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
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
);