@uppy/golden-retriever: fix service worker (#5963)

this fixes #5945
This commit is contained in:
Prakash 2025-09-12 01:30:14 +05:30 committed by GitHub
parent 84cf20ea4c
commit 6c0cbe620f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 88 additions and 59 deletions

View file

@ -0,0 +1,5 @@
---
"@uppy/golden-retriever": minor
---
Converted sw.js to sw.ts so that it can be transpiled, in the build.

View file

@ -4,7 +4,9 @@
"version": "5.0.0",
"license": "MIT",
"type": "module",
"sideEffects": false,
"sideEffects": [
"lib/ServiceWorker.js"
],
"scripts": {
"build": "tsc --build tsconfig.build.json",
"typecheck": "tsc --build"
@ -34,6 +36,7 @@
],
"exports": {
".": "./lib/index.js",
"./lib/ServiceWorker.js": "./lib/ServiceWorker.js",
"./package.json": "./package.json"
},
"dependencies": {

View file

@ -1,58 +0,0 @@
/* globals clients */
const fileCache = Object.create(null)
function getCache(name) {
fileCache[name] ??= Object.create(null)
return fileCache[name]
}
self.addEventListener('install', (event) => {
event.waitUntil(Promise.resolve().then(() => self.skipWaiting()))
})
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim())
})
function sendMessageToAllClients(msg) {
clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage(msg)
})
})
}
function addFile(store, file) {
getCache(store)[file.id] = file.data
}
function removeFile(store, fileID) {
delete getCache(store)[fileID]
}
function getFiles(store) {
sendMessageToAllClients({
type: 'uppy/ALL_FILES',
store,
files: getCache(store),
})
}
self.addEventListener('message', (event) => {
switch (event.data.type) {
case 'uppy/ADD_FILE':
addFile(event.data.store, event.data.file)
break
case 'uppy/REMOVE_FILE':
removeFile(event.data.store, event.data.fileID)
break
case 'uppy/GET_FILES':
getFiles(event.data.store)
break
default:
throw new Error(
`[ServiceWorker] Unsupported event.data.type. Got: ${event?.data?.type}`,
)
}
})

View file

@ -0,0 +1,79 @@
/// <reference lib="webworker" />
declare const self: ServiceWorkerGlobalScope
type StoreName = string
type FileId = string
type CachedStore = Record<FileId, Blob>
type FileCache = Record<StoreName, CachedStore>
const fileCache: FileCache = Object.create(null)
function getCache(name: StoreName): CachedStore {
fileCache[name] ??= Object.create(null)
return fileCache[name]
}
self.addEventListener('install', (event: ExtendableEvent) => {
event.waitUntil(Promise.resolve().then(() => self.skipWaiting()))
})
self.addEventListener('activate', (event: ExtendableEvent) => {
event.waitUntil(self.clients.claim())
})
type AllFilesMessage = {
type: 'uppy/ALL_FILES'
store: StoreName
files: CachedStore
}
function sendMessageToAllClients(msg: AllFilesMessage): void {
self.clients.matchAll().then((clientList) => {
clientList.forEach((client) => {
client.postMessage(msg)
})
})
}
type AddFilePayload = { id: FileId; data: Blob }
function addFile(store: StoreName, file: AddFilePayload): void {
getCache(store)[file.id] = file.data
}
function removeFile(store: StoreName, fileID: FileId): void {
delete getCache(store)[fileID]
}
function getFiles(store: StoreName): void {
sendMessageToAllClients({
type: 'uppy/ALL_FILES',
store,
files: getCache(store),
})
}
type IncomingMessage =
| { type: 'uppy/ADD_FILE'; store: StoreName; file: AddFilePayload }
| { type: 'uppy/REMOVE_FILE'; store: StoreName; fileID: FileId }
| { type: 'uppy/GET_FILES'; store: StoreName }
self.addEventListener('message', (event: ExtendableMessageEvent) => {
const data = event.data as IncomingMessage | undefined
switch (data?.type) {
case 'uppy/ADD_FILE':
addFile(data.store, data.file)
break
case 'uppy/REMOVE_FILE':
removeFile(data.store, data.fileID)
break
case 'uppy/GET_FILES':
getFiles(data.store)
break
default:
throw new Error(
`[ServiceWorker] Unsupported event.data.type. Got: ${(data as any)?.type}`,
)
}
})