UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.47 kB
import{createRequire}from"node:module"; //!src/node-host/node-worker-publication-snapshot.generated.worker.mjs const require=createRequire(import.meta.url);process.argv.splice(1,1);const fs=require("node:fs");const os=require("node:os");const path=require("node:path");const crypto=require("node:crypto");const{spawnSync}=require("node:child_process");const[cwd,baseCommit,output]=process.argv.slice(1);const maxFile=67108864;const maxTotal=805306368;const maxEntries=5e5;const objectPattern=/^[a-f0-9]{40}$/;const gitNullPath=process.platform===`win32`?`NUL`:`/dev/null`;const env={...process.env,GIT_NO_REPLACE_OBJECTS:`1`,GIT_ATTR_NOSYSTEM:`1`,GIT_CONFIG_GLOBAL:gitNullPath,GIT_CONFIG_SYSTEM:gitNullPath,GIT_CONFIG_KEY_0:`maintenance.auto`,GIT_CONFIG_VALUE_0:`false`,GIT_CONFIG_KEY_1:`gc.auto`,GIT_CONFIG_VALUE_1:`0`,GIT_CONFIG_COUNT:`2`};function git(e,t={}){const n=spawnSync(`git`,[`-c`,`core.hooksPath=`+gitNullPath,`-c`,`core.fsmonitor=false`,`-c`,`core.attributesFile=`+gitNullPath,...e],{cwd,env,timeout:6e4,maxBuffer:67108865,...t});if(n.error||n.status!==0)throw new Error(`Publication snapshot Git command failed`);return n.stdout}function text(e){return git(e).toString(`utf8`).trim()}function noFilters(e){for(const t of e.toString(`latin1`).split(/\r?\n/)){const e=t.trimStart().split(/[\t ]+/);if(e[0]&&!e[0].startsWith(`#`)&&e.slice(1).some(e=>/^(?:-|!)?filter(?:=|$)/.test(e)))throw new Error(`Publication snapshot uses an unsupported Git clean filter`)}}if(!objectPattern.test(baseCommit))throw new Error(`Publication snapshot base commit is invalid`);const relativeOutput=path.relative(path.resolve(cwd),path.resolve(output));if(!relativeOutput.startsWith(`..`+path.sep)&&!path.isAbsolute(relativeOutput))throw new Error(`Publication snapshot output must be outside the workspace`);const scopes=[`--local`];const worktreeConfig=spawnSync(`git`,[`config`,`--local`,`--includes`,`--bool`,`--default=false`,`--get`,`extensions.worktreeConfig`],{cwd,env,timeout:6e4,maxBuffer:131072});if(worktreeConfig.error||worktreeConfig.status!==0)throw new Error(`Publication workspace has unsupported Git transport configuration`);if(worktreeConfig.stdout.toString(`utf8`).trim()===`true`)scopes.push(`--worktree`);for(const e of scopes){const t=spawnSync(`git`,[`config`,e,`--includes`,`--get-regexp`,`^(core\\.(alternaterefscommand|askpass|fsmonitor|gitproxy|sshcommand|worktree)|credential\\..*helper|filter\\..*|http\\..*|include(if)?\\..*|push\\..*|remote\\..*\\.(proxy|receivepack|uploadpack|vcs)|uploadpack\\.packobjectshook|url\\..*\\.(insteadof|pushinsteadof))$`],{cwd,env,timeout:6e4,maxBuffer:131072});if(t.error||t.status!==1||t.stdout.length)throw new Error(`Publication workspace has unsupported Git transport configuration`)}if(text([`for-each-ref`,`--count=1`,`--format=%(refname)`,`refs/replace`]))throw new Error(`Publication snapshot has unsupported Git replacement metadata`);for(const e of[`info/grafts`,`info/attributes`]){const t=path.resolve(cwd,text([`rev-parse`,`--git-path`,e]));if(!fs.existsSync(t))continue;const n=fs.readFileSync(t);if(e===`info/grafts`&&n.length)throw new Error(`Publication snapshot has Git grafts`);if(e===`info/attributes`)noFilters(n)}const temporary=fs.mkdtempSync(path.join(os.tmpdir(),`openclaw-publication-index-`));try{const e=path.resolve(cwd,text([`rev-parse`,`--git-path`,`index`]));env.GIT_INDEX_FILE=path.join(temporary,`index`);const t=fs.statSync(e,{bigint:true});fs.copyFileSync(e,env.GIT_INDEX_FILE);const n=Number(t.mtimeNs/1000000000n);fs.utimesSync(env.GIT_INDEX_FILE,n,n);git([`write-tree`]);git([`add`,`-A`]);const r=text([`write-tree`]);const i=text([`rev-parse`,baseCommit+`^{tree}`]);const a=git([`ls-tree`,`-r`,`-z`,`--full-tree`,r]);let o=0;for(const e of a.toString(`utf8`).split(`\0`)){const t=e.indexOf(` `);const n=e.slice(t+1).toLowerCase();if(t<0||n!==`.gitattributes`&&!n.endsWith(`/.gitattributes`))continue;if(++o>1024)throw new Error(`Publication snapshot has too many attribute files`);noFilters(git([`cat-file`,`blob`,e.slice(0,t).split(` `)[2]]))}const s=git([`diff-tree`,`--no-commit-id`,`--raw`,`-r`,`-z`,`--no-renames`,i,r]);const c=s.toString(`utf8`);if(!Buffer.from(c,`utf8`).equals(s))throw new Error(`Publication snapshot paths must be UTF-8`);const l=c.split(`\0`);const u=[];const d=/* @__PURE__ */ new Set;let f=0;fs.mkdirSync(path.join(output,`blobs`),{recursive:true,mode:448});for(let e=0;e<l.length-1;e+=2){const[t,n,,r,i]=l[e].slice(1).split(` `);const a=i===`D`;const o=a?t:n;const s=a?null:r;if(![`100644`,`100755`,`120000`,`160000`].includes(o)||s!==null&&!objectPattern.test(s))throw new Error(`Publication snapshot entry is invalid`);u.push({path:l[e+1],mode:o,sha:s});if(u.length>maxEntries)throw new Error(`Publication snapshot has too many changes`);if(!s||o===`160000`||d.has(s))continue;const c=Number(text([`cat-file`,`-s`,s]));if(!Number.isSafeInteger(c)||c<0||c>maxFile||f+c>maxTotal)throw new Error(`Publication snapshot exceeds its byte budget`);const p=git([`cat-file`,`blob`,s]);if(p.length!==c)throw new Error(`Publication snapshot blob changed`);f+=c;fs.writeFileSync(path.join(output,`blobs`,s),p,{mode:384,flag:`wx`});d.add(s)}const p=JSON.stringify({version:1,baseCommit,baseTree:i,workspaceTree:r,entries:u});fs.writeFileSync(path.join(output,`snapshot.json`),p,{mode:384,flag:`wx`});process.stdout.write(`sha256:`+crypto.createHash(`sha256`).update(p).digest(`hex`))}finally{fs.rmSync(temporary,{recursive:true,force:true})}export{};