Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-socket

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-socket - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

48

lib/json-socket.js

@@ -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 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc