Skip to content

Commit 9e00aab

Browse files
committed
Make id optional for JSON-RPC
1 parent 9f8d6b7 commit 9e00aab

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

client/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ function main() {
9797
const msg: unknown = JSON.parse(event.data);
9898
console.log("received:", msg);
9999

100+
const result: { [key: string]: unknown } = {};
101+
100102
if (!msg || typeof msg !== "object") {
101103
throw new TypeError("received message not object");
102104
}
@@ -109,8 +111,8 @@ function main() {
109111
throw new TypeError("'body' not an object");
110112
}
111113

112-
if (!("id" in msg.body)) {
113-
throw new TypeError("'id' not in message body");
114+
if ("id" in msg.body) {
115+
result.id = msg.body.id;
114116
}
115117

116118
if (!("method" in msg.body)) {
@@ -133,17 +135,14 @@ function main() {
133135
throw new TypeError("'number' not in message body");
134136
}
135137

136-
const response: unknown = await blockchain.send(
138+
result.response = await blockchain.send(
137139
msg.body.method,
138140
msg.body.params
139141
);
140142
webSocket?.send(
141143
JSON.stringify({
142144
number: msg.number,
143-
result: {
144-
id: msg.body.id,
145-
result: response,
146-
},
145+
result,
147146
})
148147
);
149148
})

0 commit comments

Comments
 (0)