aboutsummaryrefslogtreecommitdiff
path: root/src/wasm/msgpackrpc.js
blob: 11d467d9cbe6d8ec67d7a2eff7581885302c782b (plain) (blame)
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
// msgpack-rpc message shapes:
//   request:      [0, msgid, method, params]
//   response:     [1, msgid, error, result]
//   notification: [2, method, params]

const Protocol = {
  encodeRequest(msgid, method, params) {
    return MsgpackCodec.encode([0, msgid, method, params]);
  },

  encodeNotification(method, params) {
    return MsgpackCodec.encode([2, method, params]);
  },

  // classifies a decoded msgpack-rpc array into a tagged object.
  // throws if msg doesn't look like a valid rpc message.
  parseMessage(msg) {
    if (!Array.isArray(msg)) throw new Error("rpc message is not an array");
    const [type, ...rest] = msg;
    if (type === 0) {
      const [msgid, method, params] = rest;
      return { kind: "request", msgid, method, params };
    }
    if (type === 1) {
      const [msgid, error, result] = rest;
      return { kind: "response", msgid, error, result };
    }
    if (type === 2) {
      const [method, params] = rest;
      return { kind: "notification", method, params };
    }
    throw new Error("unknown rpc message type: " + type);
  },
};