How to generate random base64 in mutation
I have a "create API key" mutation and I'd like to generate a base64 API key, but it seems the
btoa
provided by the Convex function runtime fails if the string contains values outside a certain range.
This newApiKey()
function works in browsers but fails in Convex: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABGApgdwIIAcYGkUCeAFAJQBciAzlAE4xgDmiA3gFCKID0niDKqNAIZQUiAMwAmALQAjAiMQYACgElEAa0LtEEBNURyRlRAF5FNIQQB0wGnAC2RZogA2-BlAAWFSYgC+ADSIpKYAfIgAssKeNi5wcDREUV5WQmAAJg4hAFSIEgCsAGwkJADc2tyI-LrpovSIAKoASgAyUpSCwKIygpQohQAsFTxEfaKeUFBYlGTc-FZoMOowWCjpMIJWCQyci8ucAEK9-QMAxABqgnSCYFCUAPqUIPb2VwT3UIIybiTaNChQEA0JAyKBwQREADKtHoDBsdnsAGFPFdEXBalZBFgsC5iGAQC4XEFDChKCVtBxUigcYIICgiJwADoAak4DCCAHIpBzfhxKf8aXSGYzuOzEBz7jyKYgqYL6ZwTGzOVK-KxWII3DQoERUJgcPhiCUgA
Is there an alternative way to generate a random base64 string?TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
3 Replies
thanks for reporting! i found the bug on our side and should ship the fix on monday. i'm not sure if you can implement base64-encoding with
TextEncoder
-- might that work?
hmm i don't see how to make that work. maybe until we deploy the fix, you could create a slightly-less-random key, with values from 0 to 127? sorry about this. string encoding is tricky.Awesome, thanks!! I can use a dumbed-down key generator for now, so no rush 👍
i ran your function in my own app and it seems to work now 🙂