Skip to content

Commit

Permalink
fix: ArrayBuffer is already detached (ipfs#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Jan 15, 2021
1 parent 61adc66 commit e945623
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/ipfs-message-port-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,25 @@ exports.Server = class Server {
if (!query.signal.aborted) {
try {
const value = await query.result
const transfer = [...new Set(value.transfer || [])]

// Only keep buffers that aren't detached
const transferValue = (value.transfer || []).map(t => {
if (t instanceof ArrayBuffer || ArrayBuffer.isView(t)) {
return t.byteLength === 0 ? t.slice(0) : t
} else {
return t
}
})

// Make a Set of the transfer values, so we're definitely working with unique values
const transfer = new Set(transferValue)

// Don't need the transfer value in the result
delete value.transfer

port.postMessage(
{ type: 'result', id, result: { ok: true, value } },
// @ts-ignore - Typescript expects the transfer list to be an Array, but it can actually be any iterable.
transfer
)
} catch (error) {
Expand Down

0 comments on commit e945623

Please sign in to comment.