-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
executable file
·139 lines (113 loc) · 3.56 KB
/
client.js
File metadata and controls
executable file
·139 lines (113 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
const http = require('http');
const {EventSource} = require('eventsource');
class EthersyncProxy {
constructor() {
this.buffer = '';
this.expectedLength = null;
this.sseConnection = null;
}
start() {
// Set up stdin reading
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
this.buffer += chunk;
this.processBuffer();
});
process.stdin.on('end', () => {
if (this.sseConnection) {
this.sseConnection.close();
}
process.exit(0);
});
// Establish SSE connection to receive messages from server
this.connectSSE();
console.error('Ethersync proxy started, connecting to http://localhost:1009');
}
connectSSE() {
this.sseConnection = new EventSource('http://localhost:1009/events');
this.sseConnection.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
this.sendToStdout(message);
console.error('Received from server:', JSON.stringify(message, null, 2));
} catch (e) {
console.error('Failed to parse SSE message:', e);
}
};
this.sseConnection.onerror = (error) => {
console.error('SSE connection error:', error);
// Retry connection automatically handled by EventSource
};
this.sseConnection.onopen = () => {
console.error('SSE connection established');
};
}
processBuffer() {
while (true) {
if (this.expectedLength === null) {
// Look for Content-Length header
const headerMatch = this.buffer.match(/Content-Length: (\d+)\r?\n\r?\n/);
if (!headerMatch) {
break; // Wait for more data
}
this.expectedLength = parseInt(headerMatch[1]);
const headerLength = headerMatch[0].length;
this.buffer = this.buffer.slice(headerMatch.index + headerLength);
}
if (this.buffer.length >= this.expectedLength) {
// We have a complete message
const messageStr = this.buffer.slice(0, this.expectedLength);
this.buffer = this.buffer.slice(this.expectedLength);
this.expectedLength = null;
try {
const message = JSON.parse(messageStr);
this.forwardToServer(message);
} catch (e) {
console.error('Failed to parse message:', e);
}
} else {
break; // Wait for more data
}
}
}
forwardToServer(message) {
console.error('Forwarding to server:', JSON.stringify(message, null, 2));
const postData = JSON.stringify(message);
const options = {
hostname: 'localhost',
port: 1009,
path: '/rpc',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (res.statusCode !== 200) {
console.error('Server returned error:', res.statusCode, body);
}
});
});
req.on('error', (error) => {
console.error('Failed to forward message to server:', error);
});
req.write(postData);
req.end();
}
sendToStdout(message) {
const messageStr = JSON.stringify(message);
const contentLength = Buffer.byteLength(messageStr, 'utf8');
process.stdout.write(`Content-Length: ${contentLength}\r\n\r\n`);
process.stdout.write(messageStr);
}
}
// Main execution
const proxy = new EthersyncProxy();
proxy.start();