@tbox.cn/app-sdk-server 0.23.0 → 0.24.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/platform/index.js +4 -4
- package/package.json +3 -3
package/dist/platform/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
var
|
|
1
|
+
var B="https://env-00jx4e7pq1un-static.normal.cloudstatic.cn/asset/codingbox/inspector.umd.js",R="/__vs_inspector__.js",b=`<script src="${R}" defer></script>`,y="",w=!1;async function j(e=3){for(let o=0;o<e;o++)try{let n=await fetch(B);if(!n.ok)throw new Error(`HTTP ${n.status}`);y=await n.text(),w=!0,console.log(`[inspector] runtime loaded (${y.length} bytes)`);return}catch(n){console.warn(`[inspector] fetch failed (attempt ${o+1}/${e}):`,n.message),o<e-1&&await new Promise(s=>setTimeout(s,1e3*(o+1)))}console.error("[inspector] runtime unavailable; inspector script will be skipped")}function D(e){if(e.includes(R))return e;let o=e.search(/<\/head\s*>/i);if(o!==-1)return e.slice(0,o)+b+e.slice(o);let n=e.search(/<body[\s>]/i);return n!==-1?e.slice(0,n)+b+e.slice(n):e}function A(){return w}function L(e){j(),e.get(R,(o,n)=>{if(!y){n.status(503).type("application/javascript").send("// inspector runtime not ready");return}n.type("application/javascript").set("Cache-Control","no-cache").send(y)}),e.use((o,n,s)=>{if(!w)return s();let i=[],f=n.write.bind(n),u=n.end.bind(n),m=!1,v=()=>{let a=String(n.getHeader("content-type")||""),p=String(n.getHeader("content-encoding")||"");return a.includes("text/html")&&!p};n.write=((a,...p)=>(!m&&!i.length&&(m=v()),m&&a?(i.push(Buffer.isBuffer(a)?a:Buffer.from(a)),!0):f(a,...p))),n.end=((a,...p)=>{if(!m&&!i.length&&(m=v()),m){a&&i.push(Buffer.isBuffer(a)?a:Buffer.from(a));try{let g=Buffer.concat(i).toString("utf8"),_=D(g);return n.setHeader("Content-Length",Buffer.byteLength(_)),u(_,...p)}catch(g){return console.warn("[inspector] inject failed, fallback to original:",g.message),u(Buffer.concat(i),...p)}}return u(a,...p)}),s()})}import{createHash as k,timingSafeEqual as W}from"crypto";import{readFile as H}from"fs/promises";import M from"express";import C from"path";var P="TBOX_DEV_TOKEN",h="tbox_dev_token",T="/__tbox_dev_hot_reload",E=/^[A-Za-z0-9_-]{20,128}$/,$={debug(){},info(){},warn(){}};function U(e){let o=e[P];return o===void 0||o===""?{mode:"classic"}:E.test(o)?{mode:"protected",token:o}:{mode:"protected",token:void 0}}function S(e,o){if(!e||!o||!E.test(e)||!E.test(o))return!1;let n=k("sha256").update(e).digest(),s=k("sha256").update(o).digest();return W(n,s)}function N(e){try{return new URL(e,"http://tbox.local").searchParams.get(h)??void 0}catch{return}}function V(e){if(e)for(let o of e.split(";")){let n=o.trim();if(n.startsWith(`${h}=`))return n.slice(h.length+1)}}function K(e,o){if(o===void 0)return{face:"static",credSource:"none",reason:"no-cred"};let n=N(e.url??"/"),s=V(e.headers.cookie),i="none";if(n!==void 0&&S(n,o)?i="query":s!==void 0&&S(s,o)?i="cookie":(n!==void 0||s!==void 0)&&(i="invalid"),i!=="query"&&i!=="cookie")return{face:"static",credSource:i,reason:"no-cred"};let f=e.headers["sec-fetch-site"],u=e.headers["sec-fetch-mode"];return u==="navigate"?{face:"dev",credSource:i,reason:"navigate"}:f==="same-origin"||f==="none"?{face:"dev",credSource:i,reason:"same-origin"}:f===void 0&&u===void 0?{face:"dev",credSource:i,reason:"no-sec-fetch-header"}:{face:"static",credSource:i,reason:"cross-site-subresource"}}function O(e,o){let n=/<head[^>]*>/i.exec(e);if(!n)return e;let s=n.index+n[0].length;return e.slice(0,s)+o+e.slice(s)}function G(e,o){return o.some(n=>e===n||e.startsWith(n.endsWith("/")?n:`${n}/`))}function J(e){return`<script>window.__TBOX_DEV_TOKEN__='${e}';(function(){var OW=window.WebSocket;function PW(url,protocols){if(typeof url==='string'&&url.indexOf('/__vite_hmr')!==-1){url+=(url.indexOf('?')===-1?'?':'&')+'${h}='+encodeURIComponent(window.__TBOX_DEV_TOKEN__);}return protocols===undefined?new OW(url):new OW(url,protocols);}PW.prototype=OW.prototype;Object.setPrototypeOf(PW,OW);window.WebSocket=PW;})();</script>`}var z=`<script>(function(){var es=new EventSource('${T}');var last=null;es.onmessage=function(e){try{var id=JSON.parse(e.data).buildId;if(last===null){last=id;return;}if(id!==last){location.reload();}last=id;}catch(err){}};})();</script>`;function X(e){let o=e.logger??$,n=Symbol("tboxDevFace"),s=e.tokenState.mode==="classic",i=e.tokenState.mode==="protected"?e.tokenState.token:void 0,f=0,u=new Set,m=e.sseKeepaliveMs??15e3,v=M.static(e.clientDist,{index:!1,fallthrough:!0}),a=new Set;o.info("dev_guard_mode",{mode:e.tokenState.mode,tokenShapeOk:e.tokenState.mode==="protected"&&i!==void 0,fsAllowNarrowed:e.fsAllowNarrowed===!0});function p(t){t.setHeader("Content-Type","text/event-stream; charset=utf-8"),t.setHeader("Cache-Control","no-store"),t.setHeader("X-Accel-Buffering","no"),t.flushHeaders(),t.write(`data: ${JSON.stringify({buildId:f})}
|
|
2
2
|
|
|
3
3
|
`),u.add(t);let r=setInterval(()=>{t.destroyed||t.write(`: keepalive
|
|
4
4
|
|
|
5
|
-
`)},m);t.on("close",()=>{clearInterval(r),u.delete(t),o.debug("dev_sse_close",{subscriberCount:u.size})}),o.debug("dev_sse_conn",{subscriberCount:u.size})}let g=(t,r,
|
|
5
|
+
`)},m);t.on("close",()=>{clearInterval(r),u.delete(t),o.debug("dev_sse_close",{subscriberCount:u.size})}),o.debug("dev_sse_conn",{subscriberCount:u.size})}let g=(t,r,l)=>{if(s){e.vite.middlewares(t,r,l);return}if(t.method==="GET"&&t.path===T){p(r);return}let d=K(t,i);t[n]=d.face,o.debug("dev_face_decision",{face:d.face,credSource:d.credSource,reason:d.reason,method:t.method,pathname:t.path,secFetchSite:t.headers["sec-fetch-site"]??null,secFetchMode:t.headers["sec-fetch-mode"]??null,requestId:t.headers["x-request-id"]??null,reqIdSource:"header"}),d.face==="dev"?e.vite.middlewares(t,r,l):l()},_=(t,r,l)=>{I(t,r,l)};async function I(t,r,l){if(G(t.path??"/",e.spaExcludePrefixes)){l();return}let d=s?"classic":t[n]??"static";if(d!=="static"){try{let c=await H(C.join(e.clientRoot,"index.html"),"utf-8");c=await e.vite.transformIndexHtml(t.originalUrl??"/",c),d==="dev"&&(c=O(c,J(i))),c=e.decorateHtml?e.decorateHtml(c,"dev"):c,r.statusCode=200,r.setHeader("Content-Type","text/html; charset=utf-8"),d==="dev"&&r.setHeader("Cache-Control","no-store"),r.end(c),a.has("dev")||(a.add("dev"),o.info("dev_html_face",{face:"dev",pathname:t.path,source:"clientRoot"}))}catch(c){l(c)}return}v(t,r,()=>{if(t.method!=="GET"&&t.method!=="HEAD"){r.statusCode=404,r.setHeader("Cache-Control","no-store"),r.end("Not Found");return}H(C.join(e.clientDist,"index.html"),"utf-8").then(c=>{let x=O(c,z),F=e.decorateHtml?e.decorateHtml(x,"static"):x;r.statusCode=200,r.setHeader("Content-Type","text/html; charset=utf-8"),r.setHeader("Cache-Control","no-store"),r.end(F),o.debug("dev_spa_fallback_hit",{face:"static",pathname:t.path,method:t.method,distReady:!0})}).catch(()=>{r.statusCode=404,r.setHeader("Cache-Control","no-store"),r.end("Not Found"),o.debug("dev_spa_fallback_hit",{face:"static",pathname:t.path,method:t.method,distReady:!1})})})}return{gate:g,final:_,handleUpgrade:(t,r,l)=>{let d;try{d=new URL(t.url||"/","http://localhost").pathname}catch{return!1}if(d!==e.hmrPath)return!1;let c=t.headers["sec-websocket-protocol"];if(!c)t.headers["sec-websocket-protocol"]="vite-hmr";else if(c!=="vite-hmr"&&c!=="vite-ping")return!1;if(!s){let x=N(t.url??"/");if(i===void 0||!S(x,i))return r.write(`HTTP/1.1 401 Unauthorized\r
|
|
6
6
|
Connection: close\r
|
|
7
7
|
Content-Length: 0\r
|
|
8
8
|
\r
|
|
9
|
-
`),r.end(()=>{r.destroyed||r.destroy()}),o.info("dev_hmr_upgrade",{outcome:"rejected-401",pathname:d,protocol:t.headers["sec-websocket-protocol"]}),!0}return e.hmrHub.emit("upgrade",t,r,
|
|
9
|
+
`),r.end(()=>{r.destroyed||r.destroy()}),o.info("dev_hmr_upgrade",{outcome:"rejected-401",pathname:d,protocol:t.headers["sec-websocket-protocol"]}),!0}return e.hmrHub.emit("upgrade",t,r,l),o.info("dev_hmr_upgrade",{outcome:"claimed",pathname:d,protocol:t.headers["sec-websocket-protocol"]}),!0},notifyBuildEnd:()=>{f+=1;for(let t of u)t.destroyed||t.write(`data: ${JSON.stringify({buildId:f})}
|
|
10
10
|
|
|
11
|
-
`);o.info("dev_build_end",{buildId:
|
|
11
|
+
`);o.info("dev_build_end",{buildId:f})}}}export{T as DEV_SSE_PATH,P as DEV_TOKEN_ENV,h as DEV_TOKEN_NAME,R as RUNTIME_PATH,b as SCRIPT_TAG,X as createTboxDevRuntime,D as injectScript,L as installInspector,A as isReady,U as resolveDevTokenFromEnv};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-sdk-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"tbox-inspector-check": "bin/check-inspector.mjs"
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
"tsup": "8.5.1",
|
|
52
52
|
"typescript": "5.9.3",
|
|
53
53
|
"vitest": "4.1.10",
|
|
54
|
-
"@tbox.cn/app-
|
|
55
|
-
"@tbox.cn/app-sdk-
|
|
54
|
+
"@tbox.cn/app-sdk-client": "0.21.1",
|
|
55
|
+
"@tbox.cn/app-agent-sdk-server": "0.2.1"
|
|
56
56
|
},
|
|
57
57
|
"license": "MIT",
|
|
58
58
|
"scripts": {
|