@preventive/triage 1.0.0-alpha.0 → 1.0.0-alpha.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.
- package/api/reap.ts +79 -0
- package/common/save-error-reason.ts +20 -7
- package/common/server-info.ts +30 -0
- package/out/brotli-fallback.js +1 -1
- package/out/client-admin.js +28 -0
- package/out/client-managed.js +1 -0
- package/out/client-sync.js +17 -10
- package/out/graph.js +5 -4
- package/out/index.html +43 -38
- package/out/prism.js +2 -2
- package/out/terminal.js +32 -28
- package/out/view.css +1 -1
- package/out/view.js +78 -51
- package/package.json +70 -49
- package/{server → server-common}/origin.ts +5 -5
- package/{server → server-e2e}/auth.ts +16 -1
- package/server-e2e/bus-receiver.ts +95 -0
- package/server-e2e/cli.js +22 -0
- package/{server → server-e2e}/config.ts +21 -8
- package/{server → server-e2e}/db-neon.ts +41 -25
- package/{server → server-e2e}/db-revision-sql.ts +15 -9
- package/{server → server-e2e}/db-stmt.ts +2 -2
- package/{server → server-e2e}/db.ts +113 -135
- package/server-e2e/http.ts +266 -0
- package/{server → server-e2e}/hub.ts +27 -8
- package/{server → server-e2e}/index.ts +185 -52
- package/{server → server-e2e}/lifecycle.ts +36 -5
- package/server-e2e/npm-proxy.ts +348 -0
- package/{server → server-e2e}/objstore/blob-fs.ts +6 -8
- package/{server → server-e2e}/objstore/blob-vercel.ts +69 -36
- package/{server → server-e2e}/objstore/blob.ts +24 -9
- package/server-e2e/objstore/fetch-mint-guard.ts +74 -0
- package/{server → server-e2e}/objstore/handlers.ts +25 -15
- package/{server → server-e2e}/objstore/init.ts +52 -12
- package/{server → server-e2e}/objstore/reaper.ts +31 -11
- package/server-e2e/objstore/rest-deny.ts +28 -0
- package/server-e2e/objstore/rest-mint.ts +224 -0
- package/{server → server-e2e}/objstore/rest.ts +119 -84
- package/{server → server-e2e}/objstore/sign.ts +105 -0
- package/{server → server-e2e}/objstore/store-neon.ts +19 -19
- package/{server → server-e2e}/objstore/store.ts +98 -118
- package/{server → server-e2e}/objstore/tokens.ts +9 -12
- package/{server → server-e2e}/peer.ts +7 -9
- package/server-e2e/pubsub.ts +394 -0
- package/{server → server-e2e}/sign.ts +12 -14
- package/server-e2e/sse-server.ts +384 -0
- package/server-e2e/sse-session.ts +216 -0
- package/{server → server-e2e}/static.ts +22 -17
- package/server-e2e/sync-handlers.ts +382 -0
- package/{server → server-e2e}/util.ts +9 -0
- package/server-e2e/ws-server.ts +276 -0
- package/strip-types-loader.js +94 -0
- package/server/http.ts +0 -142
- package/server/sync-handlers.ts +0 -311
- package/server/ws-server.ts +0 -245
- /package/{server → server-e2e}/config.example.json +0 -0
- /package/{server → server-e2e}/neon-driver.ts +0 -0
- /package/{server → server-e2e}/objstore/fs.ts +0 -0
- /package/{server → server-e2e}/validation.ts +0 -0
package/out/terminal.js
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Q=globalThis[Symbol.for("@rray/frontend")];if(!Q)throw new Error("@rray/frontend global not installed; view.js must run before lazy bundles import this wrapper");var{LitElement:tt,html:M,nothing:H,unsafeCSS:et,render:jn,StateElement:qn,classMap:Pn,repeat:Hn,styleMap:Bn}=Q;function nt(t,e){if(t.length===0)return{argv:[],quoted:new Set};let r=[t[0]],n=new Set;e.has(0)&&n.add(0);for(let s=1;s<t.length;s++){let o=t[s];if(e.has(s)){n.add(r.length),r.push(o);continue}for(let i of rt(o))r.push(i)}return{argv:r,quoted:n}}function rt(t){for(let e=0;e<t.length;e++){if(t[e]!=="{")continue;let r=Gt(t,e);if(r===-1)continue;let n=Ut(t.slice(e+1,r));if(n.length<2)continue;let s=t.slice(0,e),o=t.slice(r+1),i=[];for(let u of n)i.push(...rt(s+u+o));return i}return[t]}function Gt(t,e){let r=1;for(let n=e+1;n<t.length;n++)if(t[n]==="{")r++;else if(t[n]==="}"&&(r--,r===0))return n;return-1}function Ut(t){let e=[],r=0,n=0;for(let s=0;s<t.length;s++)t[s]==="{"?r++:t[s]==="}"?r--:t[s]===","&&r===0&&(e.push(t.slice(n,s)),n=s+1);return e.push(t.slice(n)),e}function st(t){let e=Vt(t);for(let s of e)if(s.kind==="amp")throw new Error("background processes (`&`) are not supported");for(;e.length>0&&e.at(-1).kind==="semi";)e.pop();let{steps:r,consumed:n}=it(e,0,!1);if(n!==e.length)throw new Error("unexpected `)`");return ot(r),r}function ot(t){for(let e of t){if(e.stages.length===0)throw new Error("empty pipeline stage");for(let r of e.stages)if(r.group)ot(r.group);else if(r.argv.length===0)throw new Error("empty pipeline stage")}}function it(t,e,r){let n=[{gate:"first",stages:[]}],s={argv:[],quoted:new Set},o=e;for(;o<t.length;){let i=t[o];if(i.kind==="paren_close"){if(!r)throw new Error("unexpected `)`");return zt(n,s,o+1)}if(i.kind==="paren_open"){if(s.group||s.argv.length>0)throw new Error("unexpected `(`");let u=it(t,o+1,!0);s.group=u.steps,o=u.consumed;continue}if(i.kind==="pipe"||i.kind==="and"||i.kind==="or"||i.kind==="semi"){n.at(-1).stages.push(s),s={argv:[],quoted:new Set},i.kind==="and"?n.push({gate:"and",stages:[]}):i.kind==="or"?n.push({gate:"or",stages:[]}):i.kind==="semi"&&n.push({gate:"seq",stages:[]}),o++;continue}if(i.kind==="redir"){o=Xt(s,t,o)+1;continue}if(s.group)throw new Error("unexpected token after `)`");i.quoted&&s.quoted.add(s.argv.length),s.argv.push(i.value),o++}if(r)throw new Error("unmatched `(`");return n.at(-1).stages.push(s),{steps:n,consumed:o}}function zt(t,e,r){let n=t.at(-1);if(!e.group&&e.argv.length===0&&!Yt(e)&&n.stages.length===0){if(t.length===1)throw new Error("empty subshell `()`");t.pop()}else n.stages.push(e);return{steps:t,consumed:r}}function Yt(t){return!!(t.stdoutToNull||t.stderrToNull||t.mergeStderrToStdout||t.mergeStdoutToStderr)}function Xt(t,e,r){let n=e[r],s=n.fd==="1"?"":n.fd;if(n.toFd)return n.fd==="2"&&n.toFd==="1"?t.mergeStderrToStdout=!0:n.fd==="1"&&n.toFd==="2"&&(t.mergeStdoutToStderr=!0),r;let o=e[r+1],i=s+">"+(n.append?">":"");if(n.append)throw new Error(`filesystem is read-only \u2014 \`${i}\` append is not supported; use \`|\` to pipe or \`${s}>/dev/null\` to discard`);if(!o||o.kind!=="word")throw new Error(`redirect \`${i}\` requires a target`);if(o.value!=="/dev/null")throw new Error(`filesystem is read-only \u2014 use \`|\` to pipe between commands, or \`${i}/dev/null\` to discard`);return n.fd==="1"?t.stdoutToNull=!0:t.stderrToNull=!0,r+1}function Vt(t){let e=[],r="",n=null,s=!1,o=!1,i=()=>{s&&e.push({kind:"word",value:r,quoted:o}),r="",s=!1,o=!1};for(let u=0;u<t.length;u++){let c=t[u];if(n){c===n?n=null:r+=c;continue}if(c==="'"||c==='"'){n=c,s=!0,o=!0;continue}if(!s&&(c==="1"||c==="2")&&t[u+1]===">"){if(i(),t[u+2]==="&"){let a=t[u+3],l=t[u+4];if((a==="1"||a==="2")&&(l===void 0||/[\s|&>;()]/u.test(l))){e.push({kind:"redir",fd:c,toFd:a}),u+=3;continue}throw new Error(`redirect \`${c}>&\` requires fd 1 or 2 followed by a token boundary`)}t[u+2]===">"?(e.push({kind:"redir",fd:c,append:!0}),u+=2):(e.push({kind:"redir",fd:c,append:!1}),u+=1);continue}if(c==="|"){i(),t[u+1]==="|"?(e.push({kind:"or"}),u++):e.push({kind:"pipe"});continue}if(c==="&"){i(),t[u+1]==="&"?(e.push({kind:"and"}),u++):e.push({kind:"amp"});continue}if(c===">"){i(),t[u+1]===">"?(e.push({kind:"redir",fd:"1",append:!0}),u++):e.push({kind:"redir",fd:"1",append:!1});continue}if(c===";"){i(),e.push({kind:"semi"});continue}if(c==="("){i(),e.push({kind:"paren_open"});continue}if(c===")"){i(),e.push({kind:"paren_close"});continue}if(/\s/u.test(c)){i();continue}r+=c,s=!0}if(n)throw new Error(`unterminated ${n==="'"?"single":"double"} quote`);return i(),e}function m(t,e={}){let r=F(e.short),n=F(e.long),s=F(e.valueShort),o=F(e.valueLong),i=F(e.repeatable),u=e.stopAtFirstPositional??!1,c=new Set,a=new Map,l=[];for(let f=0;f<t.length;f++){let h=t[f];if(h==="--"){l.push(...t.slice(f+1));break}if(/^-+$/u.test(h)){l.push(h);continue}if(h.startsWith("--")&&h.length>2){let p=h.slice(2);if(o.has(p)||i.has(p))B(a,i,p,ut(t,++f,`--${p}`));else if(n.has(p))c.add(p);else throw new Error(`unknown option: --${p}`);continue}if(h.startsWith("-")&&h.length>1&&!/^-\d/u.test(h)){f=Zt(t,f,r,s,i,c,a);continue}if(u){l.push(...t.slice(f));break}l.push(h)}return{flags:c,values:a,positional:l}}function F(t){return t instanceof Set?t:new Set(t??[])}function ut(t,e,r){if(e>=t.length)throw new Error(`${r} requires an argument`);return t[e]}function B(t,e,r,n){if(!e.has(r)){t.set(r,n);return}let s=t.get(r);s?s.push(n):t.set(r,[n])}function Zt(t,e,r,n,s,o,i){let u=t[e].slice(1);for(let c=0;c<u.length;c++){let a=u[c];if(n.has(a)||s.has(a))return c+1<u.length?(B(i,s,a,u.slice(c+1)),e):(B(i,s,a,ut(t,++e,`-${a}`)),e);if(!r.has(a))throw new Error(`unknown option: -${a}`);o.add(a)}return e}function I(t){let e=t.startsWith("/"),r=t.split("/").filter(Boolean),n=[];for(let s of r)if(s!=="."){if(s===".."){n.length>0&&n.at(-1)!==".."?n.pop():e||n.push("..");continue}n.push(s)}return e?"/"+n.join("/"):n.length===0?".":n.join("/")}function x(t,e){return e.startsWith("/")?I(e):I(t+"/"+e)}function W(t){let e=I(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return r<0?".":r===0?"/":e.slice(0,r)}function D(t){let e=I(t);if(e==="/")return"/";let r=e.lastIndexOf("/");return e.slice(r+1)}function A(t,e){return t==="/"?"/"+e:t+"/"+e}function L(t,e){return t==="/"?e.slice(1):e.slice(t.length+1)}function ct(t){let e=new Map,r=t instanceof Map?t.entries():Object.entries(t??{});for(let[o,i]of r)typeof i=="string"&&e.set(I("/"+String(o)),i);let n=new Map([["/",{dirs:[],files:[]}]]);for(let o of e.keys()){let i=W(o);Kt(n,i),n.get(i).files.push(D(o))}for(let o of n.values())o.dirs.sort(),o.files.sort();let s={isFile:o=>e.has(o),isDir:o=>n.has(o),readFile:o=>e.get(o),listDir:o=>{let i=n.get(o);if(!i)throw new Error(`not a directory: ${o}`);return i},walkFiles:o=>Jt(s,o)};return s}function*G(t,e,r=Number.POSITIVE_INFINITY){if(t.isFile(e)){yield{path:e,kind:"file",depth:0};return}if(!t.isDir(e))return;yield{path:e,kind:"dir",depth:0};let n=[{path:e,depth:0}];for(let s=0;s<n.length;s++){let o=n[s];if(o.depth>=r)continue;let{dirs:i,files:u}=t.listDir(o.path),c=o.depth+1;for(let a of i){let l=A(o.path,a);yield{path:l,kind:"dir",depth:c},n.push({path:l,depth:c})}for(let a of u)yield{path:A(o.path,a),kind:"file",depth:c}}}function*Jt(t,e){for(let r of G(t,e))r.kind==="file"&&(yield r.path)}function Kt(t,e){let r=[],n=e;for(;n!=="/"&&!t.has(n);)r.push(n),n=W(n);for(let s=r.length-1;s>=0;s--){let o=r[s];t.set(o,{dirs:[],files:[]}),t.get(W(o)).dirs.push(D(o))}}var w=(t="")=>({stdout:t,stderr:"",exitCode:0}),d=(t,e=1)=>({stdout:"",stderr:t.endsWith(`
|
|
2
2
|
`)?t:t+`
|
|
3
|
-
`,exitCode:e}),k=t=>
|
|
4
|
-
`);return e.at(-1)===""&&e.pop(),e}var
|
|
3
|
+
`,exitCode:e}),k=t=>d(`usage: ${t}`,2);function v(t){if(t==="")return[];let e=t.split(`
|
|
4
|
+
`);return e.at(-1)===""&&e.pop(),e}var $=t=>t.length===0?"":t.join(`
|
|
5
5
|
`)+`
|
|
6
|
-
`;function
|
|
7
|
-
`,
|
|
8
|
-
`,
|
|
9
|
-
`:
|
|
6
|
+
`;function U(t,e,r){let n=[],s="",o=!1;for(let i of e){let u=x(r.cwd,i);if(r.fs.isDir(u)){s+=`${t}: ${i}: is a directory
|
|
7
|
+
`,o=!0;continue}if(!r.fs.isFile(u)){s+=`${t}: ${i}: no such file or directory
|
|
8
|
+
`,o=!0;continue}n.push({name:i,content:r.fs.readFile(u)})}return{inputs:n,stderr:s,failed:o}}function E(t,e,r,n){return e.length===0?{inputs:[{name:null,content:r}],stderr:"",failed:!1}:U(t,e,n)}function R(t,e,r,n){let s=E(t,e,r,n);return{content:s.inputs.map(o=>o.content).join(""),stderr:s.stderr,failed:s.failed}}var T=(t,e)=>({stdout:t,stderr:e.stderr,exitCode:e.failed?1:0});function _(t,e){if(typeof t!="string"||!/^\d+$/u.test(t))return{error:d(`${e}: invalid count: ${t}`)};let r=Number(t);return Number.isSafeInteger(r)?{value:r}:{error:d(`${e}: out of range: ${t}`)}}function Qt(t,e,r){let{positional:n}=m(e),s=E("tac",n,t,r),o=[];for(let{content:i}of s.inputs)o.push(...v(i).toReversed());return T($(o),s)}var at=1e6;function te(t,e){let{positional:r}=m(e);if(r.length===0||r.length>3)return k("seq LAST | seq FIRST LAST | seq FIRST INCR LAST");let n=[];for(let l of r){if(!/^-?\d+$/u.test(l))return d(`seq: invalid integer: ${l}`);n.push(Number(l))}let s,o,i;if(n.length===1?(s=1,o=1,i=n[0]):n.length===2?(s=n[0],i=n[1],o=s<=i?1:-1):[s,o,i]=n,o===0)return d("seq: increment must be non-zero");let c=(o>0?s<=i:s>=i)?Math.floor(Math.abs(i-s)/Math.abs(o))+1:0;if(c>at)return d(`seq: range too large: ${c} elements exceeds limit of ${at}`);let a=[];if(o>0)for(let l=s;l<=i;l+=o)a.push(String(l));else for(let l=s;l>=i;l+=o)a.push(String(l));return w($(a))}function ee(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["b"]}),o=n.get("b")??"t";if(o!=="a"&&o!=="t")return d(`nl: -b: only \`a\` and \`t\` are supported (got \`${o}\`)`);let i=E("nl",s,t,r),u=[],c=0;for(let{content:a}of i.inputs)for(let l of v(a))o==="a"||l!==""?(c++,u.push(`${String(c).padStart(6)} ${l}`)):u.push(l);return T($(u),i)}function ne(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["d","f","c"]}),o=n.has("f"),i=n.has("c");if(o===i)return k("cut -f LIST [-d DELIM] [file...] | cut -c LIST [file...]");if(i&&n.has("d"))return d("cut: -d is only valid with -f");let u=re(o?n.get("f"):n.get("c"));if(u.error)return u.error;let c=n.get("d")??" ";if(o&&c.length!==1)return d("cut: -d delimiter must be a single character");let a=E("cut",s,t,r),l=[];for(let{content:f}of a.inputs)for(let h of v(f))l.push(o?se(h,c,u.ranges):ft([...h],u.ranges).join(""));return T($(l),a)}function re(t){let e=[];for(let r of t.split(",")){if(r==="")return{error:d(`cut: empty list item in \`${t}\``)};if(/^\d+$/u.test(r)){let i=Number(r);if(i<1)return{error:d("cut: list items must be >= 1")};e.push([i,i]);continue}let n=r.match(/^(\d*)-(\d*)$/u);if(!n||n[1]===""&&n[2]==="")return{error:d(`cut: invalid list item: ${r}`)};let s=n[1]===""?1:Number(n[1]),o=n[2]===""?Number.POSITIVE_INFINITY:Number(n[2]);if(s<1)return{error:d("cut: list items must be >= 1")};if(o<s)return{error:d(`cut: reversed range: ${r}`)};e.push([s,o])}return{ranges:e}}function ft(t,e){let r=new Set,n=[];for(let[s,o]of e)for(let i=s;i<=Math.min(o,t.length);i++)r.has(i)||(r.add(i),n.push(i));return n.sort((s,o)=>s-o),n.map(s=>t[s-1])}function se(t,e,r){let n=t.split(e);return n.length===1?t:ft(n,r).join(e)}function oe(t,e){let{flags:r,positional:n}=m(e,{short:["d","s"]}),s=r.has("d"),o=r.has("s");if(s&&o)return d("tr: -d combined with -s is not supported");let i=s||o?1:2;if(n.length!==i)return k("tr SET1 SET2 | tr -d SET | tr -s SET");let u=lt(n[0]);if(u.error)return u.error;if(s){let f=new Set(u.chars);return w([...t].filter(h=>!f.has(h)).join(""))}if(o)return w(ie(t,new Set(u.chars)));let c=lt(n[1]);if(c.error)return c.error;if(c.chars.length===0)return d("tr: SET2 must not be empty");let a=new Map,l=c.chars.at(-1);for(let f=0;f<u.chars.length;f++)a.set(u.chars[f],c.chars[f]??l);return w([...t].map(f=>a.get(f)??f).join(""))}function lt(t){let e=[...t],r=[],n=0,s=()=>{if(e[n]!=="\\")return e[n++];if(n+1>=e.length)return null;let o=e[n+1];return n+=2,o==="n"?`
|
|
9
|
+
`:o==="t"?" ":o==="\\"?"\\":o==="0"?"\0":o};for(;n<e.length;){let o=s();if(o===null)return{error:d("tr: trailing backslash in set")};if(e[n]==="-"&&n+1<e.length){n++;let i=s();if(i===null)return{error:d("tr: trailing backslash in set")};let u=o.codePointAt(0),c=i.codePointAt(0);if(c<u)return{error:d(`tr: reversed range: ${o}-${i}`)};for(let a=u;a<=c;a++)r.push(String.fromCodePoint(a))}else r.push(o)}return{chars:r}}function ie(t,e){let r="",n=null;for(let s of t)e.has(s)&&s===n||(r+=s,n=s);return r}function ue(t,e,r){let{positional:n}=m(e);if(n.length===0)return k("which COMMAND...");let s=[],o=0;for(let i of n)r.hasCommand(i)?s.push(`/usr/bin/${i}`):(s.push(`${i} not found`),o=1);return{stdout:$(s),stderr:"",exitCode:o}}function ce(t,e,r){let{positional:n}=m(e);return n.length>0?d(`whoami: extra operand: ${n[0]}`):w((r.user??"user")+`
|
|
10
|
+
`)}function ae(t,e){let{flags:r,positional:n}=m(e,{short:["u"]});if(n.length>1)return d(`date: at most one +FORMAT argument is supported (got: ${n[1]})`);let s="%a %b %e %T %Z %Y";if(n.length===1){if(!n[0].startsWith("+"))return k("date [-u] [+FORMAT]");s=n[0].slice(1)}return w(pe(new Date,s,r.has("u"))+`
|
|
11
|
+
`)}var le=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],fe=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],he=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],de=["January","February","March","April","May","June","July","August","September","October","November","December"];function pe(t,e,r){let n=r?{Y:t.getUTCFullYear(),M:t.getUTCMonth(),D:t.getUTCDate(),h:t.getUTCHours(),m:t.getUTCMinutes(),s:t.getUTCSeconds(),w:t.getUTCDay()}:{Y:t.getFullYear(),M:t.getMonth(),D:t.getDate(),h:t.getHours(),m:t.getMinutes(),s:t.getSeconds(),w:t.getDay()},s=(o,i="0")=>String(o).padStart(2,i);return e.replace(/%./gu,o=>{switch(o[1]){case"Y":return String(n.Y);case"m":return s(n.M+1);case"d":return s(n.D);case"e":return s(n.D," ");case"H":return s(n.h);case"M":return s(n.m);case"S":return s(n.s);case"T":return`${s(n.h)}:${s(n.m)}:${s(n.s)}`;case"F":return`${n.Y}-${s(n.M+1)}-${s(n.D)}`;case"s":return String(Math.floor(t.getTime()/1e3));case"a":return le[n.w];case"A":return fe[n.w];case"b":return he[n.M];case"B":return de[n.M];case"Z":return me(t,r);case"z":return ge(t,r);case"n":return`
|
|
12
|
+
`;case"t":return" ";case"%":return"%";default:return o}})}function me(t,e){if(e)return"UTC";try{return new Intl.DateTimeFormat("en-US",{timeZoneName:"short"}).formatToParts(t).find(s=>s.type==="timeZoneName")?.value??"Local"}catch{return"Local"}}function ge(t,e){if(e)return"+0000";let r=-t.getTimezoneOffset(),n=r>=0?"+":"-",s=Math.abs(r);return`${n}${String(Math.floor(s/60)).padStart(2,"0")}${String(s%60).padStart(2,"0")}`}var ht={cut:ne,tac:Qt,tr:oe,seq:te,nl:ee,which:ue},dt={whoami:ce,date:ae};var mt=/[*?]/u,pt=/[.+*?^${}()|[\]\\]/u;function Y(t){let e="^";for(let r=0;r<t.length;r++){let n=t[r];if(n==="\\"&&r+1<t.length){let s=t[r+1];e+=pt.test(s)?"\\"+s:s,r++}else n==="*"?e+=".*":n==="?"?e+=".":pt.test(n)?e+="\\"+n:e+=n}return new RegExp(e+"$","u")}function gt(t,e,r){if(t.length===0)return[];let n=[t[0]];for(let s=1;s<t.length;s++){let o=t[s];if(e.has(s)||!mt.test(o)){n.push(o);continue}let i=we(o,r);i.length>0?n.push(...i):n.push(o)}return n}function we(t,e){let r=t.startsWith("/"),n=t.startsWith("./"),s=t.endsWith("/")&&t.length>1,o=t.split("/").filter(Boolean);if(o.length===0)return r?["/"]:[];let i=[r?"/":"."];for(let u=0;u<o.length;u++){let c=o[u];if(!mt.test(c)){i=i.map(a=>z(a,c));continue}i=be(i,c,u===o.length-1,e)}return s?i=i.filter(u=>e.fs.isDir(x(e.cwd,u))).map(u=>u==="/"?u:u+"/"):i=i.filter(u=>xe(u,e)),n&&(i=i.map(u=>u.startsWith("./")?u:"./"+u)),i.sort(),i}function xe(t,e){let r=x(e.cwd,t);return e.fs.isFile(r)||e.fs.isDir(r)}function be(t,e,r,n){let s=Y(e),o=e.startsWith("."),i=c=>!o&&c.startsWith(".")?!1:s.test(c),u=[];for(let c of t){let a=x(n.cwd,c);if(!n.fs.isDir(a))continue;let{dirs:l,files:f}=n.fs.listDir(a);for(let h of l)i(h)&&u.push(z(c,h));if(r)for(let h of f)i(h)&&u.push(z(c,h))}return u}function z(t,e){return t==="."?e:A(t,e)}var wt=new Set(["name","type","path","mindepth","maxdepth"]);function xt(t,e,r){let n=Se(e);if(n.error)return n.error;let{starts:s,minDepth:o,maxDepth:i,groups:u,hasExec:c,batches:a}=n,l=[],f="",h="",p=0;for(let g of s){let y=x(r.cwd,g);if(!r.fs.isDir(y)&&!r.fs.isFile(y)){h+=`find: ${g}: no such file or directory
|
|
13
|
+
`,p=1;continue}for(let S of G(r.fs,y,i)){if(S.depth<o)continue;let C=Ae(g,y,S.path),N=Ce(u,{kind:S.kind,path:C},r);f+=N.stdout,h+=N.stderr,N.matched&&!c&&l.push(C)}}for(let g of a){if(g.collected.length===0)continue;let y=g.args.slice(0,-1).concat(g.collected),S=r.dispatch(g.cmd,y,"");f+=S.stdout,h+=S.stderr,S.exitCode!==0&&(p=1)}return{stdout:(l.length===0?"":l.join(`
|
|
10
14
|
`)+`
|
|
11
|
-
`)}function
|
|
12
|
-
`)}function
|
|
15
|
+
`)+f,stderr:h,exitCode:p}}function Se(t){let e=ye(t);if(e.error)return e;let r=ve(e.tokens,e.minDepth,e.maxDepth);if(r.error)return r;let n=[];for(let s of r.groups)for(let o of s)o.kind==="exec"&&n.push(o);return{...r,hasExec:n.length>0,batches:n.filter(s=>s.mode==="batch")}}function ye(t){let e=[],r=0,n=Number.POSITIVE_INFINITY;for(let s=0;s<t.length;s++){let o=t[s],i=o==="-mindepth"||o==="--mindepth"?"mindepth":o==="-maxdepth"||o==="--maxdepth"?"maxdepth":null;if(i){if(s+1>=t.length)return{error:d(`find: -${i} requires a value`)};let u=_(t[s+1],`find: -${i}`);if(u.error)return u;i==="mindepth"?r=u.value:n=u.value,s++;continue}if(o==="--"){e.push(...t.slice(s));break}e.push(o)}return{tokens:e,minDepth:r,maxDepth:n}}function ve(t,e,r){let n=[[]],s=[],o=!1,i=!1,u=!1,c=null;for(let a=0;a<t.length;a++){let l=t[a];if(i){s.push(l);continue}if(l==="-not"||l==="!"){if(o)return{error:d("find: `-not` cannot precede another `-not`")};o=!0,u=!0;continue}if(l==="-a"||l==="-and"||l==="--and"){if(o)return{error:d("find: `-not` must be followed by a primary")};if(c)return{error:d(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:d("find: `-a` with no left-hand expression")};c="-a",u=!0;continue}if(l==="-o"||l==="-or"||l==="--or"){if(o)return{error:d("find: `-not` must be followed by a primary")};if(c)return{error:d(`find: \`${c}\` with no right-hand expression`)};if(n.at(-1).length===0)return{error:d("find: `-o` with no left-hand expression")};n.push([]),c="-o",u=!0;continue}if(l==="-exec"||l==="--exec"){let h=ke(t,a,o);if(h.error)return h;n.at(-1).push(h.pred),o=!1,c=null,u=!0,a=h.nextI;continue}let f=$e(l);if(f!==null){if(a+1>=t.length)return{error:d(`find: ${l} requires a value`)};let h=t[a+1],p=Te(f,h);if(p.error)return p;let b={kind:f,value:h,negate:o};(f==="name"||f==="path")&&(b.re=Y(h)),n.at(-1).push(b),o=!1,c=null,u=!0,a++;continue}if(l==="--"){i=!0;continue}if(o)return{error:d(`find: \`-not\` must be followed by a primary, got: ${l}`)};if(l.startsWith("-")&&l!=="-"&&!/^-\d/u.test(l))return{error:d(`find: unknown option: ${l}`)};if(u)return{error:d(`find: paths must precede expression: ${l}`)};s.push(l)}return o?{error:d("find: trailing `-not` with no primary")}:c?{error:d(`find: \`${c}\` with no right-hand expression`)}:{starts:s.length>0?s:["."],minDepth:e,maxDepth:r,groups:n}}function $e(t){return/^--?(?:min|max)depth$/u.test(t)?null:t.startsWith("--")&&wt.has(t.slice(2))?t.slice(2):t.startsWith("-")&&wt.has(t.slice(1))?t.slice(1):null}function Te(t,e){return t==="type"&&e!=="f"&&e!=="d"?{error:d(`find: -type/--type expects 'f' or 'd', got: ${e}`)}:{}}function ke(t,e,r){let n=e+1;for(;n<t.length&&t[n]!==";"&&t[n]!=="+";)n++;if(n>=t.length)return{error:d("find: -exec: missing terminator (`;` or `+`); use `';'` (quoted) \u2014 bare `\\;` is consumed by the shell as a step separator")};if(n===e+1)return{error:d("find: -exec: requires a command")};let s=t.slice(e+1,n),o=t[n]===";"?"each":"batch";if(o==="batch"){if(s.at(-1)!=="{}")return{error:d("find: -exec ... +: `{}` must be the last argument before `+`")};if(s.slice(0,-1).includes("{}"))return{error:d("find: -exec ... +: only one instance of `{}` is supported")};if(r)return{error:d("find: `-not -exec ... +` is not supported (the `+` form has no meaningful negation)")}}let i={kind:"exec",mode:o,cmd:s[0],args:s.slice(1),negate:r};return o==="batch"&&(i.collected=[]),{pred:i,nextI:n}}function Ce(t,e,r){let n="",s="",o=!1;for(let i of t){let u=!0;for(let c of i){let a=Ee(c,e,r);if(n+=a.stdout,s+=a.stderr,!a.matched){u=!1;break}}if(u){o=!0;break}}return{matched:o,stdout:n,stderr:s}}function Ee(t,e,r){let n=_e(t,e,r);return{...n,matched:t.negate?!n.matched:n.matched}}function _e(t,e,r){return t.kind==="type"?O(t.value==="f"?e.kind==="file":e.kind==="dir"):t.kind==="name"?O(t.re.test(D(e.path))):t.kind==="path"?O(t.re.test(e.path)):Me(t,e,r)}function O(t){return{matched:t,stdout:"",stderr:""}}function Me(t,e,r){if(t.mode==="batch")return t.collected.push(e.path),O(!0);let n=t.args.map(o=>o.replaceAll("{}",e.path)),s=r.dispatch(t.cmd,n,"");return{matched:s.exitCode===0,stdout:s.stdout,stderr:s.stderr}}function Ae(t,e,r){if(r===e)return t;let n=L(e,r);return t.endsWith("/")?t+n:t+"/"+n}function De(t,e,r){return m(e),w(r.cwd+`
|
|
16
|
+
`)}function Ne(t,e,r){let{positional:n}=m(e),s=n[0]??"/",o=x(r.cwd,s);return r.fs.isDir(o)?(r.cwd=o,w()):d(`cd: not a directory: ${s}`)}function Fe(t,e,r){let{flags:n,positional:s}=m(Le(e),{short:["l","a","R"]}),o={long:n.has("l"),all:n.has("a"),recursive:n.has("R")},i=s.length>0?s:["."],u=o.recursive?Ie(i,o.all,r):i,c=[],a=[],l=0;for(let f=0;f<u.length;f++){let h=Re(u[f],u.length>1,o,r);if(h.error){l=1,a.push(h.error);continue}f>0&&u.length>1&&c.push(""),c.push(...h.lines)}return{stdout:c.length===0?"":c.join(`
|
|
13
17
|
`)+`
|
|
14
|
-
`,stderr:
|
|
18
|
+
`,stderr:a.length===0?"":a.join(`
|
|
15
19
|
`)+`
|
|
16
|
-
`,exitCode:l}}function
|
|
20
|
+
`,exitCode:l}}function Ie(t,e,r){let n=[];for(let s of t){let o=x(r.cwd,s);if(!r.fs.isDir(o)){n.push(s);continue}let i=[{display:s,abs:o}];for(;i.length>0;){let{display:u,abs:c}=i.pop();n.push(u);let{dirs:a}=r.fs.listDir(c);for(let l=a.length-1;l>=0;l--){let f=a[l];!e&&f.startsWith(".")||i.push({display:We(u,f),abs:A(c,f)})}}}return n}function We(t,e){return t.endsWith("/")?t+e:t+"/"+e}function Le(t){let e=[],r=!1;for(let n of t){if(r){e.push(n);continue}if(n==="--"){e.push(n),r=!0;continue}if(n!=="-1"){if(/^-[a-zA-Z0-9]+$/u.test(n)&&n.includes("1")&&/[a-zA-Z]/u.test(n)){e.push("-"+n.slice(1).replaceAll("1",""));continue}e.push(n)}}return e}function Re(t,e,r,n){let s=x(n.cwd,t);if(n.fs.isFile(s))return{lines:[X(t,n.fs.readFile(s).length,!1,r.long)]};if(!n.fs.isDir(s))return{error:`ls: ${t}: no such file or directory`};let o=e||r.recursive?[`${t}:`]:[];r.all&&(o.push(bt(".",r.long)),o.push(bt("..",r.long)));let{dirs:i,files:u}=n.fs.listDir(s);for(let c of i)!r.all&&c.startsWith(".")||o.push(X(c,0,!0,r.long));for(let c of u){if(!r.all&&c.startsWith("."))continue;let a=A(s,c);o.push(X(c,n.fs.readFile(a).length,!1,r.long))}return{lines:o}}function bt(t,e){return e?`d ${String(0).padStart(8)} ${t}`:t}function X(t,e,r,n){let s=t+(r?"/":"");return n?`${r?"d":"-"} ${String(e).padStart(8)} ${s}`:s}function Oe(t,e,r){let{positional:n}=m(e),s=n[0]??".",o=x(r.cwd,s);if(!r.fs.isDir(o))return d(`tree: ${s}: not a directory`);let i=[s];return je(r.fs,o,i),w(i.join(`
|
|
17
21
|
`)+`
|
|
18
|
-
`)}function
|
|
19
|
-
`)}function
|
|
20
|
-
`)}var
|
|
22
|
+
`)}function je(t,e,r){let n=[{dir:e,prefix:"",items:St(t,e),i:0}];for(;n.length>0;){let s=n.at(-1);if(s.i>=s.items.length){n.pop();continue}let{n:o,isDir:i}=s.items[s.i],u=s.i===s.items.length-1;if(r.push(s.prefix+(u?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ")+o+(i?"/":"")),s.i++,!i)continue;let c=A(s.dir,o);n.push({dir:c,prefix:s.prefix+(u?" ":"\u2502 "),items:St(t,c),i:0})}}function St(t,e){let{dirs:r,files:n}=t.listDir(e);return[...r.map(s=>({n:s,isDir:!0})),...n.map(s=>({n:s,isDir:!1}))]}function qe(t,e){let{positional:r}=m(e);return r.length===0?k("basename PATH"):w(D(r[0])+`
|
|
23
|
+
`)}function Pe(t,e){let{positional:r}=m(e);return r.length===0?k("dirname PATH"):w(W(r[0])+`
|
|
24
|
+
`)}var yt={pwd:De,cd:Ne,ls:Fe,find:xt,tree:Oe,basename:qe,dirname:Pe};var He=/^(\d+)(?:,(\d+))?p$/u;function vt(t,e,r){let n;try{n=m(e,{short:["n"]})}catch{return j()}let{flags:s,positional:o}=n;if(!s.has("n")||o.length===0)return j();let i=Be(o[0]);if(i.error)return i.error;let{ranges:u}=i;if(u.length===0)return j();let c=o.slice(1),a=E("sed",c,t,r),l=a.inputs.flatMap(h=>v(h.content)),f=[];for(let h=0;h<l.length;h++){let p=h+1;for(let{start:b,end:g}of u)p>=b&&p<=g&&f.push(l[h])}return T(f.length>0?f.join(`
|
|
21
25
|
`)+`
|
|
22
|
-
`:"",
|
|
23
|
-
or: grep ${
|
|
24
|
-
`,
|
|
25
|
-
`))}let
|
|
26
|
-
`);return s?
|
|
27
|
-
`:"")):V()}function
|
|
26
|
+
`:"",a)}function Be(t){let e=[];for(let r of t.split(";")){if(r==="")continue;let n=He.exec(r);if(!n)return{error:j()};let s=Number(n[1]),o=n[2]===void 0?s:Number(n[2]);if(s<1)return{error:d("sed: line numbers must be >= 1")};if(o<s)return{error:d(`sed: reversed range: ${s},${o}`)};e.push({start:s,end:o})}return{ranges:e}}function j(){return d("sed: only `-n 'X[,Y]p'` (optionally `;`-joined into multi-range scripts) is supported")}function $t(t,e){let r=t[e+1];if(r===void 0)return-1;if("^$\\.*+?()[]{}|/bBdDsSwW".includes(r)||r>="1"&&r<="9")return 2;let n=o=>o!==void 0&&/[0-9A-Fa-f]/u.test(o),s=(o,i)=>{if(t[e+2]!==o)return 0;let u=t.indexOf(i,e+3);return u>e+3?u-e+1:0};if(r==="x")return n(t[e+2])&&n(t[e+3])?4:0;if(r==="u"){if(t[e+2]==="{"){let o=s("{","}"),i=o?t.slice(e+3,e+o-1):"";return i.length>=1&&i.length<=6&&[...i].every(n)&&parseInt(i,16)<=1114111?o:0}return[2,3,4,5].every(o=>n(t[e+o]))?6:0}return r==="p"||r==="P"?s("{","}"):r==="c"?/[A-Za-z]/u.test(t[e+2]??"")?3:0:r==="k"?s("<",">"):0}function kt(t){let e="(){}+?|",r="",n=!1;for(let s=0;s<t.length;s++){let o=t[s];if(n){if(o==="\\"){let i=$t(t,s);if(i===-1)return{error:"trailing backslash (\\)"};if(i>0){r+=t.slice(s,s+i),s+=i-1;continue}r+=t[s+1],s++;continue}r+=o,o==="]"&&(n=!1);continue}if(o==="["){r+=o,n=!0,t[s+1]==="^"&&(r+="^",s++),t[s+1]==="]"&&(r+="\\]",s++);continue}if(o==="\\"){if(s+1>=t.length)return{error:"trailing backslash (\\)"};let i=t[s+1];if(e.includes(i)){r+=i,s++;continue}if(i==="<"||i===">"){r+="\\b",s++;continue}let u=$t(t,s);if(u>0){r+=t.slice(s,s+u),s+=u-1;continue}r+=i,s++;continue}if(o==="*"&&(s===0||t[s-1]==="^"&&Tt(t,s-1))){r+="\\*";continue}if(o==="^"&&!Tt(t,s)){r+="\\^";continue}if(o==="$"&&!(s===t.length-1||t[s+1]==="\\"&&(t[s+2]===")"||t[s+2]==="|"))){r+="\\$";continue}if(e.includes(o)){r+="\\"+o;continue}r+=o}return{source:r}}function Tt(t,e){return e===0?!0:e>=2&&t[e-2]==="\\"&&(t[e-1]==="("||t[e-1]==="|")}var Ct="[-i] [-v] [-n] [-r|-R] [-w] [-o] [-E|-F|-G] [-l] [-L] [-c] [-h] [-H] [-A N] [-B N] [-C N]",Ge=`grep ${Ct} PATTERN [PATH...]
|
|
27
|
+
or: grep ${Ct} -e PATTERN ... [PATH...]`,Ue=["i","v","n","r","R","l","L","c","w","h","H","o","E","F","G"],ze=["A","B","C"];function _t(t,e,r){let n;try{n=m(e,{short:Ue,valueShort:ze,repeatable:["e"]})}catch(N){return d(`grep: ${N.message}`,2)}let{flags:s,values:o,positional:i}=n,u=o.get("e")??[],c,a;if(u.length>0)c=u,a=i;else if(i.length>0)c=[i[0]],a=i.slice(1);else return k(Ge);let l=Ye(s);if(l)return l;let f=Xe(c,s);if(f.error)return f.error;let h=Ve(o);if(h.error)return h.error;let p=s.has("r")||s.has("R"),b=Je(p,t,a,r),g=Ze(s,p,a.length),y=s.has("v"),S={showName:g,invert:y,showLine:s.has("n"),only:s.has("o"),after:h.after,before:h.before},C=s.has("l")?Et(b.inputs,f.res,y,!1):s.has("L")?Et(b.inputs,f.res,y,!0):s.has("c")?rn(b.inputs,f.res,y,g):tn(b.inputs,f.res,S);return b.failed?{stdout:C.stdout,stderr:b.stderr+C.stderr,exitCode:2}:C}function Ye(t){if(t.has("h")&&t.has("H"))return d("grep: -h and -H are mutually exclusive");let e=["l","L","c"].filter(n=>t.has(n));if(e.length>1)return d(`grep: ${e.map(n=>`-${n}`).join(" / ")} are mutually exclusive`);let r=["E","F","G"].filter(n=>t.has(n));return r.length>1?d(`grep: ${r.map(n=>`-${n}`).join(" / ")} are mutually exclusive`):null}function Xe(t,e){let r=[],n=e.has("i")?"iu":"u";for(let s of t){let o;if(e.has("F"))o=RegExp.escape(s);else if(e.has("E"))o=s;else{let i=kt(s);if(i.error)return{error:d(`grep: ${i.error}`,2)};o=i.source}e.has("w")&&(o=`\\b(?:${o})\\b`);try{r.push(new RegExp(o,n))}catch(i){let u=e.has("F")?`fixed-string /${n}`:e.has("E")?`ERE / ECMAScript /${n}`:`BRE /${n}`;return{error:d(`grep: invalid pattern (${u}): ${i.message}`,2)}}}return{res:r}}function q(t,e){return t.some(r=>r.test(e))}var V=()=>({stdout:"",stderr:"",exitCode:1});function Ve(t){let e=t.get("C"),r=e===void 0?null:_(e,"grep: -C");if(r?.error)return r;let n=_(t.get("A")??e??"0","grep: -A");if(n.error)return n;let s=_(t.get("B")??e??"0","grep: -B");return s.error?s:{after:n.value,before:s.value}}function Ze(t,e,r){return t.has("h")?!1:t.has("H")?!0:e||r>1}function Je(t,e,r,n){return t?Ke("grep",r.length>0?r:["."],n):r.length>0?U("grep",r,n):{inputs:[{name:null,content:e}],stderr:"",failed:!1}}function Ke(t,e,r){let n=[],s="",o=!1;for(let i of e){let u=x(r.cwd,i);if(r.fs.isFile(u)){n.push({name:i,content:r.fs.readFile(u)});continue}if(!r.fs.isDir(u)){s+=`${t}: ${i}: no such file or directory
|
|
28
|
+
`,o=!0;continue}for(let c of r.fs.walkFiles(u))n.push({name:Qe(i,u,c),content:r.fs.readFile(c)})}return{inputs:n,stderr:s,failed:o}}function Qe(t,e,r){let n=L(e,r);return t==="."?n:t.endsWith("/")?t+n:t+"/"+n}function tn(t,e,r){let n=[],s=!1;for(let{name:i,content:u}of t){let c=v(u),a=en(c,e,i,r);a.matched&&(s=!0),a.lines.length>0&&n.push(a.lines.join(`
|
|
29
|
+
`))}let o=n.join(`
|
|
30
|
+
`);return s?w(o+(o?`
|
|
31
|
+
`:"")):V()}function en(t,e,r,n){let{invert:s,after:o,before:i,only:u}=n,c=i>0||o>0,a=[],l=!1,f=-1;for(let h=0;h<t.length;h++){if(q(e,t[h])===s)continue;if(l=!0,u){a.push(...nn(t[h],r,h+1,e,n));continue}let p=Math.max(0,h-i),b=Math.min(t.length-1,h+o);c&&f>=0&&p>f+1&&a.push("--");for(let g=Math.max(p,f+1);g<=b;g++){let y=g===h||q(e,t[g])!==s;a.push(Mt(t[g],r,g+1,y,n))}f=b}return{lines:a,matched:l}}function nn(t,e,r,n,s){let o=[];for(let c of n){let a=new RegExp(c.source,c.flags+"g");for(let l of t.matchAll(a))l[0].length>0&&o.push({index:l.index,text:l[0]})}o.sort((c,a)=>c.index-a.index||a.text.length-c.text.length);let i=[],u=0;for(let c of o)c.index<u||(i.push(Mt(c.text,e,r,!0,s)),u=c.index+c.text.length);return i}function Mt(t,e,r,n,s){let{showName:o,showLine:i}=s,u=n?":":"-",c=[];return o&&c.push(e??"(standard input)"),i&&c.push(String(r)),c.length>0?c.join(u)+u+t:t}function Et(t,e,r,n){let s=[];for(let{name:o,content:i}of t){let c=v(i).some(a=>q(e,a)!==r);(n?!c:c)&&s.push(o??"(standard input)")}return s.length>0?w($(s)):V()}function rn(t,e,r,n){let s=[],o=!1;for(let{name:i,content:u}of t){let c=v(u),a=0;for(let l of c)q(e,l)!==r&&a++;a>0&&(o=!0),n?s.push(`${i??"(standard input)"}:${a}`):s.push(String(a))}return s.length===0?V():{stdout:$(s),stderr:"",exitCode:o?0:1}}function sn(t,e,r){let{flags:n,positional:s}=m(e,{short:["n"]}),o=R("cat",s,t,r);return T(n.has("n")?on(o.content):o.content,o)}function on(t){if(t==="")return"";let e=t.endsWith(`
|
|
28
32
|
`)?`
|
|
29
33
|
`:"";return(e?t.slice(0,-1).split(`
|
|
30
34
|
`):t.split(`
|
|
31
35
|
`)).map((n,s)=>`${String(s+1).padStart(6)} ${n}`).join(`
|
|
32
|
-
`)+e}function
|
|
33
|
-
`);
|
|
34
|
-
`:""}==> ${
|
|
35
|
-
${
|
|
36
|
-
`:""}`):
|
|
37
|
-
`)}return
|
|
38
|
-
`:s)}function
|
|
39
|
-
`;function
|
|
36
|
+
`)+e}function un(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["n"]});Nt(n,s);let o=_(n.get("n")??"10","head: -n");return o.error?o.error:Ft("head",t,s,r,i=>i.slice(0,o.value))}function cn(t,e,r){let{values:n,positional:s}=m(e,{valueShort:["n"]});Nt(n,s);let o=_(n.get("n")??"10","tail: -n");return o.error?o.error:Ft("tail",t,s,r,i=>o.value===0?[]:i.slice(-o.value))}function Nt(t,e){t.has("n")||e[0]&&/^-\d+$/u.test(e[0])&&t.set("n",e.shift().slice(1))}function Ft(t,e,r,n,s){let o=E(t,r,e,n),i=o.inputs.length>1,u=[];for(let c=0;c<o.inputs.length;c++){let{name:a,content:l}=o.inputs[c],f=s(v(l)).join(`
|
|
37
|
+
`);i?u.push(`${c>0?`
|
|
38
|
+
`:""}==> ${a} <==
|
|
39
|
+
${f}${f?`
|
|
40
|
+
`:""}`):f.length>0&&u.push(f+`
|
|
41
|
+
`)}return T(u.join(""),o)}function an(t,e,r){let{flags:n,positional:s}=m(e,{short:["l","w","c"]}),o=fn(n),i=E("wc",s,t,r),u=[],c={l:0,w:0,c:0};for(let{name:l,content:f}of i.inputs){let h=dn(f);u.push({counts:h,name:l}),c.l+=h.l,c.w+=h.w,c.c+=h.c}i.inputs.length>1&&u.push({counts:c,name:"total"});let a=ln(u,o);return T($(u.map(l=>pn(l.counts,l.name,o,a))),i)}function ln(t,e){let r=0;for(let{counts:n}of t)e.l&&(r=Math.max(r,String(n.l).length)),e.w&&(r=Math.max(r,String(n.w).length)),e.c&&(r=Math.max(r,String(n.c).length));return r}function fn(t){return t.has("l")||t.has("w")||t.has("c")?{l:t.has("l"),w:t.has("w"),c:t.has("c")}:{l:!0,w:!0,c:!0}}var hn=new TextEncoder;function dn(t){return{l:(t.match(/\n/gu)??[]).length,w:(t.match(/\S+/gu)??[]).length,c:hn.encode(t).length}}function pn(t,e,r,n){let s=[];return r.l&&s.push(String(t.l).padStart(n)),r.w&&s.push(String(t.w).padStart(n)),r.c&&s.push(String(t.c).padStart(n)),s.join(" ")+(e?" "+e:"")}function mn(t,e,r){let{flags:n,positional:s}=m(e,{short:["n","r","u"]}),o=R("sort",s,t,r),i=v(o.content),u=n.has("n"),c=n.has("u");if(u){let a=i.map(l=>({line:l,key:At(l)}));a.sort(c?(l,f)=>l.key-f.key:(l,f)=>l.key-f.key||(l.line<f.line?-1:l.line>f.line?1:0)),i=a.map(l=>l.line)}else i.sort();if(c){let a=new Set;i=i.filter(l=>{let f=u?At(l):l;return a.has(f)?!1:(a.add(f),!0)})}return n.has("r")&&i.reverse(),T($(i),o)}function At(t){let e=/^[ \t]*(-?(?:\d+\.?\d*|\.\d+))/u.exec(t);return e?Number(e[1]):0}function gn(t,e,r){let{flags:n,positional:s}=m(e,{short:["c","d","u","i"]}),o=R("uniq",s,t,r),i=n.has("c"),u=n.has("d"),c=n.has("u"),a=n.has("i"),l=S=>a?S.toLowerCase():S,f=v(o.content),h=[],p=null,b=null,g=0,y=()=>{if(p===null)return;let S=g>=2;!(u&&c)&&(u?S:!c||!S)&&h.push(i?`${String(g).padStart(7)} ${p}`:p)};for(let S of f){let C=l(S);if(C===b){g++;continue}y(),p=S,b=C,g=1}return y(),T($(h),o)}function wn(t,e){let{flags:r,positional:n}=m(e,{short:["n","e","E"]}),s=n.join(" "),o=!r.has("n");if(r.has("e")){let i=xn(s);s=i.text,i.stop&&(o=!1)}return w(o?s+`
|
|
42
|
+
`:s)}function xn(t){let e={a:7,b:8,e:27,f:12,n:10,r:13,t:9,v:11,"\\":92},r="";for(let n=0;n<t.length;n++){if(t[n]!=="\\"||n+1>=t.length){r+=t[n];continue}let s=t[++n];if(s==="c")return{text:r,stop:!0};if(s in e){r+=String.fromCodePoint(e[s]);continue}if(s==="0"){let o="";for(;o.length<3&&/[0-7]/u.test(t[n+1]??"");)o+=t[++n];r+=String.fromCodePoint(o===""?0:parseInt(o,8));continue}if(s==="x"&&/[0-9a-fA-F]/u.test(t[n+1]??"")){let o="";for(;o.length<2&&/[0-9a-fA-F]/u.test(t[n+1]??"");)o+=t[++n];r+=String.fromCodePoint(parseInt(o,16));continue}r+="\\"+s}return{text:r,stop:!1}}function bn(t,e,r){let{flags:n,values:s,positional:o}=m(e,{short:["r"],valueShort:["n"],stopAtFirstPositional:!0}),[i="echo",...u]=o,c=t.split(/\s+/u).filter(Boolean);if(c.length===0)return n.has("r")?w():r.dispatch(i,u,"");let a=s.has("n")?_(s.get("n"),"xargs: -n"):{value:c.length};return a.error?a.error:s.has("n")&&a.value===0?d("xargs: -n: must be at least 1"):Sn(r,i,u,c,a.value)}function Sn(t,e,r,n,s){let o="",i="",u=0;for(let c=0;c<n.length;c+=s){let a=t.dispatch(e,[...r,...n.slice(c,c+s)],"");o+=a.stdout,i+=a.stderr,a.exitCode!==0&&(u=a.exitCode)}return{stdout:o,stderr:i,exitCode:u}}function Dt(){return w()}function yn(){return{stdout:"",stderr:"",exitCode:1}}var It={cat:sn,grep:_t,head:un,tail:cn,wc:an,sort:mn,uniq:gn,echo:wn,xargs:bn},Wt={true:Dt,false:yn,":":Dt};function Lt(t,e,r){let{index:n,pipe:s}=$n(t),o=t.slice(n),i=Tn(o),u=o.slice(i),c=o.slice(0,i).trim(),a=c==="",l=a?"":r.resolveCommand(c.split(/\s+/u)[0]),f=t.slice(0,n+i),h=s&&f.endsWith("|")?" ":"";return vn(u,a,s,l,e,r).map(p=>f+h+p)}function vn(t,e,r,n,s,o){if(!e)return r?[]:kn(t,s,n==="cd");let i=r?o.pipeNames:o.names;for(let u of o.binPrefixes)if(t.startsWith(u)){let c=t.slice(u.length);return i.filter(a=>a.startsWith(c)).map(a=>u+a)}return t.startsWith("/")||t.startsWith("./")?[]:i.filter(u=>u.startsWith(t))}function $n(t){let e=0,r=!1,n=0;for(;n<t.length;){let s=t[n];if(s==="|"){let o=t[n+1]==="|";n+=o?2:1,e=n,r=!o}else s==="&"&&t[n+1]==="&"?(n+=2,e=n,r=!1):s===";"?(n++,e=n,r=!1):n++}return{index:e,pipe:r}}function Tn(t){for(let e=t.length-1;e>=0;e--)if(/\s/u.test(t[e]))return e+1;return 0}function kn(t,e,r=!1){let n=t.lastIndexOf("/"),s=t.slice(0,n+1),o=t.slice(n+1),i=x(e.cwd,s);if(!e.fs.isDir(i))return[];let{dirs:u,files:c}=e.fs.listDir(i),a=o.startsWith("."),l=[];for(let f of u)!a&&f.startsWith(".")||f.startsWith(o)&&l.push(s+f+"/");if(r)return l;for(let f of c)!a&&f.startsWith(".")||f.startsWith(o)&&l.push(s+f);return l}var P={__proto__:null,...It,...yt,...ht},Z={__proto__:null,sed:vt,...dt,...Wt},Cn=["ls","cd","cat","grep","find","head","tail","wc","tree","sort","uniq","cut","tr","nl","tac","xargs","echo","pwd","seq","which","basename","dirname"],Rt=Mn(),En=Rt.join(", "),_n=["grep","head","tail","wc","sort","uniq","cut","xargs","tr","nl","tac","cat"];function Mn(){let t=new Set(Object.keys(P)),e=[];for(let r of Cn)t.delete(r)&&e.push(r);return e.push(...[...t].sort()),e}function Ot(t,e={}){let r=ct(t),s={cwd:e.cwd===void 0?"/":x("/",e.cwd),fs:r,user:e.user??"user"};if(s.dispatch=(o,i,u)=>Pt(o,i,u,s),s.hasCommand=o=>!!(P[o]||Z[o]),!r.isDir(s.cwd))throw new Error(`createTerminal: cwd is not a directory: ${s.cwd}`);return{run:o=>An(o,s),cwd:()=>s.cwd,complete:o=>Lt(o,s,{names:Rt,pipeNames:_n,binPrefixes:jt,resolveCommand:qt})}}var jt=["/usr/local/bin/","/usr/bin/","/bin/","/sbin/"];function qt(t){for(let e of jt)if(t.startsWith(e)){let r=t.slice(e.length);if(P[r]||Z[r])return r}return t}function Pt(t,e,r,n){let s=qt(t),o=P[s]??Z[s];if(!o)return In(t);try{return o(r,e,n)}catch(i){return d(`${t}: ${i.message}`)}}function An(t,e){try{let r=t.trim();if(r==="")return{stdout:"",stderr:"",exitCode:0,cwd:e.cwd};let n=st(r);return{...Ht(n,e,""),cwd:e.cwd}}catch(r){return{...d(`error: ${r.message}`),cwd:e.cwd}}}function Ht(t,e,r){let n="",s="",o=0,i=!1;for(let u=0;u<t.length;u++){let c=t[u];if(c.gate==="and"&&o!==0||c.gate==="or"&&o===0)continue;let a=Dn(c.stages,e,u===0?r:"");n+=a.stdout,s+=a.stderr,o=a.exitCode,i=!0}return i||(o=0),{stdout:n,stderr:s,exitCode:o}}function Dn(t,e,r){let n=r,s="";for(let o=0;o<t.length;o++){let i=t[o],u=i.group?Fn(i.group,e,n):Nn(i,e,n),c=u.stdout,a=u.stderr;if(i.mergeStderrToStdout&&(c+=a,a=""),i.mergeStdoutToStderr&&(a+=c,c=""),i.stdoutToNull&&(c=""),i.stderrToNull&&(a=""),s+=a,o===t.length-1)return{stdout:c,stderr:s,exitCode:u.exitCode};n=c}return{stdout:"",stderr:s,exitCode:0}}function Nn(t,e,r){let n=nt(t.argv,t.quoted??new Set),s=gt(n.argv,n.quoted,e);return Pt(s[0],s.slice(1),r,e)}function Fn(t,e,r){let n=e.cwd;try{return Ht(t,e,r)}finally{e.cwd=n}}function In(t){return d(`${t}: command not found. Available: ${En}`,127)}var Bt=`:host{display:flex;flex-direction:column;height:100%;min-height:0;background:var(--bg, #0c0c0c);color:var(--text, #e6edf3);font-family:ui-monospace,SF Mono,Menlo,Consolas,monospace;font-size:13px;line-height:1.45}.output{flex:1 1 auto;overflow-y:auto;overscroll-behavior:none;-webkit-app-region:no-drag;app-region:no-drag;scrollbar-gutter:stable;padding:12px 14px 6px;cursor:text}.banner{color:var(--muted, #8b949e);margin-bottom:8px}.banner .cmd{color:var(--accent, #58a6ff)}.banner .cmd:hover{text-decoration:underline}.line{white-space:pre-wrap;overflow-wrap:break-word}.cwd{color:var(--accent, #58a6ff)}.sigil{color:var(--muted, #8b949e);margin:0 6px 0 4px}.stdout,.stderr{margin:0 0 4px;font:inherit;white-space:pre-wrap;overflow-wrap:break-word}.stderr{color:var(--critical, #d8413a)}.form{flex:0 0 auto;display:flex;align-items:center;padding:8px 14px 10px;border-top:1px solid var(--border, #2c2c2c)}.form:focus-within{border-top-color:var(--accent, #58a6ff)}.form .cwd{flex:0 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;user-select:none}.form .sigil{flex:0 0 auto;user-select:none}.input-wrap{flex:1 1 auto;min-width:0;position:relative;display:flex;align-items:center}.input{width:100%;background:transparent;border:none;outline:none;color:inherit;font:inherit;padding:0;position:relative}.ghost{position:absolute;inset:0;display:flex;align-items:center;font:inherit;white-space:pre;overflow:hidden;pointer-events:none;user-select:none;color:var(--muted, #8b949e);opacity:.55}.ghost-pad{color:transparent}
|
|
43
|
+
`;function Ln(t){let r=["ls","find /",`grep -r ${Rn(t)} .`];if(!t||t.size===0)return r;let n=null;for(let s of t.keys()){let o=On(s);(n===null||o.length<n.length)&&(n=o)}return n&&r.push(`cat ${n} | head -n 20`),r}var J=["TODO","FIXME","XXX","HACK","NOTE","BUG","export","import","require","//"];function Rn(t){if(!t||t.size===0)return J[0];for(let e of J)for(let r of t.values())if(r.includes(e))return e;return J[0]}function On(t){return t.startsWith("./")?t.slice(2):t.startsWith("/")?t.slice(1):t}var K=class t extends tt{static properties={sources:{attribute:!1},_lines:{state:!0},_input:{state:!0},_cwd:{state:!0},_ghost:{state:!0}};static styles=et(Bt);#n=null;#c=null;#e=[];#t=-1;#s="";#r=null;#o=null;static#f=200;constructor(){super(),this.sources=null,this._lines=[],this._input="",this._cwd="/",this._ghost=""}willUpdate(e){e.has("sources")&&this.sources!==this.#c&&(this.#n=this.sources?Ot(this.sources):null,this._cwd=this.#n?this.#n.cwd():"/",this._lines=[{kind:"banner",commands:Ln(this.sources)}],this.#e=[],this.#t=-1,this.#s="",this.#r=null,this._input="",this.#c=this.sources),e.has("_input")&&(this.#u(),this.#l())}connectedCallback(){super.connectedCallback(),this.updateComplete.then(()=>this.#a())}disconnectedCallback(){super.disconnectedCallback(),this.#u()}firstUpdated(){this.#i()}updated(e){(e.has("_lines")||e.has("sources"))&&this.#a()}#i(){let e=this.renderRoot.querySelector(".input");e&&e.focus()}#a(){let e=this.renderRoot.querySelector(".output");e&&(e.scrollTop=e.scrollHeight)}#h=e=>{if(e.preventDefault(),!this.#n)return;let r=this._input,n=r.trim();if(n==="clear"){this._lines=[],this._input="",n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1;return}let s=this._cwd,o=this.#n.run(r);this._cwd=o.cwd;let i=[...this._lines,{kind:"prompt",cwd:s,text:r}];o.stdout&&i.push({kind:"stdout",text:o.stdout}),o.stderr&&i.push({kind:"stderr",text:o.stderr}),this._lines=i,n.length>0&&(this.#e=[...this.#e,r]),this.#t=-1,this._input=""};#d=e=>{if(e.key==="Tab"&&!e.shiftKey){e.preventDefault(),this.#p();return}this.#r=null,e.key==="ArrowUp"?(e.preventDefault(),this.#g()):e.key==="ArrowDown"&&(e.preventDefault(),this.#w()),this.#u(),this.#l()};#p(){if(!this.#n)return;let e=this.#r;if(e&&e.items[e.index]===this._input){e.index=(e.index+1)%e.items.length,this._input=e.items[e.index];return}let r=this.#n.complete(this._input);if(r.length===0){this.#r=null;return}this._input=r[0],this.#r=r.length>1?{items:r,index:0}:null}#u(){this._ghost="",this.#o!==null&&(clearTimeout(this.#o),this.#o=null)}#l(){this.#o=setTimeout(()=>{this.#o=null,this.#m()},t.#f)}#m(){if(!this.#n||this.#r)return;let e=this._input,r=e.slice(-1),n=e===""||r===" "||r==="|",s=/[\w./]/u.test(r);if(!n&&!s)return;let o=this.#n.complete(e);if(o.length===0)return;let i=o[0];!i.startsWith(e)||i===e||s&&o.includes(e)||(this._ghost=i.slice(e.length))}#g(){this.#e.length!==0&&(this.#t<0?(this.#s=this._input,this.#t=this.#e.length-1):this.#t=Math.max(0,this.#t-1),this._input=this.#e[this.#t])}#w(){if(this.#t<0)return;let e=this.#t+1;e>=this.#e.length?(this.#t=-1,this._input=this.#s,this.#s=""):(this.#t=e,this._input=this.#e[e])}#x=e=>{this._input=e.target.value};#b=e=>{this._input=e,this.#t=-1,this.#s="",this.#i()};#S=()=>{let e=this.renderRoot.getSelection?.()??document.getSelection();e&&!e.isCollapsed||this.#i()};render(){return M`<div class="output" @click="${this.#S}">${this._lines.map(e=>this.#y(e))}</div><form class="form" @submit="${this.#h}"><span class="cwd">${this._cwd}</span><span class="sigil">$</span><div class="input-wrap">${this._ghost?M`<div class="ghost" aria-hidden="true"><span class="ghost-pad">${this._input}</span>${this._ghost}</div>`:H} <input class="input" type="text" aria-label="Terminal command" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" .value="${this._input}" @input="${this.#x}" @keydown="${this.#d}"></div></form>`}#y(e){return e.kind==="banner"?M`<div class="banner">Virtual shell over the bundle source tree. Try ${e.commands.map((r,n)=>M`${n>0?", ":""}\`<span class="cmd" @click="${()=>this.#b(r)}">${r}</span>\``)}. ↑/↓ for history, Tab to complete.</div>`:e.kind==="prompt"?M`<div class="line"><span class="cwd">${e.cwd}</span><span class="sigil">$</span>${e.text}</div>`:e.kind==="stdout"?M`<pre class="stdout">${e.text}</pre>`:e.kind==="stderr"?M`<pre class="stderr">${e.text}</pre>`:H}};customElements.define("bundle-terminal",K);function qr(t,e){let r=document.createElement("bundle-terminal");return r.dataset.integrity=e??"",r.sources=t,r}export{qr as createTerminalElement};
|