@web-applets/sdk 0.2.0 → 0.2.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.
Files changed (32) hide show
  1. package/dist/elements/applet-frame.js +2 -99
  2. package/dist/{applets → sdk/src/applets}/applet-scope.js +10 -6
  3. package/dist/sdk/src/debug.js +9 -0
  4. package/dist/sdk/src/elements/applet-frame.js +111 -0
  5. package/dist/utils/common-utils.d.mts +1 -0
  6. package/dist/utils/common-utils.mjs +3 -0
  7. package/dist/web-applets.min.js +2 -2
  8. package/package.json +7 -4
  9. package/dist/debug.js +0 -6
  10. /package/dist/{applets → sdk/src/applets}/actions.d.ts +0 -0
  11. /package/dist/{applets → sdk/src/applets}/actions.js +0 -0
  12. /package/dist/{applets → sdk/src/applets}/applet-factory.d.ts +0 -0
  13. /package/dist/{applets → sdk/src/applets}/applet-factory.js +0 -0
  14. /package/dist/{applets → sdk/src/applets}/applet-scope.d.ts +0 -0
  15. /package/dist/{applets → sdk/src/applets}/applet.d.ts +0 -0
  16. /package/dist/{applets → sdk/src/applets}/applet.js +0 -0
  17. /package/dist/{applets → sdk/src/applets}/errors.d.ts +0 -0
  18. /package/dist/{applets → sdk/src/applets}/errors.js +0 -0
  19. /package/dist/{applets → sdk/src/applets}/events.d.ts +0 -0
  20. /package/dist/{applets → sdk/src/applets}/events.js +0 -0
  21. /package/dist/{constants.d.ts → sdk/src/constants.d.ts} +0 -0
  22. /package/dist/{constants.js → sdk/src/constants.js} +0 -0
  23. /package/dist/{debug.d.ts → sdk/src/debug.d.ts} +0 -0
  24. /package/dist/{elements → sdk/src/elements}/applet-frame.d.ts +0 -0
  25. /package/dist/{index.d.ts → sdk/src/index.d.ts} +0 -0
  26. /package/dist/{index.js → sdk/src/index.js} +0 -0
  27. /package/dist/{messages.d.ts → sdk/src/messages.d.ts} +0 -0
  28. /package/dist/{messages.js → sdk/src/messages.js} +0 -0
  29. /package/dist/{polyfill.d.ts → sdk/src/polyfill.d.ts} +0 -0
  30. /package/dist/{polyfill.js → sdk/src/polyfill.js} +0 -0
  31. /package/dist/{utils.d.ts → sdk/src/utils.d.ts} +0 -0
  32. /package/dist/{utils.js → sdk/src/utils.js} +0 -0
@@ -1,69 +1,4 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _AppletFrameElement_instances, _AppletFrameElement_root, _AppletFrameElement_src, _AppletFrameElement_applet, _AppletFrameElement_dispatchEventAndHandler, _AppletFrameElement_iframe, _AppletFrameElement_loadApplet, _AppletFrameElement_resizeContainer;
13
- import { applets } from '../index.js';
14
- import { dispatchEventAndHandler } from '../utils.js';
15
- export class AppletFrameElement extends HTMLElement {
16
- constructor() {
17
- super(...arguments);
18
- _AppletFrameElement_instances.add(this);
19
- _AppletFrameElement_root.set(this, void 0);
20
- _AppletFrameElement_src.set(this, void 0);
21
- _AppletFrameElement_applet.set(this, void 0);
22
- _AppletFrameElement_dispatchEventAndHandler.set(this, dispatchEventAndHandler.bind(this));
23
- _AppletFrameElement_iframe.set(this, document.createElement('iframe'));
24
- }
25
- connectedCallback() {
26
- __classPrivateFieldSet(this, _AppletFrameElement_root, this.attachShadow({ mode: 'closed' }), "f");
27
- __classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f"));
28
- const styles = document.createElement('style');
29
- styles.textContent = this.styles;
30
- __classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(styles);
31
- this.src = this.getAttribute('src');
32
- }
33
- get contentWindow() {
34
- return __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
35
- }
36
- set src(value) {
37
- __classPrivateFieldSet(this, _AppletFrameElement_src, value, "f");
38
- __classPrivateFieldSet(this, _AppletFrameElement_applet, undefined, "f");
39
- __classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_loadApplet).call(this, value);
40
- }
41
- get src() {
42
- return __classPrivateFieldGet(this, _AppletFrameElement_src, "f") || '';
43
- }
44
- attributeChangedCallback(name, oldValue, newValue) {
45
- if (name === 'src') {
46
- this.src = newValue;
47
- }
48
- }
49
- get applet() {
50
- return __classPrivateFieldGet(this, _AppletFrameElement_applet, "f");
51
- }
52
- set data(data) {
53
- if (this.applet) {
54
- this.applet.data = data;
55
- }
56
- else {
57
- const listener = () => {
58
- if (this.applet)
59
- this.applet.data = data;
60
- this.removeEventListener('load', listener);
61
- };
62
- this.addEventListener('load', listener);
63
- }
64
- }
65
- get styles() {
66
- return /*css*/ `
1
+ (()=>{var d=class extends Error{constructor(t){super(t),this.name="AppletExecutionError"}},m=class extends Error{constructor(t){super(t),this.name="AppletConnectionError"}};function c(o){typeof this[`on${o.type}`]=="function"&&this[`on${o.type}`](o),this.dispatchEvent(o)}var r=class extends Event{constructor(t,e){super(t,{bubbles:e?.bubbles,composed:e?.composed,cancelable:e?.cancelable}),this.data=e?.data,this.actions=e?.actions}};function T(o,...t){}var a={log:T};var A=class extends EventTarget{#s;#o={};#t;#i;#n;#e;#a;#r;#p;constructor(t){super(),a.log("Applet","Constructor called"),this.#s=t,this.#n=c.bind(this),this.#c();let e=s=>{s.source===this.#s&&"type"in s.data&&s.data.type==="appletconnect"&&(a.log("Applet","Recieved message",s.data),this.#c(),this.removeEventListener("message",e))};window.addEventListener("message",e)}#c(){this.#e&&this.#e.close();let t=new MessageChannel,e={type:"appletconnect"};a.log("Applet","Send message",e),this.#e=t.port1,this.#e.onmessage=this.#h.bind(this),this.#s.postMessage(e,"*",[t.port2]),this.#a=this.#e.postMessage.bind(this.#e)}#h(t){let e=t.data;switch(a.log("Applet","Recieved message",e),e.type){case"register":let s=e;this.#t=s.manifest;let n=new r("connect");this.#n(n),this.#o=s.actions,this.#d(s.actions),this.#i=s.data,this.#l(s.data);break;case"data":let i=e;this.#i=i.data,this.#l(i.data);break;case"resize":let p=e;this.#r=p.width,this.#p=p.height;let h=new r("resize");this.#n(h);break;case"actions":let g=e;this.#o=g.actions,this.#d(g.actions);break}}#l(t){let e=new r("data",{data:t});this.#n(e)}#d(t){let e=new r("actions",{actions:t});this.#n(e)}async sendAction(t,e){let s={id:crypto.randomUUID(),type:"action",actionId:t,arguments:e};return new Promise((n,i)=>{this.#a(s);let p=setTimeout(()=>{i(new d(`Applet action handler failed to complete before timeout (${1e4}ms)`))},1e4),h=g=>{let l=g.data;if(["actioncomplete","actionerror"].includes(l.type)&&"id"in l&&l.id===s.id)if(this.#e.removeEventListener("message",h),clearTimeout(p),l.type==="actionerror"){let w=l;i(new d(w.message))}else n()};this.#e.addEventListener("message",h)})}get data(){return this.#i}set data(t){this.#i=t;let e={type:"data",data:t};this.#a(e)}get window(){return this.#s}get manifest(){return this.#t}get actions(){return this.#o}get width(){return this.#r}get height(){return this.#p}};function v(o){return Object.keys(o).length===0}var u=class extends EventTarget{#s={};#o;#t;#i;#n;#e;#a;#r;constructor(t){super(),a.log("AppletScope","Constructor called"),this.#n=c.bind(this),t&&(this.#o=t);let e=n=>{if(n.source===window.parent&&n.data.type==="appletconnect"&&n.ports&&n.ports.length>0){a.log("AppletScope","Recieved message",n.data);let i=n.ports[0];this.#e=i.postMessage.bind(i),i.onmessage=this.#c.bind(this),this.removeEventListener("message",e),this.#p()}};window.addEventListener("message",e);let s={type:"appletconnect"};window.parent.postMessage(s,"*"),a.log("AppletScope","Send message",s)}async#p(){let t=this.manifest??await this.#g();this.#o=t||{},this.#t=this.#t||t?.actions||{};let e={type:"register",manifest:this.#o,actions:this.#t,data:this.#i};this.#e(e),a.log("AppletScope","Send message",e);let s=new r("connect");this.#n(s),this.#l()}#c(t){let e=t.data;switch(a.log("AppletScope","Recieved message",e),e.type){case"data":"data"in e&&(this.data=e.data);break;case"action":"type"in e&&e.type==="action"&&"id"in e&&typeof e.id=="string"&&"actionId"in e&&typeof e.actionId=="string"&&"arguments"in e&&this.#h(e);break}}async#h(t){let{actionId:e,arguments:s,id:n}=t;if(Object.keys(this.#s).includes(e))try{await this.#s[e](s);let i={type:"actioncomplete",id:n};this.#e(i)}catch(i){let p={type:"actionerror",id:n,message:i.message};this.#e(p),console.error(i)}}#l(){new ResizeObserver(e=>{for(let s of e)this.#d({width:s.contentRect.width,height:s.contentRect.height})}).observe(document.querySelector("html"))}#d({width:t,height:e}){this.#a=t,this.#r=e;let s={type:"resize",width:t,height:e};a.log("AppletScope","Send message",s),this.#e(s)}async#g(){let t=document.querySelector('link[rel="manifest"]');if(!t){console.warn("No manifest link found");return}try{let s=await(await fetch(t.href)).json();for(let n in s.actions){let i=s.actions[n];i.params_schema&&!v(this.#t.params_schema)&&(i.params_schema=void 0)}return s}catch{return}}setActionHandler(t,e){this.#s[t]=e}defineAction(t,e){let{handler:s,...n}=e;s&&(this.#s[t]=s),this.actions={...this.actions,[t]:n}}set actions(t){if(!t)return;this.#t=t;let e={type:"actions",actions:this.#t};a.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new r("actions",{actions:t});setTimeout(()=>this.#n(s),1)}get actions(){return this.#t}get manifest(){return this.#o}get actionHandlers(){return this.#s}set actionHandlers(t){this.#s=t}set data(t){this.#i=t;let e={type:"data",data:t};a.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new r("data",{data:t});setTimeout(()=>this.#n(s),1)}get data(){return this.#i}get width(){return this.#a}get height(){return this.#r}};var f=class{async connect(t){return new Promise((e,s)=>{let n=new A(t),i=setTimeout(()=>{s(new m(`Applet failed to connect before the timeout was reached (${1e4}ms)`))},1e4),p=()=>{e(n),n.removeEventListener("connect",p),clearTimeout(i)};n.addEventListener("connect",p)})}register(t){return new u(t)}};var b=new f;var E=class extends HTMLElement{#s;#o;#t;#i=c.bind(this);#n=document.createElement("iframe");static{this.observedAttributes=["src"]}connectedCallback(){this.#s=this.attachShadow({mode:"closed"}),this.#s.appendChild(this.#n);let t=document.createElement("style");t.textContent=this.styles,this.#s.appendChild(t),this.src=this.getAttribute("src")}get contentWindow(){return this.#n.contentWindow}set src(t){this.#o=t,this.#t=void 0,this.#e(t)}get src(){return this.#o||""}attributeChangedCallback(t,e,s){t==="src"&&(this.src=s)}async#e(t){this.#n.src=t;let e=this.#n.contentWindow;e&&(this.#t=await b.connect(e),this.#t.ondata=s=>{this.#i(s)},this.#t.onresize=s=>{this.#a({width:this.#t.width,height:this.#t.height})},this.#t.onactions=s=>{this.#i(s)},this.#i(new Event("load")),this.load&&typeof this.load=="function"&&this.onload(new Event("load")))}get applet(){return this.#t}set data(t){if(this.applet)this.applet.data=t;else{let e=()=>{this.applet&&(this.applet.data=t),this.removeEventListener("load",e)};this.addEventListener("load",e)}}#a(t){this.style.height=`${t.height}px`}get styles(){return`
67
2
  :host {
68
3
  background: white;
69
4
  display: flex;
@@ -76,36 +11,4 @@ export class AppletFrameElement extends HTMLElement {
76
11
  height: 100%;
77
12
  width: 100%;
78
13
  }
79
- `;
80
- }
81
- }
82
- _AppletFrameElement_root = new WeakMap(), _AppletFrameElement_src = new WeakMap(), _AppletFrameElement_applet = new WeakMap(), _AppletFrameElement_dispatchEventAndHandler = new WeakMap(), _AppletFrameElement_iframe = new WeakMap(), _AppletFrameElement_instances = new WeakSet(), _AppletFrameElement_loadApplet = async function _AppletFrameElement_loadApplet(url) {
83
- __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").src = url;
84
- const window = __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
85
- if (!window)
86
- return;
87
- __classPrivateFieldSet(this, _AppletFrameElement_applet, await applets.connect(window), "f");
88
- // When data received, bubble the event up
89
- __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").ondata = (event) => {
90
- __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
91
- };
92
- // Resize
93
- __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onresize = (event) => {
94
- __classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_resizeContainer).call(this, {
95
- width: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").width,
96
- height: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").height,
97
- });
98
- };
99
- __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onactions = (event) => {
100
- __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
101
- };
102
- // Emit load event when setup & connection complete
103
- __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, new Event('load'));
104
- if (this['load'] && typeof this['load'] === 'function') {
105
- this.onload(new Event('load'));
106
- }
107
- }, _AppletFrameElement_resizeContainer = function _AppletFrameElement_resizeContainer(dimensions) {
108
- this.style.height = `${dimensions.height}px`;
109
- };
110
- AppletFrameElement.observedAttributes = ['src'];
111
- customElements.define('applet-frame', AppletFrameElement);
14
+ `}};customElements.define("applet-frame",E);})();
@@ -13,6 +13,7 @@ var _AppletScope_instances, _AppletScope_actionHandlers, _AppletScope_manifest,
13
13
  import { debug } from '../debug.js';
14
14
  import { AppletEvent } from './events.js';
15
15
  import { dispatchEventAndHandler } from '../utils.js';
16
+ import { isEmpty } from '../../../utils/common-utils.mjs';
16
17
  export class AppletScope extends EventTarget {
17
18
  constructor(manifest) {
18
19
  super();
@@ -149,19 +150,20 @@ _AppletScope_actionHandlers = new WeakMap(), _AppletScope_manifest = new WeakMap
149
150
  break;
150
151
  }
151
152
  }, _AppletScope_handleActionMessage = async function _AppletScope_handleActionMessage(message) {
152
- if (Object.keys(__classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")).includes(message.actionId)) {
153
+ const { actionId, arguments: args, id } = message;
154
+ if (Object.keys(__classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")).includes(actionId)) {
153
155
  try {
154
- await __classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")[message.actionId](message.arguments);
156
+ await __classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")[actionId](args);
155
157
  const actionCompleteMessage = {
156
158
  type: 'actioncomplete',
157
- id: message.id,
159
+ id,
158
160
  };
159
161
  __classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, actionCompleteMessage);
160
162
  }
161
163
  catch (e) {
162
164
  const actionErrorMessage = {
163
165
  type: 'actionerror',
164
- id: message.id,
166
+ id,
165
167
  message: e.message,
166
168
  };
167
169
  __classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, actionErrorMessage);
@@ -189,15 +191,17 @@ _AppletScope_actionHandlers = new WeakMap(), _AppletScope_manifest = new WeakMap
189
191
  __classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, resizeMessage);
190
192
  }, _AppletScope_loadManifest = async function _AppletScope_loadManifest() {
191
193
  const manifestLinkElem = document.querySelector('link[rel="manifest"]');
192
- if (!manifestLinkElem)
194
+ if (!manifestLinkElem) {
195
+ console.warn('No manifest link found');
193
196
  return;
197
+ }
194
198
  // TODO: Add timeout
195
199
  try {
196
200
  const manifestRequest = await fetch(manifestLinkElem.href);
197
201
  const manifest = (await manifestRequest.json());
198
202
  for (const key in manifest.actions) {
199
203
  const action = manifest.actions[key];
200
- if (action.params_schema && !Object.keys(action.params_schema).length) {
204
+ if (action.params_schema && !isEmpty(__classPrivateFieldGet(this, _AppletScope_actions, "f").params_schema)) {
201
205
  action.params_schema = undefined;
202
206
  }
203
207
  }
@@ -0,0 +1,9 @@
1
+ const isProduction = process.env.NODE_ENV === "production";
2
+ function log(location, ...messages) {
3
+ if (!isProduction) {
4
+ console.log(`[${location}]`, ...messages);
5
+ }
6
+ }
7
+ export const debug = {
8
+ log,
9
+ };
@@ -0,0 +1,111 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _AppletFrameElement_instances, _AppletFrameElement_root, _AppletFrameElement_src, _AppletFrameElement_applet, _AppletFrameElement_dispatchEventAndHandler, _AppletFrameElement_iframe, _AppletFrameElement_loadApplet, _AppletFrameElement_resizeContainer;
13
+ import { applets } from '../index.js';
14
+ import { dispatchEventAndHandler } from '../utils.js';
15
+ export class AppletFrameElement extends HTMLElement {
16
+ constructor() {
17
+ super(...arguments);
18
+ _AppletFrameElement_instances.add(this);
19
+ _AppletFrameElement_root.set(this, void 0);
20
+ _AppletFrameElement_src.set(this, void 0);
21
+ _AppletFrameElement_applet.set(this, void 0);
22
+ _AppletFrameElement_dispatchEventAndHandler.set(this, dispatchEventAndHandler.bind(this));
23
+ _AppletFrameElement_iframe.set(this, document.createElement('iframe'));
24
+ }
25
+ connectedCallback() {
26
+ __classPrivateFieldSet(this, _AppletFrameElement_root, this.attachShadow({ mode: 'closed' }), "f");
27
+ __classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f"));
28
+ const styles = document.createElement('style');
29
+ styles.textContent = this.styles;
30
+ __classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(styles);
31
+ this.src = this.getAttribute('src');
32
+ }
33
+ get contentWindow() {
34
+ return __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
35
+ }
36
+ set src(value) {
37
+ __classPrivateFieldSet(this, _AppletFrameElement_src, value, "f");
38
+ __classPrivateFieldSet(this, _AppletFrameElement_applet, undefined, "f");
39
+ __classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_loadApplet).call(this, value);
40
+ }
41
+ get src() {
42
+ return __classPrivateFieldGet(this, _AppletFrameElement_src, "f") || '';
43
+ }
44
+ attributeChangedCallback(name, oldValue, newValue) {
45
+ if (name === 'src') {
46
+ this.src = newValue;
47
+ }
48
+ }
49
+ get applet() {
50
+ return __classPrivateFieldGet(this, _AppletFrameElement_applet, "f");
51
+ }
52
+ set data(data) {
53
+ if (this.applet) {
54
+ this.applet.data = data;
55
+ }
56
+ else {
57
+ const listener = () => {
58
+ if (this.applet)
59
+ this.applet.data = data;
60
+ this.removeEventListener('load', listener);
61
+ };
62
+ this.addEventListener('load', listener);
63
+ }
64
+ }
65
+ get styles() {
66
+ return /*css*/ `
67
+ :host {
68
+ background: white;
69
+ display: flex;
70
+ flex-direction: column;
71
+ height: 350px;
72
+ }
73
+
74
+ iframe {
75
+ border: none;
76
+ height: 100%;
77
+ width: 100%;
78
+ }
79
+ `;
80
+ }
81
+ }
82
+ _AppletFrameElement_root = new WeakMap(), _AppletFrameElement_src = new WeakMap(), _AppletFrameElement_applet = new WeakMap(), _AppletFrameElement_dispatchEventAndHandler = new WeakMap(), _AppletFrameElement_iframe = new WeakMap(), _AppletFrameElement_instances = new WeakSet(), _AppletFrameElement_loadApplet = async function _AppletFrameElement_loadApplet(url) {
83
+ __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").src = url;
84
+ const window = __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
85
+ if (!window)
86
+ return;
87
+ __classPrivateFieldSet(this, _AppletFrameElement_applet, await applets.connect(window), "f");
88
+ // When data received, bubble the event up
89
+ __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").ondata = (event) => {
90
+ __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
91
+ };
92
+ // Resize
93
+ __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onresize = (event) => {
94
+ __classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_resizeContainer).call(this, {
95
+ width: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").width,
96
+ height: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").height,
97
+ });
98
+ };
99
+ __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onactions = (event) => {
100
+ __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
101
+ };
102
+ // Emit load event when setup & connection complete
103
+ __classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, new Event('load'));
104
+ if (this['load'] && typeof this['load'] === 'function') {
105
+ this.onload(new Event('load'));
106
+ }
107
+ }, _AppletFrameElement_resizeContainer = function _AppletFrameElement_resizeContainer(dimensions) {
108
+ this.style.height = `${dimensions.height}px`;
109
+ };
110
+ AppletFrameElement.observedAttributes = ['src'];
111
+ customElements.define('applet-frame', AppletFrameElement);
@@ -0,0 +1 @@
1
+ export declare function isEmpty(obj: object): boolean;
@@ -0,0 +1,3 @@
1
+ export function isEmpty(obj) {
2
+ return Object.keys(obj).length === 0;
3
+ }
@@ -1,4 +1,4 @@
1
- (()=>{var h=class extends Error{constructor(e){super(e),this.name="AppletExecutionError"}},A=class extends Error{constructor(e){super(e),this.name="AppletConnectionError"}};function c(p){typeof this[`on${p.type}`]=="function"&&this[`on${p.type}`](p),this.dispatchEvent(p)}var o=class extends Event{constructor(e,t){super(e,{bubbles:t?.bubbles,composed:t?.composed,cancelable:t?.cancelable}),this.data=t?.data,this.actions=t?.actions}};function b(p,...e){console.log(`[${p}]`,...e)}var a={log:b};var f=class extends EventTarget{#s;#o={};#t;#i;#n;#e;#a;#p;#r;constructor(e){super(),a.log("Applet","Constructor called"),this.#s=e,this.#n=c.bind(this),this.#c();let t=s=>{s.source===this.#s&&"type"in s.data&&s.data.type==="appletconnect"&&(a.log("Applet","Recieved message",s.data),this.#c(),this.removeEventListener("message",t))};window.addEventListener("message",t)}#c(){this.#e&&this.#e.close();let e=new MessageChannel,t={type:"appletconnect"};a.log("Applet","Send message",t),this.#e=e.port1,this.#e.onmessage=this.#h.bind(this),this.#s.postMessage(t,"*",[e.port2]),this.#a=this.#e.postMessage.bind(this.#e)}#h(e){let t=e.data;switch(a.log("Applet","Recieved message",t),t.type){case"register":let s=t;this.#t=s.manifest;let n=new o("connect");this.#n(n),this.#o=s.actions,this.#d(s.actions),this.#i=s.data,this.#l(s.data);break;case"data":let i=t;this.#i=i.data,this.#l(i.data);break;case"resize":let r=t;this.#p=r.width,this.#r=r.height;let g=new o("resize");this.#n(g);break;case"actions":let m=t;this.#o=m.actions,this.#d(m.actions);break}}#l(e){let t=new o("data",{data:e});this.#n(t)}#d(e){let t=new o("actions",{actions:e});this.#n(t)}async sendAction(e,t){let s={id:crypto.randomUUID(),type:"action",actionId:e,arguments:t};return new Promise((n,i)=>{this.#a(s);let r=setTimeout(()=>{i(new h(`Applet action handler failed to complete before timeout (${1e4}ms)`))},1e4),g=m=>{let d=m.data;if(["actioncomplete","actionerror"].includes(d.type)&&"id"in d&&d.id===s.id)if(this.#e.removeEventListener("message",g),clearTimeout(r),d.type==="actionerror"){let w=d;i(new h(w.message))}else n()};this.#e.addEventListener("message",g)})}get data(){return this.#i}set data(e){this.#i=e;let t={type:"data",data:e};this.#a(t)}get window(){return this.#s}get manifest(){return this.#t}get actions(){return this.#o}get width(){return this.#p}get height(){return this.#r}};var E=class extends EventTarget{#s={};#o;#t;#i;#n;#e;#a;#p;constructor(e){super(),a.log("AppletScope","Constructor called"),this.#n=c.bind(this),e&&(this.#o=e);let t=n=>{if(n.source===window.parent&&n.data.type==="appletconnect"&&n.ports&&n.ports.length>0){a.log("AppletScope","Recieved message",n.data);let i=n.ports[0];this.#e=i.postMessage.bind(i),i.onmessage=this.#c.bind(this),this.removeEventListener("message",t),this.#r()}};window.addEventListener("message",t);let s={type:"appletconnect"};window.parent.postMessage(s,"*"),a.log("AppletScope","Send message",s)}async#r(){let e=this.manifest??await this.#g();this.#o=e||{},this.#t=this.#t||e?.actions||{};let t={type:"register",manifest:this.#o,actions:this.#t,data:this.#i};this.#e(t),a.log("AppletScope","Send message",t);let s=new o("connect");this.#n(s),this.#l()}#c(e){let t=e.data;switch(a.log("AppletScope","Recieved message",t),t.type){case"data":"data"in t&&(this.data=t.data);break;case"action":"type"in t&&t.type==="action"&&"id"in t&&typeof t.id=="string"&&"actionId"in t&&typeof t.actionId=="string"&&"arguments"in t&&this.#h(t);break}}async#h(e){if(Object.keys(this.#s).includes(e.actionId))try{await this.#s[e.actionId](e.arguments);let t={type:"actioncomplete",id:e.id};this.#e(t)}catch(t){let s={type:"actionerror",id:e.id,message:t.message};this.#e(s),console.error(t)}}#l(){new ResizeObserver(t=>{for(let s of t)this.#d({width:s.contentRect.width,height:s.contentRect.height})}).observe(document.querySelector("html"))}#d({width:e,height:t}){this.#a=e,this.#p=t;let s={type:"resize",width:e,height:t};a.log("AppletScope","Send message",s),this.#e(s)}async#g(){let e=document.querySelector('link[rel="manifest"]');if(e)try{let s=await(await fetch(e.href)).json();for(let n in s.actions){let i=s.actions[n];i.params_schema&&!Object.keys(i.params_schema).length&&(i.params_schema=void 0)}return s}catch{return}}setActionHandler(e,t){this.#s[e]=t}defineAction(e,t){let{handler:s,...n}=t;s&&(this.#s[e]=s),this.actions={...this.actions,[e]:n}}set actions(e){if(!e)return;this.#t=e;let t={type:"actions",actions:this.#t};a.log("AppletScope","Send message",t),this.#e&&this.#e(t);let s=new o("actions",{actions:e});setTimeout(()=>this.#n(s),1)}get actions(){return this.#t}get manifest(){return this.#o}get actionHandlers(){return this.#s}set actionHandlers(e){this.#s=e}set data(e){this.#i=e;let t={type:"data",data:e};a.log("AppletScope","Send message",t),this.#e&&this.#e(t);let s=new o("data",{data:e});setTimeout(()=>this.#n(s),1)}get data(){return this.#i}get width(){return this.#a}get height(){return this.#p}};var u=class{async connect(e){return new Promise((t,s)=>{let n=new f(e),i=setTimeout(()=>{s(new A(`Applet failed to connect before the timeout was reached (${1e4}ms)`))},1e4),r=()=>{t(n),n.removeEventListener("connect",r),clearTimeout(i)};n.addEventListener("connect",r)})}register(e){return new E(e)}};var l=class extends HTMLElement{#s;#o;#t;#i=c.bind(this);#n=document.createElement("iframe");static{this.observedAttributes=["src"]}connectedCallback(){this.#s=this.attachShadow({mode:"closed"}),this.#s.appendChild(this.#n);let e=document.createElement("style");e.textContent=this.styles,this.#s.appendChild(e),this.src=this.getAttribute("src")}get contentWindow(){return this.#n.contentWindow}set src(e){this.#o=e,this.#t=void 0,this.#e(e)}get src(){return this.#o||""}attributeChangedCallback(e,t,s){e==="src"&&(this.src=s)}async#e(e){this.#n.src=e;let t=this.#n.contentWindow;t&&(this.#t=await y.connect(t),this.#t.ondata=s=>{this.#i(s)},this.#t.onresize=s=>{this.#a({width:this.#t.width,height:this.#t.height})},this.#t.onactions=s=>{this.#i(s)},this.#i(new Event("load")),this.load&&typeof this.load=="function"&&this.onload(new Event("load")))}get applet(){return this.#t}set data(e){if(this.applet)this.applet.data=e;else{let t=()=>{this.applet&&(this.applet.data=e),this.removeEventListener("load",t)};this.addEventListener("load",t)}}#a(e){this.style.height=`${e.height}px`}get styles(){return`
1
+ (()=>{var h=class extends Error{constructor(t){super(t),this.name="AppletExecutionError"}},A=class extends Error{constructor(t){super(t),this.name="AppletConnectionError"}};function c(o){typeof this[`on${o.type}`]=="function"&&this[`on${o.type}`](o),this.dispatchEvent(o)}var a=class extends Event{constructor(t,e){super(t,{bubbles:e?.bubbles,composed:e?.composed,cancelable:e?.cancelable}),this.data=e?.data,this.actions=e?.actions}};function T(o,...t){}var p={log:T};var f=class extends EventTarget{#s;#o={};#t;#i;#n;#e;#a;#p;#r;constructor(t){super(),p.log("Applet","Constructor called"),this.#s=t,this.#n=c.bind(this),this.#c();let e=s=>{s.source===this.#s&&"type"in s.data&&s.data.type==="appletconnect"&&(p.log("Applet","Recieved message",s.data),this.#c(),this.removeEventListener("message",e))};window.addEventListener("message",e)}#c(){this.#e&&this.#e.close();let t=new MessageChannel,e={type:"appletconnect"};p.log("Applet","Send message",e),this.#e=t.port1,this.#e.onmessage=this.#h.bind(this),this.#s.postMessage(e,"*",[t.port2]),this.#a=this.#e.postMessage.bind(this.#e)}#h(t){let e=t.data;switch(p.log("Applet","Recieved message",e),e.type){case"register":let s=e;this.#t=s.manifest;let n=new a("connect");this.#n(n),this.#o=s.actions,this.#d(s.actions),this.#i=s.data,this.#l(s.data);break;case"data":let i=e;this.#i=i.data,this.#l(i.data);break;case"resize":let r=e;this.#p=r.width,this.#r=r.height;let g=new a("resize");this.#n(g);break;case"actions":let m=e;this.#o=m.actions,this.#d(m.actions);break}}#l(t){let e=new a("data",{data:t});this.#n(e)}#d(t){let e=new a("actions",{actions:t});this.#n(e)}async sendAction(t,e){let s={id:crypto.randomUUID(),type:"action",actionId:t,arguments:e};return new Promise((n,i)=>{this.#a(s);let r=setTimeout(()=>{i(new h(`Applet action handler failed to complete before timeout (${1e4}ms)`))},1e4),g=m=>{let d=m.data;if(["actioncomplete","actionerror"].includes(d.type)&&"id"in d&&d.id===s.id)if(this.#e.removeEventListener("message",g),clearTimeout(r),d.type==="actionerror"){let b=d;i(new h(b.message))}else n()};this.#e.addEventListener("message",g)})}get data(){return this.#i}set data(t){this.#i=t;let e={type:"data",data:t};this.#a(e)}get window(){return this.#s}get manifest(){return this.#t}get actions(){return this.#o}get width(){return this.#p}get height(){return this.#r}};function M(o){return Object.keys(o).length===0}var E=class extends EventTarget{#s={};#o;#t;#i;#n;#e;#a;#p;constructor(t){super(),p.log("AppletScope","Constructor called"),this.#n=c.bind(this),t&&(this.#o=t);let e=n=>{if(n.source===window.parent&&n.data.type==="appletconnect"&&n.ports&&n.ports.length>0){p.log("AppletScope","Recieved message",n.data);let i=n.ports[0];this.#e=i.postMessage.bind(i),i.onmessage=this.#c.bind(this),this.removeEventListener("message",e),this.#r()}};window.addEventListener("message",e);let s={type:"appletconnect"};window.parent.postMessage(s,"*"),p.log("AppletScope","Send message",s)}async#r(){let t=this.manifest??await this.#g();this.#o=t||{},this.#t=this.#t||t?.actions||{};let e={type:"register",manifest:this.#o,actions:this.#t,data:this.#i};this.#e(e),p.log("AppletScope","Send message",e);let s=new a("connect");this.#n(s),this.#l()}#c(t){let e=t.data;switch(p.log("AppletScope","Recieved message",e),e.type){case"data":"data"in e&&(this.data=e.data);break;case"action":"type"in e&&e.type==="action"&&"id"in e&&typeof e.id=="string"&&"actionId"in e&&typeof e.actionId=="string"&&"arguments"in e&&this.#h(e);break}}async#h(t){let{actionId:e,arguments:s,id:n}=t;if(Object.keys(this.#s).includes(e))try{await this.#s[e](s);let i={type:"actioncomplete",id:n};this.#e(i)}catch(i){let r={type:"actionerror",id:n,message:i.message};this.#e(r),console.error(i)}}#l(){new ResizeObserver(e=>{for(let s of e)this.#d({width:s.contentRect.width,height:s.contentRect.height})}).observe(document.querySelector("html"))}#d({width:t,height:e}){this.#a=t,this.#p=e;let s={type:"resize",width:t,height:e};p.log("AppletScope","Send message",s),this.#e(s)}async#g(){let t=document.querySelector('link[rel="manifest"]');if(!t){console.warn("No manifest link found");return}try{let s=await(await fetch(t.href)).json();for(let n in s.actions){let i=s.actions[n];i.params_schema&&!M(this.#t.params_schema)&&(i.params_schema=void 0)}return s}catch{return}}setActionHandler(t,e){this.#s[t]=e}defineAction(t,e){let{handler:s,...n}=e;s&&(this.#s[t]=s),this.actions={...this.actions,[t]:n}}set actions(t){if(!t)return;this.#t=t;let e={type:"actions",actions:this.#t};p.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new a("actions",{actions:t});setTimeout(()=>this.#n(s),1)}get actions(){return this.#t}get manifest(){return this.#o}get actionHandlers(){return this.#s}set actionHandlers(t){this.#s=t}set data(t){this.#i=t;let e={type:"data",data:t};p.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new a("data",{data:t});setTimeout(()=>this.#n(s),1)}get data(){return this.#i}get width(){return this.#a}get height(){return this.#p}};var u=class{async connect(t){return new Promise((e,s)=>{let n=new f(t),i=setTimeout(()=>{s(new A(`Applet failed to connect before the timeout was reached (${1e4}ms)`))},1e4),r=()=>{e(n),n.removeEventListener("connect",r),clearTimeout(i)};n.addEventListener("connect",r)})}register(t){return new E(t)}};var l=class extends HTMLElement{#s;#o;#t;#i=c.bind(this);#n=document.createElement("iframe");static{this.observedAttributes=["src"]}connectedCallback(){this.#s=this.attachShadow({mode:"closed"}),this.#s.appendChild(this.#n);let t=document.createElement("style");t.textContent=this.styles,this.#s.appendChild(t),this.src=this.getAttribute("src")}get contentWindow(){return this.#n.contentWindow}set src(t){this.#o=t,this.#t=void 0,this.#e(t)}get src(){return this.#o||""}attributeChangedCallback(t,e,s){t==="src"&&(this.src=s)}async#e(t){this.#n.src=t;let e=this.#n.contentWindow;e&&(this.#t=await y.connect(e),this.#t.ondata=s=>{this.#i(s)},this.#t.onresize=s=>{this.#a({width:this.#t.width,height:this.#t.height})},this.#t.onactions=s=>{this.#i(s)},this.#i(new Event("load")),this.load&&typeof this.load=="function"&&this.onload(new Event("load")))}get applet(){return this.#t}set data(t){if(this.applet)this.applet.data=t;else{let e=()=>{this.applet&&(this.applet.data=t),this.removeEventListener("load",e)};this.addEventListener("load",e)}}#a(t){this.style.height=`${t.height}px`}get styles(){return`
2
2
  :host {
3
3
  background: white;
4
4
  display: flex;
@@ -11,4 +11,4 @@
11
11
  height: 100%;
12
12
  width: 100%;
13
13
  }
14
- `}};customElements.define("applet-frame",l);var y=new u;window.applets=y;window.AppletEvent=o;window.AppletFrameElement=l;})();
14
+ `}};customElements.define("applet-frame",l);var y=new u;window.applets=y;window.AppletEvent=a;window.AppletFrameElement=l;})();
package/package.json CHANGED
@@ -17,15 +17,18 @@
17
17
  "url": "git+https://github.com/unternet-co/web-applets.git"
18
18
  },
19
19
  "scripts": {
20
- "build": "npm run build:module && npm run build:shim",
20
+ "build": "cross-env NODE_ENV=production npm run build:module && npm run build:shim:prod",
21
+ "build:dev": "cross-env NODE_ENV=development npm run build:module && npm run build:shim:dev",
21
22
  "build:module": "tsc && cp ../README.md ./README.md",
22
- "build:shim": "node ./scripts/build.js",
23
+ "build:shim:prod": "cross-env NODE_ENV=production node ./scripts/build.js",
24
+ "build:shim:dev": "cross-env NODE_ENV=development node ./scripts/build.js",
23
25
  "prepublishOnly": "npm run build"
24
26
  },
25
27
  "devDependencies": {
26
- "@types/node": "^22.13.9",
28
+ "@types/node": "^22.13.10",
29
+ "cross-env": "^7.0.3",
27
30
  "esbuild": "^0.25.0",
28
31
  "typescript": "^5.6.2"
29
32
  },
30
- "version": "0.2.0"
33
+ "version": "0.2.1"
31
34
  }
package/dist/debug.js DELETED
@@ -1,6 +0,0 @@
1
- function log(location, ...messages) {
2
- console.log(`[${location}]`, ...messages);
3
- }
4
- export const debug = {
5
- log,
6
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes