node-datachannel 0.6.0 → 0.7.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.
- package/CMakeLists.txt +1 -1
- package/jest.config.cjs +7 -0
- package/lib/index.cjs +142 -0
- package/lib/index.d.ts +0 -1
- package/package.json +13 -5
- package/polyfill/RTCPeerConnection.js +0 -1
- package/polyfill/polyfill.cjs +1069 -0
- package/src/data-channel-wrapper.cpp +15 -1
- package/src/data-channel-wrapper.h +5 -1
- package/src/media-track-wrapper.cpp +25 -5
- package/src/media-track-wrapper.h +7 -3
- package/src/peer-connection-wrapper.cpp +21 -27
- package/src/peer-connection-wrapper.h +2 -5
- package/src/rtc-wrapper.cpp +5 -2
- package/src/thread-safe-callback.cpp +6 -2
- package/src/thread-safe-callback.h +3 -1
- package/src/web-socket-wrapper.cpp +16 -3
- package/src/web-socket-wrapper.h +6 -2
- package/test/jest-tests/basic.test.js +37 -0
- package/test/jest-tests/multiple-run.test.js +73 -0
- package/test/jest-tests/p2p.test.js +99 -0
- package/test/jest-tests/streams.test.js +48 -0
- package/test/jest-tests/websocket.test.js +69 -0
- package/test/test.js +0 -252
package/CMakeLists.txt
CHANGED
package/jest.config.cjs
ADDED
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var module$1 = require('module');
|
|
6
|
+
var stream = require('stream');
|
|
7
|
+
|
|
8
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
|
+
/**
|
|
10
|
+
* Turns a node-datachannel DataChannel into a real Node.js stream, complete with buffering,
|
|
11
|
+
* backpressure (up to a point - if the buffer fills up, messages are dropped), and
|
|
12
|
+
* support for piping data elsewhere.
|
|
13
|
+
*
|
|
14
|
+
* Read & written data may be either UTF-8 strings or Buffers - this difference exists at
|
|
15
|
+
* the protocol level, and is preserved here throughout.
|
|
16
|
+
*/
|
|
17
|
+
class DataChannelStream extends stream.Duplex {
|
|
18
|
+
constructor(rawChannel, streamOptions) {
|
|
19
|
+
super({
|
|
20
|
+
allowHalfOpen: false, // Default to autoclose on end().
|
|
21
|
+
...streamOptions,
|
|
22
|
+
objectMode: true, // Preserve the string/buffer distinction (WebRTC treats them differently)
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
this._rawChannel = rawChannel;
|
|
26
|
+
this._readActive = true;
|
|
27
|
+
|
|
28
|
+
rawChannel.onMessage((msg) => {
|
|
29
|
+
if (!this._readActive) return; // If the buffer is full, drop messages.
|
|
30
|
+
|
|
31
|
+
// If the push is rejected, we pause reading until the next call to _read().
|
|
32
|
+
this._readActive = this.push(msg);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// When the DataChannel closes, the readable & writable ends close
|
|
36
|
+
rawChannel.onClosed(() => {
|
|
37
|
+
this.push(null);
|
|
38
|
+
this.destroy();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
rawChannel.onError((errMsg) => {
|
|
42
|
+
this.destroy(new Error(`DataChannel error: ${errMsg}`));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Buffer all writes until the DataChannel opens
|
|
46
|
+
if (!rawChannel.isOpen()) {
|
|
47
|
+
this.cork();
|
|
48
|
+
rawChannel.onOpen(() => this.uncork());
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_read() {
|
|
53
|
+
// Stop dropping messages, if the buffer filling up meant we were doing so before.
|
|
54
|
+
this._readActive = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_write(chunk, encoding, callback) {
|
|
58
|
+
let sentOk;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
if (Buffer.isBuffer(chunk)) {
|
|
62
|
+
sentOk = this._rawChannel.sendMessageBinary(chunk);
|
|
63
|
+
} else if (typeof chunk === 'string') {
|
|
64
|
+
sentOk = this._rawChannel.sendMessage(chunk);
|
|
65
|
+
} else {
|
|
66
|
+
const typeName = chunk.constructor.name || typeof chunk;
|
|
67
|
+
throw new Error(`Cannot write ${typeName} to DataChannel stream`);
|
|
68
|
+
}
|
|
69
|
+
} catch (err) {
|
|
70
|
+
return callback(err);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (sentOk) {
|
|
74
|
+
callback(null);
|
|
75
|
+
} else {
|
|
76
|
+
callback(new Error('Failed to write to DataChannel'));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_final(callback) {
|
|
81
|
+
if (!this.allowHalfOpen) this.destroy();
|
|
82
|
+
callback(null);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_destroy(maybeErr, callback) {
|
|
86
|
+
// When the stream is destroyed, we close the DataChannel.
|
|
87
|
+
this._rawChannel.close();
|
|
88
|
+
callback(maybeErr);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get label() {
|
|
92
|
+
return this._rawChannel.getLabel();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get id() {
|
|
96
|
+
return this._rawChannel.getId();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get protocol() {
|
|
100
|
+
return this._rawChannel.getProtocol();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
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)));
|
|
106
|
+
|
|
107
|
+
const nodeDataChannel = require$1('../build/Release/node_datachannel.node');
|
|
108
|
+
|
|
109
|
+
const {
|
|
110
|
+
initLogger,
|
|
111
|
+
cleanup,
|
|
112
|
+
preload,
|
|
113
|
+
setSctpSettings,
|
|
114
|
+
RtcpReceivingSession,
|
|
115
|
+
Track,
|
|
116
|
+
Video,
|
|
117
|
+
Audio,
|
|
118
|
+
DataChannel,
|
|
119
|
+
PeerConnection,
|
|
120
|
+
WebSocket,
|
|
121
|
+
WebSocketServer,
|
|
122
|
+
} = nodeDataChannel;
|
|
123
|
+
|
|
124
|
+
var index = {
|
|
125
|
+
...nodeDataChannel,
|
|
126
|
+
DataChannelStream,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
exports.Audio = Audio;
|
|
130
|
+
exports.DataChannel = DataChannel;
|
|
131
|
+
exports.DataChannelStream = DataChannelStream;
|
|
132
|
+
exports.PeerConnection = PeerConnection;
|
|
133
|
+
exports.RtcpReceivingSession = RtcpReceivingSession;
|
|
134
|
+
exports.Track = Track;
|
|
135
|
+
exports.Video = Video;
|
|
136
|
+
exports.WebSocket = WebSocket;
|
|
137
|
+
exports.WebSocketServer = WebSocketServer;
|
|
138
|
+
exports.cleanup = cleanup;
|
|
139
|
+
exports.default = index;
|
|
140
|
+
exports.initLogger = initLogger;
|
|
141
|
+
exports.preload = preload;
|
|
142
|
+
exports.setSctpSettings = setSctpSettings;
|
package/lib/index.d.ts
CHANGED
|
@@ -275,7 +275,6 @@ export class WebSocketServer {
|
|
|
275
275
|
export class PeerConnection {
|
|
276
276
|
constructor(peerName: string, config: RtcConfig);
|
|
277
277
|
close(): void;
|
|
278
|
-
destroy(): void;
|
|
279
278
|
setLocalDescription(type?: DescriptionType): void;
|
|
280
279
|
setRemoteDescription(sdp: string, type: DescriptionType): void;
|
|
281
280
|
localDescription(): { type: string; sdp: string } | null;
|
package/package.json
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-datachannel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "libdatachannel node bindings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
".":
|
|
8
|
-
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./lib/index.js",
|
|
9
|
+
"require": "./lib/index.cjs"
|
|
10
|
+
},
|
|
11
|
+
"./polyfill": {
|
|
12
|
+
"import": "./polyfill/index.js",
|
|
13
|
+
"require": "./polyfill/polyfill.cjs"
|
|
14
|
+
}
|
|
9
15
|
},
|
|
10
16
|
"typings": "lib/index.d.ts",
|
|
11
17
|
"engines": {
|
|
@@ -27,7 +33,8 @@
|
|
|
27
33
|
"lint": "eslint lib/**/* test/**/*",
|
|
28
34
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
29
35
|
"wpt-test": "node test/wpt.js",
|
|
30
|
-
"test-types": "tsc"
|
|
36
|
+
"test-types": "tsc",
|
|
37
|
+
"prepublishOnly": "rollup lib/index.js --file lib/index.cjs --format cjs && rollup polyfill/index.js --file polyfill/polyfill.cjs --format cjs"
|
|
31
38
|
},
|
|
32
39
|
"binary": {
|
|
33
40
|
"napi_versions": [
|
|
@@ -78,6 +85,7 @@
|
|
|
78
85
|
},
|
|
79
86
|
"dependencies": {
|
|
80
87
|
"node-domexception": "^2.0.1",
|
|
81
|
-
"prebuild-install": "^7.0.1"
|
|
88
|
+
"prebuild-install": "^7.0.1",
|
|
89
|
+
"rollup": "^4.14.1"
|
|
82
90
|
}
|
|
83
91
|
}
|