cloudvision-connector
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [4.5.0](https://github.com/aristanetworks/cloudvision/compare/v4.4.0...v4.5.0) (2020-07-27) | ||
### Features | ||
* **cloudvision-connector:** add server getAndSubscribe ([#134](https://github.com/aristanetworks/cloudvision/issues/134)) ([ee7b861](https://github.com/aristanetworks/cloudvision/commit/ee7b861f6015f9c90dba3fce7f83fc94795b0252)) | ||
# [4.4.0](https://github.com/aristanetworks/cloudvision/compare/v4.3.1...v4.4.0) (2020-07-14) | ||
@@ -8,0 +19,0 @@ |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('a-msgpack'), require('base64-js'), require('imurmurhash')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'a-msgpack', 'base64-js', 'imurmurhash'], factory) : | ||
(global = global || self, factory(global.CloudvisionConnector = {}, global.msgpack, global['base64-js'], global.MurmurHash3)); | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CloudvisionConnector = {}, global.msgpack, global['base64-js'], global.MurmurHash3)); | ||
}(this, (function (exports, aMsgpack, base64Js, MurmurHash3) { 'use strict'; | ||
@@ -236,2 +236,3 @@ | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: {}, | ||
@@ -247,2 +248,3 @@ }; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: [], | ||
@@ -380,2 +382,3 @@ }; | ||
const WARN = 'WARN'; | ||
const EOF = 'EOF'; | ||
/** | ||
@@ -393,2 +396,3 @@ * Status code for EOF (End Of File). | ||
const PAUSED_CODE = 1002; | ||
const GET_REQUEST_COMPLETED = 'GetRequest'; | ||
const CLOSE = 'close'; | ||
@@ -398,2 +402,3 @@ const DEVICES_DATASET_ID = 'DEVICES_DATASET_ID'; | ||
const GET_DATASETS = 'getDatasets'; | ||
const GET_AND_SUBSCRIBE = 'getAndSubscribe'; | ||
const PAUSE = 'pause'; | ||
@@ -1148,2 +1153,3 @@ const PUBLISH = 'publish'; | ||
this.ws.onmessage = (event) => { | ||
var _a; | ||
if (typeof event.data !== 'string') { | ||
@@ -1190,2 +1196,6 @@ // messages that aren't strings are invalid | ||
this.events.emit(token, null, msg.result, status); | ||
if (((_a = msg.result) === null || _a === void 0 ? void 0 : _a.metadata) && | ||
msg.result.metadata[GET_REQUEST_COMPLETED] === EOF) { | ||
this.events.emit(token, null, null, { message: GET_REQUEST_COMPLETED, code: EOF_CODE }); | ||
} | ||
}; | ||
@@ -1408,20 +1418,3 @@ } | ||
}; | ||
const getToken = makeToken(GET, params); | ||
const subscribeAckCallback = (err, result, status, token) => { | ||
const subscribeNotifCallback = makeNotifCallback(callback); | ||
if (status && status.code === ACTIVE_CODE) { | ||
this.getWithOptions(query, callback, sanitizedOptions); | ||
} | ||
else { | ||
subscribeNotifCallback(err, result, status, token, { | ||
command: SUBSCRIBE, | ||
token: token || DEFAULT_CONTEXT.token, | ||
encodedParams: toBinaryKey(query), | ||
}); | ||
} | ||
}; | ||
return { | ||
subscribe: this.stream(SUBSCRIBE, { query }, subscribeAckCallback), | ||
get: getToken, | ||
}; | ||
return this.stream(GET_AND_SUBSCRIBE, params, makeNotifCallback(callback)); | ||
} | ||
@@ -1428,0 +1421,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("a-msgpack"),require("base64-js"),require("imurmurhash")):"function"==typeof define&&define.amd?define(["exports","a-msgpack","base64-js","imurmurhash"],t):t((e=e||self).CloudvisionConnector={},e.msgpack,e["base64-js"],e.MurmurHash3)}(this,(function(e,t,n,s){"use strict";s=s&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s;const r=e=>t.encode(e,{extensionCodec:t.Codec}),i=e=>t.decode(e,{extensionCodec:t.Codec,useJSBI:!0});function o(e){const t=[];for(let s=0;s<e.length;s+=1)t.push(n.fromByteArray(r(e[s])));return t}function a(e){const t={path_elements:[],timestamp:0};t.path_elements=function(e){const t=[];for(let s=0;s<e.length;s+=1)t.push(i(n.toByteArray(e[s])));return t}(e.path_elements||[]);let s="";return e.timestamp.nanos&&(s+=e.timestamp.nanos),s=s.padStart(9,"0"),t.timestamp=parseInt((""+e.timestamp.seconds+s).slice(0,13),10),e.updates&&(t.updates=function(e){const t={};for(let s=0;s<e.length;s+=1){const r=e[s].key,o=e[s].value,a=i(n.toByteArray(r)),c=i(n.toByteArray(o));t[r]={key:a,value:c}}return t}(e.updates)),e.deletes&&(t.deletes=function(e){const t={};for(let s=0;s<e.length;s+=1){const r=e[s];t[r]={key:i(n.toByteArray(r))}}return t}(e.deletes)),e.delete_all&&(t.deletes={}),t}function c(e,t){const n=a(t);e.notifications[JSON.stringify(n.path_elements)]?e.notifications[JSON.stringify(n.path_elements)].push(n):e.notifications[JSON.stringify(n.path_elements)]=[n]}function u(e,t){if(e.timestamp.seconds<t.timestamp.seconds)return-1;if(e.timestamp.seconds>t.timestamp.seconds)return 1;if(e.timestamp.nanos&&t.timestamp.nanos){if(e.timestamp.nanos<t.timestamp.nanos)return-1;if(e.timestamp.nanos>t.timestamp.nanos)return 1}return e.timestamp.nanos?t.timestamp.nanos?0:1:-1}function l(e){const t={timestamp:e.timestamp};return e.path_elements&&e.path_elements.length&&(t.path_elements=o(e.path_elements)),e.updates&&(t.updates=function(e){const t=[];for(let s=0;s<e.length;s+=1){const i=e[s].key,o=e[s].value;t.push({key:n.fromByteArray(r(i)),value:n.fromByteArray(r(o))})}return t}(e.updates)),e.deletes&&(Object.keys(e.deletes).length?t.deletes=function(e){const t=[];for(let s=0;s<e.length;s+=1){const i=e[s];t.push(n.fromByteArray(r(i)))}return t}(e.deletes):t.delete_all=!0),t}class h{static parse(e,t){const n=JSON.parse(e),s=n.result;if(s&&!s.datasets&&s.notifications){const e=function(e,t){if(e.notifications.sort(u),t){const t={dataset:e.dataset,notifications:{}};for(let n=0;n<e.notifications.length;n+=1){c(t,e.notifications[n])}return t}const n={dataset:e.dataset,notifications:[]};for(let t=0;t<e.notifications.length;t+=1){const s=a(e.notifications[t]);n.notifications.push(s)}return n}(s,t);return{error:n.error,result:e,status:n.status,token:n.token}}return{error:n.error,result:n.result,status:n.status,token:n.token}}static stringify(e){const t=Object.assign({},e.params);return h.isQuery(t)&&(t.query=function(e){const t=[];for(let s=0;s<e.length;s+=1){const i={dataset:e[s].dataset,paths:[]},a=e[s];for(let e=0;e<a.paths.length;e+=1){const t=a.paths[e],s={path_elements:[]},c=[];if(t.keys&&Array.isArray(t.keys)){for(let e=0;e<t.keys.length;e+=1)c.push(n.fromByteArray(r(t.keys[e])));s.keys=c}s.path_elements=o(t.path_elements),i.paths.push(s)}t.push(i)}return t}(t.query)),h.isPublish(t)&&(t.batch=function(e){const t=[],n={dataset:e.dataset,notifications:t};if(Array.isArray(e.notifications))for(let n=0;n<e.notifications.length;n+=1)t.push(l(e.notifications[n]));else{const n=Object.keys(e.notifications);for(let s=0;s<n.length;s+=1){const r=n[s];for(let n=0;n<e.notifications[r].length;n+=1)t.push(l(e.notifications[r][n]))}}return n}(t.batch)),JSON.stringify({token:e.token,command:e.command,params:t})}static isQuery(e){const t=e;return void 0!==t.query&&Array.isArray(t.query)}static isPublish(e){const t=e;return void 0!==t.sync&&void 0!==t.batch}}const d="ERROR",m=new Set(["ANY","IP","MAC"]),f="cloudvision-connector",p={command:"NO_COMMAND",token:"NO_TOKEN",encodedParams:"NO_PARAMS"};class g{constructor(){this.events=new Map,this.requestContext=new Map}getEventsMap(){return this.events}getRequestContextMap(){return this.requestContext}has(e){return this.events.has(e)}bind(e,t,n){let s=this.events.get(e);return s?s.push(n):(s=[n],this.events.set(e,s),this.requestContext.set(e,t)),s.length}unbind(e,t){const n=this.events.get(e);if(!n)return null;const s=n.indexOf(t);-1!==s&&(n.splice(s,1),this.events.set(e,n));const r=n.length;return 0===r&&(this.events.delete(e),this.requestContext.delete(e)),r}unbindAll(e){this.events.delete(e),this.requestContext.delete(e)}emit(e,...t){const n=this.events.get(e);if(n)for(let s=n.length-1;s>=0;s-=1){const r=this.requestContext.get(e)||p;n[s](r,...t)}}close(){this.events.clear(),this.requestContext.clear()}}function y(){}class b{constructor(e){this.enableInstrumentation=!1,e&&(this.enableInstrumentation=!0),this.instrumentedCommands=e?e.commands:[],this.startFunction=e?e.start:y,this.infoFunction=e?e.info:y,this.endFunction=e?e.end:y}callStart(e,t){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.startFunction(t)}callInfo(e,t,n){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.infoFunction(t,n)}callEnd(e,t){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.endFunction(t)}}function S(e,t,n,s){const r=t?`${e}: ${t}`:e+": No message provided",i=n?`Status Code: ${n.code}, Status Message: ${n.message}`:void 0,o=s?"Request Token: "+s:void 0;switch(console.groupCollapsed("[[ CloudVision Connector ]]"),e){case d:o&&console.error(o),console.error(r),i&&console.error(i);break;case"WARN":o&&console.warn(o),console.warn(r),i&&console.warn(i);break;default:o&&console.log(o),console.log(r),i&&console.log(i)}console.groupEnd()}function v(e){return"number"==typeof e&&e>0}function C(e){return e/1e15>1}function k(e){const{start:t,end:n,versions:s}=e;let r,i,o;return v(t)&&(r=Math.floor(t)*(C(t)?1:1e6)),v(n)&&(i=Math.floor(n)*(C(n)?1:1e6)),v(s)&&(o=Math.floor(s)),{start:r,end:i,versions:o}}function O(e,t){const{start:n,end:s,versions:r}=e;return n&&s&&n>=s?(t(function(e,t,n){return`invalid params: start: ${e}, end: ${t}, versions: ${n||"undefined"}`}(n,s,r)),!1):!n||!r||(t("Defining start and versions is invalid"),!1)}function E(e,t,n=!1){return Array.isArray(e)?!!(n||e&&e.length)||(t("`query` param cannot be empty"),!1):(t("`query` param must be an array"),!1)}function A(e){return function e(t,n){const s=Object.keys(t);for(let r=0;r<s.length;r+=1){const i=s[r],o=t[i];o&&"object"==typeof o?n.hash(i.toString()).hash(e(o,n)):n.hash(i+""+o)}return n.result().toString()}(e,s())}function N(e,t){return A({command:e,params:t})}function w(e,t={}){return(n,s,r,i,o)=>{if(r&&1001===r.code)return void e(null,void 0,r,i,o);if(n)return e(`Error: ${n}\nOptions: ${JSON.stringify(t)}`,void 0,r,i,o),void e(null,void 0,{code:1001},i,o);const a=s;if(a&&a.datasets)return void e(null,a,r,i,o);const c=s;(c&&c.dataset||c)&&e(null,c,r,i,o)}}function q(e,t){const n={};if(Array.isArray(e)){const s=e.length;for(let r=0;r<s;r+=1){const{token:s,callback:i}=e[r];0===t.unbind(s,i)&&(n[s]=!0)}}else{const{token:s,callback:r}=e;0===t.unbind(s,r)&&(n[s]=!0)}return Object.keys(n).length?n:null}function T(e){const t=e.searchType;let n="ANY";return t&&m.has(t)&&(n=t),{search:e.search||"",searchType:n}}function D(e){return n.fromByteArray(t.encode(e,{extensionCodec:t.Codec}))}class P{constructor(e={batchResults:!0,debugMode:!1,pauseStreams:!1},t=WebSocket,n=h){this.connectorOptions=e,this.isRunning=!1,this.connectionEvents=new g,this.events=new g,this.closingStreams=new Map,this.waitingStreams=new Map,this.activeStreams=new Set,this.activeRequests=new Set,this.WebSocket=t,this.Parser=n,this.instrumentation=new b(e.instrumentationConfig)}get websocket(){return this.ws}get connectionEmitter(){return this.connectionEvents}get eventsEmitter(){return this.events}get streams(){return this.activeStreams}get streamInClosingState(){return this.closingStreams}addWaitingStream(e,t,n,s,r){const i=this.waitingStreams.get(e)||[];i.push([t,n,r,s]),this.waitingStreams.set(e,i)}callCommand(e,t,n){if(!this.isRunning)return n("Connection is down"),null;const s=N(e,t),r={command:e,encodedParams:D(t),token:s};if(this.activeRequests.has(s)){const e=this.makeQueuedCallbackWithUnbind(s,n);this.events.bind(s,r,e)}else{this.activeRequests.add(s);const i=this.makeCallbackWithUnbind(s,n);this.events.bind(s,r,i),this.sendMessageOrError(s,e,t,r)}return s}cleanUpConnections(){this.activeStreams.clear(),this.events.close()}close(){this.closeWs(new CloseEvent("close")),this.connectionEvents.close(),this.ws.close()}closeWs(e){this.isRunning=!1,this.cleanUpConnections(),this.connectionEvents.emit("connection",P.DISCONNECTED,e)}closeCommand(e,t,n){const s=N("close",t),r={command:"close",encodedParams:D(e),token:s};this.setStreamClosingState(e,s,r,n);try{this.sendMessage(s,"close",r,t)}catch(t){S(d,t),this.removeStreamClosingState(e,s),n(t,void 0,void 0,s)}return s}closeStreams(e,t){const n=q(e,this.events);return n?this.closeCommand(e,n,t):null}closeStream(e,t){const n=q(e,this.events);return n?this.closeCommand(e,n,t):null}connection(e){const t=(t,n,s)=>{e(n,s)};return this.connectionEvents.bind("connection",{command:"connection",encodedParams:"",token:""},t),()=>{this.connectionEvents.unbind("connection",t)}}enableOptions(e){this.connectorOptions.pauseStreams?this.pause({pauseStreams:!0},e):e(null)}get(e,t,n){return this.callCommand(e,t,n)}makeCallbackWithUnbind(e,t){const n=(s,r,i,o)=>{r&&(this.activeRequests.delete(e),this.events.unbind(e,n),this.instrumentation.callInfo(s.command,s,{error:r})),this.instrumentation.callEnd(s.command,s),t(r,i,o,e,s)};return n}makeQueuedCallbackWithUnbind(e,t){const n=(s,r,i,o)=>{r&&(this.events.unbind(e,n),t(r,i,o,e,s))};return n}publish(e,t){return this.callCommand("publish",e,t)}removeStreamClosingState(e,t){if(Array.isArray(e)){const n=e.length;for(let s=0;s<n;s+=1){const{token:n}=e[s];this.closingStreams.delete(n);const r=this.waitingStreams.get(t);if(r){const e=r.length;for(let n=0;n<e;n+=1)this.reSubscribeStream(r[n],t)}}}else{this.closingStreams.delete(e.token);const n=this.waitingStreams.get(t);n&&this.reSubscribeStream(n[0],t)}}requestService(e,t){return this.callCommand("serviceRequest",e,t)}reSubscribeStream(e,t){this.events.unbindAll(t),this.waitingStreams.delete(t),this.sendMessage(...e)}resume(e,t){return this.callCommand("resume",e,t)}run(e){this.runWithWs(new this.WebSocket(e))}runWithWs(e){this.ws=e,this.ws.onopen=e=>{this.isRunning||(this.isRunning=!0,this.enableOptions((t,n,s,r)=>{s&&1001!==s.code&&S(d,t,s,r),this.connectionEvents.emit("connection",P.CONNECTED,e)}))},this.ws.onclose=e=>{this.closeWs(e)},this.ws.onmessage=e=>{if("string"!=typeof e.data)return;let t;try{t=this.Parser.parse(e.data,this.connectorOptions.batchResults),this.connectorOptions.debugMode&&self.postMessage({response:t,source:f,timestamp:Date.now()},"*")}catch(e){return void S(d,e)}if(!t||!t.token)return void S(d,"No message body or message token");const{error:n,status:s,token:r}=t;if(n)this.events.emit(r,n,t.result,s);else if(function(e,t,n,s){if(t&&Object.keys(t).length)return;const r=e;!r.dataset||r.dataset.name?!r.dataset||r.dataset.type?!r.dataset||r.notifications?s||!r.dataset||Array.isArray(r.notifications)||S(d,"Key 'notifications' is not an array",void 0,n):S(d,"No key 'notifications' found in response",void 0,n):S(d,"No key 'type' found in dataset",void 0,n):S(d,"No key 'name' found in dataset",void 0,n)}(t.result,s,r,this.connectorOptions.batchResults),s&&1002===s.code){const e=(e,t,n)=>{e&&S(d,e,n)};this.resume({token:r},e)}else this.events.emit(r,null,t.result,s)}}search(e,t){return this.callCommand("alpha/search",e,t)}sendMessage(e,t,n,s){this.connectorOptions.debugMode&&self.postMessage({request:{token:e,command:t,params:s},source:f,timestamp:Date.now()},"*"),this.instrumentation.callStart(t,n),this.ws.send(this.Parser.stringify({token:e,command:t,params:s}))}sendMessageOrError(e,t,n,s){try{this.sendMessage(e,t,s,n)}catch(t){S(d,t),this.events.emit(e,t,void 0,void 0)}}setStreamClosingState(e,t,n,s){if(Array.isArray(e)){const n=e.length;for(let s=0;s<n;s+=1){const{token:n}=e[s],r=this.closingStreams.get(n);this.waitingStreams.delete(r||""),this.closingStreams.set(n,t)}}else this.closingStreams.set(e.token,t);this.events.bind(t,n,(n,r,i,o)=>{this.removeStreamClosingState(e,t),s(r,i,o,t,n)})}stream(e,t,n){if(!this.isRunning)return n("Connection is down"),null;const s=N(e,t),r={command:e,encodedParams:D(t),token:s},i=(e,t,o,a)=>{t&&(this.events.unbind(s,i),this.instrumentation.callInfo(r.command,r,{error:t}),this.instrumentation.callEnd(e.command,e)),a&&3001===a.code&&(this.activeStreams.add(s),this.instrumentation.callInfo(r.command,r,{message:"stream active"})),n(t,o,a,s,e)};if(1===this.events.bind(s,r,i)){const n=this.closingStreams.get(s);n?this.addWaitingStream(n,s,e,t,r):this.sendMessageOrError(s,e,t,r)}else this.activeStreams.has(s)&&n(null,void 0,{code:3001},s,r);return{token:s,callback:i}}pause(e,t){return this.callCommand("pause",e,t)}}P.CONNECTED="connected",P.DISCONNECTED="disconnected";class I extends P{closeSubscriptions(e,t){return this.closeStreams(e,w(t))}getAndSubscribe(e,t,n){if(!E(e,t))return null;if(!O(n,t))return null;const s=k(n),r=N("get",{query:e,start:s.start,end:s.end,versions:s.versions});return{subscribe:this.stream("subscribe",{query:e},(n,r,i,o)=>{const a=w(t);i&&3001===i.code?this.getWithOptions(e,t,s):a(n,r,i,o,{command:"subscribe",token:o||p.token,encodedParams:D(e)})}),get:r}}getApps(e){const t={types:["app"]};return this.get("getDatasets",t,w(e))}getDatasets(e){const t={types:["app","device"]};return this.get("getDatasets",t,w(e))}getDevices(e){const t=["device"];return this.get("getDatasets",{types:t},w(e))}getCommandToken(e,t){return N(e,t)}getWithOptions(e,t,n){if("DEVICES_DATASET_ID"===e)return this.getDatasets(t),null;if(!E(e,t))return null;if(!O(n,t))return null;const{start:s,end:r,versions:i}=k(n),o={query:e,start:s,end:r,versions:i};return this.get("get",o,w(t,n))}runService(e,t){this.requestService(e,w(t))}runStreamingService(e,t){return e?this.stream("serviceRequest",e,w(t)):(t("`request` param cannot be empty"),null)}searchWithOptions(e,t,n){if(!E(e,t,!0))return null;if(!O(n,t))return null;const{start:s,end:r}=k(n),i=T(n),o={query:e,start:s,end:r,search:i.search,searchType:i.searchType};return this.search(o,w(t,n))}searchSubscribe(e,t,n={search:""}){if(!E(e,t,!0))return null;const s=T(n);return this.stream("alpha/searchSubscribe",{query:e,search:s.search,searchType:s.searchType},w(t))}subscribe(e,t){return E(e,t)?this.stream("subscribe",{query:e},w(t)):null}writeSync(e,t){this.publish({sync:!0,batch:{dataset:e.dataset,notifications:e.notifications}},function(e){return(t,n,s)=>{s&&1001===s.code?e(!0):t&&e(!1,`Error: ${t}\n`)}}(t))}}I.CLOSE="close",I.DEVICES_DATASET_ID="DEVICES_DATASET_ID",I.GET_DATASETS="getDatasets",I.GET="get",I.ID=f,I.PAUSE="pause",I.PUBLISH="publish",I.SEARCH_SUBSCRIBE="alpha/searchSubscribe",I.SEARCH="alpha/search",I.SUBSCRIBE="subscribe",Object.defineProperty(e,"Bool",{enumerable:!0,get:function(){return t.Bool}}),Object.defineProperty(e,"Float32",{enumerable:!0,get:function(){return t.Float32}}),Object.defineProperty(e,"Float64",{enumerable:!0,get:function(){return t.Float64}}),Object.defineProperty(e,"Int",{enumerable:!0,get:function(){return t.Int}}),Object.defineProperty(e,"NeatTypeSerializer",{enumerable:!0,get:function(){return t.NeatTypeSerializer}}),Object.defineProperty(e,"NeatTypes",{enumerable:!0,get:function(){return t.NeatTypes}}),Object.defineProperty(e,"Nil",{enumerable:!0,get:function(){return t.Nil}}),Object.defineProperty(e,"Pointer",{enumerable:!0,get:function(){return t.Pointer}}),Object.defineProperty(e,"Str",{enumerable:!0,get:function(){return t.Str}}),Object.defineProperty(e,"createBaseType",{enumerable:!0,get:function(){return t.createBaseType}}),Object.defineProperty(e,"isJsbi",{enumerable:!0,get:function(){return t.isJsbi}}),Object.defineProperty(e,"isNeatType",{enumerable:!0,get:function(){return t.isNeatType}}),e.ACTIVE_CODE=3001,e.CONNECTED="connected",e.DISCONNECTED="disconnected",e.EOF_CODE=1001,e.Parser=h,e.default=I,e.fromBinaryKey=function(e,s=!0){return t.decode(n.toByteArray(e),{extensionCodec:t.Codec,useJSBI:s})},e.hashObject=A,e.sanitizeOptions=k,e.toBinaryKey=D,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("a-msgpack"),require("base64-js"),require("imurmurhash")):"function"==typeof define&&define.amd?define(["exports","a-msgpack","base64-js","imurmurhash"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CloudvisionConnector={},e.msgpack,e["base64-js"],e.MurmurHash3)}(this,(function(e,t,n,s){"use strict";s=s&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s;const i=e=>t.encode(e,{extensionCodec:t.Codec}),r=e=>t.decode(e,{extensionCodec:t.Codec,useJSBI:!0});function o(e){const t=[];for(let s=0;s<e.length;s+=1)t.push(n.fromByteArray(i(e[s])));return t}function a(e){const t={path_elements:[],timestamp:0};t.path_elements=function(e){const t=[];for(let s=0;s<e.length;s+=1)t.push(r(n.toByteArray(e[s])));return t}(e.path_elements||[]);let s="";return e.timestamp.nanos&&(s+=e.timestamp.nanos),s=s.padStart(9,"0"),t.timestamp=parseInt((""+e.timestamp.seconds+s).slice(0,13),10),e.updates&&(t.updates=function(e){const t={};for(let s=0;s<e.length;s+=1){const i=e[s].key,o=e[s].value,a=r(n.toByteArray(i)),c=r(n.toByteArray(o));t[i]={key:a,value:c}}return t}(e.updates)),e.deletes&&(t.deletes=function(e){const t={};for(let s=0;s<e.length;s+=1){const i=e[s];t[i]={key:r(n.toByteArray(i))}}return t}(e.deletes)),e.delete_all&&(t.deletes={}),t}function c(e,t){const n=a(t);e.notifications[JSON.stringify(n.path_elements)]?e.notifications[JSON.stringify(n.path_elements)].push(n):e.notifications[JSON.stringify(n.path_elements)]=[n]}function u(e,t){if(e.timestamp.seconds<t.timestamp.seconds)return-1;if(e.timestamp.seconds>t.timestamp.seconds)return 1;if(e.timestamp.nanos&&t.timestamp.nanos){if(e.timestamp.nanos<t.timestamp.nanos)return-1;if(e.timestamp.nanos>t.timestamp.nanos)return 1}return e.timestamp.nanos?t.timestamp.nanos?0:1:-1}function l(e){const t={timestamp:e.timestamp};return e.path_elements&&e.path_elements.length&&(t.path_elements=o(e.path_elements)),e.updates&&(t.updates=function(e){const t=[];for(let s=0;s<e.length;s+=1){const r=e[s].key,o=e[s].value;t.push({key:n.fromByteArray(i(r)),value:n.fromByteArray(i(o))})}return t}(e.updates)),e.deletes&&(Object.keys(e.deletes).length?t.deletes=function(e){const t=[];for(let s=0;s<e.length;s+=1){const r=e[s];t.push(n.fromByteArray(i(r)))}return t}(e.deletes):t.delete_all=!0),t}class h{static parse(e,t){const n=JSON.parse(e),s=n.result;if(s&&!s.datasets&&s.notifications){const e=function(e,t){if(e.notifications.sort(u),t){const t={dataset:e.dataset,metadata:e.metadata||{},notifications:{}};for(let n=0;n<e.notifications.length;n+=1){c(t,e.notifications[n])}return t}const n={dataset:e.dataset,metadata:e.metadata||{},notifications:[]};for(let t=0;t<e.notifications.length;t+=1){const s=a(e.notifications[t]);n.notifications.push(s)}return n}(s,t);return{error:n.error,result:e,status:n.status,token:n.token}}return{error:n.error,result:n.result,status:n.status,token:n.token}}static stringify(e){const t=Object.assign({},e.params);return h.isQuery(t)&&(t.query=function(e){const t=[];for(let s=0;s<e.length;s+=1){const r={dataset:e[s].dataset,paths:[]},a=e[s];for(let e=0;e<a.paths.length;e+=1){const t=a.paths[e],s={path_elements:[]},c=[];if(t.keys&&Array.isArray(t.keys)){for(let e=0;e<t.keys.length;e+=1)c.push(n.fromByteArray(i(t.keys[e])));s.keys=c}s.path_elements=o(t.path_elements),r.paths.push(s)}t.push(r)}return t}(t.query)),h.isPublish(t)&&(t.batch=function(e){const t=[],n={dataset:e.dataset,notifications:t};if(Array.isArray(e.notifications))for(let n=0;n<e.notifications.length;n+=1)t.push(l(e.notifications[n]));else{const n=Object.keys(e.notifications);for(let s=0;s<n.length;s+=1){const i=n[s];for(let n=0;n<e.notifications[i].length;n+=1)t.push(l(e.notifications[i][n]))}}return n}(t.batch)),JSON.stringify({token:e.token,command:e.command,params:t})}static isQuery(e){const t=e;return void 0!==t.query&&Array.isArray(t.query)}static isPublish(e){const t=e;return void 0!==t.sync&&void 0!==t.batch}}const d="ERROR",m=new Set(["ANY","IP","MAC"]),f="cloudvision-connector",p={command:"NO_COMMAND",token:"NO_TOKEN",encodedParams:"NO_PARAMS"};class g{constructor(){this.events=new Map,this.requestContext=new Map}getEventsMap(){return this.events}getRequestContextMap(){return this.requestContext}has(e){return this.events.has(e)}bind(e,t,n){let s=this.events.get(e);return s?s.push(n):(s=[n],this.events.set(e,s),this.requestContext.set(e,t)),s.length}unbind(e,t){const n=this.events.get(e);if(!n)return null;const s=n.indexOf(t);-1!==s&&(n.splice(s,1),this.events.set(e,n));const i=n.length;return 0===i&&(this.events.delete(e),this.requestContext.delete(e)),i}unbindAll(e){this.events.delete(e),this.requestContext.delete(e)}emit(e,...t){const n=this.events.get(e);if(n)for(let s=n.length-1;s>=0;s-=1){const i=this.requestContext.get(e)||p;n[s](i,...t)}}close(){this.events.clear(),this.requestContext.clear()}}function y(){}class b{constructor(e){this.enableInstrumentation=!1,e&&(this.enableInstrumentation=!0),this.instrumentedCommands=e?e.commands:[],this.startFunction=e?e.start:y,this.infoFunction=e?e.info:y,this.endFunction=e?e.end:y}callStart(e,t){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.startFunction(t)}callInfo(e,t,n){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.infoFunction(t,n)}callEnd(e,t){this.enableInstrumentation&&this.instrumentedCommands.includes(e)&&this.endFunction(t)}}function v(e,t,n,s){const i=t?`${e}: ${t}`:e+": No message provided",r=n?`Status Code: ${n.code}, Status Message: ${n.message}`:void 0,o=s?"Request Token: "+s:void 0;switch(console.groupCollapsed("[[ CloudVision Connector ]]"),e){case d:o&&console.error(o),console.error(i),r&&console.error(r);break;case"WARN":o&&console.warn(o),console.warn(i),r&&console.warn(r);break;default:o&&console.log(o),console.log(i),r&&console.log(r)}console.groupEnd()}function S(e){return"number"==typeof e&&e>0}function C(e){return e/1e15>1}function k(e){const{start:t,end:n,versions:s}=e;let i,r,o;return S(t)&&(i=Math.floor(t)*(C(t)?1:1e6)),S(n)&&(r=Math.floor(n)*(C(n)?1:1e6)),S(s)&&(o=Math.floor(s)),{start:i,end:r,versions:o}}function O(e,t){const{start:n,end:s,versions:i}=e;return n&&s&&n>=s?(t(function(e,t,n){return`invalid params: start: ${e}, end: ${t}, versions: ${n||"undefined"}`}(n,s,i)),!1):!n||!i||(t("Defining start and versions is invalid"),!1)}function E(e,t,n=!1){return Array.isArray(e)?!!(n||e&&e.length)||(t("`query` param cannot be empty"),!1):(t("`query` param must be an array"),!1)}function A(e){return function e(t,n){const s=Object.keys(t);for(let i=0;i<s.length;i+=1){const r=s[i],o=t[r];o&&"object"==typeof o?n.hash(r.toString()).hash(e(o,n)):n.hash(r+""+o)}return n.result().toString()}(e,s())}function N(e,t){return A({command:e,params:t})}function w(e,t={}){return(n,s,i,r,o)=>{if(i&&1001===i.code)return void e(null,void 0,i,r,o);if(n)return e(`Error: ${n}\nOptions: ${JSON.stringify(t)}`,void 0,i,r,o),void e(null,void 0,{code:1001},r,o);const a=s;if(a&&a.datasets)return void e(null,a,i,r,o);const c=s;(c&&c.dataset||c)&&e(null,c,i,r,o)}}function T(e,t){const n={};if(Array.isArray(e)){const s=e.length;for(let i=0;i<s;i+=1){const{token:s,callback:r}=e[i];0===t.unbind(s,r)&&(n[s]=!0)}}else{const{token:s,callback:i}=e;0===t.unbind(s,i)&&(n[s]=!0)}return Object.keys(n).length?n:null}function q(e){const t=e.searchType;let n="ANY";return t&&m.has(t)&&(n=t),{search:e.search||"",searchType:n}}function D(e){return n.fromByteArray(t.encode(e,{extensionCodec:t.Codec}))}class P{constructor(e={batchResults:!0,debugMode:!1,pauseStreams:!1},t=WebSocket,n=h){this.connectorOptions=e,this.isRunning=!1,this.connectionEvents=new g,this.events=new g,this.closingStreams=new Map,this.waitingStreams=new Map,this.activeStreams=new Set,this.activeRequests=new Set,this.WebSocket=t,this.Parser=n,this.instrumentation=new b(e.instrumentationConfig)}get websocket(){return this.ws}get connectionEmitter(){return this.connectionEvents}get eventsEmitter(){return this.events}get streams(){return this.activeStreams}get streamInClosingState(){return this.closingStreams}addWaitingStream(e,t,n,s,i){const r=this.waitingStreams.get(e)||[];r.push([t,n,i,s]),this.waitingStreams.set(e,r)}callCommand(e,t,n){if(!this.isRunning)return n("Connection is down"),null;const s=N(e,t),i={command:e,encodedParams:D(t),token:s};if(this.activeRequests.has(s)){const e=this.makeQueuedCallbackWithUnbind(s,n);this.events.bind(s,i,e)}else{this.activeRequests.add(s);const r=this.makeCallbackWithUnbind(s,n);this.events.bind(s,i,r),this.sendMessageOrError(s,e,t,i)}return s}cleanUpConnections(){this.activeStreams.clear(),this.events.close()}close(){this.closeWs(new CloseEvent("close")),this.connectionEvents.close(),this.ws.close()}closeWs(e){this.isRunning=!1,this.cleanUpConnections(),this.connectionEvents.emit("connection",P.DISCONNECTED,e)}closeCommand(e,t,n){const s=N("close",t),i={command:"close",encodedParams:D(e),token:s};this.setStreamClosingState(e,s,i,n);try{this.sendMessage(s,"close",i,t)}catch(t){v(d,t),this.removeStreamClosingState(e,s),n(t,void 0,void 0,s)}return s}closeStreams(e,t){const n=T(e,this.events);return n?this.closeCommand(e,n,t):null}closeStream(e,t){const n=T(e,this.events);return n?this.closeCommand(e,n,t):null}connection(e){const t=(t,n,s)=>{e(n,s)};return this.connectionEvents.bind("connection",{command:"connection",encodedParams:"",token:""},t),()=>{this.connectionEvents.unbind("connection",t)}}enableOptions(e){this.connectorOptions.pauseStreams?this.pause({pauseStreams:!0},e):e(null)}get(e,t,n){return this.callCommand(e,t,n)}makeCallbackWithUnbind(e,t){const n=(s,i,r,o)=>{i&&(this.activeRequests.delete(e),this.events.unbind(e,n),this.instrumentation.callInfo(s.command,s,{error:i})),this.instrumentation.callEnd(s.command,s),t(i,r,o,e,s)};return n}makeQueuedCallbackWithUnbind(e,t){const n=(s,i,r,o)=>{i&&(this.events.unbind(e,n),t(i,r,o,e,s))};return n}publish(e,t){return this.callCommand("publish",e,t)}removeStreamClosingState(e,t){if(Array.isArray(e)){const n=e.length;for(let s=0;s<n;s+=1){const{token:n}=e[s];this.closingStreams.delete(n);const i=this.waitingStreams.get(t);if(i){const e=i.length;for(let n=0;n<e;n+=1)this.reSubscribeStream(i[n],t)}}}else{this.closingStreams.delete(e.token);const n=this.waitingStreams.get(t);n&&this.reSubscribeStream(n[0],t)}}requestService(e,t){return this.callCommand("serviceRequest",e,t)}reSubscribeStream(e,t){this.events.unbindAll(t),this.waitingStreams.delete(t),this.sendMessage(...e)}resume(e,t){return this.callCommand("resume",e,t)}run(e){this.runWithWs(new this.WebSocket(e))}runWithWs(e){this.ws=e,this.ws.onopen=e=>{this.isRunning||(this.isRunning=!0,this.enableOptions((t,n,s,i)=>{s&&1001!==s.code&&v(d,t,s,i),this.connectionEvents.emit("connection",P.CONNECTED,e)}))},this.ws.onclose=e=>{this.closeWs(e)},this.ws.onmessage=e=>{var t;if("string"!=typeof e.data)return;let n;try{n=this.Parser.parse(e.data,this.connectorOptions.batchResults),this.connectorOptions.debugMode&&self.postMessage({response:n,source:f,timestamp:Date.now()},"*")}catch(e){return void v(d,e)}if(!n||!n.token)return void v(d,"No message body or message token");const{error:s,status:i,token:r}=n;if(s)this.events.emit(r,s,n.result,i);else if(function(e,t,n,s){if(t&&Object.keys(t).length)return;const i=e;!i.dataset||i.dataset.name?!i.dataset||i.dataset.type?!i.dataset||i.notifications?s||!i.dataset||Array.isArray(i.notifications)||v(d,"Key 'notifications' is not an array",void 0,n):v(d,"No key 'notifications' found in response",void 0,n):v(d,"No key 'type' found in dataset",void 0,n):v(d,"No key 'name' found in dataset",void 0,n)}(n.result,i,r,this.connectorOptions.batchResults),i&&1002===i.code){const e=(e,t,n)=>{e&&v(d,e,n)};this.resume({token:r},e)}else this.events.emit(r,null,n.result,i),(null===(t=n.result)||void 0===t?void 0:t.metadata)&&"EOF"===n.result.metadata.GetRequest&&this.events.emit(r,null,null,{message:"GetRequest",code:1001})}}search(e,t){return this.callCommand("alpha/search",e,t)}sendMessage(e,t,n,s){this.connectorOptions.debugMode&&self.postMessage({request:{token:e,command:t,params:s},source:f,timestamp:Date.now()},"*"),this.instrumentation.callStart(t,n),this.ws.send(this.Parser.stringify({token:e,command:t,params:s}))}sendMessageOrError(e,t,n,s){try{this.sendMessage(e,t,s,n)}catch(t){v(d,t),this.events.emit(e,t,void 0,void 0)}}setStreamClosingState(e,t,n,s){if(Array.isArray(e)){const n=e.length;for(let s=0;s<n;s+=1){const{token:n}=e[s],i=this.closingStreams.get(n);this.waitingStreams.delete(i||""),this.closingStreams.set(n,t)}}else this.closingStreams.set(e.token,t);this.events.bind(t,n,(n,i,r,o)=>{this.removeStreamClosingState(e,t),s(i,r,o,t,n)})}stream(e,t,n){if(!this.isRunning)return n("Connection is down"),null;const s=N(e,t),i={command:e,encodedParams:D(t),token:s},r=(e,t,o,a)=>{t&&(this.events.unbind(s,r),this.instrumentation.callInfo(i.command,i,{error:t}),this.instrumentation.callEnd(e.command,e)),a&&3001===a.code&&(this.activeStreams.add(s),this.instrumentation.callInfo(i.command,i,{message:"stream active"})),n(t,o,a,s,e)};if(1===this.events.bind(s,i,r)){const n=this.closingStreams.get(s);n?this.addWaitingStream(n,s,e,t,i):this.sendMessageOrError(s,e,t,i)}else this.activeStreams.has(s)&&n(null,void 0,{code:3001},s,i);return{token:s,callback:r}}pause(e,t){return this.callCommand("pause",e,t)}}P.CONNECTED="connected",P.DISCONNECTED="disconnected";class I extends P{closeSubscriptions(e,t){return this.closeStreams(e,w(t))}getAndSubscribe(e,t,n){if(!E(e,t))return null;if(!O(n,t))return null;const s=k(n),i={query:e,start:s.start,end:s.end,versions:s.versions};return this.stream("getAndSubscribe",i,w(t))}getApps(e){const t={types:["app"]};return this.get("getDatasets",t,w(e))}getDatasets(e){const t={types:["app","device"]};return this.get("getDatasets",t,w(e))}getDevices(e){const t=["device"];return this.get("getDatasets",{types:t},w(e))}getCommandToken(e,t){return N(e,t)}getWithOptions(e,t,n){if("DEVICES_DATASET_ID"===e)return this.getDatasets(t),null;if(!E(e,t))return null;if(!O(n,t))return null;const{start:s,end:i,versions:r}=k(n),o={query:e,start:s,end:i,versions:r};return this.get("get",o,w(t,n))}runService(e,t){this.requestService(e,w(t))}runStreamingService(e,t){return e?this.stream("serviceRequest",e,w(t)):(t("`request` param cannot be empty"),null)}searchWithOptions(e,t,n){if(!E(e,t,!0))return null;if(!O(n,t))return null;const{start:s,end:i}=k(n),r=q(n),o={query:e,start:s,end:i,search:r.search,searchType:r.searchType};return this.search(o,w(t,n))}searchSubscribe(e,t,n={search:""}){if(!E(e,t,!0))return null;const s=q(n);return this.stream("alpha/searchSubscribe",{query:e,search:s.search,searchType:s.searchType},w(t))}subscribe(e,t){return E(e,t)?this.stream("subscribe",{query:e},w(t)):null}writeSync(e,t){this.publish({sync:!0,batch:{dataset:e.dataset,notifications:e.notifications}},function(e){return(t,n,s)=>{s&&1001===s.code?e(!0):t&&e(!1,`Error: ${t}\n`)}}(t))}}I.CLOSE="close",I.DEVICES_DATASET_ID="DEVICES_DATASET_ID",I.GET_DATASETS="getDatasets",I.GET="get",I.ID=f,I.PAUSE="pause",I.PUBLISH="publish",I.SEARCH_SUBSCRIBE="alpha/searchSubscribe",I.SEARCH="alpha/search",I.SUBSCRIBE="subscribe",Object.defineProperty(e,"Bool",{enumerable:!0,get:function(){return t.Bool}}),Object.defineProperty(e,"Float32",{enumerable:!0,get:function(){return t.Float32}}),Object.defineProperty(e,"Float64",{enumerable:!0,get:function(){return t.Float64}}),Object.defineProperty(e,"Int",{enumerable:!0,get:function(){return t.Int}}),Object.defineProperty(e,"NeatTypeSerializer",{enumerable:!0,get:function(){return t.NeatTypeSerializer}}),Object.defineProperty(e,"NeatTypes",{enumerable:!0,get:function(){return t.NeatTypes}}),Object.defineProperty(e,"Nil",{enumerable:!0,get:function(){return t.Nil}}),Object.defineProperty(e,"Pointer",{enumerable:!0,get:function(){return t.Pointer}}),Object.defineProperty(e,"Str",{enumerable:!0,get:function(){return t.Str}}),Object.defineProperty(e,"createBaseType",{enumerable:!0,get:function(){return t.createBaseType}}),Object.defineProperty(e,"isJsbi",{enumerable:!0,get:function(){return t.isJsbi}}),Object.defineProperty(e,"isNeatType",{enumerable:!0,get:function(){return t.isNeatType}}),e.ACTIVE_CODE=3001,e.CONNECTED="connected",e.DISCONNECTED="disconnected",e.EOF_CODE=1001,e.Parser=h,e.default=I,e.fromBinaryKey=function(e,s=!0){return t.decode(n.toByteArray(e),{extensionCodec:t.Codec,useJSBI:s})},e.hashObject=A,e.sanitizeOptions=k,e.toBinaryKey=D,Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -233,2 +233,3 @@ import { encode, Codec, decode } from 'a-msgpack'; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: {}, | ||
@@ -244,2 +245,3 @@ }; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: [], | ||
@@ -377,2 +379,3 @@ }; | ||
const WARN = 'WARN'; | ||
const EOF = 'EOF'; | ||
/** | ||
@@ -390,2 +393,3 @@ * Status code for EOF (End Of File). | ||
const PAUSED_CODE = 1002; | ||
const GET_REQUEST_COMPLETED = 'GetRequest'; | ||
const CLOSE = 'close'; | ||
@@ -395,2 +399,3 @@ const DEVICES_DATASET_ID = 'DEVICES_DATASET_ID'; | ||
const GET_DATASETS = 'getDatasets'; | ||
const GET_AND_SUBSCRIBE = 'getAndSubscribe'; | ||
const PAUSE = 'pause'; | ||
@@ -1145,2 +1150,3 @@ const PUBLISH = 'publish'; | ||
this.ws.onmessage = (event) => { | ||
var _a; | ||
if (typeof event.data !== 'string') { | ||
@@ -1187,2 +1193,6 @@ // messages that aren't strings are invalid | ||
this.events.emit(token, null, msg.result, status); | ||
if (((_a = msg.result) === null || _a === void 0 ? void 0 : _a.metadata) && | ||
msg.result.metadata[GET_REQUEST_COMPLETED] === EOF) { | ||
this.events.emit(token, null, null, { message: GET_REQUEST_COMPLETED, code: EOF_CODE }); | ||
} | ||
}; | ||
@@ -1405,20 +1415,3 @@ } | ||
}; | ||
const getToken = makeToken(GET, params); | ||
const subscribeAckCallback = (err, result, status, token) => { | ||
const subscribeNotifCallback = makeNotifCallback(callback); | ||
if (status && status.code === ACTIVE_CODE) { | ||
this.getWithOptions(query, callback, sanitizedOptions); | ||
} | ||
else { | ||
subscribeNotifCallback(err, result, status, token, { | ||
command: SUBSCRIBE, | ||
token: token || DEFAULT_CONTEXT.token, | ||
encodedParams: toBinaryKey(query), | ||
}); | ||
} | ||
}; | ||
return { | ||
subscribe: this.stream(SUBSCRIBE, { query }, subscribeAckCallback), | ||
get: getToken, | ||
}; | ||
return this.stream(GET_AND_SUBSCRIBE, params, makeNotifCallback(callback)); | ||
} | ||
@@ -1425,0 +1418,0 @@ /** |
@@ -238,2 +238,3 @@ 'use strict'; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: {}, | ||
@@ -249,2 +250,3 @@ }; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: [], | ||
@@ -382,2 +384,3 @@ }; | ||
const WARN = 'WARN'; | ||
const EOF = 'EOF'; | ||
/** | ||
@@ -395,2 +398,3 @@ * Status code for EOF (End Of File). | ||
const PAUSED_CODE = 1002; | ||
const GET_REQUEST_COMPLETED = 'GetRequest'; | ||
const CLOSE = 'close'; | ||
@@ -400,2 +404,3 @@ const DEVICES_DATASET_ID = 'DEVICES_DATASET_ID'; | ||
const GET_DATASETS = 'getDatasets'; | ||
const GET_AND_SUBSCRIBE = 'getAndSubscribe'; | ||
const PAUSE = 'pause'; | ||
@@ -1150,2 +1155,3 @@ const PUBLISH = 'publish'; | ||
this.ws.onmessage = (event) => { | ||
var _a; | ||
if (typeof event.data !== 'string') { | ||
@@ -1192,2 +1198,6 @@ // messages that aren't strings are invalid | ||
this.events.emit(token, null, msg.result, status); | ||
if (((_a = msg.result) === null || _a === void 0 ? void 0 : _a.metadata) && | ||
msg.result.metadata[GET_REQUEST_COMPLETED] === EOF) { | ||
this.events.emit(token, null, null, { message: GET_REQUEST_COMPLETED, code: EOF_CODE }); | ||
} | ||
}; | ||
@@ -1410,20 +1420,3 @@ } | ||
}; | ||
const getToken = makeToken(GET, params); | ||
const subscribeAckCallback = (err, result, status, token) => { | ||
const subscribeNotifCallback = makeNotifCallback(callback); | ||
if (status && status.code === ACTIVE_CODE) { | ||
this.getWithOptions(query, callback, sanitizedOptions); | ||
} | ||
else { | ||
subscribeNotifCallback(err, result, status, token, { | ||
command: SUBSCRIBE, | ||
token: token || DEFAULT_CONTEXT.token, | ||
encodedParams: toBinaryKey(query), | ||
}); | ||
} | ||
}; | ||
return { | ||
subscribe: this.stream(SUBSCRIBE, { query }, subscribeAckCallback), | ||
get: getToken, | ||
}; | ||
return this.stream(GET_AND_SUBSCRIBE, params, makeNotifCallback(callback)); | ||
} | ||
@@ -1430,0 +1423,0 @@ /** |
{ | ||
"name": "cloudvision-connector", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"description": "A module to communicate with the CloudVision API server", | ||
@@ -53,3 +53,3 @@ "author": "extensions@arista.com", | ||
"dependencies": { | ||
"a-msgpack": "4.4.0", | ||
"a-msgpack": "4.5.0", | ||
"base64-js": "1.3.1", | ||
@@ -81,5 +81,5 @@ "imurmurhash": "0.1.4", | ||
"rimraf": "3.0.2", | ||
"rollup": "2.21.0", | ||
"rollup": "2.23.0", | ||
"rollup-plugin-terser": "5.3.0", | ||
"ts-jest": "26.1.2", | ||
"ts-jest": "26.1.3", | ||
"tslib": "2.0.0", | ||
@@ -89,5 +89,5 @@ "typedoc": "0.17.8", | ||
"typedoc-plugin-markdown": "2.2.17", | ||
"typescript": "3.9.6" | ||
"typescript": "3.9.7" | ||
}, | ||
"gitHead": "f234605a98f4df8bfa7ffd6de950d440b6351bdf" | ||
"gitHead": "93c46d3900b4540ed59aa47151d91008aef2a1d5" | ||
} |
import { | ||
AppParams, | ||
CloudVisionBatchedResult, | ||
CloudVisionParams, | ||
CloudVisionResult, | ||
CloudVisionServiceResult, | ||
CloudVisionStatus, | ||
DatasetType, | ||
@@ -24,9 +20,8 @@ NotifCallback, | ||
import { | ||
ACTIVE_CODE, | ||
APP_DATASET_TYPE, | ||
CLOSE, | ||
DEFAULT_CONTEXT, | ||
DEVICES_DATASET_ID, | ||
DEVICE_DATASET_TYPE, | ||
GET, | ||
GET_AND_SUBSCRIBE, | ||
GET_DATASETS, | ||
@@ -38,4 +33,4 @@ ID, | ||
SEARCH_SUBSCRIBE, | ||
SERVICE_REQUEST, | ||
SUBSCRIBE, | ||
SERVICE_REQUEST, | ||
} from './constants'; | ||
@@ -48,3 +43,2 @@ import { | ||
sanitizeSearchOptions, | ||
toBinaryKey, | ||
validateOptions, | ||
@@ -153,6 +147,3 @@ validateQuery, | ||
options: Options, | ||
): { | ||
subscribe: SubscriptionIdentifier | null; | ||
get: string; | ||
} | null { | ||
): SubscriptionIdentifier | null { | ||
if (!validateQuery(query, callback)) { | ||
@@ -174,26 +165,4 @@ return null; | ||
}; | ||
const getToken = makeToken(GET, params); | ||
const subscribeAckCallback = ( | ||
err: string | null, | ||
result?: CloudVisionBatchedResult | CloudVisionResult | CloudVisionServiceResult, | ||
status?: CloudVisionStatus, | ||
token?: string, | ||
): void => { | ||
const subscribeNotifCallback = makeNotifCallback(callback); | ||
if (status && status.code === ACTIVE_CODE) { | ||
this.getWithOptions(query, callback, sanitizedOptions); | ||
} else { | ||
subscribeNotifCallback(err, result, status, token, { | ||
command: SUBSCRIBE, | ||
token: token || DEFAULT_CONTEXT.token, | ||
encodedParams: toBinaryKey(query), | ||
}); | ||
} | ||
}; | ||
return { | ||
subscribe: this.stream(SUBSCRIBE, { query }, subscribeAckCallback), | ||
get: getToken, | ||
}; | ||
return this.stream(GET_AND_SUBSCRIBE, params, makeNotifCallback(callback)); | ||
} | ||
@@ -200,0 +169,0 @@ |
@@ -46,2 +46,4 @@ // Copyright (c) 2018, Arista Networks, Inc. | ||
export const GET_REQUEST_COMPLETED = 'GetRequest'; | ||
export const CLOSE = 'close'; | ||
@@ -51,2 +53,3 @@ export const DEVICES_DATASET_ID = 'DEVICES_DATASET_ID'; | ||
export const GET_DATASETS = 'getDatasets'; | ||
export const GET_AND_SUBSCRIBE = 'getAndSubscribe'; | ||
export const PAUSE = 'pause'; | ||
@@ -53,0 +56,0 @@ export const PUBLISH = 'publish'; |
@@ -299,2 +299,3 @@ // Copyright (c) 2018, Arista Networks, Inc. | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: {}, | ||
@@ -312,2 +313,3 @@ }; | ||
dataset: result.dataset, | ||
metadata: result.metadata || {}, | ||
notifications: [], | ||
@@ -314,0 +316,0 @@ }; |
@@ -22,2 +22,3 @@ // Copyright (c) 2018, Arista Networks, Inc. | ||
CloudVisionMessage, | ||
CloudVisionMetaData, | ||
CloudVisionParams, | ||
@@ -47,4 +48,6 @@ CloudVisionPublishRequest, | ||
DISCONNECTED, | ||
EOF, | ||
EOF_CODE, | ||
ERROR, | ||
GET_REQUEST_COMPLETED, | ||
ID, | ||
@@ -513,3 +516,10 @@ PAUSE, | ||
} | ||
this.events.emit(token, null, msg.result, status); | ||
if ( | ||
msg.result?.metadata && | ||
(msg.result.metadata as CloudVisionMetaData<string>)[GET_REQUEST_COMPLETED] === EOF | ||
) { | ||
this.events.emit(token, null, null, { message: GET_REQUEST_COMPLETED, code: EOF_CODE }); | ||
} | ||
}; | ||
@@ -516,0 +526,0 @@ } |
@@ -83,6 +83,3 @@ import { CloudVisionParams, NotifCallback, Options, PublishCallback, PublishRequest, Query, SearchOptions, ServiceRequest, SubscriptionIdentifier, WsCommand } from '../types'; | ||
*/ | ||
getAndSubscribe(query: Query, callback: NotifCallback, options: Options): { | ||
subscribe: SubscriptionIdentifier | null; | ||
get: string; | ||
} | null; | ||
getAndSubscribe(query: Query, callback: NotifCallback, options: Options): SubscriptionIdentifier | null; | ||
/** | ||
@@ -89,0 +86,0 @@ * Returns a list of apps (datasets with type == 'app'). |
@@ -22,2 +22,3 @@ import { RequestContext, SearchType } from '../types'; | ||
export declare const RESPONSE_COMPLETED = "RESPONSE_COMPLETED"; | ||
export declare const GET_REQUEST_COMPLETED = "GetRequest"; | ||
export declare const CLOSE = "close"; | ||
@@ -27,2 +28,3 @@ export declare const DEVICES_DATASET_ID = "DEVICES_DATASET_ID"; | ||
export declare const GET_DATASETS = "getDatasets"; | ||
export declare const GET_AND_SUBSCRIBE = "getAndSubscribe"; | ||
export declare const PAUSE = "pause"; | ||
@@ -29,0 +31,0 @@ export declare const PUBLISH = "publish"; |
@@ -5,2 +5,3 @@ import { PathElements, Timestamp } from 'a-msgpack'; | ||
export interface CloudVisionDatasets { | ||
metadata: CloudVisionMetaData<unknown>; | ||
datasets: DatasetObject[]; | ||
@@ -33,2 +34,5 @@ } | ||
} | ||
export interface CloudVisionMetaData<V> { | ||
[key: string]: V; | ||
} | ||
/** @deprecated: Use `CloudVisionDeletes`. */ | ||
@@ -41,2 +45,3 @@ export declare type CloudVisionDelete<K> = CloudVisionDeletes<K>; | ||
dataset: DatasetObject; | ||
metadata: CloudVisionMetaData<unknown>; | ||
notifications: ConvertedNotification[]; | ||
@@ -46,2 +51,3 @@ } | ||
dataset: DatasetObject; | ||
metadata?: CloudVisionMetaData<unknown>; | ||
notifications: RawNotification[]; | ||
@@ -51,2 +57,3 @@ } | ||
dataset: DatasetObject; | ||
metadata: CloudVisionMetaData<unknown>; | ||
notifications: { | ||
@@ -53,0 +60,0 @@ [path: string]: ConvertedNotification[]; |
import { NeatType, PathElements } from 'a-msgpack'; | ||
import { APP_DATASET_TYPE, CLOSE, DEVICE_DATASET_TYPE, GET_DATASETS, GET, PAUSE, PUBLISH, RESUME, SEARCH_SUBSCRIBE, SEARCH_TYPE_ANY, SEARCH_TYPE_IP, SEARCH_TYPE_MAC, SEARCH, SERVICE_REQUEST, SUBSCRIBE } from '../src/constants'; | ||
import { APP_DATASET_TYPE, CLOSE, DEVICE_DATASET_TYPE, GET, GET_AND_SUBSCRIBE, GET_DATASETS, PAUSE, PUBLISH, RESUME, SEARCH, SEARCH_SUBSCRIBE, SEARCH_TYPE_ANY, SEARCH_TYPE_IP, SEARCH_TYPE_MAC, SERVICE_REQUEST, SUBSCRIBE } from '../src/constants'; | ||
export declare type GetCommand = typeof GET | typeof GET_DATASETS | typeof SEARCH; | ||
/** @deprecated: Use `GetCommand`. */ | ||
export declare type GetCommands = GetCommand; | ||
export declare type StreamCommand = typeof SERVICE_REQUEST | typeof SEARCH_SUBSCRIBE | typeof SUBSCRIBE; | ||
export declare type StreamCommand = typeof SERVICE_REQUEST | typeof SEARCH_SUBSCRIBE | typeof SUBSCRIBE | typeof GET_AND_SUBSCRIBE; | ||
/** @deprecated: Use `StreamCommand`. */ | ||
@@ -8,0 +8,0 @@ export declare type StreamCommands = StreamCommand; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14
349083
7897
+ Addeda-msgpack@4.5.0(transitive)
- Removeda-msgpack@4.4.0(transitive)
Updateda-msgpack@4.5.0