Last active
February 21, 2020 10:15
-
-
Save Cologler/ab053c813e175d11be131a852d25185f to your computer and use it in GitHub Desktop.
a script to debug rsshub route without run koa http server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* to use this script, | |
* 1. save debug-route.js to rsshub root dir. | |
* 2. try: node debug-route.js "/chrome/webstore/extensions/SOME_ID" | |
*/ | |
const moduleAlias = require('module-alias'); | |
moduleAlias.addAlias('@', () => __dirname + '/lib'); | |
const util = require('util'); | |
const router = require("./lib/router"); | |
async function getState(path) { | |
const ctx = { | |
method: 'GET', | |
path, | |
state: {}, | |
cache: { | |
get: () => {}, | |
set: () => {}, | |
tryGet: (key, getValueFunc) => getValueFunc() | |
} | |
}; | |
const match = router.match(path, ctx.method); | |
if (match.route) { | |
await router.routes()(ctx, () => {}); | |
return ctx.state; | |
} else { | |
let msg = `"${path}" does not match any route.`; | |
for (const layer of router.stack) { | |
if (layer.path.startsWith(path)) { | |
msg += '\n' + ` - did you mean: "${layer.path}" ?`; | |
} | |
} | |
console.error(msg); | |
} | |
} | |
(async function main() { | |
const argv = process.argv.slice(2); | |
if (argv.length > 1) { | |
return; | |
} | |
const path = argv[0]; | |
let state = null; | |
try { | |
state = await getState(path); | |
} catch (error) { | |
return console.error(error); | |
} | |
if (state) { | |
console.log(util.inspect(state, { | |
depth: null, | |
colors: true, | |
maxArrayLength: 3 | |
})); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment