node-datachannel
Version:
WebRTC For Node.js and Electron. libdatachannel node bindings.
102 lines (95 loc) • 2.96 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var stream = require('stream');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var stream__namespace = /*#__PURE__*/_interopNamespaceDefault(stream);
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
class DataChannelStream extends stream__namespace.Duplex {
constructor(rawChannel, streamOptions) {
super({
allowHalfOpen: false,
// Default to autoclose on end().
...streamOptions,
objectMode: true
// Preserve the string/buffer distinction (WebRTC treats them differently)
});
__publicField(this, "_rawChannel");
__publicField(this, "_readActive");
this._rawChannel = rawChannel;
this._readActive = true;
rawChannel.onMessage((msg) => {
if (!this._readActive) return;
this._readActive = this.push(msg);
});
rawChannel.onClosed(() => {
this.push(null);
this.destroy();
});
rawChannel.onError((errMsg) => {
this.destroy(new Error(`DataChannel error: ${errMsg}`));
});
if (!rawChannel.isOpen()) {
this.cork();
rawChannel.onOpen(() => this.uncork());
}
}
_read() {
this._readActive = true;
}
_write(chunk, _encoding, callback) {
let sentOk;
try {
if (Buffer.isBuffer(chunk)) {
sentOk = this._rawChannel.sendMessageBinary(chunk);
} else if (typeof chunk === "string") {
sentOk = this._rawChannel.sendMessage(chunk);
} else {
const typeName = chunk.constructor.name || typeof chunk;
throw new Error(`Cannot write ${typeName} to DataChannel stream`);
}
} catch (err) {
return callback(err);
}
if (sentOk) {
callback(null);
} else {
callback(new Error("Failed to write to DataChannel"));
}
}
_final(callback) {
if (!this.allowHalfOpen) this.destroy();
callback(null);
}
_destroy(maybeErr, callback) {
this._rawChannel.close();
callback(maybeErr);
}
get label() {
return this._rawChannel.getLabel();
}
get id() {
return this._rawChannel.getId();
}
get protocol() {
return this._rawChannel.getProtocol();
}
}
exports.default = DataChannelStream;
//# sourceMappingURL=datachannel-stream.cjs.map
;