@yuandc/aica 0.1.73 → 0.1.75
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isValidPpmHeader as
|
|
1
|
+
import{isValidPpmHeader as E}from"./ppm-header.js";import n from"node:fs";import w from"node:os";import f from"node:path";import{fileURLToPath as I}from"node:url";function B(i,o,e){let t=o.trim();if(/^file:/i.test(t)){const r=new URL(t);if(r.hostname&&r.hostname!=="localhost")throw new Error("image URL must reference a local file");t=I(r)}if(!t||t.includes("\0"))throw new Error("image path is invalid");if(!f.isAbsolute(t)&&!i)throw new Error("relative image path requires a project root");const g=f.resolve(i,t),s=n.lstatSync(g);if(s.isSymbolicLink())throw new Error("image cannot be a symbolic link");if(!s.isFile())throw new Error("image target is not a regular file");const l=n.realpathSync(g),c=n.openSync(l,n.constants.O_RDONLY|(n.constants.O_NOFOLLOW||0)|(n.constants.O_NONBLOCK||0));try{const r=n.fstatSync(c);if(!r.isFile()||s.dev!==r.dev||s.ino!==r.ino)throw new Error("image file changed while opening");if(!Number.isFinite(e)||e<=0||r.size>e)throw new Error("image file is too large");let a=Buffer.alloc(512),m=n.readSync(c,a,0,a.length,0);a[0]===80&&[51,54].includes(a[1])&&r.size>a.length&&(a=Buffer.alloc(64*1024),m=n.readSync(c,a,0,a.length,0));const u=L(a.subarray(0,m),r.size);if(!u&&!d(i,l)&&!d(w.tmpdir(),l))throw new Error("file is not a supported raster image");const h=u||"application/octet-stream",p=n.createReadStream(l,{fd:c,autoClose:!0,start:0,end:Math.max(0,r.size-1)});return{path:l,sizeBytes:r.size,mimeType:h,stream:p}}catch(r){throw n.closeSync(c),r}}function d(i,o){if(!i)return!1;try{const e=f.relative(n.realpathSync(i),o);return e!==""&&e!==".."&&!e.startsWith(`..${f.sep}`)&&!f.isAbsolute(e)}catch{return!1}}function L(i,o){if(E(i,o))return"image/x-portable-pixmap";if(i.length>=24&&i.subarray(0,8).equals(Buffer.from([137,80,78,71,13,10,26,10]))&&i.toString("ascii",12,16)==="IHDR"&&i.readUInt32BE(16)>0&&i.readUInt32BE(20)>0)return"image/png";if(i.length>=4&&i[0]===255&&i[1]===216&&i[2]===255)return"image/jpeg";if(i.length>=10&&["GIF87a","GIF89a"].includes(i.toString("ascii",0,6))&&i.readUInt16LE(6)>0&&i.readUInt16LE(8)>0)return"image/gif";if(i.length>=16&&i.toString("ascii",0,4)==="RIFF"&&i.toString("ascii",8,12)==="WEBP"&&["VP8 ","VP8L","VP8X"].includes(i.toString("ascii",12,16)))return"image/webp";if(i.length>=26&&i.toString("ascii",0,2)==="BM"&&i.readUInt32LE(10)>=26&&[12,40,52,56,64,108,124].includes(i.readUInt32LE(14)))return"image/bmp";if(i.length>=22&&i.readUInt16LE(0)===0&&i.readUInt16LE(2)===1&&i.readUInt16LE(4)>0&&i.readUInt32LE(14)>0&&i.readUInt32LE(18)>=22)return"image/x-icon";if(i.length>=16&&i.toString("ascii",4,8)==="ftyp"){const e=i.readUInt32BE(0);if(e>=16&&e<=i.length&&e%4===0){for(let t=8;t<e;t+=4)if(t!==12&&["avif","avis"].includes(i.toString("ascii",t,t+4)))return"image/avif"}}return null}export{B as openImageFile};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import N from"node:http";import
|
|
1
|
+
import N from"node:http";import n from"node:fs";import f from"node:path";import{randomBytes as O}from"node:crypto";import{getAcaHome as j}from"../paths.js";import{managementActivity as e}from"./activity.js";import{enableManagedClaims as b,cancelManagedClaims as D}from"./claims.js";import{activeTerminalService as v}from"../terminal/service.js";import{readProcessStartTicks as k}from"../process-identity.js";import{AICA_VERSION as P}from"../../version.js";function E(s,y){const g=`${s}.${process.pid}.tmp`,i=n.openSync(g,"w",384);try{n.writeFileSync(i,JSON.stringify(y)),n.fsyncSync(i)}finally{n.closeSync(i)}n.renameSync(g,s);const c=n.openSync(f.dirname(s),"r");try{n.fsyncSync(c)}finally{n.closeSync(c)}}async function z(s,y,g){if(process.platform!=="linux")return;const i=f.join(j(),"management");n.mkdirSync(i,{recursive:!0,mode:448});const c=f.join(i,"worker.sock"),S=f.join(i,"worker.json");try{const r=JSON.parse(n.readFileSync(S,"utf8"));if(k(r.pid)===r.startTicks&&r.startTicks)throw new Error("已有 Worker 管理实例正在运行")}catch(r){if(r instanceof Error&&r.message==="已有 Worker 管理实例正在运行")throw r}n.rmSync(c,{force:!0});const I=O(32).toString("hex");let p=process.env.AICA_MANAGER_ID||"";const h=process.env.AICA_UPDATE_OPERATION||"";e.paused=!!h;let _=!1;const m=new Map,o=()=>({protocol:1,instanceId:s,pid:process.pid,startTicks:k(process.pid),version:P,entry:f.resolve(process.argv[1]),node:process.execPath,managerId:p,state:e.stopping?"stopping":e.paused?"paused":"running",operationId:h,busy:[...e.busy,...v?.hasActivity?["terminal"]:[]],uncertain:[...e.uncertain]}),u=N.createServer(async(r,d)=>{try{if(r.headers.authorization!==`Bearer ${I}`){d.writeHead(403).end();return}let l="";for await(const w of r)if(l+=w,l.length>16384)throw new Error("request_too_large");const t=JSON.parse(l||"{}");let a;if(t.command==="hello"||t.command==="status")a=o();else if(t.command==="manual"){if(t.instanceId!==s||e.paused||e.stopping)throw new Error("maintenance_in_progress");e.uncertain.add("manual-operation"),e.managed=!1,a={ok:!0}}else{if(t.instanceId!==s)throw new Error("worker_instance_changed");if(typeof t.managerId!="string"||!t.managerId)throw new Error("missing_manager");if(p&&p!==t.managerId)throw new Error("manager_conflict");if(t.command==="attach")await b(),p=t.managerId,e.managed=!0,a=o();else{if(!p)throw new Error("worker_not_attached");if(t.command==="detach"){if(e.paused||e.stopping)throw new Error("maintenance_in_progress");p="",e.managed=!1,a=o()}else if(t.command==="activate"){if(t.operationId!==h||!h||e.stopping)throw new Error("activation_mismatch");e.paused&&(e.paused=!1,g()),a=o()}else if(t.command==="canUpdate"){if(!t.operationId||!t.attemptId)throw new Error("missing_operation");const w=`${t.operationId}/${t.attemptId}`;if(m.has(w))a=m.get(w);else{if(!Number.isFinite(t.validUntil)||t.validUntil<Date.now()||t.validUntil>Date.now()+1e4)throw new Error("expired_update_attempt");if(_||e.paused||e.stopping||e.hasBusiness||v?.hasActivity||e.uncertain.has("manual-operation")||!e.managed)a={allowed:!1,...o()};else{_=!0,e.paused=!0;try{const A=Date.now()+1800;for(await D();e.busy.length&&Date.now()<A&&!e.hasBusiness;)await new Promise(T=>setTimeout(T,10));e.busy.length||v?.hasActivity||e.uncertain.has("manual-operation")||Date.now()>t.validUntil?a={allowed:!1,...o()}:(a={allowed:!0,...o(),state:"stopping",operationId:t.operationId,attemptId:t.attemptId},E(f.join(i,"shutdown.json"),a),e.stopping=!0,setTimeout(()=>{y()},50))}catch{a={allowed:!1,...o()}}finally{_=!1,e.stopping||(e.paused=!1)}}}m.size>1e3&&m.delete(m.keys().next().value),m.set(w,a)}else throw new Error("unsupported_command")}}d.setHeader("content-type","application/json"),d.end(JSON.stringify(a))}catch(l){d.writeHead(409,{"content-type":"application/json"}).end(JSON.stringify({error:l instanceof Error?l.message:"control_failed"}))}});u.requestTimeout=5e3,await new Promise((r,d)=>{u.once("error",d),u.listen(c,r)});try{n.chmodSync(c,384),E(S,{socketPath:c,secret:I,...o()})}catch(r){throw u.close(),r}return u}export{z as startManagementControl};
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const o="0.1.
|
|
1
|
+
const o="0.1.75";export{o as AICA_VERSION};
|