larvitreqparser 0.4.10 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "root": true,
3
3
  "parserOptions": {
4
- "ecmaVersion": 2017
4
+ "ecmaVersion": 2020,
5
5
  },
6
6
  "env": {
7
7
  "es6": true,
package/.travis.yml CHANGED
@@ -1,13 +1,11 @@
1
1
  language: node_js
2
2
 
3
3
  node_js:
4
- - 10
5
- - 12
6
- - 13
4
+ - 14
7
5
 
8
6
  script: npm test
9
7
 
10
- after_script: "nyc report --reporter=text-lcov | coveralls"
8
+ after_script: "nyc report --reporter=text-lcov"
11
9
 
12
10
  notifications:
13
11
  email:
package/index.js CHANGED
@@ -3,9 +3,9 @@
3
3
  const topLogPrefix = 'larvitreqparser: ./index.js: ';
4
4
  const EventEmitter = require('events').EventEmitter;
5
5
  const Readable = require('stream').Readable;
6
- const uuidv4 = require('uuid/v4');
7
- const Busboy = require('busboy');
8
- const LUtils = require('larvitutils');
6
+ const { Log } = require('larvitutils');
7
+ const uuidLib = require('uuid');
8
+ const busboy = require('busboy');
9
9
  const async = require('async');
10
10
  const url = require('url');
11
11
  const qs = require('qs');
@@ -28,7 +28,7 @@ function ReqParser(options) {
28
28
  }
29
29
 
30
30
  if (!that.options.log) {
31
- that.options.log = new LUtils.Log();
31
+ that.options.log = new Log();
32
32
  }
33
33
 
34
34
  if (!that.options.fs) {
@@ -96,7 +96,7 @@ ReqParser.prototype.parse = function parse(req, res, cb) {
96
96
  const that = this;
97
97
 
98
98
  if (!req.uuid) {
99
- req.uuid = uuidv4();
99
+ req.uuid = uuidLib.v4();
100
100
  }
101
101
  req.reqParser = {};
102
102
 
@@ -106,9 +106,11 @@ ReqParser.prototype.parse = function parse(req, res, cb) {
106
106
  });
107
107
 
108
108
  // Parse URL
109
- tasks.push(function (cb) {
110
- that.parseUrl(req, res, cb);
111
- });
109
+ if (!req.urlParsed) {
110
+ tasks.push(function (cb) {
111
+ that.parseUrl(req, res, cb);
112
+ });
113
+ }
112
114
 
113
115
  if (req.headers) {
114
116
  for (const headerName of Object.keys(req.headers)) {
@@ -136,7 +138,7 @@ ReqParser.prototype.parse = function parse(req, res, cb) {
136
138
  };
137
139
 
138
140
  ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
139
- const busboy = new Busboy(this.busboyOptions);
141
+ const bb = busboy(this.busboyOptions);
140
142
  const logPrefix = topLogPrefix + 'parseFormMultipart() - reqUuid: ' + req.uuid + ' - ';
141
143
  const that = this;
142
144
 
@@ -145,12 +147,12 @@ ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
145
147
  req.formFields = {};
146
148
  req.formFiles = {};
147
149
 
148
- busboy.on('file', function (fieldName, file, filename, encoding, mimetype) {
150
+ bb.on('file', function (fieldName, file, info) {
149
151
  const formFile = {};
150
152
 
151
- formFile.filename = filename;
152
- formFile.encoding = encoding;
153
- formFile.mimetype = mimetype;
153
+ formFile.filename = info.filename;
154
+ formFile.encoding = info.encoding;
155
+ formFile.mimetype = info.mimeType;
154
156
 
155
157
  if (that.options.storage === 'memory') {
156
158
  formFile.buffer = [];
@@ -159,7 +161,7 @@ ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
159
161
  formFile.buffer.push(data);
160
162
  });
161
163
  } else {
162
- formFile.path = that.storage + '/' + uuidv4();
164
+ formFile.path = that.storage + '/' + uuidLib.v4();
163
165
  formFile.writeStream = that.fs.createWriteStream(formFile.path);
164
166
  file.pipe(formFile.writeStream);
165
167
  formFile.writeStream.on('close', function () {
@@ -184,18 +186,18 @@ ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
184
186
  });
185
187
  });
186
188
 
187
- busboy.on('field', function (fieldName, fieldVal) {
189
+ bb.on('field', function (fieldName, fieldVal) {
188
190
  fieldsStr += encodeURIComponent(fieldName) + '=' + encodeURIComponent(fieldVal) + '&';
189
191
  });
190
192
 
191
- busboy.on('finish', function () {
193
+ bb.on('finish', function () {
192
194
  const tasks = [];
193
195
 
194
196
  if (fieldsStr.endsWith('&')) {
195
197
  fieldsStr = fieldsStr.substr(0, fieldsStr.length - 1);
196
198
  }
197
199
 
198
- req.formFields = qs.parse(fieldsStr);
200
+ req.formFields = qs.parse(fieldsStr, that.qsParseOptions || {});
199
201
 
200
202
  if (that.options.storage === 'memory') {
201
203
  return cb();
@@ -230,7 +232,7 @@ ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
230
232
 
231
233
  stream.push(req.rawBody);
232
234
  stream.push(null);
233
- stream.pipe(busboy);
235
+ stream.pipe(bb);
234
236
  } else {
235
237
  const readStream = that.fs.createReadStream(req.rawBodyPath);
236
238
 
@@ -238,7 +240,7 @@ ReqParser.prototype.parseFormMultipart = function parseFormMultipart(req, cb) {
238
240
  that.log.error(logPrefix + 'Can not create read stream to file. Path: "' + req.rawBodyPath + '", err: ' + err.message);
239
241
  });
240
242
 
241
- readStream.pipe(busboy);
243
+ readStream.pipe(bb);
242
244
  }
243
245
  };
244
246
 
@@ -263,7 +265,7 @@ ReqParser.prototype.parseFormUrlEncoded = function parseFormUrlEncoded(req, cb)
263
265
  return cb(err);
264
266
  }
265
267
 
266
- req.formFields = qs.parse(content.toString());
268
+ req.formFields = qs.parse(content.toString(), that.qsParseOptions || {});
267
269
  cb();
268
270
  });
269
271
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "larvitreqparser",
3
- "version": "0.4.10",
3
+ "version": "0.5.0",
4
4
  "description": "Middleware to larvitbase or Express to handle forms",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "lint": "eslint ./",
8
- "test": "npm run lint && nyc --reporter=lcov tape test/**/*.js | tap-spec; nyc report ---reporter=text"
8
+ "test": "npm run lint && nyc --reporter=lcov tape test/**/*.js"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -27,19 +27,18 @@
27
27
  },
28
28
  "homepage": "https://github.com/larvit/larvitreqparser#readme",
29
29
  "devDependencies": {
30
- "coveralls": "3.0.9",
31
- "eslint": "6.8.0",
32
- "nyc": "15.0.0",
33
- "request": "2.88.2",
34
- "tap-spec": "5.0.0",
35
- "tape": "4.13.2"
30
+ "axios": "0.26.0",
31
+ "eslint": "8.9.0",
32
+ "form-data": "4.0.0",
33
+ "nyc": "15.1.0",
34
+ "tape": "5.5.2"
36
35
  },
37
36
  "dependencies": {
38
- "async": "3.2.0",
39
- "busboy": "0.3.1",
40
- "fs-extra": "8.1.0",
41
- "larvitutils": "3.2.1",
42
- "qs": "6.9.1",
43
- "uuid": "7.0.2"
37
+ "async": "3.2.3",
38
+ "busboy": "1.4.0",
39
+ "fs-extra": "10.0.0",
40
+ "larvitutils": "5.0.0",
41
+ "qs": "6.10.3",
42
+ "uuid": "8.3.2"
44
43
  }
45
44
  }
package/test/test.js CHANGED
@@ -1,15 +1,18 @@
1
1
  'use strict';
2
2
 
3
3
  const ReqParser = require(__dirname + '/../index.js');
4
- const request = require('request');
5
- const uuidv4 = require('uuid/v4');
4
+ const FormData = require('form-data');
5
+ const { Log } = require('larvitutils');
6
+ const uuidLib = require('uuid');
6
7
  const tmpDir = require('os').tmpdir();
7
- const LUtils = require('larvitutils');
8
+ const axios = require('axios').default;
8
9
  const test = require('tape');
9
10
  const http = require('http');
10
- const log = new LUtils.Log('none');
11
11
  const fs = require('fs-extra');
12
12
 
13
+
14
+ const log = new Log('none');
15
+
13
16
  /* eslint-disable require-jsdoc */
14
17
 
15
18
  function startServer(reqParser, reqHandler, cb) {
@@ -50,7 +53,7 @@ test('GET Http request, no body, no passed log instance', t => {
50
53
  if (err) throw err;
51
54
  server = result;
52
55
  port = server.address().port;
53
- request('http://127.0.0.1:' + port + '/foo?bar=baz');
56
+ axios('http://127.0.0.1:' + port + '/foo?bar=baz');
54
57
  });
55
58
  });
56
59
 
@@ -125,15 +128,15 @@ test('POST, raw body, memory storage', t => {
125
128
  if (err) throw err;
126
129
  server = result;
127
130
  port = server.address().port;
128
- request({
131
+ axios({
129
132
  url: 'http://127.0.0.1:' + port + '/',
130
- body: 'foobar',
133
+ data: 'foobar',
131
134
  });
132
135
  });
133
136
  });
134
137
 
135
138
  test('POST, raw body, fs storage', t => {
136
- const storagePath = tmpDir + '/' + uuidv4();
139
+ const storagePath = tmpDir + '/' + uuidLib.v4();
137
140
  const reqParser = new ReqParser({ storage: storagePath, log });
138
141
 
139
142
  let server;
@@ -159,15 +162,16 @@ test('POST, raw body, fs storage', t => {
159
162
  if (err) throw err;
160
163
  server = result;
161
164
  port = server.address().port;
162
- request({
165
+ axios({
166
+ method: 'POST',
163
167
  url: 'http://127.0.0.1:' + port + '/',
164
- body: 'foobar',
168
+ data: 'foobar',
165
169
  });
166
170
  });
167
171
  });
168
172
 
169
173
  test('POST, raw body, custom fs storage and busboy options', t => {
170
- const storagePath = tmpDir + '/' + uuidv4();
174
+ const storagePath = tmpDir + '/' + uuidLib.v4();
171
175
  const reqParser = new ReqParser({
172
176
  storage: storagePath,
173
177
  log,
@@ -198,9 +202,10 @@ test('POST, raw body, custom fs storage and busboy options', t => {
198
202
  if (err) throw err;
199
203
  server = result;
200
204
  port = server.address().port;
201
- request({
205
+ axios({
206
+ method: 'POST',
202
207
  url: 'http://127.0.0.1:' + port + '/',
203
- body: 'foobar',
208
+ data: 'foobar',
204
209
  });
205
210
  });
206
211
  });
@@ -226,9 +231,10 @@ test('POST, raw body, fs storage, invalid path', t => {
226
231
  if (err) throw err;
227
232
  server = result;
228
233
  port = server.address().port;
229
- request({
234
+ axios({
235
+ method: 'POST',
230
236
  url: 'http://127.0.0.1:' + port + '/',
231
- body: 'foobar',
237
+ data: 'foobar',
232
238
  });
233
239
  });
234
240
  });
@@ -248,7 +254,7 @@ test('POST, application/x-www-form-urlencoded, memory storage', t => {
248
254
 
249
255
  t.equal(
250
256
  req.rawBody.toString(),
251
- 'foo=bar&beng%20tops%5B0%5D=arr&beng%20tops%5B1%5D=ay&beng%20tops%5B2%5D=%C3%B6ber%20alles%20%26%20%C3%A4nnu%20mer',
257
+ 'foo=bar&beng+tops=arr&beng+tops=ay&beng+tops=%C3%B6ber+alles+%26+%C3%A4nnu+mer',
252
258
  'rawBody should be the raw form content',
253
259
  );
254
260
  t.equal(
@@ -262,22 +268,17 @@ test('POST, application/x-www-form-urlencoded, memory storage', t => {
262
268
  if (err) throw err;
263
269
  server = result;
264
270
  port = server.address().port;
265
- request({
266
- url: 'http://127.0.0.1:' + port + '/',
267
- form: {
268
- foo: 'bar',
269
- 'beng tops': [
270
- 'arr',
271
- 'ay',
272
- 'öber alles & ännu mer',
273
- ],
274
- },
275
- });
271
+ const formData = new URLSearchParams();
272
+ formData.append('foo', 'bar');
273
+ formData.append('beng tops', 'arr');
274
+ formData.append('beng tops', 'ay');
275
+ formData.append('beng tops', 'öber alles & ännu mer');
276
+ axios.post('http://127.0.0.1:' + port + '/', formData);
276
277
  });
277
278
  });
278
279
 
279
280
  test('POST, application/x-www-form-urlencoded, fs storage', t => {
280
- const storagePath = tmpDir + '/' + uuidv4();
281
+ const storagePath = tmpDir + '/' + uuidLib.v4();
281
282
  const reqParser = new ReqParser({ storage: storagePath, log });
282
283
 
283
284
  let server;
@@ -292,7 +293,7 @@ test('POST, application/x-www-form-urlencoded, fs storage', t => {
292
293
 
293
294
  t.equal(
294
295
  fs.readFileSync(req.rawBodyPath).toString(),
295
- 'foo=bar&beng%20tops%5B0%5D=arr&beng%20tops%5B1%5D=ay&beng%20tops%5B2%5D=%C3%B6ber%20alles%20%26%20%C3%A4nnu%20mer',
296
+ 'foo=bar&beng+tops=arr&beng+tops=ay&beng+tops=%C3%B6ber+alles+%26+%C3%A4nnu+mer',
296
297
  'rawBody should be the raw form content',
297
298
  );
298
299
  t.equal(
@@ -306,17 +307,54 @@ test('POST, application/x-www-form-urlencoded, fs storage', t => {
306
307
  if (err) throw err;
307
308
  server = result;
308
309
  port = server.address().port;
309
- request({
310
- url: 'http://127.0.0.1:' + port + '/',
311
- form: {
312
- foo: 'bar',
313
- 'beng tops': [
314
- 'arr',
315
- 'ay',
316
- 'öber alles & ännu mer',
317
- ],
318
- },
310
+ const formData = new URLSearchParams();
311
+ formData.append('foo', 'bar');
312
+ formData.append('beng tops', 'arr');
313
+ formData.append('beng tops', 'ay');
314
+ formData.append('beng tops', 'öber alles & ännu mer');
315
+ axios.post('http://127.0.0.1:' + port + '/', formData);
316
+ });
317
+ });
318
+
319
+ test('POST, application/x-www-form-urlencoded, fs storage parameterLimit: 6', t => {
320
+ const storagePath = tmpDir + '/' + uuidLib.v4();
321
+ const reqParser = new ReqParser({ storage: storagePath, qsParseOptions: { parameterLimit: 6 }, log });
322
+
323
+ let server;
324
+ let port;
325
+
326
+ startServer(reqParser, (req, res) => {
327
+ if (req.err) throw req.err;
328
+ res.end();
329
+ server.close(err => {
330
+ if (err) throw err;
319
331
  });
332
+
333
+ t.equal(
334
+ fs.readFileSync(req.rawBodyPath).toString(),
335
+ 'foo=bar&beng+tops=arr&beng+tops=ay&beng+tops=%C3%B6ber+alles+%26+%C3%A4nnu+mer&testar=jaha&testar=nehe&testar=ojd%C3%A5',
336
+ 'rawBody should be the raw form content',
337
+ );
338
+ t.equal(
339
+ JSON.stringify(req.formFields),
340
+ '{"foo":"bar","beng tops":["arr","ay","öber alles & ännu mer"],"testar":["jaha","nehe"]}', // ,"ojdå" missing due to parameterLimit: 6.
341
+ 'req.formFields should be a nice stringifyed JSON of the form data',
342
+ );
343
+
344
+ t.end();
345
+ }, (err, result) => {
346
+ if (err) throw err;
347
+ server = result;
348
+ port = server.address().port;
349
+ const formData = new URLSearchParams();
350
+ formData.append('foo', 'bar');
351
+ formData.append('beng tops', 'arr');
352
+ formData.append('beng tops', 'ay');
353
+ formData.append('beng tops', 'öber alles & ännu mer');
354
+ formData.append('testar', 'jaha');
355
+ formData.append('testar', 'nehe');
356
+ formData.append('testar', 'ojdå');
357
+ axios.post('http://127.0.0.1:' + port + '/', formData);
320
358
  });
321
359
  });
322
360
 
@@ -347,41 +385,32 @@ test('POST, multipart/form-data, memory storage', t => {
347
385
 
348
386
  t.end();
349
387
  }, (err, result) => {
350
- const formData = {};
351
-
352
388
  if (err) throw err;
353
389
 
354
390
  server = result;
355
391
  port = server.address().port;
356
392
 
357
- formData.foo = 'bar';
358
- formData['ove[]'] = 'första ove';
359
- formData['beng tops'] = 'öber alles & ännu mer';
360
- formData['untz[firstname]'] = 'kalle';
361
- formData['untz[lastname][first]'] = 'benktsson';
362
- formData['untz[lastname][second]'] = 'lurresson';
363
- formData.enLitenBuffer = Buffer.from('foo feng fall');
364
- formData['arrWithBuffers[]'] = [
365
- Buffer.from('apa'),
366
- Buffer.from('bengbison'),
367
- ];
368
- formData.customFile = {
369
- value: Buffer.from('skruppelräv'),
370
- options: {
371
- filename: 'reven.txt',
372
- contentType: 'text/plain',
373
- },
374
- };
375
-
376
- request({
377
- url: 'http://127.0.0.1:' + port + '/',
378
- formData: formData,
393
+ const formData = new FormData();
394
+ formData.append('foo', 'bar');
395
+ formData.append('ove[]', 'första ove');
396
+ formData.append('beng tops', 'öber alles & ännu mer');
397
+ formData.append('untz[firstname]', 'kalle');
398
+ formData.append('untz[lastname][first]', 'benktsson');
399
+ formData.append('untz[lastname][second]', 'lurresson');
400
+ formData.append('enLitenBuffer', Buffer.from('foo feng fall'));
401
+ formData.append('arrWithBuffers[]', Buffer.from('apa'));
402
+ formData.append('arrWithBuffers[]', Buffer.from('bengbison'));
403
+ formData.append('customFile', Buffer.from('skruppelräv'), {
404
+ filename: 'reven.txt',
405
+ contentType: 'text/plain',
379
406
  });
407
+
408
+ axios.post('http://127.0.0.1:' + port + '/', formData, { headers: formData.getHeaders() });
380
409
  });
381
410
  });
382
411
 
383
412
  test('POST, multipart/form-data, fs storage', t => {
384
- const storagePath = tmpDir + '/' + uuidv4();
413
+ const storagePath = tmpDir + '/' + uuidLib.v4();
385
414
  const reqParser = new ReqParser({ storage: storagePath, log });
386
415
 
387
416
  let server;
@@ -417,33 +446,24 @@ test('POST, multipart/form-data, fs storage', t => {
417
446
  }, 50);
418
447
  });
419
448
  }, (err, result) => {
420
- const formData = {};
421
-
422
449
  if (err) throw err;
423
450
 
424
451
  server = result;
425
452
  port = server.address().port;
426
453
 
427
- formData.foo = 'bar';
428
- formData['ove[]'] = 'första ove';
429
- formData['beng tops'] = 'öber alles & ännu mer';
430
- formData.enLitenBuffer = Buffer.from('foo feng fall');
431
- formData['arrWithBuffers[]'] = [
432
- Buffer.from('apa'),
433
- Buffer.from('bengbison'),
434
- ];
435
- formData.customFile = {
436
- value: Buffer.from('skruppelräv'),
437
- options: {
438
- filename: 'reven.txt',
439
- contentType: 'text/plain',
440
- },
441
- };
442
-
443
- request({
444
- url: 'http://127.0.0.1:' + port + '/',
445
- formData: formData,
454
+ const formData = new FormData();
455
+ formData.append('foo', 'bar');
456
+ formData.append('ove[]', 'första ove');
457
+ formData.append('beng tops', 'öber alles & ännu mer');
458
+ formData.append('enLitenBuffer', Buffer.from('foo feng fall'));
459
+ formData.append('arrWithBuffers[]', Buffer.from('apa'));
460
+ formData.append('arrWithBuffers[]', Buffer.from('bengbison'));
461
+ formData.append('customFile', Buffer.from('skruppelräv'), {
462
+ filename: 'reven.txt',
463
+ contentType: 'text/plain',
446
464
  });
465
+
466
+ axios.post('http://127.0.0.1:' + port + '/', formData, { headers: formData.getHeaders() });
447
467
  });
448
468
  });
449
469
 
@@ -468,18 +488,19 @@ test('POST, empty form should not crasch application', t => {
468
488
  server = result;
469
489
  port = server.address().port;
470
490
 
471
- request.post({
491
+ axios({
492
+ method: 'POST',
472
493
  url: 'http://127.0.0.1:' + port + '/',
494
+ data: undefined,
473
495
  headers: {
474
496
  'Content-Type': 'application/x-www-form-urlencoded',
475
497
  },
476
- body: undefined,
477
498
  });
478
499
  });
479
500
  });
480
501
 
481
502
  test('clean will remove temporary form files', t => {
482
- const storagePath = tmpDir + '/' + uuidv4();
503
+ const storagePath = tmpDir + '/' + uuidLib.v4();
483
504
  const reqParser = new ReqParser({ storage: storagePath, log });
484
505
 
485
506
  let server;
@@ -512,43 +533,30 @@ test('clean will remove temporary form files', t => {
512
533
  }, 50);
513
534
  });
514
535
  }, (err, result) => {
515
- const formData = {};
516
-
517
536
  if (err) throw err;
518
537
 
519
538
  server = result;
520
539
  port = server.address().port;
521
540
 
522
- formData.customFile = {
523
- value: Buffer.from('skruppelräv'),
524
- options: {
525
- filename: 'reven.txt',
526
- contentType: 'text/plain',
527
- },
528
- };
529
-
530
- formData.manuallyCleanedUp = {
531
- value: Buffer.from('nissepisse'),
532
- options: {
533
- filename: 'nisse.txt',
534
- contentType: 'text/plain',
535
- },
536
- };
541
+ const formData = new FormData();
542
+ formData.append('customFile', Buffer.from('skruppelräv'), {
543
+ filename: 'reven.txt',
544
+ contentType: 'text/plain',
545
+ });
546
+ formData.append('manuallyCleanedUp', Buffer.from('nissepisse'), {
547
+ filename: 'nisse.txt',
548
+ contentType: 'text/plain',
549
+ });
537
550
 
538
- formData['arrWithBuffers[]'] = [
539
- Buffer.from('apa'),
540
- Buffer.from('bengbison'),
541
- ];
551
+ formData.append('arrWithBuffers[]', Buffer.from('apa'));
552
+ formData.append('arrWithBuffers[]', Buffer.from('bengbison'));
542
553
 
543
- request({
544
- url: 'http://127.0.0.1:' + port + '/',
545
- formData: formData,
546
- });
554
+ axios.post('http://127.0.0.1:' + port + '/', formData, { headers: formData.getHeaders() });
547
555
  });
548
556
  });
549
557
 
550
558
  test('clean will remove temporary raw body file', t => {
551
- const storagePath = tmpDir + '/' + uuidv4();
559
+ const storagePath = tmpDir + '/' + uuidLib.v4();
552
560
  const reqParser = new ReqParser({ storage: storagePath, log });
553
561
 
554
562
  let server;
@@ -581,7 +589,7 @@ test('clean will remove temporary raw body file', t => {
581
589
 
582
590
  formData.foo = 'bar';
583
591
 
584
- request({
592
+ axios({
585
593
  url: 'http://127.0.0.1:' + port + '/',
586
594
  formData: formData,
587
595
  });