| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | module.exports = parseArguments |
| 2 | |
| 3 | function isWritable(stream) { |
| 4 | return typeof stream.write === "function" && |
| 5 | typeof stream.end === "function" |
| 6 | } |
| 7 | |
| 8 | function parseArguments(req, res, opts, callback) { |
| 9 | // (req, cb) |
| 10 | if (typeof res === "function") { |
| 11 | callback = res |
| 12 | opts = {} |
| 13 | res = null |
| 14 | } |
| 15 | |
| 16 | // (req, res, cb) |
| 17 | if (typeof opts === "function") { |
| 18 | callback = opts |
| 19 | opts = {} |
| 20 | } |
| 21 | |
| 22 | // (req, opts, cb) |
| 23 | if (res && !isWritable(res)) { |
| 24 | opts = res |
| 25 | res = null |
| 26 | } |
| 27 | |
| 28 | // default (req, res, opts, cb) |
| 29 | return { req: req, res: res, opts: opts, callback: callback } |
| 30 | } |