tor-browser

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

index.js (1309B)


      1 /*
      2 * SockJS is a browser JavaScript library that provides a WebSocket-like object.
      3 * SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency,
      4 * full duplex, cross-domain communication channel between the browser and the web server.
      5 *
      6 * Copyright (c) 2011-2018 The sockjs-client Authors.
      7 *
      8 * This source code is licensed under the MIT license found in the
      9 * LICENSE file in the root directory of the library source tree.
     10 *
     11 * https://github.com/sockjs/sockjs-client
     12 */
     13 
     14 "use strict";
     15 
     16 function parseSockJS(msg) {
     17  const type = msg.slice(0, 1);
     18  const content = msg.slice(1);
     19 
     20  // first check for messages that don't need a payload
     21  switch (type) {
     22    case "o":
     23      return { type: "open" };
     24    case "h":
     25      return { type: "heartbeat" };
     26  }
     27 
     28  let payload;
     29  if (content) {
     30    try {
     31      payload = JSON.parse(content);
     32    } catch (e) {
     33      return null;
     34    }
     35  }
     36 
     37  if (typeof payload === "undefined") {
     38    return null;
     39  }
     40 
     41  switch (type) {
     42    case "a":
     43      return { type: "message", data: payload };
     44    case "m":
     45      return { type: "message", data: payload };
     46    case "c": {
     47      const [code, message] = payload;
     48      return { type: "close", code, message };
     49    }
     50    default:
     51      return null;
     52  }
     53 }
     54 
     55 module.exports = {
     56  parseSockJS,
     57 };