iosignal 7.0.0 → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/esm/io.d.ts +16 -13
- package/dist/browser/esm/io.js +1 -1
- package/dist/browser/esm/io.js.map +1 -1
- package/dist/browser/iife/io.js +1 -1
- package/dist/browser/iife/io.js.map +1 -1
- package/dist/node/iosignal.cjs +174 -107
- package/dist/node/iosignal.d.cts +980 -0
- package/dist/node/iosignal.d.ts +980 -0
- package/dist/node/iosignal.js +174 -107
- package/dist/types/client/IOCore.d.ts +6 -5
- package/dist/types/client/browser/IOWebSocket.d.ts +3 -3
- package/package.json +20 -6
package/dist/node/iosignal.cjs
CHANGED
|
@@ -17,7 +17,7 @@ var fs = require('fs');
|
|
|
17
17
|
var process$1 = require('process');
|
|
18
18
|
var path = require('path');
|
|
19
19
|
|
|
20
|
-
var version$1 = "7.0.
|
|
20
|
+
var version$1 = "7.0.3";
|
|
21
21
|
var pkg = {
|
|
22
22
|
version: version$1};
|
|
23
23
|
|
|
@@ -761,7 +761,6 @@ const t=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1
|
|
|
761
761
|
*/var J=(Y||(Y=1,function(t){const e=function(){if(K)return V;K=1,V.byteLength=function(t){var e=o(t),r=e[0],n=e[1];return 3*(r+n)/4-n},V.toByteArray=function(t){var n,i,f=o(t),s=f[0],u=f[1],h=new r(function(t,e,r){return 3*(e+r)/4-r}(0,s,u)),a=0,c=u>0?s-4:s;for(i=0;i<c;i+=4)n=e[t.charCodeAt(i)]<<18|e[t.charCodeAt(i+1)]<<12|e[t.charCodeAt(i+2)]<<6|e[t.charCodeAt(i+3)],h[a++]=n>>16&255,h[a++]=n>>8&255,h[a++]=255&n;return 2===u&&(n=e[t.charCodeAt(i)]<<2|e[t.charCodeAt(i+1)]>>4,h[a++]=255&n),1===u&&(n=e[t.charCodeAt(i)]<<10|e[t.charCodeAt(i+1)]<<4|e[t.charCodeAt(i+2)]>>2,h[a++]=n>>8&255,h[a++]=255&n),h},V.fromByteArray=function(e){for(var r,n=e.length,i=n%3,o=[],f=16383,u=0,h=n-i;u<h;u+=f)o.push(s(e,u,u+f>h?h:u+f));return 1===i?(r=e[n-1],o.push(t[r>>2]+t[r<<4&63]+"==")):2===i&&(r=(e[n-2]<<8)+e[n-1],o.push(t[r>>10]+t[r>>4&63]+t[r<<2&63]+"=")),o.join("")};for(var t=[],e=[],r="undefined"!=typeof Uint8Array?Uint8Array:Array,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",i=0;i<64;++i)t[i]=n[i],e[n.charCodeAt(i)]=i;function o(t){var e=t.length;if(e%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var r=t.indexOf("=");return -1===r&&(r=e),[r,r===e?0:4-r%4]}function f(e){return t[e>>18&63]+t[e>>12&63]+t[e>>6&63]+t[63&e]}function s(t,e,r){for(var n,i=[],o=e;o<r;o+=3)n=(t[o]<<16&16711680)+(t[o+1]<<8&65280)+(255&t[o+2]),i.push(f(n));return i.join("")}return e["-".charCodeAt(0)]=62,e["_".charCodeAt(0)]=63,V}(),r=Q(),n="function"==typeof Symbol&&"function"==typeof Symbol.for?Symbol.for("nodejs.util.inspect.custom"):null;t.Buffer=f,t.SlowBuffer=function(t){return +t!=t&&(t=0),f.alloc(+t)},t.INSPECT_MAX_BYTES=50;const i=2147483647;function o(t){if(t>i)throw new RangeError('The value "'+t+'" is invalid for option "size"');const e=new Uint8Array(t);return Object.setPrototypeOf(e,f.prototype),e}function f(t,e,r){if("number"==typeof t){if("string"==typeof e)throw new TypeError('The "string" argument must be of type string. Received type number');return h(t)}return s(t,e,r)}function s(t,e,r){if("string"==typeof t)return function(t,e){if("string"==typeof e&&""!==e||(e="utf8"),!f.isEncoding(e))throw new TypeError("Unknown encoding: "+e);const r=0|p(t,e);let n=o(r);const i=n.write(t,e);return i!==r&&(n=n.slice(0,i)),n}(t,e);if(ArrayBuffer.isView(t))return function(t){if(J(t,Uint8Array)){const e=new Uint8Array(t);return c(e.buffer,e.byteOffset,e.byteLength)}return a(t)}(t);if(null==t)throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t);if(J(t,ArrayBuffer)||t&&J(t.buffer,ArrayBuffer))return c(t,e,r);if("undefined"!=typeof SharedArrayBuffer&&(J(t,SharedArrayBuffer)||t&&J(t.buffer,SharedArrayBuffer)))return c(t,e,r);if("number"==typeof t)throw new TypeError('The "value" argument must not be of type number. Received type number');const n=t.valueOf&&t.valueOf();if(null!=n&&n!==t)return f.from(n,e,r);const i=function(t){if(f.isBuffer(t)){const e=0|l(t.length),r=o(e);return 0===r.length||t.copy(r,0,0,e),r}return void 0!==t.length?"number"!=typeof t.length||W(t.length)?o(0):a(t):"Buffer"===t.type&&Array.isArray(t.data)?a(t.data):void 0}(t);if(i)return i;if("undefined"!=typeof Symbol&&null!=Symbol.toPrimitive&&"function"==typeof t[Symbol.toPrimitive])return f.from(t[Symbol.toPrimitive]("string"),e,r);throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t)}function u(t){if("number"!=typeof t)throw new TypeError('"size" argument must be of type number');if(t<0)throw new RangeError('The value "'+t+'" is invalid for option "size"')}function h(t){return u(t),o(t<0?0:0|l(t))}function a(t){const e=t.length<0?0:0|l(t.length),r=o(e);for(let n=0;n<e;n+=1)r[n]=255&t[n];return r}function c(t,e,r){if(e<0||t.byteLength<e)throw new RangeError('"offset" is outside of buffer bounds');if(t.byteLength<e+(r||0))throw new RangeError('"length" is outside of buffer bounds');let n;return n=void 0===e&&void 0===r?new Uint8Array(t):void 0===r?new Uint8Array(t,e):new Uint8Array(t,e,r),Object.setPrototypeOf(n,f.prototype),n}function l(t){if(t>=i)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+i.toString(16)+" bytes");return 0|t}function p(t,e){if(f.isBuffer(t))return t.length;if(ArrayBuffer.isView(t)||J(t,ArrayBuffer))return t.byteLength;if("string"!=typeof t)throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type '+typeof t);const r=t.length,n=arguments.length>2&&true===arguments[2];if(!n&&0===r)return 0;let i=false;for(;;)switch(e){case "ascii":case "latin1":case "binary":return r;case "utf8":case "utf-8":return q(t).length;case "ucs2":case "ucs-2":case "utf16le":case "utf-16le":return 2*r;case "hex":return r>>>1;case "base64":return Y(t).length;default:if(i)return n?-1:q(t).length;e=(""+e).toLowerCase(),i=true;}}function y(t,e,r){let n=false;if((void 0===e||e<0)&&(e=0),e>this.length)return "";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return "";if((r>>>=0)<=(e>>>=0))return "";for(t||(t="utf8");;)switch(t){case "hex":return T(this,e,r);case "utf8":case "utf-8":return U(this,e,r);case "ascii":return v(this,e,r);case "latin1":case "binary":return L(this,e,r);case "base64":return A(this,e,r);case "ucs2":case "ucs-2":case "utf16le":case "utf-16le":return R(this,e,r);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=true;}}function g(t,e,r){const n=t[e];t[e]=t[r],t[r]=n;}function d(t,e,r,n,i){if(0===t.length)return -1;if("string"==typeof r?(n=r,r=0):r>2147483647?r=2147483647:r<-2147483648&&(r=-2147483648),W(r=+r)&&(r=i?0:t.length-1),r<0&&(r=t.length+r),r>=t.length){if(i)return -1;r=t.length-1;}else if(r<0){if(!i)return -1;r=0;}if("string"==typeof e&&(e=f.from(e,n)),f.isBuffer(e))return 0===e.length?-1:b(t,e,r,n,i);if("number"==typeof e)return e&=255,"function"==typeof Uint8Array.prototype.indexOf?i?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):b(t,[e],r,n,i);throw new TypeError("val must be string, number or Buffer")}function b(t,e,r,n,i){let o,f=1,s=t.length,u=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return -1;f=2,s/=2,u/=2,r/=2;}function h(t,e){return 1===f?t[e]:t.readUInt16BE(e*f)}if(i){let n=-1;for(o=r;o<s;o++)if(h(t,o)===h(e,-1===n?0:o-n)){if(-1===n&&(n=o),o-n+1===u)return n*f}else -1!==n&&(o-=o-n),n=-1;}else for(r+u>s&&(r=s-u),o=r;o>=0;o--){let r=true;for(let n=0;n<u;n++)if(h(t,o+n)!==h(e,n)){r=false;break}if(r)return o}return -1}function w(t,e,r,n){r=Number(r)||0;const i=t.length-r;n?(n=Number(n))>i&&(n=i):n=i;const o=e.length;let f;for(n>o/2&&(n=o/2),f=0;f<n;++f){const n=parseInt(e.substr(2*f,2),16);if(W(n))return f;t[r+f]=n;}return f}function B(t,e,r,n){return G(q(e,t.length-r),t,r,n)}function E(t,e,r,n){return G(function(t){const e=[];for(let r=0;r<t.length;++r)e.push(255&t.charCodeAt(r));return e}(e),t,r,n)}function m(t,e,r,n){return G(Y(e),t,r,n)}function _(t,e,r,n){return G(function(t,e){let r,n,i;const o=[];for(let f=0;f<t.length&&!((e-=2)<0);++f)r=t.charCodeAt(f),n=r>>8,i=r%256,o.push(i),o.push(n);return o}(e,t.length-r),t,r,n)}function A(t,r,n){return 0===r&&n===t.length?e.fromByteArray(t):e.fromByteArray(t.slice(r,n))}function U(t,e,r){r=Math.min(t.length,r);const n=[];let i=e;for(;i<r;){const e=t[i];let o=null,f=e>239?4:e>223?3:e>191?2:1;if(i+f<=r){let r,n,s,u;switch(f){case 1:e<128&&(o=e);break;case 2:r=t[i+1],128==(192&r)&&(u=(31&e)<<6|63&r,u>127&&(o=u));break;case 3:r=t[i+1],n=t[i+2],128==(192&r)&&128==(192&n)&&(u=(15&e)<<12|(63&r)<<6|63&n,u>2047&&(u<55296||u>57343)&&(o=u));break;case 4:r=t[i+1],n=t[i+2],s=t[i+3],128==(192&r)&&128==(192&n)&&128==(192&s)&&(u=(15&e)<<18|(63&r)<<12|(63&n)<<6|63&s,u>65535&&u<1114112&&(o=u));}}null===o?(o=65533,f=1):o>65535&&(o-=65536,n.push(o>>>10&1023|55296),o=56320|1023&o),n.push(o),i+=f;}return function(t){const e=t.length;if(e<=I)return String.fromCharCode.apply(String,t);let r="",n=0;for(;n<e;)r+=String.fromCharCode.apply(String,t.slice(n,n+=I));return r}(n)}t.kMaxLength=i,f.TYPED_ARRAY_SUPPORT=function(){try{const t=new Uint8Array(1),e={foo:function(){return 42}};return Object.setPrototypeOf(e,Uint8Array.prototype),Object.setPrototypeOf(t,e),42===t.foo()}catch(t){return false}}(),f.TYPED_ARRAY_SUPPORT||"undefined"==typeof console||"function"!=typeof console.error||console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."),Object.defineProperty(f.prototype,"parent",{enumerable:true,get:function(){if(f.isBuffer(this))return this.buffer}}),Object.defineProperty(f.prototype,"offset",{enumerable:true,get:function(){if(f.isBuffer(this))return this.byteOffset}}),f.poolSize=8192,f.from=function(t,e,r){return s(t,e,r)},Object.setPrototypeOf(f.prototype,Uint8Array.prototype),Object.setPrototypeOf(f,Uint8Array),f.alloc=function(t,e,r){return function(t,e,r){return u(t),t<=0?o(t):void 0!==e?"string"==typeof r?o(t).fill(e,r):o(t).fill(e):o(t)}(t,e,r)},f.allocUnsafe=function(t){return h(t)},f.allocUnsafeSlow=function(t){return h(t)},f.isBuffer=function(t){return null!=t&&true===t._isBuffer&&t!==f.prototype},f.compare=function(t,e){if(J(t,Uint8Array)&&(t=f.from(t,t.offset,t.byteLength)),J(e,Uint8Array)&&(e=f.from(e,e.offset,e.byteLength)),!f.isBuffer(t)||!f.isBuffer(e))throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');if(t===e)return 0;let r=t.length,n=e.length;for(let i=0,o=Math.min(r,n);i<o;++i)if(t[i]!==e[i]){r=t[i],n=e[i];break}return r<n?-1:n<r?1:0},f.isEncoding=function(t){switch(String(t).toLowerCase()){case "hex":case "utf8":case "utf-8":case "ascii":case "latin1":case "binary":case "base64":case "ucs2":case "ucs-2":case "utf16le":case "utf-16le":return true;default:return false}},f.concat=function(t,e){if(!Array.isArray(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return f.alloc(0);let r;if(void 0===e)for(e=0,r=0;r<t.length;++r)e+=t[r].length;const n=f.allocUnsafe(e);let i=0;for(r=0;r<t.length;++r){let e=t[r];if(J(e,Uint8Array))i+e.length>n.length?(f.isBuffer(e)||(e=f.from(e)),e.copy(n,i)):Uint8Array.prototype.set.call(n,e,i);else {if(!f.isBuffer(e))throw new TypeError('"list" argument must be an Array of Buffers');e.copy(n,i);}i+=e.length;}return n},f.byteLength=p,f.prototype._isBuffer=true,f.prototype.swap16=function(){const t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(let e=0;e<t;e+=2)g(this,e,e+1);return this},f.prototype.swap32=function(){const t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(let e=0;e<t;e+=4)g(this,e,e+3),g(this,e+1,e+2);return this},f.prototype.swap64=function(){const t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(let e=0;e<t;e+=8)g(this,e,e+7),g(this,e+1,e+6),g(this,e+2,e+5),g(this,e+3,e+4);return this},f.prototype.toString=function(){const t=this.length;return 0===t?"":0===arguments.length?U(this,0,t):y.apply(this,arguments)},f.prototype.toLocaleString=f.prototype.toString,f.prototype.equals=function(t){if(!f.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===f.compare(this,t)},f.prototype.inspect=function(){let e="";const r=t.INSPECT_MAX_BYTES;return e=this.toString("hex",0,r).replace(/(.{2})/g,"$1 ").trim(),this.length>r&&(e+=" ... "),"<Buffer "+e+">"},n&&(f.prototype[n]=f.prototype.inspect),f.prototype.compare=function(t,e,r,n,i){if(J(t,Uint8Array)&&(t=f.from(t,t.offset,t.byteLength)),!f.isBuffer(t))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof t);if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===i&&(i=this.length),e<0||r>t.length||n<0||i>this.length)throw new RangeError("out of range index");if(n>=i&&e>=r)return 0;if(n>=i)return -1;if(e>=r)return 1;if(this===t)return 0;let o=(i>>>=0)-(n>>>=0),s=(r>>>=0)-(e>>>=0);const u=Math.min(o,s),h=this.slice(n,i),a=t.slice(e,r);for(let t=0;t<u;++t)if(h[t]!==a[t]){o=h[t],s=a[t];break}return o<s?-1:s<o?1:0},f.prototype.includes=function(t,e,r){return -1!==this.indexOf(t,e,r)},f.prototype.indexOf=function(t,e,r){return d(this,t,e,r,true)},f.prototype.lastIndexOf=function(t,e,r){return d(this,t,e,r,false)},f.prototype.write=function(t,e,r,n){if(void 0===e)n="utf8",r=this.length,e=0;else if(void 0===r&&"string"==typeof e)n=e,r=this.length,e=0;else {if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e>>>=0,isFinite(r)?(r>>>=0,void 0===n&&(n="utf8")):(n=r,r=void 0);}const i=this.length-e;if((void 0===r||r>i)&&(r=i),t.length>0&&(r<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");n||(n="utf8");let o=false;for(;;)switch(n){case "hex":return w(this,t,e,r);case "utf8":case "utf-8":return B(this,t,e,r);case "ascii":case "latin1":case "binary":return E(this,t,e,r);case "base64":return m(this,t,e,r);case "ucs2":case "ucs-2":case "utf16le":case "utf-16le":return _(this,t,e,r);default:if(o)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),o=true;}},f.prototype.toJSON=function(){return {type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};const I=4096;function v(t,e,r){let n="";r=Math.min(t.length,r);for(let i=e;i<r;++i)n+=String.fromCharCode(127&t[i]);return n}function L(t,e,r){let n="";r=Math.min(t.length,r);for(let i=e;i<r;++i)n+=String.fromCharCode(t[i]);return n}function T(t,e,r){const n=t.length;(!e||e<0)&&(e=0),(!r||r<0||r>n)&&(r=n);let i="";for(let n=e;n<r;++n)i+=X[t[n]];return i}function R(t,e,r){const n=t.slice(e,r);let i="";for(let t=0;t<n.length-1;t+=2)i+=String.fromCharCode(n[t]+256*n[t+1]);return i}function S(t,e,r){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(t+e>r)throw new RangeError("Trying to access beyond buffer length")}function N(t,e,r,n,i,o){if(!f.isBuffer(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(e>i||e<o)throw new RangeError('"value" argument is out of bounds');if(r+n>t.length)throw new RangeError("Index out of range")}function O(t,e,r,n,i){F(e,n,i,t,r,7);let o=Number(e&BigInt(4294967295));t[r++]=o,o>>=8,t[r++]=o,o>>=8,t[r++]=o,o>>=8,t[r++]=o;let f=Number(e>>BigInt(32)&BigInt(4294967295));return t[r++]=f,f>>=8,t[r++]=f,f>>=8,t[r++]=f,f>>=8,t[r++]=f,r}function M(t,e,r,n,i){F(e,n,i,t,r,7);let o=Number(e&BigInt(4294967295));t[r+7]=o,o>>=8,t[r+6]=o,o>>=8,t[r+5]=o,o>>=8,t[r+4]=o;let f=Number(e>>BigInt(32)&BigInt(4294967295));return t[r+3]=f,f>>=8,t[r+2]=f,f>>=8,t[r+1]=f,f>>=8,t[r]=f,r+8}function C(t,e,r,n,i,o){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function x(t,e,n,i,o){return e=+e,n>>>=0,o||C(t,0,n,4),r.write(t,e,n,i,23,4),n+4}function k(t,e,n,i,o){return e=+e,n>>>=0,o||C(t,0,n,8),r.write(t,e,n,i,52,8),n+8}f.prototype.slice=function(t,e){const r=this.length;(t=~~t)<0?(t+=r)<0&&(t=0):t>r&&(t=r),(e=void 0===e?r:~~e)<0?(e+=r)<0&&(e=0):e>r&&(e=r),e<t&&(e=t);const n=this.subarray(t,e);return Object.setPrototypeOf(n,f.prototype),n},f.prototype.readUintLE=f.prototype.readUIntLE=function(t,e,r){t>>>=0,e>>>=0,r||S(t,e,this.length);let n=this[t],i=1,o=0;for(;++o<e&&(i*=256);)n+=this[t+o]*i;return n},f.prototype.readUintBE=f.prototype.readUIntBE=function(t,e,r){t>>>=0,e>>>=0,r||S(t,e,this.length);let n=this[t+--e],i=1;for(;e>0&&(i*=256);)n+=this[t+--e]*i;return n},f.prototype.readUint8=f.prototype.readUInt8=function(t,e){return t>>>=0,e||S(t,1,this.length),this[t]},f.prototype.readUint16LE=f.prototype.readUInt16LE=function(t,e){return t>>>=0,e||S(t,2,this.length),this[t]|this[t+1]<<8},f.prototype.readUint16BE=f.prototype.readUInt16BE=function(t,e){return t>>>=0,e||S(t,2,this.length),this[t]<<8|this[t+1]},f.prototype.readUint32LE=f.prototype.readUInt32LE=function(t,e){return t>>>=0,e||S(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},f.prototype.readUint32BE=f.prototype.readUInt32BE=function(t,e){return t>>>=0,e||S(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},f.prototype.readBigUInt64LE=Z(function(t){H(t>>>=0,"offset");const e=this[t],r=this[t+7];void 0!==e&&void 0!==r||z(t,this.length-8);const n=e+256*this[++t]+65536*this[++t]+this[++t]*2**24,i=this[++t]+256*this[++t]+65536*this[++t]+r*2**24;return BigInt(n)+(BigInt(i)<<BigInt(32))}),f.prototype.readBigUInt64BE=Z(function(t){H(t>>>=0,"offset");const e=this[t],r=this[t+7];void 0!==e&&void 0!==r||z(t,this.length-8);const n=e*2**24+65536*this[++t]+256*this[++t]+this[++t],i=this[++t]*2**24+65536*this[++t]+256*this[++t]+r;return (BigInt(n)<<BigInt(32))+BigInt(i)}),f.prototype.readIntLE=function(t,e,r){t>>>=0,e>>>=0,r||S(t,e,this.length);let n=this[t],i=1,o=0;for(;++o<e&&(i*=256);)n+=this[t+o]*i;return i*=128,n>=i&&(n-=Math.pow(2,8*e)),n},f.prototype.readIntBE=function(t,e,r){t>>>=0,e>>>=0,r||S(t,e,this.length);let n=e,i=1,o=this[t+--n];for(;n>0&&(i*=256);)o+=this[t+--n]*i;return i*=128,o>=i&&(o-=Math.pow(2,8*e)),o},f.prototype.readInt8=function(t,e){return t>>>=0,e||S(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},f.prototype.readInt16LE=function(t,e){t>>>=0,e||S(t,2,this.length);const r=this[t]|this[t+1]<<8;return 32768&r?4294901760|r:r},f.prototype.readInt16BE=function(t,e){t>>>=0,e||S(t,2,this.length);const r=this[t+1]|this[t]<<8;return 32768&r?4294901760|r:r},f.prototype.readInt32LE=function(t,e){return t>>>=0,e||S(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},f.prototype.readInt32BE=function(t,e){return t>>>=0,e||S(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},f.prototype.readBigInt64LE=Z(function(t){H(t>>>=0,"offset");const e=this[t],r=this[t+7];void 0!==e&&void 0!==r||z(t,this.length-8);const n=this[t+4]+256*this[t+5]+65536*this[t+6]+(r<<24);return (BigInt(n)<<BigInt(32))+BigInt(e+256*this[++t]+65536*this[++t]+this[++t]*2**24)}),f.prototype.readBigInt64BE=Z(function(t){H(t>>>=0,"offset");const e=this[t],r=this[t+7];void 0!==e&&void 0!==r||z(t,this.length-8);const n=(e<<24)+65536*this[++t]+256*this[++t]+this[++t];return (BigInt(n)<<BigInt(32))+BigInt(this[++t]*2**24+65536*this[++t]+256*this[++t]+r)}),f.prototype.readFloatLE=function(t,e){return t>>>=0,e||S(t,4,this.length),r.read(this,t,true,23,4)},f.prototype.readFloatBE=function(t,e){return t>>>=0,e||S(t,4,this.length),r.read(this,t,false,23,4)},f.prototype.readDoubleLE=function(t,e){return t>>>=0,e||S(t,8,this.length),r.read(this,t,true,52,8)},f.prototype.readDoubleBE=function(t,e){return t>>>=0,e||S(t,8,this.length),r.read(this,t,false,52,8)},f.prototype.writeUintLE=f.prototype.writeUIntLE=function(t,e,r,n){t=+t,e>>>=0,r>>>=0,n||N(this,t,e,r,Math.pow(2,8*r)-1,0);let i=1,o=0;for(this[e]=255&t;++o<r&&(i*=256);)this[e+o]=t/i&255;return e+r},f.prototype.writeUintBE=f.prototype.writeUIntBE=function(t,e,r,n){t=+t,e>>>=0,r>>>=0,n||N(this,t,e,r,Math.pow(2,8*r)-1,0);let i=r-1,o=1;for(this[e+i]=255&t;--i>=0&&(o*=256);)this[e+i]=t/o&255;return e+r},f.prototype.writeUint8=f.prototype.writeUInt8=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,1,255,0),this[e]=255&t,e+1},f.prototype.writeUint16LE=f.prototype.writeUInt16LE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,2,65535,0),this[e]=255&t,this[e+1]=t>>>8,e+2},f.prototype.writeUint16BE=f.prototype.writeUInt16BE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,2,65535,0),this[e]=t>>>8,this[e+1]=255&t,e+2},f.prototype.writeUint32LE=f.prototype.writeUInt32LE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,4,4294967295,0),this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t,e+4},f.prototype.writeUint32BE=f.prototype.writeUInt32BE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,4,4294967295,0),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},f.prototype.writeBigUInt64LE=Z(function(t,e=0){return O(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))}),f.prototype.writeBigUInt64BE=Z(function(t,e=0){return M(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))}),f.prototype.writeIntLE=function(t,e,r,n){if(t=+t,e>>>=0,!n){const n=Math.pow(2,8*r-1);N(this,t,e,r,n-1,-n);}let i=0,o=1,f=0;for(this[e]=255&t;++i<r&&(o*=256);)t<0&&0===f&&0!==this[e+i-1]&&(f=1),this[e+i]=(t/o|0)-f&255;return e+r},f.prototype.writeIntBE=function(t,e,r,n){if(t=+t,e>>>=0,!n){const n=Math.pow(2,8*r-1);N(this,t,e,r,n-1,-n);}let i=r-1,o=1,f=0;for(this[e+i]=255&t;--i>=0&&(o*=256);)t<0&&0===f&&0!==this[e+i+1]&&(f=1),this[e+i]=(t/o|0)-f&255;return e+r},f.prototype.writeInt8=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,1,127,-128),t<0&&(t=255+t+1),this[e]=255&t,e+1},f.prototype.writeInt16LE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,2,32767,-32768),this[e]=255&t,this[e+1]=t>>>8,e+2},f.prototype.writeInt16BE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,2,32767,-32768),this[e]=t>>>8,this[e+1]=255&t,e+2},f.prototype.writeInt32LE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,4,2147483647,-2147483648),this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24,e+4},f.prototype.writeInt32BE=function(t,e,r){return t=+t,e>>>=0,r||N(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},f.prototype.writeBigInt64LE=Z(function(t,e=0){return O(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))}),f.prototype.writeBigInt64BE=Z(function(t,e=0){return M(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))}),f.prototype.writeFloatLE=function(t,e,r){return x(this,t,e,true,r)},f.prototype.writeFloatBE=function(t,e,r){return x(this,t,e,false,r)},f.prototype.writeDoubleLE=function(t,e,r){return k(this,t,e,true,r)},f.prototype.writeDoubleBE=function(t,e,r){return k(this,t,e,false,r)},f.prototype.copy=function(t,e,r,n){if(!f.isBuffer(t))throw new TypeError("argument should be a Buffer");if(r||(r=0),n||0===n||(n=this.length),e>=t.length&&(e=t.length),e||(e=0),n>0&&n<r&&(n=r),n===r)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e<n-r&&(n=t.length-e+r);const i=n-r;return this===t&&"function"==typeof Uint8Array.prototype.copyWithin?this.copyWithin(e,r,n):Uint8Array.prototype.set.call(t,this.subarray(r,n),e),i},f.prototype.fill=function(t,e,r,n){if("string"==typeof t){if("string"==typeof e?(n=e,e=0,r=this.length):"string"==typeof r&&(n=r,r=this.length),void 0!==n&&"string"!=typeof n)throw new TypeError("encoding must be a string");if("string"==typeof n&&!f.isEncoding(n))throw new TypeError("Unknown encoding: "+n);if(1===t.length){const e=t.charCodeAt(0);("utf8"===n&&e<128||"latin1"===n)&&(t=e);}}else "number"==typeof t?t&=255:"boolean"==typeof t&&(t=Number(t));if(e<0||this.length<e||this.length<r)throw new RangeError("Out of range index");if(r<=e)return this;let i;if(e>>>=0,r=void 0===r?this.length:r>>>0,t||(t=0),"number"==typeof t)for(i=e;i<r;++i)this[i]=t;else {const o=f.isBuffer(t)?t:f.from(t,n),s=o.length;if(0===s)throw new TypeError('The value "'+t+'" is invalid for argument "value"');for(i=0;i<r-e;++i)this[i+e]=o[i%s];}return this};const P={};function $(t,e,r){P[t]=class extends r{constructor(){super(),Object.defineProperty(this,"message",{value:e.apply(this,arguments),writable:true,configurable:true}),this.name=`${this.name} [${t}]`,this.stack,delete this.name;}get code(){return t}set code(t){Object.defineProperty(this,"code",{configurable:true,enumerable:true,value:t,writable:true});}toString(){return `${this.name} [${t}]: ${this.message}`}};}function j(t){let e="",r=t.length;const n="-"===t[0]?1:0;for(;r>=n+4;r-=3)e=`_${t.slice(r-3,r)}${e}`;return `${t.slice(0,r)}${e}`}function F(t,e,r,n,i,o){if(t>r||t<e){const r="bigint"==typeof e?"n":"";let n;throw n=0===e||e===BigInt(0)?`>= 0${r} and < 2${r} ** ${8*(o+1)}${r}`:`>= -(2${r} ** ${8*(o+1)-1}${r}) and < 2 ** ${8*(o+1)-1}${r}`,new P.ERR_OUT_OF_RANGE("value",n,t)}!function(t,e,r){H(e,"offset"),void 0!==t[e]&&void 0!==t[e+r]||z(e,t.length-(r+1));}(n,i,o);}function H(t,e){if("number"!=typeof t)throw new P.ERR_INVALID_ARG_TYPE(e,"number",t)}function z(t,e,r){if(Math.floor(t)!==t)throw H(t,r),new P.ERR_OUT_OF_RANGE("offset","an integer",t);if(e<0)throw new P.ERR_BUFFER_OUT_OF_BOUNDS;throw new P.ERR_OUT_OF_RANGE("offset",`>= 0 and <= ${e}`,t)}$("ERR_BUFFER_OUT_OF_BOUNDS",function(t){return t?`${t} is outside of buffer bounds`:"Attempt to access memory outside buffer bounds"},RangeError),$("ERR_INVALID_ARG_TYPE",function(t,e){return `The "${t}" argument must be of type number. Received type ${typeof e}`},TypeError),$("ERR_OUT_OF_RANGE",function(t,e,r){let n=`The value of "${t}" is out of range.`,i=r;return Number.isInteger(r)&&Math.abs(r)>2**32?i=j(String(r)):"bigint"==typeof r&&(i=String(r),(r>BigInt(2)**BigInt(32)||r<-(BigInt(2)**BigInt(32)))&&(i=j(i)),i+="n"),n+=` It must be ${e}. Received ${i}`,n},RangeError);const D=/[^+/0-9A-Za-z-_]/g;function q(t,e){let r;e=e||1/0;const n=t.length;let i=null;const o=[];for(let f=0;f<n;++f){if(r=t.charCodeAt(f),r>55295&&r<57344){if(!i){if(r>56319){(e-=3)>-1&&o.push(239,191,189);continue}if(f+1===n){(e-=3)>-1&&o.push(239,191,189);continue}i=r;continue}if(r<56320){(e-=3)>-1&&o.push(239,191,189),i=r;continue}r=65536+(i-55296<<10|r-56320);}else i&&(e-=3)>-1&&o.push(239,191,189);if(i=null,r<128){if((e-=1)<0)break;o.push(r);}else if(r<2048){if((e-=2)<0)break;o.push(r>>6|192,63&r|128);}else if(r<65536){if((e-=3)<0)break;o.push(r>>12|224,r>>6&63|128,63&r|128);}else {if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;o.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128);}}return o}function Y(t){return e.toByteArray(function(t){if((t=(t=t.split("=")[0]).trim().replace(D,"")).length<2)return "";for(;t.length%4!=0;)t+="=";return t}(t))}function G(t,e,r,n){let i;for(i=0;i<n&&!(i+r>=e.length||i>=t.length);++i)e[i+r]=t[i];return i}function J(t,e){return t instanceof e||null!=t&&null!=t.constructor&&null!=t.constructor.name&&t.constructor.name===e.name}function W(t){return t!=t}const X=function(){const t="0123456789abcdef",e=new Array(256);for(let r=0;r<16;++r){const n=16*r;for(let i=0;i<16;++i)e[n+i]=t[r]+t[i];}return e}();function Z(t){return "undefined"==typeof BigInt?tt:t}function tt(){throw new Error("BigInt not supported")}}(D)),D);let W={SERVER_TIME_NONCE:176,AUTH_REQ:177,AUTH_RES:178,AUTH_FAIL:179,ENC_PACK:182,ENC_E2E:183,ENC_488:184};for(let t in W)W[W[t]]=t;const X={SERVER_TIME_NONCE:H.meta(H.MB("header","8",0),H.MB("unixTime","32L",0),H.MB("milTime","16L",0),H.MB("counter","16L",0),H.MB("nonce",J.Buffer.alloc(4))),AUTH_REQ:H.meta(H.MB("header","8",0),H.MB("id8",J.Buffer.alloc(8)),H.MB("nonce",J.Buffer.alloc(4)),H.MB("hmac32",J.Buffer.alloc(32))),AUTH_RES:H.meta(H.MB("header","8",0),H.MB("hmac32",J.Buffer.alloc(32))),ENC_PACK:H.meta(H.MB("type","8",0),H.MB("len","32L",0),H.MB("salt12",J.Buffer.alloc(12)),H.MB("hmac",8,0)),ENC_488:H.meta(H.MB("type","8",0),H.MB("len","32L",0),H.MB("otpSrc8",J.Buffer.alloc(8)),H.MB("hmac8",J.Buffer.alloc(8)))};function Z(t){let e=t[t.length-1];return e[2]+e[3]}const tt={SERVER_TIME_NONCE:Z(X.SERVER_TIME_NONCE),AUTH_REQ:Z(X.AUTH_REQ),AUTH_RES:Z(X.AUTH_RES),ENC_PACK:Z(X.ENC_PACK),ENC_488:Z(X.ENC_488)};function et(t,e){if(!(t instanceof Uint8Array&&e instanceof Uint8Array&&t.length===e.length))return false;let r=0;for(let n=0;n<t.length;n++)r|=t[n]^e[n];return 0===r}function rt(t,e,r,n,i=false){if(!(t instanceof Uint8Array))return;const o=J.Buffer.from(t);return o.length<r||o[0]!==e||(i?o.readUInt32LE(1)!==o.length-r:o.length!==r)?void 0:H.unpack(o,n)}function nt(t){return globalThis.crypto.getRandomValues(J.Buffer.alloc(t))}class it{constructor(){this._id8=J.Buffer.alloc(8),this._otpSrc44=J.Buffer.alloc(44),this._otp36=J.Buffer.alloc(36),this._hmac=J.Buffer.alloc(32),this.auth_salt12=J.Buffer.alloc(12),this.localNonce=J.Buffer.alloc(4),this.remoteNonce=J.Buffer.alloc(4),this.isAuthorized=false,this.counter=0,this._hasKey=false,this._lastSendTime=0;}clearAuth(){this._id8.fill(0),this._otpSrc44.fill(0),this._otp36.fill(0),this._hmac.fill(0),this.auth_salt12.fill(0),this.localNonce.fill(0),this.remoteNonce.fill(0),this.isAuthorized=false,this.counter=0,this._hasKey=false,this._lastSendTime=0;}set_hash_id8(t){H.B8(z.hash(t)).copy(this._id8,0,0,8);}set_id8(t){let e=H.B8(t);this._id8.fill(0),e.copy(this._id8,0,0,8);}set_key(t){const e=H.B8(t);if(!e||0===e.length)throw new TypeError("Key must not be empty");this.copy_key(H.B8(z.hash(e)));}set_id_key(t){let e=t.indexOf(".");if(-1==e)return;let r=t.substring(0,e),n=t.substring(e+1);this.set_id8(r),this.set_key(n);}copy_id8(t){t.copy(this._id8,0,0,8);}copy_key(t){if(!(t instanceof Uint8Array)||32!==t.length)throw new TypeError("copy_key requires exactly 32 bytes");this._otpSrc44.set(t,0),this._hasKey=true;}sha256_n(t,e){let r=z.hash(t);for(let t=0;t<e;t++)r=z.hash(r);return r}set_clock_rand(){this.set_clock_nonce(nt(4));}set_clock_nonce(t){if(!(t instanceof Uint8Array)||4!==t.length)throw new TypeError("Nonce must be 4 bytes");let e=Math.max(Date.now(),this._lastSendTime);if(this.counter=this.counter+1&65535,0===this.counter&&e===this._lastSendTime&&e++,!Number.isSafeInteger(e)||e<0||Math.floor(e/1e3)>4294967295)throw new RangeError("Clock is outside the protocol range");this._lastSendTime=e;J.Buffer.concat([H.NB("32L",Math.floor(e/1e3)),H.NB("16L",e%1e3),H.NB("16L",this.counter),J.Buffer.from(t)]).copy(this._otpSrc44,32);}_requireKey(){if(!this._hasKey)throw new Error("Set a key before encryption or authentication")}set_salt12(t,e){if(12!=t.byteLength)throw TypeError("set_salt12: Invalid salt12 byteLength.");t.copy(this._otpSrc44,32);}resetOTP(){this._requireKey(),H.B8(z.hash(this._otpSrc44)).copy(this._otp36,0,0,32);}getIndexOTP(t){return this._otp36.writeUInt32LE(t,32),z.hash(this._otp36)}generateHMAC(t){this._requireKey();let e=J.Buffer.concat([this._otpSrc44,t]);this._hmac=H.B8(z.hash(e));}getHMAC8(t){this._requireKey();let e=J.Buffer.concat([this._otpSrc44,t]);return this._hmac=H.B8(z.hash(e)),this._hmac.subarray(0,8)}xotp(t,e=0,r=false){let n=(t=H.B8(t,r)).byteLength,i=e,o=0,f=0;for(;n>0;){f=n<32?n:32;let e=this.getIndexOTP(++i);for(let r=0;r<f;r++)t[o++]^=e[r];n-=32;}return t}server_time_nonce(){const t=Date.now(),e=Math.floor(t/1e3),r=t%1e3;return this.localNonce=nt(4),this.auth_salt12=J.Buffer.concat([H.NB("32L",e),H.NB("16L",r),nt(2),this.localNonce]),J.Buffer.concat([H.NB("8",W.SERVER_TIME_NONCE),this.auth_salt12])}auth_req(t){if(!this._hasKey)return false;let e=rt(t,W.SERVER_TIME_NONCE,tt.SERVER_TIME_NONCE,X.SERVER_TIME_NONCE);if(e&&e.milTime>=1e3)return false;if(e){let t=J.Buffer.concat([H.NB("32L",e.unixTime),H.NB("16L",e.milTime),H.NB("16L",e.counter),e.nonce]);return this.set_salt12(t,"auth_req"),this.localNonce=nt(4),this.generateHMAC(this.localNonce),this.remoteNonce=e.nonce,H.pack(H.MB("#header","8",W.AUTH_REQ),H.MB("#id8",this._id8),H.MB("#nonce",this.localNonce),H.MB("#hmac32",this._hmac))}return false}verify_auth_req(t){if(!this._hasKey)return false;let e;if(t instanceof Uint8Array?e=rt(t,W.AUTH_REQ,tt.AUTH_REQ,X.AUTH_REQ):t&&"object"==typeof t&&(e=t),!e||e.header!==W.AUTH_REQ||!(e.id8 instanceof Uint8Array)||8!==e.id8.length||!(e.nonce instanceof Uint8Array)||4!==e.nonce.length||!(e.hmac32 instanceof Uint8Array)||32!==e.hmac32.length||e.$OTHERS&&0!==e.$OTHERS.length)return false;this.set_salt12(this.auth_salt12,"verify_auth_req 1/2"),this.generateHMAC(e.nonce);let r=this._hmac;if(et(e.hmac32,r)){this.remoteNonce=J.Buffer.from(e.nonce);let t=J.Buffer.concat([this.localNonce,this.remoteNonce,this.localNonce]);this.set_salt12(t,"verify_auth_req 2/2"),this.generateHMAC(e.nonce);let r=this._hmac,n=H.rawPack(H.MB("header","8",W.AUTH_RES),H.MB("hmac32",r));return this.isAuthorized=true,n}return false}verify_auth_res(t){if(!this._hasKey)return false;let e=rt(t,W.AUTH_RES,tt.AUTH_RES,X.AUTH_RES);if(e){let t=J.Buffer.concat([this.remoteNonce,this.localNonce,this.remoteNonce]);if(this.set_salt12(t,"verify_auth_res"),this.generateHMAC(this.localNonce),et(this._hmac,e.hmac32))return this.isAuthorized=true,true}}encrypt_488(t){if(!this.isAuthorized||!this._hasKey)return;t=H.B8(t),this.set_clock_nonce(this.remoteNonce),this.resetOTP();let e=this.getHMAC8(t),r=this.xotp(t);return H.pack(H.MB("#type","8",W.ENC_488),H.MB("#len","32L",t.byteLength),H.MB("#otpSrc8",this._otpSrc44.subarray(32,40)),H.MB("#hmac8",e),H.MB("#xdata",r))}decrypt_488(t){if(!this.isAuthorized||!this._hasKey)return;let e=t;if(t instanceof Uint8Array&&t[0]===W.ENC_E2E){if(t.length<tt.ENC_488)return;const r=J.Buffer.from(t),n=r.readUInt32LE(1);if(n>r.length-tt.ENC_488)return;e=J.Buffer.from(r.subarray(0,tt.ENC_488+n)),e[0]=W.ENC_488;}const r=rt(e,W.ENC_488,tt.ENC_488,X.ENC_488,true);if(!(r&&(n=r.otpSrc8,n.readUInt16LE(4)<1e3)))return;var n;this.set_salt12(J.Buffer.concat([r.otpSrc8,this.localNonce]),"decrypt_488"),this.resetOTP();const i=this.xotp(r.$OTHERS??J.Buffer.alloc(0));return et(this.getHMAC8(i),r.hmac8)?i:void 0}encryptPack(t){this._requireKey(),t=H.B8(t),this.set_clock_rand(),this.resetOTP();let e=this.getHMAC8(t),r=this.xotp(t);return H.pack(H.MB("#type","8",W.ENC_PACK),H.MB("#len","32L",t.byteLength),H.MB("#salt12",this._otpSrc44.subarray(32)),H.MB("#hmac8",e),H.MB("#xdata",r))}decryptPack(t){if(!this._hasKey)return;const e=rt(t,W.ENC_PACK,tt.ENC_PACK,X.ENC_PACK,true);if(!e)return;this.set_salt12(e.salt12,"decryptPack"),this.resetOTP();const r=this.xotp(e.$OTHERS??J.Buffer.alloc(0));return et(e.hmac,this.getHMAC8(r))?(e.data=r,e):void 0}encrypt_e2e(t,e){return this._withKey(e,()=>this.encryptPack(t))}decrypt_e2e(t,e){return this._withKey(e,()=>this.decryptPack(t))}_withKey(t,e){const r=H.B8(t);if(!r||0===r.length)throw new TypeError("Key must not be empty");const n=[J.Buffer.from(this._otpSrc44),J.Buffer.from(this._otp36),J.Buffer.from(this._hmac)],i=this._hasKey;try{return this._otpSrc44.set(z.hash(r),0),this._hasKey=!0,e()}finally{this._otpSrc44.set(n[0]),this._otp36.set(n[1]),this._hmac.set(n[2]),this._hasKey=i;for(const t of n)t.fill(0);}}}it.RAND=nt,it.BohoMsg=W,it.Meta=X,it.MetaSize=tt,it.sha256=z,it.MBP=H,it.Buffer=J.Buffer;
|
|
762
762
|
|
|
763
763
|
/**
|
|
764
|
-
* @typedef {import('meta-buffer-pack').MBP} MBP
|
|
765
764
|
* @typedef {import('../common/constants.js').IOMsg} IOMsg
|
|
766
765
|
* @typedef {import('../common/constants.js').PAYLOAD_TYPE} PAYLOAD_TYPE
|
|
767
766
|
* @typedef {import('../common/constants.js').SIZE_LIMIT} SIZE_LIMIT
|
|
@@ -870,7 +869,7 @@ class IOCore extends EventEmitter {
|
|
|
870
869
|
this.connectionCheckerPeriod = SIZE_LIMIT.CONNECTION_CHECKER_PERIOD;
|
|
871
870
|
/**
|
|
872
871
|
* Interval ID for connection checker.
|
|
873
|
-
* @type {
|
|
872
|
+
* @type {ReturnType<typeof setInterval> | null}
|
|
874
873
|
*/
|
|
875
874
|
this.connectionCheckerIntervalID = null;
|
|
876
875
|
|
|
@@ -880,6 +879,7 @@ class IOCore extends EventEmitter {
|
|
|
880
879
|
*/
|
|
881
880
|
this.boho = new it();
|
|
882
881
|
|
|
882
|
+
/** @type {import('buffer').Buffer} */
|
|
883
883
|
this.serverTimeNonce = Buffer$1.alloc( it.MetaSize.SERVER_TIME_NONCE );
|
|
884
884
|
/**
|
|
885
885
|
* Indicates if the connection is TLS (wss).
|
|
@@ -966,7 +966,7 @@ class IOCore extends EventEmitter {
|
|
|
966
966
|
|
|
967
967
|
|
|
968
968
|
/**
|
|
969
|
-
* Performs common cleanup for the connection. It
|
|
969
|
+
* Performs common cleanup for the connection. It rejects pending RPCs,
|
|
970
970
|
* resets the socket reference, and sets the state to closed.
|
|
971
971
|
* This method is guarded to only run once.
|
|
972
972
|
* If autoReconnect is false, it also clears the keep-alive timer.
|
|
@@ -1001,6 +1001,12 @@ class IOCore extends EventEmitter {
|
|
|
1001
1001
|
}
|
|
1002
1002
|
this.socket = null;
|
|
1003
1003
|
}
|
|
1004
|
+
const error = new Error('Connection closed');
|
|
1005
|
+
error.code = 'CONNECTION_CLOSED';
|
|
1006
|
+
for (const [, reject, timeoutId] of this.promiseMap.values()) {
|
|
1007
|
+
clearTimeout(timeoutId);
|
|
1008
|
+
reject(error);
|
|
1009
|
+
}
|
|
1004
1010
|
this.promiseMap.clear();
|
|
1005
1011
|
this.cid = '';
|
|
1006
1012
|
if (this.boho) this.boho.isAuthorized = false;
|
|
@@ -1694,11 +1700,12 @@ class IOCore extends EventEmitter {
|
|
|
1694
1700
|
call(target, topic, ...args) {
|
|
1695
1701
|
if (!target || !topic)
|
|
1696
1702
|
return Promise.reject(new Error('request need target and topic)'))
|
|
1703
|
+
this.mid = (this.mid + 1) & 0xffff;
|
|
1697
1704
|
let sigPack;
|
|
1698
1705
|
if (args.length > 0) {
|
|
1699
1706
|
sigPack = $$1.pack(
|
|
1700
1707
|
$$1.MB('#MsgType', '8', IOMsg.CALL),
|
|
1701
|
-
$$1.MB('mid', '16',
|
|
1708
|
+
$$1.MB('mid', '16', this.mid),
|
|
1702
1709
|
$$1.MB('target', target),
|
|
1703
1710
|
$$1.MB('topic', topic),
|
|
1704
1711
|
$$1.MBA(...args)
|
|
@@ -1706,7 +1713,7 @@ class IOCore extends EventEmitter {
|
|
|
1706
1713
|
} else {
|
|
1707
1714
|
sigPack = $$1.pack(
|
|
1708
1715
|
$$1.MB('#MsgType', '8', IOMsg.CALL),
|
|
1709
|
-
$$1.MB('mid', '16',
|
|
1716
|
+
$$1.MB('mid', '16', this.mid),
|
|
1710
1717
|
$$1.MB('target', target),
|
|
1711
1718
|
$$1.MB('topic', topic)
|
|
1712
1719
|
);
|
|
@@ -2016,6 +2023,9 @@ function pack(payload) {
|
|
|
2016
2023
|
}
|
|
2017
2024
|
|
|
2018
2025
|
class CongRx extends require$$0.Transform {
|
|
2026
|
+
/**
|
|
2027
|
+
* @param {import('stream').TransformOptions} [options] Stream options.
|
|
2028
|
+
*/
|
|
2019
2029
|
constructor(options) {
|
|
2020
2030
|
super(options);
|
|
2021
2031
|
this.buffer = Buffer.alloc(0);
|
|
@@ -2058,54 +2068,45 @@ class CongRx extends require$$0.Transform {
|
|
|
2058
2068
|
|
|
2059
2069
|
|
|
2060
2070
|
parse() {
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
this.buffer = Buffer.alloc(0);
|
|
2088
|
-
}
|
|
2071
|
+
while (this.buffer.byteLength > 0) {
|
|
2072
|
+
const head = this.buffer[0];
|
|
2073
|
+
let headerLen;
|
|
2074
|
+
let payloadSize;
|
|
2075
|
+
|
|
2076
|
+
if (head === CongType.TYPE_LEN1) {
|
|
2077
|
+
headerLen = 2;
|
|
2078
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2079
|
+
payloadSize = this.buffer.readUint8(1);
|
|
2080
|
+
} else if (head === CongType.TYPE_LEN2) {
|
|
2081
|
+
headerLen = 3;
|
|
2082
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2083
|
+
payloadSize = this.buffer.readUint16LE(1);
|
|
2084
|
+
} else if (head === CongType.TYPE_LEN3) {
|
|
2085
|
+
headerLen = 4;
|
|
2086
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2087
|
+
payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536;
|
|
2088
|
+
} else if (head === CongType.TYPE_LEN4) {
|
|
2089
|
+
headerLen = 5;
|
|
2090
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2091
|
+
payloadSize = this.buffer.readUint32LE(1);
|
|
2092
|
+
} else {
|
|
2093
|
+
this.emit('wrong', this.buffer);
|
|
2094
|
+
this.buffer = Buffer.alloc(0);
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
2089
2097
|
|
|
2098
|
+
const frameSize = headerLen + payloadSize;
|
|
2099
|
+
if (this.buffer.byteLength < frameSize) return;
|
|
2090
2100
|
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
this.buffer = this.buffer.subarray(
|
|
2098
|
-
this.parse();
|
|
2099
|
-
} else {
|
|
2100
|
-
// not ready
|
|
2101
|
-
// console.log('+')
|
|
2102
|
-
return
|
|
2101
|
+
this.frames.push(this.buffer.subarray(headerLen, frameSize));
|
|
2102
|
+
if (this.buffer.byteLength === frameSize) {
|
|
2103
|
+
this.buffer = Buffer.alloc(0);
|
|
2104
|
+
return;
|
|
2105
|
+
}
|
|
2106
|
+
// Consume each complete frame without growing the call stack.
|
|
2107
|
+
this.buffer = this.buffer.subarray(frameSize);
|
|
2103
2108
|
}
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
2109
|
}
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
2110
|
}
|
|
2110
2111
|
|
|
2111
2112
|
class IOCongSocket extends IOCore {
|
|
@@ -7333,6 +7334,7 @@ let serverOption = {
|
|
|
7333
7334
|
httpServer: null,
|
|
7334
7335
|
wsPath: null,
|
|
7335
7336
|
timeout: 50000,
|
|
7337
|
+
pingTimeoutGrace: 5000,
|
|
7336
7338
|
showMessage: 'none',
|
|
7337
7339
|
showMetric: 0,
|
|
7338
7340
|
showChannel: 0,
|
|
@@ -7484,6 +7486,7 @@ class RemoteCore {
|
|
|
7484
7486
|
rxQuotaChecker(message) {
|
|
7485
7487
|
let rxBytes = message.byteLength;
|
|
7486
7488
|
this.manager.rxBytes += rxBytes;
|
|
7489
|
+
if (this.manager.telemetry) { this.manager.telemetry.rxBytes += rxBytes; this.manager.telemetry.rxMessages++; }
|
|
7487
7490
|
|
|
7488
7491
|
if (serverOption.useQuota.signalSize && (rxBytes > this.quota.signalSize)) {
|
|
7489
7492
|
console.log('## quota: size over');
|
|
@@ -7955,6 +7958,10 @@ const decoder$2 = new TextDecoder();
|
|
|
7955
7958
|
class Remote extends RemoteCore {
|
|
7956
7959
|
constructor(socket, req, manager) {
|
|
7957
7960
|
super(socket, manager);
|
|
7961
|
+
this.txQueuePeakBytes = 0;
|
|
7962
|
+
this.txWriteFalseCount = 0;
|
|
7963
|
+
this.txBlockedSince = null;
|
|
7964
|
+
this.txDrainMaxMs = 0;
|
|
7958
7965
|
this.socketType = socket.socketType;
|
|
7959
7966
|
|
|
7960
7967
|
if (this.socketType === 'websocket') {
|
|
@@ -7991,6 +7998,11 @@ class Remote extends RemoteCore {
|
|
|
7991
7998
|
this.manager.removeRemote(this);
|
|
7992
7999
|
};
|
|
7993
8000
|
} else { // TCP else
|
|
8001
|
+
socket.on('drain', () => {
|
|
8002
|
+
if (this.txBlockedSince === null) return;
|
|
8003
|
+
this.txDrainMaxMs = Math.max(this.txDrainMaxMs, node_perf_hooks.performance.now() - this.txBlockedSince);
|
|
8004
|
+
this.txBlockedSince = null;
|
|
8005
|
+
});
|
|
7994
8006
|
this.congRx = new CongRx();
|
|
7995
8007
|
socket.on('data', data => { this.congRx.write(data); });
|
|
7996
8008
|
|
|
@@ -8059,6 +8071,7 @@ class Remote extends RemoteCore {
|
|
|
8059
8071
|
// any type of received messages: message, ping, pong
|
|
8060
8072
|
receiveMonitor() {
|
|
8061
8073
|
this.socket.rxCounter++;
|
|
8074
|
+
if (this.socket.isAlive === false) this.manager.heartbeatPending.delete(this);
|
|
8062
8075
|
this.socket.isAlive = true;
|
|
8063
8076
|
}
|
|
8064
8077
|
|
|
@@ -8122,10 +8135,16 @@ class Remote extends RemoteCore {
|
|
|
8122
8135
|
}
|
|
8123
8136
|
}
|
|
8124
8137
|
|
|
8138
|
+
recordTxQueue() {
|
|
8139
|
+
const queued = this.socket.bufferedAmount ?? this.socket.writableLength ?? 0;
|
|
8140
|
+
if (queued > this.txQueuePeakBytes) this.txQueuePeakBytes = queued;
|
|
8141
|
+
}
|
|
8142
|
+
|
|
8125
8143
|
// node.js server side remote.
|
|
8126
8144
|
send(message, isBinary) {
|
|
8127
8145
|
// console.log(`<-S [${IOMsg[ message[0]]}]`)
|
|
8128
8146
|
this.manager.txBytes += message.byteLength;
|
|
8147
|
+
if (this.manager.telemetry) { this.manager.telemetry.txBytes += message.byteLength; this.manager.telemetry.txMessages++; }
|
|
8129
8148
|
this.socket.txCounter++;
|
|
8130
8149
|
if (this.socketType === 'websocket') {
|
|
8131
8150
|
if (this.socket.readyState === 1) {
|
|
@@ -8134,6 +8153,7 @@ class Remote extends RemoteCore {
|
|
|
8134
8153
|
} else {
|
|
8135
8154
|
this.socket.send(message);
|
|
8136
8155
|
}
|
|
8156
|
+
this.recordTxQueue();
|
|
8137
8157
|
} else {
|
|
8138
8158
|
console.log('server(ws)Remote.send() called. not open state. current readyState:', this.socket.readyState );
|
|
8139
8159
|
this.close(true);
|
|
@@ -8141,7 +8161,12 @@ class Remote extends RemoteCore {
|
|
|
8141
8161
|
|
|
8142
8162
|
} else { //CongSocket
|
|
8143
8163
|
if (this.socket.readyState == 'open') {
|
|
8144
|
-
this.socket.write(pack(message));
|
|
8164
|
+
const writable = this.socket.write(pack(message));
|
|
8165
|
+
this.recordTxQueue();
|
|
8166
|
+
if (!writable) {
|
|
8167
|
+
this.txWriteFalseCount++;
|
|
8168
|
+
if (this.txBlockedSince === null) this.txBlockedSince = node_perf_hooks.performance.now();
|
|
8169
|
+
}
|
|
8145
8170
|
} else {
|
|
8146
8171
|
console.log('server(cong)Remote.send() called. not open state. current readyState:', this.socket.readyState );
|
|
8147
8172
|
this.close(true);
|
|
@@ -8345,6 +8370,12 @@ class Manager {
|
|
|
8345
8370
|
|
|
8346
8371
|
constructor(server, authManager) {
|
|
8347
8372
|
this.server = server;
|
|
8373
|
+
this.pingPeriod = serverOption.timeout;
|
|
8374
|
+
this.pingGrace = serverOption.pingTimeoutGrace;
|
|
8375
|
+
if (!Number.isFinite(this.pingPeriod) || this.pingPeriod <= 0 ||
|
|
8376
|
+
!Number.isFinite(this.pingGrace) || this.pingGrace < 0) {
|
|
8377
|
+
throw new RangeError('Invalid heartbeat period or grace');
|
|
8378
|
+
}
|
|
8348
8379
|
this.txBytes = 0;
|
|
8349
8380
|
this.rxBytes = 0;
|
|
8350
8381
|
|
|
@@ -8371,25 +8402,20 @@ class Manager {
|
|
|
8371
8402
|
this.metrics = new Metrics(this);
|
|
8372
8403
|
this.lastSSID = 0;
|
|
8373
8404
|
|
|
8374
|
-
|
|
8375
|
-
|
|
8376
|
-
|
|
8377
|
-
|
|
8378
|
-
|
|
8379
|
-
|
|
8380
|
-
|
|
8381
|
-
|
|
8382
|
-
|
|
8383
|
-
|
|
8384
|
-
|
|
8385
|
-
|
|
8386
|
-
|
|
8387
|
-
|
|
8388
|
-
remo.ping();
|
|
8389
|
-
}
|
|
8390
|
-
|
|
8391
|
-
});
|
|
8392
|
-
}, serverOption.timeout);
|
|
8405
|
+
const groups = Math.min(50, Math.max(1, Math.floor(this.pingPeriod / 1000)));
|
|
8406
|
+
this.pingGroups = Array.from({ length: groups }, () => new Set());
|
|
8407
|
+
this.nextPingGroup = 0;
|
|
8408
|
+
this.pingGroupCursor = 0;
|
|
8409
|
+
// Same timeout for all entries: insertion order is also deadline order.
|
|
8410
|
+
this.heartbeatPending = new Map();
|
|
8411
|
+
this.pingTickMs = this.pingPeriod / groups;
|
|
8412
|
+
this.heartbeatStopped = false;
|
|
8413
|
+
const tick = () => {
|
|
8414
|
+
if (this.heartbeatStopped) return;
|
|
8415
|
+
this.heartbeatTick();
|
|
8416
|
+
if (!this.heartbeatStopped) this.pingIntervalID = setTimeout(tick, this.pingTickMs);
|
|
8417
|
+
};
|
|
8418
|
+
this.pingIntervalID = setTimeout(tick, this.pingTickMs);
|
|
8393
8419
|
|
|
8394
8420
|
if (serverOption.showMetric) {
|
|
8395
8421
|
this.monitIntervalID = setInterval((e) => {
|
|
@@ -8401,10 +8427,37 @@ class Manager {
|
|
|
8401
8427
|
}
|
|
8402
8428
|
|
|
8403
8429
|
|
|
8430
|
+
heartbeatTick() {
|
|
8431
|
+
const now = node_perf_hooks.performance.now();
|
|
8432
|
+
// Process only expired requests, not every connected peer.
|
|
8433
|
+
for (const [remote, deadline] of this.heartbeatPending) {
|
|
8434
|
+
if (deadline > now) break;
|
|
8435
|
+
this.heartbeatPending.delete(remote);
|
|
8436
|
+
if (!remote._closing && remote.socket.isAlive === false) remote.close(true);
|
|
8437
|
+
}
|
|
8438
|
+
// The single timer schedules its next tick after this work; missed groups are not replayed.
|
|
8439
|
+
const group = this.pingGroups[this.pingGroupCursor];
|
|
8440
|
+
this.pingGroupCursor = (this.pingGroupCursor + 1) % this.pingGroups.length;
|
|
8441
|
+
for (const remote of group) {
|
|
8442
|
+
if (remote._closing || this.heartbeatPending.has(remote)) continue;
|
|
8443
|
+
remote.socket.isAlive = false;
|
|
8444
|
+
this.heartbeatPending.set(remote, node_perf_hooks.performance.now() + this.pingPeriod + this.pingGrace);
|
|
8445
|
+
try {
|
|
8446
|
+
remote.ping();
|
|
8447
|
+
} catch {
|
|
8448
|
+
this.heartbeatPending.delete(remote);
|
|
8449
|
+
remote.close(true);
|
|
8450
|
+
}
|
|
8451
|
+
}
|
|
8452
|
+
}
|
|
8453
|
+
|
|
8404
8454
|
addRemote(socket, req) {
|
|
8405
8455
|
socket.isAlive = true;
|
|
8406
8456
|
let remote = new Remote(socket, req, this);
|
|
8407
8457
|
this.remotes.add(remote);
|
|
8458
|
+
remote.pingGroup = this.nextPingGroup;
|
|
8459
|
+
this.pingGroups[remote.pingGroup].add(remote);
|
|
8460
|
+
this.nextPingGroup = (this.nextPingGroup + 1) % this.pingGroups.length;
|
|
8408
8461
|
remote.send(remote.issueAuthChallenge());
|
|
8409
8462
|
remote.send( Buffer.from([ IOMsg.SERVER_READY]) );
|
|
8410
8463
|
remote.setState(STATE.SERVER_READY);
|
|
@@ -8418,6 +8471,8 @@ class Manager {
|
|
|
8418
8471
|
removeRemote(remote) {
|
|
8419
8472
|
|
|
8420
8473
|
remote._closing = true;
|
|
8474
|
+
this.pingGroups[remote.pingGroup]?.delete(remote);
|
|
8475
|
+
this.heartbeatPending.delete(remote);
|
|
8421
8476
|
remote.setState(STATE.CLOSED);
|
|
8422
8477
|
|
|
8423
8478
|
let remoteInfo = `- IP:${remote.ip} #${remote.ssid} cid:${remote.cid} ${remote?.socket.socketType === 'websocket' ? "WS" : "CS"} `;
|
|
@@ -8596,6 +8651,7 @@ class Manager {
|
|
|
8596
8651
|
|
|
8597
8652
|
}
|
|
8598
8653
|
|
|
8654
|
+
if (this.telemetry) this.telemetry.publishes++;
|
|
8599
8655
|
let sentCounter = 0;
|
|
8600
8656
|
if (this.channel_map.has(tag)) {
|
|
8601
8657
|
// console.log('raw channel_map matched tag:', tag)
|
|
@@ -8607,6 +8663,7 @@ class Manager {
|
|
|
8607
8663
|
} else {
|
|
8608
8664
|
limit = remotes.size;
|
|
8609
8665
|
}
|
|
8666
|
+
if (this.telemetry) this.telemetry.quotaExcluded += remotes.size - limit;
|
|
8610
8667
|
let peers = remotes.values();
|
|
8611
8668
|
while (sentCounter < limit) {
|
|
8612
8669
|
let c = peers.next().value;
|
|
@@ -8626,6 +8683,7 @@ class Manager {
|
|
|
8626
8683
|
// } else {
|
|
8627
8684
|
// // console.log(`pub >> [${tag}] sent: ${sentCounter} [no quota limit] total: ${remotes.size} subscribers. ` )
|
|
8628
8685
|
// }
|
|
8686
|
+
if (this.telemetry) this.telemetry.deliveryAttempts += sentCounter;
|
|
8629
8687
|
return ['ok', sentCounter]
|
|
8630
8688
|
}
|
|
8631
8689
|
}
|
|
@@ -8759,7 +8817,10 @@ class Manager {
|
|
|
8759
8817
|
|
|
8760
8818
|
close() {
|
|
8761
8819
|
this.metrics.close();
|
|
8762
|
-
|
|
8820
|
+
this.heartbeatStopped = true;
|
|
8821
|
+
clearTimeout(this.pingIntervalID);
|
|
8822
|
+
this.heartbeatPending.clear();
|
|
8823
|
+
for (const group of this.pingGroups) group.clear();
|
|
8763
8824
|
if (this.monitIntervalID) {
|
|
8764
8825
|
clearInterval(this.monitIntervalID);
|
|
8765
8826
|
}
|
|
@@ -8785,6 +8846,10 @@ const STATUS = {
|
|
|
8785
8846
|
|
|
8786
8847
|
class Server extends require$$0$3 {
|
|
8787
8848
|
|
|
8849
|
+
/**
|
|
8850
|
+
* @param {object} options Server configuration.
|
|
8851
|
+
* @param {any} [authManager] Optional authentication manager.
|
|
8852
|
+
*/
|
|
8788
8853
|
constructor(options, authManager) {
|
|
8789
8854
|
super();
|
|
8790
8855
|
const security = { ...serverOption.security, ...options.security };
|
|
@@ -8799,6 +8864,13 @@ class Server extends require$$0$3 {
|
|
|
8799
8864
|
this.port = null;
|
|
8800
8865
|
this.congPort = null;
|
|
8801
8866
|
|
|
8867
|
+
if (options.pingTimeoutGrace !== undefined) {
|
|
8868
|
+
if (!Number.isFinite(options.pingTimeoutGrace) || options.pingTimeoutGrace < 0) {
|
|
8869
|
+
throw new RangeError('pingTimeoutGrace must be a finite non-negative number');
|
|
8870
|
+
}
|
|
8871
|
+
serverOption.pingTimeoutGrace = options.pingTimeoutGrace;
|
|
8872
|
+
}
|
|
8873
|
+
|
|
8802
8874
|
if (options.timeout) {
|
|
8803
8875
|
let pingT = parseInt(options.timeout);
|
|
8804
8876
|
if (pingT && pingT >= 1000) serverOption.timeout = pingT;
|
|
@@ -8932,49 +9004,44 @@ class Server extends require$$0$3 {
|
|
|
8932
9004
|
throw new Error(`Service ${service} : no commands or !Array.`)
|
|
8933
9005
|
}
|
|
8934
9006
|
|
|
8935
|
-
|
|
8936
|
-
|
|
8937
|
-
|
|
8938
|
-
|
|
8939
|
-
|
|
8940
|
-
|
|
8941
|
-
|
|
8942
|
-
|
|
8943
|
-
|
|
8944
|
-
|
|
8945
|
-
|
|
8946
|
-
|
|
8947
|
-
|
|
8948
|
-
|
|
8949
|
-
|
|
8950
|
-
|
|
8951
|
-
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
9007
|
+
const handleError = (remote, req, error) => {
|
|
9008
|
+
console.error(`Unhandled Service Error in service [${service}]:`, error);
|
|
9009
|
+
if (remote._closing) return;
|
|
9010
|
+
// A disconnected transport can also fail while reporting the service error.
|
|
9011
|
+
try {
|
|
9012
|
+
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
9013
|
+
} catch (responseError) {
|
|
9014
|
+
console.error(`Service error response failed [${service}]:`, responseError);
|
|
9015
|
+
}
|
|
9016
|
+
};
|
|
9017
|
+
const useCall = typeof service_module.call === 'function';
|
|
9018
|
+
this.on(service, (remote, req) => {
|
|
9019
|
+
try {
|
|
9020
|
+
if (!service_module.checkPermission(remote, req)) {
|
|
9021
|
+
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.");
|
|
9022
|
+
return;
|
|
8952
9023
|
}
|
|
8953
|
-
|
|
8954
|
-
|
|
8955
|
-
|
|
8956
|
-
this.on(service, (remote, req) => {
|
|
8957
|
-
try {
|
|
8958
|
-
if (!service_module.checkPermission(remote, req)) {
|
|
8959
|
-
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.");
|
|
8960
|
-
return
|
|
8961
|
-
}
|
|
8962
|
-
if( service_module.commands.includes( req.topic )){
|
|
8963
|
-
service_module[req.topic](remote, req);
|
|
8964
|
-
}else {
|
|
8965
|
-
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
8966
|
-
}
|
|
8967
|
-
} catch (error) {
|
|
8968
|
-
console.error(`Unhandled Service Error in service [${service}]:`, error);
|
|
8969
|
-
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
9024
|
+
if (!service_module.commands.includes(req.topic)) {
|
|
9025
|
+
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
9026
|
+
return;
|
|
8970
9027
|
}
|
|
8971
|
-
|
|
8972
|
-
|
|
9028
|
+
// Keep synchronous invocation and the service receiver; also observe async failures.
|
|
9029
|
+
const result = useCall
|
|
9030
|
+
? service_module.call(remote, req)
|
|
9031
|
+
: service_module[req.topic](remote, req);
|
|
9032
|
+
Promise.resolve(result).catch(error => handleError(remote, req, error));
|
|
9033
|
+
} catch (error) {
|
|
9034
|
+
handleError(remote, req, error);
|
|
9035
|
+
}
|
|
9036
|
+
});
|
|
8973
9037
|
this.serviceNames.add(service);
|
|
8974
9038
|
// console.log(`[Service attached: ${service} ] accept commands: ${service_module.commands}`)
|
|
8975
9039
|
return this
|
|
8976
9040
|
}
|
|
8977
9041
|
|
|
9042
|
+
/**
|
|
9043
|
+
* @param {() => void} [callback] Called when the server has closed.
|
|
9044
|
+
*/
|
|
8978
9045
|
close(callback) {
|
|
8979
9046
|
console.log('closing iosignal server...');
|
|
8980
9047
|
if (this.manager) {
|