@ruvector/edge-net 0.1.0 → 0.1.2

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/index.js ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @ruvector/edge-net - Universal Entry Point
3
+ *
4
+ * Auto-detects environment (Browser vs Node.js) and loads appropriate module
5
+ */
6
+
7
+ // Environment detection
8
+ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
9
+ const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
10
+
11
+ let wasmModule = null;
12
+ let initPromise = null;
13
+
14
+ /**
15
+ * Initialize the WASM module
16
+ * @returns {Promise<Object>} The initialized WASM module
17
+ */
18
+ export async function init() {
19
+ if (wasmModule) return wasmModule;
20
+
21
+ if (initPromise) return initPromise;
22
+
23
+ initPromise = (async () => {
24
+ if (isBrowser) {
25
+ // Browser: use web target
26
+ const wasm = await import('./ruvector_edge_net.js');
27
+ await wasm.default();
28
+ wasmModule = wasm;
29
+ } else if (isNode) {
30
+ // Node.js: Setup polyfills first
31
+ await setupNodePolyfills();
32
+
33
+ // Dynamic import for ESM compatibility
34
+ const { createRequire } = await import('module');
35
+ const require = createRequire(import.meta.url);
36
+
37
+ // Try nodejs target first, fall back to web target with polyfills
38
+ try {
39
+ wasmModule = require('./node/ruvector_edge_net.js');
40
+ } catch (e) {
41
+ // Fallback to web version with polyfills
42
+ const wasm = await import('./ruvector_edge_net.js');
43
+ await wasm.default();
44
+ wasmModule = wasm;
45
+ }
46
+ } else {
47
+ throw new Error('Unsupported environment');
48
+ }
49
+
50
+ return wasmModule;
51
+ })();
52
+
53
+ return initPromise;
54
+ }
55
+
56
+ /**
57
+ * Setup Node.js polyfills for web APIs
58
+ */
59
+ async function setupNodePolyfills() {
60
+ if (typeof globalThis.crypto === 'undefined') {
61
+ const { webcrypto } = await import('crypto');
62
+ globalThis.crypto = webcrypto;
63
+ }
64
+
65
+ if (typeof globalThis.performance === 'undefined') {
66
+ const { performance } = await import('perf_hooks');
67
+ globalThis.performance = performance;
68
+ }
69
+
70
+ // Mock minimal web APIs
71
+ if (typeof globalThis.window === 'undefined') {
72
+ globalThis.window = {
73
+ crypto: globalThis.crypto,
74
+ performance: globalThis.performance,
75
+ localStorage: createMemoryStorage(),
76
+ navigator: { userAgent: 'node' },
77
+ };
78
+ }
79
+
80
+ if (typeof globalThis.document === 'undefined') {
81
+ globalThis.document = {};
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Create in-memory storage for Node.js
87
+ */
88
+ function createMemoryStorage() {
89
+ const store = new Map();
90
+ return {
91
+ getItem: (key) => store.get(key) || null,
92
+ setItem: (key, value) => store.set(key, String(value)),
93
+ removeItem: (key) => store.delete(key),
94
+ clear: () => store.clear(),
95
+ get length() { return store.size; },
96
+ key: (i) => [...store.keys()][i] || null,
97
+ };
98
+ }
99
+
100
+ // Re-export everything from the web module for type compatibility
101
+ export * from './ruvector_edge_net.js';
102
+
103
+ // Default export is the init function
104
+ export default init;