tweakcc 4.0.8 → 4.0.10

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,29 +0,0 @@
1
- import{E as e,Q as t,Z as n,b as r,c as i,d as a,f as o,l as s,n as c,p as l,r as u,x as d}from"./config-DDzjEq4l.mjs";import{Box as f,Text as p,render as m,useInput as h}from"ink";import g,{useState as ee}from"react";import*as te from"node:os";import _ from"node:os";import*as ne from"node:fs/promises";import v from"node:fs/promises";import y from"path";import{jsx as b,jsxs as x}from"react/jsx-runtime";import S from"which";import{WASMagic as C}from"wasmagic";import{globbySync as w}from"globby";const T=(()=>{let e=[],t=process.platform==`win32`?_.homedir().replace(/\\/g,`/`):_.homedir(),n=`node_modules/@anthropic-ai/claude-code`,r=(t,n=!1)=>{if(n)try{let n=w(t,{onlyFiles:!1});e.push({pattern:t,isGlob:!0,expandedPaths:n})}catch(n){n instanceof Error&&`code`in n&&(n.code===`EACCES`||n.code===`EPERM`)?o(`Permission denied accessing: ${t} (${n.code})`):o(`Error expanding glob pattern "${t}": ${n instanceof Error?n.message:String(n)}`),e.push({pattern:t,isGlob:!0,expandedPaths:[]})}else e.push({pattern:t,isGlob:!1,expandedPaths:[t]})};return r(`${_.homedir()}/.claude/local/${n}`),process.env.NPM_PREFIX&&r(`${process.env.NPM_PREFIX}/lib/${n}`),process.env.N_PREFIX&&r(`${process.env.N_PREFIX}/lib/${n}`),process.env.VOLTA_HOME&&r(`${process.env.VOLTA_HOME}/lib/${n}`),process.env.FNM_DIR&&r(`${process.env.FNM_DIR}/lib/${n}`),process.env.NVM_DIR&&r(`${process.env.NVM_DIR}/lib/${n}`),process.env.NODENV_ROOT&&r(`${process.env.NODENV_ROOT}/versions/*/lib/${n}`,!0),process.env.NVS_HOME&&r(`${process.env.NVS_HOME}/node/*/*/lib/${n}`,!0),process.env.ASDF_DATA_DIR&&r(`${process.env.ASDF_DATA_DIR}/installs/nodejs/*/lib/${n}`,!0),process.platform==`win32`?(r(`${t}/AppData/Local/Volta/tools/image/packages/@anthropic-ai/claude-code/${n}`),r(`${t}/AppData/Roaming/npm/${n}`),r(`${t}/AppData/Roaming/nvm/*/${n}`,!0),r(`${t}/AppData/Local/Yarn/config/global/${n}`),r(`${t}/AppData/Local/pnpm/global/*/${n}`,!0),r(`C:/nvm4w/nodejs/${n}`),r(`${t}/n/versions/node/*/lib/${n}`,!0),r(`${t}/AppData/Roaming/Yarn/config/global/${n}`),r(`${t}/AppData/Roaming/pnpm-global/${n}`),r(`${t}/AppData/Roaming/pnpm-global/*/${n}`,!0),r(`${t}/.bun/install/global/${n}`),r(`${t}/.bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0),r(`${t}/AppData/Local/Bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0),r(`${t}/AppData/Local/fnm_multishells/*/node_modules/${n}`,!0),r(`${t}/AppData/Local/mise/installs/node/*/${n}`,!0),r(`${t}/AppData/Local/mise/installs/npm-anthropic-ai-claude-code/*/${n}`,!0)):(process.platform==`darwin`&&(r(`${t}/Library/${n}`),r(`/opt/local/lib/${n}`),r(`${t}/.bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0),r(`${t}/Library/Caches/bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0)),r(`${t}/.local/lib/${n}`),r(`${t}/.local/share/${n}`),r(`${t}/.npm-global/lib/${n}`),r(`${t}/.npm-packages/lib/${n}`),r(`${t}/.npm/lib/${n}`),r(`${t}/npm/lib/${n}`),r(`/etc/${n}`),r(`/lib/${n}`),r(`/opt/node/lib/${n}`),r(`/usr/lib/${n}`),r(`/usr/local/lib/${n}`),r(`/usr/share/${n}`),r(`/var/lib/${n}`),r(`/opt/homebrew/lib/${n}`),r(`${t}/.linuxbrew/lib/${n}`),r(`${t}/.config/yarn/global/${n}`),r(`${t}/.yarn/global/${n}`),r(`${t}/.bun/install/global/${n}`),r(`${t}/.pnpm-global/${n}`),r(`${t}/.pnpm-global/*/${n}`,!0),r(`${t}/pnpm-global/${n}`),r(`${t}/pnpm-global/*/${n}`,!0),r(`${t}/.local/share/pnpm/global/${n}`),r(`${t}/.local/share/pnpm/global/*/${n}`,!0),r(`${t}/.bun/install/global/${n}`),r(`${t}/.bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0),r(`${t}/.local/share/bun/install/cache/@anthropic-ai/claude-code*@@@*`,!0),r(`/usr/local/n/versions/node/*/lib/${n}`,!0),r(`${t}/n/versions/node/*/lib/${n}`,!0),r(`${t}/n/lib/${n}`),r(`${t}/.volta/tools/image/node/*/lib/${n}`,!0),r(`${t}/.fnm/node-versions/*/installation/lib/${n}`,!0),r(`${t}/.local/state/fnm_multishells/*/lib/${n}`,!0),r(`/usr/local/nvm/versions/node/*/lib/${n}`,!0),r(`/usr/local/share/nvm/versions/node/*/lib/${n}`,!0),r(`${t}/.nvm/versions/node/*/lib/${n}`,!0),r(`${t}/.nodenv/versions/*/lib/${n}`,!0),r(`${t}/.nvs/*/lib/${n}`,!0),r(`${t}/.asdf/installs/nodejs/*/lib/${n}`,!0),process.env.MISE_DATA_DIR&&r(`${process.env.MISE_DATA_DIR}/installs/node/*/lib/${n}`,!0),r(`${t}/.local/share/mise/installs/node/*/lib/${n}`,!0),process.env.MISE_DATA_DIR&&r(`${process.env.MISE_DATA_DIR}/installs/npm-anthropic-ai-claude-code/*/lib/${n}`,!0),r(`${t}/.local/share/mise/installs/npm-anthropic-ai-claude-code/*/lib/${n}`,!0)),process.platform==`win32`&&e.forEach(e=>{e.pattern=e.pattern.replace(/\//g,`\\`),e.expandedPaths=e.expandedPaths.map(e=>e.replace(/\//g,`\\`))}),e})().flatMap(e=>e.expandedPaths),re=(()=>{let e=process.platform===`win32`?_.homedir().replace(/\\/g,`/`):_.homedir(),t=[],n=(e,n=!1)=>{if(n)try{let n=w(e,{onlyFiles:!0});t.push({pattern:e,isGlob:!0,expandedPaths:n})}catch(n){n instanceof Error&&`code`in n&&(n.code===`EACCES`||n.code===`EPERM`)?o(`Permission denied accessing: ${e} (${n.code})`):o(`Error expanding glob pattern "${e}": ${n instanceof Error?n.message:String(n)}`),t.push({pattern:e,isGlob:!0,expandedPaths:[]})}else t.push({pattern:e,isGlob:!1,expandedPaths:[e]})};return n(`${e}/.local/bin/claude`),n(`${e}/.local/share/claude/versions/*`,!0),process.platform===`win32`&&t.forEach(e=>{e.pattern=e.pattern.replace(/\//g,`\\`),e.expandedPaths=e.expandedPaths.map(e=>e.replace(/\//g,`\\`))}),t})().flatMap(e=>e.expandedPaths);var E=class extends Error{constructor(e){super(e),this.name=`InstallationDetectionError`}};let D=null;async function ie(){return D||=C.create().catch(e=>(o(`WASMagic initialization failed (likely SIMD unsupported on this CPU):`,e),o(`Using fallback file type detection`),null)),D}const ae=Buffer.from([127,69,76,70]),oe=Buffer.from([254,237,250,206]),se=Buffer.from([254,237,250,207]),O=Buffer.from([206,250,237,254]),k=Buffer.from([207,250,237,254]),A=Buffer.from([202,254,186,190]),j=Buffer.from([77,90]);function M(e){if(e.length===0)return null;if(e[0]===35&&e[1]===33&&e.subarray(0,Math.min(256,e.length)).toString(`utf8`).split(`
2
- `)[0].includes(`node`))return o(`detectFileTypeFallback: Detected JavaScript via shebang`),`javascript`;if(e.length>=4){let t=e.subarray(0,4);if(t.equals(ae))return o(`detectFileTypeFallback: Detected ELF binary`),`binary`;if(t.equals(oe)||t.equals(se)||t.equals(O)||t.equals(k)||t.equals(A))return o(`detectFileTypeFallback: Detected Mach-O binary`),`binary`}if(e.length>=2&&e.subarray(0,2).equals(j))return o(`detectFileTypeFallback: Detected PE binary`),`binary`;let t=Math.min(512,e.length),n=0,r=0;for(let i=0;i<t;i++){let t=e[i];(t>=32&&t<=126||t===9||t===10||t===13)&&n++,t===0&&r++}if(r>0)return o(`detectFileTypeFallback: Detected binary (${r} null bytes in first ${t} bytes)`),`binary`;let i=n/t;return i>.9?(o(`detectFileTypeFallback: Detected JavaScript via text heuristic (${Math.round(i*100)}% printable)`),`javascript`):(o(`detectFileTypeFallback: Unknown file type (${Math.round(i*100)}% printable)`),null)}async function N(e,t=4096){try{let n=await v.open(e,`r`);try{let e=Buffer.allocUnsafe(t),{bytesRead:r}=await n.read({buffer:e,position:0,length:t});return r<=0?null:e.subarray(0,r)}finally{await n.close()}}catch(e){return o(`Failed to read file prefix:`,e),null}}async function P(e){try{let t=await v.realpath(e);o(`resolvePathToInstallationType: ${e} -> ${t}`);let n=await N(t);if(!n)return o(`resolvePathToInstallationType: Could not read file prefix`),null;let r=await ie();if(r&&typeof r.detect==`function`){let e=r.detect(n)||null;if(e){let n=e.toLowerCase();return o(`resolvePathToInstallationType: Detected mime type: ${n}`),n.includes(`javascript`)?{kind:`npm-based`,resolvedPath:t}:n.startsWith(`text/`)?(o(`resolvePathToInstallationType: Unrecognized file type`),null):{kind:`native-binary`,resolvedPath:t}}}o(`resolvePathToInstallationType: Using fallback file type detection`);let i=M(n);return i===`javascript`?{kind:`npm-based`,resolvedPath:t}:i===`binary`?{kind:`native-binary`,resolvedPath:t}:(o(`resolvePathToInstallationType: Fallback could not determine file type`),null)}catch(e){return o(`resolvePathToInstallationType: Error:`,e),null}}async function F(){try{let e=await S(`claude`);return o(`getClaudeFromPath: Found claude at ${e}`),e}catch{return o(`getClaudeFromPath: claude not found on PATH`),null}}function I(e){let t=/\bVERSION:"(\d+\.\d+\.\d+)"/g,n=new Map,r;for(;(r=t.exec(e))!==null;){let e=r[1];n.set(e,(n.get(e)||0)+1)}if(n.size===0)return null;let i=0,a;for(let[e,t]of n.entries())o(`Found version ${e} with ${t} occurrences`),t>i&&(i=t,a=e);return a&&o(`Extracted version ${a} (${i} occurrences)`),a||null}async function L(e){let t=I(await v.readFile(e,`utf8`));if(!t)throw Error(`No VERSION strings found in JS file: ${e}`);return t}async function R(e){let t=await n(e);if(!t)throw Error(`Could not extract JS from native binary: ${e}`);let r=I(t.toString(`utf8`));if(!r)throw Error(`No VERSION strings found in extracted JS from: ${e}`);return r}function z(e){let t=y.basename(e).match(/^(\d+\.\d+\.\d+)$/);return t?t[1]:null}async function B(e,t){let n=z(e);return n?(o(`extractVersion: Got version ${n} from filename`),n):t===`npm-based`?L(e):R(e)}async function V(){let e=[],t=new Set;for(let n of T){let r=y.join(n,`cli.js`);if(!t.has(r))try{if(await l(r)){o(`collectCandidates: Found cli.js at ${r}`);let n=await L(r);e.push({path:r,kind:`npm-based`,version:n}),t.add(r)}}catch(e){o(`collectCandidates: Error checking ${r}:`,e)}}for(let n of re)if(!t.has(n))try{if(await l(n)){o(`collectCandidates: Found native binary at ${n}`);let r=await B(n,`native-binary`);e.push({path:n,kind:`native-binary`,version:r}),t.add(n)}}catch(e){o(`collectCandidates: Error checking ${n}:`,e)}return[...e].sort((e,t)=>a(e.version,t.version))}function H(e){return e.map(e=>` • ${e.path} (${e.kind}, v${e.version})`).join(`
3
- `)}function U(e){return` 2. Set ccInstallationPath in your config file (${u}):
4
-
5
- {
6
- "ccInstallationPath": "${e}"
7
- }`}function W(e){return` 1. Set the TWEAKCC_CC_INSTALLATION_PATH environment variable:
8
-
9
- export TWEAKCC_CC_INSTALLATION_PATH="${e}"`}function G(e){let t=e[0]?.path||`/path/to/claude`;return`Multiple Claude Code installations found.
10
-
11
- Found installations:
12
- ${H(e)}
13
-
14
- To specify which installation to use, either:
15
-
16
- ${W(t)}
17
-
18
- ${U(t)}`}function K(){return`Could not find Claude Code installation.
19
-
20
- To fix this, either:
21
-
22
- ${W(`/path/to/claude`)}
23
-
24
- ${U(`/path/to/claude`)}
25
-
26
- 3. Install Claude Code:
27
- • npm install -g @anthropic-ai/claude-code
28
- • Or download from https://claude.ai/download`}function q(e,t,n,r){return t===`npm-based`?{cliPath:e,version:n,source:r}:{nativeInstallationPath:e,version:n,source:r}}async function J(e,t){let n=process.env.TWEAKCC_CC_INSTALLATION_PATH?.trim();if(n&&n.length>0){if(o(`Checking TWEAKCC_CC_INSTALLATION_PATH: ${n}`),!await l(n))throw new E(`TWEAKCC_CC_INSTALLATION_PATH is set to '${n}' but file does not exist.`);let e=await P(n);if(!e)throw new E(`Unable to detect installation type from TWEAKCC_CC_INSTALLATION_PATH value '${n}'.\nExpected a Claude Code cli.js file or native binary.`);let t=await B(e.resolvedPath,e.kind);return d()&&e.kind===`npm-based`&&o(`SHA256 hash: ${await r(e.resolvedPath)}`),o(`Using Claude Code from TWEAKCC_CC_INSTALLATION_PATH: ${e.resolvedPath} (${e.kind}, v${t})`),q(e.resolvedPath,e.kind,t,`env-var`)}if(e.ccInstallationPath){let t=e.ccInstallationPath;if(o(`Checking ccInstallationPath: ${t}`),!await l(t))throw new E(`ccInstallationPath is set to '${t}' but file does not exist.`);let n=await P(t);if(!n)throw new E(`Unable to detect installation type from configured ccInstallationPath '${t}'.\nExpected a Claude Code cli.js file or native binary.`);let i=await B(n.resolvedPath,n.kind);return d()&&n.kind===`npm-based`&&o(`SHA256 hash: ${await r(n.resolvedPath)}`),o(`Using Claude Code from ccInstallationPath: ${n.resolvedPath} (${n.kind}, v${i})`),q(n.resolvedPath,n.kind,i,`config`)}let i=await F();if(i){o(`Checking claude on PATH: ${i}`);let e=await P(i);if(!e)o(`Unable to detect installation type from 'claude' on PATH (${i}). Falling back to hardcoded search paths.`);else{let t=await B(e.resolvedPath,e.kind);return d()&&e.kind===`npm-based`&&o(`SHA256 hash: ${await r(e.resolvedPath)}`),o(`Using Claude Code from PATH: ${e.resolvedPath} (${e.kind}, v${t})`),q(e.resolvedPath,e.kind,t,`path`)}}o(`Collecting candidates from hardcoded search paths...`);let a=await V();if(a.length===0){if(t.interactive)return null;throw new E(K())}if(a.length===1){let e=a[0];return o(`Using single candidate: ${e.path} (${e.kind}, v${e.version})`),q(e.path,e.kind,e.version,`search-paths`)}if(!t.interactive)throw new E(G(a));return{version:``,source:`search-paths`,_pendingCandidates:a}}function Y(e){return e._pendingCandidates||null}async function ce(e){return o(`Saving selected installation to config: ${e.path}`),await s(t=>{t.ccInstallationPath=e.path}),q(e.path,e.kind,e.version,`config`)}function le(){return K()}function X({candidates:e,onSelect:t,onCancel:n}){let[r,i]=ee(0);return h((a,o)=>{o.escape?n?n():process.exit(0):o.upArrow?i(t=>t>0?t-1:e.length-1):o.downArrow?i(t=>t<e.length-1?t+1:0):o.return&&t(e[r])}),x(f,{flexDirection:`column`,children:[b(p,{bold:!0,color:`yellow`,children:`No claude executable was found in PATH, but multiple Claude Code installations were found on this machine. Please select one:`}),b(p,{children:` `}),e.map((e,t)=>x(f,{children:[x(p,{bold:t===r,color:t===r?`cyan`:void 0,children:[t===r?`❯ `:` `,e.path]}),x(p,{dimColor:!0,children:[` `,`(`,e.kind,`, v`,e.version,`)`]})]},e.path)),b(p,{children:` `}),x(p,{children:[`Your choice will be saved to`,` `,b(p,{color:`blue`,children:`ccInstallationPath`}),` in`,` `,x(p,{color:`blue`,children:[c.replace(te.homedir(),`~`),`/config.json`]}),`.`]}),b(p,{children:` `}),b(p,{dimColor:!0,children:`Use ↑↓ arrows to navigate, Enter to select, Esc to quit`})]})}function Z(e){return{path:e.path,version:e.version,kind:e.kind===`npm-based`?`npm`:`native`}}function Q(e){let t=e.cliPath??e.nativeInstallationPath;if(!t)throw Error(`Installation info has neither cliPath nor nativeInstallationPath`);return{path:t,version:e.version,kind:e.cliPath?`npm`:`native`}}async function ue(){return(await V()).map(Z)}async function de(e={}){if(e.path){let t=await P(e.path);if(!t)throw Error(`Unable to detect installation type from path '${e.path}'. Expected a Claude Code cli.js file or native binary.\n`);let n=await B(t.resolvedPath,t.kind);return{path:t.resolvedPath,version:n,kind:t.kind===`npm-based`?`npm`:`native`}}let t=await J(await i(),{interactive:!0});if(!t)throw Error(`Could not find Claude Code installation. Install Claude Code or specify the path explicitly.`);let n=Y(t);if(n){if(!e.interactive){let e=n.map(e=>` • ${e.path} (${e.kind}, v${e.version})`).join(`
29
- `);throw Error(`Multiple Claude Code installations found:\n${e}\n\nPass { interactive: true } to show a picker, or { path: "..." } to specify explicitly.\nAlternatively, set "ccInstallationPath" in ${u} to the path to a Claude Code installation.`)}let t=await $(n.map(Z));if(!t)throw Error(`No installation selected.`);return t}return Q(t)}async function $(e){let t=e.map(e=>({path:e.path,version:e.version,kind:e.kind===`npm`?`npm-based`:`native-binary`}));return new Promise((e,n)=>{let r,i=()=>{try{r.unmount()}catch{}process.stdin.ref()},a=t=>{i(),e(Z(t))},o=()=>{i(),e(null)};try{r=m(g.createElement(X,{candidates:t,onSelect:a,onCancel:o}),{exitOnCtrlC:!1}),r.waitUntilExit().catch(e=>{i(),n(e instanceof Error?e:Error(String(e)))})}catch(e){process.stdin.ref(),n(e instanceof Error?e:Error(String(e)))}})}async function fe(e){if(e.kind===`native`){let t=await n(e.path);if(!t)throw Error(`Failed to extract JavaScript from native installation: ${e.path}`);return t.toString(`utf8`)}else return ne.readFile(e.path,{encoding:`utf8`})}async function pe(n,r){if(n.kind===`native`){let e=Buffer.from(r,`utf8`);await t(n.path,e,n.path)}else await e(n.path,r,`patch`)}export{de as a,J as c,ce as d,$ as i,le as l,pe as n,X as o,ue as r,E as s,fe as t,Y as u};
@@ -1,4 +0,0 @@
1
- import{f as e,x as t}from"./config-DDzjEq4l.mjs";import n from"node:fs";import{execSync as r}from"node:child_process";import i from"node-lief";const a=Buffer.from(`
2
- ---- Bun! ----
3
- `);function o(e,t){return e.subarray(t.offset,t.offset+t.length)}function s(e,t){return{offset:e.readUInt32LE(t),length:e.readUInt32LE(t+4)}}function c(e){return e.endsWith(`/claude`)||e===`claude`||e.endsWith(`/claude.exe`)||e===`claude.exe`}function l(t){let n=t%52==0,r=t%36==0;return n&&!r?52:r&&!n?36:n&&r?(e(`detectModuleStructSize: Ambiguous module list length ${t}, assuming new format`),52):(e(`detectModuleStructSize: Module list length ${t} doesn't cleanly divide by either struct size, assuming new format`),52)}function u(e,t,n,r){let i=o(e,t.modulesPtr),a=Math.floor(i.length/n);for(let t=0;t<a;t++){let a=f(i,t*n,n),s=r(a,o(e,a.name).toString(`utf-8`),t);if(s!==void 0)return s}}function d(e){let t=0,n=e.readBigUInt64LE(t);t+=8;let r=s(e,t);t+=8;let i=e.readUInt32LE(t);t+=4;let a=s(e,t);return t+=8,{byteCount:n,modulesPtr:r,entryPointId:i,compileExecArgvPtr:a,flags:e.readUInt32LE(t)}}function f(e,t,n){let r=t,i=s(e,r);r+=8;let a=s(e,r);r+=8;let o=s(e,r);r+=8;let c=s(e,r);r+=8;let l,u;n===52?(l=s(e,r),r+=8,u=s(e,r),r+=8):(l={offset:0,length:0},u={offset:0,length:0});let d=e.readUInt8(r);r+=1;let f=e.readUInt8(r);r+=1;let p=e.readUInt8(r);r+=1;let m=e.readUInt8(r);return{name:i,contents:a,sourcemap:o,bytecode:c,moduleInfo:l,bytecodeOriginPath:u,encoding:d,loader:f,moduleFormat:p,side:m}}function p(t){if(t.length<32+a.length)throw Error(`BUN data is too small to contain trailer and offsets`);let n=t.length-a.length,r=t.subarray(n);if(e(`parseBunDataBlob: Expected trailer: ${a.toString(`hex`)}`),e(`parseBunDataBlob: Got trailer: ${r.toString(`hex`)}`),!r.equals(a))throw e(`Expected: ${a.toString(`hex`)}`),e(`Got: ${r.toString(`hex`)}`),Error(`BUN trailer bytes do not match trailer`);let i=t.length-32-a.length,o=d(t.subarray(i,i+32));return{bunOffsets:o,bunData:t,moduleStructSize:l(o.modulesPtr.length)}}function m(t){if(t.length<4)throw Error(`Section data too small`);e(`extractBunDataFromSection: sectionData.length=${t.length}`);let n=t.readUInt32LE(0),r=4+n,i=t.length>=8?Number(t.readBigUInt64LE(0)):0,a=8+i;e(`extractBunDataFromSection: u32 header would give size=${n}, expected total=${r}`),e(`extractBunDataFromSection: u64 header would give size=${i}, expected total=${a}`);let o,s;if(t.length>=8&&a<=t.length&&a>=t.length-4096)o=8,s=i,e(`extractBunDataFromSection: detected u64 header format (Bun >= 1.3.4)`);else if(r<=t.length&&r>=t.length-4096)o=4,s=n,e(`extractBunDataFromSection: detected u32 header format (Bun < 1.3.4)`);else throw Error(`Cannot determine section header format: sectionData.length=${t.length}, u64 would expect ${a}, u32 would expect ${r}`);e(`extractBunDataFromSection: bunDataSize from header=${s}`);let c=t.subarray(o,o+s);e(`extractBunDataFromSection: bunDataContent.length=${c.length}`);let{bunOffsets:l,bunData:u,moduleStructSize:d}=p(c);return{bunOffsets:l,bunData:u,sectionHeaderSize:o,moduleStructSize:d}}function h(t){if(!t.hasOverlay)throw Error(`ELF binary has no overlay data`);let n=t.overlay;if(e(`extractBunDataFromELFOverlay: Overlay size=${n.length} bytes`),n.length<a.length+8+32)throw Error(`ELF overlay data is too small`);let r=n.readBigUInt64LE(n.length-8);if(e(`extractBunDataFromELFOverlay: Total byte count from tail=${r}`),r<4096n||r>2n**32n-1n)throw Error(`ELF total byte count is out of range: ${r}`);let i=n.length-8-a.length,o=n.subarray(i,n.length-8);if(e(`extractBunDataFromELFOverlay: Expected trailer: ${a.toString(`hex`)}`),e(`extractBunDataFromELFOverlay: Got trailer: ${o.toString(`hex`)}`),!o.equals(a))throw Error(`BUN trailer bytes do not match trailer`);let s=n.length-8-a.length-32,c=n.subarray(s,n.length-8-a.length),u=d(c);e(`extractBunDataFromELFOverlay: Offsets.byteCount=${u.byteCount}`);let f=typeof u.byteCount==`bigint`?u.byteCount:BigInt(u.byteCount);if(f>=r)throw Error(`ELF total byte count is out of range`);let p=8+a.length+32,m=n.length-p-Number(f),h=n.subarray(m,n.length-p);return e(`extractBunDataFromELFOverlay: Extracted ${h.length} bytes of data`),{bunOffsets:u,bunData:Buffer.concat([h,c,o]),moduleStructSize:l(u.modulesPtr.length)}}function g(e){let t=e.getSegment(`__BUN`);if(!t)throw Error(`__BUN segment not found`);let n=t.getSection(`__bun`);if(!n)throw Error(`__bun section not found`);return m(n.content)}function _(e){let t=e.sections().find(e=>e.name===`.bun`);if(!t)throw Error(`.bun section not found`);return m(t.content)}function v(t){switch(e(`getBunData: Binary format detected as ${t.format}`),t.format){case`MachO`:return g(t);case`PE`:return _(t);case`ELF`:return h(t);default:throw Error(`Unsupported binary format: ${t.format}`)}}function y(t){try{i.logging.disable();let{bunOffsets:n,bunData:r,moduleStructSize:a}=v(i.parse(t));return e(`extractClaudeJsFromNativeInstallation: Got bunData, size=${r.length} bytes, moduleStructSize=${a}`),u(r,n,a,(t,n,i)=>{if(e(`extractClaudeJsFromNativeInstallation: Module ${i}: ${n}`),!c(n))return;let a=o(r,t.contents);return e(`extractClaudeJsFromNativeInstallation: Found claude module, contents length=${a.length}`),a.length>0?a:void 0})||(e(`extractClaudeJsFromNativeInstallation: claude module not found in any module`),null)}catch(t){return e(`extractClaudeJsFromNativeInstallation: Error during extraction:`,t),null}}function b(e,t,n,r){let i=[],s=[];u(e,t,r,(t,a)=>{let l=o(e,t.name),u;u=n&&c(a)?n:o(e,t.contents);let d=o(e,t.sourcemap),f=o(e,t.bytecode),p=o(e,t.moduleInfo),m=o(e,t.bytecodeOriginPath);s.push({name:l,contents:u,sourcemap:d,bytecode:f,moduleInfo:p,bytecodeOriginPath:m,encoding:t.encoding,loader:t.loader,moduleFormat:t.moduleFormat,side:t.side}),r===52?i.push(l,u,d,f,p,m):i.push(l,u,d,f)});let l=r===52?6:4,d=0,f=[];for(let e of i)f.push({offset:d,length:e.length}),d+=e.length+1;let p=d,m=s.length*r;d+=m;let h=o(e,t.compileExecArgvPtr),g=d,_=h.length;d+=_+1;let v=d;d+=32;let y=d;d+=a.length;let b=Buffer.allocUnsafe(d);b.fill(0);let x=0;for(let{offset:e,length:t}of f)t>0&&i[x].copy(b,e,0,t),b[e+t]=0,x++;_>0&&(h.copy(b,g,0,_),b[g+_]=0);for(let e=0;e<s.length;e++){let t=s[e],n=e*l,i={name:f[n],contents:f[n+1],sourcemap:f[n+2],bytecode:f[n+3],moduleInfo:r===52?f[n+4]:{offset:0,length:0},bytecodeOriginPath:r===52?f[n+5]:{offset:0,length:0},encoding:t.encoding,loader:t.loader,moduleFormat:t.moduleFormat,side:t.side},a=p+e*r;b.writeUInt32LE(i.name.offset,a),b.writeUInt32LE(i.name.length,a+4),a+=8,b.writeUInt32LE(i.contents.offset,a),b.writeUInt32LE(i.contents.length,a+4),a+=8,b.writeUInt32LE(i.sourcemap.offset,a),b.writeUInt32LE(i.sourcemap.length,a+4),a+=8,b.writeUInt32LE(i.bytecode.offset,a),b.writeUInt32LE(i.bytecode.length,a+4),a+=8,r===52&&(b.writeUInt32LE(i.moduleInfo.offset,a),b.writeUInt32LE(i.moduleInfo.length,a+4),a+=8,b.writeUInt32LE(i.bytecodeOriginPath.offset,a),b.writeUInt32LE(i.bytecodeOriginPath.length,a+4),a+=8),b.writeUInt8(i.encoding,a),b.writeUInt8(i.loader,a+1),b.writeUInt8(i.moduleFormat,a+2),b.writeUInt8(i.side,a+3)}let S={byteCount:v,modulesPtr:{offset:p,length:m},entryPointId:t.entryPointId,compileExecArgvPtr:{offset:g,length:_},flags:t.flags},C=v,w=typeof S.byteCount==`bigint`?S.byteCount:BigInt(S.byteCount);return b.writeBigUInt64LE(w,C),C+=8,b.writeUInt32LE(S.modulesPtr.offset,C),b.writeUInt32LE(S.modulesPtr.length,C+4),C+=8,b.writeUInt32LE(S.entryPointId,C),C+=4,b.writeUInt32LE(S.compileExecArgvPtr.offset,C),b.writeUInt32LE(S.compileExecArgvPtr.length,C+4),C+=8,b.writeUInt32LE(S.flags,C),a.copy(b,y),b}function x(e,t,r,i=!0){let a=t+`.tmp`;if(e.write(a),i){let e=n.statSync(r);n.chmodSync(a,e.mode)}try{n.renameSync(a,t)}catch(e){try{n.existsSync(a)&&n.unlinkSync(a)}catch{}throw e instanceof Error&&`code`in e&&(e.code===`ETXTBSY`||e.code===`EBUSY`||e.code===`EPERM`)?Error(`Cannot update the Claude executable while it is running.
4
- Please close all Claude instances and try again.`):e}}function S(e,t=8){let n=Buffer.allocUnsafe(t+e.length);return t===8?n.writeBigUInt64LE(BigInt(e.length),0):n.writeUInt32LE(e.length,0),e.copy(n,t),n}function C(n,a,o,s,c){try{e(`repackMachO: Has code signature: ${n.hasCodeSignature}`),n.hasCodeSignature&&(e(`repackMachO: Removing code signature...`),n.removeSignature());let l=n.getSegment(`__BUN`);if(!l)throw Error(`__BUN segment not found`);let u=l.getSection(`__bun`);if(!u)throw Error(`__bun section not found`);let d=S(o,c);e(`repackMachO: Original section size: ${u.size}`),e(`repackMachO: Original segment fileSize: ${l.fileSize}`),e(`repackMachO: Original segment virtualSize: ${l.virtualSize}`),e(`repackMachO: New data size: ${d.length}`),e(`repackMachO: Using header size: ${c}`);let f=d.length-Number(u.size);if(f>0){let t=n.header.cpuType===i.MachO.Header.CPU_TYPE.ARM64,r=t?16384:4096,a=Math.ceil(f/r)*r;e(`repackMachO: CPU type: ${t?`ARM64`:`x86_64`}`),e(`repackMachO: Page size: ${r} bytes`),e(`repackMachO: Need to expand by ${f} bytes`),e(`repackMachO: Rounding up to page-aligned: ${a} bytes`);let o=n.extendSegment(l,a);if(e(`repackMachO: extendSegment returned: ${o}`),!o)throw Error(`Failed to extend __BUN segment`);e(`repackMachO: Section size after extend: ${u.size}`),e(`repackMachO: Segment fileSize after extend: ${l.fileSize}`),e(`repackMachO: Segment virtualSize after extend: ${l.virtualSize}`)}u.content=d,u.size=BigInt(d.length),e(`repackMachO: Final section size: ${u.size}`),e(`repackMachO: Writing modified binary to ${s}...`),x(n,s,a);try{e(`repackMachO: Re-signing binary with ad-hoc signature...`),r(`codesign -s - -f "${s}"`,{stdio:t()?`inherit`:`ignore`}),e(`repackMachO: Code signing completed successfully`)}catch(e){console.warn(`Warning: Failed to re-sign binary. The binary may not run correctly on macOS:`,e)}e(`repackMachO: Write completed successfully`)}catch(e){throw console.error(`repackMachO failed:`,e),e}}function w(t,n,r,i,a){try{let o=t.sections().find(e=>e.name===`.bun`);if(!o)throw Error(`.bun section not found`);let s=S(r,a);e(`repackPE: Original section size: ${o.size}, virtual size: ${o.virtualSize}`),e(`repackPE: New data size: ${s.length}`),e(`repackPE: Using header size: ${a}`),o.content=s,o.virtualSize=BigInt(s.length),o.size=BigInt(s.length),e(`repackPE: Writing modified binary to ${i}...`),x(t,i,n,!1),e(`repackPE: Write completed successfully`)}catch(e){throw console.error(`repackPE failed:`,e),e}}function T(t,n,r,i){try{let a=Buffer.allocUnsafe(r.length+8);r.copy(a,0),a.writeBigUInt64LE(BigInt(r.length),r.length),e(`repackELF: Setting overlay data (${a.length} bytes)`),t.overlay=a,e(`repackELF: Writing modified binary to ${i}...`),x(t,i,n),e(`repackELF: Write completed successfully`)}catch(e){throw console.error(`repackELF failed:`,e),e}}function E(e,t,n){i.logging.disable();let r=i.parse(e),{bunOffsets:a,bunData:o,sectionHeaderSize:s,moduleStructSize:c}=v(r),l=b(o,a,t,c);switch(r.format){case`MachO`:if(!s)throw Error(`sectionHeaderSize is required for Mach-O binaries`);C(r,e,l,n,s);break;case`PE`:if(!s)throw Error(`sectionHeaderSize is required for PE binaries`);w(r,e,l,n,s);break;case`ELF`:T(r,e,l,n);break;default:throw Error(`Unsupported binary format: ${r.format}`)}}export{y as extractClaudeJsFromNativeInstallation,E as repackNativeInstallation};