-
Notifications
You must be signed in to change notification settings - Fork 9
/
sw.ts
532 lines (466 loc) · 18.4 KB
/
sw.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import { createVerifiedFetch, type VerifiedFetch } from '@helia/verified-fetch'
import { dnsJsonOverHttps } from '@multiformats/dns/resolvers'
import { HeliaServiceWorkerCommsChannel, type ChannelMessage } from './lib/channel.js'
import { getConfig } from './lib/config-db.js'
import { contentTypeParser } from './lib/content-type-parser.js'
import { getRedirectUrl, isDeregisterRequest } from './lib/deregister-request.js'
import { GenericIDB } from './lib/generic-db.js'
import { getSubdomainParts } from './lib/get-subdomain-parts.js'
import { isConfigPage } from './lib/is-config-page.js'
import { error, log, trace } from './lib/logger.js'
import { findOriginIsolationRedirect } from './lib/path-or-subdomain.js'
/**
******************************************************
* Types
******************************************************
*/
/**
* Not available in ServiceWorkerGlobalScope
*/
interface AggregateError extends Error {
errors: Error[]
}
interface FetchHandlerArg {
path: string
request: Request
event: FetchEvent
}
interface GetVerifiedFetchUrlOptions {
protocol?: string | null
id?: string | null
path: string
}
interface StoreReponseInCacheOptions {
response: Response
cacheKey: string
isMutable: boolean
event: FetchEvent
}
/**
* IndexedDB schema for each registered service worker
*
* NOTE: this is not intended to be shared between service workers, unlike the
* default used by config-db.ts
*/
interface LocalSwConfig {
installTimestamp: number
}
/**
******************************************************
* "globals"
******************************************************
*/
declare let self: ServiceWorkerGlobalScope
/**
* This is one best practice that can be followed in general to keep track of
* multiple caches used by a given service worker, and keep them all versioned.
* It maps a shorthand identifier for a cache to a specific, versioned cache name.
*
* Note that since global state is discarded in between service worker restarts, these
* variables will be reinitialized each time the service worker handles an event, and you
* should not attempt to change their values inside an event handler. (Treat them as constants.)
*
* If at any point you want to force pages that use this service worker to start using a fresh
* cache, then increment the CACHE_VERSION value. It will kick off the service worker update
* flow and the old cache(s) will be purged as part of the activate event handler when the
* updated service worker is activated.
*
* @see https://googlechrome.github.io/samples/service-worker/prefetch-video/
*/
const CACHE_VERSION = 1
const CURRENT_CACHES = Object.freeze({
mutable: `mutable-cache-v${CACHE_VERSION}`,
immutable: `immutable-cache-v${CACHE_VERSION}`
})
let verifiedFetch: VerifiedFetch
const channel = new HeliaServiceWorkerCommsChannel('SW')
const timeoutAbortEventType = 'verified-fetch-timeout'
const ONE_HOUR_IN_SECONDS = 3600
const urlInterceptRegex = [new RegExp(`${self.location.origin}/ip(n|f)s/`)]
const updateVerifiedFetch = async (): Promise<void> => {
verifiedFetch = await getVerifiedFetch()
}
let swIdb: GenericIDB<LocalSwConfig>
let firstInstallTime: number
const getSwConfig = (): GenericIDB<LocalSwConfig> => {
return swIdb ?? new GenericIDB<LocalSwConfig>('helia-sw-unique', 'config')
}
/**
******************************************************
* Service Worker Lifecycle Events
******************************************************
*/
self.addEventListener('install', (event) => {
// 👇 When a new version of the SW is installed, activate immediately
void self.skipWaiting()
event.waitUntil(addInstallTimestampToConfig())
})
self.addEventListener('activate', (event) => {
// ensure verifiedFetch is ready for use
event.waitUntil(updateVerifiedFetch())
/**
* 👇 Claim all clients immediately. This handles the case when subdomain is
* loaded for the first time, and config is updated and then a pre-fetch is
* sent (await fetch(window.location.href, { method: 'GET' })) to start
* loading the content prior the user reloading or clicking the "load content"
* button.
*/
event.waitUntil(self.clients.claim())
channel.onmessagefrom('WINDOW', async (message: MessageEvent<ChannelMessage<'WINDOW', null>>) => {
const { action } = message.data
switch (action) {
case 'RELOAD_CONFIG':
void updateVerifiedFetch().then(() => {
channel.postMessage({ action: 'RELOAD_CONFIG_SUCCESS' })
trace('sw: RELOAD_CONFIG_SUCCESS for %s', self.location.origin)
})
break
default:
log('unknown action: ', action)
}
})
// Delete all caches that aren't named in CURRENT_CACHES.
const expectedCacheNames = Object.keys(CURRENT_CACHES).map(function (key) {
return CURRENT_CACHES[key]
})
event.waitUntil(
caches.keys().then(async function (cacheNames) {
return Promise.all(
cacheNames.map(async function (cacheName) {
if (!expectedCacheNames.includes(cacheName)) {
// If this cache name isn't present in the array of "expected" cache names, then delete it.
log('helia-sw: deleting out of date cache:', cacheName)
return caches.delete(cacheName)
}
})
)
})
)
})
self.addEventListener('fetch', (event) => {
const request = event.request
const urlString = request.url
const url = new URL(urlString)
log('helia-sw: incoming request url: %s:', event.request.url)
log('helia-sw: request range header value: "%s"', event.request.headers.get('range'))
event.waitUntil(requestRouting(event, url).then(async (shouldHandle) => {
if (shouldHandle) {
event.respondWith(getResponseFromCacheOrFetch(event))
}
}))
})
/**
******************************************************
* Functions
******************************************************
*/
async function requestRouting (event: FetchEvent, url: URL): Promise<boolean> {
if (await isTimebombExpired()) {
trace('helia-sw: timebomb expired, deregistering service worker')
event.waitUntil(deregister(event, false))
return false
} else if (isDeregisterRequest(event.request.url)) {
event.waitUntil(deregister(event))
return false
} else if (isConfigPageRequest(url) || isSwAssetRequest(event)) {
// get the assets from the server
trace('helia-sw: config page or js asset request, ignoring ', event.request.url)
return false
} else if (!isValidRequestForSW(event)) {
trace('helia-sw: not a valid request for helia-sw, ignoring ', event.request.url)
return false
}
if (isRootRequestForContent(event) || isSubdomainRequest(event)) {
return true
}
return false
}
async function getVerifiedFetch (): Promise<VerifiedFetch> {
const config = await getConfig()
log(`config-debug: got config for sw location ${self.location.origin}`, config)
const verifiedFetch = await createVerifiedFetch({
gateways: config.gateways ?? ['https://trustless-gateway.link'],
routers: config.routers ?? ['https://delegated-ipfs.dev'],
dnsResolvers: {
'.': dnsJsonOverHttps('https://delegated-ipfs.dev/dns-query')
}
}, {
contentTypeParser
})
return verifiedFetch
}
// potential race condition
async function deregister (event, redirectToConfig = true): Promise<void> {
if (!isSubdomainRequest(event)) {
// if we are at the root, we need to ignore this request due to race conditions with the UI
return
}
await self.registration.unregister()
const clients = await self.clients.matchAll({ type: 'window' })
for (const client of clients) {
const newUrl = redirectToConfig ? getRedirectUrl(client.url) : client.url
try {
await client.navigate(newUrl)
} catch (e) {
error('error navigating client to ', newUrl, e)
}
}
}
function isRootRequestForContent (event: FetchEvent): boolean {
const urlIsPreviouslyIntercepted = urlInterceptRegex.some(regex => regex.test(event.request.url))
const isRootRequest = urlIsPreviouslyIntercepted
return isRootRequest // && getCidFromUrl(event.request.url) != null
}
function isSubdomainRequest (event: FetchEvent): boolean {
const { id, protocol } = getSubdomainParts(event.request.url)
trace('isSubdomainRequest.id: ', id)
trace('isSubdomainRequest.protocol: ', protocol)
return id != null && protocol != null
}
function isConfigPageRequest (url: URL): boolean {
return isConfigPage(url.hash)
}
function isValidRequestForSW (event: FetchEvent): boolean {
return isSubdomainRequest(event) || isRootRequestForContent(event)
}
function isAggregateError (err: unknown): err is AggregateError {
return err instanceof Error && (err as AggregateError).errors != null
}
function getVerifiedFetchUrl ({ protocol, id, path }: GetVerifiedFetchUrlOptions): string {
if (id != null && protocol != null) {
return `${protocol}://${id}${path}`
}
const pathParts = path.split('/')
let pathPartIndex = 0
let namespaceString = pathParts[pathPartIndex++]
if (namespaceString === '') {
// we have a prefixed '/' in the path, use the new index instead
namespaceString = pathParts[pathPartIndex++]
}
if (namespaceString !== 'ipfs' && namespaceString !== 'ipns') {
throw new Error(`only /ipfs or /ipns namespaces supported got ${namespaceString}`)
}
const pathRootString = pathParts[pathPartIndex++]
const contentPath = pathParts.slice(pathPartIndex++).join('/')
return `${namespaceString}://${pathRootString}/${contentPath}`
}
function isSwAssetRequest (event: FetchEvent): boolean {
const isActualSwAsset = /^.+\/(?:ipfs-sw-).+\.js$/.test(event.request.url)
return isActualSwAsset
}
/**
* Set the expires header on a response object to a timestamp based on the passed ttl interval
* Defaults to
*/
function setExpiresHeader (response: Response, ttlSeconds: number = ONE_HOUR_IN_SECONDS): void {
const expirationTime = new Date(Date.now() + ttlSeconds * 1000)
response.headers.set('sw-cache-expires', expirationTime.toUTCString())
}
/**
* Checks whether a cached response object has expired by looking at the expires header
* Note that this ignores the Cache-Control header since the expires header is set by us
*/
function hasExpired (response: Response): boolean {
const expiresHeader = response.headers.get('sw-cache-expires')
if (expiresHeader == null) {
return false
}
const expires = new Date(expiresHeader)
const now = new Date()
return expires < now
}
function getCacheKey (event: FetchEvent): string {
return `${event.request.url}-${event.request.headers.get('Accept') ?? ''}`
}
async function fetchAndUpdateCache (event: FetchEvent, url: URL, cacheKey: string): Promise<Response> {
const response = await fetchHandler({ path: url.pathname, request: event.request, event })
// log all of the headers:
response.headers.forEach((value, key) => {
log.trace('helia-sw: response headers: %s: %s', key, value)
})
log('helia-sw: response status: %s', response.status)
try {
await storeReponseInCache({ response, isMutable: true, cacheKey, event })
trace('helia-ws: updated cache for %s', cacheKey)
} catch (err) {
error('helia-ws: failed updating response in cache for %s', cacheKey, err)
}
return response
}
async function getResponseFromCacheOrFetch (event: FetchEvent): Promise<Response> {
const { protocol } = getSubdomainParts(event.request.url)
const url = new URL(event.request.url)
const isMutable = protocol === 'ipns'
const cacheKey = getCacheKey(event)
trace('helia-sw: cache key: %s', cacheKey)
const cache = await caches.open(isMutable ? CURRENT_CACHES.mutable : CURRENT_CACHES.immutable)
const cachedResponse = await cache.match(cacheKey)
const validCacheHit = cachedResponse != null && !hasExpired(cachedResponse)
if (validCacheHit) {
log('helia-ws: cached response HIT for %s (expires: %s) %o', cacheKey, cachedResponse.headers.get('sw-cache-expires'), cachedResponse)
if (isMutable) {
// If the response is mutable, update the cache in the background.
void fetchAndUpdateCache(event, url, cacheKey)
}
return cachedResponse
}
log('helia-ws: cached response MISS for %s', cacheKey)
return fetchAndUpdateCache(event, url, cacheKey)
}
function shouldCacheResponse ({ event, response }: { event: FetchEvent, response: Response }): boolean {
if (!response.ok) {
return false
}
const statusCodesToNotCache = [206]
if (statusCodesToNotCache.some(code => code === response.status)) {
log('helia-sw: not caching response with status %s', response.status)
return false
}
if (event.request.headers.get('pragma') === 'no-cache' || event.request.headers.get('cache-control') === 'no-cache') {
log('helia-sw: request indicated no-cache, not caching')
return false
}
return true
}
async function storeReponseInCache ({ response, isMutable, cacheKey, event }: StoreReponseInCacheOptions): Promise<void> {
if (!shouldCacheResponse({ event, response })) {
return
}
trace('helia-ws: updating cache for %s in the background', cacheKey)
const cache = await caches.open(isMutable ? CURRENT_CACHES.mutable : CURRENT_CACHES.immutable)
// Clone the response since streams can only be consumed once.
const respToCache = response.clone()
if (isMutable) {
trace('helia-ws: setting expires header on response key %s before storing in cache', cacheKey)
// 👇 Set expires header to an hour from now for mutable (ipns://) resources
// Note that this technically breaks HTTP semantics, whereby the cache-control max-age takes precendence
// Setting this header is only used by the service worker using a mechanism similar to stale-while-revalidate
setExpiresHeader(respToCache, ONE_HOUR_IN_SECONDS)
}
log('helia-ws: storing response for key %s in cache', cacheKey)
// do not await this.. large responses will delay [TTFB](https://web.dev/articles/ttfb) and [TTI](https://web.dev/articles/tti)
void cache.put(cacheKey, respToCache)
}
async function fetchHandler ({ path, request, event }: FetchHandlerArg): Promise<Response> {
// test and enforce origin isolation before anything else is executed
const originLocation = await findOriginIsolationRedirect(new URL(request.url))
if (originLocation !== null) {
const body = 'Gateway supports subdomain mode, redirecting to ensure Origin isolation..'
return new Response(body, {
status: 301,
headers: {
'Content-Type': 'text/plain',
Location: originLocation
}
})
}
/**
* > Any global variables you set will be lost if the service worker shuts down.
*
* @see https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle
*/
verifiedFetch = verifiedFetch ?? await getVerifiedFetch()
/**
* Note that there are existing bugs regarding service worker signal handling:
* * https://bugs.chromium.org/p/chromium/issues/detail?id=823697
* * https://bugzilla.mozilla.org/show_bug.cgi?id=1394102
*/
const abortController = new AbortController()
const signal = abortController.signal
const abortFn = (event: Pick<AbortSignalEventMap['abort'], 'type'>): void => {
clearTimeout(signalAbortTimeout)
if (event?.type === timeoutAbortEventType) {
log.trace('helia-sw: timeout waiting for response from @helia/verified-fetch')
abortController.abort('timeout')
} else {
log.trace('helia-sw: request signal aborted')
abortController.abort('request signal aborted')
}
}
/**
* five minute delay to get the initial response.
*
* @todo reduce to 2 minutes?
*/
const signalAbortTimeout = setTimeout(() => {
abortFn({ type: timeoutAbortEventType })
}, 5 * 60 * 1000)
// if the fetch event is aborted, we need to abort the signal we give to @helia/verified-fetch
event.request.signal.addEventListener('abort', abortFn)
try {
const { id, protocol } = getSubdomainParts(request.url)
const verifiedFetchUrl = getVerifiedFetchUrl({ id, protocol, path })
log('verifiedFetch for ', verifiedFetchUrl)
const headers = request.headers
headers.forEach((value, key) => {
log.trace('fetchHandler: request headers: %s: %s', key, value)
})
const response = await verifiedFetch(verifiedFetchUrl, {
signal,
headers,
// TODO redirect: 'manual', // enable when http urls are supported by verified-fetch: https://github.com/ipfs-shipyard/helia-service-worker-gateway/issues/62#issuecomment-1977661456
onProgress: (e) => {
trace(`${e.type}: `, e.detail)
}
})
/**
* Now that we've got a response back from Helia, don't abort the promise since any additional networking calls
* that may performed by Helia would be dropped.
*
* If `event.request.signal` is aborted, that would cancel any underlying network requests.
*
* Note: we haven't awaited the arrayBuffer, blob, json, etc. `await verifiedFetch` only awaits the construction of
* the response object, regardless of it's inner content
*/
clearTimeout(signalAbortTimeout)
return response
} catch (err: unknown) {
const errorMessages: string[] = []
if (isAggregateError(err)) {
error('fetchHandler aggregate error: ', err.message)
for (const e of err.errors) {
errorMessages.push(e.message)
error('fetchHandler errors: ', e)
}
} else {
errorMessages.push(err instanceof Error ? err.message : JSON.stringify(err))
error('fetchHandler error: ', err)
}
const errorMessage = errorMessages.join('\n')
if (errorMessage.includes('aborted')) {
return new Response('heliaFetch error aborted due to timeout: ' + errorMessage, { status: 408 })
}
return new Response('heliaFetch error: ' + errorMessage, { status: 500 })
}
}
async function isTimebombExpired (): Promise<boolean> {
firstInstallTime = firstInstallTime ?? await getInstallTimestamp()
const now = Date.now()
// max life (for now) is 24 hours
const timebomb = 24 * 60 * 60 * 1000
return now - firstInstallTime > timebomb
}
async function getInstallTimestamp (): Promise<number> {
try {
const swidb = getSwConfig()
await swidb.open()
firstInstallTime = await swidb.get('installTimestamp')
swidb.close()
return firstInstallTime
} catch (e) {
error('getInstallTimestamp error: ', e)
return 0
}
}
async function addInstallTimestampToConfig (): Promise<void> {
try {
const timestamp = Date.now()
firstInstallTime = timestamp
const swidb = getSwConfig()
await swidb.open()
await swidb.put('installTimestamp', timestamp)
swidb.close()
} catch (e) {
error('addInstallTimestampToConfig error: ', e)
}
}