@antzsoft/chat-core 1.4.4 → 1.4.6
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/README.md +64 -1
- package/dist/{chunk-XTQYF5HU.js → chunk-U637W5MD.js} +147 -28
- package/dist/chunk-U637W5MD.js.map +1 -0
- package/dist/index.cjs +249 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -5
- package/dist/index.d.ts +60 -5
- package/dist/index.js +117 -48
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -1
- package/dist/{storage-C8V7aVum.d.cts → storage-DlxfLVqx.d.cts} +21 -1
- package/dist/{storage-C8V7aVum.d.ts → storage-DlxfLVqx.d.ts} +21 -1
- package/docs/integration-guide.html +151 -4
- package/package.json +1 -1
- package/dist/chunk-XTQYF5HU.js.map +0 -1
package/dist/internal.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/internal.ts","../src/compression/compress.ts","../src/api/client.ts","../src/errors.ts","../src/crypto/uuid.ts","../src/api/storage.ts"],"sourcesContent":["// Internal exports for use by the web and RN SDKs only.\n// Not part of the public API — do not use in application code.\nexport { uploadBatchWithSlots } from './api/storage.js';\nexport { generateUUID } from './crypto/uuid.js';\n","import type { UploadableFile, CompressedFile, CompressionAlgorithm } from '../types/index.js';\nimport type { PlatformCompressFn, ResolvedCompressionConfig } from '../config/types.js';\n\n// MIME types that benefit from gzip (text-based, not already compressed)\nconst GZIP_MIME_TYPES = new Set([\n 'text/plain', 'text/csv', 'text/markdown', 'text/x-markdown',\n 'text/xml', 'application/xml', 'text/yaml', 'text/x-yaml',\n 'application/x-yaml', 'application/rtf', 'text/rtf',\n 'application/json', 'image/svg+xml',\n]);\n\nconst IMAGE_MIME_TYPES = new Set([\n 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp', 'image/tiff',\n]);\n\n// Already-compressed formats — no gain from recompressing\nconst SKIP_MIME_TYPES = new Set([\n 'video/mp4', 'video/webm', 'video/quicktime',\n 'audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/webm', 'audio/mp4',\n 'application/zip', 'application/pdf',\n 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',\n 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',\n 'application/vnd.openxmlformats-officedocument.presentationml.presentation',\n]);\n\nexport type CompressionStrategy = 'image' | 'gzip' | 'skip';\n\nexport function getCompressionStrategy(\n mimeType: string,\n config: ResolvedCompressionConfig,\n): CompressionStrategy {\n if (SKIP_MIME_TYPES.has(mimeType)) return 'skip';\n if (IMAGE_MIME_TYPES.has(mimeType)) return 'image';\n if (config.compressDocuments && GZIP_MIME_TYPES.has(mimeType)) return 'gzip';\n return 'skip';\n}\n\n/**\n * Attempt to compress a file using the platform-provided compressor.\n * Returns the original file unchanged (as a CompressedFile with compressed=false)\n * if compression is disabled, no compressor is provided, or the strategy is 'skip'.\n */\nexport async function compressFile(\n file: UploadableFile,\n platformCompressFn: PlatformCompressFn | undefined,\n config: ResolvedCompressionConfig,\n): Promise<CompressedFile> {\n const noop: CompressedFile = {\n ...file,\n originalSize: file.size,\n compressed: false,\n compressionAlgorithm: 'none' as CompressionAlgorithm,\n };\n\n if (!config.enabled || !platformCompressFn) return noop;\n\n const strategy = getCompressionStrategy(file.type, config);\n if (strategy === 'skip') return noop;\n\n try {\n return await platformCompressFn(file, config);\n } catch {\n // Compression failure is non-fatal — fall back to original\n return noop;\n }\n}\n","import axios, {\n AxiosInstance,\n InternalAxiosRequestConfig,\n} from 'axios';\nimport type { ResolvedConfig } from '../config/types.js';\nimport type { AuthTokens } from '../types/index.js';\nimport { encryptPayload, decryptPayload, isTransitEnvelope } from '../crypto/transit.js';\nimport { getSessionKey, getSessionId, isTransitEnabled, waitForTransitReady, configureTransit, setTransitSession, getTransitSession } from '../crypto/session.js';\nimport { createRestTransitSession } from '../crypto/handshake.js';\nimport { detectTransitAlgo } from '../crypto/detect.js';\nimport { normalizeAxiosError } from '../errors.js';\n\nexport type TokenStore = {\n getAccessToken: () => string | null | undefined;\n getRefreshToken: () => string | null | undefined;\n setTokens: (tokens: AuthTokens) => void;\n clearTokens: () => void;\n};\n\nlet _tokenStore: TokenStore | null = null;\nlet _config: ResolvedConfig | null = null;\nlet _avatarSent = false;\n// In-flight transit handshake promise — shared between initApiClient and connectSocket\n// so they never fire two concurrent HTTPS handshakes for the same session.\nlet _transitHandshakePromise: Promise<void> | null = null;\n\nexport function getTransitHandshakePromise(): Promise<void> | null {\n return _transitHandshakePromise;\n}\n\nexport function initApiClient(config: ResolvedConfig, tokenStore: TokenStore): AxiosInstance {\n _config = config;\n _tokenStore = tokenStore;\n _avatarSent = false; // reset on re-init (new session / authToken change)\n\n const client = axios.create({\n baseURL: config.apiUrl,\n headers: { 'Content-Type': 'application/json' },\n });\n\n // Configure transit as early as possible — before any requests fire —\n // so waitForTransitReady() in the interceptor knows whether to block or not.\n configureTransit(config.transitEncryption);\n\n // Kick off the HTTPS transit handshake immediately so REST calls that fire\n // before connectSocket (e.g. getMe() right after initApiClient) are not\n // blocked indefinitely. Store the promise so connectSocket can await it\n // instead of firing a duplicate handshake.\n if (config.transitEncryption && !getTransitSession() && !_transitHandshakePromise) {\n _transitHandshakePromise = (async () => {\n try {\n const session = await createRestTransitSession(config.apiUrl);\n if (session && !getTransitSession()) {\n const algo = typeof globalThis.crypto?.subtle !== 'undefined'\n ? await detectTransitAlgo()\n : 'x25519';\n setTransitSession({ sessionKey: session.sessionKey as CryptoKey, algo, sessionId: session.sessionId, enabled: true });\n } else if (!session) {\n configureTransit(false);\n }\n } catch {\n configureTransit(false);\n } finally {\n _transitHandshakePromise = null;\n }\n })();\n }\n\n // ── Request interceptor ──────────────────────────────────────────────────\n client.interceptors.request.use(async (req: InternalAxiosRequestConfig) => {\n const token = _tokenStore?.getAccessToken();\n if (token) req.headers['Authorization'] = `Bearer ${token}`;\n if (_config?.userId) req.headers['x-user-id'] = _config.userId;\n if (_config?.tenantId) req.headers['X-Tenant-ID'] = _config.tenantId;\n // Send avatar on the first request only — server hashes and deduplicates\n if (token && !_avatarSent && _config?.avatar) {\n if (_config.avatar.base64) req.headers['x-avatar-base64'] = _config.avatar.base64;\n else if (_config.avatar.url) req.headers['x-avatar-url'] = _config.avatar.url;\n _avatarSent = true;\n }\n\n // Wait for the transit session key before sending authenticated requests.\n // Unauthenticated requests (no token) fire before login/socket connect —\n // they can never have a transit session so never block.\n if (token) await waitForTransitReady();\n\n if (isTransitEnabled()) {\n const sessionId = getSessionId();\n const key = getSessionKey();\n if (sessionId && key) {\n req.headers['x-transit-session'] = sessionId;\n if (req.data !== undefined && req.data !== null) {\n const envelope = await encryptPayload(req.data, key);\n req.data = envelope;\n req.headers['x-transit-encrypted'] = '1';\n }\n }\n }\n\n return req;\n });\n\n let isRefreshing = false;\n let refreshQueue: Array<(token: string) => void> = [];\n\n // ── Response interceptor ─────────────────────────────────────────────────\n client.interceptors.response.use(\n async (response) => {\n // Transit decryption — server wraps encrypted payload inside { success, data: <envelope> }.\n // Decrypt the inner envelope, then let the standard unwrap below handle { success, data }.\n if (isTransitEnabled()) {\n const key = getSessionKey();\n if (key) {\n // Case 1: entire response is the envelope (unlikely but handle it)\n if (isTransitEnvelope(response.data)) {\n response.data = await decryptPayload(response.data, key);\n }\n // Case 2: envelope is nested inside { success, data: <envelope> }\n else if (response.data?.data && isTransitEnvelope(response.data.data)) {\n response.data.data = await decryptPayload(response.data.data, key);\n }\n }\n }\n\n // Standard { success, data } unwrap\n if (\n response.data &&\n typeof response.data === 'object' &&\n 'success' in response.data &&\n 'data' in response.data\n ) {\n response.data = response.data.data;\n }\n return response;\n },\n async (error) => {\n // Decrypt error response body — error filter encrypts it too\n if (isTransitEnabled() && error.response?.data) {\n const key = getSessionKey();\n if (key) {\n try {\n if (isTransitEnvelope(error.response.data)) {\n error.response.data = await decryptPayload(error.response.data, key);\n } else if (error.response.data?.data && isTransitEnvelope(error.response.data.data)) {\n error.response.data.data = await decryptPayload(error.response.data.data, key);\n }\n } catch { /* decryption failed — leave as-is */ }\n }\n }\n\n const original = error.config as InternalAxiosRequestConfig & { _retry?: boolean };\n\n if (error.response?.status === 401 && !original._retry) {\n const refreshToken = _tokenStore?.getRefreshToken();\n if (!refreshToken) {\n _tokenStore?.clearTokens();\n return Promise.reject(normalizeAxiosError(error));\n }\n\n if (isRefreshing) {\n return new Promise((resolve) => {\n refreshQueue.push((newToken) => {\n original.headers['Authorization'] = `Bearer ${newToken}`;\n resolve(client(original));\n });\n });\n }\n\n original._retry = true;\n isRefreshing = true;\n\n try {\n const { data } = await axios.post<{ data: AuthTokens }>(\n `${_config!.apiUrl}/auth/refresh`,\n { refreshToken },\n );\n const tokens: AuthTokens = (data as any).data ?? data;\n _tokenStore?.setTokens(tokens);\n refreshQueue.forEach((cb) => cb(tokens.accessToken));\n refreshQueue = [];\n original.headers['Authorization'] = `Bearer ${tokens.accessToken}`;\n return client(original);\n } catch {\n _tokenStore?.clearTokens();\n return Promise.reject(normalizeAxiosError(error));\n } finally {\n isRefreshing = false;\n }\n }\n\n return Promise.reject(normalizeAxiosError(error));\n },\n );\n\n return client;\n}\n\nlet _instance: AxiosInstance | null = null;\n\nexport function setApiClientInstance(instance: AxiosInstance) {\n _instance = instance;\n}\n\nexport function getApiClient(): AxiosInstance {\n if (!_instance) throw new Error('[AntzChat] API client not initialized. Call initApiClient first.');\n return _instance;\n}\n","import { isAxiosError } from 'axios';\nimport { isTransitEnvelope } from './crypto/transit.js';\n\n// ─── Base error class ─────────────────────────────────────────────────────────\n\nexport class AntzChatError extends Error {\n readonly code: string;\n readonly retryable: boolean;\n readonly context?: Record<string, unknown>;\n\n constructor(\n code: string,\n message: string,\n retryable = false,\n context?: Record<string, unknown>,\n ) {\n super(message);\n this.name = 'AntzChatError';\n this.code = code;\n this.retryable = retryable;\n this.context = context;\n // Maintain proper prototype chain in transpiled environments\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n// ─── Semantic subclasses ──────────────────────────────────────────────────────\n\n/** Thrown on 401 (after refresh also fails) or when no refresh token exists. */\nexport class AntzChatAuthError extends AntzChatError {\n constructor(message: string, code = 'AUTH_FAILED', context?: Record<string, unknown>) {\n super(code, message, false, context);\n this.name = 'AntzChatAuthError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 400 / 422 — bad input, validation failure. */\nexport class AntzChatValidationError extends AntzChatError {\n /** Server-returned field error array (when the server sends message as string[]). */\n readonly fields?: string[];\n\n constructor(message: string | string[], context?: Record<string, unknown>) {\n const msg = Array.isArray(message) ? message.join('; ') : message;\n super('VALIDATION_ERROR', msg, false, context);\n this.name = 'AntzChatValidationError';\n this.fields = Array.isArray(message) ? message : undefined;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on network failures, timeouts, socket disconnections, and queue overflow. retryable = true. */\nexport class AntzChatNetworkError extends AntzChatError {\n constructor(message: string, code = 'NETWORK_ERROR', context?: Record<string, unknown>) {\n super(code, message, true, context);\n this.name = 'AntzChatNetworkError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 403 — insufficient permissions. */\nexport class AntzChatPermissionError extends AntzChatError {\n constructor(message: string, context?: Record<string, unknown>) {\n super('PERMISSION_DENIED', message, false, context);\n this.name = 'AntzChatPermissionError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 5xx or other unexpected server errors. retryable = true. */\nexport class AntzChatServerError extends AntzChatError {\n readonly httpStatus?: number;\n\n constructor(message: string, httpStatus?: number, context?: Record<string, unknown>) {\n super('SERVER_ERROR', message, true, context);\n this.name = 'AntzChatServerError';\n this.httpStatus = httpStatus;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n// ─── Error code reference ─────────────────────────────────────────────────────\n//\n// Code Class Source\n// ───────────────────── ──────────────────────── ─────────────────────────\n// AUTH_FAILED AntzChatAuthError 401 after refresh fails\n// SESSION_EXPIRED AntzChatAuthError 401, no refresh token\n// PERMISSION_DENIED AntzChatPermissionError 403\n// VALIDATION_ERROR AntzChatValidationError 400 / 422\n// NOT_FOUND AntzChatServerError 404\n// RATE_LIMITED AntzChatNetworkError 429\n// NETWORK_ERROR AntzChatNetworkError No response / conn failure\n// SOCKET_TIMEOUT AntzChatNetworkError ACK timeout / reconnect timeout\n// SOCKET_NOT_CONNECTED AntzChatNetworkError withAck when socket is down\n// SEND_QUEUE_FULL AntzChatNetworkError Queue overflow (>100 msgs)\n// MESSAGE_DROPPED AntzChatNetworkError Queue TTL expired (30s)\n// TRANSIT_MISMATCH AntzChatError SDK/server encryption config mismatch\n// SERVER_ERROR AntzChatServerError 5xx or unknown HTTP error\n\n// ─── REST error normaliser ────────────────────────────────────────────────────\n\n/**\n * Converts a raw axios error (or any unknown throw) into a typed AntzChatError.\n *\n * Call site: client.ts response interceptor — runs AFTER transit decryption,\n * so error.response.data is always plaintext by the time this function sees it.\n * If decryption itself failed, error.response.data remains the raw encrypted\n * envelope — detected via isTransitEnvelope() and noted in context.\n */\nexport function normalizeAxiosError(error: unknown): AntzChatError {\n if (error instanceof AntzChatError) return error;\n\n if (isAxiosError(error)) {\n const status = error.response?.status;\n const body = error.response?.data;\n\n // Detect if decryption failed — body is still an encrypted envelope\n const decryptionFailed = body != null && isTransitEnvelope(body);\n\n const rawMessage: string | string[] | undefined = decryptionFailed\n ? undefined\n : (body?.message ?? undefined);\n\n const message: string =\n (Array.isArray(rawMessage) ? rawMessage.join('; ') : rawMessage) ||\n error.message ||\n 'Request failed';\n\n const ctx: Record<string, unknown> = {\n ...(status != null && { httpStatus: status }),\n ...(body?.path != null && { path: body.path }),\n ...(body?.error != null && { serverError: body.error }),\n ...(error.code != null && { axiosCode: error.code }),\n ...(decryptionFailed && { decryptionFailed: true, note: 'Transit decryption failed — server error body is an encrypted envelope' }),\n };\n\n // No response at all — network/timeout failure\n if (!error.response) {\n return new AntzChatNetworkError(message || 'Network error', 'NETWORK_ERROR', ctx);\n }\n\n if (status === 401) {\n // AUTH_FAILED is used when a refresh was attempted but failed (set by interceptor).\n // SESSION_EXPIRED is the default: 401 with no prior retry = token simply expired.\n const code = (error.config as any)?._retry ? 'AUTH_FAILED' : 'SESSION_EXPIRED';\n return new AntzChatAuthError(message, code, ctx);\n }\n if (status === 403) return new AntzChatPermissionError(message, ctx);\n if (status === 400 || status === 422) {\n return new AntzChatValidationError(\n Array.isArray(rawMessage) ? rawMessage : message,\n ctx,\n );\n }\n if (status === 404) return new AntzChatServerError(message, 404, ctx);\n if (status === 429) return new AntzChatNetworkError(message, 'RATE_LIMITED', ctx);\n if (status != null && status >= 500) return new AntzChatServerError(message, status, ctx);\n\n return new AntzChatServerError(message, status, ctx);\n }\n\n const msg = error instanceof Error ? error.message : String(error);\n return new AntzChatError('UNKNOWN_ERROR', msg, false);\n}\n","// crypto.randomUUID() doesn't exist in React Native's Hermes engine.\n// Fall back to a RFC 4122 v4 UUID built from Math.random() when unavailable.\nexport function generateUUID(): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return globalThis.crypto.randomUUID();\n }\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);\n });\n}\n","import type {\n BatchUploadResult,\n FileResponse,\n PaginatedResponse,\n PresignedUrlRequest,\n PresignedUrlResponse,\n FileType,\n UploadableFile,\n CompletedPart,\n} from '../types/index.js';\nimport type { PlatformUploadFn, PlatformCompressFn, PlatformUploadPartFn, ResolvedCompressionConfig } from '../config/types.js';\nimport { compressFile } from '../compression/compress.js';\nimport { getApiClient } from './client.js';\nimport { generateUUID } from '../crypto/uuid.js';\n\nexport const storageApi = {\n async requestPresignedUrl(payload: PresignedUrlRequest): Promise<PresignedUrlResponse> {\n const { data } = await getApiClient().post<PresignedUrlResponse>('/storage/presigned-url', payload);\n return data;\n },\n\n async requestPresignedUrlBatch(files: PresignedUrlRequest[]): Promise<{\n urls: PresignedUrlResponse[];\n errors: Array<{ filename: string; error: string; clientIndex?: number }>;\n }> {\n const { data } = await getApiClient().post('/storage/presigned-url/batch', { files });\n return data;\n },\n\n async confirmUpload(fileId: string): Promise<FileResponse> {\n const { data } = await getApiClient().post<FileResponse>(`/storage/confirm/${fileId}`);\n return data;\n },\n\n async getFile(fileId: string): Promise<FileResponse> {\n const { data } = await getApiClient().get<FileResponse>(`/storage/files/${fileId}`);\n return data;\n },\n\n async getFileUrl(fileId: string, expiresIn?: number): Promise<{ url: string; expiresAt: string }> {\n const { data } = await getApiClient().get(`/storage/files/${fileId}/url`, {\n params: expiresIn ? { expiresIn } : {},\n });\n return data;\n },\n\n async deleteFile(fileId: string): Promise<void> {\n await getApiClient().post(`/storage/files/${fileId}/delete`);\n },\n\n async completeMultipartUpload(\n fileId: string,\n uploadId: string,\n parts: CompletedPart[],\n ): Promise<FileResponse> {\n const { data } = await getApiClient().post<FileResponse>(\n `/storage/multipart/complete/${fileId}`,\n { uploadId, parts },\n );\n return data;\n },\n\n async getConversationFiles(\n conversationId: string,\n params: { page?: number; limit?: number; type?: FileType } = {},\n ): Promise<PaginatedResponse<FileResponse>> {\n const { data } = await getApiClient().get(\n `/storage/conversations/${conversationId}/files`,\n { params },\n );\n return data;\n },\n\n async getMyFiles(params: { page?: number; limit?: number } = {}): Promise<PaginatedResponse<FileResponse>> {\n const { data } = await getApiClient().get('/storage/my-files', { params });\n return data;\n },\n};\n\nasync function runMultipartUpload(\n presigned: PresignedUrlResponse,\n file: UploadableFile,\n platformUploadPartFn: PlatformUploadPartFn,\n onProgress?: (pct: number) => void,\n): Promise<FileResponse> {\n const { multipart } = presigned;\n if (!multipart) throw new Error('No multipart info on presigned response');\n\n const CONCURRENCY = 3;\n const completedParts: CompletedPart[] = [];\n const partProgress: Record<number, number> = {};\n\n multipart.partUrls.forEach(({ partNumber }) => { partProgress[partNumber] = 0; });\n\n const reportProgress = () => {\n if (!onProgress) return;\n const vals = Object.values(partProgress);\n const avg = vals.reduce((s, v) => s + v, 0) / Math.max(vals.length, 1);\n onProgress(Math.round(avg * 0.95));\n };\n\n const uploadPart = async (partNumber: number, uploadUrl: string, method: 'PUT' | 'POST'): Promise<void> => {\n const offset = (partNumber - 1) * multipart.chunkSize;\n const end = Math.min(offset + multipart.chunkSize, file.size);\n const blob = await fetch(file.uri).then((r) => r.blob());\n const slice = blob.slice(offset, end);\n\n const etag = await platformUploadPartFn(uploadUrl, slice, (pct) => {\n partProgress[partNumber] = pct;\n reportProgress();\n }, method);\n\n completedParts.push({ partNumber, etag });\n partProgress[partNumber] = 100;\n reportProgress();\n };\n\n for (let i = 0; i < multipart.partUrls.length; i += CONCURRENCY) {\n const batch = multipart.partUrls.slice(i, i + CONCURRENCY);\n const results = await Promise.allSettled(\n batch.map(({ partNumber, uploadUrl, method }) => uploadPart(partNumber, uploadUrl, method ?? 'PUT')),\n );\n const failed = results.find((r) => r.status === 'rejected') as PromiseRejectedResult | undefined;\n if (failed) throw failed.reason;\n }\n\n completedParts.sort((a, b) => a.partNumber - b.partNumber);\n\n const fileResponse = await storageApi.completeMultipartUpload(\n presigned.fileId,\n multipart.uploadId,\n completedParts,\n );\n onProgress?.(100);\n return fileResponse;\n}\n\n/**\n * Core upload implementation. Returns the public BatchUploadResult plus a\n * slotId → FileResponse map that useChat hooks use internally to match\n * confirmed uploads back to optimistic UI slots by position rather than\n * filename. The slotToFile map is never part of the public API.\n */\nasync function runUploadBatch(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n slotIds: string[],\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<{ result: BatchUploadResult; slotToFile: Map<string, FileResponse> }> {\n // Compress all files first (no-ops for unsupported types or when disabled)\n const compressedFiles = await Promise.all(\n files.map((f) => compressFile(f, platformCompressFn, compressionConfig ?? { enabled: false, imageQuality: 0.85, imageMaxDimension: 1920, compressDocuments: true })),\n );\n\n // Pair each compressed file with its slot ID and a clientIndex.\n // clientIndex is sent to the server and echoed back in both urls and errors,\n // giving us a reliable position mapping regardless of which files fail.\n const slotted = compressedFiles.map((f, i) => ({ file: f, slotId: slotIds[i], clientIndex: i }));\n\n const requests: PresignedUrlRequest[] = slotted.map(({ file: f, clientIndex }) => ({\n filename: f.name,\n mimeType: f.type,\n size: f.size,\n conversationId,\n clientIndex,\n ...(f.compressed && {\n metadata: {\n compressed: f.compressed,\n originalSize: f.originalSize,\n compressionAlgorithm: f.compressionAlgorithm,\n },\n }),\n }));\n\n const { urls, errors: requestErrors } = await storageApi.requestPresignedUrlBatch(requests);\n\n // Use the echoed clientIndex to identify which original slots failed.\n // This is reliable even for same-named files and any failure pattern.\n const failedSlotIds = new Set<string>();\n const failed: Array<{ filename: string; error: string }> = requestErrors.map((e) => {\n const idx = e.clientIndex ?? slotted.findIndex((s) => s.file.name === e.filename);\n const slotId = slotted[idx]?.slotId;\n if (slotId) failedSlotIds.add(slotId);\n return { filename: e.filename, error: e.error };\n });\n\n // Map each presigned URL back to its original slot via clientIndex.\n const progressMap: Record<number, number> = {};\n const reportProgress = () => {\n if (!onProgress) return;\n const vals = Object.values(progressMap);\n const avg = vals.reduce((s, v) => s + v, 0) / Math.max(vals.length, 1);\n onProgress(Math.round(avg));\n };\n\n const successful: FileResponse[] = [];\n const slotToFile = new Map<string, FileResponse>();\n\n await Promise.all(\n urls.map(async (presigned, idx) => {\n // Resolve the original slot via echoed clientIndex; fall back to position\n // in urls[] only if the server didn't echo it (older server version).\n const originalIdx = presigned.clientIndex ?? idx;\n const { file, slotId } = slotted[originalIdx];\n progressMap[originalIdx] = 0;\n try {\n let fileResponse: FileResponse;\n if (presigned.multipart && platformUploadPartFn) {\n fileResponse = await runMultipartUpload(presigned, file, platformUploadPartFn, (pct) => {\n progressMap[originalIdx] = pct;\n reportProgress();\n });\n } else {\n await platformUploadFn(presigned, file, (pct) => {\n progressMap[originalIdx] = Math.round(pct * 0.9);\n reportProgress();\n });\n fileResponse = await storageApi.confirmUpload(presigned.fileId);\n }\n progressMap[originalIdx] = 100;\n reportProgress();\n successful.push(fileResponse);\n slotToFile.set(slotId, fileResponse);\n } catch (err) {\n failed.push({ filename: file.name, error: (err as Error).message });\n }\n }),\n );\n\n return { result: { successful, failed }, slotToFile };\n}\n\n/** Public API — returns standard BatchUploadResult, slot tracking is internal. */\nexport async function uploadBatch(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<BatchUploadResult> {\n const slotIds = files.map(() => generateUUID());\n const { result } = await runUploadBatch(files, platformUploadFn, slotIds, conversationId, onProgress, platformCompressFn, compressionConfig, platformUploadPartFn);\n return result;\n}\n\n/**\n * Used only by useChat hooks (web + RN) to get the slotId → FileResponse map\n * for matching confirmed uploads back to optimistic UI slots.\n * Not exported from the package index — internal SDK use only.\n */\nexport async function uploadBatchWithSlots(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n slotIds: string[],\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<{ result: BatchUploadResult; slotToFile: Map<string, FileResponse> }> {\n return runUploadBatch(files, platformUploadFn, slotIds, conversationId, onProgress, platformCompressFn, compressionConfig, platformUploadPartFn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIA,IAAM,kBAAkB,oBAAI,IAAI;AAAA,EAC9B;AAAA,EAAc;AAAA,EAAY;AAAA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iBAAiB,WAAW,MAAM,CAAC,QAAQ;AAC/C,wBAAY,WAAW,IAAI,KAAK,MAAM,MAAM,GAAG;AAC/C,2BAAe;AAAA,UACjB,CAAC;AACD,yBAAe,MAAM,WAAW,cAAc,UAAU,MAAM;AAAA,QAChE;AACA,oBAAY,WAAW,IAAI;AAC3B,uBAAe;AACf,mBAAW,KAAK,YAAY;AAC5B,mBAAW,IAAI,QAAQ,YAAY;AAAA,MACrC,SAAS,KAAK;AACZ,eAAO,KAAK,EAAE,UAAU,KAAK,MAAM,OAAQ,IAAc,QAAQ,CAAC;AAAA,MACpE;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,QAAQ,EAAE,YAAY,OAAO,GAAG,WAAW;AACtD;AAsBA,eAAsB,qBACpB,OACA,kBACA,SACA,gBACA,YACA,oBACA,mBACA,sBAC+E;AAC/E,SAAO,eAAe,OAAO,kBAAkB,SAAS,gBAAgB,YAAY,oBAAoB,mBAAmB,oBAAoB;AACjJ;","names":["import_axios"]}
|
|
1
|
+
{"version":3,"sources":["../src/internal.ts","../src/compression/compress.ts","../src/api/client.ts","../src/errors.ts","../src/crypto/uuid.ts","../src/api/storage.ts"],"sourcesContent":["// Internal exports for use by the web and RN SDKs only.\n// Not part of the public API — do not use in application code.\nexport { uploadBatchWithSlots } from './api/storage.js';\nexport { generateUUID } from './crypto/uuid.js';\n","import type { UploadableFile, CompressedFile, CompressionAlgorithm } from '../types/index.js';\nimport type { PlatformCompressFn, ResolvedCompressionConfig } from '../config/types.js';\n\n// MIME types that benefit from gzip (text-based, not already compressed)\nconst GZIP_MIME_TYPES = new Set([\n 'text/plain', 'text/csv', 'text/markdown', 'text/x-markdown',\n 'text/xml', 'application/xml', 'text/yaml', 'text/x-yaml',\n 'application/x-yaml', 'application/rtf', 'text/rtf',\n 'application/json', 'image/svg+xml',\n]);\n\nconst IMAGE_MIME_TYPES = new Set([\n 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp', 'image/tiff',\n]);\n\n// Already-compressed formats — no gain from recompressing\nconst SKIP_MIME_TYPES = new Set([\n 'video/mp4', 'video/webm', 'video/quicktime',\n 'audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/webm', 'audio/mp4',\n 'application/zip', 'application/pdf',\n 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',\n 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',\n 'application/vnd.openxmlformats-officedocument.presentationml.presentation',\n]);\n\nexport type CompressionStrategy = 'image' | 'gzip' | 'skip';\n\nexport function getCompressionStrategy(\n mimeType: string,\n config: ResolvedCompressionConfig,\n): CompressionStrategy {\n if (SKIP_MIME_TYPES.has(mimeType)) return 'skip';\n if (IMAGE_MIME_TYPES.has(mimeType)) return 'image';\n if (config.compressDocuments && GZIP_MIME_TYPES.has(mimeType)) return 'gzip';\n return 'skip';\n}\n\n/**\n * Attempt to compress a file using the platform-provided compressor.\n * Returns the original file unchanged (as a CompressedFile with compressed=false)\n * if compression is disabled, no compressor is provided, or the strategy is 'skip'.\n */\nexport async function compressFile(\n file: UploadableFile,\n platformCompressFn: PlatformCompressFn | undefined,\n config: ResolvedCompressionConfig,\n): Promise<CompressedFile> {\n const noop: CompressedFile = {\n ...file,\n originalSize: file.size,\n compressed: false,\n compressionAlgorithm: 'none' as CompressionAlgorithm,\n };\n\n if (!config.enabled || !platformCompressFn) return noop;\n\n const strategy = getCompressionStrategy(file.type, config);\n if (strategy === 'skip') return noop;\n\n try {\n return await platformCompressFn(file, config);\n } catch {\n // Compression failure is non-fatal — fall back to original\n return noop;\n }\n}\n","import axios, {\n AxiosInstance,\n InternalAxiosRequestConfig,\n} from 'axios';\nimport type { ResolvedConfig } from '../config/types.js';\nimport type { AuthTokens } from '../types/index.js';\nimport { encryptPayload, decryptPayload, isTransitEnvelope } from '../crypto/transit.js';\nimport { getSessionKey, getSessionId, isTransitEnabled, awaitTransitReadyOr, configureTransit, setTransitSession, getTransitSession } from '../crypto/session.js';\nimport { createRestTransitSession, fetchServerKeys, TransitRateLimitedError } from '../crypto/handshake.js';\nimport { detectTransitAlgo } from '../crypto/detect.js';\nimport { normalizeAxiosError, AntzChatNetworkError } from '../errors.js';\n\n// Hard ceiling on how long a single request will block waiting for the transit\n// handshake. A legitimate cold-start handshake resolves in well under this even\n// on a slow link (TransitGate already spent ~6s, establishTransit keeps\n// retrying). Past this we FAIL the request with a retryable error rather than\n// leave it pending forever — react-query cannot retry / refetch-on-focus a\n// request that never settles, so an unbounded wait here is an unrecoverable\n// silent hang. Each failed+retried request also re-arms ensureRestTransitHandshake.\nconst TRANSIT_GATE_MAX_WAIT_MS = 30_000;\n\nexport type TokenStore = {\n getAccessToken: () => string | null | undefined;\n getRefreshToken: () => string | null | undefined;\n setTokens: (tokens: AuthTokens) => void;\n clearTokens: () => void;\n};\n\nlet _tokenStore: TokenStore | null = null;\nlet _config: ResolvedConfig | null = null;\nlet _avatarSent = false;\n// In-flight transit handshake promise — shared between initApiClient and connectSocket\n// so they never fire two concurrent HTTPS handshakes for the same session.\nlet _transitHandshakePromise: Promise<void> | null = null;\n// Gate the request interceptor until the SDK has resolved a token (async\n// authProvider) and, where wired, the transit handshake. Set by the SDK\n// provider; the interceptor awaits it before attaching the Authorization\n// header so early requests (e.g. useConversations' initial fetch) don't race\n// ahead unauthenticated.\nlet _authReadyPromise: Promise<unknown> | null = null;\n\nexport function getTransitHandshakePromise(): Promise<void> | null {\n return _transitHandshakePromise;\n}\n\nexport function setAuthReadyPromise(promise: Promise<unknown> | null): void {\n _authReadyPromise = promise;\n}\n\n// True once initApiClient() has run and its config has not been torn down by a\n// subsequent disconnectSocket(). Lets the SDK provider detect the case where a\n// React remount skipped re-init (its key was unchanged) but disconnectSocket()\n// had nulled _config in between — leaving the request interceptor unable to see\n// transitEncryption and firing every request as unencrypted plaintext.\nexport function isApiClientConfigured(): boolean {\n return _config !== null;\n}\n\n// (Re-)kick the HTTPS transit handshake. Idempotent: no-ops when transit is\n// disabled, a session already exists, or an attempt is already in flight.\n// Called both at init and from the request interceptor when a request is about\n// to block on waitForTransitReady() with no session — e.g. after a socket\n// disconnect cleared the session and nothing else re-established it.\n//\n// It only calls configureTransit(false) — which un-gates the interceptor and\n// lets requests go out as PLAINTEXT — when the server itself reports transit\n// disabled (GET /crypto/pubkey → enabled:false, i.e. an old server). A transient\n// failure of POST /crypto/session (network blip, rate limit, 5xx) must NOT\n// disable transit: the server still requires it, so plaintext would just 403.\n// Instead we retry with backoff; waitForTransitReady() keeps requests pending\n// and they dispatch the moment a retry sets the session.\nexport function ensureRestTransitHandshake(): void {\n if (!_config?.transitEncryption || getTransitSession() || _transitHandshakePromise) return;\n const apiUrl = _config.apiUrl;\n _transitHandshakePromise = (async () => {\n try {\n // A 429 is \"wait\", not \"broken\", so it must NOT consume the attempt\n // budget — otherwise a rate-limited client exhausts 5 attempts in a few\n // seconds and gives up on an endpoint that was working fine. Failures are\n // counted separately from rate-limit hits, and the loop is additionally\n // bounded by wall-clock time so a persistently limited server cannot keep\n // it running forever.\n const MAX_FAILURES = 5;\n const BACKSTOP_MS = 2 * 60_000;\n const deadline = Date.now() + BACKSTOP_MS;\n let failures = 0;\n let rateLimitHits = 0;\n\n while (failures < MAX_FAILURES && Date.now() < deadline) {\n if (getTransitSession()) return;\n let waitMs: number;\n try {\n // Identity is sent so the server can rate-limit these pre-auth routes\n // per user rather than per IP (see TransitIdentity in handshake.ts).\n const identity = { userId: _config?.userId, tenantId: _config?.tenantId };\n const keys = await fetchServerKeys(apiUrl, identity);\n if (!keys?.enabled) {\n configureTransit(false); // server genuinely doesn't want transit\n return;\n }\n const session = await createRestTransitSession(apiUrl, identity);\n if (session && !getTransitSession()) {\n const algo = typeof globalThis.crypto?.subtle !== 'undefined'\n ? await detectTransitAlgo()\n : 'x25519';\n setTransitSession({ sessionKey: session.sessionKey as CryptoKey, algo, sessionId: session.sessionId, enabled: true });\n return;\n }\n // Reachable when the server returned no session but did not throw\n // (e.g. an old server without the endpoint) — treat as a failure.\n failures++;\n waitMs = Math.min(500 * 2 ** failures, 8_000);\n } catch (err) {\n if (err instanceof TransitRateLimitedError) {\n // Honour the server's own figure when it sent one (clamped to a\n // sane 1-60s), else escalate blind since the window is unknown.\n const blind = Math.min(15_000 * 2 ** rateLimitHits, 60_000);\n waitMs = err.retryAfterMs != null\n ? Math.min(Math.max(err.retryAfterMs, 1_000), 60_000)\n : blind;\n rateLimitHits++;\n console.warn(\n `[AntzChat] transit handshake rate-limited (429) — retrying in ${Math.round(waitMs / 1000)}s` +\n `${err.retryAfterMs != null ? ' (per Retry-After)' : ''}.`,\n );\n } else {\n failures++;\n waitMs = Math.min(500 * 2 ** failures, 8_000);\n }\n }\n await new Promise((r) => setTimeout(r, waitMs));\n }\n console.error(\n '[AntzChat] transit handshake could not establish a session — ' +\n \"chat requests stay gated until one succeeds (server requires transit).\",\n );\n } finally {\n _transitHandshakePromise = null;\n }\n })();\n}\n\nexport function initApiClient(config: ResolvedConfig, tokenStore: TokenStore): AxiosInstance {\n _config = config;\n _tokenStore = tokenStore;\n _avatarSent = false; // reset on re-init (new session / authToken change)\n\n const client = axios.create({\n baseURL: config.apiUrl,\n headers: { 'Content-Type': 'application/json' },\n });\n\n // Configure transit as early as possible — before any requests fire —\n // so waitForTransitReady() in the interceptor knows whether to block or not.\n configureTransit(config.transitEncryption);\n\n // Kick off the HTTPS transit handshake immediately so REST calls that fire\n // before connectSocket (e.g. getMe() right after initApiClient) are not\n // blocked indefinitely. Store the promise so connectSocket can await it\n // instead of firing a duplicate handshake.\n ensureRestTransitHandshake();\n\n // ── Request interceptor ──────────────────────────────────────────────────\n client.interceptors.request.use(async (req: InternalAxiosRequestConfig) => {\n // Wait for the SDK's auth (and, where wired, transit) gate before reading\n // the token — otherwise a request fired during boot goes out with no\n // Authorization header.\n if (_authReadyPromise) await _authReadyPromise;\n\n const token = _tokenStore?.getAccessToken();\n if (token) req.headers['Authorization'] = `Bearer ${token}`;\n if (_config?.userId) req.headers['x-user-id'] = _config.userId;\n if (_config?.tenantId) req.headers['X-Tenant-ID'] = _config.tenantId;\n // Send avatar on the first request only — server hashes and deduplicates\n if (token && !_avatarSent && _config?.avatar) {\n if (_config.avatar.base64) req.headers['x-avatar-base64'] = _config.avatar.base64;\n else if (_config.avatar.url) req.headers['x-avatar-url'] = _config.avatar.url;\n _avatarSent = true;\n }\n\n // Wait for the transit session key before sending any request. The server\n // enforces transit encryption independent of auth (e.g. GET /app/config\n // fires before the async authProvider token resolves) — gating this on\n // `token` let those pre-auth requests race ahead of the handshake and get\n // rejected with 403 \"Transit encryption required\".\n if (_config?.transitEncryption) {\n // If the session is gone (socket disconnect cleared it, first boot still\n // pending), make sure a handshake is running before we block — otherwise\n // the wait could hang with nothing to resolve it.\n if (!getTransitSession()) ensureRestTransitHandshake();\n const ready = await awaitTransitReadyOr(TRANSIT_GATE_MAX_WAIT_MS);\n if (!ready) {\n // Handshake still hasn't produced a session. Do NOT send plaintext\n // (server requires transit); fail loudly instead so the error surfaces\n // in the UI and react-query's retry re-drives the handshake.\n throw new AntzChatNetworkError(\n 'Secure channel to chat server not established — request not sent. It will retry automatically.',\n 'TRANSIT_NOT_READY',\n { url: req.url },\n );\n }\n }\n\n if (isTransitEnabled()) {\n const sessionId = getSessionId();\n const key = getSessionKey();\n if (sessionId && key) {\n req.headers['x-transit-session'] = sessionId;\n if (req.data !== undefined && req.data !== null) {\n const envelope = await encryptPayload(req.data, key);\n req.data = envelope;\n req.headers['x-transit-encrypted'] = '1';\n }\n }\n }\n\n return req;\n });\n\n let isRefreshing = false;\n let refreshQueue: Array<(token: string) => void> = [];\n\n // ── Response interceptor ─────────────────────────────────────────────────\n client.interceptors.response.use(\n async (response) => {\n // Transit decryption — server wraps encrypted payload inside { success, data: <envelope> }.\n // Decrypt the inner envelope, then let the standard unwrap below handle { success, data }.\n if (isTransitEnabled()) {\n const key = getSessionKey();\n if (key) {\n // Case 1: entire response is the envelope (unlikely but handle it)\n if (isTransitEnvelope(response.data)) {\n response.data = await decryptPayload(response.data, key);\n }\n // Case 2: envelope is nested inside { success, data: <envelope> }\n else if (response.data?.data && isTransitEnvelope(response.data.data)) {\n response.data.data = await decryptPayload(response.data.data, key);\n }\n }\n }\n\n // Standard { success, data } unwrap\n if (\n response.data &&\n typeof response.data === 'object' &&\n 'success' in response.data &&\n 'data' in response.data\n ) {\n response.data = response.data.data;\n }\n return response;\n },\n async (error) => {\n // Decrypt error response body — error filter encrypts it too\n if (isTransitEnabled() && error.response?.data) {\n const key = getSessionKey();\n if (key) {\n try {\n if (isTransitEnvelope(error.response.data)) {\n error.response.data = await decryptPayload(error.response.data, key);\n } else if (error.response.data?.data && isTransitEnvelope(error.response.data.data)) {\n error.response.data.data = await decryptPayload(error.response.data.data, key);\n }\n } catch { /* decryption failed — leave as-is */ }\n }\n }\n\n const original = error.config as InternalAxiosRequestConfig & { _retry?: boolean };\n\n if (error.response?.status === 401 && !original._retry) {\n const refreshToken = _tokenStore?.getRefreshToken();\n if (!refreshToken) {\n _tokenStore?.clearTokens();\n return Promise.reject(normalizeAxiosError(error));\n }\n\n if (isRefreshing) {\n return new Promise((resolve) => {\n refreshQueue.push((newToken) => {\n original.headers['Authorization'] = `Bearer ${newToken}`;\n resolve(client(original));\n });\n });\n }\n\n original._retry = true;\n isRefreshing = true;\n\n try {\n const { data } = await axios.post<{ data: AuthTokens }>(\n `${_config!.apiUrl}/auth/refresh`,\n { refreshToken },\n );\n const tokens: AuthTokens = (data as any).data ?? data;\n _tokenStore?.setTokens(tokens);\n refreshQueue.forEach((cb) => cb(tokens.accessToken));\n refreshQueue = [];\n original.headers['Authorization'] = `Bearer ${tokens.accessToken}`;\n return client(original);\n } catch {\n _tokenStore?.clearTokens();\n return Promise.reject(normalizeAxiosError(error));\n } finally {\n isRefreshing = false;\n }\n }\n\n return Promise.reject(normalizeAxiosError(error));\n },\n );\n\n return client;\n}\n\nlet _instance: AxiosInstance | null = null;\n\nexport function setApiClientInstance(instance: AxiosInstance) {\n _instance = instance;\n}\n\nexport function getApiClient(): AxiosInstance {\n if (!_instance) throw new Error('[AntzChat] API client not initialized. Call initApiClient first.');\n return _instance;\n}\n","import { isAxiosError } from 'axios';\nimport { isTransitEnvelope } from './crypto/transit.js';\n\n// ─── Base error class ─────────────────────────────────────────────────────────\n\nexport class AntzChatError extends Error {\n readonly code: string;\n readonly retryable: boolean;\n readonly context?: Record<string, unknown>;\n\n constructor(\n code: string,\n message: string,\n retryable = false,\n context?: Record<string, unknown>,\n ) {\n super(message);\n this.name = 'AntzChatError';\n this.code = code;\n this.retryable = retryable;\n this.context = context;\n // Maintain proper prototype chain in transpiled environments\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n// ─── Semantic subclasses ──────────────────────────────────────────────────────\n\n/** Thrown on 401 (after refresh also fails) or when no refresh token exists. */\nexport class AntzChatAuthError extends AntzChatError {\n constructor(message: string, code = 'AUTH_FAILED', context?: Record<string, unknown>) {\n super(code, message, false, context);\n this.name = 'AntzChatAuthError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 400 / 422 — bad input, validation failure. */\nexport class AntzChatValidationError extends AntzChatError {\n /** Server-returned field error array (when the server sends message as string[]). */\n readonly fields?: string[];\n\n constructor(message: string | string[], context?: Record<string, unknown>) {\n const msg = Array.isArray(message) ? message.join('; ') : message;\n super('VALIDATION_ERROR', msg, false, context);\n this.name = 'AntzChatValidationError';\n this.fields = Array.isArray(message) ? message : undefined;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on network failures, timeouts, socket disconnections, and queue overflow. retryable = true. */\nexport class AntzChatNetworkError extends AntzChatError {\n constructor(message: string, code = 'NETWORK_ERROR', context?: Record<string, unknown>) {\n super(code, message, true, context);\n this.name = 'AntzChatNetworkError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 403 — insufficient permissions. */\nexport class AntzChatPermissionError extends AntzChatError {\n constructor(message: string, context?: Record<string, unknown>) {\n super('PERMISSION_DENIED', message, false, context);\n this.name = 'AntzChatPermissionError';\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown on 5xx or other unexpected server errors. retryable = true. */\nexport class AntzChatServerError extends AntzChatError {\n readonly httpStatus?: number;\n\n constructor(message: string, httpStatus?: number, context?: Record<string, unknown>) {\n super('SERVER_ERROR', message, true, context);\n this.name = 'AntzChatServerError';\n this.httpStatus = httpStatus;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n// ─── Error code reference ─────────────────────────────────────────────────────\n//\n// Code Class Source\n// ───────────────────── ──────────────────────── ─────────────────────────\n// AUTH_FAILED AntzChatAuthError 401 after refresh fails\n// SESSION_EXPIRED AntzChatAuthError 401, no refresh token\n// PERMISSION_DENIED AntzChatPermissionError 403\n// VALIDATION_ERROR AntzChatValidationError 400 / 422\n// NOT_FOUND AntzChatServerError 404\n// RATE_LIMITED AntzChatNetworkError 429\n// NETWORK_ERROR AntzChatNetworkError No response / conn failure\n// SOCKET_TIMEOUT AntzChatNetworkError ACK timeout / reconnect timeout\n// SOCKET_NOT_CONNECTED AntzChatNetworkError withAck when socket is down\n// SEND_QUEUE_FULL AntzChatNetworkError Queue overflow (>100 msgs)\n// MESSAGE_DROPPED AntzChatNetworkError Queue TTL expired (30s)\n// TRANSIT_MISMATCH AntzChatError SDK/server encryption config mismatch\n// SERVER_ERROR AntzChatServerError 5xx or unknown HTTP error\n\n// ─── REST error normaliser ────────────────────────────────────────────────────\n\n/**\n * Converts a raw axios error (or any unknown throw) into a typed AntzChatError.\n *\n * Call site: client.ts response interceptor — runs AFTER transit decryption,\n * so error.response.data is always plaintext by the time this function sees it.\n * If decryption itself failed, error.response.data remains the raw encrypted\n * envelope — detected via isTransitEnvelope() and noted in context.\n */\nexport function normalizeAxiosError(error: unknown): AntzChatError {\n if (error instanceof AntzChatError) return error;\n\n if (isAxiosError(error)) {\n const status = error.response?.status;\n const body = error.response?.data;\n\n // Detect if decryption failed — body is still an encrypted envelope\n const decryptionFailed = body != null && isTransitEnvelope(body);\n\n const rawMessage: string | string[] | undefined = decryptionFailed\n ? undefined\n : (body?.message ?? undefined);\n\n const message: string =\n (Array.isArray(rawMessage) ? rawMessage.join('; ') : rawMessage) ||\n error.message ||\n 'Request failed';\n\n const ctx: Record<string, unknown> = {\n ...(status != null && { httpStatus: status }),\n ...(body?.path != null && { path: body.path }),\n ...(body?.error != null && { serverError: body.error }),\n ...(error.code != null && { axiosCode: error.code }),\n ...(decryptionFailed && { decryptionFailed: true, note: 'Transit decryption failed — server error body is an encrypted envelope' }),\n };\n\n // No response at all — network/timeout failure\n if (!error.response) {\n return new AntzChatNetworkError(message || 'Network error', 'NETWORK_ERROR', ctx);\n }\n\n if (status === 401) {\n // AUTH_FAILED is used when a refresh was attempted but failed (set by interceptor).\n // SESSION_EXPIRED is the default: 401 with no prior retry = token simply expired.\n const code = (error.config as any)?._retry ? 'AUTH_FAILED' : 'SESSION_EXPIRED';\n return new AntzChatAuthError(message, code, ctx);\n }\n if (status === 403) return new AntzChatPermissionError(message, ctx);\n if (status === 400 || status === 422) {\n return new AntzChatValidationError(\n Array.isArray(rawMessage) ? rawMessage : message,\n ctx,\n );\n }\n if (status === 404) return new AntzChatServerError(message, 404, ctx);\n if (status === 429) return new AntzChatNetworkError(message, 'RATE_LIMITED', ctx);\n if (status != null && status >= 500) return new AntzChatServerError(message, status, ctx);\n\n return new AntzChatServerError(message, status, ctx);\n }\n\n const msg = error instanceof Error ? error.message : String(error);\n return new AntzChatError('UNKNOWN_ERROR', msg, false);\n}\n","// crypto.randomUUID() doesn't exist in React Native's Hermes engine.\n// Fall back to a RFC 4122 v4 UUID built from Math.random() when unavailable.\nexport function generateUUID(): string {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return globalThis.crypto.randomUUID();\n }\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);\n });\n}\n","import type {\n BatchUploadResult,\n FileResponse,\n PaginatedResponse,\n PresignedUrlRequest,\n PresignedUrlResponse,\n FileType,\n UploadableFile,\n CompletedPart,\n} from '../types/index.js';\nimport type { PlatformUploadFn, PlatformCompressFn, PlatformUploadPartFn, ResolvedCompressionConfig } from '../config/types.js';\nimport { compressFile } from '../compression/compress.js';\nimport { getApiClient } from './client.js';\nimport { generateUUID } from '../crypto/uuid.js';\n\nexport const storageApi = {\n async requestPresignedUrl(payload: PresignedUrlRequest): Promise<PresignedUrlResponse> {\n const { data } = await getApiClient().post<PresignedUrlResponse>('/storage/presigned-url', payload);\n return data;\n },\n\n async requestPresignedUrlBatch(files: PresignedUrlRequest[]): Promise<{\n urls: PresignedUrlResponse[];\n errors: Array<{ filename: string; error: string; clientIndex?: number }>;\n }> {\n const { data } = await getApiClient().post('/storage/presigned-url/batch', { files });\n return data;\n },\n\n async confirmUpload(fileId: string): Promise<FileResponse> {\n const { data } = await getApiClient().post<FileResponse>(`/storage/confirm/${fileId}`);\n return data;\n },\n\n async getFile(fileId: string): Promise<FileResponse> {\n const { data } = await getApiClient().get<FileResponse>(`/storage/files/${fileId}`);\n return data;\n },\n\n async getFileUrl(fileId: string, expiresIn?: number): Promise<{ url: string; expiresAt: string }> {\n const { data } = await getApiClient().get(`/storage/files/${fileId}/url`, {\n params: expiresIn ? { expiresIn } : {},\n });\n return data;\n },\n\n async deleteFile(fileId: string): Promise<void> {\n await getApiClient().post(`/storage/files/${fileId}/delete`);\n },\n\n async completeMultipartUpload(\n fileId: string,\n uploadId: string,\n parts: CompletedPart[],\n ): Promise<FileResponse> {\n const { data } = await getApiClient().post<FileResponse>(\n `/storage/multipart/complete/${fileId}`,\n { uploadId, parts },\n );\n return data;\n },\n\n async getConversationFiles(\n conversationId: string,\n params: { page?: number; limit?: number; type?: FileType } = {},\n ): Promise<PaginatedResponse<FileResponse>> {\n const { data } = await getApiClient().get(\n `/storage/conversations/${conversationId}/files`,\n { params },\n );\n return data;\n },\n\n async getMyFiles(params: { page?: number; limit?: number } = {}): Promise<PaginatedResponse<FileResponse>> {\n const { data } = await getApiClient().get('/storage/my-files', { params });\n return data;\n },\n};\n\nasync function runMultipartUpload(\n presigned: PresignedUrlResponse,\n file: UploadableFile,\n platformUploadPartFn: PlatformUploadPartFn,\n onProgress?: (pct: number) => void,\n): Promise<FileResponse> {\n const { multipart } = presigned;\n if (!multipart) throw new Error('No multipart info on presigned response');\n\n const CONCURRENCY = 3;\n const completedParts: CompletedPart[] = [];\n const partProgress: Record<number, number> = {};\n\n multipart.partUrls.forEach(({ partNumber }) => { partProgress[partNumber] = 0; });\n\n const reportProgress = () => {\n if (!onProgress) return;\n const vals = Object.values(partProgress);\n const avg = vals.reduce((s, v) => s + v, 0) / Math.max(vals.length, 1);\n onProgress(Math.round(avg * 0.95));\n };\n\n const uploadPart = async (partNumber: number, uploadUrl: string, method: 'PUT' | 'POST'): Promise<void> => {\n const offset = (partNumber - 1) * multipart.chunkSize;\n const end = Math.min(offset + multipart.chunkSize, file.size);\n const blob = await fetch(file.uri).then((r) => r.blob());\n const slice = blob.slice(offset, end);\n\n const etag = await platformUploadPartFn(uploadUrl, slice, (pct) => {\n partProgress[partNumber] = pct;\n reportProgress();\n }, method);\n\n completedParts.push({ partNumber, etag });\n partProgress[partNumber] = 100;\n reportProgress();\n };\n\n for (let i = 0; i < multipart.partUrls.length; i += CONCURRENCY) {\n const batch = multipart.partUrls.slice(i, i + CONCURRENCY);\n const results = await Promise.allSettled(\n batch.map(({ partNumber, uploadUrl, method }) => uploadPart(partNumber, uploadUrl, method ?? 'PUT')),\n );\n const failed = results.find((r) => r.status === 'rejected') as PromiseRejectedResult | undefined;\n if (failed) throw failed.reason;\n }\n\n completedParts.sort((a, b) => a.partNumber - b.partNumber);\n\n const fileResponse = await storageApi.completeMultipartUpload(\n presigned.fileId,\n multipart.uploadId,\n completedParts,\n );\n onProgress?.(100);\n return fileResponse;\n}\n\n/**\n * Core upload implementation. Returns the public BatchUploadResult plus a\n * slotId → FileResponse map that useChat hooks use internally to match\n * confirmed uploads back to optimistic UI slots by position rather than\n * filename. The slotToFile map is never part of the public API.\n */\nasync function runUploadBatch(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n slotIds: string[],\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<{ result: BatchUploadResult; slotToFile: Map<string, FileResponse> }> {\n // Compress all files first (no-ops for unsupported types or when disabled)\n const compressedFiles = await Promise.all(\n files.map((f) => compressFile(f, platformCompressFn, compressionConfig ?? { enabled: false, imageQuality: 0.85, imageMaxDimension: 1920, compressDocuments: true })),\n );\n\n // Pair each compressed file with its slot ID and a clientIndex.\n // clientIndex is sent to the server and echoed back in both urls and errors,\n // giving us a reliable position mapping regardless of which files fail.\n const slotted = compressedFiles.map((f, i) => ({ file: f, slotId: slotIds[i], clientIndex: i }));\n\n const requests: PresignedUrlRequest[] = slotted.map(({ file: f, clientIndex }) => ({\n filename: f.name,\n mimeType: f.type,\n size: f.size,\n conversationId,\n clientIndex,\n ...(f.compressed && {\n metadata: {\n compressed: f.compressed,\n originalSize: f.originalSize,\n compressionAlgorithm: f.compressionAlgorithm,\n },\n }),\n }));\n\n const { urls, errors: requestErrors } = await storageApi.requestPresignedUrlBatch(requests);\n\n // Use the echoed clientIndex to identify which original slots failed.\n // This is reliable even for same-named files and any failure pattern.\n const failedSlotIds = new Set<string>();\n const failed: Array<{ filename: string; error: string }> = requestErrors.map((e) => {\n const idx = e.clientIndex ?? slotted.findIndex((s) => s.file.name === e.filename);\n const slotId = slotted[idx]?.slotId;\n if (slotId) failedSlotIds.add(slotId);\n return { filename: e.filename, error: e.error };\n });\n\n // Map each presigned URL back to its original slot via clientIndex.\n const progressMap: Record<number, number> = {};\n const reportProgress = () => {\n if (!onProgress) return;\n const vals = Object.values(progressMap);\n const avg = vals.reduce((s, v) => s + v, 0) / Math.max(vals.length, 1);\n onProgress(Math.round(avg));\n };\n\n const successful: FileResponse[] = [];\n const slotToFile = new Map<string, FileResponse>();\n\n await Promise.all(\n urls.map(async (presigned, idx) => {\n // Resolve the original slot via echoed clientIndex; fall back to position\n // in urls[] only if the server didn't echo it (older server version).\n const originalIdx = presigned.clientIndex ?? idx;\n const { file, slotId } = slotted[originalIdx];\n progressMap[originalIdx] = 0;\n try {\n let fileResponse: FileResponse;\n if (presigned.multipart && platformUploadPartFn) {\n fileResponse = await runMultipartUpload(presigned, file, platformUploadPartFn, (pct) => {\n progressMap[originalIdx] = pct;\n reportProgress();\n });\n } else {\n await platformUploadFn(presigned, file, (pct) => {\n progressMap[originalIdx] = Math.round(pct * 0.9);\n reportProgress();\n });\n fileResponse = await storageApi.confirmUpload(presigned.fileId);\n }\n progressMap[originalIdx] = 100;\n reportProgress();\n successful.push(fileResponse);\n slotToFile.set(slotId, fileResponse);\n } catch (err) {\n failed.push({ filename: file.name, error: (err as Error).message });\n }\n }),\n );\n\n return { result: { successful, failed }, slotToFile };\n}\n\n/** Public API — returns standard BatchUploadResult, slot tracking is internal. */\nexport async function uploadBatch(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<BatchUploadResult> {\n const slotIds = files.map(() => generateUUID());\n const { result } = await runUploadBatch(files, platformUploadFn, slotIds, conversationId, onProgress, platformCompressFn, compressionConfig, platformUploadPartFn);\n return result;\n}\n\n/**\n * Used only by useChat hooks (web + RN) to get the slotId → FileResponse map\n * for matching confirmed uploads back to optimistic UI slots.\n * Not exported from the package index — internal SDK use only.\n */\nexport async function uploadBatchWithSlots(\n files: UploadableFile[],\n platformUploadFn: PlatformUploadFn,\n slotIds: string[],\n conversationId?: string,\n onProgress?: (pct: number) => void,\n platformCompressFn?: PlatformCompressFn,\n compressionConfig?: ResolvedCompressionConfig,\n platformUploadPartFn?: PlatformUploadPartFn,\n): Promise<{ result: BatchUploadResult; slotToFile: Map<string, FileResponse> }> {\n return runUploadBatch(files, platformUploadFn, slotIds, conversationId, onProgress, platformCompressFn, compressionConfig, platformUploadPartFn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIA,IAAM,kBAAkB,oBAAI,IAAI;AAAA,EAC9B;AAAA,EAAc;AAAA,EAAY;AAAA,EAAiB;AAAA,EAC3C;AAAA,EAAY;AAAA,EAAmB;AAAA,EAAa;AAAA,EAC5C;AAAA,EAAsB;AAAA,EAAmB;AAAA,EACzC;AAAA,EAAoB;AACtB,CAAC;AAED,IAAM,mBAAmB,oBAAI,IAAI;AAAA,EAC/B;AAAA,EAAc;AAAA,EAAa;AAAA,EAAa;AAAA,EAAc;AAAA,EAAa;AACrE,CAAC;AAGD,IAAM,kBAAkB,oBAAI,IAAI;AAAA,EAC9B;AAAA,EAAa;AAAA,EAAc;AAAA,EAC3B;AAAA,EAAc;AAAA,EAAa;AAAA,EAAa;AAAA,EAAc;AAAA,EACtD;AAAA,EAAmB;AAAA,EACnB;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAIM,SAAS,uBACd,UACA,QACqB;AACrB,MAAI,gBAAgB,IAAI,QAAQ,EAAG,QAAO;AAC1C,MAAI,iBAAiB,IAAI,QAAQ,EAAG,QAAO;AAC3C,MAAI,OAAO,qBAAqB,gBAAgB,IAAI,QAAQ,EAAG,QAAO;AACtE,SAAO;AACT;AAOA,eAAsB,aACpB,MACA,oBACA,QACyB;AACzB,QAAM,OAAuB;AAAA,IAC3B,GAAG;AAAA,IACH,cAAc,KAAK;AAAA,IACnB,YAAY;AAAA,IACZ,sBAAsB;AAAA,EACxB;AAEA,MAAI,CAAC,OAAO,WAAW,CAAC,mBAAoB,QAAO;AAEnD,QAAM,WAAW,uBAAu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,IACZ,KAAK,IAAI,OAAO,WAAW,QAAQ;AAGjC,YAAM,cAAc,UAAU,eAAe;AAC7C,YAAM,EAAE,MAAM,OAAO,IAAI,QAAQ,WAAW;AAC5C,kBAAY,WAAW,IAAI;AAC3B,UAAI;AACF,YAAI;AACJ,YAAI,UAAU,aAAa,sBAAsB;AAC/C,yBAAe,MAAM,mBAAmB,WAAW,MAAM,sBAAsB,CAAC,QAAQ;AACtF,wBAAY,WAAW,IAAI;AAC3B,2BAAe;AAAA,UACjB,CAAC;AAAA,QACH,OAAO;AACL,gBAAM,iBAAiB,WAAW,MAAM,CAAC,QAAQ;AAC/C,wBAAY,WAAW,IAAI,KAAK,MAAM,MAAM,GAAG;AAC/C,2BAAe;AAAA,UACjB,CAAC;AACD,yBAAe,MAAM,WAAW,cAAc,UAAU,MAAM;AAAA,QAChE;AACA,oBAAY,WAAW,IAAI;AAC3B,uBAAe;AACf,mBAAW,KAAK,YAAY;AAC5B,mBAAW,IAAI,QAAQ,YAAY;AAAA,MACrC,SAAS,KAAK;AACZ,eAAO,KAAK,EAAE,UAAU,KAAK,MAAM,OAAQ,IAAc,QAAQ,CAAC;AAAA,MACpE;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,QAAQ,EAAE,YAAY,OAAO,GAAG,WAAW;AACtD;AAsBA,eAAsB,qBACpB,OACA,kBACA,SACA,gBACA,YACA,oBACA,mBACA,sBAC+E;AAC/E,SAAO,eAAe,OAAO,kBAAkB,SAAS,gBAAgB,YAAY,oBAAoB,mBAAmB,oBAAoB;AACjJ;","names":["import_axios"]}
|
package/dist/internal.d.cts
CHANGED
package/dist/internal.d.ts
CHANGED
package/dist/internal.js
CHANGED
|
@@ -726,6 +726,19 @@ interface UploadConfig {
|
|
|
726
726
|
/** Called with 0–100 aggregate progress during a batch upload */
|
|
727
727
|
onProgress?: (progress: number) => void;
|
|
728
728
|
}
|
|
729
|
+
/** Context passed to onSendError. */
|
|
730
|
+
interface SendErrorContext {
|
|
731
|
+
conversationId: string;
|
|
732
|
+
/** tempId of the optimistic message the failure belongs to. */
|
|
733
|
+
tempId: string;
|
|
734
|
+
/**
|
|
735
|
+
* True when resending the identical payload can't succeed (file size/type
|
|
736
|
+
* limits, message too long, no longer a participant, …). The SDK has removed
|
|
737
|
+
* the optimistic message from the timeline. False for transient failures
|
|
738
|
+
* (network / socket / generic server error), where a "! Retry" bubble stays.
|
|
739
|
+
*/
|
|
740
|
+
permanent: boolean;
|
|
741
|
+
}
|
|
729
742
|
interface AntzChatConfig {
|
|
730
743
|
/** REST API base URL — e.g. "https://api.yourapp.com/api/v1" */
|
|
731
744
|
apiUrl: string;
|
|
@@ -780,6 +793,12 @@ interface AntzChatConfig {
|
|
|
780
793
|
* Each SDK (web, RN) provides its own default; Node.js users wire in their own.
|
|
781
794
|
*/
|
|
782
795
|
platformCompressFn?: PlatformCompressFn;
|
|
796
|
+
/**
|
|
797
|
+
* Called when sending a message ultimately fails, after the SDK has updated
|
|
798
|
+
* the timeline (removed the bubble for permanent failures, flagged it retryable
|
|
799
|
+
* otherwise). Use it to surface a toast / error banner to the user.
|
|
800
|
+
*/
|
|
801
|
+
onSendError?: (error: Error, context: SendErrorContext) => void;
|
|
783
802
|
/**
|
|
784
803
|
* Number of messages fetched per page when loading chat history.
|
|
785
804
|
* Default: 40
|
|
@@ -844,6 +863,7 @@ interface ResolvedConfig {
|
|
|
844
863
|
platformCompressFn?: PlatformCompressFn;
|
|
845
864
|
compression: ResolvedCompressionConfig;
|
|
846
865
|
persistStorage: PersistStorage;
|
|
866
|
+
onSendError?: (error: Error, context: SendErrorContext) => void;
|
|
847
867
|
messagePageSize: number;
|
|
848
868
|
starredMessagePageSize: number;
|
|
849
869
|
searchPageSize: number;
|
|
@@ -890,4 +910,4 @@ declare function uploadBatchWithSlots(files: UploadableFile[], platformUploadFn:
|
|
|
890
910
|
slotToFile: Map<string, FileResponse>;
|
|
891
911
|
}>;
|
|
892
912
|
|
|
893
|
-
export { type MessageStarUpdatedEvent as $, type AuthResponse as A, type BatchUploadResult as B, type CrossConversationSyncResponse as C, type CompressionConfig as D, type ConversationType as E, type ForwardMessagePayload as F, type ConversationUpdatedEvent as G, type FileSizeLimits as H, type ForwardResultPayload as I, type LastReaction as J, MENTION_ALL_ID as K, type LoginCredentials as L, type Message as M, type MessageAckEvent as N, type MessageContent as O, type PaginatedResponse as P, type MessageDeletedEvent as Q, type ResolvedCompressionConfig as R, type SendMessagePayload as S, type MessageDeletedForMeEvent as T, type User as U, type MessageDeliveredEvent as V, type MessageForwardReference as W, type MessageMetadata as X, type MessageReaction as Y, type MessageReceiptEntry as Z, type MessageReplyReference as _, type RegisterData as a, type MessageUpdatedEvent as a0, type MessagesDeliveredEvent as a1, type MultipartPartUrl as a2, type MultipartUploadInfo as a3, type NewMessageEvent as a4, type OptimisticAttachment as a5, type PlatformCompressFn as a6, type PlatformUploadFn as a7, type PlatformUploadPartFn as a8, type QuietHours as a9, type ReactionGroup as aa, type ReactionUpdatedEvent as ab, type ReactionUser as ac, type ReadReceiptEvent as ad, type ReplyAttachmentSnapshot as ae, type ResolvedFileSizeLimits as af, type
|
|
913
|
+
export { type MessageStarUpdatedEvent as $, type AuthResponse as A, type BatchUploadResult as B, type CrossConversationSyncResponse as C, type CompressionConfig as D, type ConversationType as E, type ForwardMessagePayload as F, type ConversationUpdatedEvent as G, type FileSizeLimits as H, type ForwardResultPayload as I, type LastReaction as J, MENTION_ALL_ID as K, type LoginCredentials as L, type Message as M, type MessageAckEvent as N, type MessageContent as O, type PaginatedResponse as P, type MessageDeletedEvent as Q, type ResolvedCompressionConfig as R, type SendMessagePayload as S, type MessageDeletedForMeEvent as T, type User as U, type MessageDeliveredEvent as V, type MessageForwardReference as W, type MessageMetadata as X, type MessageReaction as Y, type MessageReceiptEntry as Z, type MessageReplyReference as _, type RegisterData as a, type MessageUpdatedEvent as a0, type MessagesDeliveredEvent as a1, type MultipartPartUrl as a2, type MultipartUploadInfo as a3, type NewMessageEvent as a4, type OptimisticAttachment as a5, type PlatformCompressFn as a6, type PlatformUploadFn as a7, type PlatformUploadPartFn as a8, type QuietHours as a9, type ReactionGroup as aa, type ReactionUpdatedEvent as ab, type ReactionUser as ac, type ReadReceiptEvent as ad, type ReplyAttachmentSnapshot as ae, type ResolvedFileSizeLimits as af, type SendErrorContext as ag, type SendMessageAttachment as ah, type SyncDeletedForMe as ai, type SyncDeliveredReceipt as aj, type SyncParticipantChange as ak, type SyncReactionEntry as al, type SyncReactions as am, type SyncReadReceipt as an, type SyncStarEntry as ao, type SystemMessageMetadata as ap, type TypingIndicatorEvent as aq, type UploadConfig as ar, type UploadProgress as as, type UserStatusEvent as at, resolveConfig as au, storageApi as av, uploadBatch as aw, uploadBatchWithSlots as ax, type AuthTokens as b, type ConversationSyncResponse as c, type AppConfig as d, type CursorPaginatedResponse as e, type MessageReactionsResponse as f, type MessageReceiptsResponse as g, type ConversationListParams as h, type Conversation as i, type Participant as j, type ConversationUnreadCount as k, type UnreadSummary as l, type UserPreferences as m, type ResolvedConfig as n, type ForwardAckPayload as o, type PersistStorage as p, type PresignedUrlRequest as q, type PresignedUrlResponse as r, type FileResponse as s, type CompletedPart as t, type FileType as u, type AntzChatConfig as v, type UploadableFile as w, type Attachment as x, type CompressedFile as y, type CompressionAlgorithm as z };
|
|
@@ -726,6 +726,19 @@ interface UploadConfig {
|
|
|
726
726
|
/** Called with 0–100 aggregate progress during a batch upload */
|
|
727
727
|
onProgress?: (progress: number) => void;
|
|
728
728
|
}
|
|
729
|
+
/** Context passed to onSendError. */
|
|
730
|
+
interface SendErrorContext {
|
|
731
|
+
conversationId: string;
|
|
732
|
+
/** tempId of the optimistic message the failure belongs to. */
|
|
733
|
+
tempId: string;
|
|
734
|
+
/**
|
|
735
|
+
* True when resending the identical payload can't succeed (file size/type
|
|
736
|
+
* limits, message too long, no longer a participant, …). The SDK has removed
|
|
737
|
+
* the optimistic message from the timeline. False for transient failures
|
|
738
|
+
* (network / socket / generic server error), where a "! Retry" bubble stays.
|
|
739
|
+
*/
|
|
740
|
+
permanent: boolean;
|
|
741
|
+
}
|
|
729
742
|
interface AntzChatConfig {
|
|
730
743
|
/** REST API base URL — e.g. "https://api.yourapp.com/api/v1" */
|
|
731
744
|
apiUrl: string;
|
|
@@ -780,6 +793,12 @@ interface AntzChatConfig {
|
|
|
780
793
|
* Each SDK (web, RN) provides its own default; Node.js users wire in their own.
|
|
781
794
|
*/
|
|
782
795
|
platformCompressFn?: PlatformCompressFn;
|
|
796
|
+
/**
|
|
797
|
+
* Called when sending a message ultimately fails, after the SDK has updated
|
|
798
|
+
* the timeline (removed the bubble for permanent failures, flagged it retryable
|
|
799
|
+
* otherwise). Use it to surface a toast / error banner to the user.
|
|
800
|
+
*/
|
|
801
|
+
onSendError?: (error: Error, context: SendErrorContext) => void;
|
|
783
802
|
/**
|
|
784
803
|
* Number of messages fetched per page when loading chat history.
|
|
785
804
|
* Default: 40
|
|
@@ -844,6 +863,7 @@ interface ResolvedConfig {
|
|
|
844
863
|
platformCompressFn?: PlatformCompressFn;
|
|
845
864
|
compression: ResolvedCompressionConfig;
|
|
846
865
|
persistStorage: PersistStorage;
|
|
866
|
+
onSendError?: (error: Error, context: SendErrorContext) => void;
|
|
847
867
|
messagePageSize: number;
|
|
848
868
|
starredMessagePageSize: number;
|
|
849
869
|
searchPageSize: number;
|
|
@@ -890,4 +910,4 @@ declare function uploadBatchWithSlots(files: UploadableFile[], platformUploadFn:
|
|
|
890
910
|
slotToFile: Map<string, FileResponse>;
|
|
891
911
|
}>;
|
|
892
912
|
|
|
893
|
-
export { type MessageStarUpdatedEvent as $, type AuthResponse as A, type BatchUploadResult as B, type CrossConversationSyncResponse as C, type CompressionConfig as D, type ConversationType as E, type ForwardMessagePayload as F, type ConversationUpdatedEvent as G, type FileSizeLimits as H, type ForwardResultPayload as I, type LastReaction as J, MENTION_ALL_ID as K, type LoginCredentials as L, type Message as M, type MessageAckEvent as N, type MessageContent as O, type PaginatedResponse as P, type MessageDeletedEvent as Q, type ResolvedCompressionConfig as R, type SendMessagePayload as S, type MessageDeletedForMeEvent as T, type User as U, type MessageDeliveredEvent as V, type MessageForwardReference as W, type MessageMetadata as X, type MessageReaction as Y, type MessageReceiptEntry as Z, type MessageReplyReference as _, type RegisterData as a, type MessageUpdatedEvent as a0, type MessagesDeliveredEvent as a1, type MultipartPartUrl as a2, type MultipartUploadInfo as a3, type NewMessageEvent as a4, type OptimisticAttachment as a5, type PlatformCompressFn as a6, type PlatformUploadFn as a7, type PlatformUploadPartFn as a8, type QuietHours as a9, type ReactionGroup as aa, type ReactionUpdatedEvent as ab, type ReactionUser as ac, type ReadReceiptEvent as ad, type ReplyAttachmentSnapshot as ae, type ResolvedFileSizeLimits as af, type
|
|
913
|
+
export { type MessageStarUpdatedEvent as $, type AuthResponse as A, type BatchUploadResult as B, type CrossConversationSyncResponse as C, type CompressionConfig as D, type ConversationType as E, type ForwardMessagePayload as F, type ConversationUpdatedEvent as G, type FileSizeLimits as H, type ForwardResultPayload as I, type LastReaction as J, MENTION_ALL_ID as K, type LoginCredentials as L, type Message as M, type MessageAckEvent as N, type MessageContent as O, type PaginatedResponse as P, type MessageDeletedEvent as Q, type ResolvedCompressionConfig as R, type SendMessagePayload as S, type MessageDeletedForMeEvent as T, type User as U, type MessageDeliveredEvent as V, type MessageForwardReference as W, type MessageMetadata as X, type MessageReaction as Y, type MessageReceiptEntry as Z, type MessageReplyReference as _, type RegisterData as a, type MessageUpdatedEvent as a0, type MessagesDeliveredEvent as a1, type MultipartPartUrl as a2, type MultipartUploadInfo as a3, type NewMessageEvent as a4, type OptimisticAttachment as a5, type PlatformCompressFn as a6, type PlatformUploadFn as a7, type PlatformUploadPartFn as a8, type QuietHours as a9, type ReactionGroup as aa, type ReactionUpdatedEvent as ab, type ReactionUser as ac, type ReadReceiptEvent as ad, type ReplyAttachmentSnapshot as ae, type ResolvedFileSizeLimits as af, type SendErrorContext as ag, type SendMessageAttachment as ah, type SyncDeletedForMe as ai, type SyncDeliveredReceipt as aj, type SyncParticipantChange as ak, type SyncReactionEntry as al, type SyncReactions as am, type SyncReadReceipt as an, type SyncStarEntry as ao, type SystemMessageMetadata as ap, type TypingIndicatorEvent as aq, type UploadConfig as ar, type UploadProgress as as, type UserStatusEvent as at, resolveConfig as au, storageApi as av, uploadBatch as aw, uploadBatchWithSlots as ax, type AuthTokens as b, type ConversationSyncResponse as c, type AppConfig as d, type CursorPaginatedResponse as e, type MessageReactionsResponse as f, type MessageReceiptsResponse as g, type ConversationListParams as h, type Conversation as i, type Participant as j, type ConversationUnreadCount as k, type UnreadSummary as l, type UserPreferences as m, type ResolvedConfig as n, type ForwardAckPayload as o, type PersistStorage as p, type PresignedUrlRequest as q, type PresignedUrlResponse as r, type FileResponse as s, type CompletedPart as t, type FileType as u, type AntzChatConfig as v, type UploadableFile as w, type Attachment as x, type CompressedFile as y, type CompressionAlgorithm as z };
|
|
@@ -155,7 +155,7 @@ section.sec>h2:hover{color:#fff}
|
|
|
155
155
|
|
|
156
156
|
<div class="section-label">What's New</div>
|
|
157
157
|
<ul>
|
|
158
|
-
<li><a href="#whats-new">v1.4.
|
|
158
|
+
<li><a href="#whats-new">v1.4.5 Release Notes</a></li>
|
|
159
159
|
</ul>
|
|
160
160
|
|
|
161
161
|
<div class="section-label">Getting Started</div>
|
|
@@ -251,12 +251,158 @@ section.sec>h2:hover{color:#fff}
|
|
|
251
251
|
<h2>What's New</h2>
|
|
252
252
|
<p style="color:var(--muted);font-size:13px;margin-bottom:20px">Version history and release notes. Click a version to expand.</p>
|
|
253
253
|
|
|
254
|
-
<!-- ── v1.4.
|
|
254
|
+
<!-- ── v1.4.5 (current) ── -->
|
|
255
|
+
<div class="wn-version open" id="wn-145">
|
|
256
|
+
<div class="wn-header" onclick="toggleVersion('wn-145')">
|
|
257
|
+
<div class="wn-title">
|
|
258
|
+
<span class="wn-ver">v1.4.5</span>
|
|
259
|
+
<span class="wn-badge current">Current</span>
|
|
260
|
+
<span class="wn-date">September 2026</span>
|
|
261
|
+
</div>
|
|
262
|
+
<span class="wn-chevron">▲</span>
|
|
263
|
+
</div>
|
|
264
|
+
<div class="wn-body">
|
|
265
|
+
<div class="callout info"><strong>Version note.</strong> This release was built and reviewed internally as <code>1.4.7</code>; it is published as <code>1.4.5</code> to fit the antz_contact_center Verdaccio pin. They are the same code — there is no <code>1.4.6</code>, and no separate <code>1.4.7</code> will be published.</div>
|
|
266
|
+
|
|
267
|
+
<p>One theme runs through this release: <strong>transit encryption must never silently degrade to plaintext.</strong> Every fix below closes a path where the SDK either sent an unencrypted request the server was guaranteed to reject with <code>403 "Transit encryption required"</code>, or hung forever on a handshake nothing was driving.</p>
|
|
268
|
+
|
|
269
|
+
<div class="wn-item" id="wn-145-transit">
|
|
270
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-transit')">
|
|
271
|
+
<span class="wn-tag fix">Fix</span>
|
|
272
|
+
<span class="wn-item-title">Transit session survives a socket drop — no more "chat dies until I reload the page"</span>
|
|
273
|
+
<span class="wn-chevron-sm">▾</span>
|
|
274
|
+
</div>
|
|
275
|
+
<div class="wn-item-body">
|
|
276
|
+
<p>The socket <code>disconnect</code> handler called <code>clearTransitSession()</code> on <em>every</em> websocket drop — a network hiccup, a backgrounded tab, a server redeploy. The REST transit session is independent of the socket transport, so wiping it there made every subsequent chat REST call fail with <code>403 "Transit encryption required"</code> until a full page reload.</p>
|
|
277
|
+
<p><strong>Fix:</strong> the session is now torn down only on an explicit <code>disconnectSocket()</code> or an auth change. socket.io's auto-reconnect re-runs the handshake auth and the server re-emits <code>transit_session</code>, which replaces the key through the persistent listener.</p>
|
|
278
|
+
<p>This was the most user-visible bug in 1.4.4: <em>"chat stops working after the laptop sleeps or the tab sits in the background, and only a reload fixes it."</em></p>
|
|
279
|
+
</div>
|
|
280
|
+
</div>
|
|
281
|
+
|
|
282
|
+
<div class="wn-item" id="wn-145-state">
|
|
283
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-state')">
|
|
284
|
+
<span class="wn-tag fix">Fix</span>
|
|
285
|
+
<span class="wn-item-title">Transit state can no longer contradict itself and 403 forever</span>
|
|
286
|
+
<span class="wn-chevron-sm">▾</span>
|
|
287
|
+
</div>
|
|
288
|
+
<div class="wn-item-body">
|
|
289
|
+
<p><code>clearTransitSession()</code> deliberately left <code>sessionEverEstablished = true</code>, which put two readers into permanent disagreement: <code>waitForTransitReady()</code> trusted the flag and returned "don't block", while <code>isTransitEnabled()</code> checked the live session and returned "don't encrypt". Requests then went out unencrypted, 403'd forever, and had no path back to a working state. The flag now resets alongside the session.</p>
|
|
290
|
+
</div>
|
|
291
|
+
</div>
|
|
292
|
+
|
|
293
|
+
<div class="wn-item" id="wn-145-timeout">
|
|
294
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-timeout')">
|
|
295
|
+
<span class="wn-tag fix">Fix</span>
|
|
296
|
+
<span class="wn-item-title">A slow handshake no longer disables transit for the whole session</span>
|
|
297
|
+
<span class="wn-chevron-sm">▾</span>
|
|
298
|
+
</div>
|
|
299
|
+
<div class="wn-item-body">
|
|
300
|
+
<p>The 5s <code>transit_session</code> safety timeout called <code>configureTransit(false)</code>, converting a transient stall into plaintext for every later REST request <em>and</em> socket emit. A timeout means "slow right now", not "the server doesn't want transit" — the only authoritative off signal is <code>GET /crypto/pubkey</code> returning <code>enabled: false</code>. The timeout now stops blocking that one connect attempt, logs, and leaves transit required while the retry paths keep working.</p>
|
|
301
|
+
</div>
|
|
302
|
+
</div>
|
|
303
|
+
|
|
304
|
+
<div class="wn-item" id="wn-145-preauth">
|
|
305
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-preauth')">
|
|
306
|
+
<span class="wn-tag fix">Fix</span>
|
|
307
|
+
<span class="wn-item-title">Pre-auth requests no longer race the handshake</span>
|
|
308
|
+
<span class="wn-chevron-sm">▾</span>
|
|
309
|
+
</div>
|
|
310
|
+
<div class="wn-item-body">
|
|
311
|
+
<p>The request interceptor gated its transit wait on the presence of an auth token, so unauthenticated calls — <code>GET /app/config</code> firing before an async <code>authProvider</code> resolves — skipped the wait and were rejected with <code>403 "Transit encryption required"</code>. The server enforces transit independent of auth, so the gate now keys off whether transit encryption is <em>configured</em>, not whether a token is present.</p>
|
|
312
|
+
</div>
|
|
313
|
+
</div>
|
|
314
|
+
|
|
315
|
+
<div class="wn-item" id="wn-145-joinroom">
|
|
316
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-joinroom')">
|
|
317
|
+
<span class="wn-tag fix">Fix</span>
|
|
318
|
+
<span class="wn-item-title">A dropped join_room no longer silently stops message delivery</span>
|
|
319
|
+
<span class="wn-chevron-sm">▾</span>
|
|
320
|
+
</div>
|
|
321
|
+
<div class="wn-item-body">
|
|
322
|
+
<p><code>join_room</code> is fire-and-forget and the UI emits it once per conversation open. One dropped during the transit-handshake gap, or lost on a plain socket reconnect, left the client outside the server-side room with no error and no retry — <code>new_message</code> broadcasts for that conversation simply never arrived.</p>
|
|
323
|
+
<p><strong>Fix:</strong> the SDK now tracks the rooms it believes it is in and re-emits <code>join_room</code> for all of them whenever a transit session (re-)establishes or the socket reconnects. Server-side join is idempotent, and a stale room (the user is no longer a member) is rejected server-side and swallowed.</p>
|
|
324
|
+
</div>
|
|
325
|
+
</div>
|
|
326
|
+
|
|
327
|
+
<div class="wn-item" id="wn-145-ordering">
|
|
328
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-ordering')">
|
|
329
|
+
<span class="wn-tag improvement">Improvement</span>
|
|
330
|
+
<span class="wn-item-title">Send ordering preserved without paying server round-trip latency</span>
|
|
331
|
+
<span class="wn-chevron-sm">▾</span>
|
|
332
|
+
</div>
|
|
333
|
+
<div class="wn-item-body">
|
|
334
|
+
<p>The per-conversation send queue awaited the full server ack before starting the next message. It now awaits only the emit phase (transit wait → encrypt → <code>socket.emit</code>), so message N is on the wire before N+1 begins encrypting, while the ack resolves out-of-band and a slow ack never blocks the next send.</p>
|
|
335
|
+
</div>
|
|
336
|
+
</div>
|
|
337
|
+
|
|
338
|
+
<div class="wn-item" id="wn-145-gate">
|
|
339
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-gate')">
|
|
340
|
+
<span class="wn-tag improvement">Changed</span>
|
|
341
|
+
<span class="wn-item-title">Requests fail loudly instead of hanging or downgrading — may require action</span>
|
|
342
|
+
<span class="wn-chevron-sm">▾</span>
|
|
343
|
+
</div>
|
|
344
|
+
<div class="wn-item-body">
|
|
345
|
+
<p>When transit is required but no session key is available, a REST request waits up to <strong>30 seconds</strong> (<code>TRANSIT_GATE_MAX_WAIT_MS</code>) for an in-flight handshake, then throws a <strong>retryable</strong> <code>AntzChatNetworkError</code> with code <code>TRANSIT_NOT_READY</code>. Socket emits do the same on a 4s budget, deliberately kept under the 5s ack timeout so the caller's own timeout still governs the total wait. Neither path ever falls through to plaintext.</p>
|
|
346
|
+
<p>The REST handshake also retries itself: <code>ensureRestTransitHandshake()</code> is idempotent and backs off up to 5 times (500ms → 8s capped). It un-gates to plaintext <em>only</em> when the server authoritatively reports transit disabled; a transient failure of <code>POST /crypto/session</code> (rate limit, 5xx, network blip) keeps transit required and keeps retrying.</p>
|
|
347
|
+
<div class="callout warn"><strong>Audit your fire-and-forget REST calls.</strong> A call that previously went out as plaintext and returned <code>403</code> now throws instead. If a bootstrap call swallows it — <code>authApi.getMe().then(...).catch(() => {})</code> — auth never resolves, the socket never connects, and the app sits on a loading screen with no visible cause. Either surface the error or retry it; with react-query, key your retry predicate on <code>error.retryable !== false</code>, which <code>AntzChatNetworkError</code> sets to <code>true</code>.</div>
|
|
348
|
+
</div>
|
|
349
|
+
</div>
|
|
350
|
+
|
|
351
|
+
<div class="wn-item" id="wn-145-onsenderror">
|
|
352
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-onsenderror')">
|
|
353
|
+
<span class="wn-tag new">New</span>
|
|
354
|
+
<span class="wn-item-title">onSendError config callback — surface why a message failed to send</span>
|
|
355
|
+
<span class="wn-chevron-sm">▾</span>
|
|
356
|
+
</div>
|
|
357
|
+
<div class="wn-item-body">
|
|
358
|
+
<p>Fires when a message ultimately fails to send, <em>after</em> the SDK has updated the timeline, so hosts can raise a toast or error banner. The callback is for notification only — you do not need to remove or flag the bubble yourself.</p>
|
|
359
|
+
<pre><code><span class="at">onSendError</span>: (error, { conversationId, tempId, permanent }) => {
|
|
360
|
+
<span class="cm">// permanent: a retry of the identical payload cannot succeed (size/type</span>
|
|
361
|
+
<span class="cm">// limit, message too long, no longer a participant) — bubble already removed.</span>
|
|
362
|
+
<span class="cm">// otherwise: transient — the "! Retry" bubble stays in the timeline.</span>
|
|
363
|
+
<span class="kw">if</span> (permanent) toast.error(error.message);
|
|
364
|
+
<span class="kw">else</span> toast.warn(<span class="st">'Message not sent — tap Retry on the message.'</span>);
|
|
365
|
+
}</code></pre>
|
|
366
|
+
<p>New exported type: <code>SendErrorContext</code>. Pair the transient case with <code>useChat().retrySendMessage(messageId)</code> (v1.4.4+) for a retry affordance outside the built-in <code>MessageItem</code>.</p>
|
|
367
|
+
</div>
|
|
368
|
+
</div>
|
|
369
|
+
|
|
370
|
+
<div class="wn-item" id="wn-145-exports">
|
|
371
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-exports')">
|
|
372
|
+
<span class="wn-tag new">New</span>
|
|
373
|
+
<span class="wn-item-title">setAuthReadyPromise, resetTrackedRooms, isApiClientConfigured</span>
|
|
374
|
+
<span class="wn-chevron-sm">▾</span>
|
|
375
|
+
</div>
|
|
376
|
+
<div class="wn-item-body">
|
|
377
|
+
<ul>
|
|
378
|
+
<li><code>setAuthReadyPromise(promise)</code> — gate the request interceptor until your host has resolved an auth token (and, where wired, the transit handshake), so a request fired during boot cannot go out without an <code>Authorization</code> header. <code>@antzsoft/chat-web-sdk</code> v1.1.6 wires this for you.</li>
|
|
379
|
+
<li><code>resetTrackedRooms()</code> — clears the tracked room set. Call it on a genuine identity change (user or tenant switch) so the reconnect re-flush cannot re-join the previous user's rooms. A token refresh is <em>not</em> an identity change and must not call this.</li>
|
|
380
|
+
<li><code>isApiClientConfigured()</code> — reports whether <code>initApiClient()</code> has run, for hosts that need to detect a remount which skipped re-init.</li>
|
|
381
|
+
</ul>
|
|
382
|
+
</div>
|
|
383
|
+
</div>
|
|
384
|
+
|
|
385
|
+
<div class="wn-item" id="wn-145-compat">
|
|
386
|
+
<div class="wn-item-header" onclick="toggleItem('wn-145-compat')">
|
|
387
|
+
<span class="wn-tag improvement">Compatibility</span>
|
|
388
|
+
<span class="wn-item-title">Upgrading from v1.4.4</span>
|
|
389
|
+
<span class="wn-chevron-sm">▾</span>
|
|
390
|
+
</div>
|
|
391
|
+
<div class="wn-item-body">
|
|
392
|
+
<p><strong>Backward compatible at the API level</strong> — additive exports only, no signature changes, no exports removed, no call-site updates required.</p>
|
|
393
|
+
<p>The one behavioral change is the transit gate described above: a request made while transit was required but unavailable used to go out as plaintext and come back <code>403</code>; it now waits up to 30s and throws a retryable <code>AntzChatNetworkError</code>. That is strictly better — recoverable instead of wedged — but only for callers that handle a rejection.</p>
|
|
394
|
+
<p>Two react-query combinations deserve a second look: <code>retry: 0</code> (and mutations, which default to no retries) leaves nothing to re-drive the handshake, and <code>staleTime: Infinity</code> together with <code>refetchOnWindowFocus: false</code> means a query that exhausts its retries will not refetch on its own.</p>
|
|
395
|
+
<p><strong>Also removed:</strong> the internal <code>_preservingSession</code> guard added in v1.4.0. Now that the disconnect handler never clears the session on a transient drop, the guard is unnecessary. Both the transit and non-transit <code>reconnectSocket()</code> paths behave the same as before.</p>
|
|
396
|
+
</div>
|
|
397
|
+
</div>
|
|
398
|
+
</div>
|
|
399
|
+
</div><!-- /.wn-version -->
|
|
400
|
+
|
|
401
|
+
<!-- ── v1.4.4 ── -->
|
|
255
402
|
<div class="wn-version open" id="wn-144">
|
|
256
403
|
<div class="wn-header" onclick="toggleVersion('wn-144')">
|
|
257
404
|
<div class="wn-title">
|
|
258
405
|
<span class="wn-ver">v1.4.4</span>
|
|
259
|
-
<span class="wn-badge current">Current</span>
|
|
260
406
|
<span class="wn-date">July 2026</span>
|
|
261
407
|
</div>
|
|
262
408
|
<span class="wn-chevron">▲</span>
|
|
@@ -435,7 +581,8 @@ section.sec>h2:hover{color:#fff}
|
|
|
435
581
|
</div>
|
|
436
582
|
<div class="wn-item-body">
|
|
437
583
|
<p><code>reconnectSocket()</code> called <code>socket.connect()</code> to apply a refreshed token while preserving the transit session — but socket.io-client's <code>connect()</code> is a no-op on an already-connected socket. A token refresh fires while the user is actively on a chat screen (socket still connected), so the new auth was never actually sent; the socket kept running on the stale token until the server eventually dropped it, and the disconnect handler then cleared the transit session, desyncing the client's key from the server's — producing continuous <code>"Transit decryption failed for event: user_online"</code>.</p>
|
|
438
|
-
<p><strong>Fix:</strong> in the transit branch of <code>reconnectSocket()</code>, force a real transport re-cycle (disconnect → connect) so the fresh auth (token + <code>transitSessionId</code>) is actually sent and the server re-links the <em>same</em> session (same key), instead of silently continuing on stale auth.
|
|
584
|
+
<p><strong>Fix:</strong> in the transit branch of <code>reconnectSocket()</code>, force a real transport re-cycle (disconnect → connect) so the fresh auth (token + <code>transitSessionId</code>) is actually sent and the server re-links the <em>same</em> session (same key), instead of silently continuing on stale auth. Non-transit and already-disconnected paths are unchanged.</p>
|
|
585
|
+
<div class="callout info"><strong>Superseded in v1.4.5.</strong> This fix originally added an internal <code>_preservingSession</code> guard to stop the disconnect handler from wiping the key being carried through the re-cycle. v1.4.5 removed that guard: the disconnect handler no longer clears the transit session on a transient drop at all, so there is nothing to guard against. The transport re-cycle described above is unchanged.</div>
|
|
439
586
|
<p><strong>Backward compatible.</strong> Only affects transit-encryption deployments that refresh auth tokens while the socket is connected. <strong>No integration changes required</strong> — the fix is entirely internal to <code>reconnectSocket()</code>.</p>
|
|
440
587
|
</div>
|
|
441
588
|
</div>
|
package/package.json
CHANGED