braid-http 0.3.3 → 0.3.5
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/braid-http-client.js +27 -20
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
var peer = Math.random().toString(36).substr(2)
|
|
1
|
+
// var peer = Math.random().toString(36).substr(2)
|
|
2
2
|
|
|
3
3
|
// ***************************
|
|
4
4
|
// http
|
|
5
5
|
// ***************************
|
|
6
6
|
|
|
7
7
|
function braidify_http (http) {
|
|
8
|
-
// Todo: Wrap .put to add `peer` header
|
|
9
8
|
http.normal_get = http.get
|
|
10
9
|
http.get = function braid_req (arg1, arg2, arg3) {
|
|
11
10
|
var url, options, cb
|
|
@@ -45,8 +44,8 @@ function braidify_http (http) {
|
|
|
45
44
|
if (options.subscribe)
|
|
46
45
|
options.headers.subscribe = 'true'
|
|
47
46
|
|
|
48
|
-
// Always add the `peer` header
|
|
49
|
-
options.headers.peer = options.headers.peer || peer
|
|
47
|
+
// // Always add the `peer` header
|
|
48
|
+
// options.headers.peer = options.headers.peer || peer
|
|
50
49
|
|
|
51
50
|
// Wrap the callback to provide our new .on('update', ...) feature
|
|
52
51
|
// on nodejs servers
|
|
@@ -158,8 +157,8 @@ async function braid_fetch (url, params = {}) {
|
|
|
158
157
|
console.assert(Array.isArray(params.parents),
|
|
159
158
|
'fetch(): `parents` must be an array')
|
|
160
159
|
|
|
161
|
-
// Always set the peer
|
|
162
|
-
params.headers.set('peer', peer)
|
|
160
|
+
// // Always set the peer
|
|
161
|
+
// params.headers.set('peer', peer)
|
|
163
162
|
|
|
164
163
|
// We provide some shortcuts for Braid params
|
|
165
164
|
if (params.version)
|
|
@@ -168,6 +167,8 @@ async function braid_fetch (url, params = {}) {
|
|
|
168
167
|
params.headers.set('parents', params.parents.map(JSON.stringify).join(', '))
|
|
169
168
|
if (params.subscribe)
|
|
170
169
|
params.headers.set('subscribe', 'true')
|
|
170
|
+
if (params.peer)
|
|
171
|
+
params.header.set('peer', params.peer)
|
|
171
172
|
|
|
172
173
|
// Prevent browsers from going to disk cache
|
|
173
174
|
params.cache = 'no-cache'
|
|
@@ -323,25 +324,24 @@ async function handle_fetch_stream (stream, cb) {
|
|
|
323
324
|
while (true) {
|
|
324
325
|
var versions = []
|
|
325
326
|
|
|
327
|
+
// Read the next chunk of stream!
|
|
326
328
|
try {
|
|
327
|
-
// Read the next chunk of stream!
|
|
328
329
|
var {done, value} = await reader.read()
|
|
329
|
-
|
|
330
|
-
// Check if this connection has been closed!
|
|
331
|
-
if (done) {
|
|
332
|
-
console.debug("Connection closed.")
|
|
333
|
-
cb(null, 'Connection closed')
|
|
334
|
-
return
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// Tell the parser to process some more stream
|
|
338
|
-
parser.read(decoder.decode(value))
|
|
339
330
|
}
|
|
340
|
-
|
|
341
331
|
catch (e) {
|
|
342
332
|
cb(null, e)
|
|
343
333
|
return
|
|
344
334
|
}
|
|
335
|
+
|
|
336
|
+
// Check if this connection has been closed!
|
|
337
|
+
if (done) {
|
|
338
|
+
console.debug("Connection closed.")
|
|
339
|
+
cb(null, 'Connection closed')
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Tell the parser to process some more stream
|
|
344
|
+
parser.read(decoder.decode(value))
|
|
345
345
|
}
|
|
346
346
|
}
|
|
347
347
|
|
|
@@ -367,9 +367,16 @@ var subscription_parser = (cb) => ({
|
|
|
367
367
|
|
|
368
368
|
// Now loop through the input and parse until we hit a dead end
|
|
369
369
|
while (this.state.input.trim() !== '') {
|
|
370
|
-
this.state = parse_update (this.state)
|
|
371
370
|
|
|
372
|
-
//
|
|
371
|
+
// Try to parse an update
|
|
372
|
+
try {
|
|
373
|
+
this.state = parse_update (this.state)
|
|
374
|
+
} catch (e) {
|
|
375
|
+
this.cb(null, e)
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Maybe we parsed an update! That's cool!
|
|
373
380
|
if (this.state.result === 'success') {
|
|
374
381
|
this.cb({
|
|
375
382
|
version: this.state.version,
|