token-goat 2.9.30 → 2.9.31
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/README.md +24 -7
- package/SECURITY.md +10 -2
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/native/linux-arm64/tg-hook +0 -0
- package/dist/native/linux-x64/tg-hook +0 -0
- package/dist/native/win32-arm64/tg-hook.exe +0 -0
- package/dist/native/win32-x64/tg-hook.exe +0 -0
- package/dist/{token-goat-chunk-QSBRTGM2.mjs → token-goat-chunk-25TRAWET.mjs} +17 -7
- package/dist/token-goat-chunk-2KXO6V2B.mjs +203 -0
- package/dist/{token-goat-chunk-IVKIZWAB.mjs → token-goat-chunk-2M7E4PF7.mjs} +41 -12
- package/dist/{token-goat-chunk-SMVRN2HU.mjs → token-goat-chunk-2UPZGKKY.mjs} +2 -2
- package/dist/token-goat-chunk-2WLPWKHV.mjs +135 -0
- package/dist/token-goat-chunk-2XBMMDBD.mjs +126 -0
- package/dist/token-goat-chunk-32ZOY4N5.mjs +23 -0
- package/dist/token-goat-chunk-3EZRNKTK.mjs +188 -0
- package/dist/token-goat-chunk-4ADZHI7J.mjs +63 -0
- package/dist/token-goat-chunk-4GBVPH3D.mjs +242 -0
- package/dist/{token-goat-chunk-KWPNJBDH.mjs → token-goat-chunk-4GILLLWY.mjs} +7 -4
- package/dist/{token-goat-chunk-67TQJ76B.mjs → token-goat-chunk-4NAPX7BA.mjs} +36 -3
- package/dist/{token-goat-chunk-PU7V3FD6.mjs → token-goat-chunk-4SC7UJYV.mjs} +3354 -2553
- package/dist/token-goat-chunk-4SMRO4TO.mjs +366 -0
- package/dist/{token-goat-chunk-AFLYLHDH.mjs → token-goat-chunk-4ZBFWLXF.mjs} +283 -427
- package/dist/{token-goat-chunk-JDWCRD6M.mjs → token-goat-chunk-57BIAXJL.mjs} +73 -40
- package/dist/token-goat-chunk-5AC66HNP.mjs +115 -0
- package/dist/token-goat-chunk-5IMLKSOP.mjs +145 -0
- package/dist/token-goat-chunk-646E6U44.mjs +398 -0
- package/dist/token-goat-chunk-65N4RHP4.mjs +33 -0
- package/dist/{token-goat-chunk-JN4XMLSD.mjs → token-goat-chunk-6DG6HWP3.mjs} +8 -2
- package/dist/token-goat-chunk-6ENQSNRZ.mjs +573 -0
- package/dist/token-goat-chunk-6ZY4GNF2.mjs +91 -0
- package/dist/{token-goat-chunk-VPBBTSJR.mjs → token-goat-chunk-76FGDLRO.mjs} +262 -255
- package/dist/{token-goat-chunk-7IN2MKDS.mjs → token-goat-chunk-7EZTZOMC.mjs} +121 -289
- package/dist/{token-goat-chunk-OJBNYTJP.mjs → token-goat-chunk-7MAOA6X4.mjs} +640 -80
- package/dist/token-goat-chunk-7R2ZVVAX.mjs +218 -0
- package/dist/{token-goat-chunk-PD5VKH46.mjs → token-goat-chunk-AUNGYYQX.mjs} +2 -2
- package/dist/{token-goat-chunk-HG45AF2B.mjs → token-goat-chunk-AUQYRMMH.mjs} +794 -37
- package/dist/token-goat-chunk-B3PEKBYG.mjs +128 -0
- package/dist/{token-goat-chunk-A3CK6N5Z.mjs → token-goat-chunk-BA4HI6BW.mjs} +1309 -110
- package/dist/{token-goat-chunk-JB6QN6VF.mjs → token-goat-chunk-BFCWY73P.mjs} +59 -23
- package/dist/{token-goat-chunk-B45VMIIH.mjs → token-goat-chunk-BWGFUZZE.mjs} +7 -5
- package/dist/{token-goat-chunk-P4QMM4WK.mjs → token-goat-chunk-C75NA7PL.mjs} +50 -23
- package/dist/token-goat-chunk-CBIQ7WTD.mjs +87 -0
- package/dist/token-goat-chunk-CETM74KC.mjs +307 -0
- package/dist/{token-goat-chunk-H2PHHGM5.mjs → token-goat-chunk-CIMQ2BB3.mjs} +57 -12
- package/dist/{token-goat-chunk-N2NMER56.mjs → token-goat-chunk-CJLS3CDL.mjs} +46 -4
- package/dist/{token-goat-chunk-VES7EL7O.mjs → token-goat-chunk-CO3UYX6G.mjs} +1 -1
- package/dist/token-goat-chunk-COPNXRJM.mjs +154 -0
- package/dist/token-goat-chunk-CSJ5L3NL.mjs +479 -0
- package/dist/{token-goat-chunk-4LCCNRTG.mjs → token-goat-chunk-CVCEVUGK.mjs} +8 -6
- package/dist/{token-goat-chunk-B2RZZCSS.mjs → token-goat-chunk-CXHOFN7R.mjs} +37 -20
- package/dist/token-goat-chunk-D735C4QM.mjs +603 -0
- package/dist/{token-goat-chunk-APGZXSPS.mjs → token-goat-chunk-DGS2LVNA.mjs} +47 -30
- package/dist/{token-goat-chunk-54K3EUV3.mjs → token-goat-chunk-DOWRSMWO.mjs} +1 -1
- package/dist/token-goat-chunk-E45INMY3.mjs +71 -0
- package/dist/token-goat-chunk-EC5F37F4.mjs +244 -0
- package/dist/{token-goat-chunk-XTQAOTSO.mjs → token-goat-chunk-EDBDRVH5.mjs} +4 -1
- package/dist/{token-goat-chunk-5BCIJXMO.mjs → token-goat-chunk-EELTBKDM.mjs} +14 -10
- package/dist/{token-goat-chunk-W3QCJS6G.mjs → token-goat-chunk-F4HYL64R.mjs} +8 -6
- package/dist/token-goat-chunk-F6PAUIEK.mjs +624 -0
- package/dist/token-goat-chunk-FDSREWLX.mjs +692 -0
- package/dist/token-goat-chunk-GGGG7RWB.mjs +236 -0
- package/dist/{token-goat-chunk-C63JQV5K.mjs → token-goat-chunk-GL3ETKSC.mjs} +233 -15
- package/dist/token-goat-chunk-GVB2MUST.mjs +1298 -0
- package/dist/token-goat-chunk-GWFLAUMQ.mjs +307 -0
- package/dist/{token-goat-chunk-ZEJVMUQH.mjs → token-goat-chunk-HLPW63A7.mjs} +2 -2
- package/dist/{token-goat-chunk-GOLEOXZP.mjs → token-goat-chunk-HU4TORC5.mjs} +44 -26
- package/dist/{token-goat-chunk-Z57YODA2.mjs → token-goat-chunk-I7VKZZAW.mjs} +2 -2
- package/dist/{token-goat-chunk-NYKZLXMK.mjs → token-goat-chunk-IA6XSPE3.mjs} +6 -6
- package/dist/{token-goat-chunk-E3OQX2XT.mjs → token-goat-chunk-IAGXMHIK.mjs} +81 -42
- package/dist/{token-goat-chunk-HYBFA5X3.mjs → token-goat-chunk-IK2DRMFX.mjs} +24 -68
- package/dist/{token-goat-chunk-COBQGL5S.mjs → token-goat-chunk-INNPTRUX.mjs} +249 -299
- package/dist/{token-goat-chunk-7NADEDKJ.mjs → token-goat-chunk-JJ5BX4F4.mjs} +6 -1
- package/dist/{token-goat-chunk-PEPTHRFF.mjs → token-goat-chunk-JVFSHVFJ.mjs} +6 -6
- package/dist/token-goat-chunk-KGEFFSHD.mjs +50 -0
- package/dist/token-goat-chunk-KQBLRPBN.mjs +433 -0
- package/dist/{token-goat-chunk-Q5VTGRH7.mjs → token-goat-chunk-LCNQ2CNN.mjs} +55 -319
- package/dist/token-goat-chunk-M3LFJDPU.mjs +58 -0
- package/dist/token-goat-chunk-M52KPT3C.mjs +87 -0
- package/dist/token-goat-chunk-MBEG42H7.mjs +150 -0
- package/dist/{token-goat-chunk-EVC4TOLE.mjs → token-goat-chunk-METECDOH.mjs} +3 -12
- package/dist/{token-goat-chunk-T4FK7VOV.mjs → token-goat-chunk-MHX7XCC3.mjs} +2 -1
- package/dist/token-goat-chunk-N5LSOVSU.mjs +137 -0
- package/dist/{token-goat-chunk-LOBGHXPB.mjs → token-goat-chunk-N5MMR2IW.mjs} +1 -1
- package/dist/token-goat-chunk-N7B3JQ2C.mjs +160 -0
- package/dist/{token-goat-chunk-RHXKCZX3.mjs → token-goat-chunk-NISNMD7T.mjs} +2182 -2959
- package/dist/token-goat-chunk-NMD42SFQ.mjs +434 -0
- package/dist/token-goat-chunk-NOSS5VWY.mjs +174 -0
- package/dist/{token-goat-chunk-KQNZDCBD.mjs → token-goat-chunk-NSNSPPBG.mjs} +25 -3
- package/dist/token-goat-chunk-OBOFIHKN.mjs +38 -0
- package/dist/token-goat-chunk-OBRFJ5O5.mjs +76 -0
- package/dist/{token-goat-chunk-JTQQGXZH.mjs → token-goat-chunk-ODBT2USM.mjs} +124 -57
- package/dist/token-goat-chunk-OS5EM5PP.mjs +107 -0
- package/dist/token-goat-chunk-OUKDTVNU.mjs +1178 -0
- package/dist/token-goat-chunk-OXFMP75N.mjs +566 -0
- package/dist/{token-goat-chunk-DZX2YDPT.mjs → token-goat-chunk-OYIYKDDT.mjs} +2 -2
- package/dist/{token-goat-chunk-QCLDAQTS.mjs → token-goat-chunk-P6HQSWQ3.mjs} +50 -53
- package/dist/token-goat-chunk-P6VGNTQT.mjs +71 -0
- package/dist/{token-goat-chunk-7BXSNBY5.mjs → token-goat-chunk-PFWIMH2E.mjs} +23 -15
- package/dist/token-goat-chunk-PJZXIS3G.mjs +39 -0
- package/dist/token-goat-chunk-PUO5INXP.mjs +606 -0
- package/dist/{token-goat-chunk-GFBSN7BD.mjs → token-goat-chunk-PX5PQWTY.mjs} +22 -250
- package/dist/token-goat-chunk-Q5QDBC6O.mjs +111 -0
- package/dist/token-goat-chunk-QFNQXSDT.mjs +40 -0
- package/dist/{token-goat-chunk-FBNQNSJK.mjs → token-goat-chunk-QH7YVYOY.mjs} +2198 -2782
- package/dist/{token-goat-chunk-64FBPDTK.mjs → token-goat-chunk-QLMUHEPB.mjs} +12 -9
- package/dist/{token-goat-chunk-RZRTDST7.mjs → token-goat-chunk-QQOJIHL3.mjs} +37 -11
- package/dist/{token-goat-chunk-XKI4SEMX.mjs → token-goat-chunk-QQTWJY2O.mjs} +19 -0
- package/dist/{token-goat-chunk-U4N3BN2W.mjs → token-goat-chunk-QYHO5CPH.mjs} +3 -2
- package/dist/token-goat-chunk-RELAVWAD.mjs +27 -0
- package/dist/{token-goat-chunk-ETBHVDB6.mjs → token-goat-chunk-RSLPOBKQ.mjs} +22 -11
- package/dist/token-goat-chunk-SAMFDIS2.mjs +712 -0
- package/dist/token-goat-chunk-SDELXLDM.mjs +48 -0
- package/dist/{token-goat-chunk-4JJS54TR.mjs → token-goat-chunk-SOAFZNNZ.mjs} +161 -38
- package/dist/{token-goat-chunk-664RTTUV.mjs → token-goat-chunk-SRTHT7KV.mjs} +21 -16
- package/dist/{token-goat-chunk-4FVIAHSI.mjs → token-goat-chunk-T5P5ACNQ.mjs} +7 -6
- package/dist/token-goat-chunk-T5YGG4V3.mjs +45 -0
- package/dist/token-goat-chunk-TAUZ4OFS.mjs +110 -0
- package/dist/{token-goat-chunk-COTXZHTS.mjs → token-goat-chunk-TM6MC754.mjs} +4 -3
- package/dist/token-goat-chunk-TWUKBOMP.mjs +72 -0
- package/dist/{token-goat-chunk-3CHUNJ2O.mjs → token-goat-chunk-U2KSB4SG.mjs} +8 -5
- package/dist/{token-goat-chunk-3Z5S4KGM.mjs → token-goat-chunk-U4WHMG6Y.mjs} +29 -49
- package/dist/{token-goat-chunk-PY533T7F.mjs → token-goat-chunk-UGJHGR5A.mjs} +21 -15
- package/dist/{token-goat-chunk-CYCEYAEO.mjs → token-goat-chunk-UOUDVA5U.mjs} +1 -1
- package/dist/{token-goat-chunk-JDHE26RP.mjs → token-goat-chunk-URRJ33GR.mjs} +16 -5
- package/dist/{token-goat-chunk-56EIMS3E.mjs → token-goat-chunk-UZOROKIS.mjs} +23 -17
- package/dist/{token-goat-chunk-CZ3LUXYZ.mjs → token-goat-chunk-VIHGGGEI.mjs} +427 -308
- package/dist/token-goat-chunk-VTJLAA7P.mjs +21 -0
- package/dist/token-goat-chunk-VWDXLT4F.mjs +678 -0
- package/dist/{token-goat-chunk-YARZFCZS.mjs → token-goat-chunk-VWZA2H4P.mjs} +62 -53
- package/dist/{token-goat-chunk-SQSSNQPE.mjs → token-goat-chunk-VXEXMJDL.mjs} +3979 -4745
- package/dist/token-goat-chunk-WBA5SKT4.mjs +547 -0
- package/dist/{token-goat-chunk-SJHWVNDQ.mjs → token-goat-chunk-WDH44VAU.mjs} +7 -4
- package/dist/token-goat-chunk-X2SUP3EI.mjs +107 -0
- package/dist/token-goat-chunk-X3BELZSJ.mjs +125 -0
- package/dist/{token-goat-chunk-FZ3NEW5O.mjs → token-goat-chunk-XLRBNZCC.mjs} +13 -8
- package/dist/{token-goat-chunk-Z44YO3QZ.mjs → token-goat-chunk-XRKUX6CM.mjs} +1 -1
- package/dist/token-goat-chunk-XX6O25RN.mjs +82 -0
- package/dist/token-goat-chunk-XZSUHBVS.mjs +65 -0
- package/dist/{token-goat-chunk-WY3L4DVT.mjs → token-goat-chunk-Y2ZTQOCL.mjs} +13 -8
- package/dist/{token-goat-chunk-UQ5UK2T7.mjs → token-goat-chunk-YB4VMGK2.mjs} +1 -1
- package/dist/token-goat-chunk-YBIIY3HR.mjs +51 -0
- package/dist/{token-goat-chunk-ZQSJMO2R.mjs → token-goat-chunk-YHVDKQBR.mjs} +105 -105
- package/dist/{token-goat-chunk-H2K3QP6H.mjs → token-goat-chunk-YJB5XLWI.mjs} +8 -6
- package/dist/{token-goat-chunk-ZCDTCVIS.mjs → token-goat-chunk-YKB6AQG3.mjs} +5 -2
- package/dist/token-goat-chunk-YMTG2YA5.mjs +35 -0
- package/dist/token-goat-chunk-YPBZYQRV.mjs +40 -0
- package/dist/{token-goat-chunk-HOJLCGDP.mjs → token-goat-chunk-YY6AVUFL.mjs} +2033 -2251
- package/dist/token-goat-chunk-Z7N5OMMP.mjs +98 -0
- package/dist/token-goat-chunk-ZNMNUPAZ.mjs +938 -0
- package/dist/token-goat-hook-client.cjs +1 -1
- package/dist/token-goat-hook-client.mjs +3 -3
- package/dist/token-goat-hook.mjs +67 -38
- package/dist/token-goat.core.mjs +4 -4
- package/docs/C4_RUNTIME_ARCHITECTURE.md +1 -1
- package/docs/cli.md +40 -21
- package/docs/install.md +19 -9
- package/docs/security.md +5 -3
- package/package.json +2 -1
- package/dist/token-goat-chunk-5KDMCMF2.mjs +0 -362
- package/dist/token-goat-chunk-6QMAS7AB.mjs +0 -42
- package/dist/token-goat-chunk-7F2UBPD3.mjs +0 -397
- package/dist/token-goat-chunk-ARDNEC2U.mjs +0 -248
- package/dist/token-goat-chunk-ATCPY7EQ.mjs +0 -134
- package/dist/token-goat-chunk-EFHMMIGP.mjs +0 -68
- package/dist/token-goat-chunk-IVALLT3S.mjs +0 -48
- package/dist/token-goat-chunk-JDU3EXSU.mjs +0 -65
- package/dist/token-goat-chunk-MKPF4CL5.mjs +0 -37
- package/dist/token-goat-chunk-RC35RBUL.mjs +0 -312
- package/dist/token-goat-chunk-USILZ3T3.mjs +0 -53
- package/dist/token-goat-chunk-VL3YIFHZ.mjs +0 -2975
- package/dist/token-goat-chunk-XU4P6Z24.mjs +0 -133
- package/dist/token-goat-chunk-Y776AENC.mjs +0 -1597
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
import { createRequire as __cjsRequire } from 'node:module';
|
|
2
|
+
const require = __cjsRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
deliveredOutputBytes
|
|
5
|
+
} from "./token-goat-chunk-DOWRSMWO.mjs";
|
|
6
|
+
import {
|
|
7
|
+
PassthroughFilter,
|
|
8
|
+
TOOL_FILTERS,
|
|
9
|
+
compressedTokensSaved,
|
|
10
|
+
deliverCompressed,
|
|
11
|
+
dispatchArgv,
|
|
12
|
+
filterByName,
|
|
13
|
+
selectFilter
|
|
14
|
+
} from "./token-goat-chunk-INNPTRUX.mjs";
|
|
15
|
+
import {
|
|
16
|
+
spawnOwned
|
|
17
|
+
} from "./token-goat-chunk-N5LSOVSU.mjs";
|
|
18
|
+
import {
|
|
19
|
+
storeBashOutputSync
|
|
20
|
+
} from "./token-goat-chunk-KQBLRPBN.mjs";
|
|
21
|
+
import {
|
|
22
|
+
recordStat
|
|
23
|
+
} from "./token-goat-chunk-U4WHMG6Y.mjs";
|
|
24
|
+
import {
|
|
25
|
+
capTokens,
|
|
26
|
+
shlexSplit
|
|
27
|
+
} from "./token-goat-chunk-4GILLLWY.mjs";
|
|
28
|
+
import {
|
|
29
|
+
powerShellCommandArgs,
|
|
30
|
+
resolvePowerShell,
|
|
31
|
+
wrappedShell
|
|
32
|
+
} from "./token-goat-chunk-X2SUP3EI.mjs";
|
|
33
|
+
import {
|
|
34
|
+
loadConfig
|
|
35
|
+
} from "./token-goat-chunk-7EZTZOMC.mjs";
|
|
36
|
+
import {
|
|
37
|
+
echoedValue
|
|
38
|
+
} from "./token-goat-chunk-GGGG7RWB.mjs";
|
|
39
|
+
import {
|
|
40
|
+
quotePowershellPath
|
|
41
|
+
} from "./token-goat-chunk-AM5V6GSX.mjs";
|
|
42
|
+
import {
|
|
43
|
+
displaySafeText
|
|
44
|
+
} from "./token-goat-chunk-FNFBKLRD.mjs";
|
|
45
|
+
import {
|
|
46
|
+
init_define_import_meta_env
|
|
47
|
+
} from "./token-goat-chunk-A37V4PBF.mjs";
|
|
48
|
+
|
|
49
|
+
// src/bash_runner.ts
|
|
50
|
+
init_define_import_meta_env();
|
|
51
|
+
import * as os from "node:os";
|
|
52
|
+
|
|
53
|
+
// src/powershell_compat.ts
|
|
54
|
+
init_define_import_meta_env();
|
|
55
|
+
var heredocRe = () => /(?:^|(?<=[;\r\n]|&&|\|\|))\s*([^\r\n;&|<]+?)\s*<<(-?)[ \t]*(['"]?)([A-Za-z0-9_]+)\3([^\r\n]*?)(?=\r?\n)/g;
|
|
56
|
+
var HEREDOC_HEAD_RE = /<<(-?)[ \t]*(['"]?)([A-Za-z0-9_]+)\2/y;
|
|
57
|
+
var HEREDOC_TERMINATOR_RE = /^(\t*)([A-Za-z0-9_]+)$/;
|
|
58
|
+
var SINGLE_QUOTES = "'\u2018\u2019\u201A\u201B";
|
|
59
|
+
var DOUBLE_QUOTES = '"\u201C\u201D\u201E';
|
|
60
|
+
function isQuote(quotes, ch) {
|
|
61
|
+
return ch !== void 0 && quotes.includes(ch);
|
|
62
|
+
}
|
|
63
|
+
function scanSingleQuoted(src, i) {
|
|
64
|
+
for (let j = i; j < src.length; j++) {
|
|
65
|
+
if (!isQuote(SINGLE_QUOTES, src[j])) continue;
|
|
66
|
+
if (isQuote(SINGLE_QUOTES, src[j + 1])) {
|
|
67
|
+
j++;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
return { end: j + 1, closed: true };
|
|
71
|
+
}
|
|
72
|
+
return { end: src.length, closed: false };
|
|
73
|
+
}
|
|
74
|
+
function scanDoubleQuoted(src, i) {
|
|
75
|
+
let j = i;
|
|
76
|
+
while (j < src.length) {
|
|
77
|
+
const ch = src[j];
|
|
78
|
+
if (ch === "`") {
|
|
79
|
+
j += 2;
|
|
80
|
+
} else if (isQuote(DOUBLE_QUOTES, ch)) {
|
|
81
|
+
if (!isQuote(DOUBLE_QUOTES, src[j + 1])) return { end: j + 1, closed: true };
|
|
82
|
+
j += 2;
|
|
83
|
+
} else if (ch === "$" && (src[j + 1] === "(" || src[j + 1] === "{")) {
|
|
84
|
+
const inner = src[j + 1] === "(" ? scanSubexpression(src, j + 2) : scanBracedVariable(src, j + 2);
|
|
85
|
+
if (!inner.closed) return inner;
|
|
86
|
+
j = inner.end;
|
|
87
|
+
} else {
|
|
88
|
+
j++;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { end: src.length, closed: false };
|
|
92
|
+
}
|
|
93
|
+
function scanBracedVariable(src, i) {
|
|
94
|
+
for (let j = i; j < src.length; j++) {
|
|
95
|
+
if (src[j] === "`") j++;
|
|
96
|
+
else if (src[j] === "}") return { end: j + 1, closed: true };
|
|
97
|
+
}
|
|
98
|
+
return { end: src.length, closed: false };
|
|
99
|
+
}
|
|
100
|
+
function scanSubexpression(src, i) {
|
|
101
|
+
let depth = 0;
|
|
102
|
+
let j = i;
|
|
103
|
+
while (j < src.length) {
|
|
104
|
+
const ch = src[j];
|
|
105
|
+
if (ch === ")") {
|
|
106
|
+
if (depth === 0) return { end: j + 1, closed: true };
|
|
107
|
+
depth--;
|
|
108
|
+
} else if (ch === "(") {
|
|
109
|
+
depth++;
|
|
110
|
+
} else {
|
|
111
|
+
const token = scanToken(src, j);
|
|
112
|
+
if (token) {
|
|
113
|
+
if (!token.closed) return token;
|
|
114
|
+
j = token.end;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
j++;
|
|
119
|
+
}
|
|
120
|
+
return { end: src.length, closed: false };
|
|
121
|
+
}
|
|
122
|
+
function scanToken(src, i) {
|
|
123
|
+
const ch = src[i];
|
|
124
|
+
const next = src[i + 1];
|
|
125
|
+
if (ch === "`") return { end: Math.min(i + 2, src.length), closed: true };
|
|
126
|
+
if (ch === "<" && next === "#") {
|
|
127
|
+
const close = src.indexOf("#>", i + 2);
|
|
128
|
+
return close === -1 ? { end: src.length, closed: false } : { end: close + 2, closed: true };
|
|
129
|
+
}
|
|
130
|
+
if (ch === "#" && (i === 0 || /[\s;|&(){},=]/.test(src[i - 1]))) {
|
|
131
|
+
const eol = src.indexOf("\n", i);
|
|
132
|
+
return { end: eol === -1 ? src.length : eol, closed: true };
|
|
133
|
+
}
|
|
134
|
+
if (ch === "@" && (isQuote(SINGLE_QUOTES, next) || isQuote(DOUBLE_QUOTES, next))) {
|
|
135
|
+
const head = /[ \t]*\r?\n/y;
|
|
136
|
+
head.lastIndex = i + 2;
|
|
137
|
+
if (head.exec(src)) {
|
|
138
|
+
const quotes = isQuote(SINGLE_QUOTES, next) ? SINGLE_QUOTES : DOUBLE_QUOTES;
|
|
139
|
+
for (let nl = head.lastIndex - 1; nl !== -1; nl = src.indexOf("\n", nl + 1)) {
|
|
140
|
+
if (isQuote(quotes, src[nl + 1]) && src[nl + 2] === "@") return { end: nl + 3, closed: true };
|
|
141
|
+
}
|
|
142
|
+
return { end: src.length, closed: false };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (isQuote(SINGLE_QUOTES, ch)) return scanSingleQuoted(src, i + 1);
|
|
146
|
+
if (isQuote(DOUBLE_QUOTES, ch)) return scanDoubleQuoted(src, i + 1);
|
|
147
|
+
if (ch === "$" && next === "{") return scanBracedVariable(src, i + 2);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
function skipHeredocBodies(src, start, pending, heredocs) {
|
|
151
|
+
const found = [];
|
|
152
|
+
let lineStart = start;
|
|
153
|
+
for (const { op, delim, stripTabs } of pending) {
|
|
154
|
+
let span = null;
|
|
155
|
+
for (let ls = lineStart; ls <= src.length; ) {
|
|
156
|
+
const nl2 = src.indexOf("\n", ls);
|
|
157
|
+
const term = HEREDOC_TERMINATOR_RE.exec(src.slice(ls, nl2 === -1 ? src.length : nl2));
|
|
158
|
+
if (term && term[2] === delim && (stripTabs || term[1] === "")) {
|
|
159
|
+
span = { bodyStart: lineStart, termStart: ls, end: ls + term[0].length };
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
if (nl2 === -1) break;
|
|
163
|
+
ls = nl2 + 1;
|
|
164
|
+
}
|
|
165
|
+
if (span === null) return start;
|
|
166
|
+
found.push([op, span]);
|
|
167
|
+
const nl = src.indexOf("\n", span.end);
|
|
168
|
+
lineStart = nl === -1 ? src.length : nl + 1;
|
|
169
|
+
}
|
|
170
|
+
for (const [op, span] of found) heredocs.set(op, span);
|
|
171
|
+
return found[found.length - 1][1].end;
|
|
172
|
+
}
|
|
173
|
+
function layoutPowerShell(src) {
|
|
174
|
+
const code = new Uint8Array(src.length);
|
|
175
|
+
const stringEnd = /* @__PURE__ */ new Map();
|
|
176
|
+
const heredocs = /* @__PURE__ */ new Map();
|
|
177
|
+
let pending = [];
|
|
178
|
+
let i = 0;
|
|
179
|
+
while (i < src.length) {
|
|
180
|
+
if (src[i] === "\n" && pending.length > 0) {
|
|
181
|
+
code[i] = 1;
|
|
182
|
+
i = skipHeredocBodies(src, i + 1, pending, heredocs);
|
|
183
|
+
pending = [];
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
HEREDOC_HEAD_RE.lastIndex = i;
|
|
187
|
+
const head = src[i] === "<" ? HEREDOC_HEAD_RE.exec(src) : null;
|
|
188
|
+
if (head) {
|
|
189
|
+
pending.push({ op: i, delim: head[3], stripTabs: head[1] === "-" });
|
|
190
|
+
code.fill(1, i, i + 2);
|
|
191
|
+
i += 2;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const token = scanToken(src, i);
|
|
195
|
+
if (token) {
|
|
196
|
+
if (token.closed && (isQuote(SINGLE_QUOTES, src[i]) || isQuote(DOUBLE_QUOTES, src[i]))) stringEnd.set(i, token.end);
|
|
197
|
+
i = token.end;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
code[i] = 1;
|
|
201
|
+
i++;
|
|
202
|
+
}
|
|
203
|
+
return { code, stringEnd, heredocs };
|
|
204
|
+
}
|
|
205
|
+
var PYTHON_NAME = String.raw`(?:python3?|py)(?:\.exe)?`;
|
|
206
|
+
var PYTHON_BIN = String.raw`(?:[A-Za-z0-9_.:\\/-]*[\\/])?${PYTHON_NAME}`;
|
|
207
|
+
var inlinePythonRe = () => new RegExp(String.raw`(?:^|(?<=[;\r\n]|&&|\|\|))\s*(${PYTHON_BIN})\s+([^;&|\r\n]*?)-c\s+`, "gi");
|
|
208
|
+
var INLINE_PYTHON_PROBE_RE = new RegExp(String.raw`${PYTHON_NAME}\s+[^;&|\r\n]*?-c\s+`, "i");
|
|
209
|
+
function interpreterOptionsOnly(preFlags) {
|
|
210
|
+
const words = preFlags.split(/\s+/).filter((w) => w !== "");
|
|
211
|
+
for (let i = 0; i < words.length; i++) {
|
|
212
|
+
const w = words[i];
|
|
213
|
+
if (!/^-[A-Za-z0-9.]+$/.test(w) || w.startsWith("-m")) return false;
|
|
214
|
+
if (w === "-X" || w === "-W") i++;
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
var PYTHON_CALL_RE = new RegExp(String.raw`^${PYTHON_BIN}(?=\s|$)`, "i");
|
|
219
|
+
var MAX_ENCODED_CHARS = 28e3;
|
|
220
|
+
var PYTHON_LOADER = "(lambda s:(not s.flags.ignore_environment and getattr(s.modules.get('os'),'environ',{}).get('PYTHONIOENCODING')) or [getattr(f,'reconfigure',lambda **k:0)(encoding='utf-8',errors=getattr(f,'errors',None)) for f in (s.stdin,s.stdout,s.stderr)])(__import__('sys'));exec(bytearray.fromhex(__import__('sys').argv.pop(1)[1:]).decode('utf-8'))";
|
|
221
|
+
var PYTHON_LOADER_ARG = `'${PYTHON_LOADER.replace(/'/g, "''")}'`;
|
|
222
|
+
function base64Bytes(text) {
|
|
223
|
+
const bytes = Buffer.from(text, "utf8");
|
|
224
|
+
return { base64: bytes.toString("base64"), bytes: bytes.length };
|
|
225
|
+
}
|
|
226
|
+
function utf8TextExpression(text) {
|
|
227
|
+
return `[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64Bytes(text).base64}'))`;
|
|
228
|
+
}
|
|
229
|
+
function inUtf8PipeScope(statements, python) {
|
|
230
|
+
const run2 = python ? `$TgPy = $env:PYTHONIOENCODING; if (-not $TgPy) { $env:PYTHONIOENCODING = 'utf-8' }; $TgQ = $false; try { ${statements} } finally { $env:PYTHONIOENCODING = $TgPy }; if (-not $TgQ)` : `$TgQ = $false; ${statements}; if (-not $TgQ)`;
|
|
231
|
+
return `& { [CmdletBinding()] param($TgEap) $ErrorActionPreference = $TgEap; $OutputEncoding = [System.Text.UTF8Encoding]::new($false); ${run2} { $PSCmdlet.WriteError([System.Management.Automation.ErrorRecord]::new([System.Exception]::new('pipeline failed'), 'TgPipelineFailed', [System.Management.Automation.ErrorCategory]::NotSpecified, $null)) } } $ErrorActionPreference -ErrorAction Ignore`;
|
|
232
|
+
}
|
|
233
|
+
function windowsArgument(arg) {
|
|
234
|
+
if (arg !== "" && !/[\s"]/.test(arg)) return arg;
|
|
235
|
+
return `"${arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, "$1$1")}"`;
|
|
236
|
+
}
|
|
237
|
+
var CMDLET_WORD_RE = /^[A-Z][A-Za-z]*-[A-Z][A-Za-z]*(?=\s|$)/;
|
|
238
|
+
function nativeCommandLine(command) {
|
|
239
|
+
const line = command.trim();
|
|
240
|
+
const wordRe = /\s*(?:'([^'‘-‛]*)'|"([^"$`\\“-„]*)"|([A-Za-z0-9_./:=+-]+))(?=\s|$)/y;
|
|
241
|
+
const words = [];
|
|
242
|
+
let quotedProgram = false;
|
|
243
|
+
for (let at = 0; at < line.length; at = wordRe.lastIndex) {
|
|
244
|
+
wordRe.lastIndex = at;
|
|
245
|
+
const word = wordRe.exec(line);
|
|
246
|
+
if (word === null) return null;
|
|
247
|
+
quotedProgram ||= words.length === 0 && word[3] === void 0;
|
|
248
|
+
words.push(word[1] ?? word[2] ?? word[3]);
|
|
249
|
+
}
|
|
250
|
+
const program = words.shift();
|
|
251
|
+
return program === void 0 || quotedProgram ? null : { program, args: words.map(windowsArgument).join(" ") };
|
|
252
|
+
}
|
|
253
|
+
function nativeStdinStatements(call, body, pipeline) {
|
|
254
|
+
const quote = (value) => `'${value.replace(/'/g, "''")}'`;
|
|
255
|
+
const start = `$TgI = [System.Diagnostics.ProcessStartInfo]::new($TgC.Source, ${quote(call.args)}); $TgI.UseShellExecute = $false; $TgI.RedirectStandardInput = $true; $TgI.WorkingDirectory = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation.ProviderPath; $TgU = $null; if ($PSVersionTable.PSEdition -eq 'Desktop') { try { $TgU = [Console]::InputEncoding; [Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false) } catch { $TgU = $null } }; try { $TgP = [System.Diagnostics.Process]::Start($TgI) } finally { if ($TgU) { try { [Console]::InputEncoding = $TgU } catch { } } }`;
|
|
256
|
+
const feed = `$TgB = [System.Convert]::FromBase64String('${body}'); try { $TgP.StandardInput.BaseStream.Write($TgB, 0, $TgB.Length); $TgP.StandardInput.Close() } catch [System.IO.IOException] { }; $TgP.WaitForExit(); $global:LASTEXITCODE = $TgP.ExitCode; $TgQ = $TgP.ExitCode -eq 0`;
|
|
257
|
+
return `$TgC = Get-Command -Name ${quote(call.program)} -ErrorAction Ignore | Select-Object -First 1; if ($TgC -and $TgC.CommandType -eq 'Application') { ${start}; ${feed} } else { ${pipeline}; $TgQ = $? }`;
|
|
258
|
+
}
|
|
259
|
+
function splitFirstPipeline(command) {
|
|
260
|
+
const layout = layoutPowerShell(command);
|
|
261
|
+
for (let i = 0; i < command.length; i++) {
|
|
262
|
+
if (layout.code[i] === 1 && /^(?:;|&&|\|\|)/.test(command.slice(i, i + 2))) return [command.slice(0, i).trimEnd(), ` ${command.slice(i)}`];
|
|
263
|
+
}
|
|
264
|
+
return [command, ""];
|
|
265
|
+
}
|
|
266
|
+
var BARE_REDIRECT_PATH_RE = /^[^\s>|;&<(){}$`'"@#*?[\\\u2018-\u201E-][^\s>|;&<(){}$`'"*?[\\\u2018-\u201E]*$/;
|
|
267
|
+
var SINGLE_QUOTED_UNSAFE_RE = /['\u2018-\u201B]/;
|
|
268
|
+
var DOUBLE_QUOTED_UNSAFE_RE = /["\u201C-\u201E`]|\\[\\$]/;
|
|
269
|
+
function redirectPath(word) {
|
|
270
|
+
const quote = word[0];
|
|
271
|
+
if (quote === "'" || quote === '"') {
|
|
272
|
+
const body = word.length >= 2 && word.endsWith(quote) ? word.slice(1, -1) : null;
|
|
273
|
+
return body !== null && !(quote === "'" ? SINGLE_QUOTED_UNSAFE_RE : DOUBLE_QUOTED_UNSAFE_RE).test(body) ? word : null;
|
|
274
|
+
}
|
|
275
|
+
return BARE_REDIRECT_PATH_RE.test(word) ? quotePowershellPath(word) : null;
|
|
276
|
+
}
|
|
277
|
+
function heredocTarget(prefix, suffix) {
|
|
278
|
+
const target = `${prefix.trim()} ${suffix.trim()}`.trim();
|
|
279
|
+
if (/^(?:.*[/\\])?(?:python3?|py|node)(?:\.exe)?$/i.test(target)) return { kind: "pipe", command: `${target} -` };
|
|
280
|
+
const cat = /^cat(?=\s|[|>]|$)(.*)$/is.exec(target);
|
|
281
|
+
if (cat) {
|
|
282
|
+
const rest = cat[1].trim();
|
|
283
|
+
if (rest === "") return { kind: "stdout" };
|
|
284
|
+
if (rest.startsWith("|")) {
|
|
285
|
+
const command = rest.slice(1).trim();
|
|
286
|
+
return command ? { kind: "pipe", command } : null;
|
|
287
|
+
}
|
|
288
|
+
const redirect = /^(>>?)\s*([^\s>].*)$/s.exec(rest);
|
|
289
|
+
const path = redirect ? redirectPath(redirect[2]) : null;
|
|
290
|
+
return redirect && path !== null ? { kind: "file", append: redirect[1] === ">>", path } : null;
|
|
291
|
+
}
|
|
292
|
+
return /^[\w.:/\\-]+(?:\s|$)/.test(target) ? { kind: "pipe", command: target } : null;
|
|
293
|
+
}
|
|
294
|
+
function heredocStatement(target, text, captured) {
|
|
295
|
+
const { base64, bytes } = base64Bytes(text);
|
|
296
|
+
if (base64.length > MAX_ENCODED_CHARS) return null;
|
|
297
|
+
if (target.kind === "file") {
|
|
298
|
+
const path = `$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(${target.path})`;
|
|
299
|
+
return `[System.IO.File]::${target.append ? "AppendAllText" : "WriteAllText"}(${path}, ${utf8TextExpression(text)}, [System.Text.UTF8Encoding]::new($false))`;
|
|
300
|
+
}
|
|
301
|
+
if (target.kind === "stdout") return captured ? null : `[System.Console]::OpenStandardOutput().Write([System.Convert]::FromBase64String('${base64}'), 0, ${bytes})`;
|
|
302
|
+
const input = text === "" ? "@()" : utf8TextExpression(text.slice(0, -1));
|
|
303
|
+
const [first, rest] = splitFirstPipeline(target.command);
|
|
304
|
+
const cmdlet = CMDLET_WORD_RE.test(first);
|
|
305
|
+
const native = cmdlet ? null : nativeCommandLine(first);
|
|
306
|
+
if (!cmdlet && (native === null || captured)) return null;
|
|
307
|
+
const pipeline = `${input} | ${first}`;
|
|
308
|
+
const statements = native === null ? `${pipeline}; $TgQ = $?` : nativeStdinStatements(native, base64, pipeline);
|
|
309
|
+
return `${inUtf8PipeScope(statements, PYTHON_CALL_RE.test(first.trim()))}${rest}`;
|
|
310
|
+
}
|
|
311
|
+
function heredocText(body, stripTabs) {
|
|
312
|
+
const text = body.replace(/\r?\n$/, "\n");
|
|
313
|
+
return stripTabs ? text.replace(/^\t+/gm, "") : text;
|
|
314
|
+
}
|
|
315
|
+
function insideParentheses(command, layout, index) {
|
|
316
|
+
let depth = 0;
|
|
317
|
+
for (let i = 0; i < index; i++) {
|
|
318
|
+
if (layout.code[i] === 1) depth += command[i] === "(" ? 1 : command[i] === ")" ? -1 : 0;
|
|
319
|
+
}
|
|
320
|
+
return depth > 0;
|
|
321
|
+
}
|
|
322
|
+
function adaptHeredoc(command) {
|
|
323
|
+
const layout = layoutPowerShell(command);
|
|
324
|
+
let result = "";
|
|
325
|
+
let lastIndex = 0;
|
|
326
|
+
let match;
|
|
327
|
+
const heredocs = heredocRe();
|
|
328
|
+
while ((match = heredocs.exec(command)) !== null) {
|
|
329
|
+
const whole = match[0];
|
|
330
|
+
const start = match.index + whole.length - whole.trimStart().length;
|
|
331
|
+
const lineEnd = match.index + whole.length;
|
|
332
|
+
const op = match.index + whole.indexOf("<<");
|
|
333
|
+
const span = layout.code[start] === 1 ? layout.heredocs.get(op) : void 0;
|
|
334
|
+
const text = span !== void 0 && span.bodyStart === lineEnd + (command[lineEnd] === "\r" ? 2 : 1) ? heredocText(command.slice(span.bodyStart, span.termStart), match[2] === "-") : null;
|
|
335
|
+
const target = text !== null && (match[3] !== "" || !/[$`\\]/.test(text)) ? heredocTarget(match[1], match[5] ?? "") : null;
|
|
336
|
+
const statement = text !== null && target !== null ? heredocStatement(target, text, insideParentheses(command, layout, op)) : null;
|
|
337
|
+
if (span === void 0 || statement === null) {
|
|
338
|
+
heredocs.lastIndex = match.index + 1;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
result += `${command.slice(lastIndex, start)}${statement}`;
|
|
342
|
+
lastIndex = span.end;
|
|
343
|
+
heredocs.lastIndex = span.end;
|
|
344
|
+
}
|
|
345
|
+
return result + command.slice(lastIndex);
|
|
346
|
+
}
|
|
347
|
+
function adaptInlinePython(command) {
|
|
348
|
+
const layout = layoutPowerShell(command);
|
|
349
|
+
let result = "";
|
|
350
|
+
let lastIndex = 0;
|
|
351
|
+
let match;
|
|
352
|
+
const inlinePython = inlinePythonRe();
|
|
353
|
+
while ((match = inlinePython.exec(command)) !== null) {
|
|
354
|
+
const matchStart = match.index;
|
|
355
|
+
const matchEnd = inlinePython.lastIndex;
|
|
356
|
+
const pyBin = match[1] ?? "python";
|
|
357
|
+
const preFlags = (match[2] ?? "").trim();
|
|
358
|
+
if (!interpreterOptionsOnly(preFlags)) continue;
|
|
359
|
+
if (!layout.code.subarray(matchStart, matchEnd).every((c) => c === 1)) continue;
|
|
360
|
+
const scriptEnd = layout.stringEnd.get(matchEnd);
|
|
361
|
+
if (scriptEnd === void 0) continue;
|
|
362
|
+
let loaderArgs;
|
|
363
|
+
if (isQuote(SINGLE_QUOTES, command[matchEnd])) {
|
|
364
|
+
const literal = command.slice(matchEnd + 1, scriptEnd - 1).replace(/['\u2018-\u201B](['\u2018-\u201B])/g, "$1");
|
|
365
|
+
const hexArg = `x${Buffer.from(literal, "utf8").toString("hex")}`;
|
|
366
|
+
if (hexArg.length > MAX_ENCODED_CHARS) continue;
|
|
367
|
+
loaderArgs = `${PYTHON_LOADER_ARG} ${hexArg}`;
|
|
368
|
+
} else {
|
|
369
|
+
const expandable = command.slice(matchEnd, scriptEnd);
|
|
370
|
+
loaderArgs = `$(& { param($TgS) $TgH = 'x' + [System.BitConverter]::ToString([System.Text.Encoding]::UTF8.GetBytes($TgS)).Replace('-', ''); if ($TgH.Length -gt ${MAX_ENCODED_CHARS}) { $TgS } else { ${PYTHON_LOADER_ARG}; $TgH } } ${expandable})`;
|
|
371
|
+
}
|
|
372
|
+
const leadMatch = command.slice(matchStart, matchEnd).match(/^\s*/);
|
|
373
|
+
const leadSpace = leadMatch ? leadMatch[0] : "";
|
|
374
|
+
result += `${command.slice(lastIndex, matchStart)}${leadSpace}${pyBin}${preFlags ? ` ${preFlags}` : ""} -c ${loaderArgs}`;
|
|
375
|
+
lastIndex = scriptEnd;
|
|
376
|
+
inlinePython.lastIndex = lastIndex;
|
|
377
|
+
}
|
|
378
|
+
result += command.slice(lastIndex);
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
function adaptPowerShellCommand(command) {
|
|
382
|
+
if (!command) return command;
|
|
383
|
+
let adapted = command;
|
|
384
|
+
if (adapted.includes("<<")) {
|
|
385
|
+
adapted = adaptHeredoc(adapted);
|
|
386
|
+
}
|
|
387
|
+
if (INLINE_PYTHON_PROBE_RE.test(adapted)) {
|
|
388
|
+
adapted = adaptInlinePython(adapted);
|
|
389
|
+
}
|
|
390
|
+
return adapted.length > MAX_ENCODED_CHARS ? command : adapted;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/bash_runner.ts
|
|
394
|
+
var DEFAULT_TIMEOUT_SECONDS = 600;
|
|
395
|
+
var MAX_CAPTURE_BYTES = 32 * 1024 * 1024;
|
|
396
|
+
var MIN_RECORD_STAT_BYTES = 32;
|
|
397
|
+
var DEFAULT_HEARTBEAT_INTERVAL_MS = 15e3;
|
|
398
|
+
var CaptureSink = class {
|
|
399
|
+
constructor(cap) {
|
|
400
|
+
this.cap = cap;
|
|
401
|
+
}
|
|
402
|
+
cap;
|
|
403
|
+
chunks = [];
|
|
404
|
+
bytes = 0;
|
|
405
|
+
overflowed = false;
|
|
406
|
+
push(chunk) {
|
|
407
|
+
if (this.overflowed) return;
|
|
408
|
+
this.bytes += chunk.length;
|
|
409
|
+
if (this.bytes > this.cap) {
|
|
410
|
+
this.overflowed = true;
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
this.chunks.push(chunk);
|
|
414
|
+
}
|
|
415
|
+
text() {
|
|
416
|
+
return decode(Buffer.concat(this.chunks));
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
function scheduleHeartbeat(startTime, intervalMs, write) {
|
|
420
|
+
let interval = intervalMs;
|
|
421
|
+
let timer;
|
|
422
|
+
const arm = () => {
|
|
423
|
+
timer = setTimeout(() => {
|
|
424
|
+
write(Math.round((Date.now() - startTime) / 1e3));
|
|
425
|
+
interval *= 2;
|
|
426
|
+
arm();
|
|
427
|
+
}, interval);
|
|
428
|
+
timer.unref();
|
|
429
|
+
};
|
|
430
|
+
arm();
|
|
431
|
+
return {
|
|
432
|
+
onOutput: () => {
|
|
433
|
+
interval = intervalMs;
|
|
434
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
435
|
+
arm();
|
|
436
|
+
},
|
|
437
|
+
stop: () => {
|
|
438
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function resolveFilter(command, filterName, cwd) {
|
|
443
|
+
const effectiveCwd = cwd ?? process.cwd();
|
|
444
|
+
let split;
|
|
445
|
+
try {
|
|
446
|
+
split = shlexSplit(command);
|
|
447
|
+
} catch {
|
|
448
|
+
split = null;
|
|
449
|
+
}
|
|
450
|
+
const argv = split === null ? [command] : dispatchArgv(split, effectiveCwd).argv;
|
|
451
|
+
if (filterName) {
|
|
452
|
+
const named = filterByName(filterName);
|
|
453
|
+
if (named !== null) return { filter: named, argv };
|
|
454
|
+
const filter = split === null ? null : selectFilter(split, effectiveCwd);
|
|
455
|
+
return { filter, argv, unknownFilter: filterName };
|
|
456
|
+
}
|
|
457
|
+
if (split === null) return { filter: null, argv };
|
|
458
|
+
return { filter: selectFilter(split, effectiveCwd), argv };
|
|
459
|
+
}
|
|
460
|
+
function spawnTarget(command, nativeShell, shellType) {
|
|
461
|
+
if (shellType === "pwsh" || shellType === "powershell") {
|
|
462
|
+
const ps = resolvePowerShell();
|
|
463
|
+
return {
|
|
464
|
+
file: ps,
|
|
465
|
+
args: powerShellCommandArgs(
|
|
466
|
+
`$TgC = $env:TG_CMD; Remove-Item Env:TG_CMD -ErrorAction SilentlyContinue; $global:TgOk = $true; $TgN = [Environment]::NewLine; $TgK = 'if ($global:TgOk = $?) { }'; $TgT = $null; $TgE = $null; $TgA = [System.Management.Automation.Language.Parser]::ParseInput($TgC, [ref]$TgT, [ref]$TgE); $TgX = [System.Collections.Generic.List[object]]::new(); $TgL = [System.Collections.Generic.List[string]]::new(); $TgTop = { param($n) $p = $n.Parent; while ($p -isnot [System.Management.Automation.Language.ScriptBlockAst]) { $p = $p.Parent }; [object]::ReferenceEquals($p, $TgA) }; if ($TgE.Count -eq 0) { if ($TgA.EndBlock.Unnamed) { $TgX.Add([pscustomobject]@{ P = $TgC.Length; O = 0; L = 0; T = ($TgN + $TgK) }) }; foreach ($TgB in @($TgA.BeginBlock, $TgA.ProcessBlock, $TgA.EndBlock, $TgA.CleanBlock)) { if ($TgB -and -not $TgB.Unnamed) { $TgX.Add([pscustomobject]@{ P = ($TgB.Extent.EndOffset - 1); O = 0; L = 0; T = ($TgN + $TgK + $TgN) }) } }; foreach ($TgR in $TgA.FindAll({ param($n) $n -is [System.Management.Automation.Language.ReturnStatementAst] -and (& $TgTop $n) }, $true)) { if ($TgR.Pipeline -and $TgR.Pipeline.GetPureExpression()) { $TgX.Add([pscustomobject]@{ P = $TgR.Extent.StartOffset; O = 1; L = 6; T = '$TgW = (' }); $TgX.Add([pscustomobject]@{ P = $TgR.Extent.EndOffset; O = 1; L = 0; T = '), $?; $global:TgOk = $TgW[1]; return $TgW[0]' }) } else { $TgX.Add([pscustomobject]@{ P = $TgR.Extent.StartOffset; O = 1; L = 6; T = 'return $(' }); $TgX.Add([pscustomobject]@{ P = $TgR.Extent.EndOffset; O = 1; L = 0; T = $(if ($TgR.Pipeline) { '; ' + $TgK + ')' } else { $TgK + ')' }) }) } }; $TgD = @($TgA.FindAll({ param($n) $n -is [System.Management.Automation.Language.LabeledStatementAst] -and $n.Label }, $true) | ForEach-Object { $_.Label } | Select-Object -Unique); foreach ($TgR in $TgA.FindAll({ param($n) ($n -is [System.Management.Automation.Language.BreakStatementAst] -or $n -is [System.Management.Automation.Language.ContinueStatementAst]) -and (& $TgTop $n) }, $true)) { $TgX.Add([pscustomobject]@{ P = $TgR.Extent.StartOffset; O = 0; L = 0; T = ($TgK + '; ') }); if ($TgR.Label -is [System.Management.Automation.Language.StringConstantExpressionAst]) { if ($TgR.Label.Value -eq '') { } elseif ($TgD -contains $TgR.Label.Value) { $TgL.Add($TgR.Label.Value) } else { $TgX.Add([pscustomobject]@{ P = $TgR.Label.Extent.StartOffset; O = 1; L = $TgR.Label.Extent.Text.Length; T = 'TgEsc' }); $TgL.Add('TgEsc') } } elseif ($TgR.Label) { $TgX.Add([pscustomobject]@{ P = $TgR.Label.Extent.StartOffset; O = 1; L = $TgR.Label.Extent.Text.Length; T = ('$(if ((-not [string]($TgV = (' + $TgR.Label.Extent.Text + '))) -or (@(' + (($TgD | ForEach-Object { "'" + $_ + "'" }) -join ',') + ') -contains $TgV)) { $TgV } else { ''TgEsc'' }; if (-not $global:TgOk) { Microsoft.PowerShell.Utility\\Write-Error TgRestore -ErrorAction Ignore })') }); foreach ($TgI in $TgD) { $TgL.Add($TgI) }; $TgL.Add('TgEsc') } }; foreach ($TgR in $TgA.FindAll({ param($n) $n -is [System.Management.Automation.Language.TryStatementAst] -and $n.Finally -and $n.Finally.Statements.Count -gt 0 -and (& $TgTop $n) }, $true)) { $TgX.Add([pscustomobject]@{ P = ($TgR.Finally.Extent.EndOffset - 1); O = 0; L = 0; T = ($TgN + $TgK + $TgN) }) } } else { throw [System.Management.Automation.ParseException]::new($TgE) }; $TgS = $TgC; foreach ($TgI in ($TgX | Sort-Object -Property @{ Expression = 'P'; Descending = $true }, @{ Expression = 'O'; Descending = $false })) { $TgS = $TgS.Substring(0, $TgI.P) + $TgI.T + $TgS.Substring($TgI.P + $TgI.L) }; $TgO = $(if ($PSVersionTable.PSVersion.Major -lt 6) { 'do { @() | & ([scriptblock]::Create($TgS)) } while ($false)' } else { 'do { & ([scriptblock]::Create($TgS)) } while ($false)' }); foreach ($TgI in ($TgL | Select-Object -Unique)) { $TgO = ':' + $TgI + ' do { ' + $TgO + ' } while ($false)' }; . ([scriptblock]::Create($TgO)); if ($global:TgOk) { exit 0 } else { exit 1 }`
|
|
467
|
+
),
|
|
468
|
+
shell: false,
|
|
469
|
+
cmdEnv: { TG_CMD: adaptPowerShellCommand(command) }
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
const shell = nativeShell || shellType === "native" ? true : wrappedShell();
|
|
473
|
+
if (typeof shell === "string") return { file: shell, args: ["-c", 'eval "$TG_CMD"'], shell: false, cmdEnv: { TG_CMD: command } };
|
|
474
|
+
return { file: command, args: [], shell };
|
|
475
|
+
}
|
|
476
|
+
function mergeSpawnEnv(env, cmdEnv) {
|
|
477
|
+
return cmdEnv ? { ...env ?? process.env, ...cmdEnv } : env;
|
|
478
|
+
}
|
|
479
|
+
function signalExitCode(signal) {
|
|
480
|
+
const num = os.constants.signals[signal];
|
|
481
|
+
return 128 + (num ?? 0);
|
|
482
|
+
}
|
|
483
|
+
function resolveProfile(explicit) {
|
|
484
|
+
if (explicit) return explicit;
|
|
485
|
+
try {
|
|
486
|
+
const p = loadConfig().compression.profile;
|
|
487
|
+
return p && p !== "auto" ? p : "balanced";
|
|
488
|
+
} catch {
|
|
489
|
+
return "balanced";
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function resolveCompressLimits() {
|
|
493
|
+
try {
|
|
494
|
+
const bc = loadConfig().bash_compress;
|
|
495
|
+
return { maxLines: bc.max_lines, maxBytes: bc.max_bytes };
|
|
496
|
+
} catch {
|
|
497
|
+
return { maxLines: 1e3, maxBytes: 64 * 1024 };
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function unknownFilterNotice(name, fallback) {
|
|
501
|
+
const typed = name.toLowerCase();
|
|
502
|
+
const names = ["generic", "passthrough", ...new Set(TOOL_FILTERS.map((f) => f.name))];
|
|
503
|
+
const near = names.filter((n) => n.split("-").some((word) => word.startsWith(typed)) || typed.startsWith(n)).slice(0, 5);
|
|
504
|
+
const instead = fallback === null ? "running it uncompressed" : `auto-detected ${fallback.name}`;
|
|
505
|
+
const hint = near.length > 0 ? `; did you mean ${near.join(", ")}?` : "";
|
|
506
|
+
return `[token-goat: no filter named ${echoedValue(name)}, ${instead}${hint}]
|
|
507
|
+
`;
|
|
508
|
+
}
|
|
509
|
+
async function run(command, opts = {}) {
|
|
510
|
+
const timeout = opts.timeout ?? DEFAULT_TIMEOUT_SECONDS;
|
|
511
|
+
const { filter, argv, unknownFilter } = resolveFilter(command, opts.filterName, opts.cwd);
|
|
512
|
+
if (unknownFilter !== void 0) (opts.writeStderr ?? ((s) => process.stderr.write(s)))(unknownFilterNotice(unknownFilter, filter));
|
|
513
|
+
if (filter === null) {
|
|
514
|
+
if ((opts.maxTokens ?? 0) > 0 || opts.quietSuccess) {
|
|
515
|
+
return wrapAndCompress(command, argv, new PassthroughFilter(), timeout, resolveProfile(opts.compressionProfile), opts);
|
|
516
|
+
}
|
|
517
|
+
return passthrough(command, timeout, opts.cwd, opts.env, opts.nativeShell, opts.shellType, opts.rawStdin);
|
|
518
|
+
}
|
|
519
|
+
return wrapAndCompress(command, argv, filter, timeout, resolveProfile(opts.compressionProfile), opts);
|
|
520
|
+
}
|
|
521
|
+
function childStdin(rawStdin, forward) {
|
|
522
|
+
return rawStdin === true ? forward : "ignore";
|
|
523
|
+
}
|
|
524
|
+
function runRaw(command, timeout = DEFAULT_TIMEOUT_SECONDS, nativeShell, shellType, rawStdin) {
|
|
525
|
+
return passthrough(command, timeout, void 0, void 0, nativeShell, shellType, rawStdin);
|
|
526
|
+
}
|
|
527
|
+
async function passthrough(command, timeout, cwd, env, nativeShell, shellType, rawStdin) {
|
|
528
|
+
const { file, args, shell, cmdEnv } = spawnTarget(command, nativeShell, shellType);
|
|
529
|
+
const owned = spawnOwned(file, args, { cwd, shell, env: mergeSpawnEnv(env, cmdEnv), stdio: [childStdin(rawStdin, "inherit"), "inherit", "inherit"] });
|
|
530
|
+
let timedOut = false;
|
|
531
|
+
const timeoutHandle = setTimeout(() => {
|
|
532
|
+
timedOut = true;
|
|
533
|
+
owned.kill();
|
|
534
|
+
}, timeout * 1e3);
|
|
535
|
+
const result = await new Promise((resolve) => {
|
|
536
|
+
owned.child.once("error", (error) => resolve({ code: null, signal: null, error }));
|
|
537
|
+
owned.child.once("close", (code, signal) => resolve({ code, signal }));
|
|
538
|
+
});
|
|
539
|
+
clearTimeout(timeoutHandle);
|
|
540
|
+
owned.release();
|
|
541
|
+
if (timedOut) return 124;
|
|
542
|
+
if (result.error) {
|
|
543
|
+
process.stderr.write(`[token-goat: ${displaySafeText(result.error.message)}]
|
|
544
|
+
`);
|
|
545
|
+
return 127;
|
|
546
|
+
}
|
|
547
|
+
if (result.code !== null) return result.code;
|
|
548
|
+
if (result.signal) return signalExitCode(result.signal);
|
|
549
|
+
return 0;
|
|
550
|
+
}
|
|
551
|
+
function decode(buf) {
|
|
552
|
+
if (buf == null) return "";
|
|
553
|
+
return typeof buf === "string" ? buf : buf.toString("utf8");
|
|
554
|
+
}
|
|
555
|
+
async function wrapAndCompress(command, argv, filter, timeout, profile, opts) {
|
|
556
|
+
const writeStdout = opts.writeStdout ?? ((s) => process.stdout.write(s));
|
|
557
|
+
const writeStderr = opts.writeStderr ?? ((s) => process.stderr.write(s));
|
|
558
|
+
const startTime = Date.now();
|
|
559
|
+
const { file, args, shell, cmdEnv } = spawnTarget(command, opts.nativeShell, opts.shellType);
|
|
560
|
+
const stdoutSink = new CaptureSink(MAX_CAPTURE_BYTES);
|
|
561
|
+
const stderrSink = new CaptureSink(MAX_CAPTURE_BYTES);
|
|
562
|
+
let timedOut = false;
|
|
563
|
+
let overflowed = false;
|
|
564
|
+
const owned = spawnOwned(file, args, { cwd: opts.cwd, shell, env: mergeSpawnEnv(opts.env, cmdEnv), stdio: [childStdin(opts.rawStdin, "pipe"), "pipe", "pipe"] });
|
|
565
|
+
const child = owned.child;
|
|
566
|
+
if (child.stdin) {
|
|
567
|
+
child.stdin.on("error", () => {
|
|
568
|
+
});
|
|
569
|
+
process.stdin.on("error", () => {
|
|
570
|
+
});
|
|
571
|
+
process.stdin.pipe(child.stdin);
|
|
572
|
+
}
|
|
573
|
+
const heartbeat = scheduleHeartbeat(startTime, opts.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS, (elapsed) => {
|
|
574
|
+
writeStderr(`[token-goat compress] still running, ${elapsed}s elapsed
|
|
575
|
+
`);
|
|
576
|
+
});
|
|
577
|
+
const timeoutHandle = setTimeout(() => {
|
|
578
|
+
timedOut = true;
|
|
579
|
+
owned.kill();
|
|
580
|
+
const stopReading = () => {
|
|
581
|
+
child.stdout?.destroy();
|
|
582
|
+
child.stderr?.destroy();
|
|
583
|
+
};
|
|
584
|
+
if (child.exitCode !== null || child.signalCode !== null) stopReading();
|
|
585
|
+
else child.once("exit", stopReading);
|
|
586
|
+
}, timeout * 1e3);
|
|
587
|
+
const onData = (sink) => (chunk) => {
|
|
588
|
+
heartbeat.onOutput();
|
|
589
|
+
sink.push(chunk);
|
|
590
|
+
if (sink.overflowed) {
|
|
591
|
+
overflowed = true;
|
|
592
|
+
owned.kill();
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
child.stdout?.on("data", onData(stdoutSink));
|
|
596
|
+
child.stderr?.on("data", onData(stderrSink));
|
|
597
|
+
let spawnError = null;
|
|
598
|
+
const { code, signal } = await new Promise((resolve) => {
|
|
599
|
+
child.on("error", (e) => {
|
|
600
|
+
spawnError = e;
|
|
601
|
+
resolve({ code: 127, signal: null });
|
|
602
|
+
});
|
|
603
|
+
child.on("close", (code2, signal2) => resolve({ code: code2, signal: signal2 }));
|
|
604
|
+
});
|
|
605
|
+
heartbeat.stop();
|
|
606
|
+
clearTimeout(timeoutHandle);
|
|
607
|
+
owned.release();
|
|
608
|
+
if (child.stdin) {
|
|
609
|
+
process.stdin.unpipe(child.stdin);
|
|
610
|
+
process.stdin.pause();
|
|
611
|
+
}
|
|
612
|
+
let stdoutText = stdoutSink.text();
|
|
613
|
+
let stderrText = stderrSink.text();
|
|
614
|
+
if (overflowed) {
|
|
615
|
+
stdoutText += `
|
|
616
|
+
[token-goat: capture capped at ${MAX_CAPTURE_BYTES / (1024 * 1024)} MiB]`;
|
|
617
|
+
}
|
|
618
|
+
if (spawnError !== null) stderrText = (stderrText ? stderrText + "\n" : "") + `[token-goat: ${displaySafeText(spawnError.message)}]`;
|
|
619
|
+
let exitCode;
|
|
620
|
+
if (timedOut) {
|
|
621
|
+
exitCode = 124;
|
|
622
|
+
stderrText = (stderrText ? stderrText + "\n" : "") + `[token-goat: command exceeded ${timeout}s timeout and was killed]`;
|
|
623
|
+
} else if (overflowed && (signal === "SIGKILL" || process.platform === "win32")) {
|
|
624
|
+
exitCode = signalExitCode("SIGTERM");
|
|
625
|
+
} else if (code !== null) {
|
|
626
|
+
exitCode = code;
|
|
627
|
+
} else if (signal) {
|
|
628
|
+
exitCode = signalExitCode(signal);
|
|
629
|
+
} else {
|
|
630
|
+
exitCode = 0;
|
|
631
|
+
}
|
|
632
|
+
if (opts.quietSuccess && exitCode === 0) {
|
|
633
|
+
const duration = ((Date.now() - startTime) / 1e3).toFixed(1) + "s";
|
|
634
|
+
const fullRaw = (stdoutText + (stderrText ? "\n" + stderrText : "")).trim();
|
|
635
|
+
const id = storeBashOutputSync(command, fullRaw, 0, opts.cwd ?? null);
|
|
636
|
+
const summary = `[tg: ok] ${command} (${duration}, recall: token-goat bash-output ${id})
|
|
637
|
+
`;
|
|
638
|
+
writeStdout(summary);
|
|
639
|
+
return 0;
|
|
640
|
+
}
|
|
641
|
+
const limits = resolveCompressLimits();
|
|
642
|
+
const { applied, marker, compressed, text: delivered } = deliverCompressed(filter, stdoutText, stderrText, exitCode, argv, {
|
|
643
|
+
compressionProfile: profile,
|
|
644
|
+
maxLines: limits.maxLines,
|
|
645
|
+
maxBytes: limits.maxBytes
|
|
646
|
+
});
|
|
647
|
+
let text = delivered;
|
|
648
|
+
const maxTokens = opts.maxTokens ?? 0;
|
|
649
|
+
if (maxTokens > 0) text = capTokens(text, maxTokens);
|
|
650
|
+
const capped = text !== delivered;
|
|
651
|
+
let body = text + marker;
|
|
652
|
+
if (applied || capped) {
|
|
653
|
+
const fullRaw = (stdoutText + (stderrText ? "\n" + stderrText : "")).trim();
|
|
654
|
+
const recallId = storeBashOutputSync(command, fullRaw, exitCode, opts.cwd ?? null);
|
|
655
|
+
body += "\n[token-goat] full output: bash-output " + recallId + " --full";
|
|
656
|
+
if (opts.capHint) body += "\n[token-goat] " + opts.capHint;
|
|
657
|
+
}
|
|
658
|
+
writeStdout(body.endsWith("\n") ? body : body + "\n");
|
|
659
|
+
if (applied) recordSavings(compressed);
|
|
660
|
+
return exitCode;
|
|
661
|
+
}
|
|
662
|
+
function recordSavings(result) {
|
|
663
|
+
const delivered = deliveredOutputBytes(result.originalBytes);
|
|
664
|
+
const bytesSaved = Math.max(0, delivered - result.compressedBytes);
|
|
665
|
+
if (bytesSaved < MIN_RECORD_STAT_BYTES) return;
|
|
666
|
+
recordStat(`bash_compress:${result.filterName}`, bytesSaved, compressedTokensSaved(bytesSaved));
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
export {
|
|
670
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
671
|
+
MAX_CAPTURE_BYTES,
|
|
672
|
+
DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
673
|
+
resolveFilter,
|
|
674
|
+
spawnTarget,
|
|
675
|
+
unknownFilterNotice,
|
|
676
|
+
run,
|
|
677
|
+
runRaw
|
|
678
|
+
};
|