@pikku/core 0.6.12 → 0.6.14

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 (79) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/channel/channel-handler.d.ts +2 -2
  3. package/dist/channel/channel-handler.js +4 -4
  4. package/dist/channel/channel-runner.d.ts +5 -4
  5. package/dist/channel/channel-runner.js +8 -14
  6. package/dist/channel/channel.types.d.ts +11 -18
  7. package/dist/channel/local/local-channel-handler.d.ts +1 -3
  8. package/dist/channel/local/local-channel-handler.js +0 -3
  9. package/dist/channel/local/local-channel-runner.js +49 -33
  10. package/dist/channel/pikku-abstract-channel-handler.d.ts +4 -7
  11. package/dist/channel/pikku-abstract-channel-handler.js +1 -5
  12. package/dist/channel/serverless/serverless-channel-runner.js +45 -38
  13. package/dist/handle-error.d.ts +14 -0
  14. package/dist/handle-error.js +53 -0
  15. package/dist/http/http-route-runner.d.ts +34 -11
  16. package/dist/http/http-route-runner.js +177 -106
  17. package/dist/http/http-routes.types.d.ts +15 -9
  18. package/dist/http/index.d.ts +0 -1
  19. package/dist/http/index.js +0 -1
  20. package/dist/http/pikku-http-abstract-request.d.ts +4 -1
  21. package/dist/http/pikku-http-abstract-request.js +7 -0
  22. package/dist/index.d.ts +2 -0
  23. package/dist/index.js +2 -0
  24. package/dist/middleware/auth-api-key.d.ts +9 -0
  25. package/dist/middleware/auth-api-key.js +23 -0
  26. package/dist/middleware/auth-cookie-middleware.d.ts +11 -0
  27. package/dist/middleware/auth-cookie-middleware.js +36 -0
  28. package/dist/middleware/auth-jwt-middleware.d.ts +10 -0
  29. package/dist/middleware/auth-jwt-middleware.js +32 -0
  30. package/dist/middleware/auth-query-middleware.d.ts +10 -0
  31. package/dist/middleware/auth-query-middleware.js +21 -0
  32. package/dist/middleware/index.d.ts +4 -0
  33. package/dist/middleware/index.js +4 -0
  34. package/dist/middleware-runner.d.ts +22 -0
  35. package/dist/middleware-runner.js +28 -0
  36. package/dist/scheduler/scheduler.types.d.ts +2 -0
  37. package/dist/schema.d.ts +1 -1
  38. package/dist/schema.js +3 -6
  39. package/dist/services/user-session-service.d.ts +20 -3
  40. package/dist/services/user-session-service.js +35 -1
  41. package/dist/types/core.types.d.ts +11 -15
  42. package/dist/types/functions.types.d.ts +4 -4
  43. package/lcov.info +1572 -1729
  44. package/package.json +1 -2
  45. package/src/channel/channel-handler.ts +6 -11
  46. package/src/channel/channel-runner.ts +13 -32
  47. package/src/channel/channel.types.ts +13 -39
  48. package/src/channel/local/local-channel-handler.ts +1 -7
  49. package/src/channel/local/local-channel-runner.test.ts +2 -5
  50. package/src/channel/local/local-channel-runner.ts +72 -54
  51. package/src/channel/pikku-abstract-channel-handler.test.ts +3 -28
  52. package/src/channel/pikku-abstract-channel-handler.ts +3 -9
  53. package/src/channel/serverless/serverless-channel-runner.ts +68 -56
  54. package/src/handle-error.ts +67 -0
  55. package/src/http/http-route-runner.test.ts +12 -45
  56. package/src/http/http-route-runner.ts +256 -182
  57. package/src/http/http-routes.types.ts +15 -10
  58. package/src/http/index.ts +0 -2
  59. package/src/http/pikku-http-abstract-request.ts +8 -1
  60. package/src/index.ts +2 -0
  61. package/src/middleware/auth-api-key.ts +32 -0
  62. package/src/middleware/auth-cookie-middleware.ts +54 -0
  63. package/src/middleware/auth-jwt-middleware.ts +47 -0
  64. package/src/middleware/auth-query-middleware.ts +33 -0
  65. package/src/middleware/index.ts +4 -0
  66. package/src/middleware-runner.ts +43 -0
  67. package/src/scheduler/scheduler.types.ts +2 -0
  68. package/src/schema.ts +4 -8
  69. package/src/services/user-session-service.ts +45 -3
  70. package/src/types/core.types.ts +19 -18
  71. package/src/types/functions.types.ts +11 -7
  72. package/tsconfig.tsbuildinfo +1 -1
  73. package/dist/http/http-session-service.d.ts +0 -15
  74. package/dist/http/http-session-service.js +0 -1
  75. package/dist/http/pikku-http-session-service.d.ts +0 -45
  76. package/dist/http/pikku-http-session-service.js +0 -92
  77. package/src/http/http-session-service.ts +0 -19
  78. package/src/http/pikku-http-session-service.test.ts +0 -106
  79. package/src/http/pikku-http-session-service.ts +0 -135
@@ -1,135 +0,0 @@
1
- import { HTTPSessionService } from './http-session-service.js'
2
- import { JWTService } from '../services/jwt-service.js'
3
- import { InvalidSessionError, MissingSessionError } from '../errors/errors.js'
4
- import { PikkuHTTPAbstractRequest } from './pikku-http-abstract-request.js'
5
- import { PikkuQuery } from './http-routes.types.js'
6
-
7
- /**
8
- * The `PikkuHTTPSessionService` class provides session management capabilities, including handling JWT-based sessions,
9
- * cookie-based sessions, and API key-based sessions. It allows for retrieving and transforming user sessions based on different
10
- * authentication mechanisms.
11
- *
12
- * @template UserSession - The type representing a user session.
13
- */
14
- export class PikkuHTTPSessionService<UserSession>
15
- implements HTTPSessionService<UserSession>
16
- {
17
- /**
18
- * Constructs a new instance of the `PikkuHTTPSessionService` class.
19
- *
20
- * @param jwtService - The service for handling JWT operations.
21
- * @param options - Options for configuring the session service.
22
- */
23
- constructor(
24
- private jwtService: JWTService<UserSession>,
25
- private options: {
26
- cookieNames?: string[]
27
- getSessionForCookieValue?: (
28
- cookieValue: string,
29
- cookieName: string
30
- ) => Promise<UserSession>
31
- getSessionForQueryValue?: (
32
- queryValue: PikkuQuery
33
- ) => Promise<UserSession> | undefined
34
- getSessionForAPIKey?: (apiKey: string) => Promise<UserSession> | undefined
35
- transformSession?: (session: any) => Promise<UserSession>
36
- }
37
- ) {}
38
-
39
- /**
40
- * Retrieves a session from cookies if available.
41
- *
42
- * @param request - The request object containing cookies.
43
- * @returns A promise that resolves to the user session, or `undefined` if no session is found.
44
- */
45
- private async getCookieSession(
46
- request: PikkuHTTPAbstractRequest
47
- ): Promise<UserSession | undefined> {
48
- const cookies = request.getCookies()
49
- if (!cookies) {
50
- return
51
- }
52
-
53
- let cookieName: string | undefined
54
- if (this.options.cookieNames) {
55
- for (const name of this.options.cookieNames) {
56
- if (cookies[name]) {
57
- cookieName = name
58
- }
59
- }
60
- }
61
-
62
- if (!cookieName) {
63
- return
64
- }
65
-
66
- const cookieValue = cookies[cookieName]
67
- if (!cookieValue) {
68
- return
69
- }
70
-
71
- if (!this.options.getSessionForCookieValue) {
72
- return
73
- }
74
-
75
- return await this.options.getSessionForCookieValue(cookieValue, cookieName)
76
- }
77
-
78
- /**
79
- * Retrieves the user session based on available credentials (JWT, API key, or cookies).
80
- *
81
- * @param credentialsRequired - Whether credentials are required to proceed.
82
- * @param request - The request object containing headers and cookies.
83
- * @param debugJWTDecode - Whether to enable debugging for JWT decoding.
84
- * @returns A promise that resolves to the user session, or `undefined` if no session is found and credentials are not required.
85
- * @throws {MissingSessionError} - Throws an error if credentials are required but no session is found.
86
- */
87
- public async getUserSession(
88
- credentialsRequired: boolean,
89
- request: PikkuHTTPAbstractRequest,
90
- debugJWTDecode?: boolean
91
- ): Promise<UserSession | undefined> {
92
- let userSession: UserSession | undefined
93
-
94
- const authorization =
95
- request.getHeader('authorization') || request.getHeader('Authorization')
96
- if (authorization) {
97
- const [part1, part2] = authorization.split(' ')
98
- if (part1 !== 'Bearer' || !part2) {
99
- throw new InvalidSessionError()
100
- }
101
- userSession = await this.jwtService.decode(
102
- part2,
103
- undefined,
104
- debugJWTDecode
105
- )
106
- }
107
-
108
- if (!userSession && this.options.getSessionForAPIKey) {
109
- const apiKey = request.getHeader('x-api-key')
110
- if (apiKey) {
111
- userSession = await this.options.getSessionForAPIKey(apiKey)
112
- }
113
- }
114
-
115
- if (!userSession && this.options.getSessionForCookieValue) {
116
- userSession = await this.getCookieSession(request)
117
- }
118
-
119
- if (!userSession && this.options.getSessionForQueryValue) {
120
- userSession = await this.options.getSessionForQueryValue(
121
- request.getQuery()
122
- )
123
- }
124
-
125
- if (!userSession && credentialsRequired) {
126
- throw new MissingSessionError()
127
- }
128
-
129
- if (userSession && this.options.transformSession) {
130
- return await this.options.transformSession(userSession)
131
- }
132
-
133
- return userSession
134
- }
135
- }