frontend-hamroun 1.2.80 → 1.2.82

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 (104) hide show
  1. package/bin/cli.js +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +1 -0
  4. package/dist/index.js.map +1 -1
  5. package/package.json +1 -1
  6. package/templates/basic-app/src/App.jsx +16 -0
  7. package/templates/basic-app/src/client.jsx +5 -0
  8. package/templates/basic-app/src/components/Counter.jsx +13 -0
  9. package/templates/basic-app/src/jsx-shim.js +3 -0
  10. package/templates/basic-app/src/jsx-shim.ts +7 -0
  11. package/templates/basic-app/src/main.jsx +98 -0
  12. package/templates/basic-app/src/server.js +47 -0
  13. package/templates/complete-app/api/hello.js +0 -0
  14. package/templates/complete-app/lib/frontend-hamroun.js +182 -0
  15. package/templates/complete-app/package.json +18 -0
  16. package/templates/complete-app/pages/about.js +119 -0
  17. package/templates/complete-app/pages/about.jsx +0 -0
  18. package/templates/complete-app/pages/index.js +157 -0
  19. package/templates/complete-app/pages/index.jsx +0 -0
  20. package/templates/complete-app/pages/wasm-demo.js +290 -0
  21. package/templates/complete-app/pages/wasm-demo.jsx +0 -0
  22. package/templates/complete-app/public/client.js +89 -0
  23. package/templates/complete-app/public/index.html +118 -0
  24. package/templates/complete-app/public/styles.css +76 -0
  25. package/templates/complete-app/server.js +226 -0
  26. package/templates/complete-app/src/App.tsx +59 -0
  27. package/templates/complete-app/src/client.tsx +18 -0
  28. package/templates/complete-app/src/server.ts +218 -0
  29. package/templates/complete-app/tsconfig.json +22 -0
  30. package/templates/complete-app/tsconfig.server.json +19 -0
  31. package/templates/{ssr-template → complete-app}/vite.config.js +16 -5
  32. package/templates/complete-app/vite.config.ts +30 -0
  33. package/templates/complete-app/wasm/build.bat +0 -0
  34. package/templates/complete-app/wasm/build.sh +0 -0
  35. package/templates/complete-app/wasm/example.go +0 -0
  36. package/templates/fullstack-app/build/main.css +874 -874
  37. package/templates/fullstack-app/build/main.css.map +7 -7
  38. package/templates/fullstack-app/build/main.js +996 -967
  39. package/templates/fullstack-app/build/main.js.map +7 -7
  40. package/templates/fullstack-app/package-lock.json +6301 -0
  41. package/templates/fullstack-app/public/styles.css +768 -768
  42. package/templates/go/example.go +154 -99
  43. package/templates/ssr-template/esbuild.config.js +33 -0
  44. package/templates/ssr-template/jsx-shim.js +1 -0
  45. package/templates/ssr-template/package.json +22 -16
  46. package/templates/ssr-template/src/App.tsx +12 -52
  47. package/templates/ssr-template/src/client.tsx +3 -17
  48. package/templates/ssr-template/src/server.ts +21 -204
  49. package/templates/ssr-template/tsconfig.json +10 -13
  50. package/templates/ssr-template/tsconfig.server.json +6 -14
  51. package/templates/wasm/build-wasm.js +228 -0
  52. package/templates/wasm/esbuild.config.js +63 -0
  53. package/templates/wasm/go/main.go +256 -0
  54. package/templates/wasm/go/wasm_exec.js +0 -0
  55. package/templates/wasm/index.html +97 -0
  56. package/templates/wasm/jsx-shim.js +9 -0
  57. package/templates/wasm/package-lock.json +5307 -0
  58. package/templates/wasm/package.json +42 -0
  59. package/templates/wasm/public/example.wasm +0 -0
  60. package/templates/{go-wasm-app/public/wasm → wasm/public}/wasm_exec.js +561 -561
  61. package/templates/wasm/src/App.tsx +564 -0
  62. package/templates/wasm/src/client.tsx +220 -0
  63. package/templates/wasm/src/index.tsx +21 -0
  64. package/templates/wasm/src/server.ts +145 -0
  65. package/templates/wasm/tsconfig.json +21 -0
  66. package/templates/wasm/tsconfig.node.json +13 -0
  67. package/templates/wasm/tsconfig.server.json +23 -0
  68. package/templates/wasm/vite.config.ts +56 -0
  69. package/templates/wasm/wasm-loader.js +103 -0
  70. package/templates/basic-app/bun.lock +0 -196
  71. package/templates/basic-app/docs/rapport_pfe.aux +0 -27
  72. package/templates/basic-app/docs/rapport_pfe.out +0 -10
  73. package/templates/basic-app/docs/rapport_pfe.pdf +0 -0
  74. package/templates/basic-app/docs/rapport_pfe.tex +0 -68
  75. package/templates/basic-app/docs/rapport_pfe.toc +0 -14
  76. package/templates/basic-app/package-lock.json +0 -4185
  77. package/templates/go-wasm-app/README.md +0 -38
  78. package/templates/go-wasm-app/babel.config.js +0 -15
  79. package/templates/go-wasm-app/build-client.js +0 -49
  80. package/templates/go-wasm-app/build-wasm.js +0 -237
  81. package/templates/go-wasm-app/package.json +0 -23
  82. package/templates/go-wasm-app/public/index.html +0 -128
  83. package/templates/go-wasm-app/public/styles.css +0 -197
  84. package/templates/go-wasm-app/public/wasm/example.wasm +0 -0
  85. package/templates/go-wasm-app/public/wasm/wasm_exec_node.js +0 -39
  86. package/templates/go-wasm-app/server.js +0 -521
  87. package/templates/go-wasm-app/src/App.jsx +0 -38
  88. package/templates/go-wasm-app/src/app.js +0 -153
  89. package/templates/go-wasm-app/src/client.js +0 -57
  90. package/templates/go-wasm-app/src/components/Footer.jsx +0 -13
  91. package/templates/go-wasm-app/src/components/Header.jsx +0 -19
  92. package/templates/go-wasm-app/src/components/WasmDemo.jsx +0 -120
  93. package/templates/go-wasm-app/src/main.jsx +0 -12
  94. package/templates/go-wasm-app/src/wasm/example.go +0 -75
  95. package/templates/go-wasm-app/tsconfig.server.json +0 -18
  96. package/templates/go-wasm-app/vite.config.js +0 -34
  97. package/templates/ssr-template/package-lock.json +0 -2478
  98. package/templates/ssr-template/public/index.html +0 -47
  99. package/templates/ssr-template/server.js +0 -369
  100. /package/templates/{ssr-template → complete-app}/client.js +0 -0
  101. /package/templates/{ssr-template → complete-app}/readme.md +0 -0
  102. /package/templates/{ssr-template → complete-app}/server.ts +0 -0
  103. /package/templates/{ssr-template → complete-app}/src/client.ts +0 -0
  104. /package/templates/{ssr-template → complete-app}/src/pages/index.tsx +0 -0
@@ -0,0 +1,220 @@
1
+ import { jsx, render } from 'frontend-hamroun';
2
+ import { loadGoWasm } from '../wasm-loader.js';
3
+
4
+ // WASM Demo Component
5
+ function WasmDemo() {
6
+ const [wasmReady, setWasmReady] = useState(false);
7
+ const [result, setResult] = useState('');
8
+ const [input1, setInput1] = useState('5');
9
+ const [input2, setInput2] = useState('3');
10
+ const [fibInput, setFibInput] = useState('10');
11
+
12
+ useEffect(() => {
13
+ loadWasm();
14
+ }, []);
15
+
16
+ const loadWasm = async () => {
17
+ try {
18
+ await loadGoWasm('/example.wasm');
19
+ setWasmReady(true);
20
+ setResult('✅ Go WASM module loaded successfully!');
21
+ } catch (error) {
22
+ setResult(`❌ Failed to load WASM: ${error.message}`);
23
+ }
24
+ };
25
+
26
+ const testAdd = () => {
27
+ if (!wasmReady) return;
28
+ try {
29
+ const num1 = parseFloat(input1);
30
+ const num2 = parseFloat(input2);
31
+ const result = (window as any).add(num1, num2);
32
+ setResult(`Add result: ${num1} + ${num2} = ${result}`);
33
+ } catch (error) {
34
+ setResult(`Error: ${error.message}`);
35
+ }
36
+ };
37
+
38
+ const testFibonacci = () => {
39
+ if (!wasmReady) return;
40
+ try {
41
+ const n = parseInt(fibInput);
42
+ const result = (window as any).fibonacci(n);
43
+ setResult(`Fibonacci(${n}) = ${result}`);
44
+ } catch (error) {
45
+ setResult(`Error: ${error.message}`);
46
+ }
47
+ };
48
+
49
+ const testProcessArray = () => {
50
+ if (!wasmReady) return;
51
+ try {
52
+ const testArray = [1, 2, 3, 4, 5];
53
+ const result = (window as any).processArray(testArray);
54
+ const resultArray = Array.from(result);
55
+ setResult(`Process array [${testArray.join(', ')}] = [${resultArray.join(', ')}]`);
56
+ } catch (error) {
57
+ setResult(`Error: ${error.message}`);
58
+ }
59
+ };
60
+
61
+ const testFormatMessage = () => {
62
+ if (!wasmReady) return;
63
+ try {
64
+ const message = 'Hello from JavaScript!';
65
+ const result = (window as any).formatMessage(message);
66
+ setResult(`Formatted: ${result}`);
67
+ } catch (error) {
68
+ setResult(`Error: ${error.message}`);
69
+ }
70
+ };
71
+
72
+ const testPerformance = () => {
73
+ if (!wasmReady) return;
74
+ try {
75
+ const iterations = 100000;
76
+ const startTime = performance.now();
77
+ const result = (window as any).performanceBenchmark(iterations);
78
+ const endTime = performance.now();
79
+
80
+ setResult(
81
+ `Performance test: ${result.message}\n` +
82
+ `Result: ${result.result}\n` +
83
+ `Time: ${(endTime - startTime).toFixed(2)}ms`
84
+ );
85
+ } catch (error) {
86
+ setResult(`Error: ${error.message}`);
87
+ }
88
+ };
89
+
90
+ const testTypeDemo = () => {
91
+ if (!wasmReady) return;
92
+ try {
93
+ const result = (window as any).typeDemo();
94
+ setResult(
95
+ `Type demo results:\n` +
96
+ `String: ${result.string}\n` +
97
+ `Number: ${result.number}\n` +
98
+ `Boolean: ${result.boolean}\n` +
99
+ `Array: [${Array.from(result.array).join(', ')}]`
100
+ );
101
+ } catch (error) {
102
+ setResult(`Error: ${error.message}`);
103
+ }
104
+ };
105
+
106
+ return jsx('div', {
107
+ className: 'wasm-demo',
108
+ children: [
109
+ jsx('h1', { children: '🚀 Frontend Hamroun + Go WASM Demo' }),
110
+
111
+ jsx('div', {
112
+ className: 'status',
113
+ children: jsx('p', {
114
+ className: wasmReady ? 'ready' : 'loading',
115
+ children: wasmReady ? '✅ WASM Ready' : '⏳ Loading WASM...'
116
+ })
117
+ }),
118
+
119
+ jsx('div', {
120
+ className: 'controls',
121
+ children: [
122
+ jsx('h2', { children: '🧮 Arithmetic Operations' }),
123
+ jsx('div', {
124
+ className: 'input-group',
125
+ children: [
126
+ jsx('input', {
127
+ type: 'number',
128
+ value: input1,
129
+ onChange: (e: any) => setInput1(e.target.value),
130
+ placeholder: 'First number'
131
+ }),
132
+ jsx('span', { children: ' + ' }),
133
+ jsx('input', {
134
+ type: 'number',
135
+ value: input2,
136
+ onChange: (e: any) => setInput2(e.target.value),
137
+ placeholder: 'Second number'
138
+ }),
139
+ jsx('button', {
140
+ onClick: testAdd,
141
+ disabled: !wasmReady,
142
+ children: 'Calculate'
143
+ })
144
+ ]
145
+ }),
146
+
147
+ jsx('h2', { children: '🔢 Fibonacci Sequence' }),
148
+ jsx('div', {
149
+ className: 'input-group',
150
+ children: [
151
+ jsx('input', {
152
+ type: 'number',
153
+ value: fibInput,
154
+ onChange: (e: any) => setFibInput(e.target.value),
155
+ placeholder: 'Fibonacci number'
156
+ }),
157
+ jsx('button', {
158
+ onClick: testFibonacci,
159
+ disabled: !wasmReady,
160
+ children: 'Calculate Fibonacci'
161
+ })
162
+ ]
163
+ }),
164
+
165
+ jsx('h2', { children: '🔧 Advanced Functions' }),
166
+ jsx('div', {
167
+ className: 'button-group',
168
+ children: [
169
+ jsx('button', {
170
+ onClick: testProcessArray,
171
+ disabled: !wasmReady,
172
+ children: 'Process Array'
173
+ }),
174
+ jsx('button', {
175
+ onClick: testFormatMessage,
176
+ disabled: !wasmReady,
177
+ children: 'Format Message'
178
+ }),
179
+ jsx('button', {
180
+ onClick: testPerformance,
181
+ disabled: !wasmReady,
182
+ children: 'Performance Test'
183
+ }),
184
+ jsx('button', {
185
+ onClick: testTypeDemo,
186
+ disabled: !wasmReady,
187
+ children: 'Type Demo'
188
+ })
189
+ ]
190
+ })
191
+ ]
192
+ }),
193
+
194
+ jsx('div', {
195
+ className: 'result',
196
+ children: [
197
+ jsx('h3', { children: '📋 Result:' }),
198
+ jsx('pre', { children: result })
199
+ ]
200
+ })
201
+ ]
202
+ });
203
+ }
204
+
205
+ // App Component
206
+ function App() {
207
+ return jsx('div', {
208
+ className: 'app',
209
+ children: jsx(WasmDemo, {})
210
+ });
211
+ }
212
+
213
+ // Render the app
214
+ const appElement = document.getElementById('app');
215
+ if (appElement) {
216
+ render(jsx(App, {}), appElement);
217
+ }
218
+
219
+ // Import statements for hooks
220
+ import { useState, useEffect } from 'frontend-hamroun';
@@ -0,0 +1,21 @@
1
+ import { jsx, render } from 'frontend-hamroun';
2
+ import App from './App.tsx';
3
+
4
+ // Simple render without infinite loops
5
+ function startApp() {
6
+ const appElement = jsx(App, {});
7
+ const rootElement = document.getElementById('app');
8
+
9
+ if (rootElement) {
10
+ render(appElement, rootElement);
11
+ } else {
12
+ console.error('Root element not found');
13
+ }
14
+ }
15
+
16
+ // Start when DOM is ready
17
+ if (document.readyState === 'loading') {
18
+ document.addEventListener('DOMContentLoaded', startApp);
19
+ } else {
20
+ startApp();
21
+ }
@@ -0,0 +1,145 @@
1
+ import { createServer } from 'frontend-hamroun';
2
+ import { loadGoWasmFromFile } from 'frontend-hamroun';
3
+ import path from 'path';
4
+ import { fileURLToPath } from 'url';
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+
9
+ // Create server instance
10
+ const server = createServer({
11
+ port: 3000,
12
+ staticDir: './public',
13
+ pagesDir: './src/pages',
14
+ apiDir: './src/api'
15
+ });
16
+
17
+ // Server-side WASM integration example
18
+ async function initServerWasm() {
19
+ try {
20
+ console.log('🔄 Initializing server-side Go WASM...');
21
+
22
+ const wasmPath = path.join(__dirname, '..', 'public', 'example.wasm');
23
+ const wasmInstance = await loadGoWasmFromFile(wasmPath, {
24
+ debug: true,
25
+ onLoad: (instance) => {
26
+ console.log('✅ Go WASM loaded on server side');
27
+
28
+ // Test server-side WASM functions
29
+ try {
30
+ if (instance.functions.add) {
31
+ const result = instance.functions.add(5, 3);
32
+ console.log('Server WASM test - add(5, 3):', result);
33
+ }
34
+
35
+ if (instance.functions.fibonacci) {
36
+ const fibResult = instance.functions.fibonacci(10);
37
+ console.log('Server WASM test - fibonacci(10):', fibResult);
38
+ }
39
+ } catch (error) {
40
+ console.log('Server WASM test functions not available:', error.message);
41
+ }
42
+ }
43
+ });
44
+
45
+ // Make WASM instance available to API routes
46
+ server.getExpressApp().locals.wasm = wasmInstance;
47
+
48
+ } catch (error) {
49
+ console.error('❌ Failed to initialize server-side WASM:', error);
50
+ }
51
+ }
52
+
53
+ // API route example using server-side WASM
54
+ server.getExpressApp().get('/api/wasm/compute', async (req, res) => {
55
+ try {
56
+ const { operation, a, b } = req.query;
57
+ const wasmInstance = req.app.locals.wasm;
58
+
59
+ if (!wasmInstance) {
60
+ return res.status(500).json({ error: 'WASM not initialized' });
61
+ }
62
+
63
+ let result;
64
+ switch (operation) {
65
+ case 'add':
66
+ if (wasmInstance.functions.add) {
67
+ result = wasmInstance.functions.add(Number(a), Number(b));
68
+ } else {
69
+ throw new Error('Add function not available');
70
+ }
71
+ break;
72
+ case 'fibonacci':
73
+ if (wasmInstance.functions.fibonacci) {
74
+ result = wasmInstance.functions.fibonacci(Number(a));
75
+ } else {
76
+ throw new Error('Fibonacci function not available');
77
+ }
78
+ break;
79
+ default:
80
+ throw new Error('Unknown operation');
81
+ }
82
+
83
+ res.json({
84
+ success: true,
85
+ operation,
86
+ result,
87
+ serverTime: new Date().toISOString()
88
+ });
89
+ } catch (error) {
90
+ res.status(400).json({
91
+ success: false,
92
+ error: error.message
93
+ });
94
+ }
95
+ });
96
+
97
+ // Health check endpoint
98
+ server.getExpressApp().get('/api/health', (req, res) => {
99
+ res.json({
100
+ status: 'healthy',
101
+ wasm: !!req.app.locals.wasm,
102
+ timestamp: new Date().toISOString()
103
+ });
104
+ });
105
+
106
+ // Start server
107
+ async function startServer() {
108
+ try {
109
+ // Initialize WASM first
110
+ await initServerWasm();
111
+
112
+ // Start the server
113
+ await server.start();
114
+
115
+ console.log('🚀 Server started successfully!');
116
+ console.log('📋 Available endpoints:');
117
+ console.log(' • http://localhost:3000 - Main app');
118
+ console.log(' • http://localhost:3000/api/health - Health check');
119
+ console.log(' • http://localhost:3000/api/wasm/compute - WASM compute API');
120
+ console.log('');
121
+ console.log('💡 Example API calls:');
122
+ console.log(' • /api/wasm/compute?operation=add&a=5&b=3');
123
+ console.log(' • /api/wasm/compute?operation=fibonacci&a=10');
124
+
125
+ } catch (error) {
126
+ console.error('❌ Failed to start server:', error);
127
+ process.exit(1);
128
+ }
129
+ }
130
+
131
+ // Graceful shutdown
132
+ process.on('SIGTERM', async () => {
133
+ console.log('🛑 Received SIGTERM, shutting down gracefully...');
134
+ await server.stop();
135
+ process.exit(0);
136
+ });
137
+
138
+ process.on('SIGINT', async () => {
139
+ console.log('🛑 Received SIGINT, shutting down gracefully...');
140
+ await server.stop();
141
+ process.exit(0);
142
+ });
143
+
144
+ // Start the server
145
+ startServer();
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
5
+ "module": "ESNext",
6
+ "skipLibCheck": true,
7
+ "moduleResolution": "bundler",
8
+ "allowImportingTsExtensions": true,
9
+ "resolveJsonModule": true,
10
+ "isolatedModules": true,
11
+ "noEmit": true,
12
+ "jsx": "preserve",
13
+ "jsxImportSource": "frontend-hamroun",
14
+ "strict": true,
15
+ "noUnusedLocals": true,
16
+ "noUnusedParameters": true,
17
+ "noFallthroughCasesInSwitch": true
18
+ },
19
+ "include": ["src"],
20
+ "references": [{ "path": "./tsconfig.node.json" }]
21
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "composite": true,
4
+ "skipLibCheck": true,
5
+ "module": "ESNext",
6
+ "moduleResolution": "bundler",
7
+ "allowSyntheticDefaultImports": true,
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "types": ["node"]
11
+ },
12
+ "include": ["vite.config.*", "vitest.config.*", "build-wasm.js", "esbuild.config.js"]
13
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "ESNext",
5
+ "outDir": "dist",
6
+ "noEmit": false,
7
+ "jsx": "preserve",
8
+ "moduleResolution": "bundler",
9
+ "composite": true,
10
+ "skipLibCheck": true,
11
+ "allowSyntheticDefaultImports": true,
12
+ "strict": true,
13
+ "target": "ES2020",
14
+ "lib": ["ES2020"],
15
+ "types": ["node"],
16
+ "esModuleInterop": true,
17
+ "allowImportingTsExtensions": true,
18
+ "resolveJsonModule": true,
19
+ "isolatedModules": true
20
+ },
21
+ "include": ["src/server.ts", "**/*.ts", "**/*.js"],
22
+ "exclude": ["node_modules", "dist"]
23
+ }
@@ -0,0 +1,56 @@
1
+ import { defineConfig } from 'vite';
2
+ import path from 'path';
3
+
4
+ export default defineConfig({
5
+ resolve: {
6
+ alias: {
7
+ 'frontend-hamroun': path.resolve(__dirname, 'node_modules/frontend-hamroun')
8
+ }
9
+ },
10
+ build: {
11
+ outDir: 'dist',
12
+ ssr: 'src/server.ts',
13
+ rollupOptions: {
14
+ input: {
15
+ client: './src/client.tsx',
16
+ server: './src/server.ts'
17
+ },
18
+ output: {
19
+ entryFileNames: '[name].js',
20
+ chunkFileNames: 'assets/[name]-[hash].js',
21
+ assetFileNames: 'assets/[name]-[hash].[ext]'
22
+ },
23
+ // Exclude WASM build script from hot reload and external dependencies
24
+ external: ['./build-wasm.js']
25
+ },
26
+ target: 'esnext'
27
+ },
28
+ esbuild: {
29
+ jsxFactory: '_jsx',
30
+ jsxFragment: '_Fragment',
31
+ jsxInject: `import { jsx as _jsx, Fragment as _Fragment } from 'frontend-hamroun'`
32
+ },
33
+ server: {
34
+ fs: {
35
+ allow: ['..']
36
+ },
37
+ headers: {
38
+ 'Cross-Origin-Opener-Policy': 'same-origin',
39
+ 'Cross-Origin-Embedder-Policy': 'require-corp'
40
+ },
41
+ watch: {
42
+ // Prevent watching go directory and wasm files to avoid infinite loops
43
+ ignored: [
44
+ '**/go/**',
45
+ '**/*.wasm',
46
+ '**/build-wasm.js',
47
+ '**/wasm_exec.js'
48
+ ]
49
+ }
50
+ },
51
+ optimizeDeps: {
52
+ exclude: ['frontend-hamroun']
53
+ },
54
+ // Prevent Vite from processing WASM files
55
+ assetsInclude: ['**/*.wasm']
56
+ });
@@ -0,0 +1,103 @@
1
+ /**
2
+ * WASM Loader for Frontend Hamroun Go WASM integration
3
+ */
4
+
5
+ let wasmInstance = null;
6
+ let wasmReady = false;
7
+
8
+ export async function loadGoWasm(wasmPath, options = {}) {
9
+ if (wasmReady) {
10
+ return wasmInstance;
11
+ }
12
+
13
+ try {
14
+ console.log('🔄 Loading Go WASM module from:', wasmPath);
15
+
16
+ // Load the Go WASM runtime
17
+ if (!window.Go) {
18
+ await loadWasmExecJs();
19
+ }
20
+
21
+ // Create Go instance
22
+ const go = new window.Go();
23
+
24
+ // Fetch and instantiate WASM
25
+ const wasmModule = await WebAssembly.instantiateStreaming(
26
+ fetch(wasmPath),
27
+ go.importObject
28
+ );
29
+
30
+ // Run the Go program
31
+ go.run(wasmModule.instance);
32
+
33
+ wasmInstance = {
34
+ instance: wasmModule.instance,
35
+ module: wasmModule.module,
36
+ go: go
37
+ };
38
+
39
+ wasmReady = true;
40
+
41
+ console.log('✅ Go WASM module loaded successfully!');
42
+
43
+ if (options.onLoad) {
44
+ options.onLoad(wasmInstance);
45
+ }
46
+
47
+ return wasmInstance;
48
+
49
+ } catch (error) {
50
+ console.error('❌ Failed to load Go WASM module:', error);
51
+ throw error;
52
+ }
53
+ }
54
+
55
+ async function loadWasmExecJs() {
56
+ return new Promise((resolve, reject) => {
57
+ const script = document.createElement('script');
58
+ script.src = '/wasm_exec.js';
59
+ script.onload = resolve;
60
+ script.onerror = () => reject(new Error('Failed to load wasm_exec.js'));
61
+ document.head.appendChild(script);
62
+ });
63
+ }
64
+
65
+ export function isWasmReady() {
66
+ return wasmReady;
67
+ }
68
+
69
+ export function getWasmInstance() {
70
+ return wasmInstance;
71
+ }
72
+
73
+ // Utility functions for calling Go functions safely
74
+ export function callGoFunction(functionName, ...args) {
75
+ if (!wasmReady) {
76
+ throw new Error('WASM module not ready');
77
+ }
78
+
79
+ if (typeof window[functionName] !== 'function') {
80
+ throw new Error(`Go function '${functionName}' not found`);
81
+ }
82
+
83
+ return window[functionName](...args);
84
+ }
85
+
86
+ export async function waitForWasm(timeout = 10000) {
87
+ if (wasmReady) return wasmInstance;
88
+
89
+ return new Promise((resolve, reject) => {
90
+ const checkInterval = setInterval(() => {
91
+ if (wasmReady) {
92
+ clearInterval(checkInterval);
93
+ clearTimeout(timeoutId);
94
+ resolve(wasmInstance);
95
+ }
96
+ }, 100);
97
+
98
+ const timeoutId = setTimeout(() => {
99
+ clearInterval(checkInterval);
100
+ reject(new Error('WASM loading timeout'));
101
+ }, timeout);
102
+ });
103
+ }