UNPKG

2.91 kBJavaScriptView Raw
1'use strict'
2
3const {
4 kFourOhFourContext,
5 kReplySerializerDefault,
6 kSchemaErrorFormatter,
7 kErrorHandler,
8 kChildLoggerFactory,
9 kOptions,
10 kReply,
11 kRequest,
12 kBodyLimit,
13 kLogLevel,
14 kContentTypeParser,
15 kRouteByFastify,
16 kRequestCacheValidateFns,
17 kReplyCacheSerializeFns,
18 kPublicRouteContext
19} = require('./symbols.js')
20
21// Object that holds the context of every request
22// Every route holds an instance of this object.
23function Context ({
24 schema,
25 handler,
26 config,
27 requestIdLogLabel,
28 childLoggerFactory,
29 errorHandler,
30 bodyLimit,
31 logLevel,
32 logSerializers,
33 attachValidation,
34 validatorCompiler,
35 serializerCompiler,
36 replySerializer,
37 schemaErrorFormatter,
38 exposeHeadRoute,
39 prefixTrailingSlash,
40 server,
41 isFastify
42}) {
43 this.schema = schema
44 this.handler = handler
45 this.Reply = server[kReply]
46 this.Request = server[kRequest]
47 this.contentTypeParser = server[kContentTypeParser]
48 this.onRequest = null
49 this.onSend = null
50 this.onError = null
51 this.onTimeout = null
52 this.preHandler = null
53 this.onResponse = null
54 this.preSerialization = null
55 this.onRequestAbort = null
56 this.config = config
57 this.errorHandler = errorHandler || server[kErrorHandler]
58 this.requestIdLogLabel = requestIdLogLabel || server[kOptions].requestIdLogLabel
59 this.childLoggerFactory = childLoggerFactory || server[kChildLoggerFactory]
60 this._middie = null
61 this._parserOptions = {
62 limit: bodyLimit || server[kBodyLimit]
63 }
64 this.exposeHeadRoute = exposeHeadRoute
65 this.prefixTrailingSlash = prefixTrailingSlash
66 this.logLevel = logLevel || server[kLogLevel]
67 this.logSerializers = logSerializers
68 this[kFourOhFourContext] = null
69 this.attachValidation = attachValidation
70 this[kReplySerializerDefault] = replySerializer
71 this.schemaErrorFormatter =
72 schemaErrorFormatter ||
73 server[kSchemaErrorFormatter] ||
74 defaultSchemaErrorFormatter
75 this[kRouteByFastify] = isFastify
76
77 this[kRequestCacheValidateFns] = null
78 this[kReplyCacheSerializeFns] = null
79 this.validatorCompiler = validatorCompiler || null
80 this.serializerCompiler = serializerCompiler || null
81
82 // Route + Userland configurations for the route
83 this[kPublicRouteContext] = getPublicRouteContext(this)
84
85 this.server = server
86}
87
88function getPublicRouteContext (context) {
89 return Object.create(null, {
90 schema: {
91 enumerable: true,
92 get () {
93 return context.schema
94 }
95 },
96 config: {
97 enumerable: true,
98 get () {
99 return context.config
100 }
101 }
102 })
103}
104
105function defaultSchemaErrorFormatter (errors, dataVar) {
106 let text = ''
107 const separator = ', '
108
109 // eslint-disable-next-line no-var
110 for (var i = 0; i !== errors.length; ++i) {
111 const e = errors[i]
112 text += dataVar + (e.instancePath || '') + ' ' + e.message + separator
113 }
114 return new Error(text.slice(0, -separator.length))
115}
116
117module.exports = Context