diff --git a/packages/localize/src/localize/src/global.ts b/packages/localize/src/localize/src/global.ts index 81fff28bfe..b188112838 100644 --- a/packages/localize/src/localize/src/global.ts +++ b/packages/localize/src/localize/src/global.ts @@ -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; diff --git a/packages/router/test/bootstrap.spec.ts b/packages/router/test/bootstrap.spec.ts index 5abe19e4a7..a5521666f7 100644 --- a/packages/router/test/bootstrap.spec.ts +++ b/packages/router/test/bootstrap.spec.ts @@ -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[] = []; diff --git a/packages/service-worker/worker/src/service-worker.d.ts b/packages/service-worker/worker/src/service-worker.d.ts index 69171abe3d..b4ea4880de 100644 --- a/packages/service-worker/worker/src/service-worker.d.ts +++ b/packages/service-worker/worker/src/service-worker.d.ts @@ -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): 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; - get(id: string): Promise; - matchAll( - options?: T - ): Promise>; - openWindow(url: string): Promise; -} - -interface ClientMatchOptions { - includeUncontrolled?: boolean; - type?: ClientTypes; -} - -interface WindowClient extends Client { - readonly focused: boolean; - readonly visibilityState: VisibilityState; - focus(): Promise; - navigate(url: string): Promise; -} - -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; - readonly request: Request; - readonly resultingClientId: string; - respondWith(r: Response|Promise): 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; - 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 { - readonly location: WorkerLocation; - readonly navigator: WorkerNavigator; - readonly self: WorkerGlobalScope & typeof globalThis; - - importScripts(...urls: string[]): void; - addEventListener(type: K, listener: (this: WorkerGlobalScope, ev: WorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; - addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; - removeEventListener(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; - addEventListener(type: K, listener: (this: ServiceWorkerGlobalScope, ev: ServiceWorkerGlobalScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; - addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; - removeEventListener(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; + +/// + +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; + } }