@adonisjs/http-server 6.8.2-4 → 6.8.2-5

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.
Files changed (155) hide show
  1. package/LICENSE.md +1 -1
  2. package/build/factories/http_context.d.ts +1 -0
  3. package/build/factories/http_context.d.ts.map +1 -0
  4. package/build/factories/main.d.ts +1 -0
  5. package/build/factories/main.d.ts.map +1 -0
  6. package/build/factories/qs_parser_factory.d.ts +1 -0
  7. package/build/factories/qs_parser_factory.d.ts.map +1 -0
  8. package/build/factories/request.d.ts +1 -0
  9. package/build/factories/request.d.ts.map +1 -0
  10. package/build/factories/response.d.ts +1 -0
  11. package/build/factories/response.d.ts.map +1 -0
  12. package/build/factories/router.d.ts +1 -0
  13. package/build/factories/router.d.ts.map +1 -0
  14. package/build/factories/server_factory.d.ts +1 -0
  15. package/build/factories/server_factory.d.ts.map +1 -0
  16. package/build/index.d.ts +1 -0
  17. package/build/index.d.ts.map +1 -0
  18. package/build/src/cookies/client.d.ts +1 -0
  19. package/build/src/cookies/client.d.ts.map +1 -0
  20. package/build/src/cookies/drivers/encrypted.d.ts +1 -0
  21. package/build/src/cookies/drivers/encrypted.d.ts.map +1 -0
  22. package/build/src/cookies/drivers/plain.d.ts +1 -0
  23. package/build/src/cookies/drivers/plain.d.ts.map +1 -0
  24. package/build/src/cookies/drivers/signed.d.ts +1 -0
  25. package/build/src/cookies/drivers/signed.d.ts.map +1 -0
  26. package/build/src/cookies/parser.d.ts +1 -0
  27. package/build/src/cookies/parser.d.ts.map +1 -0
  28. package/build/src/cookies/serializer.d.ts +1 -0
  29. package/build/src/cookies/serializer.d.ts.map +1 -0
  30. package/build/src/debug.d.ts +1 -0
  31. package/build/src/debug.d.ts.map +1 -0
  32. package/build/src/define_config.d.ts +1 -0
  33. package/build/src/define_config.d.ts.map +1 -0
  34. package/build/src/define_middleware.d.ts +1 -0
  35. package/build/src/define_middleware.d.ts.map +1 -0
  36. package/build/src/exception_handler.d.ts +1 -0
  37. package/build/src/exception_handler.d.ts.map +1 -0
  38. package/build/src/exceptions.d.ts +1 -0
  39. package/build/src/exceptions.d.ts.map +1 -0
  40. package/build/src/helpers.d.ts +1 -0
  41. package/build/src/helpers.d.ts.map +1 -0
  42. package/build/src/http_context/local_storage.d.ts +1 -0
  43. package/build/src/http_context/local_storage.d.ts.map +1 -0
  44. package/build/src/http_context/main.d.ts +1 -0
  45. package/build/src/http_context/main.d.ts.map +1 -0
  46. package/build/src/qs.d.ts +1 -0
  47. package/build/src/qs.d.ts.map +1 -0
  48. package/build/src/redirect.d.ts +1 -0
  49. package/build/src/redirect.d.ts.map +1 -0
  50. package/build/src/request.d.ts +1 -0
  51. package/build/src/request.d.ts.map +1 -0
  52. package/build/src/response.d.ts +1 -0
  53. package/build/src/response.d.ts.map +1 -0
  54. package/build/src/router/brisk.d.ts +1 -0
  55. package/build/src/router/brisk.d.ts.map +1 -0
  56. package/build/src/router/executor.d.ts +1 -0
  57. package/build/src/router/executor.d.ts.map +1 -0
  58. package/build/src/router/factories/use_return_value.d.ts +1 -0
  59. package/build/src/router/factories/use_return_value.d.ts.map +1 -0
  60. package/build/src/router/group.d.ts +1 -0
  61. package/build/src/router/group.d.ts.map +1 -0
  62. package/build/src/router/lookup_store/main.d.ts +1 -0
  63. package/build/src/router/lookup_store/main.d.ts.map +1 -0
  64. package/build/src/router/lookup_store/route_finder.d.ts +1 -0
  65. package/build/src/router/lookup_store/route_finder.d.ts.map +1 -0
  66. package/build/src/router/lookup_store/url_builder.d.ts +1 -0
  67. package/build/src/router/lookup_store/url_builder.d.ts.map +1 -0
  68. package/build/src/router/main.d.ts +1 -0
  69. package/build/src/router/main.d.ts.map +1 -0
  70. package/build/src/router/matchers.d.ts +1 -0
  71. package/build/src/router/matchers.d.ts.map +1 -0
  72. package/build/src/router/parser.d.ts +1 -0
  73. package/build/src/router/parser.d.ts.map +1 -0
  74. package/build/src/router/resource.d.ts +1 -0
  75. package/build/src/router/resource.d.ts.map +1 -0
  76. package/build/src/router/route.d.ts +1 -0
  77. package/build/src/router/route.d.ts.map +1 -0
  78. package/build/src/router/store.d.ts +1 -0
  79. package/build/src/router/store.d.ts.map +1 -0
  80. package/build/src/server/factories/final_handler.d.ts +1 -0
  81. package/build/src/server/factories/final_handler.d.ts.map +1 -0
  82. package/build/src/server/factories/middleware_handler.d.ts +1 -0
  83. package/build/src/server/factories/middleware_handler.d.ts.map +1 -0
  84. package/build/src/server/factories/write_response.d.ts +1 -0
  85. package/build/src/server/factories/write_response.d.ts.map +1 -0
  86. package/build/src/server/main.d.ts +1 -0
  87. package/build/src/server/main.d.ts.map +1 -0
  88. package/build/src/types/base.d.ts +1 -0
  89. package/build/src/types/base.d.ts.map +1 -0
  90. package/build/src/types/main.d.ts +1 -0
  91. package/build/src/types/main.d.ts.map +1 -0
  92. package/build/src/types/middleware.d.ts +1 -0
  93. package/build/src/types/middleware.d.ts.map +1 -0
  94. package/build/src/types/qs.d.ts +1 -0
  95. package/build/src/types/qs.d.ts.map +1 -0
  96. package/build/src/types/request.d.ts +1 -0
  97. package/build/src/types/request.d.ts.map +1 -0
  98. package/build/src/types/response.d.ts +1 -0
  99. package/build/src/types/response.d.ts.map +1 -0
  100. package/build/src/types/route.d.ts +1 -0
  101. package/build/src/types/route.d.ts.map +1 -0
  102. package/build/src/types/server.d.ts +1 -0
  103. package/build/src/types/server.d.ts.map +1 -0
  104. package/factories/http_context.ts +73 -0
  105. package/factories/main.ts +15 -0
  106. package/factories/qs_parser_factory.ts +54 -0
  107. package/factories/request.ts +101 -0
  108. package/factories/response.ts +106 -0
  109. package/factories/router.ts +61 -0
  110. package/factories/server_factory.ts +94 -0
  111. package/index.ts +23 -0
  112. package/package.json +27 -23
  113. package/src/cookies/client.ts +98 -0
  114. package/src/cookies/drivers/encrypted.ts +42 -0
  115. package/src/cookies/drivers/plain.ts +37 -0
  116. package/src/cookies/drivers/signed.ts +42 -0
  117. package/src/cookies/parser.ts +196 -0
  118. package/src/cookies/serializer.ts +98 -0
  119. package/src/debug.ts +11 -0
  120. package/src/define_config.ts +56 -0
  121. package/src/define_middleware.ts +61 -0
  122. package/src/exception_handler.ts +290 -0
  123. package/src/exceptions.ts +55 -0
  124. package/src/helpers.ts +108 -0
  125. package/src/http_context/local_storage.ts +50 -0
  126. package/src/http_context/main.ts +126 -0
  127. package/src/qs.ts +31 -0
  128. package/src/redirect.ts +181 -0
  129. package/src/request.ts +982 -0
  130. package/src/response.ts +1421 -0
  131. package/src/router/brisk.ts +113 -0
  132. package/src/router/executor.ts +36 -0
  133. package/src/router/factories/use_return_value.ts +26 -0
  134. package/src/router/group.ts +243 -0
  135. package/src/router/lookup_store/main.ts +102 -0
  136. package/src/router/lookup_store/route_finder.ts +60 -0
  137. package/src/router/lookup_store/url_builder.ts +250 -0
  138. package/src/router/main.ts +431 -0
  139. package/src/router/matchers.ts +40 -0
  140. package/src/router/parser.ts +20 -0
  141. package/src/router/resource.ts +277 -0
  142. package/src/router/route.ts +363 -0
  143. package/src/router/store.ts +239 -0
  144. package/src/server/factories/final_handler.ts +38 -0
  145. package/src/server/factories/middleware_handler.ts +23 -0
  146. package/src/server/factories/write_response.ts +26 -0
  147. package/src/server/main.ts +356 -0
  148. package/src/types/base.ts +30 -0
  149. package/src/types/main.ts +16 -0
  150. package/src/types/middleware.ts +59 -0
  151. package/src/types/qs.ts +85 -0
  152. package/src/types/request.ts +52 -0
  153. package/src/types/response.ts +57 -0
  154. package/src/types/route.ts +217 -0
  155. package/src/types/server.ts +92 -0
@@ -0,0 +1,181 @@
1
+ /*
2
+ * @adonisjs/http-server
3
+ *
4
+ * (c) AdonisJS
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+
10
+ import { parse } from 'node:url'
11
+ import encodeUrl from 'encodeurl'
12
+ import type { IncomingMessage } from 'node:http'
13
+
14
+ import debug from './debug.js'
15
+ import type { Qs } from './qs.js'
16
+ import type { Response } from './response.js'
17
+ import type { Router } from './router/main.js'
18
+ import type { MakeUrlOptions } from './types/route.js'
19
+
20
+ /**
21
+ * Exposes the API to construct redirect routes
22
+ */
23
+ export class Redirect {
24
+ /**
25
+ * A boolean to forward the existing query string
26
+ */
27
+ #forwardQueryString = false
28
+
29
+ /**
30
+ * The status code for the redirect
31
+ */
32
+ #statusCode = 302
33
+
34
+ /**
35
+ * A custom query string to forward
36
+ */
37
+ #queryString: Record<string, any> = {}
38
+
39
+ #request: IncomingMessage
40
+ #response: Response
41
+ #router: Router
42
+ #qs: Qs
43
+
44
+ constructor(request: IncomingMessage, response: Response, router: Router, qs: Qs) {
45
+ this.#request = request
46
+ this.#response = response
47
+ this.#router = router
48
+ this.#qs = qs
49
+ }
50
+
51
+ /**
52
+ * Sends response by setting require headers
53
+ */
54
+ #sendResponse(url: string, query: Record<string, any>) {
55
+ const stringified = this.#qs.stringify(query)
56
+
57
+ url = stringified ? `${url}?${stringified}` : url
58
+ debug('redirecting to url "%s"', url)
59
+
60
+ this.#response.location(encodeUrl(url))
61
+ this.#response.safeStatus(this.#statusCode)
62
+ this.#response.type('text/plain; charset=utf-8')
63
+ this.#response.send(`Redirecting to ${url}`)
64
+ }
65
+
66
+ /**
67
+ * Returns the referrer url
68
+ */
69
+ #getReferrerUrl(): string {
70
+ let url = this.#request.headers['referer'] || this.#request.headers['referrer'] || '/'
71
+ return Array.isArray(url) ? url[0] : url
72
+ }
73
+
74
+ /**
75
+ * Set a custom status code.
76
+ */
77
+ status(statusCode: number): this {
78
+ this.#statusCode = statusCode
79
+ return this
80
+ }
81
+
82
+ /**
83
+ * Clearing query string values added using the
84
+ * "withQs" method
85
+ */
86
+ clearQs(): this {
87
+ this.#forwardQueryString = false
88
+ this.#queryString = {}
89
+ return this
90
+ }
91
+
92
+ /**
93
+ * Define query string for the redirect. Not passing
94
+ * any value will forward the current request query
95
+ * string.
96
+ */
97
+ withQs(): this
98
+ withQs(values: Record<string, any>): this
99
+ withQs(name: string, value: any): this
100
+ withQs(name?: Record<string, any> | string, value?: any): this {
101
+ if (typeof name === 'undefined') {
102
+ this.#forwardQueryString = true
103
+ return this
104
+ }
105
+
106
+ if (typeof name === 'string') {
107
+ this.#queryString[name] = value
108
+ return this
109
+ }
110
+
111
+ Object.assign(this.#queryString, name)
112
+ return this
113
+ }
114
+
115
+ /**
116
+ * Redirect to the previous path.
117
+ */
118
+ back() {
119
+ let query: Record<string, any> = {}
120
+
121
+ const referrerUrl = this.#getReferrerUrl()
122
+ const url = parse(referrerUrl)
123
+
124
+ debug('referrer url "%s"', referrerUrl)
125
+ debug('referrer base url "%s"', url.pathname)
126
+
127
+ /**
128
+ * Parse query string from the referrer url
129
+ */
130
+ if (this.#forwardQueryString) {
131
+ query = this.#qs.parse(url.query || '')
132
+ }
133
+
134
+ /**
135
+ * Append custom query string
136
+ */
137
+ Object.assign(query, this.#queryString)
138
+
139
+ /**
140
+ * Redirect
141
+ */
142
+ this.#sendResponse(url.pathname || '', query)
143
+ }
144
+
145
+ /**
146
+ * Redirect the request using a route identifier.
147
+ */
148
+ toRoute(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions) {
149
+ if (options && options.qs) {
150
+ this.withQs(options.qs)
151
+ options.qs = undefined
152
+ }
153
+
154
+ const url = this.#router.makeUrl(routeIdentifier, params, options)
155
+ return this.toPath(url)
156
+ }
157
+
158
+ /**
159
+ * Redirect the request using a path.
160
+ */
161
+ toPath(url: string) {
162
+ let query: Record<string, any> = {}
163
+
164
+ /**
165
+ * Extract query string from the current URL
166
+ */
167
+ if (this.#forwardQueryString) {
168
+ query = this.#qs.parse(parse(this.#request.url!).query || '')
169
+ }
170
+
171
+ /**
172
+ * Assign custom query string
173
+ */
174
+ Object.assign(query, this.#queryString)
175
+
176
+ /**
177
+ * Redirect
178
+ */
179
+ this.#sendResponse(url, query)
180
+ }
181
+ }