tor-browser

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

HandshakeProtocol.js (3047B)


      1 /*
      2 * A helper class for working with SignalR handshakes.
      3 *
      4 * Copyright (c) .NET Foundation. All rights reserved.
      5 *
      6 * This source code is licensed under the Apache License, Version 2.0,
      7 * found in the LICENSE.txt file in the root directory of the library
      8 * source tree.
      9 *
     10 * https://github.com/aspnet/AspNetCore
     11 */
     12 
     13 "use strict";
     14 
     15 Object.defineProperty(exports, "__esModule", { value: true });
     16 const TextMessageFormat = require("resource://devtools/client/netmonitor/src/components/messages/parsers/signalr/TextMessageFormat.js");
     17 const Utils = require("resource://devtools/client/netmonitor/src/components/messages/parsers/signalr/Utils.js");
     18 /** @private */
     19 class HandshakeProtocol {
     20  // Handshake request is always JSON
     21  writeHandshakeRequest(handshakeRequest) {
     22    return TextMessageFormat.TextMessageFormat.write(
     23      JSON.stringify(handshakeRequest)
     24    );
     25  }
     26  parseHandshakeResponse(data) {
     27    let messageData;
     28    let remainingData;
     29    if (
     30      Utils.isArrayBuffer(data) ||
     31      // eslint-disable-next-line no-undef
     32      (typeof Buffer !== "undefined" && data instanceof Buffer)
     33    ) {
     34      // Format is binary but still need to read JSON text from handshake response
     35      const binaryData = new Uint8Array(data);
     36      const separatorIndex = binaryData.indexOf(
     37        TextMessageFormat.TextMessageFormat.RecordSeparatorCode
     38      );
     39      if (separatorIndex === -1) {
     40        throw new Error("Message is incomplete.");
     41      }
     42      // content before separator is handshake response
     43      // optional content after is additional messages
     44      const responseLength = separatorIndex + 1;
     45      messageData = String.fromCharCode.apply(
     46        null,
     47        binaryData.slice(0, responseLength)
     48      );
     49      remainingData =
     50        binaryData.byteLength > responseLength
     51          ? binaryData.slice(responseLength).buffer
     52          : null;
     53    } else {
     54      const textData = data;
     55      const separatorIndex = textData.indexOf(
     56        TextMessageFormat.TextMessageFormat.RecordSeparator
     57      );
     58      if (separatorIndex === -1) {
     59        throw new Error("Message is incomplete.");
     60      }
     61      // content before separator is handshake response
     62      // optional content after is additional messages
     63      const responseLength = separatorIndex + 1;
     64      messageData = textData.substring(0, responseLength);
     65      remainingData =
     66        textData.length > responseLength
     67          ? textData.substring(responseLength)
     68          : null;
     69    }
     70    // At this point we should have just the single handshake message
     71    const messages = TextMessageFormat.TextMessageFormat.parse(messageData);
     72    const response = JSON.parse(messages[0]);
     73    if (response.type) {
     74      throw new Error("Expected a handshake response from the server.");
     75    }
     76    const responseMessage = response;
     77    // multiple messages could have arrived with handshake
     78    // return additional data to be parsed as usual, or null if all parsed
     79    return [remainingData, responseMessage];
     80  }
     81 }
     82 exports.HandshakeProtocol = HandshakeProtocol;