Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: make port transferables unique
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Nov 27, 2020
1 parent 75dd865 commit 1721825
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/ipfs-message-port-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ values instead of copying.

> **Note:** Transferring data will empty it on the sender side which can lead to
> errors if that data is used again later. To avoid these errors transfer option
> was added so user can explicitily give up reference when it is safe to do so.
> was added so user can explicitly give up reference when it is safe to do so.
```js
/**
Expand All @@ -115,7 +115,7 @@ const example = async (data) => {
}
```

It is however recommended to prefer web native [Blob][] / [File][] intances as
It is however recommended to prefer web native [Blob][] / [File][] instances as
most web APIs provide them as option & can be send across without copying
underlying memory.

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-client/src/client/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ module.exports = class MessageTransport {
input: query.toJSON()
},
// @ts-ignore - TS seems to want second arg to postMessage to not be undefined
query.transfer()
[...new Set(query.transfer() || [])]
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-protocol/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const decodeCallback = ({ port }) => {
* @returns {void}
*/
const callback = (args, transfer = []) => {
port.postMessage(args, transfer)
port.postMessage(args, [...new Set(transfer)])
}

return callback
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-message-port-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"devDependencies": {
"@types/it-all": "^1.0.0",
"aegir": "^28.2.0",
"cids": "^1.0.0",
"rimraf": "^3.0.2",
"typescript": "4.0.x"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/ipfs-message-port-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,12 @@ exports.Server = class Server {
if (!query.signal.aborted) {
try {
const value = await query.result
const transfer = [...new Set(value.transfer || [])]
delete value.transfer

port.postMessage(
{ type: 'result', id, result: { ok: true, value } },
value.transfer || []
transfer
)
} catch (error) {
port.postMessage({
Expand Down
46 changes: 46 additions & 0 deletions packages/ipfs-message-port-server/test/transfer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

/* eslint-env mocha */
const { encodeCID } = require('ipfs-message-port-protocol/src/cid')
const { expect } = require('aegir/utils/chai')

const CID = require('cids')
const { Query, Server } = require('../src/server')
const { IPFSService } = require('../src/index')

describe('Server', function () {
this.timeout(10 * 1000)

it('should be able to transfer multiple of the same CID instances', () => {
const cid = new CID("QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D")

return new Promise((resolve, reject) => {
const channel = process.browser
? new MessageChannel()
: new (require('worker_threads').MessageChannel)()

channel.port1.onmessageerror = reject
channel.port1.onmessage = event => {
const result = event.data.result
result.ok ? resolve(result.value) : reject(new Error(result.error.message))
}

const service = new IPFSService()
const server = new Server(service)
const transfer = []

server.run = a => a
server.handleQuery(
"",
{
result: {
value: [ encodeCID(cid, transfer), encodeCID(cid, transfer) ],
transfer: transfer
},
signal: { aborted: false }
},
channel.port2
)
})
})
})

0 comments on commit 1721825

Please sign in to comment.