tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

serializers.js (2004B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  parseWampArray,
      9 } = require("resource://devtools/client/netmonitor/src/components/messages/parsers/wamp/arrayParser.js");
     10 const msgpack = require("resource://devtools/client/netmonitor/src/components/messages/msgpack.js");
     11 const cbor = require("resource://devtools/client/netmonitor/src/components/messages/cbor.js");
     12 
     13 class WampSerializer {
     14  deserializeMessage(payload) {
     15    const array = this.deserializeToArray(payload);
     16    const result = parseWampArray(array);
     17    return result;
     18  }
     19 
     20  stringToBinary(str) {
     21    const result = new Uint8Array(str.length);
     22    for (let i = 0; i < str.length; i++) {
     23      result[i] = str[i].charCodeAt(0);
     24    }
     25    return result;
     26  }
     27 }
     28 
     29 class JsonSerializer extends WampSerializer {
     30  constructor() {
     31    super(...arguments);
     32    this.subProtocol = "wamp.2.json";
     33    this.description = "WAMP JSON";
     34  }
     35  deserializeToArray(payload) {
     36    return JSON.parse(payload);
     37  }
     38 }
     39 
     40 class MessagePackSerializer extends WampSerializer {
     41  constructor() {
     42    super(...arguments);
     43    this.subProtocol = "wamp.2.msgpack";
     44    this.description = "WAMP MessagePack";
     45  }
     46  deserializeToArray(payload) {
     47    const binary = this.stringToBinary(payload);
     48    return msgpack.deserialize(binary);
     49  }
     50 }
     51 
     52 class CBORSerializer extends WampSerializer {
     53  constructor() {
     54    super(...arguments);
     55    this.subProtocol = "wamp.2.cbor";
     56    this.description = "WAMP CBOR";
     57  }
     58  deserializeToArray(payload) {
     59    const binaryBuffer = this.stringToBinary(payload).buffer;
     60    return cbor.decode(binaryBuffer);
     61  }
     62 }
     63 
     64 const serializers = {};
     65 for (var serializer of [
     66  new JsonSerializer(),
     67  new MessagePackSerializer(),
     68  new CBORSerializer(),
     69 ]) {
     70  serializers[serializer.subProtocol] = serializer;
     71 }
     72 
     73 module.exports = { wampSerializers: serializers };