node-datachannel 0.3.1 → 0.3.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/.github/workflows/lint.yml +22 -0
- package/CMakeLists.txt +2 -2
- package/examples/client-server/client-benchmark.js +43 -32
- package/examples/client-server/client-periodic.js +30 -14
- package/examples/client-server/client.js +6 -5
- package/examples/client-server/package-lock.json +183 -44
- package/examples/client-server/signaling-server.js +3 -5
- package/examples/media/media.js +9 -9
- package/lib/index.d.ts +100 -100
- package/node_modules/lru-cache/package.json +2 -4
- package/node_modules/minimist/package.json +0 -1
- package/node_modules/node-abi/node_modules/semver/README.md +3 -1
- package/node_modules/node-abi/node_modules/semver/bin/semver.js +19 -9
- package/node_modules/node-abi/node_modules/semver/classes/comparator.js +3 -2
- package/node_modules/node-abi/node_modules/semver/classes/index.js +1 -1
- package/node_modules/node-abi/node_modules/semver/classes/range.js +31 -22
- package/node_modules/node-abi/node_modules/semver/classes/semver.js +1 -1
- package/node_modules/node-abi/node_modules/semver/functions/cmp.js +8 -4
- package/node_modules/node-abi/node_modules/semver/functions/coerce.js +3 -2
- package/node_modules/node-abi/node_modules/semver/functions/inc.js +4 -1
- package/node_modules/node-abi/node_modules/semver/functions/parse.js +1 -1
- package/node_modules/node-abi/node_modules/semver/internal/constants.js +2 -2
- package/node_modules/node-abi/node_modules/semver/internal/identifiers.js +1 -1
- package/node_modules/node-abi/node_modules/semver/internal/parse-options.js +3 -3
- package/node_modules/node-abi/node_modules/semver/internal/re.js +3 -3
- package/node_modules/node-abi/node_modules/semver/package.json +51 -18
- package/node_modules/node-abi/node_modules/semver/ranges/min-version.js +2 -1
- package/node_modules/node-abi/node_modules/semver/ranges/outside.js +1 -1
- package/node_modules/node-abi/node_modules/semver/ranges/simplify.js +15 -12
- package/node_modules/node-abi/node_modules/semver/ranges/subset.js +53 -31
- package/node_modules/npmlog/node_modules/are-we-there-yet/package.json +10 -10
- package/node_modules/npmlog/package.json +1 -1
- package/node_modules/tar-stream/node_modules/bl/.travis.yml +4 -3
- package/node_modules/tar-stream/node_modules/bl/BufferList.js +1 -1
- package/node_modules/tar-stream/node_modules/bl/package.json +10 -10
- package/node_modules/tar-stream/node_modules/bl/test/test.js +20 -2
- package/node_modules/wide-align/LICENSE +0 -0
- package/node_modules/wide-align/README.md +0 -0
- package/node_modules/wide-align/align.js +0 -0
- package/node_modules/wide-align/package.json +14 -14
- package/package.json +2 -2
- package/src/data-channel-wrapper.cpp +69 -53
- package/src/data-channel-wrapper.h +0 -1
- package/src/media-track-wrapper.cpp +91 -52
- package/src/media-track-wrapper.h +0 -1
- package/src/peer-connection-wrapper.cpp +106 -70
- package/src/peer-connection-wrapper.h +0 -2
- package/test/connectivity.js +18 -17
- package/test/test.js +22 -30
- package/node_modules/node-abi/node_modules/semver/CHANGELOG.md +0 -111
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
{
|
|
2
|
+
"//": "But not version 5 of string-width, as that's ESM only",
|
|
2
3
|
"_args": [
|
|
3
4
|
[
|
|
4
|
-
"wide-align@1.1.
|
|
5
|
+
"wide-align@1.1.5",
|
|
5
6
|
"/home/runner/work/node-datachannel/node-datachannel"
|
|
6
7
|
]
|
|
7
8
|
],
|
|
8
|
-
"_from": "wide-align@1.1.
|
|
9
|
-
"_id": "wide-align@1.1.
|
|
9
|
+
"_from": "wide-align@1.1.5",
|
|
10
|
+
"_id": "wide-align@1.1.5",
|
|
10
11
|
"_inBundle": false,
|
|
11
|
-
"_integrity": "sha512-
|
|
12
|
+
"_integrity": "sha512-eDMORYaPNZ4sQIuuYPDHdQvf4gyCF9rEEV/yPxGfwPkRodwEgiMUUXTx/dex+Me0wxx53S+NgUHaP7y3MGlDmg==",
|
|
12
13
|
"_location": "/wide-align",
|
|
13
14
|
"_phantomChildren": {},
|
|
14
15
|
"_requested": {
|
|
15
16
|
"type": "version",
|
|
16
17
|
"registry": true,
|
|
17
|
-
"raw": "wide-align@1.1.
|
|
18
|
+
"raw": "wide-align@1.1.5",
|
|
18
19
|
"name": "wide-align",
|
|
19
20
|
"escapedName": "wide-align",
|
|
20
|
-
"rawSpec": "1.1.
|
|
21
|
+
"rawSpec": "1.1.5",
|
|
21
22
|
"saveSpec": null,
|
|
22
|
-
"fetchSpec": "1.1.
|
|
23
|
+
"fetchSpec": "1.1.5"
|
|
23
24
|
},
|
|
24
25
|
"_requiredBy": [
|
|
25
26
|
"/npmlog/gauge"
|
|
26
27
|
],
|
|
27
|
-
"_resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.
|
|
28
|
-
"_spec": "1.1.
|
|
28
|
+
"_resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.5.tgz",
|
|
29
|
+
"_spec": "1.1.5",
|
|
29
30
|
"_where": "/home/runner/work/node-datachannel/node-datachannel",
|
|
30
31
|
"author": {
|
|
31
32
|
"name": "Rebecca Turner",
|
|
@@ -36,11 +37,11 @@
|
|
|
36
37
|
"url": "https://github.com/iarna/wide-align/issues"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"string-width": "^1.0.2 || 2"
|
|
40
|
+
"string-width": "^1.0.2 || 2 || 3 || 4"
|
|
40
41
|
},
|
|
41
42
|
"description": "A wide-character aware text alignment function for use on the console or with fixed width fonts.",
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"tap": "
|
|
44
|
+
"tap": "*"
|
|
44
45
|
},
|
|
45
46
|
"files": [
|
|
46
47
|
"align.js"
|
|
@@ -62,8 +63,7 @@
|
|
|
62
63
|
"url": "git+https://github.com/iarna/wide-align.git"
|
|
63
64
|
},
|
|
64
65
|
"scripts": {
|
|
65
|
-
"test": "tap --coverage test/*.js"
|
|
66
|
-
"version": "perl -pi -e 's/^( \"version\": $ENV{npm_config_node_version}\").*?\",/$1abc\",/' package-lock.json ; git add package-lock.json"
|
|
66
|
+
"test": "tap --coverage test/*.js"
|
|
67
67
|
},
|
|
68
|
-
"version": "1.1.
|
|
68
|
+
"version": "1.1.5"
|
|
69
69
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-datachannel",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "libdatachannel node bindings",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"build": "cmake-js build",
|
|
16
16
|
"_prebuild": "prebuild --backend cmake-js",
|
|
17
17
|
"clean": "cmake-js clean",
|
|
18
|
-
"lint": "eslint lib/**/*",
|
|
18
|
+
"lint": "eslint lib/**/* test/**/*",
|
|
19
19
|
"test": "jest"
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|
|
@@ -44,26 +44,8 @@ Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports)
|
|
|
44
44
|
|
|
45
45
|
DataChannelWrapper::DataChannelWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap<DataChannelWrapper>(info)
|
|
46
46
|
{
|
|
47
|
-
mDataChannelPtr = *(info[0].As<Napi::External<std::shared_ptr<rtc::DataChannel>>>().Data());
|
|
47
|
+
mDataChannelPtr = *(info[0].As<Napi::External<std::shared_ptr<rtc::DataChannel>>>().Data());
|
|
48
48
|
|
|
49
|
-
mDataChannelPtr->onClosed([&]()
|
|
50
|
-
{
|
|
51
|
-
if (mOnClosedCallback)
|
|
52
|
-
mOnClosedCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
|
|
53
|
-
// Check the peer connection is not closed
|
|
54
|
-
if(instances.find(this) == instances.end())
|
|
55
|
-
throw ThreadSafeCallback::CancelException();
|
|
56
|
-
|
|
57
|
-
cleanCbsAndEraseInstance();
|
|
58
|
-
|
|
59
|
-
// This will run in main thread and needs to construct the
|
|
60
|
-
// arguments for the call
|
|
61
|
-
args = {};
|
|
62
|
-
});
|
|
63
|
-
else {
|
|
64
|
-
cleanCbsAndEraseInstance();
|
|
65
|
-
} });
|
|
66
|
-
|
|
67
49
|
instances.insert(this);
|
|
68
50
|
}
|
|
69
51
|
|
|
@@ -79,17 +61,15 @@ void DataChannelWrapper::doClose()
|
|
|
79
61
|
try
|
|
80
62
|
{
|
|
81
63
|
mDataChannelPtr->close();
|
|
64
|
+
mDataChannelPtr.reset();
|
|
82
65
|
}
|
|
83
66
|
catch (std::exception &ex)
|
|
84
67
|
{
|
|
85
|
-
std::
|
|
68
|
+
std::cerr << std::string("libdatachannel error while closing data channel: ") + ex.what() << std::endl;
|
|
86
69
|
return;
|
|
87
70
|
}
|
|
88
71
|
}
|
|
89
|
-
}
|
|
90
72
|
|
|
91
|
-
void DataChannelWrapper::cleanCbsAndEraseInstance()
|
|
92
|
-
{
|
|
93
73
|
mOnOpenCallback.reset();
|
|
94
74
|
mOnClosedCallback.reset();
|
|
95
75
|
mOnErrorCallback.reset();
|
|
@@ -97,7 +77,6 @@ void DataChannelWrapper::cleanCbsAndEraseInstance()
|
|
|
97
77
|
mOnMessageCallback.reset();
|
|
98
78
|
|
|
99
79
|
instances.erase(this);
|
|
100
|
-
mDataChannelPtr.reset();
|
|
101
80
|
}
|
|
102
81
|
|
|
103
82
|
void DataChannelWrapper::close(const Napi::CallbackInfo &info)
|
|
@@ -109,7 +88,7 @@ Napi::Value DataChannelWrapper::getLabel(const Napi::CallbackInfo &info)
|
|
|
109
88
|
{
|
|
110
89
|
if (!mDataChannelPtr)
|
|
111
90
|
{
|
|
112
|
-
Napi::Error::New(info.Env(), "
|
|
91
|
+
Napi::Error::New(info.Env(), "getLabel() called on destroyed channel").ThrowAsJavaScriptException();
|
|
113
92
|
return info.Env().Null();
|
|
114
93
|
}
|
|
115
94
|
|
|
@@ -120,7 +99,7 @@ Napi::Value DataChannelWrapper::getId(const Napi::CallbackInfo &info)
|
|
|
120
99
|
{
|
|
121
100
|
if (!mDataChannelPtr)
|
|
122
101
|
{
|
|
123
|
-
Napi::Error::New(info.Env(), "
|
|
102
|
+
Napi::Error::New(info.Env(), "getId() called on destroyed channel").ThrowAsJavaScriptException();
|
|
124
103
|
return info.Env().Null();
|
|
125
104
|
}
|
|
126
105
|
|
|
@@ -131,7 +110,7 @@ Napi::Value DataChannelWrapper::getProtocol(const Napi::CallbackInfo &info)
|
|
|
131
110
|
{
|
|
132
111
|
if (!mDataChannelPtr)
|
|
133
112
|
{
|
|
134
|
-
Napi::Error::New(info.Env(), "
|
|
113
|
+
Napi::Error::New(info.Env(), "getProtocol() called on destroyed channel").ThrowAsJavaScriptException();
|
|
135
114
|
return info.Env().Null();
|
|
136
115
|
}
|
|
137
116
|
|
|
@@ -142,7 +121,7 @@ Napi::Value DataChannelWrapper::sendMessage(const Napi::CallbackInfo &info)
|
|
|
142
121
|
{
|
|
143
122
|
if (!mDataChannelPtr)
|
|
144
123
|
{
|
|
145
|
-
Napi::Error::New(info.Env(), "
|
|
124
|
+
Napi::Error::New(info.Env(), "sendMessage() called on destroyed channel").ThrowAsJavaScriptException();
|
|
146
125
|
return info.Env().Null();
|
|
147
126
|
}
|
|
148
127
|
|
|
@@ -162,7 +141,7 @@ Napi::Value DataChannelWrapper::sendMessage(const Napi::CallbackInfo &info)
|
|
|
162
141
|
}
|
|
163
142
|
catch (std::exception &ex)
|
|
164
143
|
{
|
|
165
|
-
Napi::Error::New(env, std::string("libdatachannel error while sending
|
|
144
|
+
Napi::Error::New(env, std::string("libdatachannel error while sending data channel message: ") + ex.what()).ThrowAsJavaScriptException();
|
|
166
145
|
return Napi::Boolean::New(info.Env(), false);
|
|
167
146
|
}
|
|
168
147
|
}
|
|
@@ -171,7 +150,7 @@ Napi::Value DataChannelWrapper::sendMessageBinary(const Napi::CallbackInfo &info
|
|
|
171
150
|
{
|
|
172
151
|
if (!mDataChannelPtr)
|
|
173
152
|
{
|
|
174
|
-
Napi::Error::New(info.Env(), "
|
|
153
|
+
Napi::Error::New(info.Env(), "sendMessagBinary() called on destroyed channel").ThrowAsJavaScriptException();
|
|
175
154
|
return info.Env().Null();
|
|
176
155
|
}
|
|
177
156
|
|
|
@@ -191,7 +170,7 @@ Napi::Value DataChannelWrapper::sendMessageBinary(const Napi::CallbackInfo &info
|
|
|
191
170
|
}
|
|
192
171
|
catch (std::exception &ex)
|
|
193
172
|
{
|
|
194
|
-
Napi::Error::New(env, std::string("libdatachannel error while sending
|
|
173
|
+
Napi::Error::New(env, std::string("libdatachannel error while sending data channel message: ") + ex.what()).ThrowAsJavaScriptException();
|
|
195
174
|
return Napi::Boolean::New(info.Env(), false);
|
|
196
175
|
}
|
|
197
176
|
}
|
|
@@ -199,6 +178,7 @@ Napi::Value DataChannelWrapper::sendMessageBinary(const Napi::CallbackInfo &info
|
|
|
199
178
|
Napi::Value DataChannelWrapper::isOpen(const Napi::CallbackInfo &info)
|
|
200
179
|
{
|
|
201
180
|
Napi::Env env = info.Env();
|
|
181
|
+
|
|
202
182
|
if (!mDataChannelPtr)
|
|
203
183
|
{
|
|
204
184
|
return Napi::Boolean::New(info.Env(), false);
|
|
@@ -210,7 +190,7 @@ Napi::Value DataChannelWrapper::isOpen(const Napi::CallbackInfo &info)
|
|
|
210
190
|
}
|
|
211
191
|
catch (std::exception &ex)
|
|
212
192
|
{
|
|
213
|
-
Napi::Error::New(env, std::string("libdatachannel error
|
|
193
|
+
Napi::Error::New(env, std::string("libdatachannel error: ") + ex.what()).ThrowAsJavaScriptException();
|
|
214
194
|
return Napi::Boolean::New(info.Env(), false);
|
|
215
195
|
}
|
|
216
196
|
}
|
|
@@ -218,6 +198,7 @@ Napi::Value DataChannelWrapper::isOpen(const Napi::CallbackInfo &info)
|
|
|
218
198
|
Napi::Value DataChannelWrapper::bufferedAmount(const Napi::CallbackInfo &info)
|
|
219
199
|
{
|
|
220
200
|
Napi::Env env = info.Env();
|
|
201
|
+
|
|
221
202
|
if (!mDataChannelPtr)
|
|
222
203
|
{
|
|
223
204
|
return Napi::Number::New(info.Env(), 0);
|
|
@@ -229,7 +210,7 @@ Napi::Value DataChannelWrapper::bufferedAmount(const Napi::CallbackInfo &info)
|
|
|
229
210
|
}
|
|
230
211
|
catch (std::exception &ex)
|
|
231
212
|
{
|
|
232
|
-
Napi::Error::New(env, std::string("libdatachannel error
|
|
213
|
+
Napi::Error::New(env, std::string("libdatachannel error: ") + ex.what()).ThrowAsJavaScriptException();
|
|
233
214
|
return Napi::Number::New(info.Env(), 0);
|
|
234
215
|
}
|
|
235
216
|
}
|
|
@@ -237,6 +218,7 @@ Napi::Value DataChannelWrapper::bufferedAmount(const Napi::CallbackInfo &info)
|
|
|
237
218
|
Napi::Value DataChannelWrapper::maxMessageSize(const Napi::CallbackInfo &info)
|
|
238
219
|
{
|
|
239
220
|
Napi::Env env = info.Env();
|
|
221
|
+
|
|
240
222
|
if (!mDataChannelPtr)
|
|
241
223
|
{
|
|
242
224
|
return Napi::Number::New(info.Env(), 0);
|
|
@@ -248,7 +230,7 @@ Napi::Value DataChannelWrapper::maxMessageSize(const Napi::CallbackInfo &info)
|
|
|
248
230
|
}
|
|
249
231
|
catch (std::exception &ex)
|
|
250
232
|
{
|
|
251
|
-
Napi::Error::New(env, std::string("libdatachannel error
|
|
233
|
+
Napi::Error::New(env, std::string("libdatachannel error: ") + ex.what()).ThrowAsJavaScriptException();
|
|
252
234
|
return Napi::Number::New(info.Env(), 0);
|
|
253
235
|
}
|
|
254
236
|
}
|
|
@@ -257,7 +239,7 @@ void DataChannelWrapper::setBufferedAmountLowThreshold(const Napi::CallbackInfo
|
|
|
257
239
|
{
|
|
258
240
|
if (!mDataChannelPtr)
|
|
259
241
|
{
|
|
260
|
-
Napi::Error::New(info.Env(), "
|
|
242
|
+
Napi::Error::New(info.Env(), "setBufferedAmountLowThreshold() called on destroyed channel").ThrowAsJavaScriptException();
|
|
261
243
|
return;
|
|
262
244
|
}
|
|
263
245
|
|
|
@@ -276,13 +258,19 @@ void DataChannelWrapper::setBufferedAmountLowThreshold(const Napi::CallbackInfo
|
|
|
276
258
|
}
|
|
277
259
|
catch (std::exception &ex)
|
|
278
260
|
{
|
|
279
|
-
Napi::Error::New(env, std::string("libdatachannel error
|
|
261
|
+
Napi::Error::New(env, std::string("libdatachannel error: ") + ex.what()).ThrowAsJavaScriptException();
|
|
280
262
|
return;
|
|
281
263
|
}
|
|
282
264
|
}
|
|
283
265
|
|
|
284
266
|
void DataChannelWrapper::onOpen(const Napi::CallbackInfo &info)
|
|
285
267
|
{
|
|
268
|
+
if (!mDataChannelPtr)
|
|
269
|
+
{
|
|
270
|
+
Napi::Error::New(info.Env(), "onOpen() called on destroyed channel").ThrowAsJavaScriptException();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
286
274
|
Napi::Env env = info.Env();
|
|
287
275
|
int length = info.Length();
|
|
288
276
|
|
|
@@ -295,11 +283,10 @@ void DataChannelWrapper::onOpen(const Napi::CallbackInfo &info)
|
|
|
295
283
|
// Callback
|
|
296
284
|
mOnOpenCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
|
297
285
|
|
|
298
|
-
mDataChannelPtr->onOpen([&]()
|
|
299
|
-
{
|
|
286
|
+
mDataChannelPtr->onOpen([&]() {
|
|
300
287
|
if (mOnOpenCallback)
|
|
301
288
|
mOnOpenCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
|
|
302
|
-
// Check the
|
|
289
|
+
// Check the data channel is not closed
|
|
303
290
|
if(instances.find(this) == instances.end())
|
|
304
291
|
throw ThreadSafeCallback::CancelException();
|
|
305
292
|
|
|
@@ -311,6 +298,12 @@ void DataChannelWrapper::onOpen(const Napi::CallbackInfo &info)
|
|
|
311
298
|
|
|
312
299
|
void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info)
|
|
313
300
|
{
|
|
301
|
+
if (!mDataChannelPtr)
|
|
302
|
+
{
|
|
303
|
+
Napi::Error::New(info.Env(), "onClosed() called on destroyed channel").ThrowAsJavaScriptException();
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
314
307
|
Napi::Env env = info.Env();
|
|
315
308
|
int length = info.Length();
|
|
316
309
|
|
|
@@ -323,11 +316,25 @@ void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info)
|
|
|
323
316
|
// Callback
|
|
324
317
|
mOnClosedCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
|
325
318
|
|
|
326
|
-
|
|
319
|
+
mDataChannelPtr->onClosed([&]() {
|
|
320
|
+
if (mOnClosedCallback)
|
|
321
|
+
mOnClosedCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
|
|
322
|
+
// Do not check if the data channel has been closed here
|
|
323
|
+
|
|
324
|
+
// This will run in main thread and needs to construct the
|
|
325
|
+
// arguments for the call
|
|
326
|
+
args = {};
|
|
327
|
+
}); });
|
|
327
328
|
}
|
|
328
329
|
|
|
329
330
|
void DataChannelWrapper::onError(const Napi::CallbackInfo &info)
|
|
330
331
|
{
|
|
332
|
+
if (!mDataChannelPtr)
|
|
333
|
+
{
|
|
334
|
+
Napi::Error::New(info.Env(), "onError() called on destroyed channel").ThrowAsJavaScriptException();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
331
338
|
Napi::Env env = info.Env();
|
|
332
339
|
int length = info.Length();
|
|
333
340
|
|
|
@@ -340,11 +347,10 @@ void DataChannelWrapper::onError(const Napi::CallbackInfo &info)
|
|
|
340
347
|
// Callback
|
|
341
348
|
mOnErrorCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
|
342
349
|
|
|
343
|
-
mDataChannelPtr->onError([&](
|
|
344
|
-
{
|
|
350
|
+
mDataChannelPtr->onError([&](std::string error) {
|
|
345
351
|
if (mOnErrorCallback)
|
|
346
|
-
mOnErrorCallback->call([this, error](Napi::Env env, std::vector<napi_value> &args) {
|
|
347
|
-
// Check the
|
|
352
|
+
mOnErrorCallback->call([this, error = std::move(error)](Napi::Env env, std::vector<napi_value> &args) {
|
|
353
|
+
// Check the data channel is not closed
|
|
348
354
|
if(instances.find(this) == instances.end())
|
|
349
355
|
throw ThreadSafeCallback::CancelException();
|
|
350
356
|
|
|
@@ -356,6 +362,12 @@ void DataChannelWrapper::onError(const Napi::CallbackInfo &info)
|
|
|
356
362
|
|
|
357
363
|
void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info)
|
|
358
364
|
{
|
|
365
|
+
if (!mDataChannelPtr)
|
|
366
|
+
{
|
|
367
|
+
Napi::Error::New(info.Env(), "onBufferedAmountLow() called on destroyed channel").ThrowAsJavaScriptException();
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
359
371
|
Napi::Env env = info.Env();
|
|
360
372
|
int length = info.Length();
|
|
361
373
|
|
|
@@ -368,11 +380,10 @@ void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info)
|
|
|
368
380
|
// Callback
|
|
369
381
|
mOnBufferedAmountLowCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
|
370
382
|
|
|
371
|
-
mDataChannelPtr->onBufferedAmountLow([&]()
|
|
372
|
-
{
|
|
383
|
+
mDataChannelPtr->onBufferedAmountLow([&]() {
|
|
373
384
|
if (mOnBufferedAmountLowCallback)
|
|
374
385
|
mOnBufferedAmountLowCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
|
|
375
|
-
// Check the
|
|
386
|
+
// Check the data channel is not closed
|
|
376
387
|
if(instances.find(this) == instances.end())
|
|
377
388
|
throw ThreadSafeCallback::CancelException();
|
|
378
389
|
|
|
@@ -384,6 +395,12 @@ void DataChannelWrapper::onBufferedAmountLow(const Napi::CallbackInfo &info)
|
|
|
384
395
|
|
|
385
396
|
void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info)
|
|
386
397
|
{
|
|
398
|
+
if (!mDataChannelPtr)
|
|
399
|
+
{
|
|
400
|
+
Napi::Error::New(info.Env(), "onMessage() called on destroyed channel").ThrowAsJavaScriptException();
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
387
404
|
Napi::Env env = info.Env();
|
|
388
405
|
int length = info.Length();
|
|
389
406
|
|
|
@@ -396,24 +413,23 @@ void DataChannelWrapper::onMessage(const Napi::CallbackInfo &info)
|
|
|
396
413
|
// Callback
|
|
397
414
|
mOnMessageCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
|
|
398
415
|
|
|
399
|
-
mDataChannelPtr->onMessage([&](
|
|
400
|
-
{
|
|
416
|
+
mDataChannelPtr->onMessage([&](std::variant<rtc::binary, std::string> message) {
|
|
401
417
|
if (mOnMessageCallback)
|
|
402
|
-
mOnMessageCallback->call([this, message](Napi::Env env, std::vector<napi_value> &args) {
|
|
403
|
-
// Check the
|
|
418
|
+
mOnMessageCallback->call([this, message = std::move(message)](Napi::Env env, std::vector<napi_value> &args) {
|
|
419
|
+
// Check the data channel is not closed
|
|
404
420
|
if(instances.find(this) == instances.end())
|
|
405
421
|
throw ThreadSafeCallback::CancelException();
|
|
406
422
|
|
|
407
423
|
// This will run in main thread and needs to construct the
|
|
408
424
|
// arguments for the call
|
|
409
|
-
Napi::Object payload = Napi::Object::New(env);
|
|
410
425
|
if (std::holds_alternative<std::string>(message))
|
|
411
426
|
{
|
|
412
427
|
args = {Napi::String::New(env, std::get<std::string>(message))};
|
|
413
428
|
}
|
|
414
429
|
else
|
|
415
430
|
{
|
|
416
|
-
|
|
431
|
+
auto bin = std::get<rtc::binary>(std::move(message));
|
|
432
|
+
args = {Napi::Buffer<std::byte>::Copy(env, bin.data(), bin.size())};
|
|
417
433
|
}
|
|
418
434
|
}); });
|
|
419
435
|
}
|