-
Notifications
You must be signed in to change notification settings - Fork 0
/
pino-redis.js
136 lines (121 loc) · 4.09 KB
/
pino-redis.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
133
134
135
136
const Writable = require('readable-stream').Writable
const Redis = require('ioredis')
const Parse = require('fast-json-parse')
const split = require('split2')
const pump = require('pump')
const zlib = require('zlib')
const minimist = require('minimist')
const fs = require('fs')
const path = require('path')
function pinoRedis(opts) {
console.log('opts', opts)
const splitter = split(function(line) {
const parsed = new Parse(line)
if (parsed.err) {
this.emit('unknown', line, parsed.err)
return
}
return parsed.value
})
const redis = new Redis(opts.connectionUrl)
const writable = new Writable({
objectMode: true,
writev(chunks, cb) {
const pipeline = redis.pipeline()
chunks.forEach(item => {
const body = item.chunk
if (!body.key) throw new Error('key is required')
const ttl = body.ttl || 60
const gzip = !!body.gzip
const value = JSON.stringify(body)
const redisKey = `${body.level}:${body.key}`
if (gzip) {
zlib.gzip(value, (c_err, buffer) => {
if (!c_err) {
pipeline.setex(redisKey, ttl, buffer.toString('binary'))
} else {
splitter.emit('insertError', c_err)
}
})
} else {
pipeline.setex(redisKey, ttl, value)
}
pipeline.exec((err, result) => {
if (!err) {
result.forEach(res => {
if (!res[0]) {
splitter.emit('insert', body)
} else {
splitter.emit('insertError', res)
}
})
} else {
splitter.emit('insertError', err)
}
cb()
})
})
},
write(body, enc, cb) {
if (!body.key) throw new Error('key is required')
const ttl = body.ttl || 60
const gzip = !!body.gzip
const value = JSON.stringify(body)
const redisKey = `${body.level}:${body.key}`
if (gzip) {
zlib.gzip(value, (c_err, buffer) => {
if (!c_err) {
redis.setex(redisKey, ttl, buffer.toString('binary'), (err, result) => {
if (!err) {
splitter.emit('insert', body)
} else {
splitter.emit('insertError', err)
}
cb()
}
)
} else {
splitter.emit('insertError', c_err)
}
})
} else {
redis.setex(redisKey, ttl, value, (err, result) => {
if (!err) {
splitter.emit('insert', body)
} else {
splitter.emit('insertError', err)
}
cb()
})
}
}
})
pump(splitter, writable)
return splitter
}
module.exports = pinoRedis
function start(opts) {
if (opts.help) {
console.log(fs.readFileSync(path.join(__dirname, './options.txt'), 'utf8'))
return
}
if (opts.version) {
console.log('pino-redis', require('./package.json').version)
return
}
pump(process.stdin, pinoRedis(opts))
}
if (require.main === module) {
start(
minimist(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h',
connectionUrl: 'U'
},
default: {
connectionUrl: 'redis://:@localhost:6379'
}
})
)
}