Skip to content

Instantly share code, notes, and snippets.

@kotarou3
Created February 3, 2016 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotarou3/d94385ff4029bff8607f to your computer and use it in GitHub Desktop.
Save kotarou3/d94385ff4029bff8607f to your computer and use it in GitHub Desktop.
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