Skip to content

feat: Support ArrayBuffer and Buffer Views serialization#201

Open
ttmx wants to merge 2 commits into
cloudflare:mainfrom
ttmx:feat/bufferviews-serialization
Open

feat: Support ArrayBuffer and Buffer Views serialization#201
ttmx wants to merge 2 commits into
cloudflare:mainfrom
ttmx:feat/bufferviews-serialization

Conversation

@ttmx

@ttmx ttmx commented Jun 25, 2026

Copy link
Copy Markdown

Added serialization support for ArrayBuffer and Buffer views, like Int8Array, Uint8ClampedArray, etc.

@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ttmx ttmx force-pushed the feat/bufferviews-serialization branch from 66c4db3 to 4231ea8 Compare June 25, 2026 11:29
@changeset-bot

changeset-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2821584

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ttmx

ttmx commented Jun 25, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Jun 25, 2026
@ttmx ttmx changed the title Support ArrayBuffer and Buffer Views serialization feat: Support ArrayBuffer and Buffer Views serialization Jun 25, 2026

@kentonv kentonv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Design looks good to me. I'm overloaded and don't have time to review line-by-line, maybe @dimitropoulos or @teamchong can take it.

Comment thread protocol.md Outdated
@pkg-pr-new

pkg-pr-new Bot commented Jun 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@201

commit: 2821584

Comment thread protocol.md
`["bytes", base64]`, `["bytes", base64, type]`

A byte container, represented as a base64-encoded string. If `type` is omitted, the receiver
should deserialize bytes as its default `Uint8Array` for backwards compatibility. Otherwise,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you clarify endianness? Currently multi-byte typed arrays go on the wire as raw native-endian bytes: my recommendation is to mandate little-endian on the wire

Comment thread src/serialize.ts
throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
}

let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);

@ndisidore ndisidore Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I believe when misaligned untrusted input throws a raw RangeError ("byte length of Int16Array should be a multiple of 2"). We probably want to validate byteLength % BYTES_PER_ELEMENT === 0

Comment thread src/serialize.ts
}

let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
switch (value[2]) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is likely more then a patch change
A current-main receiver ignores value[2] entirely and returns a Uint8Array of raw bytes for a 3-element message (eg. new sender Float64Array → old receiver yields a 16-byte Buffer).

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