|
| 1 | +export type WearMode = "live" | "preview"; |
| 2 | + |
| 3 | +export type LiveMoonGeometryV1 = { |
| 4 | + radiusNorm: number; |
| 5 | + centerXNorm: number; |
| 6 | + centerYNorm: number; |
| 7 | +}; |
| 8 | + |
| 9 | +export type LiveRenderPayloadV1 = { |
| 10 | + version: 1; |
| 11 | + mode: "live"; |
| 12 | + generatedAtUtc: string; |
| 13 | + watchLatDeg: number; |
| 14 | + watchLonDeg: number; |
| 15 | +} & ( |
| 16 | + | { |
| 17 | + showMoon: false; |
| 18 | + } |
| 19 | + | { |
| 20 | + showMoon: true; |
| 21 | + moon: LiveMoonGeometryV1; |
| 22 | + } |
| 23 | +); |
| 24 | + |
| 25 | +export type PreviewVisualV1 = { |
| 26 | + sunRadiusNorm: number; |
| 27 | + moonRadiusNorm: number; |
| 28 | + moonClosestOffsetNorm: number; |
| 29 | + moonTravelHalfSpanNorm: number; |
| 30 | +}; |
| 31 | + |
| 32 | +export type PreviewRenderPayloadV1 = { |
| 33 | + version: 1; |
| 34 | + mode: "preview"; |
| 35 | + previewSessionId: string; |
| 36 | + eclipseId: string; |
| 37 | + timelineStartUtc: string; |
| 38 | + timelineEndUtc: string; |
| 39 | + initialProgress: number; |
| 40 | + visual: PreviewVisualV1; |
| 41 | +}; |
| 42 | + |
| 43 | +export type WearRenderPayloadV1 = LiveRenderPayloadV1 | PreviewRenderPayloadV1; |
| 44 | + |
| 45 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 46 | + return typeof value === "object" && value !== null; |
| 47 | +} |
| 48 | + |
| 49 | +function clamp01(value: number): number { |
| 50 | + if (!Number.isFinite(value)) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + return Math.min(1, Math.max(0, value)); |
| 54 | +} |
| 55 | + |
| 56 | +function getFiniteNumber(value: unknown): number | null { |
| 57 | + return typeof value === "number" && Number.isFinite(value) ? value : null; |
| 58 | +} |
| 59 | + |
| 60 | +function getString(value: unknown): string | null { |
| 61 | + return typeof value === "string" && value.length > 0 ? value : null; |
| 62 | +} |
| 63 | + |
| 64 | +export function sanitizeLiveRenderPayloadV1(input: unknown): LiveRenderPayloadV1 | null { |
| 65 | + if (!isRecord(input) || input.version !== 1 || input.mode !== "live") { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + const generatedAtUtc = getString(input.generatedAtUtc); |
| 70 | + const watchLatDeg = getFiniteNumber(input.watchLatDeg); |
| 71 | + const watchLonDeg = getFiniteNumber(input.watchLonDeg); |
| 72 | + |
| 73 | + if ( |
| 74 | + !generatedAtUtc || |
| 75 | + watchLatDeg === null || |
| 76 | + watchLonDeg === null || |
| 77 | + watchLatDeg < -90 || |
| 78 | + watchLatDeg > 90 || |
| 79 | + watchLonDeg < -180 || |
| 80 | + watchLonDeg > 180 || |
| 81 | + typeof input.showMoon !== "boolean" |
| 82 | + ) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + if (!input.showMoon) { |
| 87 | + return { |
| 88 | + version: 1, |
| 89 | + mode: "live", |
| 90 | + generatedAtUtc, |
| 91 | + watchLatDeg, |
| 92 | + watchLonDeg, |
| 93 | + showMoon: false, |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + if (!isRecord(input.moon)) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + const radiusNorm = getFiniteNumber(input.moon.radiusNorm); |
| 102 | + const centerXNorm = getFiniteNumber(input.moon.centerXNorm); |
| 103 | + const centerYNorm = getFiniteNumber(input.moon.centerYNorm); |
| 104 | + |
| 105 | + if (radiusNorm === null || centerXNorm === null || centerYNorm === null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + version: 1, |
| 111 | + mode: "live", |
| 112 | + generatedAtUtc, |
| 113 | + watchLatDeg, |
| 114 | + watchLonDeg, |
| 115 | + showMoon: true, |
| 116 | + moon: { |
| 117 | + radiusNorm: clamp01(radiusNorm), |
| 118 | + centerXNorm: clamp01(centerXNorm), |
| 119 | + centerYNorm: clamp01(centerYNorm), |
| 120 | + }, |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +export function sanitizePreviewRenderPayloadV1(input: unknown): PreviewRenderPayloadV1 | null { |
| 125 | + if ( |
| 126 | + !isRecord(input) || |
| 127 | + input.version !== 1 || |
| 128 | + input.mode !== "preview" || |
| 129 | + !isRecord(input.visual) |
| 130 | + ) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + const previewSessionId = getString(input.previewSessionId); |
| 135 | + const eclipseId = getString(input.eclipseId); |
| 136 | + const timelineStartUtc = getString(input.timelineStartUtc); |
| 137 | + const timelineEndUtc = getString(input.timelineEndUtc); |
| 138 | + const initialProgress = getFiniteNumber(input.initialProgress); |
| 139 | + const sunRadiusNorm = getFiniteNumber(input.visual.sunRadiusNorm); |
| 140 | + const moonRadiusNorm = getFiniteNumber(input.visual.moonRadiusNorm); |
| 141 | + const moonClosestOffsetNorm = getFiniteNumber(input.visual.moonClosestOffsetNorm); |
| 142 | + const moonTravelHalfSpanNorm = getFiniteNumber(input.visual.moonTravelHalfSpanNorm); |
| 143 | + |
| 144 | + if ( |
| 145 | + !previewSessionId || |
| 146 | + !eclipseId || |
| 147 | + !timelineStartUtc || |
| 148 | + !timelineEndUtc || |
| 149 | + initialProgress === null || |
| 150 | + sunRadiusNorm === null || |
| 151 | + moonRadiusNorm === null || |
| 152 | + moonClosestOffsetNorm === null || |
| 153 | + moonTravelHalfSpanNorm === null |
| 154 | + ) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + return { |
| 159 | + version: 1, |
| 160 | + mode: "preview", |
| 161 | + previewSessionId, |
| 162 | + eclipseId, |
| 163 | + timelineStartUtc, |
| 164 | + timelineEndUtc, |
| 165 | + initialProgress: clamp01(initialProgress), |
| 166 | + visual: { |
| 167 | + sunRadiusNorm: clamp01(sunRadiusNorm), |
| 168 | + moonRadiusNorm: clamp01(moonRadiusNorm), |
| 169 | + moonClosestOffsetNorm: clamp01(moonClosestOffsetNorm), |
| 170 | + moonTravelHalfSpanNorm: clamp01(moonTravelHalfSpanNorm), |
| 171 | + }, |
| 172 | + }; |
| 173 | +} |
| 174 | + |
| 175 | +export function sanitizeWearRenderPayloadV1(input: unknown): WearRenderPayloadV1 | null { |
| 176 | + if (!isRecord(input) || input.version !== 1) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + if (input.mode === "live") { |
| 181 | + return sanitizeLiveRenderPayloadV1(input); |
| 182 | + } |
| 183 | + |
| 184 | + if (input.mode === "preview") { |
| 185 | + return sanitizePreviewRenderPayloadV1(input); |
| 186 | + } |
| 187 | + |
| 188 | + return null; |
| 189 | +} |
| 190 | + |
| 191 | +export function createSunOnlyLivePayload(params: { |
| 192 | + generatedAtUtc: string; |
| 193 | + watchLatDeg: number; |
| 194 | + watchLonDeg: number; |
| 195 | +}): LiveRenderPayloadV1 { |
| 196 | + return { |
| 197 | + version: 1, |
| 198 | + mode: "live", |
| 199 | + generatedAtUtc: params.generatedAtUtc, |
| 200 | + watchLatDeg: params.watchLatDeg, |
| 201 | + watchLonDeg: params.watchLonDeg, |
| 202 | + showMoon: false, |
| 203 | + }; |
| 204 | +} |
0 commit comments