-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (158 loc) · 3.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*!
* bory
* Copyright(c) 2017 Daniel Röhers Moura
* MIT Licensed
*/
'use strict';
/**
* Module dependencies.
* @private
*/
const deprecate = require('depd')('bory');
const json = require('./lib/types/json');
const raw = require('./lib/types/raw');
const text = require('./lib/types/text');
const urlencoded = require('./lib/types/urlencoded');
const nested = require('./lib/types/nested');
const queryParser = require('./lib/types/queryParser');
/**
* Cache of loaded parsers.
* @private
*/
const parsers = Object.create(null);
/**
* @typedef Parsers
* @type {function}
* @property {function} json
* @property {function} raw
* @property {function} text
* @property {function} urlencoded
* @property {function} nested
* @property {function} queryParser
*/
/**
* Module exports.
* @type {Parsers}
*/
exports = module.exports = deprecate.function(bory, 'bory: use individual json/urlencoded middlewares');
/**
* JSON parser.
* @public
*/
Object.defineProperty(exports, 'json', {
configurable: true,
enumerable: true,
get: createParserGetter('json'),
});
/**
* Raw parser.
* @public
*/
Object.defineProperty(exports, 'raw', {
configurable: true,
enumerable: true,
get: createParserGetter('raw'),
});
/**
* Text parser.
* @public
*/
Object.defineProperty(exports, 'text', {
configurable: true,
enumerable: true,
get: createParserGetter('text'),
});
/**
* URL-encoded parser.
* @public
*/
Object.defineProperty(exports, 'urlencoded', {
configurable: true,
enumerable: true,
get: createParserGetter('urlencoded'),
});
/**
* Nested parser.
* @public
*/
Object.defineProperty(exports, 'nested', {
configurable: true,
enumerable: true,
get: createParserGetter('nested'),
});
/**
* Query parser.
* @public
*/
Object.defineProperty(exports, 'queryParser', {
configurable: true,
enumerable: true,
get: createParserGetter('queryParser'),
});
/**
* Create a middleware to parse json and urlencoded bodies.
*
* @param {object} [options]
* @return {function}
* @deprecated
* @public
*/
function bory(options) {
const opts = {};
// exclude type option
if (options) {
for (const prop in options) {
if (prop !== 'type') {
opts[prop] = options[prop];
}
}
}
const _urlencoded = exports.urlencoded(opts);
const _json = exports.json(opts);
return (req, res, next) => {
_json(req, res, (err) => {
if (err) return next(err);
_urlencoded(req, res, next);
});
};
}
/**
* Create a getter for loading a parser.
* @private
*/
function createParserGetter(name) {
return () => loadParser(name);
}
/**
* Load a parser module.
* @private
*/
function loadParser(parserName) {
let parser = parsers[parserName];
if (parser !== undefined) {
return parser;
}
// this uses a switch for static require analysis
switch (parserName) {
case 'json':
parser = json;
break;
case 'raw':
parser = raw;
break;
case 'text':
parser = text;
break;
case 'urlencoded':
parser = urlencoded;
break;
case 'nested':
parser = nested;
break;
case 'queryParser':
parser = queryParser;
break;
}
// store to prevent invoking require()
return (parsers[parserName] = parser);
}