degenerator
Advanced tools
Comparing version 1.0.4 to 2.0.0
{ | ||
"name": "degenerator", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "Turns sync functions into async generator functions", | ||
"main": "index.js", | ||
"main": "dist/src/index", | ||
"typings": "dist/src/index", | ||
"files": [ | ||
"dist/src" | ||
], | ||
"scripts": { | ||
"test": "mocha --reporter spec test/test.js" | ||
"prebuild": "rimraf dist", | ||
"build": "tsc", | ||
"postbuild": "cpy --parents src test '!**/*.ts' dist", | ||
"test": "mocha --reporter spec dist/test/test.js", | ||
"test-lint": "eslint src --ext .js,.ts", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -14,11 +23,30 @@ "author": "Nathan Rajlich <nathan@tootallnate.net> (http://n8.io/)", | ||
}, | ||
"engines": { | ||
"node": ">= 6" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"esprima": "3.x.x", | ||
"escodegen": "1.x.x", | ||
"ast-types": "0.x.x" | ||
"ast-types": "^0.13.2", | ||
"escodegen": "^1.8.1", | ||
"esprima": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "3.x.x" | ||
"@types/escodegen": "^0.0.6", | ||
"@types/esprima": "^4.0.2", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^10.5.3", | ||
"@typescript-eslint/eslint-plugin": "1.6.0", | ||
"@typescript-eslint/parser": "1.1.0", | ||
"cpy-cli": "^2.0.0", | ||
"eslint": "5.16.0", | ||
"eslint-config-airbnb": "17.1.0", | ||
"eslint-config-prettier": "4.1.0", | ||
"eslint-import-resolver-typescript": "1.1.1", | ||
"eslint-plugin-import": "2.16.0", | ||
"eslint-plugin-jsx-a11y": "6.2.1", | ||
"eslint-plugin-react": "7.12.4", | ||
"mocha": "^6.2.0", | ||
"rimraf": "^3.0.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
degenerator | ||
=========== | ||
### Turns sync functions into async generator functions | ||
[![Build Status](https://travis-ci.org/TooTallNate/node-degenerator.svg?branch=master)](https://travis-ci.org/TooTallNate/node-degenerator) | ||
### Turns sync functions into async functions | ||
[![Build Status](https://github.com/TooTallNate/node-degenerator/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-degenerator/actions?workflow=Node+CI) | ||
Sometimes you need to write sync looking code that's really async under the hood. | ||
This module takes a String to one or more synchronous JavaScript functions, and | ||
returns a new String that with those JS functions transpiled into ES6 Generator | ||
Functions. | ||
returns a new String that with those JS functions transpiled into `async` | ||
functions. | ||
So this: | ||
``` js | ||
function foo () { | ||
```js | ||
function foo() { | ||
return a('bar') || b(); | ||
@@ -21,12 +21,10 @@ } | ||
``` js | ||
function* foo() { | ||
return (yield a('bar')) || (yield b()); | ||
```js | ||
async function foo() { | ||
return await a('bar') || await b(); | ||
} | ||
``` | ||
From there, you can provide asynchronous thunk-based or Generator-based | ||
implementations for the `a()` and `b()` functions, in conjunction with any | ||
Generator-based flow control library to execute the contents of the | ||
function asynchronously. | ||
With the compiled output code, you can evaluate the code using the `vm` module | ||
in Node.js, or save the code to a file and require it, or whatever. | ||
@@ -39,3 +37,3 @@ | ||
``` bash | ||
```bash | ||
$ npm install degenerator | ||
@@ -55,6 +53,6 @@ ``` | ||
``` js | ||
function myFn () { | ||
var one = get('https://google.com'); | ||
var two = get('http://nodejs.org'); | ||
var three = JSON.parse(get('http://jsonip.org')); | ||
function myFn() { | ||
const one = get('https://google.com'); | ||
const two = get('http://nodejs.org'); | ||
const three = JSON.parse(get('http://jsonip.org')); | ||
return [one, two, three]; | ||
@@ -64,3 +62,3 @@ } | ||
Now we can compile this into an asyncronous generator function, implement the | ||
Now we can compile this into an asyncronous function, implement the | ||
async `get()` function, and finally evaluate it into a real JavaScript function | ||
@@ -70,10 +68,9 @@ instance with the `vm` module: | ||
``` js | ||
var co = require('co'); | ||
var vm = require('vm'); | ||
var degenerator = require('degenerator'); | ||
```typescript | ||
import vm from 'vm'; | ||
import degenerator from 'degenerator'; | ||
// the `get()` function is thunk-based (error handling omitted for brevity) | ||
function get (endpoint) { | ||
return function (fn) { | ||
// The `get()` function is Promise-based (error handling omitted for brevity) | ||
function get(endpoint: string) { | ||
return new Promise((resolve, reject) => { | ||
var mod = 0 == endpoint.indexOf('https:') ? require('https') : require('http'); | ||
@@ -86,25 +83,17 @@ var req = mod.get(endpoint); | ||
res.on('end', function () { | ||
fn(null, data); | ||
resolve(data); | ||
}); | ||
}); | ||
}; | ||
}); | ||
} | ||
// convert the JavaScript string provided from the user (assumed to be `str` var) | ||
// Convert the JavaScript string provided from the user (assumed to be `str` var) | ||
str = degenerator(str, [ 'get' ]); | ||
// at this stage, you could use a transpiler like `facebook/regenerator` | ||
// here if desired. | ||
// Turn the JS String into a real async function instance | ||
const asyncFn = vm.runInNewContext(`(${str})`, { get }); | ||
// turn the JS String into a real GeneratorFunction instance | ||
var genFn = vm.runInNewContext('(' + str + ')', { get: get }); | ||
// use a generator-based flow control library (`visionmedia/co`, `jmar777/suspend`, | ||
// etc.) to create an async function from the generator function. | ||
var asnycFn = co(genFn); | ||
// NOW USE IT!!! | ||
asyncFn(function (err, res) { | ||
// ... | ||
// Now we can invoke the function asynchronously | ||
asyncFn().then((res) => { | ||
// Do something with `res`... | ||
}); | ||
@@ -119,4 +108,3 @@ ``` | ||
Returns a "degeneratorified" JavaScript string, with ES6 Generator | ||
functions transplanted. | ||
Returns a "degeneratorified" JavaScript string, with `async`/`await` transplanted. | ||
@@ -123,0 +111,0 @@ |
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
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
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
19034
0
17
6
184
130
1
+ Addedast-types@0.13.4(transitive)
- Removedast-types@0.16.1(transitive)
- Removedesprima@3.1.3(transitive)
Updatedast-types@^0.13.2
Updatedescodegen@^1.8.1
Updatedesprima@^4.0.0