-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathfastboot-schema.js
More file actions
208 lines (182 loc) · 6.73 KB
/
fastboot-schema.js
File metadata and controls
208 lines (182 loc) · 6.73 KB
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
'use strict';
/*
This is the only module that should know about differences in the fastboot
schema version. All other consumers just ask this module for the config and it
conceals all differences.
*/
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const debug = require('debug')('fastboot:schema');
const resolve = require('resolve');
const getPackageName = require('./utils/get-package-name');
const htmlEntrypoint = require('./html-entrypoint');
/**
* Map of maintaining schema version history so that transformation of the manifest
* file can be performed correctly to maintain backward compatibility of older
* schema version.
*
* Note: `latest` schema version should always be updated (and transformation
* should be added in fastboot lib) everytime fastboot addon schema version is bumped.
*/
const FastBootSchemaVersions = {
latest: 5, // 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
strictWhitelist: 4, // schema version when fastbootDependencies and whitelist support only package names
htmlEntrypoint: 5, // schema version where we switch to loading the configuration directly from HTML
};
/**
* Given the path to a built Ember app, loads our complete configuration while
* completely hiding any differences in schema version.
*/
function loadConfig(distPath) {
let pkgPath = path.join(distPath, 'package.json');
let file;
try {
file = fs.readFileSync(pkgPath);
} catch (e) {
throw new Error(
`Couldn't find ${pkgPath}. You may need to update your version of ember-cli-fastboot.`
);
}
let schemaVersion;
let pkg;
try {
pkg = JSON.parse(file);
schemaVersion = pkg.fastboot.schemaVersion;
} catch (e) {
throw new Error(
`${pkgPath} was malformed or did not contain a fastboot config. Ensure that you have a compatible version of ember-cli-fastboot.`
);
}
const currentSchemaVersion = FastBootSchemaVersions.latest;
// set schema version to 1 if not defined
schemaVersion = schemaVersion || FastBootSchemaVersions.base;
debug(
'Current schemaVersion from `ember-cli-fastboot` is %s while latest schema version is %s',
schemaVersion,
currentSchemaVersion
);
if (schemaVersion > currentSchemaVersion) {
let errorMsg = chalk.bold.red(
'An incompatible version between `ember-cli-fastboot` and `fastboot` was found. Please update the version of fastboot library that is compatible with ember-cli-fastboot.'
);
throw new Error(errorMsg);
}
let appName, config, html, scripts;
if (schemaVersion < FastBootSchemaVersions.htmlEntrypoint) {
({ appName, config, html, scripts } = loadManifest(distPath, pkg.fastboot, schemaVersion));
} else {
appName = pkg.name;
({ config, html, scripts } = htmlEntrypoint(appName, distPath, pkg.fastboot.htmlEntrypoint));
}
let sandboxRequire = buildWhitelistedRequire(
pkg.fastboot.moduleWhitelist,
distPath,
schemaVersion < FastBootSchemaVersions.strictWhitelist
);
return {
scripts,
html,
hostWhitelist: pkg.fastboot.hostWhitelist,
renderMode: pkg.fastboot.renderMode,
config,
appName,
sandboxRequire,
};
}
/**
* Function to transform the manifest app and vendor files to an array.
*/
function transformManifestFiles(manifest) {
manifest.appFiles = [manifest.appFile];
manifest.vendorFiles = [manifest.vendorFile];
return manifest;
}
function loadManifest(distPath, fastbootConfig, schemaVersion) {
let manifest = fastbootConfig.manifest;
if (schemaVersion < FastBootSchemaVersions.manifestFileArrays) {
// transform app and vendor file to array of files
manifest = transformManifestFiles(manifest);
}
let config = fastbootConfig.config;
let appName = fastbootConfig.appName;
if (schemaVersion < FastBootSchemaVersions.configExtension) {
// read from the appConfig tree
if (fastbootConfig.appConfig) {
appName = fastbootConfig.appConfig.modulePrefix;
config = {};
config[appName] = fastbootConfig.appConfig;
}
}
let scripts = manifest.vendorFiles.concat(manifest.appFiles).map(function(file) {
return path.join(distPath, file);
});
let html = fs.readFileSync(path.join(distPath, manifest.htmlFile), 'utf8');
return { appName, config, scripts, html };
}
/**
* The Ember app runs inside a sandbox that doesn't have access to the normal
* Node.js environment, including the `require` function. Instead, we provide
* our own `require` method that only allows whitelisted packages to be
* requested.
*
* This method takes an array of whitelisted package names and the path to the
* built Ember app and constructs this "fake" `require` function that gets made
* available globally inside the sandbox.
*
* @param {string[]} whitelist array of whitelisted package names
* @param {string} distPath path to the built Ember app
* @param {boolean} isLegacyWhiteList flag to enable legacy behavior
*/
function buildWhitelistedRequire(whitelist, distPath, isLegacyWhitelist) {
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) {
let packageName = getPackageName(moduleName);
let isWhitelisted = whitelist.indexOf(packageName) > -1;
if (isWhitelisted) {
try {
let resolvedModulePath = resolve.sync(moduleName, { basedir: distPath });
return require(resolvedModulePath);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return require(moduleName);
} else {
throw error;
}
}
}
if (isLegacyWhitelist) {
if (whitelist.indexOf(moduleName) > -1) {
let nodeModulesPath = path.join(distPath, 'node_modules', moduleName);
if (fs.existsSync(nodeModulesPath)) {
return require(nodeModulesPath);
} else {
return require(moduleName);
}
} 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."
);
};
}
exports.loadConfig = loadConfig;