Cleanup of old code, moved Node class, many improvements on file handling

This commit is contained in:
Magnus Åhall 2026-06-29 10:43:21 +02:00
parent 65f8cd14a7
commit 16992db6b1
19 changed files with 485 additions and 3281 deletions

View file

@ -1,3 +1,5 @@
import { NodeStore } from '/js/{{ .VERSION }}/node_store.mjs'
const CACHE_NAME = 'notes2-{{ .VERSION }}'
const CACHED_ASSETS = [
'/',
@ -33,14 +35,11 @@ const CACHED_ASSETS = [
'/js/{{ .VERSION }}/api.mjs',
'/js/{{ .VERSION }}/app.mjs',
'/js/{{ .VERSION }}/checklist.mjs',
'/js/{{ .VERSION }}/crypto.mjs',
'/js/{{ .VERSION }}/file.mjs',
'/js/{{ .VERSION }}/key.mjs',
'/js/{{ .VERSION }}/lib/css_colorize.mjs',
'/js/{{ .VERSION }}/lib/custom_html_element.mjs',
'/js/{{ .VERSION }}/lib/node_modules/marked/lib/marked.esm.js',
'/js/{{ .VERSION }}/lib/node_modules/marked-token-position/lib/index.esm.js',
'/js/{{ .VERSION }}/lib/sjcl.js',
'/js/{{ .VERSION }}/marked_position.mjs',
'/js/{{ .VERSION }}/mbus.mjs',
'/js/{{ .VERSION }}/node_store.mjs',
@ -126,14 +125,55 @@ self.addEventListener('activate', event => {
})
self.addEventListener('fetch', event => {
const url = new URL(event.request.url)
// The fetch event is also seeing requests to other domains.
// Just let the browser handle those for itself.
const ourDomain = event.request.url.startsWith(self.location.origin)
const ourDomain = (url.host == self.location.host)
if (!ourDomain)
return event
if (url.pathname == '/stream-local') {
event.respondWith(indexeddbFile(event))
return
}
if (`{{ .DevMode }}` == 'true')
return event
event.respondWith(fetchAsset(event))
else
event.respondWith(fetchAsset(event))
})
let nodestore = null
async function getNodestore() {
if (nodestore)
return nodestore
// When setting nodestore directly, then waiting on initializeDB,
// a second call for getNodestore can return the nodestore before
// initialized.
const initializingNodeStore = new NodeStore()
await initializingNodeStore.initializeDB()
nodestore = initializingNodeStore
return nodestore
}
async function indexeddbFile(event) {
const url = new URL(event.request.url)
const uuid = url.searchParams.get('uuid')
try {
const nodeStore = await getNodestore()
const md = await nodeStore.filesMetadata.get(uuid)
const stream = await nodeStore.fileSegments.getStream(uuid)
return new Response(stream, {
headers: {
'Content-Type': md.type,
}
})
} catch (err) {
console.error(err)
}
}