Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Library for managing Coroutines using Harmony generators. Supports both Node core style callbacks and promises.
Library for managing Coroutines using Harmony generators. Supports both Node core style callbacks and promises.
Coro can be installed from NPM with the following:
npm install coro
To make use of the module you (currently) need to run at least 0.11.2 of Node with the --harmony flag.
The Coro API is considered stable for use in Node (YMMV in a browser). I'm planning to make it 1.0.0 when you no longer need to pass a flag to node to make it work.
Please note that the API did change between 0.4.3 and 0.5.0 (against expectation) to support setting the invocant and arguments of the generator function in a conveninet way. The change is that all resume functions are on the global coro object and should be called immediately to get a callback to pass to an asynchronous API.
Run a coroutine as follows:
var coro = require('coro');
coro.run(function * () {
// Coroutine goodness here
});
coro.run(generator, args, callback) takes the following arguments:
If no callback is passed, then a Promises A+ compatible promise is returned from coro.run().
Within the passed generator function, asynchronous APIs can be accessed synchronously. Two common patterns of asynchronous APIs are currently supported: ones that take a callback and ones that return a promise.
Coro makes it really easy to use a promise based API and access the resolved value synchronously. Just yield the promise returning function and you'll have the value (or an exception if the promise was rejected):
var coro = require('coro'),
read = require('q-io/fs').read;
coro.run(function * () {
try {
var fileContents = yield read('/some/file', 'r');
}
catch (e) {
// e is the value the promise was rejected wtih
}
});
The convention for asynchronous APIs in Node.js itself is for functions to take a callback argument. This callback is then invoked with an error as the first parameter. The following examples use the "exec" method from the child_process module, which runs a command. The callback passed to exec is invoked with an error (or null), the output of the command to STDOUT, and the output of the command to STDERR.
coro.resume() returns a function that will resume the current coroutine where it has been yielded. This function should be passed as the callback parameter to a to a callback based API. If the error parameter is set, then this is raised as an exception. Otherwise, the remaining parameters to the callback are returned from the call to yield in an array.
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
try {
var output = yield exec('echo -n hello', coro.resume());
console.log('STDOUT: ' + output[0] + ', STDERR: ' + output[1]);
}
catch (e) {
console.log('uh oh: ' + e.message);
}
});
If you're only interested in the first parameter (e.g. the output to STDOUT for exec), use coro.resumeFirst():
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
console.log('hello == ' + yield exec('echo -n hello', coro.resumeFirst()));
// still throws on error
});
The more general case of the above is coro.resumeNth(n). This allows you to pick out any single parameter rather than just the first. For example, to capture the output to STDERR only for exec:
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
// in this case an empty string will be yielded
console.log('STDERR: ' + yield exec('echo -n hello', coro.resumeNth(1)));
// still throws on error
});
The careful observer will notice that there's a problem with the above methods. Where there is an error (e.g. the command exits with a non-zero exit status) and an exception is thrown, there's no way to access the other parameters to the callback (e.g. the output to STDERR). Where you need this, you can use the coro.resumeNoThrow() function:
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
// destructuring assignment will be great for this
var result = yield exec('echo -n hello', coro.resumeNoThrow()),
err = result[0], stdout = result[1], stderr = result[2];
// still throws on error
});
This is also useful for callback APIs that don't follow Node's conventions for indicating an error in the first parameter.
These allow you to pick out individual parameters to the callback, rather than have yield return an array of parameters.
Some styles of callback API have a separate callback just for signalling errors (so called "errbacks"). For these use coro.resumeThrow(). In this example we're using the read method from the q-io/fs NPM module to read in a file:
var coro = require('coro'),
read = require('q-io/fs').read;
coro.run(function * () {
try {
var fileContents = yield read('/some/file', 'r').then(
coro.resumeNoThrowFirst(),
coro.resumeThrow()
);
}
catch (e) {
// e is the parameter to the errback if there is an error
}
});
Using this method for handling promises isn't recommended - use the built in promise support described above.
There are two ways to track when your coroutine completes (i.e. returns) or throws an error - providing a callback and using a returned promise. These mechanisms make it trivial to compose coroutines by having one wait for the completion of another.
You can pass a callback function as a third parameter to the run function. This follows the convention of taking an error as the first parameter (or null if successful), followed by the return value (or null if an exception is raised):
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
return yield exec('echo -n hello', coro.resumeFirst());
}, function (err, output) {
if (err) {
// deal with error
}
else {
// deal with output
}
});
If a callback is not passed, a Promises A+ compatible promise is returned from the run function. This is either resolved with the return value or rejected with an exception:
var coro = require('coro'),
exec = require('child_process').exec;
coro.run(function * () {
return yield exec('echo -n hello', coro.resumeFirst());
}).then(function (output) {
// deal with output
}, function (err) {
// deal with errors
});
If you'd like to maintain the value of this
inside a coroutine without having to call call
or apply
on the run method, then you can add a run method to your own object using the coro.boundRun method:
var coro = require('coro');
var MyThing = function () {};
MyThing.prototype.run = coro.boundRun;
MyThing.prototype.doSomething = function () {
this.run(function * () {
// this is the instance of my thing
});
};
This is convenient as it allow coroutines to arbtrarily nested while maintaining the value of this
.
The MIT License (MIT)
Copyright (C) 2013 by the project's contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Library for managing Coroutines using Harmony generators. Supports both Node core style callbacks and promises.
The npm package coro receives a total of 2 weekly downloads. As such, coro popularity was classified as not popular.
We found that coro demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.