Comparing version 0.0.10 to 0.0.11
@@ -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": {}, | ||
} | ||
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
50932
559
5
3
+ Addedchai@^4.1.2
+ Addedmailparser@0.x.x
+ Addedmocha@^4.0.1
+ Addedaddressparser@1.0.1(transitive)
+ Addedassertion-error@1.1.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowser-stdout@1.3.0(transitive)
+ Addedchai@4.5.0(transitive)
+ Addedcheck-error@1.0.3(transitive)
+ Addedcommander@2.11.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddebug@3.1.0(transitive)
+ Addeddeep-eql@4.1.4(transitive)
+ Addeddiff@3.3.1(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedget-func-name@2.0.2(transitive)
+ Addedglob@7.1.2(transitive)
+ Addedgrowl@1.10.3(transitive)
+ Addedhas-flag@2.0.0(transitive)
+ Addedhe@1.1.1(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedloupe@2.3.7(transitive)
+ Addedmailparser@0.6.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmimelib@0.3.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@0.0.8(transitive)
+ Addedmkdirp@0.5.1(transitive)
+ Addedmocha@4.1.0(transitive)
+ Addedms@2.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpathval@1.1.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsupports-color@4.4.0(transitive)
+ Addedtype-detect@4.1.0(transitive)
+ Addeduue@3.1.2(transitive)
+ Addedwrappy@1.0.2(transitive)