-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (98 loc) · 2.74 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var util = require('util');
var Dupplex = require('readable-stream').Duplex;
var WebSocket = require('ws');
var debug = require('debug')('wstream');
function Uint8ToBuffer(data) {
// check node buffer first
if (typeof Buffer != 'undefined' && data instanceof Buffer) {
return data;
} else if (data instanceof ArrayBuffer) {
return data;
} else if (data instanceof Uint8Array) {
// check node buffer first
if (typeof Buffer != 'undefined')
return new Buffer(data);
else {
var ret = new ArrayBuffer(data.length);
var viw = new Uint8Array(ret);
viw.set(data);
return ret;
}
} else {
debug('invalid Uint8ToArray:'+JSON.stringify(data));
return null;
}
}
// Stream over WebSocket
var Stream = module.exports = function(ws, options) {
if (!(this instanceof Stream))
return new Stream(ws, options);
if (!(ws instanceof WebSocket))
throw new Error('Invalid websocket');
// force writable decode string
options = options || {};
options.decodeStrings = true;
Dupplex.call(this, options);
var self = this;
// Collect data
self.ws = ws;
self.ws.on('message', function(message, flags) {
///console.log('ss message:'+JSON.stringify(message));
if (message && Buffer.isBuffer(message)) {
if (!self.push(message))
if (self.ws && self.ws.pause)
self.ws.pause();
} else if (message && message instanceof Uint8Array) {
var chunk = Uint8ToBuffer(message);
if (!self.push(chunk))
if (self.ws && self.ws.pause)
self.ws.pause();
} else {
self.emit('warn', 'Invalid ws message:'+JSON.stringify(message));
}
});
// check close
self.ws.on('close', function(){
self.push(null);
});
// check error
self.ws.on('error', function(err){
self.emit('error', 'ws error:'+JSON.stringify(err));
});
// check warn
self.ws.on('warn', function(warn){
self.emit('warn', 'ws warn:'+JSON.stringify(warn));
});
}
util.inherits(Stream, Dupplex);
// Duplex implementation
Stream.prototype._read = function(size) {
var self = this;
try {
if (self.ws && self.ws.resume && (self.ws.readyState == WebSocket.OPEN))
self.ws.resume();
} catch (e) {
}
}
Stream.prototype._write = function(chunk, encoding, callback) {
var self = this;
///console.log('ss write:'+JSON.stringify(chunk));
if (chunk instanceof Buffer) {
if (self.ws && self.ws.send)
self.ws.send(chunk, callback);
} else {
self.emit('warn', 'Invalid write buffer:'+JSON.stringify(chunk));
callback('Invalid write buffer:'+JSON.stringify(chunk));
}
}
// Expose ws close,terminate
Stream.prototype.close = function() {
var self = this;
if (self.ws && self.ws.close)
self.ws.close();
}
Stream.prototype.terminate = function() {
var self = this;
if (self.ws && self.ws.terminate)
self.ws.terminate();
}