[Pkg-javascript-commits] [node-stack-utils] 01/67: init commit - pull `lib/stack.js` out from `node-tap`.

Bastien Roucariès rouca at moszumanska.debian.org
Thu Sep 7 09:53:01 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stack-utils.

commit 5a57c89319e8f7ad6f9faf555f6c99a2f8cafcd9
Author: James Talmage <james at talmage.io>
Date:   Sun Jan 3 18:16:14 2016 -0500

    init commit - pull `lib/stack.js` out from `node-tap`.
---
 .editorconfig  |  15 ++++
 .gitattributes |   1 +
 .gitignore     |   1 +
 .travis.yml    |   5 ++
 index.js       | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 license        |  21 +++++
 package.json   |  29 +++++++
 readme.md      |  45 +++++++++++
 test.js        |  74 +++++++++++++++++
 9 files changed, 442 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8f9d77e
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2a2f1c6
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - 'stable'
+  - '0.12'
+  - '0.10'
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..5c74e63
--- /dev/null
+++ b/index.js
@@ -0,0 +1,251 @@
+module.exports = StackUtils;
+
+function StackUtils(opts) {
+	if (!(this instanceof StackUtils)) {
+		throw new Error('StackUtils constructor must be called with new');
+	}
+	opts = opts || {};
+	this._cwd = opts.cwd || process.cwd();
+	this._internals = opts.internals || [];
+}
+
+module.exports.nodeInternals = nodeInternals;
+
+function nodeInternals() {
+	// This was tap specific
+	// /node_modules[\\\/]tap[\\\/](.*?)\.js:[0-9]:[0-9]\)?$/
+	return [
+		/\(domain.js:[0-9]+:[0-9]+\)$/,
+		/\(events.js:[0-9]+:[0-9]+\)$/,
+		/\(node.js:[0-9]+:[0-9]+\)$/,
+		/\(timers.js:[0-9]+:[0-9]+\)$/,
+		/\(module.js:[0-9]+:[0-9]+\)$/,
+		/GeneratorFunctionPrototype.next \(native\)/
+	];
+}
+
+StackUtils.prototype.clean = function (stack) {
+	if (!Array.isArray(stack)) {
+		stack = stack.split('\n');
+	}
+
+	if (!(/^\s*at /.test(stack[0])) &&
+		(/^\s*at /.test(stack[1]))) {
+		stack = stack.slice(1);
+	}
+
+	stack = stack.map(function (st) {
+		var isInternal = this._internals.some(function (internal) {
+			return internal.test(st);
+		});
+
+		if (isInternal) {
+			return null;
+		}
+
+		return st.trim()
+			.replace(/^\s*at /, '')
+			.replace(this._cwd + '/', '')
+			.replace(this._cwd + '\\', '');
+	}, this).filter(function (st) {
+		return st;
+	}).join('\n').trim();
+
+	if (stack) {
+		return stack + '\n';
+	}
+	return null;
+};
+
+StackUtils.prototype.captureString = function (limit, fn) {
+	if (typeof limit === 'function') {
+		fn = limit;
+		limit = Infinity;
+	}
+	if (!fn) {
+		fn = this.captureString;
+	}
+
+	var limitBefore = Error.stackTraceLimit;
+	if (limit) {
+		Error.stackTraceLimit = limit;
+	}
+
+	var obj = {};
+
+	Error.captureStackTrace(obj, fn);
+	var stack = obj.stack;
+	Error.stackTraceLimit = limitBefore;
+
+	return this.clean(stack);
+};
+
+StackUtils.prototype.capture = function (limit, fn) {
+	if (typeof limit === 'function') {
+		fn = limit;
+		limit = Infinity;
+	}
+	if (!fn) {
+		fn = this.capture;
+	}
+	var prepBefore = Error.prepareStackTrace;
+	var limitBefore = Error.stackTraceLimit;
+
+	Error.prepareStackTrace = function (obj, site) {
+		return site;
+	};
+
+	if (limit) {
+		Error.stackTraceLimit = limit;
+	}
+
+	var obj = {};
+	Error.captureStackTrace(obj, fn);
+	var stack = obj.stack;
+	Error.prepareStackTrace = prepBefore;
+	Error.stackTraceLimit = limitBefore;
+
+	return stack;
+};
+
+StackUtils.prototype.at = function at(fn) {
+	if (!fn) {
+		fn = at;
+	}
+
+	var site = this.capture(1, fn)[0];
+
+	if (!site) {
+		return {};
+	}
+
+	var res = {
+		file: site.getFileName(),
+		line: site.getLineNumber(),
+		column: site.getColumnNumber()
+	};
+
+	if (res.file.indexOf(this._cwd + '/') === 0 ||
+		res.file.indexOf(this._cwd + '\\') === 0) {
+		res.file = res.file.substr(this._cwd.length + 1);
+	}
+
+	if (site.isConstructor()) {
+		res.constructor = true;
+	}
+
+	if (site.isEval()) {
+		res.evalOrigin = site.getEvalOrigin();
+	}
+
+	if (site.isNative()) {
+		res.native = true;
+	}
+
+	var typename = null;
+	try {
+		typename = site.getTypeName();
+	} catch (er) {}
+
+	if (typename &&
+		typename !== 'Object' &&
+		typename !== '[object Object]') {
+		res.type = typename;
+	}
+
+	var fname = site.getFunctionName();
+	if (fname) {
+		res.function = fname;
+	}
+
+	var meth = site.getMethodName();
+	if (meth && fname !== meth) {
+		res.method = meth;
+	}
+
+	return res;
+};
+
+var re = new RegExp(
+	'^' +
+		// Sometimes we strip out the '    at' because it's noisy
+	'(?:\\s*at )?' +
+		// $1 = ctor if 'new'
+	'(?:(new) )?' +
+		// Object.method [as foo] (, maybe
+		// $2 = function name
+		// $3 = method name
+	'(?:([^\\(\\[]*)(?: \\[as ([^\\]]+)\\])? \\()?' +
+		// (eval at <anonymous> (file.js:1:1),
+		// $4 = eval origin
+		// $5:$6:$7 are eval file/line/col, but not normally reported
+	'(?:eval at ([^ ]+) \\(([^\\)]+):([0-9]+):([0-9]+)\\), )?' +
+		// file:line:col
+		// $8:$9:$10
+		// $11 = 'native' if native
+	'(?:([^\\)]+):([0-9]+):([0-9]+)|(native))' +
+		// maybe close the paren, then end
+	'\\)?$'
+);
+
+StackUtils.prototype.parseLine = function parseLine(line) {
+	var match = line && line.match(re);
+	if (!match) {
+		return null;
+	}
+
+	var ctor = match[1] === 'new';
+	var fname = match[2];
+	var meth = match[3];
+	var evalOrigin = match[4];
+	var evalFile = match[5];
+	var evalLine = Number(match[6]);
+	var evalCol = Number(match[7]);
+	var file = match[8];
+	var lnum = match[9];
+	var col = match[10];
+	var native = match[11] === 'native';
+
+	var res = {
+		file: file,
+		line: Number(lnum),
+		column: Number(col)
+	};
+
+	if (res.file &&
+		(res.file.indexOf(this._cwd + '/') === 0 ||
+		res.file.indexOf(this._cwd + '\\') === 0)) {
+		res.file = res.file.substr(this._cwd.length + 1);
+	}
+
+	if (ctor) {
+		res.constructor = true;
+	}
+
+	if (evalOrigin) {
+		res.evalOrigin = evalOrigin;
+		res.evalLine = evalLine;
+		res.evalColumn = evalCol;
+		res.evalFile = evalFile;
+	}
+
+	if (native) {
+		res.native = true;
+	}
+
+	if (fname) {
+		res.function = fname;
+	}
+
+	if (meth && fname !== meth) {
+		res.method = meth;
+	}
+
+	return res;
+};
+
+var bound = new StackUtils();
+
+Object.keys(StackUtils.prototype).forEach(function (key) {
+	StackUtils[key] = bound[key].bind(bound);
+});
diff --git a/license b/license
new file mode 100644
index 0000000..ad5d021
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) James Talmage <james at talmage.io> (github.com/jamestalmage)
+
+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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..cdfe2f6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+  "name": "stack-utils",
+  "version": "0.0.0",
+  "description": "My mathematical module",
+  "license": "MIT",
+  "repository": "jamestalmage/stack-utils",
+  "author": {
+    "name": "James Talmage",
+    "email": "james at talmage.io",
+    "url": "github.com/jamestalmage"
+  },
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && ava"
+  },
+  "files": [
+    "index.js"
+  ],
+  "keywords": [
+    ""
+  ],
+  "dependencies": {},
+  "devDependencies": {
+    "ava": "^0.8.0",
+    "xo": "^0.12.1"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..52ad99a
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,45 @@
+# stack-utils [![Build Status](https://travis-ci.org/jamestalmage/stack-utils.svg?branch=master)](https://travis-ci.org/jamestalmage/stack-utils)
+
+> My mathematical module
+
+
+## Install
+
+```
+$ npm install --save stack-utils
+```
+
+
+## Usage
+
+```js
+const stackUtils = require('stack-utils');
+
+stackUtils('unicorns');
+//=> 'unicorns & rainbows'
+```
+
+
+## API
+
+### stackUtils(input, [options])
+
+#### input
+
+Type: `string`
+
+Lorem ipsum.
+
+#### options
+
+##### foo
+
+Type: `boolean`  
+Default: `false`
+
+Lorem ipsum.
+
+
+## License
+
+MIT © [James Talmage](http://github.com/jamestalmage)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..cc033c9
--- /dev/null
+++ b/test.js
@@ -0,0 +1,74 @@
+import test from 'ava';
+import StackUtils from './';
+
+function join(stack) {
+	return stack.join('\n') + '\n';
+}
+
+const LinuxStack1 = join([
+	'Error: foo',
+	'    at foo (/user/dev/project/foo.js:3:8)',
+	'    at bar (/user/dev/project/foo.js:7:2)',
+	'    at bar (/user/dev/project/bar.js:4:2)',
+	'    at Object.<anonymous> (/user/dev/project/bar.js:7:1)',
+	'    at Module._compile (module.js:398:26)',
+	'    at Object.Module._extensions..js (module.js:405:10)',
+	'    at Module.load (module.js:344:32)',
+	'    at Function.Module._load (module.js:301:12)',
+	'    at Function.Module.runMain (module.js:430:10)',
+	'    at startup (node.js:141:18)'
+]);
+
+const WindowsStack1 = join([
+	'Error: foo',
+	'    at foo (Z:\\user\\dev\\project\\foo.js:3:8)',
+	'    at bar (Z:\\user\\dev\\project\\foo.js:7:2)',
+	'    at bar (Z:\\user\\dev\\project\\bar.js:4:2)',
+	'    at Object.<anonymous> (Z:\\user\\dev\\project\\bar.js:7:1)',
+	'    at Module._compile (module.js:398:26)',
+	'    at Object.Module._extensions..js (module.js:405:10)',
+	'    at Module.load (module.js:344:32)',
+	'    at Function.Module._load (module.js:301:12)',
+	'    at Function.Module.runMain (module.js:430:10)',
+	'    at startup (node.js:141:18)'
+]);
+
+test('clean: truncates cwd', t => {
+	const expected = join([
+		'foo (foo.js:3:8)',
+		'bar (foo.js:7:2)',
+		'bar (bar.js:4:2)',
+		'Object.<anonymous> (bar.js:7:1)',
+		'Module._compile (module.js:398:26)',
+		'Object.Module._extensions..js (module.js:405:10)',
+		'Module.load (module.js:344:32)',
+		'Function.Module._load (module.js:301:12)',
+		'Function.Module.runMain (module.js:430:10)',
+		'startup (node.js:141:18)'
+	]);
+
+	let stack = new StackUtils({cwd: '/user/dev/project'});
+	t.is(stack.clean(LinuxStack1), expected);
+
+	stack = new StackUtils({cwd: 'Z:\\user\\dev\\project'});
+	t.is(stack.clean(WindowsStack1), expected);
+});
+
+test('clean: eliminates internals', t => {
+	let stack = new StackUtils({cwd: '/user/dev', internals: StackUtils.nodeInternals()});
+	t.is(stack.clean(LinuxStack1), join([
+		'foo (project/foo.js:3:8)',
+		'bar (project/foo.js:7:2)',
+		'bar (project/bar.js:4:2)',
+		'Object.<anonymous> (project/bar.js:7:1)'
+	]));
+
+	stack = new StackUtils({cwd: 'Z:\\user\\dev', internals: StackUtils.nodeInternals()});
+	t.is(stack.clean(WindowsStack1), join([
+		'foo (project\\foo.js:3:8)',
+		'bar (project\\foo.js:7:2)',
+		'bar (project\\bar.js:4:2)',
+		'Object.<anonymous> (project\\bar.js:7:1)'
+	]));
+});
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stack-utils.git



More information about the Pkg-javascript-commits mailing list