braid-http 0.3.14 → 0.3.17

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.
@@ -377,7 +377,7 @@ var subscription_parser = (cb) => ({
377
377
 
378
378
  // Maybe we parsed an update! That's cool!
379
379
  if (this.state.result === 'success') {
380
- this.cb({
380
+ var update = {
381
381
  version: this.state.version,
382
382
  parents: this.state.parents,
383
383
  body: this.state.body,
@@ -385,7 +385,10 @@ var subscription_parser = (cb) => ({
385
385
 
386
386
  // Output extra_headers if there are some
387
387
  extra_headers: extra_headers(this.state.headers)
388
- })
388
+ }
389
+ for (var k in update)
390
+ if (update[k] === undefined) delete update[k]
391
+ this.cb(update)
389
392
 
390
393
  // Reset the parser for the next version!
391
394
  this.state = {input: this.state.input}
@@ -455,7 +458,7 @@ function parse_headers (input) {
455
458
 
456
459
  // Let's parse them! First define some variables:
457
460
  var headers = {},
458
- header_regex = /([\w-_]+):\s?(.*)\r?\n?/gy, // Parses one line a time
461
+ header_regex = /(:?[\w-_]+):\s?(.*)\r?\n?/gy, // Parses one line a time
459
462
  match,
460
463
  found_last_match = false
461
464
 
@@ -63,17 +63,27 @@ ${patch.content}`
63
63
  }
64
64
 
65
65
 
66
- // This function reads num_patches in pseudoheader format from a
67
- // ReadableStream and then fires a callback when they're finished.
66
+ // Deprecated method for legacy support
68
67
  function parse_patches (req, cb) {
68
+ parse_update(req, update => {
69
+ if (update.body)
70
+ // Return body as an "everything" patch
71
+ cb([{unit: 'everything', range: '', content: update.body}])
72
+ else
73
+ cb(update.patches)
74
+ })
75
+ }
76
+
77
+ // This function reads an update (either a set of patches, or a body) from a
78
+ // ReadableStream and then fires a callback when finished.
79
+ function parse_update (req, cb) {
69
80
  var num_patches = req.headers.patches
70
81
 
71
- // Parse a request body as an "everything" patch
72
82
  if (!num_patches && !req.headers['content-range']) {
73
83
  var body = ''
74
84
  req.on('data', chunk => {body += chunk.toString()})
75
85
  req.on('end', () => {
76
- cb([{unit: 'everything', range: '', content: body}])
86
+ cb({ body, patches: undefined })
77
87
  })
78
88
  }
79
89
 
@@ -95,7 +105,7 @@ function parse_patches (req, cb) {
95
105
  // Then return it
96
106
  req.on('end', () => {
97
107
  patches = [{unit, range, content: Buffer.concat(buffer).toString('utf8')}]
98
- cb(patches)
108
+ cb({ patches, body: undefined })
99
109
  })
100
110
  }
101
111
 
@@ -108,7 +118,7 @@ function parse_patches (req, cb) {
108
118
  // We check to send send patches each time we parse one. But if there
109
119
  // are zero to parse, we will never check to send them.
110
120
  if (num_patches === 0)
111
- return cb([])
121
+ return cb({ patches: [], body: undefined })
112
122
 
113
123
  req.on('data', function parse (chunk) {
114
124
 
@@ -150,7 +160,7 @@ function parse_patches (req, cb) {
150
160
 
151
161
  // We got all the patches! Pause the stream and tell the callback!
152
162
  req.pause()
153
- cb(patches)
163
+ cb({ patches, body: undefined })
154
164
  })
155
165
  req.on('end', () => {
156
166
  // If the stream ends before we get everything, then return what we
@@ -195,6 +205,9 @@ function braidify (req, res, next) {
195
205
  // Add the braidly request/response helper methods
196
206
  res.sendUpdate = (stuff) => send_update(res, stuff, req.url, peer)
197
207
  res.sendVersion = res.sendUpdate
208
+ req.parseUpdate = () => new Promise(
209
+ (done, err) => parse_update(req, (update) => done(update))
210
+ )
198
211
  req.patches = () => new Promise(
199
212
  (done, err) => parse_patches(req, (patches) => done(patches))
200
213
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "0.3.14",
3
+ "version": "0.3.17",
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
@@ -129,7 +129,7 @@ async function connect () {
129
129
  Braidify adds these fields and methods to requests and responses:
130
130
  - `req.subscribe`
131
131
  - `req.startSubscription({onClose: cb})`
132
- - `await req.patches()`
132
+ - `await req.parseUpdate()`
133
133
  - `res.sendUpdate()`
134
134
 
135
135
  Use it like this: