@pikku/core 0.9.11 → 0.10.0
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.
- package/CHANGELOG.md +14 -0
- package/dist/errors/errors.d.ts +2 -0
- package/dist/errors/errors.js +6 -0
- package/dist/function/function-runner.d.ts +15 -9
- package/dist/function/function-runner.js +55 -37
- package/dist/function/functions.types.d.ts +67 -1
- package/dist/function/functions.types.js +43 -1
- package/dist/index.d.ts +2 -6
- package/dist/index.js +2 -6
- package/dist/middleware/auth-apikey.d.ts +19 -11
- package/dist/middleware/auth-apikey.js +33 -33
- package/dist/middleware/auth-bearer.d.ts +34 -7
- package/dist/middleware/auth-bearer.js +53 -36
- package/dist/middleware/auth-cookie.d.ts +30 -12
- package/dist/middleware/auth-cookie.js +51 -43
- package/dist/middleware/timeout.d.ts +6 -2
- package/dist/middleware/timeout.js +7 -9
- package/dist/middleware-runner.d.ts +46 -43
- package/dist/middleware-runner.js +100 -69
- package/dist/permissions.d.ts +82 -20
- package/dist/permissions.js +176 -62
- package/dist/pikku-state.d.ts +46 -1
- package/dist/pikku-state.js +16 -1
- package/dist/services/content-service.d.ts +2 -3
- package/dist/services/index.d.ts +0 -1
- package/dist/services/index.js +0 -1
- package/dist/services/local-content.d.ts +2 -4
- package/dist/types/core.types.d.ts +110 -4
- package/dist/types/core.types.js +44 -1
- package/dist/utils.d.ts +1 -1
- package/dist/wirings/channel/channel-handler.d.ts +2 -2
- package/dist/wirings/channel/channel-handler.js +16 -12
- package/dist/wirings/channel/channel-runner.d.ts +2 -2
- package/dist/wirings/channel/channel-runner.js +6 -24
- package/dist/wirings/channel/channel.types.d.ts +15 -14
- package/dist/wirings/channel/local/local-channel-runner.js +12 -12
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +7 -8
- package/dist/wirings/cli/channel/cli-channel-runner.d.ts +12 -0
- package/dist/wirings/cli/channel/cli-channel-runner.js +83 -0
- package/dist/wirings/cli/channel/index.d.ts +1 -0
- package/dist/wirings/cli/channel/index.js +1 -0
- package/dist/wirings/cli/cli-runner.d.ts +42 -0
- package/dist/wirings/cli/cli-runner.js +352 -0
- package/dist/wirings/cli/cli.types.d.ts +204 -0
- package/dist/wirings/cli/cli.types.js +1 -0
- package/dist/wirings/cli/command-parser.d.ts +19 -0
- package/dist/wirings/cli/command-parser.js +403 -0
- package/dist/wirings/cli/index.d.ts +5 -0
- package/dist/wirings/cli/index.js +5 -0
- package/dist/wirings/http/http-runner.d.ts +59 -10
- package/dist/wirings/http/http-runner.js +138 -131
- package/dist/wirings/http/http.types.d.ts +11 -10
- package/dist/wirings/http/index.d.ts +1 -1
- package/dist/wirings/http/index.js +1 -1
- package/dist/wirings/http/pikku-fetch-http-response.js +3 -1
- package/dist/wirings/http/routers/http-router.d.ts +0 -2
- package/dist/wirings/http/routers/path-to-regex.d.ts +1 -1
- package/dist/wirings/http/routers/path-to-regex.js +5 -34
- package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
- package/dist/wirings/mcp/mcp-runner.d.ts +4 -5
- package/dist/wirings/mcp/mcp-runner.js +32 -34
- package/dist/wirings/mcp/mcp.types.d.ts +38 -14
- package/dist/wirings/queue/queue-runner.d.ts +2 -2
- package/dist/wirings/queue/queue-runner.js +25 -27
- package/dist/wirings/queue/queue.types.d.ts +6 -5
- package/dist/wirings/rpc/index.d.ts +1 -1
- package/dist/wirings/rpc/index.js +1 -1
- package/dist/wirings/rpc/rpc-runner.d.ts +6 -18
- package/dist/wirings/rpc/rpc-runner.js +13 -8
- package/dist/wirings/scheduler/scheduler-runner.d.ts +2 -2
- package/dist/wirings/scheduler/scheduler-runner.js +38 -35
- package/dist/wirings/scheduler/scheduler.types.d.ts +5 -4
- package/package.json +9 -7
- package/src/errors/errors.ts +6 -0
- package/src/factory-functions.test.ts +60 -1
- package/src/function/function-runner.test.ts +81 -26
- package/src/function/function-runner.ts +89 -57
- package/src/function/functions.types.ts +85 -2
- package/src/index.ts +6 -19
- package/src/middleware/auth-apikey.ts +42 -57
- package/src/middleware/auth-bearer.ts +66 -49
- package/src/middleware/auth-cookie.ts +63 -82
- package/src/middleware/timeout.ts +10 -14
- package/src/middleware-runner.test.ts +24 -16
- package/src/middleware-runner.ts +115 -87
- package/src/permissions.test.ts +26 -27
- package/src/permissions.ts +216 -104
- package/src/pikku-state.ts +79 -2
- package/src/services/content-service.ts +5 -4
- package/src/services/index.ts +0 -1
- package/src/services/local-content.ts +9 -4
- package/src/types/core.types.ts +143 -5
- package/src/utils.ts +1 -1
- package/src/wirings/channel/channel-handler.ts +18 -21
- package/src/wirings/channel/channel-runner.ts +19 -32
- package/src/wirings/channel/channel.types.ts +29 -16
- package/src/wirings/channel/local/local-channel-runner.ts +25 -15
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +15 -17
- package/src/wirings/cli/channel/cli-channel-runner.ts +123 -0
- package/src/wirings/cli/channel/index.ts +1 -0
- package/src/wirings/cli/cli-runner.test.ts +403 -0
- package/src/wirings/cli/cli-runner.ts +539 -0
- package/src/wirings/cli/cli.types.ts +350 -0
- package/src/wirings/cli/command-parser.test.ts +445 -0
- package/src/wirings/cli/command-parser.ts +504 -0
- package/src/wirings/cli/index.ts +13 -0
- package/src/wirings/http/http-runner.test.ts +8 -7
- package/src/wirings/http/http-runner.ts +165 -158
- package/src/wirings/http/http.types.ts +58 -11
- package/src/wirings/http/index.ts +7 -1
- package/src/wirings/http/pikku-fetch-http-response.ts +3 -1
- package/src/wirings/http/routers/http-router.ts +0 -2
- package/src/wirings/http/routers/path-to-regex.test.ts +4 -5
- package/src/wirings/http/routers/path-to-regex.ts +6 -43
- package/src/wirings/mcp/mcp-endpoint-registry.ts +1 -1
- package/src/wirings/mcp/mcp-runner.ts +58 -55
- package/src/wirings/mcp/mcp.types.ts +65 -14
- package/src/wirings/queue/queue-runner.ts +44 -47
- package/src/wirings/queue/queue.types.ts +10 -6
- package/src/wirings/rpc/index.ts +1 -1
- package/src/wirings/rpc/rpc-runner.ts +27 -9
- package/src/wirings/scheduler/scheduler-runner.ts +58 -56
- package/src/wirings/scheduler/scheduler.types.ts +10 -3
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,66 +1,51 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CoreConfig,
|
|
3
|
-
CoreSingletonServices,
|
|
4
|
-
CoreUserSession,
|
|
5
|
-
CorePikkuMiddleware,
|
|
6
|
-
} from '../types/core.types.js'
|
|
1
|
+
import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js'
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
|
-
* API key middleware that retrieves a session from the 'x-api-key' header using
|
|
4
|
+
* API key middleware that retrieves a session from the 'x-api-key' header using JWT decoding.
|
|
10
5
|
*
|
|
11
|
-
*
|
|
6
|
+
* Extracts API key from either the 'x-api-key' header or 'apiKey' query parameter
|
|
7
|
+
* and decodes it using the JWT service.
|
|
8
|
+
*
|
|
9
|
+
* @param options.source - Where to look for the API key: 'header', 'query', or 'all'
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { authAPIKey } from '@pikku/core/middleware'
|
|
14
|
+
*
|
|
15
|
+
* addHTTPMiddleware([
|
|
16
|
+
* authAPIKey({ source: 'header' })
|
|
17
|
+
* ])
|
|
18
|
+
* ```
|
|
12
19
|
*/
|
|
13
|
-
export const authAPIKey = <
|
|
14
|
-
SingletonServices extends CoreSingletonServices<CoreConfig>,
|
|
15
|
-
UserSession extends CoreUserSession,
|
|
16
|
-
>({
|
|
17
|
-
source,
|
|
18
|
-
getSessionForAPIKey,
|
|
19
|
-
jwt,
|
|
20
|
-
}: {
|
|
20
|
+
export const authAPIKey = pikkuMiddlewareFactory<{
|
|
21
21
|
source: 'header' | 'query' | 'all'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
jwt
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
jwt?: false
|
|
33
|
-
}
|
|
34
|
-
)) => {
|
|
35
|
-
const middleware: CorePikkuMiddleware = async (services, { http }, next) => {
|
|
36
|
-
const { userSession: userSessionService, jwt: jwtService } = services as any
|
|
37
|
-
if (!http?.request || userSessionService.get()) {
|
|
38
|
-
return next()
|
|
39
|
-
}
|
|
22
|
+
}>(({ source }) =>
|
|
23
|
+
pikkuMiddleware(
|
|
24
|
+
async (
|
|
25
|
+
{ userSession: userSessionService, jwt: jwtService },
|
|
26
|
+
{ http },
|
|
27
|
+
next
|
|
28
|
+
) => {
|
|
29
|
+
if (!http?.request || userSessionService.get()) {
|
|
30
|
+
return next()
|
|
31
|
+
}
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
if (!apiKey && (source === 'query' || source === 'all')) {
|
|
46
|
-
apiKey = http.request.query().apiKey as string | null
|
|
47
|
-
}
|
|
48
|
-
if (apiKey) {
|
|
49
|
-
let userSession: UserSession | null = null
|
|
50
|
-
if (jwt) {
|
|
51
|
-
if (!jwtService) {
|
|
52
|
-
throw new Error('JWT service is required for JWT decoding.')
|
|
53
|
-
}
|
|
54
|
-
userSession = await jwtService.decode(apiKey)
|
|
55
|
-
} else {
|
|
56
|
-
userSession = await getSessionForAPIKey!(services as any, apiKey)
|
|
33
|
+
let apiKey: string | null = null
|
|
34
|
+
if (source === 'header' || source === 'all') {
|
|
35
|
+
apiKey = http.request.header('x-api-key') as string | null
|
|
57
36
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
37
|
+
if (!apiKey && (source === 'query' || source === 'all')) {
|
|
38
|
+
apiKey = http.request.query().apiKey as string | null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (apiKey && jwtService) {
|
|
42
|
+
const userSession = await jwtService.decode(apiKey)
|
|
43
|
+
if (userSession) {
|
|
44
|
+
userSessionService.setInitial(userSession)
|
|
45
|
+
}
|
|
60
46
|
}
|
|
61
|
-
}
|
|
62
|
-
return next()
|
|
63
|
-
}
|
|
64
47
|
|
|
65
|
-
|
|
66
|
-
}
|
|
48
|
+
return next()
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
)
|
|
@@ -1,66 +1,83 @@
|
|
|
1
1
|
import { InvalidSessionError } from '../errors/errors.js'
|
|
2
2
|
import {
|
|
3
|
-
CoreConfig,
|
|
4
|
-
CoreSingletonServices,
|
|
5
3
|
CoreUserSession,
|
|
6
|
-
|
|
4
|
+
pikkuMiddleware,
|
|
5
|
+
pikkuMiddlewareFactory,
|
|
7
6
|
} from '../types/core.types.js'
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* Bearer token middleware that extracts and validates tokens from the Authorization header.
|
|
10
|
+
*
|
|
11
|
+
* Supports two modes:
|
|
12
|
+
* - JWT decoding (default): Decodes bearer tokens using the JWT service
|
|
13
|
+
* - Static token: Provide a `token` object with a value and userSession for simple token validation
|
|
14
|
+
*
|
|
15
|
+
* @param options.token - Optional static token configuration { value: string, userSession: CoreUserSession }
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { authBearer } from '@pikku/core/middleware'
|
|
20
|
+
*
|
|
21
|
+
* // JWT mode (default)
|
|
22
|
+
* addHTTPMiddleware('*', [
|
|
23
|
+
* authBearer()
|
|
24
|
+
* ])
|
|
25
|
+
*
|
|
26
|
+
* // Static token mode
|
|
27
|
+
* addHTTPMiddleware('*', [
|
|
28
|
+
* authBearer({
|
|
29
|
+
* token: {
|
|
30
|
+
* value: process.env.API_TOKEN,
|
|
31
|
+
* userSession: { userId: 'system', role: 'admin' }
|
|
32
|
+
* }
|
|
33
|
+
* })
|
|
34
|
+
* ])
|
|
35
|
+
* ```
|
|
11
36
|
*/
|
|
12
|
-
export const authBearer = <
|
|
13
|
-
SingletonServices extends CoreSingletonServices<CoreConfig>,
|
|
14
|
-
UserSession extends CoreUserSession,
|
|
15
|
-
>({
|
|
16
|
-
token,
|
|
17
|
-
jwt,
|
|
18
|
-
getSession,
|
|
19
|
-
}: {
|
|
37
|
+
export const authBearer = pikkuMiddlewareFactory<{
|
|
20
38
|
token?: {
|
|
21
39
|
value: string
|
|
22
|
-
userSession:
|
|
40
|
+
userSession: CoreUserSession
|
|
23
41
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return next()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const authHeader =
|
|
38
|
-
http.request.header('authorization') ||
|
|
39
|
-
http.request.header('Authorization')
|
|
40
|
-
if (authHeader) {
|
|
41
|
-
const [scheme, bearerToken] = authHeader.split(' ')
|
|
42
|
-
if (scheme !== 'Bearer' || !token || !bearerToken) {
|
|
43
|
-
throw new InvalidSessionError()
|
|
42
|
+
}>(({ token } = {}) =>
|
|
43
|
+
pikkuMiddleware(
|
|
44
|
+
async (
|
|
45
|
+
{ userSession: userSessionService, jwt: jwtService },
|
|
46
|
+
{ http },
|
|
47
|
+
next
|
|
48
|
+
) => {
|
|
49
|
+
// Skip if session already exists.
|
|
50
|
+
if (!http?.request || userSessionService.get()) {
|
|
51
|
+
return next()
|
|
44
52
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
|
|
54
|
+
const authHeader =
|
|
55
|
+
http.request.header('authorization') ||
|
|
56
|
+
http.request.header('Authorization')
|
|
57
|
+
|
|
58
|
+
if (authHeader) {
|
|
59
|
+
const [scheme, bearerToken] = authHeader.split(' ')
|
|
60
|
+
if (scheme !== 'Bearer' || !bearerToken) {
|
|
61
|
+
throw new InvalidSessionError()
|
|
49
62
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
|
|
64
|
+
let userSession: CoreUserSession | null = null
|
|
65
|
+
|
|
66
|
+
// If static token provided, validate against it
|
|
67
|
+
if (token && bearerToken === token.value) {
|
|
53
68
|
userSession = token.userSession
|
|
54
69
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
// Otherwise, default to JWT decoding
|
|
71
|
+
else if (jwtService && !token) {
|
|
72
|
+
userSession = await jwtService.decode(bearerToken)
|
|
73
|
+
}
|
|
58
74
|
|
|
59
|
-
|
|
60
|
-
|
|
75
|
+
if (userSession) {
|
|
76
|
+
userSessionService.setInitial(userSession)
|
|
77
|
+
}
|
|
61
78
|
}
|
|
79
|
+
|
|
80
|
+
return next()
|
|
62
81
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return middleware
|
|
66
|
-
}
|
|
82
|
+
)
|
|
83
|
+
)
|
|
@@ -1,103 +1,84 @@
|
|
|
1
1
|
import { SerializeOptions } from 'cookie'
|
|
2
|
-
import {
|
|
3
|
-
CoreConfig,
|
|
4
|
-
CoreSingletonServices,
|
|
5
|
-
CoreUserSession,
|
|
6
|
-
CorePikkuMiddleware,
|
|
7
|
-
} from '../types/core.types.js'
|
|
2
|
+
import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js'
|
|
8
3
|
import {
|
|
9
4
|
getRelativeTimeOffsetFromNow,
|
|
10
5
|
RelativeTimeInput,
|
|
11
6
|
} from '../time-utils.js'
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
|
-
* Cookie middleware that
|
|
9
|
+
* Cookie-based session middleware that uses JWT for encoding/decoding session data.
|
|
10
|
+
*
|
|
11
|
+
* Reads session from a cookie on incoming requests and automatically updates the cookie
|
|
12
|
+
* when the session changes (e.g., after login).
|
|
13
|
+
*
|
|
14
|
+
* @param options.name - Cookie name
|
|
15
|
+
* @param options.expiresIn - Cookie expiration time (e.g., { value: 30, unit: 'day' })
|
|
16
|
+
* @param options.options - Cookie serialization options (httpOnly, secure, sameSite, etc.)
|
|
15
17
|
*
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { authCookie } from '@pikku/core/middleware'
|
|
21
|
+
*
|
|
22
|
+
* addHTTPMiddleware([
|
|
23
|
+
* authCookie({
|
|
24
|
+
* name: 'session',
|
|
25
|
+
* expiresIn: { value: 30, unit: 'day' },
|
|
26
|
+
* options: {
|
|
27
|
+
* httpOnly: true,
|
|
28
|
+
* secure: true,
|
|
29
|
+
* sameSite: 'strict',
|
|
30
|
+
* path: '/'
|
|
31
|
+
* }
|
|
32
|
+
* })
|
|
33
|
+
* ])
|
|
34
|
+
* ```
|
|
18
35
|
*/
|
|
19
|
-
export const authCookie = <
|
|
20
|
-
SingletonServices extends CoreSingletonServices<CoreConfig>,
|
|
21
|
-
UserSession extends CoreUserSession,
|
|
22
|
-
>({
|
|
23
|
-
name,
|
|
24
|
-
getSessionForCookieValue,
|
|
25
|
-
jwt,
|
|
26
|
-
options,
|
|
27
|
-
expiresIn,
|
|
28
|
-
}: {
|
|
36
|
+
export const authCookie = pikkuMiddlewareFactory<{
|
|
29
37
|
name: string
|
|
30
38
|
options: SerializeOptions
|
|
31
39
|
expiresIn: RelativeTimeInput
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
getSessionForCookieValue?: undefined
|
|
43
|
-
jwt: true
|
|
44
|
-
}
|
|
45
|
-
)): CorePikkuMiddleware => {
|
|
46
|
-
const middleware: CorePikkuMiddleware = async (services, { http }, next) => {
|
|
47
|
-
const {
|
|
48
|
-
userSession: userSessionService,
|
|
49
|
-
jwt: jwtService,
|
|
50
|
-
logger,
|
|
51
|
-
} = services as any
|
|
52
|
-
if (!http?.request || userSessionService.get()) {
|
|
53
|
-
return next()
|
|
54
|
-
}
|
|
40
|
+
}>(({ name, options, expiresIn }) =>
|
|
41
|
+
pikkuMiddleware(
|
|
42
|
+
async (
|
|
43
|
+
{ userSession: userSessionService, jwt: jwtService, logger },
|
|
44
|
+
{ http },
|
|
45
|
+
next
|
|
46
|
+
) => {
|
|
47
|
+
if (!http?.request || userSessionService.get()) {
|
|
48
|
+
return next()
|
|
49
|
+
}
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
51
|
+
// Try to decode session from cookie
|
|
52
|
+
const cookieValue = http.request.cookie(name)
|
|
53
|
+
if (cookieValue && jwtService) {
|
|
54
|
+
const userSession = await jwtService.decode(cookieValue)
|
|
55
|
+
if (userSession) {
|
|
56
|
+
userSessionService.setInitial(userSession)
|
|
62
57
|
}
|
|
63
|
-
userSession = await jwtService.decode(cookieValue)
|
|
64
|
-
} else if (getSessionForCookieValue) {
|
|
65
|
-
userSession = await getSessionForCookieValue(
|
|
66
|
-
services as any,
|
|
67
|
-
cookieValue,
|
|
68
|
-
name
|
|
69
|
-
)
|
|
70
58
|
}
|
|
71
|
-
}
|
|
72
59
|
|
|
73
|
-
|
|
74
|
-
userSessionService.setInitial(userSession)
|
|
75
|
-
}
|
|
76
|
-
await next()
|
|
60
|
+
await next()
|
|
77
61
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
// Set the cookie in the response if the session has changed
|
|
63
|
+
if (!http?.response) {
|
|
64
|
+
return
|
|
65
|
+
}
|
|
82
66
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
67
|
+
if (userSessionService.sessionChanged) {
|
|
68
|
+
const session = userSessionService.get()
|
|
69
|
+
if (jwtService) {
|
|
70
|
+
http.response.cookie(
|
|
71
|
+
name,
|
|
72
|
+
await jwtService.encode(expiresIn, session),
|
|
73
|
+
{
|
|
74
|
+
...options,
|
|
75
|
+
expires: getRelativeTimeOffsetFromNow(expiresIn),
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
} else {
|
|
79
|
+
logger.warn('No JWT service available, unable to set cookie')
|
|
88
80
|
}
|
|
89
|
-
http.response.cookie(
|
|
90
|
-
name,
|
|
91
|
-
await jwtService.encode(expiresIn, session),
|
|
92
|
-
{
|
|
93
|
-
...options,
|
|
94
|
-
expires: getRelativeTimeOffsetFromNow(expiresIn),
|
|
95
|
-
}
|
|
96
|
-
)
|
|
97
|
-
} else {
|
|
98
|
-
logger.warn('No JWT service available, unable to set cookie')
|
|
99
81
|
}
|
|
100
82
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
83
|
+
)
|
|
84
|
+
)
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import { RequestTimeoutError } from '../errors/errors.js'
|
|
2
|
-
import {
|
|
2
|
+
import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js'
|
|
3
3
|
|
|
4
|
-
export const timeout = (
|
|
5
|
-
|
|
6
|
-
_services,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
setTimeout(() => {
|
|
11
|
-
throw new RequestTimeoutError()
|
|
12
|
-
}, timeout)
|
|
4
|
+
export const timeout = () =>
|
|
5
|
+
pikkuMiddlewareFactory<{ timeout: number }>(({ timeout }) =>
|
|
6
|
+
pikkuMiddleware(async (_services, _interaction, next) => {
|
|
7
|
+
setTimeout(() => {
|
|
8
|
+
throw new RequestTimeoutError()
|
|
9
|
+
}, timeout)
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
11
|
+
await next()
|
|
12
|
+
})
|
|
13
|
+
)
|
|
@@ -55,7 +55,7 @@ describe('combineMiddleware', () => {
|
|
|
55
55
|
PikkuWiringTypes.http,
|
|
56
56
|
Math.random().toString(),
|
|
57
57
|
{
|
|
58
|
-
|
|
58
|
+
wireMiddleware: [mockMiddleware1, mockMiddleware2],
|
|
59
59
|
}
|
|
60
60
|
)
|
|
61
61
|
|
|
@@ -93,7 +93,7 @@ describe('combineMiddleware', () => {
|
|
|
93
93
|
assert.equal(result[1], mockMiddleware2)
|
|
94
94
|
})
|
|
95
95
|
|
|
96
|
-
test('should execute middleware in correct order:
|
|
96
|
+
test('should execute middleware in correct order: wireInheritedMiddleware (tags) → wireMiddleware → funcInheritedMiddleware (tags) → funcMiddleware', () => {
|
|
97
97
|
// Setup tagged middleware
|
|
98
98
|
const wiringTagMiddleware: CorePikkuMiddleware = async (
|
|
99
99
|
services,
|
|
@@ -132,19 +132,19 @@ describe('combineMiddleware', () => {
|
|
|
132
132
|
PikkuWiringTypes.http,
|
|
133
133
|
Math.random().toString(),
|
|
134
134
|
{
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
wireInheritedMiddleware: [{ type: 'tag', tag: 'wiringTag' }],
|
|
136
|
+
wireMiddleware: [wiringMiddleware],
|
|
137
|
+
funcInheritedMiddleware: [{ type: 'tag', tag: 'funcTag' }],
|
|
137
138
|
funcMiddleware: [funcMiddleware],
|
|
138
|
-
funcTags: ['funcTag'],
|
|
139
139
|
}
|
|
140
140
|
)
|
|
141
141
|
|
|
142
142
|
assert.equal(result.length, 4)
|
|
143
|
-
// Order:
|
|
144
|
-
assert.equal(result[0], wiringTagMiddleware) //
|
|
145
|
-
assert.equal(result[1], wiringMiddleware) //
|
|
146
|
-
assert.equal(result[2],
|
|
147
|
-
assert.equal(result[3],
|
|
143
|
+
// Order: wireInheritedMiddleware (tags), wireMiddleware, funcInheritedMiddleware (tags), funcMiddleware
|
|
144
|
+
assert.equal(result[0], wiringTagMiddleware) // wireInheritedMiddleware tags first
|
|
145
|
+
assert.equal(result[1], wiringMiddleware) // wireMiddleware second
|
|
146
|
+
assert.equal(result[2], funcTagMiddleware) // funcInheritedMiddleware tags third
|
|
147
|
+
assert.equal(result[3], funcMiddleware) // funcMiddleware last
|
|
148
148
|
})
|
|
149
149
|
|
|
150
150
|
test('should handle wiring tags only', () => {
|
|
@@ -162,7 +162,7 @@ describe('combineMiddleware', () => {
|
|
|
162
162
|
PikkuWiringTypes.http,
|
|
163
163
|
Math.random().toString(),
|
|
164
164
|
{
|
|
165
|
-
|
|
165
|
+
wireInheritedMiddleware: [{ type: 'tag', tag: 'testTag' }],
|
|
166
166
|
}
|
|
167
167
|
)
|
|
168
168
|
|
|
@@ -185,7 +185,7 @@ describe('combineMiddleware', () => {
|
|
|
185
185
|
PikkuWiringTypes.http,
|
|
186
186
|
Math.random().toString(),
|
|
187
187
|
{
|
|
188
|
-
|
|
188
|
+
funcInheritedMiddleware: [{ type: 'tag', tag: 'funcTestTag' }],
|
|
189
189
|
}
|
|
190
190
|
)
|
|
191
191
|
|
|
@@ -216,7 +216,10 @@ describe('combineMiddleware', () => {
|
|
|
216
216
|
PikkuWiringTypes.http,
|
|
217
217
|
Math.random().toString(),
|
|
218
218
|
{
|
|
219
|
-
|
|
219
|
+
wireInheritedMiddleware: [
|
|
220
|
+
{ type: 'tag', tag: 'tag1' },
|
|
221
|
+
{ type: 'tag', tag: 'tag2' },
|
|
222
|
+
],
|
|
220
223
|
}
|
|
221
224
|
)
|
|
222
225
|
|
|
@@ -240,8 +243,13 @@ describe('combineMiddleware', () => {
|
|
|
240
243
|
PikkuWiringTypes.http,
|
|
241
244
|
Math.random().toString(),
|
|
242
245
|
{
|
|
243
|
-
|
|
244
|
-
|
|
246
|
+
wireInheritedMiddleware: [
|
|
247
|
+
{ type: 'tag', tag: 'existingTag' },
|
|
248
|
+
{ type: 'tag', tag: 'nonExistentTag' },
|
|
249
|
+
],
|
|
250
|
+
funcInheritedMiddleware: [
|
|
251
|
+
{ type: 'tag', tag: 'anotherNonExistentTag' },
|
|
252
|
+
],
|
|
245
253
|
}
|
|
246
254
|
)
|
|
247
255
|
|
|
@@ -276,7 +284,7 @@ describe('runMiddleware', () => {
|
|
|
276
284
|
{} as any,
|
|
277
285
|
{} as any,
|
|
278
286
|
combineMiddleware(PikkuWiringTypes.rpc, Math.random().toString(), {
|
|
279
|
-
|
|
287
|
+
wireMiddleware: [middleware1, middleware2, middleware1, middleware2],
|
|
280
288
|
}),
|
|
281
289
|
async () => {
|
|
282
290
|
executionOrder.push('main')
|