@adonisjs/http-server 6.8.2-4 → 6.8.2-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.
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 +30 -26
  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,98 @@
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 type { Encryption } from '@adonisjs/encryption'
11
+
12
+ import * as plainCookiesDriver from './drivers/plain.js'
13
+ import * as signedCookiesDriver from './drivers/signed.js'
14
+ import * as encryptedCookiesDriver from './drivers/encrypted.js'
15
+
16
+ /**
17
+ * Cookie client exposes the API to parse/set AdonisJS cookies
18
+ * as a client.
19
+ */
20
+ export class CookieClient {
21
+ #encryption: Encryption
22
+
23
+ constructor(encryption: Encryption) {
24
+ this.#encryption = encryption
25
+ }
26
+
27
+ /**
28
+ * Encrypt a key value pair to be sent in the cookie header
29
+ */
30
+ encrypt(key: string, value: any): string | null {
31
+ return encryptedCookiesDriver.pack(key, value, this.#encryption)
32
+ }
33
+
34
+ /**
35
+ * Sign a key value pair to be sent in the cookie header
36
+ */
37
+ sign(key: string, value: any): string | null {
38
+ return signedCookiesDriver.pack(key, value, this.#encryption)
39
+ }
40
+
41
+ /**
42
+ * Encode a key value pair to be sent in the cookie header
43
+ */
44
+ encode(_: string, value: any): string | null {
45
+ return plainCookiesDriver.pack(value)
46
+ }
47
+
48
+ /**
49
+ * Unsign a signed cookie value
50
+ */
51
+ unsign(key: string, value: string) {
52
+ return signedCookiesDriver.canUnpack(value)
53
+ ? signedCookiesDriver.unpack(key, value, this.#encryption)
54
+ : null
55
+ }
56
+
57
+ /**
58
+ * Decrypt an encrypted cookie value
59
+ */
60
+ decrypt(key: string, value: string) {
61
+ return encryptedCookiesDriver.canUnpack(value)
62
+ ? encryptedCookiesDriver.unpack(key, value, this.#encryption)
63
+ : null
64
+ }
65
+
66
+ /**
67
+ * Decode an encoded cookie value
68
+ */
69
+ decode(_: string, value: string) {
70
+ return plainCookiesDriver.canUnpack(value) ? plainCookiesDriver.unpack(value) : null
71
+ }
72
+
73
+ /**
74
+ * Parse response cookie
75
+ */
76
+ parse(key: string, value: any) {
77
+ /**
78
+ * Unsign signed cookie
79
+ */
80
+ if (signedCookiesDriver.canUnpack(value)) {
81
+ return signedCookiesDriver.unpack(key, value, this.#encryption)
82
+ }
83
+
84
+ /**
85
+ * Decrypted encrypted cookie
86
+ */
87
+ if (encryptedCookiesDriver.canUnpack(value)) {
88
+ return encryptedCookiesDriver.unpack(key, value, this.#encryption)
89
+ }
90
+
91
+ /**
92
+ * Decode encoded cookie
93
+ */
94
+ if (plainCookiesDriver.canUnpack(value)) {
95
+ return plainCookiesDriver.unpack(value)
96
+ }
97
+ }
98
+ }
@@ -0,0 +1,42 @@
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 type { Encryption } from '@adonisjs/encryption'
11
+
12
+ /**
13
+ * Encrypt a value to be set as cookie
14
+ */
15
+ export function pack(key: string, value: any, encryption: Encryption): null | string {
16
+ if (value === undefined || value === null) {
17
+ return null
18
+ }
19
+ return `e:${encryption.encrypt(value, undefined, key)}`
20
+ }
21
+
22
+ /**
23
+ * Returns a boolean, if the unpack method from this module can attempt
24
+ * to unpack encrypted value.
25
+ */
26
+ export function canUnpack(encryptedValue: string) {
27
+ return typeof encryptedValue === 'string' && encryptedValue.substring(0, 2) === 'e:'
28
+ }
29
+
30
+ /**
31
+ * Attempts to unpack the encrypted cookie value. Returns null, when fails to do so.
32
+ * Only call this method, when `canUnpack` returns true, otherwise runtime
33
+ * exceptions can be raised.
34
+ */
35
+ export function unpack(key: string, encryptedValue: string, encryption: Encryption): null | any {
36
+ const value = encryptedValue.slice(2)
37
+ if (!value) {
38
+ return null
39
+ }
40
+
41
+ return encryption.decrypt(value, key)
42
+ }
@@ -0,0 +1,37 @@
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 { base64, MessageBuilder } from '@poppinss/utils'
11
+
12
+ /**
13
+ * Encodes a value into a base64 url encoded string to
14
+ * be set as cookie
15
+ */
16
+ export function pack(value: any): null | string {
17
+ if (value === undefined || value === null) {
18
+ return null
19
+ }
20
+ return base64.urlEncode(new MessageBuilder().build(value))
21
+ }
22
+
23
+ /**
24
+ * Returns true when this `unpack` method of this module can attempt
25
+ * to unpack the encode value.
26
+ */
27
+ export function canUnpack(encodedValue: string) {
28
+ return typeof encodedValue === 'string'
29
+ }
30
+
31
+ /**
32
+ * Attempts to unpack the value by decoding it. Make sure to call, `canUnpack`
33
+ * before calling this method
34
+ */
35
+ export function unpack(encodedValue: string): null | any {
36
+ return new MessageBuilder().verify(base64.urlDecode(encodedValue, 'utf-8', false))
37
+ }
@@ -0,0 +1,42 @@
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 type { Encryption } from '@adonisjs/encryption'
11
+
12
+ /**
13
+ * Signs a value to be shared as a cookie. The signed output has a
14
+ * hash to verify tampering with the original value
15
+ */
16
+ export function pack(key: string, value: any, encryption: Encryption): null | string {
17
+ if (value === undefined || value === null) {
18
+ return null
19
+ }
20
+ return `s:${encryption.verifier.sign(value, undefined, key)}`
21
+ }
22
+
23
+ /**
24
+ * Returns a boolean, if the unpack method from this module can attempt
25
+ * to unpack the signed value.
26
+ */
27
+ export function canUnpack(signedValue: string) {
28
+ return typeof signedValue === 'string' && signedValue.substring(0, 2) === 's:'
29
+ }
30
+
31
+ /**
32
+ * Attempts to unpack the signed value. Make sure to call `canUnpack` before
33
+ * calling this method.
34
+ */
35
+ export function unpack(key: string, signedValue: string, encryption: Encryption): null | any {
36
+ const value = signedValue.slice(2)
37
+ if (!value) {
38
+ return null
39
+ }
40
+
41
+ return encryption.verifier.unsign(value, key)
42
+ }
@@ -0,0 +1,196 @@
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 cookie from 'cookie'
11
+ import type { Encryption } from '@adonisjs/encryption'
12
+
13
+ import { CookieClient } from './client.js'
14
+
15
+ /**
16
+ * Cookie parser parses the HTTP `cookie` header and collects all cookies
17
+ * inside an object of `key-value` pair, but doesn't attempt to decrypt
18
+ * or unsign or decode the individual values.
19
+ *
20
+ * The cookie values are lazily decrypted, or unsigned to avoid unncessary
21
+ * processing, which infact can be used as a means to burden the server
22
+ * by sending too many cookies which even doesn't belongs to the
23
+ * server.
24
+ */
25
+ export class CookieParser {
26
+ #client: CookieClient
27
+
28
+ /**
29
+ * A copy of cached cookies, they are cached during a request after
30
+ * initial decoding, unsigning or decrypting.
31
+ */
32
+ #cachedCookies: {
33
+ encryptedCookies: Record<string, any>
34
+ signedCookies: Record<string, any>
35
+ plainCookies: Record<string, any>
36
+ } = {
37
+ signedCookies: {},
38
+ plainCookies: {},
39
+ encryptedCookies: {},
40
+ }
41
+
42
+ /**
43
+ * An object of key-value pair collected by parsing
44
+ * the request cookie header.
45
+ */
46
+ #cookies: Record<string, any>
47
+
48
+ constructor(cookieHeader: string, encryption: Encryption) {
49
+ this.#client = new CookieClient(encryption)
50
+ this.#cookies = this.#parse(cookieHeader)
51
+ }
52
+
53
+ /**
54
+ * Parses the request `cookie` header
55
+ */
56
+ #parse(cookieHeader?: string) {
57
+ /*
58
+ * Set to empty object when cookie header is empty string
59
+ */
60
+ if (!cookieHeader) {
61
+ return {}
62
+ }
63
+
64
+ /*
65
+ * Parse and store reference
66
+ */
67
+ return cookie.parse(cookieHeader)
68
+ }
69
+
70
+ /**
71
+ * Attempts to decode a cookie by the name. When calling this method,
72
+ * you are assuming that the cookie was just encoded in the first
73
+ * place and not signed or encrypted.
74
+ */
75
+ decode(key: string, encoded = true): any | null {
76
+ /*
77
+ * Ignore when initial value is not defined or null
78
+ */
79
+ const value = this.#cookies[key]
80
+ if (value === null || value === undefined) {
81
+ return null
82
+ }
83
+
84
+ /*
85
+ * Reference to the cache object. Mainly done to avoid typos,
86
+ * since this object is referenced a handful of times inside
87
+ * this method.
88
+ */
89
+ const cache = this.#cachedCookies.plainCookies
90
+
91
+ /*
92
+ * Return from cache, when already parsed
93
+ */
94
+ if (cache[key] !== undefined) {
95
+ return cache[key]
96
+ }
97
+
98
+ /*
99
+ * Attempt to unpack and cache it for future. The value is only
100
+ * when value it is not null.
101
+ */
102
+ const parsed = encoded ? this.#client.decode(key, value) : value
103
+ if (parsed !== null) {
104
+ cache[key] = parsed
105
+ }
106
+
107
+ return parsed
108
+ }
109
+
110
+ /**
111
+ * Attempts to unsign a cookie by the name. When calling this method,
112
+ * you are assuming that the cookie was signed in the first place.
113
+ */
114
+ unsign(key: string): null | any {
115
+ /*
116
+ * Ignore when initial value is not defined or null
117
+ */
118
+ const value = this.#cookies[key]
119
+ if (value === null || value === undefined) {
120
+ return null
121
+ }
122
+
123
+ /*
124
+ * Reference to the cache object. Mainly done to avoid typos,
125
+ * since this object is referenced a handful of times inside
126
+ * this method.
127
+ */
128
+ const cache = this.#cachedCookies.signedCookies
129
+
130
+ /*
131
+ * Return from cache, when already parsed
132
+ */
133
+ if (cache[key] !== undefined) {
134
+ return cache[key]
135
+ }
136
+
137
+ /*
138
+ * Attempt to unpack and cache it for future. The value is only
139
+ * when value it is not null.
140
+ */
141
+ const parsed = this.#client.unsign(key, value)
142
+ if (parsed !== null) {
143
+ cache[key] = parsed
144
+ }
145
+
146
+ return parsed
147
+ }
148
+
149
+ /**
150
+ * Attempts to decrypt a cookie by the name. When calling this method,
151
+ * you are assuming that the cookie was encrypted in the first place.
152
+ */
153
+ decrypt(key: string): null | any {
154
+ /*
155
+ * Ignore when initial value is not defined or null
156
+ */
157
+ const value = this.#cookies[key]
158
+ if (value === null || value === undefined) {
159
+ return null
160
+ }
161
+
162
+ /*
163
+ * Reference to the cache object. Mainly done to avoid typos,
164
+ * since this object is referenced a handful of times inside
165
+ * this method.
166
+ */
167
+ const cache = this.#cachedCookies.encryptedCookies
168
+
169
+ /*
170
+ * Return from cache, when already parsed
171
+ */
172
+ if (cache[key] !== undefined) {
173
+ return cache[key]
174
+ }
175
+
176
+ /*
177
+ * Attempt to unpack and cache it for future. The value is only
178
+ * when value it is not null.
179
+ */
180
+ const parsed = this.#client.decrypt(key, value)
181
+ if (parsed !== null) {
182
+ cache[key] = parsed
183
+ }
184
+
185
+ return parsed
186
+ }
187
+
188
+ /**
189
+ * Returns an object of cookies key-value pair. Do note, the
190
+ * cookies are not decoded, unsigned or decrypted inside this
191
+ * list.
192
+ */
193
+ list() {
194
+ return this.#cookies
195
+ }
196
+ }
@@ -0,0 +1,98 @@
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 cookie from 'cookie'
11
+ import string from '@poppinss/utils/string'
12
+ import type { Encryption } from '@adonisjs/encryption'
13
+
14
+ import { CookieClient } from './client.js'
15
+ import type { CookieOptions } from '../types/response.js'
16
+
17
+ /**
18
+ * Cookies serializer is used to serialize a value to be set on the `Set-Cookie`
19
+ * header. You can `encode`, `sign` on `encrypt` cookies using the serializer
20
+ * and then set them individually using the `set-cookie` header.
21
+ */
22
+ export class CookieSerializer {
23
+ #client: CookieClient
24
+
25
+ constructor(encryption: Encryption) {
26
+ this.#client = new CookieClient(encryption)
27
+ }
28
+
29
+ /**
30
+ * Serializes the key-value pair to a string, that can be set on the
31
+ * `Set-Cookie` header.
32
+ */
33
+ #serializeAsCookie(key: string, value: string, options?: Partial<CookieOptions>) {
34
+ /**
35
+ * Invoked expires method to get the date
36
+ */
37
+ let expires = options?.expires
38
+ if (typeof expires === 'function') {
39
+ expires = expires()
40
+ }
41
+
42
+ /**
43
+ * Parse string based max age to seconds
44
+ */
45
+ let maxAge = options?.maxAge ? string.seconds.parse(options?.maxAge) : undefined
46
+
47
+ const parsedOptions = Object.assign({}, options, { maxAge, expires })
48
+ return cookie.serialize(key, value, parsedOptions)
49
+ }
50
+
51
+ /**
52
+ * Encodes value as a plain cookie. By default, the plain value will be converted
53
+ * to a string using "JSON.stringify" method and then encoded as a base64 string.
54
+ *
55
+ * You can disable encoding of the cookie by setting `options.encoded = false`.
56
+ *
57
+ * ```ts
58
+ * serializer.encode('name', 'virk')
59
+ * ```
60
+ */
61
+ encode(
62
+ key: string,
63
+ value: any,
64
+ options?: Partial<CookieOptions & { encode: boolean }>
65
+ ): string | null {
66
+ const packedValue = options?.encode === false ? value : this.#client.encode(key, value)
67
+ if (packedValue === null || packedValue === undefined) {
68
+ return null
69
+ }
70
+
71
+ return this.#serializeAsCookie(key, packedValue, options)
72
+ }
73
+
74
+ /**
75
+ * Sign a key-value pair to a signed cookie. The signed value has a
76
+ * verification hash attached to it to detect data tampering.
77
+ */
78
+ sign(key: string, value: any, options?: Partial<CookieOptions>): string | null {
79
+ const packedValue = this.#client.sign(key, value)
80
+ if (packedValue === null) {
81
+ return null
82
+ }
83
+
84
+ return this.#serializeAsCookie(key, packedValue, options)
85
+ }
86
+
87
+ /**
88
+ * Encrypts a key-value pair to an encrypted cookie.
89
+ */
90
+ encrypt(key: string, value: any, options?: Partial<CookieOptions>): string | null {
91
+ const packedValue = this.#client.encrypt(key, value)
92
+ if (packedValue === null) {
93
+ return null
94
+ }
95
+
96
+ return this.#serializeAsCookie(key, packedValue, options)
97
+ }
98
+ }
package/src/debug.ts ADDED
@@ -0,0 +1,11 @@
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 { debuglog } from 'node:util'
11
+ export default debuglog('adonisjs:http')
@@ -0,0 +1,56 @@
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 proxyAddr from 'proxy-addr'
11
+ import string from '@poppinss/utils/string'
12
+ import type { ServerConfig } from './types/server.js'
13
+
14
+ /**
15
+ * Define configuration for the HTTP server
16
+ */
17
+ export function defineConfig(config: Partial<ServerConfig>): ServerConfig {
18
+ const normalizedConfig = {
19
+ allowMethodSpoofing: false,
20
+ trustProxy: proxyAddr.compile('loopback'),
21
+ subdomainOffset: 2,
22
+ generateRequestId: false,
23
+ useAsyncLocalStorage: false,
24
+ etag: false,
25
+ jsonpCallbackName: 'callback',
26
+ cookie: {
27
+ maxAge: '2h',
28
+ path: '/',
29
+ httpOnly: true,
30
+ secure: false,
31
+ sameSite: false,
32
+ },
33
+ qs: {
34
+ parse: {
35
+ depth: 5,
36
+ parameterLimit: 1000,
37
+ allowSparse: false,
38
+ arrayLimit: 20,
39
+ comma: true,
40
+ },
41
+ stringify: {
42
+ encode: true,
43
+ encodeValuesOnly: false,
44
+ arrayFormat: 'indices' as const,
45
+ skipNulls: false,
46
+ },
47
+ },
48
+ ...config,
49
+ }
50
+
51
+ if (normalizedConfig.cookie.maxAge) {
52
+ normalizedConfig.cookie.maxAge = string.seconds.parse(normalizedConfig.cookie.maxAge)
53
+ }
54
+
55
+ return normalizedConfig
56
+ }
@@ -0,0 +1,61 @@
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 { moduleImporter } from '@adonisjs/fold'
11
+ import type { LazyImport, UnWrapLazyImport } from './types/base.js'
12
+ import type {
13
+ GetMiddlewareArgs,
14
+ MiddlewareAsClass,
15
+ ParsedGlobalMiddleware,
16
+ } from './types/middleware.js'
17
+
18
+ /**
19
+ * Converts a middleware name and its lazy import to a factory function. The function
20
+ * can than later be used to reference the middleware with different arguments
21
+ * every time.
22
+ */
23
+ function middlewareReferenceBuilder(
24
+ name: string | number | symbol,
25
+ middleware: LazyImport<MiddlewareAsClass>
26
+ ) {
27
+ const handler = moduleImporter(middleware, 'handle').toHandleMethod()
28
+ return function (...args: any[]) {
29
+ return {
30
+ name,
31
+ args: args[0],
32
+ ...handler,
33
+ }
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Define an collection of named middleware. The collection gets converted
39
+ * into a collection of factory functions. Calling the function returns
40
+ * a reference to the executable middleware.
41
+ */
42
+ export function defineNamedMiddleware<
43
+ NamedMiddleware extends Record<string | number | symbol, LazyImport<MiddlewareAsClass>>
44
+ >(collection: NamedMiddleware) {
45
+ return Object.keys(collection).reduce(
46
+ (result, key: keyof NamedMiddleware) => {
47
+ result[key] = middlewareReferenceBuilder(key, collection[key])
48
+ return result
49
+ },
50
+ {} as {
51
+ [K in keyof NamedMiddleware]: <
52
+ Args extends GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[K]>>
53
+ >(
54
+ ...args: Args
55
+ ) => {
56
+ name: K
57
+ args: Args[0]
58
+ } & ParsedGlobalMiddleware
59
+ }
60
+ )
61
+ }