json-socket
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -5,6 +5,10 @@ var net = require('net'); | ||
var JsonSocket = function(socket) { | ||
var JsonSocket = function(socket, opts) { | ||
this._socket = socket; | ||
this._contentLength = null; | ||
this._buffer = ''; | ||
this._opts = opts || {} | ||
if (!this._opts.delimeter) { | ||
this._opts.delimeter = '#'; | ||
} | ||
this._closed = false; | ||
@@ -18,5 +22,13 @@ socket.on('data', this._onData.bind(this)); | ||
JsonSocket.sendSingleMessage = function(port, host, message, callback) { | ||
callback = callback || function(){}; | ||
var socket = new JsonSocket(new net.Socket()); | ||
JsonSocket.sendSingleMessage = function(port, host, message, opts, callback) { | ||
if (typeof(opts) === "function") { | ||
callback = opts; | ||
opts = {}; | ||
} else { | ||
callback = callback || function(){}; | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
} | ||
var socket = new JsonSocket(new net.Socket(), opts); | ||
socket.connect(port, host); | ||
@@ -31,5 +43,13 @@ socket.on('error', function(err) { | ||
JsonSocket.sendSingleMessageAndReceive = function(port, host, message, callback) { | ||
callback = callback || function(){}; | ||
var socket = new JsonSocket(new net.Socket()); | ||
JsonSocket.sendSingleMessageAndReceive = function(port, host, message, opts, callback) { | ||
if (typeof(opts) === "function") { | ||
callback = opts; | ||
opts = {}; | ||
} else { | ||
callback = callback || function(){}; | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
} | ||
var socket = new JsonSocket(new net.Socket(), opts); | ||
socket.connect(port, host); | ||
@@ -48,3 +68,3 @@ socket.on('error', function(err) { | ||
if (message.success === false) { | ||
return callback(new Error(message.message)); | ||
return callback(new Error(message.error)); | ||
} | ||
@@ -70,4 +90,4 @@ callback(null, message) | ||
if (this._contentLength == null) { | ||
var i = this._buffer.indexOf('#'); | ||
//Check if the buffer has a #, if not, the end of the buffer string might be in the middle of a content length string | ||
var i = this._buffer.indexOf(this._opts.delimeter); | ||
//Check if the buffer has a this._opts.delimeter or "#", if not, the end of the buffer string might be in the middle of a content length string | ||
if (i !== -1) { | ||
@@ -87,3 +107,3 @@ var rawContentLength = this._buffer.substring(0, i); | ||
if (this._contentLength != null) { | ||
var length = Buffer.byteLength(this._buffer, 'utf8'); | ||
var length = Buffer.byteLength(this._buffer, 'utf8'); | ||
if (length == this._contentLength) { | ||
@@ -113,3 +133,3 @@ this._handleMessage(this._buffer); | ||
}, | ||
sendError: function(err) { | ||
@@ -146,4 +166,4 @@ this.sendMessage(this._formatError(err)); | ||
var messageData = JSON.stringify(message); | ||
var length = Buffer.byteLength(messageData, 'utf8'); | ||
var data = length + '#' + messageData; | ||
var length = Buffer.byteLength(messageData, 'utf8'); | ||
var data = length + this._opts.delimeter + messageData; | ||
return data; | ||
@@ -150,0 +170,0 @@ }, |
@@ -6,3 +6,3 @@ { | ||
"author": "Sebastian Seilund", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -279,3 +279,3 @@ # JsonSocket [![Build Status](https://secure.travis-ci.org/sebastianseilund/node-json-socket.png?branch=master)](https://travis-ci.org/sebastianseilund/node-json-socket) | ||
"success": false, | ||
"message": "Something went wrong" | ||
"error": "Something went wrong" | ||
} | ||
@@ -282,0 +282,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19810
272
3