wireweave 0.3.40 → 0.3.41

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 CHANGED
@@ -83,7 +83,38 @@ session.broadcast(new Uint8Array(payload)); // → all open peers
83
83
  session.send(somePeerPubkey, new Uint8Array(p)); // → one peer
84
84
  ```
85
85
 
86
- Options: `dataChannelOptions` (default `{ ordered: true }`) and `iceServers` (override the default STUN/TURN list). Construct multiple `DataSession`s in the same page to use distinct rooms / channel configurations.
86
+ Options: `dataChannelOptions` (default `{ ordered: true }`), `iceServers` (override the default STUN/TURN list for this session only), and `createPeerConnection` (see below). Construct multiple `DataSession`s in the same page to use distinct rooms / channel configurations.
87
+
88
+ `setIceServers(list)` / `getIceServers()` (also exported from `wireweave/data` and `wireweave/voice`) override the module-wide default ICE server list for every session created afterward — useful for pointing at your own TURN infrastructure once, instead of passing `iceServers` to each session.
89
+
90
+ ## Node hosts behind NAT: `createPeerConnection`
91
+
92
+ Both `DataSession` and `VoiceSession` accept a `createPeerConnection(config)` option. It defaults to a plain `new RTCPeerConnection(config)` (browser-shaped, works as-is with any WebRTC-polyfilled `globalThis`) — wireweave itself never imports a Node-specific WebRTC binding. A Node host that is itself likely to sit behind a restrictive NAT (a CLI tool, a headless server) can instead construct its own natively-tuned peer and hand it back:
93
+
94
+ ```js
95
+ import * as ndc from 'node-datachannel';
96
+ import { RTCPeerConnection as PolyfillRTCPeerConnection } from 'node-datachannel/polyfill';
97
+
98
+ const session = createDataSession({
99
+ fsm, xstate, relayPool: pool, auth, namespace: 'mygame',
100
+ createPeerConnection: (config) => {
101
+ // node-datachannel's native RtcConfig is richer than the W3C shape:
102
+ // enableIceUdpMux shares one UDP port across all peer connections
103
+ // (fewer ports to traverse through a firewall); portRangeBegin/End
104
+ // pins ICE to a fixed range you can port-forward; proxyServer routes
105
+ // ICE through a SOCKS5/HTTP proxy on networks that block direct UDP/TCP.
106
+ const nativePc = new ndc.PeerConnection('peer', {
107
+ iceServers: config.iceServers.map(s => s.urls),
108
+ enableIceUdpMux: true,
109
+ // portRangeBegin: 50000, portRangeEnd: 51000,
110
+ // proxyServer: { type: 'Socks5', ip: '127.0.0.1', port: 1080 }
111
+ });
112
+ return new PolyfillRTCPeerConnection({ peerConnection: nativePc });
113
+ }
114
+ });
115
+ ```
116
+
117
+ This is opt-in and additive — omit `createPeerConnection` and Node hosts behave exactly as before (a plain polyfilled `RTCPeerConnection`).
87
118
 
88
119
  ## game / 3-mode usage pattern
89
120
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wireweave",
3
- "version": "0.3.40",
3
+ "version": "0.3.41",
4
4
  "description": "nostr + webrtc voice + data SDK. networking layer for 247420 projects.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -55,8 +55,8 @@
55
55
  "nostr-tools": "^2.7.0"
56
56
  },
57
57
  "devDependencies": {
58
- "nostr-tools": "^2.7.0",
59
- "ws": "^8.18.0"
58
+ "nostr-tools": "^2.23.12",
59
+ "ws": "^8.21.1"
60
60
  },
61
61
  "publishConfig": {
62
62
  "access": "public"
package/src/data.js CHANGED
@@ -1,11 +1,23 @@
1
- const ICE_SERVERS = [
1
+ // STUN handles same-LAN / non-symmetric-NAT cases; TURN is required when both
2
+ // peers sit behind symmetric or restricted-cone NAT (typical home routers,
3
+ // most carrier-grade NAT, corporate networks). UDP, TCP, and TLS TURN variants
4
+ // are included so at least one path survives strict egress filtering. Kept in
5
+ // sync with voice.js's DEFAULT_ICE_SERVERS.
6
+ const DEFAULT_ICE_SERVERS = [
2
7
  { urls: 'stun:stun.l.google.com:19302' },
3
8
  { urls: 'stun:stun1.l.google.com:19302' },
4
9
  { urls: 'stun:stun.cloudflare.com:3478' },
5
- { urls: 'turn:openrelay.metered.ca:80', username: 'openrelayproject', credential: 'openrelayproject' },
6
- { urls: 'turn:openrelay.metered.ca:443', username: 'openrelayproject', credential: 'openrelayproject' },
10
+ { urls: 'stun:global.stun.twilio.com:3478' },
11
+ { urls: 'turn:global.relay.metered.ca:80', username: 'openrelayproject', credential: 'openrelayproject' },
12
+ { urls: 'turn:global.relay.metered.ca:80?transport=tcp', username: 'openrelayproject', credential: 'openrelayproject' },
13
+ { urls: 'turn:global.relay.metered.ca:443', username: 'openrelayproject', credential: 'openrelayproject' },
14
+ { urls: 'turns:global.relay.metered.ca:443?transport=tcp', username: 'openrelayproject', credential: 'openrelayproject' },
15
+ // Legacy hostname kept as a low-priority fallback in case some deployments still resolve it.
7
16
  { urls: 'turn:openrelay.metered.ca:443?transport=tcp', username: 'openrelayproject', credential: 'openrelayproject' }
8
17
  ];
18
+ let ICE_SERVERS = DEFAULT_ICE_SERVERS;
19
+ export const setIceServers = (list) => { if (Array.isArray(list) && list.length) ICE_SERVERS = list; };
20
+ export const getIceServers = () => ICE_SERVERS.slice();
9
21
 
10
22
  const PRESENCE_EXPIRY = 300000;
11
23
  const HEARTBEAT = 30000;
@@ -17,14 +29,24 @@ const deriveRoomId = async (namespace, room) => {
17
29
  return 'wwdata' + Array.from(new Uint8Array(h)).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 16);
18
30
  };
19
31
 
32
+ // Default: a plain browser-shaped RTCPeerConnection. Node hosts that want
33
+ // deeper NAT-traversal tuning (ICE/UDP port muxing, a fixed port range for
34
+ // port-forwarded firewalls, a SOCKS5/HTTP proxy for locked-down networks)
35
+ // can pass createPeerConnection to construct their own native peer (e.g.
36
+ // node-datachannel's PeerConnection with its richer RtcConfig) and hand it
37
+ // back wrapped as an RTCPeerConnection-shaped object — wireweave itself never
38
+ // depends on any Node-specific WebRTC binding.
39
+ const defaultCreatePeerConnection = (config) => new RTCPeerConnection(config);
40
+
20
41
  export class DataSession extends EventTarget {
21
- constructor({ fsm, xstate, relayPool, auth, namespace = '', dataChannelOptions = { ordered: true }, iceServers = null }) {
42
+ constructor({ fsm, xstate, relayPool, auth, namespace = '', dataChannelOptions = { ordered: true }, iceServers = null, createPeerConnection = defaultCreatePeerConnection }) {
22
43
  super();
23
44
  if (!fsm || !xstate || !relayPool || !auth) throw new Error('DataSession: missing deps');
24
45
  this.fsm = fsm; this.xstate = xstate; this.pool = relayPool; this.auth = auth;
25
46
  this.namespace = namespace;
26
47
  this.dcOptions = dataChannelOptions;
27
48
  this.iceServers = iceServers || ICE_SERVERS;
49
+ this.createPeerConnection = createPeerConnection;
28
50
  this.actor = null;
29
51
  this.room = ''; this.roomId = '';
30
52
  this.peers = new Map(); this.participants = new Map();
@@ -152,7 +174,7 @@ export class DataSession extends EventTarget {
152
174
  fsmActor.start();
153
175
  const peer = { pc: null, dc: null, pendingCandidates: [], bufferedCandidates: [], iceTimer: null, disconnectTimer: null, failCount: 0, state: 'new', fsm: fsmActor, remoteDescSet: false };
154
176
  this.peers.set(peerPubkey, peer);
155
- const pc = new RTCPeerConnection({ iceServers: this.iceServers, bundlePolicy: 'max-bundle' });
177
+ const pc = this.createPeerConnection({ iceServers: this.iceServers, bundlePolicy: 'max-bundle', iceCandidatePoolSize: 4, iceTransportPolicy: 'all' });
156
178
  peer.pc = pc;
157
179
  const isOfferer = this.auth.pubkey > peerPubkey;
158
180
  this._wirePeer(peer, peerPubkey, fsmActor, isOfferer);
package/src/relay-pool.js CHANGED
@@ -1,14 +1,15 @@
1
+ // Ordered fastest-first by measured connect latency; dead relays removed so
2
+ // signaling reaches a live relay immediately instead of racing dead hosts.
3
+ // (relay.nostr.band / nostr.wine / relay.current.fyi / relay.0xchat.com were
4
+ // unreachable — DNS-dead or refusing connections — and only added connect
5
+ // latency and console noise. Re-audit periodically.)
1
6
  const DEFAULT_RELAYS = [
2
- 'wss://relay.damus.io',
3
7
  'wss://relay.primal.net',
4
8
  'wss://nos.lol',
5
- 'wss://relay.snort.social',
6
- 'wss://relay.nostr.band',
7
- 'wss://nostr.wine',
8
- 'wss://relay.current.fyi',
9
9
  'wss://offchain.pub',
10
10
  'wss://nostr-pub.wellorder.net',
11
- 'wss://relay.0xchat.com'
11
+ 'wss://relay.snort.social',
12
+ 'wss://relay.damus.io'
12
13
  ];
13
14
 
14
15
  const SEEN_MAX = 10000;
package/src/servers.js CHANGED
@@ -62,13 +62,13 @@ export class Servers extends EventTarget {
62
62
  await this.switchTo(serverId);
63
63
  }
64
64
 
65
- async join(serverId) {
65
+ async join(serverId, { name = null, iconColor = '#5865F2', select = true } = {}) {
66
66
  try {
67
67
  const joined = JSON.parse(this.storage.getItem('zn_joined_servers') || '[]');
68
68
  if (!joined.includes(serverId)) { joined.push(serverId); safeSetItem(this.storage, this, 'zn_joined_servers', JSON.stringify(joined)); }
69
69
  } catch {}
70
- if (!this.servers.find(s => s.id === serverId)) { this.servers = [...this.servers, { id: serverId, name: serverId.slice(0, 8), iconColor: '#5865F2' }]; this._persist(); }
71
- await this.switchTo(serverId);
70
+ if (!this.servers.find(s => s.id === serverId)) { this.servers = [...this.servers, { id: serverId, name: name || serverId.slice(0, 8), iconColor }]; this._persist(); this._emit('updated', { servers: this.servers }); }
71
+ if (select) await this.switchTo(serverId);
72
72
  }
73
73
 
74
74
  async delete(serverId) {
package/src/voice.js CHANGED
@@ -41,13 +41,19 @@ const deriveRoomId = async (serverId, channel) => {
41
41
  return 'zellous' + Array.from(new Uint8Array(h)).map(b => b.toString(16).padStart(2, '0')).join('').slice(0, 16);
42
42
  };
43
43
 
44
+ // See data.js's defaultCreatePeerConnection for why this hook exists: it lets
45
+ // a Node host inject a natively-tuned peer (ICE/UDP muxing, fixed port range,
46
+ // proxy passthrough) without wireweave depending on any Node WebRTC binding.
47
+ const defaultCreatePeerConnection = (config) => new RTCPeerConnection(config);
48
+
44
49
  export class VoiceSession extends EventTarget {
45
- constructor({ fsm, xstate, relayPool, auth, mediaDevices, bans = null, serverId = '', onAudioTrack = null, onVideoTrack = null }) {
50
+ constructor({ fsm, xstate, relayPool, auth, mediaDevices, bans = null, serverId = '', onAudioTrack = null, onVideoTrack = null, createPeerConnection = defaultCreatePeerConnection }) {
46
51
  super();
47
52
  if (!fsm || !xstate || !relayPool || !auth || !mediaDevices) throw new Error('VoiceSession: missing deps');
48
53
  this.fsm = fsm; this.xstate = xstate; this.pool = relayPool; this.auth = auth; this.md = mediaDevices; this.bans = bans;
49
54
  this.serverId = serverId;
50
55
  this.onAudioTrack = onAudioTrack; this.onVideoTrack = onVideoTrack;
56
+ this.createPeerConnection = createPeerConnection;
51
57
  this.actor = null;
52
58
  this.channelName = ''; this.roomId = '';
53
59
  this.peers = new Map(); this.participants = new Map();
@@ -520,7 +526,7 @@ export class VoiceSession extends EventTarget {
520
526
  fsmActor.start();
521
527
  const peer = { pc: null, audioEl: null, pendingCandidates: [], bufferedCandidates: [], iceTimer: null, disconnectTimer: null, failCount: 0, state: 'new', fsm: fsmActor, _stallInterval: null, remoteDescSet: false, trackEndedRestart: false };
522
528
  this.peers.set(peerPubkey, peer);
523
- const pc = new RTCPeerConnection({ iceServers: ICE_SERVERS, bundlePolicy: 'max-bundle', iceCandidatePoolSize: 4, iceTransportPolicy: 'all' });
529
+ const pc = this.createPeerConnection({ iceServers: ICE_SERVERS, bundlePolicy: 'max-bundle', iceCandidatePoolSize: 4, iceTransportPolicy: 'all' });
524
530
  peer.pc = pc;
525
531
  const isOfferer = this.auth.pubkey > peerPubkey;
526
532
  if (isOfferer) {