diff --git a/fetch/service/service.ts b/fetch/service/service.ts deleted file mode 100644 index fa201761d5e..00000000000 --- a/fetch/service/service.ts +++ /dev/null @@ -1,26 +0,0 @@ -namespace $ { - export class $mol_fetch_service extends $mol_service { - protected static plugins = new Set<$mol_fetch_service>() - - protected static inited = false - - protected static override add(plugin: $mol_fetch_service) { - this.plugins.add(plugin) - if (! this.inited) this.$.$mol_service.add(new this) - this.inited = true - } - - static blocked_response() { - return new Response( - null, - { - status: 418, - statusText: 'Blocked' - }, - ) - } - - blocked(res: Request) { return false } - modify(res: Request) { return null as null | Response | PromiseLike } - } -} diff --git a/fetch/service/service.web.ts b/fetch/service/service.web.ts deleted file mode 100644 index 9945466af44..00000000000 --- a/fetch/service/service.web.ts +++ /dev/null @@ -1,27 +0,0 @@ -namespace $ { - export class $mol_fetch_service_web extends $mol_fetch_service { - override init() { - const worker = this.$.$mol_service_web.worker() - const fetch_service = this.$.$mol_fetch_service_web - - worker.addEventListener('fetch', fetch_service.fetch_event.bind(fetch_service)) - } - - static fetch_event(event: FetchEvent) { - const request = event.request - - for (const plugin of this.plugins) { - if (plugin.blocked(request)) { - return event.respondWith(this.blocked_response()) - } - } - - for (const plugin of this.plugins) { - const response = plugin.modify(request) - if (response) return event.respondWith(response) - } - } - } - - $.$mol_fetch_service = $mol_fetch_service_web -} diff --git a/notify/notify.ts b/notify/notify.ts index a983d5363d5..c1e13a7897b 100644 --- a/notify/notify.ts +++ b/notify/notify.ts @@ -19,6 +19,6 @@ namespace $ { } - $mol_notify_service.attach() + $mol_service.attach($mol_notify_service) } diff --git a/notify/service/service.ts b/notify/service/service.ts index 555c857ed58..215e984042c 100644 --- a/notify/service/service.ts +++ b/notify/service/service.ts @@ -1,13 +1,13 @@ namespace $ { - export class $mol_notify_service extends $mol_service { - override message_data(data: {}) { + export class $mol_notify_service extends $mol_service_plugin { + static override message_data(data: {}) { if ('uri' in data && 'message' in data) { this.info(data as $mol_notify_info) } return null } - async info(info: $mol_notify_info) {} + static async info(info: $mol_notify_info) {} } diff --git a/notify/service/service.web.ts b/notify/service/service.web.ts index a1a90b8ec50..12adb79610b 100644 --- a/notify/service/service.web.ts +++ b/notify/service/service.web.ts @@ -1,15 +1,15 @@ namespace $ { export class $mol_notify_service_web extends $mol_notify_service { - override init() { + static override init() { const worker = this.$.$mol_service_web.worker() worker.addEventListener( 'notificationclick', this.notification_click_event.bind(this)) } - protected notification_click_event(event: NotificationEvent) { + protected static notification_click_event(event: NotificationEvent) { event.waitUntil(this.notification_click(event.notification)) } - override async info({ context: title, message: body, uri: data }: $mol_notify_info) { + static override async info({ context: title, message: body, uri: data }: $mol_notify_info) { const worker = this.$.$mol_service_web.worker() const tag = data const existen = await worker.registration.getNotifications({ tag }) @@ -28,7 +28,7 @@ namespace $ { } - async notification_click( notification: Notification ) { + static async notification_click( notification: Notification ) { const worker = this.$.$mol_service_web.worker() const clients = await worker.clients.matchAll({ includeUncontrolled: true, type: 'window' }) diff --git a/offline/install/install.ts b/offline/install/install.ts index e16848bd33f..59c83e6096d 100644 --- a/offline/install/install.ts +++ b/offline/install/install.ts @@ -1,3 +1,3 @@ namespace $ { - $mol_offline.attach() + $mol_service.attach($mol_offline) } diff --git a/offline/offline.ts b/offline/offline.ts index 2ae3a221747..9bcfe626c4e 100644 --- a/offline/offline.ts +++ b/offline/offline.ts @@ -1,17 +1,17 @@ namespace $ { - export class $mol_offline extends $mol_fetch_service { - blocked_urls = [ + export class $mol_offline extends $mol_service_plugin { + static blocked_urls = [ '//cse.google.com/adsense/search/async-ads.js' ] - override blocked( request: Request ) { + static override blocked( request: Request ) { const normalized_url = request.url.replace( /^https?:/, '' ) return this.blocked_urls.includes(normalized_url) } - override modify(request: Request) { + static override modify(request: Request) { if( request.method !== 'GET' ) return null if( !/^https?:/.test( request.url ) ) return null @@ -21,7 +21,7 @@ namespace $ { return this.respond(request) } - async respond(request: Request) { + protected static async respond(request: Request) { let fallback_header const index_html = /.+\/index\.html/.test(request.url) diff --git a/service/plugin.ts b/service/plugin.ts new file mode 100644 index 00000000000..1d48b99bfe3 --- /dev/null +++ b/service/plugin.ts @@ -0,0 +1,13 @@ +namespace $ { + export class $mol_service_plugin extends $mol_object { + static init() {} + static before_install() {} + static install() { return null as undefined | null | Promise } + static activate() { return null as undefined | null | Promise } + static state_change() {} + static message_data(data: {}) { return null as null | undefined | Promise } + + static blocked(res: Request) { return false } + static modify(res: Request) { return null as null | Response | PromiseLike } + } +} diff --git a/service/service.ts b/service/service.ts index bbf64114eb9..897a604abe2 100644 --- a/service/service.ts +++ b/service/service.ts @@ -8,28 +8,23 @@ namespace $ { @ $mol_action static send(data: {}) {} - protected static plugins = new Set<$mol_service>() + protected static plugins = new Set() - protected static add(plugin: $mol_service) { + static attach(plugin: typeof $mol_service_plugin) { this.plugins.add(plugin) + this.init() } - static attach< This extends typeof $mol_service >( - this : This, - config?: Partial< InstanceType< This > >, - ) { - const plugin = this.make(config ?? {}) + protected static init() {} - this.add(plugin) - - return plugin + static blocked_response() { + return new Response( + null, + { + status: 418, + statusText: 'Blocked' + }, + ) } - - init() {} - before_install() {} - install() { return null as undefined | null | Promise } - activate() { return null as undefined | null | Promise } - state_change() {} - message_data(data: {}) { return null as null | undefined | Promise } } } diff --git a/service/service.web.ts b/service/service.web.ts index dd93fbe7d17..92b2c5cfe91 100644 --- a/service/service.web.ts +++ b/service/service.web.ts @@ -30,9 +30,7 @@ namespace $ { protected static inited = false - protected static override add(plugin: $mol_service) { - super.add(plugin) - + protected static override init() { if (this.inited) return if ( this.in_worker() ) { @@ -45,7 +43,6 @@ namespace $ { this.inited = true } - static async registration_init() { try { @@ -92,6 +89,7 @@ namespace $ { worker.addEventListener( 'install' , this.install.bind(this)) worker.addEventListener( 'activate' , this.activate.bind(this)) worker.addEventListener( 'message', this.message.bind(this)) + worker.addEventListener('fetch', this.fetch_event.bind(this)) this.inited = true for (const plugin of this.plugins) plugin.init() } @@ -155,6 +153,20 @@ namespace $ { }) } + static fetch_event(event: FetchEvent) { + const request = event.request + + for (const plugin of this.plugins) { + if (plugin.blocked(request)) { + return event.respondWith(this.blocked_response()) + } + } + + for (const plugin of this.plugins) { + const response = plugin.modify(request) + if (response) return event.respondWith(response) + } + } } $.$mol_service = $mol_service_web