peerpigeon 0.0.1 → 0.0.3
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/README.md +4 -4
- package/examples/browser/index.html +2 -2
- package/examples/node/test-node.js +153 -0
- package/index.js +12 -0
- package/package.json +3 -3
- package/src/ConnectionManager.js +11 -4
- package/src/EnvironmentDetector.js +441 -0
- package/src/GossipManager.js +11 -3
- package/src/MediaManager.js +14 -0
- package/src/PeerConnection.js +30 -13
- package/src/PeerDiscovery.js +10 -3
- package/src/PeerPigeonMesh.js +187 -2
- package/src/SignalingClient.js +67 -14
- package/src/StorageManager.js +48 -6
- package/src/TimerUtils.js +36 -8
- package/websocket-server/README.md +0 -10
- package/websocket-server/server.js +1 -1
- package/examples/browser/app-dht-disabled.js +0 -42
- package/examples/browser/index-dht-disabled.html +0 -123
- package/examples/browser/ui-clean.js +0 -0
- package/websocket-server/memory-leak-test.js +0 -193
- package/websocket-server/node_modules/.package-lock.json +0 -29
- package/websocket-server/node_modules/ws/LICENSE +0 -20
- package/websocket-server/node_modules/ws/README.md +0 -548
- package/websocket-server/node_modules/ws/browser.js +0 -8
- package/websocket-server/node_modules/ws/index.js +0 -13
- package/websocket-server/node_modules/ws/lib/buffer-util.js +0 -131
- package/websocket-server/node_modules/ws/lib/constants.js +0 -18
- package/websocket-server/node_modules/ws/lib/event-target.js +0 -292
- package/websocket-server/node_modules/ws/lib/extension.js +0 -203
- package/websocket-server/node_modules/ws/lib/limiter.js +0 -55
- package/websocket-server/node_modules/ws/lib/permessage-deflate.js +0 -528
- package/websocket-server/node_modules/ws/lib/receiver.js +0 -706
- package/websocket-server/node_modules/ws/lib/sender.js +0 -602
- package/websocket-server/node_modules/ws/lib/stream.js +0 -161
- package/websocket-server/node_modules/ws/lib/subprotocol.js +0 -62
- package/websocket-server/node_modules/ws/lib/validation.js +0 -152
- package/websocket-server/node_modules/ws/lib/websocket-server.js +0 -550
- package/websocket-server/node_modules/ws/lib/websocket.js +0 -1388
- package/websocket-server/node_modules/ws/package.json +0 -69
- package/websocket-server/node_modules/ws/wrapper.mjs +0 -8
- package/websocket-server/package-lock.json +0 -39
- package/websocket-server/test.js +0 -133
package/README.md
CHANGED
|
@@ -258,11 +258,11 @@ npm start
|
|
|
258
258
|
# Serve the browser examples
|
|
259
259
|
npm run dev
|
|
260
260
|
```
|
|
261
|
-
# Navigate to http://localhost:8080/examples/browser/
|
|
262
261
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
262
|
+
#### Navigate to http://localhost:8080/examples/browser/
|
|
263
|
+
|
|
264
|
+
###### Test with query parameters
|
|
265
|
+
http://localhost:8080/examples/browser/?api=ws://localhost:3000
|
|
266
266
|
|
|
267
267
|
## 🔄 System Flow Diagrams
|
|
268
268
|
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
<p>WebRTC P2P mesh networking with XOR-based routing</p>
|
|
18
18
|
<div class="usage-hint">
|
|
19
19
|
<strong>Tip:</strong> You can pass the API Gateway URL via query parameter:<br>
|
|
20
|
-
<code>?api=
|
|
20
|
+
<code>?api=ws://localhost:3000</code>
|
|
21
21
|
</div>
|
|
22
22
|
</header>
|
|
23
23
|
|
|
@@ -183,7 +183,7 @@
|
|
|
183
183
|
<div class="input-group">
|
|
184
184
|
<label for="signaling-url">Signaling Server URL:</label>
|
|
185
185
|
<input type="url" id="signaling-url"
|
|
186
|
-
placeholder="
|
|
186
|
+
placeholder="ws://localhost:3000">
|
|
187
187
|
<small class="input-help">
|
|
188
188
|
Auto-populated from ?api, ?url, or ?signaling query parameters. Enter base API Gateway URL (without /signaling path).
|
|
189
189
|
</small>
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node.js Test for PeerPigeon Mesh Networking
|
|
5
|
+
* Demonstrates P2P mesh functionality in Node.js environment
|
|
6
|
+
*
|
|
7
|
+
* Prerequisites:
|
|
8
|
+
* - npm install ws @koush/wrtc
|
|
9
|
+
* - Start signaling server: npm run server
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { PeerPigeonMesh } from '../../index.js';
|
|
13
|
+
import WebSocket from 'ws';
|
|
14
|
+
import wrtc from '@koush/wrtc';
|
|
15
|
+
|
|
16
|
+
// Make WebRTC available globally for Node.js
|
|
17
|
+
global.RTCPeerConnection = wrtc.RTCPeerConnection;
|
|
18
|
+
global.RTCSessionDescription = wrtc.RTCSessionDescription;
|
|
19
|
+
global.RTCIceCandidate = wrtc.RTCIceCandidate;
|
|
20
|
+
global.WebSocket = WebSocket;
|
|
21
|
+
|
|
22
|
+
const SIGNALING_URL = 'ws://localhost:3000';
|
|
23
|
+
const TEST_DURATION = 30000; // 30 seconds
|
|
24
|
+
|
|
25
|
+
console.log('� PeerPigeon Node.js Mesh Test\n');
|
|
26
|
+
|
|
27
|
+
async function createPeer(peerName, peerId = null) {
|
|
28
|
+
const mesh = new PeerPigeonMesh({
|
|
29
|
+
peerId,
|
|
30
|
+
enableWebDHT: true,
|
|
31
|
+
ignoreEnvironmentErrors: true
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Set up event handlers
|
|
35
|
+
mesh.addEventListener('statusChanged', (event) => {
|
|
36
|
+
console.log(`[${peerName}] Status: ${event.type} - ${event.message || ''}`);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
mesh.addEventListener('peerConnected', (event) => {
|
|
40
|
+
console.log(`[${peerName}] 🔗 Connected to peer: ${event.peerId.substring(0, 8)}...`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
mesh.addEventListener('peerDisconnected', (event) => {
|
|
44
|
+
console.log(`[${peerName}] 💔 Disconnected from peer: ${event.peerId.substring(0, 8)}... (${event.reason})`);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
mesh.addEventListener('messageReceived', (event) => {
|
|
48
|
+
const fromPeerId = event.from || 'unknown';
|
|
49
|
+
console.log(`[${peerName}] 📨 Received: "${event.content}" from ${fromPeerId.substring(0, 8)}...`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Initialize the mesh
|
|
53
|
+
await mesh.init();
|
|
54
|
+
console.log(`[${peerName}] Initialized with ID: ${mesh.peerId.substring(0, 8)}...`);
|
|
55
|
+
|
|
56
|
+
// Display environment info
|
|
57
|
+
const envReport = mesh.getEnvironmentReport();
|
|
58
|
+
console.log(`[${peerName}] Environment: Node.js ${envReport.node?.version}, WebRTC: ${envReport.capabilities.webrtc}, WebSocket: ${envReport.capabilities.webSocket}`);
|
|
59
|
+
|
|
60
|
+
return mesh;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function runTest() {
|
|
64
|
+
try {
|
|
65
|
+
console.log('Creating two mesh peers...\n');
|
|
66
|
+
|
|
67
|
+
// Create two peers
|
|
68
|
+
const peer1 = await createPeer('PEER-1');
|
|
69
|
+
const peer2 = await createPeer('PEER-2');
|
|
70
|
+
|
|
71
|
+
console.log('\nConnecting to signaling server...\n');
|
|
72
|
+
|
|
73
|
+
// Connect both peers to signaling server
|
|
74
|
+
await peer1.connect(SIGNALING_URL);
|
|
75
|
+
await peer2.connect(SIGNALING_URL);
|
|
76
|
+
|
|
77
|
+
// Wait for connections to establish
|
|
78
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
79
|
+
|
|
80
|
+
console.log('\nTesting message exchange...\n');
|
|
81
|
+
|
|
82
|
+
// Test messaging
|
|
83
|
+
let messageCount = 0;
|
|
84
|
+
const sendMessage = () => {
|
|
85
|
+
messageCount++;
|
|
86
|
+
const message = `Hello from Node.js test #${messageCount}`;
|
|
87
|
+
|
|
88
|
+
if (messageCount % 2 === 1) {
|
|
89
|
+
console.log(`[PEER-1] 📤 Sending: "${message}"`);
|
|
90
|
+
peer1.sendMessage(message);
|
|
91
|
+
} else {
|
|
92
|
+
console.log(`[PEER-2] 📤 Sending: "${message}"`);
|
|
93
|
+
peer2.sendMessage(message);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Send messages every 3 seconds
|
|
98
|
+
const messageInterval = setInterval(sendMessage, 3000);
|
|
99
|
+
sendMessage(); // Send first message immediately
|
|
100
|
+
|
|
101
|
+
// Test DHT functionality
|
|
102
|
+
setTimeout(async () => {
|
|
103
|
+
console.log('\nTesting DHT functionality...\n');
|
|
104
|
+
try {
|
|
105
|
+
await peer1.dhtPut('test-key', { data: 'Hello from Node.js DHT!', timestamp: Date.now() });
|
|
106
|
+
console.log('[PEER-1] 📝 Stored data in DHT');
|
|
107
|
+
|
|
108
|
+
const value = await peer2.dhtGet('test-key');
|
|
109
|
+
console.log(`[PEER-2] 📖 Retrieved from DHT:`, value);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error('DHT test failed:', error.message);
|
|
112
|
+
}
|
|
113
|
+
}, 5000);
|
|
114
|
+
|
|
115
|
+
// Display stats periodically
|
|
116
|
+
const statsInterval = setInterval(() => {
|
|
117
|
+
const peer1ConnectedCount = peer1.getConnectedPeerCount();
|
|
118
|
+
const peer2ConnectedCount = peer2.getConnectedPeerCount();
|
|
119
|
+
console.log('\n--- Stats ---');
|
|
120
|
+
console.log(`PEER-1: ${peer1ConnectedCount} connected peers`);
|
|
121
|
+
console.log(`PEER-2: ${peer2ConnectedCount} connected peers`);
|
|
122
|
+
}, 10000);
|
|
123
|
+
|
|
124
|
+
// Run test for specified duration
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
console.log('\n🏁 Test completed, cleaning up...\n');
|
|
127
|
+
clearInterval(messageInterval);
|
|
128
|
+
clearInterval(statsInterval);
|
|
129
|
+
|
|
130
|
+
peer1.disconnect();
|
|
131
|
+
peer2.disconnect();
|
|
132
|
+
|
|
133
|
+
process.exit(0);
|
|
134
|
+
}, TEST_DURATION);
|
|
135
|
+
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('❌ Test failed:', error);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Check if signaling server is running
|
|
143
|
+
console.log(`Checking signaling server at ${SIGNALING_URL}...`);
|
|
144
|
+
const testWs = new WebSocket(SIGNALING_URL);
|
|
145
|
+
testWs.on('open', () => {
|
|
146
|
+
testWs.close();
|
|
147
|
+
console.log('✅ Signaling server is running\n');
|
|
148
|
+
runTest();
|
|
149
|
+
});
|
|
150
|
+
testWs.on('error', () => {
|
|
151
|
+
console.error('❌ Signaling server not running. Start it with: npm run server');
|
|
152
|
+
process.exit(1);
|
|
153
|
+
});
|
package/index.js
CHANGED
|
@@ -4,5 +4,17 @@ export { PeerConnection } from './src/PeerConnection.js';
|
|
|
4
4
|
export { SignalingClient } from './src/SignalingClient.js';
|
|
5
5
|
export { WebDHT } from './src/WebDHT.js';
|
|
6
6
|
|
|
7
|
+
// Export environment detection utilities
|
|
8
|
+
export {
|
|
9
|
+
EnvironmentDetector,
|
|
10
|
+
environmentDetector,
|
|
11
|
+
isBrowser,
|
|
12
|
+
isNodeJS,
|
|
13
|
+
isWorker,
|
|
14
|
+
hasWebRTC,
|
|
15
|
+
hasWebSocket,
|
|
16
|
+
getEnvironmentReport
|
|
17
|
+
} from './src/EnvironmentDetector.js';
|
|
18
|
+
|
|
7
19
|
// Export the WebSocket server class for programmatic use
|
|
8
20
|
export { PeerPigeonServer } from './server/index.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peerpigeon",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "WebRTC-based peer-to-peer mesh networking library with intelligent routing and signaling server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/draeder/peerpigeon#readme",
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"ws": "^8.18.0"
|
|
56
|
+
"ws": "^8.18.0",
|
|
57
|
+
"@koush/wrtc": "^0.5.3"
|
|
57
58
|
},
|
|
58
59
|
"devDependencies": {
|
|
59
|
-
"@koush/wrtc": "^0.5.3",
|
|
60
60
|
"http-server": "^14.1.1"
|
|
61
61
|
},
|
|
62
62
|
"engines": {
|
package/src/ConnectionManager.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from './EventEmitter.js';
|
|
2
2
|
import { PeerConnection } from './PeerConnection.js';
|
|
3
|
+
import { environmentDetector } from './EnvironmentDetector.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Manages individual peer connections, timeouts, and connection attempts
|
|
@@ -637,10 +638,16 @@ export class ConnectionManager extends EventEmitter {
|
|
|
637
638
|
clearInterval(this.cleanupInterval);
|
|
638
639
|
}
|
|
639
640
|
|
|
640
|
-
// Run cleanup every 30 seconds
|
|
641
|
-
|
|
642
|
-
this.
|
|
643
|
-
|
|
641
|
+
// Run cleanup every 30 seconds - environment-aware timer
|
|
642
|
+
if (environmentDetector.isBrowser) {
|
|
643
|
+
this.cleanupInterval = window.setInterval(() => {
|
|
644
|
+
this.cleanupStalePeers();
|
|
645
|
+
}, 30000);
|
|
646
|
+
} else {
|
|
647
|
+
this.cleanupInterval = setInterval(() => {
|
|
648
|
+
this.cleanupStalePeers();
|
|
649
|
+
}, 30000);
|
|
650
|
+
}
|
|
644
651
|
}
|
|
645
652
|
|
|
646
653
|
/**
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EnvironmentDetector - Utility for detecting runtime environment and capabilities
|
|
3
|
+
* Provides comprehensive environment detection for browser, Node.js, Worker contexts
|
|
4
|
+
*/
|
|
5
|
+
export class EnvironmentDetector {
|
|
6
|
+
constructor() {
|
|
7
|
+
this._cache = new Map();
|
|
8
|
+
this._init();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
_init() {
|
|
12
|
+
// Pre-compute common detections to avoid repeated checks
|
|
13
|
+
this._cache.set('isBrowser', this._detectBrowser());
|
|
14
|
+
this._cache.set('isNodeJS', this._detectNodeJS());
|
|
15
|
+
this._cache.set('isWorker', this._detectWorker());
|
|
16
|
+
this._cache.set('isServiceWorker', this._detectServiceWorker());
|
|
17
|
+
this._cache.set('isWebWorker', this._detectWebWorker());
|
|
18
|
+
this._cache.set('isSharedWorker', this._detectSharedWorker());
|
|
19
|
+
this._cache.set('isDeno', this._detectDeno());
|
|
20
|
+
this._cache.set('isBun', this._detectBun());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Primary environment detection
|
|
24
|
+
_detectBrowser() {
|
|
25
|
+
return typeof window !== 'undefined' &&
|
|
26
|
+
typeof document !== 'undefined' &&
|
|
27
|
+
typeof navigator !== 'undefined';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_detectNodeJS() {
|
|
31
|
+
return typeof process !== 'undefined' &&
|
|
32
|
+
process.versions != null &&
|
|
33
|
+
process.versions.node != null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_detectDeno() {
|
|
37
|
+
return typeof Deno !== 'undefined';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_detectBun() {
|
|
41
|
+
return typeof Bun !== 'undefined';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_detectWorker() {
|
|
45
|
+
return typeof importScripts !== 'undefined' ||
|
|
46
|
+
this._detectServiceWorker() ||
|
|
47
|
+
this._detectWebWorker() ||
|
|
48
|
+
this._detectSharedWorker();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
_detectServiceWorker() {
|
|
52
|
+
return typeof ServiceWorkerGlobalScope !== 'undefined' &&
|
|
53
|
+
typeof self !== 'undefined' &&
|
|
54
|
+
self instanceof ServiceWorkerGlobalScope;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_detectWebWorker() {
|
|
58
|
+
return typeof DedicatedWorkerGlobalScope !== 'undefined' &&
|
|
59
|
+
typeof self !== 'undefined' &&
|
|
60
|
+
self instanceof DedicatedWorkerGlobalScope;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_detectSharedWorker() {
|
|
64
|
+
return typeof SharedWorkerGlobalScope !== 'undefined' &&
|
|
65
|
+
typeof self !== 'undefined' &&
|
|
66
|
+
self instanceof SharedWorkerGlobalScope;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Public getters
|
|
70
|
+
get isBrowser() {
|
|
71
|
+
return this._cache.get('isBrowser');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get isNodeJS() {
|
|
75
|
+
return this._cache.get('isNodeJS');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get isWorker() {
|
|
79
|
+
return this._cache.get('isWorker');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get isServiceWorker() {
|
|
83
|
+
return this._cache.get('isServiceWorker');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get isWebWorker() {
|
|
87
|
+
return this._cache.get('isWebWorker');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get isSharedWorker() {
|
|
91
|
+
return this._cache.get('isSharedWorker');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
get isDeno() {
|
|
95
|
+
return this._cache.get('isDeno');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get isBun() {
|
|
99
|
+
return this._cache.get('isBun');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get isServer() {
|
|
103
|
+
return this.isNodeJS || this.isDeno || this.isBun;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get isClient() {
|
|
107
|
+
return this.isBrowser || this.isWorker;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// WebRTC capability detection
|
|
111
|
+
get hasWebRTC() {
|
|
112
|
+
if (this.isBrowser) {
|
|
113
|
+
return typeof RTCPeerConnection !== 'undefined' ||
|
|
114
|
+
typeof webkitRTCPeerConnection !== 'undefined' ||
|
|
115
|
+
typeof mozRTCPeerConnection !== 'undefined';
|
|
116
|
+
}
|
|
117
|
+
if (this.isNodeJS) {
|
|
118
|
+
// Check for Node.js WebRTC implementations or globally injected WebRTC
|
|
119
|
+
return typeof global !== 'undefined' &&
|
|
120
|
+
typeof global.RTCPeerConnection !== 'undefined' ||
|
|
121
|
+
typeof RTCPeerConnection !== 'undefined';
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get hasDataChannel() {
|
|
127
|
+
return this.hasWebRTC && this.isBrowser;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get hasGetUserMedia() {
|
|
131
|
+
if (!this.isBrowser) return false;
|
|
132
|
+
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) ||
|
|
133
|
+
!!(navigator.getUserMedia ||
|
|
134
|
+
navigator.webkitGetUserMedia ||
|
|
135
|
+
navigator.mozGetUserMedia ||
|
|
136
|
+
navigator.msGetUserMedia);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// WebSocket capability detection
|
|
140
|
+
get hasWebSocket() {
|
|
141
|
+
if (this.isBrowser || this.isWorker) {
|
|
142
|
+
return typeof WebSocket !== 'undefined';
|
|
143
|
+
}
|
|
144
|
+
if (this.isNodeJS) {
|
|
145
|
+
// Check for globally injected WebSocket first
|
|
146
|
+
if (typeof global !== 'undefined' && typeof global.WebSocket !== 'undefined') {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (typeof WebSocket !== 'undefined') {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
// Try ES module import first
|
|
155
|
+
if (typeof require !== 'undefined') {
|
|
156
|
+
require.resolve('ws');
|
|
157
|
+
return true;
|
|
158
|
+
} else {
|
|
159
|
+
// In ES modules, we can't easily check if a package is available
|
|
160
|
+
// without actually importing it, so we'll assume it might be available
|
|
161
|
+
return false; // Conservative approach
|
|
162
|
+
}
|
|
163
|
+
} catch (e) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Storage capability detection
|
|
171
|
+
get hasLocalStorage() {
|
|
172
|
+
if (!this.isBrowser) return false;
|
|
173
|
+
try {
|
|
174
|
+
const test = '__storage_test__';
|
|
175
|
+
localStorage.setItem(test, test);
|
|
176
|
+
localStorage.removeItem(test);
|
|
177
|
+
return true;
|
|
178
|
+
} catch (e) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
get hasSessionStorage() {
|
|
184
|
+
if (!this.isBrowser) return false;
|
|
185
|
+
try {
|
|
186
|
+
const test = '__storage_test__';
|
|
187
|
+
sessionStorage.setItem(test, test);
|
|
188
|
+
sessionStorage.removeItem(test);
|
|
189
|
+
return true;
|
|
190
|
+
} catch (e) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
get hasIndexedDB() {
|
|
196
|
+
if (!this.isBrowser) return false;
|
|
197
|
+
return typeof indexedDB !== 'undefined';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
get hasCookies() {
|
|
201
|
+
if (!this.isBrowser) return false;
|
|
202
|
+
return typeof document !== 'undefined' &&
|
|
203
|
+
typeof document.cookie === 'string';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Network and connectivity detection
|
|
207
|
+
get hasNetworkInformation() {
|
|
208
|
+
if (!this.isBrowser) return false;
|
|
209
|
+
return typeof navigator !== 'undefined' &&
|
|
210
|
+
'connection' in navigator;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
get isOnline() {
|
|
214
|
+
if (this.isBrowser) {
|
|
215
|
+
return typeof navigator !== 'undefined' ? navigator.onLine : true;
|
|
216
|
+
}
|
|
217
|
+
return true; // Assume online for server environments
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
get networkType() {
|
|
221
|
+
if (this.hasNetworkInformation) {
|
|
222
|
+
return navigator.connection.effectiveType || 'unknown';
|
|
223
|
+
}
|
|
224
|
+
return 'unknown';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Crypto capabilities
|
|
228
|
+
get hasCrypto() {
|
|
229
|
+
if (this.isBrowser || this.isWorker) {
|
|
230
|
+
return typeof crypto !== 'undefined' &&
|
|
231
|
+
typeof crypto.subtle !== 'undefined';
|
|
232
|
+
}
|
|
233
|
+
if (this.isNodeJS) {
|
|
234
|
+
try {
|
|
235
|
+
if (typeof require !== 'undefined') {
|
|
236
|
+
require('crypto');
|
|
237
|
+
return true;
|
|
238
|
+
} else {
|
|
239
|
+
// In ES modules, crypto is a built-in Node.js module
|
|
240
|
+
return typeof process !== 'undefined' && process.versions.node;
|
|
241
|
+
}
|
|
242
|
+
} catch (e) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
get hasRandomValues() {
|
|
250
|
+
if (this.isBrowser || this.isWorker) {
|
|
251
|
+
return typeof crypto !== 'undefined' &&
|
|
252
|
+
typeof crypto.getRandomValues === 'function';
|
|
253
|
+
}
|
|
254
|
+
if (this.isNodeJS) {
|
|
255
|
+
try {
|
|
256
|
+
if (typeof require !== 'undefined') {
|
|
257
|
+
const crypto = require('crypto');
|
|
258
|
+
return typeof crypto.randomBytes === 'function';
|
|
259
|
+
} else {
|
|
260
|
+
// In ES modules, crypto is a built-in Node.js module
|
|
261
|
+
return typeof process !== 'undefined' && process.versions.node;
|
|
262
|
+
}
|
|
263
|
+
} catch (e) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Performance and timing
|
|
271
|
+
get hasPerformanceNow() {
|
|
272
|
+
return typeof performance !== 'undefined' &&
|
|
273
|
+
typeof performance.now === 'function';
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
get hasHighResolutionTime() {
|
|
277
|
+
if (this.isNodeJS) {
|
|
278
|
+
return typeof process.hrtime === 'function' ||
|
|
279
|
+
typeof process.hrtime.bigint === 'function';
|
|
280
|
+
}
|
|
281
|
+
return this.hasPerformanceNow;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Browser-specific detection
|
|
285
|
+
getBrowserInfo() {
|
|
286
|
+
if (!this.isBrowser) return null;
|
|
287
|
+
|
|
288
|
+
const userAgent = navigator.userAgent;
|
|
289
|
+
const browsers = {
|
|
290
|
+
chrome: /Chrome\/(\d+)/.exec(userAgent),
|
|
291
|
+
firefox: /Firefox\/(\d+)/.exec(userAgent),
|
|
292
|
+
safari: /Safari\/(\d+)/.exec(userAgent) && !/Chrome/.test(userAgent),
|
|
293
|
+
edge: /Edge\/(\d+)/.exec(userAgent),
|
|
294
|
+
ie: /MSIE (\d+)/.exec(userAgent) || /Trident.*rv:(\d+)/.exec(userAgent)
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
for (const [browser, match] of Object.entries(browsers)) {
|
|
298
|
+
if (match) {
|
|
299
|
+
return {
|
|
300
|
+
name: browser,
|
|
301
|
+
version: match[1] || 'unknown'
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return { name: 'unknown', version: 'unknown' };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Node.js-specific detection
|
|
310
|
+
getNodeInfo() {
|
|
311
|
+
if (!this.isNodeJS) return null;
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
version: process.version,
|
|
315
|
+
platform: process.platform,
|
|
316
|
+
arch: process.arch,
|
|
317
|
+
versions: process.versions
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Device and platform detection
|
|
322
|
+
getPlatformInfo() {
|
|
323
|
+
if (this.isBrowser) {
|
|
324
|
+
return {
|
|
325
|
+
userAgent: navigator.userAgent,
|
|
326
|
+
platform: navigator.platform,
|
|
327
|
+
language: navigator.language,
|
|
328
|
+
languages: navigator.languages || [navigator.language],
|
|
329
|
+
cookieEnabled: navigator.cookieEnabled,
|
|
330
|
+
onLine: navigator.onLine,
|
|
331
|
+
hardwareConcurrency: navigator.hardwareConcurrency || 1
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (this.isNodeJS) {
|
|
336
|
+
return this.getNodeInfo();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Feature detection for specific APIs
|
|
343
|
+
hasFeature(feature) {
|
|
344
|
+
const features = {
|
|
345
|
+
// WebRTC
|
|
346
|
+
'webrtc': () => this.hasWebRTC,
|
|
347
|
+
'datachannel': () => this.hasDataChannel,
|
|
348
|
+
'getusermedia': () => this.hasGetUserMedia,
|
|
349
|
+
|
|
350
|
+
// WebSocket
|
|
351
|
+
'websocket': () => this.hasWebSocket,
|
|
352
|
+
|
|
353
|
+
// Storage
|
|
354
|
+
'localstorage': () => this.hasLocalStorage,
|
|
355
|
+
'sessionstorage': () => this.hasSessionStorage,
|
|
356
|
+
'indexeddb': () => this.hasIndexedDB,
|
|
357
|
+
'cookies': () => this.hasCookies,
|
|
358
|
+
|
|
359
|
+
// Crypto
|
|
360
|
+
'crypto': () => this.hasCrypto,
|
|
361
|
+
'randomvalues': () => this.hasRandomValues,
|
|
362
|
+
|
|
363
|
+
// Performance
|
|
364
|
+
'performance': () => this.hasPerformanceNow,
|
|
365
|
+
'hrtime': () => this.hasHighResolutionTime,
|
|
366
|
+
|
|
367
|
+
// Network
|
|
368
|
+
'networkinfo': () => this.hasNetworkInformation,
|
|
369
|
+
'online': () => this.isOnline,
|
|
370
|
+
|
|
371
|
+
// Workers
|
|
372
|
+
'webworker': () => typeof Worker !== 'undefined',
|
|
373
|
+
'serviceworker': () => typeof navigator !== 'undefined' && 'serviceWorker' in navigator,
|
|
374
|
+
'sharedworker': () => typeof SharedWorker !== 'undefined'
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const featureCheck = features[feature.toLowerCase()];
|
|
378
|
+
return featureCheck ? featureCheck() : false;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Get comprehensive environment report
|
|
382
|
+
getEnvironmentReport() {
|
|
383
|
+
return {
|
|
384
|
+
runtime: {
|
|
385
|
+
isBrowser: this.isBrowser,
|
|
386
|
+
isNodeJS: this.isNodeJS,
|
|
387
|
+
isWorker: this.isWorker,
|
|
388
|
+
isServiceWorker: this.isServiceWorker,
|
|
389
|
+
isWebWorker: this.isWebWorker,
|
|
390
|
+
isSharedWorker: this.isSharedWorker,
|
|
391
|
+
isDeno: this.isDeno,
|
|
392
|
+
isBun: this.isBun,
|
|
393
|
+
isServer: this.isServer,
|
|
394
|
+
isClient: this.isClient
|
|
395
|
+
},
|
|
396
|
+
capabilities: {
|
|
397
|
+
webrtc: this.hasWebRTC,
|
|
398
|
+
dataChannel: this.hasDataChannel,
|
|
399
|
+
getUserMedia: this.hasGetUserMedia,
|
|
400
|
+
webSocket: this.hasWebSocket,
|
|
401
|
+
localStorage: this.hasLocalStorage,
|
|
402
|
+
sessionStorage: this.hasSessionStorage,
|
|
403
|
+
indexedDB: this.hasIndexedDB,
|
|
404
|
+
cookies: this.hasCookies,
|
|
405
|
+
crypto: this.hasCrypto,
|
|
406
|
+
randomValues: this.hasRandomValues,
|
|
407
|
+
performance: this.hasPerformanceNow,
|
|
408
|
+
networkInfo: this.hasNetworkInformation
|
|
409
|
+
},
|
|
410
|
+
platform: this.getPlatformInfo(),
|
|
411
|
+
browser: this.getBrowserInfo(),
|
|
412
|
+
node: this.getNodeInfo(),
|
|
413
|
+
network: {
|
|
414
|
+
online: this.isOnline,
|
|
415
|
+
type: this.networkType
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Static method for quick environment check
|
|
421
|
+
static detect() {
|
|
422
|
+
return new EnvironmentDetector();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Static method for single feature check
|
|
426
|
+
static hasFeature(feature) {
|
|
427
|
+
const detector = new EnvironmentDetector();
|
|
428
|
+
return detector.hasFeature(feature);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Export singleton instance for convenience
|
|
433
|
+
export const environmentDetector = new EnvironmentDetector();
|
|
434
|
+
|
|
435
|
+
// Export individual detection functions for tree-shaking
|
|
436
|
+
export const isBrowser = () => environmentDetector.isBrowser;
|
|
437
|
+
export const isNodeJS = () => environmentDetector.isNodeJS;
|
|
438
|
+
export const isWorker = () => environmentDetector.isWorker;
|
|
439
|
+
export const hasWebRTC = () => environmentDetector.hasWebRTC;
|
|
440
|
+
export const hasWebSocket = () => environmentDetector.hasWebSocket;
|
|
441
|
+
export const getEnvironmentReport = () => environmentDetector.getEnvironmentReport();
|