node-datachannel 0.10.1 → 0.11.0

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.
Files changed (38) hide show
  1. package/.github/workflows/build-win.yml +3 -3
  2. package/.gitmodules +3 -0
  3. package/CMakeLists.txt +1 -1
  4. package/README.md +18 -4
  5. package/examples/client-server/client-benchmark.js +7 -9
  6. package/examples/client-server/client-periodic.js +7 -9
  7. package/examples/client-server/client.js +7 -9
  8. package/examples/client-server/package-lock.json +0 -27
  9. package/examples/client-server/package.json +0 -1
  10. package/examples/client-server/signaling-server.js +21 -10
  11. package/examples/electron-demo/README.md +1 -1
  12. package/examples/websocket/websocket-client.js +20 -0
  13. package/examples/websocket/websocket-server.js +22 -0
  14. package/lib/index.cjs +52 -9
  15. package/lib/index.js +22 -15
  16. package/lib/node-datachannel.js +7 -0
  17. package/lib/websocket-server.js +34 -0
  18. package/package.json +4 -4
  19. package/polyfill/Events.js +5 -3
  20. package/polyfill/Exception.js +23 -0
  21. package/polyfill/README.md +4 -0
  22. package/polyfill/RTCDataChannel.js +19 -11
  23. package/polyfill/RTCPeerConnection.js +148 -41
  24. package/polyfill/index.cjs +252 -59
  25. package/src/peer-connection-wrapper.cpp +11 -3
  26. package/src/web-socket-server-wrapper.cpp +23 -23
  27. package/src/web-socket-server-wrapper.h +8 -6
  28. package/src/web-socket-wrapper.cpp +7 -1
  29. package/test/connectivity.js +1 -23
  30. package/test/wpt-tests/README.md +46 -0
  31. package/test/wpt-tests/chrome-failed-tests.js +42 -0
  32. package/test/wpt-tests/index.js +96 -0
  33. package/test/wpt-tests/last-test-results.md +205 -0
  34. package/test/wpt-tests/package-lock.json +1712 -0
  35. package/test/wpt-tests/package.json +19 -0
  36. package/test/wpt-tests/wpt-test-list.js +137 -0
  37. package/test/wpt-tests/wpt.js +131 -0
  38. package/test/wpt.js +0 -50
@@ -17,12 +17,12 @@ jobs:
17
17
  node-version: [16.x]
18
18
 
19
19
  steps:
20
- - uses: actions/checkout@v2
20
+ - uses: actions/checkout@v4
21
21
  - uses: ilammy/msvc-dev-cmd@v1
22
22
  - name: Install OpenSSL
23
23
  run: choco install openssl
24
24
  - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v1
25
+ uses: actions/setup-node@v4
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
  - name: Build
@@ -46,7 +46,7 @@ jobs:
46
46
  node_arch:
47
47
  - x86
48
48
  steps:
49
- - uses: actions/checkout@v2
49
+ - uses: actions/checkout@v4
50
50
  - uses: ilammy/msvc-dev-cmd@v1
51
51
  with:
52
52
  arch: x86
package/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "test/wpt-tests/wpt"]
2
+ path = test/wpt-tests/wpt
3
+ url = https://github.com/web-platform-tests/wpt.git
package/CMakeLists.txt CHANGED
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
2
2
  cmake_policy(SET CMP0091 NEW)
3
3
  cmake_policy(SET CMP0042 NEW)
4
4
 
5
- project(node_datachannel VERSION 0.10.1)
5
+ project(node_datachannel VERSION 0.11.0)
6
6
 
7
7
  # -Dnapi_build_version=8
8
8
  add_definitions(-DNAPI_VERSION=8)
package/README.md CHANGED
@@ -1,11 +1,12 @@
1
- # WebRTC For Node.js and Electron
1
+ # WebRTC For Node.js and Electron ( with WebSocket)
2
2
 
3
3
  ![Linux CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Linux/badge.svg) ![Windows CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Win/badge.svg) ![Mac x64 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20x64/badge.svg) ![Mac M1 CI Build](https://github.com/murat-dogan/node-datachannel/workflows/Build%20-%20Mac%20M1/badge.svg)
4
4
 
5
5
  - Lightweight
6
6
  - No need to deal with WebRTC stack!
7
- - Small binary sizes (~8MB for Linux X64)
7
+ - Small binary sizes (~8MB for Linux x64)
8
8
  - Type infos for Typescript
9
+ - Integrated WebSocket Client & Server Implementation
9
10
 
10
11
  This project is Node.js bindings for [libdatachannel](https://github.com/paullouisageneau/libdatachannel) library.
11
12
 
@@ -35,7 +36,17 @@ Please check [electron demo](/examples/electron-demo)
35
36
 
36
37
  WebRTC polyfills to be used for libraries like `simple-peer`.
37
38
 
38
- Please check [here](/polyfill)
39
+ Please check [here for more](/polyfill)
40
+
41
+ ### web-platform-tests
42
+
43
+ Please check actual situation [here](/test/wpt-tests/)
44
+
45
+ ## WebSocket Client & Server
46
+
47
+ Integrated WebSocket Client & Server is available, which can be used separately or for signaling.
48
+
49
+ For an example usage, [check here](/examples/websocket)
39
50
 
40
51
  ## Example Usage
41
52
 
@@ -45,6 +56,9 @@ import nodeDataChannel from 'node-datachannel';
45
56
  // Log Level
46
57
  nodeDataChannel.initLogger('Debug');
47
58
 
59
+ // Integrated WebSocket available and can be used for signaling etc
60
+ // const ws = new nodeDataChannel.WebSocket();
61
+
48
62
  let dc1 = null;
49
63
  let dc2 = null;
50
64
 
@@ -105,7 +119,7 @@ Please check [docs](/API.md) page
105
119
 
106
120
  ## Contributing
107
121
 
108
- Contributions welcome!
122
+ Contributions are welcome!
109
123
 
110
124
  ## Thanks
111
125
 
@@ -6,7 +6,6 @@ const require = createRequire(import.meta.url);
6
6
  const yargs = require('yargs/yargs');
7
7
  const { hideBin } = require('yargs/helpers');
8
8
 
9
- import WebSocket from 'ws';
10
9
  import readline from 'readline';
11
10
  import nodeDataChannel from '../../lib/index.js';
12
11
 
@@ -60,23 +59,22 @@ const rl = readline.createInterface({
60
59
  output: process.stdout,
61
60
  });
62
61
 
63
- const ws = new WebSocket(wsUrl + '/' + id, {
64
- perMessageDeflate: false,
65
- });
62
+ const ws = new nodeDataChannel.WebSocket();
63
+ ws.open(wsUrl + '/' + id);
66
64
 
67
65
  console.log(`The local ID is: ${id}`);
68
66
  console.log(`Waiting for signaling to be connected...`);
69
67
 
70
- ws.on('open', () => {
68
+ ws.onOpen(() => {
71
69
  console.log('WebSocket connected, signaling ready');
72
70
  readUserInput();
73
71
  });
74
72
 
75
- ws.on('error', (err) => {
73
+ ws.onError((err) => {
76
74
  console.log('WebSocket Error: ', err);
77
75
  });
78
76
 
79
- ws.on('message', (msgStr) => {
77
+ ws.onMessage((msgStr) => {
80
78
  let msg = JSON.parse(msgStr);
81
79
  switch (msg.type) {
82
80
  case 'offer':
@@ -184,10 +182,10 @@ function createPeerConnection(peerId) {
184
182
  console.log('GatheringState: ', state);
185
183
  });
186
184
  peerConnection.onLocalDescription((description, type) => {
187
- ws.send(JSON.stringify({ id: peerId, type, description }));
185
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
188
186
  });
189
187
  peerConnection.onLocalCandidate((candidate, mid) => {
190
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
188
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
191
189
  });
192
190
  peerConnection.onDataChannel((dc) => {
193
191
  rl.close();
@@ -1,4 +1,3 @@
1
- import WebSocket from 'ws';
2
1
  import readline from 'readline';
3
2
  import nodeDataChannel from '../../lib/index.js';
4
3
 
@@ -25,23 +24,22 @@ const rl = readline.createInterface({
25
24
 
26
25
  // Signaling Server
27
26
  const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
28
- const ws = new WebSocket(WS_URL + '/' + id, {
29
- perMessageDeflate: false,
30
- });
27
+ const ws = new nodeDataChannel.WebSocket();
28
+ ws.open(WS_URL + '/' + id);
31
29
 
32
30
  console.log(`The local ID is: ${id}`);
33
31
  console.log(`Waiting for signaling to be connected...`);
34
32
 
35
- ws.on('open', () => {
33
+ ws.onOpen(() => {
36
34
  console.log('WebSocket connected, signaling ready');
37
35
  readUserInput();
38
36
  });
39
37
 
40
- ws.on('error', (err) => {
38
+ ws.onError((err) => {
41
39
  console.log('WebSocket Error: ', err);
42
40
  });
43
41
 
44
- ws.on('message', (msgStr) => {
42
+ ws.onMessage((msgStr) => {
45
43
  let msg = JSON.parse(msgStr);
46
44
  switch (msg.type) {
47
45
  case 'offer':
@@ -122,10 +120,10 @@ function createPeerConnection(peerId) {
122
120
  console.log('GatheringState: ', state);
123
121
  });
124
122
  peerConnection.onLocalDescription((description, type) => {
125
- ws.send(JSON.stringify({ id: peerId, type, description }));
123
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
126
124
  });
127
125
  peerConnection.onLocalCandidate((candidate, mid) => {
128
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
126
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
129
127
  });
130
128
  peerConnection.onDataChannel((dc) => {
131
129
  rl.close();
@@ -1,4 +1,3 @@
1
- import WebSocket from 'ws';
2
1
  import readline from 'readline';
3
2
  import nodeDataChannel from '../../lib/index.js';
4
3
 
@@ -13,23 +12,22 @@ const id = randomId(4);
13
12
 
14
13
  // Signaling Server
15
14
  const WS_URL = process.env.WS_URL || 'ws://localhost:8000';
16
- const ws = new WebSocket(WS_URL + '/' + id, {
17
- perMessageDeflate: false,
18
- });
15
+ const ws = new nodeDataChannel.WebSocket();
16
+ ws.open(WS_URL + '/' + id);
19
17
 
20
18
  console.log(`The local ID is: ${id}`);
21
19
  console.log(`Waiting for signaling to be connected...`);
22
20
 
23
- ws.on('open', () => {
21
+ ws.onOpen(() => {
24
22
  console.log('WebSocket connected, signaling ready');
25
23
  readUserInput();
26
24
  });
27
25
 
28
- ws.on('error', (err) => {
26
+ ws.onError((err) => {
29
27
  console.log('WebSocket Error: ', err);
30
28
  });
31
29
 
32
- ws.on('message', (msgStr) => {
30
+ ws.onMessage((msgStr) => {
33
31
  let msg = JSON.parse(msgStr);
34
32
  switch (msg.type) {
35
33
  case 'offer':
@@ -86,10 +84,10 @@ function createPeerConnection(peerId) {
86
84
  console.log('GatheringState: ', state);
87
85
  });
88
86
  peerConnection.onLocalDescription((description, type) => {
89
- ws.send(JSON.stringify({ id: peerId, type, description }));
87
+ ws.sendMessage(JSON.stringify({ id: peerId, type, description }));
90
88
  });
91
89
  peerConnection.onLocalCandidate((candidate, mid) => {
92
- ws.send(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
90
+ ws.sendMessage(JSON.stringify({ id: peerId, type: 'candidate', candidate, mid }));
93
91
  });
94
92
  peerConnection.onDataChannel((dc) => {
95
93
  console.log('DataChannel from ' + peerId + ' received with label "', dc.getLabel() + '"');
@@ -9,7 +9,6 @@
9
9
  "version": "1.0.0",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "ws": "^7.5.3",
13
12
  "yargs": "^16.2.0"
14
13
  }
15
14
  },
@@ -138,26 +137,6 @@
138
137
  "url": "https://github.com/chalk/wrap-ansi?sponsor=1"
139
138
  }
140
139
  },
141
- "node_modules/ws": {
142
- "version": "7.5.10",
143
- "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
144
- "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
145
- "engines": {
146
- "node": ">=8.3.0"
147
- },
148
- "peerDependencies": {
149
- "bufferutil": "^4.0.1",
150
- "utf-8-validate": "^5.0.2"
151
- },
152
- "peerDependenciesMeta": {
153
- "bufferutil": {
154
- "optional": true
155
- },
156
- "utf-8-validate": {
157
- "optional": true
158
- }
159
- }
160
- },
161
140
  "node_modules/y18n": {
162
141
  "version": "5.0.8",
163
142
  "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
@@ -282,12 +261,6 @@
282
261
  "strip-ansi": "^6.0.0"
283
262
  }
284
263
  },
285
- "ws": {
286
- "version": "7.5.10",
287
- "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
288
- "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
289
- "requires": {}
290
- },
291
264
  "y18n": {
292
265
  "version": "5.0.8",
293
266
  "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
@@ -11,7 +11,6 @@
11
11
  "author": "",
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
- "ws": "^7.5.3",
15
14
  "yargs": "^16.2.0"
16
15
  }
17
16
  }
@@ -1,15 +1,22 @@
1
- import WebSocket from 'ws';
1
+ import nodeDataChannel from '../../lib/index.js';
2
+
3
+ // Init Logger
4
+ nodeDataChannel.initLogger('Debug');
2
5
 
3
6
  const clients = {};
4
7
 
5
- const wss = new WebSocket.Server({ port: 8000 });
8
+ const wsServer = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 8000 });
9
+
10
+ wsServer.onClient((ws) => {
11
+ let id = '';
6
12
 
7
- wss.on('connection', (ws, req) => {
8
- const id = req.url.replace('/', '');
9
- console.log(`New Connection from ${id}`);
13
+ ws.onOpen(() => {
14
+ id = ws.path().replace('/', '');
15
+ console.log(`New Connection from ${id}`);
16
+ clients[id] = ws;
17
+ });
10
18
 
11
- clients[id] = ws;
12
- ws.on('message', (buffer) => {
19
+ ws.onMessage((buffer) => {
13
20
  let msg = JSON.parse(buffer);
14
21
  let peerId = msg.id;
15
22
  let peerWs = clients[peerId];
@@ -18,11 +25,15 @@ wss.on('connection', (ws, req) => {
18
25
  if (!peerWs) return console.error(`Can not find peer with ID ${peerId}`);
19
26
 
20
27
  msg.id = id;
21
- peerWs.send(JSON.stringify(msg));
28
+ peerWs.sendMessage(JSON.stringify(msg));
22
29
  });
23
30
 
24
- ws.on('close', () => {
25
- console.log(`${id} disconected`);
31
+ ws.onClosed(() => {
32
+ console.log(`${id} disconnected`);
26
33
  delete clients[id];
27
34
  });
35
+
36
+ ws.onError((err) => {
37
+ console.error(err);
38
+ });
28
39
  });
@@ -6,7 +6,7 @@ Project created with
6
6
 
7
7
  ## Electron Compatibility and Rebuilding
8
8
 
9
- `node-datachannel` uses N-API for creating binaries. Normally `electron` is compatible with N-API binarıes, so you should not need to rebuild the binaries.
9
+ `node-datachannel` uses N-API for creating binaries. Normally `electron` is compatible with N-API binaries, so you should not need to rebuild the binaries.
10
10
 
11
11
  ### If you need anyway, you can use `electron-rebuild` package with some modifications.
12
12
 
@@ -0,0 +1,20 @@
1
+ import * as nodeDataChannel from '../../lib/index.js';
2
+
3
+ const clientSocket = new nodeDataChannel.WebSocket();
4
+
5
+ clientSocket.open('ws://127.0.0.1:3000');
6
+
7
+ clientSocket.onOpen(() => {
8
+ console.log('Client socket opened');
9
+ clientSocket.sendMessage('Echo this!');
10
+ });
11
+
12
+ clientSocket.onMessage((message) => {
13
+ console.log('Message from server: ' + message);
14
+ });
15
+
16
+ clientSocket.onClosed(() => {
17
+ console.log('Client socket closed');
18
+ });
19
+
20
+ // clientSocket.close();
@@ -0,0 +1,22 @@
1
+ import * as nodeDataChannel from '../../lib/index.js';
2
+
3
+ const ws = new nodeDataChannel.WebSocketServer({ bindAddress: '127.0.0.1', port: 3000 });
4
+
5
+ ws.onClient((clientSocket) => {
6
+ clientSocket.onOpen(() => {
7
+ console.log('Client socket opened');
8
+ clientSocket.sendMessage('Hello from server');
9
+ });
10
+
11
+ clientSocket.onMessage((message) => {
12
+ console.log('Message from client: ' + message);
13
+ clientSocket.sendMessage(message);
14
+ });
15
+
16
+ clientSocket.onClosed(() => {
17
+ console.log('Client socket closed');
18
+ });
19
+ });
20
+
21
+ // When done, close the server
22
+ // ws.stop();
package/lib/index.cjs CHANGED
@@ -4,8 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var module$1 = require('module');
6
6
  var stream = require('stream');
7
+ var events = require('events');
7
8
 
8
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
+ // createRequire is native in node version >= 12
11
+ const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
12
+
13
+ const nodeDataChannel = require$1('../build/Release/node_datachannel.node');
14
+
9
15
  /**
10
16
  * Turns a node-datachannel DataChannel into a real Node.js stream, complete with buffering,
11
17
  * backpressure (up to a point - if the buffer fills up, messages are dropped), and
@@ -101,10 +107,37 @@ class DataChannelStream extends stream.Duplex {
101
107
  }
102
108
  }
103
109
 
104
- // createRequire is native in node version >= 12
105
- const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
110
+ class WebSocketServer extends events.EventEmitter {
111
+ #server;
112
+ #clients = [];
106
113
 
107
- const nodeDataChannel = require$1('../build/Release/node_datachannel.node');
114
+ constructor(options) {
115
+ super();
116
+ this.#server = new nodeDataChannel.WebSocketServer(options);
117
+
118
+ this.#server.onClient((client) => {
119
+ this.emit('client', client);
120
+ this.#clients.push(client);
121
+ });
122
+ }
123
+
124
+ port() {
125
+ return this.#server?.port() || 0;
126
+ }
127
+
128
+ stop() {
129
+ this.#clients.forEach((client) => {
130
+ client?.close();
131
+ });
132
+ this.#server?.stop();
133
+ this.#server = null;
134
+ this.removeAllListeners();
135
+ }
136
+
137
+ onClient(cb) {
138
+ if (this.#server) this.on('client', cb);
139
+ }
140
+ }
108
141
 
109
142
  const {
110
143
  initLogger,
@@ -118,14 +151,8 @@ const {
118
151
  DataChannel,
119
152
  PeerConnection,
120
153
  WebSocket,
121
- WebSocketServer,
122
154
  } = nodeDataChannel;
123
155
 
124
- var index = {
125
- ...nodeDataChannel,
126
- DataChannelStream,
127
- };
128
-
129
156
  const DescriptionType = {
130
157
  Unspec: 'unspec',
131
158
  Offer: 'offer',
@@ -134,6 +161,22 @@ const DescriptionType = {
134
161
  Rollback: 'rollback',
135
162
  };
136
163
 
164
+ var index = {
165
+ initLogger,
166
+ cleanup,
167
+ preload,
168
+ setSctpSettings,
169
+ RtcpReceivingSession,
170
+ Track,
171
+ Video,
172
+ Audio,
173
+ DataChannel,
174
+ PeerConnection,
175
+ WebSocket,
176
+ WebSocketServer,
177
+ DataChannelStream,
178
+ };
179
+
137
180
  exports.Audio = Audio;
138
181
  exports.DataChannel = DataChannel;
139
182
  exports.DataChannelStream = DataChannelStream;
package/lib/index.js CHANGED
@@ -1,9 +1,6 @@
1
- // createRequire is native in node version >= 12
2
- import { createRequire } from 'module';
3
- const require = createRequire(import.meta.url);
4
-
5
- const nodeDataChannel = require('../build/Release/node_datachannel.node');
1
+ import nodeDataChannel from './node-datachannel.js';
6
2
  import DataChannelStream from './datachannel-stream.js';
3
+ import WebSocketServer from './websocket-server.js';
7
4
 
8
5
  const {
9
6
  initLogger,
@@ -17,9 +14,16 @@ const {
17
14
  DataChannel,
18
15
  PeerConnection,
19
16
  WebSocket,
20
- WebSocketServer,
21
17
  } = nodeDataChannel;
22
18
 
19
+ export const DescriptionType = {
20
+ Unspec: 'unspec',
21
+ Offer: 'offer',
22
+ Answer: 'answer',
23
+ Pranswer: 'pranswer',
24
+ Rollback: 'rollback',
25
+ };
26
+
23
27
  export {
24
28
  initLogger,
25
29
  cleanup,
@@ -38,14 +42,17 @@ export {
38
42
  };
39
43
 
40
44
  export default {
41
- ...nodeDataChannel,
45
+ initLogger,
46
+ cleanup,
47
+ preload,
48
+ setSctpSettings,
49
+ RtcpReceivingSession,
50
+ Track,
51
+ Video,
52
+ Audio,
53
+ DataChannel,
54
+ PeerConnection,
55
+ WebSocket,
56
+ WebSocketServer,
42
57
  DataChannelStream,
43
58
  };
44
-
45
- export const DescriptionType = {
46
- Unspec: 'unspec',
47
- Offer: 'offer',
48
- Answer: 'answer',
49
- Pranswer: 'pranswer',
50
- Rollback: 'rollback',
51
- };
@@ -0,0 +1,7 @@
1
+ // createRequire is native in node version >= 12
2
+ import { createRequire } from 'module';
3
+ const require = createRequire(import.meta.url);
4
+
5
+ const nodeDataChannel = require('../build/Release/node_datachannel.node');
6
+
7
+ export default nodeDataChannel;
@@ -0,0 +1,34 @@
1
+ import { EventEmitter } from 'events';
2
+ import nodeDataChannel from './node-datachannel.js';
3
+
4
+ export default class WebSocketServer extends EventEmitter {
5
+ #server;
6
+ #clients = [];
7
+
8
+ constructor(options) {
9
+ super();
10
+ this.#server = new nodeDataChannel.WebSocketServer(options);
11
+
12
+ this.#server.onClient((client) => {
13
+ this.emit('client', client);
14
+ this.#clients.push(client);
15
+ });
16
+ }
17
+
18
+ port() {
19
+ return this.#server?.port() || 0;
20
+ }
21
+
22
+ stop() {
23
+ this.#clients.forEach((client) => {
24
+ client?.close();
25
+ });
26
+ this.#server?.stop();
27
+ this.#server = null;
28
+ this.removeAllListeners();
29
+ }
30
+
31
+ onClient(cb) {
32
+ if (this.#server) this.on('client', cb);
33
+ }
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-datachannel",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "description": "libdatachannel node bindings",
5
5
  "type": "module",
6
6
  "types": "./lib/index.d.ts",
@@ -93,11 +93,11 @@
93
93
  "node-addon-api": "^7.0.0",
94
94
  "prebuild": "^12.0.0",
95
95
  "prettier": "^2.8.8",
96
- "typescript": "^5.3.2",
97
- "rollup": "^4.14.1"
96
+ "rollup": "^4.14.1",
97
+ "typescript": "^5.3.2"
98
98
  },
99
99
  "dependencies": {
100
100
  "node-domexception": "^2.0.1",
101
101
  "prebuild-install": "^7.0.1"
102
102
  }
103
- }
103
+ }
@@ -15,10 +15,12 @@ export class RTCPeerConnectionIceEvent extends Event {
15
15
  export class RTCDataChannelEvent extends Event {
16
16
  #channel;
17
17
 
18
- constructor(channel) {
19
- super('datachannel');
18
+ constructor(type, eventInitDict) {
19
+ super(type);
20
20
 
21
- this.#channel = channel;
21
+ if (type && !eventInitDict.channel) throw new TypeError('channel member is required');
22
+
23
+ this.#channel = eventInitDict?.channel;
22
24
  }
23
25
 
24
26
  get channel() {
@@ -0,0 +1,23 @@
1
+ export const InvalidStateError = (msg) => {
2
+ return new DOMException(msg, 'InvalidStateError');
3
+ };
4
+
5
+ export const InvalidAccessError = (msg) => {
6
+ return new DOMException(msg, 'InvalidAccessError');
7
+ };
8
+
9
+ export const NotFoundError = (msg) => {
10
+ return new DOMException(msg, 'NotFoundError');
11
+ };
12
+
13
+ export const OperationError = (msg) => {
14
+ return new DOMException(msg, 'OperationError');
15
+ };
16
+
17
+ export const TypeError = (msg) => {
18
+ return new DOMException(msg, 'TypeError');
19
+ };
20
+
21
+ export const SyntaxError = (msg) => {
22
+ return new DOMException(msg, 'SyntaxError');
23
+ };
@@ -2,6 +2,10 @@
2
2
 
3
3
  WebRTC polyfills to be used for libraries like `simple-peer`.
4
4
 
5
+ # web-platform-tests
6
+
7
+ Please check actual situation [here](/test/wpt-tests/)
8
+
5
9
  ## Example Usage
6
10
 
7
11
  > For a native usage example please check test folder.