/
utils.ts
184 lines (155 loc) · 4.63 KB
/
utils.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { Stream } from 'stream';
import bunyan from 'bunyan';
import fs from 'fs-extra';
import { clone } from '../util/clone';
import { HttpError } from '../util/http/types';
import { redactedFields, sanitize } from '../util/sanitize';
import type { BunyanRecord, BunyanStream } from './types';
const excludeProps = ['pid', 'time', 'v', 'hostname'];
export class ProblemStream extends Stream {
private _problems: BunyanRecord[] = [];
readable: boolean;
writable: boolean;
constructor() {
super();
this.readable = false;
this.writable = true;
}
write(data: BunyanRecord): boolean {
const problem = { ...data };
for (const prop of excludeProps) {
delete problem[prop];
}
this._problems.push(problem);
return true;
}
getProblems(): BunyanRecord[] {
return this._problems;
}
clearProblems(): void {
this._problems = [];
}
}
const templateFields = ['prBody'];
const contentFields = [
'content',
'contents',
'packageLockParsed',
'yarnLockParsed',
];
export default function prepareError(err: Error): Record<string, unknown> {
const response: Record<string, unknown> = {
...err,
};
// Required as message is non-enumerable
if (!response.message && err.message) {
response.message = err.message;
}
// Required as stack is non-enumerable
if (!response.stack && err.stack) {
response.stack = err.stack;
}
// handle got error
if (err instanceof HttpError) {
const options: Record<string, unknown> = {
headers: clone(err.options.headers),
url: err.options.url?.toString(),
hostType: err.options.context.hostType,
};
response.options = options;
for (const k of ['username', 'password', 'method', 'http2']) {
options[k] = err.options[k];
}
// istanbul ignore else
if (err.response) {
response.response = {
statusCode: err.response?.statusCode,
statusMessage: err.response?.statusMessage,
body: clone(err.response.body),
headers: clone(err.response.headers),
httpVersion: err.response.httpVersion,
};
}
}
return response;
}
export function sanitizeValue(_value: unknown, seen = new WeakMap()): any {
let value = _value;
if (Array.isArray(value)) {
const length = value.length;
const arrayResult = Array(length);
seen.set(value, arrayResult);
for (let idx = 0; idx < length; idx += 1) {
const val = value[idx];
arrayResult[idx] = seen.has(val)
? seen.get(val)
: sanitizeValue(val, seen);
}
return arrayResult;
}
if (value instanceof Buffer) {
return '[content]';
}
if (value instanceof Error) {
value = prepareError(value);
}
const valueType = typeof value;
if (value != null && valueType !== 'function' && valueType === 'object') {
if (value instanceof Date) {
return value;
}
const objectResult: Record<string, any> = {};
seen.set(value as any, objectResult);
for (const [key, val] of Object.entries<any>(value)) {
let curValue: any;
if (!val) {
curValue = val;
} else if (redactedFields.includes(key)) {
curValue = '***********';
} else if (contentFields.includes(key)) {
curValue = '[content]';
} else if (templateFields.includes(key)) {
curValue = '[Template]';
} else if (key === 'secrets') {
curValue = {};
Object.keys(val).forEach((secretKey) => {
curValue[secretKey] = '***********';
});
} else {
curValue = seen.has(val) ? seen.get(val) : sanitizeValue(val, seen);
}
objectResult[key] = curValue;
}
return objectResult;
}
return valueType === 'string' ? sanitize(value as string) : value;
}
export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream {
if (streamConfig.type === 'rotating-file') {
throw new Error("Rotating files aren't supported");
}
const stream = streamConfig.stream as BunyanStream;
if (stream?.writable) {
const write = (chunk: BunyanRecord, enc, cb): void => {
const raw = sanitizeValue(chunk);
const result =
streamConfig.type === 'raw'
? raw
: JSON.stringify(raw, bunyan.safeCycles()).replace(/\n?$/, '\n'); // TODO #12070
stream.write(result, enc, cb);
};
return {
...streamConfig,
type: 'raw',
stream: { write },
} as bunyan.Stream;
}
if (streamConfig.path) {
const fileStream = fs.createWriteStream(streamConfig.path, {
flags: 'a',
encoding: 'utf8',
});
return withSanitizer({ ...streamConfig, stream: fileStream });
}
throw new Error("Missing 'stream' or 'path' for bunyan stream");
}