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

fix: make port transferables unique #3421

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bundlesize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install dependencies
run: npm install
- name: Bundlesize ${{ matrix.project }}
uses: ipfs/aegir/actions/bundle-size@v28.0.0
uses: ipfs/aegir/actions/bundle-size@v29.0.0
with:
project: ${{ matrix.project }}
github_token: ${{ secrets.GITHUB_TOKEN }}
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
2 changes: 2 additions & 0 deletions packages/ipfs-message-port-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"devDependencies": {
"@types/it-all": "^1.0.0",
"aegir": "^28.2.0",
"cids": "^1.0.0",
"ipfs-utils": "^5.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 globalThis = require('ipfs-utils/src/globalthis')

const CID = require('cids')
const { 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 globalThis.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
)
})
})
})