azure-devops-extension-sdk
Version:
Azure DevOps web extension JavaScript library.
2 lines (1 loc) • 8.49 kB
JavaScript
const e=parseInt("10000000000",36),t=Number.MAX_SAFE_INTEGER||9007199254740991;class n{objects={};register(e,t){this.objects[e]=t}unregister(e){delete this.objects[e]}getInstance(e,t){var n=this.objects[e];if(n)return"function"==typeof n?n(t):n}}let r=1;class o{promises={};postToWindow;targetOrigin;handshakeToken;registry;channelId;nextMessageId=1;nextProxyId=1;proxyFunctions={};constructor(o,i){this.postToWindow=o,this.targetOrigin=i,this.registry=new n,this.channelId=r++,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,n,r,o){const i={id:this.nextMessageId++,methodName:e,instanceId:t,instanceContext:r,params:this._customSerializeObject(n,o),serializationSettings:o};this.targetOrigin||(i.handshakeToken=this.handshakeToken);const s=new Promise(((e,t)=>{this.promises[i.id]={resolve:e,reject:t}}));return this._sendRpcMessage(i),s}getRemoteObjectProxy(e,t){return this.invokeRemoteMethod("",e,void 0,t)}invokeMethod(e,t){if(t.methodName){var n=e[t.methodName];if("function"==typeof n)try{var r=[];t.params&&(r=this._customDeserializeObject(t.params,{}));var o=n.apply(e,r);o&&o.then&&"function"==typeof o.then?o.then((e=>{this._success(t,e,t.handshakeToken)}),(e=>{this.error(t,e)})):this._success(t,o,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 n=this.registry.getInstance(e,t);return n||(n=i.getInstance(e,t)),n}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,n){if(this.postToWindow===e){if(this.targetOrigin)return!!t&&("null"===t.toLowerCase()||0===this.targetOrigin.toLowerCase().indexOf(t.toLowerCase()));if(n.handshakeToken&&n.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,n){this._sendRpcMessage({id:e.id,result:this._customSerializeObject([t],e.serializationSettings)[0],handshakeToken:n})}_sendRpcMessage(e){this.postToWindow.postMessage(JSON.stringify(e),"*")}_customSerializeObject(e,t,n,r=1,o=1){if(!e||o>100)return;if(e instanceof Node||e instanceof Window||e instanceof Event)return;var i;let s;s=n||{newObjects:[],originalObjects:[]},s.originalObjects.push(e);var c=(n,i,c)=>{var a;try{a=n[c]}catch(e){}var h=typeof a;if("undefined"!==h){var d=-1;if("object"===h&&(d=s.originalObjects.indexOf(a)),d>=0){var u=s.newObjects[d];u.__circularReferenceId||(u.__circularReferenceId=r++),i[c]={__circularReference:u.__circularReferenceId}}else"function"===h?(this.nextProxyId++,i[c]={__proxyFunctionId:this._registerProxyFunction(a,e),_channelId:this.channelId}):"object"===h?a&&a instanceof Date?i[c]={__proxyDate:a.getTime()}:i[c]=this._customSerializeObject(a,t,s,r,o+1):"__proxyFunctionId"!==c&&(i[c]=a)}};if(e instanceof Array){i=[],s.newObjects.push(i);for(var a=0,h=e.length;a<h;a++)c(e,i,a)}else{i={},s.newObjects.push(i);let n={};try{n=function(e){const t={};for(;e&&e!==Object.prototype;){const n=Object.getOwnPropertyNames(e);for(const e of n)"constructor"!==e&&(t[e]=!0);e=Object.getPrototypeOf(e)}return t}(e)}catch(e){}for(var d in n)(d&&"_"!==d[0]||t&&t.includeUnderscoreProperties)&&c(e,i,d)}return s.originalObjects.pop(),s.newObjects.pop(),i}_registerProxyFunction(e,t){var n=this.nextProxyId++;return this.proxyFunctions["proxy"+n]=function(){return e.apply(t,Array.prototype.slice.call(arguments,0))},n}_customDeserializeObject(e,t){var n=this;if(!e)return null;var r=(e,r)=>{var o=e[r],i=typeof o;"__circularReferenceId"===r&&"number"===i?(t[o]=e,delete e[r]):"object"===i&&o&&(o.__proxyFunctionId?e[r]=function(){return n.invokeRemoteMethod("proxy"+o.__proxyFunctionId,"__proxyFunctions",Array.prototype.slice.call(arguments,0),{},{includeUnderscoreProperties:!0})}:o.__proxyDate?e[r]=new Date(o.__proxyDate):o.__circularReference?e[r]=t[o.__circularReference]:this._customDeserializeObject(o,t))};if(e instanceof Array)for(var o=0,i=e.length;o<i;o++)r(e,o);else if("object"==typeof e)for(var s in e)r(e,s);return e}}const i=new n,s=new class{_channels=[];constructor(){window.addEventListener("message",this._handleMessageReceived)}addChannel(e,t){const n=new o(e,t);return this._channels.push(n),n}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 n,r=!1;for(const o of this._channels)o.owns(e.source,e.origin,t)&&(n=o,r=o.onMessage(t)||r);n&&!r&&(window.console&&console.error(`No handler found on any channel for message: ${JSON.stringify(t)}`),t.instanceId&&n.error(t,new Error(`The registered object ${t.instanceId} could not be found.`)))}}},c=4,a=window;var h;a._AzureDevOpsSDKVersion&&console.error("The AzureDevOps SDK is already loaded. Only one version of this module can be loaded in a given document."),a._AzureDevOpsSDKVersion=4,function(e){e[e.Unknown=0]="Unknown",e[e.Deployment=1]="Deployment",e[e.Enterprise=2]="Enterprise",e[e.Organization=4]="Organization"}(h||(h={}));const d="DevOps.HostControl",u=s.addChannel(window.parent);let l,f,g,p,m,y,_,v,w,b;const O=new Promise((e=>{b=e}));function k(e,t){const n=window;let r;"function"==typeof n.CustomEvent?r=new n.CustomEvent(e,t):(t=t||{bubbles:!1,cancelable:!1},r=document.createEvent("CustomEvent"),r.initCustomEvent(e,t.bubbles,t.cancelable,t.detail)),window.dispatchEvent(r)}function x(e){return new Promise((t=>{const n={...e,sdkVersion:4};u.invokeRemoteMethod("initialHandshake",d,[n]).then((e=>{const n=e.context;g=n.pageContext,f=g?g.webContext:void 0,l=f?f.team:void 0,m=e.initialConfig||{},y=e.contributionId,p=n.extension,_=n.user,v=n.host,e.themeData&&(J(e.themeData),window.addEventListener("themeChanged",(e=>{J(e.detail.data)}))),b(),t()}))}))}async function j(){return O}function R(){return u.invokeRemoteMethod("notifyLoadSucceeded",d)}function M(e){return u.invokeRemoteMethod("notifyLoadFailed",d,[e])}function I(e){return`Attempted to call ${e}() before init() was complete. Wait for init to complete or place within a ready() callback.`}function E(){if(!m)throw new Error(I("getConfiguration"));return m}function C(){if(!y)throw new Error(I("getContributionId"));return y}function T(){if(!_)throw new Error(I("getUser"));return _}function D(){if(!v)throw new Error(I("getHost"));return v}function S(){if(!p)throw new Error(I("getExtensionContext"));return p}function z(){if(!l)throw new Error(I("getTeamContext"));return l}function P(){if(!g)throw new Error(I("getPageContext"));return g}function A(){if(!f)throw new Error(I("getWebContext"));return f}async function F(e){return j().then((()=>u.invokeRemoteMethod("getService","DevOps.ServiceManager",[e])))}function N(e,t){u.getObjectRegistry().register(e,t)}function W(e){u.getObjectRegistry().unregister(e)}async function L(){return u.invokeRemoteMethod("getAccessToken",d).then((e=>e.token))}async function U(){return u.invokeRemoteMethod("getAppToken",d).then((e=>e.token))}function H(e,t){const n=document.body;if(n){const r="number"==typeof e?e:n?n.scrollWidth:void 0,o="number"==typeof t?t:n?n.scrollHeight:void 0;u.invokeRemoteMethod("resize",d,[r,o])}}function J(e){w||(w=document.createElement("style"),w.type="text/css",document.head.appendChild(w));const t=[];if(e)for(const n in e)t.push("--"+n+": "+e[n]);w.innerText=":root { "+t.join("; ")+" } body { color: var(--text-primary-color) }",k("themeApplied",{detail:e})}u.getObjectRegistry().register("DevOps.SdkClient",{dispatchEvent:k});export{h as HostType,J as applyTheme,L as getAccessToken,U as getAppToken,E as getConfiguration,C as getContributionId,S as getExtensionContext,D as getHost,P as getPageContext,F as getService,z as getTeamContext,T as getUser,A as getWebContext,x as init,M as notifyLoadFailed,R as notifyLoadSucceeded,j as ready,N as register,H as resize,c as sdkVersion,W as unregister};