synara 0.1.3 → 0.1.6
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 +52 -101
- package/dist/index.js +48 -36
- package/package.json +2 -2
- package/dist/auth.js +0 -50
- package/dist/permissions.js +0 -56
- package/dist/protocol.js +0 -4
- package/dist/tools/executor.js +0 -84
- package/dist/ui/terminal.js +0 -215
package/README.md
CHANGED
|
@@ -1,129 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Synara
|
|
2
2
|
|
|
3
3
|
AI-powered coding assistant. Cloud brain, local hands.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
Local CLI (thin client) Cloud Container (thick server)
|
|
9
|
-
┌─────────────────────┐ ┌──────────────────────────────┐
|
|
10
|
-
│ Terminal UI │ │ Harness Engine │
|
|
11
|
-
│ Tool Executor │◄───WS────►│ System Prompt Assembly │
|
|
12
|
-
│ Permission Control │ │ Guardrails / Skills │
|
|
13
|
-
│ SYNAPSE.md Reader │ │ Context Compact (LLM) │
|
|
14
|
-
└─────────────────────┘ │ Streaming LLM (Kimi K2.5) │
|
|
15
|
-
│ AI Gateway → Workers AI │
|
|
16
|
-
│ Trace Recording │
|
|
17
|
-
└──────────────────────────────┘
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
CLI only does three things: **display, execute, confirm**.
|
|
21
|
-
All intelligence lives on the cloud.
|
|
5
|
+
Part of the [Synapse](https://synapse.cloudc.top) autonomous AI agent platform.
|
|
22
6
|
|
|
23
7
|
## Install
|
|
24
8
|
|
|
25
9
|
```bash
|
|
26
|
-
npm install -g
|
|
10
|
+
npm install -g synara
|
|
27
11
|
```
|
|
28
12
|
|
|
29
|
-
##
|
|
13
|
+
## Quick Start
|
|
30
14
|
|
|
31
15
|
```bash
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
synapse logout # Remove stored credentials
|
|
35
|
-
synapse --help # Show help
|
|
16
|
+
synara login # Authenticate
|
|
17
|
+
synara # Start coding session in current directory
|
|
36
18
|
```
|
|
37
19
|
|
|
38
|
-
##
|
|
39
|
-
|
|
40
|
-
| Command | Description |
|
|
41
|
-
|------------|------------------------------------------|
|
|
42
|
-
| `/compact` | Compress conversation history (LLM summarization) |
|
|
43
|
-
| `/context` | Show context usage (tokens, messages, compactions) |
|
|
44
|
-
| `/tools` | List available tools and permission levels |
|
|
45
|
-
| `/quit` | End session |
|
|
46
|
-
|
|
47
|
-
## Permission Model
|
|
48
|
-
|
|
49
|
-
| Tool | Permission |
|
|
50
|
-
|-------------------------------|---------------------|
|
|
51
|
-
| read_file, grep, glob, list_dir | Auto (in-project only) |
|
|
52
|
-
| read outside project dir | Ask each time |
|
|
53
|
-
| write_file, edit_file | Ask (trustable via `t`) |
|
|
54
|
-
| bash | Ask every time (never trustable) |
|
|
55
|
-
| Destructive commands (rm -rf /, mkfs, etc.) | Blocked entirely |
|
|
56
|
-
|
|
57
|
-
When prompted:
|
|
58
|
-
- `y` — approve this operation
|
|
59
|
-
- `n` — deny (AI will ask you what to do instead)
|
|
60
|
-
- `t` — trust this tool type for the rest of the session (not available for bash)
|
|
61
|
-
|
|
62
|
-
## Streaming
|
|
63
|
-
|
|
64
|
-
- LLM responses stream token-by-token via WebSocket
|
|
65
|
-
- Content appears in real-time as the model generates it
|
|
66
|
-
- Reasoning (thinking) is hidden by default
|
|
20
|
+
## How It Works
|
|
67
21
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
- **reactiveCompact** — triggers on API 413 error, compresses and retries (max 3 attempts)
|
|
75
|
-
- **`/compact`** — manual trigger
|
|
22
|
+
```
|
|
23
|
+
You ←→ Synara CLI (local) ←—WebSocket—→ Cloud Engine (Cloudflare)
|
|
24
|
+
• Display output • LLM reasoning (Kimi K2.5)
|
|
25
|
+
• Execute tools locally • System prompt & guardrails
|
|
26
|
+
• Permission control • Context management
|
|
27
|
+
```
|
|
76
28
|
|
|
77
|
-
|
|
29
|
+
The CLI is a thin client — all intelligence lives on the cloud. Your code stays local; only tool requests and results travel over the wire.
|
|
78
30
|
|
|
79
|
-
##
|
|
31
|
+
## Commands
|
|
80
32
|
|
|
81
|
-
|
|
33
|
+
```bash
|
|
34
|
+
synara login # Login with username/password
|
|
35
|
+
synara logout # Remove stored credentials
|
|
36
|
+
synara --help # Show help
|
|
37
|
+
```
|
|
82
38
|
|
|
83
|
-
|
|
39
|
+
### Session Commands
|
|
84
40
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
41
|
+
| Command | Description |
|
|
42
|
+
|------------|-------------|
|
|
43
|
+
| `/compact` | Compress conversation (LLM summarization, never truncation) |
|
|
44
|
+
| `/context` | Show token usage and compaction stats |
|
|
45
|
+
| `/save` | Save session locally |
|
|
46
|
+
| `/load` | Restore a saved session |
|
|
47
|
+
| `/sessions`| List saved sessions |
|
|
48
|
+
| `/quit` | End session |
|
|
89
49
|
|
|
90
50
|
## Tools
|
|
91
51
|
|
|
92
|
-
| Tool
|
|
93
|
-
|
|
94
|
-
| read_file
|
|
95
|
-
| write_file | Create or overwrite
|
|
96
|
-
| edit_file
|
|
97
|
-
| grep
|
|
98
|
-
| glob
|
|
99
|
-
| list_dir
|
|
100
|
-
| bash
|
|
52
|
+
| Tool | Description |
|
|
53
|
+
|------|-------------|
|
|
54
|
+
| `read_file` | Read file contents |
|
|
55
|
+
| `write_file` | Create or overwrite files |
|
|
56
|
+
| `edit_file` | Precise string replacement |
|
|
57
|
+
| `grep` | Regex search across files |
|
|
58
|
+
| `glob` | Find files by pattern |
|
|
59
|
+
| `list_dir` | List directory contents |
|
|
60
|
+
| `bash` | Execute shell commands |
|
|
101
61
|
|
|
102
|
-
##
|
|
62
|
+
## Permission Model
|
|
103
63
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
├── package.json
|
|
114
|
-
└── tsconfig.json
|
|
115
|
-
|
|
116
|
-
harness-agent/ # Cloud server (Cloudflare Worker + Container)
|
|
117
|
-
├── src/worker/index.ts # /cli/ws route → JWT auth → Container proxy
|
|
118
|
-
└── src/services/cli_session.py # CLI session: LLM loop, streaming, compact
|
|
119
|
-
```
|
|
64
|
+
| Scope | Behavior |
|
|
65
|
+
|-------|----------|
|
|
66
|
+
| Read in project dir | Auto-approved |
|
|
67
|
+
| Read outside project | Requires approval |
|
|
68
|
+
| Write / Edit | Requires approval (trustable via `t`) |
|
|
69
|
+
| Bash | Requires approval every time (never trustable) |
|
|
70
|
+
| Destructive commands (`rm -rf /`, `mkfs`, etc.) | Blocked |
|
|
71
|
+
|
|
72
|
+
Prompt responses: `y` approve · `n` deny · `t` trust for session
|
|
120
73
|
|
|
121
|
-
##
|
|
74
|
+
## Project Config
|
|
122
75
|
|
|
123
|
-
|
|
124
|
-
- **Kiro CLI** — /compact command, streaming UX, session management
|
|
125
|
-
- **Synapse Harness** — guardrails + skills injection, Dreaming pipeline
|
|
76
|
+
Drop a `SYNAPSE.md` in your project root — it's automatically sent to the cloud as project-specific instructions.
|
|
126
77
|
|
|
127
|
-
|
|
78
|
+
## License
|
|
128
79
|
|
|
129
|
-
|
|
80
|
+
MIT · Built by Bowen Liu
|
package/dist/index.js
CHANGED
|
@@ -1,63 +1,72 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var or=Object.create;var xt=Object.defineProperty;var ar=Object.getOwnPropertyDescriptor;var lr=Object.getOwnPropertyNames;var cr=Object.getPrototypeOf,fr=Object.prototype.hasOwnProperty;var C=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var ur=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of lr(e))!fr.call(t,n)&&n!==s&&xt(t,n,{get:()=>e[n],enumerable:!(r=ar(e,n))||r.enumerable});return t};var x=(t,e,s)=>(s=t!=null?or(cr(t)):{},ur(e||!t||!t.__esModule?xt(s,"default",{value:t,enumerable:!0}):s,t));var F=C((_i,vt)=>{"use strict";var bt=["nodebuffer","arraybuffer","fragments"],Et=typeof Blob<"u";Et&&bt.push("blob");vt.exports={BINARY_TYPES:bt,CLOSE_TIMEOUT:3e4,EMPTY_BUFFER:Buffer.alloc(0),GUID:"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",hasBlob:Et,kForOnEventAttribute:Symbol("kIsForOnEventAttribute"),kListener:Symbol("kListener"),kStatusCode:Symbol("status-code"),kWebSocket:Symbol("websocket"),NOOP:()=>{}}});var ce=C((mi,xe)=>{"use strict";var{EMPTY_BUFFER:hr}=F(),Ge=Buffer[Symbol.species];function dr(t,e){if(t.length===0)return hr;if(t.length===1)return t[0];let s=Buffer.allocUnsafe(e),r=0;for(let n=0;n<t.length;n++){let i=t[n];s.set(i,r),r+=i.length}return r<e?new Ge(s.buffer,s.byteOffset,r):s}function wt(t,e,s,r,n){for(let i=0;i<n;i++)s[r+i]=t[i]^e[i&3]}function Tt(t,e){for(let s=0;s<t.length;s++)t[s]^=e[s&3]}function pr(t){return t.length===t.buffer.byteLength?t.buffer:t.buffer.slice(t.byteOffset,t.byteOffset+t.length)}function Ve(t){if(Ve.readOnly=!0,Buffer.isBuffer(t))return t;let e;return t instanceof ArrayBuffer?e=new Ge(t):ArrayBuffer.isView(t)?e=new Ge(t.buffer,t.byteOffset,t.byteLength):(e=Buffer.from(t),Ve.readOnly=!1),e}xe.exports={concat:dr,mask:wt,toArrayBuffer:pr,toBuffer:Ve,unmask:Tt};if(!process.env.WS_NO_BUFFER_UTIL)try{let t=require("bufferutil");xe.exports.mask=function(e,s,r,n,i){i<48?wt(e,s,r,n,i):t.mask(e,s,r,n,i)},xe.exports.unmask=function(e,s){e.length<32?Tt(e,s):t.unmask(e,s)}}catch{}});var Ct=C((gi,kt)=>{"use strict";var Ot=Symbol("kDone"),Ye=Symbol("kRun"),He=class{constructor(e){this[Ot]=()=>{this.pending--,this[Ye]()},this.concurrency=e||1/0,this.jobs=[],this.pending=0}add(e){this.jobs.push(e),this[Ye]()}[Ye](){if(this.pending!==this.concurrency&&this.jobs.length){let e=this.jobs.shift();this.pending++,e(this[Ot])}}};kt.exports=He});var ee=C((yi,Pt)=>{"use strict";var fe=require("zlib"),Nt=ce(),_r=Ct(),{kStatusCode:Lt}=F(),mr=Buffer[Symbol.species],gr=Buffer.from([0,0,255,255]),Ee=Symbol("permessage-deflate"),U=Symbol("total-length"),Z=Symbol("callback"),j=Symbol("buffers"),Q=Symbol("error"),be,ze=class{constructor(e){if(this._options=e||{},this._threshold=this._options.threshold!==void 0?this._options.threshold:1024,this._maxPayload=this._options.maxPayload|0,this._isServer=!!this._options.isServer,this._deflate=null,this._inflate=null,this.params=null,!be){let s=this._options.concurrencyLimit!==void 0?this._options.concurrencyLimit:10;be=new _r(s)}}static get extensionName(){return"permessage-deflate"}offer(){let e={};return this._options.serverNoContextTakeover&&(e.server_no_context_takeover=!0),this._options.clientNoContextTakeover&&(e.client_no_context_takeover=!0),this._options.serverMaxWindowBits&&(e.server_max_window_bits=this._options.serverMaxWindowBits),this._options.clientMaxWindowBits?e.client_max_window_bits=this._options.clientMaxWindowBits:this._options.clientMaxWindowBits==null&&(e.client_max_window_bits=!0),e}accept(e){return e=this.normalizeParams(e),this.params=this._isServer?this.acceptAsServer(e):this.acceptAsClient(e),this.params}cleanup(){if(this._inflate&&(this._inflate.close(),this._inflate=null),this._deflate){let e=this._deflate[Z];this._deflate.close(),this._deflate=null,e&&e(new Error("The deflate stream was closed while data was being processed"))}}acceptAsServer(e){let s=this._options,r=e.find(n=>!(s.serverNoContextTakeover===!1&&n.server_no_context_takeover||n.server_max_window_bits&&(s.serverMaxWindowBits===!1||typeof s.serverMaxWindowBits=="number"&&s.serverMaxWindowBits>n.server_max_window_bits)||typeof s.clientMaxWindowBits=="number"&&!n.client_max_window_bits));if(!r)throw new Error("None of the extension offers can be accepted");return s.serverNoContextTakeover&&(r.server_no_context_takeover=!0),s.clientNoContextTakeover&&(r.client_no_context_takeover=!0),typeof s.serverMaxWindowBits=="number"&&(r.server_max_window_bits=s.serverMaxWindowBits),typeof s.clientMaxWindowBits=="number"?r.client_max_window_bits=s.clientMaxWindowBits:(r.client_max_window_bits===!0||s.clientMaxWindowBits===!1)&&delete r.client_max_window_bits,r}acceptAsClient(e){let s=e[0];if(this._options.clientNoContextTakeover===!1&&s.client_no_context_takeover)throw new Error('Unexpected parameter "client_no_context_takeover"');if(!s.client_max_window_bits)typeof this._options.clientMaxWindowBits=="number"&&(s.client_max_window_bits=this._options.clientMaxWindowBits);else if(this._options.clientMaxWindowBits===!1||typeof this._options.clientMaxWindowBits=="number"&&s.client_max_window_bits>this._options.clientMaxWindowBits)throw new Error('Unexpected or invalid parameter "client_max_window_bits"');return s}normalizeParams(e){return e.forEach(s=>{Object.keys(s).forEach(r=>{let n=s[r];if(n.length>1)throw new Error(`Parameter "${r}" must have only a single value`);if(n=n[0],r==="client_max_window_bits"){if(n!==!0){let i=+n;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${n}`);n=i}else if(!this._isServer)throw new TypeError(`Invalid value for parameter "${r}": ${n}`)}else if(r==="server_max_window_bits"){let i=+n;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${n}`);n=i}else if(r==="client_no_context_takeover"||r==="server_no_context_takeover"){if(n!==!0)throw new TypeError(`Invalid value for parameter "${r}": ${n}`)}else throw new Error(`Unknown parameter "${r}"`);s[r]=n})}),e}decompress(e,s,r){be.add(n=>{this._decompress(e,s,(i,o)=>{n(),r(i,o)})})}compress(e,s,r){be.add(n=>{this._compress(e,s,(i,o)=>{n(),r(i,o)})})}_decompress(e,s,r){let n=this._isServer?"client":"server";if(!this._inflate){let i=`${n}_max_window_bits`,o=typeof this.params[i]!="number"?fe.Z_DEFAULT_WINDOWBITS:this.params[i];this._inflate=fe.createInflateRaw({...this._options.zlibInflateOptions,windowBits:o}),this._inflate[Ee]=this,this._inflate[U]=0,this._inflate[j]=[],this._inflate.on("error",Sr),this._inflate.on("data",Rt)}this._inflate[Z]=r,this._inflate.write(e),s&&this._inflate.write(gr),this._inflate.flush(()=>{let i=this._inflate[Q];if(i){this._inflate.close(),this._inflate=null,r(i);return}let o=Nt.concat(this._inflate[j],this._inflate[U]);this._inflate._readableState.endEmitted?(this._inflate.close(),this._inflate=null):(this._inflate[U]=0,this._inflate[j]=[],s&&this.params[`${n}_no_context_takeover`]&&this._inflate.reset()),r(null,o)})}_compress(e,s,r){let n=this._isServer?"server":"client";if(!this._deflate){let i=`${n}_max_window_bits`,o=typeof this.params[i]!="number"?fe.Z_DEFAULT_WINDOWBITS:this.params[i];this._deflate=fe.createDeflateRaw({...this._options.zlibDeflateOptions,windowBits:o}),this._deflate[U]=0,this._deflate[j]=[],this._deflate.on("data",yr)}this._deflate[Z]=r,this._deflate.write(e),this._deflate.flush(fe.Z_SYNC_FLUSH,()=>{if(!this._deflate)return;let i=Nt.concat(this._deflate[j],this._deflate[U]);s&&(i=new mr(i.buffer,i.byteOffset,i.length-4)),this._deflate[Z]=null,this._deflate[U]=0,this._deflate[j]=[],s&&this.params[`${n}_no_context_takeover`]&&this._deflate.reset(),r(null,i)})}};Pt.exports=ze;function yr(t){this[j].push(t),this[U]+=t.length}function Rt(t){if(this[U]+=t.length,this[Ee]._maxPayload<1||this[U]<=this[Ee]._maxPayload){this[j].push(t);return}this[Q]=new RangeError("Max payload size exceeded"),this[Q].code="WS_ERR_UNSUPPORTED_MESSAGE_LENGTH",this[Q][Lt]=1009,this.removeListener("data",Rt),this.reset()}function Sr(t){if(this[Ee]._inflate=null,this[Q]){this[Z](this[Q]);return}t[Lt]=1007,this[Z](t)}});var te=C((Si,ve)=>{"use strict";var{isUtf8:Bt}=require("buffer"),{hasBlob:xr}=F(),br=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0];function Er(t){return t>=1e3&&t<=1014&&t!==1004&&t!==1005&&t!==1006||t>=3e3&&t<=4999}function Ke(t){let e=t.length,s=0;for(;s<e;)if((t[s]&128)===0)s++;else if((t[s]&224)===192){if(s+1===e||(t[s+1]&192)!==128||(t[s]&254)===192)return!1;s+=2}else if((t[s]&240)===224){if(s+2>=e||(t[s+1]&192)!==128||(t[s+2]&192)!==128||t[s]===224&&(t[s+1]&224)===128||t[s]===237&&(t[s+1]&224)===160)return!1;s+=3}else if((t[s]&248)===240){if(s+3>=e||(t[s+1]&192)!==128||(t[s+2]&192)!==128||(t[s+3]&192)!==128||t[s]===240&&(t[s+1]&240)===128||t[s]===244&&t[s+1]>143||t[s]>244)return!1;s+=4}else return!1;return!0}function vr(t){return xr&&typeof t=="object"&&typeof t.arrayBuffer=="function"&&typeof t.type=="string"&&typeof t.stream=="function"&&(t[Symbol.toStringTag]==="Blob"||t[Symbol.toStringTag]==="File")}ve.exports={isBlob:vr,isValidStatusCode:Er,isValidUTF8:Ke,tokenChars:br};if(Bt)ve.exports.isValidUTF8=function(t){return t.length<24?Ke(t):Bt(t)};else if(!process.env.WS_NO_UTF_8_VALIDATE)try{let t=require("utf-8-validate");ve.exports.isValidUTF8=function(e){return e.length<32?Ke(e):t(e)}}catch{}});var et=C((xi,Dt)=>{"use strict";var{Writable:wr}=require("stream"),It=ee(),{BINARY_TYPES:Tr,EMPTY_BUFFER:At,kStatusCode:Or,kWebSocket:kr}=F(),{concat:Je,toArrayBuffer:Cr,unmask:Nr}=ce(),{isValidStatusCode:Lr,isValidUTF8:Mt}=te(),we=Buffer[Symbol.species],L=0,$t=1,Ft=2,Ut=3,Xe=4,Ze=5,Te=6,Qe=class extends wr{constructor(e={}){super(),this._allowSynchronousEvents=e.allowSynchronousEvents!==void 0?e.allowSynchronousEvents:!0,this._binaryType=e.binaryType||Tr[0],this._extensions=e.extensions||{},this._isServer=!!e.isServer,this._maxPayload=e.maxPayload|0,this._skipUTF8Validation=!!e.skipUTF8Validation,this[kr]=void 0,this._bufferedBytes=0,this._buffers=[],this._compressed=!1,this._payloadLength=0,this._mask=void 0,this._fragmented=0,this._masked=!1,this._fin=!1,this._opcode=0,this._totalPayloadLength=0,this._messageLength=0,this._fragments=[],this._errored=!1,this._loop=!1,this._state=L}_write(e,s,r){if(this._opcode===8&&this._state==L)return r();this._bufferedBytes+=e.length,this._buffers.push(e),this.startLoop(r)}consume(e){if(this._bufferedBytes-=e,e===this._buffers[0].length)return this._buffers.shift();if(e<this._buffers[0].length){let r=this._buffers[0];return this._buffers[0]=new we(r.buffer,r.byteOffset+e,r.length-e),new we(r.buffer,r.byteOffset,e)}let s=Buffer.allocUnsafe(e);do{let r=this._buffers[0],n=s.length-e;e>=r.length?s.set(this._buffers.shift(),n):(s.set(new Uint8Array(r.buffer,r.byteOffset,e),n),this._buffers[0]=new we(r.buffer,r.byteOffset+e,r.length-e)),e-=r.length}while(e>0);return s}startLoop(e){this._loop=!0;do switch(this._state){case L:this.getInfo(e);break;case $t:this.getPayloadLength16(e);break;case Ft:this.getPayloadLength64(e);break;case Ut:this.getMask();break;case Xe:this.getData(e);break;case Ze:case Te:this._loop=!1;return}while(this._loop);this._errored||e()}getInfo(e){if(this._bufferedBytes<2){this._loop=!1;return}let s=this.consume(2);if((s[0]&48)!==0){let n=this.createError(RangeError,"RSV2 and RSV3 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_2_3");e(n);return}let r=(s[0]&64)===64;if(r&&!this._extensions[It.extensionName]){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(this._fin=(s[0]&128)===128,this._opcode=s[0]&15,this._payloadLength=s[1]&127,this._opcode===0){if(r){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(!this._fragmented){let n=this.createError(RangeError,"invalid opcode 0",!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}this._opcode=this._fragmented}else if(this._opcode===1||this._opcode===2){if(this._fragmented){let n=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}this._compressed=r}else if(this._opcode>7&&this._opcode<11){if(!this._fin){let n=this.createError(RangeError,"FIN must be set",!0,1002,"WS_ERR_EXPECTED_FIN");e(n);return}if(r){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(this._payloadLength>125||this._opcode===8&&this._payloadLength===1){let n=this.createError(RangeError,`invalid payload length ${this._payloadLength}`,!0,1002,"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");e(n);return}}else{let n=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}if(!this._fin&&!this._fragmented&&(this._fragmented=this._opcode),this._masked=(s[1]&128)===128,this._isServer){if(!this._masked){let n=this.createError(RangeError,"MASK must be set",!0,1002,"WS_ERR_EXPECTED_MASK");e(n);return}}else if(this._masked){let n=this.createError(RangeError,"MASK must be clear",!0,1002,"WS_ERR_UNEXPECTED_MASK");e(n);return}this._payloadLength===126?this._state=$t:this._payloadLength===127?this._state=Ft:this.haveLength(e)}getPayloadLength16(e){if(this._bufferedBytes<2){this._loop=!1;return}this._payloadLength=this.consume(2).readUInt16BE(0),this.haveLength(e)}getPayloadLength64(e){if(this._bufferedBytes<8){this._loop=!1;return}let s=this.consume(8),r=s.readUInt32BE(0);if(r>Math.pow(2,21)-1){let n=this.createError(RangeError,"Unsupported WebSocket frame: payload length > 2^53 - 1",!1,1009,"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");e(n);return}this._payloadLength=r*Math.pow(2,32)+s.readUInt32BE(4),this.haveLength(e)}haveLength(e){if(this._payloadLength&&this._opcode<8&&(this._totalPayloadLength+=this._payloadLength,this._totalPayloadLength>this._maxPayload&&this._maxPayload>0)){let s=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");e(s);return}this._masked?this._state=Ut:this._state=Xe}getMask(){if(this._bufferedBytes<4){this._loop=!1;return}this._mask=this.consume(4),this._state=Xe}getData(e){let s=At;if(this._payloadLength){if(this._bufferedBytes<this._payloadLength){this._loop=!1;return}s=this.consume(this._payloadLength),this._masked&&(this._mask[0]|this._mask[1]|this._mask[2]|this._mask[3])!==0&&Nr(s,this._mask)}if(this._opcode>7){this.controlMessage(s,e);return}if(this._compressed){this._state=Ze,this.decompress(s,e);return}s.length&&(this._messageLength=this._totalPayloadLength,this._fragments.push(s)),this.dataMessage(e)}decompress(e,s){this._extensions[It.extensionName].decompress(e,this._fin,(n,i)=>{if(n)return s(n);if(i.length){if(this._messageLength+=i.length,this._messageLength>this._maxPayload&&this._maxPayload>0){let o=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");s(o);return}this._fragments.push(i)}this.dataMessage(s),this._state===L&&this.startLoop(s)})}dataMessage(e){if(!this._fin){this._state=L;return}let s=this._messageLength,r=this._fragments;if(this._totalPayloadLength=0,this._messageLength=0,this._fragmented=0,this._fragments=[],this._opcode===2){let n;this._binaryType==="nodebuffer"?n=Je(r,s):this._binaryType==="arraybuffer"?n=Cr(Je(r,s)):this._binaryType==="blob"?n=new Blob(r):n=r,this._allowSynchronousEvents?(this.emit("message",n,!0),this._state=L):(this._state=Te,setImmediate(()=>{this.emit("message",n,!0),this._state=L,this.startLoop(e)}))}else{let n=Je(r,s);if(!this._skipUTF8Validation&&!Mt(n)){let i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");e(i);return}this._state===Ze||this._allowSynchronousEvents?(this.emit("message",n,!1),this._state=L):(this._state=Te,setImmediate(()=>{this.emit("message",n,!1),this._state=L,this.startLoop(e)}))}}controlMessage(e,s){if(this._opcode===8){if(e.length===0)this._loop=!1,this.emit("conclude",1005,At),this.end();else{let r=e.readUInt16BE(0);if(!Lr(r)){let i=this.createError(RangeError,`invalid status code ${r}`,!0,1002,"WS_ERR_INVALID_CLOSE_CODE");s(i);return}let n=new we(e.buffer,e.byteOffset+2,e.length-2);if(!this._skipUTF8Validation&&!Mt(n)){let i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");s(i);return}this._loop=!1,this.emit("conclude",r,n),this.end()}this._state=L;return}this._allowSynchronousEvents?(this.emit(this._opcode===9?"ping":"pong",e),this._state=L):(this._state=Te,setImmediate(()=>{this.emit(this._opcode===9?"ping":"pong",e),this._state=L,this.startLoop(s)}))}createError(e,s,r,n,i){this._loop=!1,this._errored=!0;let o=new e(r?`Invalid WebSocket frame: ${s}`:s);return Error.captureStackTrace(o,this.createError),o.code=i,o[Or]=n,o}};Dt.exports=Qe});var rt=C((Ei,qt)=>{"use strict";var{Duplex:bi}=require("stream"),{randomFillSync:Rr}=require("crypto"),Wt=ee(),{EMPTY_BUFFER:Pr,kWebSocket:Br,NOOP:Ir}=F(),{isBlob:se,isValidStatusCode:Ar}=te(),{mask:jt,toBuffer:V}=ce(),R=Symbol("kByteLength"),Mr=Buffer.alloc(4),Oe=8*1024,Y,re=Oe,I=0,$r=1,Fr=2,tt=class t{constructor(e,s,r){this._extensions=s||{},r&&(this._generateMask=r,this._maskBuffer=Buffer.alloc(4)),this._socket=e,this._firstFragment=!0,this._compress=!1,this._bufferedBytes=0,this._queue=[],this._state=I,this.onerror=Ir,this[Br]=void 0}static frame(e,s){let r,n=!1,i=2,o=!1;s.mask&&(r=s.maskBuffer||Mr,s.generateMask?s.generateMask(r):(re===Oe&&(Y===void 0&&(Y=Buffer.alloc(Oe)),Rr(Y,0,Oe),re=0),r[0]=Y[re++],r[1]=Y[re++],r[2]=Y[re++],r[3]=Y[re++]),o=(r[0]|r[1]|r[2]|r[3])===0,i=6);let a;typeof e=="string"?(!s.mask||o)&&s[R]!==void 0?a=s[R]:(e=Buffer.from(e),a=e.length):(a=e.length,n=s.mask&&s.readOnly&&!o);let c=a;a>=65536?(i+=8,c=127):a>125&&(i+=2,c=126);let l=Buffer.allocUnsafe(n?a+i:i);return l[0]=s.fin?s.opcode|128:s.opcode,s.rsv1&&(l[0]|=64),l[1]=c,c===126?l.writeUInt16BE(a,2):c===127&&(l[2]=l[3]=0,l.writeUIntBE(a,4,6)),s.mask?(l[1]|=128,l[i-4]=r[0],l[i-3]=r[1],l[i-2]=r[2],l[i-1]=r[3],o?[l,e]:n?(jt(e,r,l,i,a),[l]):(jt(e,r,e,0,a),[l,e])):[l,e]}close(e,s,r,n){let i;if(e===void 0)i=Pr;else{if(typeof e!="number"||!Ar(e))throw new TypeError("First argument must be a valid error code number");if(s===void 0||!s.length)i=Buffer.allocUnsafe(2),i.writeUInt16BE(e,0);else{let a=Buffer.byteLength(s);if(a>123)throw new RangeError("The message must not be greater than 123 bytes");i=Buffer.allocUnsafe(2+a),i.writeUInt16BE(e,0),typeof s=="string"?i.write(s,2):i.set(s,2)}}let o={[R]:i.length,fin:!0,generateMask:this._generateMask,mask:r,maskBuffer:this._maskBuffer,opcode:8,readOnly:!1,rsv1:!1};this._state!==I?this.enqueue([this.dispatch,i,!1,o,n]):this.sendFrame(t.frame(i,o),n)}ping(e,s,r){let n,i;if(typeof e=="string"?(n=Buffer.byteLength(e),i=!1):se(e)?(n=e.size,i=!1):(e=V(e),n=e.length,i=V.readOnly),n>125)throw new RangeError("The data size must not be greater than 125 bytes");let o={[R]:n,fin:!0,generateMask:this._generateMask,mask:s,maskBuffer:this._maskBuffer,opcode:9,readOnly:i,rsv1:!1};se(e)?this._state!==I?this.enqueue([this.getBlobData,e,!1,o,r]):this.getBlobData(e,!1,o,r):this._state!==I?this.enqueue([this.dispatch,e,!1,o,r]):this.sendFrame(t.frame(e,o),r)}pong(e,s,r){let n,i;if(typeof e=="string"?(n=Buffer.byteLength(e),i=!1):se(e)?(n=e.size,i=!1):(e=V(e),n=e.length,i=V.readOnly),n>125)throw new RangeError("The data size must not be greater than 125 bytes");let o={[R]:n,fin:!0,generateMask:this._generateMask,mask:s,maskBuffer:this._maskBuffer,opcode:10,readOnly:i,rsv1:!1};se(e)?this._state!==I?this.enqueue([this.getBlobData,e,!1,o,r]):this.getBlobData(e,!1,o,r):this._state!==I?this.enqueue([this.dispatch,e,!1,o,r]):this.sendFrame(t.frame(e,o),r)}send(e,s,r){let n=this._extensions[Wt.extensionName],i=s.binary?2:1,o=s.compress,a,c;typeof e=="string"?(a=Buffer.byteLength(e),c=!1):se(e)?(a=e.size,c=!1):(e=V(e),a=e.length,c=V.readOnly),this._firstFragment?(this._firstFragment=!1,o&&n&&n.params[n._isServer?"server_no_context_takeover":"client_no_context_takeover"]&&(o=a>=n._threshold),this._compress=o):(o=!1,i=0),s.fin&&(this._firstFragment=!0);let l={[R]:a,fin:s.fin,generateMask:this._generateMask,mask:s.mask,maskBuffer:this._maskBuffer,opcode:i,readOnly:c,rsv1:o};se(e)?this._state!==I?this.enqueue([this.getBlobData,e,this._compress,l,r]):this.getBlobData(e,this._compress,l,r):this._state!==I?this.enqueue([this.dispatch,e,this._compress,l,r]):this.dispatch(e,this._compress,l,r)}getBlobData(e,s,r,n){this._bufferedBytes+=r[R],this._state=Fr,e.arrayBuffer().then(i=>{if(this._socket.destroyed){let a=new Error("The socket was closed while the blob was being read");process.nextTick(st,this,a,n);return}this._bufferedBytes-=r[R];let o=V(i);s?this.dispatch(o,s,r,n):(this._state=I,this.sendFrame(t.frame(o,r),n),this.dequeue())}).catch(i=>{process.nextTick(Ur,this,i,n)})}dispatch(e,s,r,n){if(!s){this.sendFrame(t.frame(e,r),n);return}let i=this._extensions[Wt.extensionName];this._bufferedBytes+=r[R],this._state=$r,i.compress(e,r.fin,(o,a)=>{if(this._socket.destroyed){let c=new Error("The socket was closed while data was being compressed");st(this,c,n);return}this._bufferedBytes-=r[R],this._state=I,r.readOnly=!1,this.sendFrame(t.frame(a,r),n),this.dequeue()})}dequeue(){for(;this._state===I&&this._queue.length;){let e=this._queue.shift();this._bufferedBytes-=e[3][R],Reflect.apply(e[0],this,e.slice(1))}}enqueue(e){this._bufferedBytes+=e[3][R],this._queue.push(e)}sendFrame(e,s){e.length===2?(this._socket.cork(),this._socket.write(e[0]),this._socket.write(e[1],s),this._socket.uncork()):this._socket.write(e[0],s)}};qt.exports=tt;function st(t,e,s){typeof s=="function"&&s(e);for(let r=0;r<t._queue.length;r++){let n=t._queue[r],i=n[n.length-1];typeof i=="function"&&i(e)}}function Ur(t,e,s){st(t,e,s),t.onerror(e)}});var Zt=C((vi,Xt)=>{"use strict";var{kForOnEventAttribute:ue,kListener:nt}=F(),Gt=Symbol("kCode"),Vt=Symbol("kData"),Yt=Symbol("kError"),Ht=Symbol("kMessage"),zt=Symbol("kReason"),ne=Symbol("kTarget"),Kt=Symbol("kType"),Jt=Symbol("kWasClean"),D=class{constructor(e){this[ne]=null,this[Kt]=e}get target(){return this[ne]}get type(){return this[Kt]}};Object.defineProperty(D.prototype,"target",{enumerable:!0});Object.defineProperty(D.prototype,"type",{enumerable:!0});var H=class extends D{constructor(e,s={}){super(e),this[Gt]=s.code===void 0?0:s.code,this[zt]=s.reason===void 0?"":s.reason,this[Jt]=s.wasClean===void 0?!1:s.wasClean}get code(){return this[Gt]}get reason(){return this[zt]}get wasClean(){return this[Jt]}};Object.defineProperty(H.prototype,"code",{enumerable:!0});Object.defineProperty(H.prototype,"reason",{enumerable:!0});Object.defineProperty(H.prototype,"wasClean",{enumerable:!0});var ie=class extends D{constructor(e,s={}){super(e),this[Yt]=s.error===void 0?null:s.error,this[Ht]=s.message===void 0?"":s.message}get error(){return this[Yt]}get message(){return this[Ht]}};Object.defineProperty(ie.prototype,"error",{enumerable:!0});Object.defineProperty(ie.prototype,"message",{enumerable:!0});var he=class extends D{constructor(e,s={}){super(e),this[Vt]=s.data===void 0?null:s.data}get data(){return this[Vt]}};Object.defineProperty(he.prototype,"data",{enumerable:!0});var Dr={addEventListener(t,e,s={}){for(let n of this.listeners(t))if(!s[ue]&&n[nt]===e&&!n[ue])return;let r;if(t==="message")r=function(i,o){let a=new he("message",{data:o?i:i.toString()});a[ne]=this,ke(e,this,a)};else if(t==="close")r=function(i,o){let a=new H("close",{code:i,reason:o.toString(),wasClean:this._closeFrameReceived&&this._closeFrameSent});a[ne]=this,ke(e,this,a)};else if(t==="error")r=function(i){let o=new ie("error",{error:i,message:i.message});o[ne]=this,ke(e,this,o)};else if(t==="open")r=function(){let i=new D("open");i[ne]=this,ke(e,this,i)};else return;r[ue]=!!s[ue],r[nt]=e,s.once?this.once(t,r):this.on(t,r)},removeEventListener(t,e){for(let s of this.listeners(t))if(s[nt]===e&&!s[ue]){this.removeListener(t,s);break}}};Xt.exports={CloseEvent:H,ErrorEvent:ie,Event:D,EventTarget:Dr,MessageEvent:he};function ke(t,e,s){typeof t=="object"&&t.handleEvent?t.handleEvent.call(t,s):t.call(e,s)}});var Ce=C((wi,Qt)=>{"use strict";var{tokenChars:de}=te();function M(t,e,s){t[e]===void 0?t[e]=[s]:t[e].push(s)}function Wr(t){let e=Object.create(null),s=Object.create(null),r=!1,n=!1,i=!1,o,a,c=-1,l=-1,h=-1,p=0;for(;p<t.length;p++)if(l=t.charCodeAt(p),o===void 0)if(h===-1&&de[l]===1)c===-1&&(c=p);else if(p!==0&&(l===32||l===9))h===-1&&c!==-1&&(h=p);else if(l===59||l===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${p}`);h===-1&&(h=p);let w=t.slice(c,h);l===44?(M(e,w,s),s=Object.create(null)):o=w,c=h=-1}else throw new SyntaxError(`Unexpected character at index ${p}`);else if(a===void 0)if(h===-1&&de[l]===1)c===-1&&(c=p);else if(l===32||l===9)h===-1&&c!==-1&&(h=p);else if(l===59||l===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${p}`);h===-1&&(h=p),M(s,t.slice(c,h),!0),l===44&&(M(e,o,s),s=Object.create(null),o=void 0),c=h=-1}else if(l===61&&c!==-1&&h===-1)a=t.slice(c,p),c=h=-1;else throw new SyntaxError(`Unexpected character at index ${p}`);else if(n){if(de[l]!==1)throw new SyntaxError(`Unexpected character at index ${p}`);c===-1?c=p:r||(r=!0),n=!1}else if(i)if(de[l]===1)c===-1&&(c=p);else if(l===34&&c!==-1)i=!1,h=p;else if(l===92)n=!0;else throw new SyntaxError(`Unexpected character at index ${p}`);else if(l===34&&t.charCodeAt(p-1)===61)i=!0;else if(h===-1&&de[l]===1)c===-1&&(c=p);else if(c!==-1&&(l===32||l===9))h===-1&&(h=p);else if(l===59||l===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${p}`);h===-1&&(h=p);let w=t.slice(c,h);r&&(w=w.replace(/\\/g,""),r=!1),M(s,a,w),l===44&&(M(e,o,s),s=Object.create(null),o=void 0),a=void 0,c=h=-1}else throw new SyntaxError(`Unexpected character at index ${p}`);if(c===-1||i||l===32||l===9)throw new SyntaxError("Unexpected end of input");h===-1&&(h=p);let g=t.slice(c,h);return o===void 0?M(e,g,s):(a===void 0?M(s,g,!0):r?M(s,a,g.replace(/\\/g,"")):M(s,a,g),M(e,o,s)),e}function jr(t){return Object.keys(t).map(e=>{let s=t[e];return Array.isArray(s)||(s=[s]),s.map(r=>[e].concat(Object.keys(r).map(n=>{let i=r[n];return Array.isArray(i)||(i=[i]),i.map(o=>o===!0?n:`${n}=${o}`).join("; ")})).join("; ")).join(", ")}).join(", ")}Qt.exports={format:jr,parse:Wr}});var Pe=C((ki,us)=>{"use strict";var qr=require("events"),Gr=require("https"),Vr=require("http"),ss=require("net"),Yr=require("tls"),{randomBytes:Hr,createHash:zr}=require("crypto"),{Duplex:Ti,Readable:Oi}=require("stream"),{URL:it}=require("url"),q=ee(),Kr=et(),Jr=rt(),{isBlob:Xr}=te(),{BINARY_TYPES:es,CLOSE_TIMEOUT:Zr,EMPTY_BUFFER:Ne,GUID:Qr,kForOnEventAttribute:ot,kListener:en,kStatusCode:tn,kWebSocket:O,NOOP:rs}=F(),{EventTarget:{addEventListener:sn,removeEventListener:rn}}=Zt(),{format:nn,parse:on}=Ce(),{toBuffer:an}=ce(),ns=Symbol("kAborted"),at=[8,13],W=["CONNECTING","OPEN","CLOSING","CLOSED"],ln=/^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/,b=class t extends qr{constructor(e,s,r){super(),this._binaryType=es[0],this._closeCode=1006,this._closeFrameReceived=!1,this._closeFrameSent=!1,this._closeMessage=Ne,this._closeTimer=null,this._errorEmitted=!1,this._extensions={},this._paused=!1,this._protocol="",this._readyState=t.CONNECTING,this._receiver=null,this._sender=null,this._socket=null,e!==null?(this._bufferedAmount=0,this._isServer=!1,this._redirects=0,s===void 0?s=[]:Array.isArray(s)||(typeof s=="object"&&s!==null?(r=s,s=[]):s=[s]),is(this,e,s,r)):(this._autoPong=r.autoPong,this._closeTimeout=r.closeTimeout,this._isServer=!0)}get binaryType(){return this._binaryType}set binaryType(e){es.includes(e)&&(this._binaryType=e,this._receiver&&(this._receiver._binaryType=e))}get bufferedAmount(){return this._socket?this._socket._writableState.length+this._sender._bufferedBytes:this._bufferedAmount}get extensions(){return Object.keys(this._extensions).join()}get isPaused(){return this._paused}get onclose(){return null}get onerror(){return null}get onopen(){return null}get onmessage(){return null}get protocol(){return this._protocol}get readyState(){return this._readyState}get url(){return this._url}setSocket(e,s,r){let n=new Kr({allowSynchronousEvents:r.allowSynchronousEvents,binaryType:this.binaryType,extensions:this._extensions,isServer:this._isServer,maxPayload:r.maxPayload,skipUTF8Validation:r.skipUTF8Validation}),i=new Jr(e,this._extensions,r.generateMask);this._receiver=n,this._sender=i,this._socket=e,n[O]=this,i[O]=this,e[O]=this,n.on("conclude",un),n.on("drain",hn),n.on("error",dn),n.on("message",pn),n.on("ping",_n),n.on("pong",mn),i.onerror=gn,e.setTimeout&&e.setTimeout(0),e.setNoDelay&&e.setNoDelay(),s.length>0&&e.unshift(s),e.on("close",ls),e.on("data",Re),e.on("end",cs),e.on("error",fs),this._readyState=t.OPEN,this.emit("open")}emitClose(){if(!this._socket){this._readyState=t.CLOSED,this.emit("close",this._closeCode,this._closeMessage);return}this._extensions[q.extensionName]&&this._extensions[q.extensionName].cleanup(),this._receiver.removeAllListeners(),this._readyState=t.CLOSED,this.emit("close",this._closeCode,this._closeMessage)}close(e,s){if(this.readyState!==t.CLOSED){if(this.readyState===t.CONNECTING){N(this,this._req,"WebSocket was closed before the connection was established");return}if(this.readyState===t.CLOSING){this._closeFrameSent&&(this._closeFrameReceived||this._receiver._writableState.errorEmitted)&&this._socket.end();return}this._readyState=t.CLOSING,this._sender.close(e,s,!this._isServer,r=>{r||(this._closeFrameSent=!0,(this._closeFrameReceived||this._receiver._writableState.errorEmitted)&&this._socket.end())}),as(this)}}pause(){this.readyState===t.CONNECTING||this.readyState===t.CLOSED||(this._paused=!0,this._socket.pause())}ping(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof e=="function"?(r=e,e=s=void 0):typeof s=="function"&&(r=s,s=void 0),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){lt(this,e,r);return}s===void 0&&(s=!this._isServer),this._sender.ping(e||Ne,s,r)}pong(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof e=="function"?(r=e,e=s=void 0):typeof s=="function"&&(r=s,s=void 0),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){lt(this,e,r);return}s===void 0&&(s=!this._isServer),this._sender.pong(e||Ne,s,r)}resume(){this.readyState===t.CONNECTING||this.readyState===t.CLOSED||(this._paused=!1,this._receiver._writableState.needDrain||this._socket.resume())}send(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof s=="function"&&(r=s,s={}),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){lt(this,e,r);return}let n={binary:typeof e!="string",mask:!this._isServer,compress:!0,fin:!0,...s};this._extensions[q.extensionName]||(n.compress=!1),this._sender.send(e||Ne,n,r)}terminate(){if(this.readyState!==t.CLOSED){if(this.readyState===t.CONNECTING){N(this,this._req,"WebSocket was closed before the connection was established");return}this._socket&&(this._readyState=t.CLOSING,this._socket.destroy())}}};Object.defineProperty(b,"CONNECTING",{enumerable:!0,value:W.indexOf("CONNECTING")});Object.defineProperty(b.prototype,"CONNECTING",{enumerable:!0,value:W.indexOf("CONNECTING")});Object.defineProperty(b,"OPEN",{enumerable:!0,value:W.indexOf("OPEN")});Object.defineProperty(b.prototype,"OPEN",{enumerable:!0,value:W.indexOf("OPEN")});Object.defineProperty(b,"CLOSING",{enumerable:!0,value:W.indexOf("CLOSING")});Object.defineProperty(b.prototype,"CLOSING",{enumerable:!0,value:W.indexOf("CLOSING")});Object.defineProperty(b,"CLOSED",{enumerable:!0,value:W.indexOf("CLOSED")});Object.defineProperty(b.prototype,"CLOSED",{enumerable:!0,value:W.indexOf("CLOSED")});["binaryType","bufferedAmount","extensions","isPaused","protocol","readyState","url"].forEach(t=>{Object.defineProperty(b.prototype,t,{enumerable:!0})});["open","error","close","message"].forEach(t=>{Object.defineProperty(b.prototype,`on${t}`,{enumerable:!0,get(){for(let e of this.listeners(t))if(e[ot])return e[en];return null},set(e){for(let s of this.listeners(t))if(s[ot]){this.removeListener(t,s);break}typeof e=="function"&&this.addEventListener(t,e,{[ot]:!0})}})});b.prototype.addEventListener=sn;b.prototype.removeEventListener=rn;us.exports=b;function is(t,e,s,r){let n={allowSynchronousEvents:!0,autoPong:!0,closeTimeout:Zr,protocolVersion:at[1],maxPayload:104857600,skipUTF8Validation:!1,perMessageDeflate:!0,followRedirects:!1,maxRedirects:10,...r,socketPath:void 0,hostname:void 0,protocol:void 0,timeout:void 0,method:"GET",host:void 0,path:void 0,port:void 0};if(t._autoPong=n.autoPong,t._closeTimeout=n.closeTimeout,!at.includes(n.protocolVersion))throw new RangeError(`Unsupported protocol version: ${n.protocolVersion} (supported versions: ${at.join(", ")})`);let i;if(e instanceof it)i=e;else try{i=new it(e)}catch{throw new SyntaxError(`Invalid URL: ${e}`)}i.protocol==="http:"?i.protocol="ws:":i.protocol==="https:"&&(i.protocol="wss:"),t._url=i.href;let o=i.protocol==="wss:",a=i.protocol==="ws+unix:",c;if(i.protocol!=="ws:"&&!o&&!a?c=`The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`:a&&!i.pathname?c="The URL's pathname is empty":i.hash&&(c="The URL contains a fragment identifier"),c){let _=new SyntaxError(c);if(t._redirects===0)throw _;Le(t,_);return}let l=o?443:80,h=Hr(16).toString("base64"),p=o?Gr.request:Vr.request,g=new Set,w;if(n.createConnection=n.createConnection||(o?fn:cn),n.defaultPort=n.defaultPort||l,n.port=i.port||l,n.host=i.hostname.startsWith("[")?i.hostname.slice(1,-1):i.hostname,n.headers={...n.headers,"Sec-WebSocket-Version":n.protocolVersion,"Sec-WebSocket-Key":h,Connection:"Upgrade",Upgrade:"websocket"},n.path=i.pathname+i.search,n.timeout=n.handshakeTimeout,n.perMessageDeflate&&(w=new q({...n.perMessageDeflate,isServer:!1,maxPayload:n.maxPayload}),n.headers["Sec-WebSocket-Extensions"]=nn({[q.extensionName]:w.offer()})),s.length){for(let _ of s){if(typeof _!="string"||!ln.test(_)||g.has(_))throw new SyntaxError("An invalid or duplicated subprotocol was specified");g.add(_)}n.headers["Sec-WebSocket-Protocol"]=s.join(",")}if(n.origin&&(n.protocolVersion<13?n.headers["Sec-WebSocket-Origin"]=n.origin:n.headers.Origin=n.origin),(i.username||i.password)&&(n.auth=`${i.username}:${i.password}`),a){let _=n.path.split(":");n.socketPath=_[0],n.path=_[1]}let m;if(n.followRedirects){if(t._redirects===0){t._originalIpc=a,t._originalSecure=o,t._originalHostOrSocketPath=a?n.socketPath:i.host;let _=r&&r.headers;if(r={...r,headers:{}},_)for(let[f,d]of Object.entries(_))r.headers[f.toLowerCase()]=d}else if(t.listenerCount("redirect")===0){let _=a?t._originalIpc?n.socketPath===t._originalHostOrSocketPath:!1:t._originalIpc?!1:i.host===t._originalHostOrSocketPath;(!_||t._originalSecure&&!o)&&(delete n.headers.authorization,delete n.headers.cookie,_||delete n.headers.host,n.auth=void 0)}n.auth&&!r.headers.authorization&&(r.headers.authorization="Basic "+Buffer.from(n.auth).toString("base64")),m=t._req=p(n),t._redirects&&t.emit("redirect",t.url,m)}else m=t._req=p(n);n.timeout&&m.on("timeout",()=>{N(t,m,"Opening handshake has timed out")}),m.on("error",_=>{m===null||m[ns]||(m=t._req=null,Le(t,_))}),m.on("response",_=>{let f=_.headers.location,d=_.statusCode;if(f&&n.followRedirects&&d>=300&&d<400){if(++t._redirects>n.maxRedirects){N(t,m,"Maximum redirects exceeded");return}m.abort();let S;try{S=new it(f,e)}catch{let y=new SyntaxError(`Invalid URL: ${f}`);Le(t,y);return}is(t,S,s,r)}else t.emit("unexpected-response",m,_)||N(t,m,`Unexpected server response: ${_.statusCode}`)}),m.on("upgrade",(_,f,d)=>{if(t.emit("upgrade",_),t.readyState!==b.CONNECTING)return;m=t._req=null;let S=_.headers.upgrade;if(S===void 0||S.toLowerCase()!=="websocket"){N(t,f,"Invalid Upgrade header");return}let T=zr("sha1").update(h+Qr).digest("base64");if(_.headers["sec-websocket-accept"]!==T){N(t,f,"Invalid Sec-WebSocket-Accept header");return}let y=_.headers["sec-websocket-protocol"],B;if(y!==void 0?g.size?g.has(y)||(B="Server sent an invalid subprotocol"):B="Server sent a subprotocol but none was requested":g.size&&(B="Server sent no subprotocol"),B){N(t,f,B);return}y&&(t._protocol=y);let X=_.headers["sec-websocket-extensions"];if(X!==void 0){if(!w){N(t,f,"Server sent a Sec-WebSocket-Extensions header but no extension was requested");return}let G;try{G=on(X)}catch{N(t,f,"Invalid Sec-WebSocket-Extensions header");return}let St=Object.keys(G);if(St.length!==1||St[0]!==q.extensionName){N(t,f,"Server indicated an extension that was not requested");return}try{w.accept(G[q.extensionName])}catch{N(t,f,"Invalid Sec-WebSocket-Extensions header");return}t._extensions[q.extensionName]=w}t.setSocket(f,d,{allowSynchronousEvents:n.allowSynchronousEvents,generateMask:n.generateMask,maxPayload:n.maxPayload,skipUTF8Validation:n.skipUTF8Validation})}),n.finishRequest?n.finishRequest(m,t):m.end()}function Le(t,e){t._readyState=b.CLOSING,t._errorEmitted=!0,t.emit("error",e),t.emitClose()}function cn(t){return t.path=t.socketPath,ss.connect(t)}function fn(t){return t.path=void 0,!t.servername&&t.servername!==""&&(t.servername=ss.isIP(t.host)?"":t.host),Yr.connect(t)}function N(t,e,s){t._readyState=b.CLOSING;let r=new Error(s);Error.captureStackTrace(r,N),e.setHeader?(e[ns]=!0,e.abort(),e.socket&&!e.socket.destroyed&&e.socket.destroy(),process.nextTick(Le,t,r)):(e.destroy(r),e.once("error",t.emit.bind(t,"error")),e.once("close",t.emitClose.bind(t)))}function lt(t,e,s){if(e){let r=Xr(e)?e.size:an(e).length;t._socket?t._sender._bufferedBytes+=r:t._bufferedAmount+=r}if(s){let r=new Error(`WebSocket is not open: readyState ${t.readyState} (${W[t.readyState]})`);process.nextTick(s,r)}}function un(t,e){let s=this[O];s._closeFrameReceived=!0,s._closeMessage=e,s._closeCode=t,s._socket[O]!==void 0&&(s._socket.removeListener("data",Re),process.nextTick(os,s._socket),t===1005?s.close():s.close(t,e))}function hn(){let t=this[O];t.isPaused||t._socket.resume()}function dn(t){let e=this[O];e._socket[O]!==void 0&&(e._socket.removeListener("data",Re),process.nextTick(os,e._socket),e.close(t[tn])),e._errorEmitted||(e._errorEmitted=!0,e.emit("error",t))}function ts(){this[O].emitClose()}function pn(t,e){this[O].emit("message",t,e)}function _n(t){let e=this[O];e._autoPong&&e.pong(t,!this._isServer,rs),e.emit("ping",t)}function mn(t){this[O].emit("pong",t)}function os(t){t.resume()}function gn(t){let e=this[O];e.readyState!==b.CLOSED&&(e.readyState===b.OPEN&&(e._readyState=b.CLOSING,as(e)),this._socket.end(),e._errorEmitted||(e._errorEmitted=!0,e.emit("error",t)))}function as(t){t._closeTimer=setTimeout(t._socket.destroy.bind(t._socket),t._closeTimeout)}function ls(){let t=this[O];if(this.removeListener("close",ls),this.removeListener("data",Re),this.removeListener("end",cs),t._readyState=b.CLOSING,!this._readableState.endEmitted&&!t._closeFrameReceived&&!t._receiver._writableState.errorEmitted&&this._readableState.length!==0){let e=this.read(this._readableState.length);t._receiver.write(e)}t._receiver.end(),this[O]=void 0,clearTimeout(t._closeTimer),t._receiver._writableState.finished||t._receiver._writableState.errorEmitted?t.emitClose():(t._receiver.on("error",ts),t._receiver.on("finish",ts))}function Re(t){this[O]._receiver.write(t)||this.pause()}function cs(){let t=this[O];t._readyState=b.CLOSING,t._receiver.end(),this.end()}function fs(){let t=this[O];this.removeListener("error",fs),this.on("error",rs),t&&(t._readyState=b.CLOSING,this.destroy())}});var _s=C((Ni,ps)=>{"use strict";var Ci=Pe(),{Duplex:yn}=require("stream");function hs(t){t.emit("close")}function Sn(){!this.destroyed&&this._writableState.finished&&this.destroy()}function ds(t){this.removeListener("error",ds),this.destroy(),this.listenerCount("error")===0&&this.emit("error",t)}function xn(t,e){let s=!0,r=new yn({...e,autoDestroy:!1,emitClose:!1,objectMode:!1,writableObjectMode:!1});return t.on("message",function(i,o){let a=!o&&r._readableState.objectMode?i.toString():i;r.push(a)||t.pause()}),t.once("error",function(i){r.destroyed||(s=!1,r.destroy(i))}),t.once("close",function(){r.destroyed||r.push(null)}),r._destroy=function(n,i){if(t.readyState===t.CLOSED){i(n),process.nextTick(hs,r);return}let o=!1;t.once("error",function(c){o=!0,i(c)}),t.once("close",function(){o||i(n),process.nextTick(hs,r)}),s&&t.terminate()},r._final=function(n){if(t.readyState===t.CONNECTING){t.once("open",function(){r._final(n)});return}t._socket!==null&&(t._socket._writableState.finished?(n(),r._readableState.endEmitted&&r.destroy()):(t._socket.once("finish",function(){n()}),t.close()))},r._read=function(){t.isPaused&&t.resume()},r._write=function(n,i,o){if(t.readyState===t.CONNECTING){t.once("open",function(){r._write(n,i,o)});return}t.send(n,o)},r.on("end",Sn),r.on("error",ds),r}ps.exports=xn});var ct=C((Li,ms)=>{"use strict";var{tokenChars:bn}=te();function En(t){let e=new Set,s=-1,r=-1,n=0;for(n;n<t.length;n++){let o=t.charCodeAt(n);if(r===-1&&bn[o]===1)s===-1&&(s=n);else if(n!==0&&(o===32||o===9))r===-1&&s!==-1&&(r=n);else if(o===44){if(s===-1)throw new SyntaxError(`Unexpected character at index ${n}`);r===-1&&(r=n);let a=t.slice(s,r);if(e.has(a))throw new SyntaxError(`The "${a}" subprotocol is duplicated`);e.add(a),s=r=-1}else throw new SyntaxError(`Unexpected character at index ${n}`)}if(s===-1||r!==-1)throw new SyntaxError("Unexpected end of input");let i=t.slice(s,n);if(e.has(i))throw new SyntaxError(`The "${i}" subprotocol is duplicated`);return e.add(i),e}ms.exports={parse:En}});var vs=C((Pi,Es)=>{"use strict";var vn=require("events"),Be=require("http"),{Duplex:Ri}=require("stream"),{createHash:wn}=require("crypto"),gs=Ce(),z=ee(),Tn=ct(),On=Pe(),{CLOSE_TIMEOUT:kn,GUID:Cn,kWebSocket:Nn}=F(),Ln=/^[+/0-9A-Za-z]{22}==$/,ys=0,Ss=1,bs=2,ft=class extends vn{constructor(e,s){if(super(),e={allowSynchronousEvents:!0,autoPong:!0,maxPayload:100*1024*1024,skipUTF8Validation:!1,perMessageDeflate:!1,handleProtocols:null,clientTracking:!0,closeTimeout:kn,verifyClient:null,noServer:!1,backlog:null,server:null,host:null,path:null,port:null,WebSocket:On,...e},e.port==null&&!e.server&&!e.noServer||e.port!=null&&(e.server||e.noServer)||e.server&&e.noServer)throw new TypeError('One and only one of the "port", "server", or "noServer" options must be specified');if(e.port!=null?(this._server=Be.createServer((r,n)=>{let i=Be.STATUS_CODES[426];n.writeHead(426,{"Content-Length":i.length,"Content-Type":"text/plain"}),n.end(i)}),this._server.listen(e.port,e.host,e.backlog,s)):e.server&&(this._server=e.server),this._server){let r=this.emit.bind(this,"connection");this._removeListeners=Rn(this._server,{listening:this.emit.bind(this,"listening"),error:this.emit.bind(this,"error"),upgrade:(n,i,o)=>{this.handleUpgrade(n,i,o,r)}})}e.perMessageDeflate===!0&&(e.perMessageDeflate={}),e.clientTracking&&(this.clients=new Set,this._shouldEmitClose=!1),this.options=e,this._state=ys}address(){if(this.options.noServer)throw new Error('The server is operating in "noServer" mode');return this._server?this._server.address():null}close(e){if(this._state===bs){e&&this.once("close",()=>{e(new Error("The server is not running"))}),process.nextTick(pe,this);return}if(e&&this.once("close",e),this._state!==Ss)if(this._state=Ss,this.options.noServer||this.options.server)this._server&&(this._removeListeners(),this._removeListeners=this._server=null),this.clients?this.clients.size?this._shouldEmitClose=!0:process.nextTick(pe,this):process.nextTick(pe,this);else{let s=this._server;this._removeListeners(),this._removeListeners=this._server=null,s.close(()=>{pe(this)})}}shouldHandle(e){if(this.options.path){let s=e.url.indexOf("?");if((s!==-1?e.url.slice(0,s):e.url)!==this.options.path)return!1}return!0}handleUpgrade(e,s,r,n){s.on("error",xs);let i=e.headers["sec-websocket-key"],o=e.headers.upgrade,a=+e.headers["sec-websocket-version"];if(e.method!=="GET"){K(this,e,s,405,"Invalid HTTP method");return}if(o===void 0||o.toLowerCase()!=="websocket"){K(this,e,s,400,"Invalid Upgrade header");return}if(i===void 0||!Ln.test(i)){K(this,e,s,400,"Missing or invalid Sec-WebSocket-Key header");return}if(a!==13&&a!==8){K(this,e,s,400,"Missing or invalid Sec-WebSocket-Version header",{"Sec-WebSocket-Version":"13, 8"});return}if(!this.shouldHandle(e)){_e(s,400);return}let c=e.headers["sec-websocket-protocol"],l=new Set;if(c!==void 0)try{l=Tn.parse(c)}catch{K(this,e,s,400,"Invalid Sec-WebSocket-Protocol header");return}let h=e.headers["sec-websocket-extensions"],p={};if(this.options.perMessageDeflate&&h!==void 0){let g=new z({...this.options.perMessageDeflate,isServer:!0,maxPayload:this.options.maxPayload});try{let w=gs.parse(h);w[z.extensionName]&&(g.accept(w[z.extensionName]),p[z.extensionName]=g)}catch{K(this,e,s,400,"Invalid or unacceptable Sec-WebSocket-Extensions header");return}}if(this.options.verifyClient){let g={origin:e.headers[`${a===8?"sec-websocket-origin":"origin"}`],secure:!!(e.socket.authorized||e.socket.encrypted),req:e};if(this.options.verifyClient.length===2){this.options.verifyClient(g,(w,m,_,f)=>{if(!w)return _e(s,m||401,_,f);this.completeUpgrade(p,i,l,e,s,r,n)});return}if(!this.options.verifyClient(g))return _e(s,401)}this.completeUpgrade(p,i,l,e,s,r,n)}completeUpgrade(e,s,r,n,i,o,a){if(!i.readable||!i.writable)return i.destroy();if(i[Nn])throw new Error("server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration");if(this._state>ys)return _e(i,503);let l=["HTTP/1.1 101 Switching Protocols","Upgrade: websocket","Connection: Upgrade",`Sec-WebSocket-Accept: ${wn("sha1").update(s+Cn).digest("base64")}`],h=new this.options.WebSocket(null,void 0,this.options);if(r.size){let p=this.options.handleProtocols?this.options.handleProtocols(r,n):r.values().next().value;p&&(l.push(`Sec-WebSocket-Protocol: ${p}`),h._protocol=p)}if(e[z.extensionName]){let p=e[z.extensionName].params,g=gs.format({[z.extensionName]:[p]});l.push(`Sec-WebSocket-Extensions: ${g}`),h._extensions=e}this.emit("headers",l,n),i.write(l.concat(`\r
|
|
2
|
+
"use strict";var ur=Object.create;var Tt=Object.defineProperty;var hr=Object.getOwnPropertyDescriptor;var dr=Object.getOwnPropertyNames;var pr=Object.getPrototypeOf,_r=Object.prototype.hasOwnProperty;var R=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var mr=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of dr(e))!_r.call(t,n)&&n!==s&&Tt(t,n,{get:()=>e[n],enumerable:!(r=hr(e,n))||r.enumerable});return t};var x=(t,e,s)=>(s=t!=null?ur(pr(t)):{},mr(e||!t||!t.__esModule?Tt(s,"default",{value:t,enumerable:!0}):s,t));var j=R((xi,Ct)=>{"use strict";var Ot=["nodebuffer","arraybuffer","fragments"],kt=typeof Blob<"u";kt&&Ot.push("blob");Ct.exports={BINARY_TYPES:Ot,CLOSE_TIMEOUT:3e4,EMPTY_BUFFER:Buffer.alloc(0),GUID:"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",hasBlob:kt,kForOnEventAttribute:Symbol("kIsForOnEventAttribute"),kListener:Symbol("kListener"),kStatusCode:Symbol("status-code"),kWebSocket:Symbol("websocket"),NOOP:()=>{}}});var pe=R((bi,Te)=>{"use strict";var{EMPTY_BUFFER:gr}=j(),Ke=Buffer[Symbol.species];function yr(t,e){if(t.length===0)return gr;if(t.length===1)return t[0];let s=Buffer.allocUnsafe(e),r=0;for(let n=0;n<t.length;n++){let i=t[n];s.set(i,r),r+=i.length}return r<e?new Ke(s.buffer,s.byteOffset,r):s}function Nt(t,e,s,r,n){for(let i=0;i<n;i++)s[r+i]=t[i]^e[i&3]}function Lt(t,e){for(let s=0;s<t.length;s++)t[s]^=e[s&3]}function Sr(t){return t.length===t.buffer.byteLength?t.buffer:t.buffer.slice(t.byteOffset,t.byteOffset+t.length)}function Je(t){if(Je.readOnly=!0,Buffer.isBuffer(t))return t;let e;return t instanceof ArrayBuffer?e=new Ke(t):ArrayBuffer.isView(t)?e=new Ke(t.buffer,t.byteOffset,t.byteLength):(e=Buffer.from(t),Je.readOnly=!1),e}Te.exports={concat:yr,mask:Nt,toArrayBuffer:Sr,toBuffer:Je,unmask:Lt};if(!process.env.WS_NO_BUFFER_UTIL)try{let t=require("bufferutil");Te.exports.mask=function(e,s,r,n,i){i<48?Nt(e,s,r,n,i):t.mask(e,s,r,n,i)},Te.exports.unmask=function(e,s){e.length<32?Lt(e,s):t.unmask(e,s)}}catch{}});var Bt=R((Ei,Pt)=>{"use strict";var Rt=Symbol("kDone"),Xe=Symbol("kRun"),Ze=class{constructor(e){this[Rt]=()=>{this.pending--,this[Xe]()},this.concurrency=e||1/0,this.jobs=[],this.pending=0}add(e){this.jobs.push(e),this[Xe]()}[Xe](){if(this.pending!==this.concurrency&&this.jobs.length){let e=this.jobs.shift();this.pending++,e(this[Rt])}}};Pt.exports=Ze});var se=R((vi,$t)=>{"use strict";var _e=require("zlib"),It=pe(),xr=Bt(),{kStatusCode:At}=j(),br=Buffer[Symbol.species],Er=Buffer.from([0,0,255,255]),ke=Symbol("permessage-deflate"),q=Symbol("total-length"),ee=Symbol("callback"),Y=Symbol("buffers"),te=Symbol("error"),Oe,Qe=class{constructor(e){if(this._options=e||{},this._threshold=this._options.threshold!==void 0?this._options.threshold:1024,this._maxPayload=this._options.maxPayload|0,this._isServer=!!this._options.isServer,this._deflate=null,this._inflate=null,this.params=null,!Oe){let s=this._options.concurrencyLimit!==void 0?this._options.concurrencyLimit:10;Oe=new xr(s)}}static get extensionName(){return"permessage-deflate"}offer(){let e={};return this._options.serverNoContextTakeover&&(e.server_no_context_takeover=!0),this._options.clientNoContextTakeover&&(e.client_no_context_takeover=!0),this._options.serverMaxWindowBits&&(e.server_max_window_bits=this._options.serverMaxWindowBits),this._options.clientMaxWindowBits?e.client_max_window_bits=this._options.clientMaxWindowBits:this._options.clientMaxWindowBits==null&&(e.client_max_window_bits=!0),e}accept(e){return e=this.normalizeParams(e),this.params=this._isServer?this.acceptAsServer(e):this.acceptAsClient(e),this.params}cleanup(){if(this._inflate&&(this._inflate.close(),this._inflate=null),this._deflate){let e=this._deflate[ee];this._deflate.close(),this._deflate=null,e&&e(new Error("The deflate stream was closed while data was being processed"))}}acceptAsServer(e){let s=this._options,r=e.find(n=>!(s.serverNoContextTakeover===!1&&n.server_no_context_takeover||n.server_max_window_bits&&(s.serverMaxWindowBits===!1||typeof s.serverMaxWindowBits=="number"&&s.serverMaxWindowBits>n.server_max_window_bits)||typeof s.clientMaxWindowBits=="number"&&!n.client_max_window_bits));if(!r)throw new Error("None of the extension offers can be accepted");return s.serverNoContextTakeover&&(r.server_no_context_takeover=!0),s.clientNoContextTakeover&&(r.client_no_context_takeover=!0),typeof s.serverMaxWindowBits=="number"&&(r.server_max_window_bits=s.serverMaxWindowBits),typeof s.clientMaxWindowBits=="number"?r.client_max_window_bits=s.clientMaxWindowBits:(r.client_max_window_bits===!0||s.clientMaxWindowBits===!1)&&delete r.client_max_window_bits,r}acceptAsClient(e){let s=e[0];if(this._options.clientNoContextTakeover===!1&&s.client_no_context_takeover)throw new Error('Unexpected parameter "client_no_context_takeover"');if(!s.client_max_window_bits)typeof this._options.clientMaxWindowBits=="number"&&(s.client_max_window_bits=this._options.clientMaxWindowBits);else if(this._options.clientMaxWindowBits===!1||typeof this._options.clientMaxWindowBits=="number"&&s.client_max_window_bits>this._options.clientMaxWindowBits)throw new Error('Unexpected or invalid parameter "client_max_window_bits"');return s}normalizeParams(e){return e.forEach(s=>{Object.keys(s).forEach(r=>{let n=s[r];if(n.length>1)throw new Error(`Parameter "${r}" must have only a single value`);if(n=n[0],r==="client_max_window_bits"){if(n!==!0){let i=+n;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${n}`);n=i}else if(!this._isServer)throw new TypeError(`Invalid value for parameter "${r}": ${n}`)}else if(r==="server_max_window_bits"){let i=+n;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${n}`);n=i}else if(r==="client_no_context_takeover"||r==="server_no_context_takeover"){if(n!==!0)throw new TypeError(`Invalid value for parameter "${r}": ${n}`)}else throw new Error(`Unknown parameter "${r}"`);s[r]=n})}),e}decompress(e,s,r){Oe.add(n=>{this._decompress(e,s,(i,o)=>{n(),r(i,o)})})}compress(e,s,r){Oe.add(n=>{this._compress(e,s,(i,o)=>{n(),r(i,o)})})}_decompress(e,s,r){let n=this._isServer?"client":"server";if(!this._inflate){let i=`${n}_max_window_bits`,o=typeof this.params[i]!="number"?_e.Z_DEFAULT_WINDOWBITS:this.params[i];this._inflate=_e.createInflateRaw({...this._options.zlibInflateOptions,windowBits:o}),this._inflate[ke]=this,this._inflate[q]=0,this._inflate[Y]=[],this._inflate.on("error",wr),this._inflate.on("data",Mt)}this._inflate[ee]=r,this._inflate.write(e),s&&this._inflate.write(Er),this._inflate.flush(()=>{let i=this._inflate[te];if(i){this._inflate.close(),this._inflate=null,r(i);return}let o=It.concat(this._inflate[Y],this._inflate[q]);this._inflate._readableState.endEmitted?(this._inflate.close(),this._inflate=null):(this._inflate[q]=0,this._inflate[Y]=[],s&&this.params[`${n}_no_context_takeover`]&&this._inflate.reset()),r(null,o)})}_compress(e,s,r){let n=this._isServer?"server":"client";if(!this._deflate){let i=`${n}_max_window_bits`,o=typeof this.params[i]!="number"?_e.Z_DEFAULT_WINDOWBITS:this.params[i];this._deflate=_e.createDeflateRaw({...this._options.zlibDeflateOptions,windowBits:o}),this._deflate[q]=0,this._deflate[Y]=[],this._deflate.on("data",vr)}this._deflate[ee]=r,this._deflate.write(e),this._deflate.flush(_e.Z_SYNC_FLUSH,()=>{if(!this._deflate)return;let i=It.concat(this._deflate[Y],this._deflate[q]);s&&(i=new br(i.buffer,i.byteOffset,i.length-4)),this._deflate[ee]=null,this._deflate[q]=0,this._deflate[Y]=[],s&&this.params[`${n}_no_context_takeover`]&&this._deflate.reset(),r(null,i)})}};$t.exports=Qe;function vr(t){this[Y].push(t),this[q]+=t.length}function Mt(t){if(this[q]+=t.length,this[ke]._maxPayload<1||this[q]<=this[ke]._maxPayload){this[Y].push(t);return}this[te]=new RangeError("Max payload size exceeded"),this[te].code="WS_ERR_UNSUPPORTED_MESSAGE_LENGTH",this[te][At]=1009,this.removeListener("data",Mt),this.reset()}function wr(t){if(this[ke]._inflate=null,this[te]){this[ee](this[te]);return}t[At]=1007,this[ee](t)}});var re=R((wi,Ce)=>{"use strict";var{isUtf8:Ft}=require("buffer"),{hasBlob:Tr}=j(),Or=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0];function kr(t){return t>=1e3&&t<=1014&&t!==1004&&t!==1005&&t!==1006||t>=3e3&&t<=4999}function et(t){let e=t.length,s=0;for(;s<e;)if((t[s]&128)===0)s++;else if((t[s]&224)===192){if(s+1===e||(t[s+1]&192)!==128||(t[s]&254)===192)return!1;s+=2}else if((t[s]&240)===224){if(s+2>=e||(t[s+1]&192)!==128||(t[s+2]&192)!==128||t[s]===224&&(t[s+1]&224)===128||t[s]===237&&(t[s+1]&224)===160)return!1;s+=3}else if((t[s]&248)===240){if(s+3>=e||(t[s+1]&192)!==128||(t[s+2]&192)!==128||(t[s+3]&192)!==128||t[s]===240&&(t[s+1]&240)===128||t[s]===244&&t[s+1]>143||t[s]>244)return!1;s+=4}else return!1;return!0}function Cr(t){return Tr&&typeof t=="object"&&typeof t.arrayBuffer=="function"&&typeof t.type=="string"&&typeof t.stream=="function"&&(t[Symbol.toStringTag]==="Blob"||t[Symbol.toStringTag]==="File")}Ce.exports={isBlob:Cr,isValidStatusCode:kr,isValidUTF8:et,tokenChars:Or};if(Ft)Ce.exports.isValidUTF8=function(t){return t.length<24?et(t):Ft(t)};else if(!process.env.WS_NO_UTF_8_VALIDATE)try{let t=require("utf-8-validate");Ce.exports.isValidUTF8=function(e){return e.length<32?et(e):t(e)}}catch{}});var it=R((Ti,Vt)=>{"use strict";var{Writable:Nr}=require("stream"),Ut=se(),{BINARY_TYPES:Lr,EMPTY_BUFFER:Dt,kStatusCode:Rr,kWebSocket:Pr}=j(),{concat:tt,toArrayBuffer:Br,unmask:Ir}=pe(),{isValidStatusCode:Ar,isValidUTF8:Wt}=re(),Ne=Buffer[Symbol.species],B=0,jt=1,qt=2,Gt=3,st=4,rt=5,Le=6,nt=class extends Nr{constructor(e={}){super(),this._allowSynchronousEvents=e.allowSynchronousEvents!==void 0?e.allowSynchronousEvents:!0,this._binaryType=e.binaryType||Lr[0],this._extensions=e.extensions||{},this._isServer=!!e.isServer,this._maxPayload=e.maxPayload|0,this._skipUTF8Validation=!!e.skipUTF8Validation,this[Pr]=void 0,this._bufferedBytes=0,this._buffers=[],this._compressed=!1,this._payloadLength=0,this._mask=void 0,this._fragmented=0,this._masked=!1,this._fin=!1,this._opcode=0,this._totalPayloadLength=0,this._messageLength=0,this._fragments=[],this._errored=!1,this._loop=!1,this._state=B}_write(e,s,r){if(this._opcode===8&&this._state==B)return r();this._bufferedBytes+=e.length,this._buffers.push(e),this.startLoop(r)}consume(e){if(this._bufferedBytes-=e,e===this._buffers[0].length)return this._buffers.shift();if(e<this._buffers[0].length){let r=this._buffers[0];return this._buffers[0]=new Ne(r.buffer,r.byteOffset+e,r.length-e),new Ne(r.buffer,r.byteOffset,e)}let s=Buffer.allocUnsafe(e);do{let r=this._buffers[0],n=s.length-e;e>=r.length?s.set(this._buffers.shift(),n):(s.set(new Uint8Array(r.buffer,r.byteOffset,e),n),this._buffers[0]=new Ne(r.buffer,r.byteOffset+e,r.length-e)),e-=r.length}while(e>0);return s}startLoop(e){this._loop=!0;do switch(this._state){case B:this.getInfo(e);break;case jt:this.getPayloadLength16(e);break;case qt:this.getPayloadLength64(e);break;case Gt:this.getMask();break;case st:this.getData(e);break;case rt:case Le:this._loop=!1;return}while(this._loop);this._errored||e()}getInfo(e){if(this._bufferedBytes<2){this._loop=!1;return}let s=this.consume(2);if((s[0]&48)!==0){let n=this.createError(RangeError,"RSV2 and RSV3 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_2_3");e(n);return}let r=(s[0]&64)===64;if(r&&!this._extensions[Ut.extensionName]){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(this._fin=(s[0]&128)===128,this._opcode=s[0]&15,this._payloadLength=s[1]&127,this._opcode===0){if(r){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(!this._fragmented){let n=this.createError(RangeError,"invalid opcode 0",!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}this._opcode=this._fragmented}else if(this._opcode===1||this._opcode===2){if(this._fragmented){let n=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}this._compressed=r}else if(this._opcode>7&&this._opcode<11){if(!this._fin){let n=this.createError(RangeError,"FIN must be set",!0,1002,"WS_ERR_EXPECTED_FIN");e(n);return}if(r){let n=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(n);return}if(this._payloadLength>125||this._opcode===8&&this._payloadLength===1){let n=this.createError(RangeError,`invalid payload length ${this._payloadLength}`,!0,1002,"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");e(n);return}}else{let n=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(n);return}if(!this._fin&&!this._fragmented&&(this._fragmented=this._opcode),this._masked=(s[1]&128)===128,this._isServer){if(!this._masked){let n=this.createError(RangeError,"MASK must be set",!0,1002,"WS_ERR_EXPECTED_MASK");e(n);return}}else if(this._masked){let n=this.createError(RangeError,"MASK must be clear",!0,1002,"WS_ERR_UNEXPECTED_MASK");e(n);return}this._payloadLength===126?this._state=jt:this._payloadLength===127?this._state=qt:this.haveLength(e)}getPayloadLength16(e){if(this._bufferedBytes<2){this._loop=!1;return}this._payloadLength=this.consume(2).readUInt16BE(0),this.haveLength(e)}getPayloadLength64(e){if(this._bufferedBytes<8){this._loop=!1;return}let s=this.consume(8),r=s.readUInt32BE(0);if(r>Math.pow(2,21)-1){let n=this.createError(RangeError,"Unsupported WebSocket frame: payload length > 2^53 - 1",!1,1009,"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");e(n);return}this._payloadLength=r*Math.pow(2,32)+s.readUInt32BE(4),this.haveLength(e)}haveLength(e){if(this._payloadLength&&this._opcode<8&&(this._totalPayloadLength+=this._payloadLength,this._totalPayloadLength>this._maxPayload&&this._maxPayload>0)){let s=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");e(s);return}this._masked?this._state=Gt:this._state=st}getMask(){if(this._bufferedBytes<4){this._loop=!1;return}this._mask=this.consume(4),this._state=st}getData(e){let s=Dt;if(this._payloadLength){if(this._bufferedBytes<this._payloadLength){this._loop=!1;return}s=this.consume(this._payloadLength),this._masked&&(this._mask[0]|this._mask[1]|this._mask[2]|this._mask[3])!==0&&Ir(s,this._mask)}if(this._opcode>7){this.controlMessage(s,e);return}if(this._compressed){this._state=rt,this.decompress(s,e);return}s.length&&(this._messageLength=this._totalPayloadLength,this._fragments.push(s)),this.dataMessage(e)}decompress(e,s){this._extensions[Ut.extensionName].decompress(e,this._fin,(n,i)=>{if(n)return s(n);if(i.length){if(this._messageLength+=i.length,this._messageLength>this._maxPayload&&this._maxPayload>0){let o=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");s(o);return}this._fragments.push(i)}this.dataMessage(s),this._state===B&&this.startLoop(s)})}dataMessage(e){if(!this._fin){this._state=B;return}let s=this._messageLength,r=this._fragments;if(this._totalPayloadLength=0,this._messageLength=0,this._fragmented=0,this._fragments=[],this._opcode===2){let n;this._binaryType==="nodebuffer"?n=tt(r,s):this._binaryType==="arraybuffer"?n=Br(tt(r,s)):this._binaryType==="blob"?n=new Blob(r):n=r,this._allowSynchronousEvents?(this.emit("message",n,!0),this._state=B):(this._state=Le,setImmediate(()=>{this.emit("message",n,!0),this._state=B,this.startLoop(e)}))}else{let n=tt(r,s);if(!this._skipUTF8Validation&&!Wt(n)){let i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");e(i);return}this._state===rt||this._allowSynchronousEvents?(this.emit("message",n,!1),this._state=B):(this._state=Le,setImmediate(()=>{this.emit("message",n,!1),this._state=B,this.startLoop(e)}))}}controlMessage(e,s){if(this._opcode===8){if(e.length===0)this._loop=!1,this.emit("conclude",1005,Dt),this.end();else{let r=e.readUInt16BE(0);if(!Ar(r)){let i=this.createError(RangeError,`invalid status code ${r}`,!0,1002,"WS_ERR_INVALID_CLOSE_CODE");s(i);return}let n=new Ne(e.buffer,e.byteOffset+2,e.length-2);if(!this._skipUTF8Validation&&!Wt(n)){let i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");s(i);return}this._loop=!1,this.emit("conclude",r,n),this.end()}this._state=B;return}this._allowSynchronousEvents?(this.emit(this._opcode===9?"ping":"pong",e),this._state=B):(this._state=Le,setImmediate(()=>{this.emit(this._opcode===9?"ping":"pong",e),this._state=B,this.startLoop(s)}))}createError(e,s,r,n,i){this._loop=!1,this._errored=!0;let o=new e(r?`Invalid WebSocket frame: ${s}`:s);return Error.captureStackTrace(o,this.createError),o.code=i,o[Rr]=n,o}};Vt.exports=nt});var lt=R((ki,Ht)=>{"use strict";var{Duplex:Oi}=require("stream"),{randomFillSync:Mr}=require("crypto"),Yt=se(),{EMPTY_BUFFER:$r,kWebSocket:Fr,NOOP:Ur}=j(),{isBlob:ne,isValidStatusCode:Dr}=re(),{mask:zt,toBuffer:K}=pe(),I=Symbol("kByteLength"),Wr=Buffer.alloc(4),Re=8*1024,J,ie=Re,M=0,jr=1,qr=2,ot=class t{constructor(e,s,r){this._extensions=s||{},r&&(this._generateMask=r,this._maskBuffer=Buffer.alloc(4)),this._socket=e,this._firstFragment=!0,this._compress=!1,this._bufferedBytes=0,this._queue=[],this._state=M,this.onerror=Ur,this[Fr]=void 0}static frame(e,s){let r,n=!1,i=2,o=!1;s.mask&&(r=s.maskBuffer||Wr,s.generateMask?s.generateMask(r):(ie===Re&&(J===void 0&&(J=Buffer.alloc(Re)),Mr(J,0,Re),ie=0),r[0]=J[ie++],r[1]=J[ie++],r[2]=J[ie++],r[3]=J[ie++]),o=(r[0]|r[1]|r[2]|r[3])===0,i=6);let a;typeof e=="string"?(!s.mask||o)&&s[I]!==void 0?a=s[I]:(e=Buffer.from(e),a=e.length):(a=e.length,n=s.mask&&s.readOnly&&!o);let c=a;a>=65536?(i+=8,c=127):a>125&&(i+=2,c=126);let u=Buffer.allocUnsafe(n?a+i:i);return u[0]=s.fin?s.opcode|128:s.opcode,s.rsv1&&(u[0]|=64),u[1]=c,c===126?u.writeUInt16BE(a,2):c===127&&(u[2]=u[3]=0,u.writeUIntBE(a,4,6)),s.mask?(u[1]|=128,u[i-4]=r[0],u[i-3]=r[1],u[i-2]=r[2],u[i-1]=r[3],o?[u,e]:n?(zt(e,r,u,i,a),[u]):(zt(e,r,e,0,a),[u,e])):[u,e]}close(e,s,r,n){let i;if(e===void 0)i=$r;else{if(typeof e!="number"||!Dr(e))throw new TypeError("First argument must be a valid error code number");if(s===void 0||!s.length)i=Buffer.allocUnsafe(2),i.writeUInt16BE(e,0);else{let a=Buffer.byteLength(s);if(a>123)throw new RangeError("The message must not be greater than 123 bytes");i=Buffer.allocUnsafe(2+a),i.writeUInt16BE(e,0),typeof s=="string"?i.write(s,2):i.set(s,2)}}let o={[I]:i.length,fin:!0,generateMask:this._generateMask,mask:r,maskBuffer:this._maskBuffer,opcode:8,readOnly:!1,rsv1:!1};this._state!==M?this.enqueue([this.dispatch,i,!1,o,n]):this.sendFrame(t.frame(i,o),n)}ping(e,s,r){let n,i;if(typeof e=="string"?(n=Buffer.byteLength(e),i=!1):ne(e)?(n=e.size,i=!1):(e=K(e),n=e.length,i=K.readOnly),n>125)throw new RangeError("The data size must not be greater than 125 bytes");let o={[I]:n,fin:!0,generateMask:this._generateMask,mask:s,maskBuffer:this._maskBuffer,opcode:9,readOnly:i,rsv1:!1};ne(e)?this._state!==M?this.enqueue([this.getBlobData,e,!1,o,r]):this.getBlobData(e,!1,o,r):this._state!==M?this.enqueue([this.dispatch,e,!1,o,r]):this.sendFrame(t.frame(e,o),r)}pong(e,s,r){let n,i;if(typeof e=="string"?(n=Buffer.byteLength(e),i=!1):ne(e)?(n=e.size,i=!1):(e=K(e),n=e.length,i=K.readOnly),n>125)throw new RangeError("The data size must not be greater than 125 bytes");let o={[I]:n,fin:!0,generateMask:this._generateMask,mask:s,maskBuffer:this._maskBuffer,opcode:10,readOnly:i,rsv1:!1};ne(e)?this._state!==M?this.enqueue([this.getBlobData,e,!1,o,r]):this.getBlobData(e,!1,o,r):this._state!==M?this.enqueue([this.dispatch,e,!1,o,r]):this.sendFrame(t.frame(e,o),r)}send(e,s,r){let n=this._extensions[Yt.extensionName],i=s.binary?2:1,o=s.compress,a,c;typeof e=="string"?(a=Buffer.byteLength(e),c=!1):ne(e)?(a=e.size,c=!1):(e=K(e),a=e.length,c=K.readOnly),this._firstFragment?(this._firstFragment=!1,o&&n&&n.params[n._isServer?"server_no_context_takeover":"client_no_context_takeover"]&&(o=a>=n._threshold),this._compress=o):(o=!1,i=0),s.fin&&(this._firstFragment=!0);let u={[I]:a,fin:s.fin,generateMask:this._generateMask,mask:s.mask,maskBuffer:this._maskBuffer,opcode:i,readOnly:c,rsv1:o};ne(e)?this._state!==M?this.enqueue([this.getBlobData,e,this._compress,u,r]):this.getBlobData(e,this._compress,u,r):this._state!==M?this.enqueue([this.dispatch,e,this._compress,u,r]):this.dispatch(e,this._compress,u,r)}getBlobData(e,s,r,n){this._bufferedBytes+=r[I],this._state=qr,e.arrayBuffer().then(i=>{if(this._socket.destroyed){let a=new Error("The socket was closed while the blob was being read");process.nextTick(at,this,a,n);return}this._bufferedBytes-=r[I];let o=K(i);s?this.dispatch(o,s,r,n):(this._state=M,this.sendFrame(t.frame(o,r),n),this.dequeue())}).catch(i=>{process.nextTick(Gr,this,i,n)})}dispatch(e,s,r,n){if(!s){this.sendFrame(t.frame(e,r),n);return}let i=this._extensions[Yt.extensionName];this._bufferedBytes+=r[I],this._state=jr,i.compress(e,r.fin,(o,a)=>{if(this._socket.destroyed){let c=new Error("The socket was closed while data was being compressed");at(this,c,n);return}this._bufferedBytes-=r[I],this._state=M,r.readOnly=!1,this.sendFrame(t.frame(a,r),n),this.dequeue()})}dequeue(){for(;this._state===M&&this._queue.length;){let e=this._queue.shift();this._bufferedBytes-=e[3][I],Reflect.apply(e[0],this,e.slice(1))}}enqueue(e){this._bufferedBytes+=e[3][I],this._queue.push(e)}sendFrame(e,s){e.length===2?(this._socket.cork(),this._socket.write(e[0]),this._socket.write(e[1],s),this._socket.uncork()):this._socket.write(e[0],s)}};Ht.exports=ot;function at(t,e,s){typeof s=="function"&&s(e);for(let r=0;r<t._queue.length;r++){let n=t._queue[r],i=n[n.length-1];typeof i=="function"&&i(e)}}function Gr(t,e,s){at(t,e,s),t.onerror(e)}});var rs=R((Ci,ss)=>{"use strict";var{kForOnEventAttribute:me,kListener:ct}=j(),Kt=Symbol("kCode"),Jt=Symbol("kData"),Xt=Symbol("kError"),Zt=Symbol("kMessage"),Qt=Symbol("kReason"),oe=Symbol("kTarget"),es=Symbol("kType"),ts=Symbol("kWasClean"),G=class{constructor(e){this[oe]=null,this[es]=e}get target(){return this[oe]}get type(){return this[es]}};Object.defineProperty(G.prototype,"target",{enumerable:!0});Object.defineProperty(G.prototype,"type",{enumerable:!0});var X=class extends G{constructor(e,s={}){super(e),this[Kt]=s.code===void 0?0:s.code,this[Qt]=s.reason===void 0?"":s.reason,this[ts]=s.wasClean===void 0?!1:s.wasClean}get code(){return this[Kt]}get reason(){return this[Qt]}get wasClean(){return this[ts]}};Object.defineProperty(X.prototype,"code",{enumerable:!0});Object.defineProperty(X.prototype,"reason",{enumerable:!0});Object.defineProperty(X.prototype,"wasClean",{enumerable:!0});var ae=class extends G{constructor(e,s={}){super(e),this[Xt]=s.error===void 0?null:s.error,this[Zt]=s.message===void 0?"":s.message}get error(){return this[Xt]}get message(){return this[Zt]}};Object.defineProperty(ae.prototype,"error",{enumerable:!0});Object.defineProperty(ae.prototype,"message",{enumerable:!0});var ge=class extends G{constructor(e,s={}){super(e),this[Jt]=s.data===void 0?null:s.data}get data(){return this[Jt]}};Object.defineProperty(ge.prototype,"data",{enumerable:!0});var Vr={addEventListener(t,e,s={}){for(let n of this.listeners(t))if(!s[me]&&n[ct]===e&&!n[me])return;let r;if(t==="message")r=function(i,o){let a=new ge("message",{data:o?i:i.toString()});a[oe]=this,Pe(e,this,a)};else if(t==="close")r=function(i,o){let a=new X("close",{code:i,reason:o.toString(),wasClean:this._closeFrameReceived&&this._closeFrameSent});a[oe]=this,Pe(e,this,a)};else if(t==="error")r=function(i){let o=new ae("error",{error:i,message:i.message});o[oe]=this,Pe(e,this,o)};else if(t==="open")r=function(){let i=new G("open");i[oe]=this,Pe(e,this,i)};else return;r[me]=!!s[me],r[ct]=e,s.once?this.once(t,r):this.on(t,r)},removeEventListener(t,e){for(let s of this.listeners(t))if(s[ct]===e&&!s[me]){this.removeListener(t,s);break}}};ss.exports={CloseEvent:X,ErrorEvent:ae,Event:G,EventTarget:Vr,MessageEvent:ge};function Pe(t,e,s){typeof t=="object"&&t.handleEvent?t.handleEvent.call(t,s):t.call(e,s)}});var Be=R((Ni,ns)=>{"use strict";var{tokenChars:ye}=re();function F(t,e,s){t[e]===void 0?t[e]=[s]:t[e].push(s)}function Yr(t){let e=Object.create(null),s=Object.create(null),r=!1,n=!1,i=!1,o,a,c=-1,u=-1,h=-1,_=0;for(;_<t.length;_++)if(u=t.charCodeAt(_),o===void 0)if(h===-1&&ye[u]===1)c===-1&&(c=_);else if(_!==0&&(u===32||u===9))h===-1&&c!==-1&&(h=_);else if(u===59||u===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${_}`);h===-1&&(h=_);let y=t.slice(c,h);u===44?(F(e,y,s),s=Object.create(null)):o=y,c=h=-1}else throw new SyntaxError(`Unexpected character at index ${_}`);else if(a===void 0)if(h===-1&&ye[u]===1)c===-1&&(c=_);else if(u===32||u===9)h===-1&&c!==-1&&(h=_);else if(u===59||u===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${_}`);h===-1&&(h=_),F(s,t.slice(c,h),!0),u===44&&(F(e,o,s),s=Object.create(null),o=void 0),c=h=-1}else if(u===61&&c!==-1&&h===-1)a=t.slice(c,_),c=h=-1;else throw new SyntaxError(`Unexpected character at index ${_}`);else if(n){if(ye[u]!==1)throw new SyntaxError(`Unexpected character at index ${_}`);c===-1?c=_:r||(r=!0),n=!1}else if(i)if(ye[u]===1)c===-1&&(c=_);else if(u===34&&c!==-1)i=!1,h=_;else if(u===92)n=!0;else throw new SyntaxError(`Unexpected character at index ${_}`);else if(u===34&&t.charCodeAt(_-1)===61)i=!0;else if(h===-1&&ye[u]===1)c===-1&&(c=_);else if(c!==-1&&(u===32||u===9))h===-1&&(h=_);else if(u===59||u===44){if(c===-1)throw new SyntaxError(`Unexpected character at index ${_}`);h===-1&&(h=_);let y=t.slice(c,h);r&&(y=y.replace(/\\/g,""),r=!1),F(s,a,y),u===44&&(F(e,o,s),s=Object.create(null),o=void 0),a=void 0,c=h=-1}else throw new SyntaxError(`Unexpected character at index ${_}`);if(c===-1||i||u===32||u===9)throw new SyntaxError("Unexpected end of input");h===-1&&(h=_);let g=t.slice(c,h);return o===void 0?F(e,g,s):(a===void 0?F(s,g,!0):r?F(s,a,g.replace(/\\/g,"")):F(s,a,g),F(e,o,s)),e}function zr(t){return Object.keys(t).map(e=>{let s=t[e];return Array.isArray(s)||(s=[s]),s.map(r=>[e].concat(Object.keys(r).map(n=>{let i=r[n];return Array.isArray(i)||(i=[i]),i.map(o=>o===!0?n:`${n}=${o}`).join("; ")})).join("; ")).join(", ")}).join(", ")}ns.exports={format:zr,parse:Yr}});var $e=R((Pi,ms)=>{"use strict";var Hr=require("events"),Kr=require("https"),Jr=require("http"),as=require("net"),Xr=require("tls"),{randomBytes:Zr,createHash:Qr}=require("crypto"),{Duplex:Li,Readable:Ri}=require("stream"),{URL:ft}=require("url"),z=se(),en=it(),tn=lt(),{isBlob:sn}=re(),{BINARY_TYPES:is,CLOSE_TIMEOUT:rn,EMPTY_BUFFER:Ie,GUID:nn,kForOnEventAttribute:ut,kListener:on,kStatusCode:an,kWebSocket:C,NOOP:ls}=j(),{EventTarget:{addEventListener:ln,removeEventListener:cn}}=rs(),{format:fn,parse:un}=Be(),{toBuffer:hn}=pe(),cs=Symbol("kAborted"),ht=[8,13],V=["CONNECTING","OPEN","CLOSING","CLOSED"],dn=/^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/,b=class t extends Hr{constructor(e,s,r){super(),this._binaryType=is[0],this._closeCode=1006,this._closeFrameReceived=!1,this._closeFrameSent=!1,this._closeMessage=Ie,this._closeTimer=null,this._errorEmitted=!1,this._extensions={},this._paused=!1,this._protocol="",this._readyState=t.CONNECTING,this._receiver=null,this._sender=null,this._socket=null,e!==null?(this._bufferedAmount=0,this._isServer=!1,this._redirects=0,s===void 0?s=[]:Array.isArray(s)||(typeof s=="object"&&s!==null?(r=s,s=[]):s=[s]),fs(this,e,s,r)):(this._autoPong=r.autoPong,this._closeTimeout=r.closeTimeout,this._isServer=!0)}get binaryType(){return this._binaryType}set binaryType(e){is.includes(e)&&(this._binaryType=e,this._receiver&&(this._receiver._binaryType=e))}get bufferedAmount(){return this._socket?this._socket._writableState.length+this._sender._bufferedBytes:this._bufferedAmount}get extensions(){return Object.keys(this._extensions).join()}get isPaused(){return this._paused}get onclose(){return null}get onerror(){return null}get onopen(){return null}get onmessage(){return null}get protocol(){return this._protocol}get readyState(){return this._readyState}get url(){return this._url}setSocket(e,s,r){let n=new en({allowSynchronousEvents:r.allowSynchronousEvents,binaryType:this.binaryType,extensions:this._extensions,isServer:this._isServer,maxPayload:r.maxPayload,skipUTF8Validation:r.skipUTF8Validation}),i=new tn(e,this._extensions,r.generateMask);this._receiver=n,this._sender=i,this._socket=e,n[C]=this,i[C]=this,e[C]=this,n.on("conclude",mn),n.on("drain",gn),n.on("error",yn),n.on("message",Sn),n.on("ping",xn),n.on("pong",bn),i.onerror=En,e.setTimeout&&e.setTimeout(0),e.setNoDelay&&e.setNoDelay(),s.length>0&&e.unshift(s),e.on("close",ds),e.on("data",Me),e.on("end",ps),e.on("error",_s),this._readyState=t.OPEN,this.emit("open")}emitClose(){if(!this._socket){this._readyState=t.CLOSED,this.emit("close",this._closeCode,this._closeMessage);return}this._extensions[z.extensionName]&&this._extensions[z.extensionName].cleanup(),this._receiver.removeAllListeners(),this._readyState=t.CLOSED,this.emit("close",this._closeCode,this._closeMessage)}close(e,s){if(this.readyState!==t.CLOSED){if(this.readyState===t.CONNECTING){P(this,this._req,"WebSocket was closed before the connection was established");return}if(this.readyState===t.CLOSING){this._closeFrameSent&&(this._closeFrameReceived||this._receiver._writableState.errorEmitted)&&this._socket.end();return}this._readyState=t.CLOSING,this._sender.close(e,s,!this._isServer,r=>{r||(this._closeFrameSent=!0,(this._closeFrameReceived||this._receiver._writableState.errorEmitted)&&this._socket.end())}),hs(this)}}pause(){this.readyState===t.CONNECTING||this.readyState===t.CLOSED||(this._paused=!0,this._socket.pause())}ping(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof e=="function"?(r=e,e=s=void 0):typeof s=="function"&&(r=s,s=void 0),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){dt(this,e,r);return}s===void 0&&(s=!this._isServer),this._sender.ping(e||Ie,s,r)}pong(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof e=="function"?(r=e,e=s=void 0):typeof s=="function"&&(r=s,s=void 0),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){dt(this,e,r);return}s===void 0&&(s=!this._isServer),this._sender.pong(e||Ie,s,r)}resume(){this.readyState===t.CONNECTING||this.readyState===t.CLOSED||(this._paused=!1,this._receiver._writableState.needDrain||this._socket.resume())}send(e,s,r){if(this.readyState===t.CONNECTING)throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");if(typeof s=="function"&&(r=s,s={}),typeof e=="number"&&(e=e.toString()),this.readyState!==t.OPEN){dt(this,e,r);return}let n={binary:typeof e!="string",mask:!this._isServer,compress:!0,fin:!0,...s};this._extensions[z.extensionName]||(n.compress=!1),this._sender.send(e||Ie,n,r)}terminate(){if(this.readyState!==t.CLOSED){if(this.readyState===t.CONNECTING){P(this,this._req,"WebSocket was closed before the connection was established");return}this._socket&&(this._readyState=t.CLOSING,this._socket.destroy())}}};Object.defineProperty(b,"CONNECTING",{enumerable:!0,value:V.indexOf("CONNECTING")});Object.defineProperty(b.prototype,"CONNECTING",{enumerable:!0,value:V.indexOf("CONNECTING")});Object.defineProperty(b,"OPEN",{enumerable:!0,value:V.indexOf("OPEN")});Object.defineProperty(b.prototype,"OPEN",{enumerable:!0,value:V.indexOf("OPEN")});Object.defineProperty(b,"CLOSING",{enumerable:!0,value:V.indexOf("CLOSING")});Object.defineProperty(b.prototype,"CLOSING",{enumerable:!0,value:V.indexOf("CLOSING")});Object.defineProperty(b,"CLOSED",{enumerable:!0,value:V.indexOf("CLOSED")});Object.defineProperty(b.prototype,"CLOSED",{enumerable:!0,value:V.indexOf("CLOSED")});["binaryType","bufferedAmount","extensions","isPaused","protocol","readyState","url"].forEach(t=>{Object.defineProperty(b.prototype,t,{enumerable:!0})});["open","error","close","message"].forEach(t=>{Object.defineProperty(b.prototype,`on${t}`,{enumerable:!0,get(){for(let e of this.listeners(t))if(e[ut])return e[on];return null},set(e){for(let s of this.listeners(t))if(s[ut]){this.removeListener(t,s);break}typeof e=="function"&&this.addEventListener(t,e,{[ut]:!0})}})});b.prototype.addEventListener=ln;b.prototype.removeEventListener=cn;ms.exports=b;function fs(t,e,s,r){let n={allowSynchronousEvents:!0,autoPong:!0,closeTimeout:rn,protocolVersion:ht[1],maxPayload:104857600,skipUTF8Validation:!1,perMessageDeflate:!0,followRedirects:!1,maxRedirects:10,...r,socketPath:void 0,hostname:void 0,protocol:void 0,timeout:void 0,method:"GET",host:void 0,path:void 0,port:void 0};if(t._autoPong=n.autoPong,t._closeTimeout=n.closeTimeout,!ht.includes(n.protocolVersion))throw new RangeError(`Unsupported protocol version: ${n.protocolVersion} (supported versions: ${ht.join(", ")})`);let i;if(e instanceof ft)i=e;else try{i=new ft(e)}catch{throw new SyntaxError(`Invalid URL: ${e}`)}i.protocol==="http:"?i.protocol="ws:":i.protocol==="https:"&&(i.protocol="wss:"),t._url=i.href;let o=i.protocol==="wss:",a=i.protocol==="ws+unix:",c;if(i.protocol!=="ws:"&&!o&&!a?c=`The URL's protocol must be one of "ws:", "wss:", "http:", "https:", or "ws+unix:"`:a&&!i.pathname?c="The URL's pathname is empty":i.hash&&(c="The URL contains a fragment identifier"),c){let p=new SyntaxError(c);if(t._redirects===0)throw p;Ae(t,p);return}let u=o?443:80,h=Zr(16).toString("base64"),_=o?Kr.request:Jr.request,g=new Set,y;if(n.createConnection=n.createConnection||(o?_n:pn),n.defaultPort=n.defaultPort||u,n.port=i.port||u,n.host=i.hostname.startsWith("[")?i.hostname.slice(1,-1):i.hostname,n.headers={...n.headers,"Sec-WebSocket-Version":n.protocolVersion,"Sec-WebSocket-Key":h,Connection:"Upgrade",Upgrade:"websocket"},n.path=i.pathname+i.search,n.timeout=n.handshakeTimeout,n.perMessageDeflate&&(y=new z({...n.perMessageDeflate,isServer:!1,maxPayload:n.maxPayload}),n.headers["Sec-WebSocket-Extensions"]=fn({[z.extensionName]:y.offer()})),s.length){for(let p of s){if(typeof p!="string"||!dn.test(p)||g.has(p))throw new SyntaxError("An invalid or duplicated subprotocol was specified");g.add(p)}n.headers["Sec-WebSocket-Protocol"]=s.join(",")}if(n.origin&&(n.protocolVersion<13?n.headers["Sec-WebSocket-Origin"]=n.origin:n.headers.Origin=n.origin),(i.username||i.password)&&(n.auth=`${i.username}:${i.password}`),a){let p=n.path.split(":");n.socketPath=p[0],n.path=p[1]}let w;if(n.followRedirects){if(t._redirects===0){t._originalIpc=a,t._originalSecure=o,t._originalHostOrSocketPath=a?n.socketPath:i.host;let p=r&&r.headers;if(r={...r,headers:{}},p)for(let[k,D]of Object.entries(p))r.headers[k.toLowerCase()]=D}else if(t.listenerCount("redirect")===0){let p=a?t._originalIpc?n.socketPath===t._originalHostOrSocketPath:!1:t._originalIpc?!1:i.host===t._originalHostOrSocketPath;(!p||t._originalSecure&&!o)&&(delete n.headers.authorization,delete n.headers.cookie,p||delete n.headers.host,n.auth=void 0)}n.auth&&!r.headers.authorization&&(r.headers.authorization="Basic "+Buffer.from(n.auth).toString("base64")),w=t._req=_(n),t._redirects&&t.emit("redirect",t.url,w)}else w=t._req=_(n);n.timeout&&w.on("timeout",()=>{P(t,w,"Opening handshake has timed out")}),w.on("error",p=>{w===null||w[cs]||(w=t._req=null,Ae(t,p))}),w.on("response",p=>{let k=p.headers.location,D=p.statusCode;if(k&&n.followRedirects&&D>=300&&D<400){if(++t._redirects>n.maxRedirects){P(t,w,"Maximum redirects exceeded");return}w.abort();let H;try{H=new ft(k,e)}catch{let f=new SyntaxError(`Invalid URL: ${k}`);Ae(t,f);return}fs(t,H,s,r)}else t.emit("unexpected-response",w,p)||P(t,w,`Unexpected server response: ${p.statusCode}`)}),w.on("upgrade",(p,k,D)=>{if(t.emit("upgrade",p),t.readyState!==b.CONNECTING)return;w=t._req=null;let H=p.headers.upgrade;if(H===void 0||H.toLowerCase()!=="websocket"){P(t,k,"Invalid Upgrade header");return}let N=Qr("sha1").update(h+nn).digest("base64");if(p.headers["sec-websocket-accept"]!==N){P(t,k,"Invalid Sec-WebSocket-Accept header");return}let f=p.headers["sec-websocket-protocol"],d;if(f!==void 0?g.size?g.has(f)||(d="Server sent an invalid subprotocol"):d="Server sent a subprotocol but none was requested":g.size&&(d="Server sent no subprotocol"),d){P(t,k,d);return}f&&(t._protocol=f);let m=p.headers["sec-websocket-extensions"];if(m!==void 0){if(!y){P(t,k,"Server sent a Sec-WebSocket-Extensions header but no extension was requested");return}let S;try{S=un(m)}catch{P(t,k,"Invalid Sec-WebSocket-Extensions header");return}let T=Object.keys(S);if(T.length!==1||T[0]!==z.extensionName){P(t,k,"Server indicated an extension that was not requested");return}try{y.accept(S[z.extensionName])}catch{P(t,k,"Invalid Sec-WebSocket-Extensions header");return}t._extensions[z.extensionName]=y}t.setSocket(k,D,{allowSynchronousEvents:n.allowSynchronousEvents,generateMask:n.generateMask,maxPayload:n.maxPayload,skipUTF8Validation:n.skipUTF8Validation})}),n.finishRequest?n.finishRequest(w,t):w.end()}function Ae(t,e){t._readyState=b.CLOSING,t._errorEmitted=!0,t.emit("error",e),t.emitClose()}function pn(t){return t.path=t.socketPath,as.connect(t)}function _n(t){return t.path=void 0,!t.servername&&t.servername!==""&&(t.servername=as.isIP(t.host)?"":t.host),Xr.connect(t)}function P(t,e,s){t._readyState=b.CLOSING;let r=new Error(s);Error.captureStackTrace(r,P),e.setHeader?(e[cs]=!0,e.abort(),e.socket&&!e.socket.destroyed&&e.socket.destroy(),process.nextTick(Ae,t,r)):(e.destroy(r),e.once("error",t.emit.bind(t,"error")),e.once("close",t.emitClose.bind(t)))}function dt(t,e,s){if(e){let r=sn(e)?e.size:hn(e).length;t._socket?t._sender._bufferedBytes+=r:t._bufferedAmount+=r}if(s){let r=new Error(`WebSocket is not open: readyState ${t.readyState} (${V[t.readyState]})`);process.nextTick(s,r)}}function mn(t,e){let s=this[C];s._closeFrameReceived=!0,s._closeMessage=e,s._closeCode=t,s._socket[C]!==void 0&&(s._socket.removeListener("data",Me),process.nextTick(us,s._socket),t===1005?s.close():s.close(t,e))}function gn(){let t=this[C];t.isPaused||t._socket.resume()}function yn(t){let e=this[C];e._socket[C]!==void 0&&(e._socket.removeListener("data",Me),process.nextTick(us,e._socket),e.close(t[an])),e._errorEmitted||(e._errorEmitted=!0,e.emit("error",t))}function os(){this[C].emitClose()}function Sn(t,e){this[C].emit("message",t,e)}function xn(t){let e=this[C];e._autoPong&&e.pong(t,!this._isServer,ls),e.emit("ping",t)}function bn(t){this[C].emit("pong",t)}function us(t){t.resume()}function En(t){let e=this[C];e.readyState!==b.CLOSED&&(e.readyState===b.OPEN&&(e._readyState=b.CLOSING,hs(e)),this._socket.end(),e._errorEmitted||(e._errorEmitted=!0,e.emit("error",t)))}function hs(t){t._closeTimer=setTimeout(t._socket.destroy.bind(t._socket),t._closeTimeout)}function ds(){let t=this[C];if(this.removeListener("close",ds),this.removeListener("data",Me),this.removeListener("end",ps),t._readyState=b.CLOSING,!this._readableState.endEmitted&&!t._closeFrameReceived&&!t._receiver._writableState.errorEmitted&&this._readableState.length!==0){let e=this.read(this._readableState.length);t._receiver.write(e)}t._receiver.end(),this[C]=void 0,clearTimeout(t._closeTimer),t._receiver._writableState.finished||t._receiver._writableState.errorEmitted?t.emitClose():(t._receiver.on("error",os),t._receiver.on("finish",os))}function Me(t){this[C]._receiver.write(t)||this.pause()}function ps(){let t=this[C];t._readyState=b.CLOSING,t._receiver.end(),this.end()}function _s(){let t=this[C];this.removeListener("error",_s),this.on("error",ls),t&&(t._readyState=b.CLOSING,this.destroy())}});var xs=R((Ii,Ss)=>{"use strict";var Bi=$e(),{Duplex:vn}=require("stream");function gs(t){t.emit("close")}function wn(){!this.destroyed&&this._writableState.finished&&this.destroy()}function ys(t){this.removeListener("error",ys),this.destroy(),this.listenerCount("error")===0&&this.emit("error",t)}function Tn(t,e){let s=!0,r=new vn({...e,autoDestroy:!1,emitClose:!1,objectMode:!1,writableObjectMode:!1});return t.on("message",function(i,o){let a=!o&&r._readableState.objectMode?i.toString():i;r.push(a)||t.pause()}),t.once("error",function(i){r.destroyed||(s=!1,r.destroy(i))}),t.once("close",function(){r.destroyed||r.push(null)}),r._destroy=function(n,i){if(t.readyState===t.CLOSED){i(n),process.nextTick(gs,r);return}let o=!1;t.once("error",function(c){o=!0,i(c)}),t.once("close",function(){o||i(n),process.nextTick(gs,r)}),s&&t.terminate()},r._final=function(n){if(t.readyState===t.CONNECTING){t.once("open",function(){r._final(n)});return}t._socket!==null&&(t._socket._writableState.finished?(n(),r._readableState.endEmitted&&r.destroy()):(t._socket.once("finish",function(){n()}),t.close()))},r._read=function(){t.isPaused&&t.resume()},r._write=function(n,i,o){if(t.readyState===t.CONNECTING){t.once("open",function(){r._write(n,i,o)});return}t.send(n,o)},r.on("end",wn),r.on("error",ys),r}Ss.exports=Tn});var pt=R((Ai,bs)=>{"use strict";var{tokenChars:On}=re();function kn(t){let e=new Set,s=-1,r=-1,n=0;for(n;n<t.length;n++){let o=t.charCodeAt(n);if(r===-1&&On[o]===1)s===-1&&(s=n);else if(n!==0&&(o===32||o===9))r===-1&&s!==-1&&(r=n);else if(o===44){if(s===-1)throw new SyntaxError(`Unexpected character at index ${n}`);r===-1&&(r=n);let a=t.slice(s,r);if(e.has(a))throw new SyntaxError(`The "${a}" subprotocol is duplicated`);e.add(a),s=r=-1}else throw new SyntaxError(`Unexpected character at index ${n}`)}if(s===-1||r!==-1)throw new SyntaxError("Unexpected end of input");let i=t.slice(s,n);if(e.has(i))throw new SyntaxError(`The "${i}" subprotocol is duplicated`);return e.add(i),e}bs.exports={parse:kn}});var Cs=R(($i,ks)=>{"use strict";var Cn=require("events"),Fe=require("http"),{Duplex:Mi}=require("stream"),{createHash:Nn}=require("crypto"),Es=Be(),Z=se(),Ln=pt(),Rn=$e(),{CLOSE_TIMEOUT:Pn,GUID:Bn,kWebSocket:In}=j(),An=/^[+/0-9A-Za-z]{22}==$/,vs=0,ws=1,Os=2,_t=class extends Cn{constructor(e,s){if(super(),e={allowSynchronousEvents:!0,autoPong:!0,maxPayload:100*1024*1024,skipUTF8Validation:!1,perMessageDeflate:!1,handleProtocols:null,clientTracking:!0,closeTimeout:Pn,verifyClient:null,noServer:!1,backlog:null,server:null,host:null,path:null,port:null,WebSocket:Rn,...e},e.port==null&&!e.server&&!e.noServer||e.port!=null&&(e.server||e.noServer)||e.server&&e.noServer)throw new TypeError('One and only one of the "port", "server", or "noServer" options must be specified');if(e.port!=null?(this._server=Fe.createServer((r,n)=>{let i=Fe.STATUS_CODES[426];n.writeHead(426,{"Content-Length":i.length,"Content-Type":"text/plain"}),n.end(i)}),this._server.listen(e.port,e.host,e.backlog,s)):e.server&&(this._server=e.server),this._server){let r=this.emit.bind(this,"connection");this._removeListeners=Mn(this._server,{listening:this.emit.bind(this,"listening"),error:this.emit.bind(this,"error"),upgrade:(n,i,o)=>{this.handleUpgrade(n,i,o,r)}})}e.perMessageDeflate===!0&&(e.perMessageDeflate={}),e.clientTracking&&(this.clients=new Set,this._shouldEmitClose=!1),this.options=e,this._state=vs}address(){if(this.options.noServer)throw new Error('The server is operating in "noServer" mode');return this._server?this._server.address():null}close(e){if(this._state===Os){e&&this.once("close",()=>{e(new Error("The server is not running"))}),process.nextTick(Se,this);return}if(e&&this.once("close",e),this._state!==ws)if(this._state=ws,this.options.noServer||this.options.server)this._server&&(this._removeListeners(),this._removeListeners=this._server=null),this.clients?this.clients.size?this._shouldEmitClose=!0:process.nextTick(Se,this):process.nextTick(Se,this);else{let s=this._server;this._removeListeners(),this._removeListeners=this._server=null,s.close(()=>{Se(this)})}}shouldHandle(e){if(this.options.path){let s=e.url.indexOf("?");if((s!==-1?e.url.slice(0,s):e.url)!==this.options.path)return!1}return!0}handleUpgrade(e,s,r,n){s.on("error",Ts);let i=e.headers["sec-websocket-key"],o=e.headers.upgrade,a=+e.headers["sec-websocket-version"];if(e.method!=="GET"){Q(this,e,s,405,"Invalid HTTP method");return}if(o===void 0||o.toLowerCase()!=="websocket"){Q(this,e,s,400,"Invalid Upgrade header");return}if(i===void 0||!An.test(i)){Q(this,e,s,400,"Missing or invalid Sec-WebSocket-Key header");return}if(a!==13&&a!==8){Q(this,e,s,400,"Missing or invalid Sec-WebSocket-Version header",{"Sec-WebSocket-Version":"13, 8"});return}if(!this.shouldHandle(e)){xe(s,400);return}let c=e.headers["sec-websocket-protocol"],u=new Set;if(c!==void 0)try{u=Ln.parse(c)}catch{Q(this,e,s,400,"Invalid Sec-WebSocket-Protocol header");return}let h=e.headers["sec-websocket-extensions"],_={};if(this.options.perMessageDeflate&&h!==void 0){let g=new Z({...this.options.perMessageDeflate,isServer:!0,maxPayload:this.options.maxPayload});try{let y=Es.parse(h);y[Z.extensionName]&&(g.accept(y[Z.extensionName]),_[Z.extensionName]=g)}catch{Q(this,e,s,400,"Invalid or unacceptable Sec-WebSocket-Extensions header");return}}if(this.options.verifyClient){let g={origin:e.headers[`${a===8?"sec-websocket-origin":"origin"}`],secure:!!(e.socket.authorized||e.socket.encrypted),req:e};if(this.options.verifyClient.length===2){this.options.verifyClient(g,(y,w,p,k)=>{if(!y)return xe(s,w||401,p,k);this.completeUpgrade(_,i,u,e,s,r,n)});return}if(!this.options.verifyClient(g))return xe(s,401)}this.completeUpgrade(_,i,u,e,s,r,n)}completeUpgrade(e,s,r,n,i,o,a){if(!i.readable||!i.writable)return i.destroy();if(i[In])throw new Error("server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration");if(this._state>vs)return xe(i,503);let u=["HTTP/1.1 101 Switching Protocols","Upgrade: websocket","Connection: Upgrade",`Sec-WebSocket-Accept: ${Nn("sha1").update(s+Bn).digest("base64")}`],h=new this.options.WebSocket(null,void 0,this.options);if(r.size){let _=this.options.handleProtocols?this.options.handleProtocols(r,n):r.values().next().value;_&&(u.push(`Sec-WebSocket-Protocol: ${_}`),h._protocol=_)}if(e[Z.extensionName]){let _=e[Z.extensionName].params,g=Es.format({[Z.extensionName]:[_]});u.push(`Sec-WebSocket-Extensions: ${g}`),h._extensions=e}this.emit("headers",u,n),i.write(u.concat(`\r
|
|
3
3
|
`).join(`\r
|
|
4
|
-
`)),i.removeListener("error",
|
|
4
|
+
`)),i.removeListener("error",Ts),h.setSocket(i,o,{allowSynchronousEvents:this.options.allowSynchronousEvents,maxPayload:this.options.maxPayload,skipUTF8Validation:this.options.skipUTF8Validation}),this.clients&&(this.clients.add(h),h.on("close",()=>{this.clients.delete(h),this._shouldEmitClose&&!this.clients.size&&process.nextTick(Se,this)})),a(h,n)}};ks.exports=_t;function Mn(t,e){for(let s of Object.keys(e))t.on(s,e[s]);return function(){for(let r of Object.keys(e))t.removeListener(r,e[r])}}function Se(t){t._state=Os,t.emit("close")}function Ts(){this.destroy()}function xe(t,e,s,r){s=s||Fe.STATUS_CODES[e],r={Connection:"close","Content-Type":"text/html","Content-Length":Buffer.byteLength(s),...r},t.once("finish",t.destroy),t.end(`HTTP/1.1 ${e} ${Fe.STATUS_CODES[e]}\r
|
|
5
5
|
`+Object.keys(r).map(n=>`${n}: ${r[n]}`).join(`\r
|
|
6
6
|
`)+`\r
|
|
7
7
|
\r
|
|
8
|
-
`+s)}function
|
|
8
|
+
`+s)}function Q(t,e,s,r,n,i){if(t.listenerCount("wsClientError")){let o=new Error(n);Error.captureStackTrace(o,Q),t.emit("wsClientError",o,s,e)}else xe(s,r,n,i)}});var $n=x(xs(),1),Fn=x(Be(),1),Un=x(se(),1),Dn=x(it(),1),Wn=x(lt(),1),jn=x(pt(),1),Ns=x($e(),1),qn=x(Cs(),1);var Ls=Ns.default;var wt=x(require("readline")),O=x(require("fs")),U=x(require("path"));var Rs=(t=0)=>e=>`\x1B[${e+t}m`,Ps=(t=0)=>e=>`\x1B[${38+t};5;${e}m`,Bs=(t=0)=>(e,s,r)=>`\x1B[${38+t};2;${e};${s};${r}m`,E={modifier:{reset:[0,0],bold:[1,22],dim:[2,22],italic:[3,23],underline:[4,24],overline:[53,55],inverse:[7,27],hidden:[8,28],strikethrough:[9,29]},color:{black:[30,39],red:[31,39],green:[32,39],yellow:[33,39],blue:[34,39],magenta:[35,39],cyan:[36,39],white:[37,39],blackBright:[90,39],gray:[90,39],grey:[90,39],redBright:[91,39],greenBright:[92,39],yellowBright:[93,39],blueBright:[94,39],magentaBright:[95,39],cyanBright:[96,39],whiteBright:[97,39]},bgColor:{bgBlack:[40,49],bgRed:[41,49],bgGreen:[42,49],bgYellow:[43,49],bgBlue:[44,49],bgMagenta:[45,49],bgCyan:[46,49],bgWhite:[47,49],bgBlackBright:[100,49],bgGray:[100,49],bgGrey:[100,49],bgRedBright:[101,49],bgGreenBright:[102,49],bgYellowBright:[103,49],bgBlueBright:[104,49],bgMagentaBright:[105,49],bgCyanBright:[106,49],bgWhiteBright:[107,49]}},Ui=Object.keys(E.modifier),Gn=Object.keys(E.color),Vn=Object.keys(E.bgColor),Di=[...Gn,...Vn];function Yn(){let t=new Map;for(let[e,s]of Object.entries(E)){for(let[r,n]of Object.entries(s))E[r]={open:`\x1B[${n[0]}m`,close:`\x1B[${n[1]}m`},s[r]=E[r],t.set(n[0],n[1]);Object.defineProperty(E,e,{value:s,enumerable:!1})}return Object.defineProperty(E,"codes",{value:t,enumerable:!1}),E.color.close="\x1B[39m",E.bgColor.close="\x1B[49m",E.color.ansi=Rs(),E.color.ansi256=Ps(),E.color.ansi16m=Bs(),E.bgColor.ansi=Rs(10),E.bgColor.ansi256=Ps(10),E.bgColor.ansi16m=Bs(10),Object.defineProperties(E,{rgbToAnsi256:{value(e,s,r){return e===s&&s===r?e<8?16:e>248?231:Math.round((e-8)/247*24)+232:16+36*Math.round(e/255*5)+6*Math.round(s/255*5)+Math.round(r/255*5)},enumerable:!1},hexToRgb:{value(e){let s=/[a-f\d]{6}|[a-f\d]{3}/i.exec(e.toString(16));if(!s)return[0,0,0];let[r]=s;r.length===3&&(r=[...r].map(i=>i+i).join(""));let n=Number.parseInt(r,16);return[n>>16&255,n>>8&255,n&255]},enumerable:!1},hexToAnsi256:{value:e=>E.rgbToAnsi256(...E.hexToRgb(e)),enumerable:!1},ansi256ToAnsi:{value(e){if(e<8)return 30+e;if(e<16)return 90+(e-8);let s,r,n;if(e>=232)s=((e-232)*10+8)/255,r=s,n=s;else{e-=16;let a=e%36;s=Math.floor(e/36)/5,r=Math.floor(a/6)/5,n=a%6/5}let i=Math.max(s,r,n)*2;if(i===0)return 30;let o=30+(Math.round(n)<<2|Math.round(r)<<1|Math.round(s));return i===2&&(o+=60),o},enumerable:!1},rgbToAnsi:{value:(e,s,r)=>E.ansi256ToAnsi(E.rgbToAnsi256(e,s,r)),enumerable:!1},hexToAnsi:{value:e=>E.ansi256ToAnsi(E.hexToAnsi256(e)),enumerable:!1}}),E}var zn=Yn(),$=zn;var De=x(require("node:process"),1),As=x(require("node:os"),1),mt=x(require("node:tty"),1);function A(t,e=globalThis.Deno?globalThis.Deno.args:De.default.argv){let s=t.startsWith("-")?"":t.length===1?"-":"--",r=e.indexOf(s+t),n=e.indexOf("--");return r!==-1&&(n===-1||r<n)}var{env:v}=De.default,Ue;A("no-color")||A("no-colors")||A("color=false")||A("color=never")?Ue=0:(A("color")||A("colors")||A("color=true")||A("color=always"))&&(Ue=1);function Hn(){if("FORCE_COLOR"in v)return v.FORCE_COLOR==="true"?1:v.FORCE_COLOR==="false"?0:v.FORCE_COLOR.length===0?1:Math.min(Number.parseInt(v.FORCE_COLOR,10),3)}function Kn(t){return t===0?!1:{level:t,hasBasic:!0,has256:t>=2,has16m:t>=3}}function Jn(t,{streamIsTTY:e,sniffFlags:s=!0}={}){let r=Hn();r!==void 0&&(Ue=r);let n=s?Ue:r;if(n===0)return 0;if(s){if(A("color=16m")||A("color=full")||A("color=truecolor"))return 3;if(A("color=256"))return 2}if("TF_BUILD"in v&&"AGENT_NAME"in v)return 1;if(t&&!e&&n===void 0)return 0;let i=n||0;if(v.TERM==="dumb")return i;if(De.default.platform==="win32"){let o=As.default.release().split(".");return Number(o[0])>=10&&Number(o[2])>=10586?Number(o[2])>=14931?3:2:1}if("CI"in v)return["GITHUB_ACTIONS","GITEA_ACTIONS","CIRCLECI"].some(o=>o in v)?3:["TRAVIS","APPVEYOR","GITLAB_CI","BUILDKITE","DRONE"].some(o=>o in v)||v.CI_NAME==="codeship"?1:i;if("TEAMCITY_VERSION"in v)return/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(v.TEAMCITY_VERSION)?1:0;if(v.COLORTERM==="truecolor"||v.TERM==="xterm-kitty"||v.TERM==="xterm-ghostty"||v.TERM==="wezterm")return 3;if("TERM_PROGRAM"in v){let o=Number.parseInt((v.TERM_PROGRAM_VERSION||"").split(".")[0],10);switch(v.TERM_PROGRAM){case"iTerm.app":return o>=3?3:2;case"Apple_Terminal":return 2}}return/-256(color)?$/i.test(v.TERM)?2:/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(v.TERM)||"COLORTERM"in v?1:i}function Is(t,e={}){let s=Jn(t,{streamIsTTY:t&&t.isTTY,...e});return Kn(s)}var Xn={stdout:Is({isTTY:mt.default.isatty(1)}),stderr:Is({isTTY:mt.default.isatty(2)})},Ms=Xn;function $s(t,e,s){let r=t.indexOf(e);if(r===-1)return t;let n=e.length,i=0,o="";do o+=t.slice(i,r)+e+s,i=r+n,r=t.indexOf(e,i);while(r!==-1);return o+=t.slice(i),o}function Fs(t,e,s,r){let n=0,i="";do{let o=t[r-1]==="\r";i+=t.slice(n,o?r-1:r)+e+(o?`\r
|
|
9
9
|
`:`
|
|
10
10
|
`)+s,n=r+1,r=t.indexOf(`
|
|
11
|
-
`,n)}while(r!==-1);return i+=t.slice(n),i}var{stdout:
|
|
12
|
-
`);return i!==-1&&(e=
|
|
13
|
-
`)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
`,n)}while(r!==-1);return i+=t.slice(n),i}var{stdout:Us,stderr:Ds}=Ms,gt=Symbol("GENERATOR"),le=Symbol("STYLER"),be=Symbol("IS_EMPTY"),Ws=["ansi","ansi","ansi256","ansi16m"],ce=Object.create(null),Zn=(t,e={})=>{if(e.level&&!(Number.isInteger(e.level)&&e.level>=0&&e.level<=3))throw new Error("The `level` option should be an integer from 0 to 3");let s=Us?Us.level:0;t.level=e.level===void 0?s:e.level};var Qn=t=>{let e=(...s)=>s.join(" ");return Zn(e,t),Object.setPrototypeOf(e,Ee.prototype),e};function Ee(t){return Qn(t)}Object.setPrototypeOf(Ee.prototype,Function.prototype);for(let[t,e]of Object.entries($))ce[t]={get(){let s=We(this,St(e.open,e.close,this[le]),this[be]);return Object.defineProperty(this,t,{value:s}),s}};ce.visible={get(){let t=We(this,this[le],!0);return Object.defineProperty(this,"visible",{value:t}),t}};var yt=(t,e,s,...r)=>t==="rgb"?e==="ansi16m"?$[s].ansi16m(...r):e==="ansi256"?$[s].ansi256($.rgbToAnsi256(...r)):$[s].ansi($.rgbToAnsi(...r)):t==="hex"?yt("rgb",e,s,...$.hexToRgb(...r)):$[s][t](...r),ei=["rgb","hex","ansi256"];for(let t of ei){ce[t]={get(){let{level:s}=this;return function(...r){let n=St(yt(t,Ws[s],"color",...r),$.color.close,this[le]);return We(this,n,this[be])}}};let e="bg"+t[0].toUpperCase()+t.slice(1);ce[e]={get(){let{level:s}=this;return function(...r){let n=St(yt(t,Ws[s],"bgColor",...r),$.bgColor.close,this[le]);return We(this,n,this[be])}}}}var ti=Object.defineProperties(()=>{},{...ce,level:{enumerable:!0,get(){return this[gt].level},set(t){this[gt].level=t}}}),St=(t,e,s)=>{let r,n;return s===void 0?(r=t,n=e):(r=s.openAll+t,n=e+s.closeAll),{open:t,close:e,openAll:r,closeAll:n,parent:s}},We=(t,e,s)=>{let r=(...n)=>si(r,n.length===1?""+n[0]:n.join(" "));return Object.setPrototypeOf(r,ti),r[gt]=t,r[le]=e,r[be]=s,r},si=(t,e)=>{if(t.level<=0||!e)return t[be]?"":e;let s=t[le];if(s===void 0)return e;let{openAll:r,closeAll:n}=s;if(e.includes("\x1B"))for(;s!==void 0;)e=$s(e,s.close,s.open),s=s.parent;let i=e.indexOf(`
|
|
12
|
+
`);return i!==-1&&(e=Fs(e,n,r,i)),r+e+n};Object.defineProperties(Ee.prototype,ce);var ri=Ee(),zi=Ee({level:Ds?Ds.level:0});var l=ri;function js(t){let e=t.split(`
|
|
13
|
+
`),s=[],r=!1,n="";for(let i of e){if(i.trimStart().startsWith("```")){r?(s.push(l.gray(" \u2514"+"\u2500".repeat(44))),r=!1,n=""):(n=i.trim().slice(3).trim(),s.push(l.gray(` \u250C\u2500 ${n||"code"} ${"\u2500".repeat(Math.max(0,40-(n||"code").length))}`)),r=!0);continue}if(r){s.push(l.green(" \u2502 ")+l.white(i));continue}let o=i.match(/^(#{1,3})\s+(.+)/);if(o){s.push(l.bold.cyan(o[2]));continue}if(/^\s*[-*]\s/.test(i)){s.push(i.replace(/^(\s*)[-*]\s/,"$1\u2022 ").replace(/`([^`]+)`/g,(a,c)=>l.cyan(c)).replace(/\*\*([^*]+)\*\*/g,(a,c)=>l.bold(c)));continue}if(/^\s*\d+\.\s/.test(i)){s.push(i.replace(/`([^`]+)`/g,(a,c)=>l.cyan(c)).replace(/\*\*([^*]+)\*\*/g,(a,c)=>l.bold(c)));continue}s.push(i.replace(/`([^`]+)`/g,(a,c)=>l.cyan(c)).replace(/\*\*([^*]+)\*\*/g,(a,c)=>l.bold(c)).replace(/\*([^*]+)\*/g,(a,c)=>l.italic(c)))}return r&&s.push(l.gray(" \u2514"+"\u2500".repeat(44))),s.join(`
|
|
14
|
+
`)}var je=require("child_process"),L=x(require("fs")),qe=x(require("path")),Ge=process.cwd();async function xt(t,e){try{switch(t){case"read_file":return ni(e);case"write_file":return ii(e);case"edit_file":return oi(e);case"grep":return ai(e);case"glob":return li(e);case"list_dir":return ci(e);case"bash":return fi(e);default:return{success:!1,output:`Unknown tool: ${t}`}}}catch(s){return{success:!1,output:s.message}}}function Ve(t){return qe.resolve(Ge,t)}var qs=100*1024;function ni(t){let e=Ve(t.path),s=L.statSync(e);if(s.size>qs){let n=L.readFileSync(e,"utf-8").slice(0,qs),i=L.readFileSync(e,"utf-8").split(`
|
|
15
|
+
`).length,o=n.split(`
|
|
16
|
+
`).length;return{success:!0,output:`${n}
|
|
17
|
+
|
|
18
|
+
[FILE TRUNCATED: showing ${o}/${i} lines (${(s.size/1024).toFixed(0)}KB). Use grep to find specific sections.]`}}return{success:!0,output:L.readFileSync(e,"utf-8")}}function ii(t){let e=Ve(t.path);return L.mkdirSync(qe.dirname(e),{recursive:!0}),L.writeFileSync(e,t.content,"utf-8"),{success:!0,output:`Written ${t.path} (${t.content.length} chars)`}}function oi(t){let e=Ve(t.path),s=L.readFileSync(e,"utf-8");if(!s.includes(t.old_str))return{success:!1,output:`old_str not found in ${t.path}`};let r=s.split(t.old_str).length-1;return r>1?{success:!1,output:`old_str found ${r} times in ${t.path}, must be unique`}:(L.writeFileSync(e,s.replace(t.old_str,t.new_str),"utf-8"),{success:!0,output:`Edited ${t.path}`})}function ai(t){let e=t.path||".",r=`grep -rn ${t.context_lines&&t.context_lines>0?`-C ${Math.min(t.context_lines,10)}`:""} --include='${t.include||"*"}' '${t.pattern}' ${e} 2>/dev/null | head -100`;return{success:!0,output:(0,je.execSync)(r,{cwd:Ge,encoding:"utf-8",timeout:1e4})||"(no matches)"}}function li(t){let e=`find . -name '${t.pattern}' -not -path '*/node_modules/*' -not -path '*/.git/*' 2>/dev/null | head -100`;return{success:!0,output:(0,je.execSync)(e,{cwd:Ge,encoding:"utf-8",timeout:1e4})||"(no files found)"}}function ci(t){let e=Ve(t.path||".");return{success:!0,output:L.readdirSync(e,{withFileTypes:!0}).filter(n=>!n.name.startsWith(".")&&n.name!=="node_modules").map(n=>`${n.isDirectory()?"\u{1F4C1}":"\u{1F4C4}"} ${n.name}`).join(`
|
|
19
|
+
`)||"(empty)"}}function fi(t){let e=t.command.trim();return[/\brm\s+-rf\s+[\/~]/,/\bmkfs\b/,/\bdd\s+if=/,/\b>\s*\/dev\/sd/,/\bchmod\s+-R\s+777\s+\//,/\bsudo\s+rm\b/].some(n=>n.test(e))?{success:!1,output:"Blocked: dangerous command detected"}:{success:!0,output:(0,je.execSync)(e,{cwd:Ge,encoding:"utf-8",timeout:t.timeout||3e4,maxBuffer:1024*1024})}}var ve=x(require("path")),Gs=x(require("fs")),Vs=x(require("readline")),Ye=process.cwd(),Ys=new Set;function ui(t){let e=ve.resolve(Ye,t);return e.startsWith(Ye+ve.sep)||e===Ye}var hi=new Set(["read_file","grep","glob","list_dir"]);async function zs(t,e,s){if(hi.has(t)){let r=e.path||e.pattern||".";if(ui(r))return"y"}return t!=="bash"&&Ys.has(t)?"t":"ask"}async function Hs(t,e,s){let i=(await di(`
|
|
20
|
+
`+(t==="bash"?"Allow this action? [y/n]: ":"Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]: "))).trim().toLowerCase()[0]||"n";return i==="t"&&t!=="bash"?(Ys.add(t),"t"):i==="y"?"y":"n"}function Ks(t){try{let e=ve.resolve(Ye,t);return Gs.readFileSync(e,"utf-8")}catch{return}}function di(t){return new Promise(e=>{let s=Vs.createInterface({input:process.stdin,output:process.stdout,terminal:!0});s.question(t,r=>{s.close(),e(r)})})}var Xs=x(require("fs")),Zs=x(require("path"));function pi(t){return t.replace(/\x1b\[[0-9;]*m/g,"")}function Qs(){let t=process.stdout.columns||60,e=Math.min(t-4,62),s=l.gray,r=l.cyan,n=l.cyanBright.bold,i=l.white,o=r("\u2502"),a=(u,h)=>u+" ".repeat(Math.max(0,h-pi(u).length)),c=u=>` ${o} ${a(u,e-4)} ${o}`;console.log(""),console.log(r(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2557 \u2588\u2588\u2557\u2588\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557")),console.log(r(" \u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D\u255A\u2588\u2588\u2557 \u2588\u2588\u2554\u255D\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255D")),console.log(n(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u255A\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2554\u2588\u2588\u2557 \u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2557 ")),console.log(r(" \u255A\u2550\u2550\u2550\u2550\u2588\u2588\u2551 \u255A\u2588\u2588\u2554\u255D \u2588\u2588\u2551\u255A\u2588\u2588\u2557\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u255D ")),console.log(n(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551 \u255A\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557")),console.log(r(" \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u2550\u2550\u255D\u255A\u2550\u255D \u255A\u2550\u255D\u255A\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u255D")),console.log(""),console.log(` ${r("\u250C"+"\u2500".repeat(e-2)+"\u2510")}`),console.log(c(s("AI-powered coding assistant")+" ".repeat(Math.max(0,e-38))+s("v0.1.6"))),console.log(c("")),console.log(c(s("model ")+i("Kimi K2.5")+s(" context ")+i("256k"))),console.log(c(s("location ")+i(process.cwd()))),console.log(c("")),console.log(c(s("/save save session /load restore session"))),console.log(c(s("/compact compress history /context show usage"))),console.log(c(s("/tools list tools /quit exit"))),console.log(c("")),console.log(c(s("Powered by Cloudflare \xB7 Built by Bowen Liu"))),console.log(` ${r("\u2514"+"\u2500".repeat(e-2)+"\u2518")}`),console.log("")}function er(t){console.log(l.white(t))}function tr(t){console.log(l.gray(` \u{1F4AD} ${t}`))}function bt(t,e,s){if(s){console.log(l.yellow(`
|
|
21
|
+
\u{1F527} `)+s+l.gray(` (using tool: ${t})`));return}let r="",n=e.path||e.pattern||"",i=n?l.cyan(n):"";switch(t){case"write_file":{r=n&&Xs.existsSync(Zs.resolve(process.cwd(),n))?`Updating ${i}`:`Creating ${i}`;break}case"edit_file":r=`Updating ${i}`;break;case"read_file":r=`Reading ${i}`;break;case"bash":r=`Running: ${l.cyan(e.command||"?")}`;break;case"web_search":r=`Searching: ${l.cyan(e.query||"?")}`;break;case"web_fetch":r=`Fetching: ${l.cyan(e.url||"?")}`;break;case"grep":r=`Searching '${e.pattern}' in ${i||"."}`;break;case"glob":r=`Finding files: ${e.pattern}`;break;case"list_dir":r=`Listing ${i||"."}`;break;default:r=`${t}`}console.log(l.yellow(`
|
|
22
|
+
\u{1F527} `)+r)}function Et(t,e,s,r){let n=r!=null?l.gray(` - Completed in ${(r/1e3).toFixed(1)}s`):"",i=s.length;if(!e){console.log(l.red(" \u2717 Failed")+n),s.trim()&&console.log(l.gray(` ${s.slice(0,200)}`));return}if(t==="bash"){if(console.log(l.blue(" \u2713 Command executed")+n),s.trim()){let o=s.split(`
|
|
17
23
|
`).map(a=>` ${a}`).join(`
|
|
18
|
-
`);console.log(
|
|
19
|
-
`).length:0;if(console.log(
|
|
20
|
-
`).slice(0,10).map(c=>` ${
|
|
21
|
-
`);console.log(a),o>10&&console.log(
|
|
22
|
-
`).length:0;console.log(
|
|
23
|
-
`).length:0;console.log(
|
|
24
|
-
`).find(i=>i.startsWith("["))||"";console.log(` ${r} ${
|
|
25
|
-
`)[0]?.slice(0,80)||"fetched";console.log(` ${r} ${
|
|
24
|
+
`);console.log(l.white(o))}}else if(t==="read_file")console.log(l.blue(` \u2713 Successfully read ${Js(i)}`)+n);else if(t==="write_file")console.log(l.blue(` \u2713 Successfully wrote ${Js(i)}`)+n);else if(t==="edit_file")console.log(l.blue(" \u2713 Successfully edited")+n);else if(t==="grep"){let o=s.trim()?s.split(`
|
|
25
|
+
`).length:0;if(console.log(l.blue(` \u2713 ${o} matches found`)+n),s.trim()){let a=s.split(`
|
|
26
|
+
`).slice(0,10).map(c=>` ${l.gray(c)}`).join(`
|
|
27
|
+
`);console.log(a),o>10&&console.log(l.gray(` ... (${o} lines)`))}}else if(t==="glob"){let o=s.trim()?s.split(`
|
|
28
|
+
`).length:0;console.log(l.blue(` \u2713 ${o} files found`)+n)}else if(t==="list_dir"){let o=s.trim()?s.split(`
|
|
29
|
+
`).length:0;console.log(l.blue(` \u2713 ${o} entries`)+n)}else console.log(l.blue(" \u2713 Done")+n)}function Js(t){return t<1024?`${t} bytes`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:`${(t/(1024*1024)).toFixed(1)} MB`}function we(t){console.log(l.red(` \u274C ${t}`))}function sr(t,e,s){let r=e?l.green("\u2713"):l.red("\u2717");if(t==="web_search"){let n=s.split(`
|
|
30
|
+
`).find(i=>i.startsWith("["))||"";console.log(` ${r} ${l.gray(n.slice(0,120))}`)}else if(t==="web_fetch"){let n=s.split(`
|
|
31
|
+
`)[0]?.slice(0,80)||"fetched";console.log(` ${r} ${l.gray(n)}`)}else console.log(` ${r}`)}function rr(t,e){console.log(l.cyan(`
|
|
26
32
|
\u2500\u2500 ${t} \u2500\u2500`));let s=e.split(`
|
|
27
|
-
`);for(let r=0;r<s.length;r++){let n=
|
|
28
|
-
`))}function
|
|
33
|
+
`);for(let r=0;r<s.length;r++){let n=l.gray(String(r+1).padStart(4)+" \u2502 ");console.log(n+l.green("+ "+s[r]))}console.log(l.cyan(` \u2500\u2500 end \u2500\u2500
|
|
34
|
+
`))}function nr(t,e,s,r){console.log("");let n=t.split(`
|
|
29
35
|
`),i=e.split(`
|
|
30
36
|
`),o=0;if(r){let a=r.indexOf(t);a>=0&&(o=r.substring(0,a).split(`
|
|
31
37
|
`).length)}if(r&&o>1){let a=r.split(`
|
|
32
|
-
`),c=Math.max(0,o-1-3);for(let
|
|
33
|
-
`),c=o-1+n.length,
|
|
38
|
+
`),c=Math.max(0,o-1-3);for(let u=c;u<o-1;u++){let h=l.gray(String(u+1).padStart(4)+" \u2502 ");console.log(h+l.gray(" "+a[u]))}}for(let a=0;a<n.length;a++){let c=o>0?o+a:a+1,u=l.gray(String(c).padStart(4)+" \u2502 ");console.log(u+l.red("- "+n[a]))}for(let a=0;a<i.length;a++){let c=o>0?o+a:a+1,u=l.gray(String(c).padStart(4)+" \u2502 ");console.log(u+l.green("+ "+i[a]))}if(r&&o>0){let a=r.split(`
|
|
39
|
+
`),c=o-1+n.length,u=Math.min(a.length,c+3);for(let h=c;h<u;h++){let _=l.gray(String(h+1).padStart(4)+" \u2502 ");console.log(_+l.gray(" "+a[h]))}}console.log("")}var fe=x(require("fs")),vt=x(require("path")),or=x(require("readline"));var mi=process.env.SYNAPSE_API||"https://api.synapse.cloudc.top",ar=vt.join(process.env.HOME||"~",".synapse"),lr=vt.join(ar,"token");function ir(t,e=!1){let s=or.createInterface({input:process.stdin,output:process.stderr});return new Promise(r=>{s.question(t,n=>{s.close(),r(n.trim())})})}async function cr(){console.log(l.cyan.bold(`
|
|
34
40
|
\u26A1 Synapse Login
|
|
35
|
-
`));let t=await
|
|
36
|
-
\u274C ${r.error||"Login failed"}`)),process.exit(1)),
|
|
37
|
-
\u2713 Logged in. Token saved to ~/.synapse/token`))}catch(s){console.log(
|
|
38
|
-
\u274C Connection failed: ${s.message}`)),process.exit(1)}}function
|
|
39
|
-
${
|
|
41
|
+
`));let t=await ir(" Username: "),e=await ir(" Password: ");try{let s=await fetch(`${mi}/api/login`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({username:t,password:e})}),r=await s.json();(!s.ok||!r.token)&&(console.log(l.red(`
|
|
42
|
+
\u274C ${r.error||"Login failed"}`)),process.exit(1)),fe.mkdirSync(ar,{recursive:!0}),fe.writeFileSync(lr,r.token,{mode:384}),console.log(l.green(`
|
|
43
|
+
\u2713 Logged in. Token saved to ~/.synapse/token`))}catch(s){console.log(l.red(`
|
|
44
|
+
\u274C Connection failed: ${s.message}`)),process.exit(1)}}function fr(){try{fe.unlinkSync(lr),console.log(l.green(" \u2713 Logged out."))}catch{console.log(l.gray(" Already logged out."))}}var gi=process.env.SYNAPSE_CLI_WS||"wss://cli.synapse.cloudc.top",yi=U.join(process.env.HOME||"~",".synapse","token"),ue=[],he="idle",ze=process.argv[2];(async()=>{ze==="login"&&(await cr(),process.exit(0)),ze==="logout"&&(await fr(),process.exit(0)),(ze==="--help"||ze==="-h")&&(console.log(`
|
|
45
|
+
${l.cyan.bold("\u26A1 Synapse CLI")} \u2014 AI coding assistant
|
|
40
46
|
|
|
41
|
-
${
|
|
47
|
+
${l.bold("Usage:")}
|
|
42
48
|
synapse Start coding session in current directory
|
|
43
49
|
synara login Login with username/password
|
|
44
50
|
synara logout Remove stored credentials
|
|
45
51
|
|
|
46
|
-
${
|
|
52
|
+
${l.bold("Session commands:")}
|
|
47
53
|
/compact Compress conversation history
|
|
48
54
|
/context Show context usage (tokens, messages)
|
|
49
55
|
/quit End session
|
|
50
56
|
|
|
51
|
-
${
|
|
57
|
+
${l.bold("Permission prompts:")}
|
|
52
58
|
y Approve this operation
|
|
53
59
|
n Deny this operation
|
|
54
60
|
t Trust this tool type for the session
|
|
55
|
-
`),process.exit(0));let t=null,e=null,s=null;function r(){let f=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],d=0;process.stdout.write(
|
|
56
|
-
${f[0]} Thinking...`)),s=setInterval(()=>{d=(d+1)%f.length,process.stdout.write(`\r ${
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
`),process.exit(0));let t=null,e=null,s=null;function r(){he="streaming";let f=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],d=0;process.stdout.write(l.gray(`
|
|
62
|
+
${f[0]} Thinking...`)),s=setInterval(()=>{d=(d+1)%f.length,process.stdout.write(`\r ${l.gray(f[d]+" Thinking...")}`)},80)}function n(){s&&(clearInterval(s),s=null,process.stdout.write("\r"+" ".repeat(30)+"\r"))}function i(){try{return O.readFileSync(yi,"utf-8").trim()}catch{return null}}function o(){for(let f of["SYNAPSE.md","synapse.md"]){let d=U.join(process.cwd(),f);if(O.existsSync(d))return O.readFileSync(d,"utf-8")}}async function a(){Qs();let f=i();f||(we("Not logged in. Run: synara login"),process.exit(1));let d=`${gi}/ws?token=${encodeURIComponent(f)}`,m=new Ls(d);t=m,process.on("SIGINT",()=>{he==="idle"&&(console.log(l.gray(`
|
|
63
|
+
Bye!`)),process.exit(0)),n(),console.log(l.yellow(`
|
|
64
|
+
\u23F9 Cancelled`)),N({type:"cancel"}),he="idle",p()}),m.on("open",()=>{N({type:"auth",token:f,project:{path:process.cwd(),synapseMd:o()}})}),m.on("message",async S=>{let T=JSON.parse(S.toString());switch(T.type){case"session_ready":w();break;case"thinking":tr(T.content);break;case"reasoning_token":break;case"reasoning_end":break;case"content_token":n(),process.stdout.write(T.content);break;case"content_end":process.stdout.write(`
|
|
65
|
+
`);break;case"turn_end":n(),he="idle",p();break;case"assistant_message":n(),er(js(T.content)),p();break;case"tool_request":n(),he="tool",T.tool==="web_search"||T.tool==="web_fetch"?bt(T.tool,T.args):await c(T);break;case"tool_result_from_cloud":sr(T.tool,T.success,T.output),r();break;case"session_save":n(),_(T),p();break;case"session_load":n(),g(T);break;case"session_list_request":n(),y(),p();break;case"error":n(),we(T.message),p();break}}),m.on("close",()=>{console.log(l.gray(`
|
|
66
|
+
Connection closed.`)),process.exit(0)}),m.on("error",S=>{we(`Connection failed: ${S.message}`),process.exit(1)})}async function c(f){let d=f.description||`${f.tool}`;bt(f.tool,f.args,d);let m=await zs(f.tool,f.args,d);if(m==="y"||m==="t"){let de=Date.now();u(f.tool,f.args);let He=await xt(f.tool,f.args);Et(f.tool,He.success,He.output,Date.now()-de),N({type:"tool_result",call_id:f.call_id,...He}),r();return}if(f.tool==="write_file"&&f.args.content)rr(f.args.path,f.args.content);else if(f.tool==="edit_file"&&f.args.old_str&&f.args.new_str){let de=Ks(f.args.path);nr(f.args.old_str,f.args.new_str,f.args.path,de)}if(e?.close(),e=null,await Hs(f.tool,f.args,d)==="n"){N({type:"tool_result",call_id:f.call_id,success:!1,output:"User denied"}),r();return}let T=Date.now();u(f.tool,f.args);let W=await xt(f.tool,f.args);Et(f.tool,W.success,W.output,Date.now()-T),N({type:"tool_result",call_id:f.call_id,...W}),r()}function u(f,d){if(f!=="write_file"&&f!=="edit_file")return;let m=d.path;if(!m)return;let S=U.resolve(process.cwd(),m);try{O.existsSync(S)?ue.push({tool:f,path:m,content:O.readFileSync(S,"utf-8"),timestamp:Date.now()}):ue.push({tool:f,path:m,content:"",timestamp:Date.now()}),ue.length>50&&ue.shift()}catch{}}let h=U.join(process.cwd(),".synapse","sessions");function _(f){O.mkdirSync(h,{recursive:!0});let d=f.name||new Date().toISOString().replace(/[:.]/g,"-").slice(0,19),m=U.join(h,`${d}.json`);if(O.existsSync(m)&&!f.force){we(`Session '${d}' already exists. Use /save ${d} --force to overwrite.`);return}let S={name:d,saved_at:new Date().toISOString(),messages:f.messages,meta:f.meta};O.writeFileSync(m,JSON.stringify(S,null,2),"utf-8"),console.log(l.blue(` \u2713 Session saved: ${d}`)+l.gray(` (${f.messages.length} messages, ${(Buffer.byteLength(JSON.stringify(S))/1024).toFixed(1)} KB)`))}function g(f){let d=f.name;if(!d){y(),N({type:"session_data",messages:null,error:"No session name provided. Use /sessions to list, then /load <name>."}),p();return}let m=U.join(h,`${d}.json`);if(!O.existsSync(m)){N({type:"session_data",messages:null,error:`Session '${d}' not found.`}),p();return}try{let S=JSON.parse(O.readFileSync(m,"utf-8"));N({type:"session_data",name:d,messages:S.messages,meta:S.meta||{}}),r()}catch(S){N({type:"session_data",messages:null,error:`Failed to read session: ${S}`}),p()}}function y(){if(!O.existsSync(h)){console.log(l.gray(" No saved sessions."));return}let f=O.readdirSync(h).filter(d=>d.endsWith(".json")).sort();if(f.length===0){console.log(l.gray(" No saved sessions."));return}console.log(l.white(`
|
|
67
|
+
Saved sessions (${f.length}):`));for(let d of f)try{let m=JSON.parse(O.readFileSync(U.join(h,d),"utf-8")),S=d.replace(".json",""),T=m.messages?.length||0,W=m.saved_at?new Date(m.saved_at).toLocaleString():"?";console.log(l.cyan(` ${S}`)+l.gray(` \u2014 ${T} messages, saved ${W}`))}catch{console.log(l.cyan(` ${d.replace(".json","")}`)+l.gray(" \u2014 (unreadable)"))}console.log("")}function w(){p()}function p(){e&&(e.close(),e=null),he="idle",e=wt.createInterface({input:process.stdin,output:process.stdout,terminal:!0}),e.question(l.cyan(`
|
|
68
|
+
> `),f=>{if(e?.close(),e=null,f.trimEnd().endsWith("\\")){k([f.trimEnd().slice(0,-1)]);return}D(f)})}function k(f){e&&(e.close(),e=null),e=wt.createInterface({input:process.stdin,output:process.stdout,terminal:!0}),e.question(l.gray("\u2026 "),d=>{e?.close(),e=null,d.trimEnd().endsWith("\\")?(f.push(d.trimEnd().slice(0,-1)),k(f)):(f.push(d),D(f.join(`
|
|
69
|
+
`)))})}function D(f){let d=f.trim();if(!d){p();return}if((d==="/quit"||d==="/exit")&&process.exit(0),d==="/help"){console.log(l.gray(`
|
|
61
70
|
/save [name] [--force] Save current session locally
|
|
62
71
|
/load <name> Load a saved session
|
|
63
72
|
/sessions List saved sessions
|
|
@@ -65,6 +74,9 @@
|
|
|
65
74
|
/context Show context usage
|
|
66
75
|
/usage Show token usage
|
|
67
76
|
/tools List available tools
|
|
77
|
+
/undo Undo last file change
|
|
68
78
|
/help Show this help
|
|
69
79
|
/quit End session
|
|
70
|
-
|
|
80
|
+
|
|
81
|
+
Line ending with \\ continues on next line. Ctrl+J inserts newline.
|
|
82
|
+
`)),p();return}if(d==="/undo"){H(),p();return}if(d==="/sessions"){y(),p();return}if(d.startsWith("/save")){N({type:"user_message",content:d});return}if(d.startsWith("/load")){let S=d.split(/\s+/)[1]||"";if(!S){y(),console.log(l.gray(" Usage: /load <name>")),p();return}g({type:"session_load",name:S});return}N({type:"user_message",content:f}),r()}function H(){if(ue.length===0){console.log(l.gray(" Nothing to undo."));return}let f=ue.pop(),d=U.resolve(process.cwd(),f.path);try{f.content===""&&O.existsSync(d)?(O.unlinkSync(d),console.log(l.blue(` \u2713 Undo: deleted ${f.path} (was newly created)`))):(O.writeFileSync(d,f.content,"utf-8"),console.log(l.blue(` \u2713 Undo: restored ${f.path}`)))}catch(m){console.log(l.red(` \u2717 Undo failed: ${m.message}`))}}function N(f){t?.send(JSON.stringify(f))}a().catch(f=>{console.error(f),process.exit(1)})})();
|
package/package.json
CHANGED
package/dist/auth.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// Login flow — username/password → D1 auth → JWT stored locally
|
|
2
|
-
import * as fs from "fs";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import * as readline from "readline";
|
|
5
|
-
import chalk from "chalk";
|
|
6
|
-
const API_URL = process.env.SYNAPSE_API || "https://api.synapse.cloudc.top";
|
|
7
|
-
const TOKEN_DIR = path.join(process.env.HOME || "~", ".synapse");
|
|
8
|
-
const TOKEN_PATH = path.join(TOKEN_DIR, "token");
|
|
9
|
-
function prompt(question, hidden = false) {
|
|
10
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
|
|
11
|
-
return new Promise((resolve) => {
|
|
12
|
-
rl.question(question, (answer) => {
|
|
13
|
-
rl.close();
|
|
14
|
-
resolve(answer.trim());
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export async function login() {
|
|
19
|
-
console.log(chalk.cyan.bold("\n ⚡ Synapse Login\n"));
|
|
20
|
-
const username = await prompt(" Username: ");
|
|
21
|
-
const password = await prompt(" Password: ");
|
|
22
|
-
try {
|
|
23
|
-
const resp = await fetch(`${API_URL}/api/login`, {
|
|
24
|
-
method: "POST",
|
|
25
|
-
headers: { "Content-Type": "application/json" },
|
|
26
|
-
body: JSON.stringify({ username, password }),
|
|
27
|
-
});
|
|
28
|
-
const data = await resp.json();
|
|
29
|
-
if (!resp.ok || !data.token) {
|
|
30
|
-
console.log(chalk.red(`\n ❌ ${data.error || "Login failed"}`));
|
|
31
|
-
process.exit(1);
|
|
32
|
-
}
|
|
33
|
-
fs.mkdirSync(TOKEN_DIR, { recursive: true });
|
|
34
|
-
fs.writeFileSync(TOKEN_PATH, data.token, { mode: 0o600 });
|
|
35
|
-
console.log(chalk.green("\n ✓ Logged in. Token saved to ~/.synapse/token"));
|
|
36
|
-
}
|
|
37
|
-
catch (e) {
|
|
38
|
-
console.log(chalk.red(`\n ❌ Connection failed: ${e.message}`));
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
export function logout() {
|
|
43
|
-
try {
|
|
44
|
-
fs.unlinkSync(TOKEN_PATH);
|
|
45
|
-
console.log(chalk.green(" ✓ Logged out."));
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
console.log(chalk.gray(" Already logged out."));
|
|
49
|
-
}
|
|
50
|
-
}
|
package/dist/permissions.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
// Permission manager — CLI-side authority on all permission decisions
|
|
2
|
-
import * as path from "path";
|
|
3
|
-
import * as fs from "fs";
|
|
4
|
-
import * as readline from "readline";
|
|
5
|
-
const PROJECT_ROOT = process.cwd();
|
|
6
|
-
const trusted = new Set();
|
|
7
|
-
function isInsideProject(filePath) {
|
|
8
|
-
const resolved = path.resolve(PROJECT_ROOT, filePath);
|
|
9
|
-
return resolved.startsWith(PROJECT_ROOT + path.sep) || resolved === PROJECT_ROOT;
|
|
10
|
-
}
|
|
11
|
-
const READ_ONLY_TOOLS = new Set(["read_file", "grep", "glob", "list_dir"]);
|
|
12
|
-
/**
|
|
13
|
-
* Auto-check: returns "y" if auto-approved, "t" if trusted, or "ask" if needs interactive prompt.
|
|
14
|
-
*/
|
|
15
|
-
export async function askPermission(tool, args, description) {
|
|
16
|
-
// Read-only tools: auto-approve only within project dir
|
|
17
|
-
if (READ_ONLY_TOOLS.has(tool)) {
|
|
18
|
-
const targetPath = args.path || args.pattern || ".";
|
|
19
|
-
if (isInsideProject(targetPath))
|
|
20
|
-
return "y";
|
|
21
|
-
}
|
|
22
|
-
// Already trusted for this session (bash can never be trusted)
|
|
23
|
-
if (tool !== "bash" && trusted.has(tool))
|
|
24
|
-
return "t";
|
|
25
|
-
return "ask";
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Interactive prompt: asks user y/n/t and waits for Enter.
|
|
29
|
-
*/
|
|
30
|
-
export async function askPermissionInteractive(tool, args, description) {
|
|
31
|
-
const hint = tool === "bash"
|
|
32
|
-
? "Allow this action? [y/n]: "
|
|
33
|
-
: "Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]: ";
|
|
34
|
-
const answer = await promptLine("\n" + hint);
|
|
35
|
-
const key = answer.trim().toLowerCase()[0] || "n";
|
|
36
|
-
if (key === "t" && tool !== "bash") {
|
|
37
|
-
trusted.add(tool);
|
|
38
|
-
return "t";
|
|
39
|
-
}
|
|
40
|
-
return key === "y" ? "y" : "n";
|
|
41
|
-
}
|
|
42
|
-
export function readFileForDiff(filePath) {
|
|
43
|
-
try {
|
|
44
|
-
const resolved = path.resolve(PROJECT_ROOT, filePath);
|
|
45
|
-
return fs.readFileSync(resolved, "utf-8");
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function promptLine(question) {
|
|
52
|
-
return new Promise((resolve) => {
|
|
53
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
|
|
54
|
-
rl.question(question, (answer) => { rl.close(); resolve(answer); });
|
|
55
|
-
});
|
|
56
|
-
}
|
package/dist/protocol.js
DELETED
package/dist/tools/executor.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// Local tool executor — runs tools on user's machine
|
|
2
|
-
// All "intelligence" is on the cloud side; this just executes
|
|
3
|
-
import { execSync } from "child_process";
|
|
4
|
-
import * as fs from "fs";
|
|
5
|
-
import * as path from "path";
|
|
6
|
-
const PROJECT_ROOT = process.cwd();
|
|
7
|
-
export async function executeTool(tool, args) {
|
|
8
|
-
try {
|
|
9
|
-
switch (tool) {
|
|
10
|
-
case "read_file": return readFile(args);
|
|
11
|
-
case "write_file": return writeFile(args);
|
|
12
|
-
case "edit_file": return editFile(args);
|
|
13
|
-
case "grep": return grep(args);
|
|
14
|
-
case "glob": return globFiles(args);
|
|
15
|
-
case "list_dir": return listDir(args);
|
|
16
|
-
case "bash": return bash(args);
|
|
17
|
-
default: return { success: false, output: `Unknown tool: ${tool}` };
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
catch (e) {
|
|
21
|
-
return { success: false, output: e.message };
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function resolve(p) {
|
|
25
|
-
return path.resolve(PROJECT_ROOT, p);
|
|
26
|
-
}
|
|
27
|
-
function readFile(args) {
|
|
28
|
-
const content = fs.readFileSync(resolve(args.path), "utf-8");
|
|
29
|
-
return { success: true, output: content };
|
|
30
|
-
}
|
|
31
|
-
function writeFile(args) {
|
|
32
|
-
const full = resolve(args.path);
|
|
33
|
-
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
34
|
-
fs.writeFileSync(full, args.content, "utf-8");
|
|
35
|
-
return { success: true, output: `Written ${args.path} (${args.content.length} chars)` };
|
|
36
|
-
}
|
|
37
|
-
function editFile(args) {
|
|
38
|
-
const full = resolve(args.path);
|
|
39
|
-
const content = fs.readFileSync(full, "utf-8");
|
|
40
|
-
if (!content.includes(args.old_str)) {
|
|
41
|
-
return { success: false, output: `old_str not found in ${args.path}` };
|
|
42
|
-
}
|
|
43
|
-
const count = content.split(args.old_str).length - 1;
|
|
44
|
-
if (count > 1) {
|
|
45
|
-
return { success: false, output: `old_str found ${count} times in ${args.path}, must be unique` };
|
|
46
|
-
}
|
|
47
|
-
fs.writeFileSync(full, content.replace(args.old_str, args.new_str), "utf-8");
|
|
48
|
-
return { success: true, output: `Edited ${args.path}` };
|
|
49
|
-
}
|
|
50
|
-
function grep(args) {
|
|
51
|
-
const target = args.path || ".";
|
|
52
|
-
let cmd = `grep -rn --include='${args.include || "*"}' '${args.pattern}' ${target} 2>/dev/null | head -50`;
|
|
53
|
-
const output = execSync(cmd, { cwd: PROJECT_ROOT, encoding: "utf-8", timeout: 10000 });
|
|
54
|
-
return { success: true, output: output || "(no matches)" };
|
|
55
|
-
}
|
|
56
|
-
function globFiles(args) {
|
|
57
|
-
const cmd = `find . -name '${args.pattern}' -not -path '*/node_modules/*' -not -path '*/.git/*' 2>/dev/null | head -100`;
|
|
58
|
-
const output = execSync(cmd, { cwd: PROJECT_ROOT, encoding: "utf-8", timeout: 10000 });
|
|
59
|
-
return { success: true, output: output || "(no files found)" };
|
|
60
|
-
}
|
|
61
|
-
function listDir(args) {
|
|
62
|
-
const target = resolve(args.path || ".");
|
|
63
|
-
const entries = fs.readdirSync(target, { withFileTypes: true });
|
|
64
|
-
const lines = entries
|
|
65
|
-
.filter((e) => !e.name.startsWith(".") && e.name !== "node_modules")
|
|
66
|
-
.map((e) => `${e.isDirectory() ? "📁" : "📄"} ${e.name}`)
|
|
67
|
-
.join("\n");
|
|
68
|
-
return { success: true, output: lines || "(empty)" };
|
|
69
|
-
}
|
|
70
|
-
function bash(args) {
|
|
71
|
-
// Block obviously destructive commands
|
|
72
|
-
const cmd = args.command.trim();
|
|
73
|
-
const blocked = [/\brm\s+-rf\s+[\/~]/, /\bmkfs\b/, /\bdd\s+if=/, /\b>\s*\/dev\/sd/, /\bchmod\s+-R\s+777\s+\//, /\bsudo\s+rm\b/];
|
|
74
|
-
if (blocked.some(p => p.test(cmd))) {
|
|
75
|
-
return { success: false, output: `Blocked: dangerous command detected` };
|
|
76
|
-
}
|
|
77
|
-
const output = execSync(cmd, {
|
|
78
|
-
cwd: PROJECT_ROOT,
|
|
79
|
-
encoding: "utf-8",
|
|
80
|
-
timeout: args.timeout || 30000,
|
|
81
|
-
maxBuffer: 1024 * 1024,
|
|
82
|
-
});
|
|
83
|
-
return { success: true, output };
|
|
84
|
-
}
|
package/dist/ui/terminal.js
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
// Terminal UI — Kiro-style output
|
|
2
|
-
import chalk from "chalk";
|
|
3
|
-
import * as fs from "fs";
|
|
4
|
-
import * as path from "path";
|
|
5
|
-
function stripAnsi(s) {
|
|
6
|
-
return s.replace(/\x1b\[[0-9;]*m/g, "");
|
|
7
|
-
}
|
|
8
|
-
export function printWelcome() {
|
|
9
|
-
const w = process.stdout.columns || 60;
|
|
10
|
-
const bw = Math.min(w - 4, 62);
|
|
11
|
-
const dim = chalk.gray;
|
|
12
|
-
const hi = chalk.cyan;
|
|
13
|
-
const hiBold = chalk.cyanBright.bold;
|
|
14
|
-
const white = chalk.white;
|
|
15
|
-
const border = hi("│");
|
|
16
|
-
const pad = (s, len) => s + " ".repeat(Math.max(0, len - stripAnsi(s).length));
|
|
17
|
-
const line = (content) => ` ${border} ${pad(content, bw - 4)} ${border}`;
|
|
18
|
-
console.log("");
|
|
19
|
-
console.log(hi(" ███████╗██╗ ██╗███╗ ██╗ █████╗ ██████╗ ███████╗███████╗"));
|
|
20
|
-
console.log(hi(" ██╔════╝╚██╗ ██╔╝████╗ ██║██╔══██╗██╔══██╗██╔════╝██╔════╝"));
|
|
21
|
-
console.log(hiBold(" ███████╗ ╚████╔╝ ██╔██╗ ██║███████║██████╔╝███████╗█████╗ "));
|
|
22
|
-
console.log(hi(" ╚════██║ ╚██╔╝ ██║╚██╗██║██╔══██║██╔═══╝ ╚════██║██╔══╝ "));
|
|
23
|
-
console.log(hiBold(" ███████║ ██║ ██║ ╚████║██║ ██║██║ ███████║███████╗"));
|
|
24
|
-
console.log(hi(" ╚══════╝ ╚═╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚══════╝╚══════╝"));
|
|
25
|
-
console.log("");
|
|
26
|
-
console.log(` ${hi("┌" + "─".repeat(bw - 2) + "┐")}`);
|
|
27
|
-
console.log(line(dim("AI-powered coding assistant") + " ".repeat(Math.max(0, bw - 38)) + dim("v0.1.2")));
|
|
28
|
-
console.log(line(""));
|
|
29
|
-
console.log(line(dim("model ") + white("Kimi K2.5") + dim(" context ") + white("256k")));
|
|
30
|
-
console.log(line(dim("location ") + white(process.cwd())));
|
|
31
|
-
console.log(line(""));
|
|
32
|
-
console.log(line(dim("/save save session /load restore session")));
|
|
33
|
-
console.log(line(dim("/compact compress history /context show usage")));
|
|
34
|
-
console.log(line(dim("/tools list tools /quit exit")));
|
|
35
|
-
console.log(line(""));
|
|
36
|
-
console.log(line(dim("Powered by Cloudflare · Built by Bowen Liu")));
|
|
37
|
-
console.log(` ${hi("└" + "─".repeat(bw - 2) + "┘")}`);
|
|
38
|
-
console.log("");
|
|
39
|
-
}
|
|
40
|
-
export function printAssistant(content) {
|
|
41
|
-
console.log(chalk.white(content));
|
|
42
|
-
}
|
|
43
|
-
export function printThinking(content) {
|
|
44
|
-
console.log(chalk.gray(` 💭 ${content}`));
|
|
45
|
-
}
|
|
46
|
-
export function printToolCall(tool, args, description) {
|
|
47
|
-
if (description) {
|
|
48
|
-
console.log(chalk.yellow(`\n 🔧 `) + description + chalk.gray(` (using tool: ${tool})`));
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
// Kiro-style: "Creating file.py", "Updating file.py", "Running: cmd"
|
|
52
|
-
let label = "";
|
|
53
|
-
const filePath = args.path || args.pattern || "";
|
|
54
|
-
const fileDisplay = filePath ? chalk.cyan(filePath) : "";
|
|
55
|
-
switch (tool) {
|
|
56
|
-
case "write_file": {
|
|
57
|
-
const exists = filePath && fs.existsSync(path.resolve(process.cwd(), filePath));
|
|
58
|
-
label = exists ? `Updating ${fileDisplay}` : `Creating ${fileDisplay}`;
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
case "edit_file":
|
|
62
|
-
label = `Updating ${fileDisplay}`;
|
|
63
|
-
break;
|
|
64
|
-
case "read_file":
|
|
65
|
-
label = `Reading ${fileDisplay}`;
|
|
66
|
-
break;
|
|
67
|
-
case "bash":
|
|
68
|
-
label = `Running: ${chalk.cyan(args.command || "?")}`;
|
|
69
|
-
break;
|
|
70
|
-
case "web_search":
|
|
71
|
-
label = `Searching: ${chalk.cyan(args.query || "?")}`;
|
|
72
|
-
break;
|
|
73
|
-
case "web_fetch":
|
|
74
|
-
label = `Fetching: ${chalk.cyan(args.url || "?")}`;
|
|
75
|
-
break;
|
|
76
|
-
case "grep":
|
|
77
|
-
label = `Searching '${args.pattern}' in ${fileDisplay || "."}`;
|
|
78
|
-
break;
|
|
79
|
-
case "glob":
|
|
80
|
-
label = `Finding files: ${args.pattern}`;
|
|
81
|
-
break;
|
|
82
|
-
case "list_dir":
|
|
83
|
-
label = `Listing ${fileDisplay || "."}`;
|
|
84
|
-
break;
|
|
85
|
-
default:
|
|
86
|
-
label = `${tool}`;
|
|
87
|
-
}
|
|
88
|
-
console.log(chalk.yellow(`\n 🔧 `) + label);
|
|
89
|
-
}
|
|
90
|
-
export function printToolResult(tool, success, output, elapsedMs) {
|
|
91
|
-
const elapsed = elapsedMs != null ? chalk.gray(` - Completed in ${(elapsedMs / 1000).toFixed(1)}s`) : "";
|
|
92
|
-
const bytes = output.length;
|
|
93
|
-
if (!success) {
|
|
94
|
-
console.log(chalk.red(` ✗ Failed`) + elapsed);
|
|
95
|
-
if (output.trim())
|
|
96
|
-
console.log(chalk.gray(` ${output.slice(0, 200)}`));
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (tool === "bash") {
|
|
100
|
-
console.log(chalk.blue(` ✓ Command executed`) + elapsed);
|
|
101
|
-
if (output.trim()) {
|
|
102
|
-
const lines = output.split("\n").map(l => ` ${l}`).join("\n");
|
|
103
|
-
console.log(chalk.white(lines));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else if (tool === "read_file") {
|
|
107
|
-
console.log(chalk.blue(` ✓ Successfully read ${formatBytes(bytes)}`) + elapsed);
|
|
108
|
-
}
|
|
109
|
-
else if (tool === "write_file") {
|
|
110
|
-
console.log(chalk.blue(` ✓ Successfully wrote ${formatBytes(bytes)}`) + elapsed);
|
|
111
|
-
}
|
|
112
|
-
else if (tool === "edit_file") {
|
|
113
|
-
console.log(chalk.blue(` ✓ Successfully edited`) + elapsed);
|
|
114
|
-
}
|
|
115
|
-
else if (tool === "grep") {
|
|
116
|
-
const matchCount = output.trim() ? output.split("\n").length : 0;
|
|
117
|
-
console.log(chalk.blue(` ✓ ${matchCount} matches found`) + elapsed);
|
|
118
|
-
if (output.trim()) {
|
|
119
|
-
const lines = output.split("\n").slice(0, 10).map(l => ` ${chalk.gray(l)}`).join("\n");
|
|
120
|
-
console.log(lines);
|
|
121
|
-
if (matchCount > 10)
|
|
122
|
-
console.log(chalk.gray(` ... (${matchCount} lines)`));
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else if (tool === "glob") {
|
|
126
|
-
const fileCount = output.trim() ? output.split("\n").length : 0;
|
|
127
|
-
console.log(chalk.blue(` ✓ ${fileCount} files found`) + elapsed);
|
|
128
|
-
}
|
|
129
|
-
else if (tool === "list_dir") {
|
|
130
|
-
const entryCount = output.trim() ? output.split("\n").length : 0;
|
|
131
|
-
console.log(chalk.blue(` ✓ ${entryCount} entries`) + elapsed);
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
console.log(chalk.blue(` ✓ Done`) + elapsed);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function formatBytes(bytes) {
|
|
138
|
-
if (bytes < 1024)
|
|
139
|
-
return `${bytes} bytes`;
|
|
140
|
-
if (bytes < 1024 * 1024)
|
|
141
|
-
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
142
|
-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
143
|
-
}
|
|
144
|
-
export function printError(msg) {
|
|
145
|
-
console.log(chalk.red(` ❌ ${msg}`));
|
|
146
|
-
}
|
|
147
|
-
export function printCloudToolResult(tool, success, output) {
|
|
148
|
-
const icon = success ? chalk.green("✓") : chalk.red("✗");
|
|
149
|
-
if (tool === "web_search") {
|
|
150
|
-
// Show first result title/url
|
|
151
|
-
const firstLine = output.split("\n").find(l => l.startsWith("[")) || "";
|
|
152
|
-
console.log(` ${icon} ${chalk.gray(firstLine.slice(0, 120))}`);
|
|
153
|
-
}
|
|
154
|
-
else if (tool === "web_fetch") {
|
|
155
|
-
// Show title or brief status
|
|
156
|
-
const title = output.split("\n")[0]?.slice(0, 80) || "fetched";
|
|
157
|
-
console.log(` ${icon} ${chalk.gray(title)}`);
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
console.log(` ${icon}`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
// Green + with line numbers for new file
|
|
164
|
-
export function printFileContent(filePath, content) {
|
|
165
|
-
console.log(chalk.cyan(`\n ── ${filePath} ──`));
|
|
166
|
-
const lines = content.split("\n");
|
|
167
|
-
for (let i = 0; i < lines.length; i++) {
|
|
168
|
-
const num = chalk.gray(String(i + 1).padStart(4) + " │ ");
|
|
169
|
-
console.log(num + chalk.green("+ " + lines[i]));
|
|
170
|
-
}
|
|
171
|
-
console.log(chalk.cyan(" ── end ──\n"));
|
|
172
|
-
}
|
|
173
|
-
// Red - / green + with context lines and line numbers
|
|
174
|
-
export function printDiff(oldStr, newStr, filePath, fullContent) {
|
|
175
|
-
console.log("");
|
|
176
|
-
const oldLines = oldStr.split("\n");
|
|
177
|
-
const newLines = newStr.split("\n");
|
|
178
|
-
let startLine = 0;
|
|
179
|
-
if (fullContent) {
|
|
180
|
-
const idx = fullContent.indexOf(oldStr);
|
|
181
|
-
if (idx >= 0) {
|
|
182
|
-
startLine = fullContent.substring(0, idx).split("\n").length;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// 3 lines before
|
|
186
|
-
if (fullContent && startLine > 1) {
|
|
187
|
-
const allLines = fullContent.split("\n");
|
|
188
|
-
const ctxStart = Math.max(0, startLine - 1 - 3);
|
|
189
|
-
for (let i = ctxStart; i < startLine - 1; i++) {
|
|
190
|
-
const num = chalk.gray(String(i + 1).padStart(4) + " │ ");
|
|
191
|
-
console.log(num + chalk.gray(" " + allLines[i]));
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
for (let i = 0; i < oldLines.length; i++) {
|
|
195
|
-
const lineNo = startLine > 0 ? startLine + i : i + 1;
|
|
196
|
-
const num = chalk.gray(String(lineNo).padStart(4) + " │ ");
|
|
197
|
-
console.log(num + chalk.red("- " + oldLines[i]));
|
|
198
|
-
}
|
|
199
|
-
for (let i = 0; i < newLines.length; i++) {
|
|
200
|
-
const lineNo = startLine > 0 ? startLine + i : i + 1;
|
|
201
|
-
const num = chalk.gray(String(lineNo).padStart(4) + " │ ");
|
|
202
|
-
console.log(num + chalk.green("+ " + newLines[i]));
|
|
203
|
-
}
|
|
204
|
-
// 3 lines after
|
|
205
|
-
if (fullContent && startLine > 0) {
|
|
206
|
-
const allLines = fullContent.split("\n");
|
|
207
|
-
const afterStart = startLine - 1 + oldLines.length;
|
|
208
|
-
const afterEnd = Math.min(allLines.length, afterStart + 3);
|
|
209
|
-
for (let i = afterStart; i < afterEnd; i++) {
|
|
210
|
-
const num = chalk.gray(String(i + 1).padStart(4) + " │ ");
|
|
211
|
-
console.log(num + chalk.gray(" " + allLines[i]));
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
console.log("");
|
|
215
|
-
}
|