@fastify/express 1.0.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/.github/dependabot.yml +13 -0
- package/.github/stale.yml +21 -0
- package/.github/workflows/nodejs.yml +49 -0
- package/LICENSE +21 -0
- package/README.md +207 -0
- package/index.d.ts +20 -0
- package/index.js +83 -0
- package/package.json +54 -0
- package/test/404s.test.js +338 -0
- package/test/application.test.js +179 -0
- package/test/basic.test.js +671 -0
- package/test/enhance-request.test.js +69 -0
- package/test/hooks.test.js +37 -0
- package/test/middleware.test.js +413 -0
- package/test/types/index.ts +12 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const sget = require('simple-get').concat
|
|
6
|
+
|
|
7
|
+
const expressPlugin = require('../index')
|
|
8
|
+
|
|
9
|
+
test('Should enhance the Node.js core request/response objects', t => {
|
|
10
|
+
t.plan(10)
|
|
11
|
+
const fastify = Fastify()
|
|
12
|
+
t.teardown(fastify.close)
|
|
13
|
+
|
|
14
|
+
fastify.register(expressPlugin)
|
|
15
|
+
|
|
16
|
+
fastify.get('/', async (req, reply) => {
|
|
17
|
+
t.strictEqual(req.raw.originalUrl, req.raw.url)
|
|
18
|
+
t.strictEqual(req.raw.id, req.id)
|
|
19
|
+
t.strictEqual(req.raw.hostname, req.hostname)
|
|
20
|
+
t.strictEqual(req.raw.protocol, req.protocol)
|
|
21
|
+
t.strictEqual(req.raw.ip, req.ip)
|
|
22
|
+
t.deepEqual(req.raw.ips, req.ips)
|
|
23
|
+
t.ok(req.raw.log)
|
|
24
|
+
t.ok(reply.raw.log)
|
|
25
|
+
return { hello: 'world' }
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
fastify.listen(0, (err, address) => {
|
|
29
|
+
t.error(err)
|
|
30
|
+
sget({
|
|
31
|
+
method: 'GET',
|
|
32
|
+
url: address
|
|
33
|
+
}, (err, res, data) => {
|
|
34
|
+
t.error(err)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('trust proxy protocol', (t) => {
|
|
40
|
+
t.plan(5)
|
|
41
|
+
const fastify = Fastify({ trustProxy: true })
|
|
42
|
+
|
|
43
|
+
t.tearDown(fastify.close.bind(fastify))
|
|
44
|
+
|
|
45
|
+
fastify.register(expressPlugin).after(() => {
|
|
46
|
+
fastify.use('/', function (req, res) {
|
|
47
|
+
t.strictEqual(req.ip, '1.1.1.1', 'gets ip from x-forwarded-for')
|
|
48
|
+
t.strictEqual(req.hostname, 'example.com', 'gets hostname from x-forwarded-host')
|
|
49
|
+
t.strictEqual(req.protocol, 'lorem', 'gets protocol from x-forwarded-proto')
|
|
50
|
+
|
|
51
|
+
res.sendStatus(200)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
fastify.listen(0, (err, address) => {
|
|
56
|
+
t.error(err)
|
|
57
|
+
sget({
|
|
58
|
+
method: 'GET',
|
|
59
|
+
headers: {
|
|
60
|
+
'X-Forwarded-For': '1.1.1.1',
|
|
61
|
+
'X-Forwarded-Host': 'example.com',
|
|
62
|
+
'X-Forwarded-Proto': 'lorem'
|
|
63
|
+
},
|
|
64
|
+
url: address
|
|
65
|
+
}, (err, res, data) => {
|
|
66
|
+
t.error(err)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const expressPlugin = require('../index')
|
|
6
|
+
|
|
7
|
+
test('onSend hook should receive valid request and reply objects if middleware fails', t => {
|
|
8
|
+
t.plan(4)
|
|
9
|
+
const fastify = Fastify()
|
|
10
|
+
fastify.register(expressPlugin)
|
|
11
|
+
.after(() => {
|
|
12
|
+
fastify.use(function (req, res, next) {
|
|
13
|
+
next(new Error('middlware failed'))
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
fastify.decorateRequest('testDecorator', 'testDecoratorVal')
|
|
18
|
+
fastify.decorateReply('testDecorator', 'testDecoratorVal')
|
|
19
|
+
|
|
20
|
+
fastify.addHook('onSend', function (request, reply, payload, next) {
|
|
21
|
+
t.strictEqual(request.testDecorator, 'testDecoratorVal')
|
|
22
|
+
t.strictEqual(reply.testDecorator, 'testDecoratorVal')
|
|
23
|
+
next()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
fastify.get('/', (req, reply) => {
|
|
27
|
+
reply.send('hello')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
fastify.inject({
|
|
31
|
+
method: 'GET',
|
|
32
|
+
url: '/'
|
|
33
|
+
}, (err, res) => {
|
|
34
|
+
t.error(err)
|
|
35
|
+
t.strictEqual(res.statusCode, 500)
|
|
36
|
+
})
|
|
37
|
+
})
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Original Fastify test/middlewares.test.js file
|
|
4
|
+
|
|
5
|
+
const t = require('tap')
|
|
6
|
+
const test = t.test
|
|
7
|
+
const sget = require('simple-get').concat
|
|
8
|
+
const fastify = require('fastify')
|
|
9
|
+
const fp = require('fastify-plugin')
|
|
10
|
+
const cors = require('cors')
|
|
11
|
+
const helmet = require('helmet')
|
|
12
|
+
|
|
13
|
+
const expressPlugin = require('../index')
|
|
14
|
+
|
|
15
|
+
test('use a middleware', t => {
|
|
16
|
+
t.plan(7)
|
|
17
|
+
|
|
18
|
+
const instance = fastify()
|
|
19
|
+
instance.register(expressPlugin)
|
|
20
|
+
.after(() => {
|
|
21
|
+
const useRes = instance.use(function (req, res, next) {
|
|
22
|
+
t.pass('middleware called')
|
|
23
|
+
next()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
t.equal(useRes, instance)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
instance.get('/', function (request, reply) {
|
|
30
|
+
reply.send({ hello: 'world' })
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
instance.listen(0, err => {
|
|
34
|
+
t.error(err)
|
|
35
|
+
|
|
36
|
+
t.tearDown(instance.server.close.bind(instance.server))
|
|
37
|
+
|
|
38
|
+
sget({
|
|
39
|
+
method: 'GET',
|
|
40
|
+
url: 'http://localhost:' + instance.server.address().port
|
|
41
|
+
}, (err, response, body) => {
|
|
42
|
+
t.error(err)
|
|
43
|
+
t.strictEqual(response.statusCode, 200)
|
|
44
|
+
t.strictEqual(response.headers['content-length'], '' + body.length)
|
|
45
|
+
t.deepEqual(JSON.parse(body), { hello: 'world' })
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('use cors', t => {
|
|
51
|
+
t.plan(3)
|
|
52
|
+
|
|
53
|
+
const instance = fastify()
|
|
54
|
+
instance.register(expressPlugin)
|
|
55
|
+
.after(() => {
|
|
56
|
+
instance.use(cors())
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
instance.get('/', function (request, reply) {
|
|
60
|
+
reply.send({ hello: 'world' })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
instance.listen(0, err => {
|
|
64
|
+
t.error(err)
|
|
65
|
+
|
|
66
|
+
t.tearDown(instance.server.close.bind(instance.server))
|
|
67
|
+
|
|
68
|
+
sget({
|
|
69
|
+
method: 'GET',
|
|
70
|
+
url: 'http://localhost:' + instance.server.address().port
|
|
71
|
+
}, (err, response, body) => {
|
|
72
|
+
t.error(err)
|
|
73
|
+
t.equal(response.headers['access-control-allow-origin'], '*')
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('use helmet', t => {
|
|
79
|
+
t.plan(3)
|
|
80
|
+
|
|
81
|
+
const instance = fastify()
|
|
82
|
+
instance.register(expressPlugin)
|
|
83
|
+
.after(() => {
|
|
84
|
+
instance.use(helmet())
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
instance.get('/', function (request, reply) {
|
|
88
|
+
reply.send({ hello: 'world' })
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
instance.listen(0, err => {
|
|
92
|
+
t.error(err)
|
|
93
|
+
|
|
94
|
+
t.tearDown(instance.server.close.bind(instance.server))
|
|
95
|
+
|
|
96
|
+
sget({
|
|
97
|
+
method: 'GET',
|
|
98
|
+
url: 'http://localhost:' + instance.server.address().port
|
|
99
|
+
}, (err, response, body) => {
|
|
100
|
+
t.error(err)
|
|
101
|
+
t.ok(response.headers['x-xss-protection'])
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('use helmet and cors', t => {
|
|
107
|
+
t.plan(4)
|
|
108
|
+
|
|
109
|
+
const instance = fastify()
|
|
110
|
+
instance.register(expressPlugin)
|
|
111
|
+
.after(() => {
|
|
112
|
+
instance.use(cors())
|
|
113
|
+
instance.use(helmet())
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
instance.get('/', function (request, reply) {
|
|
117
|
+
reply.send({ hello: 'world' })
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
instance.listen(0, err => {
|
|
121
|
+
t.error(err)
|
|
122
|
+
|
|
123
|
+
t.tearDown(instance.server.close.bind(instance.server))
|
|
124
|
+
|
|
125
|
+
sget({
|
|
126
|
+
method: 'GET',
|
|
127
|
+
url: 'http://localhost:' + instance.server.address().port
|
|
128
|
+
}, (err, response, body) => {
|
|
129
|
+
t.error(err)
|
|
130
|
+
t.ok(response.headers['x-xss-protection'])
|
|
131
|
+
t.equal(response.headers['access-control-allow-origin'], '*')
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('middlewares with prefix', t => {
|
|
137
|
+
t.plan(5)
|
|
138
|
+
|
|
139
|
+
const instance = fastify()
|
|
140
|
+
instance.register(expressPlugin)
|
|
141
|
+
.after(() => {
|
|
142
|
+
instance.use(function (req, res, next) {
|
|
143
|
+
req.global = true
|
|
144
|
+
next()
|
|
145
|
+
})
|
|
146
|
+
instance.use('', function (req, res, next) {
|
|
147
|
+
req.global2 = true
|
|
148
|
+
next()
|
|
149
|
+
})
|
|
150
|
+
instance.use('/', function (req, res, next) {
|
|
151
|
+
req.root = true
|
|
152
|
+
next()
|
|
153
|
+
})
|
|
154
|
+
instance.use('/prefix', function (req, res, next) {
|
|
155
|
+
req.prefixed = true
|
|
156
|
+
next()
|
|
157
|
+
})
|
|
158
|
+
instance.use('/prefix/', function (req, res, next) {
|
|
159
|
+
req.slashed = true
|
|
160
|
+
next()
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
function handler (request, reply) {
|
|
165
|
+
reply.send({
|
|
166
|
+
prefixed: request.raw.prefixed,
|
|
167
|
+
slashed: request.raw.slashed,
|
|
168
|
+
global: request.raw.global,
|
|
169
|
+
global2: request.raw.global2,
|
|
170
|
+
root: request.raw.root
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
instance.get('/', handler)
|
|
175
|
+
instance.get('/prefix', handler)
|
|
176
|
+
instance.get('/prefix/', handler)
|
|
177
|
+
instance.get('/prefix/inner', handler)
|
|
178
|
+
|
|
179
|
+
instance.listen(0, err => {
|
|
180
|
+
t.error(err)
|
|
181
|
+
t.tearDown(instance.server.close.bind(instance.server))
|
|
182
|
+
|
|
183
|
+
t.test('/', t => {
|
|
184
|
+
t.plan(2)
|
|
185
|
+
sget({
|
|
186
|
+
method: 'GET',
|
|
187
|
+
url: 'http://localhost:' + instance.server.address().port + '/',
|
|
188
|
+
json: true
|
|
189
|
+
}, (err, response, body) => {
|
|
190
|
+
t.error(err)
|
|
191
|
+
t.deepEqual(body, {
|
|
192
|
+
global: true,
|
|
193
|
+
global2: true,
|
|
194
|
+
root: true
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
t.test('/prefix', t => {
|
|
200
|
+
t.plan(2)
|
|
201
|
+
sget({
|
|
202
|
+
method: 'GET',
|
|
203
|
+
url: 'http://localhost:' + instance.server.address().port + '/prefix',
|
|
204
|
+
json: true
|
|
205
|
+
}, (err, response, body) => {
|
|
206
|
+
t.error(err)
|
|
207
|
+
t.deepEqual(body, {
|
|
208
|
+
prefixed: true,
|
|
209
|
+
global: true,
|
|
210
|
+
global2: true,
|
|
211
|
+
root: true,
|
|
212
|
+
slashed: true
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
t.test('/prefix/', t => {
|
|
218
|
+
t.plan(2)
|
|
219
|
+
sget({
|
|
220
|
+
method: 'GET',
|
|
221
|
+
url: 'http://localhost:' + instance.server.address().port + '/prefix/',
|
|
222
|
+
json: true
|
|
223
|
+
}, (err, response, body) => {
|
|
224
|
+
t.error(err)
|
|
225
|
+
t.deepEqual(body, {
|
|
226
|
+
prefixed: true,
|
|
227
|
+
slashed: true,
|
|
228
|
+
global: true,
|
|
229
|
+
global2: true,
|
|
230
|
+
root: true
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
t.test('/prefix/inner', t => {
|
|
236
|
+
t.plan(2)
|
|
237
|
+
sget({
|
|
238
|
+
method: 'GET',
|
|
239
|
+
url: 'http://localhost:' + instance.server.address().port + '/prefix/inner',
|
|
240
|
+
json: true
|
|
241
|
+
}, (err, response, body) => {
|
|
242
|
+
t.error(err)
|
|
243
|
+
t.deepEqual(body, {
|
|
244
|
+
prefixed: true,
|
|
245
|
+
slashed: true,
|
|
246
|
+
global: true,
|
|
247
|
+
global2: true,
|
|
248
|
+
root: true
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('res.end should block middleware execution', t => {
|
|
256
|
+
t.plan(5)
|
|
257
|
+
|
|
258
|
+
const instance = fastify()
|
|
259
|
+
t.teardown(instance.close)
|
|
260
|
+
instance.register(expressPlugin)
|
|
261
|
+
.after(() => {
|
|
262
|
+
instance.use(function (req, res, next) {
|
|
263
|
+
res.send('hello')
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
instance.use(function (req, res, next) {
|
|
267
|
+
t.fail('we should not be here')
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
instance.addHook('onRequest', (req, res, next) => {
|
|
272
|
+
t.ok('called')
|
|
273
|
+
next()
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
instance.addHook('preHandler', (req, reply, next) => {
|
|
277
|
+
t.fail('this should not be called')
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
instance.addHook('onSend', (req, reply, payload, next) => {
|
|
281
|
+
t.fail('this should not be called')
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
instance.addHook('onResponse', (request, reply, next) => {
|
|
285
|
+
t.ok('called')
|
|
286
|
+
next()
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
instance.get('/', function (request, reply) {
|
|
290
|
+
t.fail('we should no be here')
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
instance.listen(0, (err, address) => {
|
|
294
|
+
t.error(err)
|
|
295
|
+
sget({
|
|
296
|
+
method: 'GET',
|
|
297
|
+
url: address
|
|
298
|
+
}, (err, res, data) => {
|
|
299
|
+
t.error(err)
|
|
300
|
+
t.is(res.statusCode, 200)
|
|
301
|
+
t.is(data.toString(), 'hello')
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
test('Use a middleware inside a plugin after an encapsulated plugin', t => {
|
|
307
|
+
t.plan(5)
|
|
308
|
+
const f = fastify()
|
|
309
|
+
t.teardown(f.close)
|
|
310
|
+
f.register(expressPlugin)
|
|
311
|
+
|
|
312
|
+
f.register(function (instance, opts, next) {
|
|
313
|
+
instance.use(function (req, res, next) {
|
|
314
|
+
t.ok('first middleware called')
|
|
315
|
+
next()
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
instance.get('/', function (request, reply) {
|
|
319
|
+
reply.send({ hello: 'world' })
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
next()
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
f.register(fp(function (instance, opts, next) {
|
|
326
|
+
instance.use(function (req, res, next) {
|
|
327
|
+
t.ok('second middleware called')
|
|
328
|
+
next()
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
next()
|
|
332
|
+
}))
|
|
333
|
+
|
|
334
|
+
f.listen(0, (err, address) => {
|
|
335
|
+
t.error(err)
|
|
336
|
+
sget({
|
|
337
|
+
method: 'GET',
|
|
338
|
+
url: address
|
|
339
|
+
}, (err, res, data) => {
|
|
340
|
+
t.error(err)
|
|
341
|
+
t.is(res.statusCode, 200)
|
|
342
|
+
t.deepEqual(JSON.parse(data), { hello: 'world' })
|
|
343
|
+
})
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
test('middlewares should run in the order in which they are defined', t => {
|
|
348
|
+
t.plan(10)
|
|
349
|
+
const f = fastify()
|
|
350
|
+
t.teardown(f.close)
|
|
351
|
+
f.register(expressPlugin)
|
|
352
|
+
|
|
353
|
+
f.register(fp(function (instance, opts, next) {
|
|
354
|
+
instance.use(function (req, res, next) {
|
|
355
|
+
t.strictEqual(req.previous, undefined)
|
|
356
|
+
req.previous = 1
|
|
357
|
+
next()
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
instance.register(fp(function (i, opts, next) {
|
|
361
|
+
i.use(function (req, res, next) {
|
|
362
|
+
t.strictEqual(req.previous, 2)
|
|
363
|
+
req.previous = 3
|
|
364
|
+
next()
|
|
365
|
+
})
|
|
366
|
+
next()
|
|
367
|
+
}))
|
|
368
|
+
|
|
369
|
+
instance.use(function (req, res, next) {
|
|
370
|
+
t.strictEqual(req.previous, 1)
|
|
371
|
+
req.previous = 2
|
|
372
|
+
next()
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
next()
|
|
376
|
+
}))
|
|
377
|
+
|
|
378
|
+
f.register(function (instance, opts, next) {
|
|
379
|
+
instance.use(function (req, res, next) {
|
|
380
|
+
t.strictEqual(req.previous, 3)
|
|
381
|
+
req.previous = 4
|
|
382
|
+
next()
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
instance.get('/', function (request, reply) {
|
|
386
|
+
t.strictEqual(request.raw.previous, 5)
|
|
387
|
+
reply.send({ hello: 'world' })
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
instance.register(fp(function (i, opts, next) {
|
|
391
|
+
i.use(function (req, res, next) {
|
|
392
|
+
t.strictEqual(req.previous, 4)
|
|
393
|
+
req.previous = 5
|
|
394
|
+
next()
|
|
395
|
+
})
|
|
396
|
+
next()
|
|
397
|
+
}))
|
|
398
|
+
|
|
399
|
+
next()
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
f.listen(0, (err, address) => {
|
|
403
|
+
t.error(err)
|
|
404
|
+
sget({
|
|
405
|
+
method: 'GET',
|
|
406
|
+
url: address
|
|
407
|
+
}, (err, res, data) => {
|
|
408
|
+
t.error(err)
|
|
409
|
+
t.is(res.statusCode, 200)
|
|
410
|
+
t.deepEqual(JSON.parse(data), { hello: 'world' })
|
|
411
|
+
})
|
|
412
|
+
})
|
|
413
|
+
})
|