frontend-hamroun 1.2.74 → 1.2.77

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 (194) hide show
  1. package/dist/Counter.d.ts +0 -0
  2. package/dist/batch/package.json +16 -0
  3. package/dist/client-router/package.json +16 -0
  4. package/dist/component/package.json +16 -0
  5. package/dist/context/package.json +16 -0
  6. package/dist/event-bus/package.json +16 -0
  7. package/dist/forms/package.json +16 -0
  8. package/dist/hooks/package.json +16 -0
  9. package/dist/hooks-0728361a.cjs +1 -0
  10. package/dist/hooks-b58f947c.js +133 -0
  11. package/dist/hooks.js +1 -0
  12. package/dist/hooks.mjs +13 -0
  13. package/dist/index.js +1 -384
  14. package/dist/index.mjs +130 -374
  15. package/dist/jsx-runtime/package.json +16 -0
  16. package/dist/jsx-runtime.js +1 -0
  17. package/dist/jsx-runtime.mjs +64 -0
  18. package/dist/lifecycle-events/package.json +16 -0
  19. package/dist/package.json +71 -0
  20. package/dist/render-component/package.json +16 -0
  21. package/dist/renderer/package.json +16 -0
  22. package/dist/renderer.js +1 -0
  23. package/dist/renderer.mjs +27 -0
  24. package/dist/router/package.json +16 -0
  25. package/dist/server/package.json +17 -0
  26. package/dist/server/src/batch.d.ts +3 -0
  27. package/dist/server/src/batch.js +23 -0
  28. package/dist/server/src/batch.js.map +1 -0
  29. package/dist/server/src/client-router.d.ts +60 -0
  30. package/dist/server/src/client-router.js +210 -0
  31. package/dist/server/src/client-router.js.map +1 -0
  32. package/dist/server/src/component.d.ts +14 -0
  33. package/dist/server/src/component.js +106 -0
  34. package/dist/server/src/component.js.map +1 -0
  35. package/dist/server/src/context.d.ts +13 -0
  36. package/dist/server/src/context.js +21 -0
  37. package/dist/server/src/context.js.map +1 -0
  38. package/dist/server/src/event-bus.d.ts +23 -0
  39. package/dist/server/src/event-bus.js +75 -0
  40. package/dist/server/src/event-bus.js.map +1 -0
  41. package/dist/server/src/forms.d.ts +40 -0
  42. package/dist/server/src/forms.js +148 -0
  43. package/dist/server/src/forms.js.map +1 -0
  44. package/dist/server/src/hooks.d.ts +12 -0
  45. package/dist/server/src/hooks.js +170 -0
  46. package/dist/server/src/hooks.js.map +1 -0
  47. package/dist/server/src/index.client.d.ts +12 -0
  48. package/dist/server/src/index.client.js +14 -0
  49. package/dist/server/src/index.client.js.map +1 -0
  50. package/dist/server/src/index.d.ts +88 -0
  51. package/dist/server/src/index.js +78 -0
  52. package/dist/server/src/index.js.map +1 -0
  53. package/dist/server/src/jsx-runtime/jsx-dev-runtime.d.ts +1 -0
  54. package/dist/server/src/jsx-runtime/jsx-dev-runtime.js +2 -0
  55. package/dist/server/src/jsx-runtime/jsx-dev-runtime.js.map +1 -0
  56. package/dist/server/src/jsx-runtime/jsx-runtime.d.ts +4 -0
  57. package/dist/server/src/jsx-runtime/jsx-runtime.js +41 -0
  58. package/dist/server/src/jsx-runtime/jsx-runtime.js.map +1 -0
  59. package/dist/server/src/jsx-runtime.d.ts +20 -0
  60. package/dist/server/src/jsx-runtime.js +105 -0
  61. package/dist/server/src/jsx-runtime.js.map +1 -0
  62. package/dist/server/src/lifecycle-events.d.ts +108 -0
  63. package/dist/server/src/lifecycle-events.js +177 -0
  64. package/dist/server/src/lifecycle-events.js.map +1 -0
  65. package/dist/server/src/renderComponent.d.ts +14 -0
  66. package/dist/server/src/renderComponent.js +25 -0
  67. package/dist/server/src/renderComponent.js.map +1 -0
  68. package/dist/server/src/renderer.d.ts +2 -0
  69. package/dist/server/src/renderer.js +31 -0
  70. package/dist/server/src/renderer.js.map +1 -0
  71. package/dist/server/src/router.d.ts +55 -0
  72. package/dist/server/src/router.js +166 -0
  73. package/dist/server/src/router.js.map +1 -0
  74. package/dist/server/src/server/api-router.d.ts +15 -0
  75. package/dist/server/src/server/api-router.js +111 -0
  76. package/dist/server/src/server/api-router.js.map +1 -0
  77. package/dist/server/src/server/auth.d.ts +32 -0
  78. package/dist/server/src/server/auth.js +80 -0
  79. package/dist/server/src/server/auth.js.map +1 -0
  80. package/dist/server/src/server/database.d.ts +24 -0
  81. package/dist/server/src/server/database.js +135 -0
  82. package/dist/server/src/server/database.js.map +1 -0
  83. package/dist/server/src/server/index.d.ts +127 -0
  84. package/dist/server/src/server/index.js +388 -0
  85. package/dist/server/src/server/index.js.map +1 -0
  86. package/dist/server/src/server/middleware.d.ts +11 -0
  87. package/dist/server/src/server/middleware.js +46 -0
  88. package/dist/server/src/server/middleware.js.map +1 -0
  89. package/dist/server/src/server/server.d.ts +9 -0
  90. package/dist/server/src/server/server.js +87 -0
  91. package/dist/server/src/server/server.js.map +1 -0
  92. package/dist/server/src/server/templates.d.ts +28 -0
  93. package/dist/server/src/server/templates.js +204 -0
  94. package/dist/server/src/server/templates.js.map +1 -0
  95. package/dist/server/src/server/types.d.ts +38 -0
  96. package/dist/server/src/server/types.js +4 -0
  97. package/dist/server/src/server/types.js.map +1 -0
  98. package/dist/server/src/server/utils.d.ts +70 -0
  99. package/dist/server/src/server/utils.js +156 -0
  100. package/dist/server/src/server/utils.js.map +1 -0
  101. package/dist/server/src/server/wasm.d.ts +9 -0
  102. package/dist/server/src/server/wasm.js +117 -0
  103. package/dist/server/src/server/wasm.js.map +1 -0
  104. package/dist/server/src/server-renderer.d.ts +5 -0
  105. package/dist/server/src/server-renderer.js +106 -0
  106. package/dist/server/src/server-renderer.js.map +1 -0
  107. package/dist/server/src/server-types.d.ts +42 -0
  108. package/dist/server/src/server-types.js +6 -0
  109. package/dist/server/src/server-types.js.map +1 -0
  110. package/dist/server/src/store.d.ts +41 -0
  111. package/dist/server/src/store.js +99 -0
  112. package/dist/server/src/store.js.map +1 -0
  113. package/dist/server/src/types.d.ts +19 -0
  114. package/dist/server/src/types.js +2 -0
  115. package/dist/server/src/types.js.map +1 -0
  116. package/dist/server/src/utils.d.ts +46 -0
  117. package/dist/server/src/utils.js +144 -0
  118. package/dist/server/src/utils.js.map +1 -0
  119. package/dist/server/src/vdom.d.ts +8 -0
  120. package/dist/server/src/vdom.js +22 -0
  121. package/dist/server/src/vdom.js.map +1 -0
  122. package/dist/server/src/wasm.d.ts +36 -0
  123. package/dist/server/src/wasm.js +159 -0
  124. package/dist/server/src/wasm.js.map +1 -0
  125. package/dist/server/tsconfig.server.tsbuildinfo +1 -0
  126. package/dist/server-renderer/package.json +16 -0
  127. package/dist/server-renderer.js +1 -0
  128. package/dist/server-renderer.mjs +64 -0
  129. package/dist/store/package.json +16 -0
  130. package/dist/types/package.json +16 -0
  131. package/dist/utils/package.json +16 -0
  132. package/dist/vdom/package.json +16 -0
  133. package/dist/wasm/package.json +16 -0
  134. package/dist/wasm.js +1 -0
  135. package/dist/wasm.mjs +103 -0
  136. package/package.json +14 -13
  137. package/templates/complete-app/build.js +284 -0
  138. package/templates/complete-app/package.json +40 -0
  139. package/templates/complete-app/public/styles.css +345 -0
  140. package/templates/complete-app/src/api/index.js +31 -0
  141. package/templates/complete-app/src/client.js +93 -0
  142. package/templates/complete-app/src/components/App.js +66 -0
  143. package/templates/complete-app/src/components/Footer.js +19 -0
  144. package/templates/complete-app/src/components/Header.js +38 -0
  145. package/templates/complete-app/src/pages/About.js +59 -0
  146. package/templates/complete-app/src/pages/Home.js +54 -0
  147. package/templates/complete-app/src/pages/WasmDemo.js +136 -0
  148. package/templates/complete-app/src/server.js +186 -0
  149. package/templates/complete-app/src/wasm/build.bat +16 -0
  150. package/templates/complete-app/src/wasm/build.sh +16 -0
  151. package/templates/complete-app/src/wasm/example.go +101 -0
  152. package/templates/fullstack-app/build/main.css +225 -15
  153. package/templates/fullstack-app/build/main.css.map +2 -2
  154. package/templates/fullstack-app/build/main.js +657 -372
  155. package/templates/fullstack-app/build/main.js.map +4 -4
  156. package/templates/fullstack-app/build.ts +3 -4
  157. package/templates/fullstack-app/public/styles.css +222 -15
  158. package/templates/fullstack-app/server.ts +46 -12
  159. package/templates/fullstack-app/src/components/ClientHome.tsx +0 -0
  160. package/templates/fullstack-app/src/components/ErrorBoundary.tsx +36 -0
  161. package/templates/fullstack-app/src/components/Layout.tsx +23 -26
  162. package/templates/fullstack-app/src/components/StateDemo.tsx +207 -0
  163. package/templates/fullstack-app/src/components/UserList.tsx +30 -13
  164. package/templates/fullstack-app/src/data/api.ts +173 -38
  165. package/templates/fullstack-app/src/main.tsx +88 -154
  166. package/templates/fullstack-app/src/middleware.ts +28 -0
  167. package/templates/fullstack-app/src/pages/404.tsx +28 -0
  168. package/templates/fullstack-app/src/pages/[id].tsx +0 -0
  169. package/templates/fullstack-app/src/pages/_app.tsx +11 -0
  170. package/templates/fullstack-app/src/pages/_document.tsx +25 -0
  171. package/templates/fullstack-app/src/pages/_error.tsx +45 -0
  172. package/templates/fullstack-app/src/pages/about.tsx +71 -0
  173. package/templates/fullstack-app/src/pages/api/users/[id].ts +73 -0
  174. package/templates/fullstack-app/src/pages/api/users/index.ts +43 -0
  175. package/templates/fullstack-app/src/pages/index.tsx +97 -20
  176. package/templates/fullstack-app/src/pages/users/[id].tsx +153 -0
  177. package/templates/fullstack-app/src/pages/wasm-demo.tsx +1 -0
  178. package/templates/go/build.sh +43 -43
  179. package/templates/go/example.go +99 -86
  180. package/templates/go-wasm-app/babel.config.js +8 -2
  181. package/templates/go-wasm-app/build-wasm.js +84 -84
  182. package/templates/go-wasm-app/build.config.js +62 -0
  183. package/templates/go-wasm-app/build.js +218 -0
  184. package/templates/go-wasm-app/package.json +21 -11
  185. package/templates/go-wasm-app/public/index.html +49 -53
  186. package/templates/go-wasm-app/server.js +56 -695
  187. package/templates/go-wasm-app/src/app.js +173 -0
  188. package/templates/go-wasm-app/vite.config.js +16 -5
  189. package/templates/ssr-template/client.js +54 -26
  190. package/templates/ssr-template/server.js +5 -28
  191. package/templates/ssr-template/vite.config.js +21 -5
  192. package/dist/index.d.ts +0 -1
  193. package/dist/index.js.map +0 -1
  194. package/dist/index.mjs.map +0 -1
@@ -0,0 +1,136 @@
1
+ import { jsx, useState, useEffect, useContext } from '../client.js';
2
+ import { AppContext } from '../components/App.js';
3
+
4
+ export default function WasmDemo() {
5
+ const { wasm } = useContext(AppContext);
6
+
7
+ // State for error display
8
+ const [error, setError] = useState(null);
9
+
10
+ // State for calculation form
11
+ const [num1, setNum1] = useState(5);
12
+ const [num2, setNum2] = useState(7);
13
+ const [result, setResult] = useState(null);
14
+
15
+ // State for JSON form
16
+ const [jsonInput, setJsonInput] = useState('{"name": "Baraqex", "version": "1.0"}');
17
+ const [jsonResult, setJsonResult] = useState(null);
18
+
19
+ useEffect(() => {
20
+ document.title = 'WebAssembly Demo - Baraqex Complete App';
21
+
22
+ // Demonstrate a calculation on mount if WASM is available
23
+ if (wasm && wasm.functions.goAdd) {
24
+ try {
25
+ const sum = wasm.functions.goAdd(5, 7);
26
+ setResult(sum);
27
+ } catch (err) {
28
+ console.error('Error in initial WASM calculation:', err);
29
+ }
30
+ }
31
+ }, [wasm]);
32
+
33
+ // Function to calculate using WASM
34
+ const calculateResult = () => {
35
+ if (!wasm || !wasm.functions.goAdd) {
36
+ setError('WASM module not loaded or function not available');
37
+ return;
38
+ }
39
+
40
+ try {
41
+ // Call the WASM function
42
+ const sum = wasm.functions.goAdd(parseInt(num1), parseInt(num2));
43
+ setResult(sum);
44
+ setError(null);
45
+ } catch (err) {
46
+ console.error('Error calling WASM function:', err);
47
+ setError('Error calling WASM function: ' + err);
48
+ }
49
+ };
50
+
51
+ // Function to parse JSON using WASM
52
+ const parseJsonWithWasm = () => {
53
+ if (!wasm || !wasm.functions.goParseJSON) {
54
+ setError('WASM module not loaded or JSON parsing function not available');
55
+ return;
56
+ }
57
+
58
+ try {
59
+ // Call the WASM function
60
+ const parsed = wasm.functions.goParseJSON(jsonInput);
61
+ setJsonResult(JSON.stringify(parsed, null, 2));
62
+ setError(null);
63
+ } catch (err) {
64
+ console.error('Error parsing JSON with WASM:', err);
65
+ setError('Error parsing JSON with WASM: ' + err);
66
+ }
67
+ };
68
+
69
+ return (
70
+ <div className="wasm-demo-page">
71
+ <h1>WebAssembly Integration Demo</h1>
72
+
73
+ {!wasm && (
74
+ <div className="wasm-status error">
75
+ <h3>WASM Not Available</h3>
76
+ <p>WebAssembly module could not be loaded. Please check the console for details.</p>
77
+ </div>
78
+ )}
79
+
80
+ {error && (
81
+ <div className="wasm-status error">
82
+ <h3>Error:</h3>
83
+ <pre>{error}</pre>
84
+ </div>
85
+ )}
86
+
87
+ {wasm && (
88
+ <div className="wasm-status success">
89
+ <h3>WASM Module Loaded Successfully</h3>
90
+ <p>The Go WebAssembly module has been loaded and is ready to use.</p>
91
+ </div>
92
+ )}
93
+
94
+ <div className="demo-section">
95
+ <h2>WASM Addition</h2>
96
+ <div className="calculator">
97
+ <input
98
+ type="number"
99
+ value={num1}
100
+ onChange={(e) => setNum1(e.target.value)}
101
+ />
102
+ <span> + </span>
103
+ <input
104
+ type="number"
105
+ value={num2}
106
+ onChange={(e) => setNum2(e.target.value)}
107
+ />
108
+ <button onClick={calculateResult}>Calculate</button>
109
+ <div className="result">
110
+ Result: <strong>{result}</strong>
111
+ </div>
112
+ </div>
113
+
114
+ <h2>WASM JSON Parsing</h2>
115
+ <div className="json-parser">
116
+ <textarea
117
+ rows="5"
118
+ value={jsonInput}
119
+ onChange={(e) => setJsonInput(e.target.value)}
120
+ />
121
+ <button onClick={parseJsonWithWasm}>Parse JSON</button>
122
+ {jsonResult && (
123
+ <pre className="json-result">{jsonResult}</pre>
124
+ )}
125
+ </div>
126
+
127
+ <h2>Available WASM Functions</h2>
128
+ <ul className="function-list">
129
+ {wasm && Object.keys(wasm.functions).map(funcName => (
130
+ <li key={funcName}>{funcName}</li>
131
+ ))}
132
+ </ul>
133
+ </div>
134
+ </div>
135
+ );
136
+ }
@@ -0,0 +1,186 @@
1
+ import express from 'express';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { fileURLToPath } from 'url';
5
+ import compression from 'compression';
6
+ import cors from 'cors';
7
+ import { Server as SocketServer } from 'socket.io';
8
+ import http from 'http';
9
+ import dotenv from 'dotenv';
10
+
11
+ // Import from frontend-hamroun
12
+ import {
13
+ renderToString,
14
+ loadGoWasmFromFile,
15
+ initNodeWasm
16
+ } from 'frontend-hamroun/server';
17
+
18
+ // Import components
19
+ import App from './components/App.js';
20
+ import { ApiRouter } from './api/index.js';
21
+
22
+ // Load environment variables
23
+ dotenv.config();
24
+
25
+ // Get __dirname equivalent in ESM
26
+ const __filename = fileURLToPath(import.meta.url);
27
+ const __dirname = path.dirname(__filename);
28
+
29
+ // Server configuration
30
+ const PORT = process.env.PORT || 3000;
31
+ const PUBLIC_DIR = path.join(__dirname, 'public');
32
+ const isProd = process.env.NODE_ENV === 'production';
33
+
34
+ // Initialize the express app
35
+ const app = express();
36
+ const server = http.createServer(app);
37
+ const io = new SocketServer(server);
38
+
39
+ // WASM state
40
+ let wasmInstance = null;
41
+
42
+ // Middleware
43
+ app.use(compression()); // Compress responses
44
+ app.use(express.json()); // Parse JSON request bodies
45
+ app.use(express.urlencoded({ extended: true })); // Parse URL-encoded request bodies
46
+ app.use(cors()); // Enable CORS
47
+
48
+ // Serve static files
49
+ app.use(express.static(PUBLIC_DIR));
50
+
51
+ // Setup API routes
52
+ app.use('/api', ApiRouter);
53
+
54
+ // Setup Socket.IO
55
+ io.on('connection', (socket) => {
56
+ console.log('New client connected');
57
+
58
+ socket.on('message', (data) => {
59
+ console.log('Message received:', data);
60
+ io.emit('message', data); // Broadcast to all clients
61
+ });
62
+
63
+ socket.on('disconnect', () => {
64
+ console.log('Client disconnected');
65
+ });
66
+ });
67
+
68
+ // Initialize WASM
69
+ async function initWasm() {
70
+ try {
71
+ // Initialize Node WASM environment
72
+ await initNodeWasm();
73
+
74
+ // Check if WASM file exists
75
+ const wasmPath = path.join(__dirname, 'public/wasm/example.wasm');
76
+ if (fs.existsSync(wasmPath)) {
77
+ // Load WASM module
78
+ wasmInstance = await loadGoWasmFromFile(wasmPath, {
79
+ debug: false,
80
+ goWasmPath: path.join(__dirname, 'public/wasm/wasm_exec.js')
81
+ });
82
+
83
+ console.log('Server WASM module loaded successfully');
84
+ } else {
85
+ console.log('WASM file not found, skipping WASM initialization');
86
+ }
87
+ } catch (error) {
88
+ console.error('Failed to initialize WASM:', error);
89
+ }
90
+ }
91
+
92
+ // Handle all other requests with SSR
93
+ app.get('*', async (req, res) => {
94
+ try {
95
+ // Prepare context for rendering
96
+ const context = {
97
+ url: req.url,
98
+ wasm: wasmInstance,
99
+ initialData: {
100
+ // Add any data needed for initial render
101
+ path: req.path,
102
+ query: req.query,
103
+ timestamp: new Date().toISOString()
104
+ }
105
+ };
106
+
107
+ // Render app to string
108
+ const appHtml = renderToString(<App context={context} />);
109
+
110
+ // Format the initial data as JSON for client hydration
111
+ const initialDataScript = `
112
+ <script id="__INITIAL_DATA__" type="application/json">
113
+ ${JSON.stringify(context.initialData).replace(/</g, '\\u003c')}
114
+ </script>
115
+ `;
116
+
117
+ // Generate full HTML
118
+ const html = `
119
+ <!DOCTYPE html>
120
+ <html lang="en">
121
+ <head>
122
+ <meta charset="UTF-8">
123
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
124
+ <title>Complete Baraqex App</title>
125
+ <link rel="stylesheet" href="/styles.css">
126
+ </head>
127
+ <body>
128
+ <div id="app" data-ssr>${appHtml}</div>
129
+ ${initialDataScript}
130
+ <script type="module" src="/client.js"></script>
131
+ </body>
132
+ </html>
133
+ `;
134
+
135
+ res.send(html);
136
+ } catch (error) {
137
+ console.error('Error rendering page:', error);
138
+
139
+ // Send error page
140
+ res.status(500).send(`
141
+ <!DOCTYPE html>
142
+ <html>
143
+ <head>
144
+ <title>Server Error</title>
145
+ <style>
146
+ body { font-family: system-ui, sans-serif; padding: 2rem; line-height: 1.5; }
147
+ .error { background: #fff0f0; border: 1px solid #ffcaca; border-radius: 4px; padding: 1rem; }
148
+ pre { background: #f5f5f5; padding: 1rem; overflow: auto; border-radius: 4px; }
149
+ </style>
150
+ </head>
151
+ <body>
152
+ <h1>Server Error</h1>
153
+ <div class="error">
154
+ <p>${error.message}</p>
155
+ ${isProd ? '' : `<pre>${error.stack}</pre>`}
156
+ </div>
157
+ </body>
158
+ </html>
159
+ `);
160
+ }
161
+ });
162
+
163
+ // Start the server
164
+ async function startServer() {
165
+ // Initialize WASM
166
+ await initWasm();
167
+
168
+ // Start listening
169
+ server.listen(PORT, () => {
170
+ console.log(`
171
+ ┌─────────────────────────────────────────────────────┐
172
+ │ │
173
+ │ Complete Baraqex App Server running! │
174
+ │ │
175
+ │ - Local: http://localhost:${PORT} │
176
+ │ - API: http://localhost:${PORT}/api │
177
+ │ │
178
+ │ Mode: ${isProd ? 'production' : 'development'} │
179
+ │ │
180
+ └─────────────────────────────────────────────────────┘
181
+ `);
182
+ });
183
+ }
184
+
185
+ // Run the server
186
+ startServer().catch(console.error);
@@ -0,0 +1,16 @@
1
+ @echo off
2
+ echo Building Go WASM module...
3
+
4
+ set GOOS=js
5
+ set GOARCH=wasm
6
+ go build -o example.wasm example.go
7
+
8
+ if %ERRORLEVEL% NEQ 0 (
9
+ echo Error: Failed to build Go WASM module
10
+ exit /b 1
11
+ )
12
+
13
+ for /f "tokens=*" %%g in ('go env GOROOT') do (set GOROOT=%%g)
14
+ copy "%GOROOT%\misc\wasm\wasm_exec.js" .
15
+
16
+ echo Build complete!
@@ -0,0 +1,16 @@
1
+ #!/bin/bash
2
+ echo "Building Go WASM module..."
3
+
4
+ # Set environment variables for Go to compile to WASM
5
+ GOOS=js GOARCH=wasm go build -o example.wasm example.go
6
+
7
+ if [ $? -ne 0 ]; then
8
+ echo "Error: Failed to build Go WASM module"
9
+ exit 1
10
+ fi
11
+
12
+ # Get Go WASM exec runtime
13
+ GOROOT=$(go env GOROOT)
14
+ cp "$GOROOT/misc/wasm/wasm_exec.js" .
15
+
16
+ echo "Build completed!"
@@ -0,0 +1,101 @@
1
+ package main
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "syscall/js"
7
+ )
8
+
9
+ // These functions will be exposed to JavaScript
10
+ func main() {
11
+ // Set up functions we want to expose to JavaScript
12
+ js.Global().Set("goAdd", js.FuncOf(add))
13
+ js.Global().Set("goSubtract", js.FuncOf(subtract))
14
+ js.Global().Set("goMultiply", js.FuncOf(multiply))
15
+ js.Global().Set("goDivide", js.FuncOf(divide))
16
+ js.Global().Set("goParseJSON", js.FuncOf(parseJSON))
17
+
18
+ // Print to browser console
19
+ fmt.Println("Go WASM module initialized")
20
+
21
+ // Keep the program running
22
+ <-make(chan bool)
23
+ }
24
+
25
+ // add adds two numbers and returns the result
26
+ func add(this js.Value, args []js.Value) interface{} {
27
+ if len(args) != 2 {
28
+ return js.Error{Message: "Invalid number of arguments passed to add"}
29
+ }
30
+
31
+ // Convert the JavaScript values to Go values
32
+ a := args[0].Int()
33
+ b := args[1].Int()
34
+
35
+ return a + b
36
+ }
37
+
38
+ // subtract subtracts the second number from the first and returns the result
39
+ func subtract(this js.Value, args []js.Value) interface{} {
40
+ if len(args) != 2 {
41
+ return js.Error{Message: "Invalid number of arguments passed to subtract"}
42
+ }
43
+
44
+ a := args[0].Int()
45
+ b := args[1].Int()
46
+
47
+ return a - b
48
+ }
49
+
50
+ // multiply multiplies two numbers and returns the result
51
+ func multiply(this js.Value, args []js.Value) interface{} {
52
+ if len(args) != 2 {
53
+ return js.Error{Message: "Invalid number of arguments passed to multiply"}
54
+ }
55
+
56
+ a := args[0].Int()
57
+ b := args[1].Int()
58
+
59
+ return a * b
60
+ }
61
+
62
+ // divide divides the first number by the second and returns the result
63
+ func divide(this js.Value, args []js.Value) interface{} {
64
+ if len(args) != 2 {
65
+ return js.Error{Message: "Invalid number of arguments passed to divide"}
66
+ }
67
+
68
+ a := args[0].Int()
69
+ b := args[1].Int()
70
+
71
+ if b == 0 {
72
+ return js.Error{Message: "Division by zero"}
73
+ }
74
+
75
+ return a / b
76
+ }
77
+
78
+ // parseJSON parses a JSON string and returns a JavaScript object
79
+ func parseJSON(this js.Value, args []js.Value) interface{} {
80
+ if len(args) != 1 {
81
+ return js.Error{Message: "Invalid number of arguments passed to parseJSON"}
82
+ }
83
+
84
+ // Get the JSON string from JavaScript
85
+ jsonStr := args[0].String()
86
+
87
+ // Parse the JSON string into a map
88
+ var data map[string]interface{}
89
+ if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
90
+ return js.Error{Message: "Error parsing JSON: " + err.Error()}
91
+ }
92
+
93
+ // Convert the map to a JavaScript object
94
+ result := make(map[string]interface{})
95
+ for k, v := range data {
96
+ result[k] = v
97
+ }
98
+
99
+ // Return the JavaScript object
100
+ return result
101
+ }