UNPKG

jkdb

Version:

javascript interface for kdb+/q

1 lines 17.3 kB
"use strict";var e=require("buffer"),r=require("events"),t=require("net"),n=require("tls");function o(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=Array(r);t<r;t++)n[t]=e[t];return n}function a(e,r,t){return r=c(r),function(e,r){if(r&&("object"==typeof r||"function"==typeof r))return r;if(void 0!==r)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,u()?Reflect.construct(r,[],c(e).constructor):r.apply(e,t))}function i(e,r,t){return r&&function(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,h(n.key),n)}}(e.prototype,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function c(e){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},c(e)}function u(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(u=function(){return!!e})()}function s(e,r){return s=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},s(e,r)}function f(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,o,a,i,c=[],u=!0,s=!1;try{if(a=(t=t.call(e)).next,0===r);else for(;!(u=(n=a.call(t)).done)&&(c.push(n.value),c.length!==r);u=!0);}catch(e){s=!0,o=e}finally{try{if(!u&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(s)throw o}}return c}}(e,r)||y(e,r)||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 o(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||y(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){var r=function(e,r){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,r);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof r?r:r+""}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,r){if(e){if("string"==typeof e)return o(e,r);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(e,r):void 0}}var b=-2147483648,g=2147483647,d=-2147483647,m=-9223372036854775808n,p=9223372036854775807n,w=-9223372036854775807n,E=9466848e5,S=864e5,k=" 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},N={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:"::"},O={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"},L={0:"'",1:"/",2:"\\",3:"':",4:"/:",5:"\\:"};function T(e){if("bigint"==typeof e){var r=e<0n?"-":"";e<0n&&(e*=-1n);var t=e/1000000000n;return"".concat(r).concat(e/86400000000000n,"D").concat(String(t/3600n%24n).padStart(2,"0"),":").concat(String(t/60n%60n).padStart(2,"0"),":").concat(String(t%60n).padStart(2,"0"),".").concat(String(e%1000000000n).padStart(9,"0"))}return null}function B(e,r){if(!Number.isFinite(e))return null;var t,n,o=e<0?"-":"",a=Math.abs(e);switch(r){case 243:case 13:return t=e%12,"".concat(2e3+e/12>>>0,".").concat(String(t+(t<0?13:1)).padStart(2,"0"),"m");case 239:case 17:return o+[a/60>>>0,a%60].map(function(e){return String(e).padStart(2,"0")}).join(":");case 238:case 18:return o+[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,o+[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 - "+r)}}function A(e){switch(e){case-32768:return NaN;case 32767:return 1/0;case-32767:return-1/0;default:return e}}function _(e){switch(e){case b:return NaN;case g:return 1/0;case d:return-1/0;default:return e}}function j(e){switch(e){case m:return NaN;case p:return 1/0;case w:return-1/0;default:return e}}function D(e){if(null==e)return 101;if(e instanceof Date)return 244;if(Array.isArray(e)){var r=e[Symbol.for("kType")],t=r?k.indexOf(r):0;return t>0?t:0}switch(v(e)){case"number":return 247;case"bigint":return 249;case"boolean":return 255;case"string":return 10;case"object":return e[Symbol.for("meta")]?98:99}}function P(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(10!==(r=null!=r?r:D(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 249:case 255:return 1+I[256-r];case 98:var t=3,n=e[Symbol.for("meta")],o=n.c;t+=P(o,11);var a=e[o[0]].length;return t+=6,o.forEach(function(r,o){if(a!==e[r].length)throw new Error("NOT_SAME_SIZE_COLUMN - "+r);var i=k.indexOf(n.t[o]);i=i>0?i:0,t+=P(e[r],i)}),t;case 99:var i=Object.keys(e),c=Object.values(e);return 1+P(i,11)+P(c);case 0:var u=6;return e.forEach(function(e){return u+=P(e)}),u;case 11:var s=6;return e.forEach(function(e){return s+=e.length+1}),s;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 U={deserialize:function(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=arguments.length>3&&void 0!==arguments[3]&&arguments[3],o=8,a=function(r){var i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];switch(r){case 255:return 1===e[o++];case 254:var c=e.subarray(o,o+16).toString("hex");return o+=16,c;case 252:return e[o++];case 251:var u=e.readInt16LE(o);return o+=2,A(u);case 250:var s=e.readInt32LE(o);return o+=4,_(s);case 249:var f=j(e.readBigInt64LE(o));return o+=8,i?f:Number(f);case 248:var l=e.readFloatLE(o);return o+=4,l;case 247:var h=e.readDoubleLE(o);return o+=8,h;case 246:return String.fromCharCode(e[o++]);case 245:var v=e.indexOf(0,o),y=e.subarray(o,v).toString();return o=v+1,y;case 244:var b=a(249,!0);if("bigint"==typeof b){var g=new Date(Number(b/1000000n)+E);return t?g.toISOString().slice(0,-1)+String(b%1000000n).padStart(6,"0"):g}return t?"":null;case 243:return B(a(250),243);case 242:var d=E+a(250)*S;return n?d:Number.isFinite(d)?new Date(d):null;case 241:var m=E+a(247)*S;return n?m:Number.isFinite(m)?new Date(m):null;case 240:var p=a(249,!0);return"bigint"==typeof p?T(p):null;case 239:return B(a(250),239);case 238:return B(a(250),238);case 237:return B(a(250),237)}},i=function(i){o++;var u=a(250);if(10===i){var s=e.subarray(o,o+u).toString();return o+=u,s}var f=new Array(u);if(0===i){for(var l=0;l<u;l++)f[l]=c();return f}if(11===i){for(var h=0;h<u;h++)f[h]=a(245);return f}if(2===i){for(var v=0;v<u;v++)f[v]=e.subarray(o+16*v,o+16*(v+1)).toString("hex");return o+=16*u,f}var y=new DataView(e.buffer,o+e.offset,I[i]*u),b=I[i];o+=u*b;var g=0;switch(i){case 1:for(g=0;g<u;g++)f[g]=1===y.getUint8(g);return f;case 4:for(g=0;g<u;g++)f[g]=y.getUint8(g);return f;case 5:for(g=0;g<u;g++){var d=y.getInt16(g*b,!0);f[g]=A(d)}return f;case 6:for(g=0;g<u;g++){var k=y.getInt32(g*b,!0);f[g]=_(k)}return f;case 7:if(r)for(g=0;g<u;g++){var N=y.getBigInt64(g*b,!0);f[g]=j(N)}else for(g=0;g<u;g++){var O=y.getBigInt64(g*b,!0);f[g]=Number(j(O))}return f;case 8:for(g=0;g<u;g++)f[g]=y.getFloat32(g*b,!0);return f;case 9:for(g=0;g<u;g++)f[g]=y.getFloat64(g*b,!0);return f;case 12:for(g=0;g<u;g++){var L=y.getBigInt64(g*b,!0);if(L===m||L===p||L===w)f[g]=t?"":null;else{var D=new Date(Number(L/1000000n)+E);f[g]=t?D.toISOString().slice(0,-1)+String(L%1000000n).padStart(6,"0"):D}}return f;case 13:case 17:case 18:case 19:for(g=0;g<u;g++){var P=_(y.getInt32(g*b,!0));f[g]=Number.isFinite(P)?B(P,i):null}return f;case 14:for(g=0;g<u;g++){var U=y.getInt32(g*b,!0),C=E+_(U)*S;f[g]=n?C:Number.isFinite(C)?new Date(C):null}return f;case 15:for(g=0;g<u;g++){var x=E+y.getFloat64(g*b,!0)*S;f[g]=n?x:Number.isFinite(x)?new Date(x):null}return f;case 16:for(g=0;g<u;g++){var R=y.getBigInt64(g*b,!0);f[g]=R===m||R===p||R===w?null:T(R)}return f;default:throw new Error("UNSUPPORTED_K_LIST - "+i)}},c=function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],n=e[o++];if(128===n)throw new Error(a(245));if(237<=n&&n<=255)return a(n,r);if(0<=n&&n<=19){var u=i(n);return Array.isArray(u)&&(u[Symbol.for("kType")]=k[n]),u}if(99===n){var s=98===e[o],h=c(),v=h[Symbol.for("meta")]?c():c(t=!0);if(s){for(var y,b,g=0,d=Object.entries(v);g<d.length;g++){var m=f(d[g],2),p=m[0],w=m[1];h[p]=w}var E=h[Symbol.for("meta")];h[Symbol.for("keys")]=E.c.slice();var S=v[Symbol.for("meta")];return(y=E.c).push.apply(y,l(S.c)),(b=E.t).push.apply(b,l(S.t)),h[Symbol.for("meta")]=E,h}var I={};return h.forEach(function(e,r){return I[e]=v[r]}),I}if(98===n){o+=3;var T=i(11);o+=6;var B={c:T,t:[]},A={};if(T.forEach(function(r){var t=e[o++];A[r]=i(t),B.t.push(k[t])}),A[Symbol.for("meta")]=B,t){var _=new Array(A[B.c[0]].length);return A[B.c[0]].forEach(function(e,r){var t={};B.c.forEach(function(e){return t[e]=A[e][r]}),_[r]=t}),_}return A}if(100===n)return e[o++]>0&&o++,o++,i(10);if(101===n)return N[e[o++]];if(102===n)return O[e[o++]];if(103===n)return L[e[o++]];if(104===n)return o--,i(0);throw new Error("UNSUPPORTED_K_TYPE[read] - "+n)};return 1===e[2]&&(e=function(e){var r=e.readInt32LE(8),t=Buffer.alloc(r);e.copy(t,0,0,4),t[2]=0,e.copy(t,4,8,12);for(var n=12,o=8,a=o,i=0,c=0,u=0,s=0,f=0,l=new Int32Array(256);o<r;){if(0===s&&(i=e[n++],s=1),u=0,i&s){for(c=l[e[n++]],u=e[n++],f=0;f<u+2;f++)t[o+f]=t[c+f];o+=2}else t[o++]=e[n++];for(;a<o-1;)l[t[a]^t[a+1]]=a++;i&s&&(a=o+=u),256==(s*=2)&&(s=0)}return t}(e)),c()},serialize:function(e){var r=8,t=P(e,null),n=Buffer.alloc(8+t);n[0]=1,n.writeUInt32LE(t+8,4);var o=function(e){var t=D(e);switch(n[r]=t,r++,t){case 101:n[r++]=0;break;case 244:var o=1000000n*BigInt(e.getTime()-E);n.writeBigInt64LE(o,r),r+=8;break;case 247:n.writeDoubleLE(e,r),r+=8;break;case 249:n.writeBigInt64LE(e,r),r+=8;break;case 255:n[r++]=e?1:0;break;case 98:n[r++]=0,n[r++]=99;var i=e[Symbol.for("meta")],c=i.c;n[r++]=11,a(c,11),n[r++]=0,n[r++]=0,n.writeUInt32LE(c.length,r),r+=4,c.forEach(function(t,o){var c=k.indexOf(i.t[o]);c=c>0?c:0,n[r++]=c,a(e[t],c)});break;case 99:var u=Object.keys(e),s=Object.values(e);n[r++]=11,a(u,11),n[r++]=0,a(s,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,t)}},a=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(n[r++]=0,10===t){var a=n.write(e,r+4);n.writeUInt32LE(a,r),r+=4+a}else n.writeUInt32LE(e.length,r),r+=4;switch(t){case 0:e.forEach(function(e){o(e)});break;case 1:e.forEach(function(e){return n[r++]=e?1:0});break;case 2:e.forEach(function(e){Buffer.from(e,"hex").copy(n,r),r+=16});break;case 6:e.forEach(function(e){null==e||Number.isNaN(e)?n.writeInt32LE(b,r):e===1/0?n.writeInt32LE(g,r):e===-1/0?n.writeInt32LE(d,r):n.writeInt32LE(e,r),r+=4});break;case 7:e.forEach(function(e){if(null==e||Number.isNaN(e))n.writeBigInt64LE(m,r);else if(e===1/0)n.writeBigInt64LE(p,r);else if(e===-1/0)n.writeBigInt64LE(w,r);else{var t="bigint"==typeof e?e:BigInt(e);n.writeBigInt64LE(t,r)}r+=8});break;case 9:e.forEach(function(e){n.writeDoubleLE(e,r),r+=8});break;case 11:e.forEach(function(e){n.write(e,r),r+=e.length,n[r++]=0});break;case 12:e.forEach(function(e){if(e){var t=1000000n*BigInt(e.getTime()-E);n.writeBigInt64LE(t,r)}else n.writeBigInt64LE(m,r);r+=8});break;case 14:e.forEach(function(e){if(e){var t=(e.getTime()-E)/S;n.writeInt32LE(t,r)}else n.writeInt32LE(b,r);r+=4});break;case 15:e.forEach(function(e){if(e){var t=(e.getTime()-E)/S;n.writeDoubleLE(t,r)}else n.writeDoubleLE(NaN,r);r+=8})}};return o(e),n},ACK:Buffer.from("010200000a0000006500","hex")},C=function(){function o(r){var t,n,i,c,u,s,f,l,h,v;return function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}(this,o),(v=a(this,o)).socketArgs=r,v.host=null!==(t=r.host)&&void 0!==t?t:"localhost",v.port=r.port,v.user=null!==(n=r.user)&&void 0!==n?n:"",v.password=null!==(i=r.password)&&void 0!==i?i:"",v.useBigInt=null!==(c=r.useBigInt)&&void 0!==c&&c,v.socket=null,v.callbacks=[],v.socketTimeout=null!==(u=r.socketTimeout)&&void 0!==u?u:0,v.socketNoDelay=null===(s=r.socketNoDelay)||void 0===s||s,v.msgBuffer=e.Buffer.alloc(0),v.enableTLS=null!==(f=r.enableTLS)&&void 0!==f&&f,v.includeNanosecond=null!==(l=r.includeNanosecond)&&void 0!==l&&l,v.dateToMillisecond=null!==(h=r.dateToMillisecond)&&void 0!==h&&h,v.isConnected=!1,v}return function(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(r&&r.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),r&&s(e,r)}(o,r.EventEmitter),i(o,[{key:"setSocket",value:function(e){var r=this;this.socket=e,this.isConnected=!0,this.socket.setNoDelay(this.socketNoDelay),this.socket.setTimeout(this.socketTimeout),this.socket.on("end",function(){return r.emit("end")}),this.socket.on("timeout",function(){return r.emit("timeout")}),this.socket.on("error",function(e){return r.emit("error",e)}),this.socket.on("close",function(e){return r.emit("close",e)}),this.socket.on("data",function(e){return r.incomingMsgHandler(e)})}},{key:"auth",value:function(r,t){var n=this,o="".concat(this.user,":").concat(this.password),a=e.Buffer.byteLength(o,"ascii"),i=e.Buffer.alloc(a+2);i.write(o,0,a,"ascii"),i.writeUInt8(3,a),i.writeUInt8(0,a+1),r.write(i),r.once("data",function(e){1===e.length?e[0]>=1?(r.removeAllListeners("close"),r.removeAllListeners("error"),n.callbacks=[],n.setSocket(r),t(null),r.on("close",function(){n.callbacks.forEach(function(e){return e(new Error("LOST_CONNECTION"),null)}),n.callbacks=[],n.isConnected=!1})):t(new Error("UNSUPPORTED_KDB_VERSION<=2.5")):t(new Error("INVALID_AUTH_RESPONSE"))})}},{key:"connect",value:function(e){var r=this;if(this.isConnected)return e(null);""===this.user&&(this.user=process.env.USER),this.socket&&this.socket.end();var o,a=!1,i=function(r){a||(a=!0,e(r))},c=function(){o.removeAllListeners("error"),o.once("close",function(){o.end(),i(new Error("ERR_CONNECTION_CLOSED - Wrong Credentials?"))}),o.once("error",function(e){o.end(),i(e)}),r.auth(o,i)};(o=this.enableTLS?n.connect(this.port,this.host,{rejectUnauthorized:!1},c):t.connect(this.port,this.host,c)).once("error",function(e){return i(e)})}},{key:"close",value:function(e){this.socket?(this.socket.once("close",function(){e&&e()}),this.socket.end(),this.isConnected=!1):e&&e()}},{key:"incomingMsgHandler",value:function(r){for(this.msgBuffer=this.msgBuffer.length>0?e.Buffer.concat([this.msgBuffer,r]):r;this.msgBuffer.length>=8;){var t=this.msgBuffer.readUInt32LE(4);if(t>this.msgBuffer.length)break;var n=this.msgBuffer.subarray(0,t);this.msgBuffer=this.msgBuffer.subarray(t);var o=void 0,a=void 0;try{o=U.deserialize(n,this.useBigInt,this.includeNanosecond,this.dateToMillisecond),a=null}catch(e){o=null,a=e}var i=n.readUInt8(1);2===i?this.callbacks.shift()(a,o):0===i?!a&&Array.isArray(o)&&"upd"===o[0]&&this.emit("upd",o):this.socket.write(U.ACK)}}},{key:"sync",value:function(e,r){var t=this;if("function"!=typeof r)throw new Error("Expecting a callback function as last param");if(!this.isConnected)return r(new Error("NOT_CONNECTED"),null);if(!e||Array.isArray(e)&&0===e.length)this.callbacks.push(r);else{var n=U.serialize(e);n.writeUInt8(1,1),this.socket.write(n,function(){return t.callbacks.push(r)})}}},{key:"asyn",value:function(e,r){var t=U.serialize(e);t.writeUInt8(0,1),r?this.socket.write(t,r):this.socket.write(t)}},{key:"connectAsync",value:function(){var e=this;return new Promise(function(r,t){return e.connect(function(e){return e?t(e):r()})})}},{key:"syncAsync",value:function(e){var r=this;return new Promise(function(t,n){return r.sync(e,function(e,r){return e?n(e):t(r)})})}},{key:"closeAsync",value:function(){var e=this;return new Promise(function(r){return e.close(r)})}}])}();exports.QConnection=C;