forked from kilianc/rtail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtail-client.js
More file actions
executable file
·156 lines (140 loc) · 3.64 KB
/
rtail-client.js
File metadata and controls
executable file
·156 lines (140 loc) · 3.64 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
#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"
/*!
* rtail-client.js
* Created by Kilian Ciuffolo on Oct 26, 2014
* (c) 2014-2015
*/
'use strict'
const dgram = require('dgram')
const split = require('split')
const chrono = require('chrono-node')
const JSON5 = require('json5')
const yargs = require('yargs')
const map = require('through2-map')
const stripAnsi = require('strip-ansi')
const moniker_ = require('moniker').choose
const updateNotifier = require('update-notifier')
const pkg = require('../package')
/*!
* inform the user of updates
*/
updateNotifier({
packageName: pkg.name,
packageVersion: pkg.version
}).notify()
/*!
* parsing argv
*/
let argv = yargs
.usage('Usage: cmd | rtail [OPTIONS]')
.example('server | rtail > server.log', 'localhost + file')
.example('server | rtail --id api.domain.com', 'Name the log stream')
.example('server | rtail --host example.com', 'Sends to example.com')
.example('server | rtail --port 43567', 'Uses custom port')
.example('server | rtail --mute', 'No stdout')
.example('server | rtail --no-tty', 'Strips ansi colors')
.example('server | rtail --no-date-parse', 'Disable date parsing/stripping')
.option('host', {
alias: 'h',
type: 'string',
default: '127.0.0.1',
describe: 'The server host'
})
.option('port', {
alias: 'p',
type: 'string',
default: 9999,
describe: 'The server port'
})
.option('id', {
alias: 'name',
type: 'string',
default: function moniker() { return moniker_() } ,
describe: 'The log stream id'
})
.option('mute', {
alias: 'm',
type: 'boolean',
describe: 'Don\'t pipe stdin with stdout'
})
.option('tty', {
type: 'boolean',
default: true,
describe: 'Keeps ansi colors'
})
.option('parse-date', {
type: 'boolean',
default: true,
describe: 'Looks for dates to use as timestamp'
})
.help('help')
.version(pkg.version, 'version')
.alias('version', 'v')
.strict()
.argv
/*!
* setup pipes
*/
if (!argv.mute) {
if (!process.stdout.isTTY || !argv.tty) {
process.stdin
.pipe(map(function (chunk) {
return stripAnsi(chunk.toString('utf8'))
}))
.pipe(process.stdout)
} else {
process.stdin.pipe(process.stdout)
}
}
/*!
* initialize socket
*/
let isClosed = false
let isSending = 0
let socket = dgram.createSocket('udp4')
let baseMessage = { id: argv.id }
socket.bind(function () {
socket.setBroadcast(true)
})
/*!
* broadcast lines to browser
*/
process.stdin
.pipe(split(null, null, { trailing: false }))
.on('data', function (line) {
let timestamp = null
try {
// try to JSON parse
line = JSON5.parse(line)
} catch (err) {
// look for timestamps if not an object
timestamp = argv.parseDate ? chrono.parse(line)[0] : null
}
if (timestamp) {
// escape for regexp and remove from line
timestamp.text = timestamp.text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
line = line.replace(new RegExp(' *[^ ]?' + timestamp.text + '[^ ]? *'), '')
// use timestamp as line timestamp
baseMessage.timestamp = Date.parse(timestamp.start.date())
} else {
baseMessage.timestamp = Date.now()
}
// update default message
baseMessage.content = line
// prepare binary message
let buffer = new Buffer(JSON.stringify(baseMessage))
// set semaphore
isSending ++
socket.send(buffer, 0, buffer.length, argv.port, argv.host, function () {
isSending --
if (isClosed && !isSending) socket.close()
})
})
/*!
* drain pipe and exit
*/
process.stdin.on('end', function () {
isClosed = true
if (!isSending) socket.close()
})