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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | 1470x 1470x 1470x 1470x 1470x 1470x 1470x 1470x 1470x 408x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 82x 82x 48x 130x 130x 130x 130x 130x 130x 130x 130x 1708x 1708x 1036x 1708x 472x 304x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 145x 145x 1096x 978x 978x 846x 132x 48x 200x 200x 713x 248x 116x 248x 100x 100x 100x 100x 100x 100x 159x 100x 100x 100x 100x 159x 34x 82x 82x 82x 116x 116x 116x 116x 116x 116x 1470x 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/AssetBundle.java */ import path from 'path'; import fs from 'fs'; import url from 'url'; import AssetManifest from './assetManifest'; /** * Represent single asset in the bundle. * * @property {string} filePath * @property {string} urlPath * @property {string} fileType * @property {number} size * @property {bool} cacheable * @property {string} hash * @property {string} sourceMapFilePath * @property {string} sourceMapUrlPath * @property {AssetBundle} bundle * @constructor */ function Asset(filePath, urlPath, fileType, cacheable, hash, sourceMapUrlPath, size, bundle) { this.filePath = filePath; this.urlPath = urlPath; this.fileType = fileType; this.cacheable = cacheable; this.hash = hash; this.entrySize = size; this.sourceMapUrlPath = sourceMapUrlPath; this.bundle = bundle; this.getFile = function getFile() { return path.join(this.bundle.directoryUri, filePath); }; } export default class AssetBundle { /** * Represents assets bundle. * * @param {object} log - Winston reference. * @param {string} directoryUri - Where the bundle lies in the file system. * @param {AssetManifest=} manifest - Bundle's manifest. * @param {AssetBundle=} parentAssetBundle - Parent asset bundle. * @param {Object=} desktopVersion - Object with desktop version and compatibility * version. * @constructor */ constructor(log, directoryUri, manifest, parentAssetBundle, desktopVersion = {}) { this.log = log.getLoggerFor('AssetBundle'); this.log.verbose(`making bundle object for ${directoryUri}`); this.directoryUri = directoryUri; this.runtimeConfig = null; this.appId = null; this.desktopVersion = desktopVersion; this.rootUrlString = null; this.matcher = new RegExp( '__meteor_runtime_config__ = JSON.parse\\(decodeURIComponent\\("([^"]*)"\\)\\)' ); this.parentAssetBundle = parentAssetBundle; if (manifest === undefined) { this.log.verbose(`loading manifest from ${directoryUri}`); this.manifest = this.loadAssetManifest(); } else { this.manifest = manifest; } this.version = this.manifest.version; this.cordovaCompatibilityVersion = this.manifest.cordovaCompatibilityVersion; Eif (!desktopVersion.version) { this.log.verbose(`trying to read desktop version for ${this.version}`); this.desktopVersion = this.loadDesktopVersion(); this.log.verbose(`the desktop version is ${this.desktopVersion.version}`); } this.ownAssetsByURLPath = {}; // Filter assets that are only in this bundle. Rest can be taken from the parent. this.manifest.entries.forEach((entry) => { const urlPath = url.parse(entry.urlPath).pathname; if (parentAssetBundle === undefined || parentAssetBundle.cachedAssetForUrlPath(urlPath, entry.hash) === null) { this.addAsset(new Asset( entry.filePath, urlPath, entry.fileType, entry.cacheable, entry.hash, entry.sourceMapUrlPath, entry.size, this )); } if (entry.sourceMapFilePath !== null && entry.sourceMapUrlPath !== null) { if (parentAssetBundle === undefined || parentAssetBundle .cachedAssetForUrlPath(entry.sourceMapUrlPath, null) === null) { this.addAsset(new Asset( entry.sourceMapFilePath, entry.sourceMapUrlPath, 'json', true, null, null, entry.size, this )); } } }); const indexFile = new Asset('index.html', '/', 'html', false, null, null, null, this); this.addAsset(indexFile); this.indexFile = indexFile; } /** * Load this bundle's desktop version if present. * * @private */ loadDesktopVersion() { const desktopVersionPath = path.join(this.directoryUri, '_desktop.json'); const desktopVersionFallbackPath = path.join(this.directoryUri, 'app', 'version.desktop.json'); try { return JSON.parse(fs.readFileSync(desktopVersionPath, 'UTF-8')); } catch (e) { try { return JSON.parse(fs.readFileSync(desktopVersionFallbackPath, 'UTF-8')); } catch (err) { return {}; } } } writeDesktopVersion() { if (this.desktopVersion.version) { fs.writeFileSync( path.join(this.directoryUri, '_desktop.json'), JSON.stringify(this.desktopVersion, null, '\t') ); } } /** * Directory uri getter. * @returns {string} */ getDirectoryUri() { return this.directoryUri; } /** * Parent asset bundle getter. * @returns {null|AssetBundle} */ getParentAssetBundle() { return this.parentAssetBundle; } /** * Returns an cacheable or hash equal asset. * * @param {string} urlPath - The url path of the asset. * @param {string|null} hash - Hash of the asset. * @returns {null|Asset} */ cachedAssetForUrlPath(urlPath, hash) { if (!(urlPath in this.ownAssetsByURLPath)) return null; const asset = this.ownAssetsByURLPath[urlPath]; // If the asset is not cacheable, we require a matching hash. if ((asset.cacheable && hash === null) || (asset.hash !== null && asset.hash === hash)) { return asset; } return null; } /** * Returns an array of own assets. * * @returns {Array} */ getOwnAssets() { return Object.keys(this.ownAssetsByURLPath) .reduce((arr, key) => { arr.push(this.ownAssetsByURLPath[key]); return arr; }, []); } /** * Version getter. * @returns {string} */ getVersion() { return this.version; } /** * Loads runtime config. * * @returns {Object} */ getRuntimeConfig() { if (this.runtimeConfig === null) { this.runtimeConfig = this.loadRuntimeConfig( path.join(this.directoryUri, this.indexFile.filePath) ); } return this.runtimeConfig; } /** * App id getter. * * @returns {String} */ getAppId() { Eif (this.appId === null) { const runtimeConfig = this.getRuntimeConfig(); Eif (runtimeConfig !== null) { Iif (!('appId' in runtimeConfig)) { this.log.error('error reading APP_ID from runtime config'); } else { this.appId = runtimeConfig.appId; } } } return this.appId; } /** * Return ROOT_URL from runtime config. * * @returns {string} */ getRootUrlString() { if (this.rootUrlString === null) { const runtimeConfig = this.getRuntimeConfig(); Eif (runtimeConfig !== null) { Iif (!('ROOT_URL' in runtimeConfig)) { this.log.error('error reading ROOT_URL from runtime config'); } else { this.rootUrlString = runtimeConfig.ROOT_URL; } } } return this.rootUrlString; } /** * Changes bundle directory uri. * * @param {string} directoryUri - New directory path. */ didMoveToDirectoryAtUri(directoryUri) { this.directoryUri = directoryUri; } /** * Returns asset queried by url path. * !UNUSED! Left in case of implementation change. * * @param {string} urlPath - Url path of the asset. * * @returns {Asset} */ assetForUrlPath(urlPath) { let asset; if (urlPath in this.ownAssetsByURLPath) { asset = this.ownAssetsByURLPath[urlPath]; } else if (this.parentAssetBundle !== null) { asset = this.parentAssetBundle.assetForUrlPath(urlPath); } return asset; } /** * Load this bundle's asset manifest. * * @private * @returns {AssetManifest} */ loadAssetManifest() { const manifestPath = path.join(this.directoryUri, 'program.json'); try { return new AssetManifest( this.log, fs.readFileSync(manifestPath, 'UTF-8') ); } catch (e) { const msg = `error loading asset manifest: ${e.message}`; this.log.error(msg); this.log.debug(e); throw new Error(msg); } } /** * Extracts and parses runtime config. * TODO: no negative path errors in case loadRuntimeConfig fails? * * @param {string} index - Path for index.html. * @private * @returns {null} */ loadRuntimeConfig(index) { let content; try { content = fs.readFileSync(index, 'UTF-8'); } catch (e) { this.log.error(`error loading index file: ${e.message}`); return null; } Iif (!this.matcher.test(content)) { this.log.error('could not find runtime config in index file'); return null; } try { const matches = content.match(this.matcher); return JSON.parse(decodeURIComponent(matches[1])); } catch (e) { this.log.error('could not find runtime config in index file'); return null; } } /** * Adds an asset to own assets collection. * * @param {Asset} asset - Asset to add. * @private */ addAsset(asset) { this.ownAssetsByURLPath[asset.urlPath] = asset; } } module.exports = AssetBundle; |