-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathecho_client.ts
More file actions
170 lines (155 loc) · 5.76 KB
/
Copy pathecho_client.ts
File metadata and controls
170 lines (155 loc) · 5.76 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Echo IPC client (TypeScript) — uses the GENERATED AsyncApi client over the
* @aztec/ipc-runtime transport. Defaults to UDS; pass `--transport shm` to
* drive the bundled NAPI SHM client (`createNapiShmAsyncClient`) instead.
* Path suffix follows the same convention as ipc::make_client on the C++
* side: `.sock` for UDS, `.shm` for MPSC-SHM rings.
*
* Usage: npx tsx echo_client.ts --socket /tmp/echo.sock [--transport uds|shm]
* Exits 0 on success, 1 on failure.
*/
import {
createNapiShmAsyncClient,
UdsIpcClient,
type IpcClientAsync,
} from "@aztec/ipc-runtime";
import { AsyncApi } from "./generated/async.js";
const args = process.argv.slice(2);
const socketIdx = args.indexOf("--socket");
const socketPath = socketIdx >= 0 ? args[socketIdx + 1] : undefined;
if (!socketPath) {
console.error("Usage: echo_client.ts --socket <path> [--transport uds|shm]");
process.exit(1);
}
function testHash(base: number): Uint8Array {
return Uint8Array.from({ length: 32 }, (_v, i) => base + i);
}
const transportIdx = args.indexOf("--transport");
const transport = transportIdx >= 0 ? args[transportIdx + 1] : "uds";
if (transport !== "uds" && transport !== "shm") {
console.error(`Unknown --transport '${transport}' (expected uds|shm)`);
process.exit(1);
}
function assertEqual(actual: unknown, expected: unknown, label: string) {
const a = JSON.stringify(actual);
const e = JSON.stringify(expected);
if (a !== e) throw new Error(`${label}: expected ${e}, got ${a}`);
}
function assertBytes(actual: Uint8Array, expected: Uint8Array, label: string) {
assertEqual(
Buffer.from(actual).toString("hex"),
Buffer.from(expected).toString("hex"),
label,
);
}
async function run() {
// SHM clients identify the shared-memory base name without the `.shm`
// suffix — match ipc::make_client's behaviour on the C++ side.
const client: IpcClientAsync =
transport === "shm"
? createNapiShmAsyncClient(socketPath!.replace(/\.shm$/, ""))
: await UdsIpcClient.connect(socketPath!);
const api = new AsyncApi(client);
// Test 1: EchoBytes
const testData = Uint8Array.from([0xde, 0xad, 0xbe, 0xef, 0x42]);
const resp1 = await api.bytes({ data: testData });
assertBytes(resp1.data, testData, "EchoBytes data");
console.error("echo_client(ts): EchoBytes OK");
// Test 2: EchoFields
const resp2 = await api.fields({
a: 42,
b: 999999,
name: "hello wire compat",
});
assertEqual(resp2.a, 42, "EchoFields a");
assertEqual(resp2.b, 999999, "EchoFields b");
assertEqual(resp2.name, "hello wire compat", "EchoFields name field");
console.error("echo_client(ts): EchoFields OK");
// Test 3: EchoNested
const inner = {
values: [Uint8Array.from([1, 2, 3]), Uint8Array.from([4, 5])],
flag: true,
};
const resp3 = await api.nested({ inner });
assertEqual(resp3.inner.flag, true, "EchoNested flag");
assertEqual(resp3.inner.values.length, 2, "EchoNested values length");
assertBytes(resp3.inner.values[0]!, inner.values[0]!, "EchoNested values[0]");
console.error("echo_client(ts): EchoNested OK");
// Test 4: EchoAliases
const hash = testHash(0x10);
const second = testHash(0x40);
const resp4 = await api.aliases({
treeId: 7,
hash,
maybeHash: second,
hashes: [hash, second],
});
assertEqual(resp4.treeId, 7, "EchoAliases treeId");
assertBytes(resp4.hash, hash, "EchoAliases hash");
assertBytes(resp4.maybeHash!, second, "EchoAliases maybeHash");
assertEqual(resp4.hashes.length, 2, "EchoAliases hashes length");
assertBytes(resp4.hashes[0]!, hash, "EchoAliases hashes[0]");
assertBytes(resp4.hashes[1]!, second, "EchoAliases hashes[1]");
console.error("echo_client(ts): EchoAliases OK");
// Test 5: EchoAliases with maybeHash absent (optional over live IPC)
const resp5 = await api.aliases({
treeId: 7,
hash,
maybeHash: null,
hashes: [hash],
});
assertEqual(resp5.maybeHash, null, "EchoAliases maybeHash none");
console.error("echo_client(ts): EchoAliases none OK");
// Test 6: EchoFields with b > 2^32 (uint64 wire encoding over live IPC)
const big = Number.MAX_SAFE_INTEGER;
const resp6 = await api.fields({ a: 42, b: big, name: "big" });
assertEqual(resp6.b, big, "EchoFields u64");
// Values past 2^53 must throw client-side rather than silently lose precision.
let threw = false;
try {
await api.fields({ a: 42, b: 2 ** 60, name: "too big" });
} catch {
threw = true;
}
assertEqual(threw, true, "EchoFields u64 guard");
console.error("echo_client(ts): EchoFields u64 OK");
// Test 7: EchoBlobs — optional bytes Some/None and fixed [bytes; 2]
const resp7 = await api.blobs({
maybeData: Uint8Array.from([0xaa, 0xbb]),
parts: [Uint8Array.from([1, 2, 3]), Uint8Array.from([4])],
});
assertBytes(
resp7.maybeData!,
Uint8Array.from([0xaa, 0xbb]),
"EchoBlobs maybeData",
);
assertBytes(
resp7.parts[0]!,
Uint8Array.from([1, 2, 3]),
"EchoBlobs parts[0]",
);
assertBytes(resp7.parts[1]!, Uint8Array.from([4]), "EchoBlobs parts[1]");
const resp7b = await api.blobs({
maybeData: null,
parts: [Uint8Array.from([]), Uint8Array.from([9])],
});
assertEqual(resp7b.maybeData, null, "EchoBlobs maybeData none");
console.error("echo_client(ts): EchoBlobs OK");
// Test 8: EchoFail — server error surfaces with its message
let failMessage = "";
try {
await api.fail({ message: "deliberate failure" });
} catch (e: any) {
failMessage = e.message;
}
if (!failMessage.includes("deliberate failure")) {
throw new Error(`EchoFail: expected error message, got '${failMessage}'`);
}
console.error("echo_client(ts): EchoFail OK");
await api.destroy();
console.error("echo_client(ts): all tests passed");
}
run().catch((e) => {
console.error(`echo_client(ts): FAILED: ${e.message}`);
process.exit(1);
});