@adonisjs/http-server 6.8.2-6 → 6.8.2-8

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 (208) hide show
  1. package/README.md +3 -6
  2. package/build/factories/http_context.d.ts +10 -1
  3. package/build/factories/http_context.js +27 -0
  4. package/build/factories/http_server.d.ts +8 -0
  5. package/build/factories/http_server.js +26 -0
  6. package/build/factories/main.d.ts +0 -1
  7. package/build/factories/main.js +8 -0
  8. package/build/factories/qs_parser_factory.d.ts +10 -1
  9. package/build/factories/qs_parser_factory.js +18 -0
  10. package/build/factories/request.d.ts +10 -1
  11. package/build/factories/request.js +31 -0
  12. package/build/factories/response.d.ts +10 -1
  13. package/build/factories/response.js +34 -0
  14. package/build/factories/router.d.ts +10 -1
  15. package/build/factories/router.js +25 -0
  16. package/build/factories/server_factory.d.ts +10 -1
  17. package/build/factories/server_factory.js +34 -0
  18. package/build/index.d.ts +0 -1
  19. package/build/index.js +8 -0
  20. package/build/src/cookies/client.d.ts +25 -1
  21. package/build/src/cookies/client.js +42 -0
  22. package/build/src/cookies/drivers/encrypted.d.ts +12 -1
  23. package/build/src/cookies/drivers/encrypted.js +20 -0
  24. package/build/src/cookies/drivers/plain.d.ts +12 -1
  25. package/build/src/cookies/drivers/plain.js +20 -0
  26. package/build/src/cookies/drivers/signed.d.ts +12 -1
  27. package/build/src/cookies/drivers/signed.js +20 -0
  28. package/build/src/cookies/parser.d.ts +28 -1
  29. package/build/src/cookies/parser.js +98 -0
  30. package/build/src/cookies/serializer.d.ts +22 -1
  31. package/build/src/cookies/serializer.js +40 -0
  32. package/build/src/debug.d.ts +0 -1
  33. package/build/src/debug.js +8 -0
  34. package/build/src/define_config.d.ts +3 -1
  35. package/build/src/define_config.js +11 -0
  36. package/build/src/define_middleware.d.ts +5 -1
  37. package/build/src/define_middleware.js +18 -0
  38. package/build/src/exception_handler.d.ts +65 -1
  39. package/build/src/exception_handler.js +95 -0
  40. package/build/src/exceptions.d.ts +6 -1
  41. package/build/src/exceptions.js +11 -0
  42. package/build/src/helpers.d.ts +14 -1
  43. package/build/src/helpers.js +22 -0
  44. package/build/src/http_context/local_storage.d.ts +3 -1
  45. package/build/src/http_context/local_storage.js +25 -0
  46. package/build/src/http_context/main.d.ts +36 -1
  47. package/build/src/http_context/main.js +54 -0
  48. package/build/src/qs.d.ts +4 -1
  49. package/build/src/qs.js +12 -0
  50. package/build/src/redirect.d.ts +24 -1
  51. package/build/src/redirect.js +60 -0
  52. package/build/src/request.d.ts +466 -1
  53. package/build/src/request.js +542 -0
  54. package/build/src/response.d.ts +425 -2
  55. package/build/src/response.js +608 -7
  56. package/build/src/router/brisk.d.ts +22 -1
  57. package/build/src/router/brisk.js +42 -0
  58. package/build/src/router/executor.d.ts +4 -1
  59. package/build/src/router/executor.js +12 -0
  60. package/build/src/router/factories/use_return_value.d.ts +4 -1
  61. package/build/src/router/factories/use_return_value.js +16 -3
  62. package/build/src/router/group.d.ts +47 -1
  63. package/build/src/router/group.js +88 -0
  64. package/build/src/router/lookup_store/main.d.ts +32 -1
  65. package/build/src/router/lookup_store/main.js +49 -0
  66. package/build/src/router/lookup_store/route_finder.d.ts +13 -1
  67. package/build/src/router/lookup_store/route_finder.js +21 -0
  68. package/build/src/router/lookup_store/url_builder.d.ts +36 -1
  69. package/build/src/router/lookup_store/url_builder.js +97 -0
  70. package/build/src/router/main.d.ts +87 -1
  71. package/build/src/router/main.js +142 -0
  72. package/build/src/router/matchers.d.ts +13 -1
  73. package/build/src/router/matchers.js +21 -0
  74. package/build/src/router/parser.d.ts +3 -1
  75. package/build/src/router/parser.js +12 -0
  76. package/build/src/router/resource.d.ts +28 -1
  77. package/build/src/router/resource.js +90 -0
  78. package/build/src/router/route.d.ts +65 -1
  79. package/build/src/router/route.js +151 -2
  80. package/build/src/router/store.d.ts +54 -1
  81. package/build/src/router/store.js +107 -0
  82. package/build/src/server/factories/final_handler.d.ts +5 -1
  83. package/build/src/server/factories/final_handler.js +13 -0
  84. package/build/src/server/factories/middleware_handler.d.ts +3 -1
  85. package/build/src/server/factories/middleware_handler.js +11 -0
  86. package/build/src/server/factories/write_response.d.ts +4 -1
  87. package/build/src/server/factories/write_response.js +12 -0
  88. package/build/src/server/main.d.ts +48 -1
  89. package/build/src/server/main.js +128 -0
  90. package/build/src/types/base.d.ts +12 -1
  91. package/build/src/types/base.js +8 -0
  92. package/build/src/types/main.d.ts +0 -1
  93. package/build/src/types/main.js +8 -0
  94. package/build/src/types/middleware.d.ts +18 -1
  95. package/build/src/types/middleware.js +8 -0
  96. package/build/src/types/qs.d.ts +53 -1
  97. package/build/src/types/qs.js +8 -0
  98. package/build/src/types/request.d.ts +32 -1
  99. package/build/src/types/request.js +8 -0
  100. package/build/src/types/response.d.ts +27 -1
  101. package/build/src/types/response.js +8 -0
  102. package/build/src/types/route.d.ts +87 -1
  103. package/build/src/types/route.js +8 -0
  104. package/build/src/types/server.d.ts +35 -1
  105. package/build/src/types/server.js +8 -0
  106. package/package.json +44 -71
  107. package/build/factories/http_context.d.ts.map +0 -1
  108. package/build/factories/main.d.ts.map +0 -1
  109. package/build/factories/qs_parser_factory.d.ts.map +0 -1
  110. package/build/factories/request.d.ts.map +0 -1
  111. package/build/factories/response.d.ts.map +0 -1
  112. package/build/factories/router.d.ts.map +0 -1
  113. package/build/factories/server_factory.d.ts.map +0 -1
  114. package/build/index.d.ts.map +0 -1
  115. package/build/src/cookies/client.d.ts.map +0 -1
  116. package/build/src/cookies/drivers/encrypted.d.ts.map +0 -1
  117. package/build/src/cookies/drivers/plain.d.ts.map +0 -1
  118. package/build/src/cookies/drivers/signed.d.ts.map +0 -1
  119. package/build/src/cookies/parser.d.ts.map +0 -1
  120. package/build/src/cookies/serializer.d.ts.map +0 -1
  121. package/build/src/debug.d.ts.map +0 -1
  122. package/build/src/define_config.d.ts.map +0 -1
  123. package/build/src/define_middleware.d.ts.map +0 -1
  124. package/build/src/exception_handler.d.ts.map +0 -1
  125. package/build/src/exceptions.d.ts.map +0 -1
  126. package/build/src/helpers.d.ts.map +0 -1
  127. package/build/src/http_context/local_storage.d.ts.map +0 -1
  128. package/build/src/http_context/main.d.ts.map +0 -1
  129. package/build/src/qs.d.ts.map +0 -1
  130. package/build/src/redirect.d.ts.map +0 -1
  131. package/build/src/request.d.ts.map +0 -1
  132. package/build/src/response.d.ts.map +0 -1
  133. package/build/src/router/brisk.d.ts.map +0 -1
  134. package/build/src/router/executor.d.ts.map +0 -1
  135. package/build/src/router/factories/use_return_value.d.ts.map +0 -1
  136. package/build/src/router/group.d.ts.map +0 -1
  137. package/build/src/router/lookup_store/main.d.ts.map +0 -1
  138. package/build/src/router/lookup_store/route_finder.d.ts.map +0 -1
  139. package/build/src/router/lookup_store/url_builder.d.ts.map +0 -1
  140. package/build/src/router/main.d.ts.map +0 -1
  141. package/build/src/router/matchers.d.ts.map +0 -1
  142. package/build/src/router/parser.d.ts.map +0 -1
  143. package/build/src/router/resource.d.ts.map +0 -1
  144. package/build/src/router/route.d.ts.map +0 -1
  145. package/build/src/router/store.d.ts.map +0 -1
  146. package/build/src/server/factories/final_handler.d.ts.map +0 -1
  147. package/build/src/server/factories/middleware_handler.d.ts.map +0 -1
  148. package/build/src/server/factories/write_response.d.ts.map +0 -1
  149. package/build/src/server/main.d.ts.map +0 -1
  150. package/build/src/types/base.d.ts.map +0 -1
  151. package/build/src/types/main.d.ts.map +0 -1
  152. package/build/src/types/middleware.d.ts.map +0 -1
  153. package/build/src/types/qs.d.ts.map +0 -1
  154. package/build/src/types/request.d.ts.map +0 -1
  155. package/build/src/types/response.d.ts.map +0 -1
  156. package/build/src/types/route.d.ts.map +0 -1
  157. package/build/src/types/server.d.ts.map +0 -1
  158. package/factories/http_context.ts +0 -73
  159. package/factories/main.ts +0 -15
  160. package/factories/qs_parser_factory.ts +0 -54
  161. package/factories/request.ts +0 -101
  162. package/factories/response.ts +0 -106
  163. package/factories/router.ts +0 -61
  164. package/factories/server_factory.ts +0 -94
  165. package/index.ts +0 -23
  166. package/src/cookies/client.ts +0 -98
  167. package/src/cookies/drivers/encrypted.ts +0 -42
  168. package/src/cookies/drivers/plain.ts +0 -37
  169. package/src/cookies/drivers/signed.ts +0 -42
  170. package/src/cookies/parser.ts +0 -196
  171. package/src/cookies/serializer.ts +0 -98
  172. package/src/debug.ts +0 -11
  173. package/src/define_config.ts +0 -56
  174. package/src/define_middleware.ts +0 -61
  175. package/src/exception_handler.ts +0 -290
  176. package/src/exceptions.ts +0 -55
  177. package/src/helpers.ts +0 -108
  178. package/src/http_context/local_storage.ts +0 -50
  179. package/src/http_context/main.ts +0 -126
  180. package/src/qs.ts +0 -31
  181. package/src/redirect.ts +0 -181
  182. package/src/request.ts +0 -982
  183. package/src/response.ts +0 -1421
  184. package/src/router/brisk.ts +0 -113
  185. package/src/router/executor.ts +0 -36
  186. package/src/router/factories/use_return_value.ts +0 -26
  187. package/src/router/group.ts +0 -243
  188. package/src/router/lookup_store/main.ts +0 -102
  189. package/src/router/lookup_store/route_finder.ts +0 -60
  190. package/src/router/lookup_store/url_builder.ts +0 -250
  191. package/src/router/main.ts +0 -431
  192. package/src/router/matchers.ts +0 -40
  193. package/src/router/parser.ts +0 -20
  194. package/src/router/resource.ts +0 -277
  195. package/src/router/route.ts +0 -363
  196. package/src/router/store.ts +0 -239
  197. package/src/server/factories/final_handler.ts +0 -38
  198. package/src/server/factories/middleware_handler.ts +0 -23
  199. package/src/server/factories/write_response.ts +0 -26
  200. package/src/server/main.ts +0 -356
  201. package/src/types/base.ts +0 -30
  202. package/src/types/main.ts +0 -16
  203. package/src/types/middleware.ts +0 -59
  204. package/src/types/qs.ts +0 -85
  205. package/src/types/request.ts +0 -52
  206. package/src/types/response.ts +0 -57
  207. package/src/types/route.ts +0 -217
  208. package/src/types/server.ts +0 -92
@@ -1,94 +0,0 @@
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 { Logger } from '@adonisjs/logger'
11
- import { Emitter } from '@adonisjs/events'
12
- import type { Encryption } from '@adonisjs/encryption'
13
- import type { Application } from '@adonisjs/application'
14
- import { AppFactory } from '@adonisjs/application/factories'
15
- import { EncryptionFactory } from '@adonisjs/encryption/factories'
16
-
17
- import { Server } from '../src/server/main.js'
18
- import { defineConfig } from '../src/define_config.js'
19
- import type { ServerConfig } from '../src/types/server.js'
20
-
21
- type FactoryParameters = {
22
- app: Application<any>
23
- logger: Logger
24
- encryption: Encryption
25
- emitter: Emitter<any>
26
- config: Partial<ServerConfig>
27
- }
28
-
29
- /**
30
- * Server factory is used to generate server class instances for
31
- * testing
32
- */
33
- export class ServerFactory {
34
- #parameters: Partial<FactoryParameters> = {}
35
-
36
- /**
37
- * Returns the emitter instance
38
- */
39
- #getEmitter() {
40
- return this.#parameters.emitter || new Emitter(this.#getApp())
41
- }
42
-
43
- /**
44
- * Returns the logger instance
45
- */
46
- #getLogger() {
47
- return this.#parameters.logger || new Logger({ enabled: false })
48
- }
49
-
50
- /**
51
- * Returns the config for the server class
52
- */
53
- #getConfig() {
54
- return defineConfig(this.#parameters.config || {})
55
- }
56
-
57
- /**
58
- * Returns an instance of the application class
59
- */
60
- #getApp() {
61
- return (
62
- this.#parameters.app || new AppFactory().create(new URL('./app/', import.meta.url), () => {})
63
- )
64
- }
65
-
66
- /**
67
- * Returns an instance of the encryptor to encrypt
68
- * signed URLs
69
- */
70
- #createEncryption() {
71
- return this.#parameters.encryption || new EncryptionFactory().create()
72
- }
73
-
74
- /**
75
- * Merge factory params
76
- */
77
- merge(params: Partial<FactoryParameters>) {
78
- Object.assign(this.#parameters, params)
79
- return this
80
- }
81
-
82
- /**
83
- * Create server instance
84
- */
85
- create() {
86
- return new Server(
87
- this.#getApp(),
88
- this.#createEncryption(),
89
- this.#getEmitter(),
90
- this.#getLogger(),
91
- this.#getConfig()
92
- )
93
- }
94
- }
package/index.ts DELETED
@@ -1,23 +0,0 @@
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
- export { Request } from './src/request.js'
11
- export { Response } from './src/response.js'
12
- export { Redirect } from './src/redirect.js'
13
- export { Server } from './src/server/main.js'
14
- export { Router } from './src/router/main.js'
15
- export { Route } from './src/router/route.js'
16
- export * as errors from './src/exceptions.js'
17
- export { BriskRoute } from './src/router/brisk.js'
18
- export { RouteGroup } from './src/router/group.js'
19
- export { defineConfig } from './src/define_config.js'
20
- export { CookieClient } from './src/cookies/client.js'
21
- export { HttpContext } from './src/http_context/main.js'
22
- export { RouteResource } from './src/router/resource.js'
23
- export { ExceptionHandler } from './src/exception_handler.js'
@@ -1,98 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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
- }
@@ -1,37 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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
- }
@@ -1,196 +0,0 @@
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
- }
@@ -1,98 +0,0 @@
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 DELETED
@@ -1,11 +0,0 @@
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')
@@ -1,56 +0,0 @@
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
- }