Skip to content

Commit 4cb8ca9

Browse files
author
dvinakur
committed
fix: remove qs.parse response transformation
1 parent 4603599 commit 4cb8ca9

9 files changed

Lines changed: 25 additions & 135 deletions

File tree

examples/http2/http2.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ srv.on(
2222
restify.plugins.auditLogger({
2323
event: 'after',
2424
body: true,
25-
log: pino(
26-
{ name: 'audit' },
27-
process.stdout
28-
)
25+
log: pino({ name: 'audit' }, process.stdout)
2926
})
3027
);
3128

lib/plugins/formBodyParser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var querystring = require('qs');
77

88
var bodyReader = require('./bodyReader');
99
var errors = require('restify-errors');
10-
var utils = require('../utils');
1110

1211
///--- Globals
1312

@@ -43,7 +42,7 @@ function urlEncodedBodyParser(options) {
4342
}
4443

4544
try {
46-
var params = utils.normalizeArrays(querystring.parse(req.body));
45+
var params = querystring.parse(req.body);
4746

4847
if (opts.mapParams === true) {
4948
var keys = Object.keys(params);

lib/plugins/jsonp.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
var qs = require('qs');
66

7-
var utils = require('../utils');
8-
97
///--- API
108

119
/**
@@ -30,7 +28,7 @@ function jsonp() {
3028

3129
// If the query plugin wasn't used, we need to hack it in now
3230
if (typeof q === 'string') {
33-
req.query = utils.normalizeArrays(qs.parse(q));
31+
req.query = qs.parse(q);
3432
}
3533

3634
if (req.query.callback || req.query.jsonp) {

lib/plugins/query.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
var qs = require('qs');
66
var assert = require('assert-plus');
77

8-
var normalizeArrays = require('../utils').normalizeArrays;
9-
108
/**
119
* Parses the HTTP query string (i.e., `/foo?id=bar&name=mark`).
1210
* If you use this, the parsed content will always be available in `req.query`,
@@ -55,20 +53,7 @@ function queryParser(options) {
5553
return next();
5654
}
5755

58-
var parsed = qs.parse(req.getQuery(), opts);
59-
60-
// Skip normalization when the caller explicitly opted out of array
61-
// parsing, set a custom arrayLimit (respect their cap), or requested
62-
// plainObjects (normalizeArrays would strip the null prototype).
63-
if (
64-
opts.parseArrays !== false &&
65-
opts.arrayLimit === undefined &&
66-
!opts.plainObjects
67-
) {
68-
parsed = normalizeArrays(parsed);
69-
}
70-
71-
req.query = parsed;
56+
req.query = qs.parse(req.getQuery(), opts);
7257

7358
if (opts.mapParams === true) {
7459
Object.keys(req.query).forEach(function forEach(k) {

lib/utils.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -171,38 +171,10 @@ function parseRequestUrl(rawUrl) {
171171

172172
///--- Exports
173173

174-
// qs >= 6.14.1 enforces arrayLimit on bracket notation too. When the limit is
175-
// exceeded it returns an object with numeric string keys instead of an array.
176-
// Convert those objects back to arrays so callers always get an array.
177-
function normalizeArrays(val) {
178-
if (!val || typeof val !== 'object' || Array.isArray(val)) {
179-
return val;
180-
}
181-
var keys = Object.keys(val);
182-
var isNumericSequence =
183-
keys.length > 0 &&
184-
keys.every((k, i) => {
185-
return k === String(i);
186-
});
187-
188-
if (isNumericSequence) {
189-
return keys.map(k => {
190-
return normalizeArrays(val[k]);
191-
});
192-
}
193-
194-
var result = {};
195-
keys.forEach(k => {
196-
result[k] = normalizeArrays(val[k]);
197-
});
198-
return result;
199-
}
200-
201174
module.exports = {
202175
shallowCopy: shallowCopy,
203176
mergeQs: mergeQs,
204177
parseUrlQuery: parseUrlQuery,
205178
formatUrl: formatUrl,
206-
parseRequestUrl: parseRequestUrl,
207-
normalizeArrays: normalizeArrays
179+
parseRequestUrl: parseRequestUrl
208180
};

test/plugins/formBodyParser.test.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,14 @@ describe('form body parser', function() {
243243
client.end();
244244
});
245245

246-
it('should parse indexed array with > 20 items', function(done) {
246+
it('should return object with 25 keys', function(done) {
247247
SERVER.use(restify.plugins.bodyParser());
248248

249249
SERVER.post('/bodyurl2/:id', function(req, res, next) {
250-
assert.isArray(req.body.items, 'items should be an array');
250+
assert.isNotArray(req.body.items);
251+
assert.isObject(req.body.items);
251252
assert.equal(
252-
req.body.items.length,
253+
Object.keys(req.body.items).length,
253254
25,
254255
'all 25 items should be present'
255256
);
@@ -278,15 +279,16 @@ describe('form body parser', function() {
278279
client.end();
279280
});
280281

281-
it('should parse array with more than 20 items', function(done) {
282+
it('should return object with 23 keys', function(done) {
282283
SERVER.use(restify.plugins.bodyParser());
283284

284285
SERVER.post('/bodyurl2/:id', function(req, res, next) {
285-
assert.isArray(req.body.items, 'items should be an array');
286+
assert.isNotArray(req.body.items);
287+
assert.isObject(req.body.items);
286288
assert.equal(
287-
req.body.items.length,
288-
25,
289-
'all 25 items should be present'
289+
Object.keys(req.body.items).length,
290+
23,
291+
'all 23 items should be present'
290292
);
291293
res.send();
292294
next();
@@ -302,7 +304,7 @@ describe('form body parser', function() {
302304
'Content-Type': 'application/x-www-form-urlencoded'
303305
}
304306
};
305-
var items = Array.from({ length: 25 }, function(_, i) {
307+
var items = Array.from({ length: 23 }, function(_, i) {
306308
return 'items[]=value' + i;
307309
});
308310
var client = http.request(opts, function(res) {

test/plugins/jsonp.test.js

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,6 @@ describe('jsonp plugin', function() {
3535
SERVER.close(done);
3636
});
3737

38-
it('should return array for bracket notation', function(done) {
39-
SERVER.use(restify.plugins.jsonp());
40-
41-
SERVER.get('/jsonp/array', function(req, res, next) {
42-
assert.isArray(req.query.items);
43-
assert.equal(req.query.items.length, 25);
44-
res.send();
45-
next();
46-
});
47-
48-
var items = [];
49-
for (var i = 0; i < 25; i++) {
50-
items.push('items[]=' + i);
51-
}
52-
CLIENT.get('/jsonp/array?' + items.join('&'), function(err, _, res) {
53-
assert.ifError(err);
54-
assert.equal(res.statusCode, 200);
55-
done();
56-
});
57-
});
58-
59-
it('should return array for indexed notation', function(done) {
60-
SERVER.use(restify.plugins.jsonp());
61-
62-
SERVER.get('/jsonp/array', function(req, res, next) {
63-
assert.isArray(req.query.items);
64-
assert.equal(req.query.items.length, 25);
65-
res.send();
66-
next();
67-
});
68-
69-
var items = [];
70-
for (var i = 0; i < 25; i++) {
71-
items.push('items[' + i + ']=' + i);
72-
}
73-
CLIENT.get('/jsonp/array?' + items.join('&'), function(err, _, res) {
74-
assert.ifError(err);
75-
assert.equal(res.statusCode, 200);
76-
done();
77-
});
78-
});
79-
8038
it('should set content-type', function(done) {
8139
SERVER.use(restify.plugins.jsonp());
8240

test/plugins/query.test.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,13 @@ describe('query parser', function() {
300300
});
301301
});
302302

303-
it('should return array for [] with < 20 items', function(done) {
303+
it('should return object with 25 keys', function(done) {
304304
SERVER.use(restify.plugins.queryParser());
305305

306306
SERVER.get('/query/indexed-over', function(req, res, next) {
307-
assert.isArray(req.query.items);
308-
assert.equal(req.query.items.length, 25);
307+
assert.isNotArray(req.query.items);
308+
assert.isObject(req.query.items);
309+
assert.equal(Object.keys(req.query.items).length, 25);
309310
res.send();
310311
next();
311312
});
@@ -325,18 +326,19 @@ describe('query parser', function() {
325326
});
326327
});
327328

328-
it('should return array for [] with > 20 items', function(done) {
329+
it('should return object with 23 keys', function(done) {
329330
SERVER.use(restify.plugins.queryParser());
330331

331332
SERVER.get('/query/array', function(req, res, next) {
332-
assert.isArray(req.query.items);
333-
assert.equal(req.query.items.length, 25);
333+
assert.isNotArray(req.query.items);
334+
assert.isObject(req.query.items);
335+
assert.equal(Object.keys(req.query.items).length, 23);
334336
res.send();
335337
next();
336338
});
337339

338340
var items = [];
339-
for (var i = 0; i < 25; i++) {
341+
for (var i = 0; i < 23; i++) {
340342
items.push('items[]=' + i);
341343
}
342344
CLIENT.get('/query/array?' + items.join('&'), function(err, _, res) {

test/utils.test.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var mergeQs = require('../lib/utils').mergeQs;
55
var parseRequestUrl = require('../lib/utils').parseRequestUrl;
66
var parseUrlQuery = require('../lib/utils').parseUrlQuery;
77
var formatUrl = require('../lib/utils').formatUrl;
8-
var normalizeArrays = require('../lib/utils').normalizeArrays;
98

109
if (require.cache[__dirname + '/lib/helper.js']) {
1110
delete require.cache[__dirname + '/lib/helper.js'];
@@ -168,28 +167,6 @@ test(module, 'formatUrl: array query values become repeated params', function(
168167
t.done();
169168
});
170169

171-
// normalizeArrays
172-
173-
test(module, 'normalizeArrays', function(t) {
174-
var obj25 = {};
175-
for (var i = 0; i < 25; i++) {
176-
obj25[String(i)] = 'v' + i;
177-
}
178-
var result1 = normalizeArrays({ items: obj25 });
179-
t.ok(Array.isArray(result1.items), 'items should be an array');
180-
t.equal(result1.items.length, 25, 'all 25 items present');
181-
182-
var result2 = normalizeArrays(obj25);
183-
t.ok(Array.isArray(result2), 'result should be an array');
184-
t.equal(result2.items.length, 25, 'all 25 items present');
185-
186-
// passthrough: real arrays and plain objects are unchanged
187-
t.deepEqual(normalizeArrays({ a: [1, 2, 3] }), { a: [1, 2, 3] });
188-
t.deepEqual(normalizeArrays({ name: 'alex' }), { name: 'alex' });
189-
190-
t.done();
191-
});
192-
193170
// mergeQs
194171

195172
test(module, 'merge qs', function(t) {

0 commit comments

Comments
 (0)