@galfus/transport-node 0.0.0-stage → 0.31.1-alpha

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 CHANGED
@@ -1,3 +1,40 @@
1
- # Temporary Holding Version
1
+ # @galfus/transport-node
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Native transport backend for **Node.js** and Node-API environments using the compiled native addon `galfus.node`.
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ `@galfus/transport-node` links standard Node.js runtimes with the Galfus native core through pre-compiled Node-API (N-API) bindings. It exposes identical interfaces to `@galfus/transport-bun`, ensuring cross-runtime compatibility.
10
+
11
+ ---
12
+
13
+ ## Features
14
+
15
+ - **Node-API Native Addon**:
16
+ - Loads `galfus.node` dynamically from the local distribution or compiled targets.
17
+ - ABI-stable cross-platform execution across Node.js versions without runtime recompilation.
18
+ - **Factory API**:
19
+ - `createNodeTransport(options?: TransportInitOptions): Promise<NodeTransport>`
20
+ - Asynchronously verifies native platform initialization and returns a ready transport instance.
21
+ - **Precision Loop Management**:
22
+ - Microsecond-accurate frame loop driver with unclamped delta tracking and cancellation tokens.
23
+ - **Headless Mode**:
24
+ - Full headless support for continuous integration, visual regression pipelines, and backend simulation servers.
25
+
26
+ ---
27
+
28
+ ## Usage
29
+
30
+ ```typescript
31
+ import { createNodeTransport } from '@galfus/transport-node';
32
+
33
+ // Create ready transport instance
34
+ const transport = await createNodeTransport({
35
+ targetFps: 60,
36
+ headless: true
37
+ });
38
+
39
+ console.log('Node transport initialized:', transport.isReady);
40
+ ```
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@galfus/transport-node",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.31.1-alpha",
4
+ "description": "Node.js Node-API native addon transport for Galfus Engine",
5
+ "author": "Renato Rodrigues (morbden) <renato@vulppi.dev>",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/galfus-lang/galfus-engine.git",
10
+ "directory": "host/transport-node"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/galfus-lang/galfus-engine/issues"
14
+ },
15
+ "homepage": "https://github.com/galfus-lang/galfus-engine#readme",
16
+ "keywords": [
17
+ "galfus",
18
+ "game-engine",
19
+ "transport",
20
+ "nodejs",
21
+ "node-api",
22
+ "napi",
23
+ "addon"
24
+ ],
25
+ "type": "module",
26
+ "main": "./src/index.ts",
27
+ "types": "./src/index.ts",
28
+ "files": [
29
+ "src",
30
+ "dist",
31
+ "README.md"
32
+ ],
33
+ "exports": {
34
+ ".": "./src/index.ts"
35
+ },
36
+ "dependencies": {
37
+ "@galfus/transport-types": "^0.31.1-alpha"
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,141 @@
1
+ import {
2
+ calculateFrameTiming,
3
+ DEFAULT_TARGET_FPS,
4
+ type FrameCallback,
5
+ type GalfusEngine,
6
+ GalfusLoopError,
7
+ type GalfusTransport,
8
+ GalfusTransportError,
9
+ type LoopOptions,
10
+ type TransportInitOptions
11
+ } from '@galfus/transport-types';
12
+ import { GalfusNode } from './sdk/galfus';
13
+
14
+ export * from './sdk/galfus';
15
+
16
+ export class NodeTransport implements GalfusTransport {
17
+ private _engine: GalfusNode | null = null;
18
+ private _isRunning = false;
19
+ private _isInitialized = false;
20
+ private _isDisposed = false;
21
+ private _frameId = 0;
22
+ private _lastTime = 0;
23
+ private _loopGeneration = 0;
24
+ private _callback: FrameCallback | null = null;
25
+ private _targetFps: number = DEFAULT_TARGET_FPS;
26
+
27
+ get engine(): GalfusEngine {
28
+ if (this._isDisposed) {
29
+ throw new GalfusTransportError('Cannot access engine on a disposed NodeTransport.');
30
+ }
31
+ if (!this._engine) {
32
+ this._engine = new GalfusNode();
33
+ }
34
+ return this._engine;
35
+ }
36
+
37
+ get isInitialized(): boolean {
38
+ return this._isInitialized;
39
+ }
40
+
41
+ get isDisposed(): boolean {
42
+ return this._isDisposed;
43
+ }
44
+
45
+ get isRunning(): boolean {
46
+ return this._isRunning;
47
+ }
48
+
49
+ init(options: TransportInitOptions = {}): void {
50
+ if (this._isDisposed) {
51
+ throw new GalfusTransportError('Cannot initialize a disposed NodeTransport.');
52
+ }
53
+ if (this._isInitialized) return;
54
+ if (options.targetFps !== undefined && options.targetFps > 0) {
55
+ this._targetFps = options.targetFps;
56
+ }
57
+ try {
58
+ if (options.headless) {
59
+ this.engine.platformInitHeadless();
60
+ } else {
61
+ this.engine.platformInit();
62
+ }
63
+ } catch (err: any) {
64
+ if (err?.message?.includes('already initialized')) {
65
+ // Platform is already initialized globally in the process
66
+ } else {
67
+ throw err;
68
+ }
69
+ }
70
+ this._isInitialized = true;
71
+ }
72
+
73
+ dispose(): void {
74
+ if (this._isDisposed) return;
75
+ this.stopLoop();
76
+ this._callback = null;
77
+ if (this._isInitialized && this._engine) {
78
+ try {
79
+ this._engine.platformShutdown();
80
+ } catch {
81
+ // Ignore if already shutdown
82
+ }
83
+ }
84
+ this._engine = null;
85
+ this._isInitialized = false;
86
+ this._isDisposed = true;
87
+ }
88
+
89
+ startLoop(callback: FrameCallback, options?: LoopOptions): void {
90
+ if (this._isDisposed) {
91
+ throw new GalfusLoopError('Cannot start loop on a disposed NodeTransport.');
92
+ }
93
+ if (this._isRunning) {
94
+ throw new GalfusLoopError('NodeTransport loop is already running.');
95
+ }
96
+ const currentGen = ++this._loopGeneration;
97
+ this._callback = callback;
98
+ this._isRunning = true;
99
+ this._lastTime = performance.now();
100
+
101
+ const targetFps = options?.targetFps ?? this._targetFps;
102
+ const intervalMs = 1000 / targetFps;
103
+
104
+ const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
105
+ const runAsyncLoop = async () => {
106
+ while (this._isRunning && this._loopGeneration === currentGen && !this._isDisposed) {
107
+ const now = performance.now();
108
+ const timing = calculateFrameTiming(now, this._lastTime, ++this._frameId);
109
+ this._lastTime = now;
110
+
111
+ try {
112
+ this._callback?.(timing);
113
+ } catch (err) {
114
+ console.error('[NodeTransport] Unhandled error during frame execution:', err);
115
+ }
116
+
117
+ if (!this._isRunning || this._loopGeneration !== currentGen || this._isDisposed) break;
118
+
119
+ const elapsed = performance.now() - now;
120
+ const delay = Math.max(1, Math.round(intervalMs - elapsed));
121
+ await sleep(delay);
122
+ }
123
+ };
124
+
125
+ void runAsyncLoop();
126
+ }
127
+
128
+ stopLoop(): void {
129
+ this._isRunning = false;
130
+ this._loopGeneration++;
131
+ }
132
+ }
133
+
134
+ /** Creates a new GalfusTransport driver instance running via Node-API. */
135
+ export function createNodeTransport(options?: TransportInitOptions): GalfusTransport {
136
+ const transport = new NodeTransport();
137
+ transport.init(options);
138
+ return transport;
139
+ }
140
+
141
+ export default createNodeTransport;