braid-http
Advanced tools
Comparing version 0.3.14 to 0.3.16
@@ -458,3 +458,3 @@ // var peer = Math.random().toString(36).substr(2) | ||
var headers = {}, | ||
header_regex = /([\w-_]+):\s?(.*)\r?\n?/gy, // Parses one line a time | ||
header_regex = /(:?[\w-_]+):\s?(.*)\r?\n?/gy, // Parses one line a time | ||
match, | ||
@@ -461,0 +461,0 @@ found_last_match = false |
@@ -66,8 +66,18 @@ var assert = require('assert') | ||
// This function reads num_patches in pseudoheader format from a | ||
// ReadableStream and then fires a callback when they're finished. | ||
// Deprecated method for legacy support | ||
function parse_patches (req, cb) { | ||
parse_update(req, update => { | ||
if (update.body) | ||
// Return body as an "everything" patch | ||
cb([{unit: 'everything', range: '', content: update.body}]) | ||
else | ||
cb(update.patches) | ||
}) | ||
} | ||
// This function reads an update (either a set of patches, or a body) from a | ||
// ReadableStream and then fires a callback when finished. | ||
function parse_update (req, cb) { | ||
var num_patches = req.headers.patches | ||
// Parse a request body as an "everything" patch | ||
if (!num_patches && !req.headers['content-range']) { | ||
@@ -77,3 +87,3 @@ var body = '' | ||
req.on('end', () => { | ||
cb([{unit: 'everything', range: '', content: body}]) | ||
cb({ body, patches: undefined }) | ||
}) | ||
@@ -100,3 +110,3 @@ } | ||
patches = [{unit, range, content: Buffer.concat(buffer).toString('utf8')}] | ||
cb(patches) | ||
cb({ patches, body: undefined }) | ||
}) | ||
@@ -114,3 +124,3 @@ } | ||
if (num_patches === 0) | ||
return cb([]) | ||
return cb({ patches: [], body: undefined }) | ||
@@ -157,3 +167,3 @@ req.on('data', function parse (chunk) { | ||
req.pause() | ||
cb(patches) | ||
cb({ patches, body: undefined }) | ||
}) | ||
@@ -203,2 +213,5 @@ req.on('end', () => { | ||
res.sendVersion = res.sendUpdate | ||
req.parseUpdate = () => new Promise( | ||
(done, err) => parse_update(req, (update) => done(update)) | ||
) | ||
req.patches = () => new Promise( | ||
@@ -205,0 +218,0 @@ (done, err) => parse_patches(req, (patches) => done(patches)) |
{ | ||
"name": "braid-http", | ||
"version": "0.3.14", | ||
"version": "0.3.16", | ||
"description": "An implementation of Braid-HTTP for Node.js and Browsers", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -132,3 +132,3 @@ # Braid-HTTP | ||
- `req.startSubscription({onClose: cb})` | ||
- `await req.patches()` | ||
- `await req.parseUpdate()` | ||
- `res.sendUpdate()` | ||
@@ -135,0 +135,0 @@ |
47889
971