| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- import { MessageType } from "./IHubProtocol";
- import { LogLevel } from "./ILogger";
- import { TransferFormat } from "./ITransport";
- import { NullLogger } from "./Loggers";
- import { TextMessageFormat } from "./TextMessageFormat";
- var JSON_HUB_PROTOCOL_NAME = "json";
- /** Implements the JSON Hub Protocol. */
- var JsonHubProtocol = /** @class */ (function () {
- function JsonHubProtocol() {
- /** @inheritDoc */
- this.name = JSON_HUB_PROTOCOL_NAME;
- /** @inheritDoc */
- this.version = 1;
- /** @inheritDoc */
- this.transferFormat = TransferFormat.Text;
- }
- /** Creates an array of {@link @aspnet/signalr.HubMessage} objects from the specified serialized representation.
- *
- * @param {string} input A string containing the serialized representation.
- * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
- */
- JsonHubProtocol.prototype.parseMessages = function (input, logger) {
- // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error.
- if (typeof input !== "string") {
- throw new Error("Invalid input for JSON hub protocol. Expected a string.");
- }
- if (!input) {
- return [];
- }
- if (logger === null) {
- logger = NullLogger.instance;
- }
- // Parse the messages
- var messages = TextMessageFormat.parse(input);
- var hubMessages = [];
- for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
- var message = messages_1[_i];
- var parsedMessage = JSON.parse(message);
- if (typeof parsedMessage.type !== "number") {
- throw new Error("Invalid payload.");
- }
- switch (parsedMessage.type) {
- case MessageType.Invocation:
- this.isInvocationMessage(parsedMessage);
- break;
- case MessageType.StreamItem:
- this.isStreamItemMessage(parsedMessage);
- break;
- case MessageType.Completion:
- this.isCompletionMessage(parsedMessage);
- break;
- case MessageType.Ping:
- // Single value, no need to validate
- break;
- case MessageType.Close:
- // All optional values, no need to validate
- break;
- default:
- // Future protocol changes can add message types, old clients can ignore them
- logger.log(LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored.");
- continue;
- }
- hubMessages.push(parsedMessage);
- }
- return hubMessages;
- };
- /** Writes the specified {@link @aspnet/signalr.HubMessage} to a string and returns it.
- *
- * @param {HubMessage} message The message to write.
- * @returns {string} A string containing the serialized representation of the message.
- */
- JsonHubProtocol.prototype.writeMessage = function (message) {
- return TextMessageFormat.write(JSON.stringify(message));
- };
- JsonHubProtocol.prototype.isInvocationMessage = function (message) {
- this.assertNotEmptyString(message.target, "Invalid payload for Invocation message.");
- if (message.invocationId !== undefined) {
- this.assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message.");
- }
- };
- JsonHubProtocol.prototype.isStreamItemMessage = function (message) {
- this.assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message.");
- if (message.item === undefined) {
- throw new Error("Invalid payload for StreamItem message.");
- }
- };
- JsonHubProtocol.prototype.isCompletionMessage = function (message) {
- if (message.result && message.error) {
- throw new Error("Invalid payload for Completion message.");
- }
- if (!message.result && message.error) {
- this.assertNotEmptyString(message.error, "Invalid payload for Completion message.");
- }
- this.assertNotEmptyString(message.invocationId, "Invalid payload for Completion message.");
- };
- JsonHubProtocol.prototype.assertNotEmptyString = function (value, errorMessage) {
- if (typeof value !== "string" || value === "") {
- throw new Error(errorMessage);
- }
- };
- return JsonHubProtocol;
- }());
- export { JsonHubProtocol };
- //# sourceMappingURL=JsonHubProtocol.js.map
|