gowm
Version:
Go Wasm Manager - Loader system for Go WebAssembly modules with GitHub repository support. Features intelligent auto-detection and enhanced memory management.
245 lines (194 loc) ⢠6.73 kB
Markdown
# GoWM Source Code Structure
This directory contains the unified GoWM source code with a clean, organized structure.
## š Directory Structure
```
src/
āāā core/ # Core GoWM implementation
ā āāā gowm.js # Main GoWM class
āāā loaders/ # Unified loader system
ā āāā unified-loader.js # Handles file, HTTP, and GitHub loading
āāā bridges/ # Unified bridge system
ā āāā unified-bridge.js # WASM-JavaScript interface
āāā legacy/ # Legacy files (for reference)
ā āāā loader.js # Original Node.js loader
ā āāā loader-browser.js # Original browser loader
ā āāā loader-safe.js # Original safe loader
ā āāā bridge.js # Original Node.js bridge
ā āāā bridge-browser.js # Original browser bridge
āāā index.js # Main entry point (CommonJS)
āāā browser.js # Browser-specific entry point
āāā README.md # This file
```
## š Key Features
### Unified Loader System
- **Single loader** that handles all source types:
- Local files (Node.js only)
- HTTP/HTTPS URLs
- GitHub repositories
- **Auto-detection** of source type
- **Fallback strategies** for GitHub repos
- **Cross-platform compatibility**
### Unified Bridge System
- **Enhanced memory management**
- **Multiple data type support**
- **Callback registration**
- **Comprehensive testing utilities**
- **Detailed statistics and monitoring**
### Backward Compatibility
- All existing APIs remain functional
- Legacy class names are still available
- Existing code will continue to work without changes
## š Usage Examples
### Basic Loading
```javascript
const gowm = require('gowm');
// Auto-detect source type
await gowm.load('path/to/module.wasm'); // Local file
await gowm.load('https://example.com/module.wasm'); // HTTP URL
await gowm.load('owner/repo'); // GitHub repo
```
### Specific Loading Methods
```javascript
// Local file (Node.js only)
await gowm.loadFromFile('./module.wasm');
// HTTP URL
await gowm.loadFromUrl('https://example.com/module.wasm');
// GitHub repository
await gowm.loadFromGitHub('owner/repo', {
branch: 'main',
path: 'dist',
filename: 'module.wasm'
});
```
### Advanced Usage
```javascript
const { GoWM, UnifiedWasmLoader, UnifiedWasmBridge } = require('gowm');
// Create custom instance
const customGoWM = new GoWM();
// Use loader directly
const loader = new UnifiedWasmLoader();
const module = await loader.loadModule('owner/repo');
// Use bridge directly
const bridge = new UnifiedWasmBridge(module);
```
## š§ Configuration Options
### Loading Options
- `name`: Module name (string)
- `branch`: Git branch for GitHub (string)
- `tag`: Git tag for GitHub (string)
- `path`: Path within repository (string)
- `filename`: Specific filename (string)
- `preInit`: Pre-initialize module (boolean, default: true)
- `timeout`: Initialization timeout (number, default: 15000ms)
- `goRuntimePath`: Custom Go runtime path (string)
### Example with Options
```javascript
await gowm.loadFromGitHub('owner/repo', {
name: 'my-module',
branch: 'develop',
path: 'build',
filename: 'custom.wasm',
timeout: 30000
});
```
## š Statistics and Monitoring
### Get System Statistics
```javascript
const stats = gowm.getStats();
console.log('Total modules:', stats.totalModules);
console.log('Memory usage:', stats.totalMemoryUsage);
console.log('Environment:', stats.environment);
```
### Test All Modules
```javascript
const testResults = gowm.testAll();
console.log('Test results:', testResults);
```
### Memory Usage
```javascript
const memoryUsage = gowm.getTotalMemoryUsage();
console.log('Total memory:', memoryUsage, 'bytes');
```
## š Browser Usage
### ES6 Modules
```javascript
import gowm from 'gowm/src/browser.js';
await gowm.loadFromGitHub('owner/repo');
```
### Global Usage
```html
<script src="path/to/gowm/src/browser.js"></script>
<script>
GoWM.loadFromUrl('https://example.com/module.wasm');
</script>
```
## š Migration from Legacy System
The unified system is fully backward compatible. No changes are required for existing code:
```javascript
// These continue to work as before
const gowm = require('gowm');
await gowm.load('module.wasm');
const bridge = gowm.get();
```
### Legacy Class Access
```javascript
// Still available for backward compatibility
const { WasmLoader, WasmBridge } = require('gowm');
// These are aliases to UnifiedWasmLoader and UnifiedWasmBridge
```
## š ļø Development
### Adding New Source Types
To add a new source type to the unified loader:
1. Add detection method to `UnifiedWasmLoader`
2. Add loading method for the source type
3. Update `loadWasmBytes` method to handle the new type
### Extending Bridge Functionality
To extend the bridge functionality:
1. Add new methods to `UnifiedWasmBridge`
2. Update statistics collection
3. Add tests to the `test()` method
## š API Reference
### Main Methods
- `load(source, options)` - Load from any source
- `loadFromFile(path, options)` - Load from local file
- `loadFromUrl(url, options)` - Load from HTTP URL
- `loadFromGitHub(repo, options)` - Load from GitHub
- `get(name)` - Get loaded module bridge
- `unload(name)` - Unload module
- `unloadAll()` - Unload all modules
- `listModules()` - List loaded modules
- `getStats()` - Get system statistics
- `testAll()` - Test all modules
### Utility Methods
- `isLoaded(name)` - Check if module is loaded
- `getTotalMemoryUsage()` - Get total memory usage
- `getHelp()` - Get help information
## š Troubleshooting
### Common Issues
1. **Module not found in GitHub repo**
- Check if the repo has WASM files
- Try specifying exact path and filename
- Check branch/tag name
2. **Memory allocation errors**
- Check if WASM module has malloc/free functions
- Verify buffer size limits
- Monitor memory usage with `getStats()`
3. **Browser compatibility**
- Ensure fetch API is available
- Check CORS policy for external URLs
- Use browser.js entry point for browser environments
### Debug Information
Enable debug logging by checking module statistics:
```javascript
const stats = gowm.getStats();
console.log('Debug info:', JSON.stringify(stats, null, 2));
```
## š Version History
- **v1.1.0**: Loader and bridge system
- **v1.x**: Legacy separate loader system (moved to `/legacy/`)
## š¤ Contributing
When contributing to the unified system:
1. Maintain backward compatibility
2. Add comprehensive tests
3. Update documentation
4. Follow the unified architecture pattern