Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

autostrip-json-comments

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

autostrip-json-comments - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

2

docs/badges.md

@@ -7,4 +7,2 @@ [![NPM][autostrip-json-comments-icon]][autostrip-json-comments-url]

[![endorse][endorse-image]][endorse-url]
[autostrip-json-comments-icon]: https://nodei.co/npm/autostrip-json-comments.png?downloads=true

@@ -11,0 +9,0 @@ [autostrip-json-comments-url]: https://npmjs.org/package/autostrip-json-comments

2

docs/use.md

@@ -12,4 +12,4 @@ install:

var obj = require('commented-json-filename');
var obj = require('path/to/commented-json-filename');
// comments are automatically stripped inside require
```

@@ -24,4 +24,3 @@ 'use strict';

'*.js',
'src/**/*.js',
'!src/hook.js'
'src/**/*.js'
],

@@ -28,0 +27,0 @@ options: {

0.0.3 / 2013-11-19
==================
* Cleaning up hook file
0.0.2 / 2013-11-19

@@ -3,0 +8,0 @@ ==================

{
"name": "autostrip-json-comments",
"version": "0.0.2",
"version": "0.0.3",
"description": "Installs nodejs require hook to strip JSON comments",

@@ -24,3 +24,3 @@ "main": "index.js",

"type": "git",
"url": "git@github.com:uTest/autostrip-json-comments.git"
"url": "https://github.com/uTest/autostrip-json-comments.git"
},

@@ -27,0 +27,0 @@ "author": "Gleb Bahmutov <gleb.bahmutov@utest.com>",

@@ -1,2 +0,2 @@

# autostrip-json-comments v0.0.2
# autostrip-json-comments v0.0.3

@@ -11,4 +11,2 @@ > Installs nodejs require hook to strip JSON comments

[![endorse][endorse-image]][endorse-url]
[autostrip-json-comments-icon]: https://nodei.co/npm/autostrip-json-comments.png?downloads=true

@@ -36,3 +34,3 @@ [autostrip-json-comments-url]: https://npmjs.org/package/autostrip-json-comments

var obj = require('commented-json-filename');
var obj = require('path/to/commented-json-filename');
// comments are automatically stripped inside require

@@ -95,2 +93,7 @@ ```

0.0.3 / 2013-11-19
==================
* Cleaning up hook file
0.0.2 / 2013-11-19

@@ -97,0 +100,0 @@ ==================

@@ -8,163 +8,39 @@ // based on https://github.com/gotwarlost/istanbul/blob/master/lib/hook.js

/**
* provides a mechanism to transform code in the scope of `require` or `vm.createScript`.
* 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.
*
* For the common case of matching filesystem paths based on inclusion/ exclusion patterns, use the `matcherFor`
* function in the istanbul API to get a matcher.
*
* It is up to the transformer to perform processing with side-effects, such as caching, storing the original
* source code to disk in case of dynamically generated scripts etc. The `Store` class can help you with this.
*
* Usage
* -----
*
* var hook = require('this filename').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
*
* @class Hook
*/
var extension = '.json';
var path = require('path'),
fs = require('fs'),
Module = require('module'),
vm = require('vm'),
originalLoader = Module._extensions[extension],
originalCreateScript = vm.createScript,
originalRunInThisContext = vm.runInThisContext;
var fs = require('fs'),
Module = require('module'),
originalLoader = Module._extensions[extension];
/*
function transformFn(matcher, transformer, verbose) {
return function (code, filename) {
var shouldHook = 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; return original code');
console.error(ex);
transformed = code;
}
} else {
transformed = code;
}
return { code: transformed, changed: changed };
};
}
*/
function unloadJsonCache() {
function matcher(filename) {
return /\.json$/.test(filename);
}
function isJsonFile(filename) {
return (/\.json$/).test(filename);
}
if (matcher && typeof require !== 'undefined' && require && require.cache) {
Object.keys(require.cache).forEach(function (filename) {
if (matcher(filename)) {
delete require.cache[filename];
}
});
}
if (typeof require !== 'undefined' && require && require.cache) {
Object.keys(require.cache).forEach(function (filename) {
if (isJsonFile(filename)) {
delete require.cache[filename];
}
});
}
}
function hookJsonRequire(transformer) {
Module._extensions['.json'] = function (module, filename) {
var ret = transformer(fs.readFileSync(filename, 'utf8'), filename);
var str = 'module.exports = ' + ret;
module._compile(str, filename);
};
Module._extensions['.json'] = function (module, filename) {
var ret = transformer(fs.readFileSync(filename, 'utf8'), filename);
var str = 'module.exports = ' + ret;
module._compile(str, filename);
};
}
function unhookJsonRequire() {
Module._extensions['.json'] = originalLoader;
Module._extensions['.json'] = originalLoader;
}
/**
* 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 options {Object} options Optional.
* @param {Boolean} [options.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;
}
module.exports = {
hookJsonRequire: hookJsonRequire,
unhookJsonRequire: unhookJsonRequire,
/*,
hookCreateScript: hookCreateScript,
unhookCreateScript: unhookCreateScript,
hookRunInThisContext : hookRunInThisContext,
unhookRunInThisContext : unhookRunInThisContext,*/
unloadJsonCache: unloadJsonCache
hookJsonRequire: hookJsonRequire,
unhookJsonRequire: unhookJsonRequire,
unloadJsonCache: unloadJsonCache
};
gt.module('strips comments in require', {
setupOnce: function() {
require('../index');
// multiple requires are guarded
require('../index');
},

@@ -5,0 +7,0 @@ teardownOnce: function () {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc