braid-http 0.3.0 → 0.3.2

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.
@@ -155,7 +155,7 @@ async function braid_fetch (url, params = {}) {
155
155
  // We provide some shortcuts for Braid params
156
156
  if (params.version)
157
157
  params.headers.set('version', params.version.map(JSON.stringify).join(', '))
158
- if (params.parents)
158
+ if (params.parents?.length)
159
159
  params.headers.set('parents', params.parents.map(JSON.stringify).join(', '))
160
160
  if (params.subscribe)
161
161
  params.headers.set('subscribe', 'true')
@@ -488,8 +488,7 @@ function parse_headers (input) {
488
488
  // Success! Let's parse special headers
489
489
  if ('version' in headers)
490
490
  headers.version = JSON.parse('['+headers.version+']')
491
- if ('parents' in headers)
492
- headers.parents = JSON.parse('['+headers.parents+']')
491
+ headers.parents = JSON.parse('['+(headers.parents ?? '')+']')
493
492
  if ('patches' in headers)
494
493
  headers.patches = JSON.parse(headers.patches)
495
494
 
@@ -195,8 +195,8 @@ function braidify (req, res, next) {
195
195
  res.setHeader('Range-Request-Allow-Units', 'json')
196
196
 
197
197
  // Extract braid info from headers
198
- var version = req.headers.version && JSON.parse(req.headers.version),
199
- parents = req.headers.parents && JSON.parse('['+req.headers.parents+']'),
198
+ var version = JSON.parse('['+(req.headers.version ?? '')+']'),
199
+ parents = JSON.parse('['+(req.headers.parents ?? '')+']'),
200
200
  peer = req.headers['peer'],
201
201
  url = req.url.substr(1)
202
202
 
@@ -338,6 +338,7 @@ function send_version(res, data, url, peer) {
338
338
  value = value.map(JSON.stringify).join(", ")
339
339
  } else if (header === 'parents') {
340
340
  header = 'Parents' // Capitalize for prettiness
341
+ if (value.length == 0) continue // we express no parents as not having the field at all
341
342
  value = value.map(JSON.stringify).join(", ")
342
343
  }
343
344
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "An implementation of Braid-HTTP for Node.js and Browsers",
5
5
  "scripts": {
6
6
  "test": "node test/server.js"
package/readme.md CHANGED
@@ -51,7 +51,7 @@ fetch('https://braid.org/chat', {subscribe: true}).then(
51
51
  (new_version) => {
52
52
  console.log('We got a new version!', new_version)
53
53
  // {
54
- // version: "me",
54
+ // version: ["me"],
55
55
  // parents: ["mom", "dad"],
56
56
  // patches: [{unit: "json", range: ".foo", content: "3"}]
57
57
  // body: "3"
@@ -153,7 +153,7 @@ require('http').createServer(
153
153
 
154
154
  // Send the current version
155
155
  res.sendUpdate({
156
- version: 'greg',
156
+ version: ['greg'],
157
157
  body: JSON.stringify({greg: 'greg'})
158
158
  })
159
159
  }
@@ -184,14 +184,14 @@ app.get('/', (req, res) => {
184
184
 
185
185
  // Send the current version
186
186
  res.sendUpdate({
187
- version: 'greg',
187
+ version: ['greg'],
188
188
  parents: ['gr','eg'],
189
189
  body: JSON.stringify({greg: 'greg'})
190
190
  })
191
191
 
192
192
  // Or you can send patches like this:
193
193
  // res.sendUpdate({
194
- // version: 'greg',
194
+ // version: ['greg'],
195
195
  // parents: ['gr','eg'],
196
196
  // patches: [{range: '.greg', unit: 'json', content: '"greg"'}]
197
197
  // })
@@ -234,7 +234,7 @@ function connect () {
234
234
  (res) => {
235
235
  res.on('version', (version) => {
236
236
  // {
237
- // version: "me",
237
+ // version: ["me"],
238
238
  // parents: ["mom", "dad"],
239
239
  // patches: [{unit: "json", range: ".foo", content: "3"}]
240
240
  // body: "3"
@@ -272,5 +272,3 @@ automatically reconnect. (See
272
272
  [issue #980](https://github.com/node-fetch/node-fetch/issues/980) and
273
273
  [#753](https://github.com/node-fetch/node-fetch/issues/753).) We recommend
274
274
  using the `http` library (below) for requests on nodejs instead.
275
-
276
-