@cjser/clean-stack 6.0.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // packages/@cjser/clean-stack/index.js
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ default: () => cleanStack
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+ var import_escape_string_regexp = __toESM(require("@cjser/escape-string-regexp"), 1);
36
+ var import_url_extras = require("@cjser/url-extras");
37
+ var extractPathRegex = /\s+at.*[(\s](.*)\)?/;
38
+ var pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;
39
+ var simplePathRegex = /^\w+\.js:\d+:\d+$/;
40
+ function cleanStack(stack, { pretty = false, basePath, pathFilter } = {}) {
41
+ var _a;
42
+ if (typeof stack !== "string") {
43
+ return void 0;
44
+ }
45
+ const basePathRegex = basePath && new RegExp(`(file://)?${(0, import_escape_string_regexp.default)(basePath.replaceAll("\\", "/"))}/?`, "g");
46
+ let homeDirectory = "";
47
+ if (pretty) {
48
+ const os = (_a = process.getBuiltinModule) == null ? void 0 : _a.call(process, "node:os");
49
+ homeDirectory = os ? os.homedir().replaceAll("\\", "/") : "";
50
+ }
51
+ return stack.replaceAll("\\", "/").split("\n").filter((line) => {
52
+ const pathMatches = line.match(extractPathRegex);
53
+ if (!(pathMatches == null ? void 0 : pathMatches[1])) {
54
+ return true;
55
+ }
56
+ const match = pathMatches[1];
57
+ if (match.includes(".app/Contents/Resources/electron.asar") || match.includes(".app/Contents/Resources/default_app.asar") || match.includes("node_modules/electron/dist/resources/electron.asar") || match.includes("node_modules/electron/dist/resources/default_app.asar")) {
58
+ return false;
59
+ }
60
+ if (simplePathRegex.test(match)) {
61
+ return true;
62
+ }
63
+ if (pathRegex.test(match)) {
64
+ return false;
65
+ }
66
+ return pathFilter ? pathFilter(match) : true;
67
+ }).filter((line) => line.trim() !== "").map((line) => {
68
+ if (basePathRegex) {
69
+ line = line.replace(basePathRegex, "");
70
+ }
71
+ if (pretty) {
72
+ line = line.replace(extractPathRegex, (m, p1) => {
73
+ let filePath = p1;
74
+ if (filePath.startsWith("file://")) {
75
+ filePath = (0, import_url_extras.fileURLToPath)(filePath);
76
+ }
77
+ if (homeDirectory) {
78
+ filePath = filePath.replace(homeDirectory, "~");
79
+ }
80
+ return m.replace(p1, filePath);
81
+ });
82
+ }
83
+ return line;
84
+ }).join("\n");
85
+ }
package/index.d.ts ADDED
@@ -0,0 +1,76 @@
1
+ export type Options = {
2
+ /**
3
+ Prettify the file paths in the stack:
4
+
5
+ - `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
6
+ - `file:///Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
7
+
8
+ When enabled, file URLs are converted to regular paths for better readability and IDE integration.
9
+
10
+ @default false
11
+ */
12
+ readonly pretty?: boolean;
13
+
14
+ /**
15
+ Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones.
16
+
17
+ Example with `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`:
18
+
19
+ `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15`
20
+ */
21
+ readonly basePath?: string;
22
+
23
+ /**
24
+ Remove the stack lines where the given function returns `false`. The function receives the path part of the stack line.
25
+
26
+ @example
27
+ ```
28
+ import cleanStack from '@cjser/clean-stack';
29
+
30
+ const error = new Error('Missing unicorn');
31
+
32
+ console.log(cleanStack(error.stack));
33
+ // Error: Missing unicorn
34
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
35
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/omit-me.js:1:16)
36
+
37
+ const pathFilter = path => !/omit-me/.test(path);
38
+
39
+ console.log(cleanStack(error.stack, {pathFilter}));
40
+ // Error: Missing unicorn
41
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
42
+ ```
43
+ */
44
+ readonly pathFilter?: (path: string) => boolean;
45
+ };
46
+
47
+ /**
48
+ Clean up error stack traces. Removes the mostly unhelpful internal Node.js entries.
49
+
50
+ @param stack - The `stack` property of an `Error`.
51
+ @returns The cleaned stack or `undefined` if the given `stack` is `undefined`.
52
+
53
+ @example
54
+ ```
55
+ import cleanStack from '@cjser/clean-stack';
56
+
57
+ const error = new Error('Missing unicorn');
58
+
59
+ console.log(error.stack);
60
+
61
+ // Error: Missing unicorn
62
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
63
+ // at Module._compile (module.js:409:26)
64
+ // at Object.Module._extensions..js (module.js:416:10)
65
+ // at Module.load (module.js:343:32)
66
+ // at Function.Module._load (module.js:300:12)
67
+ // at Function.Module.runMain (module.js:441:10)
68
+ // at startup (node.js:139:18)
69
+
70
+ console.log(cleanStack(error.stack));
71
+
72
+ // Error: Missing unicorn
73
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
74
+ ```
75
+ */
76
+ export default function cleanStack<T extends string | undefined>(stack: T, options?: Options): T;
package/index.js ADDED
@@ -0,0 +1,81 @@
1
+ import escapeStringRegexp from '@cjser/escape-string-regexp';
2
+ import {fileURLToPath} from '@cjser/url-extras';
3
+
4
+ const extractPathRegex = /\s+at.*[(\s](.*)\)?/;
5
+ const pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;
6
+ const simplePathRegex = /^\w+\.js:\d+:\d+$/;
7
+
8
+ export default function cleanStack(stack, {pretty = false, basePath, pathFilter} = {}) {
9
+ if (typeof stack !== 'string') {
10
+ return undefined;
11
+ }
12
+
13
+ const basePathRegex = basePath && new RegExp(`(file://)?${escapeStringRegexp(basePath.replaceAll('\\', '/'))}/?`, 'g');
14
+
15
+ let homeDirectory = '';
16
+ if (pretty) {
17
+ const os = process.getBuiltinModule?.('node:os');
18
+ homeDirectory = os ? os.homedir().replaceAll('\\', '/') : '';
19
+ }
20
+
21
+ return stack.replaceAll('\\', '/')
22
+ .split('\n')
23
+ .filter(line => {
24
+ const pathMatches = line.match(extractPathRegex);
25
+ if (!pathMatches?.[1]) {
26
+ return true; // Keep lines without paths (like error messages)
27
+ }
28
+
29
+ const match = pathMatches[1];
30
+
31
+ // Filter out Electron internal paths
32
+ if (
33
+ match.includes('.app/Contents/Resources/electron.asar')
34
+ || match.includes('.app/Contents/Resources/default_app.asar')
35
+ || match.includes('node_modules/electron/dist/resources/electron.asar')
36
+ || match.includes('node_modules/electron/dist/resources/default_app.asar')
37
+ ) {
38
+ return false;
39
+ }
40
+
41
+ // Keep simple relative paths like 'foo.js:1:1'
42
+ if (simplePathRegex.test(match)) {
43
+ return true;
44
+ }
45
+
46
+ // Filter out internal Node.js paths
47
+ if (pathRegex.test(match)) {
48
+ return false;
49
+ }
50
+
51
+ // Apply user's path filter if provided
52
+ return pathFilter ? pathFilter(match) : true;
53
+ })
54
+ .filter(line => line.trim() !== '')
55
+ .map(line => {
56
+ if (basePathRegex) {
57
+ line = line.replace(basePathRegex, '');
58
+ }
59
+
60
+ if (pretty) {
61
+ line = line.replace(extractPathRegex, (m, p1) => {
62
+ let filePath = p1;
63
+
64
+ // Convert file:// URLs to regular paths first
65
+ if (filePath.startsWith('file://')) {
66
+ filePath = fileURLToPath(filePath);
67
+ }
68
+
69
+ // Then replace home directory with ~ (only if homeDirectory is not empty)
70
+ if (homeDirectory) {
71
+ filePath = filePath.replace(homeDirectory, '~');
72
+ }
73
+
74
+ return m.replace(p1, filePath);
75
+ });
76
+ }
77
+
78
+ return line;
79
+ })
80
+ .join('\n');
81
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@cjser/clean-stack",
3
+ "version": "6.0.0-cjser.2",
4
+ "description": "Clean up error stack traces",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://code.moenext.com/3rdeye/cjser.git"
9
+ },
10
+ "funding": "https://github.com/sponsors/sindresorhus",
11
+ "author": {
12
+ "name": "Sindre Sorhus",
13
+ "email": "sindresorhus@gmail.com",
14
+ "url": "https://sindresorhus.com"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ "types": "./index.d.ts",
19
+ "require": "./dist-cjser/index.cjs",
20
+ "default": "./index.js"
21
+ },
22
+ "sideEffects": false,
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "scripts": {
27
+ "test": "xo && ava"
28
+ },
29
+ "files": [
30
+ "index.js",
31
+ "index.d.ts",
32
+ "dist-cjser"
33
+ ],
34
+ "keywords": [
35
+ "clean",
36
+ "stack",
37
+ "trace",
38
+ "traces",
39
+ "error",
40
+ "electron"
41
+ ],
42
+ "dependencies": {
43
+ "@cjser/escape-string-regexp": "5.0.0-cjser.2",
44
+ "@cjser/url-extras": "0.1.0-cjser.2"
45
+ },
46
+ "devDependencies": {
47
+ "ava": "^6.4.1",
48
+ "xo": "^0.56.0"
49
+ },
50
+ "xo": {
51
+ "rules": {
52
+ "n/prefer-global/process": "off"
53
+ }
54
+ },
55
+ "types": "./index.d.ts",
56
+ "main": "./dist-cjser/index.cjs",
57
+ "cjser": {
58
+ "sourceVersion": "6.0.0",
59
+ "cjserVersion": 2,
60
+ "original": {
61
+ "name": "clean-stack",
62
+ "version": "6.0.0",
63
+ "exports": {
64
+ "types": "./index.d.ts",
65
+ "default": "./index.js"
66
+ },
67
+ "repository": "sindresorhus/clean-stack",
68
+ "dependencies": {
69
+ "escape-string-regexp": "5.0.0",
70
+ "url-extras": "^0.1.0"
71
+ },
72
+ "files": [
73
+ "index.js",
74
+ "index.d.ts"
75
+ ],
76
+ "scripts": {
77
+ "test": "xo && ava"
78
+ }
79
+ }
80
+ }
81
+ }
package/readme.md ADDED
@@ -0,0 +1,110 @@
1
+ # clean-stack
2
+
3
+ > Clean up error stack traces
4
+
5
+ Removes the mostly unhelpful internal Node.js entries.
6
+
7
+ Also works in the browser and Electron.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install clean-stack
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```js
18
+ import cleanStack from 'clean-stack';
19
+
20
+ const error = new Error('Missing unicorn');
21
+
22
+ console.log(error.stack);
23
+ /*
24
+ Error: Missing unicorn
25
+ at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
26
+ at Module._compile (module.js:409:26)
27
+ at Object.Module._extensions..js (module.js:416:10)
28
+ at Module.load (module.js:343:32)
29
+ at Function.Module._load (module.js:300:12)
30
+ at Function.Module.runMain (module.js:441:10)
31
+ at startup (node.js:139:18)
32
+ */
33
+
34
+ console.log(cleanStack(error.stack));
35
+ /*
36
+ Error: Missing unicorn
37
+ at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
38
+ */
39
+ ```
40
+
41
+ ## API
42
+
43
+ ### cleanStack(stack, options?)
44
+
45
+ Returns the cleaned stack or `undefined` if the given `stack` is `undefined`.
46
+
47
+ #### stack
48
+
49
+ Type: `string | undefined`
50
+
51
+ The `stack` property of an [`Error`](https://github.com/microsoft/TypeScript/blob/eac073894b172ec719ca7f28b0b94fc6e6e7d4cf/lib/lib.es5.d.ts#L972-L976).
52
+
53
+ #### options
54
+
55
+ Type: `object`
56
+
57
+ ##### pretty
58
+
59
+ Type: `boolean`\
60
+ Default: `false`
61
+
62
+ Prettify the file paths in the stack:
63
+
64
+ - `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
65
+ - `file:///Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
66
+
67
+ When enabled, file URLs are converted to regular paths for better readability and IDE integration.
68
+
69
+ ##### basePath
70
+
71
+ Type: `string?`
72
+
73
+ Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones. It will also transform absolute file URLs into relative paths.
74
+
75
+ Example with `'/Users/sindresorhus/dev/clean-stack'` as `basePath`:
76
+
77
+ `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15`
78
+
79
+ ##### pathFilter
80
+
81
+ Type: `(path: string) => boolean`
82
+
83
+ Remove the stack lines where the given function returns `false`. The function receives the path part of the stack line.
84
+
85
+ ```js
86
+ import cleanStack from 'clean-stack';
87
+
88
+ const error = new Error('Missing unicorn');
89
+
90
+ console.log(cleanStack(error.stack));
91
+ // Error: Missing unicorn
92
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
93
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/omit-me.js:1:16)
94
+
95
+ const pathFilter = path => !/omit-me/.test(path);
96
+
97
+ console.log(cleanStack(error.stack, {pathFilter}));
98
+ // Error: Missing unicorn
99
+ // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
100
+ ```
101
+
102
+ ## Related
103
+
104
+ - [extract-stack](https://github.com/sindresorhus/extract-stack) - Extract the actual stack of an error
105
+ - [stack-utils](https://github.com/tapjs/stack-utils) - Captures and cleans stack traces
106
+
107
+ ## cjser
108
+
109
+ This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
110
+ Original repository: https://github.com/sindresorhus/clean-stack