braid-http 0.1.3 → 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.
- package/braid-http-client.js +3 -3
- package/braid-http-server.js +13 -12
- package/package.json +1 -1
- package/readme.md +4 -4
package/braid-http-client.js
CHANGED
|
@@ -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[
|
|
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[
|
|
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
|
package/braid-http-server.js
CHANGED
|
@@ -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
|
-
|
|
78
|
-
|
|
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
|
|
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
|
)
|
package/package.json
CHANGED
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.
|
|
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.
|
|
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.
|
|
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.
|
|
193
|
+
// res.sendUpdate({
|
|
194
194
|
// version: 'greg',
|
|
195
195
|
// parents: ['gr','eg'],
|
|
196
196
|
// patches: [{range: '.greg', unit: 'json', content: '"greg"'}]
|