import { readIncomingType } from "../../bridge/incomingType.js" import type { InvalidSandboxMessage } from "../../bridge/messages/invalidSandboxMessage.js" export type InvalidSandboxMessageResponse = { incomingType: string | undefined nack?: InvalidSandboxMessage } export function getInvalidSandboxMessageResponse( data: unknown, ): InvalidSandboxMessageResponse { const incomingType = readIncomingType(data) if ( incomingType === "invalidHostMessage" || incomingType === "invalidSandboxMessage" ) { return { incomingType } } return { incomingType, nack: { type: "invalidSandboxMessage", reason: nackReasonForInvalidSandboxMessage(incomingType), }, } } function nackReasonForInvalidSandboxMessage( incomingType: string | undefined, ): string { if (incomingType === "ready") { return "Invalid manifest" } if (incomingType !== undefined) { return `Unsupported sandbox message of type "${incomingType}"` } return "Unsupported sandbox message (no `type` field or non-object payload)" }