npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cofy

v2.0.2

Published

Cofy is a bridge connecting callback style object to sequential style in co environment with one punch.

Downloads

38

Readme

cofy

Build Status Coverage Status NPM version Dependency Status

NPM

Cofy is a bridge connecting callback style object to sequential style in co or koa environment with one punch.

##Installation

$ npm install cofy --save

NOTE: default prefix has changed to '$'.

Some examples

use cofy in redis.

'use strict';
var co = require("co");
var cofy = require("cofy");
var redis = require('redis');

//enable redis to has a co ablitiy.
cofy.class(redis.RedisClient);

//now we can use mysql in co 
var client = redis.createClient(6379,'localhost');
co(function * () {
	//use class.$fn
	var r1 = yield client.$set('a' ,1);
	var r2 = yield client.$get('a');
	var r3 = yield client.$hmset('h' , {k1:'v1' , k2:'v2'});
	var r4 = yield client.$hget('h' ,'k1');
	console.log(r1,r2);
	console.log(r3,r4);
})();

use cofy in mysql.

'use strict';
var co = require("co");
var cofy = require("cofy");
var mysql = require('mysql');

//(felixge/node-mysql) has no extends entrance. so we can do this.
var PoolConnection = require('mysql/lib/PoolConnection.js');
var Pool = require('mysql/lib/Pool.js');
var PoolCluster = require('mysql/lib/PoolCluster.js');

//enable mysql to has a co ablitiy.
cofy.class(PoolConnection);
cofy.class(Pool);
cofy.class(PoolCluster);

var pool = mysql.createPool({
	host: "localhost",
    database: "db",
    user: "root",
    password: "pass",
});
//now we can use mysql in co 
co(function * () {
	var con = yield pool.$getConnection();
	//pay attention on precedence of operator! yield < [] .
	var result = (yield con.$query("select 1+1"))[0]; 
	console.log(result);
	con.release();
})();


//use it in transaction
co(function*(){
	var con ;
	try{
		con = yield pool.$getConnection();
		yield con.$beginTransaction();
		yield con.$query("select some");
		yield con.$query("insert some");
		yield con.$query("update some");
		yield con.$commit();
	}catch(e){
		yield con.$rollback();
		console.error(e);
		//to do 
	}finally{
		if(con){
			con.release();
		}
	}
})();

use cofy in request.

'use strict';
var request = require('request');
var cofy = require('cofy');
var co = require('co');

var $request = cofy.fn(request);

co(function*(){
	var r = yield $request({url:"http://www.google.com"});
	console.log(r);
})();

cofy nodejs fs api:

'use strict';
var cofy = require('../');
var co = require('co');

var fs = require('fs');
cofy(fs);

//fs.exists callback function has no error. we call set `throwable` to false.
fs.$exists = cofy.object(fs.exists , false ,fs);	

co(function*(){
	var file = '/hello.txt';
	if(!(yield fs.$exists(file))){
		return;
	}
	var r = yield fs.$readFile(file,'utf8');
	console.log('fda' , r);
	//also you can use it in old way
	fs.readFile(file , 'utf8' , function(e,r){
		if(e){
			throw e;
		}
		console.log(r);
	});
})();

cofy API

class(constructor,[throwable],[methods],[prefix])

Enalbe a class or a object or a function to has co ability.

  • constructor {function} - cofy target.the constructor of the class;
  • throwable {bool} - if throwable is true the first argument of callback function arguments will be deemed as a exception.if throwable is false,no exception will be throwed ,all arguments will be returned. default is true.
  • methods {array} - Array,if exists ,cofy will only cofy the methods in the target.
  • prefix {string} - cofy will add function to the target(except target is function),the function name has a prefix. default is $.

object(object,[throwable],[methods],[prefix],[context])

  • object {function} - cofy target.the constructor of the class;
  • throwable {bool} - if throwable is true the first argument of callback function arguments will be deemed as a exception.if throwable is false,no exception will be throwed ,all arguments will be returned. default is true.
  • context {object} - the function execute context.
  • methods {array} - Array,if exists ,cofy will only cofy the methods in the target.
  • prefix {string} - cofy will add function to the target(except target is function),the function name has a prefix. default is $.

fn(fn,[throwable],[context])

  • fn {function} - cofy a function; function(args , cb) -> yeild function(args)
  • throwable {bool} - if throwable is true the first argument of callback function arguments will be deemed as a exception.if throwable is false,no exception will be throwed ,all arguments will be returned. default is true.
  • context {object} - the function execute context.