@pikku/core 0.6.26 → 0.7.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.
Files changed (130) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/dist/channel/channel-handler.js +16 -38
  3. package/dist/channel/channel-runner.d.ts +7 -8
  4. package/dist/channel/channel-runner.js +58 -7
  5. package/dist/channel/channel.types.d.ts +14 -27
  6. package/dist/channel/local/local-channel-runner.js +9 -4
  7. package/dist/channel/log-channels.js +4 -4
  8. package/dist/channel/serverless/serverless-channel-runner.js +17 -6
  9. package/dist/function/function-runner.d.ts +12 -0
  10. package/dist/function/function-runner.js +38 -0
  11. package/dist/function/functions.types.d.ts +37 -0
  12. package/dist/function/functions.types.js +1 -0
  13. package/dist/function/index.d.ts +2 -0
  14. package/dist/function/index.js +2 -0
  15. package/dist/handle-error.d.ts +1 -1
  16. package/dist/http/http-route-runner.d.ts +3 -3
  17. package/dist/http/http-route-runner.js +60 -35
  18. package/dist/http/http-routes.types.d.ts +38 -7
  19. package/dist/http/http-runner.d.ts +90 -0
  20. package/dist/http/http-runner.js +336 -0
  21. package/dist/http/http.types.d.ts +177 -0
  22. package/dist/http/http.types.js +1 -0
  23. package/dist/http/index.d.ts +2 -2
  24. package/dist/http/index.js +1 -1
  25. package/dist/http/log-http-routes.js +7 -5
  26. package/dist/http/pikku-fetch-http-request.d.ts +1 -1
  27. package/dist/http/pikku-fetch-http-response.d.ts +4 -2
  28. package/dist/http/pikku-fetch-http-response.js +64 -12
  29. package/dist/http/pikku-http-request.d.ts +56 -0
  30. package/dist/http/pikku-http-request.js +95 -0
  31. package/dist/http/pikku-http-response.d.ts +13 -0
  32. package/dist/http/pikku-http-response.js +60 -0
  33. package/dist/index.d.ts +3 -2
  34. package/dist/index.js +3 -2
  35. package/dist/middleware/auth-apikey.js +5 -4
  36. package/dist/middleware/auth-bearer.js +5 -4
  37. package/dist/middleware/auth-cookie.js +10 -9
  38. package/dist/parse-relative-time-offset.d.ts +12 -0
  39. package/dist/parse-relative-time-offset.js +20 -0
  40. package/dist/permissions.d.ts +2 -2
  41. package/dist/pikku-func.d.ts +12 -0
  42. package/dist/pikku-func.js +39 -0
  43. package/dist/pikku-function.d.ts +1 -0
  44. package/dist/pikku-function.js +1 -0
  45. package/dist/pikku-state.d.ts +10 -5
  46. package/dist/pikku-state.js +7 -3
  47. package/dist/scheduler/scheduler-runner.d.ts +1 -1
  48. package/dist/scheduler/scheduler-runner.js +24 -6
  49. package/dist/scheduler/scheduler.types.d.ts +3 -2
  50. package/dist/services/content-service.d.ts +34 -36
  51. package/dist/services/index.d.ts +1 -0
  52. package/dist/services/index.js +1 -0
  53. package/dist/services/local-content.d.ts +4 -2
  54. package/dist/services/local-content.js +19 -9
  55. package/dist/types/core.types.d.ts +15 -1
  56. package/dist/types/functions.types.d.ts +11 -6
  57. package/dist/utils.d.ts +2 -0
  58. package/dist/utils.js +15 -0
  59. package/package.json +2 -1
  60. package/lcov.info +0 -4310
  61. package/src/channel/channel-handler.ts +0 -227
  62. package/src/channel/channel-runner.ts +0 -98
  63. package/src/channel/channel-store.ts +0 -29
  64. package/src/channel/channel.types.ts +0 -171
  65. package/src/channel/eventhub-service.ts +0 -30
  66. package/src/channel/eventhub-store.ts +0 -8
  67. package/src/channel/index.ts +0 -8
  68. package/src/channel/local/index.ts +0 -3
  69. package/src/channel/local/local-channel-handler.ts +0 -50
  70. package/src/channel/local/local-channel-runner.test.ts +0 -158
  71. package/src/channel/local/local-channel-runner.ts +0 -126
  72. package/src/channel/local/local-eventhub-service.test.ts +0 -95
  73. package/src/channel/local/local-eventhub-service.ts +0 -95
  74. package/src/channel/log-channels.ts +0 -20
  75. package/src/channel/pikku-abstract-channel-handler.test.ts +0 -52
  76. package/src/channel/pikku-abstract-channel-handler.ts +0 -38
  77. package/src/channel/serverless/index.ts +0 -1
  78. package/src/channel/serverless/serverless-channel-runner.ts +0 -209
  79. package/src/errors/error-handler.ts +0 -61
  80. package/src/errors/error.test.ts +0 -54
  81. package/src/errors/errors.ts +0 -339
  82. package/src/errors/index.ts +0 -2
  83. package/src/handle-error.ts +0 -73
  84. package/src/http/http-route-runner.test.ts +0 -117
  85. package/src/http/http-route-runner.ts +0 -460
  86. package/src/http/http-routes.types.ts +0 -211
  87. package/src/http/incomingmessage-to-request-convertor.ts +0 -0
  88. package/src/http/index.ts +0 -8
  89. package/src/http/log-http-routes.ts +0 -20
  90. package/src/http/pikku-fetch-http-request.test.ts +0 -237
  91. package/src/http/pikku-fetch-http-request.ts +0 -167
  92. package/src/http/pikku-fetch-http-response.test.ts +0 -82
  93. package/src/http/pikku-fetch-http-response.ts +0 -84
  94. package/src/index.ts +0 -19
  95. package/src/middleware/auth-apikey.ts +0 -65
  96. package/src/middleware/auth-bearer.ts +0 -65
  97. package/src/middleware/auth-cookie.ts +0 -98
  98. package/src/middleware/index.ts +0 -3
  99. package/src/middleware/timeout.ts +0 -13
  100. package/src/middleware-runner.ts +0 -43
  101. package/src/permissions.test.ts +0 -58
  102. package/src/permissions.ts +0 -45
  103. package/src/pikku-request.ts +0 -23
  104. package/src/pikku-response.ts +0 -5
  105. package/src/pikku-state.ts +0 -71
  106. package/src/scheduler/index.ts +0 -5
  107. package/src/scheduler/log-schedulers.ts +0 -20
  108. package/src/scheduler/scheduler-runner.ts +0 -87
  109. package/src/scheduler/scheduler.types.ts +0 -29
  110. package/src/schema.test.ts +0 -57
  111. package/src/schema.ts +0 -99
  112. package/src/services/content-service.ts +0 -69
  113. package/src/services/index.ts +0 -17
  114. package/src/services/jwt-service.ts +0 -30
  115. package/src/services/local-content.ts +0 -91
  116. package/src/services/local-secrets.ts +0 -43
  117. package/src/services/local-variables.ts +0 -17
  118. package/src/services/logger-console.ts +0 -97
  119. package/src/services/logger.ts +0 -57
  120. package/src/services/schema-service.ts +0 -26
  121. package/src/services/secret-service.ts +0 -17
  122. package/src/services/user-session-service.ts +0 -53
  123. package/src/services/variables-service.ts +0 -6
  124. package/src/time-utils.test.ts +0 -56
  125. package/src/time-utils.ts +0 -32
  126. package/src/types/core.types.ts +0 -159
  127. package/src/types/functions.types.ts +0 -63
  128. package/src/utils.ts +0 -21
  129. package/tsconfig.json +0 -13
  130. package/tsconfig.tsbuildinfo +0 -1
@@ -1,158 +0,0 @@
1
- import { test, beforeEach, afterEach } from 'node:test'
2
- import * as assert from 'node:assert/strict'
3
- import { runLocalChannel } from './local-channel-runner.js'
4
- import { resetPikkuState } from '../../pikku-state.js'
5
- import { addChannel } from '../channel-runner.js'
6
- import {
7
- HTTPMethod,
8
- PikkuHTTPRequest,
9
- PikkuHTTPResponse,
10
- PikkuQuery,
11
- } from '../../http/http-routes.types.js'
12
- import { SerializeOptions } from 'cookie'
13
-
14
- /**
15
- * Minimal stubs for dependencies that runChannel expects.
16
- * In a real test setup, you may provide more comprehensive mocks
17
- * or refactor your code to allow dependency injection.
18
- */
19
- const mockLogger = {
20
- info: () => {},
21
- error: () => {},
22
- warn: () => {},
23
- debug: () => {},
24
- }
25
-
26
- const mockSingletonServices = {
27
- logger: mockLogger,
28
- } as any
29
-
30
- // Mock request and response objects
31
- export class PikkuMockRequest implements PikkuHTTPRequest {
32
- private _params: Record<string, string | string[] | undefined> = {}
33
-
34
- constructor(
35
- private _route: string,
36
- private _method: HTTPMethod
37
- ) {}
38
-
39
- method(): HTTPMethod {
40
- return this._method
41
- }
42
- path(): string {
43
- return this._route
44
- }
45
- json(): Promise<unknown> {
46
- throw new Error('Method not implemented.')
47
- }
48
- arrayBuffer(): Promise<ArrayBuffer> {
49
- throw new Error('Method not implemented.')
50
- }
51
- header(headerName: string): string | null {
52
- throw new Error('Method not implemented.')
53
- }
54
- cookie(name?: string): string | null {
55
- throw new Error('Method not implemented.')
56
- }
57
- params(): Partial<Record<string, string | string[]>> {
58
- return this._params
59
- }
60
- setParams(params: Record<string, string | string[] | undefined>): void {
61
- this._params = params
62
- }
63
- query(): PikkuQuery {
64
- throw new Error('Method not implemented.')
65
- }
66
- public async data() {
67
- return { test: 'data' }
68
- }
69
- }
70
-
71
- export class PikkuMockResponse implements PikkuHTTPResponse {
72
- public _status: number | undefined
73
-
74
- status(code: number): this {
75
- this._status = code
76
- return this
77
- }
78
-
79
- cookie(name: string, value: string | null, options: SerializeOptions): this {
80
- throw new Error('Method not implemented.')
81
- }
82
- header(name: string, value: string | string[]): this {
83
- throw new Error('Method not implemented.')
84
- }
85
- arrayBuffer(data: XMLHttpRequestBodyInit): this {
86
- throw new Error('Method not implemented.')
87
- }
88
- json(data: unknown): this {
89
- // We don't need to implement this for our test
90
- return this
91
- }
92
- redirect(location: string, status?: number): this {
93
- throw new Error('Method not implemented.')
94
- }
95
- }
96
-
97
- const mockCreateSessionServices = async () => ({ sessionServiceMock: true })
98
-
99
- beforeEach(() => {
100
- resetPikkuState()
101
- })
102
-
103
- afterEach(() => {
104
- resetPikkuState()
105
- })
106
-
107
- test('runChannel should return undefined and 404 if no matching channel is found', async () => {
108
- const mockResponse = new PikkuMockResponse()
109
-
110
- const result = await runLocalChannel({
111
- singletonServices: mockSingletonServices,
112
- channelId: 'test-channel-id',
113
- request: new PikkuMockRequest('/non-existent-channel', 'get'),
114
- response: mockResponse,
115
- createSessionServices: mockCreateSessionServices,
116
- })
117
-
118
- assert.equal(
119
- result,
120
- undefined,
121
- 'Should return undefined if no channel matches'
122
- )
123
- assert.equal(mockResponse._status, 404, 'Should set response status to 404')
124
- // assert.equal(mockResponse._ended, true, 'Should end the response')
125
- })
126
-
127
- test('runChannel should return a channel handler if channel matches and no auth required', async () => {
128
- resetPikkuState()
129
- addChannel({
130
- name: 'test',
131
- route: '/test-channel',
132
- auth: false,
133
- })
134
-
135
- // Provide a fake channelPermissionService if needed
136
- const singletonServicesWithPerm = {
137
- ...mockSingletonServices,
138
- channelPermissionService: {
139
- verifyChannelAccess: async () => {},
140
- },
141
- }
142
-
143
- const result = await runLocalChannel({
144
- singletonServices: singletonServicesWithPerm,
145
- channelId: 'test-channel-id',
146
- request: new PikkuMockRequest('/test-channel', 'get'),
147
- response: new PikkuMockResponse(),
148
- route: '/test-channel',
149
- createSessionServices: mockCreateSessionServices,
150
- })
151
-
152
- assert.ok(result, 'Should return a PikkuChannelHandler instance')
153
-
154
- // Simulate opening the channel
155
- result.open()
156
-
157
- // TODO: Test that the opened channel works
158
- })
@@ -1,126 +0,0 @@
1
- import { openChannel } from '../channel-runner.js'
2
- import { createHTTPInteraction } from '../../http/http-route-runner.js'
3
- import { closeSessionServices } from '../../utils.js'
4
- import { processMessageHandlers } from '../channel-handler.js'
5
- import {
6
- CoreAPIChannel,
7
- RunChannelOptions,
8
- RunChannelParams,
9
- } from '../channel.types.js'
10
- import { PikkuLocalChannelHandler } from './local-channel-handler.js'
11
- import { SessionServices } from '../../types/core.types.js'
12
- import { handleError } from '../../handle-error.js'
13
- import { runMiddleware } from '../../middleware-runner.js'
14
- import { PikkuUserSessionService } from '../../services/user-session-service.js'
15
- import { PikkuHTTP } from '../../http/http-routes.types.js'
16
-
17
- export const runLocalChannel = async ({
18
- singletonServices,
19
- channelId,
20
- request,
21
- response,
22
- route,
23
- createSessionServices,
24
- skipUserSession = false,
25
- respondWith404 = true,
26
- coerceDataFromSchema = true,
27
- logWarningsForStatusCodes = [],
28
- bubbleErrors = false,
29
- }: Partial<Pick<CoreAPIChannel<unknown, any>, 'route'>> &
30
- RunChannelOptions &
31
- RunChannelParams<unknown>): Promise<PikkuLocalChannelHandler | void> => {
32
- let sessionServices: SessionServices<typeof singletonServices> | undefined
33
-
34
- let channelHandler: PikkuLocalChannelHandler | undefined
35
- const userSession = new PikkuUserSessionService()
36
-
37
- let http: PikkuHTTP | undefined
38
- if (request) {
39
- http = createHTTPInteraction(request, response)
40
- route = http?.request?.path()
41
- }
42
-
43
- const main = async () => {
44
- try {
45
- const { openingData, channelConfig } = await openChannel({
46
- channelId,
47
- createSessionServices,
48
- respondWith404,
49
- request,
50
- response,
51
- route,
52
- singletonServices,
53
- skipUserSession,
54
- coerceDataFromSchema,
55
- userSession,
56
- })
57
-
58
- channelHandler = new PikkuLocalChannelHandler(
59
- channelId,
60
- channelConfig.name,
61
- openingData
62
- )
63
- const channel = channelHandler.getChannel()
64
- const session = await userSession.get()
65
- if (createSessionServices) {
66
- sessionServices = await createSessionServices(
67
- singletonServices,
68
- { http },
69
- session
70
- )
71
- }
72
-
73
- const allServices = {
74
- ...singletonServices,
75
- ...sessionServices,
76
- userSession: userSession,
77
- }
78
-
79
- channelHandler.registerOnOpen(() => {
80
- channelConfig.onConnect?.(allServices, channel)
81
- })
82
-
83
- channelHandler.registerOnClose(async () => {
84
- channelConfig.onDisconnect?.(allServices, channel)
85
- if (sessionServices) {
86
- await closeSessionServices(singletonServices.logger, sessionServices)
87
- }
88
- })
89
-
90
- channelHandler.registerOnMessage(
91
- processMessageHandlers(
92
- allServices,
93
- session,
94
- channelConfig,
95
- channelHandler
96
- )
97
- )
98
- } catch (e: any) {
99
- handleError(
100
- e,
101
- http,
102
- channelId,
103
- singletonServices.logger,
104
- logWarningsForStatusCodes,
105
- respondWith404,
106
- bubbleErrors
107
- )
108
- } finally {
109
- if (sessionServices) {
110
- await closeSessionServices(singletonServices.logger, sessionServices)
111
- }
112
- }
113
- }
114
-
115
- await runMiddleware(
116
- {
117
- ...singletonServices,
118
- userSession,
119
- },
120
- { http },
121
- route.middleware || [],
122
- main
123
- )
124
-
125
- return channelHandler!
126
- }
@@ -1,95 +0,0 @@
1
- import * as assert from 'assert'
2
- import { test } from 'node:test'
3
- import { PikkuChannelHandler } from '../channel.types.js'
4
- import { CoreUserSession } from '../../types/core.types.js'
5
- import { LocalEventHubService } from './local-eventhub-service.js'
6
-
7
- class MockChannelHandler implements PikkuChannelHandler {
8
- private channelId: string
9
-
10
- constructor(channelId: string) {
11
- this.channelId = channelId
12
- }
13
-
14
- setUserSession(session: CoreUserSession): Promise<void> | void {
15
- throw new Error('Method not needed.')
16
- }
17
-
18
- getChannel() {
19
- return { channelId: this.channelId } as any
20
- }
21
-
22
- send(data: unknown, isBinary?: boolean) {
23
- // Mock send functionality
24
- console.log(`Sent to ${this.channelId}:`, { data, isBinary })
25
- }
26
- }
27
-
28
- test('LocalEventHubService: subscribe and unsubscribe', () => {
29
- const eventHub = new LocalEventHubService()
30
-
31
- // Subscribe a channel to a topic
32
- eventHub.subscribe('topic1', 'channel1')
33
- assert.strictEqual(eventHub['subscriptions'].get('topic1')!.size, 1)
34
-
35
- // Unsubscribe the channel
36
- eventHub.unsubscribe('topic1', 'channel1')
37
- assert.strictEqual(eventHub['subscriptions'].has('topic1'), false)
38
- })
39
-
40
- test('LocalEventHubService: publish messages', () => {
41
- const eventHub = new LocalEventHubService()
42
-
43
- // Register mock channels
44
- const channel1 = new MockChannelHandler('channel1')
45
- const channel2 = new MockChannelHandler('channel2')
46
- eventHub.onChannelOpened(channel1)
47
- eventHub.onChannelOpened(channel2)
48
-
49
- // Subscribe channels to a topic
50
- eventHub.subscribe('topic1', 'channel1')
51
- eventHub.subscribe('topic1', 'channel2')
52
-
53
- // Mock `send` method to track calls
54
- let sendCallCount = 0
55
- channel1.send = () => {
56
- sendCallCount++
57
- }
58
- channel2.send = () => {
59
- sendCallCount++
60
- }
61
-
62
- // Publish a message
63
- eventHub.publish('topic1', 'channel1', { message: 'Hello!' })
64
-
65
- // Ensure the message was sent only to channel2
66
- assert.strictEqual(sendCallCount, 1)
67
- })
68
-
69
- test('LocalEventHubService: onChannelOpened and onChannelClosed', () => {
70
- const eventHub = new LocalEventHubService()
71
-
72
- // Register and verify channel
73
- const channel1 = new MockChannelHandler('channel1')
74
- eventHub.onChannelOpened(channel1)
75
- assert.strictEqual(eventHub['channels'].has('channel1'), true)
76
-
77
- // Close and verify removal
78
- eventHub.onChannelClosed('channel1')
79
- assert.strictEqual(eventHub['channels'].has('channel1'), false)
80
- })
81
-
82
- test('LocalEventHubService: clean up empty topics on channel close', () => {
83
- const eventHub = new LocalEventHubService()
84
-
85
- // Register and subscribe channels
86
- const channel1 = new MockChannelHandler('channel1')
87
- eventHub.onChannelOpened(channel1)
88
- eventHub.subscribe('topic1', 'channel1')
89
-
90
- // Close channel
91
- eventHub.onChannelClosed('channel1')
92
-
93
- // Ensure topic1 is removed
94
- assert.strictEqual(eventHub['subscriptions'].has('topic1'), false)
95
- })
@@ -1,95 +0,0 @@
1
- import { PikkuChannelHandler } from '../channel.types.js'
2
- import { EventHubService } from '../eventhub-service.js'
3
-
4
- /**
5
- * Implementation of the SubscriptionService interface.
6
- * Manages subscriptions and publishes messages to subscribers.
7
- */
8
- export class LocalEventHubService<Data = unknown>
9
- implements EventHubService<Data>
10
- {
11
- private channels = new Map<string, PikkuChannelHandler>()
12
-
13
- /**
14
- * A map storing topics and their associated connection IDs.
15
- */
16
- private subscriptions: Map<string, Set<string>> = new Map()
17
-
18
- /**
19
- * Subscribes a connection to a specific topic.
20
- * Creates the topic if it does not already exist.
21
- * @param topic - The topic to subscribe to.
22
- * @param channelId - The unique ID of the connection to subscribe.
23
- */
24
- public subscribe(topic: string, channelId: string): void {
25
- if (!this.subscriptions.has(topic)) {
26
- this.subscriptions.set(topic, new Set())
27
- }
28
- this.subscriptions.get(topic)!.add(channelId)
29
- }
30
-
31
- /**
32
- * Unsubscribes a connection from a specific topic.
33
- * Removes the topic if it has no more subscribers.
34
- * @param topic - The topic to unsubscribe from.
35
- * @param channelId - The unique ID of the connection to unsubscribe.
36
- */
37
- public unsubscribe(topic: string, channelId: string): void {
38
- const topicSubscriptions = this.subscriptions.get(topic)
39
- if (topicSubscriptions) {
40
- topicSubscriptions.delete(channelId)
41
- if (topicSubscriptions.size === 0) {
42
- this.subscriptions.delete(topic) // Cleanup empty subscriptions
43
- }
44
- }
45
- }
46
-
47
- /**
48
- * Sends data to all connections subscribed to a topic.
49
- * @param topic - The topic to send data to.
50
- * @param data - The data to send to the subscribers.
51
- */
52
- public publish(
53
- topic: string,
54
- channelId: string,
55
- data: Data,
56
- isBinary?: boolean
57
- ): void {
58
- const subscribedChannelIds = this.subscriptions.get(topic)
59
- if (!subscribedChannelIds) {
60
- return
61
- }
62
- for (const toChannelId of subscribedChannelIds) {
63
- if (channelId === toChannelId) continue // Skip sending to the sender
64
- const channel = this.channels.get(toChannelId)
65
- if (channel) {
66
- channel.send(data, isBinary)
67
- } else {
68
- // TODO: Websocket is closed, remove the channel from the topic
69
- }
70
- }
71
- }
72
-
73
- /**
74
- * Registers a channel on open
75
- */
76
- public onChannelOpened(channelHandler: PikkuChannelHandler): void {
77
- this.channels.set(channelHandler.getChannel().channelId, channelHandler)
78
- }
79
-
80
- /**
81
- * Handles cleanup when a channel is closed.
82
- * Removes the channel from all topics it is subscribed to.
83
- * Deletes the topic if no more channels are subscribed to it.
84
- * @param channelId - The ID of the channel that was closed.
85
- */
86
- public onChannelClosed(channelId: string): void {
87
- for (const [topic, channelIds] of this.subscriptions.entries()) {
88
- channelIds.delete(channelId)
89
- if (channelIds.size === 0) {
90
- this.subscriptions.delete(topic) // Cleanup empty topics
91
- }
92
- }
93
- this.channels.delete(channelId)
94
- }
95
- }
@@ -1,20 +0,0 @@
1
- import { pikkuState } from '../pikku-state.js'
2
- import { Logger } from '../services/index.js'
3
-
4
- /**
5
- * Logs all the loaded channels.
6
- * @param logger - A logger for logging information.
7
- */
8
- export const logChannels = (logger: Logger) => {
9
- const channels = pikkuState('channel', 'channels')
10
- if (channels.length === 0) {
11
- logger.info('No channels added')
12
- return
13
- }
14
-
15
- let scheduledChannels = 'Channels:'
16
- for (const { name, route } of channels) {
17
- scheduledChannels += `\n\t- ${name} at ${route}`
18
- }
19
- logger.info(scheduledChannels)
20
- }
@@ -1,52 +0,0 @@
1
- import { test, beforeEach } from 'node:test'
2
- import * as assert from 'node:assert/strict'
3
- import { PikkuAbstractChannelHandler } from './pikku-abstract-channel-handler.js'
4
-
5
- // A concrete implementation of the abstract class for testing
6
- class TestChannelHandler extends PikkuAbstractChannelHandler<
7
- { param: string },
8
- { msg: string }
9
- > {
10
- public async send(message: { msg: string }, isBinary = false): Promise<void> {
11
- // Mock send implementation
12
- }
13
- }
14
-
15
- let handler: TestChannelHandler
16
-
17
- beforeEach(() => {
18
- handler = new TestChannelHandler('test-channel-id', 'channel-name', {
19
- param: 'testParam',
20
- })
21
- })
22
-
23
- test('getChannel should return a channel with initial state', () => {
24
- const channel = handler.getChannel()
25
- assert.equal(channel.channelId, 'test-channel-id', 'Channel ID should match')
26
- assert.equal(channel.state, 'initial', 'Initial state should be "initial"')
27
- assert.deepEqual(
28
- channel.openingData,
29
- { param: 'testParam' },
30
- 'Opening data should be accessible'
31
- )
32
- })
33
-
34
- test('open should change channel state to open', () => {
35
- handler.open()
36
- const channel = handler.getChannel()
37
- assert.equal(
38
- channel.state,
39
- 'open',
40
- 'State should be "open" after calling open()'
41
- )
42
- })
43
-
44
- test('close should change channel state to closed', () => {
45
- handler.close()
46
- const channel = handler.getChannel()
47
- assert.equal(
48
- channel.state,
49
- 'closed',
50
- 'State should be "closed" after calling close()'
51
- )
52
- })
@@ -1,38 +0,0 @@
1
- import { PikkuChannel, PikkuChannelHandler } from './channel.types.js'
2
-
3
- export abstract class PikkuAbstractChannelHandler<
4
- OpeningData = unknown,
5
- Out = unknown,
6
- > implements PikkuChannelHandler<OpeningData, Out>
7
- {
8
- protected channel?: PikkuChannel<OpeningData, Out>
9
-
10
- constructor(
11
- public channelId: string,
12
- public channelName: string,
13
- protected openingData: OpeningData
14
- ) {}
15
-
16
- public abstract send(message: Out, isBinary?: boolean): Promise<void> | void
17
-
18
- public getChannel(): PikkuChannel<OpeningData, Out> {
19
- if (!this.channel) {
20
- this.channel = {
21
- channelId: this.channelId,
22
- openingData: this.openingData,
23
- send: this.send.bind(this),
24
- close: this.close.bind(this),
25
- state: 'initial',
26
- }
27
- }
28
- return this.channel
29
- }
30
-
31
- public open(): void {
32
- this.getChannel().state = 'open'
33
- }
34
-
35
- public close(): Promise<void> | void {
36
- this.getChannel().state = 'closed'
37
- }
38
- }
@@ -1 +0,0 @@
1
- export * from './serverless-channel-runner.js'