├── .gitignore ├── .npmignore ├── History.md ├── LICENSE ├── Makefile ├── Readme.md ├── examples ├── assert.js └── trace.js ├── index.js ├── package.json └── test ├── __line.js └── __stack.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.sock 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.0 / 2013-01-24 3 | ================== 4 | 5 | * remove lame magical getters 6 | 7 | 0.0.1 / 2010-01-03 8 | ================== 9 | 10 | * Initial release 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2011, 2013 TJ Holowaychuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should 5 | 6 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # callsite 2 | 3 | Access to v8's "raw" `CallSite`s. 4 | 5 | ## Installation 6 | 7 | $ npm install callsite 8 | 9 | ## Example 10 | 11 | ```js 12 | var stack = require('callsite'); 13 | 14 | foo(); 15 | 16 | function foo() { 17 | bar(); 18 | } 19 | 20 | function bar() { 21 | baz(); 22 | } 23 | 24 | function baz() { 25 | console.log(); 26 | stack().forEach(function(site){ 27 | console.log(' \033[36m%s\033[90m in %s:%d\033[0m' 28 | , site.getFunctionName() || 'anonymous' 29 | , site.getFileName() 30 | , site.getLineNumber()); 31 | }); 32 | console.log(); 33 | } 34 | ``` 35 | 36 | ## Why? 37 | 38 | Because you can do weird, stupid, clever, wacky things such as: 39 | 40 | - [better-assert](https://github.com/visionmedia/better-assert) 41 | 42 | ## License 43 | 44 | MIT 45 | -------------------------------------------------------------------------------- /examples/assert.js: -------------------------------------------------------------------------------- 1 | 2 | var stack = require('..'); 3 | 4 | var fs = require('fs'); 5 | 6 | function assert(expr) { 7 | if (expr) return; 8 | 9 | var call = stack()[1] 10 | , file = call.getFileName() 11 | , lineno = call.getLineNumber() 12 | , src = fs.readFileSync(file, 'utf8') 13 | , line = src.split('\n')[lineno-1] 14 | , src = parse(line); 15 | 16 | var fmt = ' \033[91massert: \033[31m%s\033[0m' 17 | + '\n \033[90min: %s:%d' 18 | + '\n value: %j\033[0m' 19 | + '\n'; 20 | 21 | console.error(fmt, src, file, lineno, expr); 22 | } 23 | 24 | function parse(str) { 25 | return str.match(/assert\((.*)\)/)[1]; 26 | } 27 | 28 | assert('wahoo'); 29 | 30 | var user = { authenticated: false }; 31 | assert(user.authenticated); 32 | 33 | user.authenticated = true; 34 | assert(user.authenticated); 35 | 36 | user.authenticated = 0; 37 | assert(user.authenticated); 38 | 39 | -------------------------------------------------------------------------------- /examples/trace.js: -------------------------------------------------------------------------------- 1 | 2 | var stack = require('..'); 3 | 4 | foo(); 5 | 6 | function foo() { 7 | bar(); 8 | } 9 | 10 | function bar() { 11 | baz(); 12 | } 13 | 14 | function baz() { 15 | console.log(); 16 | stack().forEach(function(site){ 17 | console.log(' \033[36m%s\033[90m in %s:%d\033[0m' 18 | , site.getFunctionName() || 'anonymous' 19 | , site.getFileName() 20 | , site.getLineNumber()); 21 | }); 22 | console.log(); 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function(){ 3 | var orig = Error.prepareStackTrace; 4 | Error.prepareStackTrace = function(_, stack){ return stack; }; 5 | var err = new Error; 6 | Error.captureStackTrace(err, arguments.callee); 7 | var stack = err.stack; 8 | Error.prepareStackTrace = orig; 9 | return stack; 10 | }; 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "callsite" 3 | , "version": "1.0.0" 4 | , "description": "access to v8's CallSites" 5 | , "keywords": ["stack", "trace", "line"] 6 | , "author": "TJ Holowaychuk " 7 | , "dependencies": {} 8 | , "devDependencies": { "mocha": "*", "should": "*" } 9 | , "main": "index" 10 | , "repository": { "type": "git", "url": "https://github.com/visionmedia/callsite.git" } 11 | , "engines": { "node": "*" } 12 | , "license" : "MIT" 13 | } 14 | -------------------------------------------------------------------------------- /test/__line.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | require('../'); 7 | 8 | describe('__line', function(){ 9 | it('should return the current lineno', function(){ 10 | __line.should.equal(10); 11 | }) 12 | }) -------------------------------------------------------------------------------- /test/__stack.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | require('../'); 7 | 8 | describe('__stack', function(){ 9 | it('should return an array of CallSites', function(){ 10 | foo(); 11 | 12 | function foo() { 13 | bar(); 14 | } 15 | 16 | function bar() { 17 | baz(); 18 | } 19 | 20 | function baz() { 21 | __stack[0].fun.should.equal(baz); 22 | __stack[1].fun.should.equal(bar); 23 | __stack[2].fun.should.equal(foo); 24 | } 25 | }) 26 | 27 | it('should restore stack preparation', function(){ 28 | __stack; 29 | new Error().stack.should.be.a('string'); 30 | }) 31 | }) --------------------------------------------------------------------------------