openwebcontainer 0.0.0-stage → 0.1.0

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/dist/owc-sw.js ADDED
@@ -0,0 +1 @@
1
+ (()=>{var y=new URL(self.registration.scope).pathname.replace(/\/$/,""),H=new RegExp(`^${y}/__owc/vm/([a-z0-9]+)/(fs|builtin|missing)(/.*)?$`),P=new RegExp(`^${y}/__owc/p/([a-z0-9]+)-(\\d+)(/.*)?$`),x=new Map;function B(t,s,i){if(window.__owcPreview)return;window.__owcPreview=!0;let r=new Set(["localhost","127.0.0.1","0.0.0.0","[::1]",location.hostname]),c=i+"/__owc/p/"+t+"-"+s,a=window.WebSocket,l=n=>{let e=n.port?+n.port:n.protocol==="wss:"||n.protocol==="https:"?443:80,o=n.pathname+n.search;return n.host===location.host&&(e=s,o.startsWith(c)&&(o=o.slice(c.length)||"/")),{port:e,path:o}};class d extends EventTarget{constructor(e,o){super();let h=new URL(e,location.href);if(!r.has(h.hostname)||!navigator.serviceWorker||!navigator.serviceWorker.controller)return new a(e,o);this.url=h.href,this.readyState=0,this.protocol="",this.extensions="",this.bufferedAmount=0,this.binaryType="blob",this.onopen=this.onmessage=this.onclose=this.onerror=null;let{port:S,path:m}=l(h),w=new MessageChannel;this._p=w.port1,w.port1.onmessage=v=>this._on(v.data),navigator.serviceWorker.controller.postMessage({type:"owc-ws",cid:t,port:S,path:m,protocols:o==null?[]:[].concat(o),origin:"http://localhost:"+s},[w.port2])}_fire(e,o){let h=e==="message"?new MessageEvent("message",o):e==="close"?new CloseEvent("close",o):new Event(e);typeof this["on"+e]=="function"&&this["on"+e](h),this.dispatchEvent(h)}_on(e){if(e.t==="open")this.readyState=1,this.protocol=e.protocol||"",this._fire("open");else if(e.t==="message"){let o=e.data;typeof o!="string"&&this.binaryType==="blob"&&(o=new Blob([o])),this._fire("message",{data:o,origin:new URL(this.url).origin})}else if(e.t==="close"){let o=this.readyState;this.readyState=3,!e.wasClean&&o===0&&this._fire("error"),this._fire("close",{code:e.code,reason:e.reason||"",wasClean:!!e.wasClean})}}send(e){if(this.readyState===0)throw new DOMException("Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.","InvalidStateError");if(this.readyState===1){if(e instanceof Blob)return void e.arrayBuffer().then(o=>this._p.postMessage({t:"send",data:o},[o]));ArrayBuffer.isView(e)&&(e=e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)),this._p.postMessage({t:"send",data:e})}}close(e=1e3,o=""){this.readyState>=2||(this.readyState=2,this._p.postMessage({t:"close",code:e,reason:o}))}}for(let[n,e]of Object.entries({CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3}))d[n]=e,d.prototype[n]=e;window.WebSocket=d;let _=n=>{try{let e=new URL(n,location.href);if(e.host!==location.host&&r.has(e.hostname)&&/^https?:$/.test(e.protocol)){let{port:o,path:h}=l(e);return location.origin+i+"/__owc/p/"+t+"-"+o+h+e.hash}}catch{}return null},C=window.fetch;window.fetch=function(n,e){let o=typeof n=="string"||n instanceof URL?String(n):n.url,h=_(o);return h&&(n=typeof n=="string"||n instanceof URL?h:new Request(h,n)),C.call(this,n,e)};let R=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(n,e,...o){return R.call(this,n,_(e)||e,...o)}}var D=(t,s)=>"<script>("+B.toString()+")("+JSON.stringify(t)+","+Number(s)+","+JSON.stringify(y)+")<\/script>";self.addEventListener("install",()=>self.skipWaiting());self.addEventListener("activate",t=>t.waitUntil(self.clients.claim()));self.addEventListener("message",t=>{let s=t.data;if(s&&s.type==="owc-register"&&t.source)x.set(s.cid,t.source.id),t.ports[0]?.postMessage({ok:!0});else if(s&&s.type==="owc-ws"&&t.ports[0]){let i=t.ports[0];t.waitUntil(U(s.cid).then(r=>{r?r.postMessage(s,[i]):i.postMessage({t:"close",code:1006,reason:"container not running",wasClean:!1})}))}else s&&s.type==="owc-claim"&&t.waitUntil(self.clients.claim())});function A(t,s,i=[],r=0){return new Promise((c,a)=>{let l=new MessageChannel,d;l.port1.onmessage=_=>{d&&clearTimeout(d),c(_.data)},r&&(d=setTimeout(()=>a(new Error("timeout")),r)),t.postMessage(s,[l.port2,...i])})}async function U(t){let s=x.get(t);if(s){let a=await self.clients.get(s);if(a)return a;x.delete(t)}let i=await self.clients.matchAll({type:"window",includeUncontrolled:!0}),c=(await Promise.all(i.map(a=>A(a,{type:"owc-ping",cid:t},[],1500).then(l=>l&&l.ok?a:null,()=>null)))).find(Boolean);return c&&x.set(t,c.id),c||null}var M={"cross-origin-embedder-policy":"require-corp","cross-origin-resource-policy":"cross-origin","cross-origin-opener-policy":"same-origin"};async function I(t,s,i,r){let c=await U(t);if(!c)return new Response(`throw new Error('OpenWebContainer: container ${t} is not running (host page closed?)')`,{status:503,headers:{"content-type":"text/javascript",...M}});let a=await A(c,{type:"owc-module",cid:t,kind:s,rest:i||"",query:r.replace(/^\?/,"")});return new Response(a.body,{status:a.status,headers:{"content-type":a.contentType,"cache-control":"no-store",...M}})}var j="<script>(function poll(){setTimeout(function(){fetch(location.href,{method:'HEAD',cache:'no-store'}).then(function(r){if(r.status!==502)location.reload();else poll()},poll)},1000)})()<\/script>";function O(t,s,i,r=!1){let c=`<!doctype html><meta charset="utf-8"><title>${s}</title><style>body{font:14px system-ui,sans-serif;color:#ddd;background:#111;display:grid;place-items:center;height:100vh;margin:0}main{max-width:40em;padding:2em}h1{font-size:18px}code{color:#9cf}</style><main><h1>${s}</h1><p>${i}</p></main>${r?j:""}`;return new Response(c,{status:t,headers:{"content-type":"text/html; charset=utf-8",...M}})}var T=new Set(["connection","keep-alive","transfer-encoding","upgrade","proxy-connection","te","trailer"]);async function k(t,s,i,r){let c=await U(t);if(!c)return O(503,"Preview unavailable",`The page running container <code>${t}</code> is not open.`);let a=r.method,l=a==="GET"||a==="HEAD"?null:await r.arrayBuffer(),d=`${y}/__owc/p/${t}-${s}`,_=`http://localhost:${s}`,C=n=>{if(!n.startsWith(self.location.origin))return n;let e=n.slice(self.location.origin.length);return _+(e.startsWith(d)?e.slice(d.length)||"/":e||"/")},R=[];return r.headers.forEach((n,e)=>{T.has(e)||(e==="origin"?n=n===self.location.origin?_:n:e==="referer"&&(n=C(n)),R.push([e,n]))}),R.push(["host",`localhost:${s}`]),new Promise(n=>{let e=new MessageChannel,o,h=!1,S=null,m=null,w=new ReadableStream({start(v){o=v},cancel(){e.port1.postMessage({t:"abort"})}});e.port1.onmessage=v=>{let p=v.data;if(p.t==="head"){h=!0;let u=new Headers;for(let[f,g]of p.headers){let L=f.toLowerCase();if(!(T.has(L)||L==="set-cookie"||L.startsWith("cross-origin-"))){if(L==="location")try{let b=new URL(g,`http://localhost:${s}`);if(/^(localhost|127\.0\.0\.1|0\.0\.0\.0)$/.test(b.hostname)&&(!b.port||+b.port==+s||b.port==="")){u.append(f,d+b.pathname+b.search+b.hash);continue}if(g.startsWith("/")&&!g.startsWith("//")){u.append(f,d+g);continue}}catch{}try{u.append(f,g)}catch{}}}for(let[f,g]of Object.entries(M))u.set(f,g);let W=/text\/html/i.test(u.get("content-type")||""),$=a==="HEAD"||[101,204,205,304].includes(p.status),E=r.mode==="navigate"||r.destination==="document"||r.destination==="iframe";if(W&&!$&&E){S=D(t,s)+(p.inject||""),m=[],u.delete("content-length"),w.__headers=u,w.__status=p.status;return}n(new Response($?null:w,{status:p.status<200?200:p.status,headers:u}))}else if(p.t==="chunk")m?m.push(new Uint8Array(p.data)):o.enqueue(new Uint8Array(p.data));else if(p.t==="end"){if(!h){n(O(502,`Nothing is listening on port ${s}`,`Start a server in the container (e.g. <code>npm run dev</code>); this page reloads when it is up. <br><small>${p.error||""}</small>`,!0));return}if(m){let u=m.reduce((f,g)=>f+g.length,0),W=new Uint8Array(u),$=0;for(let f of m)W.set(f,$),$+=f.length;let E=new TextDecoder().decode(W);E=/<head[^>]*>/i.test(E)?E.replace(/<head[^>]*>/i,f=>f+S):S+E,n(new Response(E,{status:w.__status,headers:w.__headers}));return}p.error?o.error(new Error(p.error)):o.close()}},c.postMessage({type:"owc-http",cid:t,port:s,method:a,path:i,headers:R,body:l},[e.port2,...l?[l]:[]])})}function N(t){try{let s=P.exec(new URL(t).pathname);return s?{cid:s[1],port:s[2]}:null}catch{return null}}self.addEventListener("fetch",t=>{let s=t.request,i=new URL(s.url);if(i.origin!==self.location.origin)return;let r=H.exec(i.pathname);if(r){t.respondWith(I(r[1],r[2],r[3]||"",i.search));return}if(r=P.exec(i.pathname),r){t.respondWith(k(r[1],r[2],(r[3]||"/")+i.search,s));return}if(i.pathname.startsWith(`${y}/__owc/`))return;let c=N(s.referrer);if(c){let a=i.pathname.slice(y.length)||"/";s.mode==="navigate"?t.respondWith(Response.redirect(`${y}/__owc/p/${c.cid}-${c.port}${a}${i.search}`,302)):t.respondWith(k(c.cid,c.port,a+i.search,s));return}t.clientId&&t.respondWith((async()=>{let a=await self.clients.get(t.clientId),l=a&&N(a.url);return l?k(l.cid,l.port,(i.pathname.slice(y.length)||"/")+i.search,s):fetch(s)})())});})();
Binary file
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "openwebcontainer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Open-source in-browser Node.js runtime (WebContainer-compatible API), Rust + WebAssembly",
5
+ "keywords": [
6
+ "webcontainer",
7
+ "nodejs",
8
+ "browser",
9
+ "wasm",
10
+ "rust",
11
+ "sandbox",
12
+ "terminal",
13
+ "npm"
14
+ ],
15
+ "homepage": "https://github.com/jamiephan/openwebcontainer#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/jamiephan/openwebcontainer/issues"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/jamiephan/openwebcontainer.git",
22
+ "directory": "packages/openwebcontainer"
23
+ },
24
+ "license": "MIT",
25
+ "type": "module",
26
+ "main": "dist/openwebcontainer.js",
27
+ "module": "dist/openwebcontainer.js",
28
+ "types": "dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/openwebcontainer.js"
36
+ },
37
+ "./owc-sw.js": "./dist/owc-sw.js",
38
+ "./owc-runtime.js": "./dist/owc-runtime.js",
39
+ "./owc_wasm_bg.wasm": "./dist/owc_wasm_bg.wasm",
40
+ "./package.json": "./package.json"
41
+ },
42
+ "sideEffects": false,
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "provenance": true
46
+ },
47
+ "devDependencies": {
48
+ "acorn": "^8.19.0",
49
+ "buffer": "^6.0.3",
50
+ "events": "^3.3.0",
51
+ "readable-stream": "^4.7.0",
52
+ "string_decoder": "^1.3.0"
53
+ }
54
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.