mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 02:09:32 -04:00
26 lines
No EOL
889 B
JavaScript
26 lines
No EOL
889 B
JavaScript
module.exports = function amdWrapMiddleware(opts) {
|
|
opts = opts || {};
|
|
|
|
var root = opts.root || '/';
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
var pathPrefix = opts.pathPrefix || '/amd-wrap/';
|
|
|
|
return function (req, res, next) {
|
|
// only allow prefixed requests
|
|
if (req.url.substring(0, pathPrefix.length) !== pathPrefix) return next();
|
|
|
|
// strip the prefix and form the filename
|
|
var filename = path.join(root, req._parsedUrl.pathname.replace('/amd-wrap/', ''));
|
|
|
|
fs.readFile(filename, 'utf8', function (err, contents) {
|
|
// file does not exist
|
|
if (err) return next(err.code === 'ENOENT' ? void 0 : err);
|
|
|
|
// respond with the wrapped code
|
|
res.statusCode = 200;
|
|
res.setHeader('Content-Type', 'application/javascript');
|
|
res.end('define(function (require, exports, module) {\n' + contents + '\n});');
|
|
});
|
|
};
|
|
}; |