2020-08-19 06:36:59 +00:00
|
|
|
// Adapted File from 'run-middleware'
|
2021-08-04 16:17:23 +00:00
|
|
|
import * as _ from "lodash";
|
2020-08-19 06:36:59 +00:00
|
|
|
|
|
|
|
export function runMiddleware(app) {
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
req.runMiddleware = (path, options, callback) => {
|
|
|
|
if (_.isFunction(options)) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options.original_req = req;
|
|
|
|
options.original_res = res;
|
|
|
|
app.runMiddleware(path, options, callback);
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
if (app.runMiddleware) return; // Do not able to add us twice
|
|
|
|
|
|
|
|
app.runMiddleware = function (path, options, callback) {
|
|
|
|
if (callback) callback = _.once(callback);
|
|
|
|
if (typeof options == "function") {
|
|
|
|
callback = options;
|
|
|
|
options = null;
|
|
|
|
}
|
|
|
|
options = options || {};
|
|
|
|
options.url = path;
|
2021-08-04 16:17:23 +00:00
|
|
|
let new_req, new_res;
|
2020-08-19 06:36:59 +00:00
|
|
|
if (options.original_req) {
|
|
|
|
new_req = options.original_req;
|
2021-08-04 16:17:23 +00:00
|
|
|
for (const i in options) {
|
2020-08-19 06:36:59 +00:00
|
|
|
if (i == "original_req") continue;
|
|
|
|
new_req[i] = options[i];
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-12 16:07:05 +00:00
|
|
|
new_req = _createReq(path, options);
|
2020-08-19 06:36:59 +00:00
|
|
|
}
|
2020-11-12 16:07:05 +00:00
|
|
|
new_res = _createRes(callback);
|
2020-08-19 06:36:59 +00:00
|
|
|
app(new_req, new_res);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* end - APP.runMiddleware*/
|
|
|
|
};
|
|
|
|
|
2020-11-12 16:07:05 +00:00
|
|
|
function _createReq(path, options) {
|
2020-08-19 06:36:59 +00:00
|
|
|
if (!options) options = {};
|
2021-08-04 16:17:23 +00:00
|
|
|
const req = _.extend(
|
2020-08-19 06:36:59 +00:00
|
|
|
{
|
|
|
|
method: "GET",
|
|
|
|
host: "",
|
|
|
|
cookies: {},
|
|
|
|
query: {},
|
|
|
|
url: path,
|
|
|
|
headers: {},
|
|
|
|
},
|
|
|
|
options
|
|
|
|
);
|
|
|
|
req.method = req.method.toUpperCase();
|
|
|
|
// req.connection=_req.connection
|
|
|
|
return req;
|
|
|
|
}
|
2020-11-12 16:07:05 +00:00
|
|
|
function _createRes(callback) {
|
2021-08-04 16:17:23 +00:00
|
|
|
const res: any = {
|
2020-08-19 06:36:59 +00:00
|
|
|
_removedHeader: {},
|
|
|
|
};
|
|
|
|
// res=_.extend(res,require('express/lib/response'));
|
|
|
|
|
2021-08-04 16:17:23 +00:00
|
|
|
const headers = {};
|
|
|
|
let code = 200;
|
|
|
|
res.set = res.header = function (x, y) {
|
2020-08-19 06:36:59 +00:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
res.setHeader(x, y);
|
|
|
|
} else {
|
2021-08-04 16:17:23 +00:00
|
|
|
for (const key in x) {
|
2020-08-19 06:36:59 +00:00
|
|
|
res.setHeader(key, x[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2021-08-04 16:17:23 +00:00
|
|
|
};
|
2020-08-19 06:36:59 +00:00
|
|
|
res.setHeader = function (x, y) {
|
|
|
|
headers[x] = y;
|
|
|
|
headers[x.toLowerCase()] = y;
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
res.get = function (x) {
|
2021-08-04 16:17:23 +00:00
|
|
|
return headers[x];
|
|
|
|
};
|
2020-08-19 06:36:59 +00:00
|
|
|
res.redirect = function (_code, url) {
|
|
|
|
if (!_.isNumber(_code)) {
|
|
|
|
code = 301;
|
|
|
|
url = _code;
|
|
|
|
} else {
|
|
|
|
code = _code;
|
|
|
|
}
|
|
|
|
res.setHeader("Location", url);
|
|
|
|
res.end();
|
|
|
|
// callback(code,url)
|
|
|
|
};
|
|
|
|
res.status = function (number) {
|
|
|
|
code = number;
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
res.end = res.send = res.write = function (data) {
|
|
|
|
if (callback) callback(code, data, headers);
|
|
|
|
// else if (!options.quiet){
|
|
|
|
// _res.send(data)
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
return res;
|
|
|
|
}
|