forked from khanhas/minnet-quickjs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.mjs
62 lines (57 loc) · 1.63 KB
/
example.mjs
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
import { client, server, fetch } from "net"
function CreateServer() {
print("SERVER")
server({
port: 7981,
onConnect: (socket) => {
print("Client connected")
socket.send("Hello from server")
},
onMessage: (socket, msg) => {
print("Received: ", msg)
},
onClose: (why) => {
print("Client disconnected. Reason: ", why)
},
onPong: (socket, data) => {
print("Pong: ", data)
}
});
}
function CreateClient() {
print("CLIENT")
client({
port: 7981,
server: "localhost",
onConnect: (socket) => {
print("Connected to server")
socket.send("Hello from client")
},
onMessage: (socket, msg) => {
print("Received from server: ", msg)
},
onClose: (why) => {
print("Disconnected from server. Reason: ", why)
},
onPong: (socket, data) => {
print("Pong: ", data)
}
});
}
function getDownloadCount() {
const res = fetch("https://api.github.com/repos/khanhas/spicetify-cli/releases")
const dl_count = res.json().reduce((total, tag) => {
return total += tag.assets.reduce((tag_total, asset) => {
return tag_total += asset.download_count
}, 0)
}, 0)
print("Fetch:", res.url)
print("STATUS: ", res.status, "OK: ", res.ok, "TYPE: ", res.type)
print("RESULT: ", dl_count)
return `${dl_count}`
}
switch (scriptArgs[1]) {
case "s": CreateServer(); break;
case "c": CreateClient(); break;
case "f": getDownloadCount(); break;
}