@katlux/providers 0.1.0-beta.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present, The Katlux Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @katlux/providers
2
+
3
+ Core providers and services for the Katlux UI framework.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @katlux/providers
9
+ ```
10
+
11
+ ## Trademark Policy
12
+
13
+ The code in this repository is completely free and open-source under the MIT License.
14
+ However, **Katlux™** and the Katlux logo are unregistered trademarks of the Katlux project.
15
+
16
+ To protect our users from fraud, you may not use the "Katlux" name or logo to brand your own modified forks, derivative works, or malicious distributions without explicit permission. You are, however, completely free to state "Built with Katlux" in your projects!
@@ -0,0 +1,4 @@
1
+ export default {
2
+ failOnWarn: false,
3
+ externals: ['#app', '@katlux/toolkit']
4
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@katlux/providers",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Core data calculation and caching providers for the Katlux ecosystem",
5
+ "author": "Katlux",
6
+ "license": "MIT",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "main": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "scripts": {
13
+ "build": "unbuild",
14
+ "dev": "unbuild --stub"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "dependencies": {}
24
+ }
@@ -0,0 +1,13 @@
1
+ export abstract class ACacheProvider {
2
+ constructor() {
3
+ // Cleanup expired entries on initialization
4
+ this.cleanupExpired()
5
+ }
6
+ abstract getKeyList(nuxtApp?: any): Array<string> | Promise<Array<string>>
7
+ abstract get<T>(key: string, nuxtApp?: any): Promise<T | null>
8
+ abstract set<T>(key: string, data: T, lifetime: number, nuxtApp?: any): Promise<void>
9
+ abstract remove(key: string, nuxtApp?: any): Promise<void>
10
+ abstract removeByPrefix(prefix: string, nuxtApp?: any): Promise<void>
11
+ abstract getRemainingTime(key: string, nuxtApp?: any): Promise<number | null>
12
+ abstract cleanupExpired(): Promise<void>
13
+ }
@@ -0,0 +1,123 @@
1
+ import { ACacheProvider } from './ACacheProvider'
2
+ import type { ICacheEntry } from '../types'
3
+
4
+ export class CApplicationCache extends ACacheProvider {
5
+ private static instance: CApplicationCache | null = null
6
+ private static _storage: Map<string, ICacheEntry<any>> | null = null
7
+
8
+ private static get storage(): Map<string, ICacheEntry<any>> {
9
+ if (!this._storage) {
10
+ this._storage = new Map<string, ICacheEntry<any>>()
11
+ }
12
+ return this._storage
13
+ }
14
+
15
+ public static getInstance(): CApplicationCache {
16
+ if (!this.instance) {
17
+ this.instance = new CApplicationCache()
18
+ }
19
+ return this.instance
20
+ }
21
+
22
+ public static getSnapshot(): Map<string, ICacheEntry<any>> {
23
+ return this.storage
24
+ }
25
+
26
+ private constructor() {
27
+ super()
28
+ }
29
+
30
+ async getKeyList(nuxtApp?: any): Promise<Array<string>> {
31
+ const prefix = 'cache:'
32
+ const storage = CApplicationCache.storage
33
+ return Array.from(storage.keys())
34
+ .filter(key => key.startsWith(prefix))
35
+ .map(key => key.substring(prefix.length))
36
+ }
37
+
38
+ async get<T>(key: string, nuxtApp?: any): Promise<T | null> {
39
+ if (import.meta.server) {
40
+ const prefixedKey = 'cache:' + key
41
+ const storage = CApplicationCache.storage
42
+ const entry = storage.get(prefixedKey)
43
+ if (!entry) return null
44
+
45
+ if (Date.now() - entry.timestamp > entry.lifetime) {
46
+ storage.delete(prefixedKey)
47
+ return null
48
+ }
49
+
50
+ return entry.data as T
51
+ }
52
+ return null
53
+ }
54
+
55
+ async set<T>(key: string, data: T, lifetime: number, nuxtApp?: any): Promise<void> {
56
+ if (import.meta.server) {
57
+ const prefixedKey = 'cache:' + key
58
+ const storage = CApplicationCache.storage
59
+ storage.set(prefixedKey, {
60
+ data,
61
+ timestamp: Date.now(),
62
+ lifetime
63
+ })
64
+ }
65
+ }
66
+
67
+ async remove(key: string, nuxtApp?: any): Promise<void> {
68
+ if (import.meta.server) {
69
+ const prefixedKey = 'cache:' + key
70
+ CApplicationCache.storage.delete(prefixedKey)
71
+ }
72
+ }
73
+
74
+ async removeByPrefix(prefix: string, nuxtApp?: any): Promise<void> {
75
+ if (import.meta.server) {
76
+ const fullPrefix = 'cache:' + prefix
77
+ const storage = CApplicationCache.storage
78
+ const keysToDelete: string[] = []
79
+
80
+ for (const key of storage.keys()) {
81
+ if (key.startsWith(fullPrefix)) {
82
+ keysToDelete.push(key)
83
+ }
84
+ }
85
+
86
+ for (const key of keysToDelete) {
87
+ storage.delete(key)
88
+ }
89
+ }
90
+ }
91
+
92
+ async getRemainingTime(key: string, nuxtApp?: any): Promise<number | null> {
93
+ if (import.meta.server) {
94
+ const prefixedKey = 'cache:' + key
95
+ const storage = CApplicationCache.storage
96
+ const entry = storage.get(prefixedKey)
97
+ if (!entry) return null
98
+ return Math.max(0, (entry.timestamp + entry.lifetime) - Date.now())
99
+ }
100
+ return null
101
+ }
102
+
103
+ async cleanupExpired(): Promise<void> {
104
+ if (import.meta.server) {
105
+ const prefix = 'cache:'
106
+ const now = Date.now()
107
+ const storage = CApplicationCache.storage
108
+ const keysToDelete: string[] = []
109
+
110
+ for (const [key, entry] of storage.entries()) {
111
+ if (key.startsWith(prefix)) {
112
+ if (now - entry.timestamp > entry.lifetime) {
113
+ keysToDelete.push(key)
114
+ }
115
+ }
116
+ }
117
+
118
+ for (const key of keysToDelete) {
119
+ storage.delete(key)
120
+ }
121
+ }
122
+ }
123
+ }
@@ -0,0 +1,212 @@
1
+ import { ACacheProvider } from './ACacheProvider'
2
+ import type { ICacheEntry } from '../types'
3
+
4
+ export class CCookieCache extends ACacheProvider {
5
+ private static instance: CCookieCache | null = null
6
+
7
+ public static getInstance(): CCookieCache {
8
+ if (!this.instance) {
9
+ this.instance = new CCookieCache()
10
+ }
11
+ return this.instance
12
+ }
13
+
14
+ public type = 'Cookie'
15
+
16
+ private constructor() {
17
+ super()
18
+ }
19
+
20
+ async getKeyList(nuxtApp?: any): Promise<Array<string>> {
21
+ const prefix = 'cache:'
22
+ let cookieStr = ''
23
+
24
+ if (import.meta.client) {
25
+ cookieStr = document.cookie
26
+ } else if (nuxtApp) {
27
+ // Extract from H3Event or SSR Context
28
+ const headers = (nuxtApp as any).node?.req?.headers || (nuxtApp as any).ssrContext?.event?.node?.req?.headers
29
+ cookieStr = headers?.['cookie'] || ''
30
+ }
31
+
32
+ if (!cookieStr) return []
33
+
34
+ const keys: string[] = []
35
+ const cookies = cookieStr.split(';')
36
+ for (let i = 0; i < cookies.length; i++) {
37
+ const cookie = cookies[i].trim()
38
+ const eqPos = cookie.indexOf('=')
39
+ if (eqPos > 0) {
40
+ const name = decodeURIComponent(cookie.substring(0, eqPos))
41
+ if (name.startsWith(prefix)) {
42
+ keys.push(name.substring(prefix.length))
43
+ }
44
+ }
45
+ }
46
+ return keys
47
+ }
48
+
49
+ async get<T>(key: string, nuxtApp?: any): Promise<T | null> {
50
+ const prefixedKey = 'cache:' + key
51
+ let cookieValue: string | null = null
52
+
53
+ if (import.meta.client) {
54
+ const name = prefixedKey + '='
55
+ const ca = document.cookie.split(';')
56
+ for (let i = 0; i < ca.length; i++) {
57
+ let c = ca[i].trim()
58
+ if (c.indexOf(name) === 0) {
59
+ cookieValue = c.substring(name.length, c.length)
60
+ break
61
+ }
62
+ }
63
+ } else if (nuxtApp) {
64
+ const headers = (nuxtApp as any).node?.req?.headers || (nuxtApp as any).ssrContext?.event?.node?.req?.headers
65
+ const cookieStr = headers?.['cookie'] || ''
66
+ const name = prefixedKey + '='
67
+ const ca = cookieStr.split(';')
68
+ for (let i = 0; i < ca.length; i++) {
69
+ let c = ca[i].trim()
70
+ if (c.indexOf(name) === 0) {
71
+ cookieValue = c.substring(name.length, c.length)
72
+ break
73
+ }
74
+ }
75
+ }
76
+
77
+ if (!cookieValue) return null
78
+
79
+ try {
80
+ const val = JSON.parse(decodeURIComponent(cookieValue)) as ICacheEntry<T>
81
+ if (val && val.timestamp && val.lifetime) {
82
+ if (Date.now() - val.timestamp <= val.lifetime) {
83
+ return val.data
84
+ } else {
85
+ // Expired
86
+ await this.set(key, null as any, 0, nuxtApp)
87
+ }
88
+ }
89
+ return null
90
+ } catch (e) {
91
+ return null
92
+ }
93
+ }
94
+
95
+ async set<T>(key: string, data: T, lifetime: number, nuxtApp?: any): Promise<void> {
96
+ const prefixedKey = 'cache:' + key
97
+ const entry: ICacheEntry<T> = {
98
+ data,
99
+ timestamp: Date.now(),
100
+ lifetime
101
+ }
102
+
103
+ const cookieValue = encodeURIComponent(JSON.stringify(entry))
104
+ const maxAge = Math.floor(lifetime / 1000)
105
+ const expires = new Date(Date.now() + lifetime).toUTCString()
106
+ const cookieStr = `${prefixedKey}=${cookieValue}; Max-Age=${maxAge}; Path=/; SameSite=Lax${import.meta.server ? '; HttpOnly' : ''}`
107
+
108
+ if (import.meta.client) {
109
+ document.cookie = cookieStr
110
+ } else if (nuxtApp) {
111
+ const res = (nuxtApp as any).node?.res || (nuxtApp as any).ssrContext?.event?.node?.res
112
+ if (res) {
113
+ const existing = res.getHeader('Set-Cookie')
114
+ if (!existing) {
115
+ res.setHeader('Set-Cookie', cookieStr)
116
+ } else if (Array.isArray(existing)) {
117
+ res.setHeader('Set-Cookie', [...existing, cookieStr])
118
+ } else {
119
+ res.setHeader('Set-Cookie', [existing as string, cookieStr])
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ async remove(key: string, nuxtApp?: any): Promise<void> {
126
+ const prefixedKey = 'cache:' + key
127
+ const cookieStr = `${prefixedKey}=; Max-Age=0; Path=/; SameSite=Lax${import.meta.server ? '; HttpOnly' : ''}`
128
+
129
+ if (import.meta.client) {
130
+ document.cookie = cookieStr
131
+ } else if (nuxtApp) {
132
+ const res = (nuxtApp as any).node?.res || (nuxtApp as any).ssrContext?.event?.node?.res
133
+ if (res) {
134
+ const existing = res.getHeader('Set-Cookie')
135
+ if (!existing) {
136
+ res.setHeader('Set-Cookie', cookieStr)
137
+ } else if (Array.isArray(existing)) {
138
+ res.setHeader('Set-Cookie', [...existing, cookieStr])
139
+ } else {
140
+ res.setHeader('Set-Cookie', [existing as string, cookieStr])
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+ async getRemainingTime(key: string, nuxtApp?: any): Promise<number | null> {
147
+ const prefixedKey = 'cache:' + key
148
+ let cookieValue: string | null = null
149
+
150
+ if (import.meta.client) {
151
+ const name = prefixedKey + '='
152
+ const ca = document.cookie.split(';')
153
+ for (let i = 0; i < ca.length; i++) {
154
+ let c = ca[i].trim()
155
+ if (c.indexOf(name) === 0) {
156
+ cookieValue = c.substring(name.length, c.length)
157
+ break
158
+ }
159
+ }
160
+ } else if (nuxtApp) {
161
+ const headers = (nuxtApp as any).node?.req?.headers || (nuxtApp as any).ssrContext?.event?.node?.req?.headers
162
+ const cookieStr = headers?.['cookie'] || ''
163
+ const name = prefixedKey + '='
164
+ const ca = cookieStr.split(';')
165
+ for (let i = 0; i < ca.length; i++) {
166
+ let c = ca[i].trim()
167
+ if (c.indexOf(name) === 0) {
168
+ cookieValue = c.substring(name.length, c.length)
169
+ break
170
+ }
171
+ }
172
+ }
173
+
174
+ if (!cookieValue) return null
175
+
176
+ try {
177
+ const val = JSON.parse(decodeURIComponent(cookieValue)) as ICacheEntry<any>
178
+ if (val && val.timestamp && val.lifetime) {
179
+ return Math.max(0, (val.timestamp + val.lifetime) - Date.now())
180
+ }
181
+ return null
182
+ } catch (e) {
183
+ return null
184
+ }
185
+ }
186
+
187
+ async cleanupExpired(): Promise<void> {
188
+ if (import.meta.client) {
189
+ const prefix = 'cache:'
190
+ const now = Date.now()
191
+ const cookies = document.cookie.split(';')
192
+ for (let i = 0; i < cookies.length; i++) {
193
+ const cookie = cookies[i].trim()
194
+ const eqPos = cookie.indexOf('=')
195
+ if (eqPos > 0) {
196
+ const name = decodeURIComponent(cookie.substring(0, eqPos))
197
+ if (name.startsWith(prefix)) {
198
+ const value = decodeURIComponent(cookie.substring(eqPos + 1))
199
+ try {
200
+ const entry: ICacheEntry<any> = JSON.parse(value)
201
+ if (now - entry.timestamp > entry.lifetime) {
202
+ document.cookie = `${encodeURIComponent(name)}=; max-age=0; path=/`
203
+ }
204
+ } catch (e) {
205
+ document.cookie = `${encodeURIComponent(name)}=; max-age=0; path=/`
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }