Assets.js 10.8 KB
Newer Older
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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 *
 * @format
 */
"use strict";

function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread()
  );
}

function _nonIterableSpread() {
  throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) {
    for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++)
      arr2[i] = arr[i];
    return arr2;
  }
}

function _toArray(arr) {
  return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
}

function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArray(iter) {
  if (
    Symbol.iterator in Object(iter) ||
    Object.prototype.toString.call(iter) === "[object Arguments]"
  )
    return Array.from(iter);
}

function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  try {
    var info = gen[key](arg);
    var value = info.value;
  } catch (error) {
    reject(error);
    return;
  }
  if (info.done) {
    resolve(value);
  } else {
    Promise.resolve(value).then(_next, _throw);
  }
}

function _asyncToGenerator(fn) {
  return function() {
    var self = this,
      args = arguments;
    return new Promise(function(resolve, reject) {
      var gen = fn.apply(self, args);
      function _next(value) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
      }
      function _throw(err) {
        asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
      }
      _next(undefined);
    });
  };
}

const AssetPaths = require("./node-haste/lib/AssetPaths");

const crypto = require("crypto");

const denodeify = require("denodeify");

const fs = require("fs");

const imageSize = require("image-size");

const path = require("path");

const _require = require("./Bundler/util"),
  isAssetTypeAnImage = _require.isAssetTypeAnImage;

const readDir = denodeify(fs.readdir);
const readFile = denodeify(fs.readFile);
const hashFiles = denodeify(function hashFilesCb(files, hash, callback) {
  if (!files.length) {
    callback(null);
    return;
  }

  const file = files.shift();
  fs.readFile(file, (err, data) => {
    if (err) {
      callback(err);
      return;
    } else {
      hash.update(data);
      hashFilesCb(files, hash, callback);
    }
  });
});

function buildAssetMap(dir, files, platform) {
  const platforms = new Set(platform != null ? [platform] : []);
  const assets = files.map(file => AssetPaths.tryParse(file, platforms));
  const map = new Map();
  assets.forEach(function(asset, i) {
    if (asset == null) {
      return;
    }

    const file = files[i];
    const assetKey = getAssetKey(asset.assetName, asset.platform);
    let record = map.get(assetKey);

    if (!record) {
      record = {
        scales: [],
        files: []
      };
      map.set(assetKey, record);
    }

    let insertIndex;
    const length = record.scales.length;

    for (insertIndex = 0; insertIndex < length; insertIndex++) {
      if (asset.resolution < record.scales[insertIndex]) {
        break;
      }
    }

    record.scales.splice(insertIndex, 0, asset.resolution);
    record.files.splice(insertIndex, 0, path.join(dir, file));
  });
  return map;
}

function getAssetKey(assetName, platform) {
  if (platform != null) {
    return `${assetName} : ${platform}`;
  } else {
    return assetName;
  }
}

function getAbsoluteAssetRecord(_x) {
  return _getAbsoluteAssetRecord.apply(this, arguments);
}

function _getAbsoluteAssetRecord() {
  _getAbsoluteAssetRecord = _asyncToGenerator(function*(assetPath) {
    let platform =
      arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
    const filename = path.basename(assetPath);
    const dir = path.dirname(assetPath);
    const files = yield readDir(dir);
    const assetData = AssetPaths.parse(
      filename,
      new Set(platform != null ? [platform] : [])
    );
    const map = buildAssetMap(dir, files, platform);
    let record;

    if (platform != null) {
      record =
        map.get(getAssetKey(assetData.assetName, platform)) ||
        map.get(assetData.assetName);
    } else {
      record = map.get(assetData.assetName);
    }

    if (!record) {
      throw new Error(
        /* $FlowFixMe: platform can be null */
        `Asset not found: ${assetPath} for platform: ${platform}`
      );
    }

    return record;
  });
  return _getAbsoluteAssetRecord.apply(this, arguments);
}

function getAbsoluteAssetInfo(_x2) {
  return _getAbsoluteAssetInfo.apply(this, arguments);
}

function _getAbsoluteAssetInfo() {
  _getAbsoluteAssetInfo = _asyncToGenerator(function*(assetPath) {
    let platform =
      arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
    const nameData = AssetPaths.parse(
      assetPath,
      new Set(platform != null ? [platform] : [])
    );
    const name = nameData.name,
      type = nameData.type;

    const _ref = yield getAbsoluteAssetRecord(assetPath, platform),
      scales = _ref.scales,
      files = _ref.files;

    const hasher = crypto.createHash("md5");

    if (files.length > 0) {
      yield hashFiles(Array.from(files), hasher);
    }

    return {
      files,
      hash: hasher.digest("hex"),
      name,
      scales,
      type
    };
  });
  return _getAbsoluteAssetInfo.apply(this, arguments);
}

function getAssetData(_x3, _x4, _x5) {
  return _getAssetData.apply(this, arguments);
}

function _getAssetData() {
  _getAssetData = _asyncToGenerator(function*(
    assetPath,
    localPath,
    assetDataPlugins
  ) {
    let platform =
      arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
    let publicPath = arguments.length > 4 ? arguments[4] : undefined;
    // If the path of the asset is outside of the projectRoot, we don't want to
    // use `path.join` since this will generate an incorrect URL path. In that
    // case we just concatenate the publicPath with the relative path.
    let assetUrlPath = localPath.startsWith("..")
      ? publicPath.replace(/\/$/, "") + "/" + path.dirname(localPath)
      : path.join(publicPath, path.dirname(localPath)); // On Windows, change backslashes to slashes to get proper URL path from file path.

    if (path.sep === "\\") {
      assetUrlPath = assetUrlPath.replace(/\\/g, "/");
    }

    const isImage = isAssetTypeAnImage(path.extname(assetPath).slice(1));
    const assetInfo = yield getAbsoluteAssetInfo(assetPath, platform);
    const isImageInput = assetInfo.files[0].includes(".zip/")
      ? fs.readFileSync(assetInfo.files[0])
      : assetInfo.files[0];
    const dimensions = isImage ? imageSize(isImageInput) : null;
    const scale = assetInfo.scales[0];
    const assetData = {
      __packager_asset: true,
      fileSystemLocation: path.dirname(assetPath),
      httpServerLocation: assetUrlPath,
      width: dimensions ? dimensions.width / scale : undefined,
      height: dimensions ? dimensions.height / scale : undefined,
      scales: assetInfo.scales,
      files: assetInfo.files,
      hash: assetInfo.hash,
      name: assetInfo.name,
      type: assetInfo.type
    };
    return yield applyAssetDataPlugins(assetDataPlugins, assetData);
  });
  return _getAssetData.apply(this, arguments);
}

function applyAssetDataPlugins(_x6, _x7) {
  return _applyAssetDataPlugins.apply(this, arguments);
}
/**
 * Returns all the associated files (for different resolutions) of an asset.
 **/

function _applyAssetDataPlugins() {
  _applyAssetDataPlugins = _asyncToGenerator(function*(
    assetDataPlugins,
    assetData
  ) {
    if (!assetDataPlugins.length) {
      return assetData;
    }

    const _assetDataPlugins = _toArray(assetDataPlugins),
      currentAssetPlugin = _assetDataPlugins[0],
      remainingAssetPlugins = _assetDataPlugins.slice(1); // $FlowFixMe: impossible to type a dynamic require.

    const assetPluginFunction = require(currentAssetPlugin);

    const resultAssetData = yield assetPluginFunction(assetData);
    return yield applyAssetDataPlugins(remainingAssetPlugins, resultAssetData);
  });
  return _applyAssetDataPlugins.apply(this, arguments);
}

function getAssetFiles(_x8) {
  return _getAssetFiles.apply(this, arguments);
}
/**
 * Return a buffer with the actual image given a request for an image by path.
 * The relativePath can contain a resolution postfix, in this case we need to
 * find that image (or the closest one to it's resolution) in one of the
 * project roots:
 *
 * 1. We first parse the directory of the asset
 * 2. We then build a map of all assets and their scales in this directory
 * 3. Then try to pick platform-specific asset records
 * 4. Then pick the closest resolution (rounding up) to the requested one
 */

function _getAssetFiles() {
  _getAssetFiles = _asyncToGenerator(function*(assetPath) {
    let platform =
      arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
    const assetData = yield getAbsoluteAssetRecord(assetPath, platform);
    return assetData.files;
  });
  return _getAssetFiles.apply(this, arguments);
}

function getAsset(_x9, _x10, _x11) {
  return _getAsset.apply(this, arguments);
}

function _getAsset() {
  _getAsset = _asyncToGenerator(function*(
    relativePath,
    projectRoot,
    watchFolders
  ) {
    let platform =
      arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
    let assetExts = arguments.length > 4 ? arguments[4] : undefined;
    const assetData = AssetPaths.parse(
      relativePath,
      new Set(platform != null ? [platform] : [])
    );
    const absolutePath = path.resolve(projectRoot, relativePath);

    if (!assetExts.includes(assetData.type)) {
      throw new Error(
        `'${relativePath}' cannot be loaded as its extension is not registered in assetExts`
      );
    }

    if (
      !pathBelongsToRoots(
        absolutePath,
        [projectRoot].concat(_toConsumableArray(watchFolders))
      )
    ) {
      throw new Error(
        `'${relativePath}' could not be found, because it cannot be found in the project root or any watch folder`
      );
    }

    const record = yield getAbsoluteAssetRecord(absolutePath, platform);

    for (let i = 0; i < record.scales.length; i++) {
      if (record.scales[i] >= assetData.resolution) {
        return readFile(record.files[i]);
      }
    }

    return readFile(record.files[record.files.length - 1]);
  });
  return _getAsset.apply(this, arguments);
}

function pathBelongsToRoots(pathToCheck, roots) {
  for (const rootFolder of roots) {
    if (pathToCheck.startsWith(path.resolve(rootFolder))) {
      return true;
    }
  }

  return false;
}

module.exports = {
  getAsset,
  getAssetData,
  getAssetFiles
};