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

Commit b09a18c

Browse files
Gozalaicidassetachingbrain
authoredSep 30, 2021
fix: transfer set (#3573)
Co-authored-by: Steven Vandevelde <icid.asset@gmail.com> Co-authored-by: achingbrain <alex@achingbrain.net>
1 parent 3ce2f76 commit b09a18c

File tree

23 files changed

+121
-99
lines changed

23 files changed

+121
-99
lines changed
 

‎packages/ipfs-message-port-client/src/client/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @typedef {Object} QueryOptions
33
* @property {AbortSignal} [signal]
44
* @property {number} [timeout]
5-
* @property {Transferable[]} [transfer]
5+
* @property {Set<Transferable>} [transfer]
66
*/
77

88
/**
@@ -49,7 +49,7 @@ export class Query {
4949
/**
5050
* Data that will be transferred over message channel.
5151
*
52-
* @returns {Transferable[]|void}
52+
* @returns {Set<Transferable>|void}
5353
*/
5454
transfer () {
5555
return this.input.transfer

‎packages/ipfs-message-port-client/src/client/transport.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ export class MessageTransport {
180180
id,
181181
input: query.toJSON()
182182
},
183-
// @ts-ignore - TS seems to want second arg to postMessage to not be undefined
184-
[...new Set(query.transfer() || [])]
183+
// @ts-expect-error - Type signature does not expect 2nd undefined arg
184+
query.transfer()
185185
)
186186
}
187187

‎packages/ipfs-message-port-client/src/core.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class CoreClient extends Client {
7171
*/
7272
CoreClient.prototype.addAll = async function * addAll (input, options = {}) {
7373
const { timeout, signal } = options
74-
const transfer = [...(options.transfer || [])]
74+
const transfer = options.transfer || new Set()
7575
const progressCallback = options.progress
7676
? encodeCallback(options.progress, transfer)
7777
: undefined
@@ -99,7 +99,7 @@ CoreClient.prototype.addAll = async function * addAll (input, options = {}) {
9999
*/
100100
CoreClient.prototype.add = async function add (input, options = {}) {
101101
const { timeout, signal } = options
102-
const transfer = [...(options.transfer || [])]
102+
const transfer = options.transfer || new Set()
103103
const progressCallback = options.progress
104104
? encodeCallback(options.progress, transfer)
105105
: undefined
@@ -184,7 +184,7 @@ const identity = (v) => v
184184
* given input.
185185
*
186186
* @param {ImportCandidate} input
187-
* @param {Transferable[]} transfer
187+
* @param {Set<Transferable>} transfer
188188
* @returns {Promise<EncodedAddInput>}
189189
*/
190190
const encodeAddInput = async (input, transfer) => {
@@ -242,7 +242,7 @@ const encodeAddInput = async (input, transfer) => {
242242
* given input.
243243
*
244244
* @param {ImportCandidateStream} input
245-
* @param {Transferable[]} transfer
245+
* @param {Set<Transferable>} transfer
246246
* @returns {EncodedAddAllInput}
247247
*/
248248
const encodeAddAllInput = (input, transfer) => {
@@ -279,7 +279,7 @@ const encodeAddAllInput = (input, transfer) => {
279279
* effective strategy.
280280
*
281281
* @param {ImportCandidate} content
282-
* @param {Transferable[]} transfer
282+
* @param {Set<Transferable>} transfer
283283
* @returns {EncodedAddInput}
284284
*/
285285
const encodeAsyncIterableContent = (content, transfer) => {
@@ -303,7 +303,7 @@ const encodeAsyncIterableContent = (content, transfer) => {
303303

304304
/**
305305
* @param {ImportCandidate} content
306-
* @param {Transferable[]} transfer
306+
* @param {Set<Transferable>} transfer
307307
* @returns {EncodedAddInput}
308308
*/
309309
const encodeIterableContent = (content, transfer) => {
@@ -329,7 +329,7 @@ const encodeIterableContent = (content, transfer) => {
329329

330330
/**
331331
* @param {ToFile | ToDirectory} file
332-
* @param {Transferable[]} transfer
332+
* @param {Set<Transferable>} transfer
333333
* @returns {EncodedFileInput | EncodedDirectoryInput}
334334
*/
335335
const encodeFileObject = ({ path, mode, mtime, content }, transfer) => {
@@ -349,7 +349,7 @@ const encodeFileObject = ({ path, mode, mtime, content }, transfer) => {
349349

350350
/**
351351
* @param {ToContent|undefined} content
352-
* @param {Transferable[]} transfer
352+
* @param {Set<Transferable>} transfer
353353
* @returns {EncodedFileContent}
354354
*/
355355
const encodeFileContent = (content, transfer) => {

‎packages/ipfs-message-port-client/src/dag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ DAGClient.prototype.resolve = async function resolve (cid, options = {}) {
6262

6363
/**
6464
* @param {string|CID} input
65-
* @param {Transferable[]} [transfer]
65+
* @param {Set<Transferable>} [transfer]
6666
* @returns {string|EncodedCID}
6767
*/
6868
const encodeCIDOrPath = (input, transfer) => {

‎packages/ipfs-message-port-client/src/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// JSDoc syntax or that result in a different behaviour when typed in JSDoc.
33

44
export interface MessagePortClientOptions {
5-
transfer?: Transferable[]
5+
transfer?: Set<Transferable>
66
}

‎packages/ipfs-message-port-protocol/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ port2.onmessage = async ({data}) => {
146146

147147
### Callback
148148

149-
Primitive callbacks that take single parameter supported by [structured cloning algorithm][] like progress callback used across IPFS APIs can be encoded / decoded. Unilke most encoders `transfer` argument is required (because value is encoded to a [MessagePort][] that can only be transferred)
149+
Primitive callbacks that take single parameter supported by [structured cloning algorithm][] like progress callback used across IPFS APIs can be encoded / decoded. Unlike most encoders `transfer` argument is required (because value is encoded to a [MessagePort][] that can only be transferred)
150150

151151
```js
152152
import { encodeCallback, decodeCallback } from 'ipfs-message-port-protocol/core'
@@ -186,4 +186,3 @@ Check out our [contributing document](https://github.com/ipfs/community/blob/mas
186186
## License
187187

188188
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs?ref=badge_large)
189-

‎packages/ipfs-message-port-protocol/src/block.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
* copy.
1616
*
1717
* @param {Uint8Array} data
18-
* @param {Transferable[]} [transfer]
18+
* @param {Set<Transferable>} [transfer]
1919
*/
2020
export const encodeBlock = (data, transfer) => {
2121
if (transfer) {
22-
transfer.push(data.buffer)
22+
transfer.add(data.buffer)
2323
}
2424
return data
2525
}

‎packages/ipfs-message-port-protocol/src/cid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { CID } from 'multiformats/cid'
1414
* will be added for the transfer list.
1515
*
1616
* @param {CID} cid
17-
* @param {Transferable[]} [transfer]
17+
* @param {Set<Transferable>} [transfer]
1818
* @returns {EncodedCID}
1919
*/
2020
export const encodeCID = (cid, transfer) => {
2121
if (transfer) {
22-
transfer.push(cid.multihash.bytes.buffer)
22+
transfer.add(cid.multihash.bytes.buffer)
2323
}
2424
return cid
2525
}

‎packages/ipfs-message-port-protocol/src/core.js

+30-14
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ export const decodeIterable = async function * ({ port }, decode) {
9292
/**
9393
* @template I,O
9494
* @param {AsyncIterable<I>|Iterable<I>} iterable
95-
* @param {function(I, Transferable[]):O} encode
96-
* @param {Transferable[]} transfer
95+
* @param {function(I, Set<Transferable>):O} encode
96+
* @param {Set<Transferable>} transfer
9797
* @returns {RemoteIterable<O>}
9898
*/
9999
export const encodeIterable = (iterable, encode, transfer) => {
100100
const { port1: port, port2: remote } = new MessageChannel()
101-
/** @type {Transferable[]} */
102-
const itemTransfer = []
103101
/** @type {Iterator<I>|AsyncIterator<I>} */
104102
const iterator = toIterator(iterable)
103+
// Note that port.onmessage will receive multiple 'next' method messages.
104+
// Instead of allocating set every time we allocate one here and recycle
105+
// it on each 'next' message.
106+
/** @type {Set<Transferable>} */
107+
const itemTransfer = new Set()
105108

106109
port.onmessage = async ({ data: { method } }) => {
107110
switch (method) {
@@ -112,12 +115,15 @@ export const encodeIterable = (iterable, encode, transfer) => {
112115
port.postMessage({ type: 'next', done: true })
113116
port.close()
114117
} else {
115-
itemTransfer.length = 0
116-
port.postMessage(
118+
itemTransfer.clear()
119+
const encodedValue = encode(value, itemTransfer)
120+
121+
postMessage(
122+
port,
117123
{
118124
type: 'next',
119125
done: false,
120-
value: encode(value, itemTransfer)
126+
value: encodedValue
121127
},
122128
itemTransfer
123129
)
@@ -144,7 +150,7 @@ export const encodeIterable = (iterable, encode, transfer) => {
144150
}
145151
}
146152
port.start()
147-
transfer.push(remote)
153+
transfer.add(remote)
148154

149155
return { type: 'RemoteIterable', port: remote }
150156
}
@@ -170,31 +176,41 @@ const toIterator = iterable => {
170176

171177
/**
172178
* @param {Function} callback
173-
* @param {Transferable[]} transfer
179+
* @param {Set<Transferable>} transfer
174180
* @returns {RemoteCallback}
175181
*/
176182
export const encodeCallback = (callback, transfer) => {
177183
// eslint-disable-next-line no-undef
178184
const { port1: port, port2: remote } = new MessageChannel()
179185
port.onmessage = ({ data }) => callback.apply(null, data)
180-
transfer.push(remote)
186+
transfer.add(remote)
181187
return { type: 'RemoteCallback', port: remote }
182188
}
183189

184190
/**
185191
* @template T
186192
* @param {RemoteCallback} remote
187-
* @returns {function(T[]):void | function(T[], Transferable[]):void}
193+
* @returns {function(T[]):void | function(T[], Set<Transferable>):void}
188194
*/
189195
export const decodeCallback = ({ port }) => {
190196
/**
191197
* @param {T[]} args
192-
* @param {Transferable[]} [transfer]
198+
* @param {Set<Transferable>} [transfer]
193199
* @returns {void}
194200
*/
195-
const callback = (args, transfer = []) => {
196-
port.postMessage(args, [...new Set(transfer)])
201+
const callback = (args, transfer) => {
202+
postMessage(port, args, transfer)
197203
}
198204

199205
return callback
200206
}
207+
208+
/**
209+
* @param {MessagePort} port
210+
* @param {any} message
211+
* @param {Iterable<Transferable>} [transfer]
212+
*/
213+
const postMessage = (port, message, transfer) =>
214+
// @ts-expect-error - Built in types expect Transferable[] but it really
215+
// should be Iterable<Transferable>
216+
port.postMessage(message, transfer)

‎packages/ipfs-message-port-protocol/src/dag.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const decodeNode = ({ dagNode, cids }) => {
4141
* this node will be added to transfer so they are moved across without copy.
4242
*
4343
* @param {DAGNode} dagNode
44-
* @param {Transferable[]} [transfer]
44+
* @param {Set<Transferable>} [transfer]
4545
* @returns {EncodedDAGNode}
4646
*/
4747
export const encodeNode = (dagNode, transfer) => {
@@ -58,7 +58,7 @@ export const encodeNode = (dagNode, transfer) => {
5858
*
5959
* @param {DAGNode} value
6060
* @param {CID[]} cids
61-
* @param {Transferable[]} [transfer]
61+
* @param {Set<Transferable>} [transfer]
6262
* @returns {void}
6363
*/
6464
const collectNode = (value, cids, transfer) => {
@@ -70,11 +70,11 @@ const collectNode = (value, cids, transfer) => {
7070
encodeCID(cid, transfer)
7171
} else if (value instanceof ArrayBuffer) {
7272
if (transfer) {
73-
transfer.push(value)
73+
transfer.add(value)
7474
}
7575
} else if (ArrayBuffer.isView(value)) {
7676
if (transfer) {
77-
transfer.push(value.buffer)
77+
transfer.add(value.buffer)
7878
}
7979
} else if (Array.isArray(value)) {
8080
for (const member of value) {

‎packages/ipfs-message-port-protocol/src/rpc.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ export type Remote<T extends Record<string, unknown>> = {
66
[K in keyof T]: Procedure<T[K]>
77
}
88

9-
type Return<T> = T extends Promise<infer U>
9+
export type Return<T> = T extends Promise<infer U>
1010
? Promise<U & TransferOptions>
1111
: Promise<T & TransferOptions>
1212

1313
export interface QueryOptions {
1414
signal?: AbortSignal
1515
timeout?: number
16-
transfer?: Transferable[]
16+
transfer?: Set<Transferable>
1717
}
1818

1919
export interface TransferOptions {
20-
transfer?: Transferable[]
20+
transfer?: Set<Transferable>
2121
}
2222

2323
export type NonUndefined<A> = A extends undefined ? never : A

‎packages/ipfs-message-port-protocol/test/block.browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('block (browser)', function () {
2222
it('should decode Block over message channel & transfer bytes', async () => {
2323
const blockIn = uint8ArrayFromString('hello')
2424

25-
const transfer = []
25+
const transfer = new Set()
2626

2727
const blockOut = await move(encodeBlock(blockIn, transfer), transfer)
2828

‎packages/ipfs-message-port-protocol/test/cid.browser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('cid (browser)', function () {
2626

2727
it('should decode CID and transfer bytes', async () => {
2828
const cidIn = CID.parse('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
29-
const transfer = []
29+
const transfer = new Set()
3030
const cidDataIn = encodeCID(cidIn, transfer)
3131
const cidDataOut = await move(cidDataIn, transfer)
3232
const cidOut = decodeCID(cidDataOut)

‎packages/ipfs-message-port-protocol/test/core.browser.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('core', function () {
2626
deliver = resolve
2727
})
2828

29-
const transfer = []
29+
const transfer = new Set()
3030
const remote = decodeCallback(
3131
await move(encodeCallback(callback, transfer), transfer)
3232
)
@@ -49,7 +49,7 @@ describe('core', function () {
4949
deliver = resolve
5050
})
5151

52-
const transfer = []
52+
const transfer = new Set()
5353
const remote = decodeCallback(
5454
await move(encodeCallback(callback, transfer), transfer)
5555
)
@@ -74,7 +74,7 @@ describe('core', function () {
7474
yield { items: [uint8ArrayFromString('bla'), uint8ArrayFromString('bla')] }
7575
}
7676

77-
const transfer = []
77+
const transfer = new Set()
7878

7979
const remote = decodeIterable(
8080
await move(
@@ -118,7 +118,7 @@ describe('core', function () {
118118
}
119119
}
120120

121-
const transfer = []
121+
const transfer = new Set()
122122

123123
const remote = decodeIterable(
124124
await move(
@@ -159,7 +159,7 @@ describe('core', function () {
159159
throw Error('Producer Boom!')
160160
}
161161

162-
const transfer = []
162+
const transfer = new Set()
163163

164164
const remote = decodeIterable(
165165
await move(
@@ -200,7 +200,7 @@ describe('core', function () {
200200
}
201201
}
202202

203-
const transfer = []
203+
const transfer = new Set()
204204

205205
const remote = decodeIterable(
206206
await move(
@@ -245,14 +245,14 @@ describe('core', function () {
245245
yield * outgoing
246246
}
247247

248-
const transfer = []
248+
const transfer = new Set()
249249

250250
const remote = decodeIterable(
251251
await move(
252252
encodeIterable(
253253
iterate(),
254254
(data, transfer) => {
255-
transfer.push(data.buffer)
255+
transfer.add(data.buffer)
256256
return data
257257
},
258258
transfer
@@ -287,7 +287,7 @@ describe('core', function () {
287287
yield { items: [uint8ArrayFromString('bla'), uint8ArrayFromString('bla')] }
288288
}
289289

290-
const transfer = []
290+
const transfer = new Set()
291291

292292
const remote = decodeIterable(
293293
await move(
@@ -331,7 +331,7 @@ describe('core', function () {
331331
}
332332
}
333333

334-
const transfer = []
334+
const transfer = new Set()
335335

336336
const remote = decodeIterable(
337337
await move(
@@ -371,7 +371,7 @@ describe('core', function () {
371371
throw Error('Producer Boom!')
372372
}
373373

374-
const transfer = []
374+
const transfer = new Set()
375375

376376
const remote = decodeIterable(
377377
await move(
@@ -411,7 +411,7 @@ describe('core', function () {
411411
}
412412
}
413413

414-
const transfer = []
414+
const transfer = new Set()
415415

416416
const remote = decodeIterable(
417417
await move(
@@ -455,14 +455,14 @@ describe('core', function () {
455455
yield * outgoing
456456
}
457457

458-
const transfer = []
458+
const transfer = new Set()
459459

460460
const remote = decodeIterable(
461461
await move(
462462
encodeIterable(
463463
iterate(),
464464
(data, transfer) => {
465-
transfer.push(data.buffer)
465+
transfer.add(data.buffer)
466466
return data
467467
},
468468
transfer

‎packages/ipfs-message-port-protocol/test/dag.browser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('dag (browser)', function () {
5858
link: CID.parse(cid2)
5959
}
6060
}
61-
const transfer = []
61+
const transfer = new Set()
6262

6363
const nodeOut = decodeNode(
6464
await move(encodeNode(nodeIn, transfer), transfer)
@@ -80,7 +80,7 @@ describe('dag (browser)', function () {
8080
}
8181
})
8282

83-
expect(transfer).to.containSubset(
83+
expect([...transfer]).to.containSubset(
8484
[{ byteLength: 0 }, { byteLength: 0 }, { byteLength: 0 }],
8585
'transferred buffers were cleared'
8686
)

‎packages/ipfs-message-port-protocol/test/dag.spec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('dag', function () {
1010
this.timeout(10 * 1000)
1111

1212
describe('encodeNode / decodeNode', () => {
13-
it('shoud encode node', () => {
13+
it('should encode node', () => {
1414
const cid1 = CID.parse(
1515
'bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce'
1616
)
@@ -34,7 +34,7 @@ describe('dag', function () {
3434
expect(data.cids).to.include(cid2)
3535
})
3636

37-
it('shoud encode and add buffers to transfer list', () => {
37+
it('should encode and add buffers to transfer list', () => {
3838
const cid1 = CID.parse(
3939
'bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce'
4040
)
@@ -55,7 +55,7 @@ describe('dag', function () {
5555
}
5656
}
5757

58-
const transfer = []
58+
const transfer = new Set()
5959
const data = encodeNode(dagNode, transfer)
6060

6161
expect(data.dagNode).to.be.equal(dagNode)
@@ -64,14 +64,14 @@ describe('dag', function () {
6464
expect(data.cids).to.include(cid1)
6565
expect(data.cids).to.include(cid2)
6666

67-
expect(transfer).to.be.an.instanceOf(Array)
68-
expect(transfer).to.have.property('length', 3)
67+
expect(transfer).to.be.an.instanceOf(Set)
68+
expect(transfer).to.have.property('size', 3)
6969
expect(transfer).to.include(cid1.multihash.bytes.buffer)
7070
expect(transfer).to.include(cid2.multihash.bytes.buffer)
7171
expect(transfer).to.include(hi.buffer)
7272
})
7373

74-
it('shoud decode node', () => {
74+
it('should decode node', () => {
7575
const cid1 = CID.parse(
7676
'bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce'
7777
)
@@ -92,7 +92,7 @@ describe('dag', function () {
9292
}
9393
}
9494

95-
const transfer = []
95+
const transfer = new Set()
9696
const data = encodeNode(dagNode, transfer)
9797

9898
expect(data.dagNode).to.be.equal(dagNode)
@@ -101,8 +101,8 @@ describe('dag', function () {
101101
expect(data.cids).to.include(cid1)
102102
expect(data.cids).to.include(cid2)
103103

104-
expect(transfer).to.be.an.instanceOf(Array)
105-
expect(transfer).to.have.property('length', 3)
104+
expect(transfer).to.be.an.instanceOf(Set)
105+
expect(transfer).to.have.property('size', 3)
106106
expect(transfer).to.include(cid1.multihash.bytes.buffer)
107107
expect(transfer).to.include(cid2.multihash.bytes.buffer)
108108
expect(transfer).to.include(hi.buffer)

‎packages/ipfs-message-port-protocol/test/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const ipc = () => {
1515
/**
1616
* @template T
1717
* @param {T} data
18-
* @param {Transferable[]} [transfer]
18+
* @param {Iterable<Transferable>} [transfer]
1919
* @returns {Promise<T>}
2020
*/
2121
const ipcMove = async (data, transfer = []) => {

‎packages/ipfs-message-port-server/src/block.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class BlockService {
2323
/**
2424
* @typedef {Object} GetResult
2525
* @property {Uint8Array} block
26-
* @property {Transferable[]} transfer
26+
* @property {Set<Transferable>} transfer
2727
*
2828
* @typedef {Object} GetQuery
2929
* @property {EncodedCID} cid
@@ -36,15 +36,15 @@ export class BlockService {
3636
async get (query) {
3737
const cid = decodeCID(query.cid)
3838
const block = await this.ipfs.block.get(cid, query)
39-
/** @type {Transferable[]} */
40-
const transfer = []
39+
/** @type {Set<Transferable>} */
40+
const transfer = new Set()
4141
return { transfer, block: encodeBlock(block, transfer) }
4242
}
4343

4444
/**
4545
* @typedef {Object} PutResult
4646
* @property {EncodedCID} cid
47-
* @property {Transferable[]} transfer
47+
* @property {Set<Transferable>} transfer
4848
*
4949
* @typedef {Object} PutQuery
5050
* @property {Uint8Array} block
@@ -58,9 +58,8 @@ export class BlockService {
5858
async put (query) {
5959
const input = query.block
6060
const result = await this.ipfs.block.put(input, query)
61-
62-
/** @type {Transferable[]} */
63-
const transfer = []
61+
/** @type {Set<Transferable>} */
62+
const transfer = new Set()
6463

6564
return { transfer, cid: encodeCID(result, transfer) }
6665
}
@@ -78,8 +77,8 @@ export class BlockService {
7877
* @returns {Promise<EncodedRmResult[]>}
7978
*/
8079
async rm (query) {
81-
/** @type {Transferable[]} */
82-
const transfer = []
80+
/** @type {Set<Transferable>} */
81+
const transfer = new Set()
8382
const result = await all(
8483
this.ipfs.block.rm(query.cids.map(decodeCID), query)
8584
)
@@ -113,7 +112,7 @@ export class BlockService {
113112
* @param {Object} entry
114113
* @param {CID} entry.cid
115114
* @param {Error|void} [entry.error]
116-
* @param {Transferable[]} transfer
115+
* @param {Set<Transferable>} transfer
117116
*/
118117
const encodeRmEntry = (entry, transfer) => {
119118
const cid = encodeCID(entry.cid, transfer)

‎packages/ipfs-message-port-server/src/core.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ const matchInput = (input, decode) => {
247247
* @param {AsyncIterable<AddResult>} out
248248
*/
249249
const encodeAddAllResult = out => {
250-
/** @type {Transferable[]} */
251-
const transfer = []
250+
/** @type {Set<Transferable>} */
251+
const transfer = new Set()
252252
return {
253253
data: encodeIterable(out, encodeFileOutput, transfer),
254254
transfer
@@ -259,8 +259,8 @@ const encodeAddAllResult = out => {
259259
* @param {AddResult} out
260260
*/
261261
const encodeAddResult = out => {
262-
/** @type {Transferable[]} */
263-
const transfer = []
262+
/** @type {Set<Transferable>} */
263+
const transfer = new Set()
264264
return {
265265
data: encodeFileOutput(out, transfer),
266266
transfer
@@ -271,17 +271,17 @@ const encodeAddResult = out => {
271271
* @param {AsyncIterable<Uint8Array>} content
272272
*/
273273
const encodeCatResult = content => {
274-
/** @type {Transferable[]} */
275-
const transfer = []
274+
/** @type {Set<Transferable>} */
275+
const transfer = new Set()
276276
return { data: encodeIterable(content, moveBuffer, transfer), transfer }
277277
}
278278

279279
/**
280280
* @param {AsyncIterable<IPFSEntry>} entries
281281
*/
282282
const encodeLsResult = entries => {
283-
/** @type {Transferable[]} */
284-
const transfer = []
283+
/** @type {Set<Transferable>} */
284+
const transfer = new Set()
285285
return { data: encodeIterable(entries, encodeLsEntry, transfer), transfer }
286286
}
287287

@@ -302,17 +302,17 @@ const encodeLsEntry = ({ name, path, size, cid, type, mode, mtime }) => ({
302302
* Adds underlying `ArrayBuffer` to the transfer list.
303303
*
304304
* @param {Uint8Array} buffer
305-
* @param {Transferable[]} transfer
305+
* @param {Set<Transferable>} transfer
306306
* @returns {Uint8Array}
307307
*/
308308
const moveBuffer = (buffer, transfer) => {
309-
transfer.push(buffer.buffer)
309+
transfer.add(buffer.buffer)
310310
return buffer
311311
}
312312

313313
/**
314314
* @param {AddResult} file
315-
* @param {Transferable[]} _transfer
315+
* @param {Set<Transferable>} _transfer
316316
*/
317317
const encodeFileOutput = (file, _transfer) => ({
318318
...file,

‎packages/ipfs-message-port-server/src/dag.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class DAGService {
3434

3535
/**
3636
* @typedef {Object} EncodedGetResult
37-
* @property {Transferable[]} transfer
37+
* @property {Set<Transferable>} transfer
3838
* @property {string} [remainderPath]
3939
* @property {EncodedDAGNode} value
4040
*
@@ -60,8 +60,8 @@ export class DAGService {
6060
}
6161
)
6262

63-
/** @type {Transferable[]} */
64-
const transfer = []
63+
/** @type {Set<Transferable>} */
64+
const transfer = new Set()
6565
return { remainderPath, value: encodeNode(value, transfer), transfer }
6666
}
6767

‎packages/ipfs-message-port-server/src/files.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class FilesService {
2626
*/
2727
async stat (input) {
2828
const stat = await this.ipfs.files.stat(input.path, input)
29-
/** @type {Transferable[]} */
30-
const transfer = []
29+
/** @type {Set<Transferable>} */
30+
const transfer = new Set()
3131
return { stat: { ...stat, cid: encodeCID(stat.cid, transfer) }, transfer }
3232
}
3333
}

‎packages/ipfs-message-port-server/src/server.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import { encodeError } from 'ipfs-message-port-protocol/error'
3232
* @typedef {import('ipfs-message-port-protocol/src/rpc').ServiceQuery<T>} ServiceQuery
3333
*/
3434

35+
/**
36+
* @template T
37+
* @typedef {import('ipfs-message-port-protocol/src/rpc').Return<T>} Return
38+
*/
39+
3540
/**
3641
* @template T
3742
* @typedef {import('ipfs-message-port-protocol/src/rpc').RPCQuery<T>} RPCQuery
@@ -65,7 +70,7 @@ import { encodeError } from 'ipfs-message-port-protocol/error'
6570

6671
/**
6772
* @typedef {Object} TransferOptions
68-
* @property {Transferable[]} [transfer]
73+
* @property {Set<Transferable>} [transfer]
6974
*/
7075

7176
/**
@@ -97,6 +102,7 @@ export class Query {
97102
* @param {Inn<T>} input
98103
*/
99104
constructor (namespace, method, input) {
105+
/** @type {Return<any>} */
100106
this.result = new Promise((resolve, reject) => {
101107
this.succeed = resolve
102108
this.fail = reject
@@ -210,7 +216,9 @@ export class Server {
210216
if (!query.signal.aborted) {
211217
try {
212218
const value = await query.result
213-
const transfer = [...new Set(value.transfer || [])]
219+
const transfer = value.transfer
220+
221+
// Don't need the transfer value in the result
214222
delete value.transfer
215223

216224
port.postMessage(

‎packages/ipfs-message-port-server/test/transfer.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Server', function () {
1414
const cid = CID.parse('QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D')
1515

1616
return new Promise((resolve, reject) => {
17-
const channel = process.browser
17+
const channel = globalThis.MessageChannel
1818
? new globalThis.MessageChannel()
1919
: new MessageChannel()
2020

@@ -29,7 +29,7 @@ describe('Server', function () {
2929

3030
const service = new IPFSService()
3131
const server = new Server(service)
32-
const transfer = []
32+
const transfer = new Set()
3333

3434
server.run = a => a
3535
server.handleQuery(

0 commit comments

Comments
 (0)
This repository has been archived.