Skip to content

Commit

Permalink
$mol_service_plugin - include mol_service, add waitUntil to modify, $…
Browse files Browse the repository at this point in the history
…mol_offline - abstract fetch to remove undici install in node
  • Loading branch information
zerkalica committed Oct 30, 2024
1 parent ea7d089 commit 02d8dcc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
12 changes: 9 additions & 3 deletions offline/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ namespace $.$mol_service {
return this.respond(request)
}

protected static fetch(request: Request): Promise<Response> {
throw new Error('implement')
}

static force_cache_regexp = /.+\/(index|test)\.html/

protected static async respond(request: Request) {
let fallback_header

const index_html = /.+\/index\.html/.test(request.url)
const index_html = this.force_cache_regexp.test(request.url)

const cache = request.cache

Expand All @@ -36,7 +42,7 @@ namespace $.$mol_service {

// fetch with fallback to cache if statuses not match
try {
const actual = await fetch(request)
const actual = await this.fetch(request)
if (actual.status < 400) return actual

throw new Error(
Expand All @@ -54,7 +60,7 @@ namespace $.$mol_service {
request = new Request(request, { cache: 'force-cache' })
}

const cached = await fetch(request)
const cached = await this.fetch(request)

if (! fallback_header || cached.headers.get('$mol_offline_remote_status')) return cached

Expand Down
9 changes: 9 additions & 0 deletions offline/offline.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace $.$mol_service {
export class $mol_offline_web extends $mol_offline {
protected static override fetch(request: Request) {
return fetch(request)
}
}

$.$mol_service.$mol_offline = $mol_offline_web
}
4 changes: 3 additions & 1 deletion service/host/host.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ namespace $ {
}
}

const waitUntil = event.waitUntil.bind(event)

for (const plugin of this.plugins) {
const response = plugin.modify(request)
const response = plugin.modify(request, waitUntil)
if (response) return event.respondWith(response)
}
}
Expand Down
8 changes: 6 additions & 2 deletions service/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace $ {
static state_change() {}
static message_data(data: {}) { return null as null | undefined | Promise<unknown> }

static blocked(res: Request) { return false }
static modify(res: Request) { return null as null | Response | PromiseLike<Response> }
static blocked(request: Request) { return false }
static modify(request: Request, waitUntil: (promise: Promise<unknown>) => void) {
return null as null | Response | PromiseLike<Response>
}

static service() { return this.$.$mol_service_host }
}
}

0 comments on commit 02d8dcc

Please sign in to comment.