alepha 0.13.0 → 0.13.1

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.
Files changed (195) hide show
  1. package/dist/api-jobs/index.d.ts +26 -26
  2. package/dist/api-users/index.d.ts +1 -1
  3. package/dist/cli/{dist-Sz2EXvQX.cjs → dist-Dl9Vl7Ur.js} +17 -13
  4. package/dist/cli/{dist-BBPjuQ56.js.map → dist-Dl9Vl7Ur.js.map} +1 -1
  5. package/dist/cli/index.d.ts +3 -11
  6. package/dist/cli/index.js +106 -74
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/email/index.js +71 -73
  9. package/dist/email/index.js.map +1 -1
  10. package/dist/orm/index.d.ts +1 -1
  11. package/dist/orm/index.js.map +1 -1
  12. package/dist/queue/index.d.ts +4 -4
  13. package/dist/retry/index.d.ts +1 -1
  14. package/dist/retry/index.js +2 -2
  15. package/dist/retry/index.js.map +1 -1
  16. package/dist/scheduler/index.d.ts +6 -6
  17. package/dist/security/index.d.ts +28 -28
  18. package/dist/server/index.js +1 -1
  19. package/dist/server/index.js.map +1 -1
  20. package/dist/server-health/index.d.ts +17 -17
  21. package/dist/server-metrics/index.js +170 -174
  22. package/dist/server-metrics/index.js.map +1 -1
  23. package/dist/server-security/index.d.ts +9 -9
  24. package/dist/vite/index.js +4 -5
  25. package/dist/vite/index.js.map +1 -1
  26. package/dist/websocket/index.d.ts +7 -7
  27. package/package.json +52 -103
  28. package/src/cli/apps/AlephaPackageBuilderCli.ts +7 -2
  29. package/src/cli/assets/appRouterTs.ts +9 -0
  30. package/src/cli/assets/indexHtml.ts +2 -1
  31. package/src/cli/assets/mainBrowserTs.ts +10 -0
  32. package/src/cli/commands/CoreCommands.ts +6 -5
  33. package/src/cli/commands/DrizzleCommands.ts +65 -57
  34. package/src/cli/commands/VerifyCommands.ts +1 -1
  35. package/src/cli/services/ProjectUtils.ts +44 -38
  36. package/src/orm/providers/DrizzleKitProvider.ts +1 -1
  37. package/src/retry/descriptors/$retry.ts +5 -3
  38. package/src/server/providers/NodeHttpServerProvider.ts +1 -1
  39. package/src/vite/helpers/boot.ts +3 -3
  40. package/dist/api-files/index.cjs +0 -1293
  41. package/dist/api-files/index.cjs.map +0 -1
  42. package/dist/api-files/index.d.cts +0 -829
  43. package/dist/api-jobs/index.cjs +0 -274
  44. package/dist/api-jobs/index.cjs.map +0 -1
  45. package/dist/api-jobs/index.d.cts +0 -654
  46. package/dist/api-notifications/index.cjs +0 -380
  47. package/dist/api-notifications/index.cjs.map +0 -1
  48. package/dist/api-notifications/index.d.cts +0 -289
  49. package/dist/api-parameters/index.cjs +0 -66
  50. package/dist/api-parameters/index.cjs.map +0 -1
  51. package/dist/api-parameters/index.d.cts +0 -84
  52. package/dist/api-users/index.cjs +0 -6009
  53. package/dist/api-users/index.cjs.map +0 -1
  54. package/dist/api-users/index.d.cts +0 -4740
  55. package/dist/api-verifications/index.cjs +0 -407
  56. package/dist/api-verifications/index.cjs.map +0 -1
  57. package/dist/api-verifications/index.d.cts +0 -207
  58. package/dist/batch/index.cjs +0 -408
  59. package/dist/batch/index.cjs.map +0 -1
  60. package/dist/batch/index.d.cts +0 -330
  61. package/dist/bin/index.cjs +0 -17
  62. package/dist/bin/index.cjs.map +0 -1
  63. package/dist/bin/index.d.cts +0 -1
  64. package/dist/bucket/index.cjs +0 -303
  65. package/dist/bucket/index.cjs.map +0 -1
  66. package/dist/bucket/index.d.cts +0 -355
  67. package/dist/cache/index.cjs +0 -241
  68. package/dist/cache/index.cjs.map +0 -1
  69. package/dist/cache/index.d.cts +0 -202
  70. package/dist/cache-redis/index.cjs +0 -84
  71. package/dist/cache-redis/index.cjs.map +0 -1
  72. package/dist/cache-redis/index.d.cts +0 -40
  73. package/dist/cli/chunk-DSlc6foC.cjs +0 -43
  74. package/dist/cli/dist-BBPjuQ56.js +0 -2778
  75. package/dist/cli/dist-Sz2EXvQX.cjs.map +0 -1
  76. package/dist/cli/index.cjs +0 -1241
  77. package/dist/cli/index.cjs.map +0 -1
  78. package/dist/cli/index.d.cts +0 -422
  79. package/dist/command/index.cjs +0 -693
  80. package/dist/command/index.cjs.map +0 -1
  81. package/dist/command/index.d.cts +0 -340
  82. package/dist/core/index.cjs +0 -2264
  83. package/dist/core/index.cjs.map +0 -1
  84. package/dist/core/index.d.cts +0 -1927
  85. package/dist/datetime/index.cjs +0 -318
  86. package/dist/datetime/index.cjs.map +0 -1
  87. package/dist/datetime/index.d.cts +0 -145
  88. package/dist/email/index.cjs +0 -10874
  89. package/dist/email/index.cjs.map +0 -1
  90. package/dist/email/index.d.cts +0 -186
  91. package/dist/fake/index.cjs +0 -34641
  92. package/dist/fake/index.cjs.map +0 -1
  93. package/dist/fake/index.d.cts +0 -74
  94. package/dist/file/index.cjs +0 -1212
  95. package/dist/file/index.cjs.map +0 -1
  96. package/dist/file/index.d.cts +0 -698
  97. package/dist/lock/index.cjs +0 -226
  98. package/dist/lock/index.cjs.map +0 -1
  99. package/dist/lock/index.d.cts +0 -361
  100. package/dist/lock-redis/index.cjs +0 -113
  101. package/dist/lock-redis/index.cjs.map +0 -1
  102. package/dist/lock-redis/index.d.cts +0 -24
  103. package/dist/logger/index.cjs +0 -521
  104. package/dist/logger/index.cjs.map +0 -1
  105. package/dist/logger/index.d.cts +0 -281
  106. package/dist/orm/index.cjs +0 -2986
  107. package/dist/orm/index.cjs.map +0 -1
  108. package/dist/orm/index.d.cts +0 -2213
  109. package/dist/queue/index.cjs +0 -1044
  110. package/dist/queue/index.cjs.map +0 -1
  111. package/dist/queue/index.d.cts +0 -1265
  112. package/dist/queue-redis/index.cjs +0 -873
  113. package/dist/queue-redis/index.cjs.map +0 -1
  114. package/dist/queue-redis/index.d.cts +0 -82
  115. package/dist/redis/index.cjs +0 -153
  116. package/dist/redis/index.cjs.map +0 -1
  117. package/dist/redis/index.d.cts +0 -82
  118. package/dist/retry/index.cjs +0 -146
  119. package/dist/retry/index.cjs.map +0 -1
  120. package/dist/retry/index.d.cts +0 -172
  121. package/dist/router/index.cjs +0 -111
  122. package/dist/router/index.cjs.map +0 -1
  123. package/dist/router/index.d.cts +0 -46
  124. package/dist/scheduler/index.cjs +0 -576
  125. package/dist/scheduler/index.cjs.map +0 -1
  126. package/dist/scheduler/index.d.cts +0 -145
  127. package/dist/security/index.cjs +0 -2402
  128. package/dist/security/index.cjs.map +0 -1
  129. package/dist/security/index.d.cts +0 -598
  130. package/dist/server/index.cjs +0 -1680
  131. package/dist/server/index.cjs.map +0 -1
  132. package/dist/server/index.d.cts +0 -810
  133. package/dist/server-auth/index.cjs +0 -3146
  134. package/dist/server-auth/index.cjs.map +0 -1
  135. package/dist/server-auth/index.d.cts +0 -1164
  136. package/dist/server-cache/index.cjs +0 -252
  137. package/dist/server-cache/index.cjs.map +0 -1
  138. package/dist/server-cache/index.d.cts +0 -164
  139. package/dist/server-compress/index.cjs +0 -141
  140. package/dist/server-compress/index.cjs.map +0 -1
  141. package/dist/server-compress/index.d.cts +0 -38
  142. package/dist/server-cookies/index.cjs +0 -234
  143. package/dist/server-cookies/index.cjs.map +0 -1
  144. package/dist/server-cookies/index.d.cts +0 -144
  145. package/dist/server-cors/index.cjs +0 -201
  146. package/dist/server-cors/index.cjs.map +0 -1
  147. package/dist/server-cors/index.d.cts +0 -140
  148. package/dist/server-health/index.cjs +0 -62
  149. package/dist/server-health/index.cjs.map +0 -1
  150. package/dist/server-health/index.d.cts +0 -58
  151. package/dist/server-helmet/index.cjs +0 -131
  152. package/dist/server-helmet/index.cjs.map +0 -1
  153. package/dist/server-helmet/index.d.cts +0 -97
  154. package/dist/server-links/index.cjs +0 -992
  155. package/dist/server-links/index.cjs.map +0 -1
  156. package/dist/server-links/index.d.cts +0 -513
  157. package/dist/server-metrics/index.cjs +0 -4535
  158. package/dist/server-metrics/index.cjs.map +0 -1
  159. package/dist/server-metrics/index.d.cts +0 -35
  160. package/dist/server-multipart/index.cjs +0 -237
  161. package/dist/server-multipart/index.cjs.map +0 -1
  162. package/dist/server-multipart/index.d.cts +0 -50
  163. package/dist/server-proxy/index.cjs +0 -186
  164. package/dist/server-proxy/index.cjs.map +0 -1
  165. package/dist/server-proxy/index.d.cts +0 -234
  166. package/dist/server-rate-limit/index.cjs +0 -241
  167. package/dist/server-rate-limit/index.cjs.map +0 -1
  168. package/dist/server-rate-limit/index.d.cts +0 -183
  169. package/dist/server-security/index.cjs +0 -316
  170. package/dist/server-security/index.cjs.map +0 -1
  171. package/dist/server-security/index.d.cts +0 -173
  172. package/dist/server-static/index.cjs +0 -170
  173. package/dist/server-static/index.cjs.map +0 -1
  174. package/dist/server-static/index.d.cts +0 -121
  175. package/dist/server-swagger/index.cjs +0 -1021
  176. package/dist/server-swagger/index.cjs.map +0 -1
  177. package/dist/server-swagger/index.d.cts +0 -382
  178. package/dist/sms/index.cjs +0 -221
  179. package/dist/sms/index.cjs.map +0 -1
  180. package/dist/sms/index.d.cts +0 -130
  181. package/dist/thread/index.cjs +0 -350
  182. package/dist/thread/index.cjs.map +0 -1
  183. package/dist/thread/index.d.cts +0 -260
  184. package/dist/topic/index.cjs +0 -282
  185. package/dist/topic/index.cjs.map +0 -1
  186. package/dist/topic/index.d.cts +0 -523
  187. package/dist/topic-redis/index.cjs +0 -71
  188. package/dist/topic-redis/index.cjs.map +0 -1
  189. package/dist/topic-redis/index.d.cts +0 -42
  190. package/dist/vite/index.cjs +0 -1077
  191. package/dist/vite/index.cjs.map +0 -1
  192. package/dist/vite/index.d.cts +0 -542
  193. package/dist/websocket/index.cjs +0 -1117
  194. package/dist/websocket/index.cjs.map +0 -1
  195. package/dist/websocket/index.d.cts +0 -861
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["AlephaError","DateTimeProvider","t","Descriptor","DateTimeProvider","keysToDelete: string[]","KIND","alepha"],"sources":["../../src/cache/errors/CacheError.ts","../../src/cache/providers/CacheProvider.ts","../../src/cache/providers/MemoryCacheProvider.ts","../../src/cache/descriptors/$cache.ts","../../src/cache/index.ts"],"sourcesContent":["import { AlephaError } from \"alepha\";\n\nexport class CacheError extends AlephaError {}\n","/**\n * Cache provider interface.\n *\n * All methods are asynchronous and return promises.\n * Values are stored as Uint8Array.\n */\nexport abstract class CacheProvider {\n /**\n * Get the value of a key.\n *\n * @param name Cache name, used to group keys. Should be Redis-like \"some:group:name\" format.\n * @param key The key of the value to get.\n *\n * @return The value of the key, or undefined if the key does not exist.\n */\n public abstract get(\n name: string,\n key: string,\n ): Promise<Uint8Array | undefined>;\n\n /**\n * Set the string value of a key.\n *\n * @param name Cache name, used to group keys. Should be Redis-like \"some:group:name\" format.\n * @param key The key of the value to set.\n * @param value The value to set.\n * @param ttl The time-to-live of the key, in milliseconds.\n *\n * @return The value of the key.\n */\n public abstract set(\n name: string,\n key: string,\n value: Uint8Array,\n ttl?: number,\n ): Promise<Uint8Array>;\n\n /**\n * Remove the specified keys.\n *\n * @param name Cache name, used to group keys. Should be Redis-like \"some:group:name\" format.\n * @param keys The keys to delete.\n */\n public abstract del(name: string, ...keys: string[]): Promise<void>;\n\n public abstract has(name: string, key: string): Promise<boolean>;\n\n public abstract keys(name: string, filter?: string): Promise<string[]>;\n\n /**\n * Remove all keys from all cache names.\n */\n public abstract clear(): Promise<void>;\n}\n","import { $inject } from \"alepha\";\nimport { DateTimeProvider, type Timeout } from \"alepha/datetime\";\nimport { $logger } from \"alepha/logger\";\nimport type { CacheProvider } from \"./CacheProvider.ts\";\n\ntype CacheName = string;\ntype CacheKey = string;\ntype CacheValue = {\n data?: Uint8Array;\n timeout?: Timeout;\n};\n\nexport class MemoryCacheProvider implements CacheProvider {\n protected readonly dateTimeProvider = $inject(DateTimeProvider);\n protected readonly log = $logger();\n\n protected store: Record<CacheName, Record<CacheKey, CacheValue>> = {};\n\n public async get(name: string, key: string): Promise<Uint8Array | undefined> {\n return this.store[name]?.[key]?.data;\n }\n\n public async set(\n name: string,\n key: string,\n value: Uint8Array,\n ttl?: number,\n ): Promise<Uint8Array> {\n if (this.store[name] == null) {\n this.store[name] = {};\n }\n\n this.store[name][key] ??= {};\n this.store[name][key].data = value;\n\n this.log.debug(`Setting cache for name`, { name, key, ttl });\n\n // clear previous timeout if exists\n if (this.store[name][key].timeout) {\n this.dateTimeProvider.clearTimeout(this.store[name][key].timeout);\n this.store[name][key].timeout = undefined;\n }\n\n if (ttl) {\n this.store[name][key].timeout = this.dateTimeProvider.createTimeout(\n () => this.del(name, key),\n ttl,\n );\n }\n\n return this.store[name][key].data;\n }\n\n public async del(name: string, ...keys: string[]): Promise<void> {\n // delete all keys in name\n if (keys.length === 0) {\n this.log.debug(`Deleting all cache for name`, { name });\n\n if (this.store[name]) {\n for (const key of Object.keys(this.store[name])) {\n const timeout = this.store[name][key]?.timeout;\n if (timeout) {\n this.dateTimeProvider.clearTimeout(timeout);\n }\n }\n }\n delete this.store[name];\n return;\n }\n\n this.log.debug(`Deleting cache for name`, { name, keys });\n\n // delete specific keys in name\n for (const key of keys) {\n if (this.store[name] == null) break;\n\n const timeout = this.store[name][key]?.timeout;\n if (timeout) {\n this.dateTimeProvider.clearTimeout(timeout);\n }\n\n delete this.store[name][key];\n }\n\n if (Object.keys(this.store[name] ?? {}).length === 0) {\n // if name is empty, delete it\n delete this.store[name];\n }\n }\n\n public async has(name: string, key: string): Promise<boolean> {\n return this.store[name]?.[key]?.data != null;\n }\n\n public async keys(name: string, filter?: string): Promise<string[]> {\n const store = this.store[name] ?? {};\n const keys = Object.keys(store);\n if (filter) {\n return keys.filter((key) => key.startsWith(filter));\n }\n return keys;\n }\n\n public async clear(): Promise<void> {\n this.log.debug(\"Clearing all cache\");\n\n // Clear all timeouts before clearing the store\n for (const name of Object.keys(this.store)) {\n for (const key of Object.keys(this.store[name])) {\n const timeout = this.store[name][key]?.timeout;\n if (timeout) {\n this.dateTimeProvider.clearTimeout(timeout);\n }\n }\n }\n\n this.store = {};\n }\n}\n","import {\n $env,\n $inject,\n createDescriptor,\n Descriptor,\n type InstantiableClass,\n KIND,\n t,\n} from \"alepha\";\nimport { DateTimeProvider, type DurationLike } from \"alepha/datetime\";\nimport { CacheError } from \"../errors/CacheError.ts\";\nimport { CacheProvider } from \"../providers/CacheProvider.ts\";\nimport { MemoryCacheProvider } from \"../providers/MemoryCacheProvider.ts\";\n\n/**\n * Creates a cache descriptor for high-performance data caching with automatic management.\n *\n * Provides a caching layer that improves application performance by storing frequently accessed\n * data in memory or external stores like Redis, with support for both function result caching\n * and manual cache operations.\n *\n * **Key Features**\n * - Automatic function result caching based on input parameters\n * - Multiple storage backends (in-memory, Redis, custom providers)\n * - Intelligent serialization for JSON, strings, and binary data\n * - Configurable TTL with automatic expiration\n * - Pattern-based cache invalidation with wildcard support\n * - Environment controls to enable/disable caching\n *\n * **Storage Backends**\n * - Memory: Fast in-memory cache (default for development)\n * - Redis: Distributed cache for production environments\n * - Custom providers: Implement your own storage backend\n *\n * @example\n * ```ts\n * class DataService {\n * // Function result caching\n * getUserData = $cache({\n * name: \"user-data\",\n * ttl: [10, \"minutes\"],\n * handler: async (userId: string) => {\n * return await database.users.findById(userId);\n * }\n * });\n *\n * // Manual cache operations\n * sessionCache = $cache<UserSession>({\n * name: \"sessions\",\n * ttl: [1, \"hour\"]\n * });\n *\n * async storeSession(id: string, session: UserSession) {\n * await this.sessionCache.set(id, session);\n * }\n *\n * async invalidateUserSessions(userId: string) {\n * await this.sessionCache.invalidate(`user:${userId}:*`);\n * }\n * }\n * ```\n */\nexport const $cache = <TReturn = string, TParameter extends any[] = any[]>(\n options: CacheDescriptorOptions<TReturn, TParameter> = {},\n): CacheDescriptorFn<TReturn, TParameter> => {\n const instance = createDescriptor(\n CacheDescriptor<TReturn, TParameter>,\n options,\n );\n const fn = (...args: TParameter): Promise<TReturn> => instance.run(...args);\n return Object.setPrototypeOf(fn, instance) as CacheDescriptorFn<\n TReturn,\n TParameter\n >;\n};\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport interface CacheDescriptorOptions<\n TReturn = any,\n TParameter extends any[] = any[],\n> {\n /**\n * The cache name. This is useful for invalidating multiple caches at once.\n *\n * Store key as `cache:$name:$key`.\n *\n * @default Name of the key of the class.\n */\n name?: string;\n\n /**\n * Function which returns cached data.\n */\n handler?: (...args: TParameter) => TReturn;\n\n /**\n * The key generator for the cache.\n * If not provided, the arguments will be json.stringify().\n */\n key?: (...args: TParameter) => string;\n\n /**\n * The store provider for the cache.\n * If not provided, the default store provider will be used.\n */\n provider?: InstantiableClass<CacheProvider> | \"memory\";\n\n /**\n * The time-to-live for the cache in seconds.\n * Set 0 to skip expiration.\n *\n * @default 300 (5 minutes).\n */\n ttl?: DurationLike;\n\n /**\n * If the cache is disabled.\n */\n disabled?: boolean;\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nconst envSchema = t.object({\n CACHE_ENABLED: t.boolean({ default: true }),\n CACHE_DEFAULT_TTL: t.number({\n default: 300, // 5 minutes\n description: \"The default time to live for cache entries. In seconds.\",\n }),\n});\n\nexport class CacheDescriptor<\n TReturn = any,\n TParameter extends any[] = any[],\n> extends Descriptor<CacheDescriptorOptions<TReturn, TParameter>> {\n protected readonly env = $env(envSchema);\n protected readonly dateTimeProvider = $inject(DateTimeProvider);\n protected readonly provider = this.$provider();\n protected encoder: TextEncoder = new TextEncoder();\n protected decoder: TextDecoder = new TextDecoder();\n protected codes = {\n BINARY: 0x01,\n JSON: 0x02,\n STRING: 0x03,\n };\n\n public get container(): string {\n return (\n this.options.name ??\n `${this.config.service.name}:${this.config.propertyKey}`\n );\n }\n\n public async run(...args: TParameter): Promise<TReturn> {\n const handler = this.options.handler;\n if (!handler) {\n throw new Error(\"Cache handler is not defined.\");\n }\n\n const key = this.key(...args);\n const cached = await this.get(key);\n if (cached) {\n return cached;\n }\n\n const result = await handler(...args);\n // note: when exception occurs, don't cache the result\n\n await this.set(key, result);\n\n return result;\n }\n\n public key(...args: TParameter): string {\n return this.options.key ? this.options.key(...args) : JSON.stringify(args);\n }\n\n public async invalidate(...keys: string[]): Promise<void> {\n const keysToDelete: string[] = [];\n\n for (const key of keys) {\n if (key.endsWith(\"*\")) {\n const result = await this.provider.keys(\n this.container,\n key.slice(0, -1),\n );\n keysToDelete.push(...result);\n } else {\n keysToDelete.push(key);\n }\n }\n\n await this.provider.del(this.container, ...keysToDelete);\n }\n\n public async set(\n key: string,\n value: TReturn,\n ttl?: DurationLike,\n ): Promise<void> {\n const px = this.dateTimeProvider\n .duration(\n ttl ?? this.options.ttl ?? [this.env.CACHE_DEFAULT_TTL, \"seconds\"],\n )\n .as(\"milliseconds\");\n\n await this.provider.set(\n this.container,\n key,\n this.serialize(value),\n px > 0 ? px : undefined,\n );\n }\n\n public async get(key: string): Promise<TReturn | undefined> {\n if (\n !this.alepha.isStarted() ||\n this.options.disabled ||\n !this.env.CACHE_ENABLED\n ) {\n return undefined;\n }\n\n const data = await this.provider.get(this.container, key);\n if (data) {\n return await this.deserialize<TReturn>(data);\n }\n\n return undefined;\n }\n\n protected serialize<TReturn>(value: TReturn): Uint8Array {\n if (value instanceof Uint8Array) {\n return new Uint8Array([this.codes.BINARY, ...value]); // TODO: check if copy is ok?\n }\n\n if (typeof value === \"string\") {\n return new Uint8Array([this.codes.STRING, ...this.encoder.encode(value)]);\n }\n\n return new Uint8Array([\n this.codes.JSON,\n ...this.encoder.encode(JSON.stringify(value)),\n ]);\n }\n\n protected async deserialize<TReturn>(\n uint8Array: Uint8Array,\n ): Promise<TReturn> {\n const type = uint8Array[0];\n const payload = uint8Array.slice(1);\n\n if (type === this.codes.BINARY) {\n return payload as TReturn;\n }\n if (type === this.codes.JSON) {\n return JSON.parse(this.decoder.decode(payload)) as TReturn;\n }\n if (type === this.codes.STRING) {\n return this.decoder.decode(payload) as TReturn;\n }\n\n throw new CacheError(`Unknown serialization type: ${type}`);\n }\n\n protected $provider(): CacheProvider {\n if (!this.options.provider) {\n return this.alepha.inject(CacheProvider);\n }\n\n if (this.options.provider === \"memory\") {\n return this.alepha.inject(MemoryCacheProvider);\n }\n\n return this.alepha.inject(this.options.provider);\n }\n}\n\nexport interface CacheDescriptorFn<\n TReturn = any,\n TParameter extends any[] = any[],\n> extends CacheDescriptor<TReturn, TParameter> {\n /**\n * Run the cache descriptor with the provided arguments.\n */\n (...args: TParameter): Promise<TReturn>;\n}\n\n$cache[KIND] = CacheDescriptor;\n","import { $module } from \"alepha\";\nimport { $cache } from \"./descriptors/$cache.ts\";\nimport { CacheProvider } from \"./providers/CacheProvider.ts\";\nimport { MemoryCacheProvider } from \"./providers/MemoryCacheProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./descriptors/$cache.ts\";\nexport * from \"./providers/CacheProvider.ts\";\nexport * from \"./providers/MemoryCacheProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Provides high-performance caching capabilities for Alepha applications with configurable TTL and multiple storage backends.\n *\n * The cache module enables declarative caching through the `$cache` descriptor, allowing you to cache method results,\n * API responses, or computed values with automatic invalidation and type safety. It supports both in-memory and\n * persistent storage backends for different performance and durability requirements.\n *\n * @see {@link $cache}\n * @see {@link CacheProvider}\n * @module alepha.cache\n */\nexport const AlephaCache = $module({\n name: \"alepha.cache\",\n descriptors: [$cache],\n services: [CacheProvider, MemoryCacheProvider],\n register: (alepha) =>\n alepha.with({\n optional: true,\n provide: CacheProvider,\n use: MemoryCacheProvider,\n }),\n});\n"],"mappings":";;;;;AAEA,IAAa,aAAb,cAAgCA,mBAAY;;;;;;;;;;ACI5C,IAAsB,gBAAtB,MAAoC;;;;ACMpC,IAAa,sBAAb,MAA0D;CACxD,AAAmB,uCAA2BC,iCAAiB;CAC/D,AAAmB,kCAAe;CAElC,AAAU,QAAyD,EAAE;CAErE,MAAa,IAAI,MAAc,KAA8C;AAC3E,SAAO,KAAK,MAAM,QAAQ,MAAM;;CAGlC,MAAa,IACX,MACA,KACA,OACA,KACqB;AACrB,MAAI,KAAK,MAAM,SAAS,KACtB,MAAK,MAAM,QAAQ,EAAE;AAGvB,OAAK,MAAM,MAAM,SAAS,EAAE;AAC5B,OAAK,MAAM,MAAM,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M,QAAQ,GAAG,KAAK,QAAQ,OAAO,MAAM,CAAC,CAAC;AAG3E,SAAO,IAAI,WAAW,CACpB,KAAK,MAAM,MACX,GAAG,KAAK,QAAQ,OAAO,KAAK,UAAU,MAAM,CAAC,CAC9C,CAAC;;CAGJ,MAAgB,YACd,YACkB;EAClB,MAAM,OAAO,WAAW;EACxB,MAAM,UAAU,WAAW,MAAM,EAAE;AAEnC,MAAI,SAAS,KAAK,MAAM,OACtB,QAAO;AAET,MAAI,SAAS,KAAK,MAAM,KACtB,QAAO,KAAK,MAAM,KAAK,QAAQ,OAAO,QAAQ,CAAC;AAEjD,MAAI,SAAS,KAAK,MAAM,OACtB,QAAO,KAAK,QAAQ,OAAO,QAAQ;AAGrC,QAAM,IAAI,WAAW,+BAA+B,OAAO;;CAG7D,AAAU,YAA2B;AACnC,MAAI,CAAC,KAAK,QAAQ,SAChB,QAAO,KAAK,OAAO,OAAO,cAAc;AAG1C,MAAI,KAAK,QAAQ,aAAa,SAC5B,QAAO,KAAK,OAAO,OAAO,oBAAoB;AAGhD,SAAO,KAAK,OAAO,OAAO,KAAK,QAAQ,SAAS;;;AAcpD,OAAOC,eAAQ;;;;;;;;;;;;;;;ACzQf,MAAa,kCAAsB;CACjC,MAAM;CACN,aAAa,CAAC,OAAO;CACrB,UAAU,CAAC,eAAe,oBAAoB;CAC9C,WAAW,aACTC,SAAO,KAAK;EACV,UAAU;EACV,SAAS;EACT,KAAK;EACN,CAAC;CACL,CAAC"}
@@ -1,202 +0,0 @@
1
- import * as alepha0 from "alepha";
2
- import { Descriptor, InstantiableClass, KIND } from "alepha";
3
- import { DateTimeProvider, DurationLike, Timeout } from "alepha/datetime";
4
- import * as alepha_logger0 from "alepha/logger";
5
-
6
- //#region src/cache/providers/CacheProvider.d.ts
7
- /**
8
- * Cache provider interface.
9
- *
10
- * All methods are asynchronous and return promises.
11
- * Values are stored as Uint8Array.
12
- */
13
- declare abstract class CacheProvider {
14
- /**
15
- * Get the value of a key.
16
- *
17
- * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
18
- * @param key The key of the value to get.
19
- *
20
- * @return The value of the key, or undefined if the key does not exist.
21
- */
22
- abstract get(name: string, key: string): Promise<Uint8Array | undefined>;
23
- /**
24
- * Set the string value of a key.
25
- *
26
- * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
27
- * @param key The key of the value to set.
28
- * @param value The value to set.
29
- * @param ttl The time-to-live of the key, in milliseconds.
30
- *
31
- * @return The value of the key.
32
- */
33
- abstract set(name: string, key: string, value: Uint8Array, ttl?: number): Promise<Uint8Array>;
34
- /**
35
- * Remove the specified keys.
36
- *
37
- * @param name Cache name, used to group keys. Should be Redis-like "some:group:name" format.
38
- * @param keys The keys to delete.
39
- */
40
- abstract del(name: string, ...keys: string[]): Promise<void>;
41
- abstract has(name: string, key: string): Promise<boolean>;
42
- abstract keys(name: string, filter?: string): Promise<string[]>;
43
- /**
44
- * Remove all keys from all cache names.
45
- */
46
- abstract clear(): Promise<void>;
47
- }
48
- //#endregion
49
- //#region src/cache/descriptors/$cache.d.ts
50
- /**
51
- * Creates a cache descriptor for high-performance data caching with automatic management.
52
- *
53
- * Provides a caching layer that improves application performance by storing frequently accessed
54
- * data in memory or external stores like Redis, with support for both function result caching
55
- * and manual cache operations.
56
- *
57
- * **Key Features**
58
- * - Automatic function result caching based on input parameters
59
- * - Multiple storage backends (in-memory, Redis, custom providers)
60
- * - Intelligent serialization for JSON, strings, and binary data
61
- * - Configurable TTL with automatic expiration
62
- * - Pattern-based cache invalidation with wildcard support
63
- * - Environment controls to enable/disable caching
64
- *
65
- * **Storage Backends**
66
- * - Memory: Fast in-memory cache (default for development)
67
- * - Redis: Distributed cache for production environments
68
- * - Custom providers: Implement your own storage backend
69
- *
70
- * @example
71
- * ```ts
72
- * class DataService {
73
- * // Function result caching
74
- * getUserData = $cache({
75
- * name: "user-data",
76
- * ttl: [10, "minutes"],
77
- * handler: async (userId: string) => {
78
- * return await database.users.findById(userId);
79
- * }
80
- * });
81
- *
82
- * // Manual cache operations
83
- * sessionCache = $cache<UserSession>({
84
- * name: "sessions",
85
- * ttl: [1, "hour"]
86
- * });
87
- *
88
- * async storeSession(id: string, session: UserSession) {
89
- * await this.sessionCache.set(id, session);
90
- * }
91
- *
92
- * async invalidateUserSessions(userId: string) {
93
- * await this.sessionCache.invalidate(`user:${userId}:*`);
94
- * }
95
- * }
96
- * ```
97
- */
98
- declare const $cache: {
99
- <TReturn = string, TParameter extends any[] = any[]>(options?: CacheDescriptorOptions<TReturn, TParameter>): CacheDescriptorFn<TReturn, TParameter>;
100
- [KIND]: typeof CacheDescriptor;
101
- };
102
- interface CacheDescriptorOptions<TReturn = any, TParameter extends any[] = any[]> {
103
- /**
104
- * The cache name. This is useful for invalidating multiple caches at once.
105
- *
106
- * Store key as `cache:$name:$key`.
107
- *
108
- * @default Name of the key of the class.
109
- */
110
- name?: string;
111
- /**
112
- * Function which returns cached data.
113
- */
114
- handler?: (...args: TParameter) => TReturn;
115
- /**
116
- * The key generator for the cache.
117
- * If not provided, the arguments will be json.stringify().
118
- */
119
- key?: (...args: TParameter) => string;
120
- /**
121
- * The store provider for the cache.
122
- * If not provided, the default store provider will be used.
123
- */
124
- provider?: InstantiableClass<CacheProvider> | "memory";
125
- /**
126
- * The time-to-live for the cache in seconds.
127
- * Set 0 to skip expiration.
128
- *
129
- * @default 300 (5 minutes).
130
- */
131
- ttl?: DurationLike;
132
- /**
133
- * If the cache is disabled.
134
- */
135
- disabled?: boolean;
136
- }
137
- declare class CacheDescriptor<TReturn = any, TParameter extends any[] = any[]> extends Descriptor<CacheDescriptorOptions<TReturn, TParameter>> {
138
- protected readonly env: {
139
- CACHE_ENABLED: boolean;
140
- CACHE_DEFAULT_TTL: number;
141
- };
142
- protected readonly dateTimeProvider: DateTimeProvider;
143
- protected readonly provider: CacheProvider;
144
- protected encoder: TextEncoder;
145
- protected decoder: TextDecoder;
146
- protected codes: {
147
- BINARY: number;
148
- JSON: number;
149
- STRING: number;
150
- };
151
- get container(): string;
152
- run(...args: TParameter): Promise<TReturn>;
153
- key(...args: TParameter): string;
154
- invalidate(...keys: string[]): Promise<void>;
155
- set(key: string, value: TReturn, ttl?: DurationLike): Promise<void>;
156
- get(key: string): Promise<TReturn | undefined>;
157
- protected serialize<TReturn>(value: TReturn): Uint8Array;
158
- protected deserialize<TReturn>(uint8Array: Uint8Array): Promise<TReturn>;
159
- protected $provider(): CacheProvider;
160
- }
161
- interface CacheDescriptorFn<TReturn = any, TParameter extends any[] = any[]> extends CacheDescriptor<TReturn, TParameter> {
162
- /**
163
- * Run the cache descriptor with the provided arguments.
164
- */
165
- (...args: TParameter): Promise<TReturn>;
166
- }
167
- //#endregion
168
- //#region src/cache/providers/MemoryCacheProvider.d.ts
169
- type CacheName = string;
170
- type CacheKey = string;
171
- type CacheValue = {
172
- data?: Uint8Array;
173
- timeout?: Timeout;
174
- };
175
- declare class MemoryCacheProvider implements CacheProvider {
176
- protected readonly dateTimeProvider: DateTimeProvider;
177
- protected readonly log: alepha_logger0.Logger;
178
- protected store: Record<CacheName, Record<CacheKey, CacheValue>>;
179
- get(name: string, key: string): Promise<Uint8Array | undefined>;
180
- set(name: string, key: string, value: Uint8Array, ttl?: number): Promise<Uint8Array>;
181
- del(name: string, ...keys: string[]): Promise<void>;
182
- has(name: string, key: string): Promise<boolean>;
183
- keys(name: string, filter?: string): Promise<string[]>;
184
- clear(): Promise<void>;
185
- }
186
- //#endregion
187
- //#region src/cache/index.d.ts
188
- /**
189
- * Provides high-performance caching capabilities for Alepha applications with configurable TTL and multiple storage backends.
190
- *
191
- * The cache module enables declarative caching through the `$cache` descriptor, allowing you to cache method results,
192
- * API responses, or computed values with automatic invalidation and type safety. It supports both in-memory and
193
- * persistent storage backends for different performance and durability requirements.
194
- *
195
- * @see {@link $cache}
196
- * @see {@link CacheProvider}
197
- * @module alepha.cache
198
- */
199
- declare const AlephaCache: alepha0.Service<alepha0.Module>;
200
- //#endregion
201
- export { $cache, AlephaCache, CacheDescriptor, CacheDescriptorFn, CacheDescriptorOptions, CacheProvider, MemoryCacheProvider };
202
- //# sourceMappingURL=index.d.cts.map
@@ -1,84 +0,0 @@
1
- let alepha = require("alepha");
2
- let alepha_cache = require("alepha/cache");
3
- let alepha_logger = require("alepha/logger");
4
- let alepha_redis = require("alepha/redis");
5
-
6
- //#region src/cache-redis/providers/RedisCacheProvider.ts
7
- const envSchema = alepha.t.object({ REDIS_CACHE_PREFIX: alepha.t.optional(alepha.t.text({ description: "Force a prefix for all cache keys in Redis. Useful for testing or multi-tenant applications." })) });
8
- var RedisCacheProvider = class {
9
- log = (0, alepha_logger.$logger)();
10
- redisProvider = (0, alepha.$inject)(alepha_redis.RedisProvider);
11
- env = (0, alepha.$env)(envSchema);
12
- alepha = (0, alepha.$inject)(alepha.Alepha);
13
- async get(name, key) {
14
- if (!this.alepha.isStarted()) return;
15
- const keyWithPrefix = this.prefix(name, key);
16
- const buffer = await this.redisProvider.get(keyWithPrefix);
17
- if (!buffer) return;
18
- this.log.debug(`Cache hit`, {
19
- size: buffer.byteLength,
20
- key: keyWithPrefix
21
- });
22
- return new Uint8Array(buffer);
23
- }
24
- async set(name, key, value, ttl) {
25
- if (!this.alepha.isReady()) return new Uint8Array(Buffer.from(value));
26
- const buffer = Buffer.from(value);
27
- const prefix = this.prefix(name, key);
28
- if (ttl) return new Uint8Array(await this.redisProvider.set(prefix, buffer, { expiration: {
29
- type: "PX",
30
- value: ttl
31
- } }));
32
- return new Uint8Array(await this.redisProvider.set(prefix, buffer));
33
- }
34
- async del(name, ...keys) {
35
- const nameKey = this.prefix(name);
36
- if (keys.length === 0) {
37
- const keys$1 = await this.redisProvider.keys(`${nameKey}:*`);
38
- await this.redisProvider.del(keys$1);
39
- return;
40
- }
41
- await this.redisProvider.del(keys.map((key) => key.startsWith(nameKey) ? key : this.prefix(name, key)));
42
- }
43
- async has(name, key) {
44
- return this.get(name, key).then((value) => value != null);
45
- }
46
- async keys(name, filter) {
47
- if (filter) return await this.redisProvider.keys(`${this.prefix(name)}:${filter}*`);
48
- return this.redisProvider.keys(`${this.prefix(name)}:*`);
49
- }
50
- async clear() {
51
- this.log.debug("Clearing all cache");
52
- const pattern = `${this.prefix()}:*`;
53
- const keys = await this.redisProvider.keys(pattern);
54
- await this.redisProvider.del(keys);
55
- }
56
- prefix(...path) {
57
- const parts = ["cache", ...path];
58
- if (this.env.REDIS_CACHE_PREFIX) parts.unshift(this.env.REDIS_CACHE_PREFIX);
59
- return parts.join(":");
60
- }
61
- };
62
-
63
- //#endregion
64
- //#region src/cache-redis/index.ts
65
- /**
66
- * Plugin for Alepha Cache that provides Redis caching capabilities.
67
- *
68
- * @see {@link RedisCacheProvider}
69
- * @module alepha.cache.redis
70
- */
71
- const AlephaCacheRedis = (0, alepha.$module)({
72
- name: "alepha.cache.redis",
73
- services: [RedisCacheProvider],
74
- register: (alepha$1) => alepha$1.with({
75
- provide: alepha_cache.CacheProvider,
76
- use: RedisCacheProvider,
77
- optional: true
78
- }).with(alepha_cache.AlephaCache)
79
- });
80
-
81
- //#endregion
82
- exports.AlephaCacheRedis = AlephaCacheRedis;
83
- exports.RedisCacheProvider = RedisCacheProvider;
84
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["t","RedisProvider","Alepha","keys","alepha","CacheProvider","AlephaCache"],"sources":["../../src/cache-redis/providers/RedisCacheProvider.ts","../../src/cache-redis/index.ts"],"sourcesContent":["import { $env, $inject, Alepha, type Static, t } from \"alepha\";\nimport type { CacheProvider } from \"alepha/cache\";\nimport { $logger } from \"alepha/logger\";\nimport { RedisProvider } from \"alepha/redis\";\n\nconst envSchema = t.object({\n REDIS_CACHE_PREFIX: t.optional(\n t.text({\n description:\n \"Force a prefix for all cache keys in Redis. Useful for testing or multi-tenant applications.\",\n }),\n ),\n});\n\ndeclare module \"alepha\" {\n interface Env extends Partial<Static<typeof envSchema>> {}\n}\n\nexport class RedisCacheProvider implements CacheProvider {\n protected readonly log = $logger();\n protected readonly redisProvider = $inject(RedisProvider);\n protected readonly env = $env(envSchema);\n protected readonly alepha = $inject(Alepha);\n\n public async get(name: string, key: string): Promise<Uint8Array | undefined> {\n if (!this.alepha.isStarted()) {\n return;\n }\n\n const keyWithPrefix = this.prefix(name, key);\n const buffer = await this.redisProvider.get(keyWithPrefix);\n if (!buffer) {\n return;\n }\n\n this.log.debug(`Cache hit`, {\n size: buffer.byteLength,\n key: keyWithPrefix,\n });\n\n return new Uint8Array(buffer);\n }\n\n public async set(\n name: string,\n key: string,\n value: Uint8Array | string,\n ttl?: number,\n ): Promise<Uint8Array> {\n if (!this.alepha.isReady()) {\n return new Uint8Array(Buffer.from(value));\n }\n\n const buffer = Buffer.from(value);\n const prefix = this.prefix(name, key);\n\n if (ttl) {\n return new Uint8Array(\n await this.redisProvider.set(prefix, buffer, {\n expiration: { type: \"PX\", value: ttl },\n }),\n );\n }\n\n return new Uint8Array(await this.redisProvider.set(prefix, buffer));\n }\n\n public async del(name: string, ...keys: string[]): Promise<void> {\n const nameKey = this.prefix(name);\n\n if (keys.length === 0) {\n const keys = await this.redisProvider.keys(`${nameKey}:*`);\n await this.redisProvider.del(keys);\n return;\n }\n\n await this.redisProvider.del(\n keys.map((key) =>\n key.startsWith(nameKey) ? key : this.prefix(name, key),\n ),\n );\n }\n\n public async has(name: string, key: string): Promise<boolean> {\n return this.get(name, key).then((value) => value != null);\n }\n\n public async keys(name: string, filter?: string): Promise<string[]> {\n if (filter) {\n return await this.redisProvider.keys(`${this.prefix(name)}:${filter}*`);\n }\n return this.redisProvider.keys(`${this.prefix(name)}:*`);\n }\n\n public async clear(): Promise<void> {\n this.log.debug(\"Clearing all cache\");\n const pattern = `${this.prefix()}:*`;\n const keys = await this.redisProvider.keys(pattern);\n await this.redisProvider.del(keys);\n }\n\n protected prefix(...path: string[]): string {\n const parts = [\"cache\", ...path];\n\n if (this.env.REDIS_CACHE_PREFIX) {\n parts.unshift(this.env.REDIS_CACHE_PREFIX);\n }\n\n return parts.join(\":\");\n }\n}\n","import { $module } from \"alepha\";\nimport { AlephaCache, CacheProvider } from \"alepha/cache\";\nimport { RedisCacheProvider } from \"./providers/RedisCacheProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./providers/RedisCacheProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Plugin for Alepha Cache that provides Redis caching capabilities.\n *\n * @see {@link RedisCacheProvider}\n * @module alepha.cache.redis\n */\nexport const AlephaCacheRedis = $module({\n name: \"alepha.cache.redis\",\n services: [RedisCacheProvider],\n register: (alepha) =>\n alepha\n .with({\n provide: CacheProvider,\n use: RedisCacheProvider,\n optional: true,\n })\n .with(AlephaCache),\n});\n"],"mappings":";;;;;;AAKA,MAAM,YAAYA,SAAE,OAAO,EACzB,oBAAoBA,SAAE,SACpBA,SAAE,KAAK,EACL,aACE,gGACH,CAAC,CACH,EACF,CAAC;AAMF,IAAa,qBAAb,MAAyD;CACvD,AAAmB,kCAAe;CAClC,AAAmB,oCAAwBC,2BAAc;CACzD,AAAmB,uBAAW,UAAU;CACxC,AAAmB,6BAAiBC,cAAO;CAE3C,MAAa,IAAI,MAAc,KAA8C;AAC3E,MAAI,CAAC,KAAK,OAAO,WAAW,CAC1B;EAGF,MAAM,gBAAgB,KAAK,OAAO,MAAM,IAAI;EAC5C,MAAM,SAAS,MAAM,KAAK,cAAc,IAAI,cAAc;AAC1D,MAAI,CAAC,OACH;AAGF,OAAK,IAAI,MAAM,aAAa;GAC1B,MAAM,OAAO;GACb,KAAK;GACN,CAAC;AAEF,SAAO,IAAI,WAAW,OAAO;;CAG/B,MAAa,IACX,MACA,KACA,OACA,KACqB;AACrB,MAAI,CAAC,KAAK,OAAO,SAAS,CACxB,QAAO,IAAI,WAAW,OAAO,KAAK,MAAM,CAAC;EAG3C,MAAM,SAAS,OAAO,KAAK,MAAM;EACjC,MAAM,SAAS,KAAK,OAAO,MAAM,IAAI;AAErC,MAAI,IACF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mBAAmB;CAC9B,WAAW,aACTC,SACG,KAAK;EACJ,SAASC;EACT,KAAK;EACL,UAAU;EACX,CAAC,CACD,KAAKC,yBAAY;CACvB,CAAC"}
@@ -1,40 +0,0 @@
1
- import * as alepha1 from "alepha";
2
- import { Alepha, Static } from "alepha";
3
- import * as alepha_logger0 from "alepha/logger";
4
- import { CacheProvider } from "alepha/cache";
5
- import { RedisProvider } from "alepha/redis";
6
-
7
- //#region src/cache-redis/providers/RedisCacheProvider.d.ts
8
- declare const envSchema: alepha1.TObject<{
9
- REDIS_CACHE_PREFIX: alepha1.TOptional<alepha1.TString>;
10
- }>;
11
- declare module "alepha" {
12
- interface Env extends Partial<Static<typeof envSchema>> {}
13
- }
14
- declare class RedisCacheProvider implements CacheProvider {
15
- protected readonly log: alepha_logger0.Logger;
16
- protected readonly redisProvider: RedisProvider;
17
- protected readonly env: {
18
- REDIS_CACHE_PREFIX?: string | undefined;
19
- };
20
- protected readonly alepha: Alepha;
21
- get(name: string, key: string): Promise<Uint8Array | undefined>;
22
- set(name: string, key: string, value: Uint8Array | string, ttl?: number): Promise<Uint8Array>;
23
- del(name: string, ...keys: string[]): Promise<void>;
24
- has(name: string, key: string): Promise<boolean>;
25
- keys(name: string, filter?: string): Promise<string[]>;
26
- clear(): Promise<void>;
27
- protected prefix(...path: string[]): string;
28
- }
29
- //#endregion
30
- //#region src/cache-redis/index.d.ts
31
- /**
32
- * Plugin for Alepha Cache that provides Redis caching capabilities.
33
- *
34
- * @see {@link RedisCacheProvider}
35
- * @module alepha.cache.redis
36
- */
37
- declare const AlephaCacheRedis: alepha1.Service<alepha1.Module>;
38
- //#endregion
39
- export { AlephaCacheRedis, RedisCacheProvider };
40
- //# sourceMappingURL=index.d.cts.map
@@ -1,43 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __commonJS = (cb, mod) => function() {
9
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
14
- key = keys[i];
15
- if (!__hasOwnProp.call(to, key) && key !== except) {
16
- __defProp(to, key, {
17
- get: ((k) => from[k]).bind(null, key),
18
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
- });
20
- }
21
- }
22
- }
23
- return to;
24
- };
25
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: true
28
- }) : target, mod));
29
-
30
- //#endregion
31
-
32
- Object.defineProperty(exports, '__commonJS', {
33
- enumerable: true,
34
- get: function () {
35
- return __commonJS;
36
- }
37
- });
38
- Object.defineProperty(exports, '__toESM', {
39
- enumerable: true,
40
- get: function () {
41
- return __toESM;
42
- }
43
- });