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 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.6.0)
5
+ project(node_datachannel VERSION 0.7.0)
6
6
 
7
7
  # -Dnapi_build_version=8
8
8
  add_definitions(-DNAPI_VERSION=8)
@@ -0,0 +1,7 @@
1
+ /** @type {import('jest').Config} */
2
+ const config = {
3
+ verbose: true,
4
+ testPathIgnorePatterns: ['<rootDir>/node_modules/', 'multiple-run.test'],
5
+ };
6
+
7
+ module.exports = config;
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.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "libdatachannel node bindings",
5
5
  "type": "module",
6
6
  "exports": {
7
- ".": "./lib/index.js",
8
- "./polyfill": "./polyfill/index.js"
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
  }
@@ -222,7 +222,6 @@ export default class _RTCPeerConnection extends EventTarget {
222
222
  });
223
223
 
224
224
  this.#peerConnection.close();
225
- this.#peerConnection.destroy();
226
225
  }
227
226
 
228
227
  createAnswer() {