|
1 | | -const LIVE_PREVIEW_CHANNEL_NAME = "toolboxaid.livePreviewSync.v1"; |
| 1 | +export const LIVE_PREVIEW_CHANNEL_NAME = "toolboxaid.livePreviewSync.v1"; |
2 | 2 | const LIVE_PREVIEW_STORAGE_KEY = "__toolboxaid_live_preview_sync_v1__"; |
| 3 | +const VALID_EVENT_TYPES = new Set([ |
| 4 | + "update", |
| 5 | + "tool-live-preview-sync", |
| 6 | + "runtime-state-binding" |
| 7 | +]); |
3 | 8 |
|
4 | 9 | function sanitizeText(value) { |
5 | 10 | return typeof value === "string" ? value.trim() : ""; |
6 | 11 | } |
7 | 12 |
|
8 | 13 | function toMessageEnvelope(sourceId, payload, eventType) { |
| 14 | + const normalizedEventType = sanitizeText(eventType) || "update"; |
9 | 15 | return { |
10 | 16 | channel: LIVE_PREVIEW_CHANNEL_NAME, |
11 | 17 | sourceId: sanitizeText(sourceId) || "unknown-source", |
12 | | - eventType: sanitizeText(eventType) || "update", |
| 18 | + eventType: VALID_EVENT_TYPES.has(normalizedEventType) ? normalizedEventType : "update", |
13 | 19 | updatedAt: Date.now(), |
14 | 20 | payload |
15 | 21 | }; |
16 | 22 | } |
17 | 23 |
|
| 24 | +function hasObjectField(payload, key) { |
| 25 | + return payload |
| 26 | + && Object.prototype.hasOwnProperty.call(payload, key) |
| 27 | + && payload[key] |
| 28 | + && typeof payload[key] === "object"; |
| 29 | +} |
| 30 | + |
| 31 | +function hasRuntimeState(payload) { |
| 32 | + return hasObjectField(payload, "runtimeState"); |
| 33 | +} |
| 34 | + |
| 35 | +function hasToolStatePayload(payload) { |
| 36 | + return hasObjectField(payload, "tileMapDocument") |
| 37 | + || hasObjectField(payload, "parallaxDocument"); |
| 38 | +} |
| 39 | + |
| 40 | +export function validateStateBindingPayload(payload) { |
| 41 | + if (!payload || typeof payload !== "object") { |
| 42 | + return { valid: false, reason: "payload must be an object." }; |
| 43 | + } |
| 44 | + |
| 45 | + const toolId = sanitizeText(payload.toolId || ""); |
| 46 | + if (!toolId) { |
| 47 | + return { valid: false, reason: "payload.toolId is required." }; |
| 48 | + } |
| 49 | + |
| 50 | + if (!hasRuntimeState(payload) && !hasToolStatePayload(payload)) { |
| 51 | + return { |
| 52 | + valid: false, |
| 53 | + reason: "payload must include runtimeState, tileMapDocument, or parallaxDocument." |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + if (hasRuntimeState(payload)) { |
| 58 | + const runtimeState = payload.runtimeState; |
| 59 | + const heroX = Number(runtimeState.heroX); |
| 60 | + const heroY = Number(runtimeState.heroY); |
| 61 | + const cameraX = Number(runtimeState.cameraX); |
| 62 | + const cameraY = Number(runtimeState.cameraY); |
| 63 | + if ( |
| 64 | + !Number.isFinite(heroX) |
| 65 | + || !Number.isFinite(heroY) |
| 66 | + || !Number.isFinite(cameraX) |
| 67 | + || !Number.isFinite(cameraY) |
| 68 | + ) { |
| 69 | + return { |
| 70 | + valid: false, |
| 71 | + reason: "runtimeState requires finite hero/camera coordinates." |
| 72 | + }; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return { valid: true }; |
| 77 | +} |
| 78 | + |
18 | 79 | export function createLivePreviewSyncBridge(options = {}) { |
19 | 80 | const sourceId = sanitizeText(options.sourceId) || "unknown-source"; |
20 | 81 | const onMessage = typeof options.onMessage === "function" ? options.onMessage : null; |
@@ -60,6 +121,10 @@ export function createLivePreviewSyncBridge(options = {}) { |
60 | 121 | if (disposed || !payload || typeof payload !== "object") { |
61 | 122 | return { status: "ignored" }; |
62 | 123 | } |
| 124 | + const validation = validateStateBindingPayload(payload); |
| 125 | + if (!validation.valid) { |
| 126 | + return { status: "rejected", reason: validation.reason }; |
| 127 | + } |
63 | 128 | const signature = JSON.stringify(payload); |
64 | 129 | if (signature === lastPayloadSignature) { |
65 | 130 | return { status: "deduped" }; |
@@ -104,4 +169,3 @@ export function createLivePreviewSyncBridge(options = {}) { |
104 | 169 | dispose |
105 | 170 | }; |
106 | 171 | } |
107 | | - |
|
0 commit comments