fastify 3.29.4 → 3.29.5

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/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '3.29.4'
3
+ const VERSION = '3.29.5'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
@@ -6,7 +6,7 @@ let lru = require('tiny-lru')
6
6
  // See https://github.com/fastify/fastify/issues/2356
7
7
  // and https://github.com/fastify/fastify/discussions/2907.
8
8
  lru = typeof lru === 'function' ? lru : lru.default
9
- const { parse: parseContentType } = require('content-type')
9
+ const { safeParse: safeParseContentType, defaultContentType } = require('fast-content-type-parse')
10
10
 
11
11
  const secureJson = require('secure-json-parse')
12
12
  const {
@@ -72,6 +72,7 @@ ContentTypeParser.prototype.add = function (contentType, opts, parserFn) {
72
72
  if (contentTypeIsString) {
73
73
  this.parserList.unshift(new ParserListItem(contentType))
74
74
  } else {
75
+ contentType.isEssence = contentType.source.indexOf(';') === -1
75
76
  this.parserRegExpList.unshift(contentType)
76
77
  }
77
78
  this.customParsers.set(contentType.toString(), parser)
@@ -107,7 +108,7 @@ ContentTypeParser.prototype.getParser = function (contentType) {
107
108
 
108
109
  // dummyContentType always the same object
109
110
  // we can use === for the comparsion and return early
110
- if (parsed === dummyContentType) {
111
+ if (parsed === defaultContentType) {
111
112
  return this.customParsers.get('')
112
113
  }
113
114
 
@@ -313,6 +314,7 @@ function buildContentTypeParser (c) {
313
314
  contentTypeParser[kDefaultJsonParse] = c[kDefaultJsonParse]
314
315
  contentTypeParser.customParsers = new Map(c.customParsers.entries())
315
316
  contentTypeParser.parserList = c.parserList.slice()
317
+ contentTypeParser.parserRegExpList = c.parserRegExpList.slice()
316
318
  return contentTypeParser
317
319
  }
318
320
 
@@ -364,17 +366,6 @@ function removeAllContentTypeParsers () {
364
366
  this[kContentTypeParser].removeAll()
365
367
  }
366
368
 
367
- // dummy here to prevent repeated object creation
368
- const dummyContentType = { type: '', parameters: Object.create(null) }
369
-
370
- function safeParseContentType (contentType) {
371
- try {
372
- return parseContentType(contentType)
373
- } catch (err) {
374
- return dummyContentType
375
- }
376
- }
377
-
378
369
  function compareContentType (contentType, parserListItem) {
379
370
  if (parserListItem.isEssence) {
380
371
  // we do essence check
@@ -395,7 +386,7 @@ function compareContentType (contentType, parserListItem) {
395
386
  }
396
387
 
397
388
  function compareRegExpContentType (contentType, essenceMIMEType, regexp) {
398
- if (regexp.source.indexOf(';') === -1) {
389
+ if (regexp.isEssence) {
399
390
  // we do essence check
400
391
  return regexp.test(essenceMIMEType)
401
392
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "3.29.4",
3
+ "version": "3.29.5",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -158,9 +158,9 @@
158
158
  "ienoopen": "^1.1.0",
159
159
  "JSONStream": "^1.3.5",
160
160
  "license-checker": "^25.0.1",
161
- "pem": "^1.14.4",
162
161
  "proxyquire": "^2.1.3",
163
162
  "pump": "^3.0.0",
163
+ "self-cert": "^2.0.0",
164
164
  "send": "^0.17.1",
165
165
  "serve-static": "^1.14.1",
166
166
  "simple-get": "^4.0.0",
@@ -181,13 +181,13 @@
181
181
  "@fastify/error": "^2.0.0",
182
182
  "abstract-logging": "^2.0.0",
183
183
  "avvio": "^7.1.2",
184
+ "fast-content-type-parse": "^1.0.0",
184
185
  "fast-json-stringify": "^2.5.2",
185
186
  "find-my-way": "^4.5.0",
186
187
  "flatstr": "^1.0.12",
187
188
  "light-my-request": "^4.2.0",
188
189
  "pino": "^6.13.0",
189
190
  "process-warning": "^1.0.0",
190
- "content-type": "^1.0.4",
191
191
  "proxy-addr": "^2.0.7",
192
192
  "rfdc": "^1.1.4",
193
193
  "secure-json-parse": "^2.0.0",
@@ -1,19 +1,18 @@
1
1
  'use strict'
2
2
 
3
- const util = require('util')
4
- const pem = require('pem')
5
-
6
- const createCertificate = util.promisify(pem.createCertificate)
3
+ const selfCert = require('self-cert')
7
4
 
8
5
  async function buildCertificate () {
9
6
  // "global" is used in here because "t.context" is only supported by "t.beforeEach" and "t.afterEach"
10
7
  // For the test case which execute this code which will be using `t.before` and it can reduce the
11
8
  // number of times executing it.
12
9
  if (!global.context || !global.context.cert || !global.context.key) {
13
- const keys = await createCertificate({ days: 1, selfSigned: true })
10
+ const certs = selfCert({
11
+ expires: new Date(Date.now() + 86400000)
12
+ })
14
13
  global.context = {
15
- cert: keys.certificate,
16
- key: keys.serviceKey
14
+ cert: certs.certificate,
15
+ key: certs.privateKey
17
16
  }
18
17
  }
19
18
  }
@@ -542,3 +542,46 @@ test('content-type fail when parameters not match - regexp', async t => {
542
542
 
543
543
  t.same(response.statusCode, 415)
544
544
  })
545
+
546
+ // Refs: https://github.com/fastify/fastify/issues/4495
547
+ test('content-type regexp list should be cloned when plugin override', async t => {
548
+ t.plan(6)
549
+
550
+ const fastify = Fastify()
551
+
552
+ fastify.addContentTypeParser(/^image\/.*/, { parseAs: 'buffer' }, (req, payload, done) => {
553
+ done(null, payload)
554
+ })
555
+
556
+ fastify.register(function plugin (fastify, options, done) {
557
+ fastify.post('/', function (request, reply) {
558
+ reply.type(request.headers['content-type']).send(request.body)
559
+ })
560
+
561
+ done()
562
+ })
563
+
564
+ {
565
+ const { payload, headers, statusCode } = await fastify.inject({
566
+ method: 'POST',
567
+ path: '/',
568
+ payload: 'jpeg',
569
+ headers: { 'content-type': 'image/jpeg' }
570
+ })
571
+ t.same(statusCode, 200)
572
+ t.same(headers['content-type'], 'image/jpeg')
573
+ t.same(payload, 'jpeg')
574
+ }
575
+
576
+ {
577
+ const { payload, headers, statusCode } = await fastify.inject({
578
+ method: 'POST',
579
+ path: '/',
580
+ payload: 'png',
581
+ headers: { 'content-type': 'image/png' }
582
+ })
583
+ t.same(statusCode, 200)
584
+ t.same(headers['content-type'], 'image/png')
585
+ t.same(payload, 'png')
586
+ }
587
+ })