forked from pinojs/pino-eventhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pino-eventhub.js
125 lines (112 loc) · 3.02 KB
/
pino-eventhub.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
#! /usr/bin/env node
"use strict";
const { Writable, pipeline } = require("stream");
const split = require("split2");
const crypto = require("crypto");
const https = require("https");
const debug = require("debug")("pino-eventhub");
const socketCount = 10;
function createSignature(uri, ttl, sapk) {
const signature = uri + "\n" + ttl;
const hash = crypto
.createHmac("sha256", sapk)
.update(signature)
.digest("base64");
return encodeURIComponent(hash);
}
function pinoEventHub(opts) {
const {
host,
port,
eh,
sr,
sig,
se,
skn,
max,
["bulk-size"]: bulkSize,
} = opts;
const splitter = split(function (line) {
return line;
});
const agent = new https.Agent({
keepAlive: true,
maxSockets: max || socketCount,
});
const options = {
method: "POST",
host: host.slice(8), // remove 'https://'
port: port,
agent: agent,
path: "/" + eh + "/messages?timeout=60&api-version=2014-01",
headers: {
Authorization:
"SharedAccessSignature sr=" +
sr +
"&sig=" +
sig +
"&se=" +
se +
"&skn=" +
skn,
"Content-Type": "application/atom+xml;type=entry;charset=utf-8",
},
};
const bulkHeaders = Object.assign({}, options.headers, {
"Content-Type": "application/vnd.microsoft.servicebus.json",
});
const bulkOptions = Object.assign({}, options, { headers: bulkHeaders });
function callback(done) {
return function inner(response) {
debug("response.statusCode =", response.statusCode);
debug("response.statusMessage =", response.statusMessage);
if (response.statusCode !== 201) {
splitter.emit("error", new Error(response.statusCode));
}
response.on("data", function (data) {
debug("data =", data.toString());
});
response.on("end", function () {
debug("call completed");
done();
});
};
}
const writable = new Writable({
objectMode: true,
highWaterMark: bulkSize || 500,
writev: function (blocks, done) {
// https://docs.microsoft.com/en-us/rest/api/eventhub/send-batch-events
const events = blocks
.map((block) => block.chunk)
.map((line) => ({ Body: line }));
const body = JSON.stringify(events);
debug(`body =`, body);
const req = https.request(bulkOptions, callback(done));
req.on("error", (e) => {
console.error(`request error =`, e);
done();
});
req.write(body);
req.end();
},
write: function (line, enc, done) {
debug(`write: line =`, line);
debug(`write: typeof ===`, typeof line);
if (line) {
const req = https.request(options, callback(done));
req.on("error", (e) => {
console.error(`request error =`, e);
});
req.write(line);
req.end();
} else {
done();
}
},
});
pipeline(splitter, writable, () => {});
return splitter;
}
module.exports = pinoEventHub;
module.exports.createSignature = createSignature;