-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathlogtail.ts
More file actions
90 lines (84 loc) · 3.1 KB
/
logtail.ts
File metadata and controls
90 lines (84 loc) · 3.1 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
/*
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EMPTY, Observable, catchError, concatMap, of, timer } from './rxjs';
export default (getFn, interval, initialSize = 300 * 1024) => {
let range = `bytes=-${initialSize}`;
let size = 0;
let atTheEnd = false;
return timer(0, interval).pipe(
concatMap(() => {
return new Observable((observer) => {
getFn({
responseType: 'text',
headers: { range, Accept: 'text/plain' },
})
.then((response) => {
observer.next(response);
observer.complete();
})
.catch((error) => observer.error(error));
}).pipe(
catchError((error) => of({ data: '', status: error.response.status })),
);
}),
concatMap((response) => {
let initial = size === 0;
const contentLength = response.data.length;
if (response.status === 200) {
if (!initial) {
throw 'Expected 206 - Partial Content on subsequent requests.';
}
size = contentLength;
range = `bytes=${size - 1}-`;
} else if (response.status === 206) {
const contentRangeParts = response.headers['content-range'].split('/');
size = parseInt(contentRangeParts[1]);
// The end value of the range is always one byte less than the size when at the end
atTheEnd = parseInt(contentRangeParts[0].split('-')[1]) == size - 1;
range = `bytes=${size - 1}-`;
} else if (response.status === 416) {
size = 0;
range = `bytes=-${initialSize}`;
initial = true;
} else {
throw 'Unexpected response status: ' + response.status;
}
let addendum = null;
let skipped = 0;
if (initial) {
if (contentLength >= size) {
addendum = response.data;
} else {
// In case of a partial response find the first line break.
addendum = response.data.substring(response.data.indexOf('\n') + 1);
skipped = size - addendum.length;
}
} else if (response.data.length > 1) {
// Remove the first byte which has been part of the previous response.
addendum = response.data.substring(1);
}
return addendum
? of({
totalBytes: size,
skipped,
// The log file always temporarily ends with a new line until the next one is written.
// Therefore, if we're at the end of it, we drop such a new line.
addendum: atTheEnd ? addendum.trimEnd() : addendum,
})
: EMPTY;
}),
);
};