-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
132 lines (125 loc) · 3.22 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var util = require('util');
var color = require('ansi-color').set;
module.exports = function(backend, options) {
if (!options) options = {};
backend.use('receive', function(request, next) {
onReceive(request.agent, request.data, options);
next();
});
backend.on('send', function(agent, message) {
onSend(agent, message, options);
});
};
function log(value) {
process.stdout.write(value + ' ');
}
function clientName(agent) {
return (agent.stream.isServer) ? 'Server Client' : 'Remote Client';
}
function getId(agent, message, options) {
var id = agent.clientId;
if (options.getId) id += ' ' + options.getId(agent, message);
return id;
}
function onReceive(agent, message, options) {
if (!agent || !message) return;
try {
var id = getId(agent, message, options);
logHeader(color(clientName(agent) + ' -> Backend ' + id, 'magenta'));
var dataColor = 'green';
onMessage(message, dataColor);
} catch (err) {
console.warn('Logger error', err);
}
}
function onSend(agent, message, options) {
if (!agent || !message) return;
try {
var id = getId(agent, message, options);
logHeader(color('Backend -> ' + clientName(agent) + ' ' + id, 'blue'));
var dataColor = 'white';
onMessage(message, dataColor);
} catch (err) {
console.warn('Logger error', err);
}
}
function onMessage(message, dataColor) {
switch (message.a) {
case 'qf':
logAction('query fetch ' + message.id);
logQuery(message);
break;
case 'qs':
logAction('query subscribe ' + message.id);
logQuery(message);
break;
case 'qu':
logAction('query unsubscribe ' + message.id);
break;
case 'bf':
case 'f':
logAction('doc fetch');
logDocs(message);
break;
case 'bs':
case 's':
logAction('doc subscribe');
logDocs(message);
break;
case 'bu':
case 'u':
logAction('doc unsubscribe');
logDocs(message);
break;
case 'op':
logAction('op');
logOp(message, dataColor);
break;
case 'init':
logAction('init');
break;
}
logDiff(message, dataColor);
logError(message);
process.stdout.write('\n');
}
function logHeader(value) {
var time = new Date().toISOString().replace('T', ' ').slice(0, 19) + 'Z';
log(color(time, 'white'));
log(value);
}
function logAction(value) {
log(color(value, 'yellow'));
}
function logQuery(message) {
if (message.q == null) return;
var query = util.inspect(message.q, {depth: null});
log(color(message.c + ' ' + query, 'cyan'));
}
function logDocs(message) {
var docs;
if (message.b) {
if (Array.isArray(message.b)) {
docs = message.b.join(',');
} else {
docs = Object.keys(message.b).join(',');
}
} else {
docs = message.d;
}
log(color(message.c + ' ' + docs, 'cyan'));
}
function logOp(message, dataColor) {
var op = util.inspect(message, {depth: null});
log(color(op, dataColor));
}
function logDiff(message, dataColor) {
if (!message.diff) return;
var diff = util.inspect(message.diff, {depth: 2});
log(color(diff, dataColor));
}
function logError(message) {
if (!message.error) return;
var error = util.inspect(message, {depth: null});
log(color('Error: ' + error, 'red'));
}