@galfus/transport-bun 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,43 @@
1
- # Temporary Holding Version
1
+ # @galfus/transport-bun
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
+ High-performance native transport backend for **Bun** using direct FFI bindings (`bun:ffi`) to link against `libgalfus_native.so`, `galfus.dll`, or `libgalfus_native.dylib`.
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ `@galfus/transport-bun` provides zero-overhead interoperability between Bun and the Galfus native core. Hot-path spatial operations (transforms, matrices, look-at queries) use direct memory pointer buffers, bypassing JSON marshalling completely.
10
+
11
+ ---
12
+
13
+ ## Features
14
+
15
+ - **Direct Bun FFI Integration**:
16
+ - Dynamically loads the pre-compiled `galfus-native` shared library.
17
+ - Zero-copy C struct and pointer marshalling for high-frequency transforms.
18
+ - **Factory API**:
19
+ - `createBunTransport(options?: TransportInitOptions): Promise<BunTransport>`
20
+ - Asynchronously initializes the native platform and display capabilities, returning a transport that is immediately `ready`.
21
+ - **Target FPS & Loop Timing**:
22
+ - Precision sleep-based microsecond loop timer supporting target frame rates (e.g. 60, 120 FPS).
23
+ - Unclamped real `deltaMs` metrics.
24
+ - **Headless & Server Support**:
25
+ - Supports headless execution for server-side rendering, CI test suites, and offscreen compute passes.
26
+
27
+ ---
28
+
29
+ ## Usage
30
+
31
+ ```typescript
32
+ import { createBunTransport } from '@galfus/transport-bun';
33
+
34
+ // Create and initialize the transport backend
35
+ const transport = await createBunTransport({
36
+ targetFps: 120,
37
+ headless: false
38
+ });
39
+
40
+ // Access capabilities and engine bindings
41
+ console.log('Capabilities:', transport.capabilities);
42
+ transport.engine.multiverseCreateTarget(1, { kind: { type: 'window', windowId: 1 } });
43
+ ```
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@galfus/transport-bun",
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": "Bun native FFI 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-bun"
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
+ "bun",
21
+ "ffi",
22
+ "native",
23
+ "wgpu"
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,140 @@
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 { GalfusBun } from './sdk/galfus';
13
+
14
+ export * from './sdk/galfus';
15
+
16
+ export class BunTransport implements GalfusTransport {
17
+ private _engine: GalfusBun | 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 BunTransport.');
30
+ }
31
+ if (!this._engine) {
32
+ this._engine = new GalfusBun();
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 BunTransport.');
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 BunTransport.');
92
+ }
93
+ if (this._isRunning) {
94
+ throw new GalfusLoopError('BunTransport 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 runAsyncLoop = async () => {
105
+ while (this._isRunning && this._loopGeneration === currentGen && !this._isDisposed) {
106
+ const now = performance.now();
107
+ const timing = calculateFrameTiming(now, this._lastTime, ++this._frameId);
108
+ this._lastTime = now;
109
+
110
+ try {
111
+ this._callback?.(timing);
112
+ } catch (err) {
113
+ console.error('[BunTransport] Unhandled error during frame execution:', err);
114
+ }
115
+
116
+ if (!this._isRunning || this._loopGeneration !== currentGen || this._isDisposed) break;
117
+
118
+ const elapsed = performance.now() - now;
119
+ const delay = Math.max(1, Math.round(intervalMs - elapsed));
120
+ await Bun.sleep(delay);
121
+ }
122
+ };
123
+
124
+ void runAsyncLoop();
125
+ }
126
+
127
+ stopLoop(): void {
128
+ this._isRunning = false;
129
+ this._loopGeneration++;
130
+ }
131
+ }
132
+
133
+ /** Creates a new GalfusTransport driver instance running via Bun FFI. */
134
+ export function createBunTransport(options?: TransportInitOptions): GalfusTransport {
135
+ const transport = new BunTransport();
136
+ transport.init(options);
137
+ return transport;
138
+ }
139
+
140
+ export default createBunTransport;