UNPKG

@sassoftware/esp-connect

Version:

Package used to connect to an ESP server (version 6.2+)

314 lines (272 loc) 7.97 kB
/* Copyright © 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ import {Connection} from "./connection.js"; import {Options} from "./options.js"; import {v7} from "./v7.js"; import {tools} from "./tools.js"; import {ajax} from "./ajax.js"; import {xpath} from "./xpath.js"; class ServerConnection extends Connection { constructor(connect,host,port,path,secure,delegate,options) { super(host,port,path,secure,options,connect.config); this._connect = connect; this._delegates = []; if (delegate != null) { if (Array.isArray(delegate)) { delegate.forEach((d) => { tools.addTo(this._delegates,d); }); } else { tools.addTo(this._delegates,delegate); } } this._impl = null; } get connect() { return(this._connect); } handshakeComplete() { var version = this.getHeader("version"); if (version != null) { this._impl = new v7(this,this.getOpts()); } if (this._impl != null) { this._impl.version = version; this._delegates.forEach((d) => { if (tools.supports(d,"ready")) { d.ready(this._impl); } }); } } load(model) { return(new Promise((resolve,reject) => { var opts = new Options(model); var options = model.hasOwnProperty("options") ? model.options : {}; var parms = model.hasOwnProperty("parms") ? model.parms : {}; var env = model.hasOwnProperty("env") ? model.env : {}; if (opts.getOpt("force",false)) { this._impl.loadProject(model.name,model.data,options,parms,env).then( function() { resolve(); }, function(result) { reject(result); } ); } else { const self = this; this._impl.getProjectXml(model.name).then( function(xml) { var node = null; if (xml.nodeType == 1) { node = xml; } else if (xml.nodeType == 9) { node = xml.documentElement; } if (node == null) { self._impl.loadProject(model.name,model.data,options,parms,env).then( function() { resolve(); }, function(result) { reject(result); } ); } else { node.removeAttribute("name"); const s = xpath.xmlString(node); if (model.data != s) { self._impl.loadProject(model.name,model.data,options,parms,env).then( function() { resolve(); }, function(result) { reject(result); } ); } else { resolve(); } } } ); } })); } closed(e) { if (this._impl != null) { this._delegates.forEach((d) => { if (tools.supports(d,"closed")) { d.closed(this._impl); } }); const closed = this._impl.closed; if (this._impl != null) { this._impl = null; } if (closed == false) { var reconnect = this.getOpt("reconnect",1); if (reconnect > 0) { this.reconnect(reconnect); } } } } error(e) { if (this._impl != null && this._impl.closed) { return; } if (tools.established(this.getUrl()) == false) { if (tools.isNode) { if (this.isSecure) { var msg = "\n\nconnection to " + this.getUrl() + " failed. If you are using self-signed certificates you may need to\n\nexport NODE_TLS_REJECT_UNAUTHORIZED=0\n\nin your environment\n\n"; throw new Error(msg); } } else if (this.isSecure) { var url = this.httpurlBase; url += "/eventStreamProcessing/v1/server"; window.open(url); } } this._delegates.forEach((d) => { if (tools.supports(d,"connectionError")) { d.connectionError(this); } }); var reconnect = this.getOpt("reconnect",5); if (reconnect > 0) { this.reconnect(reconnect); } } message(data) { if (this.isHandshakeComplete() == false) { super.message(data); return; } if (this._impl != null) { this._impl.message(data); } } data(o) { if (this._impl != null) { this._impl.data(o); } } addDelegate(delegate) { tools.addTo(this._delegates,delegate); if (this._impl != null && tools.supports(delegate,"ready")) { delegate.ready(this._impl); } } removeDelegate(delegate) { tools.removeFrom(this._delegates,delegate); } getUrl() { var url = ""; url += this.protocol; url += "://"; url += this.host; url += ":"; url += this.port; if (this._path != null && this._path.length > 0) { url += this._path; } if (url.endsWith("/") == false) { url += "/"; } url += "eventStreamProcessing/v1/connect"; var parms = ""; var threads = ""; if (this.hasOpt("access_token")) { parms += "access_token=" + this.getOpt("access_token"); } if (this.hasOpt("threadin")) { threads += "incoming"; } if (this.hasOpt("threadout")) { if (threads.length > 0) { threads += ","; } threads += "outgoing"; } if (threads.length > 0) { if (parms.length > 0) { parms += "&"; } parms += "_threads=" + threads; } if (parms.length > 0) { url += "?" + parms; } return(url); } } var _api = { create:function(connect,url,delegate,options) { var u = tools.createUrl(decodeURI(url)); return(new ServerConnection(connect,u["host"],u["port"],u["path"],u["secure"],delegate,options)); } }; export {_api as serverconn}