JsonHubProtocol.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 IHubProtocol_1 = require("./IHubProtocol");
  6. var ILogger_1 = require("./ILogger");
  7. var ITransport_1 = require("./ITransport");
  8. var Loggers_1 = require("./Loggers");
  9. var TextMessageFormat_1 = require("./TextMessageFormat");
  10. var JSON_HUB_PROTOCOL_NAME = "json";
  11. /** Implements the JSON Hub Protocol. */
  12. var JsonHubProtocol = /** @class */ (function () {
  13. function JsonHubProtocol() {
  14. /** @inheritDoc */
  15. this.name = JSON_HUB_PROTOCOL_NAME;
  16. /** @inheritDoc */
  17. this.version = 1;
  18. /** @inheritDoc */
  19. this.transferFormat = ITransport_1.TransferFormat.Text;
  20. }
  21. /** Creates an array of {@link @aspnet/signalr.HubMessage} objects from the specified serialized representation.
  22. *
  23. * @param {string} input A string containing the serialized representation.
  24. * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
  25. */
  26. JsonHubProtocol.prototype.parseMessages = function (input, logger) {
  27. // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error.
  28. if (typeof input !== "string") {
  29. throw new Error("Invalid input for JSON hub protocol. Expected a string.");
  30. }
  31. if (!input) {
  32. return [];
  33. }
  34. if (logger === null) {
  35. logger = Loggers_1.NullLogger.instance;
  36. }
  37. // Parse the messages
  38. var messages = TextMessageFormat_1.TextMessageFormat.parse(input);
  39. var hubMessages = [];
  40. for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
  41. var message = messages_1[_i];
  42. var 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 @aspnet/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. JsonHubProtocol.prototype.writeMessage = function (message) {
  77. return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(message));
  78. };
  79. JsonHubProtocol.prototype.isInvocationMessage = function (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. JsonHubProtocol.prototype.isStreamItemMessage = function (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. JsonHubProtocol.prototype.isCompletionMessage = function (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. JsonHubProtocol.prototype.assertNotEmptyString = function (value, errorMessage) {
  101. if (typeof value !== "string" || value === "") {
  102. throw new Error(errorMessage);
  103. }
  104. };
  105. return JsonHubProtocol;
  106. }());
  107. exports.JsonHubProtocol = JsonHubProtocol;
  108. //# sourceMappingURL=JsonHubProtocol.js.map