Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b58014f
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var StreamSplitter = require('json-stream-splitter'); | ||
|
||
function RemnantError(remnant) { | ||
this.name = 'RemnantError' | ||
this.message = 'There was data remaining on a JSON stream.' | ||
this.remnant = remnant; | ||
} | ||
RemnantError.prototype = new Error(); | ||
RemnantError.prototype.constructor = RemnantError; | ||
|
||
function NonObjectError(nonObject) { | ||
this.name = 'NonObjectError' | ||
this.message = 'Tried to send something other than an object.' | ||
this.nonObject = nonObject; | ||
} | ||
RemnantError.prototype = new Error(); | ||
RemnantError.prototype.constructor = RemnantError; | ||
|
||
|
||
// req can be an http | ||
exports.recieveStream = function(req, cbEach, cbDone) { | ||
var splitStream = StreamSplitter.splitStream(req); | ||
splitStream.on('object', cbEach); | ||
|
||
var errs = []; | ||
splitStream.on('error', errs.push); | ||
|
||
splitStream.on('end', function() { | ||
cbDone(errs.length === 0 ? undefined :errs); | ||
}); | ||
} | ||
|
||
exports.createSendStream = function(req) { | ||
req.headers['content-type'] = 'application/jsonstream'; | ||
return { | ||
sendObject: function(object) { | ||
if (!(object && object instanceof Object)) throw new NonObjectError(object); | ||
req.write(JSON.stringify(object) + '\n'); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "request-jsonstream", | ||
"description": "Simple module to wrap a request and send or receive JSON as newline delimited object via request", | ||
"version": "0.0.1", | ||
"author": "Simon Murtha-Smith <simon@murtha-smith.com>", | ||
"keywords": [ | ||
"request", | ||
"json", | ||
"streaming" | ||
], | ||
"main" : "index.js", | ||
"repository" : { | ||
"type": "git", | ||
"url": "http://github.com/smurthas/request-jsonstreaming.git" | ||
}, | ||
"dependencies": { | ||
"json-stream-splitter": "0.0.2" | ||
}, | ||
"engines": { | ||
"node": ">=0.6.0 <0.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var express = require('express'); | ||
var expressStream = require('express-jsonstream'); | ||
var request = require('request'); | ||
|
||
var stream = require('./index'); | ||
|
||
// add express-jsonstream middleware | ||
var app = express.createServer(expressStream()); | ||
|
||
// listen for GETs, write out some objects | ||
app.get('/stream-get', function(req, res) { | ||
res.jsonStream({small:'world'}); | ||
res.jsonStream({after:'all'}); | ||
res.end(); | ||
}); | ||
|
||
// listen for POSTs, handle | ||
app.post('/stream-post', function(req, res) { | ||
req.jsonStream(function(object) { | ||
console.error("got object from post", object); | ||
// put it in the database | ||
}, function(errs) { | ||
if (errs) console.error("post errs", errs); | ||
res.send('ok'); | ||
}); | ||
}); | ||
|
||
app.listen(12345); | ||
|
||
// GET some JSON out of the streaming endpoint | ||
stream.recieveStream(request('http://localhost:12345/stream-get'), function(object) { | ||
console.error("got object", object); | ||
}, function(errs) { | ||
if (errs) console.error("get errs", errs); | ||
}); | ||
|
||
// POST some JSON back to the streaming endpoint | ||
stream.createSendStream(request.post('http://localhost:12345/stream-post')).sendObject({hello:'world'}); |