import { ActionType } from '../../_contract-src/action-payloads/action-type'; import { EventType } from '../../_contract-src/event-payloads/event-type'; import type { ActionRejectedPayload } from '../../_contract-src/event-payloads/action-rejected/action-rejected-payload'; import type { StructEventMessage } from '../../_contract-src/shared/struct-event-message'; import { getHostOrigin, isAllowedOrigin } from '../../_contract-src/setup/configure'; import { postToHost } from './post-to-host'; export interface HostRequest { /** The SDK method name, so errors name the API the developer actually called. */ label: string; actionType: ActionType; /** The event the host answers this action with. */ resolvesWith: EventType; timeoutMs: number; } const isNative = (fn: unknown): boolean => { try { return Function.prototype.toString.call(fn).includes('[native code]'); } catch { return false; } }; /** * Describes what can actually be observed about the failure. The previous message blamed the * embedding ("is the SDK running inside a Struct PIM iframe?") even when the SDK was embedded * correctly, which pointed real incident investigations at the wrong thing. */ function timedOutMessage(label: string, timeoutMs: number): string { if (window.parent === window) { return `[extension-sdk] ${label} timed out after ${timeoutMs}ms: this page is not embedded in an iframe, so no Struct PIM host can answer it.`; } const observations = [ `configured host origin: ${getHostOrigin() ?? 'none'}`, `window.postMessage: ${isNative(window.postMessage) ? 'native' : 'replaced by another script or browser extension'}`, `window.addEventListener: ${isNative(window.addEventListener) ? 'native' : 'replaced by another script or browser extension'}`, ].join('; '); return `[extension-sdk] ${label} timed out after ${timeoutMs}ms with no answer from the Struct PIM host (${observations}).`; } /** * Sends an action to Struct PIM and settles on the host's answer. * * Rejects as soon as the host reports it cannot serve the request instead of waiting out the * timeout. The host answers a request it cannot serve with ACTION_REJECTED, so ignoring that * event turns an immediate, specific reason into an indistinguishable timeout. */ export function requestFromHost({ label, actionType, resolvesWith, timeoutMs }: HostRequest): Promise { return new Promise((resolve, reject) => { const stopListening = () => { clearTimeout(timeoutHandle); window.removeEventListener('message', handler); }; function handler(event: MessageEvent>) { const message = event.data; if (message?.eventType !== resolvesWith && message?.eventType !== EventType.ACTION_REJECTED) return; if (!isAllowedOrigin(event.origin)) return; if (message.eventType === EventType.ACTION_REJECTED) { const rejection = message.payload as ActionRejectedPayload; // The host addresses a rejection to one action; leave other requests' rejections alone. if (rejection?.rejectedAction !== actionType) return; stopListening(); reject(new Error(`[extension-sdk] Struct PIM rejected ${label}: ${rejection.reason}`)); return; } stopListening(); resolve(message.payload as T); } const timeoutHandle = setTimeout(() => { stopListening(); reject(new Error(timedOutMessage(label, timeoutMs))); }, timeoutMs); window.addEventListener('message', handler); postToHost({ actionType }); }); }