degenerator
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -0,5 +1,7 @@ | ||
/// <reference types="node" /> | ||
import { Context } from 'vm'; | ||
/** | ||
* Turns sync JavaScript code into an JavaScript with async Functions. | ||
* | ||
* @param {String} jsStr JavaScript string to convert | ||
* @param {String} code JavaScript string to convert | ||
* @param {Array} names Array of function names to add `yield` operators to | ||
@@ -9,3 +11,3 @@ * @return {String} Converted JavaScript string with async/await injected | ||
*/ | ||
declare function degenerator(jsStr: string, _names: degenerator.DegeneratorNames, { output }?: degenerator.DegeneratorOptions): string; | ||
declare function degenerator(code: string, _names: degenerator.DegeneratorNames, { output }?: degenerator.DegeneratorOptions): string; | ||
declare namespace degenerator { | ||
@@ -17,3 +19,7 @@ type DegeneratorName = string | RegExp; | ||
} | ||
interface CompileOptions extends DegeneratorOptions { | ||
sandbox?: Context; | ||
} | ||
function compile<T>(code: string, returnName: string, names: DegeneratorNames, options?: CompileOptions): T; | ||
} | ||
export = degenerator; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const co_1 = require("co"); | ||
const util_1 = require("util"); | ||
const escodegen_1 = require("escodegen"); | ||
const esprima_1 = require("esprima"); | ||
const vm_1 = require("vm"); | ||
const ast_types_1 = require("ast-types"); | ||
const supports_async_1 = __importDefault(require("./supports-async")); | ||
/** | ||
* Turns sync JavaScript code into an JavaScript with async Functions. | ||
* | ||
* @param {String} jsStr JavaScript string to convert | ||
* @param {String} code JavaScript string to convert | ||
* @param {Array} names Array of function names to add `yield` operators to | ||
@@ -14,3 +20,3 @@ * @return {String} Converted JavaScript string with async/await injected | ||
*/ | ||
function degenerator(jsStr, _names, { output = 'async' } = {}) { | ||
function degenerator(code, _names, { output = 'async' } = {}) { | ||
if (!Array.isArray(_names)) { | ||
@@ -21,3 +27,3 @@ throw new TypeError('an array of async function "names" is required'); | ||
const names = _names.slice(0); | ||
const ast = esprima_1.parseScript(jsStr); | ||
const ast = esprima_1.parseScript(code); | ||
// First pass is to find the `function` nodes and turn them into async or | ||
@@ -118,2 +124,16 @@ // generator functions only if their body includes `CallExpressions` to | ||
} | ||
(function (degenerator) { | ||
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); | ||
if (supports_async_1.default) { | ||
return fn; | ||
} | ||
else { | ||
return co_1.wrap(fn); | ||
} | ||
} | ||
degenerator.compile = compile; | ||
})(degenerator || (degenerator = {})); | ||
/** | ||
@@ -151,3 +171,3 @@ * Returns `true` if `node` has a matching name to one of the entries in the | ||
else { | ||
throw new Error(`don't know how to get name for: ${callee.type}`); | ||
throw new Error(`Don't know how to get name for: ${callee.type}`); | ||
} | ||
@@ -154,0 +174,0 @@ return checkName(name, names); |
{ | ||
"name": "degenerator", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Turns sync functions into async generator functions", | ||
@@ -29,2 +29,3 @@ "main": "dist/src/index", | ||
"ast-types": "^0.13.2", | ||
"co": "^4.6.0", | ||
"escodegen": "^1.8.1", | ||
@@ -34,2 +35,3 @@ "esprima": "^4.0.0" | ||
"devDependencies": { | ||
"@types/co": "^4.6.2", | ||
"@types/escodegen": "^0.0.6", | ||
@@ -36,0 +38,0 @@ "@types/esprima": "^4.0.2", |
@@ -100,7 +100,14 @@ degenerator | ||
### degenerator(String jsStr, Array functionNames) → String | ||
### degenerator(code: string, names: Array<string|RegExp>, options?: DegeneratorOptions): String | ||
Returns a "degeneratorified" JavaScript string, with `async`/`await` transplanted. | ||
#### DegeneratorOptions | ||
An options object _may_ be passed in as the third parameter, with the following | ||
values: | ||
* `output` - string - May be `"async"` or `"generator"`, defaults to `"async"`. | ||
License | ||
@@ -107,0 +114,0 @@ ------- |
Sorry, the diff of this file is not supported yet
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
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
19805
8
225
137
0
4
18
2