Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / lib / types / json.js
1 /*!
2  * body-parser
3  * Copyright(c) 2014 Jonathan Ong
4  * Copyright(c) 2014 Douglas Christopher Wilson
5  * MIT Licensed
6  */
7
8 /**
9  * Module dependencies.
10  */
11
12 var bytes = require('bytes')
13 var read = require('../read')
14 var typer = require('media-typer')
15 var typeis = require('type-is')
16
17 /**
18  * Module exports.
19  */
20
21 module.exports = json
22
23 /**
24  * RegExp to match the first non-space in a string.
25  *
26  * Allowed whitespace is defined in RFC 7159:
27  *
28  *    ws = *(
29  *            %x20 /              ; Space
30  *            %x09 /              ; Horizontal tab
31  *            %x0A /              ; Line feed or New line
32  *            %x0D )              ; Carriage return
33  */
34
35 var firstcharRegExp = /^[\x20\x09\x0a\x0d]*(.)/
36
37 /**
38  * Create a middleware to parse JSON bodies.
39  *
40  * @param {object} [options]
41  * @return {function}
42  * @api public
43  */
44
45 function json(options) {
46   options = options || {}
47
48   var limit = typeof options.limit !== 'number'
49     ? bytes(options.limit || '100kb')
50     : options.limit
51   var inflate = options.inflate !== false
52   var reviver = options.reviver
53   var strict = options.strict !== false
54   var type = options.type || 'json'
55   var verify = options.verify || false
56
57   if (verify !== false && typeof verify !== 'function') {
58     throw new TypeError('option verify must be function')
59   }
60
61   function parse(body) {
62     if (body.length === 0) {
63       // special-case empty json body, as it's a common client-side mistake
64       // TODO: maybe make this configurable or part of "strict" option
65       return {}
66     }
67
68     if (strict) {
69       var first = firstchar(body)
70
71       if (first !== '{' && first !== '[') {
72         throw new Error('invalid json')
73       }
74     }
75
76     return JSON.parse(body, reviver)
77   }
78
79   return function jsonParser(req, res, next) {
80     if (req._body) return next()
81     req.body = req.body || {}
82
83     if (!typeis(req, type)) return next()
84
85     // RFC 7159 sec 8.1
86     var charset = (typer.parse(req).parameters.charset || 'utf-8').toLowerCase()
87     if (charset.substr(0, 4) !== 'utf-') {
88       var err = new Error('unsupported charset "' + charset.toUpperCase() + '"')
89       err.charset = charset
90       err.status = 415
91       next(err)
92       return
93     }
94
95     // read
96     read(req, res, next, parse, {
97       encoding: charset,
98       inflate: inflate,
99       limit: limit,
100       verify: verify
101     })
102   }
103 }
104
105 /**
106  * Get the first non-whitespace character in a string.
107  *
108  * @param {string} str
109  * @return {function}
110  * @api public
111  */
112
113
114 function firstchar(str) {
115   var match = firstcharRegExp.exec(str)
116   return match ? match[1] : ''
117 }