UNPKG

2.01 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9var StaticHandler = require("./static").StaticHandler;
10var LangUtils = require('@themost/common/utils').LangUtils;
11var fs = require("fs");
12var url = require("url");
13var path = require("path");
14
15/**
16 * @class
17 * @constructor
18 * @inherits StaticHandler
19 */
20function NodeModulesHandler() {
21 NodeModulesHandler.super_.bind(this)();
22}
23
24LangUtils.inherits(NodeModulesHandler, StaticHandler);
25
26NodeModulesHandler.prototype.mapRequest = function(context, callback) {
27 callback = callback || function() {};
28 try {
29 //get file path
30 var uri = url.parse(context.request.url);
31 if (!/^\/node_modules\//i.test(uri.pathname)) {
32 return callback();
33 }
34 var p = path.join(process.cwd(), uri.pathname);
35 fs.stat(p, function(err, stats) {
36 if (err) {
37 //requested file does not exists
38 if (err.code === "ENOENT") { return callback(); }
39 return callback(err);
40 }
41 else {
42 //if file exists
43 if (stats && stats.isFile()) {
44 //set request current handler
45 context.request.currentHandler = new NodeModulesHandler();
46 //set current execution path
47 context.request.currentExecutionPath = p;
48 //set file stats
49 context.request.currentExecutionFileStats = stats;
50 }
51 callback(null);
52 }
53 });
54 } catch (e) {
55 callback(e);
56 }
57};
58
59if (typeof exports !== 'undefined') {
60 module.exports = {
61 NodeModulesHandler:NodeModulesHandler,
62 createInstance : function() {
63 return new NodeModulesHandler();
64 }
65 };
66}
\No newline at end of file