/* eslint-disable @typescript-eslint/no-explicit-any */ // /// /** * Copyright (c) 2018, Tiernan Cridland * * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby * granted, provided that the above copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. * * Service Worker Typings to supplement lib.webworker.ts * @author Tiernan Cridland * @email tiernanc@gmail.com * @license: ISC * * lib.webworker.d.ts as well as an es5+ library (es5, es2015, etc) are required. * Recommended to be used with a triple slash directive in the files requiring the typings only. * e.g. your-service-worker.js, register-service-worker.js * e.g. /// */ // Registration interface WorkerNavigator { readonly serviceWorker: ServiceWorkerContainer; } interface ServiceWorkerContainer { readonly controller: ServiceWorker; readonly ready: Promise; onerror?(this: ServiceWorkerContainer, event?: Event): any getRegistration(scope?: string): Promise; getRegistrations(): Promise; register(url: string, options?: ServiceWorkerRegistrationOptions): Promise; } interface ServiceWorkerMessageEvent extends Event { readonly data: any; readonly lastEventId: string; readonly origin: string; readonly ports: ReadonlyArray | null; readonly source: ServiceWorker | MessagePort | null; } interface ServiceWorkerRegistrationOptions { scope?: string; } // Client API interface Client { postMessage(message, transfer?): void id: string; type: ClientType; url: string; } type ClientFrameType = "auxiliary" | "top-level" | "nested" | "none"; // Events interface InstallEvent { readonly activeWorker: ServiceWorker; } // Fetch API interface Body { readonly body: ReadableStream; } interface Headers { entries(): string[][]; keys(): string[]; values(): string[]; } interface Response extends Body { readonly useFinalURL: boolean; clone(): Response; error(): Response; redirect(): Response; } // Notification API //eslint-disable-next-line @typescript-eslint/no-empty-interface interface NotificationAction { } declare type ClientType = 'window' | 'worker' | 'sharedworker'; interface WindowClient extends Client { focus(): PromiseLike; navigate(url: string): PromiseLike; } interface Clients { get(id: string): PromiseLike; matchAll(options?: { includeUncontrolled?: boolean, type?: ClientType | 'all' }): PromiseLike openWindow(url: string): PromiseLike claim(): Promise } // ServiceWorkerGlobalScope interface ExtendableEvent extends Event { waitUntil(f: Promise): void; } interface PushEvent extends ExtendableEvent { data: PushData; } interface PushData { arrayBuffer(): Promise; blob(): Promise; json(): Promise; text(): Promise; } interface FetchEvent extends ExtendableEvent { readonly clientId: string; readonly preloadResponse: Promise; readonly request: Request; readonly resultingClientId: string; readonly targetClientId: string; respondWith(r: Promise): void; } declare var FetchEvent: { prototype: FetchEvent; new(type: string, eventInitDict: FetchEventInit): FetchEvent; }; type ExtendableEventInit = EventInit interface FetchEventInit extends ExtendableEventInit { clientId?: string; preloadResponse?: Promise; request: Request; resultingClientId?: string; targetClientId?: string; } interface ServiceWorkerRegistrationEventMap { 'push': PushEvent 'fetch': FetchEvent 'install': InstallEvent & ExtendableEvent } interface ServiceWorkerGlobalScope extends WindowOrWorkerGlobalScope { clients: Clients; registration: ServiceWorkerRegistration; addEventListener(type: K, listener: (this: ServiceWorkerRegistration, ev: ServiceWorkerRegistrationEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: ServiceWorkerRegistration, ev: ServiceWorkerRegistrationEventMap[K]) => any, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void; skipWaiting(): Promise } declare function skipWaiting(): void;