Skip to content

Commit

Permalink
Merge pull request ember-fastboot#200 from zonkyio/require-module-path
Browse files Browse the repository at this point in the history
Allow to require module path from whitelisted dependency
  • Loading branch information
kratiahuja committed Nov 29, 2018
2 parents b79df75 + 708e8da commit 4516747
Show file tree
Hide file tree
Showing 13 changed files with 84,989 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"cookie": "^0.3.1",
"debug": "^3.0.0",
"najax": "^1.0.2",
"resolve": "^1.8.1",
"rsvp": "^4.7.0",
"simple-dom": "^1.0.0",
"source-map-support": "^0.5.0"
Expand Down
42 changes: 34 additions & 8 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const chalk = require('chalk');

const najax = require('najax');
const SimpleDOM = require('simple-dom');
const resolve = require('resolve');
const debug = require('debug')('fastboot:ember-app');

const FastBootInfo = require('./fastboot-info');
const Result = require('./result');
const FastBootSchemaVersions = require('./fastboot-schema-versions');
const getPackageName = require('./utils/get-package-name');

/**
* @private
Expand Down Expand Up @@ -38,6 +40,7 @@ class EmberApp {
this.hostWhitelist = config.hostWhitelist;
this.config = config.config;
this.appName = config.appName;
this.schemaVersion = config.schemaVersion;

if (process.env.APP_CONFIG) {
let appConfig = JSON.parse(process.env.APP_CONFIG);
Expand Down Expand Up @@ -114,23 +117,45 @@ class EmberApp {
* @param {string} distPath path to the built Ember app
*/
buildWhitelistedRequire(whitelist, distPath) {
let isLegacyWhitelist = this.schemaVersion < FastBootSchemaVersions.strictWhitelist;

whitelist.forEach(function(whitelistedModule) {
debug("module whitelisted; module=%s", whitelistedModule);

if (isLegacyWhitelist) {
let packageName = getPackageName(whitelistedModule);

if (packageName !== whitelistedModule && whitelist.indexOf(packageName) === -1) {
console.error("Package '" + packageName + "' is required to be in the whitelist.");
}
}
});

return function(moduleName) {
if (whitelist.indexOf(moduleName) > -1) {
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);
let packageName = getPackageName(moduleName);
let isWhitelisted = whitelist.indexOf(packageName) > -1;

if (fs.existsSync(nodeModulesPath)) {
return require(nodeModulesPath);
if (isWhitelisted) {
let resolvedModulePath = resolve.sync(moduleName, { basedir: distPath });
return require(resolvedModulePath);
}

if (isLegacyWhitelist) {
if (whitelist.indexOf(moduleName) > -1) {
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);

if (fs.existsSync(nodeModulesPath)) {
return require(nodeModulesPath);
} else {
// If it's not on disk, assume it's a built-in node package
return require(moduleName);
}
} else {
// If it's not on disk, assume it's a built-in node package
return require(moduleName);
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
}
} else {
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
}

throw new Error("Unable to require module '" + moduleName + "' because its package '" + packageName + "' was not in the whitelist.");
};
}

Expand Down Expand Up @@ -405,6 +430,7 @@ class EmberApp {
hostWhitelist: pkg.fastboot.hostWhitelist,
config: config,
appName: appName,
schemaVersion: schemaVersion,
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/fastboot-schema-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
* should be added in fastboot lib) everytime fastboot addon schema version is bumped.
*/
const FastBootSchemaVersions = {
'latest': 3, // latest schema version supported by fastboot library
'latest': 4, // latest schema version supported by fastboot library
'base': 1, // first schema version supported by fastboot library
'manifestFileArrays': 2, // schema version when app and vendor in manifest supported an array of files
'configExtension': 3 // schema version when FastBoot.config can read arbitrary indexed config
'configExtension': 3, // schema version when FastBoot.config can read arbitrary indexed config
'strictWhitelist': 4 // schema version when fastbootDependencies and whitelist support only package names
};

module.exports = FastBootSchemaVersions;
12 changes: 12 additions & 0 deletions src/utils/get-package-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

function getPackageName(modulePath) {
let parts = modulePath.split('/');

if (modulePath[0] === '@') {
return parts[0] + '/' + parts[1];
}
return parts[0];
}

module.exports = getPackageName;
23 changes: 23 additions & 0 deletions test/fastboot-dependencies-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const expect = require('chai').expect;
const fs = require('fs');
const path = require('path');
const fixture = require('./helpers/fixture-path');
const FastBoot = require('./../src/index');

describe.only("FastBoot with dependencies", function() {
it("it works with dependencies", function() {
var fastboot = new FastBoot({
distPath: fixture('app-with-dependencies')
});

return fastboot.visit('/')
.then(r => r.html())
.then(html => {
expect(html).to.match(/https:\/\/emberjs.com/);
expect(html).to.match(/FOO/);
expect(html).to.match(/BAR/);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
define('app-with-dependencies/initializers/ajax', ['exports'], function (exports) {
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});


const { get } = Ember; /* globals najax */


var nodeAjax = function (options) {
let httpRegex = /^https?:\/\//;
let protocolRelativeRegex = /^\/\//;
let protocol = get(this, 'fastboot.request.protocol');

if (protocolRelativeRegex.test(options.url)) {
options.url = protocol + options.url;
} else if (!httpRegex.test(options.url)) {
try {
options.url = protocol + '//' + get(this, 'fastboot.request.host') + options.url;
} catch (fbError) {
throw new Error('You are using Ember Data with no host defined in your adapter. This will attempt to use the host of the FastBoot request, which is not configured for the current host of this request. Please set the hostWhitelist property for in your environment.js. FastBoot Error: ' + fbError.message);
}
}

if (najax) {
najax(options);
} else {
throw new Error('najax does not seem to be defined in your app. Did you override it via `addOrOverrideSandboxGlobals` in the fastboot server?');
}
};

exports.default = {
name: 'ajax-service',

initialize: function (application) {
application.register('ajax:node', nodeAjax, { instantiate: false });
application.inject('adapter', '_ajaxRequest', 'ajax:node');
application.inject('adapter', 'fastboot', 'service:fastboot');
}
};
});
define('app-with-dependencies/initializers/error-handler', ['exports'], function (exports) {
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'error-handler',

initialize: function (application) {
if (!Ember.onerror) {
// if no onerror handler is defined, define one for fastboot environments
Ember.onerror = function (err) {
let errorMessage = `There was an error running your app in fastboot. More info about the error: \n ${err.stack || err}`;
Ember.Logger.error(errorMessage);
};
}
}
};
});//# sourceMappingURL=app-with-dependencies-fastboot.map
Loading

0 comments on commit 4516747

Please sign in to comment.