UNPKG

jkdb

Version:

javascript interface for kdb+/q

1 lines 18 kB
"use strict";var e=require("buffer"),t=require("events"),r=require("net"),n=require("tls");function i(e){return i="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},i(e)}function a(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,(i=n.key,a=void 0,"symbol"==typeof(a=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(i,"string"))?a:String(a)),n)}var i,a}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function s(e,t){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},s(e,t)}function c(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)}function u(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var r,n=o(e);if(t){var i=o(this).constructor;r=Reflect.construct(n,arguments,i)}else r=n.apply(this,arguments);return c(this,r)}}function f(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,a,o,s=[],c=!0,u=!1;try{if(a=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=a.call(r)).done)&&(s.push(n.value),s.length!==t);c=!0);}catch(e){u=!0,i=e}finally{try{if(!c&&null!=r.return&&(o=r.return(),Object(o)!==o))return}finally{if(u)throw i}}return s}}(e,t)||h(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 l(e){return function(e){if(Array.isArray(e))return g(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||h(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function h(e,t){if(e){if("string"==typeof e)return g(e,t);var r=Object.prototype.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)?g(e,t):void 0}}function g(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var v=-32768,m=32767,y=-32767,b=-2147483648,d=2147483647,p=-2147483647,w=-9223372036854775808n,E=9223372036854775807n,S=-9223372036854775807n,k=9466848e5,B=864e5,O=" bg xhijefcspmdznuvt",I={1:1,2:16,4:1,5:2,6:4,7:8,8:4,9:8,10:1,12:8,13:4,14:4,15:8,16:8,17:4,18:4,19:4,101:1},L={0:null,1:"+:",2:"-:",3:"*:",4:"%:",5:"&:",6:"|:",7:"^:",8:"=:",9:"<:",10:">:",11:"$:",12:",:",13:"#:",14:"_:",15:"~:",16:"!:",17:"?:",18:"@:",19:".:",20:"0::",21:"1::",22:"2::",23:"avg",24:"last",25:"sum",26:"prd",27:"min",28:"max",29:"exit",30:"getenv",31:"abs",32:"sqrt",33:"log",34:"exp",35:"sin",36:"asin",37:"cos",38:"acos",39:"tan",40:"atan",41:"enlist",42:"var",43:"dev",44:"hopen",255:"::"},N={0:":",1:"+",2:"-",3:"*",4:"%",5:"&",6:"|",7:"^",8:"=",9:"<",10:">",11:"$",12:",",13:"#",14:"_",15:"~",16:"!",17:"?",18:"@",19:".",20:"0:",21:"1:",22:"2:",23:"in",24:"within",25:"like",26:"bin",27:"ss",28:"insert",29:"wsum",30:"wavg",31:"div",32:"xexp",33:"setenv",34:"binr",35:"cov",36:"cor"},T={0:"'",1:"/",2:"\\",3:"':",4:"/:",5:"\\:"};function A(e){if(Number.isNaN(e))return null;var t=e<0n?"-":"";e<0n&&(e*=-1n);var r=e/1000000000n;return"".concat(t).concat(e/86400000000000n,"D").concat(String(r/3600n%24n).padStart(2,"0"),":").concat(String(r/60n%60n).padStart(2,"0"),":").concat(String(r%60n).padStart(2,"0"),".").concat(String(e%1000000000n).padStart(9,"0"))}function _(e,t){if(!Number.isFinite(e))return null;var r,n,i=e<0?"-":"",a=Math.abs(e);switch(t){case 243:case 13:return r=e%12,"".concat(2e3+e/12>>>0,".").concat(String(r+(r<0?13:1)).padStart(2,"0"),"m");case 239:case 17:return i+[a/60>>>0,a%60].map((function(e){return String(e).padStart(2,"0")})).join(":");case 238:case 18:return i+[a/3600>>>0,a/60%60>>>0,a%60].map((function(e){return String(e).padStart(2,"0")})).join(":");case 237:case 19:return n=a%1e3,i+[a/36e5>>>0,a/6e4%60>>>0,a/1e3%60>>>0].map((function(e){return String(e).padStart(2,"0")})).join(":")+"."+String(n).padStart(3,"0");default:throw new Error("FAILED_TO_CAST_TO_TEMPORAL_STRING - "+t)}}function j(e){switch(e){case v:return NaN;case m:return 1/0;case y:return-1/0;default:return e}}function U(e){switch(e){case b:return NaN;case d:return 1/0;case p:return-1/0;default:return e}}function D(e){switch(e){case w:return NaN;case E:return 1/0;case S:return-1/0;default:return e}}function P(e){if(null==e)return 101;if(e instanceof Date)return 244;if(Array.isArray(e)){var t=e[Symbol.for("kType")],r=t?O.indexOf(t):0;return r>0?r:0}switch(i(e)){case"number":return 247;case"boolean":return 255;case"string":return 10;case"object":return e[Symbol.for("meta")]?98:99}}function x(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(10!==(r=null!==(t=r)&&void 0!==t?t:P(e))&&r<20&&!Array.isArray(e))throw new Error("NOT_AN_ARRAY[calcMsgLength]");switch(r){case 101:return 1+I[r];case 244:case 246:case 247:case 255:return 1+I[256-r];case 98:var n=3,i=e[Symbol.for("meta")],a=i.c;n+=x(a,11);var o=e[a[0]].length;return n+=6,a.forEach((function(t,r){if(o!==e[t].length)throw new Error("NOT_SAME_SIZE_COLUMN - "+t);var a=O.indexOf(i.t[r]);a=a>0?a:0,n+=x(e[t],a)})),n;case 99:var s=Object.keys(e),c=Object.values(e);return 1+x(s,11)+x(c);case 0:var u=6;return e.forEach((function(e){return u+=x(e)})),u;case 11:var f=6;return e.forEach((function(e){return f+=e.length+1})),f;case 10:return 6+Buffer.byteLength(e,"utf8");case 1:case 2:case 6:case 7:case 9:case 12:case 14:case 15:return 6+I[r]*e.length;default:throw new Error("UNSUPPORTED_K_TYPE[calcMsgLength] - "+r)}}var R={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=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=8,a=function t(a){var o=arguments.length>1&&void 0!==arguments[1]&&arguments[1];switch(a){case 255:return 1===e[i++];case 254:var s=e.subarray(i,i+16).toString("hex");return i+=16,s;case 252:return e[i++];case 251:var c=e.readInt16LE(i);return i+=2,j(c);case 250:var u=e.readInt32LE(i);return i+=4,U(u);case 249:var f=D(e.readBigInt64LE(i));return i+=8,o?f:Number(f);case 248:var l=e.readFloatLE(i);return i+=4,l;case 247:var h=e.readDoubleLE(i);return i+=8,h;case 246:return String.fromCharCode(e[i++]);case 245:var g=e.indexOf(0,i),v=e.subarray(i,g).toString();return i=g+1,v;case 244:var m=t(249,!0);if("bigint"==typeof m){var y=new Date(Number(m/1000000n)+k);return r?y.toISOString().slice(0,-1)+String(m%1000000n).padStart(6,"0"):y}return r?"":null;case 243:return _(t(250),243);case 242:var b=k+t(250)*B;return n?b:Number.isFinite(b)?new Date(b):null;case 241:var d=k+t(247)*B;return n?d:Number.isFinite(d)?new Date(d):null;case 240:var p=t(249,!0);return"bigint"==typeof p?A(p):null;case 239:return _(t(250),239);case 238:return _(t(250),238);case 237:return _(t(250),237)}},o=function(o){i++;var c=a(250);if(10===o){var u=e.subarray(i,i+c).toString();return i+=c,u}var f=new Array(c);if(0===o){for(g=0;g<c;g++)f[g]=s();return f}if(11===o){for(g=0;g<c;g++)f[g]=a(245);return f}if(2===o){for(g=0;g<c;g++)f[g]=e.subarray(i+16*g,i+16*(g+1)).toString("hex");return i+=16*c,f}var l=new DataView(e.buffer,i+e.offset,I[o]*c),h=I[o];i+=c*h;var g=0;switch(o){case 1:for(g=0;g<c;g++)f[g]=1===l.getUint8(g);return f;case 4:for(g=0;g<c;g++)f[g]=l.getUint8(g);return f;case 5:for(g=0;g<c;g++){var v=l.getInt16(g*h,!0);f[g]=j(v)}return f;case 6:for(g=0;g<c;g++){var m=l.getInt32(g*h,!0);f[g]=U(m)}return f;case 7:if(t)for(g=0;g<c;g++){var y=l.getBigInt64(g*h,!0);f[g]=D(y)}else for(g=0;g<c;g++){var b=l.getBigInt64(g*h,!0);f[g]=Number(D(b))}return f;case 8:for(g=0;g<c;g++)f[g]=l.getFloat32(g*h,!0);return f;case 9:for(g=0;g<c;g++)f[g]=l.getFloat64(g*h,!0);return f;case 12:for(g=0;g<c;g++){var d=l.getBigInt64(g*h,!0);if(d===w||d===E||d===S)f[g]=r?"":null;else{var p=new Date(Number(d/1000000n)+k);f[g]=r?p.toISOString().slice(0,-1)+String(d%1000000n).padStart(6,"0"):p}}return f;case 13:case 17:case 18:case 19:for(g=0;g<c;g++){var O=U(l.getInt32(g*h,!0));f[g]=Number.isFinite(O)?_(O,o):null}return f;case 14:for(g=0;g<c;g++){var L=l.getInt32(g*h,!0),N=k+U(L)*B;f[g]=n?N:Number.isFinite(N)?new Date(N):null}return f;case 15:for(g=0;g<c;g++){var T=k+l.getFloat64(g*h,!0)*B;f[g]=n?T:Number.isFinite(T)?new Date(T):null}return f;case 16:for(g=0;g<c;g++){var P=l.getBigInt64(g*h,!0);f[g]=P===w||P===E||P===S?null:A(P)}return f;default:throw new Error("UNSUPPORTED_K_LIST - "+o)}},s=function r(){var n=arguments.length>0&&void 0!==arguments[0]&&arguments[0],s=e[i++];if(128===s)throw new Error(a(245));if(237<=s&&s<=255)return a(s,t);if(0<=s&&s<=19){var c=o(s);return Array.isArray(c)&&(c[Symbol.for("kType")]=O[s]),c}if(99===s){var u=98===e[i],h=r(),g=h[Symbol.for("meta")]?r():r(n=!0);if(u){for(var v,m,y=0,b=Object.entries(g);y<b.length;y++){var d=f(b[y],2),p=d[0],w=d[1];h[p]=w}var E=h[Symbol.for("meta")];h[Symbol.for("keys")]=E.c.slice();var S=g[Symbol.for("meta")];return(v=E.c).push.apply(v,l(S.c)),(m=E.t).push.apply(m,l(S.t)),h[Symbol.for("meta")]=E,h}var k={};return h.forEach((function(e,t){return k[e]=g[t]})),k}if(98===s){i+=3;var B=o(11);i+=6;var I={c:B,t:[]},A={};if(B.forEach((function(t){var r=e[i++];A[t]=o(r),I.t.push(O[r])})),A[Symbol.for("meta")]=I,n){var _=new Array(A[I.c[0]].length);return A[I.c[0]].forEach((function(e,t){var r={};I.c.forEach((function(e){return r[e]=A[e][t]})),_[t]=r})),_}return A}if(100===s)return e[i++]>0&&i++,i++,o(10);if(101===s)return L[e[i++]];if(102===s)return N[e[i++]];if(103===s)return T[e[i++]];if(104===s)return i--,o(0);throw new Error("UNSUPPORTED_K_TYPE[read] - "+s)};return 1===e[2]&&(e=function(e){var t=e.readInt32LE(8),r=Buffer.alloc(t);e.copy(r,0,0,4),r[2]=0,e.copy(r,4,8,12);for(var n=12,i=8,a=i,o=0,s=0,c=0,u=0,f=0,l=new Int32Array(256);i<t;){if(0===u&&(o=e[n++],u=1),c=0,o&u){for(s=l[e[n++]],c=e[n++],f=0;f<c+2;f++)r[i+f]=r[s+f];i+=2}else r[i++]=e[n++];for(;a<i-1;)l[r[a]^r[a+1]]=a++;o&u&&(a=i+=c),256==(u*=2)&&(u=0)}return r}(e)),s()},serialize:function(e){var t=8,r=x(e,null),n=Buffer.alloc(8+r);n[0]=1,n.writeUInt32LE(r+8,4);var i=function(e){var r=P(e);switch(n[t]=r,t++,r){case 101:n[t++]=0;break;case 244:var i=1000000n*BigInt(e.getTime()-k);n.writeBigInt64LE(i,t),t+=8;break;case 247:n.writeDoubleLE(e,t),t+=8;break;case 255:n[t++]=e?1:0;break;case 98:n[t++]=0,n[t++]=99;var o=e[Symbol.for("meta")],s=o.c;n[t++]=11,a(Object.keys(e),11),n[t++]=0,n[t++]=0,n.writeUInt32LE(s.length,t),t+=4,s.forEach((function(r,i){var s=O.indexOf(o.t[i]);s=s>0?s:0,n[t++]=s,a(e[r],s)}));break;case 99:var c=Object.keys(e),u=Object.values(e);n[t++]=11,a(c,11),n[t++]=0,a(u,0);break;case 0:case 1:case 2:case 6:case 7:case 9:case 10:case 11:case 12:case 14:case 15:a(e,r)}},a=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(n[t++]=0,10===r){var a=n.write(e,t+4);n.writeUInt32LE(a,t),t+=4+a}else n.writeUInt32LE(e.length,t),t+=4;switch(r){case 0:e.forEach((function(e){i(e)}));break;case 1:e.forEach((function(e){return n[t++]=e?1:0}));break;case 2:e.forEach((function(e){Buffer.from(e,"hex").copy(n,t),t+=16}));break;case 6:e.forEach((function(e){e?(e===1/0?n.writeInt32LE(d,t):e===-1/0?n.writeInt32LE(p,t):n.writeInt32LE(e,t),t+=4):(n.writeInt32LE(b,t),t+=4)}));break;case 7:e.forEach((function(e){if(e)if(e===1/0)n.writeBigInt64LE(E,t);else if(e===-1/0)n.writeBigInt64LE(S,t);else{var r="bigint"==typeof e?e:BigInt(e);n.writeBigInt64LE(r,t)}else n.writeBigInt64LE(w,t);t+=8}));break;case 9:e.forEach((function(e){n.writeDoubleLE(e,t),t+=8}));break;case 11:e.forEach((function(e){n.write(e,t),t+=e.length,n[t++]=0}));break;case 12:e.forEach((function(e){if(e){var r=1000000n*BigInt(e.getTime()-k);n.writeBigInt64LE(r,t)}else n.writeBigInt64LE(w,t);t+=8}));break;case 14:e.forEach((function(e){if(e){var r=(e.getTime()-k)/B;n.writeInt32LE(r,t)}else n.writeInt32LE(b,t);t+=4}));break;case 15:e.forEach((function(e){if(e){var r=(e.getTime()-k)/B;n.writeDoubleLE(r,t)}else n.writeDoubleLE(NaN,t);t+=8}))}};return i(e),n},ACK:Buffer.from("010200000a0000006500","hex")},C=function(i){!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&&s(e,t)}(h,t.EventEmitter);var o,c,f,l=u(h);function h(t){var r,n,i,a,o,s,c,u,f,g;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,h),(g=l.call(this)).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!==(a=t.useBigInt)&&void 0!==a&&a,g.socket=null,g.callbacks=[],g.socketTimeout=null!==(o=t.socketTimeout)&&void 0!==o?o:0,g.socketNoDelay=null===(s=t.socketNoDelay)||void 0===s||s,g.msgBuffer=e.Buffer.alloc(0),g.msgOffset=0,g.enableTLS=null!==(c=t.enableTLS)&&void 0!==c&&c,g.includeNanosecond=null!==(u=t.includeNanosecond)&&void 0!==u&&u,g.dateToMillisecond=null!==(f=t.dateToMillisecond)&&void 0!==f&&f,g}return o=h,(c=[{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),a=e.Buffer.byteLength(i,"ascii"),o=e.Buffer.alloc(a+2);o.write(i,0,a,"ascii"),o.writeUInt8(3,a),o.writeUInt8(0,a+1),t.write(o),t.once("data",(function(e){1===e.length?e[0]>=1?(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_KDB_VERSION<=2.5")):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 a=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},a):r.connect(this.port,this.host,a)).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>8)t.copy(this.msgBuffer,this.msgOffset),this.msgOffset+=t.length;else if(0===this.msgBuffer.length&&t.length>=8){var r=t.readUInt32LE(4);if(r>t.length)return this.msgBuffer=e.Buffer.alloc(r),t.copy(this.msgBuffer),void(this.msgOffset=t.length);this.msgBuffer=t.subarray(0,r),this.msgOffset=t.length}else if(this.msgBuffer.length+t.length>=8){var n=e.Buffer.alloc(8);this.msgBuffer.copy(n),t.copy(n,this.msgBuffer.length);var i=n.readUInt32LE(4);this.msgBuffer=e.Buffer.alloc(i),n.copy(this.msgBuffer),t.copy(this.msgBuffer,this.msgBuffer.length+t.length-8),this.msgOffset=this.msgBuffer.length+t.length}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.msgOffset=0}for(;this.msgOffset>0&&this.msgOffset>=this.msgBuffer.length;){var o=void 0,s=void 0;try{o=R.deserialize(this.msgBuffer,this.useBigInt,this.includeNanosecond,this.dateToMillisecond),s=null}catch(e){o=null,s=e}if(2===this.msgBuffer.readUInt8(1)?this.callbacks.shift()(s,o):0===this.msgBuffer.readUInt8(1)?!s&&Array.isArray(o)&&"upd"===o[0]&&this.emit("upd",o):this.socket.write(R.ACK),this.msgOffset>this.msgBuffer.length){var c=t.subarray(t.length+this.msgBuffer.length-this.msgOffset);if(c.length>=8){var u=c.readUInt32LE(4);if(u>c.length){var f=e.Buffer.alloc(u);c.copy(f),this.msgBuffer=f}else this.msgBuffer=c;this.msgOffset=c.length}else this.msgBuffer=c,this.msgOffset=0}else this.msgBuffer=e.Buffer.alloc(0),this.msgOffset=0}}},{key:"sync",value:function(e,t){var r=this;if("function"!=typeof t)throw new Error("Expecting a callback function as last param");if(!e||Array.isArray(e)&&0===e.length)this.callbacks.push(t);else{var n=R.serialize(e);n.writeUInt8(1,1),this.socket.write(n,(function(){return r.callbacks.push(t)}))}}},{key:"asyn",value:function(e,t){var r=R.serialize(e);r.writeUInt8(1,0),t?this.socket.write(r,t):this.socket.write(r)}}])&&a(o.prototype,c),f&&a(o,f),Object.defineProperty(o,"prototype",{writable:!1}),h}();exports.QConnection=C;