Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts to help with examining middlewares
function examineMiddlewares(middlewares) {
"use strict";
let results = [];
for (let middleware of middlewares) {
let script = %FunctionGetScript(middleware);
let position = %FunctionGetScriptSourcePosition(middleware);
let line = script.source.slice(0, position).split("\n").length - 1;
let source = %FunctionGetSourceCode(middleware);
let functionScope = %GetFunctionScopeDetails(middleware, 0)[1];
// Put any middleware-specific examining code here
results.push(script.name + ":" + line + ": " + script.source.split("\n")[line].trim() + "\n" + source);
}
return results.join("\n");
}
--- a/lib/layer.js 2016-02-03 18:38:03.715769846 +1100
+++ b/lib/layer.js 2016-02-03 18:37:58.254769583 +1100
@@ -46,6 +46,27 @@
this.regexp = pathToRegExp(path, this.paramNames, this.opts);
debug('defined route %s %s', this.methods, this.opts.prefix + this.path);
+
+ if (!global.routes) {
+ global.routes = new Map();
+ setTimeout(function () {
+ console.warn(
+ Array.from(global.routes).sort((a, b) => a[0].localeCompare(b[0]))
+ .map((route) =>
+ `======> ${route[0]} <======\n` +
+ route[1].map((details) => [
+ "===> " + details.methods.join(" "),
+ examineMiddlewares(details.middleware),
+ details.stack.split("\n")[4]
+ ].join("\n")).join("\n")
+ ).join(`\n${"=".repeat(80)}\n`)
+ );
+ }, 5000);
+ }
+ var data = global.routes.get(this.opts.prefix + this.path);
+ if (!data)
+ global.routes.set(this.opts.prefix + this.path, data = []);
+ data.push({methods: this.methods, middleware: this.stack.slice(0), stack: new Error().stack});
};
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.