-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.js
181 lines (148 loc) · 4.66 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const CACHE_KEY = 'v16'
const UNSYNCED_KEY = 'unsynced'
self.addEventListener('install', () => self.skipWaiting())
self.addEventListener('activate', event =>
event.waitUntil(
caches.keys().then(keyList => {
return Promise.all(
keyList
.filter(key => [CACHE_KEY, UNSYNCED_KEY].indexOf(key) === -1)
.map(key => caches.delete(key))
)
})
)
)
self.addEventListener('fetch', event => {
const strategy = request => {
const url = new URL(request.url)
if (request.headers.get('X-Requested-With') === 'fc-client') {
switch (request.method) {
case 'GET': return managePull(request)
case 'POST': return managePush(request)
default: return fetch(request)
}
} else if (url.host === self.location.host || url.host === 'dev.jspm.io') {
return manageAssets(request)
} else {
return fetch(request)
}
}
event.respondWith(strategy(event.request).catch(e => {
console.error(`Error when handling request: ${e}`)
return new Response(
JSON.stringify({ error: e.message }),
{ status: 500, headers: new Headers({ 'Content-Type': 'application/json' }) }
)
}))
})
async function manageAssets (request) {
const cache = await caches.open(CACHE_KEY)
const cached = await cache.match(request)
if (cached) {
return cached
} else {
const response = await fetch(request)
if (response.ok) {
cache.put(request, response.clone())
}
return response
}
}
async function managePull (request) {
const url = new URL(request.url)
const cachedCommandsKey = `${self.location.origin}${url.pathname}`
const clientOffset = request.headers.has('X-Fc-Offset')
? parseInt(request.headers.get('X-Fc-Offset'))
: null
if (url.pathname.indexOf('/unsynced') === 0) {
const stored = await caches.match(request)
return stored || new Response(null, { status: 404 })
}
if (clientOffset === 0) {
const cached = await caches.match(cachedCommandsKey)
if (cached) {
return url.href === cachedCommandsKey
? cached
: new Response(null, {
status: 302,
statusText: 'Found',
headers: new Headers({ Location: cachedCommandsKey, 'Content-Type': 'application/json' })
})
}
}
const response = await smartFetch(request)
if (response.ok) {
const responsePayload = await response.clone().json()
if (responsePayload.length) {
updateJsonCache(cachedCommandsKey, [], commands =>
commands.length === clientOffset
? [].concat(commands, responsePayload)
: commands
)
}
}
return response
}
async function managePush (request) {
const response = await smartFetch(request.clone())
if (response.ok) {
return response
} else {
const boxId = (r => (new URL(r.url)).pathname)(request)
const payload = await request.json()
await updateJsonCache(
`/unsynced${boxId}@${UNSYNCED_KEY}`,
[],
commands => [].concat(commands, payload)
)
return new Response(null, { status: 503, statusText: response.statusText })
}
}
async function readFQCache (cacheKey) {
const parts = cacheKey.split('@')
const key = parts[0]
const cacheName = parts.length > 1 ? parts[1] : CACHE_KEY
return caches.open(cacheName).then(
cache => cache.match(key),
() => undefined
)
}
async function writeFQCache (cacheKey, payload) {
const parts = cacheKey.split('@')
const key = parts[0]
const cacheName = parts.length > 1 ? parts[1] : CACHE_KEY
return caches.open(cacheName).then(cache => cache.put(key, payload))
}
async function jsonCache (key, default_) {
const cached = await readFQCache(key)
try {
return cached ? await cached.json() : default_
} catch (e) {
return default_
}
}
async function updateJsonCache (cacheKey, default_, fn) {
const cachedBody = await jsonCache(cacheKey, default_)
try {
return writeFQCache(cacheKey, new Response(JSON.stringify(fn(cachedBody)), {
status: 203,
statusText: 'From cache',
headers: new Headers({ Via: 'Service Worker', 'Content-Type': 'application/json' })
}))
} catch (e) {
return Promise.resolve(false)
}
}
function smartFetch (request, timeout = 5000) {
const controller = new self.AbortController()
const signal = controller.signal
const fetchPromise = fetch(request, { signal })
const timeoutId = setTimeout(() => controller.abort(), timeout)
if (self.navigator.offline) {
return Promise.resolve(new Response(null, { status: 503, statusText: 'Offline' }))
}
return fetchPromise.then(response => {
clearTimeout(timeoutId)
return response
}).catch(err => new Response(null, { status: 503, statusText: err }))
}