web-log-viewer 0.0.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/server.js CHANGED
@@ -24,8 +24,6 @@ var stream_utils_1 = require("./stream-utils");
24
24
  var config_1 = require("./config");
25
25
  var log_index_1 = require("./log-index");
26
26
  var path_1 = __importDefault(require("path"));
27
- var LOG_WINDOW_SIZE = 100;
28
- var clients = [];
29
27
  /**Contains all the logs that came into the server up until now */
30
28
  var logs = [];
31
29
  // setup the server
@@ -50,11 +48,15 @@ server.listen(config_1.config.port, function () {
50
48
  * pipes them to all registered clients using websockets.
51
49
  */
52
50
  var setupLogStream = function () {
53
- return ramda_1.pipe(function () { return stream_utils_1.createReadlineStream(); }, core_1.tap(function (rawMessage) {
51
+ return ramda_1.pipe(function () { return stream_utils_1.createReadlineStream(); },
52
+ // optionally log the message to the server's console
53
+ core_1.tap(function (rawMessage) {
54
54
  if (config_1.config.stdout) {
55
55
  console.log(rawMessage);
56
56
  }
57
- }), core_1.map(function (rawMessage) {
57
+ }),
58
+ // parse the message with the currently configured parser
59
+ core_1.map(function (rawMessage) {
58
60
  var data = config_1.config.parseRawMessage(rawMessage);
59
61
  var msg = {
60
62
  seq: logs.length + 1,
@@ -63,55 +65,92 @@ var setupLogStream = function () {
63
65
  };
64
66
  logs.push(msg);
65
67
  return msg;
66
- }), core_1.tap(function (rawLog) {
67
- var updateMsg = {
68
- type: 'update',
69
- size: logs.length,
70
- message: rawLog,
71
- };
68
+ }),
69
+ // potentially, send the message to all currently connected clients
70
+ core_1.tap(function (rawLog) {
72
71
  wss.clients.forEach(function (ws) {
73
- ws.send(encode(updateMsg));
72
+ updateWsClientStatus(ws, function (status) {
73
+ var updateMsg = {
74
+ type: 'update',
75
+ size: status.count + 1,
76
+ message: rawLog,
77
+ };
78
+ // only send the new message to the client if it passes the client's filter
79
+ var matcher = log_index_1.isIndexMatch(status.filter);
80
+ if (matcher(updateMsg.message.index)) {
81
+ ws.send(encode(__assign(__assign({}, updateMsg), { message: __assign({}, updateMsg.message) })));
82
+ return __assign(__assign({}, status), { count: updateMsg.size });
83
+ }
84
+ else {
85
+ return status;
86
+ }
87
+ });
74
88
  });
75
89
  }));
76
90
  };
91
+ /**
92
+ * Update the status associated with a particular WS client
93
+ * @param ws the WS client instance
94
+ * @param statusFn a function that receives the current status and returns the new one
95
+ */
96
+ function updateWsClientStatus(ws, statusFn) {
97
+ var currentStatus = getWsClientStatus(ws);
98
+ ws._status = statusFn(currentStatus);
99
+ }
100
+ /**
101
+ * Get the status associated with a particular wS client
102
+ */
103
+ function getWsClientStatus(ws) {
104
+ return ws._status;
105
+ }
77
106
  /**
78
107
  * Function executed every time a new WS client connects to the server.
79
108
  * This will send a `InitMessage` to that client, so it can immediately start showing to the user.
80
109
  */
81
110
  function setupNewClient(ws) {
82
- var clientId = clients.length;
83
- clients.push({ mode: 'tail' });
84
- // send a window with the last logs to newly registered clients
85
- ws.send(encode(buildTailMessage('')));
111
+ updateWsClientStatus(ws, function () { return ({ mode: 'tail', filter: '', count: 0 }); });
112
+ // when the client send a message, it means one of the following:
113
+ // - change mode to tail/static
114
+ // - specify a new filter query
115
+ // - change the offset of static mode
86
116
  ws.on('message', function (encodedMsg) {
87
117
  var msg = decode(encodedMsg);
118
+ var matcher = log_index_1.isIndexMatch(msg.filter);
119
+ var filteredLogs = logs.filter(function (l) { return matcher(l.index); });
88
120
  if (msg.mode == 'tail') {
89
- ws.send(encode(buildTailMessage(msg.filter)));
121
+ updateWsClientStatus(ws, function (currentStatus) { return (__assign(__assign({}, currentStatus), { mode: 'tail', filter: msg.filter, count: filteredLogs.length })); });
122
+ ws.send(encode(buildTailMessage(filteredLogs, msg.maxMessages)));
90
123
  }
91
124
  else if (msg.mode == 'static') {
92
- ws.send(encode(buildStaticMessage(msg.filter, msg.offsetSeq)));
125
+ updateWsClientStatus(ws, function (currentStatus) { return (__assign(__assign({}, currentStatus), { mode: 'static', offsetStart: msg.offsetStart, maxMessages: msg.maxMessages, filter: msg.filter, count: filteredLogs.length })); });
126
+ ws.send(encode(buildStaticMessage(filteredLogs, msg.offsetStart, msg.maxMessages)));
93
127
  }
94
128
  });
95
- function buildTailMessage(filter) {
96
- var matcher = log_index_1.isIndexMatch(filter);
97
- var filteredLogs = logs.filter(function (l) { return matcher(l.index); }).map(function (l, i) { return (__assign(__assign({}, l), { seq: i + 1 })); });
129
+ function buildTailMessage(filteredLogs, maxMessages) {
98
130
  return {
99
131
  type: 'init',
100
132
  mode: 'tail',
101
- size: filteredLogs.length,
102
- offsetSeq: -1,
103
- window: filteredLogs.slice(-LOG_WINDOW_SIZE),
133
+ totalSize: logs.length,
134
+ window: {
135
+ size: filteredLogs.length,
136
+ maxMessages: maxMessages,
137
+ messages: filteredLogs.slice(-maxMessages),
138
+ },
104
139
  };
105
140
  }
106
- function buildStaticMessage(filter, offsetSeq) {
107
- var matcher = log_index_1.isIndexMatch(filter);
108
- var filteredLogs = logs.filter(function (l) { return matcher(l.index); }).map(function (l, i) { return (__assign(__assign({}, l), { seq: i + 1 })); });
141
+ function buildStaticMessage(filteredLogs, offsetStart, maxMessages) {
142
+ var totalSize = logs.length;
143
+ var filteredSize = filteredLogs.length;
109
144
  return {
110
145
  type: 'init',
111
146
  mode: 'static',
112
- size: filteredLogs.length,
113
- offsetSeq: offsetSeq,
114
- window: filteredLogs.slice(Math.max(offsetSeq - LOG_WINDOW_SIZE / 2, 0), offsetSeq + LOG_WINDOW_SIZE / 2),
147
+ totalSize: totalSize,
148
+ window: {
149
+ size: filteredSize,
150
+ offsetStart: offsetStart,
151
+ maxMessages: maxMessages,
152
+ messages: filteredLogs.slice(offsetStart, offsetStart + maxMessages),
153
+ },
115
154
  };
116
155
  }
117
156
  }
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "web-log-viewer",
3
- "version": "0.0.4",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
- "files": ["build/"],
6
+ "files": [
7
+ "build/"
8
+ ],
7
9
  "scripts": {
8
- "tail": "tail -n 100000 -f nginx.log | node --inspect=0 --enable-source-maps build/server.js -k",
10
+ "tail": "tail -n 1000 -f ~/app-logs.log | node --inspect=0 --enable-source-maps build/server.js -k",
9
11
  "cat": "cat log.log | node --inspect=0 --enable-source-maps build/server.js",
10
12
  "start": "concurrently 'npm:tsc-watch' 'npm:public:dev'",
11
13
  "tsc-watch": "tsc-watch --outDir build --noClear --onSuccess 'npm run tail'",
@@ -20,7 +22,14 @@
20
22
  "web-log-viewer": "./bin/global.js"
21
23
  },
22
24
  "preferGlobal": true,
23
- "keywords": ["json-log-viewer", "json", "web-log-viewer", "logs", "log4j", "winston"],
25
+ "keywords": [
26
+ "json-log-viewer",
27
+ "json",
28
+ "web-log-viewer",
29
+ "logs",
30
+ "log4j",
31
+ "winston"
32
+ ],
24
33
  "author": "rjbma",
25
34
  "license": "ISC",
26
35
  "dependencies": {
@@ -42,6 +51,7 @@
42
51
  "@types/node": "^14.14.27",
43
52
  "@types/ramda": "^0.27.38",
44
53
  "@types/ws": "^7.4.0",
54
+ "concurrently": "^6.0.2",
45
55
  "parcel-bundler": "^1.12.4",
46
56
  "ts-node-dev": "^1.1.1",
47
57
  "tsc-watch": "^4.2.9",