refactor(service-worker): switch to the official TypeScript typings (#42736)

Previously, we used custom typings for the ServiceWorker environment.
This was necessary back when the ServiceWorker package was introduced,
since there were no official typings.

Since there are now official typings for Web Workers (including
ServiceWorkers) offered by TypeScript as [lib.webworker.d.ts][1], this
commit gets rid of our custom typings in favor of using the official
ones.

[1]: https://github.com/microsoft/TypeScript/blob/v4.3.4/lib/lib.webworker.d.ts

PR Close #42736
This commit is contained in:
George Kalpakas 2021-07-08 16:25:16 +03:00 committed by atscott
parent a47aaabf70
commit 5aa0138726
3 changed files with 37 additions and 156 deletions

View File

@ -10,7 +10,18 @@
// This code to access the global object is mostly copied from `packages/core/src/util/global.ts`
declare global {
var WorkerGlobalScope: any;
// The definition of `WorkerGlobalScope` must be compatible with the one in `lib.webworker.d.ts`,
// because all files under `packages/` are compiled together as part of the
// [legacy-unit-tests-saucelabs][1] CI job, including the `lib.webworker.d.ts` typings brought in
// by [service-worker/worker/src/service-worker.d.ts][2].
//
// [1]:
// https://github.com/angular/angular/blob/ffeea63f43e6a7fd46be4a8cd5a5d254c98dea08/.circleci/config.yml#L681
// [2]:
// https://github.com/angular/angular/blob/316dc2f12ce8931f5ff66fa5f8da21c0d251a337/packages/service-worker/worker/src/service-worker.d.ts#L9
interface WorkerGlobalScope extends EventTarget, WindowOrWorkerGlobalScope {}
var WorkerGlobalScope: {prototype: WorkerGlobalScope; new (): WorkerGlobalScope;};
}
const __globalThis = typeof globalThis !== 'undefined' && globalThis;

View File

@ -13,6 +13,16 @@ import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {NavigationEnd, Resolve, Router, RouterModule} from '@angular/router';
// This is needed, because all files under `packages/` are compiled together as part of the
// [legacy-unit-tests-saucelabs][1] CI job, including the `lib.webworker.d.ts` typings brought in by
// [service-worker/worker/src/service-worker.d.ts][2].
//
// [1]:
// https://github.com/angular/angular/blob/ffeea63f43e6a7fd46be4a8cd5a5d254c98dea08/.circleci/config.yml#L681
// [2]:
// https://github.com/angular/angular/blob/316dc2f12ce8931f5ff66fa5f8da21c0d251a337/packages/service-worker/worker/src/service-worker.d.ts#L9
declare var window: Window;
describe('bootstrap', () => {
if (isNode) return;
let log: any[] = [];

View File

@ -1,159 +1,19 @@
// tslint:disable:file-header
/**
* Copyright (c) 2016, Tiernan Cridland
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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.
*
* Typings for Service Worker
* @author Tiernan Cridland
* @email tiernanc@gmail.com
* @license: ISC
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
interface ExtendableEvent extends Event {
waitUntil(fn: Promise<any>): void;
}
// Client API
declare class Client {
readonly frameType: FrameType;
readonly id: string;
readonly type: ClientTypes;
readonly url: string;
postMessage(message: any): void;
}
interface Clients {
claim(): Promise<void>;
get(id: string): Promise<Client | undefined>;
matchAll<T extends ClientMatchOptions>(
options?: T
): Promise<ReadonlyArray<T['type'] extends 'window' ? WindowClient : Client>>;
openWindow(url: string): Promise<WindowClient | null>;
}
interface ClientMatchOptions {
includeUncontrolled?: boolean;
type?: ClientTypes;
}
interface WindowClient extends Client {
readonly focused: boolean;
readonly visibilityState: VisibilityState;
focus(): Promise<WindowClient>;
navigate(url: string): Promise<WindowClient | null>;
}
type FrameType = 'auxiliary'|'top-level'|'nested'|'none';
type ClientTypes = 'window'|'worker'|'sharedworker'|'all';
type VisibilityState = 'hidden'|'visible';
// Fetch API
interface FetchEvent extends ExtendableEvent {
readonly clientId: string;
readonly preloadResponse: Promise<any>;
readonly request: Request;
readonly resultingClientId: string;
respondWith(r: Response|Promise<Response>): void;
}
// Notification API
interface NotificationEvent extends ExtendableEvent {
readonly action: string;
readonly notification: Notification;
}
// Push API
interface PushEvent extends ExtendableEvent {
readonly data: PushMessageData|null;
}
interface PushMessageData {
arrayBuffer(): ArrayBuffer;
blob(): Blob;
json(): any;
text(): string;
}
// Sync API
interface SyncEvent extends ExtendableEvent {
readonly lastChance: boolean;
readonly tag: string;
}
interface ExtendableMessageEvent extends ExtendableEvent {
readonly data: any;
readonly lastEventId: string;
readonly origin: string;
readonly ports: ReadonlyArray<MessagePort>;
readonly source: Client|ServiceWorker|MessagePort|null;
}
// WorkerGlobalScope
// Explicitly omit the `caches` property to disallow accessing `CacheStorage` APIs directly. All
// interactions with `CacheStorage` should go through a `NamedCacheStorage` instance (exposed by the
// `Adapter`).
interface WorkerGlobalScope extends EventTarget, Omit<WindowOrWorkerGlobalScope, 'caches'> {
readonly location: WorkerLocation;
readonly navigator: WorkerNavigator;
readonly self: WorkerGlobalScope & typeof globalThis;
importScripts(...urls: string[]): void;
addEventListener<K extends keyof WorkerGlobalScopeEventMap>(type: K, listener: (this: WorkerGlobalScope, ev: WorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof WorkerGlobalScopeEventMap>(type: K, listener: (this: WorkerGlobalScope, ev: WorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
interface WorkerGlobalScopeEventMap {
error: ErrorEvent;
languagechange: Event;
offline: Event;
online: Event;
rejectionhandled: PromiseRejectionEvent;
unhandledrejection: PromiseRejectionEvent;
}
// ServiceWorkerGlobalScope
interface ServiceWorkerGlobalScope extends WorkerGlobalScope {
readonly clients: Clients;
readonly registration: ServiceWorkerRegistration;
readonly serviceWorker: ServiceWorker;
skipWaiting(): Promise<void>;
addEventListener<K extends keyof ServiceWorkerGlobalScopeEventMap>(type: K, listener: (this: ServiceWorkerGlobalScope, ev: ServiceWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof ServiceWorkerGlobalScopeEventMap>(type: K, listener: (this: ServiceWorkerGlobalScope, ev: ServiceWorkerGlobalScopeEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
interface ServiceWorkerGlobalScopeEventMap extends WorkerGlobalScopeEventMap {
activate: ExtendableEvent;
fetch: FetchEvent;
install: ExtendableEvent;
message: ExtendableMessageEvent;
messageerror: MessageEvent;
notificationclick: NotificationEvent;
notificationclose: NotificationEvent;
push: PushEvent;
sync: SyncEvent;
/// <reference lib="webworker" />
export declare global {
interface ServiceWorkerGlobalScope {
/**
* Disallow accessing `CacheStorage APIs directly to ensure that all accesses go through a
* `NamedCacheStorage` instance (exposed by the `Adapter`).
*/
caches: unknown;
}
}