Skip to content

src: throw DataCloneError on transfering untransferable objects

The HTML StructuredSerializeWithTransfer algorithm defines that when an untransferable object is in the transfer list, a DataCloneError is thrown. An array buffer that is already transferred is also considered as untransferable.

The following script throws in the browser but succeeds in Node.js:

const ab = new ArrayBuffer(10)
const first = structuredClone(ab, { transfer: [ab] })
const second = structuredClone(ab, { transfer: [ab] })

The motivation of the PR is throwing an error prominently to avoid copy buffer silently when it is expected to be transferred:

const buffer = Buffer.allocUnsafe(10)
// this should throw instead of falling back to copying
structuredClone(buffer, { transfer: [buffer.buffer] })

Merge request reports

Loading