@lokvis/runtime 0.1.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 (61) hide show
  1. package/dist/asset-store.d.ts +72 -0
  2. package/dist/asset-store.d.ts.map +1 -0
  3. package/dist/asset-store.js +167 -0
  4. package/dist/asset-store.js.map +1 -0
  5. package/dist/capability-registry.d.ts +38 -0
  6. package/dist/capability-registry.d.ts.map +1 -0
  7. package/dist/capability-registry.js +100 -0
  8. package/dist/capability-registry.js.map +1 -0
  9. package/dist/event-bus.d.ts +9 -0
  10. package/dist/event-bus.d.ts.map +1 -0
  11. package/dist/event-bus.js +33 -0
  12. package/dist/event-bus.js.map +1 -0
  13. package/dist/executor.d.ts +42 -0
  14. package/dist/executor.d.ts.map +1 -0
  15. package/dist/executor.js +237 -0
  16. package/dist/executor.js.map +1 -0
  17. package/dist/history.d.ts +92 -0
  18. package/dist/history.d.ts.map +1 -0
  19. package/dist/history.js +171 -0
  20. package/dist/history.js.map +1 -0
  21. package/dist/idb-asset-store.d.ts +46 -0
  22. package/dist/idb-asset-store.d.ts.map +1 -0
  23. package/dist/idb-asset-store.js +76 -0
  24. package/dist/idb-asset-store.js.map +1 -0
  25. package/dist/index.d.ts +19 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +19 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/opfs-asset-store.d.ts +38 -0
  30. package/dist/opfs-asset-store.d.ts.map +1 -0
  31. package/dist/opfs-asset-store.js +122 -0
  32. package/dist/opfs-asset-store.js.map +1 -0
  33. package/dist/runtime.d.ts +87 -0
  34. package/dist/runtime.d.ts.map +1 -0
  35. package/dist/runtime.js +475 -0
  36. package/dist/runtime.js.map +1 -0
  37. package/dist/types.d.ts +99 -0
  38. package/dist/types.d.ts.map +1 -0
  39. package/dist/types.js +8 -0
  40. package/dist/types.js.map +1 -0
  41. package/dist/worker-host.d.ts +154 -0
  42. package/dist/worker-host.d.ts.map +1 -0
  43. package/dist/worker-host.js +394 -0
  44. package/dist/worker-host.js.map +1 -0
  45. package/dist/worker-protocol.d.ts +103 -0
  46. package/dist/worker-protocol.d.ts.map +1 -0
  47. package/dist/worker-protocol.js +87 -0
  48. package/dist/worker-protocol.js.map +1 -0
  49. package/package.json +49 -0
  50. package/src/asset-store.ts +222 -0
  51. package/src/capability-registry.ts +135 -0
  52. package/src/event-bus.ts +41 -0
  53. package/src/executor.ts +303 -0
  54. package/src/history.ts +229 -0
  55. package/src/idb-asset-store.ts +121 -0
  56. package/src/index.ts +20 -0
  57. package/src/opfs-asset-store.ts +167 -0
  58. package/src/runtime.ts +564 -0
  59. package/src/types.ts +121 -0
  60. package/src/worker-host.ts +531 -0
  61. package/src/worker-protocol.ts +187 -0
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Worker 通信协议
3
+ *
4
+ * 定义 主线程(Host) ↔ Worker 之间的消息格式与握手/心跳规范。
5
+ * 协议本身与传输层解耦(纯类型 + 常量 + 校验助手),
6
+ * 既可用于浏览器 Web Worker,也可在测试中被 Fake 传输层复用。
7
+ *
8
+ * 设计目标(对应 whitepaper T1「浏览器内存/性能 ★★★★★」与 PROJECT_PLAN 1.6/1.7):
9
+ * - 每个请求有唯一 id,Response 按 id 关联,避免错配。
10
+ * - 心跳(Heartbeat):Host 周期性 ping,Worker 必须在超时内 pong,否则视为崩溃。
11
+ * - Ready 握手:Worker 启动后主动发 ready,Host 据此判定可用。
12
+ * - 错误通道:Worker 主动 error(可恢复) vs 传输层 error/exit(不可恢复,触发重启)。
13
+ *
14
+ * 消息流向:
15
+ * Host → Worker: request | ping
16
+ * Worker → Host: response | pong | event | ready | error
17
+ */
18
+
19
+ /** 协议版本(Host 与 Worker 握手时校验,不一致则拒绝) */
20
+ export const WORKER_PROTOCOL_VERSION = '0.1.0';
21
+
22
+ // ─── 默认参数(可通过 WorkerHostOptions 覆盖)──────────────────────
23
+ /** 心跳发送间隔 */
24
+ export const DEFAULT_HEARTBEAT_INTERVAL_MS = 5_000;
25
+ /** 心跳超时:超过该时间未收到 pong 视为 Worker 无响应 */
26
+ export const DEFAULT_HEARTBEAT_TIMEOUT_MS = 15_000;
27
+ /** 单个请求的默认超时 */
28
+ export const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
29
+ /** 崩溃后最大重启次数(超过则放弃,交由上层降级) */
30
+ export const DEFAULT_MAX_RESTARTS = 3;
31
+ /** Worker 启动后等待 ready 的超时 */
32
+ export const DEFAULT_READY_TIMEOUT_MS = 10_000;
33
+
34
+ // ─── Host → Worker 消息 ─────────────────────────────────────────
35
+
36
+ /** 请求:Host 调用 Worker 暴露的方法 */
37
+ export interface WorkerRequest {
38
+ id: string;
39
+ type: 'request';
40
+ method: string;
41
+ params: unknown;
42
+ }
43
+
44
+ /** 心跳探测 */
45
+ export interface WorkerPing {
46
+ id: string;
47
+ type: 'ping';
48
+ ts: number;
49
+ }
50
+
51
+ export type WorkerMessageToWorker = WorkerRequest | WorkerPing;
52
+
53
+ // ─── Worker → Host 消息 ─────────────────────────────────────────
54
+
55
+ /** 响应:成功 */
56
+ export interface WorkerResponseOk {
57
+ id: string;
58
+ type: 'response';
59
+ ok: true;
60
+ result: unknown;
61
+ }
62
+
63
+ /** 响应:失败 */
64
+ export interface WorkerResponseErr {
65
+ id: string;
66
+ type: 'response';
67
+ ok: false;
68
+ error: {
69
+ message: string;
70
+ code?: string;
71
+ stack?: string;
72
+ };
73
+ }
74
+
75
+ export type WorkerResponse = WorkerResponseOk | WorkerResponseErr;
76
+
77
+ /** 心跳回应 */
78
+ export interface WorkerPong {
79
+ id: string;
80
+ type: 'pong';
81
+ ts: number;
82
+ }
83
+
84
+ /** Worker 主动事件(进度、日志等,无需 ack) */
85
+ export interface WorkerEvent {
86
+ type: 'event';
87
+ event: string;
88
+ payload?: unknown;
89
+ }
90
+
91
+ /** Worker 就绪(启动后主动发送,携带协议版本) */
92
+ export interface WorkerReady {
93
+ type: 'ready';
94
+ protocolVersion: string;
95
+ }
96
+
97
+ /** Worker 主动上报的致命错误(自身仍存活但无法继续) */
98
+ export interface WorkerFatalError {
99
+ type: 'error';
100
+ message: string;
101
+ stack?: string;
102
+ }
103
+
104
+ export type WorkerMessageToHost =
105
+ | WorkerResponse
106
+ | WorkerPong
107
+ | WorkerEvent
108
+ | WorkerReady
109
+ | WorkerFatalError;
110
+
111
+ // ─── 助手函数 ───────────────────────────────────────────────────
112
+
113
+ /** 生成唯一请求/心跳 id */
114
+ export function createRequestId(): string {
115
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
116
+ return crypto.randomUUID();
117
+ }
118
+ return `req_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
119
+ }
120
+
121
+ /** 类型守卫:是否为 Worker → Host 消息 */
122
+ export function isWorkerMessageToHost(
123
+ data: unknown
124
+ ): data is WorkerMessageToHost {
125
+ if (typeof data !== 'object' || data === null) return false;
126
+ const type = (data as { type?: unknown }).type;
127
+ return (
128
+ type === 'response' ||
129
+ type === 'pong' ||
130
+ type === 'event' ||
131
+ type === 'ready' ||
132
+ type === 'error'
133
+ );
134
+ }
135
+
136
+ /** 类型守卫:是否为响应消息 */
137
+ export function isWorkerResponse(data: unknown): data is WorkerResponse {
138
+ return (
139
+ typeof data === 'object' &&
140
+ data !== null &&
141
+ (data as { type?: unknown }).type === 'response' &&
142
+ typeof (data as { id?: unknown }).id === 'string' &&
143
+ typeof (data as { ok?: unknown }).ok === 'boolean'
144
+ );
145
+ }
146
+
147
+ /** 类型守卫:是否为 pong */
148
+ export function isWorkerPong(data: unknown): data is WorkerPong {
149
+ return (
150
+ typeof data === 'object' &&
151
+ data !== null &&
152
+ (data as { type?: unknown }).type === 'pong'
153
+ );
154
+ }
155
+
156
+ /** 类型守卫:是否为 ready */
157
+ export function isWorkerReady(data: unknown): data is WorkerReady {
158
+ return (
159
+ typeof data === 'object' &&
160
+ data !== null &&
161
+ (data as { type?: unknown }).type === 'ready'
162
+ );
163
+ }
164
+
165
+ /** 类型守卫:是否为 Worker 主动事件 */
166
+ export function isWorkerEvent(data: unknown): data is WorkerEvent {
167
+ return (
168
+ typeof data === 'object' &&
169
+ data !== null &&
170
+ (data as { type?: unknown }).type === 'event' &&
171
+ typeof (data as { event?: unknown }).event === 'string'
172
+ );
173
+ }
174
+
175
+ /** 类型守卫:是否为 Worker 主动上报错误 */
176
+ export function isWorkerFatalError(data: unknown): data is WorkerFatalError {
177
+ return (
178
+ typeof data === 'object' &&
179
+ data !== null &&
180
+ (data as { type?: unknown }).type === 'error'
181
+ );
182
+ }
183
+
184
+ /** 协议版本是否兼容(目前采用严格相等) */
185
+ export function isProtocolCompatible(version: string): boolean {
186
+ return version === WORKER_PROTOCOL_VERSION;
187
+ }