This repository was archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathnet.js
More file actions
225 lines (213 loc) · 5.97 KB
/
net.js
File metadata and controls
225 lines (213 loc) · 5.97 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2015-present runtime.js project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const EventEmitter = require('events');
const Duplex = require('stream').Duplex;
const dns = require('dns');
function rmFromArrayByVal(array, val) {
const i = array.indexOf(val);
if (i !== -1) array.splice(i, 1);
}
class Server extends EventEmitter {
constructor(opts, connectionListener, runtimeServer) {
super();
this._handle = runtimeServer || new runtime.net.TCPServerSocket();
this._connections = [];
this._handle.onconnect = (runtimeSocket) => {
const socket = new Socket(runtimeSocket);
this.emit('connection', socket);
socket.once('close', () => rmFromArrayByVal(this._connections, socket));
this._connections.push(socket);
}
this._handle.onclose = () => this.emit('close');
this._handle.onerror = (err) => {
this.emit('error', err);
this.close();
}
this._handle.onlisten = () => this.emit('listening');
if (connectionListener) this.on('connection', connectionListener);
}
address() {
return {
port: this._handle.localPort,
family: 'IPv4',
address: '127.0.0.1'
}
}
close(cb) {
if (cb) this.once('close', cb);
this._handle.close();
}
get connections() {
return this._connections.length;
}
getConnections(cb) {
if (cb) cb(null, this._connections.length);
}
listen(port, hostname, backlog, callback) {
let options = {};
if (typeof hostname === 'function') {
callback = hostname;
hostname = null;
}
if (typeof backlog === 'function') {
callback = backlog;
backlog = null;
}
if (typeof port === 'object') {
options = port;
port = options.port || null;
}
if (typeof port === 'function') {
callback = port;
port = null;
}
options.port = port;
if (callback) this.once('listening', callback);
this._handle.listen(options.port);
}
// ref and unref do nothing, since runtime isn't a process
ref() {}
unref() {}
}
class Socket extends Duplex {
constructor(opts, runtimeSocket) {
super();
if (opts instanceof runtime.net.TCPSocket) {
runtimeSocket = opts;
opts = null;
}
this._handle = runtimeSocket || new runtime.net.TCPSocket();
this._handle.ondata = (u8) => this.push(new Buffer(u8));
this._handle.onopen = () => this.emit('connect');
this._handle.onend = () => this.push(null);
this._handle.onclose = () => this.emit('close', false);
this.bufferSize = 0;
this.bytesRead = 0
this.bytesWritten = 0;
}
address() {
return {
port: null,
family: null,
address: null
}
}
connect(port, host, cb) {
let opts = {};
if (typeof port === 'object') {
opts = port;
port = opts.port || 80;
host = opts.host || 'localhost';
}
if (typeof host === 'function') {
cb = host;
host = 'localhost';
}
if (!port || typeof port === 'function' || port === null) {
const err = new Error('Socket.connect: Must provide a port.');
if (cb) cb(err);
return;
}
host = host || 'localhost';
if (cb) this.once('connect', cb);
if (exports.isIP(host) !== 0) {
this.emit('lookup', null, host, exports.isIP(host));
this._handle.open(host, parseInt(port));
} else {
dns.lookup(host, (err, addr, family) => {
if (err) return this.emit('lookup', err, null, null);
this.emit('lookup', null, addr, family);
this._handle.open(addr, parseInt(port));
});
}
}
destroy() {
this._handle.close();
}
end(data, encoding) {
if (data) {
this.write(data, encoding);
}
this._handle.halfclose();
}
get localAddress() {
return this._handle.localAddress;
}
get localPort() {
return this._handle.localPort;
}
ref() {}
get remoteAddress() {
return this._handle.remoteAddress;
}
get remoteFamily() {
return 'IPv4';
}
get remotePort() {
return this._handle.remotePort;
}
setKeepAlive() {}
setNoDelay() {}
setTimeout() {}
unref() {}
_read(size) {
// can't force a read. do nothing.
}
_write(chunk, encoding, callback) {
if (!(chunk instanceof Buffer)) {
chunk = new Buffer(chunk);
}
this._handle.send(new Uint8Array(chunk));
callback(null);
}
}
exports.Server = Server;
exports.Socket = Socket;
exports.createConnection = function(port, host, cb) {
const socket = new Socket();
socket.connect(port, host, cb);
return socket;
}
exports.createServer = function(opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};
const server = new Server();
if (cb) server.on('connection', cb);
return server;
}
exports.isIPv4 = function(ip) {
var arr = ip.split('.');
if (arr.length !== 4) return false;
// check if it contains non-number characters or exceeds the maximum length:
for (var i = 0, len = arr.length; i < len; i++) if (parseInt(arr[i]) === NaN || arr[i].length > 3) return false;
return true;
}
exports.isIPv6 = function(ip) {
if (ip.length > 45) return false;
var arr = ip.split(':');
if (arr.length !== 6) return false;
// check if it contains only letters and numbers (or is empty):
for (var i = 0, len = arr.length; i < len; i++) if (arr[i].search(/[a-zA-Z0-9]*/) === -1) return false;
return true;
}
exports.isIP = function(ip) {
if (exports.isIPv4(ip)) return 4;
if (exports.isIPv6(ip)) return 6;
return 0;
}
exports.connect = exports.createConnection;