Browse files

parse arguments for state machine

  • Loading branch information...
1 parent 68a5896 commit 0d9bc129103d961ca79c4737fb6af5ae01c78371 @Raynos committed Nov 27, 2013
Showing with 63 additions and 23 deletions.
  1. +8 −6 any.js
  2. +7 −5 form.js
  3. +7 −4 index.js
  4. +8 −5 json.js
  5. +30 −0 parse-arguments.js
  6. +3 −3 test/integration.js
View
14 any.js
@@ -1,7 +1,8 @@
var TypedError = require("error/typed")
-var jsonBody = require("./json")
-var formBody = require("./form")
+var parseArguments = require("./parse-arguments.js")
+var jsonBody = require("./json.js")
+var formBody = require("./form.js")
var jsonType = "application/json"
var formType = "application/x-www-form-urlencoded"
@@ -14,10 +15,11 @@ var INVALID_CONTENT_TYPE = TypedError({
module.exports = anyBody
function anyBody(req, res, opts, callback) {
- if (typeof opts === "function") {
- callback = opts
- opts = {}
- }
+ var args = parseArguments(req, res, opts, callback)
+ req = args.req
+ res = args.res
+ opts = args.opts
+ callback = args.callback
if (!callback) {
return anyBody.bind(null, req, res, opts)
View
12 form.js
@@ -1,14 +1,16 @@
var querystringParse = require("querystring").parse
-var body = require("./index")
+var body = require("./index.js")
+var parseArguments = require("./parse-arguments.js")
module.exports = formBody
function formBody(req, res, opts, callback) {
- if (typeof opts === "function") {
- callback = opts
- opts = {}
- }
+ var args = parseArguments(req, res, opts, callback)
+ req = args.req
+ res = args.res
+ opts = args.opts
+ callback = args.callback
if (!callback) {
return formBody.bind(null, req, res, opts)
View
11 index.js
@@ -1,14 +1,17 @@
var rawBody = require("raw-body")
+var parseArguments = require("./parse-arguments.js")
+
var ONE_MB = 1024 * 1024
module.exports = body
function body(req, res, opts, callback) {
- if (typeof opts === "function") {
- callback = opts
- opts = {}
- }
+ var args = parseArguments(req, res, opts, callback)
+ req = args.req
+ res = args.res
+ opts = args.opts
+ callback = args.callback
if (!callback) {
return body.bind(null, req, res, opts)
View
13 json.js
@@ -1,13 +1,16 @@
-var body = require("./index")
var jsonParse = require("safe-json-parse")
+var body = require("./index.js")
+var parseArguments = require("./parse-arguments.js")
+
module.exports = jsonBody
function jsonBody(req, res, opts, callback) {
- if (typeof opts === "function") {
- callback = opts
- opts = {}
- }
+ var args = parseArguments(req, res, opts, callback)
+ req = args.req
+ res = args.res
+ opts = args.opts
+ callback = args.callback
if (!callback) {
return jsonBody.bind(null, req, res, opts)
View
30 parse-arguments.js
@@ -0,0 +1,30 @@
+module.exports = parseArguments
+
+function isWritable(stream) {
+ return typeof stream.write === "function" &&
+ typeof stream.end === "function"
+}
+
+function parseArguments(req, res, opts, callback) {
+ // (req, cb)
+ if (typeof res === "function") {
+ callback = res
+ opts = {}
+ res = null
+ }
+
+ // (req, res, cb)
+ if (typeof opts === "function") {
+ callback = opts
+ opts = {}
+ }
+
+ // (req, opts, cb)
+ if (res && !isWritable(res)) {
+ opts = res
+ res = null
+ }
+
+ // default (req, res, opts, cb)
+ return { req: req, res: res, opts: opts, callback: callback }
+}
View
6 test/integration.js
@@ -20,13 +20,13 @@ function handleRequest(req, res) {
}
if (req.url === "/body") {
- body(req, res, send)
+ body(req, res, {}, send)
} else if (req.url === "/form") {
formBody(req, res, send)
} else if (req.url === "/json") {
- jsonBody(req, res, send)
+ jsonBody(req, {}, send)
} else if (req.url === "/any") {
- anyBody(req, res, send)
+ anyBody(req, send)
}
}

0 comments on commit 0d9bc12

Please sign in to comment.