const MAX_DISCRIMINATOR_LENGTH = 100; function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null; } /* * A discriminator is printed only when it is a string, and cut to a fixed length, so an input that * puts arbitrary data under `source` or `detail-type` can't push it into a log in full. */ function describeDiscriminator(value: unknown): string | undefined { if (typeof value !== "string") { return undefined; } return value.slice(0, MAX_DISCRIMINATOR_LENGTH); } /** * What an incoming input looks like, safe to put in an error message: its top-level keys and the two * EventBridge discriminators (`source`, `detail-type`, strings only, cut to 100 characters), never the * values of the other keys. For a value that isn't an object, only its `typeof`. */ export class InputShape { private constructor(private readonly summary: Record) {} static from(input: unknown): InputShape { if (!isRecord(input)) { return new InputShape({ type: typeof input }); } return new InputShape({ keys: Object.keys(input), source: describeDiscriminator(input.source), detailType: describeDiscriminator(input["detail-type"]) }); } toString(): string { return JSON.stringify(this.summary); } }