@livon/runtime 0.27.0-rc.3 → 0.28.0-rc.4
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/LICENSE.md +21 -0
- package/README.md +166 -5
- package/THIRD_PARTY_NOTICES.md +1 -28
- package/dist/index.cjs +36 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/runtime.cjs +372 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.js +338 -1
- package/dist/types.cjs +18 -1
- package/package.json +25 -24
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LIVON contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,169 @@
|
|
|
1
|
+
<!-- Generated from website/docs/packages/*.md. Do not edit directly. -->
|
|
2
|
+
|
|
1
3
|
# @livon/runtime
|
|
2
4
|
|
|
3
|
-
Package docs (GitHub Pages):
|
|
4
|
-
- https://live-input-vector-output-node.github.io/livon-ts/docs/packages/runtime
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
[](https://www.npmjs.com/package/@livon/runtime)
|
|
7
|
+
[](https://libraries.io/npm/%40livon%2Fruntime)
|
|
8
|
+
[](https://github.com/live-input-vector-output-node/livon-ts/actions/workflows/code-quality.yml)
|
|
9
|
+
[](https://www.npmjs.com/package/@livon/runtime)
|
|
10
|
+
[](https://github.com/live-input-vector-output-node/livon-ts)
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @livon/runtime
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Purpose
|
|
19
|
+
|
|
20
|
+
[@livon/runtime](https://livon.tech/docs/packages/runtime) is the event pipeline core. It composes modules and executes hook chains for:
|
|
21
|
+
|
|
22
|
+
- `onReceive`
|
|
23
|
+
- `onSend`
|
|
24
|
+
- `onError`
|
|
25
|
+
|
|
26
|
+
## Best for
|
|
27
|
+
|
|
28
|
+
Use this package when you need deterministic event flow orchestration across transports and schema execution.
|
|
29
|
+
|
|
30
|
+
## Basic usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import {runtime} from '@livon/runtime';
|
|
34
|
+
|
|
35
|
+
runtime(moduleA, moduleB);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Parameters
|
|
39
|
+
|
|
40
|
+
`runtime(...modules)`:
|
|
41
|
+
|
|
42
|
+
- `modules` (`RuntimeModule[]`): ordered module list to register and execute.
|
|
43
|
+
|
|
44
|
+
`RuntimeModule`:
|
|
45
|
+
|
|
46
|
+
- `name` (`string`): module identifier for debugging and traceability.
|
|
47
|
+
- `register` (`(registry) => void`): setup callback where hooks are registered.
|
|
48
|
+
|
|
49
|
+
## Execution order (important)
|
|
50
|
+
|
|
51
|
+
Runtime module order defines execution order.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
runtime(moduleA, moduleB, moduleC);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Hook chains run from left to right:
|
|
58
|
+
|
|
59
|
+
1. `moduleA`
|
|
60
|
+
2. `moduleB`
|
|
61
|
+
3. `moduleC`
|
|
62
|
+
|
|
63
|
+
This applies to `onReceive`, `onSend`, and `onError` registration order.
|
|
64
|
+
|
|
65
|
+
## Writing a runtime module
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import type {RuntimeModule} from '@livon/runtime';
|
|
69
|
+
|
|
70
|
+
const traceModule: RuntimeModule = {
|
|
71
|
+
name: 'trace',
|
|
72
|
+
register: ({onReceive, onSend, onError}) => {
|
|
73
|
+
onReceive(async (envelope, ctx, next) => {
|
|
74
|
+
return next();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
onSend(async (envelope, ctx, next) => {
|
|
78
|
+
return next();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
onError((error, envelope, ctx) => {
|
|
82
|
+
// error handling
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Hook callback parameters
|
|
89
|
+
|
|
90
|
+
`onReceive((envelope, ctx, next) => ...)` and `onSend((envelope, ctx, next) => ...)`:
|
|
91
|
+
|
|
92
|
+
- `envelope` (`EventEnvelope`): current event envelope flowing through the chain.
|
|
93
|
+
- `ctx` (`RuntimeContext`): emit APIs, room-scoped context, and shared runtime state.
|
|
94
|
+
- `next` (`(update?) => Promise<EventEnvelope>`): continue chain and optionally merge envelope updates.
|
|
95
|
+
|
|
96
|
+
`onError((error, envelope, ctx) => ...)`:
|
|
97
|
+
|
|
98
|
+
- `error` (`unknown`): thrown/normalized error from runtime chain.
|
|
99
|
+
- `envelope` (`EventEnvelope`): failed envelope snapshot.
|
|
100
|
+
- `ctx` (`RuntimeContext`): same runtime context for recovery/reporting logic.
|
|
101
|
+
|
|
102
|
+
## Runtime context model
|
|
103
|
+
|
|
104
|
+
`RuntimeContext` (`ctx` in hooks) is runtime control surface.
|
|
105
|
+
It is separate from `envelope.context` (event data flowing through the pipeline).
|
|
106
|
+
|
|
107
|
+
```mermaid
|
|
108
|
+
flowchart TD
|
|
109
|
+
Runtime["runtime(...)"] --> Ctx["RuntimeContext"]
|
|
110
|
+
Ctx --> EmitReceive["emitReceive(input)"]
|
|
111
|
+
Ctx --> EmitSend["emitSend(input)"]
|
|
112
|
+
Ctx --> EmitError["emitError(input)"]
|
|
113
|
+
Ctx --> EmitEvent["emitEvent(input)"]
|
|
114
|
+
Ctx --> Room["room(name) -> RuntimeContext"]
|
|
115
|
+
Ctx --> State["state.get/set shared map"]
|
|
116
|
+
EmitReceive --> Envelope["EventEnvelope"]
|
|
117
|
+
EmitSend --> Envelope
|
|
118
|
+
EmitError --> Envelope
|
|
119
|
+
EmitEvent --> Envelope
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```mermaid
|
|
123
|
+
sequenceDiagram
|
|
124
|
+
participant M1 as moduleA
|
|
125
|
+
participant R as runtime
|
|
126
|
+
participant M2 as moduleB
|
|
127
|
+
|
|
128
|
+
M1->>R: next({ context: { traceId: "t-1" } })
|
|
129
|
+
R->>R: mergeContext(base, update)
|
|
130
|
+
R->>M2: onReceive(... envelope.context includes traceId)
|
|
131
|
+
M2->>R: throw { message, context: { phase: "auth" } }
|
|
132
|
+
R->>R: buildFailedEnvelope + mergeContext(error.context)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Rules:
|
|
136
|
+
|
|
137
|
+
- `ctx.room(name)` creates a room-scoped context that injects `metadata.room`.
|
|
138
|
+
- `ctx.state` is shared across room scopes for one runtime instance.
|
|
139
|
+
- `envelope.context` is immutable hook input; use `next(update)` to merge context changes.
|
|
140
|
+
|
|
141
|
+
## Emit APIs in runtime context
|
|
142
|
+
|
|
143
|
+
Inside hooks/modules you can emit:
|
|
144
|
+
|
|
145
|
+
- `ctx.emitReceive(...)`
|
|
146
|
+
- `ctx.emitSend(...)`
|
|
147
|
+
- `ctx.emitError(...)`
|
|
148
|
+
- `ctx.emitEvent(...)` (alias to send path)
|
|
149
|
+
|
|
150
|
+
You can also scope to rooms via `ctx.room(roomId)`.
|
|
151
|
+
|
|
152
|
+
### Emit input parameters
|
|
153
|
+
|
|
154
|
+
`emitReceive`, `emitSend`, `emitError`, `emitEvent` all accept one `EmitInput`:
|
|
155
|
+
|
|
156
|
+
- `event` (`string`, required): event name.
|
|
157
|
+
- `payload` (`Uint8Array`, required unless `error` is provided): transport payload.
|
|
158
|
+
- `error` (`EventError`, required unless `payload` is provided): error payload.
|
|
159
|
+
- `id` (`string`, optional): envelope id override.
|
|
160
|
+
- `status` (`'sending' | 'receiving' | 'failed'`, optional): explicit status override.
|
|
161
|
+
- `metadata` (`Record<string, unknown>`, optional): routing/correlation metadata.
|
|
162
|
+
- `context` (`RuntimeEventContext`, optional): module context object merged across pipeline.
|
|
163
|
+
|
|
164
|
+
## Related pages
|
|
165
|
+
|
|
166
|
+
- [Validated by Default](https://livon.tech/docs/core/validated-by-default)
|
|
167
|
+
- [@livon/schema](https://livon.tech/docs/packages/schema)
|
|
168
|
+
- [Runtime Design](https://livon.tech/docs/technical/runtime-design)
|
|
169
|
+
- [Event Flow](https://livon.tech/docs/technical/event-flow)
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -1,28 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
This package includes third-party software. The following licenses apply:
|
|
4
|
-
|
|
5
|
-
------------------------------------------------------------------------------
|
|
6
|
-
msgpackr
|
|
7
|
-
------------------------------------------------------------------------------
|
|
8
|
-
License: MIT
|
|
9
|
-
|
|
10
|
-
Copyright (c) 2020 Kris Zyp
|
|
11
|
-
|
|
12
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
-
in the Software without restriction, including without limitation the rights
|
|
15
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
-
furnished to do so, subject to the following conditions:
|
|
18
|
-
|
|
19
|
-
The above copyright notice and this permission notice shall be included in all
|
|
20
|
-
copies or substantial portions of the Software.
|
|
21
|
-
|
|
22
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
-
SOFTWARE.
|
|
1
|
+
No third-party notices are required for this package at this time.
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
runtime: ()=>external_runtime_cjs_namespaceObject.runtime
|
|
28
|
+
});
|
|
29
|
+
const external_runtime_cjs_namespaceObject = require("./runtime.cjs");
|
|
30
|
+
exports.runtime = __webpack_exports__.runtime;
|
|
31
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
32
|
+
"runtime"
|
|
33
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
34
|
+
Object.defineProperty(exports, '__esModule', {
|
|
35
|
+
value: true
|
|
36
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Public package entrypoint for `@livon/runtime`.
|
|
3
3
|
*
|
|
4
|
-
* @see https://
|
|
4
|
+
* @see https://livon.tech/docs/packages/runtime
|
|
5
5
|
*/
|
|
6
6
|
export { runtime } from './runtime.js';
|
|
7
7
|
export type { RuntimeEventContext, EventStatus, EventError, EventEnvelopeBase, EventEnvelopePayload, EventEnvelopeError, EventEnvelope, EventAck, EmitInputBase, EmitInputPayload, EmitInputError, EmitInput, EmitEvent, EmitReceive, EmitSend, EmitError, RoomSelector, StateGet, StateSet, RuntimeState, RuntimeContext, RuntimeHook, RuntimeModule, RuntimeOnError, RuntimeInput, RuntimeModuleRegister, RuntimeHookRegister, RuntimeOnErrorRegister, RuntimeHookSubscription, RuntimeUnsubscribe, RuntimeRegistry, RuntimeStart, PartialEventEnvelopeBase, PartialEventEnvelopePayload, PartialEventEnvelopeError, PartialEventEnvelopeEmpty, PartialEventEnvelope, } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
import{runtime
|
|
1
|
+
import { runtime } from "./runtime.js";
|
|
2
|
+
export { runtime };
|
package/dist/runtime.cjs
CHANGED
|
@@ -1 +1,372 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
runtime: ()=>runtime
|
|
28
|
+
});
|
|
29
|
+
const createHookStore = ()=>({
|
|
30
|
+
onReceive: [],
|
|
31
|
+
onSend: [],
|
|
32
|
+
onError: []
|
|
33
|
+
});
|
|
34
|
+
const createState = ()=>{
|
|
35
|
+
const store = new Map();
|
|
36
|
+
const get = (key)=>store.get(key);
|
|
37
|
+
const set = (key, value)=>{
|
|
38
|
+
store.set(key, value);
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
get,
|
|
42
|
+
set
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const mergeMetadata = (base, extra)=>{
|
|
46
|
+
if (!base && !extra) return;
|
|
47
|
+
return {
|
|
48
|
+
...base ?? {},
|
|
49
|
+
...extra ?? {}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
let fallbackEventIdCounter = 0;
|
|
53
|
+
const createEventId = ()=>{
|
|
54
|
+
const cryptoValue = globalThis.crypto;
|
|
55
|
+
if (cryptoValue?.randomUUID) return cryptoValue.randomUUID();
|
|
56
|
+
fallbackEventIdCounter += 1;
|
|
57
|
+
return `evt_${Date.now().toString(36)}_${fallbackEventIdCounter.toString(36)}`;
|
|
58
|
+
};
|
|
59
|
+
const isContextRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
60
|
+
const contextRecordFrom = (value)=>{
|
|
61
|
+
if (!value || !isContextRecord(value)) return;
|
|
62
|
+
return value;
|
|
63
|
+
};
|
|
64
|
+
const errorFromUnknown = (error)=>{
|
|
65
|
+
if (error instanceof Error) {
|
|
66
|
+
const context = isContextRecord(error.context) ? error.context : void 0;
|
|
67
|
+
return {
|
|
68
|
+
message: error.message,
|
|
69
|
+
...error.name ? {
|
|
70
|
+
name: error.name
|
|
71
|
+
} : {},
|
|
72
|
+
...error.stack ? {
|
|
73
|
+
stack: error.stack
|
|
74
|
+
} : {},
|
|
75
|
+
...context ? {
|
|
76
|
+
context
|
|
77
|
+
} : {}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if ('string' == typeof error) return {
|
|
81
|
+
message: error
|
|
82
|
+
};
|
|
83
|
+
if ('object' == typeof error && null !== error) {
|
|
84
|
+
const value = error;
|
|
85
|
+
const message = 'string' == typeof value.message ? value.message : 'Unknown error';
|
|
86
|
+
const name = 'string' == typeof value.name ? value.name : void 0;
|
|
87
|
+
const stack = 'string' == typeof value.stack ? value.stack : void 0;
|
|
88
|
+
const context = isContextRecord(value.context) ? value.context : void 0;
|
|
89
|
+
return {
|
|
90
|
+
message,
|
|
91
|
+
...name ? {
|
|
92
|
+
name
|
|
93
|
+
} : {},
|
|
94
|
+
...stack ? {
|
|
95
|
+
stack
|
|
96
|
+
} : {},
|
|
97
|
+
...context ? {
|
|
98
|
+
context
|
|
99
|
+
} : {}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
message: 'Unknown error'
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
const registerHook = (hooks)=>(hook)=>{
|
|
107
|
+
hooks.push(hook);
|
|
108
|
+
const unsub = ()=>{
|
|
109
|
+
const index = hooks.indexOf(hook);
|
|
110
|
+
if (index >= 0) hooks.splice(index, 1);
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
unsub
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
const registerErrorHook = (hooks)=>(hook)=>{
|
|
117
|
+
hooks.push(hook);
|
|
118
|
+
const unsub = ()=>{
|
|
119
|
+
const index = hooks.indexOf(hook);
|
|
120
|
+
if (index >= 0) hooks.splice(index, 1);
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
unsub
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
const runtime = (inputOrModule, ...rest)=>{
|
|
127
|
+
const input = 'modules' in inputOrModule ? inputOrModule : {
|
|
128
|
+
modules: [
|
|
129
|
+
inputOrModule,
|
|
130
|
+
...rest
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
const modules = input.modules;
|
|
134
|
+
const state = createState();
|
|
135
|
+
const hooks = createHookStore();
|
|
136
|
+
const onReceive = registerHook(hooks.onReceive);
|
|
137
|
+
const onSend = registerHook(hooks.onSend);
|
|
138
|
+
const onError = registerErrorHook(hooks.onError);
|
|
139
|
+
const emitErrorHooks = ({ runtimeError, eventEnvelope, runtimeContext })=>{
|
|
140
|
+
hooks.onError.forEach((handler)=>{
|
|
141
|
+
try {
|
|
142
|
+
handler(runtimeError, eventEnvelope, runtimeContext);
|
|
143
|
+
} catch {}
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
const mergeContext = (base, update)=>{
|
|
147
|
+
const left = contextRecordFrom(base);
|
|
148
|
+
const right = contextRecordFrom(update);
|
|
149
|
+
if (!left && !right) return;
|
|
150
|
+
return {
|
|
151
|
+
...left ?? {},
|
|
152
|
+
...right ?? {}
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
const payloadFromEvent = (event)=>event.payload;
|
|
156
|
+
const errorFromEvent = (event)=>event.error;
|
|
157
|
+
const mergeEvent = (base, update)=>{
|
|
158
|
+
if (!update) return base;
|
|
159
|
+
const nextId = update.id ?? base.id;
|
|
160
|
+
const nextEvent = update.event ?? base.event;
|
|
161
|
+
const nextStatus = update.status ?? base.status;
|
|
162
|
+
const nextMetadata = mergeMetadata(base.metadata, update.metadata);
|
|
163
|
+
const nextContext = mergeContext(base.context, update.context);
|
|
164
|
+
const nextPayload = payloadFromEvent(update) ?? payloadFromEvent(base);
|
|
165
|
+
const nextError = errorFromEvent(update) ?? errorFromEvent(base);
|
|
166
|
+
if (void 0 === nextPayload && void 0 === nextError) throw new Error('Event envelope must contain payload or error.');
|
|
167
|
+
const mergedEnvelopeBase = {
|
|
168
|
+
...base,
|
|
169
|
+
id: nextId,
|
|
170
|
+
event: nextEvent,
|
|
171
|
+
status: nextStatus,
|
|
172
|
+
metadata: nextMetadata,
|
|
173
|
+
context: nextContext
|
|
174
|
+
};
|
|
175
|
+
if (void 0 !== nextPayload && void 0 !== nextError) return {
|
|
176
|
+
...mergedEnvelopeBase,
|
|
177
|
+
payload: nextPayload,
|
|
178
|
+
error: nextError
|
|
179
|
+
};
|
|
180
|
+
if (void 0 !== nextPayload) return {
|
|
181
|
+
...mergedEnvelopeBase,
|
|
182
|
+
payload: nextPayload
|
|
183
|
+
};
|
|
184
|
+
const definedError = nextError;
|
|
185
|
+
return {
|
|
186
|
+
...mergedEnvelopeBase,
|
|
187
|
+
error: definedError
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
const buildEnvelope = ({ emitInput, fallbackStatus, roomName })=>{
|
|
191
|
+
const { id: inputId, event, status: inputStatus, metadata: inputMetadata, context, payload, error } = emitInput;
|
|
192
|
+
const roomMeta = roomName ? {
|
|
193
|
+
room: roomName
|
|
194
|
+
} : void 0;
|
|
195
|
+
const metadata = mergeMetadata(roomMeta, inputMetadata);
|
|
196
|
+
const id = inputId ?? createEventId();
|
|
197
|
+
const status = inputStatus ?? fallbackStatus;
|
|
198
|
+
if (void 0 === payload && void 0 === error) throw new Error('Emit input must contain payload or error.');
|
|
199
|
+
const envelopeBase = {
|
|
200
|
+
id,
|
|
201
|
+
event,
|
|
202
|
+
status,
|
|
203
|
+
metadata,
|
|
204
|
+
context
|
|
205
|
+
};
|
|
206
|
+
if (void 0 !== payload && void 0 !== error) return {
|
|
207
|
+
...envelopeBase,
|
|
208
|
+
payload,
|
|
209
|
+
error
|
|
210
|
+
};
|
|
211
|
+
if (void 0 !== payload) return {
|
|
212
|
+
...envelopeBase,
|
|
213
|
+
payload
|
|
214
|
+
};
|
|
215
|
+
const definedError = error;
|
|
216
|
+
return {
|
|
217
|
+
...envelopeBase,
|
|
218
|
+
error: definedError
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
const buildFailedEnvelope = ({ runtimeError, eventEnvelope })=>{
|
|
222
|
+
const normalizedError = errorFromUnknown(runtimeError);
|
|
223
|
+
const nextContext = mergeContext(eventEnvelope.context, normalizedError.context);
|
|
224
|
+
return {
|
|
225
|
+
...eventEnvelope,
|
|
226
|
+
error: normalizedError,
|
|
227
|
+
context: nextContext
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
const invokeHook = (input)=>{
|
|
231
|
+
const hook = input.hookChain[input.hookIndex];
|
|
232
|
+
if (!hook) return Promise.resolve(input.eventEnvelope);
|
|
233
|
+
let nextPromise;
|
|
234
|
+
let nextCalled = false;
|
|
235
|
+
const next = (update)=>{
|
|
236
|
+
nextCalled = true;
|
|
237
|
+
nextPromise = invokeHook({
|
|
238
|
+
hookChain: input.hookChain,
|
|
239
|
+
hookIndex: input.hookIndex + 1,
|
|
240
|
+
eventEnvelope: mergeEvent(input.eventEnvelope, update),
|
|
241
|
+
runtimeContext: input.runtimeContext
|
|
242
|
+
});
|
|
243
|
+
return nextPromise;
|
|
244
|
+
};
|
|
245
|
+
const result = hook(input.eventEnvelope, input.runtimeContext, next);
|
|
246
|
+
if (void 0 === result && nextCalled) return nextPromise ?? Promise.resolve(input.eventEnvelope);
|
|
247
|
+
return Promise.resolve(result).then((value)=>value ?? nextPromise ?? input.eventEnvelope);
|
|
248
|
+
};
|
|
249
|
+
const runReceive = (eventEnvelope, runtimeContext)=>invokeHook({
|
|
250
|
+
hookChain: hooks.onReceive,
|
|
251
|
+
hookIndex: 0,
|
|
252
|
+
eventEnvelope,
|
|
253
|
+
runtimeContext
|
|
254
|
+
});
|
|
255
|
+
const runSend = (eventEnvelope, runtimeContext)=>invokeHook({
|
|
256
|
+
hookChain: hooks.onSend,
|
|
257
|
+
hookIndex: 0,
|
|
258
|
+
eventEnvelope,
|
|
259
|
+
runtimeContext
|
|
260
|
+
});
|
|
261
|
+
const createEmitError = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
262
|
+
const envelope = buildEnvelope({
|
|
263
|
+
emitInput,
|
|
264
|
+
fallbackStatus: 'sending',
|
|
265
|
+
roomName
|
|
266
|
+
});
|
|
267
|
+
const fallbackError = envelope.error ?? {
|
|
268
|
+
message: 'Unknown error'
|
|
269
|
+
};
|
|
270
|
+
const failedEnvelope = {
|
|
271
|
+
...envelope,
|
|
272
|
+
error: fallbackError
|
|
273
|
+
};
|
|
274
|
+
emitErrorHooks({
|
|
275
|
+
runtimeError: fallbackError,
|
|
276
|
+
eventEnvelope: failedEnvelope,
|
|
277
|
+
runtimeContext
|
|
278
|
+
});
|
|
279
|
+
return {
|
|
280
|
+
ok: true
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
const createEmitReceive = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
284
|
+
const envelope = buildEnvelope({
|
|
285
|
+
emitInput,
|
|
286
|
+
fallbackStatus: 'receiving',
|
|
287
|
+
roomName
|
|
288
|
+
});
|
|
289
|
+
try {
|
|
290
|
+
await runReceive(envelope, runtimeContext);
|
|
291
|
+
return {
|
|
292
|
+
ok: true
|
|
293
|
+
};
|
|
294
|
+
} catch (error) {
|
|
295
|
+
const failedEnvelope = buildFailedEnvelope({
|
|
296
|
+
runtimeError: error,
|
|
297
|
+
eventEnvelope: envelope
|
|
298
|
+
});
|
|
299
|
+
emitErrorHooks({
|
|
300
|
+
runtimeError: error,
|
|
301
|
+
eventEnvelope: failedEnvelope,
|
|
302
|
+
runtimeContext
|
|
303
|
+
});
|
|
304
|
+
return {
|
|
305
|
+
ok: false,
|
|
306
|
+
error: failedEnvelope.error.message
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const createEmitSend = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
311
|
+
const envelope = buildEnvelope({
|
|
312
|
+
emitInput,
|
|
313
|
+
fallbackStatus: 'sending',
|
|
314
|
+
roomName
|
|
315
|
+
});
|
|
316
|
+
try {
|
|
317
|
+
await runSend(envelope, runtimeContext);
|
|
318
|
+
return {
|
|
319
|
+
ok: true
|
|
320
|
+
};
|
|
321
|
+
} catch (error) {
|
|
322
|
+
const failedEnvelope = buildFailedEnvelope({
|
|
323
|
+
runtimeError: error,
|
|
324
|
+
eventEnvelope: envelope
|
|
325
|
+
});
|
|
326
|
+
emitErrorHooks({
|
|
327
|
+
runtimeError: error,
|
|
328
|
+
eventEnvelope: failedEnvelope,
|
|
329
|
+
runtimeContext
|
|
330
|
+
});
|
|
331
|
+
return {
|
|
332
|
+
ok: false,
|
|
333
|
+
error: failedEnvelope.error.message
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const createEmitEvent = (input)=>async (eventInput)=>createEmitSend(input)(eventInput);
|
|
338
|
+
const createContext = (roomName)=>{
|
|
339
|
+
const runtimeContext = {};
|
|
340
|
+
const createEmitInput = {
|
|
341
|
+
runtimeContext,
|
|
342
|
+
roomName
|
|
343
|
+
};
|
|
344
|
+
runtimeContext.emitReceive = createEmitReceive(createEmitInput);
|
|
345
|
+
runtimeContext.emitSend = createEmitSend(createEmitInput);
|
|
346
|
+
runtimeContext.emitError = createEmitError(createEmitInput);
|
|
347
|
+
runtimeContext.emitEvent = createEmitEvent(createEmitInput);
|
|
348
|
+
runtimeContext.room = (nextRoomName)=>createContext(nextRoomName);
|
|
349
|
+
runtimeContext.state = state;
|
|
350
|
+
return runtimeContext;
|
|
351
|
+
};
|
|
352
|
+
const baseContext = createContext();
|
|
353
|
+
const registry = {
|
|
354
|
+
emitReceive: baseContext.emitReceive,
|
|
355
|
+
emitSend: baseContext.emitSend,
|
|
356
|
+
emitError: baseContext.emitError,
|
|
357
|
+
onReceive,
|
|
358
|
+
onSend,
|
|
359
|
+
onError,
|
|
360
|
+
state
|
|
361
|
+
};
|
|
362
|
+
modules.forEach((mod)=>{
|
|
363
|
+
mod.register(registry);
|
|
364
|
+
});
|
|
365
|
+
};
|
|
366
|
+
exports.runtime = __webpack_exports__.runtime;
|
|
367
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
368
|
+
"runtime"
|
|
369
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
370
|
+
Object.defineProperty(exports, '__esModule', {
|
|
371
|
+
value: true
|
|
372
|
+
});
|
package/dist/runtime.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { RuntimeStart } from './types.js';
|
|
|
5
5
|
* @remarks
|
|
6
6
|
* Parameter and return types are defined in the TypeScript signature.
|
|
7
7
|
*
|
|
8
|
-
* @see https://
|
|
8
|
+
* @see https://livon.tech/docs/packages/runtime
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* const result = runtime(undefined as never);
|
package/dist/runtime.js
CHANGED
|
@@ -1 +1,338 @@
|
|
|
1
|
-
|
|
1
|
+
const createHookStore = ()=>({
|
|
2
|
+
onReceive: [],
|
|
3
|
+
onSend: [],
|
|
4
|
+
onError: []
|
|
5
|
+
});
|
|
6
|
+
const createState = ()=>{
|
|
7
|
+
const store = new Map();
|
|
8
|
+
const get = (key)=>store.get(key);
|
|
9
|
+
const set = (key, value)=>{
|
|
10
|
+
store.set(key, value);
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
get,
|
|
14
|
+
set
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const mergeMetadata = (base, extra)=>{
|
|
18
|
+
if (!base && !extra) return;
|
|
19
|
+
return {
|
|
20
|
+
...base ?? {},
|
|
21
|
+
...extra ?? {}
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
let fallbackEventIdCounter = 0;
|
|
25
|
+
const createEventId = ()=>{
|
|
26
|
+
const cryptoValue = globalThis.crypto;
|
|
27
|
+
if (cryptoValue?.randomUUID) return cryptoValue.randomUUID();
|
|
28
|
+
fallbackEventIdCounter += 1;
|
|
29
|
+
return `evt_${Date.now().toString(36)}_${fallbackEventIdCounter.toString(36)}`;
|
|
30
|
+
};
|
|
31
|
+
const isContextRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
32
|
+
const contextRecordFrom = (value)=>{
|
|
33
|
+
if (!value || !isContextRecord(value)) return;
|
|
34
|
+
return value;
|
|
35
|
+
};
|
|
36
|
+
const errorFromUnknown = (error)=>{
|
|
37
|
+
if (error instanceof Error) {
|
|
38
|
+
const context = isContextRecord(error.context) ? error.context : void 0;
|
|
39
|
+
return {
|
|
40
|
+
message: error.message,
|
|
41
|
+
...error.name ? {
|
|
42
|
+
name: error.name
|
|
43
|
+
} : {},
|
|
44
|
+
...error.stack ? {
|
|
45
|
+
stack: error.stack
|
|
46
|
+
} : {},
|
|
47
|
+
...context ? {
|
|
48
|
+
context
|
|
49
|
+
} : {}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if ('string' == typeof error) return {
|
|
53
|
+
message: error
|
|
54
|
+
};
|
|
55
|
+
if ('object' == typeof error && null !== error) {
|
|
56
|
+
const value = error;
|
|
57
|
+
const message = 'string' == typeof value.message ? value.message : 'Unknown error';
|
|
58
|
+
const name = 'string' == typeof value.name ? value.name : void 0;
|
|
59
|
+
const stack = 'string' == typeof value.stack ? value.stack : void 0;
|
|
60
|
+
const context = isContextRecord(value.context) ? value.context : void 0;
|
|
61
|
+
return {
|
|
62
|
+
message,
|
|
63
|
+
...name ? {
|
|
64
|
+
name
|
|
65
|
+
} : {},
|
|
66
|
+
...stack ? {
|
|
67
|
+
stack
|
|
68
|
+
} : {},
|
|
69
|
+
...context ? {
|
|
70
|
+
context
|
|
71
|
+
} : {}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
message: 'Unknown error'
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
const registerHook = (hooks)=>(hook)=>{
|
|
79
|
+
hooks.push(hook);
|
|
80
|
+
const unsub = ()=>{
|
|
81
|
+
const index = hooks.indexOf(hook);
|
|
82
|
+
if (index >= 0) hooks.splice(index, 1);
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
unsub
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const registerErrorHook = (hooks)=>(hook)=>{
|
|
89
|
+
hooks.push(hook);
|
|
90
|
+
const unsub = ()=>{
|
|
91
|
+
const index = hooks.indexOf(hook);
|
|
92
|
+
if (index >= 0) hooks.splice(index, 1);
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
unsub
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
const runtime = (inputOrModule, ...rest)=>{
|
|
99
|
+
const input = 'modules' in inputOrModule ? inputOrModule : {
|
|
100
|
+
modules: [
|
|
101
|
+
inputOrModule,
|
|
102
|
+
...rest
|
|
103
|
+
]
|
|
104
|
+
};
|
|
105
|
+
const modules = input.modules;
|
|
106
|
+
const state = createState();
|
|
107
|
+
const hooks = createHookStore();
|
|
108
|
+
const onReceive = registerHook(hooks.onReceive);
|
|
109
|
+
const onSend = registerHook(hooks.onSend);
|
|
110
|
+
const onError = registerErrorHook(hooks.onError);
|
|
111
|
+
const emitErrorHooks = ({ runtimeError, eventEnvelope, runtimeContext })=>{
|
|
112
|
+
hooks.onError.forEach((handler)=>{
|
|
113
|
+
try {
|
|
114
|
+
handler(runtimeError, eventEnvelope, runtimeContext);
|
|
115
|
+
} catch {}
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
const mergeContext = (base, update)=>{
|
|
119
|
+
const left = contextRecordFrom(base);
|
|
120
|
+
const right = contextRecordFrom(update);
|
|
121
|
+
if (!left && !right) return;
|
|
122
|
+
return {
|
|
123
|
+
...left ?? {},
|
|
124
|
+
...right ?? {}
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
const payloadFromEvent = (event)=>event.payload;
|
|
128
|
+
const errorFromEvent = (event)=>event.error;
|
|
129
|
+
const mergeEvent = (base, update)=>{
|
|
130
|
+
if (!update) return base;
|
|
131
|
+
const nextId = update.id ?? base.id;
|
|
132
|
+
const nextEvent = update.event ?? base.event;
|
|
133
|
+
const nextStatus = update.status ?? base.status;
|
|
134
|
+
const nextMetadata = mergeMetadata(base.metadata, update.metadata);
|
|
135
|
+
const nextContext = mergeContext(base.context, update.context);
|
|
136
|
+
const nextPayload = payloadFromEvent(update) ?? payloadFromEvent(base);
|
|
137
|
+
const nextError = errorFromEvent(update) ?? errorFromEvent(base);
|
|
138
|
+
if (void 0 === nextPayload && void 0 === nextError) throw new Error('Event envelope must contain payload or error.');
|
|
139
|
+
const mergedEnvelopeBase = {
|
|
140
|
+
...base,
|
|
141
|
+
id: nextId,
|
|
142
|
+
event: nextEvent,
|
|
143
|
+
status: nextStatus,
|
|
144
|
+
metadata: nextMetadata,
|
|
145
|
+
context: nextContext
|
|
146
|
+
};
|
|
147
|
+
if (void 0 !== nextPayload && void 0 !== nextError) return {
|
|
148
|
+
...mergedEnvelopeBase,
|
|
149
|
+
payload: nextPayload,
|
|
150
|
+
error: nextError
|
|
151
|
+
};
|
|
152
|
+
if (void 0 !== nextPayload) return {
|
|
153
|
+
...mergedEnvelopeBase,
|
|
154
|
+
payload: nextPayload
|
|
155
|
+
};
|
|
156
|
+
const definedError = nextError;
|
|
157
|
+
return {
|
|
158
|
+
...mergedEnvelopeBase,
|
|
159
|
+
error: definedError
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
const buildEnvelope = ({ emitInput, fallbackStatus, roomName })=>{
|
|
163
|
+
const { id: inputId, event, status: inputStatus, metadata: inputMetadata, context, payload, error } = emitInput;
|
|
164
|
+
const roomMeta = roomName ? {
|
|
165
|
+
room: roomName
|
|
166
|
+
} : void 0;
|
|
167
|
+
const metadata = mergeMetadata(roomMeta, inputMetadata);
|
|
168
|
+
const id = inputId ?? createEventId();
|
|
169
|
+
const status = inputStatus ?? fallbackStatus;
|
|
170
|
+
if (void 0 === payload && void 0 === error) throw new Error('Emit input must contain payload or error.');
|
|
171
|
+
const envelopeBase = {
|
|
172
|
+
id,
|
|
173
|
+
event,
|
|
174
|
+
status,
|
|
175
|
+
metadata,
|
|
176
|
+
context
|
|
177
|
+
};
|
|
178
|
+
if (void 0 !== payload && void 0 !== error) return {
|
|
179
|
+
...envelopeBase,
|
|
180
|
+
payload,
|
|
181
|
+
error
|
|
182
|
+
};
|
|
183
|
+
if (void 0 !== payload) return {
|
|
184
|
+
...envelopeBase,
|
|
185
|
+
payload
|
|
186
|
+
};
|
|
187
|
+
const definedError = error;
|
|
188
|
+
return {
|
|
189
|
+
...envelopeBase,
|
|
190
|
+
error: definedError
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
const buildFailedEnvelope = ({ runtimeError, eventEnvelope })=>{
|
|
194
|
+
const normalizedError = errorFromUnknown(runtimeError);
|
|
195
|
+
const nextContext = mergeContext(eventEnvelope.context, normalizedError.context);
|
|
196
|
+
return {
|
|
197
|
+
...eventEnvelope,
|
|
198
|
+
error: normalizedError,
|
|
199
|
+
context: nextContext
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
const invokeHook = (input)=>{
|
|
203
|
+
const hook = input.hookChain[input.hookIndex];
|
|
204
|
+
if (!hook) return Promise.resolve(input.eventEnvelope);
|
|
205
|
+
let nextPromise;
|
|
206
|
+
let nextCalled = false;
|
|
207
|
+
const next = (update)=>{
|
|
208
|
+
nextCalled = true;
|
|
209
|
+
nextPromise = invokeHook({
|
|
210
|
+
hookChain: input.hookChain,
|
|
211
|
+
hookIndex: input.hookIndex + 1,
|
|
212
|
+
eventEnvelope: mergeEvent(input.eventEnvelope, update),
|
|
213
|
+
runtimeContext: input.runtimeContext
|
|
214
|
+
});
|
|
215
|
+
return nextPromise;
|
|
216
|
+
};
|
|
217
|
+
const result = hook(input.eventEnvelope, input.runtimeContext, next);
|
|
218
|
+
if (void 0 === result && nextCalled) return nextPromise ?? Promise.resolve(input.eventEnvelope);
|
|
219
|
+
return Promise.resolve(result).then((value)=>value ?? nextPromise ?? input.eventEnvelope);
|
|
220
|
+
};
|
|
221
|
+
const runReceive = (eventEnvelope, runtimeContext)=>invokeHook({
|
|
222
|
+
hookChain: hooks.onReceive,
|
|
223
|
+
hookIndex: 0,
|
|
224
|
+
eventEnvelope,
|
|
225
|
+
runtimeContext
|
|
226
|
+
});
|
|
227
|
+
const runSend = (eventEnvelope, runtimeContext)=>invokeHook({
|
|
228
|
+
hookChain: hooks.onSend,
|
|
229
|
+
hookIndex: 0,
|
|
230
|
+
eventEnvelope,
|
|
231
|
+
runtimeContext
|
|
232
|
+
});
|
|
233
|
+
const createEmitError = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
234
|
+
const envelope = buildEnvelope({
|
|
235
|
+
emitInput,
|
|
236
|
+
fallbackStatus: 'sending',
|
|
237
|
+
roomName
|
|
238
|
+
});
|
|
239
|
+
const fallbackError = envelope.error ?? {
|
|
240
|
+
message: 'Unknown error'
|
|
241
|
+
};
|
|
242
|
+
const failedEnvelope = {
|
|
243
|
+
...envelope,
|
|
244
|
+
error: fallbackError
|
|
245
|
+
};
|
|
246
|
+
emitErrorHooks({
|
|
247
|
+
runtimeError: fallbackError,
|
|
248
|
+
eventEnvelope: failedEnvelope,
|
|
249
|
+
runtimeContext
|
|
250
|
+
});
|
|
251
|
+
return {
|
|
252
|
+
ok: true
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
const createEmitReceive = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
256
|
+
const envelope = buildEnvelope({
|
|
257
|
+
emitInput,
|
|
258
|
+
fallbackStatus: 'receiving',
|
|
259
|
+
roomName
|
|
260
|
+
});
|
|
261
|
+
try {
|
|
262
|
+
await runReceive(envelope, runtimeContext);
|
|
263
|
+
return {
|
|
264
|
+
ok: true
|
|
265
|
+
};
|
|
266
|
+
} catch (error) {
|
|
267
|
+
const failedEnvelope = buildFailedEnvelope({
|
|
268
|
+
runtimeError: error,
|
|
269
|
+
eventEnvelope: envelope
|
|
270
|
+
});
|
|
271
|
+
emitErrorHooks({
|
|
272
|
+
runtimeError: error,
|
|
273
|
+
eventEnvelope: failedEnvelope,
|
|
274
|
+
runtimeContext
|
|
275
|
+
});
|
|
276
|
+
return {
|
|
277
|
+
ok: false,
|
|
278
|
+
error: failedEnvelope.error.message
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
const createEmitSend = ({ runtimeContext, roomName })=>async (emitInput)=>{
|
|
283
|
+
const envelope = buildEnvelope({
|
|
284
|
+
emitInput,
|
|
285
|
+
fallbackStatus: 'sending',
|
|
286
|
+
roomName
|
|
287
|
+
});
|
|
288
|
+
try {
|
|
289
|
+
await runSend(envelope, runtimeContext);
|
|
290
|
+
return {
|
|
291
|
+
ok: true
|
|
292
|
+
};
|
|
293
|
+
} catch (error) {
|
|
294
|
+
const failedEnvelope = buildFailedEnvelope({
|
|
295
|
+
runtimeError: error,
|
|
296
|
+
eventEnvelope: envelope
|
|
297
|
+
});
|
|
298
|
+
emitErrorHooks({
|
|
299
|
+
runtimeError: error,
|
|
300
|
+
eventEnvelope: failedEnvelope,
|
|
301
|
+
runtimeContext
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
ok: false,
|
|
305
|
+
error: failedEnvelope.error.message
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
const createEmitEvent = (input)=>async (eventInput)=>createEmitSend(input)(eventInput);
|
|
310
|
+
const createContext = (roomName)=>{
|
|
311
|
+
const runtimeContext = {};
|
|
312
|
+
const createEmitInput = {
|
|
313
|
+
runtimeContext,
|
|
314
|
+
roomName
|
|
315
|
+
};
|
|
316
|
+
runtimeContext.emitReceive = createEmitReceive(createEmitInput);
|
|
317
|
+
runtimeContext.emitSend = createEmitSend(createEmitInput);
|
|
318
|
+
runtimeContext.emitError = createEmitError(createEmitInput);
|
|
319
|
+
runtimeContext.emitEvent = createEmitEvent(createEmitInput);
|
|
320
|
+
runtimeContext.room = (nextRoomName)=>createContext(nextRoomName);
|
|
321
|
+
runtimeContext.state = state;
|
|
322
|
+
return runtimeContext;
|
|
323
|
+
};
|
|
324
|
+
const baseContext = createContext();
|
|
325
|
+
const registry = {
|
|
326
|
+
emitReceive: baseContext.emitReceive,
|
|
327
|
+
emitSend: baseContext.emitSend,
|
|
328
|
+
emitError: baseContext.emitError,
|
|
329
|
+
onReceive,
|
|
330
|
+
onSend,
|
|
331
|
+
onError,
|
|
332
|
+
state
|
|
333
|
+
};
|
|
334
|
+
modules.forEach((mod)=>{
|
|
335
|
+
mod.register(registry);
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
export { runtime };
|
package/dist/types.cjs
CHANGED
|
@@ -1 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
package/package.json
CHANGED
|
@@ -1,42 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livon/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0-rc.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
+
"description": "Deterministic event pipeline runtime for LIVON.",
|
|
6
7
|
"publishConfig": {
|
|
7
8
|
"access": "public"
|
|
8
9
|
},
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
9
13
|
"files": [
|
|
10
14
|
"dist",
|
|
11
15
|
"README.md",
|
|
16
|
+
"LICENSE.md",
|
|
12
17
|
"THIRD_PARTY_NOTICES.md"
|
|
13
18
|
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/live-input-vector-output-node/livon-ts.git",
|
|
23
|
+
"directory": "packages/runtime"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://livon.tech/docs/packages/runtime",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/live-input-vector-output-node/livon-ts/issues"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"livon",
|
|
31
|
+
"runtime",
|
|
32
|
+
"api",
|
|
33
|
+
"events",
|
|
34
|
+
"typescript"
|
|
35
|
+
],
|
|
14
36
|
"exports": {
|
|
15
37
|
".": {
|
|
16
|
-
"
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
17
39
|
"import": "./dist/index.js",
|
|
18
|
-
"require": "./dist/index.cjs"
|
|
19
|
-
"types": "./dist/index.d.ts"
|
|
40
|
+
"require": "./dist/index.cjs"
|
|
20
41
|
}
|
|
21
|
-
},
|
|
22
|
-
"dependencies": {},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@rslib/core": "latest",
|
|
25
|
-
"@typescript-eslint/eslint-plugin": "8.54.0",
|
|
26
|
-
"@typescript-eslint/parser": "8.54.0",
|
|
27
|
-
"eslint": "9.0.0",
|
|
28
|
-
"typescript": "latest",
|
|
29
|
-
"vitest": "latest",
|
|
30
|
-
"@livon/config": "0.27.0-rc.3"
|
|
31
|
-
},
|
|
32
|
-
"scripts": {
|
|
33
|
-
"build": "rslib build",
|
|
34
|
-
"build:watch": "rslib dev",
|
|
35
|
-
"dev": "true",
|
|
36
|
-
"lint": "eslint .",
|
|
37
|
-
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
38
|
-
"test": "vitest run -c vitest.unit.config.ts",
|
|
39
|
-
"test:unit": "vitest run -c vitest.unit.config.ts",
|
|
40
|
-
"test:integration": "vitest run -c vitest.integration.config.ts"
|
|
41
42
|
}
|
|
42
43
|
}
|