| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- "use strict";
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- Object.defineProperty(exports, "__esModule", { value: true });
- var TextMessageFormat_1 = require("./TextMessageFormat");
- var Utils_1 = require("./Utils");
- /** @private */
- var HandshakeProtocol = /** @class */ (function () {
- function HandshakeProtocol() {
- }
- // Handshake request is always JSON
- HandshakeProtocol.prototype.writeHandshakeRequest = function (handshakeRequest) {
- return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(handshakeRequest));
- };
- HandshakeProtocol.prototype.parseHandshakeResponse = function (data) {
- var responseMessage;
- var messageData;
- var remainingData;
- if (Utils_1.isArrayBuffer(data) || (typeof Buffer !== "undefined" && data instanceof Buffer)) {
- // Format is binary but still need to read JSON text from handshake response
- var binaryData = new Uint8Array(data);
- var separatorIndex = binaryData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparatorCode);
- if (separatorIndex === -1) {
- throw new Error("Message is incomplete.");
- }
- // content before separator is handshake response
- // optional content after is additional messages
- var responseLength = separatorIndex + 1;
- messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength));
- remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null;
- }
- else {
- var textData = data;
- var separatorIndex = textData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparator);
- if (separatorIndex === -1) {
- throw new Error("Message is incomplete.");
- }
- // content before separator is handshake response
- // optional content after is additional messages
- var responseLength = separatorIndex + 1;
- messageData = textData.substring(0, responseLength);
- remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null;
- }
- // At this point we should have just the single handshake message
- var messages = TextMessageFormat_1.TextMessageFormat.parse(messageData);
- var response = JSON.parse(messages[0]);
- if (response.type) {
- throw new Error("Expected a handshake response from the server.");
- }
- responseMessage = response;
- // multiple messages could have arrived with handshake
- // return additional data to be parsed as usual, or null if all parsed
- return [remainingData, responseMessage];
- };
- return HandshakeProtocol;
- }());
- exports.HandshakeProtocol = HandshakeProtocol;
- //# sourceMappingURL=HandshakeProtocol.js.map
|