Skip to content

Add Support for ArrayBuffer and the various TypedArrays#69

Merged
Rich-Harris merged 4 commits into
sveltejs:mainfrom
LorisSigrist:master
Sep 25, 2024
Merged

Add Support for ArrayBuffer and the various TypedArrays#69
Rich-Harris merged 4 commits into
sveltejs:mainfrom
LorisSigrist:master

Conversation

@LorisSigrist

Copy link
Copy Markdown
Contributor

This is something that tripped me up a couple times, and given that #26 is three years old, I'm likely not the only one.

This PR adds support for ArrayBuffer and the various Typed Arrays (Uint8Array etc). It serialises them by encoding the underlying ArrayBuffer in base64.

I chose not to do the pointer-value style encoding that arrays use for the following reasons:

  • ArrayBuffers are never hole-y, so the potential savings are lesser
  • ArrayBuffers often represent images and likely have highly varied values. Pointer-value would likely be longer.

Since atob and btoa are not included in the project's ts libs I used JS implementations of both. I didn't want to introduce additional libs without consulting first.

@nounder

nounder commented Aug 5, 2024

Copy link
Copy Markdown

As an alternative, check out typeson. It supports different types of binary arrays (including ES6 classes) and optional type registry.

I still use devalue in Svelte realm but for server-to-server communication typeson is the king.

@Rich-Harris Rich-Harris merged commit 0bbcc96 into sveltejs:main Sep 25, 2024
@Rich-Harris

Copy link
Copy Markdown
Member

only took a year to merge, sorry! and thank you, this is awesome

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