-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
var stream = require('stream');
var toBuffer = require('typedarray-to-buffer');
var util = require('util');
var debug = require('debug')('rtcstream')
function RtcStream(id, channel) {
if (!(this instanceof RtcStream)) {
return new RtcStream(id, channel);
}
stream.Duplex.call(this);
this.id = id;
this.channel = channel;
this.channel.binaryType = 'arraybuffer';
this.channel.addEventListener('message', this.handleRtcMessage.bind(this));
this.channel.addEventListener('close', this.handleRtcClose.bind(this));
this.channel.addEventListener('open', this.handleRtcOpen.bind(this));
this.channel.addEventListener('error', this.handleRtcError.bind(this));
}
module.exports = RtcStream;
util.inherits(RtcStream, stream.Duplex);
// stream implementation
RtcStream.prototype._read = function() {
// noop, we just push data in handleRtcMessage
}
RtcStream.prototype._write = function(chunk, encoding, next) {
try {
if (Buffer.isBuffer(chunk)) {
this.channel.send(new Uint8Array(chunk).buffer);
} else {
this.channel.send(chunk);
}
} finally {
next();
}
}
// data channel events
RtcStream.prototype.handleRtcMessage = function(event) {
var data = event.data ? event.data : event;
this.push(toBuffer(data));
this.emit('readable');
};
RtcStream.prototype.handleRtcClose = function(event) {
this.push(null); // push null to close the read stream
};
RtcStream.prototype.handleRtcOpen = function(event) {
debug('open event: %s', this.id);
};
RtcStream.prototype.handleRtcError = function(err) {
debug('error event: %s %s', this.id, err);
};