Skip to content

base64/hex methods missing from Uint8Array #60612

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

Closed
printfn opened this issue Nov 26, 2024 · 6 comments
Closed

base64/hex methods missing from Uint8Array #60612

printfn opened this issue Nov 26, 2024 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@printfn
Copy link

printfn commented Nov 26, 2024

⚙ Compilation target

ESNext

⚙ Library

ESNext

Missing / Incorrect Definition

Uint8Array.fromBase64()
Uint8Array.fromHex()
Uint8Array.prototype.setFromBase64()
Uint8Array.prototype.setFromHex()
Uint8Array.prototype.toBase64()
Uint8Array.prototype.toHex()

These functions are implemented in stable Firefox and in the Safari Technology Preview.

Sample Code

new TextEncoder().encode('Hello World').toBase64()
// "SGVsbG8gV29ybGQ="

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64

https://github.com/tc39/proposal-arraybuffer-base64

@xiBread
Copy link

xiBread commented Nov 26, 2024

I would argue that support in a beta version does not fully count as "two or more major browser engines."

@RyanCavanaugh
Copy link
Member

Yeah, you'll have to wait for this to be widely-supported first. Please open a new issue at that time

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 28, 2024
@printfn printfn closed this as completed Nov 28, 2024
@joakim
Copy link

joakim commented Dec 30, 2024

I'll just add a simple workaround until it's widely supported:

declare global {
  interface Uint8ArrayConstructor {
    fromBase64: (base64: string) => Uint8Array
    fromHex: (hex: string) => Uint8Array
  }

  interface Uint8Array {
    toBase64: () => string
    toHex: () => string
  }
}

Add to global.d.ts or some other appropriate place.

I haven't added setFromBase64() and setFromHex() as I didn't need it. Just follow my lead if you want those :)

@aryzing
Copy link

aryzing commented Jan 4, 2025

Seems it's now available in two major engines no? Bun supports this too.

@joakim
Copy link

joakim commented Jan 4, 2025

@aryzing Still not in V8, see this issue. I think we'll have to wait for that, as it covers most of the runtimes.

@dontwanttothink
Copy link

dontwanttothink commented May 12, 2025

This feature is supported in Bun, Firefox and Safari. It has exceeded the threshold for inclusion that exists for DOM APIs. It should be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

6 participants