(value: unknown, allowEmptyText = false)
| 364 | } |
| 365 | |
| 366 | export function decodeMessageContent(value: unknown, allowEmptyText = false): MessageContent { |
| 367 | let content: MessageContent; |
| 368 | try { |
| 369 | content = decodeCanonicalMessageContent(value); |
| 370 | } catch { |
| 371 | throw invalidProtocolFrame('Invalid Message content'); |
| 372 | } |
| 373 | requireUtf8String(content.text, 'Message text', TURN_MESSAGE_TEXT_MAX_BYTES, allowEmptyText); |
| 374 | if (content.displayText !== undefined) { |
| 375 | requireUtf8String( |
| 376 | content.displayText, |
| 377 | 'Message displayText', |
| 378 | TURN_MESSAGE_TEXT_MAX_BYTES, |
| 379 | true, |
| 380 | ); |
| 381 | } |
| 382 | if ((content.attachments?.length ?? 0) > MAX_ATTACHMENT_COUNT) { |
| 383 | throw invalidProtocolFrame('Invalid Message attachments'); |
| 384 | } |
| 385 | for (const attachment of content.attachments ?? []) { |
| 386 | if (!isCanonicalAttachmentRef(attachment)) { |
| 387 | throw invalidProtocolFrame('Invalid AttachmentRef'); |
| 388 | } |
| 389 | requireUtf8String(attachment.name, 'AttachmentRef name', ATTACHMENT_NAME_MAX_BYTES, false); |
| 390 | requireUtf8String( |
| 391 | attachment.mimeType, |
| 392 | 'AttachmentRef mimeType', |
| 393 | ATTACHMENT_MIME_TYPE_MAX_BYTES, |
| 394 | false, |
| 395 | ); |
| 396 | if (attachment.bytes > MAX_ATTACHMENT_BYTES) { |
| 397 | throw invalidProtocolFrame('Invalid AttachmentRef bytes'); |
| 398 | } |
| 399 | if (attachment.ref.kind === 'session_file') { |
| 400 | requireEntityId(attachment.ref.sessionId, 'AttachmentRef sessionId'); |
| 401 | } |
| 402 | const path = |
| 403 | attachment.ref.kind === 'external_file' |
| 404 | ? attachment.ref.absolutePath |
| 405 | : attachment.ref.relativePath; |
| 406 | requireUtf8String(path, 'AttachmentRef path', ATTACHMENT_PATH_MAX_BYTES, false); |
| 407 | } |
| 408 | if ((content.quotes?.length ?? 0) > TURN_MESSAGE_QUOTE_MAX_COUNT) { |
| 409 | throw invalidProtocolFrame('Invalid Message quotes'); |
| 410 | } |
| 411 | for (const quote of content.quotes ?? []) { |
| 412 | requireString(quote.text, 'QuoteRef text', TURN_MESSAGE_QUOTE_TEXT_MAX_LENGTH); |
| 413 | if (quote.label !== undefined) { |
| 414 | requireString(quote.label, 'QuoteRef label', TURN_MESSAGE_QUOTE_LABEL_MAX_LENGTH); |
| 415 | } |
| 416 | if (quote.sourceTurnId !== undefined) { |
| 417 | requireEntityId(quote.sourceTurnId, 'QuoteRef sourceTurnId'); |
| 418 | } |
| 419 | } |
| 420 | requireEncodedByteLimit(content, 'Message content', TURN_MESSAGE_CONTENT_MAX_BYTES); |
| 421 | return content; |
| 422 | } |
| 423 |
no test coverage detected