diff options
Diffstat (limited to 'node_modules/istanbul-lib-hook')
-rw-r--r-- | node_modules/istanbul-lib-hook/CHANGELOG.md | 30 | ||||
-rw-r--r-- | node_modules/istanbul-lib-hook/LICENSE | 24 | ||||
-rw-r--r-- | node_modules/istanbul-lib-hook/README.md | 7 | ||||
-rw-r--r-- | node_modules/istanbul-lib-hook/index.js | 5 | ||||
-rw-r--r-- | node_modules/istanbul-lib-hook/lib/hook.js | 183 | ||||
-rw-r--r-- | node_modules/istanbul-lib-hook/package.json | 36 |
6 files changed, 285 insertions, 0 deletions
diff --git a/node_modules/istanbul-lib-hook/CHANGELOG.md b/node_modules/istanbul-lib-hook/CHANGELOG.md new file mode 100644 index 000000000..37056c0c2 --- /dev/null +++ b/node_modules/istanbul-lib-hook/CHANGELOG.md @@ -0,0 +1,30 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +<a name="1.0.6"></a> +## [1.0.6](https://github.com/istanbuljs/istanbul-lib-hook/compare/istanbul-lib-hook@1.0.5...istanbul-lib-hook@1.0.6) (2017-04-29) + + + + +<a name="1.0.5"></a> +## [1.0.5](https://github.com/istanbuljs/istanbul-lib-hook/compare/istanbul-lib-hook@1.0.4...istanbul-lib-hook@1.0.5) (2017-03-27) + +<a name="1.0.4"></a> +## [1.0.4](https://github.com/istanbuljs/istanbul-lib-hook/compare/istanbul-lib-hook@1.0.3...istanbul-lib-hook@1.0.4) (2017-03-21) + +<a name="1.0.3"></a> +## [1.0.3](https://github.com/istanbuljs/istanbul-lib-hook/compare/istanbul-lib-hook@1.0.2...istanbul-lib-hook@1.0.3) (2017-03-21) + +<a name="1.0.2"></a> +## [1.0.2](https://github.com/istanbuljs/istanbul-lib-hook/compare/istanbul-lib-hook@1.0.0...istanbul-lib-hook@1.0.2) (2017-03-21) + +<a name="1.0.0"></a> +# [1.0.0](https://github.com/istanbuljs/istanbul-lib-hook/compare/v1.0.0-alpha.3...v1.0.0) (2017-01-17) + + +### Bug Fixes + +* update append-transform to version that fixes issues run into by ts-node ([f4aaf79](https://github.com/istanbuljs/istanbul-lib-hook/commit/f4aaf79)) diff --git a/node_modules/istanbul-lib-hook/LICENSE b/node_modules/istanbul-lib-hook/LICENSE new file mode 100644 index 000000000..d55d2916e --- /dev/null +++ b/node_modules/istanbul-lib-hook/LICENSE @@ -0,0 +1,24 @@ +Copyright 2012-2015 Yahoo! Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yahoo! Inc. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/node_modules/istanbul-lib-hook/README.md b/node_modules/istanbul-lib-hook/README.md new file mode 100644 index 000000000..0f3ec181d --- /dev/null +++ b/node_modules/istanbul-lib-hook/README.md @@ -0,0 +1,7 @@ +istanbul-lib-hook +================= + +[![Greenkeeper badge](https://badges.greenkeeper.io/istanbuljs/istanbul-lib-hook.svg)](https://greenkeeper.io/) +[![Build Status](https://travis-ci.org/istanbuljs/istanbul-lib-hook.svg?branch=master)](https://travis-ci.org/istanbuljs/istanbul-lib-hook) + +Hooks for require, vm and script used in istanbul diff --git a/node_modules/istanbul-lib-hook/index.js b/node_modules/istanbul-lib-hook/index.js new file mode 100644 index 000000000..c59df033b --- /dev/null +++ b/node_modules/istanbul-lib-hook/index.js @@ -0,0 +1,5 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +module.exports = require('./lib/hook'); diff --git a/node_modules/istanbul-lib-hook/lib/hook.js b/node_modules/istanbul-lib-hook/lib/hook.js new file mode 100644 index 000000000..ff1119d8e --- /dev/null +++ b/node_modules/istanbul-lib-hook/lib/hook.js @@ -0,0 +1,183 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +var path = require('path'), + vm = require('vm'), + appendTransform = require('append-transform'), + originalCreateScript = vm.createScript, + originalRunInThisContext = vm.runInThisContext; + +function transformFn(matcher, transformer, verbose) { + + return function (code, filename) { + var shouldHook = typeof filename === 'string' && matcher(path.resolve(filename)), + transformed, + changed = false; + + if (shouldHook) { + if (verbose) { + console.error('Module load hook: transform [' + filename + ']'); + } + try { + transformed = transformer(code, filename); + changed = true; + } catch (ex) { + console.error('Transformation error for', filename, '; return original code'); + console.error(ex.message || String(ex)); + if (verbose) { + console.error(ex.stack); + } + transformed = code; + } + } else { + transformed = code; + } + return { code: transformed, changed: changed }; + }; +} +/** + * unloads the required caches, removing all files that would have matched + * the supplied matcher. + * @param {Function} matcher - the match function that accepts a file name and + * returns if that file should be unloaded from the cache. + */ +function unloadRequireCache(matcher) { + /* istanbul ignore else: impossible to test */ + if (matcher && typeof require !== 'undefined' && require && require.cache) { + Object.keys(require.cache).forEach(function (filename) { + if (matcher(filename)) { + delete require.cache[filename]; + } + }); + } +} +/** + * hooks `require` to return transformed code to the node module loader. + * Exceptions in the transform result in the original code being used instead. + * @method hookRequire + * @static + * @param matcher {Function(filePath)} a function that is called with the absolute path to the file being + * `require`-d. Should return a truthy value when transformations need to be applied to the code, a falsy value otherwise + * @param transformer {Function(code, filePath)} a function called with the original code and the associated path of the file + * from where the code was loaded. Should return the transformed code. + * @param options {Object} options Optional. + * @param {Boolean} [options.verbose] write a line to standard error every time the transformer is called + * @param {Function} [options.postLoadHook] a function that is called with the name of the file being + * required. This is called after the require is processed irrespective of whether it was transformed. + * @returns {Function} a reset function that can be called to remove the hook + */ +function hookRequire(matcher, transformer, options) { + options = options || {}; + var extensions, + disable = false, + fn = transformFn(matcher, transformer, options.verbose), + postLoadHook = options.postLoadHook && + typeof options.postLoadHook === 'function' ? options.postLoadHook : null; + + extensions = options.extensions || ['.js']; + + extensions.forEach(function(ext){ + appendTransform(function (code, filename) { + if (disable) { + return code; + } + var ret = fn(code, filename); + if (postLoadHook) { + postLoadHook(filename); + } + return ret.code; + }, ext); + }); + + return function () { + disable = true; + }; +} +/** + * hooks `vm.createScript` to return transformed code out of which a `Script` object will be created. + * Exceptions in the transform result in the original code being used instead. + * @method hookCreateScript + * @static + * @param matcher {Function(filePath)} a function that is called with the filename passed to `vm.createScript` + * Should return a truthy value when transformations need to be applied to the code, a falsy value otherwise + * @param transformer {Function(code, filePath)} a function called with the original code and the filename passed to + * `vm.createScript`. Should return the transformed code. + * @param options {Object} options Optional. + * @param {Boolean} [options.verbose] write a line to standard error every time the transformer is called + */ +function hookCreateScript(matcher, transformer, opts) { + opts = opts || {}; + var fn = transformFn(matcher, transformer, opts.verbose); + vm.createScript = function (code, file) { + var ret = fn(code, file); + return originalCreateScript(ret.code, file); + }; +} +/** + * unhooks vm.createScript, restoring it to its original state. + * @method unhookCreateScript + * @static + */ +function unhookCreateScript() { + vm.createScript = originalCreateScript; +} +/** + * hooks `vm.runInThisContext` to return transformed code. + * @method hookRunInThisContext + * @static + * @param matcher {Function(filePath)} a function that is called with the filename passed to `vm.createScript` + * Should return a truthy value when transformations need to be applied to the code, a falsy value otherwise + * @param transformer {Function(code, filePath)} a function called with the original code and the filename passed to + * `vm.createScript`. Should return the transformed code. + * @param opts {Object} [opts={}] options + * @param {Boolean} [opts.verbose] write a line to standard error every time the transformer is called + */ +function hookRunInThisContext(matcher, transformer, opts) { + opts = opts || {}; + var fn = transformFn(matcher, transformer, opts.verbose); + vm.runInThisContext = function (code, file) { + var ret = fn(code, file); + return originalRunInThisContext(ret.code, file); + }; +} +/** + * unhooks vm.runInThisContext, restoring it to its original state. + * @method unhookRunInThisContext + * @static + */ +function unhookRunInThisContext() { + vm.runInThisContext = originalRunInThisContext; +} +/** + * istanbul-lib-hook provides mechanisms to transform code in the scope of `require`, + * `vm.createScript`, `vm.runInThisContext` etc. + * + * This mechanism is general and relies on a user-supplied `matcher` function that + * determines when transformations should be performed and a user-supplied `transformer` + * function that performs the actual transform. Instrumenting code for coverage is + * one specific example of useful hooking. + * + * Note that both the `matcher` and `transformer` must execute synchronously. + * + * @module Exports + * @example + * var hook = require('istanbul-lib-hook'), + * myMatcher = function (file) { return file.match(/foo/); }, + * myTransformer = function (code, file) { + * return 'console.log("' + file + '");' + code; + * }; + * + * hook.hookRequire(myMatcher, myTransformer); + * var foo = require('foo'); //will now print foo's module path to console + */ +module.exports = { + hookRequire: hookRequire, + hookCreateScript: hookCreateScript, + unhookCreateScript: unhookCreateScript, + hookRunInThisContext : hookRunInThisContext, + unhookRunInThisContext : unhookRunInThisContext, + unloadRequireCache: unloadRequireCache +}; + + diff --git a/node_modules/istanbul-lib-hook/package.json b/node_modules/istanbul-lib-hook/package.json new file mode 100644 index 000000000..f92137771 --- /dev/null +++ b/node_modules/istanbul-lib-hook/package.json @@ -0,0 +1,36 @@ +{ + "name": "istanbul-lib-hook", + "version": "1.0.6", + "description": "Hooks for require, vm and script used in istanbul", + "author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>", + "main": "index.js", + "files": [ + "lib", + "index.js" + ], + "scripts": { + "pretest": "jshint index.js lib/ test/", + "test": "mocha" + }, + "dependencies": { + "append-transform": "^0.4.0" + }, + "devDependencies": { + "chai": "^3.0.0", + "jshint": "^2.8.0", + "mocha": "^3.2.0" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/istanbuljs/istanbul-lib-hook.git" + }, + "keywords": [ + "istanbul", + "hook" + ], + "license": "BSD-3-Clause", + "bugs": { + "url": "https://github.com/istanbuljs/istanbul-lib-hook/issues" + }, + "homepage": "https://github.com/istanbuljs/istanbul-lib-hook#readme" +} |