pax_global_header00006660000000000000000000000064142121771450014516gustar00rootroot0000000000000052 comment=7d49264161c6e10b2bb96fdbb2031c180015910b caller-1.1.0/000077500000000000000000000000001421217714500127575ustar00rootroot00000000000000caller-1.1.0/.gitignore000066400000000000000000000000631421217714500147460ustar00rootroot00000000000000.idea node_modules *.log package-lock.json .vscode caller-1.1.0/.travis.yml000066400000000000000000000000631421217714500150670ustar00rootroot00000000000000language: node_js node_js: - "0.11" - "0.10" caller-1.1.0/CHANGELOG.md000066400000000000000000000001541421217714500145700ustar00rootroot00000000000000# Changelog ## Unreleased ## v1.1.0 (March 9, 2022) - fix: handle case where Error class is wrapped (#10) caller-1.1.0/LICENSE000066400000000000000000000020601421217714500137620ustar00rootroot00000000000000This software is released under the MIT license: 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.caller-1.1.0/README.md000066400000000000000000000016201421217714500142350ustar00rootroot00000000000000#### caller Figure out your caller (thanks to @substack). ##### Initialization Time Caller ```javascript // foo.js var bar = require('bar'); ``` ```javascript // bar.js var caller = require('caller'); console.log(caller()); // `/path/to/foo.js` ``` ##### Runtime Caller ```javascript // foo.js var bar = require('bar'); bar.doWork(); ``` ```javascript // bar.js var caller = require('caller'); exports.doWork = function () { console.log(caller()); // `/path/to/foo.js` }; ``` ### Depth Caller also accepts a `depth` argument for tracing back further (defaults to `1`). ```javascript // foo.js var bar = require('bar'); bar.doWork(); ``` ```javascript // bar.js var baz = require('baz'); exports.doWork = function () { baz.doWork(); }; ``` ```javascript // baz.js var caller = require('caller'); exports.doWork = function () { console.log(caller(2)); // `/path/to/foo.js` }; ``` caller-1.1.0/index.js000066400000000000000000000022641421217714500144300ustar00rootroot00000000000000'use strict'; var assert = require('assert'); /** * Module wrapper of @substack's `caller.js` * @original: https://github.com/substack/node-resolve/blob/master/lib/caller.js * @blessings: https://twitter.com/eriktoth/statuses/413719312273125377 * @see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi */ module.exports = function (depth) { var pst, stack, file, frame, startIdx; pst = Error.prepareStackTrace; Error.prepareStackTrace = function (_, stack) { Error.prepareStackTrace = pst; return stack; }; stack = (new Error()).stack; // Handle case where error object is wrapped by say babel. Try to find current file's index first. startIdx = 0; while(startIdx < stack.length && stack[startIdx].getFileName() !== __filename) startIdx++; assert(startIdx < stack.length, 'Unexpected: unable to find caller/index.js in the stack'); depth = !depth || isNaN(depth) ? 1 : (depth > stack.length - 2 ? stack.length - 2 : depth); stack = stack.slice(startIdx + depth + 1); do { frame = stack.shift(); file = frame && frame.getFileName(); } while (stack.length && file === 'module.js'); return file; }; caller-1.1.0/package.json000066400000000000000000000011071421217714500152440ustar00rootroot00000000000000{ "name": "caller", "version": "1.1.0", "description": "@substack's caller.js as a module", "main": "index.js", "files": [ "index.js" ], "scripts": { "test": "tape test/*.js" }, "repository": { "type": "git", "url": "git://github.com/totherik/caller.git" }, "keywords": [ "caller", "file", "require" ], "author": "Erik Toth ", "license": "MIT", "devDependencies": { "tape": "~2.3.2" }, "readmeFilename": "README.md", "gitHead": "15bef0805246629cc89fb71ded29e674009ffc45", "dependencies": {} } caller-1.1.0/test/000077500000000000000000000000001421217714500137365ustar00rootroot00000000000000caller-1.1.0/test/caller.js000066400000000000000000000034631421217714500155440ustar00rootroot00000000000000'use strict'; var test = require('tape'), caller = require('../'); test('caller', function (t) { t.test('determine caller', function (t) { var actual, expected; actual = caller(); expected = require.resolve('tape/lib/test'); t.equal(actual, expected); t.end(); }); t.test('determine caller at runtime', function (t) { var callee, actual, expected; callee = require('./fixtures/callee'); actual = callee(caller); expected = __filename; t.equal(actual, expected); t.end(); }); t.test('determine caller with depth', function (t) { var callee, actual, expected; callee = require('./fixtures/callee'); actual = callee(caller.bind(null, 2)); expected = require.resolve('tape/lib/test'); t.equal(actual, expected); t.end(); }); t.test('determine caller when Error is wrapped', function (t) { var restoreError, actual, expected; restoreError = require('./fixtures/wrapped-error')(); try { actual = caller(); } finally { restoreError(); } expected = require.resolve('tape/lib/test'); t.equal(actual, expected); t.end(); }); t.test('determine caller with depth cap', function (t) { var callee, actual, expected; callee = require('./fixtures/callee'); actual = callee(caller.bind(null, 99)); expected = require.resolve('tape/lib/test'); t.equal(actual, expected); t.end(); }); t.test('determine caller at initialization time', function (t) { var actual, expected; actual = require('./fixtures/init'); expected = __filename; t.equal(actual, expected); t.end(); }); }); caller-1.1.0/test/fixtures/000077500000000000000000000000001421217714500156075ustar00rootroot00000000000000caller-1.1.0/test/fixtures/callee.js000066400000000000000000000001131421217714500173650ustar00rootroot00000000000000'use strict'; module.exports = function (caller) { return caller(); };caller-1.1.0/test/fixtures/init.js000066400000000000000000000001411421217714500171040ustar00rootroot00000000000000'use strict'; var caller = require('../../'); console.log(caller()); module.exports = caller();caller-1.1.0/test/fixtures/wrapped-error.js000066400000000000000000000046041421217714500207420ustar00rootroot00000000000000function copyConstructorProperties(target, source) { const keys = Object.getOwnPropertyNames(source); for (let i = 0; i < keys.length; i++) { var key = keys[i]; if (!target.hasOwnProperty(key)) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); } } } // inspired from https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/wrap-error-constructor-with-cause.js function wrapErrorConstructor(ERROR_NAME, wrapper) { const clearErrorStack = (function () { const TEST = (function (arg) { return String(Error(arg).stack); })('zxcasd'); const V8_OR_CHAKRA_STACK_ENTRY = /\n\s*at [^:]*:[^\n]*/; const IS_V8_OR_CHAKRA_STACK = V8_OR_CHAKRA_STACK_ENTRY.test(TEST); return function clearErrorStackInner(stack, dropEntries) { if (IS_V8_OR_CHAKRA_STACK && typeof stack == 'string') { while (dropEntries--) stack = stack.replace(V8_OR_CHAKRA_STACK_ENTRY, ''); } return stack; }; })(); const OriginalError = globalThis[ERROR_NAME]; const OriginalErrorPrototype = OriginalError.prototype; const WrappedError = wrapper(function (a) { const message = String(a); const result = new OriginalError(); if (message !== undefined) Object.defineProperty(result, 'message', { value: message, enumerable: false, configurable: true, writable: true }); Object.defineProperty(result, 'stack', { value: clearErrorStack(result.stack, 2), enumerable: false, configurable: true, writable: true }); // if (this && OriginalErrorPrototype.isPrototypeOf(this)) { // // inheritIfRequired(result, this, WrappedError); // Object.setPrototypeOf(result, this.constructor); //?? // } return result; }); WrappedError.prototype = OriginalErrorPrototype; // Copy ownKeys from OriginalError to WrappedError copyConstructorProperties(WrappedError, OriginalError); globalThis[ERROR_NAME] = WrappedError; }; module.exports = function wrapError() { const ErrorClassName = 'Error'; const OriginalError = globalThis[ErrorClassName]; wrapErrorConstructor(ErrorClassName, function (init) { return function Error(message) { return init.apply(this, arguments); }; }); return function restore() { globalThis[ErrorClassName] = OriginalError; }; };