UNPKG

1.3 kBJavaScriptView Raw
1"use strict";
2
3const {
4 sep
5} = require(`path`); // Removes all user paths
6
7
8const regexpEscape = str => str.replace(/[-[/{}()*+?.\\^$|]/g, `\\$&`);
9
10const cleanPaths = (str, separator = sep) => {
11 const stack = process.cwd().split(separator);
12
13 while (stack.length > 1) {
14 const currentPath = stack.join(separator);
15 const currentRegex = new RegExp(regexpEscape(currentPath), `g`);
16 str = str.replace(currentRegex, `$SNIP`);
17 const currentPath2 = stack.join(separator + separator);
18 const currentRegex2 = new RegExp(regexpEscape(currentPath2), `g`);
19 str = str.replace(currentRegex2, `$SNIP`);
20 stack.pop();
21 }
22
23 return str;
24}; // Takes an Error and returns a sanitized JSON String
25
26
27const sanitizeError = (error, pathSeparator = sep) => {
28 // Convert Buffers to Strings
29 if (error.stderr) error.stderr = String(error.stderr);
30 if (error.stdout) error.stdout = String(error.stdout) // Remove sensitive and useless keys
31 ;
32 [`envPairs`, `options`, `output`].forEach(key => delete error[key]); // Hack because Node
33
34 error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));
35 const errorString = JSON.stringify(error); // Removes all user paths
36
37 return cleanPaths(errorString, pathSeparator);
38};
39
40module.exports = {
41 sanitizeError,
42 cleanPaths
43};
\No newline at end of file