3
3
## Encapsulation
4
4
<a id =" encapsulation " ></a >
5
5
6
- A fundamental feature of Fastify is the "encapsulation context." The
7
- encapsulation context governs which [ decorators] ( ./Decorators.md ) , registered
8
- [ hooks] ( ./Hooks.md ) , and [ plugins] ( ./Plugins.md ) are available to
9
- [ routes] ( ./Routes.md ) . A visual representation of the encapsulation context
10
- is shown in the following figure:
6
+ A fundamental feature of Fastify is the "encapsulation context." It governs
7
+ which [ decorators] ( ./Decorators.md ) , registered [ hooks] ( ./Hooks.md ) , and
8
+ [ plugins] ( ./Plugins.md ) are available to [ routes] ( ./Routes.md ) . A visual
9
+ representation of the encapsulation context is shown in the following figure:
11
10
12
11
![ Figure 1] ( ../resources/encapsulation_context.svg )
13
12
14
- In the above figure, there are several entities:
13
+ In the figure above , there are several entities:
15
14
16
15
1 . The _ root context_
17
16
2 . Three _ root plugins_
18
- 3 . Two _ child contexts_ where each _ child context _ has
17
+ 3 . Two _ child contexts_ , each with:
19
18
* Two _ child plugins_
20
- * One _ grandchild context_ where each _ grandchild context _ has
19
+ * One _ grandchild context_ , each with:
21
20
- Three _ child plugins_
22
21
23
22
Every _ child context_ and _ grandchild context_ has access to the _ root plugins_ .
@@ -26,15 +25,14 @@ _child plugins_ registered within the containing _child context_, but the
26
25
containing _ child context_ ** does not** have access to the _ child plugins_
27
26
registered within its _ grandchild context_ .
28
27
29
- Given that everything in Fastify is a [ plugin] ( ./Plugins.md ) , except for the
28
+ Given that everything in Fastify is a [ plugin] ( ./Plugins.md ) except for the
30
29
_ root context_ , every "context" and "plugin" in this example is a plugin
31
- that can consist of decorators, hooks, plugins, and routes. Thus, to put
32
- this example into concrete terms, consider a basic scenario of a REST API
33
- server that has three routes: the first route (` /one ` ) requires authentication,
34
- the second route (` /two ` ) does not, and the third route (` /three ` ) has
35
- access to the same context as the second route. Using
36
- [ @fastify/bearer-auth ] [ bearer ] to provide the authentication, the code for this
37
- example is as follows:
30
+ that can consist of decorators, hooks, plugins, and routes. To put this
31
+ example into concrete terms, consider a basic scenario of a REST API server
32
+ with three routes: the first route (` /one ` ) requires authentication, the
33
+ second route (` /two ` ) does not, and the third route (` /three ` ) has access to
34
+ the same context as the second route. Using [ @fastify/bearer-auth ] [ bearer ] to
35
+ provide authentication, the code for this example is as follows:
38
36
39
37
``` js
40
38
' use strict'
@@ -52,9 +50,9 @@ fastify.register(async function authenticatedContext (childServer) {
52
50
handler (request , response ) {
53
51
response .send ({
54
52
answer: request .answer ,
55
- // request.foo will be undefined as it's only defined in publicContext
53
+ // request.foo will be undefined as it is only defined in publicContext
56
54
foo: request .foo ,
57
- // request.bar will be undefined as it's only defined in grandchildContext
55
+ // request.bar will be undefined as it is only defined in grandchildContext
58
56
bar: request .bar
59
57
})
60
58
}
@@ -71,7 +69,7 @@ fastify.register(async function publicContext (childServer) {
71
69
response .send ({
72
70
answer: request .answer ,
73
71
foo: request .foo ,
74
- // request.bar will be undefined as it's only defined in grandchildContext
72
+ // request.bar will be undefined as it is only defined in grandchildContext
75
73
bar: request .bar
76
74
})
77
75
}
@@ -97,16 +95,16 @@ fastify.register(async function publicContext (childServer) {
97
95
fastify .listen ({ port: 8000 })
98
96
```
99
97
100
- The above server example shows all of the encapsulation concepts outlined in the
98
+ The server example above demonstrates the encapsulation concepts from the
101
99
original diagram:
102
100
103
101
1 . Each _ child context_ (` authenticatedContext ` , ` publicContext ` , and
104
- ` grandchildContext ` ) has access to the ` answer ` request decorator defined in
105
- the _ root context_ .
102
+ ` grandchildContext ` ) has access to the ` answer ` request decorator defined in
103
+ the _ root context_ .
106
104
2 . Only the ` authenticatedContext ` has access to the ` @fastify/bearer-auth `
107
- plugin.
105
+ plugin.
108
106
3 . Both the ` publicContext ` and ` grandchildContext ` have access to the ` foo `
109
- request decorator.
107
+ request decorator.
110
108
4 . Only the ` grandchildContext ` has access to the ` bar ` request decorator.
111
109
112
110
To see this, start the server and issue requests:
@@ -125,16 +123,13 @@ To see this, start the server and issue requests:
125
123
## Sharing Between Contexts
126
124
<a id =" shared-context " ></a >
127
125
128
- Notice that each context in the prior example inherits _ only_ from the parent
129
- contexts. Parent contexts cannot access any entities within their descendent
130
- contexts. This default is occasionally not desired. In such cases, the
131
- encapsulation context can be broken through the usage of
132
- [ fastify-plugin] [ fastify-plugin ] such that anything registered in a descendent
133
- context is available to the containing parent context.
126
+ Each context in the prior example inherits _ only_ from its parent contexts. Parent
127
+ contexts cannot access entities within their descendant contexts. If needed,
128
+ encapsulation can be broken using [ fastify-plugin] [ fastify-plugin ] , making
129
+ anything registered in a descendant context available to the parent context.
134
130
135
- Assuming the ` publicContext ` needs access to the ` bar ` decorator defined
136
- within the ` grandchildContext ` in the previous example, the code can be
137
- rewritten as:
131
+ To allow ` publicContext ` access to the ` bar ` decorator in ` grandchildContext ` ,
132
+ rewrite the code as follows:
138
133
139
134
``` js
140
135
' use strict'
0 commit comments