fastify 3.25.1 → 3.25.2

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.
@@ -49,7 +49,7 @@ const server = require('./app')({
49
49
 
50
50
  server.listen(3000, (err, address) => {
51
51
  if (err) {
52
- console.log(err)
52
+ server.log.error(err)
53
53
  process.exit(1)
54
54
  }
55
55
  })
@@ -51,9 +51,6 @@ in a blank http Fastify server.
51
51
  }
52
52
  ```
53
53
 
54
- *Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid
55
- [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
56
-
57
54
  3. Initialize a TypeScript configuration file:
58
55
  ```bash
59
56
  npx tsc --init
@@ -61,6 +58,9 @@ in a blank http Fastify server.
61
58
  or use one of the [recommended
62
59
  ones](https://github.com/tsconfig/bases#node-10-tsconfigjson).
63
60
 
61
+ *Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid
62
+ [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
63
+
64
64
  4. Create an `index.ts` file - this will contain the server code
65
65
  5. Add the following code block to your file:
66
66
  ```typescript
@@ -228,8 +228,8 @@ can do it as follows:
228
228
  },
229
229
  },
230
230
  },
231
- (req, rep) => {
232
- const { body: user } = req;
231
+ (request, reply) => {
232
+ const { body: user } = request;
233
233
  /* user has type
234
234
  * const user: StaticProperties<{
235
235
  * name: TString;
@@ -237,7 +237,7 @@ can do it as follows:
237
237
  * }>
238
238
  */
239
239
  //...
240
- rep.status(200).send(user);
240
+ reply.status(200).send(user);
241
241
  }
242
242
  );
243
243
  ```
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '3.25.1'
3
+ const VERSION = '3.25.2'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { AsyncResource } = require('async_hooks')
3
4
  let lru = require('tiny-lru')
4
5
  // Needed to handle Webpack and faux modules
5
6
  // See https://github.com/fastify/fastify/issues/2356
@@ -137,6 +138,7 @@ ContentTypeParser.prototype.remove = function (contentType) {
137
138
 
138
139
  ContentTypeParser.prototype.run = function (contentType, handler, request, reply) {
139
140
  const parser = this.cache.get(contentType) || this.getParser(contentType)
141
+ const resource = new AsyncResource('content-type-parser:run', request)
140
142
 
141
143
  if (parser === undefined) {
142
144
  reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType))
@@ -163,12 +165,15 @@ ContentTypeParser.prototype.run = function (contentType, handler, request, reply
163
165
  }
164
166
 
165
167
  function done (error, body) {
166
- if (error) {
167
- reply.send(error)
168
- } else {
169
- request.body = body
170
- handler(request, reply)
171
- }
168
+ // We cannot use resource.bind() because it is broken in node v12.
169
+ resource.runInAsyncScope(() => {
170
+ if (error) {
171
+ reply.send(error)
172
+ } else {
173
+ request.body = body
174
+ handler(request, reply)
175
+ }
176
+ })
172
177
  }
173
178
  }
174
179
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "3.25.1",
3
+ "version": "3.25.2",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -0,0 +1,74 @@
1
+ 'use strict'
2
+
3
+ const { AsyncLocalStorage } = require('async_hooks')
4
+ const t = require('tap')
5
+ const Fastify = require('..')
6
+ const sget = require('simple-get').concat
7
+
8
+ if (!AsyncLocalStorage) {
9
+ t.skip('AsyncLocalStorage not available, skipping test')
10
+ process.exit(0)
11
+ }
12
+
13
+ const storage = new AsyncLocalStorage()
14
+ const app = Fastify({ logger: false })
15
+
16
+ let counter = 0
17
+ app.addHook('onRequest', (req, reply, next) => {
18
+ const id = counter++
19
+ storage.run({ id }, next)
20
+ })
21
+
22
+ app.get('/', function (request, reply) {
23
+ t.ok(storage.getStore())
24
+ const id = storage.getStore().id
25
+ reply.send({ id })
26
+ })
27
+
28
+ app.post('/', function (request, reply) {
29
+ t.ok(storage.getStore())
30
+ const id = storage.getStore().id
31
+ reply.send({ id })
32
+ })
33
+
34
+ app.listen(3000, function (err, address) {
35
+ t.error(err)
36
+
37
+ sget({
38
+ method: 'POST',
39
+ url: 'http://localhost:' + app.server.address().port,
40
+ body: {
41
+ hello: 'world'
42
+ },
43
+ json: true
44
+ }, (err, response, body) => {
45
+ t.error(err)
46
+ t.equal(response.statusCode, 200)
47
+ t.same(body, { id: 0 })
48
+
49
+ sget({
50
+ method: 'POST',
51
+ url: 'http://localhost:' + app.server.address().port,
52
+ body: {
53
+ hello: 'world'
54
+ },
55
+ json: true
56
+ }, (err, response, body) => {
57
+ t.error(err)
58
+ t.equal(response.statusCode, 200)
59
+ t.same(body, { id: 1 })
60
+
61
+ sget({
62
+ method: 'GET',
63
+ url: 'http://localhost:' + app.server.address().port,
64
+ json: true
65
+ }, (err, response, body) => {
66
+ t.error(err)
67
+ t.equal(response.statusCode, 200)
68
+ t.same(body, { id: 2 })
69
+ app.close()
70
+ t.end()
71
+ })
72
+ })
73
+ })
74
+ })