Skip to content
Snippets Groups Projects
Commit 9354abe4 authored by richard.petersen's avatar richard.petersen :sailboat:
Browse files

Improve logging

parent 760df1b9
No related branches found
No related tags found
No related merge requests found
......@@ -7,11 +7,12 @@ import RedisMock from 'ioredis-mock'
describe('JS files with dependencies contain events', function () {
let app
let mockFetchConfig
before(async function () {
beforeEach(async function () {
mockConfig({ urls: ['http://ui-server/'] })
mockRedis()
mockFetch({
mockFetch(mockFetchConfig = {
'http://ui-server': {
'/manifest.json': generateSimpleViteManifest({
'example.js': {},
......@@ -34,9 +35,6 @@ describe('JS files with dependencies contain events', function () {
afterEach(async function () {
await new RedisMock().flushdb()
})
after(function () {
td.reset()
})
......@@ -46,4 +44,34 @@ describe('JS files with dependencies contain events', function () {
expect(response.headers.dependencies).to.equal('main.css')
expect(response.text).to.equal('console.log("this is index.html.js")\n/*injected by ui-middleware*/document.dispatchEvent(new CustomEvent("load-css",{detail:{css:["main.css"]}}))')
})
it('javascript files from different versions have corrent dependencies', async function () {
const r1 = await request(app).get('/index.html.js')
expect(r1.headers.dependencies).to.equal('main.css')
mockFetchConfig['http://ui-server']['/manifest.json'] = generateSimpleViteManifest({
'example.js': {},
'other.css': {},
'index.html': {
file: 'index.html.js',
isEntry: true,
imports: ['example.js'],
css: ['other.css']
}
})
await import('../src/version.js').then(({ updateVersionProcessor }) => updateVersionProcessor())
const r2 = await request(app).get('/index.html.js')
expect(r2.headers.dependencies).to.equal('other.css')
const r3 = await request(app).get('/index.html.js').set('version', r1.headers.version)
expect(r3.headers.dependencies).to.equal('main.css')
const r4 = await request(app).get('/index.html.js').set('version', r2.headers.version)
expect(r4.headers.dependencies).to.equal('other.css')
const r5 = await request(app).get('/index.html.js')
expect(r5.headers.dependencies).to.equal('other.css')
})
})
......@@ -20,7 +20,7 @@ export async function clear () {
export function get (key, fallback) {
if (cache[key]) {
logger.debug(`[Cache] Resolve "${key}" from memory`)
logger.debug(`[Cache] Resolve from memory: ${key}`)
return cache[key]
}
......@@ -28,7 +28,7 @@ export function get (key, fallback) {
if (redis.isEnabled()) {
let result = await redis.client.get(key)
if (result) {
logger.debug(`[Cache] Resolve "${key}" from redis`)
logger.debug(`[Cache] Resolve from redis: ${key}`)
result = JSON.parse(result)
cache[key] = result
return result
......@@ -39,7 +39,7 @@ export function get (key, fallback) {
const fallbackResult = await fallback()
if (fallbackResult) {
logger.debug(`[Cache] Found a fallback for "${key}"`)
logger.debug(`[Cache] Found a getter for: ${key}`)
cache[key] = fallbackResult
if (redis.isEnabled()) redis.client.set(key, JSON.stringify(fallbackResult))
}
......
......@@ -61,7 +61,10 @@ export function getFile ({ version, path }) {
// try to get the file synchronously.
const data = cache.getCache()[key]
if (data) return data
if (data) {
logger.debug(`[Files] Resolve from memory: ${path}`)
return data
}
// if synchronously does not work, store the async promise for further requests
const promise = (async () => {
......@@ -75,18 +78,21 @@ export function getFile ({ version, path }) {
])
if (body) {
logger.debug(`[Files] Resolve from redis: ${path}`)
return (cache.getCache()[key] = { body, ...JSON.parse(meta) })
}
}
const dataFromServer = await fetchFileWithHeaders({ version, path })
if (redis.isEnabled()) {
logger.debug(`[Files] Store in redis: ${path}`)
const { body, ...rest } = dataFromServer
redis.client.set(bodyKey, createWritable(body))
redis.client.set(metaKey, JSON.stringify(rest))
}
// overwrite cache with synchronous data
logger.debug(`[Files] Store in memory: ${path}`)
return (cache.getCache()[key] = dataFromServer)
})()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment