-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (131 loc) · 3.7 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Influx stream processor.
*/
module.exports = class Influx {
/**
* Creates a new Influx instance.
*
* @param {NodeJS.ReadStream} stream
* @param {Object} options
*/
constructor(stream, options) {
this.rest = '';
this.buf = [];
this.eof = false;
// Merge options
this.options = {
charset: 'utf-8',
newline: '\n',
trim: false,
chunkSize: 512,
callback: null,
...(options || {}),
};
stream.on('end', () => {
// Process the rest
this._processLine(this.rest);
// Set end of file flag
this.eof = true;
});
stream.on('readable', () => {
let chunk;
// Process chunks till there's nothing left
while ((chunk = stream.read(this.options.chunkSize)) !== null) {
this._processChunk(chunk);
}
});
}
/**
* Reads the next line, or `undefined` if the buffer is empty.
* This does not block, but also does not indicate EOF.
*
* @returns {string|undefined}
*/
next() {
return this.buf.shift();
}
nextAsync() {
// Define limits
const maxSpinCount = 4096;
const maxTimeout = 32;
return new Promise(resolve => {
// Initialize state
let spinCount = 0;
let timeout = 1;
// Spin until
// - a line arrives in the buffer
// - or EOF happens
// - or spin count > maxSpinCount
while (this.buf.length == 0 && !this.eof && ++spinCount < maxSpinCount) {
// spin
}
// Lines in buffer
if (this.buf.length != 0) {
return resolve(this.next());
}
// EOF
if (this.eof) {
return resolve(null);
}
// Polling function
const fn = () => {
// Lines in buffer
if (this.buf.length != 0) {
return resolve(this.next());
}
// EOF
else if (this.eof) {
return resolve(null);
}
// Increase timeout
timeout = Math.min(maxTimeout, timeout * 2);
// Defer next check
setTimeout(fn, timeout);
}
// Defer next check
setTimeout(fn, timeout);
});
}
_processLine(_line) {
let line = this.options.trim ? _line.trim() : _line;
if (this.options.callback !== null) {
this.options.callback(line);
} else {
this.buf.push(line);
}
}
_processChunk(chunk) {
// Process input
let data = this.buf + chunk.toString(this.options.charset);
let lines = data.split(this.options.newline);
// Iterate over all lines except for the last one
while (lines.length > 1) {
// Process the line
let line = lines.shift();
this._processLine(line);
}
// Remember the rest
this.rest = lines.shift();
}
/**
* Creates a new Influx instance from stdin.
*
* @param {Object} options
*
* @returns {Influx}
*/
static stdin(options) {
return new Influx(process.stdin, options);
}
/**
* Creates a new Influx instance from a stream.
*
* @param {NodeJS.ReadStream} stream
* @param {Object} options
*
* @returns {Influx}
*/
static open(stream, options) {
return new Influx(stream, options);
}
}