@open-wa/cli 5.0.0-alpha.8 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,169 +0,0 @@
1
- import WebSocket from 'ws';
2
-
3
- export interface TunnelClientOptions {
4
- proxyHost: string;
5
- proxyToken: string;
6
- sessionId: string;
7
- localSessionPort: number;
8
- log?: (level: 'info' | 'warn' | 'error', message: string) => void;
9
- }
10
-
11
- export class TunnelClient {
12
- private ws: WebSocket | null = null;
13
- private reconnectTimeout: NodeJS.Timeout | null = null;
14
- private isDestroyed = false;
15
-
16
- constructor(private options: TunnelClientOptions) {}
17
-
18
- private log(level: 'info' | 'warn' | 'error', message: string) {
19
- if (this.options.log) {
20
- this.options.log(level, message);
21
- return;
22
- }
23
-
24
- if (level === 'warn') {
25
- console.warn(message);
26
- return;
27
- }
28
-
29
- if (level === 'error') {
30
- console.error(message);
31
- return;
32
- }
33
-
34
- console.log(message);
35
- }
36
-
37
- public connect() {
38
- if (this.isDestroyed) return;
39
-
40
- let urlStr = this.options.proxyHost;
41
- if (urlStr.startsWith('http')) {
42
- urlStr = urlStr.replace(/^http/, 'ws');
43
- }
44
- const connectUrl = `${urlStr}/sessions/${encodeURIComponent(this.options.sessionId)}/upstream?token=${encodeURIComponent(this.options.proxyToken)}`;
45
-
46
- this.ws = new WebSocket(connectUrl);
47
-
48
- this.ws.on('open', () => {
49
- this.log('info', `[TunnelClient] Connected upstream for session ${this.options.sessionId}`);
50
- });
51
-
52
- this.ws.on('message', async (data: Buffer) => {
53
- let msg: any;
54
- try {
55
- msg = JSON.parse(data.toString('utf-8'));
56
- } catch {
57
- return;
58
- }
59
-
60
- if (msg.type === 'ping') {
61
- this.ws?.send(JSON.stringify({ type: 'pong' }));
62
- return;
63
- }
64
-
65
- if (msg.type === 'http_request') {
66
- await this.handleHttpRequest(msg);
67
- } else if (msg.type === 'rpc_request') {
68
- // Advanced hybrid mode: forward to an RPC interface if exposed, or map to HTTP.
69
- // For now, if we get an RPC request in CLI, proxy it to HTTP.
70
- await this.handleRpcAsHttp(msg);
71
- }
72
- });
73
-
74
- this.ws.on('close', () => {
75
- if (!this.isDestroyed) {
76
- this.log('warn', `[TunnelClient] Disconnected. Reconnecting in 3s...`);
77
- this.reconnectTimeout = setTimeout(() => this.connect(), 3000);
78
- }
79
- });
80
-
81
- this.ws.on('error', (err: any) => {
82
- this.log('error', `[TunnelClient] WebSocket error: ${err.message}`);
83
- });
84
- }
85
-
86
- private async handleHttpRequest(msg: any) {
87
- const localUrl = `http://localhost:${this.options.localSessionPort}${msg.path}`;
88
- try {
89
- const fetchOpts: RequestInit = {
90
- method: msg.method,
91
- // @ts-ignore
92
- headers: msg.headers || {},
93
- };
94
-
95
- if (msg.body) {
96
- if (typeof msg.body === 'string') {
97
- fetchOpts.body = Buffer.from(msg.body, 'base64');
98
- }
99
- }
100
-
101
- const res = await fetch(localUrl, fetchOpts);
102
- const resBuffer = await res.arrayBuffer();
103
-
104
- const responseMsg = {
105
- type: 'http_response',
106
- id: msg.id,
107
- status: res.status,
108
- headers: Object.fromEntries(res.headers.entries()),
109
- body: Buffer.from(resBuffer).toString('base64')
110
- };
111
-
112
- this.ws?.send(JSON.stringify(responseMsg));
113
- } catch (e: any) {
114
- this.log('error', `[TunnelClient] Failed to proxy http_request: ${e instanceof Error ? e.message : String(e)}`);
115
- this.ws?.send(JSON.stringify({
116
- type: 'http_response',
117
- id: msg.id,
118
- status: 502,
119
- headers: { 'content-type': 'text/plain' },
120
- body: Buffer.from('Bad Gateway: ' + e.message).toString('base64')
121
- }));
122
- }
123
- }
124
-
125
- private async handleRpcAsHttp(msg: any) {
126
- // If the proxy sends an RPC request, map it to the REST API equivalent.
127
- // E.g. rpc 'sendText' -> HTTP POST /api/sendText
128
- const localUrl = `http://localhost:${this.options.localSessionPort}/api/${msg.method}`;
129
- try {
130
- const res = await fetch(localUrl, {
131
- method: 'POST',
132
- headers: {
133
- 'Content-Type': 'application/json'
134
- },
135
- body: JSON.stringify({ args: msg.args })
136
- });
137
-
138
- const resData = await res.json();
139
-
140
- const responseMsg = {
141
- type: 'rpc_response',
142
- id: msg.id,
143
- result: resData
144
- };
145
-
146
- this.ws?.send(JSON.stringify(responseMsg));
147
- } catch (e: any) {
148
- this.log('error', `[TunnelClient] Failed to proxy rpc_request: ${e instanceof Error ? e.message : String(e)}`);
149
- this.ws?.send(JSON.stringify({
150
- type: 'rpc_response',
151
- id: msg.id,
152
- error: e.message
153
- }));
154
- }
155
- }
156
-
157
- public destroy() {
158
- this.isDestroyed = true;
159
- if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
160
- if (this.ws) {
161
- this.ws.close();
162
- this.ws = null;
163
- }
164
- }
165
-
166
- public disconnect() {
167
- this.destroy();
168
- }
169
- }
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "lib": ["ES2022"],
7
- "outDir": "./dist",
8
- "rootDir": "./src",
9
- "declaration": true,
10
- "declarationMap": true,
11
- "composite": true,
12
- "strict": true,
13
- "esModuleInterop": true,
14
- "skipLibCheck": true,
15
- "paths": {
16
- "@open-wa/wa-automate": [
17
- "../wa-automate/dist/index.d.ts"
18
- ]
19
- }
20
- },
21
- "include": ["src/**/*"],
22
- "exclude": ["node_modules", "dist", "**/*.test.ts"]
23
- }
package/tsdown.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'tsdown';
2
-
3
- export default defineConfig({
4
- checks: { pluginTimings: false },
5
- deps: {
6
- neverBundle: ['winston']
7
- }
8
- });