braid-http 1.3.92 → 1.3.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/braid-http-client.js +16 -0
- package/braid-http-server.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,6 +110,29 @@ fetch('https://braid.org/chat', {subscribe: true, retry: true, parents: () => {
|
|
|
110
110
|
)
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
+
You can monitor the subscription's connection status with `onSubscriptionStatus`:
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
fetch('https://braid.org/chat', {
|
|
117
|
+
subscribe: true,
|
|
118
|
+
retry: true,
|
|
119
|
+
onSubscriptionStatus: ({online, error, status, statusText}) => {
|
|
120
|
+
if (online)
|
|
121
|
+
console.log('Connected!')
|
|
122
|
+
else
|
|
123
|
+
console.log('Disconnected:', error)
|
|
124
|
+
}
|
|
125
|
+
}).then(
|
|
126
|
+
res => res.subscribe(
|
|
127
|
+
(update) => { console.log('Got update!', update) }
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The callback receives an object with only the fields relevant to the event:
|
|
133
|
+
- `{online: true}` — the subscription is connected
|
|
134
|
+
- `{online: false, error}` — the subscription went offline, with the error/reason for disconnection
|
|
135
|
+
|
|
113
136
|
### Example Subscription with Async/Await
|
|
114
137
|
|
|
115
138
|
```javascript
|
package/braid-http-client.js
CHANGED
|
@@ -231,6 +231,7 @@ async function braid_fetch (url, params = {}) {
|
|
|
231
231
|
)
|
|
232
232
|
|
|
233
233
|
var retry_count = 0
|
|
234
|
+
var subscription_online = false
|
|
234
235
|
var res = null
|
|
235
236
|
var subscription_cb = null
|
|
236
237
|
var subscription_error = null
|
|
@@ -265,6 +266,12 @@ async function braid_fetch (url, params = {}) {
|
|
|
265
266
|
// and in those cases, we want to make sure to close the fetch
|
|
266
267
|
underlying_aborter?.abort()
|
|
267
268
|
|
|
269
|
+
// Notify subscription went offline
|
|
270
|
+
if (params.onSubscriptionStatus && subscription_online) {
|
|
271
|
+
subscription_online = false
|
|
272
|
+
params.onSubscriptionStatus({online: false, error: e})
|
|
273
|
+
}
|
|
274
|
+
|
|
268
275
|
// see if we should retry..
|
|
269
276
|
var retry = params.retry && // only try to reconnect if the user has chosen to
|
|
270
277
|
e.name !== "AbortError" && // don't retry if the user has chosen to abort
|
|
@@ -466,12 +473,21 @@ async function braid_fetch (url, params = {}) {
|
|
|
466
473
|
give_up = false
|
|
467
474
|
}
|
|
468
475
|
if (give_up) {
|
|
476
|
+
if (params.onSubscriptionStatus && subscription_online) {
|
|
477
|
+
subscription_online = false
|
|
478
|
+
params.onSubscriptionStatus({online: false, error: new Error(`giving up because of http status: ${res.status}`)})
|
|
479
|
+
}
|
|
469
480
|
if (subscription_cb) subscription_error?.(new Error(`giving up because of http status: ${res.status}${(res.status === 401 || res.status === 403) ? ` (access denied)` : ''}`))
|
|
470
481
|
} else if (!res.ok) throw new Error(`status not ok: ${res.status}`)
|
|
471
482
|
}
|
|
472
483
|
|
|
473
484
|
if (subscription_cb && res.ok) start_subscription(subscription_cb, subscription_error)
|
|
474
485
|
|
|
486
|
+
if (subscription_cb && res.ok && params.onSubscriptionStatus) {
|
|
487
|
+
subscription_online = true
|
|
488
|
+
params.onSubscriptionStatus({online: true})
|
|
489
|
+
}
|
|
490
|
+
|
|
475
491
|
params?.retry?.onRes?.(res)
|
|
476
492
|
retry_count = 0
|
|
477
493
|
|
package/braid-http-server.js
CHANGED
|
@@ -432,6 +432,7 @@ function braidify (req, res, next) {
|
|
|
432
432
|
}
|
|
433
433
|
}
|
|
434
434
|
var mw = new MultiplexedWritable(m, request)
|
|
435
|
+
mw.on('error', () => {}) // EPIPE when client disconnects mid-stream
|
|
435
436
|
|
|
436
437
|
// then we create a fake server response,
|
|
437
438
|
// that pipes data to our fake socket
|