Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "net-ipc", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Simple message based IPC client/server providing bi-directional communication over sockets and TCP. Supports one-way messages, promise-based request-response, survey, broadcast and zlib-stream compression", | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"scripts": { | ||
@@ -31,2 +32,3 @@ "test": "node ./test/test.js" | ||
"devDependencies": { | ||
"@types/node": "^16.0.1", | ||
"eslint": "^7.27.0" | ||
@@ -33,0 +35,0 @@ }, |
@@ -22,7 +22,9 @@ "use strict"; | ||
this.options = { | ||
path: options.path, | ||
url: options.url, | ||
compress: Boolean(options.compress), | ||
messagepack: Boolean(options.messagepack), | ||
reconnect: typeof options.reconnect !== "undefined" ? Boolean(options.reconnect) : true, | ||
retries: Number(options.retries) > 0 ? Number(options.retries) : 3, | ||
maxRetryTime: Number(options.maxRetryTime) >= 500 ? Number(options.maxRetryTime) : 10000 | ||
retries: Number(options.retries) > 0 ? Number(options.retries) : Options.DEFAULT_RETRIES, | ||
maxRetryTime: Number(options.maxRetryTime) >= Options.RETRY_INCREMENT ? Number(options.maxRetryTime) : Options.DEFAULT_MAXRETRYTIME | ||
}; | ||
@@ -34,4 +36,4 @@ this._error = null; | ||
this._payload = null; | ||
this._url = options.url || null; | ||
this._path = options.path || (options.url ? null : Options.DEFAULT_PATH); | ||
this._url = this.options.url || null; | ||
this._path = this.options.path || (this.options.url ? null : Options.DEFAULT_PATH); | ||
if(this._url && typeof this._url !== "string") { throw new Error(ErrorMessages.BAD_URL); } | ||
@@ -109,3 +111,3 @@ if(this._path && typeof this._path !== "string") { throw new Error(ErrorMessages.BAD_PATH); } | ||
} | ||
}, Math.min(500 * ++this._retries, this.options.maxRetryTime)); | ||
}, Math.min(Options.RETRY_INCREMENT * ++this._retries, this.options.maxRetryTime)); | ||
} else { | ||
@@ -219,3 +221,3 @@ this._setStatus(ClientStatus.IDLE); | ||
const retries = r + 1; | ||
await new Promise(resolve => { setTimeout(resolve, Math.min(500 * retries, this.options.maxRetryTime)); }); | ||
await new Promise(resolve => { setTimeout(resolve, Math.min(Options.RETRY_INCREMENT * retries, this.options.maxRetryTime)); }); | ||
return this._tryWrite(op, data, nonce, retries); | ||
@@ -222,0 +224,0 @@ } |
"use strict"; | ||
const { Events, ConnectionEvents, MessageTypes, ErrorMessages } = require("./constants.js"); | ||
const { Events, ConnectionEvents, MessageTypes, ErrorMessages, Options } = require("./constants.js"); | ||
const interfaces = require("./interfaces.js"); | ||
@@ -126,3 +126,3 @@ | ||
for(let i = r; i < this._retries; i++) { | ||
await new Promise(resolve => { setTimeout(resolve, 500 * (i + 1)); }); | ||
await new Promise(resolve => { setTimeout(resolve, Options.RETRY_INCREMENT * (i + 1)); }); | ||
const connection = this.server.connections.find(c => c.id === this.id); | ||
@@ -129,0 +129,0 @@ if(connection) { return connection._tryWrite(op, data, nonce, i); } |
@@ -65,3 +65,10 @@ "use strict"; | ||
}, | ||
Options: { DEFAULT_PATH: "net-ipc" } | ||
Options: { | ||
DEFAULT_PATH: "net-ipc", | ||
DEFAULT_RETRIES: 3, | ||
DEFAULT_MAXRETRYTIME: 10000, | ||
DEFAULT_TIMEOUT: 10000, | ||
DEFAULT_CONNECTIONTIMEOUT: 10000, | ||
RETRY_INCREMENT: 500 | ||
} | ||
}; |
@@ -6,3 +6,4 @@ "use strict"; | ||
MessageTypes, | ||
ErrorMessages | ||
ErrorMessages, | ||
Options | ||
} = require("./constants.js"); | ||
@@ -24,3 +25,3 @@ | ||
}, | ||
request(data, timeout = 10000) { | ||
request(data, timeout = Options.DEFAULT_TIMEOUT) { | ||
if(!Number.isInteger(timeout)) { return Promise.reject(ErrorMessages.BAD_TIMEOUT); } | ||
@@ -44,3 +45,3 @@ return new Promise((ok, nope) => { | ||
}, | ||
ping(data, timeout = 10000) { | ||
ping(data, timeout = Options.DEFAULT_TIMEOUT) { | ||
if(!Number.isInteger(timeout)) { return Promise.reject(ErrorMessages.BAD_TIMEOUT); } | ||
@@ -112,29 +113,6 @@ return new Promise((ok, nope) => { | ||
const buffer = this._buffer; | ||
const bufferLength = buffer.length; | ||
const dataHeader = buffer.charCodeAt(0) >> 6; | ||
let dataLength = 0; | ||
switch(dataHeader) { | ||
case 0: { | ||
dataLength = buffer.charCodeAt(0) & 63; | ||
break; | ||
} | ||
case 1: { | ||
if(bufferLength < 2) { return; } | ||
dataLength = ((buffer.charCodeAt(0) & 63) << 8) + buffer.charCodeAt(1); | ||
break; | ||
} | ||
case 2: { | ||
if(bufferLength < 3) { return; } | ||
dataLength = ((((buffer.charCodeAt(0) & 63) << 8) + buffer.charCodeAt(1)) << 8) + buffer.charCodeAt(2); | ||
break; | ||
} | ||
case 3: { | ||
if(bufferLength < 4) { return; } | ||
dataLength = ((((((buffer.charCodeAt(0) & 63) << 8) + buffer.charCodeAt(1)) << 8) + buffer.charCodeAt(2)) << 8) + buffer.charCodeAt(3); | ||
break; | ||
} | ||
} | ||
const start = dataHeader + 1; | ||
const end = start + dataLength; | ||
if(bufferLength < end) { return; } | ||
const tag = this._untag(buffer); | ||
const start = tag[0]; | ||
const end = start + tag[1]; | ||
if(buffer.length < end) { return; } | ||
const slice = buffer.slice(start, end); | ||
@@ -180,3 +158,2 @@ this._buffer = buffer.slice(end); | ||
let packet; | ||
let length; | ||
if(this.connection.msgpack) { | ||
@@ -187,13 +164,3 @@ packet = this.connection.msgpack.pack(data).toString("latin1"); | ||
} | ||
const L = packet.length; | ||
if(L < (1 << 6)) { | ||
length = String.fromCharCode(L); | ||
} else if(L < (1 << 14)) { | ||
length = String.fromCharCode((1 << 6) + (L >> 8), L & 255); | ||
} else if(L < (1 << 22)) { | ||
length = String.fromCharCode((2 << 6) + (L >> 16), (L >> 8) & 255, L & 255); | ||
} else { | ||
length = String.fromCharCode((3 << 6) + (L >> 24), (L >> 16) & 255, (L >> 8) & 255, L & 255); | ||
} | ||
packet = length + packet; | ||
packet = this._tag(packet.length) + packet; | ||
if(this.connection.zlib) { | ||
@@ -204,2 +171,21 @@ packet = this.connection.zlib.deflate.process(packet); | ||
}, | ||
_tag(_size) { | ||
let size = _size; | ||
let tag = ""; | ||
while(size > 127) { | ||
tag += String.fromCharCode(size & 127); | ||
size >>= 7; | ||
} | ||
return tag + String.fromCharCode(size + 128); | ||
}, | ||
_untag(data) { | ||
let size = 0; | ||
let datasize = 0; | ||
while(size < 5) { | ||
datasize <<= 7; | ||
datasize += data.charCodeAt(size) & 127; | ||
if(data.charCodeAt(size++) > 127) { break; } | ||
} | ||
return [size, datasize]; | ||
}, | ||
_drain() { | ||
@@ -206,0 +192,0 @@ for(let i = 0; i < this._drainQueue.length; i++) { |
@@ -26,3 +26,3 @@ "use strict"; | ||
if(this.options.path && process.platform === "win32") { this.options.path = `\\\\.\\pipe\\${this.options.path.replace(/^\//, "").replace(/\//g, "-")}`; } | ||
this.options.retries = Number(this.options.retries) >= 0 ? Number(this.options.retries) : 3; | ||
this.options.retries = Number(this.options.retries) >= 0 ? Number(this.options.retries) : Options.DEFAULT_RETRIES; | ||
} | ||
@@ -73,3 +73,3 @@ start() { | ||
} | ||
survey(data, timeout = 10000) { | ||
survey(data, timeout = Options.DEFAULT_TIMEOUT) { | ||
if(!Number.isInteger(timeout)) { return Promise.reject(ErrorMessages.BAD_TIMEOUT); } | ||
@@ -110,3 +110,3 @@ return Promise.allSettled(this.connections.map(c => c.request(data, timeout))); | ||
client.close(); | ||
}, 10000); | ||
}, Options.DEFAULT_CONNECTIONTIMEOUT); | ||
client.connection.once(ConnectionEvents.READY, extras => { | ||
@@ -113,0 +113,0 @@ clearTimeout(timer); |
@@ -23,3 +23,3 @@ "use strict"; | ||
console.log(`\n[SOCKET SERVER] new connection received, assigned client id ${client.id} and payload ${payload}`); | ||
const id = `socket${client.connection.msgpack ? " messagepack" : ""}${client.connection.zlib ? " zlib" : ""}`; | ||
const id = `socket${client.connection.msgpack ? " messagepack" : ""} ${client.connection.zlib ? " zlib" : ""}`; | ||
if(!received[id]) { received[id] = 0; } | ||
@@ -26,0 +26,0 @@ client.connection.on("data", d => { received[id] += d.toString().length; }); |
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
45434
13
1024
2