fastify 3.21.5 → 3.21.6
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/lib/decorate.js +9 -5
- package/lib/reply.js +4 -1
- package/lib/request.js +4 -1
- package/package.json +1 -1
- package/test/decorator.test.js +6 -8
package/lib/decorate.js
CHANGED
|
@@ -36,7 +36,7 @@ function decorate (instance, name, fn, dependencies) {
|
|
|
36
36
|
|
|
37
37
|
function decorateConstructor (konstructor, name, fn, dependencies) {
|
|
38
38
|
const instance = konstructor.prototype
|
|
39
|
-
if (instance.hasOwnProperty(name) || konstructor
|
|
39
|
+
if (instance.hasOwnProperty(name) || hasKey(konstructor, name)) {
|
|
40
40
|
throw new FST_ERR_DEC_ALREADY_PRESENT(name)
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -47,10 +47,10 @@ function decorateConstructor (konstructor, name, fn, dependencies) {
|
|
|
47
47
|
get: fn.getter,
|
|
48
48
|
set: fn.setter
|
|
49
49
|
})
|
|
50
|
-
} else if (
|
|
50
|
+
} else if (typeof fn === 'function') {
|
|
51
51
|
instance[name] = fn
|
|
52
52
|
} else {
|
|
53
|
-
konstructor.props.push(name)
|
|
53
|
+
konstructor.props.push({ key: name, value: fn })
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -74,13 +74,17 @@ function checkExistence (instance, name) {
|
|
|
74
74
|
return instance in this
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
function hasKey (fn, name) {
|
|
78
|
+
return fn.props.find(({ key }) => key === name)
|
|
79
|
+
}
|
|
80
|
+
|
|
77
81
|
function checkRequestExistence (name) {
|
|
78
|
-
if (name && this[kRequest]
|
|
82
|
+
if (name && hasKey(this[kRequest], name)) return true
|
|
79
83
|
return checkExistence(this[kRequest].prototype, name)
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
function checkReplyExistence (name) {
|
|
83
|
-
if (name && this[kReply]
|
|
87
|
+
if (name && hasKey(this[kReply], name)) return true
|
|
84
88
|
return checkExistence(this[kReply].prototype, name)
|
|
85
89
|
}
|
|
86
90
|
|
package/lib/reply.js
CHANGED
|
@@ -673,9 +673,12 @@ function buildReply (R) {
|
|
|
673
673
|
this[kReplyStartTime] = undefined
|
|
674
674
|
this.log = log
|
|
675
675
|
|
|
676
|
+
// eslint-disable-next-line no-var
|
|
677
|
+
var prop
|
|
676
678
|
// eslint-disable-next-line no-var
|
|
677
679
|
for (var i = 0; i < props.length; i++) {
|
|
678
|
-
|
|
680
|
+
prop = props[i]
|
|
681
|
+
this[prop.key] = prop.value
|
|
679
682
|
}
|
|
680
683
|
}
|
|
681
684
|
_Reply.prototype = new R()
|
package/lib/request.js
CHANGED
|
@@ -54,9 +54,12 @@ function buildRegularRequest (R) {
|
|
|
54
54
|
this.log = log
|
|
55
55
|
this.body = null
|
|
56
56
|
|
|
57
|
+
// eslint-disable-next-line no-var
|
|
58
|
+
var prop
|
|
57
59
|
// eslint-disable-next-line no-var
|
|
58
60
|
for (var i = 0; i < props.length; i++) {
|
|
59
|
-
|
|
61
|
+
prop = props[i]
|
|
62
|
+
this[prop.key] = prop.value
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
_Request.prototype = new R()
|
package/package.json
CHANGED
package/test/decorator.test.js
CHANGED
|
@@ -113,12 +113,11 @@ test('should pass error for missing request decorator', t => {
|
|
|
113
113
|
})
|
|
114
114
|
|
|
115
115
|
test('decorateReply inside register', t => {
|
|
116
|
-
t.plan(
|
|
116
|
+
t.plan(11)
|
|
117
117
|
const fastify = Fastify()
|
|
118
118
|
|
|
119
119
|
fastify.register((instance, opts, done) => {
|
|
120
120
|
instance.decorateReply('test', 'test')
|
|
121
|
-
t.ok(instance[symbols.kReply].prototype.test)
|
|
122
121
|
|
|
123
122
|
instance.get('/yes', (req, reply) => {
|
|
124
123
|
t.ok(reply.test, 'test exists')
|
|
@@ -256,12 +255,11 @@ test('decorateReply as plugin (outside .after)', t => {
|
|
|
256
255
|
})
|
|
257
256
|
|
|
258
257
|
test('decorateRequest inside register', t => {
|
|
259
|
-
t.plan(
|
|
258
|
+
t.plan(11)
|
|
260
259
|
const fastify = Fastify()
|
|
261
260
|
|
|
262
261
|
fastify.register((instance, opts, done) => {
|
|
263
262
|
instance.decorateRequest('test', 'test')
|
|
264
|
-
t.ok(instance[symbols.kRequest].prototype.test)
|
|
265
263
|
|
|
266
264
|
instance.get('/yes', (req, reply) => {
|
|
267
265
|
t.ok(req.test, 'test exists')
|
|
@@ -976,8 +974,8 @@ test('decorateRequest/decorateReply is undefined', t => {
|
|
|
976
974
|
fastify.decorateRequest('test', undefined)
|
|
977
975
|
fastify.decorateReply('test2', undefined)
|
|
978
976
|
fastify.get('/yes', (req, reply) => {
|
|
979
|
-
t.equal(req.test,
|
|
980
|
-
t.equal(reply.test2,
|
|
977
|
+
t.equal(req.test, undefined)
|
|
978
|
+
t.equal(reply.test2, undefined)
|
|
981
979
|
reply.send({ hello: 'world' })
|
|
982
980
|
})
|
|
983
981
|
t.teardown(fastify.close.bind(fastify))
|
|
@@ -1005,8 +1003,8 @@ test('decorateRequest/decorateReply is not set to a value', t => {
|
|
|
1005
1003
|
fastify.decorateRequest('test')
|
|
1006
1004
|
fastify.decorateReply('test2')
|
|
1007
1005
|
fastify.get('/yes', (req, reply) => {
|
|
1008
|
-
t.equal(req.test,
|
|
1009
|
-
t.equal(reply.test2,
|
|
1006
|
+
t.equal(req.test, undefined)
|
|
1007
|
+
t.equal(reply.test2, undefined)
|
|
1010
1008
|
reply.send({ hello: 'world' })
|
|
1011
1009
|
})
|
|
1012
1010
|
t.teardown(fastify.close.bind(fastify))
|