"Fossies" - the Fresh Open Source Software Archive

Member "angular-8.2.14/packages/service-worker/worker/src/adapter.ts" (13 Nov 2019, 2374 Bytes) of package /linux/www/angular-8.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "adapter.ts": 8.0.2_vs_8.0.3.

    1 /**
    2  * @license
    3  * Copyright Google Inc. All Rights Reserved.
    4  *
    5  * Use of this source code is governed by an MIT-style license that can be
    6  * found in the LICENSE file at https://angular.io/license
    7  */
    8 
    9 /**
   10  * Adapts the service worker to its runtime environment.
   11  *
   12  * Mostly, this is used to mock out identifiers which are otherwise read
   13  * from the global scope.
   14  */
   15 export class Adapter {
   16   readonly cacheNamePrefix: string;
   17 
   18   constructor(scope: ServiceWorkerGlobalScope) {
   19     // Suffixing `ngsw` with the baseHref to avoid clash of cache names
   20     // for SWs with different scopes on the same domain.
   21     const baseHref = this.parseUrl(scope.registration.scope).path;
   22     this.cacheNamePrefix = 'ngsw:' + baseHref;
   23   }
   24 
   25   /**
   26    * Wrapper around the `Request` constructor.
   27    */
   28   newRequest(input: string|Request, init?: RequestInit): Request {
   29     return new Request(input, init);
   30   }
   31 
   32   /**
   33    * Wrapper around the `Response` constructor.
   34    */
   35   newResponse(body: any, init?: ResponseInit) { return new Response(body, init); }
   36 
   37   /**
   38    * Wrapper around the `Headers` constructor.
   39    */
   40   newHeaders(headers: {[name: string]: string}): Headers { return new Headers(headers); }
   41 
   42   /**
   43    * Test if a given object is an instance of `Client`.
   44    */
   45   isClient(source: any): source is Client { return (source instanceof Client); }
   46 
   47   /**
   48    * Read the current UNIX time in milliseconds.
   49    */
   50   get time(): number { return Date.now(); }
   51 
   52   /**
   53    * Extract the pathname of a URL.
   54    */
   55   parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
   56     // Workaround a Safari bug, see
   57     // https://github.com/angular/angular/issues/31061#issuecomment-503637978
   58     const parsed = !relativeTo ? new URL(url) : new URL(url, relativeTo);
   59     return {origin: parsed.origin, path: parsed.pathname, search: parsed.search};
   60   }
   61 
   62   /**
   63    * Wait for a given amount of time before completing a Promise.
   64    */
   65   timeout(ms: number): Promise<void> {
   66     return new Promise<void>(resolve => { setTimeout(() => resolve(), ms); });
   67   }
   68 }
   69 
   70 /**
   71  * An event context in which an operation is taking place, which allows
   72  * the delaying of Service Worker shutdown until certain triggers occur.
   73  */
   74 export interface Context {
   75   /**
   76    * Delay shutdown of the Service Worker until the given promise resolves.
   77    */
   78   waitUntil(fn: Promise<any>): void;
   79 }