--------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by-agent: Codex
37 lines
789 B
TypeScript
37 lines
789 B
TypeScript
import type { MetadataEventSource } from '@proj-airi/server-sdk'
|
|
|
|
interface EventSourcePayload {
|
|
source?: string
|
|
metadata?: { source?: MetadataEventSource }
|
|
}
|
|
|
|
function formatMetadataSource(source?: MetadataEventSource) {
|
|
if (!source)
|
|
return undefined
|
|
|
|
if ('extension' in source) {
|
|
return `${source.extension.id}:${source.id}`
|
|
}
|
|
|
|
return source.id
|
|
}
|
|
|
|
/**
|
|
* Resolves a stable source key for websocket-originated events.
|
|
*
|
|
* Before:
|
|
* - `{ source: "minecraft" }`
|
|
* - `{ metadata: { source: { extension: { id: "p" }, id: "i" } } }`
|
|
*
|
|
* After:
|
|
* - `"minecraft"`
|
|
* - `"p:i"`
|
|
*/
|
|
export function getEventSourceKey(event: EventSourcePayload, fallback = 'unknown') {
|
|
return (
|
|
formatMetadataSource(event.metadata?.source)
|
|
?? event.source
|
|
?? fallback
|
|
)
|
|
}
|