gowm 1.0.3 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gowm",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Go Wasm Manager - Simplify integration of Go WebAssembly modules in your JavaScript projects",
5
5
  "main": "src/index.js",
6
6
  "browser": "src/browser.js",
@@ -64,5 +64,8 @@
64
64
  },
65
65
  "devDependencies": {
66
66
  "@types/node": "^20.0.0"
67
+ },
68
+ "dependencies": {
69
+ "node-fetch": "^3.3.2"
67
70
  }
68
- }
71
+ }
package/src/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # GoWM Source Architecture
2
+
3
+ ## Loader Files
4
+
5
+ ### `loader.js` (Principal)
6
+ - **Role**: Main WASM loader used by `index.js`
7
+ - **Features**:
8
+ - Support for local files and HTTP URLs
9
+ - Node.js and browser compatibility
10
+ - Automatic node-fetch import for HTTP requests in Node.js
11
+ - Module caching and cleanup
12
+ - **Usage**: Used by default in `index.js`
13
+
14
+ ### `loader-browser.js` (Spécialisé)
15
+ - **Role**: Browser-specific optimized loader
16
+ - **Features**:
17
+ - Lightweight version for browser environments
18
+ - No Node.js dependencies
19
+ - Optimized for web bundlers
20
+ - **Usage**: Can be used for browser-only builds
21
+
22
+ ### `loader-safe.js` (Alternative)
23
+ - **Role**: Enhanced version with additional error handling
24
+ - **Features**:
25
+ - Dynamic require loading to avoid bundling issues
26
+ - Enhanced error checking and fallbacks
27
+ - Better compatibility with various build systems
28
+ - **Usage**: Alternative for environments with strict security or bundling requirements
29
+
30
+ ## Core Files
31
+
32
+ ### `index.js`
33
+ - Main GoWM class with GitHub loading capabilities
34
+ - Uses `loader.js` by default
35
+
36
+ ### `bridge.js` & `bridge-browser.js`
37
+ - WASM instance wrapper and Go communication bridge
38
+ - Browser-specific optimizations in `bridge-browser.js`
39
+
40
+ ### `browser.js`
41
+ - Browser-specific utilities and optimizations
42
+
43
+ ---
44
+
45
+ ## File Usage
46
+
47
+ **Active files (used by package):**
48
+ - `index.js` ← imports `loader.js`
49
+ - `loader.js` (main)
50
+ - `loader-browser.js` (browser builds)
51
+ - `bridge.js` / `bridge-browser.js`
52
+
53
+ **Optional files:**
54
+ - `loader-safe.js` (alternative implementation)
package/src/index.js CHANGED
@@ -101,8 +101,24 @@ class GoWM {
101
101
  return `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${basePath}${options.filename}`;
102
102
  }
103
103
 
104
+ // If module name is provided, try module-specific paths first
105
+ let possibleFilenames = [];
106
+
107
+ if (options.name) {
108
+ possibleFilenames.push(
109
+ `${options.name}/main.wasm`,
110
+ `${options.name}/index.wasm`,
111
+ `${options.name}/${options.name}.wasm`,
112
+ `${options.name}/module.wasm`,
113
+ `${options.name}/dist/main.wasm`,
114
+ `${options.name}/dist/index.wasm`,
115
+ `${options.name}/build/main.wasm`,
116
+ `${options.name}/build/index.wasm`
117
+ );
118
+ }
119
+
104
120
  // Try different common WASM filenames with fallback
105
- const possibleFilenames = [
121
+ possibleFilenames.push(
106
122
  'main.wasm',
107
123
  'index.wasm',
108
124
  `${repo}.wasm`,
@@ -116,7 +132,7 @@ class GoWM {
116
132
  'build/main.wasm',
117
133
  'build/index.wasm',
118
134
  `build/${repo}.wasm`
119
- ];
135
+ );
120
136
 
121
137
  // Try each possible filename
122
138
  for (const filename of possibleFilenames) {
@@ -192,14 +208,21 @@ class GoWM {
192
208
  }
193
209
 
194
210
  /**
195
- * Check if a URL exists (for browsers)
211
+ * Check if a URL exists (for browsers and Node.js)
196
212
  * @param {string} url - URL to check
197
213
  * @returns {Promise<boolean>} Whether URL exists
198
214
  */
199
215
  async checkUrlExists(url) {
200
216
  if (typeof fetch === 'undefined') {
201
- // In Node.js, assume URL exists (will be validated during actual load)
202
- return true;
217
+ // In Node.js, import node-fetch if available
218
+ try {
219
+ const nodeFetch = require('node-fetch');
220
+ const response = await nodeFetch(url, { method: 'HEAD' });
221
+ return response.ok;
222
+ } catch (error) {
223
+ // If node-fetch is not available, assume URL exists (will be validated during actual load)
224
+ return true;
225
+ }
203
226
  }
204
227
 
205
228
  try {
package/src/loader.js CHANGED
@@ -34,12 +34,30 @@ class WasmLoader {
34
34
  const go = new globalThis.Go();
35
35
  let wasmBytes;
36
36
 
37
- if (this.isNode) {
37
+ // Check if wasmPath is a URL
38
+ const isUrl = wasmPath.startsWith('http://') || wasmPath.startsWith('https://');
39
+
40
+ if (this.isNode && !isUrl) {
41
+ // Local file path in Node.js
38
42
  if (!this.fs.existsSync(wasmPath)) {
39
43
  throw new Error(`WASM file not found: ${wasmPath}`);
40
44
  }
41
45
  wasmBytes = this.fs.readFileSync(wasmPath);
42
46
  } else {
47
+ // Use fetch for URLs in both Node.js and browser
48
+ if (this.isNode && typeof fetch === 'undefined') {
49
+ // Import node-fetch for Node.js environments that don't have fetch
50
+ try {
51
+ const nodeFetch = require('node-fetch');
52
+ global.fetch = nodeFetch;
53
+ } catch (e) {
54
+ // Fall back to built-in fetch (Node.js 18+) or throw error
55
+ if (typeof fetch === 'undefined') {
56
+ throw new Error('fetch is not available. Please install node-fetch: npm install node-fetch');
57
+ }
58
+ }
59
+ }
60
+
43
61
  const response = await fetch(wasmPath);
44
62
  if (!response.ok) {
45
63
  throw new Error(`Failed to fetch WASM file: ${response.status} ${response.statusText}`);