@nx-js/compiler-util
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "@nx-js/compiler-util", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "An NX util, responsible for executing code in the context of an object.", | ||
"main": "index.js", | ||
"main": "dist/cjs.es5.js", | ||
"module": "dist/es.es5.js", | ||
"scripts": { | ||
"test": "mocha test", | ||
"lint": "standard" | ||
"build": "node ./scripts/build.js", | ||
"test": "node ./scripts/test.js", | ||
"test-builds": "node ./scripts/testBuilds.js", | ||
"lint": "standard src/* test/*" | ||
}, | ||
@@ -33,6 +36,32 @@ "author": { | ||
"devDependencies": { | ||
"chai": "3.5.0", | ||
"mocha": "2.5.3", | ||
"standard": "7.1.2", | ||
"pre-push": "0.1.1" | ||
"babel-core": "^6.26.3", | ||
"babel-loader": "7.1.4", | ||
"babel-minify": "^0.4.3", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2", | ||
"babel-preset-es2016": "^6.24.1", | ||
"babel-preset-es2017": "^6.24.1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"buble": "^0.19.3", | ||
"chai": "4.1.2", | ||
"coveralls": "^2.13.1", | ||
"del": "^3.0.0", | ||
"karma": "^2.0.2", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-coverage": "^1.1.1", | ||
"karma-mocha": "^1.3.0", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-rollup-preprocessor": "^5.0.1", | ||
"karma-source-map-support": "^1.2.0", | ||
"mocha": "5.2.0", | ||
"nyc": "^12.0.2", | ||
"pre-push": "0.1.1", | ||
"rollup": "^0.60.1", | ||
"rollup-plugin-alias": "^1.4.0", | ||
"rollup-plugin-auto-external": "^1.2.0", | ||
"rollup-plugin-babel": "^3.0.4", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"rollup-plugin-coverage": "^0.1.4", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"standard": "11.0.1" | ||
}, | ||
@@ -39,0 +68,0 @@ "engines": { |
@@ -183,2 +183,13 @@ # The compiler util | ||
## Alternative builds | ||
This library detects if you use ES or commonJS modules and serve the right format to you. The exposed bundles are transpiled to ES5 to support common tools - like UglifyJS. If you would like a finer control over the provided build, you can specify them in your imports. | ||
* `@nx-js/compiler-util/dist/es.es6.js` exposes an ES6 build with ES modules. | ||
* `@nx-js/compiler-util/dist/es.es5.js` exposes an ES5 build with ES modules. | ||
* `@nx-js/compiler-util/dist/cjs.es6.js` exposes an ES6 build with commonJS modules. | ||
* `@nx-js/compiler-util/dist/cjs.es5.js` exposes an ES5 build with commonJS modules. | ||
If you use a bundler, set up an alias for `@nx-js/compiler-util` to point to your desired build. You can learn how to do it with webpack [here](https://webpack.js.org/configuration/resolve/#resolve-alias) and with rollup [here](https://github.com/rollup/rollup-plugin-alias#usage). | ||
## Contributions | ||
@@ -185,0 +196,0 @@ |
@@ -1,14 +0,7 @@ | ||
'use strict' | ||
import { parseExpression, parseCode } from './parser' | ||
const parser = require('./parser') | ||
const expressionCache = new Map() | ||
const codeCache = new Map() | ||
module.exports = { | ||
compileExpression, | ||
compileCode | ||
} | ||
function compileExpression (src) { | ||
export function compileExpression (src) { | ||
if (typeof src !== 'string') { | ||
@@ -19,3 +12,3 @@ throw new TypeError('First argument must be a string.') | ||
if (!expression) { | ||
expression = parser.parseExpression(src) | ||
expression = parseExpression(src) | ||
expressionCache.set(src, expression) | ||
@@ -28,4 +21,4 @@ } | ||
return function evaluateExpression (context) { | ||
let value = expression.exec(context) | ||
return function evaluateExpression (context, tempVars) { | ||
let value = expression.exec(context, tempVars) | ||
for (let filter of expression.filters) { | ||
@@ -39,3 +32,3 @@ const args = filter.argExpressions.map(evaluateArgExpression, context) | ||
function compileCode (src) { | ||
export function compileCode (src) { | ||
if (typeof src !== 'string') { | ||
@@ -46,3 +39,3 @@ throw new TypeError('First argument must be a string.') | ||
if (!code) { | ||
code = parser.parseCode(src) | ||
code = parseCode(src) | ||
codeCache.set(src, code) | ||
@@ -49,0 +42,0 @@ } |
@@ -1,6 +0,5 @@ | ||
'use strict' | ||
const hasHandler = { has } | ||
const allHandlers = { has, get } | ||
const globals = new Set() | ||
const proxies = new WeakMap() | ||
const handlers = {has} | ||
let temp | ||
@@ -12,53 +11,38 @@ let globalObj | ||
globalObj.$nxCompileToSandbox = toSandbox | ||
globalObj.$nxCompileCreateBackup = createBackup | ||
globalObj.$nxClearSandbox = clearSandbox | ||
module.exports = { | ||
expose, | ||
hide, | ||
hideAll | ||
} | ||
function expose (...globalNames) { | ||
export function expose (...globalNames) { | ||
for (let globalName of globalNames) { | ||
globals.add(globalName) | ||
} | ||
return this | ||
} | ||
function hide (...globalNames) { | ||
export function hide (...globalNames) { | ||
for (let globalName of globalNames) { | ||
globals.delete(globalName) | ||
} | ||
return this | ||
} | ||
function hideAll () { | ||
export function hideAll () { | ||
globals.clear() | ||
return this | ||
} | ||
function has (target, key) { | ||
return globals.has(key) ? Reflect.has(target, key) : true | ||
return globals.has(key) ? (key in target) : true | ||
} | ||
function toSandbox (obj) { | ||
if (typeof obj !== 'object') { | ||
throw new TypeError('first argument must be an object') | ||
} | ||
let sandbox = proxies.get(obj) | ||
if (!sandbox) { | ||
sandbox = new Proxy(obj, handlers) | ||
proxies.set(obj, sandbox) | ||
} | ||
return sandbox | ||
function get (target, key) { | ||
return key in temp ? temp[key] : target[key] | ||
} | ||
function createBackup (context, tempVars) { | ||
if (typeof tempVars === 'object') { | ||
const backup = {} | ||
for (let key of Object.keys(tempVars)) { | ||
backup[key] = context[key] | ||
} | ||
return backup | ||
function toSandbox (obj, tempVars) { | ||
if (tempVars) { | ||
temp = tempVars | ||
return new Proxy(obj, allHandlers) | ||
} | ||
return new Proxy(obj, hasHandler) | ||
} | ||
function clearSandbox () { | ||
temp = undefined | ||
} |
@@ -1,14 +0,5 @@ | ||
'use strict' | ||
export const filters = new Map() | ||
export const limiters = new Map() | ||
const filters = new Map() | ||
const limiters = new Map() | ||
module.exports = { | ||
filters, | ||
limiters, | ||
filter, | ||
limiter | ||
} | ||
function filter (name, handler) { | ||
export function filter (name, handler) { | ||
if (typeof name !== 'string') { | ||
@@ -24,6 +15,5 @@ throw new TypeError('First argument must be a string.') | ||
filters.set(name, handler) | ||
return this | ||
} | ||
function limiter (name, handler) { | ||
export function limiter (name, handler) { | ||
if (typeof name !== 'string') { | ||
@@ -39,3 +29,2 @@ throw new TypeError('First argument must be a string.') | ||
limiters.set(name, handler) | ||
return this | ||
} |
@@ -1,6 +0,4 @@ | ||
'use strict' | ||
import { limiters, filters } from './modifiers' | ||
import { compileRawExpression, compileRawCode } from './rawCompiler' | ||
const modifiers = require('./modifiers') | ||
const rawCompiler = require('./rawCompiler') | ||
const filterRegex = /(?:[^\|]|\|\|)+/g | ||
@@ -10,25 +8,20 @@ const limiterRegex = /(?:[^&]|&&)+/g | ||
module.exports = { | ||
parseExpression, | ||
parseCode | ||
} | ||
function parseExpression (src) { | ||
export function parseExpression (src) { | ||
const tokens = src.match(filterRegex) | ||
if (tokens.length === 1) { | ||
return rawCompiler.compileExpression(tokens[0]) | ||
return compileRawExpression(tokens[0]) | ||
} | ||
const expression = { | ||
exec: rawCompiler.compileExpression(tokens[0]), | ||
exec: compileRawExpression(tokens[0]), | ||
filters: [] | ||
} | ||
for (let i = 1; i < tokens.length; i++) { | ||
let filterTokens = tokens[i].match(argsRegex) || [] | ||
let filterTokens = tokens[i].match(argsRegex) | ||
const filterName = filterTokens.shift() | ||
const effect = modifiers.filters.get(filterName) | ||
const effect = filters.get(filterName) | ||
if (!effect) { | ||
throw new Error(`There is no filter named: ${filterName}.`) | ||
} | ||
expression.filters.push({effect, argExpressions: filterTokens.map(compileArgExpression)}) | ||
expression.filters.push({effect, argExpressions: filterTokens.map(compileRawExpression)}) | ||
} | ||
@@ -38,26 +31,22 @@ return expression | ||
function parseCode (src) { | ||
export function parseCode (src) { | ||
const tokens = src.match(limiterRegex) | ||
if (tokens.length === 1) { | ||
return rawCompiler.compileCode(tokens[0]) | ||
return compileRawCode(tokens[0]) | ||
} | ||
const code = { | ||
exec: rawCompiler.compileCode(tokens[0]), | ||
exec: compileRawCode(tokens[0]), | ||
limiters: [] | ||
} | ||
for (let i = 1; i < tokens.length; i++) { | ||
const limiterTokens = tokens[i].match(argsRegex) || [] | ||
const limiterTokens = tokens[i].match(argsRegex) | ||
const limiterName = limiterTokens.shift() | ||
const effect = modifiers.limiters.get(limiterName) | ||
const effect = limiters.get(limiterName) | ||
if (!effect) { | ||
throw new Error(`There is no limiter named: ${limiterName}.`) | ||
} | ||
code.limiters.push({effect, argExpressions: limiterTokens.map(compileArgExpression)}) | ||
code.limiters.push({effect, argExpressions: limiterTokens.map(compileRawExpression)}) | ||
} | ||
return code | ||
} | ||
function compileArgExpression (argExpression) { | ||
return rawCompiler.compileExpression(argExpression) | ||
} |
@@ -1,26 +0,15 @@ | ||
'use strict' | ||
module.exports = { | ||
compileCode, | ||
compileExpression | ||
} | ||
function compileExpression (src) { | ||
return new Function('context', // eslint-disable-line | ||
`const sandbox = $nxCompileToSandbox(context) | ||
export function compileRawExpression (src) { | ||
return new Function('context', 'tempVars', // eslint-disable-line | ||
`const sandbox = $nxCompileToSandbox(context, tempVars) | ||
try { with (sandbox) { return ${src} } } catch (err) { | ||
if (!(err instanceof TypeError)) throw err | ||
}`) | ||
} | ||
$nxClearSandbox()`) | ||
} | ||
function compileCode (src) { | ||
export function compileRawCode (src) { | ||
return new Function('context', 'tempVars', // eslint-disable-line | ||
`const backup = $nxCompileCreateBackup(context, tempVars) | ||
Object.assign(context, tempVars) | ||
const sandbox = $nxCompileToSandbox(context) | ||
try { | ||
with (sandbox) { ${src} } | ||
} finally { | ||
Object.assign(context, backup) | ||
}`) | ||
`const sandbox = $nxCompileToSandbox(context, tempVars) | ||
with (sandbox) { ${src} } | ||
$nxClearSandbox()`) | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
68367
24
1302
210
30
3
3