fastchat-cli 1.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.
- package/index.js +47 -0
- package/package.json +12 -0
package/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const WebSocket = require('ws');
|
|
3
|
+
const readline = require('readline');
|
|
4
|
+
|
|
5
|
+
// The WebSocket URL is completely hidden from the user interacting with the CLI!
|
|
6
|
+
const WS_URL = 'wss://log-streamer.mrsadiq471.workers.dev/ws';
|
|
7
|
+
|
|
8
|
+
const ws = new WebSocket(WS_URL);
|
|
9
|
+
|
|
10
|
+
// Set up the terminal input reader
|
|
11
|
+
const rl = readline.createInterface({
|
|
12
|
+
input: process.stdin,
|
|
13
|
+
output: process.stdout,
|
|
14
|
+
prompt: ''
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
ws.on('open', () => {
|
|
18
|
+
// The connection is established.
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
ws.on('message', (data) => {
|
|
22
|
+
// Clear the current line so incoming messages don't interrupt what the user is typing
|
|
23
|
+
process.stdout.write('\r\x1b[K');
|
|
24
|
+
console.log(data.toString());
|
|
25
|
+
rl.prompt(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
ws.on('close', () => {
|
|
29
|
+
console.log('\n🛑 \x1b[31mDisconnected\x1b[0m');
|
|
30
|
+
process.exit(0);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
ws.on('error', (err) => {
|
|
34
|
+
console.error('\n⚠️ \x1b[31mConnection error\x1b[0m', err.message);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// When the user hits enter, send the line to the hidden Cloudflare Worker
|
|
39
|
+
rl.on('line', (line) => {
|
|
40
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
41
|
+
ws.send(line);
|
|
42
|
+
}
|
|
43
|
+
// Instantly hide the user's raw ugly input by aggressively moving cursor up and clearing line!
|
|
44
|
+
// The server perfectly replies with formatted `(You): mymessage` immediately.
|
|
45
|
+
process.stdout.write('\x1b[1A\x1b[2K');
|
|
46
|
+
rl.prompt(true);
|
|
47
|
+
});
|