summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/components/messages/parsers/wamp/serializers.js
blob: c2139d320c145981c30099477eb4062776ac813a (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  parseWampArray,
} = require("resource://devtools/client/netmonitor/src/components/messages/parsers/wamp/arrayParser.js");
const msgpack = require("resource://devtools/client/netmonitor/src/components/messages/msgpack.js");
const cbor = require("resource://devtools/client/netmonitor/src/components/messages/cbor.js");

class WampSerializer {
  deserializeMessage(payload) {
    const array = this.deserializeToArray(payload);
    const result = parseWampArray(array);
    return result;
  }

  stringToBinary(str) {
    const result = new Uint8Array(str.length);
    for (let i = 0; i < str.length; i++) {
      result[i] = str[i].charCodeAt(0);
    }
    return result;
  }
}

class JsonSerializer extends WampSerializer {
  constructor() {
    super(...arguments);
    this.subProtocol = "wamp.2.json";
    this.description = "WAMP JSON";
  }
  deserializeToArray(payload) {
    return JSON.parse(payload);
  }
}

class MessagePackSerializer extends WampSerializer {
  constructor() {
    super(...arguments);
    this.subProtocol = "wamp.2.msgpack";
    this.description = "WAMP MessagePack";
  }
  deserializeToArray(payload) {
    const binary = this.stringToBinary(payload);
    return msgpack.deserialize(binary);
  }
}

class CBORSerializer extends WampSerializer {
  constructor() {
    super(...arguments);
    this.subProtocol = "wamp.2.cbor";
    this.description = "WAMP CBOR";
  }
  deserializeToArray(payload) {
    const binaryBuffer = this.stringToBinary(payload).buffer;
    return cbor.decode(binaryBuffer);
  }
}

const serializers = {};
for (var serializer of [
  new JsonSerializer(),
  new MessagePackSerializer(),
  new CBORSerializer(),
]) {
  serializers[serializer.subProtocol] = serializer;
}

module.exports = { wampSerializers: serializers };