pax_global_header00006660000000000000000000000064137552145600014522gustar00rootroot0000000000000052 comment=df89ac687c4a06a7fce4a183c4b5f27a90355aa0 clean-stack-3.0.1/000077500000000000000000000000001375521456000137105ustar00rootroot00000000000000clean-stack-3.0.1/.editorconfig000066400000000000000000000002571375521456000163710ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 clean-stack-3.0.1/.gitattributes000066400000000000000000000000231375521456000165760ustar00rootroot00000000000000* text=auto eol=lf clean-stack-3.0.1/.gitignore000066400000000000000000000000271375521456000156770ustar00rootroot00000000000000node_modules yarn.lock clean-stack-3.0.1/.npmrc000066400000000000000000000000231375521456000150230ustar00rootroot00000000000000package-lock=false clean-stack-3.0.1/.travis.yml000066400000000000000000000000661375521456000160230ustar00rootroot00000000000000language: node_js node_js: - '14' - '12' - '10' clean-stack-3.0.1/index.d.ts000066400000000000000000000027171375521456000156200ustar00rootroot00000000000000declare namespace cleanStack { interface Options { /** Prettify the file paths in the stack: `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15` @default false */ readonly pretty?: boolean; /** Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones. Example with `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`: `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15` */ readonly basePath?: string; } } /** Clean up error stack traces. Removes the mostly unhelpful internal Node.js entries. @param stack - The `stack` property of an `Error`. @example ``` import cleanStack = require('clean-stack'); const error = new Error('Missing unicorn'); console.log(error.stack); // Error: Missing unicorn // at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15) // at Module._compile (module.js:409:26) // at Object.Module._extensions..js (module.js:416:10) // at Module.load (module.js:343:32) // at Function.Module._load (module.js:300:12) // at Function.Module.runMain (module.js:441:10) // at startup (node.js:139:18) console.log(cleanStack(error.stack)); // Error: Missing unicorn // at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15) ``` */ declare function cleanStack( stack: string, options?: cleanStack.Options ): string; export = cleanStack; clean-stack-3.0.1/index.js000066400000000000000000000023571375521456000153640ustar00rootroot00000000000000'use strict'; const os = require('os'); const escapeStringRegexp = require('escape-string-regexp'); const extractPathRegex = /\s+at.*[(\s](.*)\)?/; const pathRegex = /^(?:(?:(?:node|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/; const homeDir = typeof os.homedir === 'undefined' ? '' : os.homedir(); module.exports = (stack, {pretty = false, basePath} = {}) => { const basePathRegex = basePath && new RegExp(`(at | \\()${escapeStringRegexp(basePath)}`, 'g'); return stack.replace(/\\/g, '/') .split('\n') .filter(line => { const pathMatches = line.match(extractPathRegex); if (pathMatches === null || !pathMatches[1]) { return true; } const match = pathMatches[1]; // Electron if ( match.includes('.app/Contents/Resources/electron.asar') || match.includes('.app/Contents/Resources/default_app.asar') ) { return false; } return !pathRegex.test(match); }) .filter(line => line.trim() !== '') .map(line => { if (basePathRegex) { line = line.replace(basePathRegex, '$1'); } if (pretty) { line = line.replace(extractPathRegex, (m, p1) => m.replace(p1, p1.replace(homeDir, '~'))); } return line; }) .join('\n'); }; clean-stack-3.0.1/index.test-d.ts000066400000000000000000000004661375521456000165740ustar00rootroot00000000000000import {expectType} from 'tsd'; import cleanStack = require('.'); const error = new Error('Missing unicorn'); if (error.stack) { expectType(cleanStack(error.stack)); expectType(cleanStack(error.stack, {pretty: true})); expectType(cleanStack(error.stack, {basePath: __dirname})); } clean-stack-3.0.1/license000066400000000000000000000021351375521456000152560ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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. clean-stack-3.0.1/package.json000066400000000000000000000013151375521456000161760ustar00rootroot00000000000000{ "name": "clean-stack", "version": "3.0.1", "description": "Clean up error stack traces", "license": "MIT", "repository": "sindresorhus/clean-stack", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "engines": { "node": ">=10" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "clean", "stack", "trace", "traces", "error", "electron" ], "dependencies": { "escape-string-regexp": "4.0.0" }, "devDependencies": { "ava": "^2.4.0", "tsd": "^0.11.0", "xo": "^0.32.0" }, "browser": { "os": false } } clean-stack-3.0.1/readme.md000066400000000000000000000033741375521456000154760ustar00rootroot00000000000000# clean-stack [![Build Status](https://travis-ci.com/sindresorhus/clean-stack.svg?branch=master)](https://travis-ci.com/github/sindresorhus/clean-stack) > Clean up error stack traces Removes the mostly unhelpful internal Node.js entries. Also works in Electron. ## Install ``` $ npm install clean-stack ``` ## Usage ```js const cleanStack = require('clean-stack'); const error = new Error('Missing unicorn'); console.log(error.stack); /* Error: Missing unicorn at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) */ console.log(cleanStack(error.stack)); /* Error: Missing unicorn at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15) */ ``` ## API ### cleanStack(stack, options?) #### stack Type: `string` The `stack` property of an `Error`. #### options Type: `object` ##### pretty Type: `boolean`\ Default: `false` Prettify the file paths in the stack: `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15` ##### basePath Type: `string?` Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones. Example with `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`: `/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15` ## Related - [extract-stack](https://github.com/sindresorhus/extract-stack) - Extract the actual stack of an error - [stack-utils](https://github.com/tapjs/stack-utils) - Captures and cleans stack traces clean-stack-3.0.1/test.js000066400000000000000000000216271375521456000152350ustar00rootroot00000000000000import os from 'os'; import test from 'ava'; import cleanStack from '.'; test('default', t => { const pre = 'Error: foo\n at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)'; const stack = `${pre}\n at handleMessage (internal/child_process.js:695:10)\n at Pipe.channel.onread (internal/child_process.js:440:11)\n at process.emit (events.js:172:7)`; t.is(cleanStack(stack), pre); }); test('default #2', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18)`; t.is(cleanStack(stack), pre); }); test('directly executed node script', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) at node.js:968:3`; t.is(cleanStack(stack), pre); }); test('internal child_process', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at internal/child_process.js:696:12`; t.is(cleanStack(stack), pre); }); test('internal next_tick', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)`; t.is(cleanStack(stack), pre); }); test('internal various modules', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at emitOne (events.js:101:20) at process.emit (events.js:188:7) at process._fatalException (bootstrap_node.js:300:26)`; t.is(cleanStack(stack), pre); }); test('babel-polyfill', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at run (/Users/sindresorhus/dev/clean-stack/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:87:22) at /Users/sindresorhus/dev/clean-stack/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:100:28`; t.is(cleanStack(stack), pre); }); test('pirates', t => { const pre = 'Error: foo\n at Object. (/Users/sindresorhus/dev/clean-stack/unicorn.js:4:7)'; const stack = `${pre}\n at Module._compile (/Users/zavr/dev/clean-stack/node_modules/pirates/lib/index.js:83:24) at Object.newLoader [as .js] (/Users/zavr/dev/clean-stack/node_modules/pirates/lib/index.js:88:7)`; t.is(cleanStack(stack), pre); }); test('works on Windows', t => { const expected = 'Error: foo\n at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)'; const stack = `Error: foo\n at Test.fn (\\Users\\sindresorhus\\dev\\clean-stack\\test.js:6:15)\n at handleMessage (internal\\child_process.js:695:10)\n at Pipe.channel.onread (internal\\child_process.js:440:11)\n at process.emit (events.js:172:7)`; t.is(cleanStack(stack), expected); }); test('works with Electron stack traces - dev app', t => { const expected = `Error: foo at Object. (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:17:16) at Object. (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:19:3)`; const stack = `Error: foo at Object. (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:17:16) at Object. (/Users/sindresorhus/dev/electron-unhandled/fixture-rejection.js:19:3) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at loadApplicationPackage (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:283:12) at Object. (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:325:5) at Object. (/Users/sindresorhus/dev/electron-unhandled/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:361:3)`; t.is(cleanStack(stack), expected); }); test('works with Electron stack traces - built app', t => { const expected = `Error: foo at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:107:16) at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:568:3)`; const stack = `Error: foo at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:107:16) at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/app/dist/main/index.js:568:3) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/electron.asar/browser/init.js:171:8) at Object. (/Users/sindresorhus/dev/forks/kap/dist/mac/Kap.app/Contents/Resources/electron.asar/browser/init.js:173:3) at Module._compile (module.js:571:32)`; t.is(cleanStack(stack), expected); }); test('`pretty` option', t => { const stack = `Error: foo\n at Test.fn (${os.homedir()}/dev/clean-stack/test.js:6:15)\n at handleMessage (internal/child_process.js:695:10)\n at Pipe.channel.onread (internal/child_process.js:440:11)\n at process.emit (events.js:172:7)`; const expected = 'Error: foo\n at Test.fn (~/dev/clean-stack/test.js:6:15)'; t.is(cleanStack(stack, {pretty: true}), expected); }); test('`basePath` option', t => { const basePath = '/Users/foo/dev/'; const stack = `Error: with basePath at Object. (/Users/foo/dev/node_modules/foo/bar.js:1:14) at /Users/foo/dev/node_modules/foo/baz.js:1:14 at Module._compile (internal/modules/cjs/loader.js:1200:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10) at Module.load (internal/modules/cjs/loader.js:1049:32) at Function.Module._load (internal/modules/cjs/loader.js:937:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47`; const expected = 'Error: with basePath\n at Object. (node_modules/foo/bar.js:1:14)\n at node_modules/foo/baz.js:1:14'; t.is(cleanStack(stack, {basePath}), expected); }); test('`basePath` option should have precedence over `pretty` option', t => { const basePath = `${os.homedir()}/dev/`; const stack = `Error: with basePath at Object. (${os.homedir()}/dev/node_modules/foo/bar.js:1:14) at Module._compile (internal/modules/cjs/loader.js:1200:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10) at Module.load (internal/modules/cjs/loader.js:1049:32) at Function.Module._load (internal/modules/cjs/loader.js:937:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47`; const expected = 'Error: with basePath\n at Object. (node_modules/foo/bar.js:1:14)'; t.is(cleanStack(stack, {basePath, pretty: true}), expected); }); test('new stack format on Node.js 15 and later', t => { const stack = `Error at B (/home/fengkx/projects/test/stack.js:5:19) at A (/home/fengkx/projects/test/stack.js:7:9) at Object. (/home/fengkx/projects/test/stack.js:14:1) at Module._compile (node:internal/modules/cjs/loader:1099:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) at node:internal/main/run_main_module:17:47`; const expected = `Error at B (/home/fengkx/projects/test/stack.js:5:19) at A (/home/fengkx/projects/test/stack.js:7:9) at Object. (/home/fengkx/projects/test/stack.js:14:1)`; t.is(cleanStack(stack), expected); });