@fullstackunicorn/webapp 2.1.1 → 2.1.2
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 +2 -2
- package/imports/cache.js +9 -3
- package/imports/client.js +60 -8
- package/package.json +1 -1
- package/package.md +13 -0
package/files.json
CHANGED
|
@@ -71,13 +71,13 @@
|
|
|
71
71
|
{
|
|
72
72
|
"path": "imports/cache.js",
|
|
73
73
|
"class": "byte-sacred",
|
|
74
|
-
"sha256": "
|
|
74
|
+
"sha256": "0ee910e9f06afa3ce43e11f9e439aa20eb20629ee571234e235f79e4e2008dbd",
|
|
75
75
|
"referenceSha256": "0c89a1146701f65ea10cdf590630d7c6d43f402d13eb253a7065086cf7130071"
|
|
76
76
|
},
|
|
77
77
|
{
|
|
78
78
|
"path": "imports/client.js",
|
|
79
79
|
"class": "byte-sacred",
|
|
80
|
-
"sha256": "
|
|
80
|
+
"sha256": "f9dfe083f494dcd721c37ced153cc96ee30b2a42510a50f9cb4ce0b87264659a",
|
|
81
81
|
"referenceSha256": "a5cb8305485578f25126f7e7ad986cc66b2845066692929981a9589c8689397c"
|
|
82
82
|
},
|
|
83
83
|
{
|
package/imports/cache.js
CHANGED
|
@@ -43,11 +43,11 @@ class Cache {
|
|
|
43
43
|
return !revision || record.revision === revision
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
record = async (store, key) => {
|
|
46
|
+
record = async (store, key, isBlob = false) => {
|
|
47
47
|
const response = await store.match(key)
|
|
48
48
|
if (!response) return null
|
|
49
49
|
return {
|
|
50
|
-
data: await response.json(),
|
|
50
|
+
data: isBlob ? await response.blob() : await response.json(),
|
|
51
51
|
hash: response.headers.get('X-Cache-Hash'),
|
|
52
52
|
source: response.headers.get('X-Cache-Source'),
|
|
53
53
|
revision: response.headers.get('X-Cache-Revision'),
|
|
@@ -88,7 +88,13 @@ class Cache {
|
|
|
88
88
|
|
|
89
89
|
set = async (cache, key, value, isBlob, metadata = {}) => {
|
|
90
90
|
|
|
91
|
-
const headers = { headers: {
|
|
91
|
+
const headers = { headers: {
|
|
92
|
+
'Content-Type': value?.type || 'application/octet-stream',
|
|
93
|
+
'X-Cache-Version': metadata.version || '',
|
|
94
|
+
'X-Cache-Hash': metadata.hash || '',
|
|
95
|
+
'X-Cache-Source': metadata.source || '',
|
|
96
|
+
'X-Cache-Revision': metadata.revision || '',
|
|
97
|
+
} }
|
|
92
98
|
const headersJson = { headers: {
|
|
93
99
|
'Content-Type': 'application/json',
|
|
94
100
|
'X-Cache-Hash': metadata.hash || '',
|
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)
|
|
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
|
|
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(
|
|
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) {
|
|
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.
|
|
2
|
+
"version": "2.1.2",
|
|
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,16 @@ 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.
|