parse-arguments.js 645 B

123456789101112131415161718192021222324252627282930
  1. module.exports = parseArguments
  2. function isWritable(stream) {
  3. return typeof stream.write === "function" &&
  4. typeof stream.end === "function"
  5. }
  6. function parseArguments(req, res, opts, callback) {
  7. // (req, cb)
  8. if (typeof res === "function") {
  9. callback = res
  10. opts = {}
  11. res = null
  12. }
  13. // (req, res, cb)
  14. if (typeof opts === "function") {
  15. callback = opts
  16. opts = {}
  17. }
  18. // (req, opts, cb)
  19. if (res && !isWritable(res)) {
  20. opts = res
  21. res = null
  22. }
  23. // default (req, res, opts, cb)
  24. return { req: req, res: res, opts: opts, callback: callback }
  25. }