UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

58 lines (56 loc) 2 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. import { InvalidOperationError } from "./Error"; import { createNoDashGuid } from "./Guid"; export var MessageType; (function (MessageType) { MessageType[MessageType["Text"] = 0] = "Text"; MessageType[MessageType["Binary"] = 1] = "Binary"; })(MessageType || (MessageType = {})); export class ConnectionMessage { constructor(messageType, body, headers, id) { this.privBody = null; if (messageType === MessageType.Text && body && !(typeof (body) === "string")) { throw new InvalidOperationError("Payload must be a string"); } if (messageType === MessageType.Binary && body && !(body instanceof ArrayBuffer)) { throw new InvalidOperationError("Payload must be ArrayBuffer"); } this.privMessageType = messageType; this.privBody = body; this.privHeaders = headers ? headers : {}; this.privId = id ? id : createNoDashGuid(); switch (this.messageType) { case MessageType.Binary: this.privSize = this.binaryBody !== null ? this.binaryBody.byteLength : 0; break; case MessageType.Text: this.privSize = this.textBody.length; } } get messageType() { return this.privMessageType; } get headers() { return this.privHeaders; } get body() { return this.privBody; } get textBody() { if (this.privMessageType === MessageType.Binary) { throw new InvalidOperationError("Not supported for binary message"); } return this.privBody; } get binaryBody() { if (this.privMessageType === MessageType.Text) { throw new InvalidOperationError("Not supported for text message"); } return this.privBody; } get id() { return this.privId; } } //# sourceMappingURL=ConnectionMessage.js.map