kibana/tasks/utils/amd-wrapper.js
2014-03-05 16:56:42 -07:00

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});');
});
};
};