@mateosuarezdev/brpc 1.0.66 → 1.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mateosuarezdev/brpc",
3
- "version": "1.0.66",
3
+ "version": "1.0.67",
4
4
  "description": "A Type-Safe, Flexible Web application framework for Bun",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -36,8 +36,7 @@
36
36
  "files": [
37
37
  "dist/**/*.js",
38
38
  "dist/**/*.cjs",
39
- "dist/**/*.d.ts",
40
- "dist/**/*.map"
39
+ "dist/**/*.d.ts"
41
40
  ],
42
41
  "sideEffects": false,
43
42
  "scripts": {
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["..\\src\\cache\\index.ts"],
4
- "sourcesContent": [
5
- "type CacheEntry = {\r\n value: any;\r\n expiresAt: number | null; // null means no expiration\r\n createdAt: number;\r\n};\r\n\r\ntype CacheOptions = {\r\n ttl?: number; // Time to live in milliseconds\r\n maxSize?: number; // Maximum number of entries (LRU eviction)\r\n cleanupInterval?: number; // How often to clean up expired entries (ms)\r\n};\r\n\r\nexport class CacheService {\r\n private entries: Map<string, CacheEntry>;\r\n private accessOrder: Map<string, number>; // For LRU tracking\r\n private readonly maxSize: number;\r\n private readonly defaultTTL: number | null;\r\n private cleanupTimer: Timer | null = null;\r\n private accessCounter = 0;\r\n\r\n constructor(options: CacheOptions = {}) {\r\n this.entries = new Map();\r\n this.accessOrder = new Map();\r\n this.maxSize = options.maxSize ?? Infinity;\r\n this.defaultTTL = options.ttl ?? null;\r\n\r\n // Start automatic cleanup if interval specified\r\n if (options.cleanupInterval) {\r\n this.cleanupTimer = setInterval(\r\n () => this.cleanup(),\r\n options.cleanupInterval,\r\n );\r\n }\r\n }\r\n\r\n /**\r\n * Set a value in cache with optional TTL\r\n */\r\n set(key: string, value: any, ttl?: number): void {\r\n const timeToLive = ttl ?? this.defaultTTL;\r\n const expiresAt = timeToLive ? Date.now() + timeToLive : null;\r\n\r\n // Evict LRU if at max size\r\n if (this.entries.size >= this.maxSize && !this.entries.has(key)) {\r\n this.evictLRU();\r\n }\r\n\r\n this.entries.set(key, {\r\n value,\r\n expiresAt,\r\n createdAt: Date.now(),\r\n });\r\n\r\n this.trackAccess(key);\r\n }\r\n\r\n /**\r\n * Get a value from cache (returns null if expired or not found)\r\n */\r\n get<T = any>(key: string): T | null {\r\n const entry = this.entries.get(key);\r\n\r\n if (!entry) {\r\n return null;\r\n }\r\n\r\n // Check expiration\r\n if (entry.expiresAt && Date.now() > entry.expiresAt) {\r\n this.entries.delete(key);\r\n this.accessOrder.delete(key);\r\n return null;\r\n }\r\n\r\n this.trackAccess(key);\r\n return entry.value;\r\n }\r\n\r\n /**\r\n * Check if key exists and is not expired\r\n */\r\n has(key: string): boolean {\r\n return this.get(key) !== null;\r\n }\r\n\r\n /**\r\n * Delete a specific key\r\n */\r\n delete(key: string): boolean {\r\n this.accessOrder.delete(key);\r\n return this.entries.delete(key);\r\n }\r\n\r\n /**\r\n * Clear all cache entries\r\n */\r\n clear(): void {\r\n this.entries.clear();\r\n this.accessOrder.clear();\r\n this.accessCounter = 0;\r\n }\r\n\r\n /**\r\n * Get all keys (including expired ones)\r\n */\r\n keys(): string[] {\r\n return Array.from(this.entries.keys());\r\n }\r\n\r\n /**\r\n * Get all valid (non-expired) keys\r\n */\r\n validKeys(): string[] {\r\n const now = Date.now();\r\n return Array.from(this.entries.entries())\r\n .filter(([_, entry]) => !entry.expiresAt || entry.expiresAt > now)\r\n .map(([key]) => key);\r\n }\r\n\r\n /**\r\n * Get cache size\r\n */\r\n size(): number {\r\n return this.entries.size;\r\n }\r\n\r\n /**\r\n * Get or set pattern (lazy evaluation)\r\n */\r\n async getOrSet<T = any>(\r\n key: string,\r\n factory: () => T | Promise<T>,\r\n ttl?: number,\r\n ): Promise<T> {\r\n const cached = this.get<T>(key);\r\n if (cached !== null) {\r\n return cached;\r\n }\r\n\r\n const value = await factory();\r\n this.set(key, value, ttl);\r\n return value;\r\n }\r\n\r\n /**\r\n * Set value with expiration timestamp (instead of TTL)\r\n */\r\n setWithExpiry(key: string, value: any, expiresAt: number): void {\r\n const ttl = Math.max(0, expiresAt - Date.now());\r\n this.set(key, value, ttl);\r\n }\r\n\r\n /**\r\n * Get remaining TTL for a key (in milliseconds)\r\n */\r\n ttl(key: string): number | null {\r\n const entry = this.entries.get(key);\r\n if (!entry) return null;\r\n if (!entry.expiresAt) return -1; // -1 means no expiration\r\n\r\n const remaining = entry.expiresAt - Date.now();\r\n return remaining > 0 ? remaining : 0;\r\n }\r\n\r\n /**\r\n * Update TTL for existing key\r\n */\r\n expire(key: string, ttl: number): boolean {\r\n const entry = this.entries.get(key);\r\n if (!entry) return false;\r\n\r\n entry.expiresAt = Date.now() + ttl;\r\n return true;\r\n }\r\n\r\n /**\r\n * Remove expiration from key\r\n */\r\n persist(key: string): boolean {\r\n const entry = this.entries.get(key);\r\n if (!entry) return false;\r\n\r\n entry.expiresAt = null;\r\n return true;\r\n }\r\n\r\n /**\r\n * Delete keys matching a pattern (simple wildcard support)\r\n */\r\n deletePattern(pattern: string): number {\r\n const regex = new RegExp(\r\n \"^\" + pattern.replace(/\\*/g, \".*\").replace(/\\?/g, \".\") + \"$\",\r\n );\r\n\r\n let deleted = 0;\r\n for (const key of this.entries.keys()) {\r\n if (regex.test(key)) {\r\n this.delete(key);\r\n deleted++;\r\n }\r\n }\r\n\r\n return deleted;\r\n }\r\n\r\n /**\r\n * Get multiple keys at once\r\n */\r\n mget<T = any>(keys: string[]): (T | null)[] {\r\n return keys.map((key) => this.get(key));\r\n }\r\n\r\n /**\r\n * Set multiple keys at once\r\n */\r\n mset(entries: Record<string, any>, ttl?: number): void {\r\n for (const [key, value] of Object.entries(entries)) {\r\n this.set(key, value, ttl);\r\n }\r\n }\r\n\r\n /**\r\n * Increment a numeric value (creates if doesn't exist)\r\n */\r\n incr(key: string, amount = 1): number {\r\n const current = this.get(key);\r\n const value = (typeof current === \"number\" ? current : 0) + amount;\r\n this.set(key, value as any);\r\n return value;\r\n }\r\n\r\n /**\r\n * Decrement a numeric value\r\n */\r\n decr(key: string, amount = 1): number {\r\n return this.incr(key, -amount);\r\n }\r\n\r\n /**\r\n * Get cache statistics\r\n */\r\n stats() {\r\n const now = Date.now();\r\n const entries = Array.from(this.entries.entries());\r\n\r\n const expired = entries.filter(\r\n ([_, entry]) => entry.expiresAt && entry.expiresAt <= now,\r\n ).length;\r\n\r\n const valid = entries.length - expired;\r\n\r\n return {\r\n size: this.entries.size,\r\n valid,\r\n expired,\r\n maxSize: this.maxSize,\r\n defaultTTL: this.defaultTTL,\r\n entries: entries.map(([key, entry]) => ({\r\n key,\r\n expiresIn: entry.expiresAt ? Math.max(0, entry.expiresAt - now) : null,\r\n age: now - entry.createdAt,\r\n })),\r\n };\r\n }\r\n\r\n /**\r\n * Clean up expired entries\r\n */\r\n cleanup(): number {\r\n const now = Date.now();\r\n let cleaned = 0;\r\n\r\n for (const [key, entry] of this.entries.entries()) {\r\n if (entry.expiresAt && entry.expiresAt <= now) {\r\n this.entries.delete(key);\r\n this.accessOrder.delete(key);\r\n cleaned++;\r\n }\r\n }\r\n\r\n return cleaned;\r\n }\r\n\r\n /**\r\n * Track access for LRU\r\n */\r\n private trackAccess(key: string): void {\r\n this.accessOrder.set(key, ++this.accessCounter);\r\n }\r\n\r\n /**\r\n * Evict least recently used entry\r\n */\r\n private evictLRU(): void {\r\n let lruKey: string | null = null;\r\n let lruAccess = Infinity;\r\n\r\n for (const [key, accessTime] of this.accessOrder.entries()) {\r\n if (accessTime < lruAccess) {\r\n lruAccess = accessTime;\r\n lruKey = key;\r\n }\r\n }\r\n\r\n if (lruKey) {\r\n this.entries.delete(lruKey);\r\n this.accessOrder.delete(lruKey);\r\n }\r\n }\r\n\r\n /**\r\n * Destroy cache and cleanup timers\r\n */\r\n destroy(): void {\r\n if (this.cleanupTimer) {\r\n clearInterval(this.cleanupTimer);\r\n this.cleanupTimer = null;\r\n }\r\n this.clear();\r\n }\r\n}\r\n\r\n// Export singleton instances for common use cases\r\n// export const cache = new CacheService({\r\n// maxSize: 1000,\r\n// cleanupInterval: 60000, // Clean every minute\r\n// });\r\n"
6
- ],
7
- "mappings": ";AAYO,MAAM,CAAa,CAChB,QACA,YACS,QACA,WACT,aAA6B,KAC7B,cAAgB,EAExB,WAAW,CAAC,EAAwB,CAAC,EAAG,CAOtC,GANA,KAAK,QAAU,IAAI,IACnB,KAAK,YAAc,IAAI,IACvB,KAAK,QAAU,EAAQ,SAAW,IAClC,KAAK,WAAa,EAAQ,KAAO,KAG7B,EAAQ,gBACV,KAAK,aAAe,YAClB,IAAM,KAAK,QAAQ,EACnB,EAAQ,eACV,EAOJ,GAAG,CAAC,EAAa,EAAY,EAAoB,CAC/C,IAAM,EAAa,GAAO,KAAK,WACzB,EAAY,EAAa,KAAK,IAAI,EAAI,EAAa,KAGzD,GAAI,KAAK,QAAQ,MAAQ,KAAK,SAAW,CAAC,KAAK,QAAQ,IAAI,CAAG,EAC5D,KAAK,SAAS,EAGhB,KAAK,QAAQ,IAAI,EAAK,CACpB,QACA,YACA,UAAW,KAAK,IAAI,CACtB,CAAC,EAED,KAAK,YAAY,CAAG,EAMtB,GAAY,CAAC,EAAuB,CAClC,IAAM,EAAQ,KAAK,QAAQ,IAAI,CAAG,EAElC,GAAI,CAAC,EACH,OAAO,KAIT,GAAI,EAAM,WAAa,KAAK,IAAI,EAAI,EAAM,UAGxC,OAFA,KAAK,QAAQ,OAAO,CAAG,EACvB,KAAK,YAAY,OAAO,CAAG,EACpB,KAIT,OADA,KAAK,YAAY,CAAG,EACb,EAAM,MAMf,GAAG,CAAC,EAAsB,CACxB,OAAO,KAAK,IAAI,CAAG,IAAM,KAM3B,MAAM,CAAC,EAAsB,CAE3B,OADA,KAAK,YAAY,OAAO,CAAG,EACpB,KAAK,QAAQ,OAAO,CAAG,EAMhC,KAAK,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Aa,EACxC,KAAK,QAAQ,OAAO,CAAG,EACvB,KAAK,YAAY,OAAO,CAAG,EAC3B,IAIJ,OAAO,EAMD,WAAW,CAAC,EAAmB,CACrC,KAAK,YAAY,IAAI,EAAK,EAAE,KAAK,aAAa,EAMxC,QAAQ,EAAS,CACvB,IAAI,EAAwB,KACxB,EAAY,IAEhB,QAAY,EAAK,KAAe,KAAK,YAAY,QAAQ,EACvD,GAAI,EAAa,EACf,EAAY,EACZ,EAAS,EAIb,GAAI,EACF,KAAK,QAAQ,OAAO,CAAM,EAC1B,KAAK,YAAY,OAAO,CAAM,EAOlC,OAAO,EAAS,CACd,GAAI,KAAK,aACP,cAAc,KAAK,YAAY,EAC/B,KAAK,aAAe,KAEtB,KAAK,MAAM,EAEf",
8
- "debugId": "E599AB3EA78545B764756E2164756E21",
9
- "names": []
10
- }
@@ -1,14 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["..\\src\\client\\storage.ts", "..\\src\\client\\brpc-client-error.ts", "..\\src\\client\\utils.ts", "..\\src\\client\\ws-manager.ts", "..\\src\\client\\service.ts"],
4
- "sourcesContent": [
5
- "import type { StorageObject } from \"../storage\";\r\n\r\nexport type GetObjectUrl = (\r\n storageObject: StorageObject | null | undefined,\r\n options: { placeholder?: string; throwOnInvalid?: boolean },\r\n) => string;\r\n\r\n/**\r\n * @returns url of the file\r\n */\r\nexport function getObjectUrl(\r\n storageObject: StorageObject | null | undefined,\r\n options: { placeholder?: string; throwOnInvalid?: boolean } = {\r\n placeholder: undefined,\r\n throwOnInvalid: true,\r\n },\r\n s3Endpoint: string,\r\n): string {\r\n if (!storageObject) return options?.placeholder ?? \"PLACEHOLDER\";\r\n\r\n // External file\r\n if (storageObject.url) {\r\n return storageObject.url;\r\n }\r\n\r\n // Internal file\r\n if (storageObject.key) {\r\n if (storageObject.isPrivate || storageObject.provider === \"local\") {\r\n // if private or local proxy/serve through/from server\r\n return `/storage/${storageObject.key}`;\r\n } else {\r\n // if public serve from s3\r\n return `${s3Endpoint}/${storageObject.key}`;\r\n }\r\n }\r\n\r\n if (options.throwOnInvalid) {\r\n throw new Error(\"Invalid storage object\");\r\n } else {\r\n return options?.placeholder ?? \"PLACEHOLDER\";\r\n }\r\n}\r\n",
6
- "class BrpcClientError extends Error {\r\n readonly status: number;\r\n readonly code?: string;\r\n readonly clientCode?: string;\r\n readonly data?: any;\r\n\r\n constructor(\r\n message: string,\r\n status: number,\r\n code?: string,\r\n clientCode?: string,\r\n data?: any\r\n ) {\r\n super(message);\r\n this.name = \"BrpcClientError\";\r\n this.status = status;\r\n this.code = code;\r\n this.clientCode = clientCode;\r\n this.data = data;\r\n }\r\n\r\n // Helper method to check if this is a specific client error\r\n isClientError(clientCode: string): boolean {\r\n return this.clientCode === clientCode;\r\n }\r\n\r\n // Helper methods for common error types\r\n isUnauthorized(): boolean {\r\n return this.status === 401;\r\n }\r\n\r\n isForbidden(): boolean {\r\n return this.status === 403;\r\n }\r\n\r\n isNotFound(): boolean {\r\n return this.status === 404;\r\n }\r\n\r\n isValidationError(): boolean {\r\n return this.status === 400 || this.code === \"BAD_REQUEST\";\r\n }\r\n}\r\n\r\nexport { BrpcClientError };\r\n",
7
- "/**\r\n * Removes any slashes (/) from prefixes\r\n */\r\nexport function parsePrefix(prefix?: string) {\r\n if (!prefix) return null;\r\n return prefix.replace(/\\//g, \"\");\r\n}\r\n\r\n/**\r\n * Returns base url without trailing slash\r\n */\r\nexport function getFormattedBaseUrl(baseUrl?: string) {\r\n let formattedBaseUrl =\r\n baseUrl && baseUrl.length > 0\r\n ? baseUrl\r\n : typeof window !== \"undefined\"\r\n ? window.location.origin\r\n : \"\";\r\n\r\n // Remove trailing slash if present\r\n formattedBaseUrl = formattedBaseUrl.endsWith(\"/\")\r\n ? formattedBaseUrl.slice(0, -1)\r\n : formattedBaseUrl;\r\n\r\n return formattedBaseUrl;\r\n}\r\n\r\nexport function getWsUrl(\r\n formattedBaseUrl: string,\r\n parsedPrefix: string | null\r\n) {\r\n const wsPath = parsedPrefix ? `/${parsedPrefix}/ws` : `/ws`;\r\n return `${formattedBaseUrl.replace(/^http/, \"ws\")}${wsPath}`;\r\n}\r\n\r\nexport function getProceduresPrefixPath(\r\n parsedPrefix: string | null,\r\n parsedApiPrefix: string | null\r\n) {\r\n if (parsedPrefix && parsedApiPrefix) {\r\n return `/${parsedPrefix}/${parsedApiPrefix}`;\r\n } else if (parsedPrefix && !parsedApiPrefix) {\r\n return `/${parsedPrefix}`;\r\n } else if (!parsedPrefix && parsedApiPrefix) {\r\n return `/${parsedApiPrefix}`;\r\n } else {\r\n return \"\";\r\n }\r\n}\r\n",
8
- "import type { HeadersResolver } from \"./types\";\r\n\r\n// WebSocket Manager with improved authentication handling\r\nclass WebSocketManager {\r\n private ws: WebSocket | null = null;\r\n private subscriptions: Map<string, Set<(data: any) => void>> = new Map();\r\n private messageQueue: any[] = [];\r\n private isConnected: boolean = false;\r\n private reconnectTimeout: any = null;\r\n private WebSocketImpl: typeof WebSocket;\r\n private getHeaders: () => Promise<Record<string, string>>;\r\n private authToken: string | null = null;\r\n private debug: boolean;\r\n private pendingAuth: boolean = false; // Track if we're waiting for auth response\r\n private authActions: (() => void)[] = []; // Actions to perform after successful auth\r\n\r\n constructor(\r\n private baseUrl: string,\r\n WebSocketImpl: typeof WebSocket,\r\n headersResolver: HeadersResolver,\r\n debug: boolean = false,\r\n nodeEnv: string,\r\n ) {\r\n this.WebSocketImpl = WebSocketImpl;\r\n this.debug = debug && nodeEnv === \"development\";\r\n\r\n // Create headers resolver function\r\n this.getHeaders = async () => {\r\n if (typeof headersResolver === \"function\") {\r\n return await headersResolver();\r\n }\r\n return headersResolver ?? {};\r\n };\r\n\r\n this.connect();\r\n }\r\n\r\n private async connect() {\r\n try {\r\n // Get latest headers for authentication\r\n const headers = await this.getHeaders();\r\n this.authToken = headers[\"Authorization\"] || null;\r\n\r\n const wsUrl = this.baseUrl;\r\n\r\n if (this.debug) {\r\n console.log(`Connecting to WebSocket: ${wsUrl}`);\r\n }\r\n\r\n this.ws = new this.WebSocketImpl(wsUrl);\r\n\r\n this.ws.onopen = async () => {\r\n if (this.debug) {\r\n console.log(\"WebSocket connection established\");\r\n }\r\n\r\n this.isConnected = true;\r\n\r\n // Clear any pending auth actions from previous connection attempts\r\n this.authActions = [];\r\n\r\n // If we have auth token, send it first and mark auth as pending\r\n if (this.authToken) {\r\n this.pendingAuth = true;\r\n this.send({ type: \"authenticate\", token: this.authToken });\r\n if (this.debug) {\r\n console.log(\"Sent authentication token, waiting for response\");\r\n }\r\n\r\n // Queue resubscriptions to happen after authentication\r\n this.authActions.push(() => {\r\n // Resubscribe to all topics\r\n for (const topic of this.subscriptions.keys()) {\r\n this.send({ type: \"subscribe\", topic });\r\n if (this.debug) {\r\n console.log(`Resubscribed to topic: ${topic}`);\r\n }\r\n }\r\n\r\n // Send any queued messages\r\n if (this.messageQueue.length > 0 && this.debug) {\r\n console.log(\r\n `Sending ${this.messageQueue.length} queued messages`,\r\n );\r\n }\r\n\r\n this.messageQueue.forEach((msg) => this.send(msg));\r\n this.messageQueue = [];\r\n });\r\n } else {\r\n // No auth needed, proceed with subscriptions immediately\r\n for (const topic of this.subscriptions.keys()) {\r\n this.send({ type: \"subscribe\", topic });\r\n if (this.debug) {\r\n console.log(`Resubscribed to topic: ${topic}`);\r\n }\r\n }\r\n\r\n // Send any queued messages\r\n if (this.messageQueue.length > 0 && this.debug) {\r\n console.log(`Sending ${this.messageQueue.length} queued messages`);\r\n }\r\n\r\n this.messageQueue.forEach((msg) => this.send(msg));\r\n this.messageQueue = [];\r\n }\r\n };\r\n\r\n this.ws.onmessage = (event) => {\r\n try {\r\n if (this.debug) {\r\n // console.log(\"Active subscriptions\", this.subscriptions);\r\n console.log(\"WebSocket message received:\", event.data);\r\n }\r\n\r\n const message = JSON.parse(event.data);\r\n\r\n // Handle auth-specific messages\r\n if (message.type === \"auth_success\") {\r\n // Check if authentication was successful or if it was a logout confirmation\r\n const isAuthenticated = message.authenticated !== false;\r\n\r\n if (this.debug) {\r\n console.log(\r\n `WebSocket ${\r\n isAuthenticated ? \"authentication\" : \"deauthentication\"\r\n } successful`,\r\n );\r\n }\r\n\r\n // Clear pending auth state\r\n this.pendingAuth = false;\r\n\r\n // Only execute queued actions for successful authentication\r\n if (isAuthenticated) {\r\n // Execute all queued actions that were waiting for authentication\r\n this.authActions.forEach((action) => action());\r\n }\r\n\r\n // Clear action queue\r\n this.authActions = [];\r\n\r\n return;\r\n }\r\n\r\n if (message.type === \"auth_error\") {\r\n console.error(\"WebSocket authentication failed:\", message.error);\r\n\r\n // Clear pending auth state but don't process queued actions\r\n this.pendingAuth = false;\r\n this.authActions = [];\r\n\r\n return;\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"Got new message\", message);\r\n }\r\n\r\n if (message.topic && this.subscriptions.has(message.topic)) {\r\n const callbacks = this.subscriptions.get(message.topic);\r\n if (this.debug) {\r\n console.log(\"Calling subscription callbacks\");\r\n }\r\n callbacks?.forEach((callback) => callback(message.data));\r\n } else if (message.error) {\r\n console.error(\"WebSocket error:\", message.error);\r\n } else {\r\n // Log unknown message formats to help debug\r\n console.warn(\"Unhandled WebSocket message format:\", message);\r\n }\r\n } catch (error) {\r\n if (this.debug) {\r\n console.error(\"Error processing WebSocket message:\", error);\r\n console.error(\"Raw message:\", event.data);\r\n }\r\n }\r\n };\r\n\r\n this.ws.onclose = (event) => {\r\n this.isConnected = false;\r\n this.pendingAuth = false; // Reset auth state\r\n\r\n if (this.debug) {\r\n console.log(\r\n `WebSocket connection closed. Code: ${event.code}, Reason: ${event.reason}`,\r\n );\r\n }\r\n\r\n // Try to reconnect after a delay\r\n this.reconnectTimeout = setTimeout(() => this.connect(), 2000);\r\n };\r\n\r\n this.ws.onerror = (error) => {\r\n if (this.debug) {\r\n console.log(\"WebSocket error:\", error);\r\n }\r\n };\r\n } catch (error) {\r\n if (this.debug) {\r\n console.log(\"Failed to create WebSocket connection:\", error);\r\n }\r\n this.pendingAuth = false;\r\n // Try to reconnect after a delay\r\n this.reconnectTimeout = setTimeout(() => this.connect(), 2000);\r\n }\r\n }\r\n\r\n public disconnect() {\r\n if (this.reconnectTimeout) {\r\n clearTimeout(this.reconnectTimeout);\r\n this.reconnectTimeout = null;\r\n }\r\n\r\n if (this.ws) {\r\n this.ws.close();\r\n this.ws = null;\r\n }\r\n\r\n this.isConnected = false;\r\n this.pendingAuth = false;\r\n this.authActions = [];\r\n this.subscriptions.clear();\r\n }\r\n\r\n public send(message: any) {\r\n if (this.isConnected && this.ws?.readyState === this.WebSocketImpl.OPEN) {\r\n // If authentication is pending and this isn't an auth message, queue it\r\n if (this.pendingAuth && message.type !== \"authenticate\") {\r\n this.messageQueue.push(message);\r\n if (this.debug) {\r\n console.log(\r\n \"Message queued until authentication completes:\",\r\n message,\r\n );\r\n }\r\n return;\r\n }\r\n\r\n const messageStr = JSON.stringify(message);\r\n this.ws.send(messageStr);\r\n\r\n if (this.debug) {\r\n console.log(\"WebSocket message sent:\", messageStr);\r\n }\r\n } else {\r\n this.messageQueue.push(message);\r\n\r\n if (this.debug) {\r\n console.log(\"WebSocket message queued (not connected):\", message);\r\n }\r\n }\r\n }\r\n\r\n public subscribe(topic: string, callback: (data: any) => void) {\r\n if (!this.subscriptions.has(topic)) {\r\n this.subscriptions.set(topic, new Set());\r\n\r\n // If we're connected, send the subscription (with auth awareness)\r\n if (this.isConnected) {\r\n if (this.pendingAuth) {\r\n // Queue the subscription for after auth\r\n this.authActions.push(() => {\r\n this.send({ type: \"subscribe\", topic });\r\n if (this.debug) {\r\n console.log(`Subscribed to topic after auth: ${topic}`);\r\n }\r\n });\r\n } else {\r\n // Send immediately\r\n this.send({ type: \"subscribe\", topic });\r\n if (this.debug) {\r\n console.log(`Subscribed to topic: ${topic}`);\r\n }\r\n }\r\n }\r\n }\r\n\r\n this.subscriptions.get(topic)!.add(callback);\r\n\r\n return {\r\n unsubscribe: () => this.unsubscribe(topic, callback),\r\n publish: (data: any) => this.publish(topic, data),\r\n };\r\n }\r\n\r\n private unsubscribe(topic: string, callback: (data: any) => void) {\r\n const callbacks = this.subscriptions.get(topic);\r\n if (callbacks) {\r\n callbacks.delete(callback);\r\n if (callbacks.size === 0) {\r\n this.subscriptions.delete(topic);\r\n this.send({ type: \"unsubscribe\", topic });\r\n\r\n if (this.debug) {\r\n console.log(`Unsubscribed from topic: ${topic}`);\r\n }\r\n }\r\n }\r\n }\r\n\r\n public publish(topic: string, data: any) {\r\n this.send({ type: \"publish\", topic, data });\r\n\r\n if (this.debug) {\r\n console.log(`Published to topic: ${topic}`, data);\r\n }\r\n }\r\n\r\n // Method to update auth token when it changes\r\n public async updateAuth() {\r\n const headers = await this.getHeaders();\r\n const authHeader = headers[\"Authorization\"] || \"\";\r\n\r\n // Extract token from header - may be empty (\"Bearer \")\r\n const newToken = authHeader.startsWith(\"Bearer \")\r\n ? authHeader.substring(7).trim()\r\n : authHeader.trim();\r\n\r\n const hasValidToken = newToken.length > 0;\r\n const hadValidToken =\r\n this.authToken !== null && this.authToken.trim().length > 0;\r\n\r\n // If token status changed and we're connected, handle the change\r\n if (\r\n (hasValidToken !== hadValidToken || newToken !== this.authToken) &&\r\n this.isConnected\r\n ) {\r\n // Update stored token\r\n this.authToken = newToken;\r\n\r\n // Always send authenticate message, but with empty token when logging out\r\n this.pendingAuth = hasValidToken;\r\n this.send({ type: \"authenticate\", token: authHeader });\r\n\r\n if (this.debug) {\r\n if (hasValidToken) {\r\n console.log(\"Updated authentication token, waiting for confirmation\");\r\n } else {\r\n console.log(\"Sent empty token for deauthentication\");\r\n // Since we're not expecting auth confirmation for logout, clear pending state\r\n this.pendingAuth = false;\r\n this.authActions = [];\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\nexport { WebSocketManager };\r\n",
9
- "import { BrpcClientError } from \"./brpc-client-error\";\r\nimport { getObjectUrl } from \"./storage\";\r\nimport type {\r\n HeadersResolver,\r\n BrpcClient,\r\n InferClientRoutes,\r\n BrpcClientOptions,\r\n} from \"./types\";\r\nimport {\r\n getFormattedBaseUrl,\r\n getProceduresPrefixPath,\r\n getWsUrl,\r\n parsePrefix,\r\n} from \"./utils\";\r\nimport { WebSocketManager } from \"./ws-manager\";\r\n\r\n/**\r\n * Creates a type-safe client for the BRPC API\r\n * @param baseUrl The base URL of the BRPC server\r\n * @param options Client configuration options\r\n * @returns A client object that mirrors the server routes\r\n */\r\nexport function createBrpcClient<TRoutesStructure>(\r\n baseUrl: string,\r\n options: BrpcClientOptions = {},\r\n): BrpcClient<TRoutesStructure> {\r\n const fetchImpl = options.fetch || fetch;\r\n const WebSocketImpl =\r\n options.WebSocket ?? (typeof WebSocket !== \"undefined\" ? WebSocket : null);\r\n const prefix = options.prefix ?? \"\";\r\n const apiPrefix = options.apiPrefix ?? \"\";\r\n const debug = options.debug ?? false;\r\n const s3Endpoint = options.s3Endpoint;\r\n const nodeEnv = options.nodeEnv;\r\n\r\n if (!options.s3Endpoint) {\r\n throw new Error(\"BRPC Client: Pass s3Endpoint option to createBrpcClient\");\r\n }\r\n\r\n if (!options.nodeEnv) {\r\n throw new Error(\"BRPC Client: Pass nodeEnv option to createBrpcClient\");\r\n }\r\n\r\n if (!WebSocketImpl) {\r\n throw new Error(\"WebSocket is not available in this environment\");\r\n }\r\n\r\n let currentHeaders: HeadersResolver = options.headers ?? {};\r\n\r\n const headersResolver: HeadersResolver = async () => {\r\n if (typeof currentHeaders === \"function\") {\r\n return await currentHeaders();\r\n }\r\n return currentHeaders;\r\n };\r\n\r\n // Properly format the base URL\r\n const formattedBaseUrl = getFormattedBaseUrl(baseUrl);\r\n\r\n // Format prefixes (returns clean word, like api, without\r\n // initial and trailing slahes)\r\n const parsedPrefix = parsePrefix(options.prefix);\r\n const parsedApiPrefix = parsePrefix(options.apiPrefix);\r\n\r\n const proceduresPrefixedPath = getProceduresPrefixPath(\r\n parsedPrefix,\r\n parsedApiPrefix,\r\n );\r\n\r\n // Create WebSocket URL with proper path joining\r\n const wsUrl = getWsUrl(formattedBaseUrl, parsedPrefix);\r\n\r\n // Create WebSocket manager with headers resolver\r\n const wsManager = new WebSocketManager(\r\n wsUrl,\r\n WebSocketImpl,\r\n currentHeaders,\r\n debug,\r\n nodeEnv!,\r\n );\r\n\r\n // Helper to resolve headers before each request\r\n const resolveHeaders = async (\r\n defaultContentType = true,\r\n ): Promise<Record<string, string>> => {\r\n const baseHeaders: Record<string, string> = {};\r\n\r\n if (defaultContentType) {\r\n baseHeaders[\"Content-Type\"] = \"application/json\";\r\n }\r\n\r\n // Use the same headersResolver as WebSocket\r\n const dynamicHeaders = await headersResolver();\r\n return { ...baseHeaders, ...dynamicHeaders };\r\n };\r\n\r\n // Helper to handle HTTP responses\r\n const handleResponse = async (response: Response) => {\r\n if (!response.ok) {\r\n let errorData;\r\n try {\r\n errorData = await response.json();\r\n } catch {\r\n try {\r\n errorData = { error: await response.text() };\r\n } catch {\r\n errorData = { error: \"Failed to parse error response\" };\r\n }\r\n }\r\n\r\n // Handle the new BRPCError format from server\r\n if (errorData?.error && typeof errorData.error === \"object\") {\r\n const serverError = errorData.error;\r\n\r\n throw new BrpcClientError(\r\n serverError.message || response.statusText,\r\n response.status,\r\n serverError.code,\r\n serverError.clientCode,\r\n serverError.data,\r\n );\r\n }\r\n\r\n // Handle legacy error format or simple string errors\r\n const errorMessage =\r\n errorData?.error || errorData?.message || response.statusText;\r\n\r\n throw new BrpcClientError(\r\n errorMessage,\r\n response.status,\r\n undefined,\r\n undefined,\r\n errorData,\r\n );\r\n }\r\n\r\n // Check content type to determine how to handle the response\r\n const contentType = response.headers.get(\"Content-Type\") || \"\";\r\n\r\n try {\r\n if (contentType.includes(\"application/json\")) {\r\n const result = await response.json();\r\n return result.data;\r\n } else if (contentType.includes(\"text/\")) {\r\n return response.text();\r\n } else {\r\n return response.blob();\r\n }\r\n } catch (error) {\r\n // Wrap JSON parsing errors\r\n throw new BrpcClientError(\r\n `Failed to parse response: ${\r\n error instanceof Error ? error.message : \"Unknown error\"\r\n }`,\r\n response.status,\r\n \"PARSE_ERROR\",\r\n );\r\n }\r\n };\r\n\r\n // Wrap fetch AND response handling to always return BrpcClientError\r\n const safeFetch = async (url: string | URL, init?: RequestInit) => {\r\n try {\r\n const response = await fetchImpl(url, init);\r\n return await handleResponse(response); // This throws BrpcClientError if not ok\r\n } catch (error) {\r\n // If already a BrpcClientError (from handleResponse), re-throw as-is\r\n if (error instanceof BrpcClientError) {\r\n throw error;\r\n }\r\n\r\n // Network errors - wrap them\r\n throw new BrpcClientError(\r\n error instanceof Error ? error.message : \"Network request failed\",\r\n 0,\r\n \"NETWORK_ERROR\",\r\n );\r\n }\r\n };\r\n\r\n // Create a proxy for a specific path\r\n const createProxy = (pathSegments: string[] = []): any => {\r\n return new Proxy(\r\n {},\r\n {\r\n get(_, key: string | symbol) {\r\n if (typeof key === \"symbol\") return undefined;\r\n\r\n // Special methods for procedures\r\n if (\r\n key === \"query\" ||\r\n key === \"mutation\" ||\r\n key === \"formMutation\" ||\r\n key === \"subscription\" ||\r\n key === \"getStringKey\" ||\r\n key === \"getArrayKey\" ||\r\n key === \"getNoInputsArrayKey\"\r\n ) {\r\n // The path up to this point is the procedure path\r\n const procedurePath = pathSegments.join(\"/\");\r\n const fullUrl = `${formattedBaseUrl}${proceduresPrefixedPath}/${procedurePath}`;\r\n\r\n if (debug) {\r\n console.log(`BRPC ${key} procedure path: ${procedurePath}`);\r\n console.log(`BRPC ${key} full URL: ${fullUrl}`);\r\n }\r\n\r\n if (key === \"query\") {\r\n return async (input: any) => {\r\n // Get fresh headers for this request\r\n const headers = await resolveHeaders(true);\r\n\r\n if (debug) {\r\n console.log(`BRPC query request to ${procedurePath}`, {\r\n input,\r\n headers,\r\n });\r\n }\r\n\r\n const url = new URL(fullUrl);\r\n\r\n // Add input parameters to URL for GET request\r\n if (input && typeof input === \"object\") {\r\n Object.entries(input).forEach(([k, v]) => {\r\n if (v !== undefined) {\r\n url.searchParams.append(k, String(v));\r\n }\r\n });\r\n }\r\n\r\n const result = await safeFetch(url.toString(), {\r\n method: \"GET\",\r\n headers,\r\n credentials: \"include\", //always include credentials\r\n });\r\n\r\n if (debug) {\r\n console.log(\r\n `BRPC query response from ${procedurePath}:`,\r\n result,\r\n );\r\n }\r\n\r\n return result;\r\n };\r\n } else if (key === \"mutation\") {\r\n return async (input: any) => {\r\n // Get fresh headers for this request\r\n const headers = await resolveHeaders(true);\r\n\r\n if (debug) {\r\n console.log(`BRPC mutation request to ${procedurePath}`, {\r\n input,\r\n headers,\r\n });\r\n }\r\n\r\n const body = JSON.stringify(input);\r\n\r\n const result = await safeFetch(fullUrl, {\r\n method: \"POST\",\r\n headers,\r\n body,\r\n credentials: \"include\",\r\n });\r\n\r\n if (debug) {\r\n console.log(\r\n `BRPC mutation response from ${procedurePath}:`,\r\n result,\r\n );\r\n }\r\n\r\n return result;\r\n };\r\n } else if (key === \"formMutation\") {\r\n return async (input: Record<string, any>) => {\r\n // Get fresh headers for this request (no Content-Type for FormData)\r\n const headers = await resolveHeaders(false);\r\n\r\n if (debug) {\r\n console.log(`BRPC formMutation request to ${procedurePath}`, {\r\n input,\r\n headers,\r\n });\r\n }\r\n\r\n // Convert object input to FormData\r\n const formData = new FormData();\r\n\r\n // Helper function to append values to FormData\r\n const appendToFormData = (\r\n key: string,\r\n value: any,\r\n parentKey?: string,\r\n ) => {\r\n const fullKey = parentKey ? `${parentKey}[${key}]` : key;\r\n\r\n if (value === null || value === undefined) {\r\n // Skip null/undefined values\r\n return;\r\n } else if (value instanceof File || value instanceof Blob) {\r\n // Handle files directly\r\n formData.append(fullKey, value);\r\n } else if (Array.isArray(value)) {\r\n // Handle arrays\r\n value.forEach((item, index) => {\r\n if (item instanceof File || item instanceof Blob) {\r\n // ✅ Append files with the same key (not indexed)\r\n formData.append(fullKey, item);\r\n } else if (typeof item === \"object\" && item !== null) {\r\n Object.entries(item).forEach(([subKey, subValue]) => {\r\n appendToFormData(\r\n subKey,\r\n subValue,\r\n `${fullKey}[${index}]`,\r\n );\r\n });\r\n } else {\r\n formData.append(`${fullKey}[${index}]`, String(item));\r\n }\r\n });\r\n } else if (typeof value === \"object\" && value !== null) {\r\n // Handle nested objects\r\n Object.entries(value).forEach(([subKey, subValue]) => {\r\n appendToFormData(subKey, subValue, fullKey);\r\n });\r\n } else {\r\n // Handle primitive values\r\n formData.append(fullKey, String(value));\r\n }\r\n };\r\n\r\n // Convert input object to FormData\r\n Object.entries(input).forEach(([key, value]) => {\r\n appendToFormData(key, value);\r\n });\r\n\r\n const result = await safeFetch(fullUrl, {\r\n method: \"POST\",\r\n headers,\r\n body: formData,\r\n credentials: \"include\",\r\n });\r\n\r\n if (debug) {\r\n console.log(\r\n `BRPC formMutation response from ${procedurePath}:`,\r\n result,\r\n );\r\n }\r\n\r\n return result;\r\n };\r\n } else if (key === \"subscription\") {\r\n return (callback: (data: any) => void) => {\r\n if (debug) {\r\n console.log(`BRPC subscription to ${procedurePath}`);\r\n }\r\n\r\n // Use the same pattern as fullUrl for consistency\r\n const topicPath = proceduresPrefixedPath\r\n ? `${proceduresPrefixedPath.slice(1)}/${procedurePath}` // Remove leading /\r\n : procedurePath;\r\n\r\n // Update WebSocket auth if needed before subscribing\r\n wsManager.updateAuth();\r\n\r\n // Return an object with unsubscribe and publish methods\r\n return wsManager.subscribe(topicPath, callback);\r\n };\r\n } else if (key === \"getStringKey\") {\r\n return (input?: any) => {\r\n // Create a deterministic key based on procedure path and input\r\n const baseKey = procedurePath;\r\n\r\n if (!input || Object.keys(input).length === 0) {\r\n return baseKey;\r\n }\r\n\r\n // Sort input keys for deterministic key generation\r\n const sortedInput = Object.keys(input)\r\n .sort()\r\n .reduce(\r\n (result, key) => {\r\n const value = input[key];\r\n if (value !== undefined && value !== null) {\r\n result[key] = value;\r\n }\r\n return result;\r\n },\r\n {} as Record<string, any>,\r\n );\r\n\r\n // Only add input hash if there are actual values\r\n if (Object.keys(sortedInput).length === 0) {\r\n return baseKey;\r\n }\r\n\r\n // Create a simple hash from the sorted input\r\n const inputString = JSON.stringify(sortedInput);\r\n return `${baseKey}?${inputString}`;\r\n };\r\n } else if (key === \"getArrayKey\") {\r\n return (\r\n input?: any,\r\n context?: Record<string, string | number | boolean>,\r\n ) => {\r\n const basePath = procedurePath;\r\n const merged = { ...(input ?? {}), ...(context ?? {}) };\r\n\r\n if (Object.keys(merged).length === 0) {\r\n return [basePath];\r\n }\r\n\r\n // Normalize and sort keys for deterministic key generation\r\n const normalizedPairs: Array<[string, any]> = [];\r\n\r\n Object.keys(merged)\r\n .sort()\r\n .forEach((key) => {\r\n const value = merged[key];\r\n if (value !== undefined && value !== null) {\r\n let normalizedValue = value;\r\n\r\n if (\r\n typeof value === \"boolean\" ||\r\n typeof value === \"number\"\r\n ) {\r\n normalizedValue = String(value);\r\n } else if (typeof value === \"object\") {\r\n normalizedValue = JSON.stringify(value);\r\n }\r\n\r\n normalizedPairs.push([key, normalizedValue]);\r\n }\r\n });\r\n\r\n if (normalizedPairs.length === 0) {\r\n return [basePath];\r\n }\r\n\r\n const inputSegments = normalizedPairs.flatMap(\r\n ([key, value]) => [key, value],\r\n );\r\n\r\n return [basePath, ...inputSegments];\r\n };\r\n } else if (key === \"getNoInputsArrayKey\") {\r\n return () => {\r\n // Always return just the procedure path, ignoring any inputs\r\n // Perfect for invalidating all variants of a procedure\r\n return [procedurePath];\r\n };\r\n }\r\n }\r\n\r\n // Continue traversing the path for nested routes\r\n return createProxy([...pathSegments, key]);\r\n },\r\n },\r\n );\r\n };\r\n\r\n // Create the base proxy\r\n const clientProxy = createProxy() as InferClientRoutes<TRoutesStructure>;\r\n\r\n if (debug) {\r\n console.log(\"BRPC client created\", {\r\n baseUrl: formattedBaseUrl,\r\n prefix: proceduresPrefixedPath,\r\n wsUrl: wsUrl,\r\n });\r\n }\r\n\r\n return {\r\n routes: clientProxy,\r\n\r\n storage: {\r\n getObjectUrl: (storageObject, opts) =>\r\n getObjectUrl(storageObject, opts, s3Endpoint!),\r\n },\r\n\r\n utils: {\r\n updateWsAuth: async () => await wsManager.updateAuth(),\r\n\r\n setHeader: async (key: string, value: string) => {\r\n if (typeof currentHeaders === \"function\") {\r\n console.warn(\r\n \"Cannot use setHeader with function-based headers resolver\",\r\n );\r\n return;\r\n }\r\n currentHeaders = { ...currentHeaders, [key]: value };\r\n await wsManager.updateAuth();\r\n },\r\n\r\n setHeaders: async (headers: Record<string, string>) => {\r\n if (typeof currentHeaders === \"function\") {\r\n console.warn(\r\n \"Cannot use setHeaders with function-based headers resolver\",\r\n );\r\n return;\r\n }\r\n currentHeaders = { ...currentHeaders, ...headers };\r\n await wsManager.updateAuth();\r\n },\r\n },\r\n };\r\n}\r\n"
10
- ],
11
- "mappings": ";AAUO,SAAS,CAAY,CAC1B,EACA,EAA8D,CAC5D,YAAa,OACb,eAAgB,EAClB,EACA,EACQ,CACR,GAAI,CAAC,EAAe,OAAO,GAAS,aAAe,cAGnD,GAAI,EAAc,IAChB,OAAO,EAAc,IAIvB,GAAI,EAAc,IAChB,GAAI,EAAc,WAAa,EAAc,WAAa,QAExD,MAAO,YAAY,EAAc,MAGjC,WAAO,GAAG,KAAc,EAAc,MAI1C,GAAI,EAAQ,eACV,MAAU,MAAM,wBAAwB,EAExC,YAAO,GAAS,aAAe,cCvCnC,MAAM,UAAwB,KAAM,CACzB,OACA,KACA,WACA,KAET,WAAW,CACT,EACA,EACA,EACA,EACA,EACA,CACA,MAAM,CAAO,EACb,KAAK,KAAO,kBACZ,KAAK,OAAS,EACd,KAAK,KAAO,EACZ,KAAK,WAAa,EAClB,KAAK,KAAO,EAId,aAAa,CAAC,EAA6B,CACzC,OAAO,KAAK,aAAe,EAI7B,cAAc,EAAY,CACxB,OAAO,KAAK,SAAW,IAGzB,WAAW,EAAY,CACrB,OAAO,KAAK,SAAW,IAGzB,UAAU,EAAY,CACpB,OAAO,KAAK,SAAW,IAGzB,iBAAiB,EAAY,CAC3B,OAAO,KAAK,SAAW,KAAO,KAAK,OAAS,cAEhD,CCvCO,SAAS,CAAW,CAAC,EAAiB,CAC3C,GAAI,CAAC,EAAQ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AC,CAAQ,EAGlB,IAAM,EAAgB,EAAgB,QACpC,EAAE,EAAK,KAAW,CAAC,EAAK,CAAK,CAC/B,EAEA,MAAO,CAAC,EAAU,GAAG,CAAa,GAE/B,QAAI,IAAQ,sBACjB,MAAO,IAAM,CAGX,MAAO,CAAC,CAAa,GAM3B,OAAO,EAAY,CAAC,GAAG,EAAc,CAAG,CAAC,EAE7C,CACF,GAII,EAAc,EAAY,EAEhC,GAAI,EACF,QAAQ,IAAI,sBAAuB,CACjC,QAAS,EACT,OAAQ,EACR,MAAO,CACT,CAAC,EAGH,MAAO,CACL,OAAQ,EAER,QAAS,CACP,aAAc,CAAC,EAAe,IAC5B,EAAa,EAAe,EAAM,CAAW,CACjD,EAEA,MAAO,CACL,aAAc,SAAY,MAAM,EAAU,WAAW,EAErD,UAAW,MAAO,EAAa,IAAkB,CAC/C,GAAI,OAAO,IAAmB,WAAY,CACxC,QAAQ,KACN,2DACF,EACA,OAEF,EAAiB,IAAK,GAAiB,GAAM,CAAM,EACnD,MAAM,EAAU,WAAW,GAG7B,WAAY,MAAO,IAAoC,CACrD,GAAI,OAAO,IAAmB,WAAY,CACxC,QAAQ,KACN,4DACF,EACA,OAEF,EAAiB,IAAK,KAAmB,CAAQ,EACjD,MAAM,EAAU,WAAW,EAE/B,CACF",
12
- "debugId": "5DCF873B33A21E1164756E2164756E21",
13
- "names": []
14
- }
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["..\\src\\client\\react\\use-subscription.tsx"],
4
- "sourcesContent": [
5
- "import { useCallback, useEffect, useRef } from \"react\";\r\n\r\n/**\r\n * Custom hook for managing brpc subscriptions\r\n * @param subscription A function that sets up a subscription and returns publish/unsubscribe handlers\r\n * @param callback Optional callback function to handle incoming data\r\n * @returns A stable publish function that can be called directly\r\n */\r\nfunction useSubscription<TInput, TOutput>(\r\n subscription: (callback: (data: TOutput) => void) => {\r\n publish: (data: TInput) => void;\r\n unsubscribe: () => void;\r\n },\r\n callback?: (data: TOutput) => void\r\n) {\r\n // Create a ref to store the publish function\r\n const publishRef = useRef<((data: TInput) => void) | null>(null);\r\n\r\n useEffect(() => {\r\n // Set up the subscription with the provided or default callback\r\n const { publish, unsubscribe } = subscription(\r\n callback || ((data) => console.log(\"New data received\", data))\r\n );\r\n\r\n // Store the publish function in the ref\r\n publishRef.current = publish;\r\n\r\n // Clean up the subscription when the component unmounts\r\n return () => {\r\n publishRef.current = null;\r\n unsubscribe();\r\n };\r\n }, [subscription, callback]);\r\n\r\n // Create a stable function that delegates to the current ref value\r\n const stablePublish = useCallback((data: TInput) => {\r\n publishRef.current?.(data);\r\n }, []);\r\n\r\n return { publish: stablePublish };\r\n}\r\n\r\nexport { useSubscription };\r\n"
6
- ],
7
- "mappings": ";AAAA,sBAAS,eAAa,YAAW,cAQjC,SAAS,CAAgC,CACvC,EAIA,EACA,CAEA,IAAM,EAAa,EAAwC,IAAI,EAuB/D,OArBA,EAAU,IAAM,CAEd,IAAQ,UAAS,eAAgB,EAC/B,IAAa,CAAC,IAAS,QAAQ,IAAI,oBAAqB,CAAI,EAC9D,EAMA,OAHA,EAAW,QAAU,EAGd,IAAM,CACX,EAAW,QAAU,KACrB,EAAY,IAEb,CAAC,EAAc,CAAQ,CAAC,EAOpB,CAAE,QAJa,EAAY,CAAC,IAAiB,CAClD,EAAW,UAAU,CAAI,GACxB,CAAC,CAAC,CAE2B",
8
- "debugId": "12418FDC428A9F4964756E2164756E21",
9
- "names": []
10
- }
package/dist/index.js.map DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["..\\src\\procedure.ts", "..\\src\\router\\router.ts", "..\\src\\errors\\BRPCError.ts", "..\\src\\router\\RouteMatcher.ts", "..\\..\\..\\node_modules\\@mateosuarezdev\\headers-builder\\dist\\index.js", "..\\src\\stream\\media.ts", "..\\src\\context.ts", "..\\src\\schemas.ts", "..\\src\\middlewares\\pathblocker\\index.ts", "..\\src\\middlewares\\ratelimiter\\index.ts", "..\\src\\middlewares\\createMiddleware.ts", "..\\src\\utils\\measure.ts"],
4
- "sourcesContent": [
5
- "import { z } from \"zod\";\r\nimport type {\r\n BaseContext,\r\n Handler,\r\n InferInput,\r\n Procedure,\r\n StreamableResponse,\r\n} from \"./types\";\r\nimport type { BunFile } from \"bun\";\r\n\r\nclass ProcedureBuilder<C extends BaseContext> {\r\n private middlewares: ((ctx: any) => Promise<Record<string, any> | void>)[] = [];\r\n\r\n constructor(middlewares: ((ctx: any) => Promise<Record<string, any> | void>)[] = []) {\r\n this.middlewares = middlewares;\r\n }\r\n\r\n use<M extends (ctx: C) => Promise<Record<string, any> | void>>(\r\n middleware: M\r\n ): ProcedureBuilder<\r\n Awaited<ReturnType<M>> extends Record<string, any>\r\n ? C & Awaited<ReturnType<M>>\r\n : C\r\n > {\r\n return new ProcedureBuilder<any>([...this.middlewares, middleware]);\r\n }\r\n\r\n input<I extends z.ZodType>(schema: I) {\r\n return new InputProcedureBuilder<C, I>(this.middlewares, schema);\r\n }\r\n\r\n query<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"query\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"query\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n mutation<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"mutation\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"mutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n formMutation<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"formMutation\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"formMutation\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n //TODO\r\n // formMutation\r\n // streamMutation\r\n // streamQuery? Is even possible?\r\n\r\n subscription<O>(\r\n handler: Handler<C, {}, O>\r\n ): Procedure<C, z.ZodObject<{}>, O, \"subscription\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"subscription\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n file(\r\n handler: Handler<C, {}, BunFile | Response>\r\n ): Procedure<C, z.ZodObject<{}>, BunFile | Response, \"file\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as BunFile | Response,\r\n _ctx: null as unknown as C,\r\n _type: \"file\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n // NEW: Video streaming file handler using existing Handler type\r\n fileStream(\r\n handler: Handler<C, {}, Blob | string>\r\n ): Procedure<\r\n C,\r\n z.ZodObject<{}>,\r\n StreamableResponse | Blob | string,\r\n \"fileStream\"\r\n > {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as StreamableResponse | Blob | string,\r\n _ctx: null as unknown as C,\r\n _type: \"fileStream\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n html(\r\n handler: Handler<C, {}, string>\r\n ): Procedure<C, z.ZodObject<{}>, string, \"html\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as string,\r\n _ctx: null as unknown as C,\r\n _type: \"html\",\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n}\r\n\r\nclass InputProcedureBuilder<C extends BaseContext, I extends z.ZodType> {\r\n constructor(\r\n private middlewares: ((ctx: any) => Promise<Record<string, any> | void>)[],\r\n private inputSchema: I,\r\n private timeoutMs?: number\r\n ) {}\r\n\r\n /**\r\n * Set a custom timeout for this procedure (in milliseconds).\r\n * Overrides the default router timeout.\r\n */\r\n timeout(ms: number) {\r\n return new InputProcedureBuilder<C, I>(\r\n this.middlewares,\r\n this.inputSchema,\r\n ms\r\n );\r\n }\r\n\r\n query<O>(handler: Handler<C, InferInput<I>, O>): Procedure<C, I, O, \"query\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"query\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n mutation<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"mutation\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"mutation\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n formMutation<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"formMutation\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"formMutation\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n subscription<O>(\r\n handler: Handler<C, InferInput<I>, O>\r\n ): Procedure<C, I, O, \"subscription\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as O,\r\n _ctx: null as unknown as C,\r\n _type: \"subscription\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n file(\r\n handler: Handler<C, InferInput<I>, BunFile | Response>\r\n ): Procedure<C, I, BunFile | Response, \"file\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as BunFile | Response,\r\n _ctx: null as unknown as C,\r\n _type: \"file\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n // NEW: Video streaming file handler using existing Handler type\r\n fileStream(\r\n handler: Handler<C, InferInput<I>, StreamableResponse | Blob | string>\r\n ): Procedure<C, I, StreamableResponse | Blob | string, \"fileStream\"> {\r\n return {\r\n _input: this.inputSchema,\r\n _output: null as unknown as StreamableResponse | Blob | string,\r\n _ctx: null as unknown as C,\r\n _type: \"fileStream\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n\r\n html(\r\n handler: Handler<C, {}, string>\r\n ): Procedure<C, z.ZodObject<{}>, string, \"html\"> {\r\n return {\r\n _input: z.object({}),\r\n _output: null as unknown as string,\r\n _ctx: null as unknown as C,\r\n _type: \"html\",\r\n _timeout: this.timeoutMs,\r\n handler,\r\n middlewares: this.middlewares,\r\n };\r\n }\r\n}\r\n\r\nexport function createProcedure<C extends BaseContext>(\r\n _context: (req: Request) => Promise<C>\r\n): ProcedureBuilder<C>;\r\nexport function createProcedure<C extends BaseContext>(): ProcedureBuilder<C>;\r\nexport function createProcedure<C extends BaseContext>(_context?: any): ProcedureBuilder<C> {\r\n return new ProcedureBuilder<C>();\r\n}\r\n",
6
- "import type { BunFile, Server, ServerWebSocket, BunRequest } from \"bun\";\r\nimport type {\r\n BaseContext,\r\n InferInput,\r\n Procedure,\r\n ProcedureType,\r\n RouterConfig,\r\n Routes,\r\n} from \"../types\";\r\nimport { ZodError, z } from \"zod\";\r\nimport { BRPCError } from \"../errors/BRPCError\";\r\nimport { type RouteMatch, RouteMatcher } from \"./RouteMatcher\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\";\r\nimport {\r\n streamMediaInternal,\r\n type StreamableMediaResponse,\r\n} from \"../stream/media\";\r\n\r\ninterface WebSocketData {\r\n ctx: any;\r\n topics: Set<string>;\r\n lastActivity: number;\r\n}\r\n\r\n//====================\r\n// #region Class\r\n//====================\r\nexport class Router<C extends BaseContext> {\r\n private routes: Routes;\r\n private routeMatcher: RouteMatcher;\r\n private contextCreator: (req: Request) => Promise<C>;\r\n private prefix: string;\r\n private globalMiddlewares: ((ctx: C) => Promise<Record<string, any> | void>)[];\r\n private server: Server | null = null;\r\n private wsConfig: RouterConfig<C>[\"websocket\"];\r\n private integrations: RouterConfig<C>[\"integrations\"];\r\n private development = process.env.NODE_ENV !== \"production\";\r\n private debug = false;\r\n private allowedOrigins = [];\r\n private onError?: (\r\n error: Error,\r\n { req, route }: { req: Request; route: string },\r\n ) => void;\r\n\r\n // Connection management fields...\r\n private activeConnections: Set<ServerWebSocket<WebSocketData>> = new Set();\r\n private readonly maxConnections: number = parseInt(\r\n process.env.MAX_WS_CONNECTIONS ?? \"1000\",\r\n );\r\n private readonly connectionTimeout: number = parseInt(\r\n process.env.WS_TIMEOUT ?? \"30000\",\r\n );\r\n private readonly maxRequestSize: number = parseInt(\r\n process.env.MAX_REQUEST_SIZE ?? \"10485760\",\r\n );\r\n private readonly requestTimeout: number = parseInt(\r\n process.env.REQUEST_TIMEOUT ?? \"30000\",\r\n );\r\n\r\n constructor(config: RouterConfig<C>) {\r\n this.contextCreator = config.context;\r\n this.prefix = config.prefix ?? \"\";\r\n this.routes = config.routes;\r\n this.globalMiddlewares = config.globalMiddlewares ?? [];\r\n this.wsConfig = config.websocket ?? {};\r\n this.integrations = config.integrations;\r\n this.debug = !!config.debug && process.env.NODE_ENV === \"development\";\r\n this.onError = config.onError;\r\n\r\n // Initialize the route matcher\r\n this.routeMatcher = new RouteMatcher(config.routes, {\r\n enableCaching: !this.development, // Disable cache in dev for hot reloading\r\n maxCacheSize: parseInt(process.env.ROUTE_CACHE_SIZE ?? \"1000\"),\r\n debug: this.debug,\r\n });\r\n\r\n // Start periodic cleanup\r\n if (!this.development) {\r\n setInterval(() => this.cleanupStaleConnections(), 60000);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"Router initialized with route matcher\");\r\n console.log(\"Route cache stats:\", this.routeMatcher.getCacheStats());\r\n }\r\n }\r\n\r\n /**\r\n * Simplified route finding using RouteMatcher\r\n */\r\n private findRoute(pathParts: string[]): RouteMatch | null {\r\n return this.routeMatcher.match(pathParts);\r\n }\r\n\r\n /**\r\n * Parses and validates the request input\r\n */\r\n private async parseInput(\r\n req: Request,\r\n schema: z.ZodType,\r\n routeType: ProcedureType,\r\n ): Promise<any> {\r\n let input: any = {};\r\n const url = new URL(req.url);\r\n const queryParams = Object.fromEntries(url.searchParams);\r\n const method = req.method;\r\n\r\n // Check request size for POST requests\r\n if (method === \"POST\") {\r\n const contentLength = parseInt(req.headers.get(\"content-length\") || \"0\");\r\n if (contentLength > this.maxRequestSize) {\r\n throw new Error(\"Request payload too large\");\r\n }\r\n }\r\n\r\n // Handle different procedure types and HTTP methods\r\n if (\r\n method === \"GET\" &&\r\n [\"query\", \"streamQuery\", \"file\", \"fileStream\", \"html\"].includes(routeType)\r\n ) {\r\n input = queryParams;\r\n } else if (\r\n method === \"POST\" &&\r\n [\"mutation\", \"formMutation\", \"streamMutation\"].includes(routeType)\r\n ) {\r\n const contentType = req.headers.get(\"content-type\");\r\n if (contentType?.includes(\"application/json\")) {\r\n input = await req.json();\r\n } else if (contentType?.includes(\"multipart/form-data\")) {\r\n const formData = await req.formData();\r\n //todo investigate about this deprecation\r\n //@ts-ignore\r\n input = await this.parseFormData(formData);\r\n } else {\r\n throw new Error(\"Unsupported content type\");\r\n }\r\n } else {\r\n throw new Error(\r\n `Unsupported request method ${method} for procedure type ${routeType}`,\r\n );\r\n }\r\n\r\n return schema.parse(input);\r\n }\r\n\r\n /**\r\n * Helper method to parse form data\r\n */\r\n private async parseFormData(\r\n formData: FormData,\r\n ): Promise<Record<string, any>> {\r\n const result: Record<string, any> = {};\r\n const fileGroups: Record<string, any[]> = {};\r\n\r\n formData.forEach(async (value, key) => {\r\n if (value instanceof File) {\r\n // Initialize array for this file key if it doesn't exist\r\n if (!fileGroups[key]) {\r\n fileGroups[key] = [];\r\n }\r\n fileGroups[key].push(value);\r\n } else if (typeof value === \"string\") {\r\n // Try to parse JSON strings\r\n try {\r\n result[key] = JSON.parse(value);\r\n } catch {\r\n // If not JSON, keep as string\r\n result[key] = value;\r\n }\r\n }\r\n });\r\n\r\n // Process file groups - ALWAYS return as array\r\n for (const [key, files] of Object.entries(fileGroups)) {\r\n result[key] = files; // ✅ Always an array, even if single file\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * handleError\r\n * @description global error handling for\r\n * each incoming request\r\n */\r\n private handleError(error: any, req: Request): Response {\r\n // Fire error logging with the REAL error (before any mapping)\r\n if (this.onError) {\r\n try {\r\n this.onError(error, { req, route: req.url });\r\n } catch (logError) {\r\n console.error(\"Error logger failed:\", logError);\r\n }\r\n }\r\n\r\n // Handle Zod validation errors\r\n if (\r\n error?.errors &&\r\n Array.isArray(error.errors) &&\r\n error.name === \"ZodError\"\r\n ) {\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n name: \"ValidationError\",\r\n code: \"BAD_REQUEST\",\r\n clientCode: \"VALIDATION_ERROR\",\r\n message: \"Input validation failed\",\r\n data: {\r\n validationErrors: error.errors.map((err: any) => ({\r\n path: err.path.join(\".\"),\r\n message: err.message,\r\n code: err.code,\r\n received: err.received,\r\n })),\r\n },\r\n httpStatus: 400,\r\n },\r\n }),\r\n {\r\n status: 400,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n },\r\n );\r\n }\r\n\r\n // Handle BRPCError\r\n if (error instanceof BRPCError) {\r\n return new Response(JSON.stringify({ error: error.toJSON() }), {\r\n status: error.httpStatus,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Handle generic Error instances with special messages (backward compatibility)\r\n if (error instanceof Error) {\r\n const status =\r\n error.message === \"Not Found\"\r\n ? 404\r\n : error.message === \"Unauthorized\"\r\n ? 401\r\n : error.message === \"Forbidden\"\r\n ? 403\r\n : error.message === \"Request payload too large\"\r\n ? 413\r\n : error.message === \"Request timeout\"\r\n ? 504 // Use 504 Gateway Timeout instead of 408 to prevent browser auto-retry\r\n : 500;\r\n\r\n const errorCode =\r\n status === 404\r\n ? \"NOT_FOUND\"\r\n : status === 401\r\n ? \"UNAUTHORIZED\"\r\n : status === 403\r\n ? \"FORBIDDEN\"\r\n : status === 413\r\n ? \"PAYLOAD_TOO_LARGE\"\r\n : status === 504\r\n ? \"GATEWAY_TIMEOUT\"\r\n : \"INTERNAL_SERVER_ERROR\";\r\n\r\n // ✅ Safe message for client (never expose internal errors)\r\n const clientMessage =\r\n status < 500\r\n ? error.message // 4xx errors are safe (client's fault)\r\n : \"Internal Server Error\"; // 5xx errors hide details\r\n\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: errorCode,\r\n message: clientMessage,\r\n },\r\n }),\r\n {\r\n status,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n },\r\n );\r\n }\r\n\r\n // Fallback for unknown errors\r\n return new Response(\r\n JSON.stringify({\r\n error: {\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message: \"Internal Server Error\", // Generic safe message\r\n },\r\n }),\r\n {\r\n status: 500,\r\n headers: buildHeaders()\r\n .contentType(\"json\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n },\r\n );\r\n }\r\n\r\n /**\r\n * handles the default cache control headers for file routes\r\n */\r\n private handleFileCacheControl(contentType: string) {\r\n let cacheControl: string;\r\n if (contentType?.includes(\"text/html\")) {\r\n cacheControl = \"public, max-age=0, must-revalidate\";\r\n } else if (\r\n contentType?.includes(\"text/css\") ||\r\n contentType?.includes(\"text/javascript\") ||\r\n contentType?.includes(\"application/javascript\")\r\n ) {\r\n cacheControl = \"public, max-age=31536000, immutable\";\r\n } else {\r\n cacheControl = \"public, max-age=86400, stale-while-revalidate=604800\";\r\n }\r\n return cacheControl;\r\n }\r\n\r\n /**\r\n * Handles the behavior of each request\r\n */\r\n private async handleRequest(\r\n req: Request,\r\n server?: Server,\r\n ): Promise<Response> {\r\n try {\r\n // Handle OPTIONS preflight requests without timeout\r\n if (req.method === \"OPTIONS\") {\r\n return new Response(null, {\r\n status: 204,\r\n headers: buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const url = new URL(req.url);\r\n let path = url.pathname;\r\n path = path.startsWith(this.prefix)\r\n ? path.slice(this.prefix.length)\r\n : path;\r\n path = path.startsWith(\"/\") ? path : \"/\" + path;\r\n\r\n // Handle WebSocket upgrade without timeout\r\n if (path === \"/ws\" && server) {\r\n if (this.debug) {\r\n console.log(\"🔌 WebSocket upgrade requested\");\r\n }\r\n return this.handleWebSocketUpgrade(req, server);\r\n }\r\n\r\n // Handle root path specially, not returning two parts [\"\",\"\"]\r\n const pathParts = path === \"/\" ? [] : path.split(\"/\").filter(Boolean);\r\n\r\n // Use the optimized route matcher\r\n const routeResult = this.findRoute(pathParts);\r\n\r\n if (!routeResult) {\r\n return new Response(\"Not Found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const { route } = routeResult;\r\n\r\n // Determine timeout: use procedure-specific timeout or fall back to default\r\n const timeout = route._timeout ?? this.requestTimeout;\r\n\r\n const timeoutPromise = new Promise<Response>((_, reject) => {\r\n setTimeout(() => reject(new Error(\"Request timeout\")), timeout);\r\n });\r\n\r\n return await Promise.race([\r\n this._handleRequest(req, server, routeResult),\r\n timeoutPromise,\r\n ]);\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n private async _handleRequest(\r\n req: Request,\r\n server?: Server,\r\n routeResult?: RouteMatch | null,\r\n ): Promise<Response> {\r\n try {\r\n // routeResult is now passed in from handleRequest\r\n if (!routeResult) {\r\n return new Response(\"Not Found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (!route.handler) {\r\n return new Response(\"Handler not implemented\", {\r\n status: 501,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Simple headers storage\r\n const customHeaders: Headers = new Headers();\r\n\r\n // Create context with all the properties from BaseContext\r\n const ctx = await this.contextCreator(req);\r\n Object.assign(ctx, {\r\n req,\r\n params,\r\n publishToProcedure: async <\r\n P extends Procedure<any, any, any, \"subscription\">,\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params: Record<string, string>,\r\n ) => {\r\n await this.publishToProcedure(procedure, input, ctx, params);\r\n },\r\n headers: customHeaders,\r\n });\r\n\r\n // Run global middlewares\r\n for (const middleware of this.globalMiddlewares) {\r\n const ext = await middleware(ctx);\r\n if (ext !== null && ext !== undefined && typeof ext === \"object\") {\r\n Object.assign(ctx, ext);\r\n }\r\n }\r\n\r\n // Run procedure-specific middlewares\r\n for (const middleware of route.middlewares) {\r\n const ext = await middleware(ctx);\r\n if (ext !== null && ext !== undefined && typeof ext === \"object\") {\r\n Object.assign(ctx, ext);\r\n }\r\n }\r\n\r\n // Parse input based on procedure type\r\n const input = await this.parseInput(req, route._input, route._type);\r\n const result = await route.handler({ ctx, input });\r\n\r\n const corsHeaders = buildHeaders()\r\n .cors({ origin: getCorsOrigin(req, this.allowedOrigins) })\r\n .build();\r\n\r\n Object.keys(corsHeaders).forEach((key) => {\r\n customHeaders.set(key, corsHeaders[key]);\r\n });\r\n\r\n // Handle different response types based on procedure type\r\n return await this.handleProcedureResponse(\r\n route._type,\r\n result,\r\n customHeaders,\r\n );\r\n } catch (error) {\r\n return this.handleError(error, req);\r\n }\r\n }\r\n\r\n /**\r\n * @description Entry point for testing router behavior without starting a server.\r\n * This method processes a request through the full routing pipeline including\r\n * middlewares, validation, and handlers.\r\n *\r\n * @example\r\n * ```typescript\r\n * const request = new Request(\"http://localhost/api/users\", {\r\n * method: \"POST\",\r\n * body: JSON.stringify({ name: \"John\" })\r\n * });\r\n * const response = await router.testRequest(request);\r\n * expect(response.status).toBe(200);\r\n * ```\r\n */\r\n public async testRequest(req: Request): Promise<Response> {\r\n return this.handleRequest(req);\r\n }\r\n\r\n /**\r\n * Handle responses based on procedure type\r\n */\r\n private async handleProcedureResponse(\r\n procedureType: ProcedureType,\r\n result: any,\r\n customHeaders: Headers,\r\n // corsHeaders: Record<string, string>,\r\n ): Promise<Response> {\r\n switch (procedureType) {\r\n case \"file\":\r\n if (result instanceof Response) {\r\n return result;\r\n }\r\n const contentType = (result as BunFile).type;\r\n customHeaders.append(\"Content-Type\", contentType);\r\n customHeaders.append(\r\n \"Cache-Control\",\r\n this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(contentType),\r\n );\r\n return new Response(result, {\r\n headers: customHeaders,\r\n });\r\n\r\n case \"fileStream\":\r\n if (\r\n typeof result === \"object\" &&\r\n result !== null &&\r\n \"type\" in result &&\r\n result.type === \"stream\"\r\n ) {\r\n const mediaStreamResult = result as StreamableMediaResponse;\r\n return await streamMediaInternal(\r\n result.file,\r\n result.request,\r\n this.allowedOrigins,\r\n mediaStreamResult.options,\r\n );\r\n }\r\n // Fallback to regular file response\r\n const streamContentType = (result as Blob).type;\r\n customHeaders.append(\"Content-Type\", streamContentType);\r\n customHeaders.append(\r\n \"Cache-Control\",\r\n this.development\r\n ? \"public, max-age=0, must-revalidate\"\r\n : this.handleFileCacheControl(streamContentType),\r\n );\r\n return new Response(result, {\r\n headers: customHeaders,\r\n });\r\n\r\n case \"html\":\r\n // Handle HTML rendering\r\n if (typeof result === \"string\") {\r\n customHeaders.append(\"Content-Type\", \"text/html\");\r\n return new Response(result, {\r\n headers: customHeaders,\r\n });\r\n }\r\n break;\r\n\r\n case \"query\":\r\n case \"mutation\":\r\n case \"formMutation\":\r\n case \"streamQuery\":\r\n case \"streamMutation\":\r\n case \"subscription\":\r\n default:\r\n // Standard JSON response\r\n customHeaders.append(\"Content-Type\", \"application/json\");\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: customHeaders,\r\n });\r\n }\r\n\r\n // Fallback\r\n customHeaders.append(\"Content-Type\", \"application/json\");\r\n return new Response(JSON.stringify({ data: result }), {\r\n headers: customHeaders,\r\n });\r\n }\r\n\r\n //====================\r\n // #region Websockets\r\n //====================\r\n private cleanupStaleConnections() {\r\n const now = Date.now();\r\n for (const ws of this.activeConnections) {\r\n if (now - ws.data.lastActivity > this.connectionTimeout) {\r\n ws.close();\r\n this.activeConnections.delete(ws);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handleWebSocketUpgrade\r\n * @description upgrades the incoming request to\r\n * a websocket connection\r\n */\r\n private async handleWebSocketUpgrade(\r\n req: Request,\r\n server: Server,\r\n ): Promise<Response> {\r\n if (this.activeConnections.size >= this.maxConnections) {\r\n return new Response(\"Too many connections\", { status: 503 });\r\n }\r\n\r\n const ctx = await this.contextCreator(req);\r\n Object.assign(ctx, { req });\r\n for (const middleware of this.globalMiddlewares) {\r\n const ext = await middleware(ctx);\r\n if (ext !== null && ext !== undefined && typeof ext === \"object\") {\r\n Object.assign(ctx, ext);\r\n }\r\n }\r\n\r\n const success = server.upgrade<WebSocketData>(req, {\r\n data: {\r\n ctx,\r\n topics: new Set<string>(),\r\n lastActivity: Date.now(),\r\n },\r\n });\r\n\r\n if (success) {\r\n return new Response(null, { status: 101 }); // Switching Protocols\r\n }\r\n\r\n return new Response(\"WebSocket upgrade failed\", { status: 500 });\r\n }\r\n\r\n /**\r\n * Handles WebSocket authentication\r\n */\r\n private async handleAuthenticate(\r\n ws: ServerWebSocket<WebSocketData>,\r\n token: string,\r\n _ctx: C,\r\n ) {\r\n try {\r\n // Extract token from \"Bearer xxx\" format if needed\r\n const finalToken = token.startsWith(\"Bearer \")\r\n ? token.substring(7)\r\n : token;\r\n\r\n // Store the token in the WebSocket context\r\n ws.data.ctx.authToken = finalToken;\r\n\r\n // Additionally, we can verify the token and update the session\r\n // For example, with Supabase:\r\n // const { data, error } = await supabase.auth.getUser(finalToken);\r\n // if (error) throw new Error(error.message);\r\n // ws.data.ctx.session = { user: data.user };\r\n\r\n // Send success response\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_success\",\r\n message: \"Authentication successful\",\r\n }),\r\n );\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket authenticated successfully\");\r\n }\r\n } catch (error) {\r\n ws.send(\r\n JSON.stringify({\r\n type: \"auth_error\",\r\n error:\r\n error instanceof Error ? error.message : \"Authentication failed\",\r\n }),\r\n );\r\n\r\n if (this.debug) {\r\n console.error(\"WebSocket authentication error:\", error);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * handles the ws client topic subscription\r\n */\r\n private async handleSubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C,\r\n ) {\r\n // Find route first to validate subscription\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.subscribe(topic);\r\n ws.data.topics.add(topic);\r\n }\r\n\r\n /**\r\n * handles the ws client topic unsubscription\r\n */\r\n private async handleUnsubscribe(\r\n ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n _ctx: C,\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult || routeResult.route._type !== \"subscription\") {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n ws.unsubscribe(topic);\r\n ws.data.topics.delete(topic);\r\n }\r\n\r\n /**\r\n * handles the message publication of a certain ws client\r\n */\r\n private async handlePublish(\r\n _ws: ServerWebSocket<WebSocketData>,\r\n topic: string,\r\n message: any,\r\n ctx: C,\r\n ) {\r\n const routeResult = this.findRoute(topic.split(\"/\").filter(Boolean));\r\n\r\n if (!routeResult) {\r\n throw new Error(\"Subscription not found\");\r\n }\r\n\r\n const { params, route } = routeResult;\r\n\r\n if (route._type !== \"subscription\" || !route.handler) {\r\n throw new Error(\"Invalid subscription route\");\r\n }\r\n\r\n Object.assign(ctx, { params });\r\n\r\n // Only validate if the procedure has defined input\r\n const validatedMessage = route._input.fields\r\n ? route._input.parse(message)\r\n : message;\r\n\r\n const publishMessage = await route.handler({\r\n ctx,\r\n input: validatedMessage,\r\n });\r\n\r\n const converted = {\r\n topic: topic,\r\n data: publishMessage,\r\n type: \"publish\",\r\n };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${topic}`, converted);\r\n }\r\n\r\n // this.server!.publish(topic, JSON.stringify(publishMessage));\r\n this.server!.publish(topic, JSON.stringify(converted));\r\n }\r\n\r\n private async publishToProcedure<\r\n P extends Procedure<any, any, any, \"subscription\">,\r\n >(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n ctx: C,\r\n params: Record<string, string>,\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params[paramName];\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { ...ctx, params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n async publish<P extends Procedure<any, any, any, \"subscription\">>(\r\n procedure: P,\r\n input: InferInput<P[\"_input\"]>,\r\n params?: Record<string, string>,\r\n ) {\r\n if (procedure._type !== \"subscription\" || !procedure.handler) {\r\n throw new Error(\"Can only publish to subscription procedures\");\r\n }\r\n\r\n const routePath = this.findRoutePath(this.routes, procedure);\r\n\r\n if (!routePath) {\r\n throw new Error(\"Subscription route not found\");\r\n }\r\n\r\n // Replace params in the path\r\n const finalPath = routePath\r\n .split(\"/\")\r\n .map((part) => {\r\n if (part.startsWith(\":\")) {\r\n const paramName = part.slice(1);\r\n const paramValue = params ? params[paramName] : null;\r\n if (!paramValue) {\r\n throw new Error(`Missing parameter: ${paramName}`);\r\n }\r\n return paramValue;\r\n }\r\n return part;\r\n })\r\n .join(\"/\");\r\n\r\n const result = await procedure.handler({\r\n ctx: { params },\r\n input,\r\n });\r\n\r\n const converted = { topic: finalPath, data: result, type: \"publish\" };\r\n\r\n if (this.debug) {\r\n console.log(`Server will publish to ${finalPath}`, converted);\r\n }\r\n\r\n // this.server?.publish(finalPath, JSON.stringify(result));\r\n this.server?.publish(finalPath, JSON.stringify(converted));\r\n return result;\r\n }\r\n\r\n /**\r\n * publish\r\n * @description publish a message to a topic from\r\n * the server itself\r\n */\r\n // private publish(topic: string, message: string | Uint8Array) {\r\n // if (this.server) {\r\n // this.server.publish(topic, message);\r\n // } else {\r\n // //?debug\r\n // //console.warn(\"Server not initialized. Cannot publish message.\");\r\n // }\r\n // }\r\n public freePublish<T>(topic: string, message: T) {\r\n const converted = { topic: topic, data: message, type: \"publish\" };\r\n this.server?.publish(topic, JSON.stringify(converted));\r\n }\r\n //====================\r\n // #endregion\r\n //====================\r\n\r\n //====================\r\n // #region Helpers\r\n //====================\r\n /**\r\n * findRoutePath\r\n * @description finds the full path to a procedure in the routes tree\r\n */\r\n private findRoutePath(\r\n routes: Routes,\r\n targetProcedure: Procedure<any, any, any, any>,\r\n prefix: string = \"\",\r\n ): string | null {\r\n for (const [key, value] of Object.entries(routes)) {\r\n const currentPath = prefix ? `${prefix}/${key}` : key;\r\n\r\n if (value === targetProcedure) {\r\n return currentPath;\r\n }\r\n\r\n if (value && typeof value === \"object\" && !(\"_type\" in value)) {\r\n const found = this.findRoutePath(\r\n value as Routes,\r\n targetProcedure,\r\n currentPath,\r\n );\r\n if (found) return found;\r\n }\r\n }\r\n\r\n return null;\r\n }\r\n\r\n private getIntegrationRoutes() {\r\n if (!this.integrations) return undefined;\r\n\r\n let routes: any = {}; // Initialize the routes object\r\n\r\n if (this.integrations.betterAuth) {\r\n const handler = this.integrations.betterAuth.handler;\r\n if (!handler) {\r\n throw new Error(\"Please provide a handler for betterAuth integration\");\r\n }\r\n routes[\"/api/auth/*\"] = async (req: BunRequest<\"auth\">) => {\r\n return await handler(req);\r\n };\r\n }\r\n\r\n if (this.integrations.rawRoutes) {\r\n Object.keys(this.integrations.rawRoutes).forEach((val) => {\r\n routes[val] = this.integrations!.rawRoutes![val];\r\n });\r\n }\r\n\r\n return Object.keys(routes).length > 0 ? routes : undefined;\r\n }\r\n\r\n /**\r\n * New utility methods for route management\r\n */\r\n public getRouteStats() {\r\n return {\r\n cache: this.routeMatcher.getCacheStats(),\r\n suggestions: this.routeMatcher.getOptimizationSuggestions(),\r\n };\r\n }\r\n\r\n public clearRouteCache() {\r\n this.routeMatcher.clearCache();\r\n if (this.debug) {\r\n console.log(\"[ROUTEMATCHER] Route cache cleared\");\r\n }\r\n }\r\n\r\n public benchmarkRoutes(\r\n testPaths: string[] = [\"/users/123\", \"/posts/456/comments\"],\r\n iterations = 1000,\r\n ) {\r\n if (this.debug) {\r\n const results = this.routeMatcher.benchmark(testPaths, iterations);\r\n console.log(\"[ROUTEMATCHER]\", \"Route matching benchmark:\", results);\r\n return results;\r\n }\r\n }\r\n\r\n /**\r\n * Hot reload support for development\r\n */\r\n public updateRoutes(newRoutes: Routes) {\r\n if (this.development) {\r\n this.routes = newRoutes;\r\n this.routeMatcher = new RouteMatcher(newRoutes, {\r\n enableCaching: false, // No caching in dev\r\n debug: this.debug,\r\n });\r\n console.log(\"[HMR]\", \"Server Routes updated\");\r\n } else {\r\n console.warn(\"Route updates are only allowed in development mode\");\r\n }\r\n }\r\n //====================\r\n // #endregion\r\n //====================\r\n\r\n //====================\r\n // #region Server\r\n //====================\r\n /**\r\n * Listen\r\n * @description starts up the Bun http server\r\n */\r\n get url(): string | null {\r\n return this.server?.url.toString() ?? null;\r\n }\r\n\r\n listen(port: number, callback?: () => void) {\r\n this.server = Bun.serve({\r\n port,\r\n routes: this.getIntegrationRoutes(),\r\n fetch: (req, server) => this.handleRequest(req, server),\r\n websocket: {\r\n open: (ws: ServerWebSocket<WebSocketData>) => {\r\n this.activeConnections.add(ws);\r\n if (this.wsConfig?.onOpen) {\r\n this.wsConfig.onOpen(ws, ws.data.ctx);\r\n }\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection opened\", {\r\n connections: this.activeConnections.size,\r\n });\r\n }\r\n },\r\n message: async (ws: ServerWebSocket<WebSocketData>, message: any) => {\r\n try {\r\n ws.data.lastActivity = Date.now();\r\n const { type, topic, token, data } = JSON.parse(message as string);\r\n // console.log(type, token);\r\n const ctx = ws.data.ctx;\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket message received:\", {\r\n type,\r\n topic,\r\n });\r\n }\r\n\r\n switch (type) {\r\n case \"authenticate\":\r\n await this.handleAuthenticate(ws, token, ctx);\r\n break;\r\n case \"subscribe\":\r\n await this.handleSubscribe(ws, topic, ctx);\r\n break;\r\n case \"unsubscribe\":\r\n await this.handleUnsubscribe(ws, topic, ctx);\r\n break;\r\n case \"publish\":\r\n await this.handlePublish(ws, topic, data, ctx);\r\n break;\r\n default:\r\n ws.send(JSON.stringify({ error: \"Unknown message type\" }));\r\n if (this.debug) {\r\n console.error(\"Unknown WebSocket message type:\", type);\r\n }\r\n }\r\n } catch (error) {\r\n if (this.debug) {\r\n console.error(\"[BRPC]\", \"WebSocket message error:\", error);\r\n }\r\n\r\n if (error instanceof ZodError) {\r\n ws.send(JSON.stringify({ error: \"Invalid message format\" }));\r\n } else if (error instanceof Error) {\r\n ws.send(JSON.stringify({ error: error.message }));\r\n }\r\n }\r\n },\r\n close: (\r\n ws: ServerWebSocket<WebSocketData>,\r\n code: number,\r\n reason: string,\r\n ) => {\r\n this.activeConnections.delete(ws);\r\n ws.data.topics.forEach((topic) => {\r\n ws.unsubscribe(topic);\r\n });\r\n\r\n if (this.debug) {\r\n console.log(\"[BRPC]\", \"WebSocket connection closed\", {\r\n code,\r\n reason,\r\n remainingConnections: this.activeConnections.size,\r\n });\r\n }\r\n\r\n if (this.wsConfig?.onClose) {\r\n this.wsConfig.onClose(ws, code, reason, ws.data.ctx);\r\n }\r\n },\r\n },\r\n });\r\n\r\n if (callback) callback();\r\n }\r\n //====================\r\n // #endregion\r\n //====================\r\n}\r\n//====================\r\n// #endregion Class\r\n//====================\r\n\r\n//====================\r\n// #region Exports\r\n//====================\r\n/**\r\n * createRouter\r\n * @description helper function to create a\r\n * Router instance\r\n */\r\nexport function createRouter<C extends BaseContext>(config: RouterConfig<C>) {\r\n return new Router(config);\r\n}\r\n\r\nexport type CreateRouter = ReturnType<typeof createRouter>;\r\n//====================\r\n// #endregion\r\n//====================\r\n",
7
- "export type BRPCErrorCode =\r\n | \"BAD_REQUEST\"\r\n | \"UNAUTHORIZED\"\r\n | \"FORBIDDEN\"\r\n | \"NOT_FOUND\"\r\n | \"METHOD_NOT_SUPPORTED\"\r\n | \"TIMEOUT\"\r\n | \"CONFLICT\"\r\n | \"PRECONDITION_FAILED\"\r\n | \"PAYLOAD_TOO_LARGE\"\r\n | \"UNPROCESSABLE_CONTENT\"\r\n | \"TOO_MANY_REQUESTS\"\r\n | \"CLIENT_CLOSED_REQUEST\"\r\n | \"INTERNAL_SERVER_ERROR\"\r\n | \"NOT_IMPLEMENTED\"\r\n | \"BAD_GATEWAY\"\r\n | \"SERVICE_UNAVAILABLE\"\r\n | \"GATEWAY_TIMEOUT\";\r\n\r\nexport interface BRPCErrorOptions {\r\n code: BRPCErrorCode;\r\n message: string;\r\n clientCode?: string; // Custom code for client-side detection\r\n cause?: Error;\r\n data?: Record<string, any>;\r\n}\r\n\r\nexport class BRPCError extends Error {\r\n public readonly code: BRPCErrorCode;\r\n public readonly clientCode?: string;\r\n public readonly httpStatus: number;\r\n public readonly data?: Record<string, any>;\r\n public readonly cause?: Error;\r\n\r\n private static readonly STATUS_MAP: Record<BRPCErrorCode, number> = {\r\n BAD_REQUEST: 400,\r\n UNAUTHORIZED: 401,\r\n FORBIDDEN: 403,\r\n NOT_FOUND: 404,\r\n METHOD_NOT_SUPPORTED: 405,\r\n TIMEOUT: 408,\r\n CONFLICT: 409,\r\n PRECONDITION_FAILED: 412,\r\n PAYLOAD_TOO_LARGE: 413,\r\n UNPROCESSABLE_CONTENT: 422,\r\n TOO_MANY_REQUESTS: 429,\r\n CLIENT_CLOSED_REQUEST: 499,\r\n INTERNAL_SERVER_ERROR: 500,\r\n NOT_IMPLEMENTED: 501,\r\n BAD_GATEWAY: 502,\r\n SERVICE_UNAVAILABLE: 503,\r\n GATEWAY_TIMEOUT: 504,\r\n };\r\n\r\n constructor(options: BRPCErrorOptions) {\r\n super(options.message);\r\n\r\n this.name = \"BRPCError\";\r\n this.code = options.code;\r\n this.clientCode = options.clientCode;\r\n this.httpStatus = BRPCError.STATUS_MAP[options.code];\r\n this.data = options.data;\r\n this.cause = options.cause;\r\n\r\n // Maintains proper stack trace for where our error was thrown (only available on V8)\r\n if (Error.captureStackTrace) {\r\n Error.captureStackTrace(this, BRPCError);\r\n }\r\n }\r\n\r\n /**\r\n * Create a serializable object for sending to frontend\r\n */\r\n toJSON() {\r\n return {\r\n name: this.name,\r\n code: this.code,\r\n clientCode: this.clientCode,\r\n message: this.message,\r\n data: this.data,\r\n httpStatus: this.httpStatus,\r\n };\r\n }\r\n\r\n /**\r\n * Static factory methods for common errors\r\n */\r\n static badRequest(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"BAD_REQUEST\", message, clientCode, data });\r\n }\r\n\r\n static unauthorized(\r\n message: string = \"Unauthorized\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"UNAUTHORIZED\", message, clientCode, data });\r\n }\r\n\r\n static forbidden(\r\n message: string = \"Forbidden\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"FORBIDDEN\", message, clientCode, data });\r\n }\r\n\r\n static notFound(\r\n message: string = \"Not Found\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static preconditionFailed(\r\n message: string = \"Precondition failed\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static conflict(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"CONFLICT\", message, clientCode, data });\r\n }\r\n\r\n static unprocessableContent(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"UNPROCESSABLE_CONTENT\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static tooManyRequests(\r\n message: string = \"Too many requests\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"TOO_MANY_REQUESTS\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static internalServerError(\r\n message: string = \"Internal Server Error\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static timeout(\r\n message: string = \"Request timeout\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"TIMEOUT\", message, clientCode, data });\r\n }\r\n}\r\n",
8
- "// TODO\r\n\r\n// Add regex pattern support\r\n// Benefits:\r\n\r\n// Validation at routing level - invalid formats don't even reach your handler\r\n// Cleaner code - no need to validate ID format in every handler\r\n// Better error messages - 404 vs validation error\r\n\r\n// Example:\r\n// {\r\n// \"users\": {\r\n// \":id(\\\\d+)\": { ... }, // only matches numeric IDs: /users/123 ✅, /users/abc ❌\r\n// \":slug([a-z-]+)\": { ... }, // only matches slugs: /users/my-post ✅, /users/My_Post ❌\r\n// \":uuid([0-9a-f]{8}-...)\": { ... }, // only UUIDs\r\n// },\r\n// \"posts\": {\r\n// \":year(\\\\d{4})\": {\r\n// \":month(\\\\d{2})\": {\r\n// \":day(\\\\d{2})\": { ... } // /posts/2024/01/30 ✅, /posts/24/1/3 ❌\r\n// }\r\n// }\r\n// },\r\n// \"files\": {\r\n// \"*.(jpg|png|gif)\": { ... }, // only image files\r\n// }\r\n// }\r\n\r\nimport type { Routes, Procedure, ProcedureType } from \"../types\";\r\n\r\ninterface RouteMatch {\r\n params: Record<string, string>;\r\n route: Procedure<any, any, any, any>;\r\n}\r\n\r\ninterface CompiledRoute {\r\n handler: Procedure<any, any, any, any>;\r\n paramNames: string[];\r\n hasWildcard: boolean;\r\n wildcardIndex?: number;\r\n}\r\n\r\ninterface RouteNode {\r\n // Static routes (exact match) - O(1) lookup with Map\r\n static: Map<string, RouteNode>;\r\n // Parameter route (single dynamic segment)\r\n param?: { name: string; node: RouteNode };\r\n // Wildcard route (catches everything)\r\n wildcard?: CompiledRoute;\r\n // Handler if this node is a terminal route\r\n handler?: CompiledRoute;\r\n // Index handler\r\n index?: CompiledRoute;\r\n}\r\n\r\ninterface RouteMatcherOptions {\r\n enableCaching?: boolean;\r\n maxCacheSize?: number;\r\n debug?: boolean;\r\n}\r\n\r\ninterface RouteStats {\r\n totalRoutes: number;\r\n staticRoutes: number;\r\n paramRoutes: number;\r\n wildcardRoutes: number;\r\n indexRoutes: number;\r\n maxDepth: number;\r\n procedureTypes: Record<ProcedureType, number>;\r\n}\r\n\r\n/**\r\n * Optimized route matcher for the BRPC Framework\r\n */\r\nexport class RouteMatcher {\r\n private routeTree: RouteNode;\r\n private routeCache: Map<string, RouteMatch | null>;\r\n private readonly options: Required<RouteMatcherOptions>;\r\n private routeStats: RouteStats;\r\n\r\n constructor(routes: Routes, options: RouteMatcherOptions = {}) {\r\n this.options = {\r\n enableCaching: true,\r\n maxCacheSize: 1000,\r\n debug: false,\r\n ...options,\r\n };\r\n\r\n this.routeCache = new Map();\r\n this.routeStats = this.analyzeRoutes(routes);\r\n this.routeTree = this.compileRoutes(routes);\r\n\r\n if (this.options.debug) {\r\n console.log(\"[ROUTEMATCHER]\", \"initialized with stats:\", this.routeStats);\r\n }\r\n }\r\n\r\n /**\r\n * Main route matching method\r\n */\r\n public match(pathParts: string[]): RouteMatch | null {\r\n if (this.options.enableCaching) {\r\n return this.matchWithCache(pathParts);\r\n }\r\n return this.matchDirect(pathParts);\r\n }\r\n\r\n /**\r\n * Route matching with caching\r\n */\r\n private matchWithCache(pathParts: string[]): RouteMatch | null {\r\n const cacheKey = pathParts.join(\"/\");\r\n\r\n // Check cache first\r\n if (this.routeCache.has(cacheKey)) {\r\n const cached = this.routeCache.get(cacheKey)!;\r\n if (this.options.debug && cached) {\r\n console.log(\"[ROUTEMATCHER]\", `🎯 Cache hit for route: /${cacheKey}`);\r\n }\r\n return cached;\r\n }\r\n\r\n // Find route\r\n const result = this.matchDirect(pathParts);\r\n\r\n // Cache the result (including null results to avoid repeated failed lookups)\r\n this.cacheResult(cacheKey, result);\r\n\r\n if (this.options.debug) {\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `💾 Cached route: /${cacheKey} -> ${result ? \"found\" : \"not found\"}`,\r\n );\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Direct route matching without caching\r\n */\r\n private matchDirect(pathParts: string[]): RouteMatch | null {\r\n const startTime = this.options.debug ? performance.now() : 0;\r\n\r\n const result = this.traverseTree(this.routeTree, pathParts, 0, {});\r\n\r\n if (this.options.debug) {\r\n const duration = performance.now() - startTime;\r\n const routeType = result?.route._type || \"none\";\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `⚡ Route match: /${pathParts.join(\r\n \"/\",\r\n )} (${routeType}) took ${duration.toFixed(3)}ms`,\r\n );\r\n }\r\n\r\n return result;\r\n }\r\n\r\n /**\r\n * Traverse the compiled route tree\r\n */\r\n private traverseTree(\r\n node: RouteNode,\r\n pathParts: string[],\r\n index: number,\r\n params: Record<string, string>,\r\n ): RouteMatch | null {\r\n // If we've consumed all path parts\r\n if (index >= pathParts.length) {\r\n // Try handler first, then index\r\n if (node.handler) {\r\n return { params, route: node.handler.handler };\r\n }\r\n if (node.index) {\r\n return { params, route: node.index.handler };\r\n }\r\n // Then only match wildcard at root if pathParts\r\n // is empty (root path) and there's no index handler defined\r\n //! This was the last change I made trying to fix\r\n //! Wildacrd not matching root (/) when index wasn't defined\r\n if (node.wildcard && pathParts.length === 0) {\r\n return {\r\n params: { ...params, wildcardPath: \"\" },\r\n route: node.wildcard.handler,\r\n };\r\n }\r\n return null;\r\n }\r\n\r\n const segment = pathParts[index];\r\n const decodedSegment = decodeURIComponent(segment);\r\n\r\n // 1. Try static route first (O(1) lookup with Map)\r\n const staticChild = node.static.get(decodedSegment);\r\n if (staticChild) {\r\n const result = this.traverseTree(\r\n staticChild,\r\n pathParts,\r\n index + 1,\r\n params,\r\n );\r\n if (result) return result;\r\n }\r\n\r\n // 2. Try parameter route\r\n if (node.param) {\r\n const newParams = { ...params, [node.param.name]: decodedSegment };\r\n const result = this.traverseTree(\r\n node.param.node,\r\n pathParts,\r\n index + 1,\r\n newParams,\r\n );\r\n if (result) return result;\r\n }\r\n\r\n // 3. Try wildcard route (catches all remaining segments including unmatched routes)\r\n if (node.wildcard) {\r\n const wildcardPath = pathParts.slice(index).join(\"/\");\r\n return {\r\n params: { ...params, wildcardPath },\r\n route: node.wildcard.handler,\r\n };\r\n }\r\n\r\n return null;\r\n }\r\n\r\n /**\r\n * Compile the routes object into an optimized tree structure\r\n */\r\n private compileRoutes(routes: Routes, paramNames: string[] = []): RouteNode {\r\n const node: RouteNode = {\r\n static: new Map(),\r\n };\r\n\r\n for (const [key, value] of Object.entries(routes)) {\r\n if (key === \"index\" && this.isProcedure(value)) {\r\n // Index route\r\n node.index = {\r\n handler: value,\r\n paramNames: [...paramNames],\r\n hasWildcard: false,\r\n };\r\n continue;\r\n }\r\n\r\n if (key === \"*\") {\r\n // Wildcard route\r\n if (this.isProcedure(value)) {\r\n node.wildcard = {\r\n handler: value,\r\n paramNames: [...paramNames, \"wildcardPath\"],\r\n hasWildcard: true,\r\n wildcardIndex: paramNames.length,\r\n };\r\n }\r\n continue;\r\n }\r\n\r\n if (this.isProcedure(value)) {\r\n // Terminal route with handler\r\n const compiledRoute: CompiledRoute = {\r\n handler: value,\r\n paramNames: [...paramNames],\r\n hasWildcard: false,\r\n };\r\n\r\n if (key.startsWith(\":\")) {\r\n // Parameter route\r\n const paramName = key.slice(1);\r\n const terminalNode: RouteNode = {\r\n static: new Map(),\r\n handler: compiledRoute,\r\n };\r\n node.param = { name: paramName, node: terminalNode };\r\n } else {\r\n // Static route\r\n const terminalNode: RouteNode = {\r\n static: new Map(),\r\n handler: compiledRoute,\r\n };\r\n node.static.set(key, terminalNode);\r\n }\r\n } else if (typeof value === \"object\" && value !== null) {\r\n // Nested routes\r\n if (key.startsWith(\":\")) {\r\n // Parameter segment with nested routes\r\n const paramName = key.slice(1);\r\n const childNode = this.compileRoutes(value as Routes, [\r\n ...paramNames,\r\n paramName,\r\n ]);\r\n node.param = { name: paramName, node: childNode };\r\n } else {\r\n // Static segment with nested routes\r\n const childNode = this.compileRoutes(value as Routes, paramNames);\r\n node.static.set(key, childNode);\r\n }\r\n }\r\n }\r\n\r\n return node;\r\n }\r\n\r\n /**\r\n * Type guard to check if value is a Procedure\r\n */\r\n private isProcedure(value: any): value is Procedure<any, any, any, any> {\r\n return (\r\n value &&\r\n typeof value === \"object\" &&\r\n \"_type\" in value &&\r\n \"_input\" in value\r\n );\r\n }\r\n\r\n /**\r\n * Cache management with LRU behavior\r\n */\r\n private cacheResult(key: string, result: RouteMatch | null): void {\r\n // Implement LRU: remove oldest entry if cache is full\r\n if (this.routeCache.size >= this.options.maxCacheSize) {\r\n const firstKey = this.routeCache.keys().next().value;\r\n if (firstKey !== undefined) {\r\n this.routeCache.delete(firstKey);\r\n\r\n if (this.options.debug) {\r\n console.log(`🗑️ Evicted oldest cache entry: ${firstKey}`);\r\n }\r\n }\r\n }\r\n\r\n this.routeCache.set(key, result);\r\n }\r\n\r\n /**\r\n * Analyze route structure for optimization insights and debugging\r\n */\r\n private analyzeRoutes(routes: Routes): RouteStats {\r\n const stats: RouteStats = {\r\n totalRoutes: 0,\r\n staticRoutes: 0,\r\n paramRoutes: 0,\r\n wildcardRoutes: 0,\r\n indexRoutes: 0,\r\n maxDepth: 0,\r\n procedureTypes: {\r\n query: 0,\r\n mutation: 0,\r\n subscription: 0,\r\n file: 0,\r\n fileStream: 0,\r\n streamQuery: 0,\r\n streamMutation: 0,\r\n formMutation: 0,\r\n html: 0,\r\n },\r\n };\r\n\r\n const traverse = (obj: any, depth = 0): void => {\r\n stats.maxDepth = Math.max(stats.maxDepth, depth);\r\n\r\n for (const [key, value] of Object.entries(obj)) {\r\n if (this.isProcedure(value)) {\r\n stats.totalRoutes++;\r\n\r\n // Count procedure types\r\n if (value._type in stats.procedureTypes) {\r\n stats.procedureTypes[value._type as ProcedureType]++;\r\n }\r\n\r\n // Count route patterns\r\n if (key === \"index\") {\r\n stats.indexRoutes++;\r\n } else if (key.startsWith(\":\")) {\r\n stats.paramRoutes++;\r\n } else if (key === \"*\") {\r\n stats.wildcardRoutes++;\r\n } else {\r\n stats.staticRoutes++;\r\n }\r\n } else if (typeof value === \"object\" && value !== null) {\r\n traverse(value, depth + 1);\r\n }\r\n }\r\n };\r\n\r\n traverse(routes);\r\n return stats;\r\n }\r\n\r\n /**\r\n * Utility methods\r\n */\r\n public clearCache(): void {\r\n this.routeCache.clear();\r\n if (this.options.debug) {\r\n console.log(\"[ROUTEMATCHER]\", \"🧹 Route cache cleared\");\r\n }\r\n }\r\n\r\n public getCacheStats(): {\r\n size: number;\r\n maxSize: number;\r\n hitRatio?: number;\r\n routeStats: RouteStats;\r\n } {\r\n return {\r\n size: this.routeCache.size,\r\n maxSize: this.options.maxCacheSize,\r\n routeStats: this.routeStats,\r\n };\r\n }\r\n\r\n /**\r\n * Get suggestions for route optimization\r\n */\r\n public getOptimizationSuggestions(): string[] {\r\n const suggestions: string[] = [];\r\n const { routeStats } = this;\r\n\r\n if (routeStats.paramRoutes > routeStats.staticRoutes * 2) {\r\n suggestions.push(\r\n \"🚀 Consider grouping routes to reduce parameter route overhead\",\r\n );\r\n }\r\n\r\n if (routeStats.maxDepth > 6) {\r\n suggestions.push(\r\n \"📊 Deep route nesting detected - consider flattening some routes\",\r\n );\r\n }\r\n\r\n if (routeStats.totalRoutes > 200) {\r\n suggestions.push(\r\n \"⚡ Large route table - consider route splitting or lazy loading\",\r\n );\r\n }\r\n\r\n if (!this.options.enableCaching && routeStats.totalRoutes > 50) {\r\n suggestions.push(\r\n \"💾 Enable caching for better performance with many routes\",\r\n );\r\n }\r\n\r\n if (routeStats.procedureTypes.file > 20) {\r\n suggestions.push(\r\n \"📁 Many file routes detected - consider static file serving\",\r\n );\r\n }\r\n\r\n if (routeStats.procedureTypes.subscription > 10) {\r\n suggestions.push(\r\n \"🔄 Many subscription routes - ensure WebSocket optimization\",\r\n );\r\n }\r\n\r\n return suggestions;\r\n }\r\n\r\n /**\r\n * Find all routes matching a pattern (useful for debugging)\r\n */\r\n public findRoutesByPattern(_pattern: string): string[] {\r\n const matchingRoutes: string[] = [];\r\n // const regex = new RegExp(pattern);\r\n\r\n // This would require storing all route paths during compilation\r\n // For now, return empty array - can be implemented if needed\r\n return matchingRoutes;\r\n }\r\n\r\n /**\r\n * Get route information for debugging\r\n */\r\n public getRouteInfo(path: string): {\r\n found: boolean;\r\n route?: Procedure<any, any, any, any>;\r\n params?: Record<string, string>;\r\n cached: boolean;\r\n matchTime?: number;\r\n } {\r\n const pathParts = path.split(\"/\").filter(Boolean);\r\n const cacheKey = pathParts.join(\"/\");\r\n const cached = this.routeCache.has(cacheKey);\r\n\r\n const startTime = performance.now();\r\n const result = this.matchDirect(pathParts);\r\n const matchTime = performance.now() - startTime;\r\n\r\n return {\r\n found: !!result,\r\n route: result?.route,\r\n params: result?.params,\r\n cached,\r\n matchTime,\r\n };\r\n }\r\n\r\n /**\r\n * Benchmark route matching performance\r\n */\r\n public benchmark(\r\n testPaths: string[],\r\n iterations = 1000,\r\n ): {\r\n averageTime: number;\r\n totalTime: number;\r\n routesPerSecond: number;\r\n cacheHitRatio?: number;\r\n } {\r\n const pathSegments = testPaths.map((path) =>\r\n path.split(\"/\").filter(Boolean),\r\n );\r\n\r\n // Clear cache for accurate benchmarking\r\n const originalCache = new Map(this.routeCache);\r\n this.routeCache.clear();\r\n\r\n const startTime = performance.now();\r\n\r\n for (let i = 0; i < iterations; i++) {\r\n for (const segments of pathSegments) {\r\n this.matchDirect(segments); // Use direct matching for pure performance\r\n }\r\n }\r\n\r\n const endTime = performance.now();\r\n const totalTime = endTime - startTime;\r\n const totalMatches = iterations * testPaths.length;\r\n const averageTime = totalTime / totalMatches;\r\n const routesPerSecond = (totalMatches / totalTime) * 1000;\r\n\r\n // Restore cache\r\n this.routeCache = originalCache;\r\n\r\n return {\r\n averageTime,\r\n totalTime,\r\n routesPerSecond,\r\n };\r\n }\r\n\r\n /**\r\n * Warm up the cache with common routes\r\n */\r\n public warmupCache(commonPaths: string[]): void {\r\n if (!this.options.enableCaching) return;\r\n\r\n const startTime = performance.now();\r\n let warmedRoutes = 0;\r\n\r\n for (const path of commonPaths) {\r\n const pathParts = path.split(\"/\").filter(Boolean);\r\n const result = this.matchDirect(pathParts);\r\n if (result) {\r\n this.cacheResult(pathParts.join(\"/\"), result);\r\n warmedRoutes++;\r\n }\r\n }\r\n\r\n const endTime = performance.now();\r\n\r\n if (this.options.debug) {\r\n console.log(\r\n \"[ROUTEMATCHER]\",\r\n `🔥 Cache warmed up: ${warmedRoutes} routes in ${(\r\n endTime - startTime\r\n ).toFixed(2)}ms`,\r\n );\r\n }\r\n }\r\n}\r\n\r\n// Export types for use in Router\r\nexport type { RouteMatch, RouteMatcherOptions, RouteStats };\r\n",
9
- "// @bun\nvar L=import.meta.require;var W={NO_CACHE:{cacheControl:\"no-cache, no-store, must-revalidate\",expires:new Date(0).toUTCString()},ONE_YEAR:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},IMMUTABLE:{cacheControl:\"public, max-age=31536000, immutable\",expires:new Date(Date.now()+31536000000).toUTCString()},ONE_MONTH:{cacheControl:\"public, max-age=2592000\"},ONE_WEEK:{cacheControl:\"public, max-age=604800\"},ONE_DAY:{cacheControl:\"public, max-age=86400, must-revalidate\"},ONE_HOUR:{cacheControl:\"public, max-age=3600, must-revalidate\"},FIVE_MINUTES:{cacheControl:\"public, max-age=300\"},API:{cacheControl:\"public, max-age=300, s-maxage=3600\"},STYLESHEET:{cacheControl:\"public, max-age=2592000, must-revalidate\"},JAVASCRIPT:{cacheControl:\"public, max-age=2592000, must-revalidate\"},HASHED_ASSET:{cacheControl:\"public, max-age=31536000, immutable\",expires:new Date(Date.now()+31536000000).toUTCString()},FONT:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},IMAGE:{cacheControl:\"public, max-age=2592000\"},FAVICON:{cacheControl:\"public, max-age=31536000\",expires:new Date(Date.now()+31536000000).toUTCString()},HTML_PAGE:{cacheControl:\"public, max-age=3600, must-revalidate\"},API_RESPONSE:{cacheControl:\"public, max-age=300, s-maxage=900\"},FEED:{cacheControl:\"public, max-age=3600\"},SITEMAP:{cacheControl:\"public, max-age=86400\"},MANIFEST:{cacheControl:\"public, max-age=86400, must-revalidate\"},SERVICE_WORKER:{cacheControl:\"no-cache, no-store, must-revalidate\",expires:new Date(0).toUTCString()},MEDIA_STREAM:{cacheControl:\"no-cache, no-store\"},DOCUMENT:{cacheControl:\"public, max-age=86400\"},ARCHIVE:{cacheControl:\"public, max-age=604800\"}};function J(k){return W[k]}var Y={html:\"text/html; charset=utf-8\",css:\"text/css\",javascript:\"application/javascript\",json:\"application/json\",xml:\"application/xml\",text:\"text/plain; charset=utf-8\",csv:\"text/csv\",markdown:\"text/markdown; charset=utf-8\",png:\"image/png\",jpeg:\"image/jpeg\",gif:\"image/gif\",svg:\"image/svg+xml\",webp:\"image/webp\",icon:\"image/x-icon\",bmp:\"image/bmp\",woff:\"font/woff\",woff2:\"font/woff2\",truetype:\"font/ttf\",\"embedded-opentype\":\"application/vnd.ms-fontobject\",opentype:\"font/otf\",mp4:\"video/mp4\",webm:\"video/webm\",ogg:\"video/ogg\",mpeg:\"audio/mpeg\",wav:\"audio/wav\",flac:\"audio/flac\",pdf:\"application/pdf\",doc:\"application/msword\",docx:\"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",zip:\"application/zip\",tar:\"application/x-tar\",gzip:\"application/gzip\",\"7z\":\"application/x-7z-compressed\",js:\"application/javascript\",txt:\"text/plain; charset=utf-8\",jpg:\"image/jpeg\",ico:\"image/x-icon\",ttf:\"font/ttf\",eot:\"application/vnd.ms-fontobject\",otf:\"font/otf\",mp3:\"audio/mpeg\",gz:\"application/gzip\"};function V(k){return Y[k]||\"application/octet-stream\"}function Z(k){if(typeof Bun!==\"undefined\"&&Bun.hash)return Bun.hash(k).toString(16);try{let z=L(\"crypto\"),K;if(typeof k===\"string\")K=Buffer.from(k);else if(k instanceof ArrayBuffer)K=Buffer.from(new Uint8Array(k));else K=Buffer.from(k);return z.createHash(\"md5\").update(K).digest(\"hex\")}catch{let z=typeof k===\"string\"?new TextEncoder().encode(k):k instanceof ArrayBuffer?new Uint8Array(k):k,K=0;for(let U=0;U<z.length;U++)K=(K<<5)-K+z[U],K=K&K;return Math.abs(K).toString(16).padStart(16,\"0\")}}function B(k,z,K){let U=k.headers.get(\"Origin\"),D=!0;if((K?.allowAll??!0)&&U)return U;if(z.length===0)return U||\"*\";if(!U)return z[0];if(z.includes(U))return U;return z[0]}class ${headers={};contentType(k){return this.headers[\"Content-Type\"]=V(k),this}filePath(k){let z=k.split(\".\").pop()?.toLowerCase()||\"\";return this.headers[\"Content-Type\"]=V(z),this}mimeType(k){return this.headers[\"Content-Type\"]=k,this}cache(k){let{cacheControl:z,expires:K}=J(k);if(this.headers[\"Cache-Control\"]=z,K)this.headers.Expires=K;return this}eTag(k){if(!k)return this;let z;if(typeof k===\"string\"&&k.startsWith('\"'))z=k;else z=`\"${Z(k)}\"`;return this.headers.ETag=z,this}lastModified(k){return this.headers[\"Last-Modified\"]=k.toUTCString(),this}contentLength(k){return this.headers[\"Content-Length\"]=k.toString(),this}redirect(k,z=!1){return this.headers.Location=k,this.headers[\"X-Redirect-Type\"]=z?\"permanent\":\"temporary\",this}cors({origin:k=\"*\",methods:z=[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"OPTIONS\"],headers:K=[\"Content-Type\",\"Authorization\"],maxAge:U=86400,credentials:D}={}){let X=D??k!==\"*\";if(X&&k===\"*\")throw new Error('CORS: Cannot use credentials with wildcard origin \"*\". Specify an exact origin or set credentials to false.');if(this.headers[\"Access-Control-Allow-Origin\"]=k,this.headers[\"Access-Control-Allow-Methods\"]=z.join(\", \"),this.headers[\"Access-Control-Allow-Headers\"]=K.join(\", \"),X)this.headers[\"Access-Control-Allow-Credentials\"]=\"true\";return this.headers[\"Access-Control-Max-Age\"]=U.toString(),this}security(k){let z={hsts:!0,noSniff:!0,frameOptions:\"SAMEORIGIN\",xssProtection:!0,...k};if(z.csp)this.headers[\"Content-Security-Policy\"]=z.csp;if(z.hsts){let K=typeof z.hsts===\"number\"?z.hsts:31536000;this.headers[\"Strict-Transport-Security\"]=`max-age=${K}; includeSubDomains`}if(z.noSniff)this.headers[\"X-Content-Type-Options\"]=\"nosniff\";if(z.frameOptions)this.headers[\"X-Frame-Options\"]=z.frameOptions;if(z.xssProtection)this.headers[\"X-XSS-Protection\"]=\"1; mode=block\";return this}custom(k,z){return this.headers[k]=z,this}customHeaders(k){return Object.assign(this.headers,k),this}vary(...k){let z=this.headers.Vary,K=z?`${z}, ${k.join(\", \")}`:k.join(\", \");return this.headers.Vary=K,this}compress(k){if(k)this.headers[\"Content-Encoding\"]=k;return this}build(k){let z={...this.headers};if(k&&!z[\"Content-Length\"]){let K=typeof k===\"string\"?new TextEncoder().encode(k).length:k.byteLength;z[\"Content-Length\"]=K.toString()}return z}buildHeaders(k){return new Headers(this.build(k))}}function Q(){return new $}function F(k,z){let K=Q().contentType(k);if(z)K.cache(z);return K.build()}var G={css:(k)=>Q().contentType(\"css\").cache(\"STYLESHEET\").eTag(k),javascript:(k)=>Q().contentType(\"javascript\").cache(\"JAVASCRIPT\").eTag(k),hashedAsset:(k,z)=>Q().filePath(k).cache(\"HASHED_ASSET\").eTag(z),font:(k)=>Q().filePath(k).cache(\"FONT\").cors({origin:\"*\"}),image:(k)=>Q().filePath(k).cache(\"IMAGE\"),favicon:()=>Q().contentType(\"icon\").cache(\"FAVICON\"),api:()=>Q().contentType(\"json\").cache(\"API_RESPONSE\").cors(),expensiveApi:(k)=>Q().contentType(\"json\").cache(\"API_RESPONSE\").cors().eTag(k),realtime:()=>Q().contentType(\"json\").cache(\"NO_CACHE\").cors(),html:()=>Q().contentType(\"html\").cache(\"HTML_PAGE\").security(),expensiveHtml:(k)=>Q().contentType(\"html\").cache(\"HTML_PAGE\").security().eTag(k),manifest:(k)=>Q().contentType(\"json\").cache(\"MANIFEST\").eTag(k),serviceWorker:()=>Q().contentType(\"javascript\").cache(\"SERVICE_WORKER\"),sitemap:(k)=>Q().contentType(\"xml\").cache(\"SITEMAP\").eTag(k),feed:(k,z=\"rss\")=>Q().contentType(\"xml\").cache(\"FEED\").eTag(k).mimeType(z===\"rss\"?\"application/rss+xml\":\"application/atom+xml\"),video:(k)=>Q().filePath(k).cache(\"MEDIA_STREAM\").custom(\"Accept-Ranges\",\"bytes\"),audio:(k)=>Q().filePath(k).cache(\"MEDIA_STREAM\").custom(\"Accept-Ranges\",\"bytes\"),pdf:(k)=>Q().contentType(\"pdf\").cache(\"DOCUMENT\").custom(\"Content-Disposition\",\"inline\").eTag(k),download:(k,z,K)=>Q().filePath(z).cache(\"ARCHIVE\").custom(\"Content-Disposition\",`attachment; filename=\"${k}\"`).eTag(K),secure:()=>Q().security({csp:\"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'\",hsts:31536000}).custom(\"X-Powered-By\",\"Bun\"),permanentRedirect:(k)=>Q().redirect(k,!0),temporaryRedirect:(k)=>Q().redirect(k,!1)};export{F as quickHeaders,Z as hashContent,V as getMimeType,B as getCorsOrigin,G as commonHeaders,Q as buildHeaders};\n\n//# debugId=4EE16D0787D156E064756E2164756E21\n",
10
- "import type { BunFile } from \"bun\";\r\nimport { buildHeaders, getCorsOrigin } from \"@mateosuarezdev/headers-builder\"; // Adjust import path as needed\r\n\r\ninterface StreamMediaOptions {\r\n maxChunkSize?: number; // Default: 2MB\r\n cacheMaxAge?: number; // Default: 3600 (1 hour)\r\n acceptedExtensions?: string[]; // Optional whitelist\r\n}\r\n\r\n/**\r\n * Metadata for deferred media streaming\r\n * Used with router's streamFile procedure\r\n */\r\nexport interface StreamableMediaResponse {\r\n type: \"stream\";\r\n file: BunFile;\r\n request: Request;\r\n options?: StreamMediaOptions;\r\n}\r\n\r\n/**\r\n * Handle media streaming with automatic Range request support\r\n * Supports video, audio, and other streamable media formats\r\n * @internal - Used by router to process StreamableMediaResponse\r\n */\r\nexport async function streamMediaInternal(\r\n file: BunFile,\r\n request: Request,\r\n allowedOrigins: string[],\r\n options: StreamMediaOptions = {}\r\n): Promise<Response> {\r\n const {\r\n maxChunkSize = 2 * 1024 * 1024,\r\n cacheMaxAge = 3600,\r\n acceptedExtensions,\r\n } = options;\r\n\r\n // Check if file exists\r\n const fileExists = await file.exists();\r\n if (!fileExists) {\r\n return new Response(\"Media file not found\", {\r\n status: 404,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Validate file extension if acceptedExtensions provided\r\n if (acceptedExtensions && acceptedExtensions.length > 0) {\r\n const ext = file.name?.toLowerCase().split(\".\").pop();\r\n if (!ext || !acceptedExtensions.includes(ext)) {\r\n return new Response(\"File type not supported\", {\r\n status: 415, // Unsupported Media Type\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n }\r\n\r\n const fileSize = file.size;\r\n const range = request.headers.get(\"Range\");\r\n\r\n // No range request - send full file\r\n if (!range) {\r\n return new Response(file, {\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n }\r\n\r\n // Parse range header\r\n const rangeMatch = range.match(/bytes=(\\d*)-(\\d*)/);\r\n if (!rangeMatch) {\r\n return new Response(\"Invalid Range header\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Calculate start and end positions\r\n const start = rangeMatch[1] ? parseInt(rangeMatch[1]) : 0;\r\n const requestedEnd = rangeMatch[2] ? parseInt(rangeMatch[2]) : fileSize - 1;\r\n\r\n // Apply maxChunkSize limit\r\n const end = Math.min(requestedEnd, start + maxChunkSize - 1, fileSize - 1);\r\n\r\n // Validate range\r\n if (start >= fileSize || end >= fileSize || start > end) {\r\n return new Response(\"Range not satisfiable\", {\r\n status: 416,\r\n headers: buildHeaders()\r\n .contentType(\"text\")\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .custom(\"Content-Range\", `bytes */${fileSize}`)\r\n .build(),\r\n });\r\n }\r\n\r\n // Bun automatically sets Content-Range and Content-Length for slices\r\n return new Response(file.slice(start, end + 1), {\r\n status: 206,\r\n headers: buildHeaders()\r\n .filePath(file.name || \"media\")\r\n .custom(\"Accept-Ranges\", \"bytes\")\r\n .custom(\"Cache-Control\", `public, max-age=${cacheMaxAge}`)\r\n .cors({ origin: getCorsOrigin(request, allowedOrigins) })\r\n .build(),\r\n });\r\n}\r\n\r\n/**\r\n * Helper function to create a streamable media response\r\n * Returns metadata that the router will process with streamMediaInternal()\r\n *\r\n * @example\r\n * ```typescript\r\n * // Stream any media\r\n * return streamMedia(file, ctx.request);\r\n *\r\n * // Stream with validation\r\n * return streamMedia(file, ctx.request, {\r\n * acceptedExtensions: [\"mp4\", \"webm\", \"mov\"],\r\n * maxChunkSize: 5 * 1024 * 1024,\r\n * });\r\n * ```\r\n */\r\nexport function streamMedia(\r\n file: BunFile,\r\n request: Request,\r\n options?: StreamMediaOptions\r\n): StreamableMediaResponse {\r\n return {\r\n type: \"stream\",\r\n file,\r\n request,\r\n options,\r\n };\r\n}\r\n\r\n//? I don't need to bundle them, users can create their owns\r\n// but just in case or if I want to add to docs\r\n// /**\r\n// * Common media extension presets\r\n// */\r\n// export const MEDIA_EXTENSIONS = {\r\n// video: [\"mp4\", \"webm\", \"mov\", \"avi\", \"mkv\", \"ogg\", \"ogv\"],\r\n// audio: [\"mp3\", \"wav\", \"ogg\", \"m4a\", \"flac\", \"aac\", \"opus\"],\r\n// all: [\r\n// \"mp4\",\r\n// \"webm\",\r\n// \"mov\",\r\n// \"avi\",\r\n// \"mkv\",\r\n// \"ogg\",\r\n// \"ogv\",\r\n// \"mp3\",\r\n// \"wav\",\r\n// \"m4a\",\r\n// \"flac\",\r\n// \"aac\",\r\n// \"opus\",\r\n// ],\r\n// } as const;\r\n",
11
- "import type { BaseContext } from \"./types\";\r\n\r\nexport function createContext<TCustom extends Record<string, any>>(\r\n contextCreator: (req: Request) => Promise<TCustom>\r\n): (req: Request) => Promise<BaseContext & TCustom>;\r\nexport function createContext(\r\n contextCreator: (req: Request) => Promise<void>\r\n): (req: Request) => Promise<BaseContext>;\r\nexport function createContext(contextCreator: any): any {\r\n return contextCreator;\r\n}\r\n",
12
- "import { z } from \"zod\";\r\n\r\n// Common MIME type groups\r\nconst MIME_TYPES = {\r\n image: [\r\n \"image/jpeg\",\r\n \"image/png\",\r\n \"image/gif\",\r\n \"image/webp\",\r\n \"image/svg+xml\",\r\n ],\r\n video: [\"video/mp4\", \"video/webm\", \"video/ogg\"],\r\n audio: [\"audio/mpeg\", \"audio/ogg\", \"audio/wav\"],\r\n document: [\r\n \"application/pdf\",\r\n \"application/msword\",\r\n \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\r\n \"application/vnd.ms-excel\",\r\n \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\r\n ],\r\n} as const;\r\n\r\ntype MimeTypeGroup = keyof typeof MIME_TYPES;\r\ntype MimeTypeValues<T extends MimeTypeGroup> = (typeof MIME_TYPES)[T][number];\r\n\r\n// Type for accepting either specific types or wildcard per group\r\ntype MimeTypeConfig = {\r\n [K in MimeTypeGroup]?: \"*\" | MimeTypeValues<K>[];\r\n};\r\n\r\n/** Options for creating a file validation schema */\r\ntype FileSchemaOptions = {\r\n /** Accepted MIME types configuration. Use \"*\" for all types in a category or specify an array of types\r\n * @example\r\n * {\r\n * image: \"*\", // All image types\r\n * audio: [\"audio/mpeg\"], // Only MP3s\r\n * document: [\"application/pdf\", \"application/msword\"] // PDFs and DOCs\r\n * }\r\n */\r\n acceptedTypes?: MimeTypeConfig;\r\n /** Maximum file size in megabytes */\r\n maxSize?: number;\r\n /** Minimum file size in megabytes */\r\n minSize?: number;\r\n /** Whether the file field is required */\r\n // required?: boolean;\r\n /** Custom error messages */\r\n messages?: {\r\n /** Custom message for type validation failure */\r\n type?: string;\r\n /** Custom message for maximum size validation failure */\r\n maxSize?: string;\r\n /** Custom message for minimum size validation failure */\r\n minSize?: string;\r\n /** Custom message for required field validation failure */\r\n required?: string;\r\n };\r\n};\r\n\r\nconst convertToBytes = (\r\n size: number,\r\n unit: \"MB\" | \"KB\" | \"B\" = \"MB\"\r\n): number => {\r\n switch (unit) {\r\n case \"MB\":\r\n return size * 1024 * 1024;\r\n case \"KB\":\r\n return size * 1024;\r\n case \"B\":\r\n return size;\r\n }\r\n};\r\n\r\n/**\r\n * Creates a Zod schema for file validation with support for MIME types and size constraints\r\n * you can extend it as any other zod schema after calling it\r\n * @param {Object} options - The configuration options for the file schema\r\n * @returns Zod schema for file validation\r\n */\r\nexport const createFileSchema = ({\r\n acceptedTypes,\r\n maxSize = Infinity,\r\n minSize,\r\n // required = true,\r\n messages = {},\r\n}: FileSchemaOptions = {}) => {\r\n // Convert MB to bytes\r\n const maxBytes = maxSize ? convertToBytes(maxSize) : Infinity;\r\n const minBytes = minSize ? convertToBytes(minSize) : 0;\r\n\r\n // Process accepted types\r\n const validMimeTypes = acceptedTypes\r\n ? Object.entries(acceptedTypes).flatMap(([category, types]) => {\r\n const cat = category as MimeTypeGroup;\r\n\r\n // Handle wildcard\r\n if (types === \"*\") {\r\n return MIME_TYPES[cat];\r\n }\r\n\r\n // Handle array of specific types\r\n if (Array.isArray(types)) {\r\n return types;\r\n }\r\n\r\n return [];\r\n })\r\n : undefined;\r\n\r\n // Base schema\r\n let schema = z.instanceof(File, {\r\n message: messages.required ?? \"File is required\",\r\n });\r\n\r\n // Add size refinement\r\n schema = schema\r\n .refine(\r\n (file) => file.size <= maxBytes,\r\n messages.maxSize ?? `File size must be less than ${maxSize}MB`\r\n )\r\n .refine(\r\n (file) => file.size >= minBytes,\r\n messages.minSize ?? `File size must be at least ${minSize}MB`\r\n );\r\n\r\n // Add type refinement if acceptedTypes is provided\r\n if (validMimeTypes?.length) {\r\n schema = schema.refine(\r\n (file) =>\r\n validMimeTypes.includes(file.type as MimeTypeValues<MimeTypeGroup>),\r\n messages.type ?? `File must be of type: ${validMimeTypes.join(\", \")}`\r\n );\r\n }\r\n\r\n return schema;\r\n};\r\n",
13
- "import type { BaseContext } from \"../..\";\r\n\r\n// blockPaths.ts\r\nexport type PathBlockerConfig = {\r\n paths: string[]; // Array of paths/patterns to block\r\n};\r\n\r\nexport const createPathBlocker = (config: PathBlockerConfig) => {\r\n const patterns = config.paths.map((path) => new RegExp(path));\r\n const message = \"Not Found\";\r\n\r\n return async <C extends BaseContext>(ctx: C): Promise<void> => {\r\n const url = new URL(ctx.req.url);\r\n const path = url.pathname;\r\n\r\n // Check if the path matches any blocked pattern\r\n if (patterns.some((pattern) => pattern.test(path))) {\r\n throw new Error(message);\r\n }\r\n };\r\n};\r\n",
14
- "import type { BaseContext } from \"../../types\";\r\n\r\nexport interface RateLimitConfig {\r\n windowMs: number;\r\n maxRequests: number;\r\n maxEntries?: number;\r\n cleanupIntervalMs?: number;\r\n message?: string;\r\n statusCode?: number;\r\n headerPrefix?: string;\r\n}\r\n\r\ninterface RequestWindow {\r\n count: number;\r\n startTime: number;\r\n lastAccessed: number;\r\n}\r\n\r\ninterface RateLimitResult {\r\n isLimited: boolean;\r\n remaining: number;\r\n resetTime: number;\r\n}\r\n\r\nclass RateLimiter {\r\n private requests: Map<string, RequestWindow> = new Map();\r\n private lastCleanupTime: number = Date.now();\r\n private readonly alertThreshold: number;\r\n private config: Required<RateLimitConfig>;\r\n\r\n constructor(config: RateLimitConfig) {\r\n this.config = {\r\n windowMs: config.windowMs,\r\n maxRequests: config.maxRequests,\r\n maxEntries: config.maxEntries ?? 10000,\r\n cleanupIntervalMs: config.cleanupIntervalMs ?? 60000,\r\n message: config.message ?? \"Too Many Requests\",\r\n statusCode: config.statusCode ?? 429,\r\n headerPrefix: config.headerPrefix ?? \"X-RateLimit\",\r\n };\r\n this.alertThreshold = this.config.maxEntries * 0.8;\r\n\r\n // Start periodic cleanup\r\n if (process.env.NODE_ENV !== \"development\") {\r\n setInterval(() => this.cleanup(), this.config.cleanupIntervalMs);\r\n }\r\n }\r\n\r\n private cleanup(): number {\r\n const now = Date.now();\r\n let deletedCount = 0;\r\n\r\n for (const [ip, window] of this.requests.entries()) {\r\n const windowExpired = now - window.startTime > this.config.windowMs;\r\n const inactive = now - window.lastAccessed > this.config.windowMs * 2;\r\n\r\n if (windowExpired || inactive) {\r\n this.requests.delete(ip);\r\n deletedCount++;\r\n }\r\n }\r\n\r\n this.lastCleanupTime = now;\r\n return deletedCount;\r\n }\r\n\r\n private handleMaxEntries(ip: string): boolean {\r\n const currentSize = this.requests.size;\r\n\r\n // Regular cleanup if interval passed\r\n if (Date.now() - this.lastCleanupTime > this.config.cleanupIntervalMs) {\r\n this.cleanup();\r\n }\r\n\r\n // Alert if nearing capacity\r\n if (currentSize >= this.alertThreshold) {\r\n console.warn(\r\n `Rate limiter at ${Math.round(\r\n (currentSize / this.config.maxEntries) * 100,\r\n )}% capacity`,\r\n );\r\n }\r\n\r\n // If IP is already tracked, allow it\r\n if (this.requests.has(ip)) {\r\n return true;\r\n }\r\n\r\n // Reject if at max capacity\r\n return currentSize < this.config.maxEntries;\r\n }\r\n\r\n check(ip: string): RateLimitResult {\r\n // Check system capacity first\r\n if (!this.handleMaxEntries(ip)) {\r\n return {\r\n isLimited: true,\r\n remaining: 0,\r\n resetTime: Date.now() + this.config.windowMs,\r\n };\r\n }\r\n\r\n const now = Date.now();\r\n const window = this.requests.get(ip);\r\n\r\n // Create new window if none exists or current one expired\r\n if (!window || now - window.startTime >= this.config.windowMs) {\r\n this.requests.set(ip, {\r\n count: 1,\r\n startTime: now,\r\n lastAccessed: now,\r\n });\r\n\r\n return {\r\n isLimited: false,\r\n remaining: this.config.maxRequests - 1,\r\n resetTime: now + this.config.windowMs,\r\n };\r\n }\r\n\r\n // Update existing window\r\n window.lastAccessed = now;\r\n const isLimited = window.count >= this.config.maxRequests;\r\n\r\n if (!isLimited) {\r\n window.count++;\r\n }\r\n\r\n return {\r\n isLimited,\r\n remaining: Math.max(0, this.config.maxRequests - window.count),\r\n resetTime: window.startTime + this.config.windowMs,\r\n };\r\n }\r\n\r\n getConfig() {\r\n return this.config;\r\n }\r\n}\r\n\r\n// Middleware factory\r\nexport const createRateLimiter = (config: RateLimitConfig) => {\r\n const limiter = new RateLimiter(config);\r\n\r\n return async <C extends BaseContext>(ctx: C): Promise<void> => {\r\n // Skip rate limiting for video range requests\r\n if (ctx.req.headers.get(\"range\")) {\r\n return; // Don't apply rate limiting to video chunks\r\n }\r\n\r\n const ip =\r\n ctx.req.headers.get(\"x-forwarded-for\")?.split(\",\")[0] || // Get first IP if multiple\r\n ctx.req.headers.get(\"x-real-ip\") ||\r\n \"unknown\";\r\n\r\n const { isLimited, remaining, resetTime } = limiter.check(ip);\r\n\r\n const { headerPrefix } = limiter.getConfig();\r\n ctx.headers.set(`${headerPrefix}-Remaining`, remaining.toString());\r\n ctx.headers.set(`${headerPrefix}-Reset`, resetTime.toString());\r\n\r\n if (isLimited) {\r\n ctx.headers.set(`${headerPrefix}-Exceeded`, \"true\");\r\n throw new Error(limiter.getConfig().message);\r\n }\r\n };\r\n};\r\n\r\n//example usage with more options\r\n// const rateLimiter = createRateLimiter({\r\n// windowMs: 60 * 1000, // 1 minute\r\n// maxRequests: 100, // 100 requests per minute\r\n// maxEntries: 10000, // Maximum number of IPs to track\r\n// cleanupIntervalMs: 30000, // Cleanup every 30 seconds\r\n// message: 'Rate limit exceeded. Please try again later.',\r\n// headerPrefix: 'X-RateLimit'\r\n// });\r\n\r\n// const router = createRouter({\r\n// context: createContext,\r\n// routes: appRoutes,\r\n// globalMiddlewares: [rateLimiter],\r\n// // ... other config\r\n// });\r\n",
15
- "import type { BaseContext } from \"../types\";\r\n\r\n/**\r\n * Helper to define a reusable, typed middleware outside of a procedure chain.\r\n *\r\n * @example\r\n * ```ts\r\n * // Infer context type from your context creator\r\n * const authMiddleware = createMiddleware(context, async (ctx) => {\r\n * const session = await getSession(ctx.req);\r\n * if (!session) throw new BRPCError({ code: \"UNAUTHORIZED\" });\r\n * return { session };\r\n * });\r\n *\r\n * // Or with an explicit generic (no context reference needed)\r\n * const authMiddleware = createMiddleware<MyContext>(async (ctx) => {\r\n * return { session: await getSession(ctx.req) };\r\n * });\r\n *\r\n * createProcedure(context)\r\n * .use(authMiddleware) // ctx.session is typed in the handler ✅\r\n * .query(async ({ ctx }) => ctx.session.user);\r\n * ```\r\n */\r\nexport function createMiddleware<C extends BaseContext, TReturn extends Record<string, any> | void = void>(\r\n _context: (req: Request) => Promise<C>,\r\n fn: (ctx: C) => Promise<TReturn>\r\n): (ctx: C) => Promise<TReturn>;\r\nexport function createMiddleware<C extends BaseContext, TReturn extends Record<string, any> | void = void>(\r\n fn: (ctx: C) => Promise<TReturn>\r\n): (ctx: C) => Promise<TReturn>;\r\nexport function createMiddleware<C extends BaseContext, TReturn extends Record<string, any> | void = void>(\r\n contextOrFn: ((req: Request) => Promise<C>) | ((ctx: C) => Promise<TReturn>),\r\n fn?: (ctx: C) => Promise<TReturn>\r\n): (ctx: C) => Promise<TReturn> {\r\n return fn ?? (contextOrFn as (ctx: C) => Promise<TReturn>);\r\n}\r\n",
16
- "export async function measure<T>(\r\n label: string,\r\n fn: () => Promise<T>,\r\n): Promise<T> {\r\n const start = performance.now();\r\n try {\r\n return await fn();\r\n } catch (err) {\r\n const end = performance.now();\r\n console.log(`[PERF] ${label} failed after ${(end - start).toFixed(4)} ms`);\r\n throw err;\r\n } finally {\r\n const end = performance.now();\r\n console.log(`[PERF] ${label}: ${(end - start).toFixed(4)} ms`);\r\n }\r\n}\r\n"
17
- ],
18
- "mappings": ";AAAA,YAAS,YAUT,MAAM,CAAwC,CACpC,YAAqE,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EtC,MAAO,OAA8B,IAA0B,CAE7D,GAAI,EAAI,IAAI,QAAQ,IAAI,OAAO,EAC7B,OAGF,IAAM,EACJ,EAAI,IAAI,QAAQ,IAAI,iBAAiB,GAAG,MAAM,GAAG,EAAE,IACnD,EAAI,IAAI,QAAQ,IAAI,WAAW,GAC/B,WAEM,YAAW,YAAW,aAAc,EAAQ,MAAM,CAAE,GAEpD,gBAAiB,EAAQ,UAAU,EAI3C,GAHA,EAAI,QAAQ,IAAI,GAAG,cAA0B,EAAU,SAAS,CAAC,EACjE,EAAI,QAAQ,IAAI,GAAG,UAAsB,EAAU,SAAS,CAAC,EAEzD,EAEF,MADA,EAAI,QAAQ,IAAI,GAAG,aAAyB,MAAM,EACxC,MAAM,EAAQ,UAAU,EAAE,OAAO,ICpI1C,SAAS,EAA0F,CACxG,EACA,EAC8B,CAC9B,OAAO,GAAO,ECnChB,eAAsB,EAAU,CAC9B,EACA,EACY,CACZ,IAAM,EAAQ,YAAY,IAAI,EAC9B,GAAI,CACF,OAAO,MAAM,EAAG,EAChB,MAAO,EAAK,CACZ,IAAM,EAAM,YAAY,IAAI,EAE5B,MADA,QAAQ,IAAI,UAAU,mBAAuB,EAAM,GAAO,QAAQ,CAAC,MAAM,EACnE,SACN,CACA,IAAM,EAAM,YAAY,IAAI,EAC5B,QAAQ,IAAI,UAAU,OAAW,EAAM,GAAO,QAAQ,CAAC,MAAM",
19
- "debugId": "AB2F51E21528B46464756E2164756E21",
20
- "names": []
21
- }
@@ -1,21 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["..\\src\\storage\\utils\\files.ts", "..\\src\\storage\\utils\\aspect-ratio.ts", "..\\src\\errors\\BRPCError.ts", "..\\src\\storage\\images\\optimize.ts", "..\\src\\storage\\images\\constants.ts", "..\\src\\storage\\images\\get-optimized.ts", "..\\src\\storage\\local\\index.ts", "..\\src\\storage\\images\\get-metadata.ts", "..\\src\\storage\\s3\\constants.ts", "..\\src\\storage\\s3\\upload.ts", "..\\src\\storage\\s3\\delete.ts", "..\\src\\storage\\s3\\helpers.ts"],
4
- "sourcesContent": [
5
- "import type { ResolvedFileType, StorageObject } from \"../types\";\r\n\r\nconst isImage = (type: string): boolean => type.startsWith(\"image/\");\r\n\r\nconst isVideo = (type: string): boolean => type.startsWith(\"video/\");\r\n\r\nconst isAudio = (type: string): boolean => type.startsWith(\"audio/\");\r\n\r\nconst isDocument = (type: string): boolean =>\r\n type.startsWith(\"text/\") ||\r\n type.includes(\"pdf\") ||\r\n type.includes(\"word\") ||\r\n type.includes(\"sheet\") ||\r\n type.includes(\"presentation\") ||\r\n type.includes(\"opendocument\") ||\r\n type === \"application/rtf\";\r\n\r\nconst isArchive = (type: string): boolean =>\r\n type.includes(\"zip\") ||\r\n type.includes(\"rar\") ||\r\n type.includes(\"tar\") ||\r\n type.includes(\"7z\") ||\r\n type.includes(\"gzip\");\r\n\r\nconst isCode = (type: string): boolean =>\r\n type === \"application/javascript\" ||\r\n type === \"application/json\" ||\r\n type === \"text/html\" ||\r\n type === \"text/css\" ||\r\n type === \"application/xml\" ||\r\n type.includes(\"javascript\");\r\n\r\nconst isFont = (type: string): boolean =>\r\n type.startsWith(\"font/\") || type.includes(\"font\");\r\n\r\n/**\r\n * Maps MIME types to file categories\r\n * @param mimeType - The MIME type string\r\n * @returns FileType category\r\n */\r\nconst mimeTypeToResolvedType = (mimeType: string): ResolvedFileType => {\r\n if (!mimeType) return \"other\";\r\n\r\n const type = mimeType.toLowerCase();\r\n\r\n if (isImage(type)) return \"image\";\r\n if (isVideo(type)) return \"video\";\r\n if (isAudio(type)) return \"audio\";\r\n if (isDocument(type)) return \"document\";\r\n if (isArchive(type)) return \"archive\";\r\n if (isCode(type)) return \"code\";\r\n if (isFont(type)) return \"font\";\r\n\r\n return \"other\";\r\n};\r\n\r\n// Optional: Export the predicate functions if you need them elsewhere\r\nconst fileTypePredicates = {\r\n isImage,\r\n isVideo,\r\n isAudio,\r\n isDocument,\r\n isArchive,\r\n isCode,\r\n isFont,\r\n};\r\n\r\n/**\r\n * Generates a text.txt file so you can test file uploading is working\r\n */\r\nfunction generateTestFile() {\r\n // Create test content\r\n const textContent = \"Hello, this is a test file for S3 upload!\";\r\n const buffer = Buffer.from(textContent, \"utf-8\");\r\n\r\n // Create a File object (assuming you have File constructor available in Bun)\r\n const testFile = new File([buffer], \"test.txt\", { type: \"text/plain\" });\r\n\r\n return testFile;\r\n}\r\n\r\n/**\r\n * @returns url of the file\r\n */\r\nfunction getObjectUrl(\r\n storageObject: StorageObject,\r\n options?: { bucket?: string }\r\n): string {\r\n // External file\r\n if (storageObject.url) {\r\n return storageObject.url;\r\n }\r\n\r\n // Internal file\r\n if (storageObject.key) {\r\n if (storageObject.isPrivate) {\r\n return `/storage/${storageObject.key}`; // Proxy through server\r\n } else {\r\n return `https://${\r\n options?.bucket ?? process.env.AWS_BUCKET\r\n }.s3.amazonaws.com/${storageObject.key}`; // Direct S3\r\n }\r\n }\r\n\r\n throw new Error(\"Invalid storage object\");\r\n}\r\n\r\nexport {\r\n mimeTypeToResolvedType,\r\n fileTypePredicates,\r\n getObjectUrl,\r\n generateTestFile,\r\n};\r\n",
6
- "import type { AspectRatioStr } from \"../types\";\r\n\r\n/**\r\n * @description\r\n * Returns standard 16:9, 1:1\r\n * from width and height\r\n */\r\nexport function getAspectRatioString(\r\n width: number,\r\n height: number,\r\n): AspectRatioStr {\r\n if (!width || width < 1 || !height || height < 1) return \"1:1\";\r\n const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));\r\n const divisor = gcd(width, height);\r\n return `${width / divisor}:${height / divisor}`;\r\n}\r\n\r\n/**\r\n * @description\r\n * Returns standard 16:9, 1:1\r\n * from width and height but rounding to\r\n * the nearest common ratios with some tolerance\r\n */\r\nexport function getAspectRatioStringRound(\r\n width: number,\r\n height: number,\r\n): AspectRatioStr {\r\n if (!width || !height || width < 1 || height < 1) {\r\n return \"1:1\";\r\n }\r\n\r\n const ratio = width / height;\r\n\r\n // Common aspect ratios for AI image generation\r\n const commonRatios: { ratio: number; label: AspectRatioStr }[] = [\r\n { ratio: 1 / 1, label: \"1:1\" }, // 1.0 - Square\r\n { ratio: 4 / 5, label: \"4:5\" }, // 0.8 - Portrait\r\n { ratio: 3 / 4, label: \"3:4\" }, // 0.75 - Portrait\r\n { ratio: 2 / 3, label: \"2:3\" }, // 0.667 - Portrait\r\n { ratio: 9 / 16, label: \"9:16\" }, // 0.5625 - Vertical video\r\n { ratio: 3 / 2, label: \"3:2\" }, // 1.5 - Classic photo\r\n { ratio: 4 / 3, label: \"4:3\" }, // 1.333 - Standard\r\n { ratio: 16 / 9, label: \"16:9\" }, // 1.778 - Widescreen\r\n { ratio: 21 / 9, label: \"21:9\" }, // 2.333 - Ultrawide\r\n ];\r\n\r\n const TOLERANCE = 0.03; // 3% tolerance for AI variations\r\n\r\n // Find closest common ratio\r\n for (const { ratio: commonRatio, label } of commonRatios) {\r\n const difference = Math.abs(ratio - commonRatio);\r\n const percentDiff = difference / commonRatio;\r\n\r\n if (percentDiff < TOLERANCE) {\r\n return label;\r\n }\r\n }\r\n\r\n // Fallback to exact calculation for unusual ratios\r\n const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));\r\n const divisor = gcd(width, height);\r\n return `${width / divisor}:${height / divisor}`;\r\n}\r\n",
7
- "export type BRPCErrorCode =\r\n | \"BAD_REQUEST\"\r\n | \"UNAUTHORIZED\"\r\n | \"FORBIDDEN\"\r\n | \"NOT_FOUND\"\r\n | \"METHOD_NOT_SUPPORTED\"\r\n | \"TIMEOUT\"\r\n | \"CONFLICT\"\r\n | \"PRECONDITION_FAILED\"\r\n | \"PAYLOAD_TOO_LARGE\"\r\n | \"UNPROCESSABLE_CONTENT\"\r\n | \"TOO_MANY_REQUESTS\"\r\n | \"CLIENT_CLOSED_REQUEST\"\r\n | \"INTERNAL_SERVER_ERROR\"\r\n | \"NOT_IMPLEMENTED\"\r\n | \"BAD_GATEWAY\"\r\n | \"SERVICE_UNAVAILABLE\"\r\n | \"GATEWAY_TIMEOUT\";\r\n\r\nexport interface BRPCErrorOptions {\r\n code: BRPCErrorCode;\r\n message: string;\r\n clientCode?: string; // Custom code for client-side detection\r\n cause?: Error;\r\n data?: Record<string, any>;\r\n}\r\n\r\nexport class BRPCError extends Error {\r\n public readonly code: BRPCErrorCode;\r\n public readonly clientCode?: string;\r\n public readonly httpStatus: number;\r\n public readonly data?: Record<string, any>;\r\n public readonly cause?: Error;\r\n\r\n private static readonly STATUS_MAP: Record<BRPCErrorCode, number> = {\r\n BAD_REQUEST: 400,\r\n UNAUTHORIZED: 401,\r\n FORBIDDEN: 403,\r\n NOT_FOUND: 404,\r\n METHOD_NOT_SUPPORTED: 405,\r\n TIMEOUT: 408,\r\n CONFLICT: 409,\r\n PRECONDITION_FAILED: 412,\r\n PAYLOAD_TOO_LARGE: 413,\r\n UNPROCESSABLE_CONTENT: 422,\r\n TOO_MANY_REQUESTS: 429,\r\n CLIENT_CLOSED_REQUEST: 499,\r\n INTERNAL_SERVER_ERROR: 500,\r\n NOT_IMPLEMENTED: 501,\r\n BAD_GATEWAY: 502,\r\n SERVICE_UNAVAILABLE: 503,\r\n GATEWAY_TIMEOUT: 504,\r\n };\r\n\r\n constructor(options: BRPCErrorOptions) {\r\n super(options.message);\r\n\r\n this.name = \"BRPCError\";\r\n this.code = options.code;\r\n this.clientCode = options.clientCode;\r\n this.httpStatus = BRPCError.STATUS_MAP[options.code];\r\n this.data = options.data;\r\n this.cause = options.cause;\r\n\r\n // Maintains proper stack trace for where our error was thrown (only available on V8)\r\n if (Error.captureStackTrace) {\r\n Error.captureStackTrace(this, BRPCError);\r\n }\r\n }\r\n\r\n /**\r\n * Create a serializable object for sending to frontend\r\n */\r\n toJSON() {\r\n return {\r\n name: this.name,\r\n code: this.code,\r\n clientCode: this.clientCode,\r\n message: this.message,\r\n data: this.data,\r\n httpStatus: this.httpStatus,\r\n };\r\n }\r\n\r\n /**\r\n * Static factory methods for common errors\r\n */\r\n static badRequest(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"BAD_REQUEST\", message, clientCode, data });\r\n }\r\n\r\n static unauthorized(\r\n message: string = \"Unauthorized\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"UNAUTHORIZED\", message, clientCode, data });\r\n }\r\n\r\n static forbidden(\r\n message: string = \"Forbidden\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"FORBIDDEN\", message, clientCode, data });\r\n }\r\n\r\n static notFound(\r\n message: string = \"Not Found\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static preconditionFailed(\r\n message: string = \"Precondition failed\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"NOT_FOUND\", message, clientCode, data });\r\n }\r\n\r\n static conflict(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"CONFLICT\", message, clientCode, data });\r\n }\r\n\r\n static unprocessableContent(\r\n message: string,\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"UNPROCESSABLE_CONTENT\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static tooManyRequests(\r\n message: string = \"Too many requests\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"TOO_MANY_REQUESTS\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static internalServerError(\r\n message: string = \"Internal Server Error\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({\r\n code: \"INTERNAL_SERVER_ERROR\",\r\n message,\r\n clientCode,\r\n data,\r\n });\r\n }\r\n\r\n static timeout(\r\n message: string = \"Request timeout\",\r\n clientCode?: string,\r\n data?: Record<string, any>\r\n ) {\r\n return new BRPCError({ code: \"TIMEOUT\", message, clientCode, data });\r\n }\r\n}\r\n",
8
- "import type { BunFile } from \"bun\";\r\nimport type { GeneratedImageVariants, UploadableImageBuffer } from \"./types\";\r\nimport { BRPCError } from \"../../errors\";\r\nimport { fileTypePredicates } from \"../utils\";\r\nimport sharp from \"sharp\";\r\nimport type { Acl } from \"../types\";\r\nimport { getAspectRatioString } from \"../utils/aspect-ratio\";\r\nimport { DEFAULT_IMAGE_MAX_SIZE, IMAGE_VARIANTS } from \"./constants\";\r\n\r\nasync function processSharpImage(\r\n buffer: Buffer,\r\n maxSize: number,\r\n quality: number,\r\n name: string,\r\n acl: Acl,\r\n): Promise<UploadableImageBuffer> {\r\n try {\r\n const { data: optimizedBuffer, info } = await sharp(buffer, {\r\n // Prevent decompression bombs / absurd images\r\n limitInputPixels: 10000 * 10000,\r\n })\r\n // Auto-apply EXIF orientation from phones/cameras\r\n .rotate()\r\n .resize({\r\n width: maxSize,\r\n height: maxSize,\r\n fit: \"inside\",\r\n withoutEnlargement: true,\r\n })\r\n .webp({\r\n quality,\r\n })\r\n .toBuffer({ resolveWithObject: true });\r\n\r\n return {\r\n buffer: optimizedBuffer,\r\n object: {\r\n name: name.replace(/\\.[^/.]+$/, \".webp\"),\r\n resolvedType: \"image\",\r\n metadata: {\r\n type: \"image/webp\",\r\n size: optimizedBuffer.length,\r\n extension: \".webp\",\r\n width: info.width,\r\n height: info.height,\r\n aspectRatio: info.width && info.height ? info.width / info.height : 1,\r\n aspectRatioStr:\r\n info.width && info.height\r\n ? getAspectRatioString(info.width, info.height)\r\n : \"1:1\",\r\n acl,\r\n },\r\n },\r\n };\r\n } catch (error) {\r\n throw BRPCError.conflict(\r\n `Failed to optimize image: ${\r\n error instanceof Error ? error.message : \"Unknown error\"\r\n }`,\r\n );\r\n }\r\n}\r\n\r\nasync function normalizeImageInput(\r\n file: File | BunFile | string,\r\n): Promise<{ buffer: Buffer; name: string }> {\r\n if (!file) {\r\n throw BRPCError.conflict(\"A file must be provided\");\r\n }\r\n\r\n let inputBuffer: Buffer;\r\n let originalName = \"unknown.webp\";\r\n\r\n if (typeof file === \"string\") {\r\n console.log(\"I'm going to fail here for file\", file);\r\n if (!file.startsWith(\"data:\")) {\r\n console.log(\"I never got here\");\r\n throw BRPCError.conflict(\"Invalid base64 format\");\r\n }\r\n\r\n const [, data] = file.split(\",\");\r\n\r\n if (!data) {\r\n throw BRPCError.conflict(\"Invalid base64 data\");\r\n }\r\n\r\n inputBuffer = Buffer.from(data, \"base64\");\r\n } else {\r\n if (!fileTypePredicates.isImage(file.type)) {\r\n throw BRPCError.conflict(\"File must be an image\");\r\n }\r\n\r\n const arrayBuffer = await file.arrayBuffer();\r\n\r\n inputBuffer = Buffer.from(arrayBuffer);\r\n originalName = file.name ?? originalName;\r\n }\r\n\r\n return {\r\n buffer: inputBuffer,\r\n name: originalName,\r\n };\r\n}\r\n\r\nexport async function optimizeImage(\r\n file: File | BunFile | string,\r\n {\r\n size,\r\n quality,\r\n acl,\r\n }: {\r\n size?: number;\r\n quality?: number;\r\n acl?: Acl;\r\n } = {},\r\n): Promise<UploadableImageBuffer> {\r\n const normalized = await normalizeImageInput(file);\r\n\r\n return processSharpImage(\r\n normalized.buffer,\r\n size ?? DEFAULT_IMAGE_MAX_SIZE,\r\n quality ?? 80,\r\n normalized.name,\r\n acl ?? \"public-read\",\r\n );\r\n}\r\n\r\nexport async function optimizeImageBuffer(\r\n buffer: Buffer,\r\n {\r\n name = \"unknown.webp\",\r\n size,\r\n quality,\r\n acl,\r\n }: {\r\n name?: string;\r\n size?: number;\r\n quality?: number;\r\n acl?: Acl;\r\n } = {},\r\n): Promise<UploadableImageBuffer> {\r\n if (!buffer?.length) {\r\n throw BRPCError.conflict(\"A buffer must be provided\");\r\n }\r\n\r\n return processSharpImage(\r\n buffer,\r\n size ?? DEFAULT_IMAGE_MAX_SIZE,\r\n quality ?? 80,\r\n name,\r\n acl ?? \"public-read\",\r\n );\r\n}\r\n\r\nexport async function generateVariants(\r\n file: File | BunFile | string,\r\n {\r\n quality = 80,\r\n acl = \"public-read\",\r\n }: {\r\n quality?: number;\r\n acl?: Acl;\r\n } = {},\r\n): Promise<GeneratedImageVariants> {\r\n console.log(\"Going to normalizeImageInput\");\r\n const normalized = await normalizeImageInput(file);\r\n\r\n console.log(\"Going to processSharpImage for original\");\r\n const original = await processSharpImage(\r\n normalized.buffer,\r\n DEFAULT_IMAGE_MAX_SIZE,\r\n quality,\r\n normalized.name,\r\n acl,\r\n );\r\n\r\n console.log(\"Going to processSharpImage for variantsEntries\");\r\n const variantsEntries = await Promise.all(\r\n Object.entries(IMAGE_VARIANTS).map(async ([variantName, size]) => {\r\n const variant = await processSharpImage(\r\n normalized.buffer,\r\n size,\r\n quality,\r\n normalized.name,\r\n acl,\r\n );\r\n\r\n return [variantName, variant] as const;\r\n }),\r\n );\r\n\r\n return {\r\n original,\r\n variants: Object.fromEntries(variantsEntries),\r\n };\r\n}\r\n\r\n// async function processSharpImage(\r\n// buffer: Buffer,\r\n// maxWidth: number,\r\n// quality: number,\r\n// name: string,\r\n// acl: Acl,\r\n// ): Promise<UploadableImageBuffer> {\r\n// try {\r\n// const { data: optimizedBuffer, info } = await sharp(buffer)\r\n// .resize(maxWidth, null, { withoutEnlargement: true })\r\n// .webp({ quality })\r\n// .toBuffer({ resolveWithObject: true });\r\n\r\n// return {\r\n// buffer: optimizedBuffer,\r\n// object: {\r\n// name: name.replace(/\\.[^/.]+$/, \".webp\"),\r\n// resolvedType: \"image\",\r\n// metadata: {\r\n// type: \"image/webp\",\r\n// size: optimizedBuffer.length,\r\n// extension: \".webp\",\r\n// width: info.width,\r\n// height: info.height,\r\n// aspectRatio: info.height > 0 ? info.width / info.height : 1,\r\n// aspectRatioStr: getAspectRatioString(info.width, info.height),\r\n// acl,\r\n// },\r\n// },\r\n// };\r\n// } catch (error) {\r\n// throw BRPCError.conflict(\r\n// `Failed to optimize image: ${error instanceof Error ? error.message : \"Unknown error\"}`,\r\n// );\r\n// }\r\n// }\r\n\r\n// export async function optimizeImage(\r\n// file: File | BunFile | string,\r\n// { width, quality, acl }: { width?: number; quality?: number; acl?: Acl } = {},\r\n// ): Promise<UploadableImageBuffer> {\r\n// const maxWidth = width ?? 1200;\r\n// const q = quality ?? 75;\r\n\r\n// if (!file) {\r\n// throw BRPCError.conflict(\"A file must be provided\");\r\n// }\r\n\r\n// let inputBuffer: Buffer;\r\n// let originalName = \"unknown.webp\";\r\n\r\n// if (typeof file === \"string\") {\r\n// if (!file.startsWith(\"data:\")) {\r\n// throw BRPCError.conflict(\"Invalid base64 format\");\r\n// }\r\n// const [, data] = file.split(\",\");\r\n// if (!data) {\r\n// throw BRPCError.conflict(\"Invalid base64 data\");\r\n// }\r\n// inputBuffer = Buffer.from(data, \"base64\");\r\n// } else {\r\n// if (!fileTypePredicates.isImage(file.type)) {\r\n// throw BRPCError.conflict(\"File must be an image\");\r\n// }\r\n// const arrayBuffer = await file.arrayBuffer();\r\n// inputBuffer = Buffer.from(arrayBuffer);\r\n// originalName = file.name ?? originalName;\r\n// }\r\n\r\n// return processSharpImage(\r\n// inputBuffer,\r\n// maxWidth,\r\n// q,\r\n// originalName,\r\n// acl ?? \"public-read\",\r\n// );\r\n// }\r\n\r\n// export async function optimizeImageBuffer(\r\n// buffer: Buffer,\r\n// {\r\n// name = \"unknown.webp\",\r\n// width,\r\n// quality,\r\n// acl,\r\n// }: { name?: string; width?: number; quality?: number; acl?: Acl } = {},\r\n// ): Promise<UploadableImageBuffer> {\r\n// if (!buffer?.length) {\r\n// throw BRPCError.conflict(\"A buffer must be provided\");\r\n// }\r\n\r\n// return processSharpImage(\r\n// buffer,\r\n// width ?? 1200,\r\n// quality ?? 75,\r\n// name,\r\n// acl ?? \"public-read\",\r\n// );\r\n// }\r\n",
9
- "export const IMAGE_VARIANTS = {\r\n xs: 200,\r\n sm: 400,\r\n md: 800,\r\n lg: 1200,\r\n} as const;\r\n\r\nexport const DEFAULT_IMAGE_MAX_SIZE = 1920;\r\n",
10
- "import sharp from \"sharp\";\r\nimport { local } from \"../local\";\r\nimport { fileTypePredicates } from \"../utils\";\r\nimport type { GetOptimizedImageOptions } from \"./types\";\r\n\r\n/**\r\n * Returns an existing optimized image from cache or\r\n * creates one on the fly\r\n * @example\r\n * ```ts\r\n * const optimizedImage = await getImage({\r\n * key: \"originalFileKey\",\r\n * width: 800\r\n * })\r\n * if (!optimizedImage) throw BRPCError.notFound()\r\n * return optimizedImage\r\n * ```\r\n */\r\nexport async function getOptimizedImage(options: GetOptimizedImageOptions) {\r\n // const url = new URL(req.url);\r\n // const imagePath = url.searchParams.get(\"src\");\r\n // const width = parseInt(url.searchParams.get(\"w\") || \"800\");\r\n // const quality = parseInt(url.searchParams.get(\"q\") || \"75\");\r\n\r\n const localCache = `./cache/images/${options.key}_${options.width}_${options.quality}.webp`;\r\n\r\n // 1. Check local cache\r\n const localFile = await local.getOne(localCache);\r\n\r\n if (localFile) {\r\n if (!fileTypePredicates.isImage(localFile.type)) {\r\n return null;\r\n }\r\n return localFile;\r\n }\r\n\r\n // 2. Check Spaces cache - using Bun's global s3 instance\r\n // const cachedFile = s3.file(`appS3Folder/cache/${cacheKey}`);\r\n // if (await cachedFile.exists()) {\r\n // const cached = await cachedFile.arrayBuffer();\r\n // await Bun.write(localCache, cached);\r\n // return new Response(cached, {\r\n // headers: { 'Content-Type': 'image/webp' }\r\n // });\r\n // }\r\n\r\n // 3. Fetch from Spaces, optimize, cache\r\n // const originalFile = s3.file(`originals/${imagePath}`);\r\n // const originalBuffer = await originalFile.arrayBuffer();\r\n\r\n // 3.2 Fetch from local\r\n // const originalPath = `./buckets/cache/images/originals/${options.path}`;\r\n const originalKey = options.key;\r\n const originalFile = await local.getOne(originalKey);\r\n\r\n if (!originalFile) return null;\r\n\r\n if (!fileTypePredicates.isImage(originalFile.type)) {\r\n return null;\r\n }\r\n\r\n const buffer = await originalFile.arrayBuffer();\r\n\r\n const optimizedBuffer = await sharp(buffer)\r\n .resize(options.width, null, { withoutEnlargement: true })\r\n .webp({ quality: options.quality ?? 75 })\r\n .toBuffer();\r\n\r\n // Cache everywhere\r\n // await Promise.all([\r\n // Bun.write(localCache, optimizedBuffer),\r\n // cachedFile.write(optimizedBuffer, { type: 'image/webp' })\r\n // ]);\r\n\r\n // Cache to local\r\n await Bun.write(localCache, optimizedBuffer);\r\n\r\n const cachedFile = Bun.file(localCache);\r\n\r\n const exists = await cachedFile.exists();\r\n\r\n if (!exists) return null;\r\n\r\n return cachedFile;\r\n}\r\n",
11
- "import { randomUUID } from \"crypto\";\r\nimport { extname } from \"path\";\r\nimport type { SafeResult } from \"../../types\";\r\n\r\ntype UploadSuccess = {\r\n key: string;\r\n file: File;\r\n bytesWritten: number;\r\n};\r\n\r\n/**\r\n * Uploads a file to local filesystem\r\n * @param path - The folder path (e.g. \"public/images\")\r\n * @param file - The file to upload\r\n * @param filename - Optional custom filename to use instead of original file.name\r\n * @returns Promise<SafeResult<number>> - Success: {data: bytesWritten, error: null}\r\n * Failure: {data: null, error: Error}\r\n *\r\n * Example:\r\n * ```ts\r\n * const {data, error} = await uploadToLocal(\"public/avatars\", userPhoto);\r\n * if (error) {\r\n * console.error(\"Upload failed:\", error);\r\n * return;\r\n * }\r\n * console.log(`Uploaded ${data} bytes`);\r\n * ```\r\n */\r\nasync function uploadOne(\r\n path: string,\r\n file: File,\r\n _filename?: string\r\n): Promise<SafeResult<UploadSuccess>> {\r\n if (!file)\r\n return {\r\n data: null,\r\n error: new Error(`File not found`),\r\n };\r\n\r\n const cleanPath = path.replace(/^\\/+|\\/+$/g, \"\");\r\n const ext = extname(file.name);\r\n const uniqueName = `${randomUUID()}${ext}`;\r\n const filePath = `${cleanPath}/${uniqueName}`;\r\n const fullPath = `./buckets/${cleanPath}/${uniqueName}`;\r\n\r\n try {\r\n const writtenBytes = await Bun.write(fullPath, file);\r\n return {\r\n data: {\r\n key: filePath,\r\n file: file,\r\n bytesWritten: writtenBytes,\r\n },\r\n error: null,\r\n };\r\n } catch (err) {\r\n return {\r\n data: null,\r\n error: new Error(`Failed to upload ${file.name} to local filesystem`),\r\n };\r\n }\r\n}\r\n\r\nasync function uploadMany(\r\n path: string,\r\n files: File[],\r\n nameGetter?: (file: File) => string\r\n): Promise<SafeResult<UploadSuccess>[]> {\r\n if (!files || files.length === 0) {\r\n return [];\r\n }\r\n\r\n const uploadPromises = files.map((file) => {\r\n const filename = nameGetter?.(file) ?? file.name;\r\n return uploadOne(path, file, filename);\r\n });\r\n\r\n const results = await Promise.all(uploadPromises);\r\n\r\n return results;\r\n}\r\n\r\nasync function getOne(key: string) {\r\n const file = Bun.file(`./buckets/${key}`);\r\n const exists = await file.exists();\r\n\r\n if (!exists) return null;\r\n\r\n return file;\r\n}\r\n\r\nasync function deleteOne(key: string) {\r\n const file = Bun.file(`./buckets/${key}`);\r\n const exists = await file.exists();\r\n\r\n if (!exists) return false;\r\n\r\n await file.delete();\r\n\r\n return true;\r\n}\r\n\r\nexport const local = {\r\n uploadOne,\r\n uploadMany,\r\n getOne,\r\n deleteOne,\r\n};\r\n",
12
- "import sharp from \"sharp\";\r\nimport type { StrictImageMetadata } from \"./types\";\r\nimport { getAspectRatioString } from \"../utils/aspect-ratio\";\r\n\r\nexport async function getImageMetadata(\r\n buffer: Buffer,\r\n): Promise<StrictImageMetadata | null> {\r\n if (!buffer?.length) return null;\r\n\r\n try {\r\n const meta = await sharp(buffer).metadata();\r\n\r\n const width = meta.width ?? 0;\r\n const height = meta.height ?? 0;\r\n const aspectRatio = meta.height > 0 ? width / height : 1;\r\n\r\n return {\r\n // general\r\n type: `image/${meta.format}`,\r\n size: meta.size ?? 0, // original file size in bytes\r\n extension: `.${meta.format}`,\r\n\r\n // image related\r\n width: width,\r\n height: height,\r\n aspectRatio: aspectRatio,\r\n aspectRatioStr: getAspectRatioString(width, height),\r\n };\r\n } catch (err) {\r\n console.warn(\"Failed to read image metadata:\", err);\r\n return null;\r\n }\r\n}\r\n",
13
- "const AWS_FOLDER = process.env.AWS_FOLDER;\r\nconst CONSTRUCTED_AWS_FOLDER = AWS_FOLDER ? `${AWS_FOLDER}/` : \"\";\r\n\r\nexport { CONSTRUCTED_AWS_FOLDER };\r\n",
14
- "import type { Acl, InsertStorageObjet, UploadableObjectBuffer } from \"../types\";\r\nimport { BRPCError } from \"../../errors/BRPCError\";\r\nimport { CONSTRUCTED_AWS_FOLDER } from \"./constants\";\r\nimport { s3 as s3Bun, type BunFile } from \"bun\";\r\nimport { extname } from \"path\";\r\nimport { mimeTypeToResolvedType } from \"../utils\";\r\nimport { generateVariants, type GeneratedImageVariants } from \"../images\";\r\nimport type { UploadedImageVariants } from \"./types\";\r\n\r\n/**\r\n * Uploads a file or optimized buffer to S3 with UUID-based naming\r\n *\r\n * @param path - S3 folder path (e.g. \"images/avatars\")\r\n * @param file - File object or OptimizedBuffer from optimize()\r\n * @returns SafeResult with S3 key, bytes written, isPrivate and metadata for database storage\r\n *\r\n * @throws {Error} When buffer is provided without required metadata\r\n *\r\n * @example\r\n * ```typescript\r\n * // regular files\r\n * const result = await uploadOne('images', userFile);\r\n *\r\n * // optimized images\r\n * const optimized = await optimize(bunFile);\r\n * const result2 = await uploadOne('images/optimized', optimized);\r\n *\r\n * if (result.error) throw BRPCError.conlict(\"Error uploading \")\r\n *\r\n * if (result.data) {\r\n * console.log(`Uploaded: ${result.data.key}`);\r\n * // Save metadata to DB: result.data.metadata\r\n * }\r\n * ```\r\n */\r\nasync function uploadOne(\r\n path: string,\r\n file: File | UploadableObjectBuffer,\r\n opts?: { acl?: Acl; externalReference?: string; uploadedBy?: string },\r\n): Promise<InsertStorageObjet> {\r\n // Extract file info based on type\r\n const fileInfo: UploadableObjectBuffer =\r\n \"buffer\" in file\r\n ? file\r\n : {\r\n buffer: file,\r\n object: {\r\n name: file.name,\r\n resolvedType: mimeTypeToResolvedType(file.type),\r\n metadata: {\r\n type: file.type,\r\n extension: extname(file.name),\r\n size: file.size,\r\n acl: opts?.acl ?? \"public-read\",\r\n },\r\n },\r\n };\r\n\r\n // Only validate the risky input (UploadableObjectBuffer)\r\n if (\"buffer\" in file) {\r\n if (!file.object?.name || !file.object?.metadata) {\r\n throw BRPCError.badRequest(\r\n \"Complete metadata required when uploading buffer\",\r\n );\r\n }\r\n }\r\n\r\n // Common upload logic\r\n const cleanPath = path.length > 1 ? `${path.replace(/^\\/+|\\/+$/g, \"\")}/` : \"\";\r\n const extension = fileInfo.object.metadata.extension.startsWith(\".\")\r\n ? fileInfo.object.metadata.extension\r\n : `.${fileInfo.object.metadata.extension}`;\r\n const fileName = fileInfo.object.name\r\n ? fileInfo.object.name\r\n : `${crypto.randomUUID()}${extension}`;\r\n const key = `${CONSTRUCTED_AWS_FOLDER}${cleanPath}${fileName}`;\r\n\r\n const s3file = s3Bun.file(key);\r\n\r\n const acl = fileInfo?.object?.metadata?.acl ?? \"public-read\";\r\n\r\n const isPrivate = acl !== \"public-read\" && acl !== \"public-read-write\";\r\n\r\n const writtenBytes = await s3file.write(fileInfo.buffer, {\r\n acl: acl,\r\n type: fileInfo.object.metadata.type,\r\n });\r\n\r\n const digestedObject: InsertStorageObjet = {\r\n uploadedBy: opts?.uploadedBy ?? null,\r\n key: key,\r\n url: null,\r\n name: fileInfo.object.name,\r\n thumbnail: null,\r\n resolvedType: fileInfo.object.resolvedType,\r\n provider: \"s3\",\r\n isPrivate: isPrivate,\r\n metadata: {\r\n ...fileInfo.object.metadata,\r\n size: writtenBytes,\r\n acl: acl,\r\n },\r\n isActive: true,\r\n };\r\n\r\n return digestedObject;\r\n}\r\n\r\nexport { uploadOne };\r\n\r\ntype UploadManyOptions = {\r\n acl?: Acl;\r\n uploadedBy?: string;\r\n throwIf?: \"any\" | \"all\" | \"ignore\"; // error behavior\r\n externalKeys?: string[]; // array to push uploaded keys to\r\n};\r\n\r\ntype UploadManyResult = {\r\n data: InsertStorageObjet[];\r\n errors?: any[];\r\n};\r\n\r\nexport async function uploadMany(\r\n path: string,\r\n files: UploadableObjectBuffer[],\r\n opts: UploadManyOptions = { throwIf: \"any\" },\r\n): Promise<UploadManyResult> {\r\n const results = await Promise.allSettled(\r\n files.map((file) =>\r\n uploadOne(path, file, {\r\n acl: opts?.acl,\r\n uploadedBy: opts?.uploadedBy,\r\n }),\r\n ),\r\n );\r\n\r\n const data: InsertStorageObjet[] = [];\r\n const errors: any[] = [];\r\n\r\n results.forEach((res) => {\r\n if (res.status === \"fulfilled\") {\r\n data.push(res.value);\r\n if (opts?.externalKeys) opts.externalKeys.push(res.value.key);\r\n } else {\r\n errors.push(res.reason);\r\n }\r\n });\r\n\r\n // Decide whether to throw based on `throwIf` option\r\n if (\r\n (opts?.throwIf === \"any\" && errors.length > 0) ||\r\n (opts?.throwIf === \"all\" && errors.length === files.length)\r\n ) {\r\n throw BRPCError.internalServerError(\r\n `Upload failed: ${errors.length} of ${files.length}`,\r\n );\r\n }\r\n\r\n return { data, errors: errors.length ? errors : undefined };\r\n}\r\n\r\nexport async function uploadGeneratedVariants(\r\n path: string,\r\n generated: GeneratedImageVariants,\r\n opts?: {\r\n acl?: Acl;\r\n uploadedBy?: string;\r\n baseId?: string;\r\n },\r\n): Promise<UploadedImageVariants> {\r\n const baseId = opts?.baseId ?? crypto.randomUUID();\r\n\r\n const originalExtension = generated.original.object.metadata.extension;\r\n\r\n const originalUploaded = await uploadOne(\r\n path,\r\n {\r\n ...generated.original,\r\n object: {\r\n ...generated.original.object,\r\n name: `${baseId}${originalExtension}`,\r\n },\r\n },\r\n opts,\r\n );\r\n\r\n const uploadedVariantsEntries = await Promise.all(\r\n Object.entries(generated.variants).map(async ([variantName, variant]) => {\r\n if (!variant) {\r\n return [variantName, null] as const;\r\n }\r\n\r\n const extension = variant.object.metadata.extension;\r\n\r\n const uploaded = await uploadOne(\r\n path,\r\n {\r\n ...variant,\r\n object: {\r\n ...variant.object,\r\n name: `${baseId}_${variantName}${extension}`,\r\n },\r\n },\r\n opts,\r\n );\r\n\r\n return [variantName, uploaded] as const;\r\n }),\r\n );\r\n\r\n return {\r\n original: originalUploaded,\r\n variants: Object.fromEntries(\r\n uploadedVariantsEntries.filter(([, value]) => value !== null),\r\n ),\r\n };\r\n}\r\n\r\nexport async function generateAndUploadImage(\r\n path: string,\r\n file: File | BunFile | string,\r\n opts?: {\r\n acl?: Acl;\r\n uploadedBy?: string;\r\n quality?: number;\r\n },\r\n): Promise<UploadedImageVariants> {\r\n if (Array.isArray(file)) {\r\n throw new Error(\"File can't be an array, pass a single file\");\r\n }\r\n\r\n const generated = await generateVariants(file, {\r\n quality: opts?.quality,\r\n acl: opts?.acl,\r\n });\r\n\r\n return uploadGeneratedVariants(path, generated, {\r\n acl: opts?.acl,\r\n uploadedBy: opts?.uploadedBy,\r\n });\r\n}\r\n",
15
- "import { BRPCError } from \"../../errors\";\r\nimport { s3 as s3Bun } from \"bun\";\r\nimport type { DeleteOneResult } from \"./types\";\r\n\r\nexport async function deleteOne(\r\n key: string,\r\n opts?: { debug?: boolean }\r\n): Promise<DeleteOneResult> {\r\n if (!key || typeof key !== \"string\") {\r\n throw BRPCError.badRequest(\"Key is required\");\r\n }\r\n\r\n const sanitizedKey = key.trim();\r\n if (!sanitizedKey || sanitizedKey.length === 0) {\r\n throw BRPCError.badRequest(\"Key must be a non-empty string\");\r\n }\r\n\r\n try {\r\n await s3Bun.delete(key);\r\n return {\r\n key: key,\r\n deleted: true,\r\n error: null,\r\n };\r\n } catch (e) {\r\n return {\r\n key: key,\r\n deleted: false,\r\n error: e as Error,\r\n };\r\n // if (opts?.debug) {\r\n // console.error(`Error deleting file with key \"${key}\"`, e);\r\n // }\r\n //? Handle this at app level\r\n // throw BRPCError.conflict(\r\n // \"There was an error when trying to delete the file\",\r\n // \"FILE_NOT_DELETED\"\r\n // );\r\n }\r\n}\r\n\r\n/**\r\n * Deletes multiple S3 keys in parallel.\r\n * Returns per-key result so caller can handle failures/rollback.\r\n */\r\nexport async function deleteMany(\r\n keys: string[],\r\n opts?: { debug?: boolean }\r\n): Promise<DeleteOneResult[]> {\r\n const results = await Promise.allSettled(\r\n keys.map((key) => deleteOne(key, opts))\r\n );\r\n\r\n return results.map((res, i) => {\r\n if (res.status === \"fulfilled\") return res.value;\r\n return { key: keys[i], deleted: false, error: res.reason as Error };\r\n });\r\n}\r\n",
16
- "import { s3 as s3Bun } from \"bun\";\r\n\r\n/**\r\n * Checks a file existance in S3\r\n */\r\nexport async function checkFileExistance(\r\n key: string,\r\n opts: { debug?: boolean } = { debug: process.env.NODE_ENV !== \"production\" },\r\n): Promise<{ exists: boolean; error: null } | { exists: null; error: Error }> {\r\n try {\r\n const exists = await s3Bun.exists(key);\r\n return { exists, error: null };\r\n } catch (e) {\r\n if (opts?.debug) {\r\n console.error(\"There was an error checking for file existance\", e);\r\n }\r\n return { exists: null, error: e as Error };\r\n }\r\n}\r\n"
17
- ],
18
- "mappings": ";AAEA,IAAM,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAU,CAAC,IAA0B,EAAK,WAAW,QAAQ,EAE7D,EAAa,CAAC,IAClB,EAAK,WAAW,OAAO,GACvB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,MAAM,GACpB,EAAK,SAAS,OAAO,GACrB,EAAK,SAAS,cAAc,GAC5B,EAAK,SAAS,cAAc,GAC5B,IAAS,kBAEL,EAAY,CAAC,IACjB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,KAAK,GACnB,EAAK,SAAS,IAAI,GAClB,EAAK,SAAS,MAAM,EAEhB,EAAS,CAAC,IACd,IAAS,0BACT,IAAS,oBACT,IAAS,aACT,IAAS,YACT,IAAS,mBACT,EAAK,SAAS,YAAY,EAEtB,EAAS,CAAC,IACd,EAAK,WAAW,OAAO,GAAK,EAAK,SAAS,MAAM,EAO5C,EAAyB,CAAC,IAAuC,CACrE,GAAI,CAAC,EAAU,MAAO,QAEtB,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