| 
 | 1 | +import { get as httpGet } from 'http';  | 
 | 2 | +import { get as httpsGet } from 'https';  | 
 | 3 | +import { browser } from 'protractor';  | 
 | 4 | +import { SitePage } from './app.po';  | 
 | 5 | + | 
 | 6 | + | 
 | 7 | +export type Json = null | boolean | number | string | Json[] | { [key: string]: Json };  | 
 | 8 | + | 
 | 9 | +/**  | 
 | 10 | + * The shape of a PWA manifest.  | 
 | 11 | + * For simplicity, we only define types for the properties we care about in tests.  | 
 | 12 | + * @see https://developer.mozilla.org/en-US/docs/Web/Manifest  | 
 | 13 | + */  | 
 | 14 | +export type PwaManifest = Json & {  | 
 | 15 | +  shortcuts?: PwaShortcutItem[],  | 
 | 16 | +};  | 
 | 17 | + | 
 | 18 | +/**  | 
 | 19 | + * The shape of an item in a PWA manifest's `shortcuts` list.  | 
 | 20 | + * @see https://developer.mozilla.org/en-US/docs/Web/Manifest/shortcuts  | 
 | 21 | + */  | 
 | 22 | +export type PwaShortcutItem = Json & {  | 
 | 23 | +  url: string,  | 
 | 24 | +  name: string,  | 
 | 25 | +  short_name?: string,  | 
 | 26 | +  description?: string,  | 
 | 27 | +  icons?: PwaImageResource[],  | 
 | 28 | +};  | 
 | 29 | + | 
 | 30 | +/**  | 
 | 31 | + * The shape of an item in a PWA manifest's icons list (such as the value of the top-level `icons` property or that of  | 
 | 32 | + * the `icons` property of a shortcut item).  | 
 | 33 | + * @see https://w3c.github.io/manifest/#manifestimageresource-and-its-members  | 
 | 34 | + */  | 
 | 35 | +export type PwaImageResource = Json & {  | 
 | 36 | +  src: string,  | 
 | 37 | +  sizes?: string,  | 
 | 38 | +  type?: string,  | 
 | 39 | +  purpose?: string,  | 
 | 40 | +};  | 
 | 41 | + | 
 | 42 | + | 
 | 43 | +export class PwaManifestPage extends SitePage {  | 
 | 44 | +  /** The base URL with the trailing `/` stripped off (if any). */  | 
 | 45 | +  baseUrl = browser.baseUrl.replace(/\/$/, '');  | 
 | 46 | + | 
 | 47 | +  /** The URL to the app's PWA manifest. */  | 
 | 48 | +  pwaManifestUrl = `${this.baseUrl}/pwa-manifest.json`;  | 
 | 49 | + | 
 | 50 | +  private pwaManifestText: string | null = null;  | 
 | 51 | + | 
 | 52 | +  /** Get the app's PWA manifest as an object. */  | 
 | 53 | +  async getPwaManifest(): Promise<PwaManifest> {  | 
 | 54 | +    if (this.pwaManifestText === null) {  | 
 | 55 | +      const get = /^https:/.test(this.pwaManifestUrl) ? httpsGet : httpGet;  | 
 | 56 | + | 
 | 57 | +      this.pwaManifestText = await new Promise<string>((resolve, reject) => {  | 
 | 58 | +        let responseText = '';  | 
 | 59 | +        get(this.pwaManifestUrl, res => res  | 
 | 60 | +            .on('data', chunk => responseText += chunk)  | 
 | 61 | +            .on('end', () => resolve(responseText))  | 
 | 62 | +            .on('error', reject));  | 
 | 63 | +      });  | 
 | 64 | +    }  | 
 | 65 | + | 
 | 66 | +    return JSON.parse(this.pwaManifestText);  | 
 | 67 | +  }  | 
 | 68 | + | 
 | 69 | +  /** Get a list of PWA shortcuts as extracted from the app's PWA manifest. */  | 
 | 70 | +  async getPwaShortcuts(): Promise<PwaShortcutItem[]> {  | 
 | 71 | +    const {shortcuts = []} = await this.getPwaManifest();  | 
 | 72 | +    return shortcuts;  | 
 | 73 | +  }  | 
 | 74 | +}  | 
0 commit comments