From c713aeefed969cba4d303d30a8a8f3bc463a97d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Franky?= Date: Sat, 30 Nov 2024 01:55:51 +0100 Subject: [PATCH] 43 allow to run rimless in nodejs (#44) * fix docs * replace integrations with storybook * update helpers * add nodejs checks * fix various issues --- declarations.d.ts | 23 +++++ docs/examples/iframe.html | 5 +- docs/examples/worker.ts | 2 +- docs/typings.d.ts | 11 --- src/guest.ts | 15 +-- src/helpers.ts | 94 ++++++++++++++----- src/host.ts | 64 ++++++++----- src/rpc.ts | 14 ++- tests/helpers.test.ts | 137 +++++++++++++++++++++++----- tests/integrations/iframe-host.html | 109 ---------------------- tests/integrations/iframe.html | 91 ------------------ tests/integrations/rimless.min.js | 2 - tests/integrations/worker-host.html | 69 -------------- vite.config.ts | 3 + 14 files changed, 272 insertions(+), 367 deletions(-) create mode 100644 declarations.d.ts delete mode 100644 docs/typings.d.ts delete mode 100644 tests/integrations/iframe-host.html delete mode 100644 tests/integrations/iframe.html delete mode 100644 tests/integrations/rimless.min.js delete mode 100644 tests/integrations/worker-host.html diff --git a/declarations.d.ts b/declarations.d.ts new file mode 100644 index 0000000..7b15b84 --- /dev/null +++ b/declarations.d.ts @@ -0,0 +1,23 @@ +declare module "*?worker&inline" { + const WorkerFactory: { + new (): Worker; + }; + export default WorkerFactory; +} + +declare module "*?worker" { + const WorkerFactory: { + new (): Worker; + }; + export default WorkerFactory; +} + +declare module "*.html?raw" { + const content: string; + export default content; +} + +declare module "*.html" { + const content: string; + export default content; +} diff --git a/docs/examples/iframe.html b/docs/examples/iframe.html index 09d0787..4c583cb 100644 --- a/docs/examples/iframe.html +++ b/docs/examples/iframe.html @@ -4,7 +4,6 @@ Rimless Guest - - - - - - -

host

-
- - - - - - - - - \ No newline at end of file diff --git a/tests/integrations/iframe.html b/tests/integrations/iframe.html deleted file mode 100644 index 81bd999..0000000 --- a/tests/integrations/iframe.html +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - guest - - - - - - -

iframe

- - - - - - - \ No newline at end of file diff --git a/tests/integrations/rimless.min.js b/tests/integrations/rimless.min.js deleted file mode 100644 index 1a8ce39..0000000 --- a/tests/integrations/rimless.min.js +++ /dev/null @@ -1,2 +0,0 @@ -var rimless=function(t){"use strict";function e(){return"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope}function n(t){const e=[];return function t(n,r=""){Object.keys(n).forEach(o=>{const i=r?`${r}.${o}`:o;n[o]===Object(n[o])&&t(n[o],i),"function"==typeof n[o]&&e.push(i)})}(t),e}const r=/^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/,o={"http:":"80","https:":"443"};var i="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},a=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,c=/^\w*$/,u=/^\./,s=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,f=/\\(\\)?/g,l=/^\[object .+?Constructor\]$/,p="object"==typeof i&&i&&i.Object===Object&&i,_="object"==typeof self&&self&&self.Object===Object&&self,h=p||_||Function("return this")();var d,y=Array.prototype,v=Function.prototype,g=Object.prototype,E=h["__core-js_shared__"],S=(d=/[^.]+$/.exec(E&&E.keys&&E.keys.IE_PROTO||""))?"Symbol(src)_1."+d:"",m=v.toString,b=g.hasOwnProperty,R=g.toString,O=RegExp("^"+m.call(b).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),w=h.Symbol,j=y.splice,A=G(h,"Map"),M=G(Object,"create"),P=w?w.prototype:void 0,C=P?P.toString:void 0;function $(t){var e=-1,n=t?t.length:0;for(this.clear();++e-1},L.prototype.set=function(t,e){var n=this.__data__,r=I(n,t);return r<0?n.push([t,e]):n[r][1]=e,this},N.prototype.clear=function(){this.__data__={hash:new $,map:new(A||L),string:new $}},N.prototype.delete=function(t){return H(this,t).delete(t)},N.prototype.get=function(t){return H(this,t).get(t)},N.prototype.has=function(t){return H(this,t).has(t)},N.prototype.set=function(t,e){return H(this,t).set(t,e),this};var J=V((function(t){var e;t=null==(e=t)?"":function(t){if("string"==typeof t)return t;if(Q(t))return C?C.call(t):"";var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(e);var n=[];return u.test(t)&&n.push(""),t.replace(s,(function(t,e,r,o){n.push(r?o.replace(f,"$1"):e||t)})),n}));function U(t){if("string"==typeof t||Q(t))return t;var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}function V(t,e){if("function"!=typeof t||e&&"function"!=typeof e)throw new TypeError("Expected a function");var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],i=n.cache;if(i.has(o))return i.get(o);var a=t.apply(this,r);return n.cache=i.set(o,a),a};return n.cache=new(V.Cache||N),n}V.Cache=N;var F=Array.isArray;function K(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function Q(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==R.call(t)}var k=function(t,e,n){var r=null==t?void 0:D(t,e);return void 0===r?n:r},x=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,Y=/^\w*$/,W=/^\./,q=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,z=/\\(\\)?/g,B=/^\[object .+?Constructor\]$/,X=/^(?:0|[1-9]\d*)$/,Z="object"==typeof i&&i&&i.Object===Object&&i,tt="object"==typeof self&&self&&self.Object===Object&&self,et=Z||tt||Function("return this")();var nt=Array.prototype,rt=Function.prototype,ot=Object.prototype,it=et["__core-js_shared__"],at=function(){var t=/[^.]+$/.exec(it&&it.keys&&it.keys.IE_PROTO||"");return t?"Symbol(src)_1."+t:""}(),ct=rt.toString,ut=ot.hasOwnProperty,st=ot.toString,ft=RegExp("^"+ct.call(ut).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),lt=et.Symbol,pt=nt.splice,_t=wt(et,"Map"),ht=wt(Object,"create"),dt=lt?lt.prototype:void 0,yt=dt?dt.toString:void 0;function vt(t){var e=-1,n=t?t.length:0;for(this.clear();++e-1&&t%1==0&&t-1},gt.prototype.set=function(t,e){var n=this.__data__,r=mt(n,t);return r<0?n.push([t,e]):n[r][1]=e,this},Et.prototype.clear=function(){this.__data__={hash:new vt,map:new(_t||gt),string:new vt}},Et.prototype.delete=function(t){return Ot(this,t).delete(t)},Et.prototype.get=function(t){return Ot(this,t).get(t)},Et.prototype.has=function(t){return Ot(this,t).has(t)},Et.prototype.set=function(t,e){return Ot(this,t).set(t,e),this};var At=Pt((function(t){var e;t=null==(e=t)?"":function(t){if("string"==typeof t)return t;if(Nt(t))return yt?yt.call(t):"";var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(e);var n=[];return W.test(t)&&n.push(""),t.replace(q,(function(t,e,r,o){n.push(r?o.replace(z,"$1"):e||t)})),n}));function Mt(t){if("string"==typeof t||Nt(t))return t;var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}function Pt(t,e){if("function"!=typeof t||e&&"function"!=typeof e)throw new TypeError("Expected a function");var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],i=n.cache;if(i.has(o))return i.get(o);var a=t.apply(this,r);return n.cache=i.set(o,a),a};return n.cache=new(Pt.Cache||Et),n}function Ct(t,e){return t===e||t!=t&&e!=e}Pt.Cache=Et;var $t=Array.isArray;function Lt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function Nt(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==st.call(t)}var It=function(t,e,n){return null==t?t:Rt(t,e,n)},Dt="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto),Tt=new Uint8Array(16);function Ht(){if(!Dt)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return Dt(Tt)}for(var Gt,Jt,Ut=[],Vt=0;Vt<256;++Vt)Ut[Vt]=(Vt+256).toString(16).substr(1);function Ft(t,e,n){var r=e&&n||0;"string"==typeof t&&(e="binary"===t?new Array(16):null,t=null);var o=(t=t||{}).random||(t.rng||Ht)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,e)for(var i=0;i<16;++i)e[r+i]=o[i];return e||function(t,e){var n=e||0,r=Ut;return[r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]]].join("")}(o)}function Kt(t={},n=[],r,o){const i=[];return n.forEach(n=>{async function a(i){const{action:a,callID:c,connectionID:u,callName:s,args:f=[]}=i.data;if(a!==Jt.RPC_REQUEST)return;if(!c||!s)return;if(s!==n)return;if(u!==r)return;const l={action:Jt.RPC_RESOLVE,callID:c,callName:s,connectionID:u,error:null,result:null};try{const e=await k(t,n)(...f);l.result=JSON.parse(JSON.stringify(e))}catch(t){l.error=JSON.parse(JSON.stringify(t,Object.getOwnPropertyNames(t)))}o?o.postMessage(l):e()?self.postMessage(l):i.source.postMessage(l,i.origin)}o?o.addEventListener(Gt.MESSAGE,a):self.addEventListener(Gt.MESSAGE,a),i.push(()=>self.removeEventListener(Gt.MESSAGE,a))}),()=>i.forEach(t=>t())}function Qt(t={},n=[],r,o,i){const a={...t},c=[];return n.forEach(t=>{const n=function(t,n,r,o=[],i){return(...a)=>new Promise((c,u)=>{const s=Ft();function f(e){const{callID:r,connectionID:o,callName:i,result:a,error:s,action:f}=e.data;if(r&&i&&i===t&&o===n)return f===Jt.RPC_RESOLVE?c(a):f===Jt.RPC_REJECT?u(s):void 0}const l={action:Jt.RPC_REQUEST,args:JSON.parse(JSON.stringify(a)),callID:s,callName:t,connectionID:n};i?i.addEventListener(Gt.MESSAGE,f):self.addEventListener(Gt.MESSAGE,f),o.push(()=>self.removeEventListener(Gt.MESSAGE,f)),i?i.postMessage(l):e()?self.postMessage(l):(r.source||r.target).postMessage(l,r.origin)})}(t,r,o,c,i);It(a,t,n)}),{remote:a,unregisterRemote:()=>c.forEach(t=>t())}}!function(t){t.MESSAGE="message"}(Gt||(Gt={})),function(t){t.HANDSHAKE_REQUEST="RIMLESS/HANDSHAKE_REQUEST",t.HANDSHAKE_REPLY="RIMLESS/HANDSHAKE_REPLY",t.RPC_REQUEST="RIMLESS/RPC_REQUEST",t.RPC_RESOLVE="RIMLESS/RPC_RESOLVE",t.RPC_REJECT="RIMLESS/RPC_REJECT"}(Jt||(Jt={}));let kt=null,xt=!1;var Yt={connect:function(t={},r={}){return new Promise((r,o)=>{const i=n(t);self.addEventListener(Gt.MESSAGE,(function e(n){if(n.data.action!==Jt.HANDSHAKE_REPLY)return;const o=Kt(t,i,n.data.connectionID),{remote:a,unregisterRemote:c}=Qt(n.data.schema,n.data.methods,n.data.connectionID,n);return xt=!0,r({remote:a,close:()=>{self.removeEventListener(Gt.MESSAGE,e),c(),o()}})}));const a={action:Jt.HANDSHAKE_REQUEST,methods:i,schema:JSON.parse(JSON.stringify(t))};kt=setInterval(()=>{if(xt)return clearInterval(kt);e()?self.postMessage(a):window.parent.postMessage(a,"*")},600),setTimeout(()=>{xt||o("connection timeout")},3e3)})}};function Wt(t,e){const n=function(t){const{location:e}=document,n=r.exec(t||"");let i,a,c;return n?[,i=e.protocol,a,,c]=n:(i=e.protocol,a=e.hostname,c=e.port),"file:"===i?"null":`${i}//${a}${c&&c!==o[i]?":"+c:""}`}(t.getAttribute("src")),i=e.origin===n,a=e.source===t.contentWindow;return i&&a}var qt={connect:function(t,e={},r){if(!t)throw new Error("a target is required");const o=void 0!==t.onerror&&void 0!==t.onmessage,i=o?t:window;return new Promise((r,a)=>{const c=Ft();i.addEventListener(Gt.MESSAGE,(function a(u){if(!o&&!Wt(t,u))return;if(u.data.action!==Jt.HANDSHAKE_REQUEST)return;const s=n(e),f=Kt(e,s,c,o?t:void 0),{remote:l,unregisterRemote:p}=Qt(u.data.schema,u.data.methods,c,u,o?t:void 0),_={action:Jt.HANDSHAKE_REPLY,connectionID:c,methods:s,schema:JSON.parse(JSON.stringify(e))};return o?t.postMessage(_):u.source.postMessage(_,u.origin),r({remote:l,close:()=>{i.removeEventListener(Gt.MESSAGE,a),p(),f()}})}))})}};return t.guest=Yt,t.host=qt,t}({}); -//# sourceMappingURL=rimless.min.js.map diff --git a/tests/integrations/worker-host.html b/tests/integrations/worker-host.html deleted file mode 100644 index 0c8cfb2..0000000 --- a/tests/integrations/worker-host.html +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - Host - - - - - - -

host

- - - - - - - \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index 95f2583..e1531d9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -21,4 +21,7 @@ export default defineConfig({ sourcemap: true, emptyOutDir: true, }, + optimizeDeps: { + exclude: ['./docs/examples/iframe.html'] + } });