-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanrpc-example-pipe-client-cli.ts
More file actions
144 lines (117 loc) · 3.22 KB
/
panrpc-example-pipe-client-cli.ts
File metadata and controls
144 lines (117 loc) · 3.22 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
140
141
142
143
144
/* eslint-disable no-console */
import { stdin, stdout } from "process";
// eslint-disable-next-line import/no-extraneous-dependencies
import { JSONParser } from "@streamparser/json-whatwg";
import { ILocalContext, IRemoteContext, Registry } from "../index";
class Local {
// eslint-disable-next-line class-methods-use-this
async Println(ctx: ILocalContext, msg: string) {
console.error("Printing message", msg, "for remote with ID", ctx.remoteID);
console.log(msg);
}
}
class Remote {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async Increment(ctx: IRemoteContext, delta: number): Promise<number> {
return 0;
}
}
let clients = 0;
const registry = new Registry(
new Local(),
new Remote(),
{
onClientConnect: () => {
clients++;
console.error(clients, "clients connected");
},
onClientDisconnect: () => {
clients--;
console.error(clients, "clients connected");
},
}
);
console.error(`Run one of the following commands to run a function on the remote(s):
- kill -SIGHUP ${process.pid}: Increment remote counter by one
- kill -SIGUSR2 ${process.pid}: Decrement remote counter by one`);
process.on("SIGHUP", async () => {
await registry.forRemotes(async (remoteID, remote) => {
console.error("Calling functions for remote with ID", remoteID);
try {
const res = await remote.Increment(undefined, 1);
console.error(res);
} catch (e) {
console.error(`Got error for Increment func: ${e}`);
}
});
});
process.on("SIGUSR2", async () => {
await registry.forRemotes(async (remoteID, remote) => {
console.error("Calling functions for remote with ID", remoteID);
try {
const res = await remote.Increment(undefined, -1);
console.error(res);
} catch (e) {
console.error(`Got error for Increment func: ${e}`);
}
});
});
const linkSignal = new AbortController();
const encoder = new WritableStream({
write(chunk) {
return new Promise<void>((res) => {
const isDrained = stdout.write(JSON.stringify(chunk));
if (!isDrained) {
stdout.once("drain", res);
} else {
res();
}
});
},
close() {
return new Promise((res) => {
stdout.end(res);
});
},
abort(reason) {
stdout.destroy(reason instanceof Error ? reason : new Error(reason));
},
});
const parser = new JSONParser({
paths: ["$"],
separator: "",
});
const parserWriter = parser.writable.getWriter();
const parserReader = parser.readable.getReader();
const decoder = new ReadableStream({
start(controller) {
parserReader
.read()
.then(async function process({ done, value }) {
if (done) {
controller.close();
return;
}
controller.enqueue(value?.value);
parserReader
.read()
.then(process)
.catch((e) => controller.error(e));
})
.catch((e) => controller.error(e));
},
});
stdin.on("data", (m) => parserWriter.write(m));
stdin.on("close", () => {
parserReader.cancel();
parserWriter.abort();
linkSignal.abort();
});
registry.linkStream(
linkSignal.signal,
encoder,
decoder,
(v) => v,
(v) => v
);
console.error("Connected to stdin and stdout");