Skip to content

Commit

Permalink
Allow to require module path from whitelisted dependency
Browse files Browse the repository at this point in the history
Required module path can be one of

- `fs` (built-in Node.js module),
- `abortcontroller-polyfill` (external main module),
- `abortcontroller-polyfill/dist/cjs-ponyfill` (external submodule).

FastBoot's build system requires only dependency whitelisting,
thus looking only for the first part of module path.
  • Loading branch information
bobisjan committed Nov 20, 2018
1 parent e5c777d commit 1aea993
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"chalk": "^2.0.1",
"cookie": "^0.3.1",
"debug": "^3.0.0",
"exists-sync": "0.0.4",
"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
38 changes: 26 additions & 12 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const chalk = require('chalk');

const najax = require('najax');
const SimpleDOM = require('simple-dom');
const existsSync = require('exists-sync');
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 @@ -117,21 +118,34 @@ class EmberApp {
buildWhitelistedRequire(whitelist, distPath) {
whitelist.forEach(function(whitelistedModule) {
debug("module whitelisted; module=%s", whitelistedModule);
});

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

if (packageName !== whitelistedModule) {
console.warn("[DEPRECATION] Whitelisting module path '" + whitelistedModule + "' will not be supported after fastboot#2.0.0 and it should be removed from the whitelist.");

if (existsSync(nodeModulesPath)) {
return require(nodeModulesPath);
} else {
// If it's not on disk, assume it's a built-in node package
return require(moduleName);
if (whitelist.indexOf(packageName) === -1) {
console.error("Package '" + packageName + "' is required to be in the whitelist.");
}
} else {
throw new Error("Unable to require module '" + moduleName + "' because it was not in the whitelist.");
}
});

return function(modulePath) {
let packageName = getPackageName(modulePath);
let isWhitelisted = whitelist.indexOf(packageName) > -1;

if (isWhitelisted) {
let resolvedModulePath = resolve.sync(modulePath, { basedir: distPath });
return require(resolvedModulePath);
}

// Remove after 2.0.0
if (whitelist.indexOf(modulePath) > -1) {
return require(modulePath);
}

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

Expand Down
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;
1 change: 0 additions & 1 deletion test/fixtures/custom-sandbox/custom-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const vm = require('vm');
const fs = require('fs');
const existsSync = require('exists-sync');
const Sandbox = require('./../../../src/sandbox');

/**
Expand Down
12 changes: 12 additions & 0 deletions test/get-package-name-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const expect = require('chai').expect;
const getPackageName = require('../src/utils/get-package-name');

describe('utils/get-package-name', function() {
it('gets package name from module path', function() {
expect(getPackageName('foo')).to.equal('foo');
expect(getPackageName('foo/bar')).to.equal('foo');
expect(getPackageName('@foo/baz')).to.equal('@foo/baz');
expect(getPackageName('@foo/baz/bar')).to.equal('@foo/baz');
});
});

0 comments on commit 1aea993

Please sign in to comment.