[react-packager] implement /assets endpoint to serve assets

This commit is contained in:
Amjad Masad
2015-04-16 13:00:11 -07:00
parent fd8bc3b5f6
commit fb7036eaac
11 changed files with 350 additions and 25 deletions

View File

@@ -1,109 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
var fs = jest.genMockFromModule('fs');
fs.realpath.mockImpl(function(filepath, callback) {
var node;
try {
node = getToNode(filepath);
} catch (e) {
return callback(e);
}
if (node && typeof node === 'object' && node.SYMLINK != null) {
return callback(null, node.SYMLINK);
}
callback(null, filepath);
});
fs.readdir.mockImpl(function(filepath, callback) {
var node;
try {
node = getToNode(filepath);
if (node && typeof node === 'object' && node.SYMLINK != null) {
node = getToNode(node.SYMLINK);
}
} catch (e) {
return callback(e);
}
if (!(node && typeof node === 'object' && node.SYMLINK == null)) {
return callback(new Error(filepath + ' is not a directory.'));
}
callback(null, Object.keys(node));
});
fs.readFile.mockImpl(function(filepath, encoding, callback) {
try {
var node = getToNode(filepath);
// dir check
if (node && typeof node === 'object' && node.SYMLINK == null) {
callback(new Error('Trying to read a dir, ESIDR, or whatever'));
}
return callback(null, node);
} catch (e) {
return callback(e);
}
});
fs.lstat.mockImpl(function(filepath, callback) {
var node;
try {
node = getToNode(filepath);
} catch (e) {
return callback(e);
}
if (node && typeof node === 'object' && node.SYMLINK == null) {
callback(null, {
isDirectory: function() {
return true;
},
isSymbolicLink: function() {
return false;
}
});
} else {
callback(null, {
isDirectory: function() {
return false;
},
isSymbolicLink: function() {
if (typeof node === 'object' && node.SYMLINK) {
return true;
}
return false;
}
});
}
});
var filesystem;
fs.__setMockFilesystem = function(object) {
filesystem = object;
return filesystem;
};
function getToNode(filepath) {
var parts = filepath.split('/');
if (parts[0] !== '') {
throw new Error('Make sure all paths are absolute.');
}
var node = filesystem;
parts.slice(1).forEach(function(part) {
node = node[part];
});
return node;
}
module.exports = fs;

View File

@@ -14,6 +14,7 @@ jest
.dontMock('absolute-path')
.dontMock('../docblock')
.dontMock('../../replacePatterns')
.dontMock('../../../../lib/extractAssetResolution')
.setMock('../../../ModuleDescriptor', function(data) {return data;});
describe('DependencyGraph', function() {

View File

@@ -18,6 +18,7 @@ var isAbsolutePath = require('absolute-path');
var debug = require('debug')('DependecyGraph');
var util = require('util');
var declareOpts = require('../../../lib/declareOpts');
var extractAssetResolution = require('../../../lib/extractAssetResolution');
var readFile = Promise.promisify(fs.readFile);
var readDir = Promise.promisify(fs.readdir);
@@ -421,7 +422,7 @@ DependecyGraph.prototype._processModule = function(modulePath) {
var module;
if (this._assetExts.indexOf(extname(modulePath)) > -1) {
var assetData = extractResolutionPostfix(this._lookupName(modulePath));
var assetData = extractAssetResolution(this._lookupName(modulePath));
moduleData.id = assetData.assetName;
moduleData.resolution = assetData.resolution;
moduleData.isAsset = true;
@@ -772,28 +773,6 @@ function extname(name) {
return path.extname(name).replace(/^\./, '');
}
function extractResolutionPostfix(filename) {
var ext = extname(filename);
var re = new RegExp('@([\\d\\.]+)x\\.' + ext + '$');
var match = filename.match(re);
var resolution;
if (!(match && match[1])) {
resolution = 1;
} else {
resolution = parseFloat(match[1], 10);
if (isNaN(resolution)) {
resolution = 1;
}
}
return {
resolution: resolution,
assetName: match ? filename.replace(re, '.' + ext) : filename,
};
}
function NotFoundError() {
Error.call(this);
Error.captureStackTrace(this, this.constructor);