"Fossies" - the Fresh Open Source Software Archive

Member "angular-16.2.7/packages/service-worker/worker/src/adapter.ts" (27 Sep 2023, 3232 Bytes) of package /linux/www/angular-16.2.7.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.

    1 /**
    2  * @license
    3  * Copyright Google LLC 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 import {NormalizedUrl} from './api';
   10 import {NamedCacheStorage} from './named-cache-storage';
   11 
   12 
   13 /**
   14  * Adapts the service worker to its runtime environment.
   15  *
   16  * Mostly, this is used to mock out identifiers which are otherwise read
   17  * from the global scope.
   18  */
   19 export class Adapter<T extends CacheStorage = CacheStorage> {
   20   readonly caches: NamedCacheStorage<T>;
   21   readonly origin: string;
   22 
   23   constructor(protected readonly scopeUrl: string, caches: T) {
   24     const parsedScopeUrl = this.parseUrl(this.scopeUrl);
   25 
   26     // Determine the origin from the registration scope. This is used to differentiate between
   27     // relative and absolute URLs.
   28     this.origin = parsedScopeUrl.origin;
   29 
   30     // Use the baseHref in the cache name prefix to avoid clash of cache names for SWs with
   31     // different scopes on the same domain.
   32     this.caches = new NamedCacheStorage(caches, `ngsw:${parsedScopeUrl.path}`);
   33   }
   34 
   35   /**
   36    * Wrapper around the `Request` constructor.
   37    */
   38   newRequest(input: string|Request, init?: RequestInit): Request {
   39     return new Request(input, init);
   40   }
   41 
   42   /**
   43    * Wrapper around the `Response` constructor.
   44    */
   45   newResponse(body: any, init?: ResponseInit) {
   46     return new Response(body, init);
   47   }
   48 
   49   /**
   50    * Wrapper around the `Headers` constructor.
   51    */
   52   newHeaders(headers: {[name: string]: string}): Headers {
   53     return new Headers(headers);
   54   }
   55 
   56   /**
   57    * Test if a given object is an instance of `Client`.
   58    */
   59   isClient(source: any): source is Client {
   60     return (source instanceof Client);
   61   }
   62 
   63   /**
   64    * Read the current UNIX time in milliseconds.
   65    */
   66   get time(): number {
   67     return Date.now();
   68   }
   69 
   70   /**
   71    * Get a normalized representation of a URL such as those found in the ServiceWorker's `ngsw.json`
   72    * configuration.
   73    *
   74    * More specifically:
   75    * 1. Resolve the URL relative to the ServiceWorker's scope.
   76    * 2. If the URL is relative to the ServiceWorker's own origin, then only return the path part.
   77    *    Otherwise, return the full URL.
   78    *
   79    * @param url The raw request URL.
   80    * @return A normalized representation of the URL.
   81    */
   82   normalizeUrl(url: string): NormalizedUrl {
   83     // Check the URL's origin against the ServiceWorker's.
   84     const parsed = this.parseUrl(url, this.scopeUrl);
   85     return (parsed.origin === this.origin ? parsed.path : url) as NormalizedUrl;
   86   }
   87 
   88   /**
   89    * Parse a URL into its different parts, such as `origin`, `path` and `search`.
   90    */
   91   parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
   92     // Workaround a Safari bug, see
   93     // https://github.com/angular/angular/issues/31061#issuecomment-503637978
   94     const parsed = !relativeTo ? new URL(url) : new URL(url, relativeTo);
   95     return {origin: parsed.origin, path: parsed.pathname, search: parsed.search};
   96   }
   97 
   98   /**
   99    * Wait for a given amount of time before completing a Promise.
  100    */
  101   timeout(ms: number): Promise<void> {
  102     return new Promise<void>(resolve => {
  103       setTimeout(() => resolve(), ms);
  104     });
  105   }
  106 }