-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-nodejs-wasm.mjs
More file actions
74 lines (62 loc) · 2.48 KB
/
test-nodejs-wasm.mjs
File metadata and controls
74 lines (62 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
/**
* Node.js test for SharpCanvas WASM
* Tests loading and running SharpCanvas in Node.js environment
*/
import { createServer } from 'http';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const wwwroot = join(__dirname, 'SharpCanvas.Blazor.Wasm/bin/Debug/net8.0/wwwroot');
console.log('🚀 SharpCanvas Node.js WASM Test\n');
console.log(`📁 Serving from: ${wwwroot}\n`);
// Simple HTTP server to serve WASM files
const server = createServer((req, res) => {
let filePath = join(wwwroot, req.url === '/' ? 'index.html' : req.url);
try {
const content = readFileSync(filePath);
// Set correct MIME types
const ext = filePath.split('.').pop();
const mimeTypes = {
'wasm': 'application/wasm',
'js': 'application/javascript',
'json': 'application/json',
'html': 'text/html',
'css': 'text/css',
'png': 'image/png',
'svg': 'image/svg+xml'
};
res.writeHead(200, {
'Content-Type': mimeTypes[ext] || 'text/plain',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin'
});
res.end(content);
} catch (err) {
res.writeHead(404);
res.end('Not found');
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`✅ Server running on http://localhost:${PORT}`);
console.log(`\n📖 Instructions:`);
console.log(` 1. Open http://localhost:${PORT} in your browser`);
console.log(` 2. Open DevTools Console to see SharpCanvas output`);
console.log(` 3. Try the canvas demos`);
console.log(`\n⏹️ Press Ctrl+C to stop\n`);
});
// Also test if we can load the WASM module directly
console.log('🔍 Checking WASM files...');
try {
const wasmPath = join(wwwroot, '_framework/dotnet.native.wasm');
const wasmBuffer = readFileSync(wasmPath);
console.log(`✅ dotnet.native.wasm loaded: ${(wasmBuffer.length / 1024 / 1024).toFixed(2)} MB`);
const contextPath = join(wwwroot, '_framework/Context.Skia.wasm');
const contextBuffer = readFileSync(contextPath);
console.log(`✅ Context.Skia.wasm loaded: ${(contextBuffer.length / 1024).toFixed(2)} MB`);
console.log('✅ All WASM files accessible\n');
} catch (err) {
console.error('❌ Error loading WASM files:', err.message);
}