node-datachannel 0.26.0 → 0.28.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 (103) hide show
  1. package/.eslintrc.json +25 -25
  2. package/.prettierignore +4 -0
  3. package/.prettierrc +1 -0
  4. package/API.md +4 -4
  5. package/BULDING.md +11 -2
  6. package/CMakeLists.txt +37 -13
  7. package/README.md +22 -18
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs/lib/datachannel-stream.cjs +13 -13
  10. package/dist/cjs/lib/datachannel-stream.cjs.map +1 -1
  11. package/dist/cjs/lib/index.cjs +1 -3
  12. package/dist/cjs/lib/index.cjs.map +1 -1
  13. package/dist/cjs/lib/websocket-server.cjs.map +1 -1
  14. package/dist/cjs/lib/websocket.cjs.map +1 -1
  15. package/dist/cjs/polyfill/Events.cjs +52 -3
  16. package/dist/cjs/polyfill/Events.cjs.map +1 -1
  17. package/dist/cjs/polyfill/Exception.cjs.map +1 -1
  18. package/dist/cjs/polyfill/RTCCertificate.cjs +5 -2
  19. package/dist/cjs/polyfill/RTCCertificate.cjs.map +1 -1
  20. package/dist/cjs/polyfill/RTCDataChannel.cjs +27 -19
  21. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
  22. package/dist/cjs/polyfill/RTCDtlsTransport.cjs +6 -5
  23. package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -1
  24. package/dist/cjs/polyfill/RTCError.cjs +9 -2
  25. package/dist/cjs/polyfill/RTCError.cjs.map +1 -1
  26. package/dist/cjs/polyfill/RTCIceCandidate.cjs +13 -8
  27. package/dist/cjs/polyfill/RTCIceCandidate.cjs.map +1 -1
  28. package/dist/cjs/polyfill/RTCIceTransport.cjs +26 -16
  29. package/dist/cjs/polyfill/RTCIceTransport.cjs.map +1 -1
  30. package/dist/cjs/polyfill/RTCPeerConnection.cjs +80 -70
  31. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
  32. package/dist/cjs/polyfill/RTCSctpTransport.cjs +9 -11
  33. package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -1
  34. package/dist/cjs/polyfill/RTCSessionDescription.cjs +10 -2
  35. package/dist/cjs/polyfill/RTCSessionDescription.cjs.map +1 -1
  36. package/dist/cjs/polyfill/index.cjs.map +1 -1
  37. package/dist/esm/index.mjs.map +1 -1
  38. package/dist/esm/lib/datachannel-stream.mjs.map +1 -1
  39. package/dist/esm/lib/index.mjs +1 -3
  40. package/dist/esm/lib/index.mjs.map +1 -1
  41. package/dist/esm/lib/websocket-server.mjs.map +1 -1
  42. package/dist/esm/lib/websocket.mjs.map +1 -1
  43. package/dist/esm/polyfill/Events.mjs +52 -4
  44. package/dist/esm/polyfill/Events.mjs.map +1 -1
  45. package/dist/esm/polyfill/Exception.mjs.map +1 -1
  46. package/dist/esm/polyfill/RTCCertificate.mjs +5 -2
  47. package/dist/esm/polyfill/RTCCertificate.mjs.map +1 -1
  48. package/dist/esm/polyfill/RTCDataChannel.mjs +27 -19
  49. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
  50. package/dist/esm/polyfill/RTCDtlsTransport.mjs +6 -5
  51. package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -1
  52. package/dist/esm/polyfill/RTCError.mjs +9 -2
  53. package/dist/esm/polyfill/RTCError.mjs.map +1 -1
  54. package/dist/esm/polyfill/RTCIceCandidate.mjs +13 -8
  55. package/dist/esm/polyfill/RTCIceCandidate.mjs.map +1 -1
  56. package/dist/esm/polyfill/RTCIceTransport.mjs +26 -17
  57. package/dist/esm/polyfill/RTCIceTransport.mjs.map +1 -1
  58. package/dist/esm/polyfill/RTCPeerConnection.mjs +80 -70
  59. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
  60. package/dist/esm/polyfill/RTCSctpTransport.mjs +9 -11
  61. package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -1
  62. package/dist/esm/polyfill/RTCSessionDescription.mjs +10 -2
  63. package/dist/esm/polyfill/RTCSessionDescription.mjs.map +1 -1
  64. package/dist/esm/polyfill/index.mjs.map +1 -1
  65. package/dist/types/lib/index.d.ts +5 -2
  66. package/dist/types/lib/types.d.ts +6 -6
  67. package/dist/types/polyfill/Events.d.ts +3 -3
  68. package/dist/types/polyfill/RTCCertificate.d.ts +1 -0
  69. package/dist/types/polyfill/RTCDataChannel.d.ts +6 -6
  70. package/dist/types/polyfill/RTCDtlsTransport.d.ts +4 -6
  71. package/dist/types/polyfill/RTCError.d.ts +1 -0
  72. package/dist/types/polyfill/RTCIceCandidate.d.ts +1 -1
  73. package/dist/types/polyfill/RTCIceTransport.d.ts +16 -10
  74. package/dist/types/polyfill/RTCPeerConnection.d.ts +29 -22
  75. package/dist/types/polyfill/RTCSctpTransport.d.ts +2 -4
  76. package/dist/types/polyfill/RTCSessionDescription.d.ts +1 -0
  77. package/jest.config.ts +8 -8
  78. package/package.json +114 -112
  79. package/rollup.config.mjs +1 -1
  80. package/src/cpp/data-channel-wrapper.cpp +11 -0
  81. package/src/cpp/main.cpp +14 -0
  82. package/src/cpp/peer-connection-wrapper.cpp +9 -1
  83. package/src/cpp/peer-connection-wrapper.h +5 -1
  84. package/src/cpp/rtc-wrapper.cpp +18 -0
  85. package/src/index.ts +0 -1
  86. package/src/lib/datachannel-stream.ts +83 -84
  87. package/src/lib/index.ts +165 -139
  88. package/src/lib/types.ts +97 -72
  89. package/src/lib/websocket-server.ts +25 -25
  90. package/src/lib/websocket.ts +19 -19
  91. package/src/polyfill/Events.ts +65 -19
  92. package/src/polyfill/Exception.ts +24 -24
  93. package/src/polyfill/README.md +8 -8
  94. package/src/polyfill/RTCCertificate.ts +16 -12
  95. package/src/polyfill/RTCDataChannel.ts +198 -192
  96. package/src/polyfill/RTCDtlsTransport.ts +38 -39
  97. package/src/polyfill/RTCError.ts +63 -56
  98. package/src/polyfill/RTCIceCandidate.ts +120 -116
  99. package/src/polyfill/RTCIceTransport.ts +71 -64
  100. package/src/polyfill/RTCPeerConnection.ts +436 -420
  101. package/src/polyfill/RTCSctpTransport.ts +43 -47
  102. package/src/polyfill/RTCSessionDescription.ts +26 -17
  103. package/src/polyfill/index.ts +22 -22
package/package.json CHANGED
@@ -1,117 +1,119 @@
1
1
  {
2
- "name": "node-datachannel",
3
- "version": "0.26.0",
4
- "description": "WebRTC For Node.js and Electron. libdatachannel node bindings.",
5
- "main": "./dist/cjs/lib/index.cjs",
6
- "module": "./dist/esm/lib/index.mjs",
7
- "types": "./dist/types/lib/index.d.ts",
8
- "typesVersions": {
9
- "*": {
10
- "*": [
11
- "dist/types/lib/index.d.ts"
12
- ],
13
- "polyfill": [
14
- "dist/types/polyfill/index.d.ts"
15
- ]
16
- }
17
- },
18
- "exports": {
19
- ".": {
20
- "types": "./dist/types/lib/index.d.ts",
21
- "require": "./dist/cjs/lib/index.cjs",
22
- "import": "./dist/esm/lib/index.mjs",
23
- "default": "./dist/lib/esm/index.mjs"
24
- },
25
- "./polyfill": {
26
- "types": "./dist/types/polyfill/index.d.ts",
27
- "require": "./dist/cjs/polyfill/index.cjs",
28
- "import": "./dist/esm/polyfill/index.mjs",
29
- "default": "./dist/polyfill/esm/index.mjs"
30
- }
31
- },
32
- "engines": {
33
- "node": ">=18.20.0"
34
- },
35
- "scripts": {
36
- "install": "prebuild-install -r napi || (npm install --ignore-scripts --production=false && npm run _prebuild)",
37
- "install:nice": "npm run clean && npm install --ignore-scripts --production=false && cmake-js build --CDUSE_NICE=1",
38
- "install:gnu": "npm run clean && npm install --ignore-scripts --production=false && cmake-js build --CDUSE_GNUTLS=1",
39
- "build": "npm run compile && npm run build:tsc",
40
- "compile": "cmake-js build",
41
- "compile:debug": "cmake-js build -D",
42
- "build:tsc": "rimraf dist && rollup -c",
43
- "build:tsc:watch": "rollup -c -w",
44
- "clean": "rimraf dist build",
45
- "lint": "eslint . --ext .ts --ext .mts",
46
- "test": "NODE_OPTIONS=--experimental-vm-modules jest",
47
- "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch",
48
- "test:wpt": "npm run run:wpt:server & (sleep 8 && (npm run run:wpt:test | tee test/wpt-tests/last-test-results.md) )",
49
- "wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
50
- "wpt:test": "ts-node test/wpt-tests/index.ts",
51
- "_prebuild": "prebuild -r napi --backend cmake-js",
52
- "prepack": "npm run build:tsc"
53
- },
54
- "binary": {
55
- "napi_versions": [
56
- 8
57
- ]
58
- },
59
- "repository": {
60
- "type": "git",
61
- "url": "git+https://github.com/murat-dogan/node-datachannel.git"
62
- },
63
- "keywords": [
64
- "libdatachannel",
65
- "webrtc",
66
- "p2p",
67
- "peer-to-peer",
68
- "datachannel",
69
- "data channel",
70
- "websocket"
71
- ],
72
- "contributors": [
73
- {
74
- "name": "Murat Doğan",
75
- "url": "https://github.com/murat-dogan"
76
- },
77
- {
78
- "name": "Paul-Louis Ageneau",
79
- "url": "https://github.com/paullouisageneau"
80
- }
81
- ],
82
- "license": "MPL 2.0",
83
- "bugs": {
84
- "url": "https://github.com/murat-dogan/node-datachannel/issues"
2
+ "name": "node-datachannel",
3
+ "version": "0.28.0",
4
+ "description": "WebRTC For Node.js and Electron. libdatachannel node bindings.",
5
+ "main": "./dist/cjs/lib/index.cjs",
6
+ "module": "./dist/esm/lib/index.mjs",
7
+ "types": "./dist/types/lib/index.d.ts",
8
+ "typesVersions": {
9
+ "*": {
10
+ "*": [
11
+ "dist/types/lib/index.d.ts"
12
+ ],
13
+ "polyfill": [
14
+ "dist/types/polyfill/index.d.ts"
15
+ ]
16
+ }
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/types/lib/index.d.ts",
21
+ "require": "./dist/cjs/lib/index.cjs",
22
+ "import": "./dist/esm/lib/index.mjs",
23
+ "default": "./dist/lib/esm/index.mjs"
85
24
  },
86
- "homepage": "https://github.com/murat-dogan/node-datachannel#readme",
87
- "devDependencies": {
88
- "@rollup/plugin-esm-shim": "^0.1.7",
89
- "@rollup/plugin-replace": "^6.0.1",
90
- "@types/jest": "^29.5.12",
91
- "@types/node": "^20.6.1",
92
- "@typescript-eslint/eslint-plugin": "^7.17.0",
93
- "@typescript-eslint/parser": "^7.17.0",
94
- "cmake-js": "^7.3.0",
95
- "eslint": "^8.57.0",
96
- "eslint-config-prettier": "^9.1.0",
97
- "eslint-plugin-jest": "^28.6.0",
98
- "eslint-plugin-prettier": "^5.2.1",
99
- "jest": "^29.7.0",
100
- "jsdom": "^24.1.1",
101
- "node-addon-api": "^7.0.0",
102
- "prebuild": "^13.0.1",
103
- "prettier": "^3.3.3",
104
- "puppeteer": "^22.14.0",
105
- "rimraf": "^5.0.9",
106
- "rollup": "^4.22.5",
107
- "rollup-plugin-dts": "^6.1.1",
108
- "rollup-plugin-esbuild": "^6.1.1",
109
- "ts-api-utils": "^1.3.0",
110
- "ts-jest": "^29.2.3",
111
- "ts-node": "^10.9.2",
112
- "typescript": "5.4"
25
+ "./polyfill": {
26
+ "types": "./dist/types/polyfill/index.d.ts",
27
+ "require": "./dist/cjs/polyfill/index.cjs",
28
+ "import": "./dist/esm/polyfill/index.mjs",
29
+ "default": "./dist/polyfill/esm/index.mjs"
30
+ }
31
+ },
32
+ "engines": {
33
+ "node": ">=18.20.0"
34
+ },
35
+ "scripts": {
36
+ "install": "prebuild-install -r napi || (npm install --ignore-scripts --production=false && npm run _prebuild)",
37
+ "install:nice": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_NICE=1 && cmake-js build",
38
+ "install:gnu": "npm run clean && npm install --ignore-scripts --production=false && cmake-js configure --CDUSE_GNUTLS=1 && cmake-js build",
39
+ "build": "npm run compile && npm run build:tsc",
40
+ "compile": "cmake-js build",
41
+ "build:debug": "npm run compile:debug && npm run build:tsc",
42
+ "compile:debug": "cmake-js configure -D && cmake-js build -D",
43
+ "build:tsc": "rimraf dist && rollup -c",
44
+ "build:tsc:watch": "rollup -c -w",
45
+ "clean": "rimraf dist build",
46
+ "lint": "eslint . --ext .ts --ext .mts",
47
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
48
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch",
49
+ "test:wpt": "npm run run:wpt:server & (sleep 8 && (npm run run:wpt:test | tee test/wpt-tests/last-test-results.md) )",
50
+ "wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
51
+ "wpt:test": "ts-node test/wpt-tests/index.ts",
52
+ "_prebuild": "prebuild -r napi --backend cmake-js",
53
+ "prepack": "npm run build:tsc"
54
+ },
55
+ "binary": {
56
+ "napi_versions": [
57
+ 8
58
+ ]
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "git+https://github.com/murat-dogan/node-datachannel.git"
63
+ },
64
+ "keywords": [
65
+ "libdatachannel",
66
+ "webrtc",
67
+ "p2p",
68
+ "peer-to-peer",
69
+ "datachannel",
70
+ "data channel",
71
+ "websocket"
72
+ ],
73
+ "contributors": [
74
+ {
75
+ "name": "Murat Doğan",
76
+ "url": "https://github.com/murat-dogan"
113
77
  },
114
- "dependencies": {
115
- "prebuild-install": "^7.1.3"
78
+ {
79
+ "name": "Paul-Louis Ageneau",
80
+ "url": "https://github.com/paullouisageneau"
116
81
  }
82
+ ],
83
+ "license": "MPL 2.0",
84
+ "bugs": {
85
+ "url": "https://github.com/murat-dogan/node-datachannel/issues"
86
+ },
87
+ "homepage": "https://github.com/murat-dogan/node-datachannel#readme",
88
+ "devDependencies": {
89
+ "@rollup/plugin-esm-shim": "^0.1.7",
90
+ "@rollup/plugin-replace": "^6.0.1",
91
+ "@types/jest": "^29.5.12",
92
+ "@types/node": "^20.6.1",
93
+ "@types/webrtc": "^0.0.46",
94
+ "@typescript-eslint/eslint-plugin": "^7.17.0",
95
+ "@typescript-eslint/parser": "^7.17.0",
96
+ "cmake-js": "^7.3.0",
97
+ "eslint": "^8.57.0",
98
+ "eslint-config-prettier": "^9.1.0",
99
+ "eslint-plugin-jest": "^28.6.0",
100
+ "eslint-plugin-prettier": "^5.2.1",
101
+ "jest": "^29.7.0",
102
+ "jsdom": "^24.1.1",
103
+ "node-addon-api": "^7.0.0",
104
+ "prebuild": "^13.0.1",
105
+ "prettier": "^3.3.3",
106
+ "puppeteer": "^22.14.0",
107
+ "rimraf": "^5.0.9",
108
+ "rollup": "^4.22.5",
109
+ "rollup-plugin-dts": "^6.1.1",
110
+ "rollup-plugin-esbuild": "^6.1.1",
111
+ "ts-api-utils": "^1.3.0",
112
+ "ts-jest": "^29.2.3",
113
+ "ts-node": "^10.9.2",
114
+ "typescript": "5.4"
115
+ },
116
+ "dependencies": {
117
+ "prebuild-install": "^7.1.3"
118
+ }
117
119
  }
package/rollup.config.mjs CHANGED
@@ -17,7 +17,7 @@ export default [
17
17
  bundle({
18
18
  plugins: [
19
19
  replace({
20
- include: "src/lib/node-datachannel.ts",
20
+ include: 'src/lib/node-datachannel.ts',
21
21
  preventAssignment: true,
22
22
  "require('../../build": "require('../../../build",
23
23
  }),
@@ -103,6 +103,17 @@ void DataChannelWrapper::doCleanup()
103
103
  {
104
104
  PLOG_DEBUG << "doCleanup() called";
105
105
  mOnClosedCallback.reset();
106
+
107
+ // For close there are 3 cases:
108
+ // 1. Close was called from JS
109
+ // 2. Close was called from RTC
110
+ // 3. Close was not called from JS or RTC (we are destroying the object)
111
+ // This could be coming from rtc (case 2)
112
+ mDataChannelPtr.reset();
113
+ mOnOpenCallback.reset();
114
+ mOnErrorCallback.reset();
115
+ mOnBufferedAmountLowCallback.reset();
116
+ mOnMessageCallback.reset();
106
117
  instances.erase(this);
107
118
  }
108
119
 
package/src/cpp/main.cpp CHANGED
@@ -2,24 +2,38 @@
2
2
  #include "rtc-wrapper.h"
3
3
  #include "peer-connection-wrapper.h"
4
4
  #include "data-channel-wrapper.h"
5
+
6
+ #if RTC_ENABLE_MEDIA == 1
5
7
  #include "media-rtcpreceivingsession-wrapper.h"
6
8
  #include "media-track-wrapper.h"
7
9
  #include "media-video-wrapper.h"
8
10
  #include "media-audio-wrapper.h"
11
+ #endif
12
+
13
+ #if RTC_ENABLE_WEBSOCKET == 1
9
14
  #include "web-socket-wrapper.h"
10
15
  #include "web-socket-server-wrapper.h"
16
+ #endif
11
17
 
12
18
  Napi::Object InitAll(Napi::Env env, Napi::Object exports)
13
19
  {
14
20
  RtcWrapper::Init(env, exports);
21
+
22
+ #if RTC_ENABLE_MEDIA == 1
15
23
  RtcpReceivingSessionWrapper::Init(env, exports);
16
24
  TrackWrapper::Init(env, exports);
17
25
  VideoWrapper::Init(env, exports);
18
26
  AudioWrapper::Init(env, exports);
27
+ #endif
28
+
19
29
  DataChannelWrapper::Init(env, exports);
20
30
  PeerConnectionWrapper::Init(env, exports);
31
+
32
+ #if RTC_ENABLE_WEBSOCKET == 1
21
33
  WebSocketWrapper::Init(env, exports);
22
34
  WebSocketServerWrapper::Init(env, exports);
35
+ #endif
36
+
23
37
  return exports;
24
38
  }
25
39
 
@@ -1,8 +1,11 @@
1
1
  #include "peer-connection-wrapper.h"
2
2
  #include "data-channel-wrapper.h"
3
+
4
+ #if RTC_ENABLE_MEDIA == 1
3
5
  #include "media-track-wrapper.h"
4
6
  #include "media-video-wrapper.h"
5
7
  #include "media-audio-wrapper.h"
8
+ #endif
6
9
 
7
10
  #include "plog/Log.h"
8
11
 
@@ -43,7 +46,11 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
43
46
  InstanceMethod("remoteDescription", &PeerConnectionWrapper::remoteDescription),
44
47
  InstanceMethod("addRemoteCandidate", &PeerConnectionWrapper::addRemoteCandidate),
45
48
  InstanceMethod("createDataChannel", &PeerConnectionWrapper::createDataChannel),
49
+
50
+ #if RTC_ENABLE_MEDIA == 1
46
51
  InstanceMethod("addTrack", &PeerConnectionWrapper::addTrack),
52
+ InstanceMethod("onTrack", &PeerConnectionWrapper::onTrack),
53
+ #endif
47
54
  InstanceMethod("hasMedia", &PeerConnectionWrapper::hasMedia),
48
55
  InstanceMethod("state", &PeerConnectionWrapper::state),
49
56
  InstanceMethod("iceState", &PeerConnectionWrapper::iceState),
@@ -56,7 +63,6 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
56
63
  InstanceMethod("onSignalingStateChange", &PeerConnectionWrapper::onSignalingStateChange),
57
64
  InstanceMethod("onGatheringStateChange", &PeerConnectionWrapper::onGatheringStateChange),
58
65
  InstanceMethod("onDataChannel", &PeerConnectionWrapper::onDataChannel),
59
- InstanceMethod("onTrack", &PeerConnectionWrapper::onTrack),
60
66
  InstanceMethod("bytesSent", &PeerConnectionWrapper::bytesSent),
61
67
  InstanceMethod("bytesReceived", &PeerConnectionWrapper::bytesReceived),
62
68
  InstanceMethod("rtt", &PeerConnectionWrapper::rtt),
@@ -1049,6 +1055,7 @@ std::string PeerConnectionWrapper::candidateTransportTypeToString(const rtc::Can
1049
1055
  }
1050
1056
  }
1051
1057
 
1058
+ #if RTC_ENABLE_MEDIA == 1
1052
1059
  Napi::Value PeerConnectionWrapper::addTrack(const Napi::CallbackInfo &info)
1053
1060
  {
1054
1061
  PLOG_DEBUG << "addTrack() called";
@@ -1132,6 +1139,7 @@ void PeerConnectionWrapper::onTrack(const Napi::CallbackInfo &info)
1132
1139
  args = {instance};
1133
1140
  }); });
1134
1141
  }
1142
+ #endif
1135
1143
 
1136
1144
  Napi::Value PeerConnectionWrapper::hasMedia(const Napi::CallbackInfo &info)
1137
1145
  {
@@ -27,7 +27,12 @@ public:
27
27
  Napi::Value remoteDescription(const Napi::CallbackInfo &info);
28
28
  void addRemoteCandidate(const Napi::CallbackInfo &info);
29
29
  Napi::Value createDataChannel(const Napi::CallbackInfo &info);
30
+
31
+ #if RTC_ENABLE_MEDIA == 1
30
32
  Napi::Value addTrack(const Napi::CallbackInfo &info);
33
+ void onTrack(const Napi::CallbackInfo &info);
34
+ #endif
35
+
31
36
  Napi::Value hasMedia(const Napi::CallbackInfo &info);
32
37
  Napi::Value state(const Napi::CallbackInfo &info);
33
38
  Napi::Value iceState(const Napi::CallbackInfo &info);
@@ -42,7 +47,6 @@ public:
42
47
  void onSignalingStateChange(const Napi::CallbackInfo &info);
43
48
  void onGatheringStateChange(const Napi::CallbackInfo &info);
44
49
  void onDataChannel(const Napi::CallbackInfo &info);
45
- void onTrack(const Napi::CallbackInfo &info);
46
50
 
47
51
  // Stats
48
52
  Napi::Value bytesSent(const Napi::CallbackInfo &info);
@@ -1,9 +1,15 @@
1
1
  #include "rtc-wrapper.h"
2
2
  #include "peer-connection-wrapper.h"
3
3
  #include "data-channel-wrapper.h"
4
+
5
+ #if RTC_ENABLE_MEDIA == 1
4
6
  #include "media-track-wrapper.h"
7
+ #endif
8
+
9
+ #if RTC_ENABLE_WEBSOCKET == 1
5
10
  #include "web-socket-wrapper.h"
6
11
  #include "web-socket-server-wrapper.h"
12
+ #endif
7
13
 
8
14
  #include "plog/Log.h"
9
15
 
@@ -118,9 +124,15 @@ void RtcWrapper::cleanup(const Napi::CallbackInfo &info)
118
124
  {
119
125
  PeerConnectionWrapper::CloseAll();
120
126
  DataChannelWrapper::CloseAll();
127
+
128
+ #if RTC_ENABLE_MEDIA == 1
121
129
  TrackWrapper::CloseAll();
130
+ #endif
131
+
132
+ #if RTC_ENABLE_WEBSOCKET == 1
122
133
  WebSocketWrapper::CloseAll();
123
134
  WebSocketServerWrapper::StopAll();
135
+ #endif
124
136
 
125
137
  const auto timeout = std::chrono::seconds(10);
126
138
  if (rtc::Cleanup().wait_for(std::chrono::seconds(timeout)) == std::future_status::timeout)
@@ -129,8 +141,14 @@ void RtcWrapper::cleanup(const Napi::CallbackInfo &info)
129
141
  // Cleanup the instances
130
142
  PeerConnectionWrapper::CleanupAll();
131
143
  DataChannelWrapper::CleanupAll();
144
+
145
+ #if RTC_ENABLE_MEDIA == 1
132
146
  TrackWrapper::CleanupAll();
147
+ #endif
148
+
149
+ #if RTC_ENABLE_WEBSOCKET == 1
133
150
  WebSocketWrapper::CleanupAll();
151
+ #endif
134
152
 
135
153
  if (logCallback)
136
154
  logCallback.reset();
package/src/index.ts CHANGED
@@ -5,6 +5,5 @@
5
5
  import n from './lib/index';
6
6
  import p from './polyfill/index';
7
7
 
8
-
9
8
  export const nodeDataChannel = n;
10
9
  export const polyfill = p;
@@ -10,92 +10,91 @@ import * as stream from 'stream';
10
10
  * the protocol level, and is preserved here throughout.
11
11
  */
12
12
  export default class DataChannelStream extends stream.Duplex {
13
- private _rawChannel: any;
14
- private _readActive: boolean;
15
-
16
-
17
- constructor(rawChannel: any, streamOptions?: Omit<stream.DuplexOptions, 'objectMode'>) {
18
- super({
19
- allowHalfOpen: false, // Default to autoclose on end().
20
- ...streamOptions,
21
- objectMode: true, // Preserve the string/buffer distinction (WebRTC treats them differently)
22
- });
23
-
24
- this._rawChannel = rawChannel;
25
- this._readActive = true;
26
-
27
- rawChannel.onMessage((msg: any) => {
28
- if (!this._readActive) return; // If the buffer is full, drop messages.
29
-
30
- // If the push is rejected, we pause reading until the next call to _read().
31
- this._readActive = this.push(msg);
32
- });
33
-
34
- // When the DataChannel closes, the readable & writable ends close
35
- rawChannel.onClosed(() => {
36
- this.push(null);
37
- this.destroy();
38
- });
39
-
40
- rawChannel.onError((errMsg: string) => {
41
- this.destroy(new Error(`DataChannel error: ${errMsg}`));
42
- });
43
-
44
- // Buffer all writes until the DataChannel opens
45
- if (!rawChannel.isOpen()) {
46
- this.cork();
47
- rawChannel.onOpen(() => this.uncork());
48
- }
13
+ private _rawChannel: any;
14
+ private _readActive: boolean;
15
+
16
+ constructor(rawChannel: any, streamOptions?: Omit<stream.DuplexOptions, 'objectMode'>) {
17
+ super({
18
+ allowHalfOpen: false, // Default to autoclose on end().
19
+ ...streamOptions,
20
+ objectMode: true, // Preserve the string/buffer distinction (WebRTC treats them differently)
21
+ });
22
+
23
+ this._rawChannel = rawChannel;
24
+ this._readActive = true;
25
+
26
+ rawChannel.onMessage((msg: any) => {
27
+ if (!this._readActive) return; // If the buffer is full, drop messages.
28
+
29
+ // If the push is rejected, we pause reading until the next call to _read().
30
+ this._readActive = this.push(msg);
31
+ });
32
+
33
+ // When the DataChannel closes, the readable & writable ends close
34
+ rawChannel.onClosed(() => {
35
+ this.push(null);
36
+ this.destroy();
37
+ });
38
+
39
+ rawChannel.onError((errMsg: string) => {
40
+ this.destroy(new Error(`DataChannel error: ${errMsg}`));
41
+ });
42
+
43
+ // Buffer all writes until the DataChannel opens
44
+ if (!rawChannel.isOpen()) {
45
+ this.cork();
46
+ rawChannel.onOpen(() => this.uncork());
49
47
  }
50
-
51
- _read(): void {
52
- // Stop dropping messages, if the buffer filling up meant we were doing so before.
53
- this._readActive = true;
54
- }
55
-
56
- _write(chunk, _encoding, callback): void {
57
- let sentOk;
58
-
59
- try {
60
- if (Buffer.isBuffer(chunk)) {
61
- sentOk = this._rawChannel.sendMessageBinary(chunk);
62
- } else if (typeof chunk === 'string') {
63
- sentOk = this._rawChannel.sendMessage(chunk);
64
- } else {
65
- const typeName = chunk.constructor.name || typeof chunk;
66
- throw new Error(`Cannot write ${typeName} to DataChannel stream`);
67
- }
68
- } catch (err) {
69
- return callback(err);
70
- }
71
-
72
- if (sentOk) {
73
- callback(null);
74
- } else {
75
- callback(new Error('Failed to write to DataChannel'));
76
- }
77
- }
78
-
79
- _final(callback): void {
80
- if (!this.allowHalfOpen) this.destroy();
81
- callback(null);
82
- }
83
-
84
- _destroy(maybeErr, callback): void {
85
- // When the stream is destroyed, we close the DataChannel.
86
- this._rawChannel.close();
87
- callback(maybeErr);
88
- }
89
-
90
- get label(): string {
91
- return this._rawChannel.getLabel();
92
- }
93
-
94
- get id(): number {
95
- return this._rawChannel.getId();
48
+ }
49
+
50
+ _read(): void {
51
+ // Stop dropping messages, if the buffer filling up meant we were doing so before.
52
+ this._readActive = true;
53
+ }
54
+
55
+ _write(chunk, _encoding, callback): void {
56
+ let sentOk;
57
+
58
+ try {
59
+ if (Buffer.isBuffer(chunk)) {
60
+ sentOk = this._rawChannel.sendMessageBinary(chunk);
61
+ } else if (typeof chunk === 'string') {
62
+ sentOk = this._rawChannel.sendMessage(chunk);
63
+ } else {
64
+ const typeName = chunk.constructor.name || typeof chunk;
65
+ throw new Error(`Cannot write ${typeName} to DataChannel stream`);
66
+ }
67
+ } catch (err) {
68
+ return callback(err);
96
69
  }
97
70
 
98
- get protocol(): string {
99
- return this._rawChannel.getProtocol();
71
+ if (sentOk) {
72
+ callback(null);
73
+ } else {
74
+ callback(new Error('Failed to write to DataChannel'));
100
75
  }
76
+ }
77
+
78
+ _final(callback): void {
79
+ if (!this.allowHalfOpen) this.destroy();
80
+ callback(null);
81
+ }
82
+
83
+ _destroy(maybeErr, callback): void {
84
+ // When the stream is destroyed, we close the DataChannel.
85
+ this._rawChannel.close();
86
+ callback(maybeErr);
87
+ }
88
+
89
+ get label(): string {
90
+ return this._rawChannel.getLabel();
91
+ }
92
+
93
+ get id(): number {
94
+ return this._rawChannel.getId();
95
+ }
96
+
97
+ get protocol(): string {
98
+ return this._rawChannel.getProtocol();
99
+ }
101
100
  }