@hypen-space/core 0.4.3 → 0.4.5
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/README.md +1 -1
- package/dist/app.d.ts +79 -22
- package/dist/app.js +111 -18
- package/dist/app.js.map +4 -4
- package/dist/components/builtin.js +114 -21
- package/dist/components/builtin.js.map +5 -5
- package/dist/discovery.d.ts +1 -1
- package/dist/discovery.js +112 -19
- package/dist/discovery.js.map +5 -5
- package/dist/engine.browser.d.ts +6 -0
- package/dist/engine.browser.js +192 -5
- package/dist/engine.browser.js.map +5 -4
- package/dist/engine.d.ts +6 -0
- package/dist/engine.js +192 -5
- package/dist/engine.js.map +5 -4
- package/dist/index.browser.d.ts +1 -1
- package/dist/index.browser.js +111 -18
- package/dist/index.browser.js.map +5 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +117 -21
- package/dist/index.js.map +6 -6
- package/dist/managed-router.d.ts +3 -4
- package/dist/remote/client.js +34 -2
- package/dist/remote/client.js.map +3 -3
- package/dist/remote/index.js +472 -26
- package/dist/remote/index.js.map +8 -7
- package/dist/remote/server.d.ts +46 -15
- package/dist/remote/server.js +472 -26
- package/dist/remote/server.js.map +8 -7
- package/dist/result.d.ts +18 -0
- package/package.json +1 -1
- package/src/app.ts +162 -41
- package/src/components/builtin.ts +3 -4
- package/src/discovery.ts +2 -2
- package/src/engine.browser.ts +27 -4
- package/src/engine.ts +27 -4
- package/src/index.browser.ts +0 -2
- package/src/index.ts +3 -2
- package/src/managed-router.ts +5 -6
- package/src/remote/server.ts +116 -21
- package/src/result.ts +48 -0
- package/wasm-browser/hypen_engine_bg.wasm +0 -0
- package/wasm-browser/package.json +1 -1
- package/wasm-node/hypen_engine_bg.wasm +0 -0
- package/wasm-node/package.json +1 -1
- package/dist/module-registry.d.ts +0 -42
- package/src/module-registry.ts +0 -76
package/src/remote/server.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* RemoteServer - Stream Hypen apps over WebSocket with Session Management
|
|
3
3
|
*
|
|
4
|
-
* Usage:
|
|
4
|
+
* Usage with inline UI:
|
|
5
5
|
* ```typescript
|
|
6
6
|
* import { RemoteServer, app } from "@hypen-space/core";
|
|
7
7
|
*
|
|
8
8
|
* const counter = app
|
|
9
9
|
* .defineState({ count: 0 })
|
|
10
10
|
* .onAction("increment", ({ state }) => state.count++)
|
|
11
|
-
* .onDisconnect(async ({ state, session }) => {
|
|
12
|
-
* await redis.set(`session:${session.id}`, JSON.stringify(state));
|
|
13
|
-
* })
|
|
14
|
-
* .onReconnect(async ({ session, restore }) => {
|
|
15
|
-
* const saved = await redis.get(`session:${session.id}`);
|
|
16
|
-
* if (saved) restore(JSON.parse(saved));
|
|
17
|
-
* })
|
|
18
|
-
* .onExpire(async ({ session }) => {
|
|
19
|
-
* await redis.del(`session:${session.id}`);
|
|
20
|
-
* })
|
|
21
11
|
* .build();
|
|
22
12
|
*
|
|
23
13
|
* new RemoteServer()
|
|
24
14
|
* .module("Counter", counter)
|
|
25
15
|
* .ui(`Column { Text("Count: \${state.count}") }`)
|
|
26
|
-
* .session({ ttl: 3600, concurrent: "kick-old" })
|
|
27
16
|
* .listen(3000);
|
|
28
17
|
* ```
|
|
18
|
+
*
|
|
19
|
+
* Usage with directory-based discovery (auto-resolves imports):
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { RemoteServer } from "@hypen-space/core";
|
|
22
|
+
* import chatModule from "./apps/chat/Chat/component.js";
|
|
23
|
+
*
|
|
24
|
+
* await new RemoteServer()
|
|
25
|
+
* .source("./apps/chat")
|
|
26
|
+
* .module("Chat", chatModule)
|
|
27
|
+
* .listen(3001);
|
|
28
|
+
* ```
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
31
|
import { Engine } from "../engine.js";
|
|
32
32
|
import { HypenModuleInstance } from "../app.js";
|
|
33
|
-
import type { HypenModule } from "../app.js";
|
|
33
|
+
import type { HypenModule, HypenModuleDefinition } from "../app.js";
|
|
34
34
|
import type {
|
|
35
35
|
RemoteMessage,
|
|
36
36
|
RemoteClient,
|
|
@@ -44,10 +44,15 @@ import type {
|
|
|
44
44
|
Session,
|
|
45
45
|
SessionConfig,
|
|
46
46
|
} from "./types.js";
|
|
47
|
-
import type { Patch } from "../types.js";
|
|
47
|
+
import type { Patch, ResolvedComponent } from "../types.js";
|
|
48
48
|
import type { ServerWebSocket } from "bun";
|
|
49
49
|
import { SessionManager } from "./session.js";
|
|
50
50
|
import { frameworkLoggers } from "../logger.js";
|
|
51
|
+
import {
|
|
52
|
+
discoverComponents,
|
|
53
|
+
loadDiscoveredComponents,
|
|
54
|
+
type DiscoveredComponent,
|
|
55
|
+
} from "../discovery.js";
|
|
51
56
|
|
|
52
57
|
const log = frameworkLoggers.remote;
|
|
53
58
|
|
|
@@ -72,6 +77,8 @@ export class RemoteServer {
|
|
|
72
77
|
private _module: HypenModule<any> | null = null;
|
|
73
78
|
private _moduleName: string = "App";
|
|
74
79
|
private _ui: string = "";
|
|
80
|
+
private _sourceDir: string | null = null;
|
|
81
|
+
private _discoveredComponents: Map<string, { template: string; module?: HypenModuleDefinition<any> }> = new Map();
|
|
75
82
|
private _config: RemoteServerConfig = {};
|
|
76
83
|
private _sessionConfig: SessionConfig = {};
|
|
77
84
|
private _onConnectionCallbacks: Array<(client: RemoteClient) => void> = [];
|
|
@@ -98,6 +105,26 @@ export class RemoteServer {
|
|
|
98
105
|
return this;
|
|
99
106
|
}
|
|
100
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Set a source directory for automatic component discovery.
|
|
110
|
+
*
|
|
111
|
+
* All .hypen and .ts components in the directory (and subdirectories) are
|
|
112
|
+
* discovered automatically. The entry component's template is used as the
|
|
113
|
+
* UI, so calling .ui() is optional when .source() is set.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* new RemoteServer()
|
|
118
|
+
* .source("./apps/chat")
|
|
119
|
+
* .module("Chat", chatModule)
|
|
120
|
+
* .listen(3001);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
source(dir: string): this {
|
|
124
|
+
this._sourceDir = dir;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
101
128
|
/**
|
|
102
129
|
* Set server configuration
|
|
103
130
|
*/
|
|
@@ -133,13 +160,20 @@ export class RemoteServer {
|
|
|
133
160
|
/**
|
|
134
161
|
* Start the WebSocket server
|
|
135
162
|
*/
|
|
136
|
-
listen(port?: number): this {
|
|
163
|
+
async listen(port?: number): Promise<this> {
|
|
137
164
|
if (!this._module) {
|
|
138
165
|
throw new Error("Module not set. Call .module() before .listen()");
|
|
139
166
|
}
|
|
140
167
|
|
|
168
|
+
// Run component discovery if source directory is set
|
|
169
|
+
if (this._sourceDir) {
|
|
170
|
+
await this.discoverFromSource();
|
|
171
|
+
}
|
|
172
|
+
|
|
141
173
|
if (!this._ui) {
|
|
142
|
-
throw new Error(
|
|
174
|
+
throw new Error(
|
|
175
|
+
"UI not set. Call .ui() or .source() before .listen()"
|
|
176
|
+
);
|
|
143
177
|
}
|
|
144
178
|
|
|
145
179
|
// Initialize session manager
|
|
@@ -214,6 +248,56 @@ export class RemoteServer {
|
|
|
214
248
|
return `ws://${hostname}:${port}`;
|
|
215
249
|
}
|
|
216
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Discover components from the source directory.
|
|
253
|
+
* Populates _discoveredComponents and sets _ui from the entry component
|
|
254
|
+
* if not already set via .ui().
|
|
255
|
+
*/
|
|
256
|
+
private async discoverFromSource(): Promise<void> {
|
|
257
|
+
const dir = this._sourceDir!;
|
|
258
|
+
log.info(`Discovering components from ${dir}...`);
|
|
259
|
+
|
|
260
|
+
const discovered = await discoverComponents(dir);
|
|
261
|
+
const loaded = await loadDiscoveredComponents(discovered);
|
|
262
|
+
|
|
263
|
+
for (const [name, { module, template }] of loaded) {
|
|
264
|
+
this._discoveredComponents.set(name, { template, module });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
log.info(
|
|
268
|
+
`Discovered ${this._discoveredComponents.size} components: ${Array.from(this._discoveredComponents.keys()).join(", ")}`
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
// If .ui() was not called, use the entry component's template
|
|
272
|
+
if (!this._ui) {
|
|
273
|
+
const entry = this._discoveredComponents.get(this._moduleName);
|
|
274
|
+
if (entry) {
|
|
275
|
+
this._ui = entry.template;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Set up a component resolver on a client engine so that imports
|
|
282
|
+
* and component references in templates are resolved from discovered
|
|
283
|
+
* components.
|
|
284
|
+
*/
|
|
285
|
+
private setupComponentResolver(engine: Engine): void {
|
|
286
|
+
if (this._discoveredComponents.size === 0) return;
|
|
287
|
+
|
|
288
|
+
engine.setComponentResolver(
|
|
289
|
+
(componentName: string, _contextPath: string | null) => {
|
|
290
|
+
const comp = this._discoveredComponents.get(componentName);
|
|
291
|
+
if (!comp) return null;
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
source: comp.template,
|
|
295
|
+
path: componentName,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
217
301
|
/**
|
|
218
302
|
* Handle new WebSocket connection
|
|
219
303
|
* Waits for hello message before fully initializing
|
|
@@ -227,6 +311,9 @@ export class RemoteServer {
|
|
|
227
311
|
const engine = new Engine();
|
|
228
312
|
await engine.init();
|
|
229
313
|
|
|
314
|
+
// Wire up component resolver if we have discovered components
|
|
315
|
+
this.setupComponentResolver(engine);
|
|
316
|
+
|
|
230
317
|
// Create module instance
|
|
231
318
|
const moduleInstance = new HypenModuleInstance(engine, this._module!);
|
|
232
319
|
|
|
@@ -618,19 +705,27 @@ export class RemoteServer {
|
|
|
618
705
|
/**
|
|
619
706
|
* Convenience function to create and start a RemoteServer
|
|
620
707
|
*/
|
|
621
|
-
export function serve(options: {
|
|
708
|
+
export async function serve(options: {
|
|
622
709
|
module: HypenModule<any>;
|
|
623
710
|
moduleName?: string;
|
|
624
|
-
ui
|
|
711
|
+
ui?: string;
|
|
712
|
+
source?: string;
|
|
625
713
|
port?: number;
|
|
626
714
|
hostname?: string;
|
|
627
715
|
session?: SessionConfig;
|
|
628
716
|
onConnection?: (client: RemoteClient) => void;
|
|
629
717
|
onDisconnection?: (client: RemoteClient) => void;
|
|
630
|
-
}): RemoteServer {
|
|
718
|
+
}): Promise<RemoteServer> {
|
|
631
719
|
const server = new RemoteServer()
|
|
632
|
-
.module(options.moduleName ?? "App", options.module)
|
|
633
|
-
|
|
720
|
+
.module(options.moduleName ?? "App", options.module);
|
|
721
|
+
|
|
722
|
+
if (options.source) {
|
|
723
|
+
server.source(options.source);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (options.ui) {
|
|
727
|
+
server.ui(options.ui);
|
|
728
|
+
}
|
|
634
729
|
|
|
635
730
|
if (options.port || options.hostname) {
|
|
636
731
|
server.config({
|
package/src/result.ts
CHANGED
|
@@ -258,3 +258,51 @@ export class StateError extends HypenError {
|
|
|
258
258
|
this.path = path;
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Error thrown when parsing Hypen DSL source fails
|
|
264
|
+
*/
|
|
265
|
+
export class ParseError extends HypenError {
|
|
266
|
+
readonly source?: string;
|
|
267
|
+
|
|
268
|
+
constructor(message: string, source?: string, cause?: unknown) {
|
|
269
|
+
super('PARSE_ERROR', message, {
|
|
270
|
+
context: { source },
|
|
271
|
+
cause: cause instanceof Error ? cause : undefined,
|
|
272
|
+
});
|
|
273
|
+
this.name = 'ParseError';
|
|
274
|
+
this.source = source;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Error thrown when rendering fails
|
|
280
|
+
*/
|
|
281
|
+
export class RenderError extends HypenError {
|
|
282
|
+
constructor(message: string, cause?: unknown) {
|
|
283
|
+
super('RENDER_ERROR', message, {
|
|
284
|
+
cause: cause instanceof Error ? cause : undefined,
|
|
285
|
+
});
|
|
286
|
+
this.name = 'RenderError';
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Classify a WASM engine error string into the appropriate HypenError subclass.
|
|
292
|
+
* The engine returns JsValue error strings with known prefixes.
|
|
293
|
+
*/
|
|
294
|
+
export function classifyEngineError(err: unknown): HypenError {
|
|
295
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
296
|
+
|
|
297
|
+
if (message.startsWith('Parse error:')) {
|
|
298
|
+
return new ParseError(message);
|
|
299
|
+
}
|
|
300
|
+
if (message.startsWith('Invalid state:') || message.startsWith('Invalid state patch:')) {
|
|
301
|
+
return new StateError(message);
|
|
302
|
+
}
|
|
303
|
+
if (message.startsWith('Parent node not found:')) {
|
|
304
|
+
return new RenderError(message);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return new RenderError(message);
|
|
308
|
+
}
|
|
Binary file
|
|
Binary file
|
package/wasm-node/package.json
CHANGED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module Registry — maps component names to module definitions.
|
|
3
|
-
*
|
|
4
|
-
* Used by the import resolution pipeline and ManagedRouter to look up
|
|
5
|
-
* module definitions by their component name (e.g., "HomePage", "ProfilePage").
|
|
6
|
-
*/
|
|
7
|
-
import type { HypenModuleDefinition } from "./app.js";
|
|
8
|
-
export declare class ModuleRegistry {
|
|
9
|
-
private definitions;
|
|
10
|
-
/**
|
|
11
|
-
* Register a module definition under a component name.
|
|
12
|
-
*/
|
|
13
|
-
register(name: string, definition: HypenModuleDefinition): void;
|
|
14
|
-
/**
|
|
15
|
-
* Get a module definition by component name.
|
|
16
|
-
*/
|
|
17
|
-
get(name: string): HypenModuleDefinition | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* Check if a module definition exists.
|
|
20
|
-
*/
|
|
21
|
-
has(name: string): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Get all registered definitions.
|
|
24
|
-
*/
|
|
25
|
-
getAll(): Map<string, HypenModuleDefinition>;
|
|
26
|
-
/**
|
|
27
|
-
* Get all registered component names.
|
|
28
|
-
*/
|
|
29
|
-
getNames(): string[];
|
|
30
|
-
/**
|
|
31
|
-
* Unregister a module definition.
|
|
32
|
-
*/
|
|
33
|
-
unregister(name: string): void;
|
|
34
|
-
/**
|
|
35
|
-
* Number of registered definitions.
|
|
36
|
-
*/
|
|
37
|
-
get size(): number;
|
|
38
|
-
/**
|
|
39
|
-
* Clear all registered definitions.
|
|
40
|
-
*/
|
|
41
|
-
clear(): void;
|
|
42
|
-
}
|
package/src/module-registry.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module Registry — maps component names to module definitions.
|
|
3
|
-
*
|
|
4
|
-
* Used by the import resolution pipeline and ManagedRouter to look up
|
|
5
|
-
* module definitions by their component name (e.g., "HomePage", "ProfilePage").
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { HypenModuleDefinition } from "./app.js";
|
|
9
|
-
import { createLogger } from "./logger.js";
|
|
10
|
-
|
|
11
|
-
const log = createLogger("ModuleRegistry");
|
|
12
|
-
|
|
13
|
-
export class ModuleRegistry {
|
|
14
|
-
private definitions = new Map<string, HypenModuleDefinition>();
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Register a module definition under a component name.
|
|
18
|
-
*/
|
|
19
|
-
register(name: string, definition: HypenModuleDefinition): void {
|
|
20
|
-
if (this.definitions.has(name)) {
|
|
21
|
-
log.warn(`Module "${name}" is already registered. Overwriting.`);
|
|
22
|
-
}
|
|
23
|
-
this.definitions.set(name, definition);
|
|
24
|
-
log.debug(`Registered module definition: ${name}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Get a module definition by component name.
|
|
29
|
-
*/
|
|
30
|
-
get(name: string): HypenModuleDefinition | undefined {
|
|
31
|
-
return this.definitions.get(name);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Check if a module definition exists.
|
|
36
|
-
*/
|
|
37
|
-
has(name: string): boolean {
|
|
38
|
-
return this.definitions.has(name);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Get all registered definitions.
|
|
43
|
-
*/
|
|
44
|
-
getAll(): Map<string, HypenModuleDefinition> {
|
|
45
|
-
return new Map(this.definitions);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Get all registered component names.
|
|
50
|
-
*/
|
|
51
|
-
getNames(): string[] {
|
|
52
|
-
return Array.from(this.definitions.keys());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Unregister a module definition.
|
|
57
|
-
*/
|
|
58
|
-
unregister(name: string): void {
|
|
59
|
-
this.definitions.delete(name);
|
|
60
|
-
log.debug(`Unregistered module definition: ${name}`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Number of registered definitions.
|
|
65
|
-
*/
|
|
66
|
-
get size(): number {
|
|
67
|
-
return this.definitions.size;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Clear all registered definitions.
|
|
72
|
-
*/
|
|
73
|
-
clear(): void {
|
|
74
|
-
this.definitions.clear();
|
|
75
|
-
}
|
|
76
|
-
}
|