File size: 11,996 Bytes
f1dd159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { createObjectDecoder, hasShape, isString, type ApiDecoder } from "@/shared/api/decoder";
import { runtimeConfig } from "@/shared/config/runtime-config";
import { i18n } from "@/shared/i18n";

export class ApiError extends Error {
  readonly status: number;
  readonly code: string;
  readonly requestId?: string;

  constructor(status: number, code: string, message: string, requestId?: string) {
    super(message);
    this.name = "ApiError";
    this.status = status;
    this.code = code;
    this.requestId = requestId;
  }
}

let accessToken: string | null = null;
let refreshPromise: Promise<RefreshResult> | null = null;
const sessionInvalidatedListeners = new Set<() => void>();
const refreshLockName = "grok2api:admin-session-refresh";
const maxEventStreamBufferCharacters = 1 << 20;
const eventStreamInactivityTimeoutMs = 60_000;

export type RefreshResult = "refreshed" | "invalid" | "unavailable";

export function setAccessToken(token: string | null): void {
  accessToken = token;
}

export function subscribeSessionInvalidated(listener: () => void): () => void {
  sessionInvalidatedListeners.add(listener);
  return () => sessionInvalidatedListeners.delete(listener);
}

function invalidateSession(): void {
  accessToken = null;
  sessionInvalidatedListeners.forEach((listener) => listener());
}

function localizedErrorMessage(code: string, fallback: string): string {
  const key = `apiErrors.${code}`;
  return i18n.exists(key) ? i18n.t(key) : fallback;
}

async function parseResponse<T>(response: Response, decode: ApiDecoder<T>): Promise<T> {
  const payload: unknown = await response.json().catch(() => null);
  if (!response.ok) {
    const error = readErrorEnvelope(payload);
    const code = error.code ?? "requestFailed";
    throw new ApiError(response.status, code, localizedErrorMessage(code, error.message ?? `HTTP ${response.status}`), error.requestId);
  }
  if (!isRecord(payload) || !("data" in payload)) {
    throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
  }
  try {
    return decode(payload.data);
  } catch {
    throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
  }
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function readErrorEnvelope(payload: unknown): { code?: string; message?: string; requestId?: string } {
  if (!isRecord(payload) || !isRecord(payload.error)) return {};
  return {
    code: typeof payload.error.code === "string" ? payload.error.code : undefined,
    message: typeof payload.error.message === "string" ? payload.error.message : undefined,
    requestId: typeof payload.error.requestId === "string" ? payload.error.requestId : undefined,
  };
}

async function requestRefresh(): Promise<RefreshResult> {
  try {
    const response = await fetch(`${runtimeConfig.apiBaseUrl}/api/admin/v1/auth/refresh`, {
      method: "POST",
      credentials: "include",
      headers: { "Content-Type": "application/json" },
      body: "{}",
    });
    if (response.status === 401) {
      invalidateSession();
      return "invalid";
    }
    const tokens = await parseResponse(response, decodeAuthTokensDTO);
    setAccessToken(tokens.accessToken);
    return "refreshed";
  } catch {
    return "unavailable";
  }
}

async function requestRefreshWithBrowserLock(): Promise<RefreshResult> {
  if (!("locks" in navigator)) {
    return requestRefresh();
  }
  try {
    return await navigator.locks.request(refreshLockName, requestRefresh);
  } catch {
    return "unavailable";
  }
}

export async function refreshAccessToken(): Promise<RefreshResult> {
  if (!refreshPromise) {
    refreshPromise = requestRefreshWithBrowserLock()
      .finally(() => {
        refreshPromise = null;
      });
  }
  return refreshPromise;
}

type RequestOptions = Omit<RequestInit, "body"> & {
  body?: BodyInit | object;
  authenticated?: boolean;
  retryAuth?: boolean;
};

async function sendApiRequest(path: string, options: RequestOptions): Promise<Response> {
  const { authenticated = true, retryAuth, body, headers, ...requestInit } = options;
  void retryAuth;
  const requestHeaders = new Headers(headers);
  let requestBody: BodyInit | undefined;

  if (body instanceof FormData || typeof body === "string" || body instanceof Blob) {
    requestBody = body;
  } else if (body !== undefined) {
    requestHeaders.set("Content-Type", "application/json");
    requestBody = JSON.stringify(body);
  }
  if (authenticated && accessToken) {
    requestHeaders.set("Authorization", `Bearer ${accessToken}`);
  }

  return fetch(`${runtimeConfig.apiBaseUrl}${path}`, {
    ...requestInit,
    body: requestBody,
    credentials: "include",
    headers: requestHeaders,
  });
}

export async function apiRequest<T>(path: string, options: RequestOptions, decode: ApiDecoder<T>): Promise<T> {
  const { authenticated = true, retryAuth = true } = options;
  const response = await sendApiRequest(path, options);

  if (response.status === 401 && authenticated && retryAuth) {
    const refreshResult = await refreshAccessToken();
    if (refreshResult === "refreshed") {
      return apiRequest<T>(path, { ...options, retryAuth: false }, decode);
    }
    if (refreshResult === "unavailable") {
      throw new ApiError(503, "sessionRefreshUnavailable", localizedErrorMessage("sessionRefreshUnavailable", "Unable to refresh the session. Please retry."));
    }
  }

  return parseResponse(response, decode);
}

export type ApiStreamEvent<T> = {
  event: string;
  data: T;
};

// apiEventStream 使用现有管理员鉴权发起 POST SSE,并正确处理任意分块边界。
export async function apiEventStream<T>(path: string, options: RequestOptions, decode: ApiDecoder<T>, onEvent: (value: ApiStreamEvent<T>) => void): Promise<void> {
  const { authenticated = true, retryAuth = true } = options;
  const response = await sendApiRequest(path, options);
  if (response.status === 401 && authenticated && retryAuth) {
    const refreshResult = await refreshAccessToken();
    if (refreshResult === "refreshed") {
      return apiEventStream(path, { ...options, retryAuth: false }, decode, onEvent);
    }
    if (refreshResult === "unavailable") {
      throw new ApiError(503, "sessionRefreshUnavailable", localizedErrorMessage("sessionRefreshUnavailable", "Unable to refresh the session. Please retry."));
    }
  }
  if (!response.ok) {
    await parseResponse(response, decodeNever);
  }
  if (!response.body) {
    throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
  }
  const contentType = response.headers.get("Content-Type")?.toLowerCase() ?? "";
  if (!contentType.startsWith("text/event-stream")) {
    await response.body.cancel().catch(() => undefined);
    throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
  }

  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  let buffer = "";
  const dispatch = (block: string) => {
    let event = "message";
    const data: string[] = [];
    block.split("\n").forEach((line) => {
      const normalized = line.endsWith("\r") ? line.slice(0, -1) : line;
      if (normalized.startsWith("event:")) event = normalized.slice(6).trim();
      if (normalized.startsWith("data:")) data.push(normalized.slice(5).trimStart());
    });
    if (data.length === 0) return;
    let payload: T;
    try {
      payload = decode(JSON.parse(data.join("\n")) as unknown);
    } catch {
      throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
    }
    onEvent({ event, data: payload });
  };

  try {
    for (;;) {
      const { done, value } = await readEventStreamChunk(reader, response.status);
      buffer += decoder.decode(value, { stream: !done });
      buffer = buffer.replaceAll("\r\n", "\n");
      let boundary = buffer.indexOf("\n\n");
      while (boundary >= 0) {
        if (boundary > maxEventStreamBufferCharacters) {
          throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
        }
        dispatch(buffer.slice(0, boundary));
        buffer = buffer.slice(boundary + 2);
        boundary = buffer.indexOf("\n\n");
      }
      if (buffer.length > maxEventStreamBufferCharacters) {
        throw new ApiError(response.status, "invalidResponse", localizedErrorMessage("invalidResponse", "Server returned an invalid response"));
      }
      if (done) break;
    }
    if (buffer.trim()) dispatch(buffer);
  } catch (error) {
    await reader.cancel().catch(() => undefined);
    throw error;
  } finally {
    reader.releaseLock();
  }
}

async function readEventStreamChunk(reader: ReadableStreamDefaultReader<Uint8Array>, status: number): Promise<ReadableStreamReadResult<Uint8Array>> {
  let timeout = 0;
  const inactivity = new Promise<never>((_, reject) => {
    timeout = window.setTimeout(() => {
      reject(new ApiError(status, "streamTimeout", localizedErrorMessage("streamTimeout", "The progress stream stopped responding")));
    }, eventStreamInactivityTimeoutMs);
  });
  try {
    return await Promise.race([reader.read(), inactivity]);
  } finally {
    window.clearTimeout(timeout);
  }
}

export type ApiDownloadResult = {
  blob: Blob;
  headers: Headers;
};

export async function apiDownloadResponse(path: string, options: RequestOptions = {}): Promise<ApiDownloadResult> {
  const { authenticated = true, retryAuth = true } = options;
  const response = await sendApiRequest(path, options);
  if (response.status === 401 && authenticated && retryAuth) {
    const refreshResult = await refreshAccessToken();
    if (refreshResult === "refreshed") return apiDownloadResponse(path, { ...options, retryAuth: false });
    if (refreshResult === "unavailable") {
      throw new ApiError(503, "sessionRefreshUnavailable", localizedErrorMessage("sessionRefreshUnavailable", "Unable to refresh the session. Please retry."));
    }
  }
  if (!response.ok) {
    await parseResponse(response, decodeNever);
    throw new ApiError(response.status, "requestFailed", localizedErrorMessage("requestFailed", "The request failed"));
  }
  return { blob: await response.blob(), headers: response.headers };
}

export async function apiDownload(path: string, options: RequestOptions = {}): Promise<Blob> {
  return (await apiDownloadResponse(path, options)).blob;
}

export type AdminDTO = {
  id: string;
  username: string;
};

export type AuthTokensDTO = {
  accessToken: string;
  accessTokenExpiresAt: string;
  refreshTokenExpiresAt: string;
};

export type LoginResponseDTO = {
  admin: AdminDTO;
  tokens: AuthTokensDTO;
};

const adminValidator = hasShape({ id: isString, username: isString });
const authTokensValidator = hasShape({ accessToken: isString, accessTokenExpiresAt: isString, refreshTokenExpiresAt: isString });

export const decodeAdminDTO = createObjectDecoder<AdminDTO>("admin", { id: isString, username: isString });
export const decodeAuthTokensDTO = createObjectDecoder<AuthTokensDTO>("auth tokens", {
  accessToken: isString,
  accessTokenExpiresAt: isString,
  refreshTokenExpiresAt: isString,
});
export const decodeLoginResponseDTO = createObjectDecoder<LoginResponseDTO>("login", { admin: adminValidator, tokens: authTokensValidator });
export const decodeLoggedOut = createObjectDecoder<{ loggedOut: boolean }>("logout", { loggedOut: (value) => typeof value === "boolean" });

function decodeNever(): never {
  throw new Error("unexpected successful response");
}

export type PaginatedDTO<T> = {
  items: T[];
  page: number;
  pageSize: number;
  total: number;
};