JsonHubProtocol.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  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. var JSON_HUB_PROTOCOL_NAME = "json";
  9. /** Implements the JSON Hub Protocol. */
  10. var JsonHubProtocol = /** @class */ (function () {
  11. function JsonHubProtocol() {
  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 @aspnet/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. JsonHubProtocol.prototype.parseMessages = function (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. var messages = TextMessageFormat.parse(input);
  37. var hubMessages = [];
  38. for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
  39. var message = messages_1[_i];
  40. var parsedMessage = JSON.parse(message);
  41. if (typeof parsedMessage.type !== "number") {
  42. throw new Error("Invalid payload.");
  43. }
  44. switch (parsedMessage.type) {
  45. case MessageType.Invocation:
  46. this.isInvocationMessage(parsedMessage);
  47. break;
  48. case MessageType.StreamItem:
  49. this.isStreamItemMessage(parsedMessage);
  50. break;
  51. case MessageType.Completion:
  52. this.isCompletionMessage(parsedMessage);
  53. break;
  54. case MessageType.Ping:
  55. // Single value, no need to validate
  56. break;
  57. case MessageType.Close:
  58. // All optional values, no need to validate
  59. break;
  60. default:
  61. // Future protocol changes can add message types, old clients can ignore them
  62. logger.log(LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored.");
  63. continue;
  64. }
  65. hubMessages.push(parsedMessage);
  66. }
  67. return hubMessages;
  68. };
  69. /** Writes the specified {@link @aspnet/signalr.HubMessage} to a string and returns it.
  70. *
  71. * @param {HubMessage} message The message to write.
  72. * @returns {string} A string containing the serialized representation of the message.
  73. */
  74. JsonHubProtocol.prototype.writeMessage = function (message) {
  75. return TextMessageFormat.write(JSON.stringify(message));
  76. };
  77. JsonHubProtocol.prototype.isInvocationMessage = function (message) {
  78. this.assertNotEmptyString(message.target, "Invalid payload for Invocation message.");
  79. if (message.invocationId !== undefined) {
  80. this.assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message.");
  81. }
  82. };
  83. JsonHubProtocol.prototype.isStreamItemMessage = function (message) {
  84. this.assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message.");
  85. if (message.item === undefined) {
  86. throw new Error("Invalid payload for StreamItem message.");
  87. }
  88. };
  89. JsonHubProtocol.prototype.isCompletionMessage = function (message) {
  90. if (message.result && message.error) {
  91. throw new Error("Invalid payload for Completion message.");
  92. }
  93. if (!message.result && message.error) {
  94. this.assertNotEmptyString(message.error, "Invalid payload for Completion message.");
  95. }
  96. this.assertNotEmptyString(message.invocationId, "Invalid payload for Completion message.");
  97. };
  98. JsonHubProtocol.prototype.assertNotEmptyString = function (value, errorMessage) {
  99. if (typeof value !== "string" || value === "") {
  100. throw new Error(errorMessage);
  101. }
  102. };
  103. return JsonHubProtocol;
  104. }());
  105. export { JsonHubProtocol };
  106. //# sourceMappingURL=JsonHubProtocol.js.map