forked from observing/thor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjolnir.js
116 lines (95 loc) · 2.72 KB
/
mjolnir.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
'use strict';
var Socket = require('ws')
, connections = {};
//
// Get the session document that is used to generate the data.
//
var session = require(process.argv[2]);
//
// WebSocket connection details.
//
var masked = process.argv[4] === 'true'
, binary = process.argv[5] === 'true'
, protocol = +process.argv[3] || 13;
process.on('message', function message(task) {
var now = Date.now();
//
// Write a new message to the socket. The message should have a size of x
//
if ('write' in task) {
Object.keys(connections).forEach(function write(id) {
write(connections[id], task, id);
});
}
//
// Shut down every single socket.
//
if (task.shutdown) {
Object.keys(connections).forEach(function shutdown(id) {
connections[id].close();
});
}
// End of the line, we are gonna start generating new connections.
if (!task.url) return;
var socket = new Socket(task.url, {
protocolVersion: protocol
});
socket.on('open', function open() {
process.send({ type: 'open', duration: Date.now() - now, id: task.id });
write(socket, task, task.id);
// As the `close` event is fired after the internal `_socket` is cleaned up
// we need to do some hacky shit in order to tack the bytes send.
});
socket.on('message', function message(data) {
process.send({
type: 'message', latency: Date.now() - socket.last,
id: task.id
});
// Only write as long as we are allowed to send messages
if (--task.messages) {
write(socket, task, task.id);
} else {
socket.close();
}
});
socket.on('close', function close() {
var internal = socket._socket || {};
process.send({
type: 'close', id: task.id,
read: internal.bytesRead || 0,
send: internal.bytesWritten || 0
});
});
socket.on('error', function error(err) {
process.send({ type: 'error', message: err.message, id: task.id });
socket.close();
delete connections[task.id];
});
// Adding a new socket to our socket collection.
connections[task.id] = socket;
});
/**
* Helper function from writing messages to the socket.
*
* @param {WebSocket} socket WebSocket connection we should write to
* @param {Object} task The given task
* @param {String} id
* @param {Function} fn The callback
* @api private
*/
function write(socket, task, id, fn) {
var start = socket.last = Date.now();
session[binary ? 'binary' : 'utf8'](task.size, function message(err, data) {
socket.send(data, {
binary: binary,
mask: masked
}, function sending(err) {
if (err) {
process.send({ type: 'error', message: err.message });
socket.close();
delete connections[id];
}
if (fn) fn(err);
});
});
}