forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc-bidirectional-stream.js
45 lines (40 loc) · 1.41 KB
/
grpc-bidirectional-stream.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
/**
* Pinpoint Node.js Agent
* Copyright 2021-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const log = require('../utils/logger')
const GrpcStream = require('./grpc-stream')
class GrpcBidirectionalStream {
constructor(name, client, newStream) {
this.grpcStream = new GrpcStream(name, () => {
const stream = newStream.call(client)
stream.on('data', (data) => {
if (log.isDebug() && name && data) {
log.debug(`GrpcBidirectionalStream stream data. ${name} on(data): ${data}`)
}
})
stream.on('end', () => {
if (log.isDebug() && name) {
log.debug(`GrpcBidirectionalStream side stream ended. ${name} on(end)`)
}
})
stream.on('error', (e) => {
if (log.isDebug() && e && name) {
log.debug(`GrpcBidirectionalStream stream on error. ${name} on(error): ${JSON.stringify(e)}`)
}
})
stream.on('status', (status) => {
if (log.isDebug() && status && name) {
log.debug(`GrpcBidirectionalStream stream data. ${name} on(status): ${JSON.stringify(status)}`)
}
})
return stream
})
}
write(data) {
this.grpcStream.write(data)
}
}
module.exports = GrpcBidirectionalStream