JsonHubProtocol.js 4.3 KB

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