azure-devops-extension-sdk
Version:
Azure DevOps web extension JavaScript library.
2 lines (1 loc) • 5.42 kB
JavaScript
const e=parseInt("10000000000",36),t=Number.MAX_SAFE_INTEGER||9007199254740991;class r{objects={};register(e,t){this.objects[e]=t}unregister(e){delete this.objects[e]}getInstance(e,t){var r=this.objects[e];if(r)return"function"==typeof r?r(t):r}}let n=1;class s{promises={};postToWindow;targetOrigin;handshakeToken;registry;channelId;nextMessageId=1;nextProxyId=1;proxyFunctions={};constructor(s,i){this.postToWindow=s,this.targetOrigin=i,this.registry=new r,this.channelId=n++,this.targetOrigin||(this.handshakeToken=Math.floor(Math.random()*(t-e)+e).toString(36)+Math.floor(Math.random()*(t-e)+e).toString(36))}getObjectRegistry(){return this.registry}async invokeRemoteMethod(e,t,r,n,s){const i={id:this.nextMessageId++,methodName:e,instanceId:t,instanceContext:n,params:this._customSerializeObject(r,s),serializationSettings:s};this.targetOrigin||(i.handshakeToken=this.handshakeToken);const o=new Promise(((e,t)=>{this.promises[i.id]={resolve:e,reject:t}}));return this._sendRpcMessage(i),o}getRemoteObjectProxy(e,t){return this.invokeRemoteMethod("",e,void 0,t)}invokeMethod(e,t){if(t.methodName){var r=e[t.methodName];if("function"==typeof r)try{var n=[];t.params&&(n=this._customDeserializeObject(t.params,{}));var s=r.apply(e,n);s&&s.then&&"function"==typeof s.then?s.then((e=>{this._success(t,e,t.handshakeToken)}),(e=>{this.error(t,e)})):this._success(t,s,t.handshakeToken)}catch(e){this.error(t,e)}else this.error(t,new Error("RPC method not found: "+t.methodName))}else this._success(t,e,t.handshakeToken)}getRegisteredObject(e,t){if("__proxyFunctions"===e)return this.proxyFunctions;var r=this.registry.getInstance(e,t);return r||(r=i.getInstance(e,t)),r}onMessage(e){if(e.instanceId){const t=this.getRegisteredObject(e.instanceId,e.instanceContext);if(!t)return!1;"function"==typeof t.then?t.then((t=>{this.invokeMethod(t,e)}),(t=>{this.error(e,t)})):this.invokeMethod(t,e)}else{const t=this.promises[e.id];if(!t)return!1;e.error?t.reject(this._customDeserializeObject([e.error],{})[0]):t.resolve(this._customDeserializeObject([e.result],{})[0]),delete this.promises[e.id]}return!0}owns(e,t,r){if(this.postToWindow===e){if(this.targetOrigin)return!!t&&("null"===t.toLowerCase()||0===this.targetOrigin.toLowerCase().indexOf(t.toLowerCase()));if(r.handshakeToken&&r.handshakeToken===this.handshakeToken)return this.targetOrigin=t,!0}return!1}error(e,t){this._sendRpcMessage({id:e.id,error:this._customSerializeObject([t],e.serializationSettings)[0],handshakeToken:e.handshakeToken})}_success(e,t,r){this._sendRpcMessage({id:e.id,result:this._customSerializeObject([t],e.serializationSettings)[0],handshakeToken:r})}_sendRpcMessage(e){this.postToWindow.postMessage(JSON.stringify(e),"*")}_customSerializeObject(e,t,r,n=1,s=1){if(!e||s>100)return;if(e instanceof Node||e instanceof Window||e instanceof Event)return;var i;let o;o=r||{newObjects:[],originalObjects:[]},o.originalObjects.push(e);var a=(r,i,a)=>{var c;try{c=r[a]}catch(e){}var h=typeof c;if("undefined"!==h){var d=-1;if("object"===h&&(d=o.originalObjects.indexOf(c)),d>=0){var u=o.newObjects[d];u.__circularReferenceId||(u.__circularReferenceId=n++),i[a]={__circularReference:u.__circularReferenceId}}else"function"===h?(this.nextProxyId++,i[a]={__proxyFunctionId:this._registerProxyFunction(c,e),_channelId:this.channelId}):"object"===h?c&&c instanceof Date?i[a]={__proxyDate:c.getTime()}:i[a]=this._customSerializeObject(c,t,o,n,s+1):"__proxyFunctionId"!==a&&(i[a]=c)}};if(e instanceof Array){i=[],o.newObjects.push(i);for(var c=0,h=e.length;c<h;c++)a(e,i,c)}else{i={},o.newObjects.push(i);let r={};try{r=function(e){const t={};for(;e&&e!==Object.prototype;){const r=Object.getOwnPropertyNames(e);for(const e of r)"constructor"!==e&&(t[e]=!0);e=Object.getPrototypeOf(e)}return t}(e)}catch(e){}for(var d in r)(d&&"_"!==d[0]||t&&t.includeUnderscoreProperties)&&a(e,i,d)}return o.originalObjects.pop(),o.newObjects.pop(),i}_registerProxyFunction(e,t){var r=this.nextProxyId++;return this.proxyFunctions["proxy"+r]=function(){return e.apply(t,Array.prototype.slice.call(arguments,0))},r}_customDeserializeObject(e,t){var r=this;if(!e)return null;var n=(e,n)=>{var s=e[n],i=typeof s;"__circularReferenceId"===n&&"number"===i?(t[s]=e,delete e[n]):"object"===i&&s&&(s.__proxyFunctionId?e[n]=function(){return r.invokeRemoteMethod("proxy"+s.__proxyFunctionId,"__proxyFunctions",Array.prototype.slice.call(arguments,0),{},{includeUnderscoreProperties:!0})}:s.__proxyDate?e[n]=new Date(s.__proxyDate):s.__circularReference?e[n]=t[s.__circularReference]:this._customDeserializeObject(s,t))};if(e instanceof Array)for(var s=0,i=e.length;s<i;s++)n(e,s);else if("object"==typeof e)for(var o in e)n(e,o);return e}}const i=new r,o=new class{_channels=[];constructor(){window.addEventListener("message",this._handleMessageReceived)}addChannel(e,t){const r=new s(e,t);return this._channels.push(r),r}removeChannel(e){this._channels=this._channels.filter((t=>t!==e))}_handleMessageReceived=e=>{let t;if("string"==typeof e.data)try{t=JSON.parse(e.data)}catch(e){}if(t){let r,n=!1;for(const s of this._channels)s.owns(e.source,e.origin,t)&&(r=s,n=s.onMessage(t)||n);r&&!n&&(window.console&&console.error(`No handler found on any channel for message: ${JSON.stringify(t)}`),t.instanceId&&r.error(t,new Error(`The registered object ${t.instanceId} could not be found.`)))}}};export{s as XDMChannel,r as XDMObjectRegistry,o as channelManager,i as globalObjectRegistry};