Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 3a0fe8a

Browse files
author
Alan Shaw
committed
fix: add workaround for subscribe in Firefox
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 370a801 commit 3a0fe8a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/pubsub/subscribe.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ const ndjson = require('iterable-ndjson')
44
const explain = require('explain-error')
55
const bs58 = require('bs58')
66
const { Buffer } = require('buffer')
7+
const log = require('debug')('ipfs-http-client:pubsub:subscribe')
78
const { objectToQuery } = require('../lib/querystring')
89
const configure = require('../lib/configure')
910
const { ok, toIterable } = require('../lib/fetch')
1011
const SubscriptionTracker = require('./subscription-tracker')
1112

1213
module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
1314
const subsTracker = SubscriptionTracker.singleton()
15+
const publish = require('./publish')({ fetch, apiAddr, apiPath, headers })
1416

1517
return async (topic, handler, options) => {
1618
options = options || {}
@@ -25,6 +27,18 @@ module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
2527
const url = `${apiAddr}${apiPath}/pubsub/sub${qs}`
2628
let res
2729

30+
// In Firefox, the initial call to fetch does not resolve until some data
31+
// is received. If this doesn't happen within 1 second send an empty message
32+
// to kickstart the process.
33+
const ffWorkaround = setTimeout(async () => {
34+
log(`Publishing empty message to "${topic}" to resolve subscription request`)
35+
try {
36+
await publish(topic, Buffer.alloc(0), options)
37+
} catch (err) {
38+
log('Failed to publish empty message', err)
39+
}
40+
}, 1000)
41+
2842
try {
2943
res = await ok(fetch(url, {
3044
method: 'POST',
@@ -36,6 +50,8 @@ module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
3650
throw err
3751
}
3852

53+
clearTimeout(ffWorkaround)
54+
3955
readMessages(ndjson(toIterable(res.body)), {
4056
onMessage: handler,
4157
onEnd: () => subsTracker.unsubscribe(topic, handler),
@@ -45,8 +61,7 @@ module.exports = configure(({ fetch, apiAddr, apiPath, headers }) => {
4561
})
4662

4763
async function readMessages (msgStream, { onMessage, onEnd, onError }) {
48-
// eslint-disable-next-line no-console
49-
onError = onError || (err => console.error(err))
64+
onError = onError || log
5065

5166
try {
5267
for await (const msg of msgStream) {
@@ -58,7 +73,7 @@ async function readMessages (msgStream, { onMessage, onEnd, onError }) {
5873
topicIDs: msg.topicIDs
5974
})
6075
} catch (err) {
61-
onError(explain(err, 'Failed to parse pubsub message'), false) // Not fatal
76+
onError(explain(err, 'Failed to parse pubsub message'), false, msg) // Not fatal
6277
}
6378
}
6479
} catch (err) {

0 commit comments

Comments
 (0)