JSONHubProtocol.js (4187B)
1 /* 2 * Implements the SignalR Hub Protocol. 3 * 4 * Copyright (c) .NET Foundation. All rights reserved. 5 * 6 * This source code is licensed under the Apache License, Version 2.0, 7 * found in the LICENSE.txt file in the root directory of the library 8 * source tree. 9 * 10 * https://github.com/aspnet/AspNetCore 11 */ 12 13 "use strict"; 14 15 Object.defineProperty(exports, "__esModule", { value: true }); 16 const IHubProtocol = require("resource://devtools/client/netmonitor/src/components/messages/parsers/signalr/IHubProtocol.js"); 17 const TextMessageFormat = require("resource://devtools/client/netmonitor/src/components/messages/parsers/signalr/TextMessageFormat.js"); 18 /** Implements the JSON Hub Protocol. */ 19 class JsonHubProtocol { 20 /** 21 * Creates an array of {@link @microsoft/signalr.HubMessage} objects from the specified serialized representation. 22 * 23 * @param {string} input A string containing the serialized representation. 24 */ 25 parseMessages(input) { 26 // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error. 27 if (typeof input !== "string") { 28 throw new Error( 29 "Invalid input for JSON hub protocol. Expected a string." 30 ); 31 } 32 if (!input) { 33 return []; 34 } 35 // Parse the messages 36 const messages = TextMessageFormat.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 IHubProtocol.MessageType.Invocation: 45 this.isInvocationMessage(parsedMessage); 46 break; 47 case IHubProtocol.MessageType.StreamItem: 48 this.isStreamItemMessage(parsedMessage); 49 break; 50 case IHubProtocol.MessageType.Completion: 51 this.isCompletionMessage(parsedMessage); 52 break; 53 case IHubProtocol.MessageType.Ping: 54 // Single value, no need to validate 55 break; 56 case IHubProtocol.MessageType.Close: 57 // All optional values, no need to validate 58 break; 59 default: 60 // Future protocol changes can add message types, new kinds of messages 61 // will show up without having to update Firefox. 62 break; 63 } 64 // Map numeric message type to their textual name if it exists 65 parsedMessage.type = 66 IHubProtocol.MessageType[parsedMessage.type] || parsedMessage.type; 67 hubMessages.push(parsedMessage); 68 } 69 return hubMessages; 70 } 71 /** 72 * Writes the specified {@link @microsoft/signalr.HubMessage} to a string and returns it. 73 * 74 * @param {HubMessage} message The message to write. 75 * @returns {string} A string containing the serialized representation of the message. 76 */ 77 writeMessage(message) { 78 return TextMessageFormat.TextMessageFormat.write(JSON.stringify(message)); 79 } 80 isInvocationMessage(message) { 81 this.assertNotEmptyString( 82 message.target, 83 "Invalid payload for Invocation message." 84 ); 85 if (message.invocationId !== undefined) { 86 this.assertNotEmptyString( 87 message.invocationId, 88 "Invalid payload for Invocation message." 89 ); 90 } 91 } 92 isStreamItemMessage(message) { 93 this.assertNotEmptyString( 94 message.invocationId, 95 "Invalid payload for StreamItem message." 96 ); 97 if (message.item === undefined) { 98 throw new Error("Invalid payload for StreamItem message."); 99 } 100 } 101 isCompletionMessage(message) { 102 if (message.result && message.error) { 103 throw new Error("Invalid payload for Completion message."); 104 } 105 if (!message.result && message.error) { 106 this.assertNotEmptyString( 107 message.error, 108 "Invalid payload for Completion message." 109 ); 110 } 111 this.assertNotEmptyString( 112 message.invocationId, 113 "Invalid payload for Completion message." 114 ); 115 } 116 assertNotEmptyString(value, errorMessage) { 117 if (typeof value !== "string" || value === "") { 118 throw new Error(errorMessage); 119 } 120 } 121 } 122 exports.JsonHubProtocol = JsonHubProtocol;