@archilogic/extension-sdk 0.4.0 → 0.5.1
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/CHANGELOG.md +23 -0
- package/dist/extension-sdk.cjs +1 -0
- package/dist/extension-sdk.js +342 -276
- package/dist/index.d.ts +137 -40
- package/package.json +7 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# @archilogic/extension-sdk
|
|
2
2
|
|
|
3
|
+
## 0.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4ea34c7: fix(extension-sdk): provide commonjs module too
|
|
8
|
+
|
|
9
|
+
As it's needed for microservices.
|
|
10
|
+
|
|
11
|
+
- 4ea34c7: fix: add main property to package.json
|
|
12
|
+
|
|
13
|
+
As typescript ignores exports if moduleResolution is "node".
|
|
14
|
+
|
|
15
|
+
## 0.5.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- ea832a1: feat(extension-sdk): add worker mode
|
|
20
|
+
- 2baadca: feat(extension-sdk): use space-graph-sdk
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- ea832a1: test(extension-sdk): add iframe integration test for extension-sdk
|
|
25
|
+
|
|
3
26
|
## 0.4.0
|
|
4
27
|
|
|
5
28
|
### Minor Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var Re=Object.defineProperty;var se=e=>{throw TypeError(e)};var Ne=(e,t,r)=>t in e?Re(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var y=(e,t,r)=>Ne(e,typeof t!="symbol"?t+"":t,r),ae=(e,t,r)=>t.has(e)||se("Cannot "+r);var s=(e,t,r)=>(ae(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>t.has(e)?se("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,r),M=(e,t,r,n)=>(ae(e,t,"write to private field"),n?n.call(e,r):t.set(e,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var _e=class extends Error{constructor(t,r){super(r);y(this,"code");this.name="PenpalError",this.code=t}},w=_e,ke=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof w?e.code:void 0}),Le=({name:e,message:t,stack:r,penpalCode:n})=>{const a=n?new w(n,t):new Error(t);return a.name=e,a.stack=r,a},Te=Symbol("Reply"),te,le,be=(le=class{constructor(e,t){y(this,"value");y(this,"transferables");d(this,te,Te);this.value=e,this.transferables=t==null?void 0:t.transferables}},te=new WeakMap,le),De=be,I="penpal",X=e=>typeof e=="object"&&e!==null,ve=e=>typeof e=="function",He=e=>X(e)&&e.namespace===I,H=e=>e.type==="SYN",q=e=>e.type==="ACK1",$=e=>e.type==="ACK2",ye=e=>e.type==="CALL",Me=e=>e.type==="REPLY",Oe=e=>e.type==="DESTROY",we=(e,t=[])=>{const r=[];for(const n of Object.keys(e)){const a=e[n];ve(a)?r.push([...t,n]):X(a)&&r.push(...we(a,[...t,n]))}return r},xe=(e,t)=>{const r=e.reduce((n,a)=>X(n)?n[a]:void 0,t);return ve(r)?r:void 0},N=e=>e.join("."),ie=(e,t,r)=>({namespace:I,channel:e,type:"REPLY",callId:t,isError:!0,...r instanceof Error?{value:ke(r),isSerializedErrorInstance:!0}:{value:r}}),ge=(e,t,r,n)=>{let a=!1;const h=async l=>{if(a||!ye(l))return;n==null||n(`Received ${N(l.methodPath)}() call`,l);const{methodPath:p,args:u,id:o}=l;let i,A;try{const f=xe(p,t);if(!f)throw new w("METHOD_NOT_FOUND",`Method \`${N(p)}\` is not found.`);let v=await f(...u);v instanceof De&&(A=v.transferables,v=await v.value),i={namespace:I,channel:r,type:"REPLY",callId:o,value:v}}catch(f){i=ie(r,o,f)}if(!a)try{n==null||n(`Sending ${N(p)}() reply`,i),e.sendMessage(i,A)}catch(f){throw f.name==="DataCloneError"&&(i=ie(r,o,f),n==null||n(`Sending ${N(p)}() reply`,i),e.sendMessage(i)),f}};return e.addMessageHandler(h),()=>{a=!0,e.removeMessageHandler(h)}},Fe=ge,he,Ee=((he=crypto.randomUUID)==null?void 0:he.bind(crypto))??(()=>new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")),We=Symbol("CallOptions"),re,ue,Ye=(ue=class{constructor(e){y(this,"transferables");y(this,"timeout");d(this,re,We);this.transferables=e==null?void 0:e.transferables,this.timeout=e==null?void 0:e.timeout}},re=new WeakMap,ue),$e=Ye,je=new Set(["apply","call","bind"]),Pe=(e,t,r=[])=>new Proxy(r.length?()=>{}:Object.create(null),{get(n,a){if(a!=="then")return r.length&&je.has(a)?Reflect.get(n,a):Pe(e,t,[...r,a])},apply(n,a,h){return e(r,h)}}),oe=e=>new w("CONNECTION_DESTROYED",`Method call ${N(e)}() failed due to destroyed connection`),Ue=(e,t,r)=>{let n=!1;const a=new Map,h=u=>{if(!Me(u))return;const{callId:o,value:i,isError:A,isSerializedErrorInstance:f}=u,v=a.get(o);v&&(a.delete(o),r==null||r(`Received ${N(v.methodPath)}() call`,u),A?v.reject(f?Le(i):i):v.resolve(i))};return e.addMessageHandler(h),{remoteProxy:Pe((u,o)=>{if(n)throw oe(u);const i=Ee(),A=o[o.length-1],f=A instanceof $e,{timeout:v,transferables:Q}=f?A:{},z=f?o.slice(0,-1):o;return new Promise((G,Y)=>{const Z=v!==void 0?window.setTimeout(()=>{a.delete(i),Y(new w("METHOD_CALL_TIMEOUT",`Method call ${N(u)}() timed out after ${v}ms`))},v):void 0;a.set(i,{methodPath:u,resolve:G,reject:Y,timeoutId:Z});try{const D={namespace:I,channel:t,type:"CALL",id:i,methodPath:u,args:z};r==null||r(`Sending ${N(u)}() call`,D),e.sendMessage(D,Q)}catch(D){Y(new w("TRANSMISSION_FAILED",D.message))}})},r),destroy:()=>{n=!0,e.removeMessageHandler(h);for(const{methodPath:u,reject:o,timeoutId:i}of a.values())clearTimeout(i),o(oe(u));a.clear()}}},Ve=Ue,Ke=()=>{let e,t;return{promise:new Promise((n,a)=>{e=n,t=a}),resolve:e,reject:t}},ze=Ke,Ge=class extends Error{constructor(e){super(`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`)}},O=Ge,ee="deprecated-penpal",Be=e=>X(e)&&"penpal"in e,Je=e=>e.split("."),de=e=>e.join("."),Ie=e=>new O(`Unexpected message to translate: ${JSON.stringify(e)}`),Xe=e=>{if(e.penpal==="syn")return{namespace:I,channel:void 0,type:"SYN",participantId:ee};if(e.penpal==="ack")return{namespace:I,channel:void 0,type:"ACK2"};if(e.penpal==="call")return{namespace:I,channel:void 0,type:"CALL",id:e.id,methodPath:Je(e.methodName),args:e.args};if(e.penpal==="reply")return e.resolution==="fulfilled"?{namespace:I,channel:void 0,type:"REPLY",callId:e.id,value:e.returnValue}:{namespace:I,channel:void 0,type:"REPLY",callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw Ie(e)},qe=e=>{if(q(e))return{penpal:"synAck",methodNames:e.methodPaths.map(de)};if(ye(e))return{penpal:"call",id:e.id,methodName:de(e.methodPath),args:e.args};if(Me(e))return e.isError?{penpal:"reply",id:e.callId,resolution:"rejected",...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:"reply",id:e.callId,resolution:"fulfilled",returnValue:e.value};throw Ie(e)},Qe=({messenger:e,methods:t,timeout:r,channel:n,log:a})=>{const h=Ee();let l;const p=[];let u=!1;const o=we(t),{promise:i,resolve:A,reject:f}=ze(),v=r!==void 0?setTimeout(()=>{f(new w("CONNECTION_TIMEOUT",`Connection timed out after ${r}ms`))},r):void 0,Q=()=>{for(const c of p)c()},z=()=>{if(u)return;p.push(Fe(e,t,n,a));const{remoteProxy:c,destroy:_}=Ve(e,n,a);p.push(_),clearTimeout(v),u=!0,A({remoteProxy:c,destroy:Q})},G=()=>{const c={namespace:I,type:"SYN",channel:n,participantId:h};a==null||a("Sending handshake SYN",c);try{e.sendMessage(c)}catch(_){f(new w("TRANSMISSION_FAILED",_.message))}},Y=c=>{if(a==null||a("Received handshake SYN",c),c.participantId===l&&l!==ee||(l=c.participantId,G(),!(h>l||l===ee)))return;const B={namespace:I,channel:n,type:"ACK1",methodPaths:o};a==null||a("Sending handshake ACK1",B);try{e.sendMessage(B)}catch(me){f(new w("TRANSMISSION_FAILED",me.message));return}},Z=c=>{a==null||a("Received handshake ACK1",c);const _={namespace:I,channel:n,type:"ACK2"};a==null||a("Sending handshake ACK2",_);try{e.sendMessage(_)}catch(B){f(new w("TRANSMISSION_FAILED",B.message));return}z()},D=c=>{a==null||a("Received handshake ACK2",c),z()},ne=c=>{H(c)&&Y(c),q(c)&&Z(c),$(c)&&D(c)};return e.addMessageHandler(ne),p.push(()=>e.removeMessageHandler(ne)),G(),i},Ze=Qe,et=e=>{let t=!1,r;return(...n)=>(t||(t=!0,r=e(...n)),r)},tt=et,ce=new WeakSet,rt=({messenger:e,methods:t={},timeout:r,channel:n,log:a})=>{if(!e)throw new w("INVALID_ARGUMENT","messenger must be defined");if(ce.has(e))throw new w("INVALID_ARGUMENT","A messenger can only be used for a single connection");ce.add(e);const h=[e.destroy],l=tt(o=>{if(o){const i={namespace:I,channel:n,type:"DESTROY"};try{e.sendMessage(i)}catch{}}for(const i of h)i();a==null||a("Connection destroyed")}),p=o=>He(o)&&o.channel===n;return{promise:(async()=>{try{e.initialize({log:a,validateReceivedMessage:p}),e.addMessageHandler(A=>{Oe(A)&&l(!1)});const{remoteProxy:o,destroy:i}=await Ze({messenger:e,methods:t,timeout:r,channel:n,log:a});return h.push(i),o}catch(o){throw l(!0),o}})(),destroy:()=>{l(!0)}}},Ae=rt,C,k,x,g,L,S,E,T,J,F,j,U,W,fe,nt=(fe=class{constructor({remoteWindow:e,allowedOrigins:t}){d(this,C);d(this,k);d(this,x);d(this,g);d(this,L);d(this,S,new Set);d(this,E);d(this,T,!1);y(this,"initialize",({log:e,validateReceivedMessage:t})=>{M(this,x,e),M(this,g,t),window.addEventListener("message",s(this,U))});y(this,"sendMessage",(e,t)=>{if(H(e)){const r=s(this,F).call(this,e);s(this,C).postMessage(e,{targetOrigin:r,transfer:t});return}if(q(e)||s(this,T)){const r=s(this,T)?qe(e):e,n=s(this,F).call(this,e);s(this,C).postMessage(r,{targetOrigin:n,transfer:t});return}if($(e)){const{port1:r,port2:n}=new MessageChannel;M(this,E,r),r.addEventListener("message",s(this,W)),r.start();const a=[n,...t||[]],h=s(this,F).call(this,e);s(this,C).postMessage(e,{targetOrigin:h,transfer:a});return}if(s(this,E)){s(this,E).postMessage(e,{transfer:t});return}throw new O("Port is undefined")});y(this,"addMessageHandler",e=>{s(this,S).add(e)});y(this,"removeMessageHandler",e=>{s(this,S).delete(e)});y(this,"destroy",()=>{window.removeEventListener("message",s(this,U)),s(this,j).call(this),s(this,S).clear()});d(this,J,e=>s(this,k).some(t=>t instanceof RegExp?t.test(e):t===e||t==="*"));d(this,F,e=>{if(H(e))return"*";if(!s(this,L))throw new O("Concrete remote origin not set");return s(this,L)==="null"&&s(this,k).includes("*")?"*":s(this,L)});d(this,j,()=>{var e,t;(e=s(this,E))==null||e.removeEventListener("message",s(this,W)),(t=s(this,E))==null||t.close(),M(this,E,void 0)});d(this,U,({source:e,origin:t,ports:r,data:n})=>{var a,h,l;if(e===s(this,C)&&(Be(n)&&((a=s(this,x))==null||a.call(this,"Please upgrade the child window to the latest version of Penpal."),M(this,T,!0),n=Xe(n)),!!((h=s(this,g))!=null&&h.call(this,n)))){if(!s(this,J).call(this,t)){(l=s(this,x))==null||l.call(this,`Received a message from origin \`${t}\` which did not match allowed origins \`[${s(this,k).join(", ")}]\``);return}if(H(n)&&(s(this,j).call(this),M(this,L,t)),$(n)&&!s(this,T)){if(M(this,E,r[0]),!s(this,E))throw new O("No port received on ACK2");s(this,E).addEventListener("message",s(this,W)),s(this,E).start()}for(const p of s(this,S))p(n)}});d(this,W,({data:e})=>{var t;if((t=s(this,g))!=null&&t.call(this,e))for(const r of s(this,S))r(e)});if(!e)throw new w("INVALID_ARGUMENT","remoteWindow must be defined");M(this,C,e),M(this,k,t!=null&&t.length?t:[window.origin])}},C=new WeakMap,k=new WeakMap,x=new WeakMap,g=new WeakMap,L=new WeakMap,S=new WeakMap,E=new WeakMap,T=new WeakMap,J=new WeakMap,F=new WeakMap,j=new WeakMap,U=new WeakMap,W=new WeakMap,fe),Ce=nt,m,V,b,P,K,R,pe,st=(pe=class{constructor({worker:e}){d(this,m);d(this,V);d(this,b,new Set);d(this,P);y(this,"initialize",({validateReceivedMessage:e})=>{M(this,V,e),s(this,m).addEventListener("message",s(this,R))});y(this,"sendMessage",(e,t)=>{if(H(e)||q(e)){s(this,m).postMessage(e,{transfer:t});return}if($(e)){const{port1:r,port2:n}=new MessageChannel;M(this,P,r),r.addEventListener("message",s(this,R)),r.start(),s(this,m).postMessage(e,{transfer:[n,...t||[]]});return}if(s(this,P)){s(this,P).postMessage(e,{transfer:t});return}throw new O("Port is undefined")});y(this,"addMessageHandler",e=>{s(this,b).add(e)});y(this,"removeMessageHandler",e=>{s(this,b).delete(e)});y(this,"destroy",()=>{s(this,m).removeEventListener("message",s(this,R)),s(this,K).call(this),s(this,b).clear()});d(this,K,()=>{var e,t;(e=s(this,P))==null||e.removeEventListener("message",s(this,R)),(t=s(this,P))==null||t.close(),M(this,P,void 0)});d(this,R,({ports:e,data:t})=>{var r;if((r=s(this,V))!=null&&r.call(this,t)){if(H(t)&&s(this,K).call(this),$(t)){if(M(this,P,e[0]),!s(this,P))throw new O("No port received on ACK2");s(this,P).addEventListener("message",s(this,R)),s(this,P).start()}for(const n of s(this,b))n(t)}});if(!e)throw new w("INVALID_ARGUMENT","worker must be defined");M(this,m,e)}},m=new WeakMap,V=new WeakMap,b=new WeakMap,P=new WeakMap,K=new WeakMap,R=new WeakMap,pe),Se=st;const at=(e,{iframe:t,worker:r})=>{let n;if(t)n=new Ce({remoteWindow:t.contentWindow,allowedOrigins:[/^http:\/\/localhost(:\d+)/,"https://extensions.archilogic.io","https://testing-extensions.archilogic.io"]});else if(r)n=new Se({worker:r});else throw Error("No iframe or worker to connect to");return Ae({messenger:n,methods:e})},it={onConnect:()=>{},onDestroy:()=>{},onLayoutChange:()=>{},onSelectionChange:()=>{},onGraphicDrag:()=>{},onCanvasClick:()=>{}},ot=(e={},t)=>{let r;t!=null&&t.useWorker?r=new Se({worker:self}):r=new Ce({remoteWindow:window.parent,allowedOrigins:[/^http:\/\/localhost(:\d+)/,/^https:\/\/(.*)app.archilogic.com/,/^https:\/\/(.*).archilogic-frontend.com/]});const n=Ae({messenger:r,methods:{...it,...e}});return new Proxy({},{get(h,l){return async(...p)=>(await n.promise)[l](...p)}})};exports.createExtensionConnection=at;exports.createHostConnection=ot;
|