Skip to content

Commit

Permalink
[refactor] Use json-stream instead of our own parser
Browse files Browse the repository at this point in the history
Previous parser was using `g!\n!t` as a separator instead of commonly
used `\n`.
  • Loading branch information
mmalecki committed Jun 5, 2013
1 parent 43a691d commit 5bcc3c7
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 89 deletions.
6 changes: 0 additions & 6 deletions lib/godot/common/index.js
Expand Up @@ -34,12 +34,6 @@ exports.clone = function (data) {
//
exports.ReadWriteStream = require('./read-write-stream');

//
// ### @JsonParser {function}
// Constructor function for the godot streaming JsonParser
//
exports.JsonParser = require('./json-parser');

//
// ### @FilterStream {function}
// Constructor function for the base FilterStream
Expand Down
68 changes: 0 additions & 68 deletions lib/godot/common/json-parser.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/godot/net/client.js
Expand Up @@ -106,9 +106,9 @@ Client.prototype.remove = function (producer, id) {
//
Client.prototype.write = function (data) {
var message = !Array.isArray(data)
? JSON.stringify(data) + 'g!\n!t'
? JSON.stringify(data) + '\n'
: data.map(function (d) {
return JSON.stringify(d) + 'g!\n!t';
return JSON.stringify(d) + '\n';
}).join('');

if (this.type === 'tcp' || this.type === 'unix') {
Expand Down
8 changes: 4 additions & 4 deletions lib/godot/net/server.js
Expand Up @@ -10,7 +10,7 @@ var dgram = require('dgram'),
net = require('net'),
utile = require('utile'),
common = require('../common'),
JsonParser = common.JsonParser,
jsonStream = require('json-stream'),
ReadWriteStream = common.ReadWriteStream;

//
Expand Down Expand Up @@ -247,7 +247,7 @@ Server.prototype._onUdpMessage = function (msg, rinfo) {
//
// TODO: Streaming JSON parsing sounds like the right things to do here
//
msg = JSON.parse(('' + msg).replace('g!\n!t', ''));
msg = JSON.parse(('' + msg).replace('\n', ''));

reactors.forEach(function (reactor) {
reactor.source.write(msg);
Expand All @@ -266,7 +266,7 @@ Server.prototype._onTcpSocket = function (socket) {
var address = socket.remoteAddress,
port = socket.remotePort,
id = address + ':' + port,
parser = new JsonParser(),
parser = jsonStream(),
self = this,
reactors;

Expand Down Expand Up @@ -300,7 +300,7 @@ Server.prototype._onTcpSocket = function (socket) {
Server.prototype._onUnixSocket = function (socket) {
var self = this,
id = this.path,
parser = new JsonParser(),
parser = jsonStream(),
reactors;

if (!this.hosts[id]) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -26,7 +26,8 @@
"telenode": "0.0.3",
"utile": "0.1.7",
"window-stream": "~0.3.1",
"backoff": "2.1.x"
"backoff": "2.1.x",
"json-stream": "0.1.x"
},
"devDependencies": {
"optimist": "0.3.4",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/producer-tagged.json
Expand Up @@ -2,8 +2,8 @@
"host": "127.0.0.1",
"service": "godot/test",
"state": "test",
"description": "Waiting to test Godot",
"description": "Waiting to test\nGodot",
"tags": ["a", "b"],
"metric": 1,
"ttl": 10
}
}
4 changes: 2 additions & 2 deletions test/fixtures/producer-test.json
Expand Up @@ -2,8 +2,8 @@
"host": "127.0.0.1",
"service": "godot/test",
"state": "test",
"description": "Waiting to test Godot",
"description": "Waiting to test\nGodot",
"tags": ["test", "unit"],
"metric": 1,
"ttl": 100
}
}
8 changes: 4 additions & 4 deletions test/mocks/net.js
Expand Up @@ -7,7 +7,7 @@

var dgram = require('dgram'),
net = require('net'),
JsonParser = require('../../lib/godot/common').JsonParser;
jsonStream = require('json-stream');

//
// ### function createServer (options, callback)
Expand Down Expand Up @@ -59,7 +59,7 @@ exports.createServer = function (options, callback) {

if (options.type === 'udp') {
server = dgram.createSocket('udp4');
parser = new JsonParser();
parser = jsonStream();
parser.on('data', function (data) {
server.emit('data', data);
})
Expand All @@ -78,7 +78,7 @@ exports.createServer = function (options, callback) {
server.on('connection', function (socket) {
socket.setEncoding('utf8');

var parser = new JsonParser();
var parser = jsonStream();
socket.pipe(parser);

parser.on('data', function (data) {
Expand All @@ -94,7 +94,7 @@ exports.createServer = function (options, callback) {
server.on('connection', function (socket) {
socket.setEncoding('utf8');

var parser = new JsonParser();
var parser = jsonStream();
socket.pipe(parser);

parser.on('data', function (data) {
Expand Down

0 comments on commit 5bcc3c7

Please sign in to comment.