@toa.io/extensions.realtime 1.0.0-alpha.26 → 1.0.0-alpha.263
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 +46 -0
- package/components/{streams → realtime.streams}/manifest.toa.yaml +12 -1
- package/components/{streams → realtime.streams}/operations/create.d.ts +6 -2
- package/components/realtime.streams/operations/create.js +66 -0
- package/components/realtime.streams/operations/create.js.map +1 -0
- package/components/realtime.streams/operations/lib/Stash.d.ts +21 -0
- package/components/realtime.streams/operations/lib/Stash.js +76 -0
- package/components/realtime.streams/operations/lib/Stash.js.map +1 -0
- package/components/realtime.streams/operations/lib/Stream.d.ts +11 -0
- package/components/realtime.streams/operations/lib/Stream.js +34 -0
- package/components/realtime.streams/operations/lib/Stream.js.map +1 -0
- package/components/realtime.streams/operations/lib/index.d.ts +2 -0
- package/components/realtime.streams/operations/lib/index.js +8 -0
- package/components/realtime.streams/operations/lib/index.js.map +1 -0
- package/components/realtime.streams/operations/lib/types.d.ts +22 -0
- package/components/realtime.streams/operations/lib/types.js.map +1 -0
- package/components/{streams → realtime.streams}/operations/push.d.ts +1 -1
- package/components/realtime.streams/operations/push.js +14 -0
- package/components/realtime.streams/operations/push.js.map +1 -0
- package/components/realtime.streams/operations/tsconfig.tsbuildinfo +1 -0
- package/components/realtime.streams/source/create.ts +94 -0
- package/components/realtime.streams/source/lib/Stash.ts +112 -0
- package/components/realtime.streams/source/lib/Stream.ts +40 -0
- package/components/realtime.streams/source/lib/index.ts +2 -0
- package/components/realtime.streams/source/lib/types.ts +24 -0
- package/components/realtime.streams/source/push.ts +12 -0
- package/{components/context.toa.yaml → context.toa.yaml} +0 -1
- package/cucumber.js +1 -1
- package/features/static.feature +120 -0
- package/features/steps/Components.ts +6 -9
- package/features/steps/Realtime.ts +5 -9
- package/features/steps/Streams.ts +44 -9
- package/features/steps/components/messages/manifest.toa.yaml +2 -0
- package/features/steps/config.ts +4 -0
- package/package.json +7 -3
- package/readme.md +33 -7
- package/source/Composition.ts +18 -7
- package/source/Factory.ts +1 -1
- package/source/Realtime.ts +34 -9
- package/source/Receiver.ts +86 -0
- package/source/Routes.ts +10 -32
- package/source/extension.ts +82 -0
- package/source/index.ts +1 -0
- package/transpiled/Composition.d.ts +7 -1
- package/transpiled/Composition.js +12 -5
- package/transpiled/Composition.js.map +1 -1
- package/transpiled/Factory.js +1 -1
- package/transpiled/Factory.js.map +1 -1
- package/transpiled/Realtime.d.ts +1 -0
- package/transpiled/Realtime.js +30 -4
- package/transpiled/Realtime.js.map +1 -1
- package/transpiled/Receiver.d.ts +26 -0
- package/transpiled/Receiver.js +59 -0
- package/transpiled/Receiver.js.map +1 -0
- package/transpiled/Routes.d.ts +3 -7
- package/transpiled/Routes.js +6 -18
- package/transpiled/Routes.js.map +1 -1
- package/transpiled/extension.d.ts +19 -0
- package/transpiled/extension.js +54 -0
- package/transpiled/extension.js.map +1 -0
- package/transpiled/index.d.ts +1 -0
- package/transpiled/index.js +15 -0
- package/transpiled/index.js.map +1 -1
- package/transpiled/tsconfig.tsbuildinfo +1 -1
- package/components/streams/operations/create.js +0 -29
- package/components/streams/operations/create.js.map +0 -1
- package/components/streams/operations/lib/stream.d.ts +0 -10
- package/components/streams/operations/lib/stream.js +0 -31
- package/components/streams/operations/lib/stream.js.map +0 -1
- package/components/streams/operations/push.js +0 -8
- package/components/streams/operations/push.js.map +0 -1
- package/components/streams/operations/tsconfig.tsbuildinfo +0 -1
- package/components/streams/operations/types.d.ts +0 -11
- package/components/streams/operations/types.js.map +0 -1
- package/components/streams/source/create.ts +0 -38
- package/components/streams/source/lib/stream.ts +0 -37
- package/components/streams/source/push.ts +0 -5
- package/components/streams/source/types.ts +0 -13
- package/stage/streams.test.ts +0 -128
- /package/components/{streams/operations → realtime.streams/operations/lib}/types.js +0 -0
- /package/components/{streams → realtime.streams}/tsconfig.json +0 -0
package/features/static.feature
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
Feature: Static routes
|
|
2
2
|
|
|
3
|
+
Scenario: Debug
|
|
4
|
+
Given the `messages` component is running with routes:
|
|
5
|
+
"""yaml
|
|
6
|
+
created: [sender, recipient]
|
|
7
|
+
"""
|
|
8
|
+
And the stream `004e02a959c04cecaf111827f91caa36` is consumed
|
|
9
|
+
|
|
3
10
|
Scenario: Routing an event
|
|
4
11
|
Given the `messages` component is running with routes:
|
|
5
12
|
"""yaml
|
|
@@ -30,3 +37,116 @@ Feature: Static routes
|
|
|
30
37
|
recipient: 004e02a959c04cecaf111827f91caa36
|
|
31
38
|
text: Hello!
|
|
32
39
|
"""
|
|
40
|
+
|
|
41
|
+
Scenario: Exposing event properties
|
|
42
|
+
Given the `messages` component is running with routes:
|
|
43
|
+
"""yaml
|
|
44
|
+
created:
|
|
45
|
+
key: [sender, recipient]
|
|
46
|
+
expose: [text, sender]
|
|
47
|
+
"""
|
|
48
|
+
And the stream `004e02a959c04cecaf111827f91caa36` is consumed
|
|
49
|
+
And the stream `96db5a47a8244eb3b21820781b7d596e` is consumed
|
|
50
|
+
When the `messages.create` is called with:
|
|
51
|
+
"""yaml
|
|
52
|
+
input:
|
|
53
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
54
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
55
|
+
text: Hello!
|
|
56
|
+
"""
|
|
57
|
+
Then an event is received from the stream `004e02a959c04cecaf111827f91caa36`:
|
|
58
|
+
"""yaml
|
|
59
|
+
event: default.messages.created
|
|
60
|
+
data:
|
|
61
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
62
|
+
text: Hello!
|
|
63
|
+
"""
|
|
64
|
+
And an event is received from the stream `96db5a47a8244eb3b21820781b7d596e`:
|
|
65
|
+
"""yaml
|
|
66
|
+
event: default.messages.created
|
|
67
|
+
data:
|
|
68
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
69
|
+
text: Hello!
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
Scenario: Routing an event using array
|
|
73
|
+
Given the `messages` component is running with routes:
|
|
74
|
+
"""yaml
|
|
75
|
+
created: watchers
|
|
76
|
+
"""
|
|
77
|
+
And the stream `51c15a7290ce47e0af8ec41d60dccb32` is consumed
|
|
78
|
+
And the stream `bb27366509a64178a39313aac42435ae` is consumed
|
|
79
|
+
When the `messages.create` is called with:
|
|
80
|
+
"""yaml
|
|
81
|
+
input:
|
|
82
|
+
watchers:
|
|
83
|
+
- 51c15a7290ce47e0af8ec41d60dccb32
|
|
84
|
+
- bb27366509a64178a39313aac42435ae
|
|
85
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
86
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
87
|
+
text: Hello!
|
|
88
|
+
"""
|
|
89
|
+
Then an event is received from the stream `51c15a7290ce47e0af8ec41d60dccb32`:
|
|
90
|
+
"""yaml
|
|
91
|
+
event: default.messages.created
|
|
92
|
+
data:
|
|
93
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
94
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
95
|
+
text: Hello!
|
|
96
|
+
"""
|
|
97
|
+
And an event is received from the stream `bb27366509a64178a39313aac42435ae`:
|
|
98
|
+
"""yaml
|
|
99
|
+
event: default.messages.created
|
|
100
|
+
data:
|
|
101
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
102
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
103
|
+
text: Hello!
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
Scenario: Continuation token
|
|
107
|
+
Given the `messages` component is running with routes:
|
|
108
|
+
"""yaml
|
|
109
|
+
created: [sender, recipient]
|
|
110
|
+
"""
|
|
111
|
+
And the stream `004e02a959c04cecaf111827f91caa36` is consumed
|
|
112
|
+
And an event is received from the stream `004e02a959c04cecaf111827f91caa36`:
|
|
113
|
+
"""yaml
|
|
114
|
+
event: token
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
When the consumer `004e02a959c04cecaf111827f91caa36` is disconnected
|
|
118
|
+
And the `messages.create` is called with:
|
|
119
|
+
"""yaml
|
|
120
|
+
input:
|
|
121
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
122
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
123
|
+
text: Hello!
|
|
124
|
+
"""
|
|
125
|
+
And the `messages.create` is called with:
|
|
126
|
+
"""yaml
|
|
127
|
+
input:
|
|
128
|
+
sender: 004e02a959c04cecaf111827f91caa36
|
|
129
|
+
recipient: 96db5a47a8244eb3b21820781b7d596e
|
|
130
|
+
text: Hi!
|
|
131
|
+
"""
|
|
132
|
+
And the consumer `004e02a959c04cecaf111827f91caa36` is reconnected
|
|
133
|
+
Then an event is received from the stream `004e02a959c04cecaf111827f91caa36`:
|
|
134
|
+
"""yaml
|
|
135
|
+
event: default.messages.created
|
|
136
|
+
data:
|
|
137
|
+
sender: 96db5a47a8244eb3b21820781b7d596e
|
|
138
|
+
recipient: 004e02a959c04cecaf111827f91caa36
|
|
139
|
+
text: Hello!
|
|
140
|
+
"""
|
|
141
|
+
And an event is received from the stream `004e02a959c04cecaf111827f91caa36`:
|
|
142
|
+
"""yaml
|
|
143
|
+
event: default.messages.created
|
|
144
|
+
data:
|
|
145
|
+
sender: 004e02a959c04cecaf111827f91caa36
|
|
146
|
+
recipient: 96db5a47a8244eb3b21820781b7d596e
|
|
147
|
+
text: Hi!
|
|
148
|
+
"""
|
|
149
|
+
And an event is received from the stream `004e02a959c04cecaf111827f91caa36`:
|
|
150
|
+
"""yaml
|
|
151
|
+
event: token
|
|
152
|
+
"""
|
|
@@ -5,6 +5,7 @@ import { parse } from '@toa.io/yaml'
|
|
|
5
5
|
import * as stage from '@toa.io/userland/stage'
|
|
6
6
|
import { type Component, type Request } from '@toa.io/core'
|
|
7
7
|
import { timeout } from '@toa.io/generic'
|
|
8
|
+
import { parse as parseRoutes, type Declaration } from '../../source/extension'
|
|
8
9
|
import { Realtime } from './Realtime'
|
|
9
10
|
|
|
10
11
|
@binding([Realtime])
|
|
@@ -18,14 +19,14 @@ export class Components {
|
|
|
18
19
|
|
|
19
20
|
@given('the `{word}` component is running with routes:')
|
|
20
21
|
public async run (component: string, yaml: string): Promise<void> {
|
|
21
|
-
const
|
|
22
|
+
const declaration = parse<Declaration>(yaml)
|
|
22
23
|
const [name, namespace = 'default'] = component.split('.').reverse()
|
|
24
|
+
const routes = parseRoutes(declaration)
|
|
23
25
|
|
|
24
|
-
for (const
|
|
25
|
-
const label = `${namespace}.${name}.${event}`
|
|
26
|
-
const properties = Array.isArray(value) ? value : [value]
|
|
26
|
+
for (const route of routes) {
|
|
27
|
+
const label = `${namespace}.${name}.${route.event}`
|
|
27
28
|
|
|
28
|
-
this.realtime.declare(label, properties)
|
|
29
|
+
this.realtime.declare(label, route.properties, route.expose)
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -65,7 +66,3 @@ function componentPaths (): string[] {
|
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
const ROOT = resolve(__dirname, 'components')
|
|
68
|
-
|
|
69
|
-
interface Manifest {
|
|
70
|
-
realtime: Map<string, string | string[]>
|
|
71
|
-
}
|
|
@@ -2,7 +2,8 @@ import * as boot from '@toa.io/boot'
|
|
|
2
2
|
import { encode } from '@toa.io/generic'
|
|
3
3
|
import { type Connector } from '@toa.io/core'
|
|
4
4
|
import { after, binding } from 'cucumber-tsflow'
|
|
5
|
-
import { Factory } from '../../source
|
|
5
|
+
import { Factory } from '../../source'
|
|
6
|
+
import type { Route } from '../../source/extension'
|
|
6
7
|
|
|
7
8
|
@binding()
|
|
8
9
|
export class Realtime {
|
|
@@ -15,14 +16,14 @@ export class Realtime {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
@after()
|
|
18
|
-
|
|
19
|
+
public async shutdown (): Promise<void> {
|
|
19
20
|
this.connected = false
|
|
20
21
|
|
|
21
22
|
await this.service.disconnect()
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
public declare (event: string, properties: string[]): void {
|
|
25
|
-
this.routes.push({ event, properties })
|
|
25
|
+
public declare (event: string, properties: string[], expose?: string[]): void {
|
|
26
|
+
this.routes.push({ event, properties, expose })
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
public async serve (): Promise<void> {
|
|
@@ -36,8 +37,3 @@ export class Realtime {
|
|
|
36
37
|
await this.service.connect()
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
interface Route {
|
|
41
|
-
event: string
|
|
42
|
-
properties: string[]
|
|
43
|
-
}
|
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as assert from 'node:assert'
|
|
2
|
+
import { setTimeout } from 'node:timers/promises'
|
|
2
3
|
import { after, binding, given, then } from 'cucumber-tsflow'
|
|
3
4
|
import { match } from '@toa.io/generic'
|
|
4
|
-
|
|
5
|
-
import { type Component } from '@toa.io/core'
|
|
6
5
|
import { parse } from '@toa.io/yaml'
|
|
7
6
|
import * as stage from '@toa.io/userland/stage'
|
|
8
7
|
import { Realtime } from './Realtime'
|
|
8
|
+
import type { Readable } from 'node:stream'
|
|
9
|
+
import type { Component } from '@toa.io/core'
|
|
9
10
|
|
|
10
11
|
@binding([Realtime])
|
|
11
12
|
export class Streams {
|
|
12
13
|
private readonly realtime: Realtime
|
|
13
14
|
private remote: Component | null = null
|
|
14
15
|
private streams: Record<string, Readable> = {}
|
|
15
|
-
private events: Record<string,
|
|
16
|
+
private events: Record<string, Event[]> = {}
|
|
16
17
|
|
|
17
18
|
public constructor (realtime: Realtime) {
|
|
18
19
|
this.realtime = realtime
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
@given('the stream `{word}` is consumed')
|
|
22
|
+
@given('the stream `{word}` is consumed', { timeout: 30_000 })
|
|
22
23
|
public async consume (key: string): Promise<void> {
|
|
23
24
|
await this.realtime.serve()
|
|
24
25
|
|
|
25
26
|
this.remote ??= await stage.remote('realtime.streams')
|
|
26
27
|
this.events[key] = []
|
|
27
|
-
|
|
28
|
-
this.
|
|
28
|
+
|
|
29
|
+
await this.createStream(key)
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
@then('an event is received from the stream `{word}`:')
|
|
32
|
-
public received (key: string, yaml: string): void {
|
|
33
|
+
public async received (key: string, yaml: string): Promise<void> {
|
|
34
|
+
await setTimeout(100)
|
|
35
|
+
|
|
33
36
|
const expected = parse<object>(yaml)
|
|
34
37
|
|
|
35
38
|
for (const event of this.events[key])
|
|
@@ -39,12 +42,44 @@ export class Streams {
|
|
|
39
42
|
throw new Error('No matching event received')
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
@then('the consumer `{word}` is disconnected')
|
|
46
|
+
public disconnected (key: string): void {
|
|
47
|
+
this.streams[key]?.destroy()
|
|
48
|
+
delete this.streams[key]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@then('the consumer `{word}` is reconnected')
|
|
52
|
+
public async reconnected (key: string): Promise<void> {
|
|
53
|
+
const last = this.events[key].findLast((event) => event.event === 'token')
|
|
54
|
+
|
|
55
|
+
assert.ok(last, `No last event found for stream ${key}`)
|
|
56
|
+
|
|
57
|
+
await this.createStream(key, last.data as string)
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
@after()
|
|
43
|
-
|
|
61
|
+
public async shutdown (): Promise<void> {
|
|
44
62
|
for (const stream of Object.values(this.streams))
|
|
45
63
|
stream.destroy()
|
|
46
64
|
|
|
47
65
|
this.streams = {}
|
|
48
66
|
this.events = {}
|
|
67
|
+
|
|
68
|
+
await setTimeout(100)
|
|
49
69
|
}
|
|
70
|
+
|
|
71
|
+
private async createStream (key: string, token?: string): Promise<void> {
|
|
72
|
+
this.streams[key] = await this.remote!.invoke('create', { input: { key, token } })
|
|
73
|
+
this.streams[key].on('data', (event: Event) => {
|
|
74
|
+
console.log('[TEST] Received event', event)
|
|
75
|
+
this.events[key]?.push(event)
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Event {
|
|
81
|
+
key: string
|
|
82
|
+
token: string
|
|
83
|
+
event: string
|
|
84
|
+
data?: unknown
|
|
50
85
|
}
|
package/features/steps/config.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/extensions.realtime",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.263",
|
|
4
4
|
"description": "Toa Realtime",
|
|
5
5
|
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/toa-io/toa#readme",
|
|
@@ -16,13 +16,17 @@
|
|
|
16
16
|
"publishConfig": {
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@toa.io/core": "1.0.0-alpha.262",
|
|
21
|
+
"openspan": "1.0.0-alpha.254"
|
|
22
|
+
},
|
|
19
23
|
"jest": {
|
|
20
24
|
"preset": "ts-jest",
|
|
21
25
|
"testEnvironment": "node"
|
|
22
26
|
},
|
|
23
27
|
"scripts": {
|
|
24
|
-
"transpile": "npx tsc && npx tsc -p ./components/streams",
|
|
28
|
+
"transpile": "npx tsc && npx tsc -p ./components/realtime.streams",
|
|
25
29
|
"features": "npx cucumber-js"
|
|
26
30
|
},
|
|
27
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "165daa284bb7a75907a593fde359e3553b083417"
|
|
28
32
|
}
|
package/readme.md
CHANGED
|
@@ -10,17 +10,20 @@
|
|
|
10
10
|
</a>
|
|
11
11
|
|
|
12
12
|
Realtime extension combines application events into streams according to defined routes.
|
|
13
|
-
Clients may consume these streams [via Exposition](
|
|
13
|
+
Clients may consume these streams [via Exposition](/extensions/exposition).
|
|
14
|
+
|
|
15
|
+
If stream is idle for 16 seconds, a `heartbeat` message is sent.
|
|
14
16
|
|
|
15
17
|
## Static routes
|
|
16
18
|
|
|
17
19
|
Static route specifies an event that should be combined into a stream using specified property of
|
|
18
|
-
event's payload as a stream key.
|
|
20
|
+
event's payload as a stream key or an array of stream keys.
|
|
19
21
|
|
|
20
22
|
Static routes may be defined in Component manifest or the Context annotation.
|
|
21
23
|
|
|
22
24
|
```yaml
|
|
23
25
|
# manifest.toa.yaml
|
|
26
|
+
|
|
24
27
|
name: users
|
|
25
28
|
|
|
26
29
|
realtime:
|
|
@@ -29,13 +32,24 @@ realtime:
|
|
|
29
32
|
|
|
30
33
|
```yaml
|
|
31
34
|
# context.toa.yaml
|
|
35
|
+
|
|
32
36
|
realtime:
|
|
33
37
|
users.updated: id
|
|
34
|
-
orders.created:
|
|
38
|
+
orders.created: customer_id
|
|
35
39
|
```
|
|
36
40
|
|
|
37
41
|
In case of conflict, the Context annotation takes precedence.
|
|
38
42
|
|
|
43
|
+
Multiple stream keys may be defined for a single event.
|
|
44
|
+
|
|
45
|
+
```yaml
|
|
46
|
+
# manifest.toa.yaml
|
|
47
|
+
name: messages
|
|
48
|
+
|
|
49
|
+
realtime:
|
|
50
|
+
updated: [sender_id, recipient_id]
|
|
51
|
+
```
|
|
52
|
+
|
|
39
53
|
### Static route examples
|
|
40
54
|
|
|
41
55
|
Given two rules: `users.updated: id` and `orders.created: customer_id`,
|
|
@@ -87,13 +101,25 @@ Dynamic routes are managed by the `realtime.routes` component, running in the Re
|
|
|
87
101
|
|
|
88
102
|
## Exposition
|
|
89
103
|
|
|
90
|
-
Streams are exposed by the [`realtime.streams`](components/streams) component, running in the
|
|
104
|
+
Streams are exposed by the [`realtime.streams`](components/realtime.streams) component, running in the
|
|
91
105
|
Realtime extension, and are
|
|
92
106
|
accessible via the `/realtime/streams/:key/` resource with
|
|
93
107
|
the [`auth:id: key`](/extensions/exposition/documentation/access.md#id) authorization rule.
|
|
94
108
|
|
|
95
|
-
Refer to the [Exposition extension](/extensions/exposition) for more
|
|
96
|
-
details:
|
|
109
|
+
Refer to the [Exposition extension](/extensions/exposition) for more details:
|
|
97
110
|
|
|
98
|
-
- [
|
|
111
|
+
- [Multipart responses](/extensions/exposition/documentation/protocol.md#multipart-types)
|
|
99
112
|
- [Access authorization](/extensions/exposition/documentation/access.md)
|
|
113
|
+
|
|
114
|
+
## Resources management
|
|
115
|
+
|
|
116
|
+
Resource requests and limits can be specified by `resources` annotation:
|
|
117
|
+
|
|
118
|
+
```yaml
|
|
119
|
+
# context.toa.yaml
|
|
120
|
+
|
|
121
|
+
realtime:
|
|
122
|
+
resources:
|
|
123
|
+
cpu: [100m, 500m]
|
|
124
|
+
memory: [100Mi, 200Mi]
|
|
125
|
+
```
|
package/source/Composition.ts
CHANGED
|
@@ -18,16 +18,10 @@ export class Composition extends Connector {
|
|
|
18
18
|
await composition.connect()
|
|
19
19
|
|
|
20
20
|
this.depends(composition)
|
|
21
|
-
|
|
22
|
-
console.info('Composition complete.')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
protected override dispose (): void {
|
|
26
|
-
console.info('Composition shutdown complete.')
|
|
27
21
|
}
|
|
28
22
|
}
|
|
29
23
|
|
|
30
|
-
function find (): string[] {
|
|
24
|
+
export function find (): string[] {
|
|
31
25
|
return entries().map((entry) => resolve(ROOT, entry.name))
|
|
32
26
|
}
|
|
33
27
|
|
|
@@ -37,4 +31,21 @@ function entries (): Dirent[] {
|
|
|
37
31
|
return entries.filter((entry) => entry.isDirectory())
|
|
38
32
|
}
|
|
39
33
|
|
|
34
|
+
export function components (): Components {
|
|
35
|
+
const labels: string[] = []
|
|
36
|
+
const paths: string[] = []
|
|
37
|
+
|
|
38
|
+
for (const entry of entries()) {
|
|
39
|
+
labels.push(entry.name.replace('.', '-'))
|
|
40
|
+
paths.push(resolve(ROOT, entry.name))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { labels, paths }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface Components {
|
|
47
|
+
labels: string[]
|
|
48
|
+
paths: string[]
|
|
49
|
+
}
|
|
50
|
+
|
|
40
51
|
const ROOT = resolve(__dirname, '../components/')
|
package/source/Factory.ts
CHANGED
|
@@ -25,7 +25,7 @@ export class Factory implements extensions.Factory {
|
|
|
25
25
|
private async discovery (): Promise<Component> {
|
|
26
26
|
const locator = new Locator('streams', 'realtime')
|
|
27
27
|
|
|
28
|
-
return await this.boot.remote(locator)
|
|
28
|
+
return await this.boot.remote(locator, { service: 'realtime' })
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
package/source/Realtime.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { console, run } from 'openspan'
|
|
1
2
|
import { type Component, Connector } from '@toa.io/core'
|
|
2
3
|
import { type Routes } from './Routes'
|
|
4
|
+
import type { Push } from './Receiver'
|
|
3
5
|
|
|
4
6
|
export class Realtime extends Connector {
|
|
5
7
|
private readonly discovery: Promise<Component>
|
|
@@ -19,20 +21,43 @@ export class Realtime extends Connector {
|
|
|
19
21
|
|
|
20
22
|
await this.streams.connect()
|
|
21
23
|
|
|
22
|
-
console.
|
|
24
|
+
console.info('Realtime service started')
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
protected override dispose (): void {
|
|
26
|
-
console.
|
|
28
|
+
console.info('Realtime service shutdown complete')
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
private push (event:
|
|
30
|
-
|
|
31
|
+
private push ({ telemetry, ...event }: Push): void {
|
|
32
|
+
const processing = telemetry === null
|
|
33
|
+
? this.deliver(event)
|
|
34
|
+
: run(telemetry, async () => await this.deliver(event))
|
|
35
|
+
|
|
36
|
+
void processing.catch((error) => console.error('Realtime push failed', error))
|
|
31
37
|
}
|
|
32
|
-
}
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
private async deliver (event: Omit<Push, 'telemetry'>): Promise<void> {
|
|
40
|
+
/*
|
|
41
|
+
* The delivery span is created on behalf of the messaging destination
|
|
42
|
+
* (same as the core Receiver), so that service graphs display the fan-out:
|
|
43
|
+
* producer -> destination -> realtime
|
|
44
|
+
*/
|
|
45
|
+
const delivery = {
|
|
46
|
+
name: `${event.event} deliver`,
|
|
47
|
+
kind: 'producer' as const,
|
|
48
|
+
service: event.event,
|
|
49
|
+
attributes: { 'messaging.destination.name': event.event }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const options = {
|
|
53
|
+
name: `${event.event} push`,
|
|
54
|
+
kind: 'consumer' as const,
|
|
55
|
+
service: 'realtime',
|
|
56
|
+
attributes: { 'messaging.destination.name': event.event }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await console.span(delivery, async () => await console.span(options, async () => {
|
|
60
|
+
await this.streams?.invoke('push', { input: event })
|
|
61
|
+
}))
|
|
62
|
+
}
|
|
38
63
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { console, decode, run } from 'openspan'
|
|
2
|
+
import { Connector, type Message } from '@toa.io/core'
|
|
3
|
+
import type { SpanContext } from 'openspan'
|
|
4
|
+
import type { Readable } from 'node:stream'
|
|
5
|
+
|
|
6
|
+
export class Receiver extends Connector {
|
|
7
|
+
private readonly event: string
|
|
8
|
+
private readonly properties: string[]
|
|
9
|
+
private readonly expose?: string[]
|
|
10
|
+
private readonly stream: Readable
|
|
11
|
+
|
|
12
|
+
public constructor ({ event, properties, stream, expose }: {
|
|
13
|
+
event: string
|
|
14
|
+
properties: string[]
|
|
15
|
+
stream: Readable
|
|
16
|
+
expose?: string[]
|
|
17
|
+
}) {
|
|
18
|
+
super()
|
|
19
|
+
|
|
20
|
+
this.event = event
|
|
21
|
+
this.properties = properties
|
|
22
|
+
this.expose = expose
|
|
23
|
+
this.stream = stream
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public receive (message: Message<Record<string, string>>): void {
|
|
27
|
+
// the push continues the trace from the producer
|
|
28
|
+
const telemetry = message.telemetry === undefined ? null : decode(message.telemetry)
|
|
29
|
+
|
|
30
|
+
if (telemetry === null)
|
|
31
|
+
this.process(message, telemetry)
|
|
32
|
+
else
|
|
33
|
+
run(telemetry, () => this.process(message, telemetry))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private process (message: Message<Record<string, string>>, telemetry: SpanContext | null): void {
|
|
37
|
+
const data = this.fit(message.payload)
|
|
38
|
+
|
|
39
|
+
for (const property of this.properties) {
|
|
40
|
+
const key = message.payload[property]
|
|
41
|
+
|
|
42
|
+
if (key === undefined) {
|
|
43
|
+
console.debug('Event does not contain key property',
|
|
44
|
+
{ property, event: this.event })
|
|
45
|
+
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (Array.isArray(key))
|
|
50
|
+
// eslint-disable-next-line max-depth
|
|
51
|
+
for (const k of key as string[])
|
|
52
|
+
this.push(k, data, telemetry)
|
|
53
|
+
else
|
|
54
|
+
this.push(key, data, telemetry)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private fit (payload: Record<string, string>): Record<string, string> {
|
|
59
|
+
if (this.expose === undefined)
|
|
60
|
+
return payload
|
|
61
|
+
|
|
62
|
+
const entries = Object.entries(payload)
|
|
63
|
+
.filter(([key]) => this.expose!.includes(key))
|
|
64
|
+
|
|
65
|
+
return Object.fromEntries(entries)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private push (key: string | null, data: Record<string, string>, telemetry: SpanContext | null): void {
|
|
69
|
+
if (key === null || typeof key === 'undefined') {
|
|
70
|
+
console.debug('Key is null or undefined, skipping', { key, event: this.event })
|
|
71
|
+
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
console.debug('Pushing event to stream', { key, event: this.event, data })
|
|
76
|
+
|
|
77
|
+
this.stream.push({ key, event: this.event, data, telemetry } satisfies Push)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface Push {
|
|
82
|
+
key: string
|
|
83
|
+
event: string
|
|
84
|
+
data: Record<string, string>
|
|
85
|
+
telemetry: SpanContext | null
|
|
86
|
+
}
|
package/source/Routes.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { Readable } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import { console } from 'openspan'
|
|
3
|
+
import { Connector } from '@toa.io/core'
|
|
3
4
|
import { decode } from '@toa.io/generic'
|
|
4
|
-
import {
|
|
5
|
+
import { Receiver } from './Receiver'
|
|
6
|
+
import type { Route } from './extension'
|
|
7
|
+
import type { Bootloader } from './Factory'
|
|
5
8
|
|
|
6
9
|
export class Routes extends Connector {
|
|
7
10
|
public events = new Events()
|
|
@@ -25,8 +28,8 @@ export class Routes extends Connector {
|
|
|
25
28
|
const routes = Routes.read()
|
|
26
29
|
const creating = []
|
|
27
30
|
|
|
28
|
-
for (const { event, properties } of routes) {
|
|
29
|
-
const consumer = this.boot.receive(event,
|
|
31
|
+
for (const { event, properties, expose } of routes) {
|
|
32
|
+
const consumer = this.boot.receive(event, new Receiver({ event, properties, stream: this.events, expose }))
|
|
30
33
|
|
|
31
34
|
creating.push(consumer)
|
|
32
35
|
}
|
|
@@ -39,29 +42,11 @@ export class Routes extends Connector {
|
|
|
39
42
|
await Promise.all(connecting)
|
|
40
43
|
this.depends(consumers)
|
|
41
44
|
|
|
42
|
-
console.
|
|
45
|
+
console.info('Event sources connected', { count: creating.length })
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
public override async close (): Promise<void> {
|
|
46
|
-
console.
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private getReceiver (event: string, properties: string[]): Receiver {
|
|
50
|
-
return {
|
|
51
|
-
receive: (message: Message<Record<string, string>>) => {
|
|
52
|
-
for (const property of properties) {
|
|
53
|
-
const key = message.payload[property]
|
|
54
|
-
|
|
55
|
-
if (key === undefined) {
|
|
56
|
-
console.error(`Event '${event}' does not contain the '${property}' property.`)
|
|
57
|
-
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.events.push({ key, event, data: message.payload })
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
49
|
+
console.info('Event sources disconnected')
|
|
65
50
|
}
|
|
66
51
|
}
|
|
67
52
|
|
|
@@ -74,11 +59,4 @@ class Events extends Readable {
|
|
|
74
59
|
}
|
|
75
60
|
}
|
|
76
61
|
|
|
77
|
-
export
|
|
78
|
-
event: string
|
|
79
|
-
properties: string[]
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
interface Receiver {
|
|
83
|
-
receive: (message: Message<Record<string, string>>) => void
|
|
84
|
-
}
|
|
62
|
+
export type { Route }
|