Skip to content

Commit

Permalink
and go
Browse files Browse the repository at this point in the history
  • Loading branch information
smurthas committed Feb 29, 2012
0 parents commit b58014f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
41 changes: 41 additions & 0 deletions index.js
@@ -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');
}
};
}
22 changes: 22 additions & 0 deletions package.json
@@ -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"
}
}
38 changes: 38 additions & 0 deletions test.js
@@ -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'});

0 comments on commit b58014f

Please sign in to comment.