Skip to content

feat: completely factorize SubtleCrypto.generateKey() #2050

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hkleungai
Copy link

@hkleungai hkleungai commented Jun 23, 2025

Opening this PR in favor of this comment from @Renegade334 in my another PR in DT repo.

Since https://nodejs.org/api/webcrypto.html#cryptokeyusages lists the usages in its own table, and it is closely aligned to the MDN reference in https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey, I believe it is possible to raise such change to @types/web as well.

Potentially we can do this to other methods in the key-usages table, but let's see how reviewers think about this change, then we may perform the refactoring incrementally in subsequent PRs.

Fallback signature is also updated, in favor of matching richer shape of possible Algorithm object.

Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

Comment on lines 1797 to 1808
"typeParameters": [
{
"name": "T",
"extends": "{ name: string }"
}
],
"param": [
{
"name": "algorithm",
"overrideType": "string | T"
}
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary generic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this generic is needed, or else the fallback signature will fail to capture poosibly-work algorithm object with extra attibutes other than name.

See https://tsplay.dev/N5oDdW.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No-one's raised any issues with AlgorithmIdentifier; this seems an arbitrary modification.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://tsplay.dev/Ndp9MN, in which the method call is taken from types/node/test/globals-non-dom.ts in DefinitelyTyped.

Without the use of generic, mismatched algorithm and keyUsages[] params would yield a confusing error, saying the code is violating the wrong overload.

But with the generic, such mismatch will pass and give Promise<CryptoKeyPair | CryptoKey>. It may not be the best way to typecheck, but I think this generic could help prevent certain possible breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand – the whole point of this PR is that it breaks in this case?

The example provided is a mismatch with the accepted usages of the new HmacKeyGenParams signature, and is genuinely incorrect usage that would error at runtime. If we're going to provide a route for the new keyUsages constraints to be ignored anyway, then there's surely there's no real advantage to these changes?

(We can always modify that test – I suspect that the selection of keyUsages there was entirely arbitrary.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right. I have repushed the PR to remove the generics.

@hkleungai hkleungai force-pushed the crypto-factorize-subtle.generateKey branch from 0dc3d22 to a6b6305 Compare July 4, 2025 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants