gong-code 0.1.0
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 +38 -0
- package/dist/SKILL-22jccbka.md +1 -0
- package/dist/chunk-01wdn84e.js +8 -0
- package/dist/chunk-04bc74vz.js +243 -0
- package/dist/chunk-05t2vqsb.js +490 -0
- package/dist/chunk-0727wret.js +159 -0
- package/dist/chunk-07rervty.js +200 -0
- package/dist/chunk-0b9nxvyg.js +478 -0
- package/dist/chunk-0e428b73.js +600 -0
- package/dist/chunk-0hqfheht.js +3439 -0
- package/dist/chunk-0pnk52c2.js +116 -0
- package/dist/chunk-0sbddf6m.js +68 -0
- package/dist/chunk-0vf7xb84.js +176 -0
- package/dist/chunk-0yz17yjy.js +1061 -0
- package/dist/chunk-0zscb6c7.js +10 -0
- package/dist/chunk-10p21kq6.js +378 -0
- package/dist/chunk-17k78pmz.js +120 -0
- package/dist/chunk-19jvvqzp.js +1390 -0
- package/dist/chunk-1d5czqnr.js +173 -0
- package/dist/chunk-1gjysfnf.js +13876 -0
- package/dist/chunk-1jjfv0wd.js +467 -0
- package/dist/chunk-1jjzp2pa.js +617 -0
- package/dist/chunk-1m38dj2k.js +155 -0
- package/dist/chunk-1pgttway.js +211 -0
- package/dist/chunk-1vvnx0gb.js +8035 -0
- package/dist/chunk-1ztm9yps.js +49145 -0
- package/dist/chunk-208x1t0m.js +88 -0
- package/dist/chunk-20xqs3yk.js +843 -0
- package/dist/chunk-2b2c0wnn.js +584 -0
- package/dist/chunk-2g07117j.js +541 -0
- package/dist/chunk-2g6p2t3w.js +780 -0
- package/dist/chunk-2kjeaeg7.js +28 -0
- package/dist/chunk-2n0s2dhc.js +12 -0
- package/dist/chunk-2p0hvt8k.js +785 -0
- package/dist/chunk-2qmxg9y7.js +40174 -0
- package/dist/chunk-2qpy8kne.js +28 -0
- package/dist/chunk-2rgbf62a.js +40 -0
- package/dist/chunk-2tx0s41y.js +150 -0
- package/dist/chunk-2x8zk5cz.js +101 -0
- package/dist/chunk-2yqy25z4.js +48 -0
- package/dist/chunk-318g78ty.js +552 -0
- package/dist/chunk-31gfg8tz.js +290 -0
- package/dist/chunk-32n0s532.js +1317 -0
- package/dist/chunk-35dw2r9g.js +137 -0
- package/dist/chunk-36b5zqvn.js +1865 -0
- package/dist/chunk-38kpx69j.js +97 -0
- package/dist/chunk-3b01vp8p.js +815 -0
- package/dist/chunk-3f76a1ek.js +317 -0
- package/dist/chunk-3fd1hkhh.js +4254 -0
- package/dist/chunk-3ffxa7zj.js +8 -0
- package/dist/chunk-3gcj7w95.js +8 -0
- package/dist/chunk-3jf3k40v.js +970 -0
- package/dist/chunk-3k51qfp9.js +9301 -0
- package/dist/chunk-3mhygd2v.js +1180 -0
- package/dist/chunk-3p6y0gvb.js +117 -0
- package/dist/chunk-3r60xdt5.js +93 -0
- package/dist/chunk-43agf6xv.js +8 -0
- package/dist/chunk-46zr418r.js +15 -0
- package/dist/chunk-48k2bs4w.js +118 -0
- package/dist/chunk-4p987a4z.js +84 -0
- package/dist/chunk-4px25pe0.js +776 -0
- package/dist/chunk-4s93jyky.js +113 -0
- package/dist/chunk-4xv8bedf.js +58 -0
- package/dist/chunk-4y382mzd.js +346 -0
- package/dist/chunk-52dzw4bc.js +246 -0
- package/dist/chunk-52qqmh5g.js +110 -0
- package/dist/chunk-53grnhp7.js +8 -0
- package/dist/chunk-59craaxx.js +4249 -0
- package/dist/chunk-5b833jqs.js +96 -0
- package/dist/chunk-5cp2q9sh.js +58 -0
- package/dist/chunk-5dbaxmts.js +87 -0
- package/dist/chunk-5f5sxgp0.js +96 -0
- package/dist/chunk-5mkfderj.js +195 -0
- package/dist/chunk-5qw0mpvq.js +389 -0
- package/dist/chunk-5rxmszm8.js +48 -0
- package/dist/chunk-5ygxa3hr.js +65 -0
- package/dist/chunk-61d5dqq2.js +71 -0
- package/dist/chunk-6350958y.js +272 -0
- package/dist/chunk-65z8hg8j.js +1337 -0
- package/dist/chunk-6aq85gdq.js +8 -0
- package/dist/chunk-6cjh9exg.js +40751 -0
- package/dist/chunk-6e41p5m6.js +187 -0
- package/dist/chunk-6jb6xcnq.js +10511 -0
- package/dist/chunk-6jta29r1.js +20 -0
- package/dist/chunk-6q7f2rrv.js +4757 -0
- package/dist/chunk-6sg0ec6v.js +41 -0
- package/dist/chunk-6v0rhx4e.js +328 -0
- package/dist/chunk-6x5mz95v.js +751 -0
- package/dist/chunk-6xqp51qr.js +87 -0
- package/dist/chunk-6xvyx1s9.js +6957 -0
- package/dist/chunk-6ywh7wgp.js +468 -0
- package/dist/chunk-75ne77gm.js +1920 -0
- package/dist/chunk-77cb06js.js +602 -0
- package/dist/chunk-78n9kfp8.js +1645 -0
- package/dist/chunk-7be87rww.js +277 -0
- package/dist/chunk-7e1qjk3s.js +125 -0
- package/dist/chunk-7e7nxnss.js +360 -0
- package/dist/chunk-7g8156qp.js +55 -0
- package/dist/chunk-7nz71s42.js +3353 -0
- package/dist/chunk-7r078t2x.js +113 -0
- package/dist/chunk-7shx41hj.js +3094 -0
- package/dist/chunk-7yexhdjs.js +216 -0
- package/dist/chunk-83kmfcjv.js +256 -0
- package/dist/chunk-846rr9n1.js +107 -0
- package/dist/chunk-84ncsm1r.js +183 -0
- package/dist/chunk-867x8aq0.js +8936 -0
- package/dist/chunk-8byh30kb.js +1947 -0
- package/dist/chunk-8c8f6gnt.js +432 -0
- package/dist/chunk-8gtdseev.js +454 -0
- package/dist/chunk-8jfh9rq6.js +642 -0
- package/dist/chunk-8n4n4m67.js +800 -0
- package/dist/chunk-8za61tze.js +847 -0
- package/dist/chunk-9029sbq1.js +57 -0
- package/dist/chunk-94dm5rrm.js +673 -0
- package/dist/chunk-9bs8n985.js +329 -0
- package/dist/chunk-9c23776j.js +763 -0
- package/dist/chunk-9e93g81n.js +298 -0
- package/dist/chunk-9f3d61y7.js +548 -0
- package/dist/chunk-9gc3andd.js +422 -0
- package/dist/chunk-9kn3tgpb.js +130 -0
- package/dist/chunk-9m5rn7hk.js +61 -0
- package/dist/chunk-9p4hsd6a.js +50 -0
- package/dist/chunk-9qasfk8n.js +758 -0
- package/dist/chunk-9snp5mn2.js +533 -0
- package/dist/chunk-9tcvras3.js +2331 -0
- package/dist/chunk-a0186ngk.js +57 -0
- package/dist/chunk-a2m8f0aj.js +268 -0
- package/dist/chunk-a2rcafav.js +716 -0
- package/dist/chunk-a9hjdzar.js +160 -0
- package/dist/chunk-aak6ts5n.js +526 -0
- package/dist/chunk-aca3w84n.js +267 -0
- package/dist/chunk-ae76ded0.js +30 -0
- package/dist/chunk-afnjhn3j.js +21933 -0
- package/dist/chunk-ahj70tf0.js +880 -0
- package/dist/chunk-apjsxssq.js +169 -0
- package/dist/chunk-av49wjj1.js +43 -0
- package/dist/chunk-axggebfy.js +206 -0
- package/dist/chunk-az7jpbv8.js +16 -0
- package/dist/chunk-azz7sep6.js +328 -0
- package/dist/chunk-b4tpz2g1.js +93 -0
- package/dist/chunk-bczf2eyq.js +115 -0
- package/dist/chunk-bd95f637.js +208 -0
- package/dist/chunk-bdhdmkya.js +10 -0
- package/dist/chunk-bp0ynk17.js +32 -0
- package/dist/chunk-bq75gxjs.js +90 -0
- package/dist/chunk-btk5jaq6.js +10464 -0
- package/dist/chunk-bv4c48a5.js +120 -0
- package/dist/chunk-byfb105n.js +341 -0
- package/dist/chunk-c0bdmy7w.js +4223 -0
- package/dist/chunk-c20aqkz8.js +641 -0
- package/dist/chunk-c4jg3s2c.js +163 -0
- package/dist/chunk-c61q5cer.js +258 -0
- package/dist/chunk-c9f761sy.js +386 -0
- package/dist/chunk-ca6jkz06.js +157 -0
- package/dist/chunk-ccyg9ap9.js +171374 -0
- package/dist/chunk-cfej8cc7.js +371 -0
- package/dist/chunk-cjqxdnf9.js +23 -0
- package/dist/chunk-ckrs789w.js +8 -0
- package/dist/chunk-cqeq13wg.js +48 -0
- package/dist/chunk-crfj3w5e.js +22821 -0
- package/dist/chunk-ct5x6z7g.js +8066 -0
- package/dist/chunk-cv5h8n8c.js +101 -0
- package/dist/chunk-cwe8h159.js +1563 -0
- package/dist/chunk-cwm234zz.js +908 -0
- package/dist/chunk-d1jkfp45.js +1580 -0
- package/dist/chunk-d3hb1qdr.js +126 -0
- package/dist/chunk-d70d0zer.js +713 -0
- package/dist/chunk-d91tx29f.js +105 -0
- package/dist/chunk-d952pymh.js +20 -0
- package/dist/chunk-d9af3z8k.js +720 -0
- package/dist/chunk-da1n53c2.js +47 -0
- package/dist/chunk-df5c1x8m.js +732 -0
- package/dist/chunk-dkv64xys.js +71 -0
- package/dist/chunk-dm7166v6.js +251 -0
- package/dist/chunk-dr0y5a61.js +258 -0
- package/dist/chunk-drzb0rrd.js +306 -0
- package/dist/chunk-dw1v1q7w.js +37 -0
- package/dist/chunk-e224qet4.js +348 -0
- package/dist/chunk-e50ckbv0.js +1099 -0
- package/dist/chunk-e5gn6by6.js +1165 -0
- package/dist/chunk-e7m3f74m.js +37 -0
- package/dist/chunk-e8ghgwaj.js +295 -0
- package/dist/chunk-ed8k4ya2.js +41 -0
- package/dist/chunk-eh0vnqdz.js +76 -0
- package/dist/chunk-epwrzr35.js +4301 -0
- package/dist/chunk-er9h6sw5.js +5379 -0
- package/dist/chunk-eta5aekm.js +79 -0
- package/dist/chunk-ev5gxscq.js +63 -0
- package/dist/chunk-evp72hv5.js +212 -0
- package/dist/chunk-ex41stfq.js +127 -0
- package/dist/chunk-ezpa510h.js +62 -0
- package/dist/chunk-f0fm6dey.js +477 -0
- package/dist/chunk-f239sbhc.js +152 -0
- package/dist/chunk-f2bj47xf.js +109 -0
- package/dist/chunk-f4t24rq4.js +258 -0
- package/dist/chunk-f9ghvzy8.js +106 -0
- package/dist/chunk-f9rahzgr.js +99 -0
- package/dist/chunk-ff75qzty.js +521 -0
- package/dist/chunk-fj0ctt6q.js +264 -0
- package/dist/chunk-fj7wxdt3.js +221 -0
- package/dist/chunk-fnnpqv92.js +714 -0
- package/dist/chunk-fpd3zzx1.js +26696 -0
- package/dist/chunk-fqv2cn76.js +227 -0
- package/dist/chunk-fqw9j3bj.js +439 -0
- package/dist/chunk-frbcqhz0.js +4984 -0
- package/dist/chunk-fzkj59sg.js +174 -0
- package/dist/chunk-fzpzbpbw.js +248 -0
- package/dist/chunk-g52cpmd1.js +686 -0
- package/dist/chunk-g98m2qe6.js +31 -0
- package/dist/chunk-g9zgq4vy.js +715 -0
- package/dist/chunk-gbpt7tm8.js +687 -0
- package/dist/chunk-gg52jka8.js +373 -0
- package/dist/chunk-gjttphax.js +5793 -0
- package/dist/chunk-gs7vjaas.js +65 -0
- package/dist/chunk-gv8n85j2.js +8 -0
- package/dist/chunk-gxdwm6pt.js +650 -0
- package/dist/chunk-gy8wajg2.js +1014 -0
- package/dist/chunk-gz3647m0.js +200 -0
- package/dist/chunk-gzg07mqh.js +16 -0
- package/dist/chunk-h3zc60tq.js +44 -0
- package/dist/chunk-h4p1qt16.js +74 -0
- package/dist/chunk-h8219f18.js +282 -0
- package/dist/chunk-h9agq92v.js +331 -0
- package/dist/chunk-hhjtpwbj.js +16 -0
- package/dist/chunk-hpgpwzra.js +548 -0
- package/dist/chunk-hsnjvn19.js +80 -0
- package/dist/chunk-hsp9qrry.js +417 -0
- package/dist/chunk-hwz6aq2m.js +3939 -0
- package/dist/chunk-hzph0hbh.js +272 -0
- package/dist/chunk-hzqkss6v.js +153 -0
- package/dist/chunk-j0t31f39.js +1149 -0
- package/dist/chunk-j1qm6n1v.js +30 -0
- package/dist/chunk-j6e4b522.js +81 -0
- package/dist/chunk-j7qfbvxk.js +84 -0
- package/dist/chunk-j7qv3hvm.js +1635 -0
- package/dist/chunk-j8mxc6k6.js +213 -0
- package/dist/chunk-jevj87jn.js +224 -0
- package/dist/chunk-jk2ps88q.js +815 -0
- package/dist/chunk-jz7tbdyv.js +1599 -0
- package/dist/chunk-k36jpjvh.js +300 -0
- package/dist/chunk-k4nmrt3w.js +61 -0
- package/dist/chunk-kavn03r9.js +16606 -0
- package/dist/chunk-kkgav8dm.js +54 -0
- package/dist/chunk-kkq26g5c.js +33 -0
- package/dist/chunk-kp6nepx4.js +106 -0
- package/dist/chunk-ksefybc8.js +1551 -0
- package/dist/chunk-kv147680.js +3436 -0
- package/dist/chunk-kya46axt.js +833 -0
- package/dist/chunk-kyz233ny.js +227 -0
- package/dist/chunk-kzs75xmj.js +143 -0
- package/dist/chunk-m0vrez9w.js +266 -0
- package/dist/chunk-m1wadav3.js +118 -0
- package/dist/chunk-m4rfx3cj.js +221 -0
- package/dist/chunk-mc9zaggs.js +6374 -0
- package/dist/chunk-mcg5ttj4.js +105 -0
- package/dist/chunk-mf4r7918.js +63 -0
- package/dist/chunk-mh9khrt4.js +286 -0
- package/dist/chunk-mhc4szw7.js +22 -0
- package/dist/chunk-mkq0yzp0.js +38 -0
- package/dist/chunk-mr58jv4w.js +256 -0
- package/dist/chunk-mrrx3bmt.js +168 -0
- package/dist/chunk-mtyk3zz6.js +87 -0
- package/dist/chunk-mvgxntv6.js +62 -0
- package/dist/chunk-mwnjydcm.js +102 -0
- package/dist/chunk-mwxt0m3f.js +862 -0
- package/dist/chunk-mz1pxck8.js +998 -0
- package/dist/chunk-n3agghys.js +214 -0
- package/dist/chunk-n6q02ya9.js +442 -0
- package/dist/chunk-nt3y91vh.js +443 -0
- package/dist/chunk-nt837qt9.js +21 -0
- package/dist/chunk-ntgmegfs.js +39 -0
- package/dist/chunk-nwk16bbd.js +30 -0
- package/dist/chunk-p367ay90.js +672 -0
- package/dist/chunk-p7m3x9qk.js +48 -0
- package/dist/chunk-pagmjwp5.js +132 -0
- package/dist/chunk-pbd4r8ek.js +161 -0
- package/dist/chunk-pdkpssgv.js +2422 -0
- package/dist/chunk-pf6z1e35.js +32 -0
- package/dist/chunk-pfzejvpt.js +213 -0
- package/dist/chunk-pktxyhvk.js +400 -0
- package/dist/chunk-pp5xhveq.js +124 -0
- package/dist/chunk-ppnd7a8x.js +38 -0
- package/dist/chunk-pqh2w7dr.js +2202 -0
- package/dist/chunk-pr878d0z.js +240 -0
- package/dist/chunk-psf0y7hy.js +140 -0
- package/dist/chunk-pz2zz2f7.js +1586 -0
- package/dist/chunk-q0xkrxy8.js +890 -0
- package/dist/chunk-q5b0kpr3.js +14369 -0
- package/dist/chunk-q7z3t531.js +334 -0
- package/dist/chunk-qdjjtgwt.js +3183 -0
- package/dist/chunk-qezv7msv.js +26 -0
- package/dist/chunk-qmxn7f86.js +2643 -0
- package/dist/chunk-qqc3b97c.js +15 -0
- package/dist/chunk-qwrp63wh.js +145 -0
- package/dist/chunk-qyvqbst3.js +537 -0
- package/dist/chunk-qztvd4hd.js +465 -0
- package/dist/chunk-r0bvez4y.js +235 -0
- package/dist/chunk-r3z0896k.js +2831 -0
- package/dist/chunk-r4cxb6t7.js +32 -0
- package/dist/chunk-rb8k68m7.js +118 -0
- package/dist/chunk-rdprgm4t.js +42 -0
- package/dist/chunk-rex82hys.js +1710 -0
- package/dist/chunk-rjjakkw2.js +2560 -0
- package/dist/chunk-rka6gcv3.js +304 -0
- package/dist/chunk-rkz12ghm.js +144 -0
- package/dist/chunk-rnc7m1qs.js +436 -0
- package/dist/chunk-rx6z23h0.js +165 -0
- package/dist/chunk-s76582j1.js +28 -0
- package/dist/chunk-sez03qd9.js +7140 -0
- package/dist/chunk-sjvdn2ep.js +130 -0
- package/dist/chunk-smwhyy8p.js +115 -0
- package/dist/chunk-snw7nh0d.js +695 -0
- package/dist/chunk-sqx9c057.js +193 -0
- package/dist/chunk-sscms68s.js +16 -0
- package/dist/chunk-stknnmsb.js +272 -0
- package/dist/chunk-sywdxbs6.js +853 -0
- package/dist/chunk-syxy4pf2.js +782 -0
- package/dist/chunk-t0hpqsqn.js +6157 -0
- package/dist/chunk-tjm70vnw.js +123 -0
- package/dist/chunk-trzh0msn.js +154 -0
- package/dist/chunk-v1z17cpg.js +723 -0
- package/dist/chunk-vsfj449x.js +333 -0
- package/dist/chunk-vvkq20tt.js +287 -0
- package/dist/chunk-vxgdzx6p.js +338 -0
- package/dist/chunk-vxqhjyfz.js +15 -0
- package/dist/chunk-vzsstfmb.js +80 -0
- package/dist/chunk-w1jew5sn.js +69 -0
- package/dist/chunk-w3kwr76v.js +789 -0
- package/dist/chunk-w40netr9.js +59 -0
- package/dist/chunk-w99cpfdg.js +559 -0
- package/dist/chunk-wacn14d2.js +271 -0
- package/dist/chunk-wbhrqrq3.js +3033 -0
- package/dist/chunk-wfg9g0p7.js +17612 -0
- package/dist/chunk-wj3vjsbx.js +93 -0
- package/dist/chunk-wjhq855a.js +372 -0
- package/dist/chunk-wr06gqxh.js +114 -0
- package/dist/chunk-ws0z2y1g.js +195 -0
- package/dist/chunk-wt62wqcj.js +98 -0
- package/dist/chunk-wx4v7ddx.js +42 -0
- package/dist/chunk-wy0t3vb2.js +6101 -0
- package/dist/chunk-wysz7qk4.js +280 -0
- package/dist/chunk-x63fx1vd.js +192 -0
- package/dist/chunk-x8b7vft8.js +132 -0
- package/dist/chunk-x8jhkgxb.js +346 -0
- package/dist/chunk-x9z4q5k5.js +275 -0
- package/dist/chunk-xjd7e9jq.js +126 -0
- package/dist/chunk-xjr0n27e.js +42 -0
- package/dist/chunk-xpwyw7cd.js +24 -0
- package/dist/chunk-xw4ycnyz.js +37 -0
- package/dist/chunk-y66bqywr.js +17303 -0
- package/dist/chunk-ycarry7d.js +157 -0
- package/dist/chunk-ycr0hp6v.js +1486 -0
- package/dist/chunk-yqmaw6hf.js +496 -0
- package/dist/chunk-yrtm7d23.js +602 -0
- package/dist/chunk-yts879rw.js +48 -0
- package/dist/chunk-yvmhx96e.js +347 -0
- package/dist/chunk-ywq00rg4.js +246 -0
- package/dist/chunk-yygeg5mj.js +752 -0
- package/dist/chunk-z0exw850.js +726 -0
- package/dist/chunk-z2dp53wn.js +17 -0
- package/dist/chunk-z4rzc9nd.js +103 -0
- package/dist/chunk-zb0akt77.js +684 -0
- package/dist/chunk-zb2xrj5t.js +1789 -0
- package/dist/chunk-zbptn0ky.js +423 -0
- package/dist/chunk-zd45wbmf.js +1090 -0
- package/dist/chunk-zjkvspz9.js +2050 -0
- package/dist/chunk-zke1sp3x.js +1524 -0
- package/dist/chunk-zv2cvfsv.js +58 -0
- package/dist/chunk-zy4tmqst.js +644 -0
- package/dist/chunk-zygzzzvk.js +192 -0
- package/dist/cli-21v3v6ny.md +1 -0
- package/dist/cli.js +246 -0
- package/dist/server-n5f2q89z.md +1 -0
- package/package.json +161 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
getWebSocketProxyAgent,
|
|
4
|
+
getWebSocketProxyUrl,
|
|
5
|
+
getWebSocketTLSOptions,
|
|
6
|
+
init_mtls,
|
|
7
|
+
init_proxy
|
|
8
|
+
} from "./chunk-z0exw850.js";
|
|
9
|
+
import {
|
|
10
|
+
init_cleanupRegistry,
|
|
11
|
+
init_debug,
|
|
12
|
+
init_errors,
|
|
13
|
+
isENOENT,
|
|
14
|
+
logForDebugging,
|
|
15
|
+
registerCleanup
|
|
16
|
+
} from "./chunk-rex82hys.js";
|
|
17
|
+
import {
|
|
18
|
+
init_envUtils,
|
|
19
|
+
isEnvTruthy
|
|
20
|
+
} from "./chunk-4s93jyky.js";
|
|
21
|
+
import"./chunk-318g78ty.js";
|
|
22
|
+
import"./chunk-c0bdmy7w.js";
|
|
23
|
+
import"./chunk-syxy4pf2.js";
|
|
24
|
+
import {
|
|
25
|
+
__require,
|
|
26
|
+
__toESM
|
|
27
|
+
} from "./chunk-eta5aekm.js";
|
|
28
|
+
import"./chunk-5cp2q9sh.js";
|
|
29
|
+
import"./chunk-rjjakkw2.js";
|
|
30
|
+
import"./chunk-0sbddf6m.js";
|
|
31
|
+
import"./chunk-q5b0kpr3.js";
|
|
32
|
+
|
|
33
|
+
// src/upstreamproxy/upstreamproxy.ts
|
|
34
|
+
init_cleanupRegistry();
|
|
35
|
+
init_debug();
|
|
36
|
+
init_envUtils();
|
|
37
|
+
init_errors();
|
|
38
|
+
import { mkdir, readFile, unlink, writeFile } from "fs/promises";
|
|
39
|
+
import { homedir } from "os";
|
|
40
|
+
import { join } from "path";
|
|
41
|
+
|
|
42
|
+
// src/upstreamproxy/relay.ts
|
|
43
|
+
init_debug();
|
|
44
|
+
init_mtls();
|
|
45
|
+
init_proxy();
|
|
46
|
+
import { createServer } from "net";
|
|
47
|
+
var nodeWSCtor;
|
|
48
|
+
var MAX_CHUNK_BYTES = 512 * 1024;
|
|
49
|
+
var PING_INTERVAL_MS = 30000;
|
|
50
|
+
function encodeChunk(data) {
|
|
51
|
+
const len = data.length;
|
|
52
|
+
const varint = [];
|
|
53
|
+
let n = len;
|
|
54
|
+
while (n > 127) {
|
|
55
|
+
varint.push(n & 127 | 128);
|
|
56
|
+
n >>>= 7;
|
|
57
|
+
}
|
|
58
|
+
varint.push(n);
|
|
59
|
+
const out = new Uint8Array(1 + varint.length + len);
|
|
60
|
+
out[0] = 10;
|
|
61
|
+
out.set(varint, 1);
|
|
62
|
+
out.set(data, 1 + varint.length);
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
function decodeChunk(buf) {
|
|
66
|
+
if (buf.length === 0)
|
|
67
|
+
return new Uint8Array(0);
|
|
68
|
+
if (buf[0] !== 10)
|
|
69
|
+
return null;
|
|
70
|
+
let len = 0;
|
|
71
|
+
let shift = 0;
|
|
72
|
+
let i = 1;
|
|
73
|
+
while (i < buf.length) {
|
|
74
|
+
const b = buf[i];
|
|
75
|
+
len |= (b & 127) << shift;
|
|
76
|
+
i++;
|
|
77
|
+
if ((b & 128) === 0)
|
|
78
|
+
break;
|
|
79
|
+
shift += 7;
|
|
80
|
+
if (shift > 28)
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
if (i + len > buf.length)
|
|
84
|
+
return null;
|
|
85
|
+
return buf.subarray(i, i + len);
|
|
86
|
+
}
|
|
87
|
+
function newConnState() {
|
|
88
|
+
return {
|
|
89
|
+
connectBuf: Buffer.alloc(0),
|
|
90
|
+
pending: [],
|
|
91
|
+
wsOpen: false,
|
|
92
|
+
established: false,
|
|
93
|
+
closed: false
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function startUpstreamProxyRelay(opts) {
|
|
97
|
+
const authHeader = "Basic " + Buffer.from(`${opts.sessionId}:${opts.token}`).toString("base64");
|
|
98
|
+
const wsAuthHeader = `Bearer ${opts.token}`;
|
|
99
|
+
const relay = typeof Bun !== "undefined" ? startBunRelay(opts.wsUrl, authHeader, wsAuthHeader) : await startNodeRelay(opts.wsUrl, authHeader, wsAuthHeader);
|
|
100
|
+
logForDebugging(`[upstreamproxy] relay listening on 127.0.0.1:${relay.port}`);
|
|
101
|
+
return relay;
|
|
102
|
+
}
|
|
103
|
+
function startBunRelay(wsUrl, authHeader, wsAuthHeader) {
|
|
104
|
+
const server = Bun.listen({
|
|
105
|
+
hostname: "127.0.0.1",
|
|
106
|
+
port: 0,
|
|
107
|
+
socket: {
|
|
108
|
+
open(sock) {
|
|
109
|
+
sock.data = { ...newConnState(), writeBuf: [] };
|
|
110
|
+
},
|
|
111
|
+
data(sock, data) {
|
|
112
|
+
const st = sock.data;
|
|
113
|
+
const adapter = {
|
|
114
|
+
write: (payload) => {
|
|
115
|
+
const bytes = typeof payload === "string" ? Buffer.from(payload, "utf8") : payload;
|
|
116
|
+
if (st.writeBuf.length > 0) {
|
|
117
|
+
st.writeBuf.push(bytes);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const n = sock.write(bytes);
|
|
121
|
+
if (n < bytes.length)
|
|
122
|
+
st.writeBuf.push(bytes.subarray(n));
|
|
123
|
+
},
|
|
124
|
+
end: () => sock.end()
|
|
125
|
+
};
|
|
126
|
+
handleData(adapter, st, data, wsUrl, authHeader, wsAuthHeader);
|
|
127
|
+
},
|
|
128
|
+
drain(sock) {
|
|
129
|
+
const st = sock.data;
|
|
130
|
+
while (st.writeBuf.length > 0) {
|
|
131
|
+
const chunk = st.writeBuf[0];
|
|
132
|
+
const n = sock.write(chunk);
|
|
133
|
+
if (n < chunk.length) {
|
|
134
|
+
st.writeBuf[0] = chunk.subarray(n);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
st.writeBuf.shift();
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
close(sock) {
|
|
141
|
+
cleanupConn(sock.data);
|
|
142
|
+
},
|
|
143
|
+
error(sock, err) {
|
|
144
|
+
logForDebugging(`[upstreamproxy] client socket error: ${err.message}`);
|
|
145
|
+
cleanupConn(sock.data);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
port: server.port,
|
|
151
|
+
stop: () => server.stop(true)
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async function startNodeRelay(wsUrl, authHeader, wsAuthHeader) {
|
|
155
|
+
nodeWSCtor = (await import("ws")).default;
|
|
156
|
+
const states = new WeakMap;
|
|
157
|
+
const server = createServer((sock) => {
|
|
158
|
+
const st = newConnState();
|
|
159
|
+
states.set(sock, st);
|
|
160
|
+
const adapter = {
|
|
161
|
+
write: (payload) => {
|
|
162
|
+
sock.write(typeof payload === "string" ? payload : Buffer.from(payload));
|
|
163
|
+
},
|
|
164
|
+
end: () => sock.end()
|
|
165
|
+
};
|
|
166
|
+
sock.on("data", (data) => handleData(adapter, st, data, wsUrl, authHeader, wsAuthHeader));
|
|
167
|
+
sock.on("close", () => cleanupConn(states.get(sock)));
|
|
168
|
+
sock.on("error", (err) => {
|
|
169
|
+
logForDebugging(`[upstreamproxy] client socket error: ${err.message}`);
|
|
170
|
+
cleanupConn(states.get(sock));
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
server.once("error", reject);
|
|
175
|
+
server.listen(0, "127.0.0.1", () => {
|
|
176
|
+
const addr = server.address();
|
|
177
|
+
if (addr === null || typeof addr === "string") {
|
|
178
|
+
reject(new Error("upstreamproxy: server has no TCP address"));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
resolve({
|
|
182
|
+
port: addr.port,
|
|
183
|
+
stop: () => server.close()
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
function handleData(sock, st, data, wsUrl, authHeader, wsAuthHeader) {
|
|
189
|
+
if (!st.ws) {
|
|
190
|
+
st.connectBuf = Buffer.concat([st.connectBuf, data]);
|
|
191
|
+
const headerEnd = st.connectBuf.indexOf(`\r
|
|
192
|
+
\r
|
|
193
|
+
`);
|
|
194
|
+
if (headerEnd === -1) {
|
|
195
|
+
if (st.connectBuf.length > 8192) {
|
|
196
|
+
sock.write(`HTTP/1.1 400 Bad Request\r
|
|
197
|
+
\r
|
|
198
|
+
`);
|
|
199
|
+
sock.end();
|
|
200
|
+
}
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const reqHead = st.connectBuf.subarray(0, headerEnd).toString("utf8");
|
|
204
|
+
const firstLine = reqHead.split(`\r
|
|
205
|
+
`)[0] ?? "";
|
|
206
|
+
const m = firstLine.match(/^CONNECT\s+(\S+)\s+HTTP\/1\.[01]$/i);
|
|
207
|
+
if (!m) {
|
|
208
|
+
sock.write(`HTTP/1.1 405 Method Not Allowed\r
|
|
209
|
+
\r
|
|
210
|
+
`);
|
|
211
|
+
sock.end();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const trailing = st.connectBuf.subarray(headerEnd + 4);
|
|
215
|
+
if (trailing.length > 0) {
|
|
216
|
+
st.pending.push(Buffer.from(trailing));
|
|
217
|
+
}
|
|
218
|
+
st.connectBuf = Buffer.alloc(0);
|
|
219
|
+
openTunnel(sock, st, firstLine, wsUrl, authHeader, wsAuthHeader);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (!st.wsOpen) {
|
|
223
|
+
st.pending.push(Buffer.from(data));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
forwardToWs(st.ws, data);
|
|
227
|
+
}
|
|
228
|
+
function openTunnel(sock, st, connectLine, wsUrl, authHeader, wsAuthHeader) {
|
|
229
|
+
const headers = {
|
|
230
|
+
"Content-Type": "application/proto",
|
|
231
|
+
Authorization: wsAuthHeader
|
|
232
|
+
};
|
|
233
|
+
let ws;
|
|
234
|
+
if (nodeWSCtor) {
|
|
235
|
+
ws = new nodeWSCtor(wsUrl, {
|
|
236
|
+
headers,
|
|
237
|
+
agent: getWebSocketProxyAgent(wsUrl),
|
|
238
|
+
...getWebSocketTLSOptions()
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
ws = new globalThis.WebSocket(wsUrl, {
|
|
242
|
+
headers,
|
|
243
|
+
proxy: getWebSocketProxyUrl(wsUrl),
|
|
244
|
+
tls: getWebSocketTLSOptions() || undefined
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
ws.binaryType = "arraybuffer";
|
|
248
|
+
st.ws = ws;
|
|
249
|
+
ws.onopen = () => {
|
|
250
|
+
const head = `${connectLine}\r
|
|
251
|
+
Proxy-Authorization: ${authHeader}\r
|
|
252
|
+
\r
|
|
253
|
+
`;
|
|
254
|
+
ws.send(encodeChunk(new Uint8Array(Buffer.from(head, "utf8"))));
|
|
255
|
+
st.wsOpen = true;
|
|
256
|
+
for (const buf of st.pending) {
|
|
257
|
+
forwardToWs(ws, buf);
|
|
258
|
+
}
|
|
259
|
+
st.pending = [];
|
|
260
|
+
st.pinger = setInterval(sendKeepalive, PING_INTERVAL_MS, ws);
|
|
261
|
+
};
|
|
262
|
+
ws.onmessage = (ev) => {
|
|
263
|
+
const raw = ev.data instanceof ArrayBuffer ? new Uint8Array(ev.data) : new Uint8Array(Buffer.from(ev.data));
|
|
264
|
+
const payload = decodeChunk(raw);
|
|
265
|
+
if (payload && payload.length > 0) {
|
|
266
|
+
st.established = true;
|
|
267
|
+
sock.write(payload);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
ws.onerror = (ev) => {
|
|
271
|
+
const msg = "message" in ev ? String(ev.message) : "websocket error";
|
|
272
|
+
logForDebugging(`[upstreamproxy] ws error: ${msg}`);
|
|
273
|
+
if (st.closed)
|
|
274
|
+
return;
|
|
275
|
+
st.closed = true;
|
|
276
|
+
if (!st.established) {
|
|
277
|
+
sock.write(`HTTP/1.1 502 Bad Gateway\r
|
|
278
|
+
\r
|
|
279
|
+
`);
|
|
280
|
+
}
|
|
281
|
+
sock.end();
|
|
282
|
+
cleanupConn(st);
|
|
283
|
+
};
|
|
284
|
+
ws.onclose = () => {
|
|
285
|
+
if (st.closed)
|
|
286
|
+
return;
|
|
287
|
+
st.closed = true;
|
|
288
|
+
sock.end();
|
|
289
|
+
cleanupConn(st);
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function sendKeepalive(ws) {
|
|
293
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
294
|
+
ws.send(encodeChunk(new Uint8Array(0)));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function forwardToWs(ws, data) {
|
|
298
|
+
if (ws.readyState !== WebSocket.OPEN)
|
|
299
|
+
return;
|
|
300
|
+
for (let off = 0;off < data.length; off += MAX_CHUNK_BYTES) {
|
|
301
|
+
const slice = new Uint8Array(data.subarray(off, off + MAX_CHUNK_BYTES));
|
|
302
|
+
ws.send(encodeChunk(slice));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function cleanupConn(st) {
|
|
306
|
+
if (!st)
|
|
307
|
+
return;
|
|
308
|
+
if (st.pinger)
|
|
309
|
+
clearInterval(st.pinger);
|
|
310
|
+
if (st.ws && st.ws.readyState <= WebSocket.OPEN) {
|
|
311
|
+
try {
|
|
312
|
+
st.ws.close();
|
|
313
|
+
} catch {}
|
|
314
|
+
}
|
|
315
|
+
st.ws = undefined;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/upstreamproxy/upstreamproxy.ts
|
|
319
|
+
var SESSION_TOKEN_PATH = "/run/ccr/session_token";
|
|
320
|
+
var SYSTEM_CA_BUNDLE = "/etc/ssl/certs/ca-certificates.crt";
|
|
321
|
+
var NO_PROXY_LIST = [
|
|
322
|
+
"localhost",
|
|
323
|
+
"127.0.0.1",
|
|
324
|
+
"::1",
|
|
325
|
+
"169.254.0.0/16",
|
|
326
|
+
"10.0.0.0/8",
|
|
327
|
+
"172.16.0.0/12",
|
|
328
|
+
"192.168.0.0/16",
|
|
329
|
+
"anthropic.com",
|
|
330
|
+
".anthropic.com",
|
|
331
|
+
"*.anthropic.com",
|
|
332
|
+
"github.com",
|
|
333
|
+
"api.github.com",
|
|
334
|
+
"*.github.com",
|
|
335
|
+
"*.githubusercontent.com",
|
|
336
|
+
"registry.npmjs.org",
|
|
337
|
+
"pypi.org",
|
|
338
|
+
"files.pythonhosted.org",
|
|
339
|
+
"index.crates.io",
|
|
340
|
+
"proxy.golang.org"
|
|
341
|
+
].join(",");
|
|
342
|
+
var state = { enabled: false };
|
|
343
|
+
async function initUpstreamProxy(opts) {
|
|
344
|
+
if (!isEnvTruthy(process.env.CLAUDE_CODE_REMOTE)) {
|
|
345
|
+
return state;
|
|
346
|
+
}
|
|
347
|
+
if (!isEnvTruthy(process.env.CCR_UPSTREAM_PROXY_ENABLED)) {
|
|
348
|
+
return state;
|
|
349
|
+
}
|
|
350
|
+
const sessionId = process.env.CLAUDE_CODE_REMOTE_SESSION_ID;
|
|
351
|
+
if (!sessionId) {
|
|
352
|
+
logForDebugging("[upstreamproxy] CLAUDE_CODE_REMOTE_SESSION_ID unset; proxy disabled", { level: "warn" });
|
|
353
|
+
return state;
|
|
354
|
+
}
|
|
355
|
+
const tokenPath = opts?.tokenPath ?? SESSION_TOKEN_PATH;
|
|
356
|
+
const token = await readToken(tokenPath);
|
|
357
|
+
if (!token) {
|
|
358
|
+
logForDebugging("[upstreamproxy] no session token file; proxy disabled");
|
|
359
|
+
return state;
|
|
360
|
+
}
|
|
361
|
+
setNonDumpable();
|
|
362
|
+
const baseUrl = opts?.ccrBaseUrl ?? process.env.ANTHROPIC_BASE_URL ?? "https://api.anthropic.com";
|
|
363
|
+
const caBundlePath = opts?.caBundlePath ?? join(homedir(), ".ccr", "ca-bundle.crt");
|
|
364
|
+
const caOk = await downloadCaBundle(baseUrl, opts?.systemCaPath ?? SYSTEM_CA_BUNDLE, caBundlePath);
|
|
365
|
+
if (!caOk)
|
|
366
|
+
return state;
|
|
367
|
+
try {
|
|
368
|
+
const wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/code/upstreamproxy/ws";
|
|
369
|
+
const relay = await startUpstreamProxyRelay({ wsUrl, sessionId, token });
|
|
370
|
+
registerCleanup(async () => relay.stop());
|
|
371
|
+
state = { enabled: true, port: relay.port, caBundlePath };
|
|
372
|
+
logForDebugging(`[upstreamproxy] enabled on 127.0.0.1:${relay.port}`);
|
|
373
|
+
await unlink(tokenPath).catch(() => {
|
|
374
|
+
logForDebugging("[upstreamproxy] token file unlink failed", {
|
|
375
|
+
level: "warn"
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
} catch (err) {
|
|
379
|
+
logForDebugging(`[upstreamproxy] relay start failed: ${err instanceof Error ? err.message : String(err)}; proxy disabled`, { level: "warn" });
|
|
380
|
+
}
|
|
381
|
+
return state;
|
|
382
|
+
}
|
|
383
|
+
function getUpstreamProxyEnv() {
|
|
384
|
+
if (!state.enabled || !state.port || !state.caBundlePath) {
|
|
385
|
+
if (process.env.HTTPS_PROXY && process.env.SSL_CERT_FILE) {
|
|
386
|
+
const inherited = {};
|
|
387
|
+
for (const key of [
|
|
388
|
+
"HTTPS_PROXY",
|
|
389
|
+
"https_proxy",
|
|
390
|
+
"NO_PROXY",
|
|
391
|
+
"no_proxy",
|
|
392
|
+
"SSL_CERT_FILE",
|
|
393
|
+
"NODE_EXTRA_CA_CERTS",
|
|
394
|
+
"REQUESTS_CA_BUNDLE",
|
|
395
|
+
"CURL_CA_BUNDLE"
|
|
396
|
+
]) {
|
|
397
|
+
if (process.env[key])
|
|
398
|
+
inherited[key] = process.env[key];
|
|
399
|
+
}
|
|
400
|
+
return inherited;
|
|
401
|
+
}
|
|
402
|
+
return {};
|
|
403
|
+
}
|
|
404
|
+
const proxyUrl = `http://127.0.0.1:${state.port}`;
|
|
405
|
+
return {
|
|
406
|
+
HTTPS_PROXY: proxyUrl,
|
|
407
|
+
https_proxy: proxyUrl,
|
|
408
|
+
NO_PROXY: NO_PROXY_LIST,
|
|
409
|
+
no_proxy: NO_PROXY_LIST,
|
|
410
|
+
SSL_CERT_FILE: state.caBundlePath,
|
|
411
|
+
NODE_EXTRA_CA_CERTS: state.caBundlePath,
|
|
412
|
+
REQUESTS_CA_BUNDLE: state.caBundlePath,
|
|
413
|
+
CURL_CA_BUNDLE: state.caBundlePath
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function resetUpstreamProxyForTests() {
|
|
417
|
+
state = { enabled: false };
|
|
418
|
+
}
|
|
419
|
+
async function readToken(path) {
|
|
420
|
+
try {
|
|
421
|
+
const raw = await readFile(path, "utf8");
|
|
422
|
+
return raw.trim() || null;
|
|
423
|
+
} catch (err) {
|
|
424
|
+
if (isENOENT(err))
|
|
425
|
+
return null;
|
|
426
|
+
logForDebugging(`[upstreamproxy] token read failed: ${err instanceof Error ? err.message : String(err)}`, { level: "warn" });
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
function setNonDumpable() {
|
|
431
|
+
if (process.platform !== "linux" || typeof Bun === "undefined")
|
|
432
|
+
return;
|
|
433
|
+
try {
|
|
434
|
+
const ffi = __require("bun:ffi");
|
|
435
|
+
const lib = ffi.dlopen("libc.so.6", {
|
|
436
|
+
prctl: {
|
|
437
|
+
args: ["int", "u64", "u64", "u64", "u64"],
|
|
438
|
+
returns: "int"
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
const PR_SET_DUMPABLE = 4;
|
|
442
|
+
const rc = lib.symbols.prctl(PR_SET_DUMPABLE, 0n, 0n, 0n, 0n);
|
|
443
|
+
if (rc !== 0) {
|
|
444
|
+
logForDebugging("[upstreamproxy] prctl(PR_SET_DUMPABLE,0) returned nonzero", {
|
|
445
|
+
level: "warn"
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
} catch (err) {
|
|
449
|
+
logForDebugging(`[upstreamproxy] prctl unavailable: ${err instanceof Error ? err.message : String(err)}`, { level: "warn" });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
async function downloadCaBundle(baseUrl, systemCaPath, outPath) {
|
|
453
|
+
try {
|
|
454
|
+
const resp = await fetch(`${baseUrl}/v1/code/upstreamproxy/ca-cert`, {
|
|
455
|
+
signal: AbortSignal.timeout(5000)
|
|
456
|
+
});
|
|
457
|
+
if (!resp.ok) {
|
|
458
|
+
logForDebugging(`[upstreamproxy] ca-cert fetch ${resp.status}; proxy disabled`, { level: "warn" });
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
const ccrCa = await resp.text();
|
|
462
|
+
const systemCa = await readFile(systemCaPath, "utf8").catch(() => "");
|
|
463
|
+
await mkdir(join(outPath, ".."), { recursive: true });
|
|
464
|
+
await writeFile(outPath, systemCa + `
|
|
465
|
+
` + ccrCa, "utf8");
|
|
466
|
+
return true;
|
|
467
|
+
} catch (err) {
|
|
468
|
+
logForDebugging(`[upstreamproxy] ca-cert download failed: ${err instanceof Error ? err.message : String(err)}; proxy disabled`, { level: "warn" });
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
export {
|
|
473
|
+
resetUpstreamProxyForTests,
|
|
474
|
+
initUpstreamProxy,
|
|
475
|
+
getUpstreamProxyEnv,
|
|
476
|
+
SESSION_TOKEN_PATH
|
|
477
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
init_analytics,
|
|
4
|
+
logEvent
|
|
5
|
+
} from "./chunk-ezpa510h.js";
|
|
6
|
+
import {
|
|
7
|
+
errorMessage,
|
|
8
|
+
init_debug,
|
|
9
|
+
init_errors,
|
|
10
|
+
init_slowOperations,
|
|
11
|
+
jsonParse,
|
|
12
|
+
logForDebugging
|
|
13
|
+
} from "./chunk-rex82hys.js";
|
|
14
|
+
import {
|
|
15
|
+
init_diagLogs,
|
|
16
|
+
logForDiagnosticsNoPII
|
|
17
|
+
} from "./chunk-65z8hg8j.js";
|
|
18
|
+
|
|
19
|
+
// src/bridge/jwtUtils.ts
|
|
20
|
+
init_analytics();
|
|
21
|
+
init_debug();
|
|
22
|
+
init_diagLogs();
|
|
23
|
+
init_errors();
|
|
24
|
+
init_slowOperations();
|
|
25
|
+
function formatDuration(ms) {
|
|
26
|
+
if (ms < 60000)
|
|
27
|
+
return `${Math.round(ms / 1000)}s`;
|
|
28
|
+
const m = Math.floor(ms / 60000);
|
|
29
|
+
const s = Math.round(ms % 60000 / 1000);
|
|
30
|
+
return s > 0 ? `${m}m ${s}s` : `${m}m`;
|
|
31
|
+
}
|
|
32
|
+
function decodeJwtPayload(token) {
|
|
33
|
+
const jwt = token.startsWith("sk-ant-si-") ? token.slice("sk-ant-si-".length) : token;
|
|
34
|
+
const parts = jwt.split(".");
|
|
35
|
+
if (parts.length !== 3 || !parts[1])
|
|
36
|
+
return null;
|
|
37
|
+
try {
|
|
38
|
+
return jsonParse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function decodeJwtExpiry(token) {
|
|
44
|
+
const payload = decodeJwtPayload(token);
|
|
45
|
+
if (payload !== null && typeof payload === "object" && "exp" in payload && typeof payload.exp === "number") {
|
|
46
|
+
return payload.exp;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
var TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1000;
|
|
51
|
+
var FALLBACK_REFRESH_INTERVAL_MS = 30 * 60 * 1000;
|
|
52
|
+
var MAX_REFRESH_FAILURES = 3;
|
|
53
|
+
var REFRESH_RETRY_DELAY_MS = 60000;
|
|
54
|
+
function createTokenRefreshScheduler({
|
|
55
|
+
getAccessToken,
|
|
56
|
+
onRefresh,
|
|
57
|
+
label,
|
|
58
|
+
refreshBufferMs = TOKEN_REFRESH_BUFFER_MS
|
|
59
|
+
}) {
|
|
60
|
+
const timers = new Map;
|
|
61
|
+
const failureCounts = new Map;
|
|
62
|
+
const generations = new Map;
|
|
63
|
+
function nextGeneration(sessionId) {
|
|
64
|
+
const gen = (generations.get(sessionId) ?? 0) + 1;
|
|
65
|
+
generations.set(sessionId, gen);
|
|
66
|
+
return gen;
|
|
67
|
+
}
|
|
68
|
+
function schedule(sessionId, token) {
|
|
69
|
+
const expiry = decodeJwtExpiry(token);
|
|
70
|
+
if (!expiry) {
|
|
71
|
+
logForDebugging(`[${label}:token] Could not decode JWT expiry for sessionId=${sessionId}, token prefix=${token.slice(0, 15)}\u2026, keeping existing timer`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const existing = timers.get(sessionId);
|
|
75
|
+
if (existing) {
|
|
76
|
+
clearTimeout(existing);
|
|
77
|
+
}
|
|
78
|
+
const gen = nextGeneration(sessionId);
|
|
79
|
+
const expiryDate = new Date(expiry * 1000).toISOString();
|
|
80
|
+
const delayMs = expiry * 1000 - Date.now() - refreshBufferMs;
|
|
81
|
+
if (delayMs <= 0) {
|
|
82
|
+
logForDebugging(`[${label}:token] Token for sessionId=${sessionId} expires=${expiryDate} (past or within buffer), refreshing immediately`);
|
|
83
|
+
doRefresh(sessionId, gen);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
logForDebugging(`[${label}:token] Scheduled token refresh for sessionId=${sessionId} in ${formatDuration(delayMs)} (expires=${expiryDate}, buffer=${refreshBufferMs / 1000}s)`);
|
|
87
|
+
const timer = setTimeout(doRefresh, delayMs, sessionId, gen);
|
|
88
|
+
timers.set(sessionId, timer);
|
|
89
|
+
}
|
|
90
|
+
function scheduleFromExpiresIn(sessionId, expiresInSeconds) {
|
|
91
|
+
const existing = timers.get(sessionId);
|
|
92
|
+
if (existing)
|
|
93
|
+
clearTimeout(existing);
|
|
94
|
+
const gen = nextGeneration(sessionId);
|
|
95
|
+
const delayMs = Math.max(expiresInSeconds * 1000 - refreshBufferMs, 30000);
|
|
96
|
+
logForDebugging(`[${label}:token] Scheduled token refresh for sessionId=${sessionId} in ${formatDuration(delayMs)} (expires_in=${expiresInSeconds}s, buffer=${refreshBufferMs / 1000}s)`);
|
|
97
|
+
const timer = setTimeout(doRefresh, delayMs, sessionId, gen);
|
|
98
|
+
timers.set(sessionId, timer);
|
|
99
|
+
}
|
|
100
|
+
async function doRefresh(sessionId, gen) {
|
|
101
|
+
let oauthToken;
|
|
102
|
+
try {
|
|
103
|
+
oauthToken = await getAccessToken();
|
|
104
|
+
} catch (err) {
|
|
105
|
+
logForDebugging(`[${label}:token] getAccessToken threw for sessionId=${sessionId}: ${errorMessage(err)}`, { level: "error" });
|
|
106
|
+
}
|
|
107
|
+
if (generations.get(sessionId) !== gen) {
|
|
108
|
+
logForDebugging(`[${label}:token] doRefresh for sessionId=${sessionId} stale (gen ${gen} vs ${generations.get(sessionId)}), skipping`);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (!oauthToken) {
|
|
112
|
+
const failures = (failureCounts.get(sessionId) ?? 0) + 1;
|
|
113
|
+
failureCounts.set(sessionId, failures);
|
|
114
|
+
logForDebugging(`[${label}:token] No OAuth token available for refresh, sessionId=${sessionId} (failure ${failures}/${MAX_REFRESH_FAILURES})`, { level: "error" });
|
|
115
|
+
logForDiagnosticsNoPII("error", "bridge_token_refresh_no_oauth");
|
|
116
|
+
if (failures < MAX_REFRESH_FAILURES) {
|
|
117
|
+
const retryTimer = setTimeout(doRefresh, REFRESH_RETRY_DELAY_MS, sessionId, gen);
|
|
118
|
+
timers.set(sessionId, retryTimer);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
failureCounts.delete(sessionId);
|
|
123
|
+
logForDebugging(`[${label}:token] Refreshing token for sessionId=${sessionId}: new token prefix=${oauthToken.slice(0, 15)}\u2026`);
|
|
124
|
+
logEvent("tengu_bridge_token_refreshed", {});
|
|
125
|
+
onRefresh(sessionId, oauthToken);
|
|
126
|
+
const timer = setTimeout(doRefresh, FALLBACK_REFRESH_INTERVAL_MS, sessionId, gen);
|
|
127
|
+
timers.set(sessionId, timer);
|
|
128
|
+
logForDebugging(`[${label}:token] Scheduled follow-up refresh for sessionId=${sessionId} in ${formatDuration(FALLBACK_REFRESH_INTERVAL_MS)}`);
|
|
129
|
+
}
|
|
130
|
+
function cancel(sessionId) {
|
|
131
|
+
nextGeneration(sessionId);
|
|
132
|
+
const timer = timers.get(sessionId);
|
|
133
|
+
if (timer) {
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
timers.delete(sessionId);
|
|
136
|
+
}
|
|
137
|
+
failureCounts.delete(sessionId);
|
|
138
|
+
}
|
|
139
|
+
function cancelAll() {
|
|
140
|
+
for (const sessionId of generations.keys()) {
|
|
141
|
+
nextGeneration(sessionId);
|
|
142
|
+
}
|
|
143
|
+
for (const timer of timers.values()) {
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
}
|
|
146
|
+
timers.clear();
|
|
147
|
+
failureCounts.clear();
|
|
148
|
+
}
|
|
149
|
+
return { schedule, scheduleFromExpiresIn, cancel, cancelAll };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export { decodeJwtExpiry, createTokenRefreshScheduler };
|