| 1 | /*
|
|---|
| 2 | Copyright 2019 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {errors} from './errors';
|
|---|
| 10 | import {ManifestEntry} from '../types';
|
|---|
| 11 |
|
|---|
| 12 | type AdditionalManifestEntriesTransform = {
|
|---|
| 13 | (manifest: Array<ManifestEntry & {size: number}>): {
|
|---|
| 14 | manifest: Array<ManifestEntry & {size: number}>;
|
|---|
| 15 | warnings: string[];
|
|---|
| 16 | };
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | export function additionalManifestEntriesTransform(
|
|---|
| 20 | additionalManifestEntries: Array<ManifestEntry | string>,
|
|---|
| 21 | ): AdditionalManifestEntriesTransform {
|
|---|
| 22 | return (manifest: Array<ManifestEntry & {size: number}>) => {
|
|---|
| 23 | const warnings: Array<string> = [];
|
|---|
| 24 | const stringEntries = new Set<string>();
|
|---|
| 25 |
|
|---|
| 26 | for (const additionalEntry of additionalManifestEntries) {
|
|---|
| 27 | // Warn about either a string or an object that lacks a revision property.
|
|---|
| 28 | // (An object with a revision property set to null is okay.)
|
|---|
| 29 | if (typeof additionalEntry === 'string') {
|
|---|
| 30 | stringEntries.add(additionalEntry);
|
|---|
| 31 | manifest.push({
|
|---|
| 32 | revision: null,
|
|---|
| 33 | size: 0,
|
|---|
| 34 | url: additionalEntry,
|
|---|
| 35 | });
|
|---|
| 36 | } else {
|
|---|
| 37 | if (additionalEntry && additionalEntry.revision === undefined) {
|
|---|
| 38 | stringEntries.add(additionalEntry.url);
|
|---|
| 39 | }
|
|---|
| 40 | manifest.push(Object.assign({size: 0}, additionalEntry));
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (stringEntries.size > 0) {
|
|---|
| 45 | let urls = '\n';
|
|---|
| 46 | for (const stringEntry of stringEntries) {
|
|---|
| 47 | urls += ` - ${stringEntry}\n`;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | warnings.push(errors['string-entry-warning'] + urls);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return {
|
|---|
| 54 | manifest,
|
|---|
| 55 | warnings,
|
|---|
| 56 | };
|
|---|
| 57 | };
|
|---|
| 58 | }
|
|---|