New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sipware

Package Overview
Dependencies
Maintainers
1
Versions
263
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sipware - npm Package Compare versions

Comparing version 0.0.10 to 0.0.11

13

lib/client.js

@@ -7,3 +7,3 @@ var net = require('net');

function Client(consumer, options) {
var buf = '';
var buf = Buffer.concat([]);

@@ -22,6 +22,11 @@ if(options.tls) {

this.client.pause();
data = buf + data.toString();
data = Buffer.concat([buf, data], buf.length + data.length);
consumer.read({data, trunk: this.client, options}).then(waste => {
buf = waste || buf;
consumer.read({data, trunk: this.client, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
this.client.resume();

@@ -28,0 +33,0 @@ })

var util = require('util');
var MailParser = require('mailparser').MailParser;
var Headers = require('./headers');
const cr = 13, lf = 10;

@@ -21,20 +22,6 @@ function Protocol(options) {

Protocol.prototype.getCounter = function(msg) {
var cnt = 1;
if(msg.headers.cc && msg.headers.cc.trim().length) {
cnt += msg.headers.cc.split(',');
}
return cnt;
}
Protocol.prototype.getId = function(msg) {
return msg.headers.id;
}
Protocol.prototype.createMessage = function(headers, data) {
if(Object.keys(headers).length) {
if(data instanceof Buffer) {
var msg = Buffer.toString();
var msg = data.toString();
} else if('object' === typeof data) {

@@ -47,3 +34,3 @@ var msg = JSON.stringify(data);

msg = msg || '';
headers['content-length'] = msg.length;
headers['content-length'] = Buffer.byteLength(msg, 'utf8');
return createHeaders(headers) + '\n' + msg;

@@ -53,33 +40,58 @@ }

function parseMessage(data, idx) {
return new Promise((res, rej) => {
var done, result = {};
var parser = new MailParser();
var msg = data.slice(0, idx);
// CRLF
function getBufSeparator(buf, offset=0) {
for(var i = offset; i < buf.length; i++) {
if(buf[i] == cr || buf[i] == lf) {
if((buf[i] == cr) && (buf[i + 1] == lf) && (buf[i + 2] == cr) && (buf[i + 3] == lf)) {
return i + 4;
} else if(buf[i] == lf && buf[i + 1] == lf) {
return i + 2;
}
}
}
parser.on('headers', headers => {
result.headers = Headers.toLower(headers);
var len = parseInt(result.headers['content-length']);
return -1;
}
if(len + idx + 2 <= data.length) {
var content = data.slice(idx + 2, idx + 2 + len);
//
// \r?\n\r?\n
function getStrSeparator(buf, offset=0) {
for(var i = offset; i < buf.length; i++) {
if(buf[i] == '\r' || buf[i] == '\n') {
if((buf[i] == '\r') && (buf[i + 1] == '\n') && (buf[i + 2] == '\r') && (buf[i + 3] == '\n')) {
return i + 4;
} else if(buf[i] == '\n' && buf[i + 1] == '\n') {
return i + 2;
}
}
}
if(content.length) {
result.content = content;
}
return -1;
}
result.waste = data.slice(idx + 2 + len);
done = true;
}
})
function getSeparator(input, offset) {
if(input instanceof Buffer) {
return getBufSeparator(input, offset)
} else {
return getStrSeparator(input, offset)
}
return -1;
}
function parseHeaders(data) {
return new Promise((res, rej) => {
var headers, parser = new MailParser();
parser.on('headers', result => {headers = Headers.toLower(result)})
parser.on('end', mobj => {
if(done) {
res(result);
var len = parseInt(headers['content-length']);
if(!isNaN(len)) {
headers['content-length'] = len;
res(headers)
} else {
res({});
res()
}
})
parser.write(msg);
parser.write(data);
parser.end();

@@ -89,35 +101,103 @@ })

async function parseMessages(data, callback) {
var msg, result = {messages: []},
input = data, idx = input.indexOf('\n\n');
//
Protocol.prototype.peek = async function(buf, offset=0) {
var sep = getSeparator(buf, offset);
while(idx > 0) {
msg = await parseMessage(input, idx);
if(sep > 0) {
var headers = await parseHeaders(buf.slice(offset, sep));
if(msg.headers) {
if(callback) {
result.messages.push(callback({headers: Headers.toLower(msg.headers), content: msg.content}));
} else {
result.messages.push({headers: Headers.toLower(msg.headers), content: msg.content});
}
if(headers) {
return {headers, sep, offset};
}
}
}
result.waste = msg.waste;
if(!msg.waste || !msg.waste.length) break;
// Return offset of next packet or -1
Protocol.prototype.next = function(info, buf) {
if(info && buf) {
var {headers, sep, offset} = info;
var len = headers['content-length'];
console.log('next:',sep,len,buf.length);
idx = msg.waste.indexOf('\n\n');
input = msg.waste;
if(buf.length - sep >= len) {
return len + sep;
}
}
return result;
return -1;
}
Protocol.prototype.readMessages = function(data, callback) {
return parseMessages(data, callback);
}
Protocol.prototype.parse = async function(buf) {
var data = {messages: [], buf};
var offset, info = await this.peek(buf, 0);
Protocol.prototype.parse = async function(data, callback) {
return await parseMessages(data);
while((offset = this.next(info, buf)) >= 0) {
var content = buf.slice(info.sep, offset);
data.buf = buf.slice(offset);
if(content && content.length) {
data.messages.push({headers: info.headers, content});
} else {
data.messages.push({headers: info.headers});
}
info = await this.peek(buf, offset);
}
if(!data.buf || !data.buf.length) {
delete data.buf;
}
return data;
}
module.exports = Protocol;
var p = new Protocol();
/*p.parse('12345').then(result => {
console.log('result1:'+JSON.stringify(result));
})
p.parse('12345\nAlala\n\n').then(result => {
console.log('result2:'+JSON.stringify(result));
})
p.parse('12345\nAlala\n\nLOL').then(result => {
console.log('result3:'+JSON.stringify(result));
})
p.parse('From: a\nTo: b\nContent-Length: 0\n\n').then(result => {
console.log('result4:'+JSON.stringify(result));
})
*/
/*p.parse('From: a\nTo: b\nContent-Length: 5\n\n54321').then(result => {
console.log('result3:'+JSON.stringify(result));
})
p.parse('From: a\nTo: b\nContent-Length: 10\n\n1111111111').then(result => {
console.log('result4:'+JSON.stringify(result));
})
p.parse('From: a\nTo: b\nContent-Length: 10\n\n1111111').then(result => {
console.log('result5:'+JSON.stringify(result));
})
p.parse('From: a\nTo: b\nContent-Length: 5\n\n54321From: a\nTo: b\nContent-Length: 6\n\n543210From: saft').then(result => {
console.log('result6:'+JSON.stringify(result));
})*/
/*p.parse('From: a\nTo: b\nContent-Length: 5\n\n54321').then(result => {
console.log('result3:'+JSON.stringify(result));
p.parse('From: a\nTo: b\nContent-Length: 10\n\n1111111111').then(result => {
console.log('result4:'+JSON.stringify(result));
p.parse('From: a\nTo: b\nContent-Length: 10\n\n1111111').then(result => {
console.log('result5:'+JSON.stringify(result));
p.parse('From: a\nTo: b\nContent-Length: 5\n\n54321From: a\nTo: b\nContent-Length: 6\n\n012345From: saft').then(result => {
console.log('result6:'+JSON.stringify(result));
})
})
})
})*/

@@ -16,3 +16,3 @@ var path = require('path');

this.server = srv.createServer(options, function(socket) {
var buf = '', id = utils.randomAscii(12);
var buf = Buffer.concat([]), id = utils.randomAscii(12);
var trunk = {id, socket, options, end: () => {socket.destroy()}};

@@ -23,6 +23,12 @@

socket.pause();
data = buf + data.toString();
console.log('SOCKET data:'+data.length+', buf:' + Buffer.byteLength(buf));
// data = buf + data.toString();
data = Buffer.concat([buf, data], buf.length + data.length);
trunk.read({data, trunk, options}).then(waste => {
buf = waste || buf;
trunk.read({data, trunk, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
socket.resume();

@@ -34,5 +40,7 @@ })

console.log('socket error: '+err);
consumer.declineChain(trunk);
})
socket.on('end', function() {
console.log('socket end');
consumer.declineChain(trunk);

@@ -39,0 +47,0 @@ })

{
"name": "sipware",
"version": "0.0.10",
"version": "0.0.11",
"main": "index.js",

@@ -16,2 +16,5 @@ "author": {

"dependencies": {
"chai": "^4.1.2",
"mailparser": "0.x.x",
"mocha": "^4.0.1"
},

@@ -24,2 +27,1 @@ "devDependencies": {},

}
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