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

cocaine

v0.12.1-r16

Published

Node.js framework for Cocaine platform

Downloads

4

Readme

Cocaine NodeJS Framework

Examples of usage

Create NodeJS app for Cocaine cloud

Let's start with simple NodeJS http application.

var http = require('http')

var server = new http.Server(function(req, res){
    var body = []
    req.on('data', function(data){
        body.push(data)
    })
    req.on('end', function(){
        res.writeHead(200, {
          'x-any-header': 'x-any-value',
          'content-type': 'text/plain'
        })
        res.end('hello, Cocaine!')
    })
})

server.listen(8080)

To get our app working in Cocaine cloud, let's add just a couple of things.

#!/path/to/node
var cocaine = require('cocaine')
var http = cocaine.http // monkey-patches node's original http server

var argv = require('optimist').argv //which is actually a hash
// looking like { opt: 'value'}

var worker = new cocaine.Worker(argv)

var handle = worker.getListenHandle("http") // the handle implements a
// low-level nodejs' listening tcp socket, and it makes nodejs
// understand cocaine streams.

var server = new http.Server(...) // the same thing as above

server.listen(handle) // as per [1], start listening on cocaine handle

To let the cocaine-runtime know what to run in our app, we put manifest.json:

{ "slave":"app.js" }

Since the app.js has to be an executable, we put shebang on first line and don't forget about setting an executable bit.

See the complete app here [2].

Deploy app to the cloud

git clone url/the_app
cd the_app
npm install
tar -czf ../the_app.tgz
cocaine-tool app upload -n the_app --package ../the_app.tgz --manifest manifest.json
>app the_app has been successfully uploaded

then,

cocaine-tool app start -n the_app -r default
>app the_app started
curl -v http://<cloud.front>/the_app/http/
>...

Make use of Cocaine services


var cocaine = require("cocaine")

var cli = new cocaine.Client(["localhost", 10053])

var log = new cli.Logger("myprefix") // logs lines like "myprefix/..."

cli.on('error', function(err){
    console.log('client error', err)
})

log.on('error', function(err){
    console.log('logger error', err)
})


log.connect()

log.on("connect", function() {

    cli.getServices(['geobase'], function(err, geo, ua){
        var names
        
        log.info("looking up regionId for ip 1.2.3.4")
        
        geo.region_id("1.2.3.4", function(err, regionId) {
            if(err) return _handleError(err)

            log.debug("found region %d for %s", regionId, "1.2.3.4")

            geo.names(regionId, function(err, names){
                if(err) return _handleError(err)

                log.debug("names for region %d are %s", regionId, names.join())

                geo.coordinates(regionId, function(coords){
                    if(err) return _handleError(err)

                    log.debug('coordinates for region %d are %s', regionId, coords.join())

                })
            })
        })
    })
})

function _handleError(err){
    console.log('service error', err)
}

See client-simple for complete source of the simplest cocaine client app.

Use Cocaine services from the outside of the cloud

To fully control a client to services, you can use Client. It resolves services for you, keeps services cache, and resets resolved services cache on locator disconnect.

var cli = new require('cocaine').Client()

var storage = cli.Service('storage')

storage.on('error', function(err){
    // reconnect on network error
})

storage.connect()

storage.on('connect', function(){
    storage0.write('collection','key','value', function(err){
        if(err){
            console.log('error writing to storage', err)
            return
        }
        
        console.log(done 'writing to storage')
    })
})

See client-reconnect for example of handling various socket-level failures when connecting and communicating to locator and target services.

Access your application as a Cocaine service

var cli = new require('cocaine').Client()
var app = cli.Service('the_app')

app.connect()

app.on('connect', function(){
    app.enqueue('handle','anydata', function(err, result){
        if(err) {
           console.log('app error', err)
        } else {
          console.log('app response is', result)
        }
    })
})

References

[1] http://nodejs.org/api/net.html#net_server_listen_handle_callback