@uniai-fe/uds-templates 0.6.11 → 0.6.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniai-fe/uds-templates",
3
- "version": "0.6.11",
3
+ "version": "0.6.13",
4
4
  "description": "UNIAI Design System; UI Templates Package",
5
5
  "type": "module",
6
6
  "private": false,
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { startWhepStream, type WhepStreamHandle } from "@uniai-fe/util-rtc";
4
4
  import {
5
+ getCctvDebugJwtMetadata,
5
6
  getCctvDebugKeyLabel,
6
7
  getCctvDebugUrlLabel,
7
8
  logCctvDebugEvent,
@@ -31,6 +32,8 @@ interface CctvRtcStreamStartParams {
31
32
  video: HTMLVideoElement;
32
33
  }
33
34
 
35
+ const WHEP_ERROR_BODY_PREVIEW_LIMIT = 1000;
36
+
34
37
  const getEntrySnapshot = (
35
38
  entry?: CctvRtcStreamEntry,
36
39
  ): CctvRtcStreamSnapshot => ({
@@ -51,6 +54,56 @@ const notifyEntry = (entry: CctvRtcStreamEntry) => {
51
54
  entry.listeners.forEach(listener => listener(snapshot));
52
55
  };
53
56
 
57
+ const getWhepResponseBodyPreview = async (
58
+ response: Response,
59
+ ): Promise<string | null> => {
60
+ try {
61
+ const text = await response.clone().text();
62
+ return text.slice(0, WHEP_ERROR_BODY_PREVIEW_LIMIT);
63
+ } catch {
64
+ return null;
65
+ }
66
+ };
67
+
68
+ /**
69
+ * WHEP 실패 body를 startWhepStream 외부에서 관찰하기 위한 fetch wrapper.
70
+ * @desc
71
+ * 성공 응답은 그대로 통과시키고, 실패 응답에서만 clone body를 읽어 debug log에 남긴다.
72
+ */
73
+ const createCctvDebugWhepFetcher =
74
+ ({
75
+ endpoint,
76
+ identityKey,
77
+ streamKey,
78
+ token,
79
+ }: {
80
+ endpoint: string;
81
+ identityKey: string;
82
+ streamKey: string;
83
+ token: string;
84
+ }): typeof fetch =>
85
+ async (input, init) => {
86
+ const response = await fetch(input, init);
87
+ if (response.ok) return response;
88
+
89
+ logCctvDebugEvent({
90
+ event: "whep:response-error",
91
+ level: "error",
92
+ payload: {
93
+ endpoint: getCctvDebugUrlLabel(endpoint),
94
+ identityKey: getCctvDebugKeyLabel(identityKey),
95
+ responseBody: await getWhepResponseBodyPreview(response),
96
+ status: response.status,
97
+ statusText: response.statusText,
98
+ streamKey: getCctvDebugKeyLabel(streamKey),
99
+ token: getCctvDebugJwtMetadata(token),
100
+ },
101
+ source: "streamRegistry",
102
+ });
103
+
104
+ return response;
105
+ };
106
+
54
107
  const closeEntry = (entry: CctvRtcStreamEntry) => {
55
108
  logCctvDebugEvent({
56
109
  event: "entry:close",
@@ -188,12 +241,19 @@ export function createCctvRtcStreamRegistry(): CctvRtcStreamRegistry {
188
241
  endpoint: getCctvDebugUrlLabel(endpoint),
189
242
  identityKey: getCctvDebugKeyLabel(identityKey),
190
243
  streamKey: getCctvDebugKeyLabel(streamKey),
244
+ token: getCctvDebugJwtMetadata(token),
191
245
  },
192
246
  source: "streamRegistry",
193
247
  });
194
248
 
195
249
  startWhepStream({
196
250
  endpoint,
251
+ fetcher: createCctvDebugWhepFetcher({
252
+ endpoint,
253
+ identityKey,
254
+ streamKey,
255
+ token,
256
+ }),
197
257
  token,
198
258
  video,
199
259
  signal: controller.signal,
@@ -32,6 +32,15 @@ export interface CctvDebugSummary {
32
32
  total: number;
33
33
  }
34
34
 
35
+ export interface CctvDebugJwtMetadata {
36
+ expiresAt: string | null;
37
+ expiresInSec: number | null;
38
+ headerAlg: string | null;
39
+ headerKid: string | null;
40
+ tokenLabel: string | null;
41
+ tokenPartCount: number;
42
+ }
43
+
35
44
  export interface CctvDebugBuffer {
36
45
  clear: () => void;
37
46
  dump: () => CctvDebugEvent[];
@@ -108,6 +117,13 @@ const getDebugStorageOverride = (keys: readonly string[]): boolean | null => {
108
117
  return null;
109
118
  };
110
119
 
120
+ const getDebugConsoleOverride = (): boolean | null => {
121
+ const queryOverride = getDebugQueryOverride(DEBUG_CONSOLE_QUERY_KEYS);
122
+ if (queryOverride !== null) return queryOverride;
123
+
124
+ return getDebugStorageOverride(DEBUG_CONSOLE_STORAGE_KEYS);
125
+ };
126
+
111
127
  const isLocalhostDebugDefaultEnabled = (): boolean => {
112
128
  const { hostname } = window.location;
113
129
  return (
@@ -221,6 +237,57 @@ const summarizeCctvDebugEvents = (
221
237
  };
222
238
  };
223
239
 
240
+ const decodeBase64UrlJson = (value: string): Record<string, unknown> | null => {
241
+ try {
242
+ const base64 = value.replace(/-/g, "+").replace(/_/g, "/");
243
+ const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
244
+ const parsed = JSON.parse(atob(padded));
245
+
246
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
247
+ ? (parsed as Record<string, unknown>)
248
+ : null;
249
+ } catch {
250
+ return null;
251
+ }
252
+ };
253
+
254
+ const getJwtStringClaim = (
255
+ claims: Record<string, unknown> | null,
256
+ key: string,
257
+ ): string | null => {
258
+ const value = claims?.[key];
259
+ return typeof value === "string" ? value : null;
260
+ };
261
+
262
+ const getJwtExpiresAt = (
263
+ claims: Record<string, unknown> | null,
264
+ ): { expiresAt: string | null; expiresInSec: number | null } => {
265
+ const exp = claims?.exp;
266
+ if (typeof exp !== "number" || !Number.isFinite(exp)) {
267
+ return { expiresAt: null, expiresInSec: null };
268
+ }
269
+
270
+ const expiresAtMs = exp * 1000;
271
+ return {
272
+ expiresAt: new Date(expiresAtMs).toISOString(),
273
+ expiresInSec: Math.floor((expiresAtMs - Date.now()) / 1000),
274
+ };
275
+ };
276
+
277
+ const stringifyCctvDebugEvent = (entry: CctvDebugEvent): string => {
278
+ try {
279
+ return JSON.stringify(entry);
280
+ } catch {
281
+ return JSON.stringify({
282
+ at: entry.at,
283
+ event: entry.event,
284
+ level: entry.level,
285
+ seq: entry.seq,
286
+ source: entry.source,
287
+ });
288
+ }
289
+ };
290
+
224
291
  /**
225
292
  * identity/stream key를 원문 대신 추적 가능한 hash label로 바꾼다.
226
293
  */
@@ -253,11 +320,37 @@ export const getCctvDebugUrlLabel = (
253
320
  }
254
321
  };
255
322
 
323
+ /**
324
+ * JWT 원문 대신 헤더/만료시각과 hash label만 기록한다.
325
+ * @desc
326
+ * token signature mismatch를 추적하기 위한 메타 정보이며, Bearer token 원문과 payload claim은
327
+ * debug log에 남기지 않는다.
328
+ */
329
+ export const getCctvDebugJwtMetadata = (
330
+ token: string | null | undefined,
331
+ ): CctvDebugJwtMetadata | null => {
332
+ if (!token) return null;
333
+
334
+ const [headerPart, payloadPart] = token.split(".");
335
+ const header = headerPart ? decodeBase64UrlJson(headerPart) : null;
336
+ const payload = payloadPart ? decodeBase64UrlJson(payloadPart) : null;
337
+ const { expiresAt, expiresInSec } = getJwtExpiresAt(payload);
338
+
339
+ return {
340
+ expiresAt,
341
+ expiresInSec,
342
+ headerAlg: getJwtStringClaim(header, "alg"),
343
+ headerKid: getJwtStringClaim(header, "kid"),
344
+ tokenLabel: getCctvDebugKeyLabel(token),
345
+ tokenPartCount: token.split(".").length,
346
+ };
347
+ };
348
+
256
349
  /**
257
350
  * CCTV debug logging 활성화 여부를 확인한다.
258
351
  * @desc
259
- * production default는 off다. `?udsCctvDebug=1` 또는
260
- * `localStorage.setItem("uds:cctv:debug", "1")`로 runtime에서 있다.
352
+ * production default는 off, localhost default는 ring buffer on이다.
353
+ * console 출력은 별도 console flag를 켠 경우에만 활성화한다.
261
354
  */
262
355
  export const isCctvDebugEnabled = (): boolean => {
263
356
  if (!isBrowser()) return false;
@@ -265,6 +358,9 @@ export const isCctvDebugEnabled = (): boolean => {
265
358
  const queryOverride = getDebugQueryOverride(DEBUG_QUERY_KEYS);
266
359
  if (queryOverride !== null) return queryOverride;
267
360
 
361
+ const consoleOverride = getDebugConsoleOverride();
362
+ if (consoleOverride === true) return true;
363
+
268
364
  const storageOverride = getDebugStorageOverride(DEBUG_STORAGE_KEYS);
269
365
  if (storageOverride !== null) return storageOverride;
270
366
 
@@ -274,18 +370,7 @@ export const isCctvDebugEnabled = (): boolean => {
274
370
  const isCctvDebugConsoleEnabled = (): boolean => {
275
371
  if (!isBrowser()) return false;
276
372
 
277
- const consoleQueryOverride = getDebugQueryOverride(DEBUG_CONSOLE_QUERY_KEYS);
278
- if (consoleQueryOverride !== null) return consoleQueryOverride;
279
-
280
- const consoleStorageOverride = getDebugStorageOverride(
281
- DEBUG_CONSOLE_STORAGE_KEYS,
282
- );
283
- if (consoleStorageOverride !== null) return consoleStorageOverride;
284
-
285
- return (
286
- getDebugQueryOverride(DEBUG_QUERY_KEYS) === true ||
287
- getDebugStorageOverride(DEBUG_STORAGE_KEYS) === true
288
- );
373
+ return getDebugConsoleOverride() === true;
289
374
  };
290
375
 
291
376
  /**
@@ -372,5 +457,5 @@ export const logCctvDebugEvent = ({
372
457
  ? console.info
373
458
  : console.debug;
374
459
 
375
- logger.call(console, "[UDS:CCTV]", entry);
460
+ logger.call(console, `[UDS:CCTV] ${stringifyCctvDebugEvent(entry)}`);
376
461
  };