All files / skeleton/modules/autoupdate assetManifest.js

90.48% Statements 19/21
80% Branches 16/20
100% Functions 5/5
90.48% Lines 19/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                                                                                                  1837x                                               141x         4x 4x     141x                   141x 141x   141x     141x       141x     141x   2x     139x   139x 1964x 1837x   139x   2x       1x              
/**
 This is a slightly modified JS port of hot code push android client from here:
 https://github.com/meteor/cordova-plugin-meteor-webapp
 
 The MIT License (MIT)
 
 Copyright (c) 2015 Meteor Development Group
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 This file is based on:
 /cordova-plugin-meteor-webapp/blob/master/src/android/AssetManifest.java
 
 */
 
import assignIn from 'lodash/assignIn';
 
/**
 * Represents single file in the manifest.
 *
 * @param {object} manifestEntry
 * @param {string} manifestEntry.path
 * @param {string} manifestEntry.url
 * @param {string} manifestEntry.type
 * @param {number} manifestEntry.size
 * @param {bool}   manifestEntry.cacheable
 * @param {string} manifestEntry.hash
 * @param {string} manifestEntry.sourceMap
 * @param {string} manifestEntry.sourceMapUrl
 *
 * @property {string} filePath
 * @property {string} urlPath
 * @property {string} fileType
 * @property {number} size
 * @property {bool}   cacheable
 * @property {string} hash
 * @property {string} sourceMapFilePath
 * @property {string} sourceMapUrlPath
 * @constructor
 */
function ManifestEntry(manifestEntry) {
    assignIn(this, {
        filePath: manifestEntry.path,
        urlPath: manifestEntry.url,
        fileType: manifestEntry.type,
        size: manifestEntry.size,
        cacheable: manifestEntry.cacheable,
        hash: manifestEntry.hash || null,
        sourceMapFilePath: manifestEntry.sourceMap || null,
        sourceMapUrlPath: manifestEntry.sourceMapUrl || null
    });
}
 
/**
 * Represents a program.json app manifest.
 *
 * @param {Object} logger         - Logger instance.
 * @param {string} manifestSource - Manifest source.
 *
 * @property {string} version
 * @property {string} cordovaCompatibilityVersion
 *
 * @constructor
 */
export default function AssetManifest(logger, manifestSource) {
    const log = logger.getLoggerFor('AssetManifest');
    let json;
    let format;
 
    function error(msg) {
        log.error(msg);
        throw new Error(msg);
    }
 
    try {
        /**
         * @type object
         * @property {string} format
         * @property {string|null} version
         * @property {object} cordovaCompatibilityVersions
         * @property {string} cordovaCompatibilityVersions.android
         * @property {string} cordovaCompatibilityVersions.ios
         * @property {Array} manifest
         */
        json = JSON.parse(manifestSource);
        format = json.format || null;
 
        Iif (format !== null && format !== 'web-program-pre1') {
            error(`The asset manifest format is incompatible: ${format}`);
        }
        Iif (!('version' in json) || json.version === null) {
            error('Asset manifest does not have a version.');
        }
 
        this.version = json.version;
 
        // We are not using compatibility versions, but for sanity check this is ok.
        if (!('cordovaCompatibilityVersions' in json) ||
            !('android' in json.cordovaCompatibilityVersions)) {
            error('Asset manifest does not have a cordovaCompatibilityVersion.');
        }
 
        this.cordovaCompatibilityVersion = json.cordovaCompatibilityVersions.android;
 
        this.entries = json.manifest
            .filter(manifestEntry => manifestEntry.where === 'client')
            .map(manifestEntry => new ManifestEntry(manifestEntry));
 
        log.debug(`${this.entries.length} entries. (Version: ${this.version})`);
    } catch (e) {
        error(`error parsing asset manifest: ${e.message}`);
    }
}
 
module.exports = AssetManifest;
 
/**
 * @typedef {Object} AssetManifest
 * @property {string} version
 * @property {string} cordovaCompatibilityVersion
 */