JsonHubProtocol.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. // Licensed to the .NET Foundation under one or more agreements.
  3. // The .NET Foundation licenses this file to you under the MIT license.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.JsonHubProtocol = void 0;
  6. const IHubProtocol_1 = require("./IHubProtocol");
  7. const ILogger_1 = require("./ILogger");
  8. const ITransport_1 = require("./ITransport");
  9. const Loggers_1 = require("./Loggers");
  10. const TextMessageFormat_1 = require("./TextMessageFormat");
  11. const JSON_HUB_PROTOCOL_NAME = "json";
  12. /** Implements the JSON Hub Protocol. */
  13. class JsonHubProtocol {
  14. constructor() {
  15. /** @inheritDoc */
  16. this.name = JSON_HUB_PROTOCOL_NAME;
  17. /** @inheritDoc */
  18. this.version = 1;
  19. /** @inheritDoc */
  20. this.transferFormat = ITransport_1.TransferFormat.Text;
  21. }
  22. /** Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation.
  23. *
  24. * @param {string} input A string containing the serialized representation.
  25. * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
  26. */
  27. parseMessages(input, logger) {
  28. // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error.
  29. if (typeof input !== "string") {
  30. throw new Error("Invalid input for JSON hub protocol. Expected a string.");
  31. }
  32. if (!input) {
  33. return [];
  34. }
  35. if (logger === null) {
  36. logger = Loggers_1.NullLogger.instance;
  37. }
  38. // Parse the messages
  39. const messages = TextMessageFormat_1.TextMessageFormat.parse(input);
  40. const hubMessages = [];
  41. for (const message of messages) {
  42. const parsedMessage = JSON.parse(message);
  43. if (typeof parsedMessage.type !== "number") {
  44. throw new Error("Invalid payload.");
  45. }
  46. switch (parsedMessage.type) {
  47. case IHubProtocol_1.MessageType.Invocation:
  48. this._isInvocationMessage(parsedMessage);
  49. break;
  50. case IHubProtocol_1.MessageType.StreamItem:
  51. this._isStreamItemMessage(parsedMessage);
  52. break;
  53. case IHubProtocol_1.MessageType.Completion:
  54. this._isCompletionMessage(parsedMessage);
  55. break;
  56. case IHubProtocol_1.MessageType.Ping:
  57. // Single value, no need to validate
  58. break;
  59. case IHubProtocol_1.MessageType.Close:
  60. // All optional values, no need to validate
  61. break;
  62. default:
  63. // Future protocol changes can add message types, old clients can ignore them
  64. logger.log(ILogger_1.LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored.");
  65. continue;
  66. }
  67. hubMessages.push(parsedMessage);
  68. }
  69. return hubMessages;
  70. }
  71. /** Writes the specified {@link @microsoft/signalr.HubMessage} to a string and returns it.
  72. *
  73. * @param {HubMessage} message The message to write.
  74. * @returns {string} A string containing the serialized representation of the message.
  75. */
  76. writeMessage(message) {
  77. return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(message));
  78. }
  79. _isInvocationMessage(message) {
  80. this._assertNotEmptyString(message.target, "Invalid payload for Invocation message.");
  81. if (message.invocationId !== undefined) {
  82. this._assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message.");
  83. }
  84. }
  85. _isStreamItemMessage(message) {
  86. this._assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message.");
  87. if (message.item === undefined) {
  88. throw new Error("Invalid payload for StreamItem message.");
  89. }
  90. }
  91. _isCompletionMessage(message) {
  92. if (message.result && message.error) {
  93. throw new Error("Invalid payload for Completion message.");
  94. }
  95. if (!message.result && message.error) {
  96. this._assertNotEmptyString(message.error, "Invalid payload for Completion message.");
  97. }
  98. this._assertNotEmptyString(message.invocationId, "Invalid payload for Completion message.");
  99. }
  100. _assertNotEmptyString(value, errorMessage) {
  101. if (typeof value !== "string" || value === "") {
  102. throw new Error(errorMessage);
  103. }
  104. }
  105. }
  106. exports.JsonHubProtocol = JsonHubProtocol;
  107. //# sourceMappingURL=JsonHubProtocol.js.map