You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 4.1 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # stack-utils
  2. > Captures and cleans stack traces.
  3. [![Linux Build](https://travis-ci.org/tapjs/stack-utils.svg?branch=master)](https://travis-ci.org/tapjs/stack-utils) [![Build status](https://ci.appveyor.com/api/projects/status/fb9i157knoixe3iq/branch/master?svg=true)](https://ci.appveyor.com/project/jamestalmage/stack-utils-oiw96/branch/master) [![Coverage](https://coveralls.io/repos/tapjs/stack-utils/badge.svg?branch=master&service=github)](https://coveralls.io/github/tapjs/stack-utils?branch=master)
  4. Extracted from `lib/stack.js` in the [`node-tap` project](https://github.com/tapjs/node-tap)
  5. ## Install
  6. ```
  7. $ npm install --save stack-utils
  8. ```
  9. ## Usage
  10. ```js
  11. const StackUtils = require('stack-utils');
  12. const stack = new StackUtils({cwd: process.cwd(), internals: StackUtils.nodeInternals()});
  13. console.log(stack.clean(new Error().stack));
  14. // outputs a beautified stack trace
  15. ```
  16. ## API
  17. ### new StackUtils([options])
  18. Creates a new `stackUtils` instance.
  19. #### options
  20. ##### internals
  21. Type: `array` of `RegularExpression`s
  22. A set of regular expressions that match internal stack stack trace lines which should be culled from the stack trace.
  23. `StackUtils.nodeInternals()` returns a relatively set of sensible defaults for use on the node platform.
  24. ##### cwd
  25. Type: `string`
  26. The path to the current working directory. File names in the stack trace will be shown relative to this directory.
  27. ##### wrapCallSite
  28. Type: `function(CallSite)`
  29. A mapping function for manipulating CallSites before processing. The first argument is a CallSite instance, and the function should return a modified CallSite. This is useful for providing source map support.
  30. ### StackUtils.nodeInternals()
  31. Returns an array of regular expressions that be used to cull lines from the stack trace that reference common Node.js internal files.
  32. ### stackUtils.clean(stack)
  33. Cleans up a stack trace by deleting any lines that match the `internals` passed to the constructor, and shortening file names relative to `cwd`.
  34. Returns a `string` with the cleaned up stack (always terminated with a `\n` newline character).
  35. #### stack
  36. *Required*
  37. Type: `string` or an `array` of `string`s
  38. ### stackUtils.capture([limit], [startStackFunction])
  39. Captures the current stack trace, returning an array of `CallSite`s. There are good overviews of the available CallSite methods [here](https://github.com/v8/v8/wiki/Stack%20Trace%20API#customizing-stack-traces), and [here](https://github.com/sindresorhus/callsites#api).
  40. #### limit
  41. Type: `number`
  42. Default: `Infinity`
  43. Limits the number of lines returned by dropping all lines in excess of the limit. This removes lines from the stack trace.
  44. #### startStackFunction
  45. Type: `function`
  46. The function where the stack trace should start. The first line of the stack trace will be the function that called `startStackFunction`. This removes lines from the end of the stack trace.
  47. ### stackUtils.captureString([limit], [startStackFunction])
  48. Captures the current stack trace, cleans it using `stackUtils.clean(stack)`, and returns a string with the cleaned stack trace. It takes the same arguments as `stackUtils.capture`.
  49. ### stackUtils.at([startStackFunction])
  50. Captures the first line of the stack trace (or the first line after `startStackFunction` if supplied), and returns a `CallSite` like object that is serialization friendly (properties are actual values instead of getter functions).
  51. The available properties are:
  52. - `line`: `number`
  53. - `column`: `number`
  54. - `file`: `string`
  55. - `constructor`: `boolean`
  56. - `evalOrigin`: `string`
  57. - `native`: `boolean`
  58. - `typename`: `string`
  59. - `function`: `string`
  60. - `method`: `string`
  61. ### stackUtils.parseLine(line)
  62. Parses a `string` (which should be a single line from a stack trace), and generates an object with the following properties:
  63. - `line`: `number`
  64. - `column`: `number`
  65. - `file`: `string`
  66. - `constructor`: `boolean`
  67. - `evalOrigin`: `string`
  68. - `evalLine`: `number`
  69. - `evalColumn`: `number`
  70. - `evalFile`: `string`
  71. - `native`: `boolean`
  72. - `function`: `string`
  73. - `method`: `string`
  74. ## License
  75. MIT © [Isaac Z. Schlueter](http://github.com/isaacs), [James Talmage](http://github.com/jamestalmage)