degenerator
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -11,16 +11,11 @@ /// <reference types="node" /> | ||
*/ | ||
declare function degenerator(code: string, _names: degenerator.DegeneratorNames, { output }?: degenerator.DegeneratorOptions): string; | ||
declare function degenerator(code: string, _names: degenerator.DegeneratorNames): string; | ||
declare namespace degenerator { | ||
type DegeneratorName = string | RegExp; | ||
type DegeneratorNames = DegeneratorName[]; | ||
type DegeneratorOutput = 'async' | 'generator'; | ||
interface DegeneratorOptions { | ||
output?: DegeneratorOutput; | ||
} | ||
interface CompileOptions extends DegeneratorOptions, RunningScriptOptions { | ||
interface CompileOptions extends RunningScriptOptions { | ||
sandbox?: Context; | ||
} | ||
const supportsAsync: boolean; | ||
function compile<T extends Function>(code: string, returnName: string, names: DegeneratorNames, options?: CompileOptions): T; | ||
function compile<R = any, A extends any[] = []>(code: string, returnName: string, names: DegeneratorNames, options?: CompileOptions): (...args: A) => Promise<R>; | ||
} | ||
export = degenerator; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const util_1 = require("util"); | ||
@@ -9,5 +6,3 @@ const escodegen_1 = require("escodegen"); | ||
const ast_types_1 = require("ast-types"); | ||
const vm_1 = require("vm"); | ||
const supports_async_1 = __importDefault(require("./supports-async")); | ||
const generator_to_promise_1 = __importDefault(require("./generator-to-promise")); | ||
const vm2_1 = require("vm2"); | ||
/** | ||
@@ -21,3 +16,3 @@ * Compiles sync JavaScript code into JavaScript with async Functions. | ||
*/ | ||
function degenerator(code, _names, { output = 'async' } = {}) { | ||
function degenerator(code, _names) { | ||
if (!Array.isArray(_names)) { | ||
@@ -71,3 +66,3 @@ throw new TypeError('an array of async function "names" is required'); | ||
return false; | ||
} | ||
}, | ||
}); | ||
@@ -78,9 +73,4 @@ if (!shouldDegenerate) { | ||
// Got a "function" expression/statement, | ||
// convert it into an async or generator function | ||
if (output === 'async') { | ||
path.node.async = true; | ||
} | ||
else if (output === 'generator') { | ||
path.node.generator = true; | ||
} | ||
// convert it into an async function | ||
path.node.async = true; | ||
// Add function name to `names` array | ||
@@ -92,3 +82,3 @@ if (!checkName(path.node.id.name, names)) { | ||
this.traverse(path); | ||
} | ||
}, | ||
}); | ||
@@ -104,13 +94,4 @@ } while (lastNamesLength !== names.length); | ||
const delegate = false; | ||
const { name, parent: { node: pNode } } = path; | ||
let expr; | ||
if (output === 'async') { | ||
expr = ast_types_1.builders.awaitExpression(path.node, delegate); | ||
} | ||
else if (output === 'generator') { | ||
expr = ast_types_1.builders.yieldExpression(path.node, delegate); | ||
} | ||
else { | ||
throw new Error('Only "async" and "generator" are allowd `output` values'); | ||
} | ||
const { name, parent: { node: pNode }, } = path; | ||
const expr = ast_types_1.builders.awaitExpression(path.node, delegate); | ||
if (ast_types_1.namedTypes.CallExpression.check(pNode)) { | ||
@@ -124,3 +105,3 @@ pNode.arguments[name] = expr; | ||
this.traverse(path); | ||
} | ||
}, | ||
}); | ||
@@ -130,27 +111,29 @@ return escodegen_1.generate(ast); | ||
(function (degenerator) { | ||
degenerator.supportsAsync = supports_async_1.default; | ||
function compile(code, returnName, names, options = {}) { | ||
const output = supports_async_1.default ? 'async' : 'generator'; | ||
const compiled = degenerator(code, names, Object.assign(Object.assign({}, options), { output })); | ||
const fn = vm_1.runInNewContext(`${compiled};${returnName}`, options.sandbox, options); | ||
const compiled = degenerator(code, names); | ||
const vm = new vm2_1.VM(options); | ||
const fn = vm.run(`${compiled};${returnName}`); | ||
if (typeof fn !== 'function') { | ||
throw new Error(`Expected a "function" to be returned for \`${returnName}\`, but got "${typeof fn}"`); | ||
} | ||
if (isAsyncFunction(fn)) { | ||
return fn; | ||
} | ||
else { | ||
const rtn = generator_to_promise_1.default(fn); | ||
Object.defineProperty(rtn, 'toString', { | ||
value: fn.toString.bind(fn), | ||
enumerable: false | ||
}); | ||
return rtn; | ||
} | ||
const r = function (...args) { | ||
try { | ||
const p = fn.apply(this, args); | ||
if (typeof p.then === 'function') { | ||
return p; | ||
} | ||
return Promise.resolve(p); | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
}; | ||
Object.defineProperty(r, 'toString', { | ||
value: fn.toString.bind(fn), | ||
enumerable: false, | ||
}); | ||
return r; | ||
} | ||
degenerator.compile = compile; | ||
})(degenerator || (degenerator = {})); | ||
function isAsyncFunction(fn) { | ||
return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'; | ||
} | ||
/** | ||
@@ -157,0 +140,0 @@ * Returns `true` if `node` has a matching name to one of the entries in the |
{ | ||
"name": "degenerator", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "Compiles sync functions into async generator functions", | ||
@@ -30,3 +30,4 @@ "main": "dist/src/index", | ||
"escodegen": "^1.8.1", | ||
"esprima": "^4.0.0" | ||
"esprima": "^4.0.0", | ||
"vm2": "^3.9.3" | ||
}, | ||
@@ -33,0 +34,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
1
18515
4
5
202
+ Addedvm2@^3.9.3
+ Addedacorn@8.12.1(transitive)
+ Addedacorn-walk@8.3.4(transitive)
+ Addedvm2@3.9.19(transitive)