UNPKG

@jo.shinonome/j-star

Version:

javascript interface for j-star

1 lines 12.8 kB
"use strict";var e=require("buffer"),t=require("events"),r=require("net"),n=require("tls"),i=require("apache-arrow");function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function a(e,t,r){return t=c(t),function(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}(e,l()?Reflect.construct(t,[],c(e).constructor):t.apply(e,r))}function u(e,t,r){return t&&function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,g(n.key),n)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function s(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=y(e))||t){r&&(e=r);var n=0,i=function(){};return{s:i,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){u=!0,o=e},f:function(){try{a||null==r.return||r.return()}finally{if(u)throw o}}}}function c(e){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},c(e)}function l(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(e){}return(l=function(){return!!e})()}function f(e,t){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},f(e,t)}function h(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,i,o,a,u=[],s=!0,c=!1;try{if(o=(r=r.call(e)).next,0===t);else for(;!(s=(n=o.call(r)).done)&&(u.push(n.value),u.length!==t);s=!0);}catch(e){c=!0,i=e}finally{try{if(!s&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw i}}return u}}(e,t)||y(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function v(e){return v="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},v(e)}function y(e,t){if(e){if("string"==typeof e)return o(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(e,t):void 0}}var m=[[],[0,0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0],[0,0,0,0],[0,0,0],[0,0],[0]];var d={deserialize:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=16,o=function(){var a,u,s,c=e[n];switch(n+=4,c){case 255:var l=1===e[n];return n+=4,l;case 254:var f=e[n];return n+=4,f;case 253:var h=e.readInt16LE(n);return n+=4,h;case 252:case 250:var g=e.readInt32LE(n);return n+=4,252===c?g:new Date(864e5*g);case 251:case 249:case 248:case 247:case 246:n+=4;var v=e.readBigInt64LE(n);if(n+=8,251===c)return t?v:Number(v);if(249===c)return u=(a=v)%1000000000n,[(s=a/1000000000n)/3600n,s/60n%60n,s%60n].map((function(e){return String(e).padStart(2,"0")})).join(":")+"."+String(u).padStart(9,"0");if(248===c)return new Date(Number(v));if(247===c){var y=new Date(Number(v/1000000n));return r?y.toISOString().slice(0,-1)+String(v%1000000n).padStart(6,"0"):y}return function(e){var t=e<0n?"-":"";e<0n&&(e*=-1n);var r=e/1000000000n,n=e/86400000000000n,i=r/3600n%24n,o=r/60n%60n,a=r%60n,u=e%1000000000n;return"".concat(t).concat(n,"D").concat(String(i).padStart(2,"0"),":").concat(String(o).padStart(2,"0"),":").concat(String(a).padStart(2,"0"),".").concat(String(u).padStart(9,"0"))}(v);case 245:var d=e.readFloatLE(n);return n+=4,d;case 244:n+=4;var p=e.readDoubleLE(n);return n+=8,p;case 0:return n+=4,null;case 243:case 242:case 128:case 154:var b=e.readUInt32LE(n);n+=4;var B=e.subarray(n,n+b).toString("utf8");return n+=b+m[b%8].length,128===c?new Error(B):B;case 90:var w=e.readUInt32LE(n);n+=4,n+=8;for(var I=new Array(w),E=0;E<w;E++)I[E]=o();return I;case 92:n+=4;var L=Number(e.readBigUInt64LE(n));n+=8;var S=i.RecordBatchStreamReader.from(e.subarray(n,n+L)),k=new i.Table(S.readAll());return n+=L,k;case c>=1&&c<=19:n+=4;var U=Number(e.readBigUInt64LE(n));n+=8;var O=i.RecordBatchStreamReader.from(e.subarray(n,n+U)),N=new i.Table(O.readAll());return n+=U,N;case 91:var T=e.readUInt32LE(n);n+=4;var A=new Map;if(0===T)return A;n+=8;var j=Number(e.readBigUInt64LE(n));n+=8;var _=new Uint32Array(e.buffer,e.byteOffset+n,T),C=e.subarray(n+4*T,n+j);n+=j+m[j%8].length,n+=8;for(var D=0,P=0;P<T;P++){var R=_[P],M=C.subarray(D,R).toString("utf8");D=R;var z=o();A.set(M,z)}return A;default:throw new Error("Unsupported jType: "+c)}};return o()},serialize:function e(t){var r=[];if(t instanceof i.Table){var n=i.tableToIPC(t,"stream"),o=Buffer.alloc(16);o.writeUInt8(92,0),o.writeBigUInt64LE(BigInt(n.length),8),r.push(o),r.push(n)}else if(t instanceof Date){var a=Buffer.alloc(16),u=BigInt(t.getTime());a.writeUInt8(248,0),a.writeBigInt64LE(u,8),r.push(a)}else if(t instanceof Array){var c,l=[],f=s(t);try{for(f.s();!(c=f.n()).done;){var g=e(c.value);l=l.concat(g)}}catch(e){f.e(e)}finally{f.f()}var y=Buffer.alloc(16);y.writeUInt8(90,0),y.writeUInt32LE(t.length,4);var d=l.reduce((function(e,t){return e+t.length}),0);y.writeBigUInt64LE(BigInt(d),8),r.push(y),r=r.concat(l)}else{if(null===t)return[Buffer.alloc(8)];switch(v(t)){case"number":if(Number.isInteger(t)){var p=Buffer.alloc(16);return p.writeUInt8(251,0),p.writeBigInt64LE(BigInt(t),8),[p]}var b=Buffer.alloc(16);return b.writeUInt8(244,0),b.writeDoubleLE(t,8),[b];case"boolean":var B=Buffer.alloc(8);return B.writeUInt8(255,0),B.writeUInt8(t?1:0,4),[B];case"string":var w=Buffer.byteLength(t,"utf8"),I=8+w+m[w%8].length,E=Buffer.alloc(I);return E.writeUInt8(243,0),E.writeUInt32LE(w,4),E.write(t,8),[E];case"bigint":var L=Buffer.alloc(16);return L.writeUInt8(251,0),L.writeBigInt64LE(t,8),[L];case"object":var S=t instanceof Map?Array.from(t):Object.entries(t);if(0===S.length)return[Buffer.from([91,0,0,0,0,0,0,0])];var k=Buffer.alloc(24);k.writeUInt8(91,0),k.writeUInt32LE(S.length,4);var U=4*S.length+S.reduce((function(e,t){return e+Buffer.byteLength(t[0],"utf8")}),0);U+=m[U%8].length;var O,N=Buffer.alloc(U),T=[],A=4*S.length,j=0,_=s(S.entries());try{for(_.s();!(O=_.n()).done;){var C=h(O.value,2),D=C[0],P=h(C[1],2),R=P[0],M=P[1],z=Buffer.byteLength(R,"utf8");j+=z,N.writeUint32LE(j,4*D),N.write(R,A),A+=z,T=T.concat(e(M))}}catch(e){_.e(e)}finally{_.f()}var q=T.reduce((function(e,t){return e+t.length}),0);k.writeBigUInt64LE(BigInt(U+q),8),k.writeBigUInt64LE(BigInt(U),16),r.push(k),r.push(N);var x=Buffer.alloc(8);x.writeBigUInt64LE(BigInt(q),0),r.push(x),r=r.concat(T)}}return r},ACK:Buffer.from([1,2,0,0,0,0,0,0,8,0,0,0,0,0,0,0,255,0,0,0,1,0,0,0])},p=function(i){function o(t){var r,n,i,u,s,c,l,f,h,g;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,o),(g=a(this,o)).socketArgs=t,g.host=null!==(r=t.host)&&void 0!==r?r:"localhost",g.port=t.port,g.user=null!==(n=t.user)&&void 0!==n?n:"",g.password=null!==(i=t.password)&&void 0!==i?i:"",g.useBigInt=null!==(u=t.useBigInt)&&void 0!==u&&u,g.socket=null,g.callbacks=[],g.socketTimeout=null!==(s=t.socketTimeout)&&void 0!==s?s:0,g.socketNoDelay=null===(c=t.socketNoDelay)||void 0===c||c,g.msgBuffer=e.Buffer.alloc(0),g.msgLength=0,g.enableTLS=null!==(l=t.enableTLS)&&void 0!==l&&l,g.includeNanosecond=null!==(f=t.includeNanosecond)&&void 0!==f&&f,g.dateToMillisecond=null!==(h=t.dateToMillisecond)&&void 0!==h&&h,g}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&f(e,t)}(o,t.EventEmitter),u(o,[{key:"setSocket",value:function(e){var t=this;this.socket=e,this.socket.setNoDelay(this.socketNoDelay),this.socket.setTimeout(this.socketTimeout),this.socket.on("end",(function(){return t.emit("end")})),this.socket.on("timeout",(function(){return t.emit("timeout")})),this.socket.on("error",(function(e){return t.emit("error",e)})),this.socket.on("close",(function(e){return t.emit("close",e)})),this.socket.on("data",(function(e){return t.incomingMsgHandler(e)}))}},{key:"auth",value:function(t,r){var n=this,i="".concat(this.user,":").concat(this.password),o=e.Buffer.byteLength(i,"ascii"),a=e.Buffer.alloc(o+2);a.write(i,0,o,"ascii"),a.writeUInt8(9,o),a.writeUInt8(0,o+1),t.write(a),t.once("data",(function(e){1===e.length?e[0]>=9?(t.removeAllListeners("close"),t.removeAllListeners("error"),n.callbacks=[],n.setSocket(t),r(null),t.on("close",(function(){n.callbacks.forEach((function(e){return e(new Error("LOST_CONNECTION"),null)})),n.callbacks=[]}))):r(new Error("UNSUPPORTED_IPC_VERSION<="+e[0])):r(new Error("INVALID_AUTH_RESPONSE"))}))}},{key:"connect",value:function(e){var t,i=this;""===this.user&&(this.user=process.env.USER),this.socket&&this.socket.end();var o=function(){t.removeAllListeners("error"),t.once("close",(function(){t.end(),e(new Error("ERR_CONNECTION_CLOSED - Wrong Credentials?"))})),t.once("error",(function(r){t.end(),e(r)})),i.auth(t,e)};(t=this.enableTLS?n.connect(this.port,this.host,{rejectUnauthorized:!1},o):r.connect(this.port,this.host,o)).once("error",(function(t){return e(t)}))}},{key:"close",value:function(e){this.socket.once("close",(function(){e&&e()})),this.socket.end()}},{key:"incomingMsgHandler",value:function(t){if(this.msgBuffer.length>16)t.copy(this.msgBuffer,this.msgLength),this.msgLength+=t.length;else if(0===this.msgBuffer.length&&t.length>=16){var r=16+Number(t.readBigUInt64LE(8));if(r>t.length)return this.msgBuffer=e.Buffer.alloc(r),t.copy(this.msgBuffer),void(this.msgLength=t.length);this.msgBuffer=t.subarray(0,r),this.msgLength=t.length}else if(this.msgBuffer.length+t.length>=16){this.msgLength=this.msgBuffer.length+t.length;var n=e.Buffer.alloc(16);this.msgBuffer.copy(n);var i=this.msgBuffer.length;t.copy(n,i);var o=16+Number(n.readBigUInt64LE(8));this.msgBuffer=e.Buffer.alloc(o),n.copy(this.msgBuffer),t.copy(this.msgBuffer,16-i)}else{var a=e.Buffer.alloc(this.msgBuffer.length+t.length);this.msgBuffer.copy(a),t.copy(a,this.msgBuffer.length),this.msgBuffer=a,this.msgLength=a.length}if(this.msgBuffer.length>16&&this.msgLength>=this.msgBuffer.length){var u,s;try{u=d.deserialize(this.msgBuffer,this.useBigInt,this.includeNanosecond),s=null}catch(e){u=null,s=e}if(2===this.msgBuffer.readUInt8(1)?this.callbacks.shift()(s,u):0===this.msgBuffer.readUInt8(1)?!s&&Array.isArray(u)&&"upd"===u[0]&&this.emit("upd",u):this.socket.write(d.ACK),this.msgLength>this.msgBuffer.length){var c=t.subarray(t.length+this.msgBuffer.length-this.msgLength);if(c.length>=16){var l=16+Number(c.readBigUInt64LE(8));if(l>c.length){var f=e.Buffer.alloc(l);c.copy(f),this.msgBuffer=f}else this.msgBuffer=c}else this.msgBuffer=c;this.msgLength=c.length}else this.msgBuffer=e.Buffer.alloc(0),this.msgLength=0}}},{key:"sync",value:function(t,r){var n=this;if("function"!=typeof r)throw new Error("Expecting a callback function as last param");if(!t||Array.isArray(t)&&0===t.length)this.callbacks.push(r);else{var i=d.serialize(t),o=i.reduce((function(e,t){return e+t.length}),0),a=e.Buffer.alloc(16);a.writeUInt8(1,0),a.writeUInt8(1,1),a.writeBigUInt64LE(BigInt(o),8),this.callbacks.push(r);try{this.socket.write(a);var u=i.map((function(e){return n.socket.write(e)})).every(Boolean);u||this.callbacks.pop()(new Error("LOST_CONNECTION"),null)}catch(e){this.callbacks.pop()(e,null)}}}},{key:"asyn",value:function(t,r){var n=this,i=d.serialize(t),o=i.reduce((function(e,t){return e+t.length}),0),a=e.Buffer.alloc(16);a.writeUInt8(1,0),a.writeBigUInt64LE(BigInt(o),8);var u=i.pop();this.socket.write(a),i.forEach((function(e){return n.socket.write(e)})),r?this.socket.write(u,r):this.socket.write(u)}}])}();exports.JConnection=p;