@chatpanel/gateway 0.88.0 → 0.97.2

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/src/ort-native.js CHANGED
@@ -1 +1 @@
1
- import{mirrorUrl as E}from"@chatpanel/events/registries.js";import{createRequire as R}from"node:module";import{gunzipSync as k}from"node:zlib";import{mkdirSync as x,writeFileSync as I,existsSync as d,renameSync as A,rmSync as $,chmodSync as O}from"node:fs";import{join as i,dirname as w}from"node:path";import{homedir as _}from"node:os";const b=globalThis.__CHATPANEL_ORT_VERSION__||"1.30.0",N="napi-v6";function g(e=process.platform,t=process.arch){return({darwin:["arm64","x64"],linux:["arm64","x64"],win32:["x64","arm64"]}[e]||[]).includes(t)?{platform:e,arch:t}:null}function T(e=i(_(),".chatpanel"),t=b,r=g()){return r?i(e,"runtime","ort",t,`${r.platform}-${r.arch}`):null}const u="onnxruntime_binding.node";function q({env:e=process.env,stateDir:t=i(_(),".chatpanel"),require:r=null}={}){const c=e.CHATPANEL_ORT_DIR?i(e.CHATPANEL_ORT_DIR,u):null;if(c&&d(c))return c;const n=T(t);if(n&&d(i(n,u)))return i(n,u);try{const o=(r||R(import.meta.url)).resolve("onnxruntime-node/package.json"),a=g(),s=a?i(w(o),"bin",N,a.platform,a.arch,u):null;if(s&&d(s))return s}catch{}return null}function*B(e,t=()=>!0){let r=0;for(;r+512<=e.length;){const c=e.subarray(r,r+512);if(c[0]===0)break;const n=(h,y)=>c.subarray(h,h+y).toString("utf8").replace(/\0.*$/,"").trim(),m=n(0,100),o=n(345,155),a=parseInt(n(124,12)||"0",8)||0,s=String.fromCharCode(c[156]||48),p=o?`${o}/${m}`:m;r+=512,(s==="0"||s==="\0"||s==="48")&&t(p)&&(yield{name:p,data:e.subarray(r,r+a)}),r+=Math.ceil(a/512)*512}}async function P({stateDir:e=i(_(),".chatpanel"),version:t=b,fetchImpl:r=fetch,onProgress:c=()=>{},target:n=g(),registries:m={}}={}){if(!n)throw new Error(`no native onnxruntime build for ${process.platform}/${process.arch}`);const o=T(e,t,n);if(d(i(o,u)))return i(o,u);const a=E("npm",`https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-${t}.tgz`,m);c({phase:"download",url:a});const s=await r(a);if(!s.ok)throw new Error(`onnxruntime-node ${t}: registry answered ${s.status}`);const p=Buffer.from(await s.arrayBuffer());c({phase:"extract",bytes:p.length});const h=Buffer.from(k(p)),y=`package/bin/${N}/${n.platform}/${n.arch}/`,f=`${o}.incoming-${process.pid}`;$(f,{recursive:!0,force:!0}),x(f,{recursive:!0});let v=0;for(const S of B(h,l=>l.startsWith(y))){const l=i(f,S.name.slice(y.length));if(x(w(l),{recursive:!0}),I(l,S.data),/\.(node|dylib|so|so\.\d+|dll)$/.test(l))try{O(l,493)}catch{}v+=1}if(!d(i(f,u)))throw $(f,{recursive:!0,force:!0}),new Error(`onnxruntime-node ${t} carries no ${n.platform}/${n.arch} binding`);return x(w(o),{recursive:!0}),$(o,{recursive:!0,force:!0}),A(f,o),c({phase:"ready",dir:o,files:v}),i(o,u)}export{b as ORT_VERSION,P as ensureNativeOrt,q as findNativeBinding,T as ortDir,g as ortTarget,B as readTar};
1
+ import{createHash as S}from"node:crypto";import{mirrorUrl as v}from"@chatpanel/events/registries.js";import{createRequire as A}from"node:module";import{gunzipSync as O}from"node:zlib";import{mkdirSync as y,writeFileSync as k,existsSync as w,renameSync as C,rmSync as $,chmodSync as B}from"node:fs";import{join as i,dirname as _}from"node:path";import{homedir as g}from"node:os";const E=globalThis.__CHATPANEL_ORT_VERSION__||"1.30.0",H=globalThis.__CHATPANEL_ORT_INTEGRITY__||"",N="napi-v6";function T(o=process.platform,n=process.arch){return({darwin:["arm64","x64"],linux:["arm64","x64"],win32:["x64","arm64"]}[o]||[]).includes(n)?{platform:o,arch:n}:null}function I(o=i(g(),".chatpanel"),n=E,r=T()){return r?i(o,"runtime","ort",n,`${r.platform}-${r.arch}`):null}const u="onnxruntime_binding.node";function Y({env:o=process.env,stateDir:n=i(g(),".chatpanel"),require:r=null}={}){const s=o.CHATPANEL_ORT_DIR?i(o.CHATPANEL_ORT_DIR,u):null;if(s&&w(s))return s;const e=I(n);if(e&&w(i(e,u)))return i(e,u);try{const l=(r||A(import.meta.url)).resolve("onnxruntime-node/package.json"),t=T(),c=t?i(_(l),"bin",N,t.platform,t.arch,u):null;if(c&&w(c))return c}catch{}return null}function*L(o,n=()=>!0){let r=0;for(;r+512<=o.length;){const s=o.subarray(r,r+512);if(s[0]===0)break;const e=(f,x)=>s.subarray(f,f+x).toString("utf8").replace(/\0.*$/,"").trim(),d=e(0,100),l=e(345,155),t=parseInt(e(124,12)||"0",8)||0,c=String.fromCharCode(s[156]||48),m=l?`${l}/${d}`:d;r+=512,(c==="0"||c==="\0"||c==="48")&&n(m)&&(yield{name:m,data:o.subarray(r,r+t)}),r+=Math.ceil(t/512)*512}}async function F({stateDir:o=i(g(),".chatpanel"),version:n=E,fetchImpl:r=fetch,onProgress:s=()=>{},target:e=T(),registries:d={},integrity:l=H}={}){if(!e)throw new Error(`no native onnxruntime build for ${process.platform}/${process.arch}`);const t=I(o,n,e);if(w(i(t,u)))return i(t,u);const c=v("npm",`https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-${n}.tgz`,d);s({phase:"download",url:c});const m=await r(c);if(!m.ok)throw new Error(`onnxruntime-node ${n}: registry answered ${m.status}`);const f=Buffer.from(await m.arrayBuffer());if(l){const[h,a]=String(l).split("-",2);if(S(h).update(f).digest("base64")!==a)throw new Error(`onnxruntime-node ${n}: the download from ${new URL(c).host} is not the one this gateway was built against (${h} mismatch) — not used`)}s({phase:"extract",bytes:f.length});const x=Buffer.from(O(f)),R=`package/bin/${N}/${e.platform}/${e.arch}/`,p=`${t}.incoming-${process.pid}`;$(p,{recursive:!0,force:!0}),y(p,{recursive:!0});let b=0;for(const h of L(x,a=>a.startsWith(R))){const a=i(p,h.name.slice(R.length));if(y(_(a),{recursive:!0}),k(a,h.data),/\.(node|dylib|so|so\.\d+|dll)$/.test(a))try{B(a,493)}catch{}b+=1}if(!w(i(p,u)))throw $(p,{recursive:!0,force:!0}),new Error(`onnxruntime-node ${n} carries no ${e.platform}/${e.arch} binding`);return y(_(t),{recursive:!0}),$(t,{recursive:!0,force:!0}),C(p,t),s({phase:"ready",dir:t,files:b}),i(t,u)}export{H as ORT_INTEGRITY,E as ORT_VERSION,F as ensureNativeOrt,Y as findNativeBinding,I as ortDir,T as ortTarget,L as readTar};
@@ -0,0 +1,5 @@
1
+ import{createHash as f,createPublicKey as c,verify as u}from"node:crypto";const l=Object.freeze([Object.freeze({id:"b865893fe0ec2f02",pem:`-----BEGIN PUBLIC KEY-----
2
+ MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWM0DU/Fv3SwoKAMqA5ctSRG/i1w8
3
+ vlHgHM2M6DyAkybJ2/nOaFFcMp02buiq9g6GpX1txMx0DFXw3J5Rks0nkA==
4
+ -----END PUBLIC KEY-----
5
+ `})]);function k(t){return f("sha256").update(c(t).export({type:"spki",format:"der"})).digest("hex").slice(0,16)}function d(t,r,n=l){const e=Buffer.from(t);for(const s of n)try{if(u("sha256",e,c(s.pem),Buffer.from(r)))return s.id}catch{}return null}function h(t,r){for(const n of String(t).split(/\r?\n/)){const e=/^([0-9a-f]{64}) [ *](.+)$/.exec(n.trim());if(e&&e[2]===r)return e[1]}return null}function g({bytes:t,name:r,sums:n,signature:e,keys:s=l}){if(!s.length)return{ok:!1,reason:"this build trusts no release key"};if(!n||!e)return{ok:!1,reason:"the release carries no signed manifest (SHA256SUMS and its .sig)"};const i=d(n,e,s);if(!i)return{ok:!1,reason:"the release manifest is not signed by a ChatPanel release key"};const o=h(n,r);if(!o)return{ok:!1,reason:`the signed manifest does not list ${r}`};const a=f("sha256").update(t).digest("hex");return a!==o?{ok:!1,reason:`${r} does not match the signed manifest (sha256 ${a.slice(0,12)}… where ${o.slice(0,12)}… was signed)`}:{ok:!0,keyId:i}}export{l as RELEASE_KEYS,g as checkReleaseFile,h as manifestHash,k as releaseKeyId,d as verifyReleaseManifest};
@@ -0,0 +1 @@
1
+ import{existsSync as p,mkdirSync as f,readFileSync as y,renameSync as w,rmSync as U,writeFileSync as l,chmodSync as j}from"node:fs";import{createHash as F,randomUUID as g}from"node:crypto";import{join as t,dirname as R,sep as W,resolve as S}from"node:path";import{pathToFileURL as b}from"node:url";import A from"node:os";import{unzipSync as D}from"fflate";function q({zipPath:u,meta:e=null,home:v=A.homedir(),log:c=()=>{}}){try{if(!u||!e?.version||!e?.sha256)return"";const r=t(v,".chatpanel","runtime","sandbox-runtime"),n=t(r,`${e.version}-${e.sha256.slice(0,12)}`),a=t(n,"sandbox-runtime","srt.mjs"),d=t(n,".unpacked");if(p(d)&&y(d,"utf8").trim()===e.sha256&&p(a))return b(a).href;const m=new Uint8Array(y(u)),h=F("sha256").update(m).digest("hex");if(h!==e.sha256)return c(`sandbox runtime: the carried copy does not match its hash (${h.slice(0,12)}…) — not unpacked`),"";const x=D(m);f(r,{recursive:!0,mode:448});const s=t(r,`.unpack-${g().slice(0,8)}`),k=S(s)+W;for(const[o,$]of Object.entries(x)){if(o.endsWith("/"))continue;const i=S(s,...o.split("/"));if(!i.startsWith(k))throw new Error(`a path outside the runtime: ${o}`);if(f(R(i),{recursive:!0,mode:448}),l(i,$,{mode:/\/vendor\//.test(o)?493:420}),/\/vendor\//.test(o))try{j(i,493)}catch{}}return l(t(s,".unpacked"),e.sha256,{mode:384}),U(n,{recursive:!0,force:!0}),w(s,n),c(`sandbox runtime ${e.version} unpacked — ${n}`),b(a).href}catch(r){return c(`sandbox runtime: could not unpack the carried copy (${String(r?.message||r).slice(0,160)})`),""}}export{q as unpackSandboxRuntime};