braid-http 0.1.2 → 0.1.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.
@@ -499,7 +499,7 @@ function parse_headers (input) {
499
499
  }
500
500
 
501
501
  // Content-range is of the form '<unit> <range>' e.g. 'json .index'
502
- var content_range_regex = /(\S+) (.*)/
502
+ var content_range_regex = /(\S+)( (.*))?/
503
503
  function parse_body (state) {
504
504
 
505
505
  // Parse Body Snapshot
@@ -529,7 +529,7 @@ function parse_body (state) {
529
529
  }
530
530
  state.patches = [{
531
531
  unit: match[1],
532
- range: match[2],
532
+ range: match[3],
533
533
  content: state.input.substring(0, content_length),
534
534
 
535
535
  // Question: Perhaps we should include headers here, like we do for
@@ -618,7 +618,7 @@ function parse_body (state) {
618
618
  }
619
619
 
620
620
  last_patch.unit = match[1]
621
- last_patch.range = match[2]
621
+ last_patch.range = match[3]
622
622
  last_patch.content = state.input.substr(0, content_length)
623
623
 
624
624
  // Consume the parsed input
@@ -74,12 +74,8 @@ function parse_patches (req, cb) {
74
74
  assert(req.headers['content-range'], 'No patches to parse: need `Patches: N` or `Content-Range:` header in ' + JSON.stringify(req.headers))
75
75
 
76
76
  // Parse the Content-Range header
77
- var match = req.headers['content-range'].match(/(\S+) (.*)/)
78
- if (!match) {
79
- console.error('Cannot parse Content-Range in', JSON.stringify(headers))
80
- process.exit(1)
81
- }
82
- var [unit, range] = match.slice(1)
77
+ // Content-range is of the form '<unit> <range>' e.g. 'json .index'
78
+ var [unit, range] = parse_content_range(req.headers['content-range'])
83
79
 
84
80
  // The contents of the patch is in the request body
85
81
  var buffer = ''
@@ -148,12 +144,7 @@ function parse_patches (req, cb) {
148
144
  // XX Todo: support custom patch types beyond content-range.
149
145
 
150
146
  // Content-range is of the form '<unit> <range>' e.g. 'json .index'
151
- var match = headers['content-range'].match(/(\S+) (.*)/)
152
- if (!match) {
153
- console.error('Cannot parse Content-Range in', JSON.stringify(headers))
154
- process.exit(1)
155
- }
156
- var [unit, range] = match.slice(1)
147
+ var [unit, range] = parse_content_range(headers['content-range'])
157
148
  var patch_content =
158
149
  buffer.substring(headers_length + blank_line.length,
159
150
  headers_length + blank_line.length + body_length)
@@ -178,6 +169,15 @@ function parse_patches (req, cb) {
178
169
  }
179
170
  }
180
171
 
172
+ function parse_content_range (range_string) {
173
+ console.log('range string is', range_string)
174
+ var match = range_string.match(/(\S+)( (.*))?/)
175
+ console.log('##### match is', match)
176
+ if (!match) throw 'Cannot parse Content-Range in ' + string
177
+ var [unit, range] = [match[1], match[3]]
178
+ return [unit, range]
179
+ }
180
+
181
181
  function braidify (req, res, next) {
182
182
  // console.log('\n## Braidifying', req.method, req.url, req.headers.peer)
183
183
 
@@ -203,6 +203,7 @@ function braidify (req, res, next) {
203
203
 
204
204
  // Add the braidly request/response helper methods
205
205
  res.sendVersion = (stuff) => send_version(res, stuff, req.url, peer)
206
+ res.sendUpdate = res.sendVersion
206
207
  req.patches = () => new Promise(
207
208
  (done, err) => parse_patches(req, (patches) => done(patches))
208
209
  )
@@ -249,7 +250,9 @@ function braidify (req, res, next) {
249
250
  }
250
251
 
251
252
  // Check the Useragent to work around Firefox bugs
252
- if (req.headers['user-agent'].toLowerCase().indexOf('firefox') > -1)
253
+ if (req.headers['user-agent']
254
+ && typeof req.headers['user-agent'] === 'string'
255
+ && req.headers['user-agent'].toLowerCase().indexOf('firefox') > -1)
253
256
  res.is_firefox = true
254
257
 
255
258
  next && next()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-http",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
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
@@ -130,7 +130,7 @@ Braidify adds these fields and methods to requests and responses:
130
130
  - `req.subscribe`
131
131
  - `req.startSubscription({onClose: cb})`
132
132
  - `await req.patches()`
133
- - `res.sendVersion()`
133
+ - `res.sendUpdate()`
134
134
 
135
135
  Use it like this:
136
136
 
@@ -152,7 +152,7 @@ require('http').createServer(
152
152
  res.statusCode = 200
153
153
 
154
154
  // Send the current version
155
- res.sendVersion({
155
+ res.sendUpdate({
156
156
  version: 'greg',
157
157
  body: JSON.stringify({greg: 'greg'})
158
158
  })
@@ -183,14 +183,14 @@ app.get('/', (req, res) => {
183
183
  res.statusCode = 200
184
184
 
185
185
  // Send the current version
186
- res.sendVersion({
186
+ res.sendUpdate({
187
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
- // res.sendVersion({
193
+ // res.sendUpdate({
194
194
  // version: 'greg',
195
195
  // parents: ['gr','eg'],
196
196
  // patches: [{range: '.greg', unit: 'json', content: '"greg"'}]