HandshakeProtocol.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. // Copyright (c) .NET Foundation. All rights reserved.
  3. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. var TextMessageFormat_1 = require("./TextMessageFormat");
  6. var Utils_1 = require("./Utils");
  7. /** @private */
  8. var HandshakeProtocol = /** @class */ (function () {
  9. function HandshakeProtocol() {
  10. }
  11. // Handshake request is always JSON
  12. HandshakeProtocol.prototype.writeHandshakeRequest = function (handshakeRequest) {
  13. return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(handshakeRequest));
  14. };
  15. HandshakeProtocol.prototype.parseHandshakeResponse = function (data) {
  16. var responseMessage;
  17. var messageData;
  18. var remainingData;
  19. if (Utils_1.isArrayBuffer(data) || (typeof Buffer !== "undefined" && data instanceof Buffer)) {
  20. // Format is binary but still need to read JSON text from handshake response
  21. var binaryData = new Uint8Array(data);
  22. var separatorIndex = binaryData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparatorCode);
  23. if (separatorIndex === -1) {
  24. throw new Error("Message is incomplete.");
  25. }
  26. // content before separator is handshake response
  27. // optional content after is additional messages
  28. var responseLength = separatorIndex + 1;
  29. messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength));
  30. remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null;
  31. }
  32. else {
  33. var textData = data;
  34. var separatorIndex = textData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparator);
  35. if (separatorIndex === -1) {
  36. throw new Error("Message is incomplete.");
  37. }
  38. // content before separator is handshake response
  39. // optional content after is additional messages
  40. var responseLength = separatorIndex + 1;
  41. messageData = textData.substring(0, responseLength);
  42. remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null;
  43. }
  44. // At this point we should have just the single handshake message
  45. var messages = TextMessageFormat_1.TextMessageFormat.parse(messageData);
  46. var response = JSON.parse(messages[0]);
  47. if (response.type) {
  48. throw new Error("Expected a handshake response from the server.");
  49. }
  50. responseMessage = response;
  51. // multiple messages could have arrived with handshake
  52. // return additional data to be parsed as usual, or null if all parsed
  53. return [remainingData, responseMessage];
  54. };
  55. return HandshakeProtocol;
  56. }());
  57. exports.HandshakeProtocol = HandshakeProtocol;
  58. //# sourceMappingURL=HandshakeProtocol.js.map