@kitware/wslink 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc.js +8 -0
- package/CHANGELOG.md +551 -0
- package/LICENSE +27 -0
- package/README.md +36 -0
- package/dist/wslink.mjs +1474 -0
- package/dist/wslink.umd.js +1 -0
- package/package.json +33 -0
- package/src/CompositeClosureHelper/index.js +166 -0
- package/src/ProcessLauncher/api.md +87 -0
- package/src/ProcessLauncher/index.js +125 -0
- package/src/SmartConnect/api.md +40 -0
- package/src/SmartConnect/index.d.ts +72 -0
- package/src/SmartConnect/index.js +179 -0
- package/src/WebsocketConnection/api.md +37 -0
- package/src/WebsocketConnection/chunking.js +211 -0
- package/src/WebsocketConnection/chunking.ts +296 -0
- package/src/WebsocketConnection/index.d.ts +70 -0
- package/src/WebsocketConnection/index.js +155 -0
- package/src/WebsocketConnection/session.js +284 -0
- package/src/index.js +11 -0
- package/test/simple.js +185 -0
- package/vite.config.js +15 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(x,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(x=typeof globalThis<"u"?globalThis:x||self,g(x.wslink={}))})(this,(function(x){"use strict";function g(n){return n.charAt(0).toUpperCase()+n.slice(1)}function he(n,e={},t=null){e.isA||(e.isA=[]),t&&e.isA.push(t),n.isA||(n.isA=r=>e.isA.indexOf(r)!==-1)}function ue(n,e={},t=[]){t.forEach(r=>{n[`set${g(r)}`]=s=>{e[r]=s}})}function de(n,e={},t=[]){t.forEach(r=>{n[`get${g(r)}`]=()=>e[r]})}function le(n,e={}){const t=n.destroy;e.subscriptions||(e.subscriptions=[]),n.destroy=()=>{for(t&&t();e.subscriptions&&e.subscriptions.length;)e.subscriptions.pop().unsubscribe();Object.keys(e).forEach(r=>{delete e[r]}),e.deleted=!0}}function pe(n,e,t,r=!0){const s=[],i=n.destroy;function o(h){s[h]=null}function c(h){function a(){o(h)}return Object.freeze({unsubscribe:a})}n[`fire${g(t)}`]=(...h)=>{if(e.deleted){console.log("instance deleted - can not call any method");return}function a(){s.forEach(l=>{if(l)try{l.apply(n,h)}catch(p){console.log("Error event:",t,p)}})}r?setTimeout(a,0):a()},n[`on${g(t)}`]=h=>{if(e.deleted)return console.log("instance deleted - can not call any method"),null;const a=s.length;return s.push(h),c(a)},n.destroy=()=>{i(),s.forEach((h,a)=>o(a))}}function we(...n){return(...e)=>n.filter(t=>!!t).forEach(t=>t(...e))}function ve(n){return(e={})=>{const t={},r={};return n(r,t,e),Object.freeze(r)}}const w={chain:we,destroy:le,event:pe,get:de,isA:he,newInstance:ve,set:ue},X=[];function ye(n,e){n.start=t=>{const r=new XMLHttpRequest,s=e.endPoint;e._retry||(e._retry=t.launcherRetry||[]),r.open("POST",s,!0),t.headers&&(e._headers=t.headers,delete t.headers),e._headers&&Object.entries(e._headers).forEach(([o,c])=>r.setRequestHeader(o,c)),r.responseType="json";const i="response"in r&&r.responseType==="json";r.onload=o=>{const c=i?r.response:JSON.parse(r.response);if(r.status===200&&c&&!c.error){X.push(c),n.fireProcessReady(c);return}else if(r.status===503&&e._retry.length>0){const h=e._retry.shift();setTimeout(n.start,h,t)}else n.fireError(r)},r.onerror=o=>{n.fireError(r)},r.send(JSON.stringify(t))},n.fetchConnection=t=>{var r=new XMLHttpRequest,s=[e.endPoint,t].join("/");r.open("GET",s,!0),r.responseType="json";const i="response"in r&&r.responseType==="json";r.onload=o=>{if(this.status===200){n.fireFetch(i?r.response:JSON.parse(r.response));return}n.fireError(r)},r.onerror=o=>{n.fireError(r)},r.send()},n.stop=t=>{var r=new XMLHttpRequest,s=[e.endPoint,t.id].join("/");r.open("DELETE",s,!0),r.responseType="json";const i="response"in r&&r.responseType==="json";r.onload=o=>{if(this.status===200){const c=i?r.response:JSON.parse(r.response);n.fireProcessStopped(c);return}n.fireError(r)},r.onerror=o=>{n.fireError(r)},r.send()},n.listConnections=()=>X}const xe={endPoint:null};function m(n,e,t={}){Object.assign(e,xe,t),w.destroy(n,e),w.event(n,e,"ProcessReady"),w.event(n,e,"ProcessStopped"),w.event(n,e,"Fetch"),w.event(n,e,"Error"),w.isA(n,e,"ProcessLauncher"),ye(n,e)}const K={newInstance:w.newInstance(m),extend:m},k=4,z=0,J=k,F=z+J,q=k,Y=F+q,ge=k,S=J+q+ge;function Ee(n,e,t){const r=new ArrayBuffer(S),s=new Uint8Array(r),i=new DataView(r);return i.setUint32(z,n,!0),i.setUint32(F,e,!0),i.setUint32(Y,t,!0),s}function Ue(n){const e=new DataView(n.buffer),t=e.getUint32(z,!0),r=e.getUint32(F,!0),s=e.getUint32(Y,!0);return{id:t,offset:r,size:s}}function*Z(n,e){const t=n.byteLength;let r;e===0?r=t:r=Math.max(e-S,1);const s=new Uint32Array(1);crypto.getRandomValues(s);let i=0;for(;i<t;){const o=Math.min(r,t-i),c=new Uint8Array(new ArrayBuffer(S+o)),h=Ee(s[0],i,t);c.set(new Uint8Array(h.buffer),0),c.set(n.subarray(i,i+o),S),yield c,i+=o}}class Se{pendingMessages;constructor(){this.pendingMessages={}}releasePendingMessages(){this.pendingMessages={}}async processChunk(e,t){const r=e.slice(0,S),s=e.slice(S),i=new Uint8Array(await r.arrayBuffer()),{id:o,offset:c,size:h}=Ue(i);let a=this.pendingMessages[o];if(a||(a={receivedSize:0,content:new Uint8Array(h),decoder:t()},this.pendingMessages[o]=a),h!==a.content.byteLength)throw delete this.pendingMessages[o],new Error(`Total size in chunk header for message ${o} does not match total size declared by previous chunk.`);const l=new Uint8Array(await s.arrayBuffer()),p=a.content;if(p.set(l,c),a.receivedSize+=l.byteLength,a.receivedSize>=h){delete this.pendingMessages[o];try{return a.decoder.decode(p)}catch{console.error("Malformed message: ",p.slice(0,100))}}}}var U=4294967295;function Te(n,e,t){var r=t/4294967296,s=t;n.setUint32(e,r),n.setUint32(e+4,s)}function Q(n,e,t){var r=Math.floor(t/4294967296),s=t;n.setUint32(e,r),n.setUint32(e+4,s)}function b(n,e){var t=n.getInt32(e),r=n.getUint32(e+4);return t*4294967296+r}function _e(n,e){var t=n.getUint32(e),r=n.getUint32(e+4);return t*4294967296+r}var $,A,B,R=(typeof process>"u"||(($=process==null?void 0:process.env)===null||$===void 0?void 0:$.TEXT_ENCODING)!=="never")&&typeof TextEncoder<"u"&&typeof TextDecoder<"u";function P(n){for(var e=n.length,t=0,r=0;r<e;){var s=n.charCodeAt(r++);if((s&4294967168)===0){t++;continue}else if((s&4294965248)===0)t+=2;else{if(s>=55296&&s<=56319&&r<e){var i=n.charCodeAt(r);(i&64512)===56320&&(++r,s=((s&1023)<<10)+(i&1023)+65536)}(s&4294901760)===0?t+=3:t+=4}}return t}function Ce(n,e,t){for(var r=n.length,s=t,i=0;i<r;){var o=n.charCodeAt(i++);if((o&4294967168)===0){e[s++]=o;continue}else if((o&4294965248)===0)e[s++]=o>>6&31|192;else{if(o>=55296&&o<=56319&&i<r){var c=n.charCodeAt(i);(c&64512)===56320&&(++i,o=((o&1023)<<10)+(c&1023)+65536)}(o&4294901760)===0?(e[s++]=o>>12&15|224,e[s++]=o>>6&63|128):(e[s++]=o>>18&7|240,e[s++]=o>>12&63|128,e[s++]=o>>6&63|128)}e[s++]=o&63|128}}var _=R?new TextEncoder:void 0,De=R?typeof process<"u"&&((A=process==null?void 0:process.env)===null||A===void 0?void 0:A.TEXT_ENCODING)!=="force"?200:0:U;function Re(n,e,t){e.set(_.encode(n),t)}function Me(n,e,t){_.encodeInto(n,e.subarray(t))}var Le=_?.encodeInto?Me:Re,Oe=4096;function j(n,e,t){for(var r=e,s=r+t,i=[],o="";r<s;){var c=n[r++];if((c&128)===0)i.push(c);else if((c&224)===192){var h=n[r++]&63;i.push((c&31)<<6|h)}else if((c&240)===224){var h=n[r++]&63,a=n[r++]&63;i.push((c&31)<<12|h<<6|a)}else if((c&248)===240){var h=n[r++]&63,a=n[r++]&63,l=n[r++]&63,p=(c&7)<<18|h<<12|a<<6|l;p>65535&&(p-=65536,i.push(p>>>10&1023|55296),p=56320|p&1023),i.push(p)}else i.push(c);i.length>=Oe&&(o+=String.fromCharCode.apply(String,i),i.length=0)}return i.length>0&&(o+=String.fromCharCode.apply(String,i)),o}var ke=R?new TextDecoder:null,ze=R?typeof process<"u"&&((B=process==null?void 0:process.env)===null||B===void 0?void 0:B.TEXT_DECODER)!=="force"?200:0:U;function Fe(n,e,t){var r=n.subarray(e,e+t);return ke.decode(r)}var M=(function(){function n(e,t){this.type=e,this.data=t}return n})(),$e=(function(){var n=function(e,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(r,s){r.__proto__=s}||function(r,s){for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(r[i]=s[i])},n(e,t)};return function(e,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");n(e,t);function r(){this.constructor=e}e.prototype=t===null?Object.create(t):(r.prototype=t.prototype,new r)}})(),y=(function(n){$e(e,n);function e(t){var r=n.call(this,t)||this,s=Object.create(e.prototype);return Object.setPrototypeOf(r,s),Object.defineProperty(r,"name",{configurable:!0,enumerable:!1,value:e.name}),r}return e})(Error),Ae=-1,Be=4294967296-1,Ie=17179869184-1;function He(n){var e=n.sec,t=n.nsec;if(e>=0&&t>=0&&e<=Ie)if(t===0&&e<=Be){var r=new Uint8Array(4),s=new DataView(r.buffer);return s.setUint32(0,e),r}else{var i=e/4294967296,o=e&4294967295,r=new Uint8Array(8),s=new DataView(r.buffer);return s.setUint32(0,t<<2|i&3),s.setUint32(4,o),r}else{var r=new Uint8Array(12),s=new DataView(r.buffer);return s.setUint32(0,t),Q(s,4,e),r}}function Ne(n){var e=n.getTime(),t=Math.floor(e/1e3),r=(e-t*1e3)*1e6,s=Math.floor(r/1e9);return{sec:t+s,nsec:r-s*1e9}}function Ve(n){if(n instanceof Date){var e=Ne(n);return He(e)}else return null}function We(n){var e=new DataView(n.buffer,n.byteOffset,n.byteLength);switch(n.byteLength){case 4:{var t=e.getUint32(0),r=0;return{sec:t,nsec:r}}case 8:{var s=e.getUint32(0),i=e.getUint32(4),t=(s&3)*4294967296+i,r=s>>>2;return{sec:t,nsec:r}}case 12:{var t=b(e,4),r=e.getUint32(0);return{sec:t,nsec:r}}default:throw new y("Unrecognized data size for timestamp (expected 4, 8, or 12): ".concat(n.length))}}function Ge(n){var e=We(n);return new Date(e.sec*1e3+e.nsec/1e6)}var Xe={type:Ae,encode:Ve,decode:Ge},ee=(function(){function n(){this.builtInEncoders=[],this.builtInDecoders=[],this.encoders=[],this.decoders=[],this.register(Xe)}return n.prototype.register=function(e){var t=e.type,r=e.encode,s=e.decode;if(t>=0)this.encoders[t]=r,this.decoders[t]=s;else{var i=1+t;this.builtInEncoders[i]=r,this.builtInDecoders[i]=s}},n.prototype.tryToEncode=function(e,t){for(var r=0;r<this.builtInEncoders.length;r++){var s=this.builtInEncoders[r];if(s!=null){var i=s(e,t);if(i!=null){var o=-1-r;return new M(o,i)}}}for(var r=0;r<this.encoders.length;r++){var s=this.encoders[r];if(s!=null){var i=s(e,t);if(i!=null){var o=r;return new M(o,i)}}}return e instanceof M?e:null},n.prototype.decode=function(e,t,r){var s=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return s?s(e,t,r):new M(t,e)},n.defaultCodec=new n,n})();function L(n){return n instanceof Uint8Array?n:ArrayBuffer.isView(n)?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):n instanceof ArrayBuffer?new Uint8Array(n):Uint8Array.from(n)}function me(n){if(n instanceof ArrayBuffer)return new DataView(n);var e=L(n);return new DataView(e.buffer,e.byteOffset,e.byteLength)}var Ke=100,Je=2048,qe=(function(){function n(e,t,r,s,i,o,c,h){e===void 0&&(e=ee.defaultCodec),t===void 0&&(t=void 0),r===void 0&&(r=Ke),s===void 0&&(s=Je),i===void 0&&(i=!1),o===void 0&&(o=!1),c===void 0&&(c=!1),h===void 0&&(h=!1),this.extensionCodec=e,this.context=t,this.maxDepth=r,this.initialBufferSize=s,this.sortKeys=i,this.forceFloat32=o,this.ignoreUndefined=c,this.forceIntegerToFloat=h,this.pos=0,this.view=new DataView(new ArrayBuffer(this.initialBufferSize)),this.bytes=new Uint8Array(this.view.buffer)}return n.prototype.reinitializeState=function(){this.pos=0},n.prototype.encodeSharedRef=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.subarray(0,this.pos)},n.prototype.encode=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.slice(0,this.pos)},n.prototype.doEncode=function(e,t){if(t>this.maxDepth)throw new Error("Too deep objects in depth ".concat(t));e==null?this.encodeNil():typeof e=="boolean"?this.encodeBoolean(e):typeof e=="number"?this.encodeNumber(e):typeof e=="string"?this.encodeString(e):this.encodeObject(e,t)},n.prototype.ensureBufferSizeToWrite=function(e){var t=this.pos+e;this.view.byteLength<t&&this.resizeBuffer(t*2)},n.prototype.resizeBuffer=function(e){var t=new ArrayBuffer(e),r=new Uint8Array(t),s=new DataView(t);r.set(this.bytes),this.view=s,this.bytes=r},n.prototype.encodeNil=function(){this.writeU8(192)},n.prototype.encodeBoolean=function(e){e===!1?this.writeU8(194):this.writeU8(195)},n.prototype.encodeNumber=function(e){Number.isSafeInteger(e)&&!this.forceIntegerToFloat?e>=0?e<128?this.writeU8(e):e<256?(this.writeU8(204),this.writeU8(e)):e<65536?(this.writeU8(205),this.writeU16(e)):e<4294967296?(this.writeU8(206),this.writeU32(e)):(this.writeU8(207),this.writeU64(e)):e>=-32?this.writeU8(224|e+32):e>=-128?(this.writeU8(208),this.writeI8(e)):e>=-32768?(this.writeU8(209),this.writeI16(e)):e>=-2147483648?(this.writeU8(210),this.writeI32(e)):(this.writeU8(211),this.writeI64(e)):this.forceFloat32?(this.writeU8(202),this.writeF32(e)):(this.writeU8(203),this.writeF64(e))},n.prototype.writeStringHeader=function(e){if(e<32)this.writeU8(160+e);else if(e<256)this.writeU8(217),this.writeU8(e);else if(e<65536)this.writeU8(218),this.writeU16(e);else if(e<4294967296)this.writeU8(219),this.writeU32(e);else throw new Error("Too long string: ".concat(e," bytes in UTF-8"))},n.prototype.encodeString=function(e){var t=5,r=e.length;if(r>De){var s=P(e);this.ensureBufferSizeToWrite(t+s),this.writeStringHeader(s),Le(e,this.bytes,this.pos),this.pos+=s}else{var s=P(e);this.ensureBufferSizeToWrite(t+s),this.writeStringHeader(s),Ce(e,this.bytes,this.pos),this.pos+=s}},n.prototype.encodeObject=function(e,t){var r=this.extensionCodec.tryToEncode(e,this.context);if(r!=null)this.encodeExtension(r);else if(Array.isArray(e))this.encodeArray(e,t);else if(ArrayBuffer.isView(e))this.encodeBinary(e);else if(typeof e=="object")this.encodeMap(e,t);else throw new Error("Unrecognized object: ".concat(Object.prototype.toString.apply(e)))},n.prototype.encodeBinary=function(e){var t=e.byteLength;if(t<256)this.writeU8(196),this.writeU8(t);else if(t<65536)this.writeU8(197),this.writeU16(t);else if(t<4294967296)this.writeU8(198),this.writeU32(t);else throw new Error("Too large binary: ".concat(t));var r=L(e);this.writeU8a(r)},n.prototype.encodeArray=function(e,t){var r=e.length;if(r<16)this.writeU8(144+r);else if(r<65536)this.writeU8(220),this.writeU16(r);else if(r<4294967296)this.writeU8(221),this.writeU32(r);else throw new Error("Too large array: ".concat(r));for(var s=0,i=e;s<i.length;s++){var o=i[s];this.doEncode(o,t+1)}},n.prototype.countWithoutUndefined=function(e,t){for(var r=0,s=0,i=t;s<i.length;s++){var o=i[s];e[o]!==void 0&&r++}return r},n.prototype.encodeMap=function(e,t){var r=Object.keys(e);this.sortKeys&&r.sort();var s=this.ignoreUndefined?this.countWithoutUndefined(e,r):r.length;if(s<16)this.writeU8(128+s);else if(s<65536)this.writeU8(222),this.writeU16(s);else if(s<4294967296)this.writeU8(223),this.writeU32(s);else throw new Error("Too large map object: ".concat(s));for(var i=0,o=r;i<o.length;i++){var c=o[i],h=e[c];this.ignoreUndefined&&h===void 0||(this.encodeString(c),this.doEncode(h,t+1))}},n.prototype.encodeExtension=function(e){var t=e.data.length;if(t===1)this.writeU8(212);else if(t===2)this.writeU8(213);else if(t===4)this.writeU8(214);else if(t===8)this.writeU8(215);else if(t===16)this.writeU8(216);else if(t<256)this.writeU8(199),this.writeU8(t);else if(t<65536)this.writeU8(200),this.writeU16(t);else if(t<4294967296)this.writeU8(201),this.writeU32(t);else throw new Error("Too large extension object: ".concat(t));this.writeI8(e.type),this.writeU8a(e.data)},n.prototype.writeU8=function(e){this.ensureBufferSizeToWrite(1),this.view.setUint8(this.pos,e),this.pos++},n.prototype.writeU8a=function(e){var t=e.length;this.ensureBufferSizeToWrite(t),this.bytes.set(e,this.pos),this.pos+=t},n.prototype.writeI8=function(e){this.ensureBufferSizeToWrite(1),this.view.setInt8(this.pos,e),this.pos++},n.prototype.writeU16=function(e){this.ensureBufferSizeToWrite(2),this.view.setUint16(this.pos,e),this.pos+=2},n.prototype.writeI16=function(e){this.ensureBufferSizeToWrite(2),this.view.setInt16(this.pos,e),this.pos+=2},n.prototype.writeU32=function(e){this.ensureBufferSizeToWrite(4),this.view.setUint32(this.pos,e),this.pos+=4},n.prototype.writeI32=function(e){this.ensureBufferSizeToWrite(4),this.view.setInt32(this.pos,e),this.pos+=4},n.prototype.writeF32=function(e){this.ensureBufferSizeToWrite(4),this.view.setFloat32(this.pos,e),this.pos+=4},n.prototype.writeF64=function(e){this.ensureBufferSizeToWrite(8),this.view.setFloat64(this.pos,e),this.pos+=8},n.prototype.writeU64=function(e){this.ensureBufferSizeToWrite(8),Te(this.view,this.pos,e),this.pos+=8},n.prototype.writeI64=function(e){this.ensureBufferSizeToWrite(8),Q(this.view,this.pos,e),this.pos+=8},n})();function I(n){return"".concat(n<0?"-":"","0x").concat(Math.abs(n).toString(16).padStart(2,"0"))}var Ye=16,Ze=16,Qe=(function(){function n(e,t){e===void 0&&(e=Ye),t===void 0&&(t=Ze),this.maxKeyLength=e,this.maxLengthPerKey=t,this.hit=0,this.miss=0,this.caches=[];for(var r=0;r<this.maxKeyLength;r++)this.caches.push([])}return n.prototype.canBeCached=function(e){return e>0&&e<=this.maxKeyLength},n.prototype.find=function(e,t,r){var s=this.caches[r-1];e:for(var i=0,o=s;i<o.length;i++){for(var c=o[i],h=c.bytes,a=0;a<r;a++)if(h[a]!==e[t+a])continue e;return c.str}return null},n.prototype.store=function(e,t){var r=this.caches[e.length-1],s={bytes:e,str:t};r.length>=this.maxLengthPerKey?r[Math.random()*r.length|0]=s:r.push(s)},n.prototype.decode=function(e,t,r){var s=this.find(e,t,r);if(s!=null)return this.hit++,s;this.miss++;var i=j(e,t,r),o=Uint8Array.prototype.slice.call(e,t,t+r);return this.store(o,i),i},n})(),be=function(n,e,t,r){function s(i){return i instanceof t?i:new t(function(o){o(i)})}return new(t||(t=Promise))(function(i,o){function c(l){try{a(r.next(l))}catch(p){o(p)}}function h(l){try{a(r.throw(l))}catch(p){o(p)}}function a(l){l.done?i(l.value):s(l.value).then(c,h)}a((r=r.apply(n,e||[])).next())})},H=function(n,e){var t={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},r,s,i,o;return o={next:c(0),throw:c(1),return:c(2)},typeof Symbol=="function"&&(o[Symbol.iterator]=function(){return this}),o;function c(a){return function(l){return h([a,l])}}function h(a){if(r)throw new TypeError("Generator is already executing.");for(;t;)try{if(r=1,s&&(i=a[0]&2?s.return:a[0]?s.throw||((i=s.return)&&i.call(s),0):s.next)&&!(i=i.call(s,a[1])).done)return i;switch(s=0,i&&(a=[a[0]&2,i.value]),a[0]){case 0:case 1:i=a;break;case 4:return t.label++,{value:a[1],done:!1};case 5:t.label++,s=a[1],a=[0];continue;case 7:a=t.ops.pop(),t.trys.pop();continue;default:if(i=t.trys,!(i=i.length>0&&i[i.length-1])&&(a[0]===6||a[0]===2)){t=0;continue}if(a[0]===3&&(!i||a[1]>i[0]&&a[1]<i[3])){t.label=a[1];break}if(a[0]===6&&t.label<i[1]){t.label=i[1],i=a;break}if(i&&t.label<i[2]){t.label=i[2],t.ops.push(a);break}i[2]&&t.ops.pop(),t.trys.pop();continue}a=e.call(n,t)}catch(l){a=[6,l],s=0}finally{r=i=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}},te=function(n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e=n[Symbol.asyncIterator],t;return e?e.call(n):(n=typeof __values=="function"?__values(n):n[Symbol.iterator](),t={},r("next"),r("throw"),r("return"),t[Symbol.asyncIterator]=function(){return this},t);function r(i){t[i]=n[i]&&function(o){return new Promise(function(c,h){o=n[i](o),s(c,h,o.done,o.value)})}}function s(i,o,c,h){Promise.resolve(h).then(function(a){i({value:a,done:c})},o)}},T=function(n){return this instanceof T?(this.v=n,this):new T(n)},Pe=function(n,e,t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r=t.apply(n,e||[]),s,i=[];return s={},o("next"),o("throw"),o("return"),s[Symbol.asyncIterator]=function(){return this},s;function o(f){r[f]&&(s[f]=function(u){return new Promise(function(d,v){i.push([f,u,d,v])>1||c(f,u)})})}function c(f,u){try{h(r[f](u))}catch(d){p(i[0][3],d)}}function h(f){f.value instanceof T?Promise.resolve(f.value.v).then(a,l):p(i[0][2],f)}function a(f){c("next",f)}function l(f){c("throw",f)}function p(f,u){f(u),i.shift(),i.length&&c(i[0][0],i[0][1])}},je=function(n){var e=typeof n;return e==="string"||e==="number"},C=-1,N=new DataView(new ArrayBuffer(0)),et=new Uint8Array(N.buffer),V=(function(){try{N.getInt8(0)}catch(n){return n.constructor}throw new Error("never reached")})(),ne=new V("Insufficient data"),tt=new Qe,nt=(function(){function n(e,t,r,s,i,o,c,h){e===void 0&&(e=ee.defaultCodec),t===void 0&&(t=void 0),r===void 0&&(r=U),s===void 0&&(s=U),i===void 0&&(i=U),o===void 0&&(o=U),c===void 0&&(c=U),h===void 0&&(h=tt),this.extensionCodec=e,this.context=t,this.maxStrLength=r,this.maxBinLength=s,this.maxArrayLength=i,this.maxMapLength=o,this.maxExtLength=c,this.keyDecoder=h,this.totalPos=0,this.pos=0,this.view=N,this.bytes=et,this.headByte=C,this.stack=[]}return n.prototype.reinitializeState=function(){this.totalPos=0,this.headByte=C,this.stack.length=0},n.prototype.setBuffer=function(e){this.bytes=L(e),this.view=me(this.bytes),this.pos=0},n.prototype.appendBuffer=function(e){if(this.headByte===C&&!this.hasRemaining(1))this.setBuffer(e);else{var t=this.bytes.subarray(this.pos),r=L(e),s=new Uint8Array(t.length+r.length);s.set(t),s.set(r,t.length),this.setBuffer(s)}},n.prototype.hasRemaining=function(e){return this.view.byteLength-this.pos>=e},n.prototype.createExtraByteError=function(e){var t=this,r=t.view,s=t.pos;return new RangeError("Extra ".concat(r.byteLength-s," of ").concat(r.byteLength," byte(s) found at buffer[").concat(e,"]"))},n.prototype.decode=function(e){this.reinitializeState(),this.setBuffer(e);var t=this.doDecodeSync();if(this.hasRemaining(1))throw this.createExtraByteError(this.pos);return t},n.prototype.decodeMulti=function(e){return H(this,function(t){switch(t.label){case 0:this.reinitializeState(),this.setBuffer(e),t.label=1;case 1:return this.hasRemaining(1)?[4,this.doDecodeSync()]:[3,3];case 2:return t.sent(),[3,1];case 3:return[2]}})},n.prototype.decodeAsync=function(e){var t,r,s,i;return be(this,void 0,void 0,function(){var o,c,h,a,l,p,f,u;return H(this,function(d){switch(d.label){case 0:o=!1,d.label=1;case 1:d.trys.push([1,6,7,12]),t=te(e),d.label=2;case 2:return[4,t.next()];case 3:if(r=d.sent(),!!r.done)return[3,5];if(h=r.value,o)throw this.createExtraByteError(this.totalPos);this.appendBuffer(h);try{c=this.doDecodeSync(),o=!0}catch(v){if(!(v instanceof V))throw v}this.totalPos+=this.pos,d.label=4;case 4:return[3,2];case 5:return[3,12];case 6:return a=d.sent(),s={error:a},[3,12];case 7:return d.trys.push([7,,10,11]),r&&!r.done&&(i=t.return)?[4,i.call(t)]:[3,9];case 8:d.sent(),d.label=9;case 9:return[3,11];case 10:if(s)throw s.error;return[7];case 11:return[7];case 12:if(o){if(this.hasRemaining(1))throw this.createExtraByteError(this.totalPos);return[2,c]}throw l=this,p=l.headByte,f=l.pos,u=l.totalPos,new RangeError("Insufficient data in parsing ".concat(I(p)," at ").concat(u," (").concat(f," in the current buffer)"))}})})},n.prototype.decodeArrayStream=function(e){return this.decodeMultiAsync(e,!0)},n.prototype.decodeStream=function(e){return this.decodeMultiAsync(e,!1)},n.prototype.decodeMultiAsync=function(e,t){return Pe(this,arguments,function(){var s,i,o,c,h,a,l,p,f;return H(this,function(u){switch(u.label){case 0:s=t,i=-1,u.label=1;case 1:u.trys.push([1,13,14,19]),o=te(e),u.label=2;case 2:return[4,T(o.next())];case 3:if(c=u.sent(),!!c.done)return[3,12];if(h=c.value,t&&i===0)throw this.createExtraByteError(this.totalPos);this.appendBuffer(h),s&&(i=this.readArraySize(),s=!1,this.complete()),u.label=4;case 4:u.trys.push([4,9,,10]),u.label=5;case 5:return[4,T(this.doDecodeSync())];case 6:return[4,u.sent()];case 7:return u.sent(),--i===0?[3,8]:[3,5];case 8:return[3,10];case 9:if(a=u.sent(),!(a instanceof V))throw a;return[3,10];case 10:this.totalPos+=this.pos,u.label=11;case 11:return[3,2];case 12:return[3,19];case 13:return l=u.sent(),p={error:l},[3,19];case 14:return u.trys.push([14,,17,18]),c&&!c.done&&(f=o.return)?[4,T(f.call(o))]:[3,16];case 15:u.sent(),u.label=16;case 16:return[3,18];case 17:if(p)throw p.error;return[7];case 18:return[7];case 19:return[2]}})})},n.prototype.doDecodeSync=function(){e:for(;;){var e=this.readHeadByte(),t=void 0;if(e>=224)t=e-256;else if(e<192)if(e<128)t=e;else if(e<144){var r=e-128;if(r!==0){this.pushMapState(r),this.complete();continue e}else t={}}else if(e<160){var r=e-144;if(r!==0){this.pushArrayState(r),this.complete();continue e}else t=[]}else{var s=e-160;t=this.decodeUtf8String(s,0)}else if(e===192)t=null;else if(e===194)t=!1;else if(e===195)t=!0;else if(e===202)t=this.readF32();else if(e===203)t=this.readF64();else if(e===204)t=this.readU8();else if(e===205)t=this.readU16();else if(e===206)t=this.readU32();else if(e===207)t=this.readU64();else if(e===208)t=this.readI8();else if(e===209)t=this.readI16();else if(e===210)t=this.readI32();else if(e===211)t=this.readI64();else if(e===217){var s=this.lookU8();t=this.decodeUtf8String(s,1)}else if(e===218){var s=this.lookU16();t=this.decodeUtf8String(s,2)}else if(e===219){var s=this.lookU32();t=this.decodeUtf8String(s,4)}else if(e===220){var r=this.readU16();if(r!==0){this.pushArrayState(r),this.complete();continue e}else t=[]}else if(e===221){var r=this.readU32();if(r!==0){this.pushArrayState(r),this.complete();continue e}else t=[]}else if(e===222){var r=this.readU16();if(r!==0){this.pushMapState(r),this.complete();continue e}else t={}}else if(e===223){var r=this.readU32();if(r!==0){this.pushMapState(r),this.complete();continue e}else t={}}else if(e===196){var r=this.lookU8();t=this.decodeBinary(r,1)}else if(e===197){var r=this.lookU16();t=this.decodeBinary(r,2)}else if(e===198){var r=this.lookU32();t=this.decodeBinary(r,4)}else if(e===212)t=this.decodeExtension(1,0);else if(e===213)t=this.decodeExtension(2,0);else if(e===214)t=this.decodeExtension(4,0);else if(e===215)t=this.decodeExtension(8,0);else if(e===216)t=this.decodeExtension(16,0);else if(e===199){var r=this.lookU8();t=this.decodeExtension(r,1)}else if(e===200){var r=this.lookU16();t=this.decodeExtension(r,2)}else if(e===201){var r=this.lookU32();t=this.decodeExtension(r,4)}else throw new y("Unrecognized type byte: ".concat(I(e)));this.complete();for(var i=this.stack;i.length>0;){var o=i[i.length-1];if(o.type===0)if(o.array[o.position]=t,o.position++,o.position===o.size)i.pop(),t=o.array;else continue e;else if(o.type===1){if(!je(t))throw new y("The type of key must be string or number but "+typeof t);if(t==="__proto__")throw new y("The key __proto__ is not allowed");o.key=t,o.type=2;continue e}else if(o.map[o.key]=t,o.readCount++,o.readCount===o.size)i.pop(),t=o.map;else{o.key=null,o.type=1;continue e}}return t}},n.prototype.readHeadByte=function(){return this.headByte===C&&(this.headByte=this.readU8()),this.headByte},n.prototype.complete=function(){this.headByte=C},n.prototype.readArraySize=function(){var e=this.readHeadByte();switch(e){case 220:return this.readU16();case 221:return this.readU32();default:{if(e<160)return e-144;throw new y("Unrecognized array type byte: ".concat(I(e)))}}},n.prototype.pushMapState=function(e){if(e>this.maxMapLength)throw new y("Max length exceeded: map length (".concat(e,") > maxMapLengthLength (").concat(this.maxMapLength,")"));this.stack.push({type:1,size:e,key:null,readCount:0,map:{}})},n.prototype.pushArrayState=function(e){if(e>this.maxArrayLength)throw new y("Max length exceeded: array length (".concat(e,") > maxArrayLength (").concat(this.maxArrayLength,")"));this.stack.push({type:0,size:e,array:new Array(e),position:0})},n.prototype.decodeUtf8String=function(e,t){var r;if(e>this.maxStrLength)throw new y("Max length exceeded: UTF-8 byte length (".concat(e,") > maxStrLength (").concat(this.maxStrLength,")"));if(this.bytes.byteLength<this.pos+t+e)throw ne;var s=this.pos+t,i;return this.stateIsMapKey()&&(!((r=this.keyDecoder)===null||r===void 0)&&r.canBeCached(e))?i=this.keyDecoder.decode(this.bytes,s,e):e>ze?i=Fe(this.bytes,s,e):i=j(this.bytes,s,e),this.pos+=t+e,i},n.prototype.stateIsMapKey=function(){if(this.stack.length>0){var e=this.stack[this.stack.length-1];return e.type===1}return!1},n.prototype.decodeBinary=function(e,t){if(e>this.maxBinLength)throw new y("Max length exceeded: bin length (".concat(e,") > maxBinLength (").concat(this.maxBinLength,")"));if(!this.hasRemaining(e+t))throw ne;var r=this.pos+t,s=this.bytes.subarray(r,r+e);return this.pos+=t+e,s},n.prototype.decodeExtension=function(e,t){if(e>this.maxExtLength)throw new y("Max length exceeded: ext length (".concat(e,") > maxExtLength (").concat(this.maxExtLength,")"));var r=this.view.getInt8(this.pos+t),s=this.decodeBinary(e,t+1);return this.extensionCodec.decode(s,r,this.context)},n.prototype.lookU8=function(){return this.view.getUint8(this.pos)},n.prototype.lookU16=function(){return this.view.getUint16(this.pos)},n.prototype.lookU32=function(){return this.view.getUint32(this.pos)},n.prototype.readU8=function(){var e=this.view.getUint8(this.pos);return this.pos++,e},n.prototype.readI8=function(){var e=this.view.getInt8(this.pos);return this.pos++,e},n.prototype.readU16=function(){var e=this.view.getUint16(this.pos);return this.pos+=2,e},n.prototype.readI16=function(){var e=this.view.getInt16(this.pos);return this.pos+=2,e},n.prototype.readU32=function(){var e=this.view.getUint32(this.pos);return this.pos+=4,e},n.prototype.readI32=function(){var e=this.view.getInt32(this.pos);return this.pos+=4,e},n.prototype.readU64=function(){var e=_e(this.view,this.pos);return this.pos+=8,e},n.prototype.readI64=function(){var e=b(this.view,this.pos);return this.pos+=8,e},n.prototype.readF32=function(){var e=this.view.getFloat32(this.pos);return this.pos+=4,e},n.prototype.readF64=function(){var e=this.view.getFloat64(this.pos);return this.pos+=8,e},n})();function D(){const n={};return n.promise=new Promise(function(e,t){n.resolve=e,n.reject=t}),n}function rt(n,e){let r=0;const s={},i=/^(rpc|publish|system):(\w+(?:\.\w+)*):(?:\d+)$/,o={};let c=null,h=512*1024;const a=new Se;function l(f){if(f&&f.id){if(f.error){const u=s[f.id];u?u.reject(f.error):console.error("Server error:",f.error)}else{const u=i.exec(f.id);if(u){const d=u[1];if(d==="rpc"){const v=s[f.id];if(!v){console.log("session message id without matching call, dropped",f);return}v.resolve(f.result)}else if(d=="publish"){console.assert(s[f.id]===void 0,"publish message received matching in-flight rpc call");const v=u[2];if(!o[v])return;o[v].forEach(E=>E([f.result]))}else if(d=="system"){const v=s[f.id];f.id==="system:c0:0"?(c=f.result.clientID,h=f.result.maxMsgSize||h,v&&v.resolve(c)):(console.error("Unknown system message",f.id),v&&v.reject({code:-32099,message:`Unknown system message ${f.id}`}))}else console.error("Unknown rpc id format",f.id)}}delete s[f.id]}}n.onconnect=f=>{const u=D(),d="system:c0:0";s[d]=u;const v={wslink:"1.0",id:d,method:"wslink.hello",args:[{secret:e.secret}],kwargs:{}},G=new se().encode(v);for(let fe of Z(G,h))e.ws.send(fe,{binary:!0});return u.promise},n.call=(f,u=[],d={})=>{const v=D();if(e.ws&&c&&e.ws.readyState===1){const E=`rpc:${c}:${r++}`;s[E]=v;const G={wslink:"1.0",id:E,method:f,args:u,kwargs:d},wt=new se().encode(G);for(let vt of Z(wt,h))e.ws.send(vt,{binary:!0})}else v.reject({code:-32099,message:`RPC call ${f} unsuccessful: connection not open`});return v.promise},n.subscribe=(f,u)=>{const d=D();return e.ws&&c?(o[f]||(o[f]=[]),o[f].push(u),d.resolve({topic:f,callback:u})):d.reject({code:-32099,message:`Subscribe call ${f} unsuccessful: connection not open`}),{topic:f,callback:u,promise:d.promise,unsubscribe:()=>n.unsubscribe({topic:f,callback:u})}},n.unsubscribe=f=>{const u=D(),{topic:d,callback:v}=f;if(!o[d])return u.reject({code:-32099,message:`Unsubscribe call ${d} unsuccessful: not subscribed`}),u.promise;const E=o[d].indexOf(v);return E!==-1?(o[d].splice(E,1),u.resolve()):u.reject({code:-32099,message:`Unsubscribe call ${d} unsuccessful: callback not found`}),u.promise},n.close=()=>{const f=D();return e.ws.close(),a.releasePendingMessages(),f.resolve(),f.promise};function p(){return new nt}n.onmessage=async f=>{const u=await a.processChunk(f.data,p);u&&l(u)},n.addAttachment=f=>f instanceof ArrayBuffer?new DataView(f):f}const st={secret:"wslink-secret",ws:null};function re(n,e,t={}){Object.assign(e,st,t),w.destroy(n,e),w.isA(n,e,"Session"),rt(n,e)}const it={newInstance:w.newInstance(re),extend:re};class se extends qe{encodeObject(e,t){return e instanceof ArrayBuffer&&(e=new DataView(e)),super.encodeObject.call(this,e,t)}}const ie="wslink-secret";function ot(n){var e=n.indexOf(":"),t=n.substring(0,e);if(t==="ws"||t==="wss")return{type:"websocket",url:n};throw new Error(`Unknown protocol (${t}) for url (${n}). Unable to create transport object.`)}function ct(n,e){n.connect=()=>{if(!e.urls)return null;var r=[].concat(e.urls),s=[];for(let i=0;i<r.length;i+=1){const o=r[i];try{const c=ot(o);s.push(c)}catch(c){return console.error(c),n.fireConnectionError(n,c),null}}if(e.connection){if(e.connection.url!==s[0].url)e.connection.close();else if(e.connection.readyState===0||e.connection.readyState===1)return e.session}try{e.wsProxy?e.connection=WSLINK.createWebSocket(s[0].url):e.connection=new WebSocket(s[0].url)}catch(i){return console.error(i),n.fireConnectionError(n,i),null}return e.connection.binaryType="blob",e.secret||(e.secret=ie),e.session=it.newInstance({ws:e.connection,secret:e.secret}),e.connection.onopen=i=>{e.session&&e.session.onconnect(i).then(()=>{n.fireConnectionReady(n)},o=>{console.error("Connection error",o),n.fireConnectionError(n,o)})},e.connection.onclose=i=>{n.fireConnectionClose(n,i),e.connection=null},e.connection.onerror=i=>{n.fireConnectionError(n,i)},e.connection.onmessage=i=>{e.session.onmessage(i)},e.session},n.getSession=()=>e.session,n.getUrl=()=>e.connection?e.connection.url:void 0;function t(r=10){e.connection&&e.connection.readyState===1&&e.session&&r>0&&e.session.call("application.exit.later",[r]),e.connection&&e.connection.close(),e.connection=null}n.destroy=w.chain(t,n.destroy)}const at={secret:ie,connection:null,session:null,retry:!1,wsProxy:!1};function oe(n,e,t={}){Object.assign(e,at,t),w.destroy(n,e),w.event(n,e,"ConnectionReady"),w.event(n,e,"ConnectionClose"),w.event(n,e,"ConnectionError"),w.isA(n,e,"WebsocketConnection"),ct(n,e)}const ce={newInstance:w.newInstance(oe),extend:oe};function W(n){return n.sessionURL&&(n.sessionURL=n.sessionURL.replaceAll("USE_HOSTNAME",window.location.hostname),n.sessionURL=n.sessionURL.replaceAll("USE_HOST",window.location.host)),n}function ft(n,e=window.location.pathname){if(e.endsWith(".html")||e.endsWith(".htm")){const t=e.split("/");t.pop(),e=t.join("/")}for(;e.length>0&&e[e.length-1]==="/";)e=e.substring(0,e.length-1);return e.length===0?n:`${e}${n}`}const ht=`${document.baseURI}paraview/`,ut=`${window.location.protocol==="https:"?"wss:":"ws:"}//${window.location.hostname}:${window.location.port}${ft("/ws")}`;function O(n,e){const t=ce.newInstance({urls:e.config.sessionURL,secret:e.config.secret,retry:e.config.retry,wsProxy:e.config.iframe||e.config.wsProxy});return e.subscriptions.push(t.onConnectionReady(n.readyForwarder)),e.subscriptions.push(t.onConnectionError(n.errorForwarder)),e.subscriptions.push(t.onConnectionClose(n.closeForwarder)),e.gc.push(t),t.connect()}function dt(n,e){let t=null;e.gc=[],n.readyForwarder=s=>{t=s.getSession(),n.fireConnectionReady(s)},n.errorForwarder=(s,i)=>{n.fireConnectionError(s,i)},n.closeForwarder=(s,i)=>{n.fireConnectionClose(s,i)},n.connect=()=>{if(e.configDecorator&&(e.config=e.configDecorator(e.config)),e.config=W(e.config),e.config.sessionURL)t=O(n,e);else if(e.config.wsProxy)e.config.sessionURL=e.config.sessionURL||"wss://proxy/",t=O(n,e);else{const s=K.newInstance({endPoint:e.config.sessionManagerURL||ht});e.subscriptions.push(s.onProcessReady(i=>{e.configDecorator?e.config=e.configDecorator(Object.assign({},e.config,i)):e.config=Object.assign({},e.config,i),e.config=W(e.config),t=O(n,e)})),e.subscriptions.push(s.onError(i=>{i&&i.response&&i.response.error?n.errorForwarder(i,i.response.error):(e.config.sessionURL=ut,e.config=W(e.config),t=O(n,e))})),s.start(e.config),e.gc.push(s)}},n.getSession=()=>t;function r(s){for(t&&(s>0&&t.call("application.exit.later",[s]),t.close()),t=null;e.gc.length;)e.gc.pop().destroy()}n.destroy=w.chain(r,n.destroy)}const lt={config:{}};function ae(n,e,t={}){Object.assign(e,lt,t),w.destroy(n,e),w.event(n,e,"ConnectionReady"),w.event(n,e,"ConnectionClose"),w.event(n,e,"ConnectionError"),w.isA(n,e,"SmartConnect"),w.get(n,e,["config","configDecorator"]),w.set(n,e,["configDecorator"]),dt(n,e)}const pt={newInstance:w.newInstance(ae),extend:ae};x.CompositeClosureHelper=w,x.ProcessLauncher=K,x.SmartConnect=pt,x.WebsocketConnection=ce,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kitware/wslink",
|
|
3
|
+
"version": "2.5.0",
|
|
4
|
+
"description": "Rpc and pub/sub between Python and JavaScript over WebSockets",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/kitware/wslink.git"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/kitware/wslink/issues"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/kitware/wslink#readme",
|
|
13
|
+
"main": "dist/wslink.umd.js",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"format": "prettier src --write",
|
|
16
|
+
"dev": "vite",
|
|
17
|
+
"build": "vite build",
|
|
18
|
+
"preview": "vite preview"
|
|
19
|
+
},
|
|
20
|
+
"author": "Kitware",
|
|
21
|
+
"license": "BSD-3-Clause",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"prettier": "^3.8.1",
|
|
24
|
+
"vite": "^7.3.1",
|
|
25
|
+
"vite-plugin-prettier-format": "^1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@msgpack/msgpack": "^2.8.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// ----------------------------------------------------------------------------
|
|
2
|
+
// capitalize provided string
|
|
3
|
+
// ----------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
export function capitalize(str) {
|
|
6
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// ----------------------------------------------------------------------------
|
|
10
|
+
// Add isA function and register your class name
|
|
11
|
+
// ----------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
function isA(publicAPI, model = {}, name = null) {
|
|
14
|
+
if (!model.isA) {
|
|
15
|
+
model.isA = [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (name) {
|
|
19
|
+
model.isA.push(name);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!publicAPI.isA) {
|
|
23
|
+
publicAPI.isA = (className) => model.isA.indexOf(className) !== -1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ----------------------------------------------------------------------------
|
|
28
|
+
// Basic setter
|
|
29
|
+
// ----------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
function set(publicAPI, model = {}, names = []) {
|
|
32
|
+
names.forEach((name) => {
|
|
33
|
+
publicAPI[`set${capitalize(name)}`] = (value) => {
|
|
34
|
+
model[name] = value;
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ----------------------------------------------------------------------------
|
|
40
|
+
// Basic getter
|
|
41
|
+
// ----------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
function get(publicAPI, model = {}, names = []) {
|
|
44
|
+
names.forEach((name) => {
|
|
45
|
+
publicAPI[`get${capitalize(name)}`] = () => model[name];
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ----------------------------------------------------------------------------
|
|
50
|
+
// Add destroy function
|
|
51
|
+
// ----------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
function destroy(publicAPI, model = {}) {
|
|
54
|
+
const previousDestroy = publicAPI.destroy;
|
|
55
|
+
|
|
56
|
+
if (!model.subscriptions) {
|
|
57
|
+
model.subscriptions = [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
publicAPI.destroy = () => {
|
|
61
|
+
if (previousDestroy) {
|
|
62
|
+
previousDestroy();
|
|
63
|
+
}
|
|
64
|
+
while (model.subscriptions && model.subscriptions.length) {
|
|
65
|
+
model.subscriptions.pop().unsubscribe();
|
|
66
|
+
}
|
|
67
|
+
Object.keys(model).forEach((field) => {
|
|
68
|
+
delete model[field];
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Flag the instance being deleted
|
|
72
|
+
model.deleted = true;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ----------------------------------------------------------------------------
|
|
77
|
+
// Event handling: onXXX(callback), fireXXX(args...)
|
|
78
|
+
// ----------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
function event(publicAPI, model, eventName, asynchronous = true) {
|
|
81
|
+
const callbacks = [];
|
|
82
|
+
const previousDestroy = publicAPI.destroy;
|
|
83
|
+
|
|
84
|
+
function off(index) {
|
|
85
|
+
callbacks[index] = null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function on(index) {
|
|
89
|
+
function unsubscribe() {
|
|
90
|
+
off(index);
|
|
91
|
+
}
|
|
92
|
+
return Object.freeze({ unsubscribe });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
publicAPI[`fire${capitalize(eventName)}`] = (...args) => {
|
|
96
|
+
if (model.deleted) {
|
|
97
|
+
console.log("instance deleted - can not call any method");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function processCallbacks() {
|
|
102
|
+
callbacks.forEach((callback) => {
|
|
103
|
+
if (callback) {
|
|
104
|
+
try {
|
|
105
|
+
callback.apply(publicAPI, args);
|
|
106
|
+
} catch (errObj) {
|
|
107
|
+
console.log("Error event:", eventName, errObj);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (asynchronous) {
|
|
114
|
+
setTimeout(processCallbacks, 0);
|
|
115
|
+
} else {
|
|
116
|
+
processCallbacks();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
publicAPI[`on${capitalize(eventName)}`] = (callback) => {
|
|
121
|
+
if (model.deleted) {
|
|
122
|
+
console.log("instance deleted - can not call any method");
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const index = callbacks.length;
|
|
127
|
+
callbacks.push(callback);
|
|
128
|
+
return on(index);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
publicAPI.destroy = () => {
|
|
132
|
+
previousDestroy();
|
|
133
|
+
callbacks.forEach((el, index) => off(index));
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ----------------------------------------------------------------------------
|
|
138
|
+
// Chain function calls
|
|
139
|
+
// ----------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
function chain(...fn) {
|
|
142
|
+
return (...args) => fn.filter((i) => !!i).forEach((i) => i(...args));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ----------------------------------------------------------------------------
|
|
146
|
+
// newInstance
|
|
147
|
+
// ----------------------------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
function newInstance(extend) {
|
|
150
|
+
return (initialValues = {}) => {
|
|
151
|
+
const model = {};
|
|
152
|
+
const publicAPI = {};
|
|
153
|
+
extend(publicAPI, model, initialValues);
|
|
154
|
+
return Object.freeze(publicAPI);
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export default {
|
|
159
|
+
chain,
|
|
160
|
+
destroy,
|
|
161
|
+
event,
|
|
162
|
+
get,
|
|
163
|
+
isA,
|
|
164
|
+
newInstance,
|
|
165
|
+
set,
|
|
166
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# ProcessLauncher
|
|
2
|
+
|
|
3
|
+
The ProcessLauncher can be used to start remote python servers, or other remote
|
|
4
|
+
processes. The ProcessLauncher is used in ParaViewWeb to start a new remote
|
|
5
|
+
server instance to perform interactive 3D post-processing using either a VTK or
|
|
6
|
+
a ParaView backend.
|
|
7
|
+
|
|
8
|
+
```javascript
|
|
9
|
+
import ProcessLauncher from "wslink/src/ProcessLauncher";
|
|
10
|
+
|
|
11
|
+
processLauncher = ProcessLauncher.newInstance({ endPoint: "/paraview" });
|
|
12
|
+
|
|
13
|
+
// Optionally you can provide a launcherRetry: [1000, 2000, 3000, 5000]
|
|
14
|
+
// with the set of time to wait in ms before another retry.
|
|
15
|
+
// Retries will only occurred on a 503 response from the server.
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## ProcessLauncher.newInstance({ endPoint, launcherRetry: [] })
|
|
19
|
+
|
|
20
|
+
Create a process launcher that will make requests to a remote server using the
|
|
21
|
+
provided endpoint url.
|
|
22
|
+
|
|
23
|
+
## start(config)
|
|
24
|
+
|
|
25
|
+
Submit a request for a new remote process.
|
|
26
|
+
|
|
27
|
+
The config object gets posted via a POST request to the endpoint provided at
|
|
28
|
+
creation time.
|
|
29
|
+
|
|
30
|
+
The current ParaViewWeb server-side **Launcher** expects at least the following
|
|
31
|
+
object.
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
{
|
|
35
|
+
application: 'NameOfTheProcess',
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
But additional key/value pairs can be added depending on the needs of the
|
|
40
|
+
targeted process.
|
|
41
|
+
|
|
42
|
+
Once the remote process becomes ready a notification is sent.
|
|
43
|
+
|
|
44
|
+
## fetchConnection(sessionId)
|
|
45
|
+
|
|
46
|
+
Trigger a request for getting the full connection information based on an
|
|
47
|
+
existing sessionId.
|
|
48
|
+
|
|
49
|
+
## stop(connection)
|
|
50
|
+
|
|
51
|
+
Trigger a request to terminate a remote process using the connection object that
|
|
52
|
+
was provided at start time.
|
|
53
|
+
|
|
54
|
+
## listConnections()
|
|
55
|
+
|
|
56
|
+
Return the list of already established connections. (From that instance)
|
|
57
|
+
|
|
58
|
+
## onProcessReady(callback) : subscription
|
|
59
|
+
|
|
60
|
+
Register a callback for when a remote process becomes available after a start()
|
|
61
|
+
request.
|
|
62
|
+
|
|
63
|
+
The callback function will then receive a json object describing how to connect
|
|
64
|
+
to that remote process.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
{
|
|
68
|
+
sessionURL: 'ws://myServer/proxy?sessionId=asdfwefasdfwerfqerfse',
|
|
69
|
+
maybe: 'something else too'
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## onProcessStopped(callback) : subscription
|
|
74
|
+
|
|
75
|
+
Register a callback for when a stop request is performed.
|
|
76
|
+
|
|
77
|
+
## onFetch(callback) : subscription
|
|
78
|
+
|
|
79
|
+
Register a callback for when a fetchConnection request is performed.
|
|
80
|
+
|
|
81
|
+
## onError(callback) : subscription
|
|
82
|
+
|
|
83
|
+
Register a callback for when an error occurred regardless of the request.
|
|
84
|
+
|
|
85
|
+
## destroy()
|
|
86
|
+
|
|
87
|
+
Free memory and detach any listeners.
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/* global XMLHttpRequest */
|
|
2
|
+
import CompositeClosureHelper from "../CompositeClosureHelper";
|
|
3
|
+
|
|
4
|
+
const connections = [];
|
|
5
|
+
|
|
6
|
+
function ProcessLauncher(publicAPI, model) {
|
|
7
|
+
publicAPI.start = (config) => {
|
|
8
|
+
const xhr = new XMLHttpRequest();
|
|
9
|
+
const url = model.endPoint;
|
|
10
|
+
if (!model._retry) {
|
|
11
|
+
model._retry = config.launcherRetry || [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
xhr.open("POST", url, true);
|
|
15
|
+
|
|
16
|
+
if (config.headers) {
|
|
17
|
+
model._headers = config.headers;
|
|
18
|
+
delete config.headers;
|
|
19
|
+
}
|
|
20
|
+
if (model._headers) {
|
|
21
|
+
Object.entries(model._headers).forEach(([key, value]) =>
|
|
22
|
+
xhr.setRequestHeader(key, value)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
xhr.responseType = "json";
|
|
27
|
+
const supportsJson = "response" in xhr && xhr.responseType === "json";
|
|
28
|
+
|
|
29
|
+
xhr.onload = (e) => {
|
|
30
|
+
const response = supportsJson ? xhr.response : JSON.parse(xhr.response);
|
|
31
|
+
if (xhr.status === 200 && response && !response.error) {
|
|
32
|
+
// Add connection to our global list
|
|
33
|
+
connections.push(response);
|
|
34
|
+
publicAPI.fireProcessReady(response);
|
|
35
|
+
return;
|
|
36
|
+
} else if (xhr.status === 503 && model._retry.length > 0) {
|
|
37
|
+
const timeout = model._retry.shift();
|
|
38
|
+
setTimeout(publicAPI.start, timeout, config);
|
|
39
|
+
} else {
|
|
40
|
+
publicAPI.fireError(xhr);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
xhr.onerror = (e) => {
|
|
45
|
+
publicAPI.fireError(xhr);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
xhr.send(JSON.stringify(config));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
publicAPI.fetchConnection = (sessionId) => {
|
|
52
|
+
var xhr = new XMLHttpRequest(),
|
|
53
|
+
url = [model.endPoint, sessionId].join("/");
|
|
54
|
+
|
|
55
|
+
xhr.open("GET", url, true);
|
|
56
|
+
xhr.responseType = "json";
|
|
57
|
+
const supportsJson = "response" in xhr && xhr.responseType === "json";
|
|
58
|
+
|
|
59
|
+
xhr.onload = (e) => {
|
|
60
|
+
if (this.status === 200) {
|
|
61
|
+
publicAPI.fireFetch(
|
|
62
|
+
supportsJson ? xhr.response : JSON.parse(xhr.response)
|
|
63
|
+
);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
publicAPI.fireError(xhr);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
xhr.onerror = (e) => {
|
|
70
|
+
publicAPI.fireError(xhr);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
xhr.send();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
publicAPI.stop = (connection) => {
|
|
77
|
+
var xhr = new XMLHttpRequest(),
|
|
78
|
+
url = [model.endPoint, connection.id].join("/");
|
|
79
|
+
|
|
80
|
+
xhr.open("DELETE", url, true);
|
|
81
|
+
xhr.responseType = "json";
|
|
82
|
+
const supportsJson = "response" in xhr && xhr.responseType === "json";
|
|
83
|
+
|
|
84
|
+
xhr.onload = (e) => {
|
|
85
|
+
if (this.status === 200) {
|
|
86
|
+
const response = supportsJson ? xhr.response : JSON.parse(xhr.response);
|
|
87
|
+
// Remove connection from the list
|
|
88
|
+
// FIXME / TODO
|
|
89
|
+
publicAPI.fireProcessStopped(response);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
publicAPI.fireError(xhr);
|
|
93
|
+
};
|
|
94
|
+
xhr.onerror = (e) => {
|
|
95
|
+
publicAPI.fireError(xhr);
|
|
96
|
+
};
|
|
97
|
+
xhr.send();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
publicAPI.listConnections = () => {
|
|
101
|
+
return connections;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const DEFAULT_VALUES = {
|
|
106
|
+
endPoint: null,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export function extend(publicAPI, model, initialValues = {}) {
|
|
110
|
+
Object.assign(model, DEFAULT_VALUES, initialValues);
|
|
111
|
+
|
|
112
|
+
CompositeClosureHelper.destroy(publicAPI, model);
|
|
113
|
+
CompositeClosureHelper.event(publicAPI, model, "ProcessReady");
|
|
114
|
+
CompositeClosureHelper.event(publicAPI, model, "ProcessStopped");
|
|
115
|
+
CompositeClosureHelper.event(publicAPI, model, "Fetch");
|
|
116
|
+
CompositeClosureHelper.event(publicAPI, model, "Error");
|
|
117
|
+
CompositeClosureHelper.isA(publicAPI, model, "ProcessLauncher");
|
|
118
|
+
|
|
119
|
+
ProcessLauncher(publicAPI, model);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ----------------------------------------------------------------------------
|
|
123
|
+
export const newInstance = CompositeClosureHelper.newInstance(extend);
|
|
124
|
+
|
|
125
|
+
export default { newInstance, extend };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# SmartConnect
|
|
2
|
+
|
|
3
|
+
SmartConnect will try to launch a new remote process based on the configuration
|
|
4
|
+
and if that fails or if a sessionURL is already provided in the configuration it
|
|
5
|
+
will establish a direct WebSocket connection using Autobahn.
|
|
6
|
+
|
|
7
|
+
## SmartConnect.newInstance({ config })
|
|
8
|
+
|
|
9
|
+
Create an instance that will use the provided configuration to connect itself to
|
|
10
|
+
a server either by requesting a new remote process or by trying to directly
|
|
11
|
+
connecting to it as a fallback.
|
|
12
|
+
|
|
13
|
+
## connect()
|
|
14
|
+
|
|
15
|
+
Trigger the connection request.
|
|
16
|
+
|
|
17
|
+
## onConnectionReady(callback) : subscription
|
|
18
|
+
|
|
19
|
+
Register callback for when the connection became ready.
|
|
20
|
+
|
|
21
|
+
## onConnectionClose(callback) : subscription
|
|
22
|
+
|
|
23
|
+
Register callback for when the connection close. Callback takes two arguments,
|
|
24
|
+
the connection object and a websocket event. If the server closes the connection
|
|
25
|
+
after sending a close frame
|
|
26
|
+
(https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1), the event will
|
|
27
|
+
have the shape {code: number, reason: string}.
|
|
28
|
+
|
|
29
|
+
## onConnectionError(callback) : subscription
|
|
30
|
+
|
|
31
|
+
Register callback for when the connection request failed. Callback takes two
|
|
32
|
+
arguments, the connection object and a websocket event.
|
|
33
|
+
|
|
34
|
+
## getSession() : session
|
|
35
|
+
|
|
36
|
+
Return the session associated with the connection.
|
|
37
|
+
|
|
38
|
+
## destroy()
|
|
39
|
+
|
|
40
|
+
Free resources and remove any listener.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import WebsocketConnection, { WebsocketSession } from "../WebsocketConnection";
|
|
2
|
+
|
|
3
|
+
export declare const DEFAULT_SESSION_MANAGER_URL: string;
|
|
4
|
+
|
|
5
|
+
export interface ISmartConnectConfig {
|
|
6
|
+
// URL to connect to. E.g., "ws://localhost:1234".
|
|
7
|
+
sessionURL?: string;
|
|
8
|
+
// Endpoint of the launcher that is responsible to start the server process.
|
|
9
|
+
// Defaults to SESSION_MANAGER_URL.
|
|
10
|
+
sessionManagerURL?: string;
|
|
11
|
+
// The secret token to be sent during handshake and validated by the server.
|
|
12
|
+
secret?: string;
|
|
13
|
+
retry?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// A function that rewrites the configuration. It is called on connect.
|
|
17
|
+
type ConfigDecorator = (config: ISmartConnectConfig) => ISmartConnectConfig;
|
|
18
|
+
|
|
19
|
+
export interface ISmartConnectInitialValues {
|
|
20
|
+
config: ISmartConnectConfig;
|
|
21
|
+
configDecorator?: ConfigDecorator;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type Event = any;
|
|
25
|
+
|
|
26
|
+
export interface WebsocketCloseEvent {
|
|
27
|
+
// This object reports the values of the close frame. Cf. RFC6455 section
|
|
28
|
+
// 5.5.1. If the connection closes w/o a close frame, the following fields are unset.
|
|
29
|
+
code?: number; // RFC6455, section 7.4.
|
|
30
|
+
reason?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SmartConnect {
|
|
34
|
+
// Starts connecting to the server.
|
|
35
|
+
connect(): void;
|
|
36
|
+
getSession(): WebsocketSession;
|
|
37
|
+
// Called when the connection is established
|
|
38
|
+
onConnectionReady(cb: (c: WebsocketConnection) => void): void;
|
|
39
|
+
// Called when the connection cannot be established.
|
|
40
|
+
onConnectionError(cb: (c: WebsocketConnection, err: Event) => void): void;
|
|
41
|
+
// Called when the connection is closed.
|
|
42
|
+
onConnectionClose(
|
|
43
|
+
cb: (c: WebsocketConnection, event: WebsocketCloseEvent) => void
|
|
44
|
+
): void;
|
|
45
|
+
// Close the connection and destroy this object.
|
|
46
|
+
destroy(): void;
|
|
47
|
+
// Return the config passed to newInstance.
|
|
48
|
+
getConfig(): ISmartConnectConfig;
|
|
49
|
+
getConfigDecorator(): ConfigDecorator | null;
|
|
50
|
+
setConfigDecorator(c: ConfigDecorator): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new SmartConnect object with the given configuration.
|
|
55
|
+
*/
|
|
56
|
+
export function newInstance(config: ISmartConnectInitialValues): SmartConnect;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Decorates a given object (publicAPI+model) with SmartConnect characteristics.
|
|
60
|
+
*/
|
|
61
|
+
export function extend(
|
|
62
|
+
publicAPI: object,
|
|
63
|
+
model: object,
|
|
64
|
+
initialValues?: ISmartConnectInitialValues
|
|
65
|
+
): void;
|
|
66
|
+
|
|
67
|
+
export declare const SmartConnect: {
|
|
68
|
+
newInstance: typeof newInstance;
|
|
69
|
+
extend: typeof extend;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export default SmartConnect;
|