@fullstackunicorn/webapp 2.1.1 → 2.1.3

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/files.json CHANGED
@@ -71,13 +71,13 @@
71
71
  {
72
72
  "path": "imports/cache.js",
73
73
  "class": "byte-sacred",
74
- "sha256": "95cdc6e07de2be1282c688b5cfe3e723d6cc0fe3dfb477522721ea5eeb6368b0",
74
+ "sha256": "53afdb2dddc6a623d1c9543f702b80f7c06b1a52ebb73c373f906f0351308bfa",
75
75
  "referenceSha256": "0c89a1146701f65ea10cdf590630d7c6d43f402d13eb253a7065086cf7130071"
76
76
  },
77
77
  {
78
78
  "path": "imports/client.js",
79
79
  "class": "byte-sacred",
80
- "sha256": "457b408b84936c8b0d65307037daf86e1bfc66da36a8e5493dd04a3f6f023cb8",
80
+ "sha256": "f9dfe083f494dcd721c37ced153cc96ee30b2a42510a50f9cb4ce0b87264659a",
81
81
  "referenceSha256": "a5cb8305485578f25126f7e7ad986cc66b2845066692929981a9589c8689397c"
82
82
  },
83
83
  {
package/imports/cache.js CHANGED
@@ -12,9 +12,20 @@ class Cache {
12
12
  generation = 0
13
13
  commits = new Map()
14
14
  sources = new Map()
15
+ fallback = new Map()
16
+ metadata = new Map()
17
+ unreadable = new Set()
18
+
19
+ persist = async (operation, work, fallback) => {
20
+ try { return await work() }
21
+ catch (error) {
22
+ console.warn('cache-persistence-failed', { operation, error })
23
+ return fallback()
24
+ }
25
+ }
15
26
 
16
27
  initialize = () => {
17
- if (!this.initialization) this.initialization = entries().then(values => {
28
+ if (!this.initialization) this.initialization = this.persist('indexeddb.entries', () => entries(), () => []).then(values => {
18
29
  for (const [key, value] of values) if (typeof key === 'string' && key.startsWith('version:') && value) this.versions.set(key.slice(8), value)
19
30
  for (const [key, value] of values) if (typeof key === 'string' && key.startsWith('source:') && value) this.sources.set(key.slice(7), value)
20
31
  this.initialized = true
@@ -32,8 +43,8 @@ class Cache {
32
43
  await this.initialize()
33
44
  if (this.source(origin) === revision && this.release(origin) === cursor) return false
34
45
  const state = { revision, cursor }
35
- await set('source:' + origin, state)
36
46
  this.sources.set(origin, state)
47
+ await this.writeMeta('source:' + origin, state)
37
48
  return true
38
49
  }
39
50
 
@@ -43,11 +54,11 @@ class Cache {
43
54
  return !revision || record.revision === revision
44
55
  }
45
56
 
46
- record = async (store, key) => {
57
+ record = async (store, key, isBlob = false) => {
47
58
  const response = await store.match(key)
48
59
  if (!response) return null
49
60
  return {
50
- data: await response.json(),
61
+ data: isBlob ? await response.blob() : await response.json(),
51
62
  hash: response.headers.get('X-Cache-Hash'),
52
63
  source: response.headers.get('X-Cache-Source'),
53
64
  revision: response.headers.get('X-Cache-Revision'),
@@ -78,7 +89,47 @@ class Cache {
78
89
  return lastDay < this.todayUTC()
79
90
  }
80
91
 
81
- open = async (name) => await caches.open(name)
92
+ open = async (name) => {
93
+ const store = await this.persist('cache.open', () => caches.open(name), () => null)
94
+ const key = request => typeof request === 'string' ? request : request.url
95
+ const retain = (request, response) => {
96
+ if (!this.fallback.has(name)) this.fallback.set(name, new Map())
97
+ this.fallback.get(name).set(key(request), response)
98
+ }
99
+ return {
100
+ match: async request => {
101
+ const retained = this.fallback.get(name)
102
+ if (retained?.has(key(request))) return retained.get(key(request))?.clone()
103
+ return store ? await this.persist('cache.match', async () => {
104
+ const response = await store.match(request)
105
+ this.unreadable.delete(name)
106
+ return response
107
+ }, () => { this.unreadable.add(name); return undefined }) : undefined
108
+ },
109
+ put: async (request, response) => {
110
+ const copy = response.clone()
111
+ const written = store && await this.persist('cache.put', async () => { await store.put(request, response); return true }, () => false)
112
+ if (written && !this.unreadable.has(name)) this.fallback.get(name)?.delete(key(request))
113
+ else retain(request, copy)
114
+ },
115
+ delete: async request => {
116
+ const retained = this.fallback.get(name)?.get(key(request))
117
+ const result = store && await this.persist('cache.delete', async () => ({ removed: await store.delete(request) }), () => null)
118
+ if (result) this.fallback.get(name)?.delete(key(request))
119
+ else retain(request, null)
120
+ return !!retained || !!result?.removed
121
+ },
122
+ keys: async () => {
123
+ const stored = store ? await this.persist('cache.keys', () => store.keys(), () => []) : []
124
+ const result = new Map(stored.map(request => [request.url, request]))
125
+ for (const [key, response] of this.fallback.get(name) || []) {
126
+ if (response) result.set(key, new Request(key))
127
+ else result.delete(key)
128
+ }
129
+ return [...result.values()]
130
+ },
131
+ }
132
+ }
82
133
 
83
134
  get = async (cache, key, isBlob) => {
84
135
  const match = await cache.match(key)
@@ -88,7 +139,13 @@ class Cache {
88
139
 
89
140
  set = async (cache, key, value, isBlob, metadata = {}) => {
90
141
 
91
- const headers = { headers: {'Content-Type': value?.type || 'application/octet-stream', 'X-Cache-Version': metadata.version || ''}}
142
+ const headers = { headers: {
143
+ 'Content-Type': value?.type || 'application/octet-stream',
144
+ 'X-Cache-Version': metadata.version || '',
145
+ 'X-Cache-Hash': metadata.hash || '',
146
+ 'X-Cache-Source': metadata.source || '',
147
+ 'X-Cache-Revision': metadata.revision || '',
148
+ } }
92
149
  const headersJson = { headers: {
93
150
  'Content-Type': 'application/json',
94
151
  'X-Cache-Hash': metadata.hash || '',
@@ -103,27 +160,36 @@ class Cache {
103
160
  await cache.put(key, response)
104
161
  }
105
162
 
163
+ readMeta = async key => this.metadata.has(key) ? this.metadata.get(key)
164
+ : await this.persist('indexeddb.get', () => get(key), () => undefined)
165
+
166
+ writeMeta = async (key, value) => {
167
+ const written = await this.persist('indexeddb.set', async () => { await set(key, value); return true }, () => false)
168
+ if (written) this.metadata.delete(key)
169
+ else this.metadata.set(key, value)
170
+ }
171
+
106
172
  getMeta = async (key) => {
107
- const etag = await get(`etag:${key}`)
108
- const timestamp = (await get(`timestamp:${key}`)) || 0
173
+ const etag = await this.readMeta(`etag:${key}`)
174
+ const timestamp = (await this.readMeta(`timestamp:${key}`)) || 0
109
175
  return { etag, timestamp }
110
176
  }
111
177
 
112
178
  setMeta = async (key, etag, version) => {
113
179
  await this.initialize()
114
180
  if (version !== undefined) {
115
- await set(`version:${key}`, version)
116
181
  if (version) this.versions.set(key, version)
117
182
  else this.versions.delete(key)
183
+ await this.writeMeta(`version:${key}`, version)
118
184
  }
119
- if (etag) await set(`etag:${key}`, etag)
120
- await set(`timestamp:${key}`, Date.now())
185
+ if (etag) await this.writeMeta(`etag:${key}`, etag)
186
+ await this.writeMeta(`timestamp:${key}`, Date.now())
121
187
  }
122
188
 
123
189
  remove = async (cache, key) => {
124
190
  await cache.delete(key)
125
- await set(`etag:${key}`, undefined)
126
- await set(`timestamp:${key}`, undefined)
191
+ await this.writeMeta(`etag:${key}`, undefined)
192
+ await this.writeMeta(`timestamp:${key}`, undefined)
127
193
  await this.setMeta(key, undefined, null)
128
194
  this.generation += 1
129
195
  }
@@ -134,7 +200,7 @@ class Cache {
134
200
  const store = await this.open(name)
135
201
  for (const request of await store.keys()) await this.remove(store, request.url)
136
202
  this.generation += 1
137
- return caches.delete(name)
203
+ return await this.persist('cache.delete-store', () => caches.delete(name), () => false)
138
204
  }
139
205
 
140
206
  preBlob = (data) => {
package/imports/client.js CHANGED
@@ -4,6 +4,7 @@ import { abs } from '#@/abstracts/abs.js'
4
4
  import { err } from '#@/abstracts/err.js'
5
5
  import { cache } from '#imports/cache.js'
6
6
  import { requestFailure } from '#imports/request.js'
7
+ import { requests } from '#load/requests.js'
7
8
 
8
9
  class Client {
9
10
 
@@ -14,7 +15,7 @@ class Client {
14
15
  this.cacheStale = constants.cache.stale
15
16
  }
16
17
 
17
- source = endpoint => {
18
+ source = (endpoint, isFile = false) => {
18
19
  if (typeof endpoint !== 'string') return null
19
20
  const origins = Object.entries(constants.env || {}).filter(([name, value]) => /^CDN\d+$/.test(name) && typeof value === 'string' && /^https?:\/\//.test(value))
20
21
  .map(([name, value]) => ({ id: name === 'CDN0' ? 'site' : name, origin: value.replace(/\/+$/, '') }))
@@ -22,7 +23,8 @@ class Client {
22
23
  for (const source of origins) {
23
24
  if (!endpoint.startsWith(source.origin + '/')) continue
24
25
  const path = endpoint.slice(source.origin.length + 1).split('#')[0]
25
- if (/^(data|page|site|lookup|menu)\//.test(path) && !path.includes('..') && !path.includes('?')) return { ...source, path }
26
+ if ((/^(data|page|site|lookup|menu)\//.test(path) || isFile && /^(media|icons|fonts)\//.test(path))
27
+ && !path.includes('..') && !path.includes('?')) return { ...source, path }
26
28
  }
27
29
  return null
28
30
  }
@@ -74,6 +76,8 @@ class Client {
74
76
  const params = isGet ? undefined : payload.data
75
77
  payload.key = abs.makeKey(payload.endpoint, payload.method, params)
76
78
  if (isNoCache) return await this.directFile(payload, isGet)
79
+ if (isGet && this.source(payload.endpoint, true)) return await requests.get('file:' + payload.key,
80
+ signal => this.cacheFile({ ...payload, config: { ...payload.config, signal } }, isGet), { signal: payload.config?.signal })
77
81
  return await this.cacheFile(payload, isGet)
78
82
  }
79
83
 
@@ -263,30 +267,75 @@ class Client {
263
267
  return { key, blob, version, isUpdated: true }
264
268
  }
265
269
 
270
+ fileHash = async blob => [...new Uint8Array(await crypto.subtle.digest('SHA-256', await blob.arrayBuffer()))]
271
+ .map(value => value.toString(16).padStart(2, '0')).join('')
272
+
273
+ fileRecord = async (store, key) => {
274
+ const record = await cache.record(store, key, true)
275
+ if (!record) return null
276
+ if (!record.data.size || /^[a-f0-9]{64}$/.test(record.hash || '') && await this.fileHash(record.data) !== record.hash) {
277
+ await cache.remove(store, key)
278
+ return null
279
+ }
280
+ return record
281
+ }
282
+
266
283
  cacheFile = async ({ key, method, endpoint, data = {}, config = {} }, isGet) => {
267
284
 
268
285
  const cacheStore = await cache.open(this.cacheNameFiles)
269
- const cachedBlob = await cache.get(cacheStore, key, true)
286
+ const source = isGet ? this.source(endpoint, true) : null
287
+ if (source) await cache.initialize()
288
+ const cachedRecord = source ? await this.fileRecord(cacheStore, key) : null
289
+ const cachedBlob = source ? cachedRecord?.data : await cache.get(cacheStore, key, true)
270
290
  const cachedResponse = cachedBlob?.size ? { key, blob: cachedBlob } : undefined
271
291
  const { etag, timestamp } = await cache.getMeta(key)
292
+ const pulse = source ? cache.source(source.origin) : undefined
293
+ const cursor = source ? cache.release(source.origin) : undefined
294
+ const url = cursor ? endpoint + '?release=' + encodeURIComponent(cursor) : endpoint
295
+ const retry = () => this.cacheFile({ key, method, endpoint, data,
296
+ config: { ...config, headers: { ...config.headers, 'If-None-Match': undefined } } }, isGet)
272
297
 
273
298
  const age = (Date.now() - timestamp) / 1000
274
- const isFresh = cachedResponse && age < this.cacheStale
299
+ const isFresh = cachedResponse && (source ? cache.current(cachedRecord, source.origin) : age < this.cacheStale)
275
300
  if (isFresh) return cachedResponse
276
301
 
277
302
  config.responseType = 'arraybuffer'
278
303
  config.timeout = 10000
279
304
 
280
- if (etag && cachedResponse) {
305
+ if (etag && cachedResponse && (!source || cachedRecord?.hash === this.etagHash(etag))) {
281
306
  if (!config.headers) config.headers = {}
282
- config.headers['If-None-Match'] = etag
307
+ config.headers['If-None-Match'] = source ? '"' + cachedRecord.hash + '"' : etag
283
308
  }
284
309
 
285
310
  try {
286
311
  const response = isGet
287
- ? await axios.get(endpoint, config)
312
+ ? await axios.get(url, config)
288
313
  : await axios[method](endpoint, data, config)
289
314
 
315
+ if (source) {
316
+ if (config.signal?.aborted) throw new DOMException('Aborted', 'AbortError')
317
+ const identity = this.identity(response.headers, source)
318
+ const confirmed = response.status === 304 && cachedResponse
319
+ let blob = confirmed ? cachedBlob : cache.getBlob(response.data, response.headers['content-type'])
320
+ if (!blob.size || await this.fileHash(blob) !== identity.hash) throw new Error('File bytes do not match publication: ' + endpoint)
321
+ let superseded = false
322
+ await cache.commit(key, async () => {
323
+ if (config.signal?.aborted) throw new DOMException('Aborted', 'AbortError')
324
+ const current = await this.fileRecord(cacheStore, key)
325
+ if (pulse !== cache.source(source.origin) && identity.revision !== cache.source(source.origin)) {
326
+ if (cache.current(current, source.origin)) blob = current.data
327
+ else superseded = true
328
+ return
329
+ }
330
+ if (pulse && identity.revision !== pulse) throw new Error('Public file revision does not match Pulse')
331
+ if (cache.current(current, source.origin)) { blob = current.data; return }
332
+ await cache.set(cacheStore, key, blob, true, identity)
333
+ await cache.setMeta(key, response.headers['etag'] || etag)
334
+ })
335
+ if (superseded) return await retry()
336
+ return { key, blob }
337
+ }
338
+
290
339
  if (cachedResponse && response.status === 304) {
291
340
  await cache.setMeta(key, response.headers['etag'] || etag)
292
341
  return cachedResponse
@@ -298,7 +347,10 @@ class Client {
298
347
 
299
348
  return { key, blob }
300
349
 
301
- } catch (error) { return await err.cache(error, cachedResponse) }
350
+ } catch (error) {
351
+ if (source) throw error
352
+ return await err.cache(error, cachedResponse)
353
+ }
302
354
  }
303
355
 
304
356
  cacheRemove = async ({ method, endpoint, cacheName }) => {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.1.1",
2
+ "version": "2.1.3",
3
3
  "name": "@fullstackunicorn/webapp",
4
4
  "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
5
  "description": "Shared Web and Native runtime engines and core components for Fullstack Unicorn applications",
package/package.md CHANGED
@@ -82,3 +82,28 @@ deselection without eviction, and physical-cache recovery. A bundle of another
82
82
  observed revision cannot replace current data. Within the same observed revision,
83
83
  an older completion cannot replace newer committed JSON, including conditional
84
84
  304 responses; an explicitly Pulse-selected rollback remains valid.
85
+
86
+ Version 2.1.2 applies the same observed-source identity to versionable public
87
+ files requested through `client.file`, including font CSS from the declared
88
+ CDNs. These Blob responses reuse `cache-files`, source/revision/hash metadata,
89
+ the existing `Requests` ownership and per-key commits. They have no TTL; Pulse
90
+ changes source validity, and the existing release cursor pins validation. Blob
91
+ bytes are checked against the origin SHA, including after physical corruption
92
+ or eviction. Concurrent file readers share one transport; late 200/304 responses
93
+ cannot replace current data. The previous external-file TTL behavior, POST and
94
+ explicit direct downloads, Native adapters, and browser-owned DOM/CSS asset
95
+ requests remain unchanged. This does not introduce font prefetch or a new cache.
96
+ The focused permanent file suite covers the demonstrated font path and is part
97
+ of the package's maintainer tests.
98
+
99
+ Version 2.1.3 keeps valid responses available when browser persistence fails.
100
+ Only the existing Cache changes: failed CacheStorage operations retain the same
101
+ Response and identity in memory, and failed IndexedDB operations retain metadata
102
+ alongside the existing version/source maps. Normal successful storage has no
103
+ duplicate memory cache. Volatile values take precedence over older disk entries;
104
+ failed deletes leave a tombstone, and successful writes replace only their own
105
+ fallback. This fallback lasts for the current page and never claims persistence
106
+ across reload. Storage failures remain diagnostic warnings. JSON/Blob parsing,
107
+ HTTP identity and hash checks, cancellation and serialized revision commits
108
+ retain their existing behavior. No cache clearing, new controller or endpoint
109
+ is introduced; Client, Native and the frontend readers are unchanged.