-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanrpc-example-pipe-server-cli.ts
137 lines (111 loc) · 2.91 KB
/
panrpc-example-pipe-server-cli.ts
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
/* 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 {
#counter = 0;
constructor() {
this.Increment = this.Increment.bind(this);
}
async Increment(ctx: ILocalContext, delta: number): Promise<number> {
console.log(
"Incrementing counter by",
delta,
"for remote with ID",
ctx.remoteID
);
this.#counter += delta;
return this.#counter;
}
}
class Remote {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
async Println(ctx: IRemoteContext, msg: string) {}
}
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`);
process.on("SIGHUP", async () => {
await registry.forRemotes(async (remoteID, remote) => {
console.error("Calling functions for remote with ID", remoteID);
try {
await remote.Println(undefined, "Hello, world!");
} 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");