Permalink
Browse files

updated json-sockets

  • Loading branch information...
1 parent 652d50b commit 61871c2923e20ce0b5b22f130bc239981a7ed1e8 @ianjorgensen ianjorgensen committed Oct 3, 2011
@@ -1,5 +1,6 @@
{
"hubs": {
- "example": "f2boE8yi2NQzmnGXX1Ve/YkFIwE="
+ "example": "f2boE8yi2NQzmnGXX1Ve/YkFIwE=",
+ "chat": "f2boE8yi2NQzmnGXX1Ve/YkFIwE="
}
}
View
@@ -0,0 +1,10 @@
+var query = require('./query');
+
+
+
+var subject = new Date('august 22 2011 10:30:00');
+//var subject = new Date();
+console.log(query.filter([{time:subject}],{time:{$datetime:'tuesday'}}));
+
+
+//console.log(query.reverse([{},{hi:1},{hi:{$gt:1}},{hi:{$gte:1}},{hi:{$gt:1,$lt:10}}],
View
@@ -0,0 +1,28 @@
+var query = require('./query');
+var berlin = {
+ lon: 52.523,
+ lat: 13.412
+};
+query.reverse(point:$distance:)
+//console.log(query.filter([{hi:3},{hi:10},{hi:14},{hi:11},{hi:30}],{hi:{$gt:5,$lt:23,$mod:[2,0]}}));
+
+//console.log(query.filter([{id:1},{id:2}],{id:{$gt:0}}));
+//console.log(query.reverse([{id:{$gt:1}},{id:1}],{id:2}));
+
+/*var query = {
+ age: {$gt:20, $lte:40, $mod:[2,0]} // only match even ages between 20+ and 40
+};*/
+
+/*var oslo = {
+ lon: 59.914,
+ lat: 10.752
+};
+var berlin = {
+ lon: 52.523,
+ lat: 13.412
+};*/
+
+
+var subject = new Date('august 22 2011 10:30:00');
+var subject = new Date();
+console.log(query.filter([{time:subject}],{time:{$datetime:'sat 09:--:-- +2'}}));
View
@@ -0,0 +1,19 @@
+var querify = require('querify');
+
+var filter = function(data, query) {
+ return querify.filter(data, {query:query});
+};
+exports.filter = filter;
+
+var reverse = function(queries, data) {
+ var result = [];
+
+ queries.forEach(function(query) {
+ if (querify.compile(query)(data)) {
+ result.push(query);
+ }
+ });
+
+ return result;
+};
+exports.reverse = reverse;
@@ -0,0 +1,27 @@
+# buffoon
+
+a module for buffering streams into strings, buffers, json or queries.
+it's available through npm
+
+ npm install buffoon
+
+usage is simple
+
+``` js
+var buffoon = require('buffoon');
+
+// convert a stream to a string
+buffoon.string(stream, console.log);
+
+// convert a stream to a specific string encoding (defaults to utf-8)
+buffoon.string(stream, 'ascii', console.log);
+
+// buffer a stream into a single buffer
+buffoon.buffer(stream, console.log);
+
+// parse a stream as json
+buffoon.json(stream, console.log);
+
+// parse a stream as a querystring
+buffoon.query(stream, console.log);
+```
@@ -0,0 +1,69 @@
+var common = require('common');
+var parseQuery = require('querystring').parse;
+
+var onclose = function(stream, callback) {
+ stream.on('error', callback);
+ stream.on('close', function() {
+ callback(new Error('premature close'));
+ });
+};
+
+exports.string = function(stream, encoding, callback) {
+ if (!callback) {
+ callback = encoding;
+ encoding = 'utf-8';
+ }
+
+ var buffer = '';
+
+ callback = common.once(callback);
+ onclose(stream, callback);
+
+ stream.setEncoding(encoding);
+ stream.on('data', function(data) {
+ buffer += data;
+ });
+ stream.on('end', function() {
+ callback(null, buffer);
+ });
+};
+
+exports.buffer = function(stream, callback) {
+ var buffer = [];
+ var length = 0;
+
+ callback = common.once(callback);
+ onclose(stream, callback);
+
+ stream.on('data', function(data) {
+ buffer.push(data);
+ length += data.length;
+ });
+ stream.on('end', function() {
+ var result = new Buffer(length);
+ var offset = 0;
+
+ for (var i = 0; i < buffer.length; i++) {
+ buffer[i].copy(result, offset);
+ offset += buffer[i].length;
+ }
+ callback(null, result);
+ });
+};
+
+var parser = function(strat) {
+ return function(stream, callback) {
+ exports.string(common.fork(callback, function(result) {
+ try {
+ result = strat(result);
+ } catch(err) {
+ callback(err);
+ return;
+ }
+ callback(null, result);
+ }));
+ };
+};
+
+exports.json = parser(JSON.parse);
+exports.query = parser(parseQuery);
@@ -0,0 +1,9 @@
+{
+ "name":"buffoon",
+ "version":"0.1.2",
+ "description":"buffer streams into strings, buffers, json or queries",
+ "keywords": ["buffering", "streams", "parsing"],
+ "dependencies": {"common":">=0.1.0"},
+ "author": "Mathias Buus Madsen <mathiasbuus@gmail.com>",
+ "main":"./index.js"
+}
Oops, something went wrong.

0 comments on commit 61871c2

Please sign in to comment.