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

Commit d910aea

Browse files
authored
fix: add onError to pubsub.subscribe types (#3706)
This was missed in #3468. Also runs the type checker on the pubsub http client tests to ensure typing is correct.
1 parent 7cf404c commit d910aea

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
lines changed

packages/ipfs-core-types/src/pubsub/index.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface API<OptionExtension = {}> {
1313
* console.log(`subscribed to ${topic}`)
1414
* ```
1515
*/
16-
subscribe: (topic: string, handler: MessageHandlerFn, options?: AbortOptions & OptionExtension) => Promise<void>
16+
subscribe: (topic: string, handler: MessageHandlerFn, options?: SubscribeOptions & OptionExtension) => Promise<void>
1717

1818
/**
1919
* Unsubscribes from a pubsub topic
@@ -81,4 +81,12 @@ export interface Message {
8181
topicIDs: string[]
8282
}
8383

84+
export interface SubscribeOptions extends AbortOptions {
85+
/**
86+
* A callback to receive an error if one occurs during processing
87+
* subscription messages. Only supported by ipfs-http-client.
88+
*/
89+
onError?: (err: Error) => void
90+
}
91+
8492
export type MessageHandlerFn = (message: Message) => void

packages/ipfs-http-client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"it-concat": "^2.0.0",
8282
"it-first": "^1.0.4",
8383
"nock": "^13.0.2",
84+
"p-defer": "^3.0.0",
8485
"rimraf": "^3.0.2"
8586
},
8687
"engines": {

packages/ipfs-http-client/test/commands.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const f = require('./utils/factory')()
77
describe('.commands', function () {
88
this.timeout(60 * 1000)
99

10+
/** @type {import('ipfs-core-types').IPFS} */
1011
let ipfs
1112

1213
before(async () => {

packages/ipfs-http-client/test/node/agent.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const { expect } = require('aegir/utils/chai')
55
const ipfsClient = require('../../src').create
66
const delay = require('delay')
77

8+
/**
9+
* @typedef {import('http').IncomingMessage} IncomingMessage
10+
*
11+
* @param {(message: IncomingMessage) => Promise<any>} handler
12+
*/
813
function startServer (handler) {
914
return new Promise((resolve) => {
1015
// spin up a test http server to inspect the requests made by the library
@@ -20,15 +25,18 @@ function startServer (handler) {
2025
})
2126

2227
server.listen(0, () => {
28+
const addressInfo = server.address()
29+
2330
resolve({
24-
port: server.address().port,
31+
port: addressInfo && (typeof addressInfo === 'string' ? addressInfo : addressInfo.port),
2532
close: () => server.close()
2633
})
2734
})
2835
})
2936
}
3037

3138
describe('agent', function () {
39+
/** @type {import('http').Agent} */
3240
let agent
3341

3442
before(() => {
@@ -40,6 +48,7 @@ describe('agent', function () {
4048
})
4149

4250
it('restricts the number of concurrent connections', async () => {
51+
/** @type {((arg: any) => void)[]} */
4352
const responses = []
4453

4554
const server = await startServer(() => {

packages/ipfs-http-client/test/pubsub.spec.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
const { expect } = require('aegir/utils/chai')
55
const { AbortController } = require('native-abort-controller')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
7+
const defer = require('p-defer')
68

79
const f = require('./utils/factory')()
810

911
describe('.pubsub', function () {
1012
this.timeout(20 * 1000)
1113
describe('.subscribe', () => {
14+
/** @type {import('ipfs-core-types').IPFS} */
1215
let ipfs
16+
/** @type {any} */
1317
let ctl
1418

1519
beforeEach(async function () {
@@ -27,8 +31,7 @@ describe('.pubsub', function () {
2731
it('.onError when connection is closed', async () => {
2832
const topic = 'gossipboom'
2933
let messageCount = 0
30-
let onError
31-
const error = new Promise(resolve => { onError = resolve })
34+
const onError = defer()
3235

3336
await ipfs.pubsub.subscribe(topic, message => {
3437
messageCount++
@@ -38,47 +41,44 @@ describe('.pubsub', function () {
3841
ctl.stop().catch()
3942
}
4043
}, {
41-
onError
44+
onError: onError.resolve
4245
})
4346

44-
await ipfs.pubsub.publish(topic, 'hello')
45-
await ipfs.pubsub.publish(topic, 'bye')
47+
await ipfs.pubsub.publish(topic, uint8ArrayFromString('hello'))
48+
await ipfs.pubsub.publish(topic, uint8ArrayFromString('bye'))
4649

47-
await expect(error).to.eventually.be.fulfilled().and.to.be.instanceOf(Error)
50+
await expect(onError.promise).to.eventually.be.fulfilled().and.to.be.instanceOf(Error)
4851
})
4952

5053
it('does not call onError when aborted', async () => {
5154
const controller = new AbortController()
5255
const topic = 'gossipabort'
5356
const messages = []
54-
let onError
55-
let onReceived
56-
57-
const received = new Promise(resolve => { onReceived = resolve })
58-
const error = new Promise(resolve => { onError = resolve })
57+
const onError = defer()
58+
const onReceived = defer()
5959

6060
await ipfs.pubsub.subscribe(topic, message => {
6161
messages.push(message)
6262
if (messages.length === 2) {
63-
onReceived()
63+
onReceived.resolve()
6464
}
6565
}, {
66-
onError,
66+
onError: onError.resolve,
6767
signal: controller.signal
6868
})
6969

70-
await ipfs.pubsub.publish(topic, 'hello')
71-
await ipfs.pubsub.publish(topic, 'bye')
70+
await ipfs.pubsub.publish(topic, uint8ArrayFromString('hello'))
71+
await ipfs.pubsub.publish(topic, uint8ArrayFromString('bye'))
7272

73-
await received
73+
await onReceived.promise
7474
controller.abort()
7575

7676
// Stop the daemon
7777
await ctl.stop()
7878
// Just to make sure no error is caused by above line
79-
setTimeout(onError, 200, 'aborted')
79+
setTimeout(onError.resolve, 200, 'aborted')
8080

81-
await expect(error).to.eventually.be.fulfilled().and.to.equal('aborted')
81+
await expect(onError.promise).to.eventually.be.fulfilled().and.to.equal('aborted')
8282
})
8383
})
8484
})

packages/ipfs-http-client/test/utils/factory.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
// @ts-ignore no types
34
const { createFactory } = require('ipfsd-ctl')
45
const merge = require('merge-options')
56
const { isNode } = require('ipfs-utils/src/env')
@@ -13,6 +14,7 @@ const commonOptions = {
1314

1415
const commonOverrides = {
1516
go: {
17+
// @ts-ignore go-ipfs has no types
1618
ipfsBin: isNode ? require('go-ipfs').path() : undefined
1719
}
1820
}

packages/ipfs-http-client/tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"outDir": "dist"
55
},
66
"include": [
7-
"src"
7+
"src",
8+
"test/utils/factory.js",
9+
"test/pubsub.spec.js"
810
],
911
"references": [
1012
{

0 commit comments

Comments
 (0)