|
| 1 | +var Fiber = require("fibers"); |
| 2 | +var fs = require("fs"); |
| 3 | +var path = require("path"); |
| 4 | +var Future = require("fibers/future"); |
| 5 | +var _ = require('underscore'); |
| 6 | +var sourcemap_support = require('source-map-support'); |
| 7 | + |
| 8 | +var bootUtils = require('./boot-utils.js'); |
| 9 | +var files = require('./mini-files.js'); |
| 10 | + |
| 11 | +// This code is duplicated in tools/main.js. |
| 12 | +var MIN_NODE_VERSION = 'v0.10.36'; |
| 13 | + |
| 14 | +if (require('semver').lt(process.version, MIN_NODE_VERSION)) { |
| 15 | + process.stderr.write( |
| 16 | + 'Meteor requires Node ' + MIN_NODE_VERSION + ' or later.\n'); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +// read our control files |
| 21 | +var serverJsonPath = path.resolve(process.argv[2]); |
| 22 | +var serverDir = path.dirname(serverJsonPath); |
| 23 | +var serverJson = JSON.parse(fs.readFileSync(serverJsonPath, 'utf8')); |
| 24 | +var configJson = |
| 25 | + JSON.parse(fs.readFileSync(path.resolve(serverDir, 'config.json'), 'utf8')); |
| 26 | + |
| 27 | +// Set up environment |
| 28 | +__meteor_bootstrap__ = { |
| 29 | + startupHooks: [], |
| 30 | + serverDir: serverDir, |
| 31 | + configJson: configJson }; |
| 32 | +__meteor_runtime_config__ = { meteorRelease: configJson.meteorRelease }; |
| 33 | + |
| 34 | + |
| 35 | +// connect (and some other NPM modules) use $NODE_ENV to make some decisions; |
| 36 | +// eg, if $NODE_ENV is not production, they send stack traces on error. connect |
| 37 | +// considers 'development' to be the default mode, but that's less safe than |
| 38 | +// assuming 'production' to be the default. If you really want development mode, |
| 39 | +// set it in your wrapper script (eg, run-app.js). |
| 40 | +if (!process.env.NODE_ENV) |
| 41 | + process.env.NODE_ENV = 'production'; |
| 42 | + |
| 43 | +// Map from load path to its source map. |
| 44 | +var parsedSourceMaps = {}; |
| 45 | + |
| 46 | +// Read all the source maps into memory once. |
| 47 | +_.each(serverJson.load, function (fileInfo) { |
| 48 | + if (fileInfo.sourceMap) { |
| 49 | + var rawSourceMap = fs.readFileSync( |
| 50 | + path.resolve(serverDir, fileInfo.sourceMap), 'utf8'); |
| 51 | + // Parse the source map only once, not each time it's needed. Also remove |
| 52 | + // the anti-XSSI header if it's there. |
| 53 | + var parsedSourceMap = JSON.parse(rawSourceMap.replace(/^\)\]\}'/, '')); |
| 54 | + // source-map-support doesn't ever look at the sourcesContent field, so |
| 55 | + // there's no point in keeping it in memory. |
| 56 | + delete parsedSourceMap.sourcesContent; |
| 57 | + var url; |
| 58 | + if (fileInfo.sourceMapRoot) { |
| 59 | + // Add the specified root to any root that may be in the file. |
| 60 | + parsedSourceMap.sourceRoot = path.join( |
| 61 | + fileInfo.sourceMapRoot, parsedSourceMap.sourceRoot || ''); |
| 62 | + } |
| 63 | + parsedSourceMaps[path.resolve(__dirname, fileInfo.path)] = parsedSourceMap; |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +var retrieveSourceMap = function (pathForSourceMap) { |
| 68 | + if (_.has(parsedSourceMaps, pathForSourceMap)) |
| 69 | + return { map: parsedSourceMaps[pathForSourceMap] }; |
| 70 | + return null; |
| 71 | +}; |
| 72 | + |
| 73 | +sourcemap_support.install({ |
| 74 | + // Use the source maps specified in program.json instead of parsing source |
| 75 | + // code for them. |
| 76 | + retrieveSourceMap: retrieveSourceMap, |
| 77 | + // For now, don't fix the source line in uncaught exceptions, because we |
| 78 | + // haven't fixed handleUncaughtExceptions in source-map-support to properly |
| 79 | + // locate the source files. |
| 80 | + handleUncaughtExceptions: false |
| 81 | +}); |
| 82 | + |
| 83 | +// Only enabled by default in development. |
| 84 | +if (process.env.METEOR_SHELL_DIR) { |
| 85 | + require('./shell-server.js').listen(process.env.METEOR_SHELL_DIR); |
| 86 | +} |
| 87 | + |
| 88 | +// As a replacement to the old keepalives mechanism, check for a running |
| 89 | +// parent every few seconds. Exit if the parent is not running. |
| 90 | +// |
| 91 | +// Two caveats to this strategy: |
| 92 | +// * Doesn't catch the case where the parent is CPU-hogging (but maybe we |
| 93 | +// don't want to catch that case anyway, since the bundler not yielding |
| 94 | +// is what caused #2536). |
| 95 | +// * Could be fooled by pid re-use, i.e. if another process comes up and |
| 96 | +// takes the parent process's place before the child process dies. |
| 97 | +var startCheckForLiveParent = function (parentPid) { |
| 98 | + if (parentPid) { |
| 99 | + if (! bootUtils.validPid(parentPid)) { |
| 100 | + console.error("METEOR_PARENT_PID must be a valid process ID."); |
| 101 | + process.exit(1); |
| 102 | + } |
| 103 | + |
| 104 | + setInterval(function () { |
| 105 | + try { |
| 106 | + process.kill(parentPid, 0); |
| 107 | + } catch (err) { |
| 108 | + console.error("Parent process is dead! Exiting."); |
| 109 | + process.exit(1); |
| 110 | + } |
| 111 | + }, 3000); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | + |
| 116 | +Fiber(function () { |
| 117 | + _.each(serverJson.load, function (fileInfo) { |
| 118 | + var code = fs.readFileSync(path.resolve(serverDir, fileInfo.path)); |
| 119 | + |
| 120 | + var Npm = { |
| 121 | + /** |
| 122 | + * @summary Require a package that was specified using |
| 123 | + * `Npm.depends()`. |
| 124 | + * @param {String} name The name of the package to require. |
| 125 | + * @locus Server |
| 126 | + * @memberOf Npm |
| 127 | + */ |
| 128 | + require: function (name) { |
| 129 | + if (! fileInfo.node_modules) { |
| 130 | + return require(name); |
| 131 | + } |
| 132 | + |
| 133 | + var nodeModuleBase = path.resolve(serverDir, |
| 134 | + files.convertToOSPath(fileInfo.node_modules)); |
| 135 | + var nodeModuleDir = path.resolve(nodeModuleBase, name); |
| 136 | + |
| 137 | + // If the user does `Npm.require('foo/bar')`, then we should resolve to |
| 138 | + // the package's node modules if `foo` was one of the modules we |
| 139 | + // installed. (`foo/bar` might be implemented as `foo/bar.js` so we |
| 140 | + // can't just naively see if all of nodeModuleDir exists. |
| 141 | + if (fs.existsSync(path.resolve(nodeModuleBase, name.split("/")[0]))) { |
| 142 | + return require(nodeModuleDir); |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + return require(name); |
| 147 | + } catch (e) { |
| 148 | + // Try to guess the package name so we can print a nice |
| 149 | + // error message |
| 150 | + // fileInfo.path is a standard path, use files.pathSep |
| 151 | + var filePathParts = fileInfo.path.split(files.pathSep); |
| 152 | + var packageName = filePathParts[1].replace(/\.js$/, ''); |
| 153 | + |
| 154 | + // XXX better message |
| 155 | + throw new Error( |
| 156 | + "Can't find npm module '" + name + |
| 157 | + "'. Did you forget to call 'Npm.depends' in package.js " + |
| 158 | + "within the '" + packageName + "' package?"); |
| 159 | + } |
| 160 | + } |
| 161 | + }; |
| 162 | + var getAsset = function (assetPath, encoding, callback) { |
| 163 | + var fut; |
| 164 | + if (! callback) { |
| 165 | + fut = new Future(); |
| 166 | + callback = fut.resolver(); |
| 167 | + } |
| 168 | + // This assumes that we've already loaded the meteor package, so meteor |
| 169 | + // itself (and weird special cases like js-analyze) can't call |
| 170 | + // Assets.get*. (We could change this function so that it doesn't call |
| 171 | + // bindEnvironment if you don't pass a callback if we need to.) |
| 172 | + var _callback = Package.meteor.Meteor.bindEnvironment(function (err, result) { |
| 173 | + if (result && ! encoding) |
| 174 | + // Sadly, this copies in Node 0.10. |
| 175 | + result = new Uint8Array(result); |
| 176 | + callback(err, result); |
| 177 | + }, function (e) { |
| 178 | + console.log("Exception in callback of getAsset", e.stack); |
| 179 | + }); |
| 180 | + |
| 181 | + // Convert a DOS-style path to Unix-style in case the application code was |
| 182 | + // written on Windows. |
| 183 | + assetPath = files.convertToStandardPath(assetPath); |
| 184 | + |
| 185 | + if (!fileInfo.assets || !_.has(fileInfo.assets, assetPath)) { |
| 186 | + _callback(new Error("Unknown asset: " + assetPath)); |
| 187 | + } else { |
| 188 | + var filePath = path.join(serverDir, fileInfo.assets[assetPath]); |
| 189 | + fs.readFile(files.convertToOSPath(filePath), encoding, _callback); |
| 190 | + } |
| 191 | + if (fut) |
| 192 | + return fut.wait(); |
| 193 | + }; |
| 194 | + |
| 195 | + var Assets = { |
| 196 | + getText: function (assetPath, callback) { |
| 197 | + return getAsset(assetPath, "utf8", callback); |
| 198 | + }, |
| 199 | + getBinary: function (assetPath, callback) { |
| 200 | + return getAsset(assetPath, undefined, callback); |
| 201 | + } |
| 202 | + }; |
| 203 | + |
| 204 | + // \n is necessary in case final line is a //-comment |
| 205 | + var wrapped = "(function(Npm, Assets){" + code + "\n})"; |
| 206 | + |
| 207 | + // It is safer to use the absolute path when source map is present as |
| 208 | + // different tooling, such as node-inspector, can get confused on relative |
| 209 | + // urls. |
| 210 | + |
| 211 | + // fileInfo.path is a standard path, convert it to OS path to join with |
| 212 | + // __dirname |
| 213 | + var fileInfoOSPath = files.convertToOSPath(fileInfo.path); |
| 214 | + var absoluteFilePath = path.resolve(__dirname, fileInfoOSPath); |
| 215 | + |
| 216 | + var scriptPath = |
| 217 | + parsedSourceMaps[absoluteFilePath] ? absoluteFilePath : fileInfoOSPath; |
| 218 | + // The final 'true' is an undocumented argument to runIn[Foo]Context that |
| 219 | + // causes it to print out a descriptive error message on parse error. It's |
| 220 | + // what require() uses to generate its errors. |
| 221 | + var func = require('vm').runInThisContext(wrapped, scriptPath, true); |
| 222 | + func.call(global, Npm, Assets); // Coffeescript |
| 223 | + }); |
| 224 | + |
| 225 | + // run the user startup hooks. other calls to startup() during this can still |
| 226 | + // add hooks to the end. |
| 227 | + while (__meteor_bootstrap__.startupHooks.length) { |
| 228 | + var hook = __meteor_bootstrap__.startupHooks.shift(); |
| 229 | + hook(); |
| 230 | + } |
| 231 | + // Setting this to null tells Meteor.startup to call hooks immediately. |
| 232 | + __meteor_bootstrap__.startupHooks = null; |
| 233 | + |
| 234 | + // find and run main() |
| 235 | + // XXX hack. we should know the package that contains main. |
| 236 | + var mains = []; |
| 237 | + var globalMain; |
| 238 | + if ('main' in global) { |
| 239 | + mains.push(main); |
| 240 | + globalMain = main; |
| 241 | + } |
| 242 | + typeof Package !== 'undefined' && _.each(Package, function (p, n) { |
| 243 | + if ('main' in p && p.main !== globalMain) { |
| 244 | + mains.push(p.main); |
| 245 | + } |
| 246 | + }); |
| 247 | + if (! mains.length) { |
| 248 | + process.stderr.write("Program has no main() function.\n"); |
| 249 | + process.exit(1); |
| 250 | + } |
| 251 | + if (mains.length > 1) { |
| 252 | + process.stderr.write("Program has more than one main() function?\n"); |
| 253 | + process.exit(1); |
| 254 | + } |
| 255 | + var exitCode = mains[0].call({}, process.argv.slice(3)); |
| 256 | + // XXX hack, needs a better way to keep alive |
| 257 | + if (exitCode !== 'DAEMON') |
| 258 | + process.exit(exitCode); |
| 259 | + |
| 260 | + if (process.env.METEOR_PARENT_PID) { |
| 261 | + startCheckForLiveParent(process.env.METEOR_PARENT_PID); |
| 262 | + } |
| 263 | +}).run(); |
0 commit comments