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

caf_cli

v0.4.2

Published

Cloud Assistants browser and node.js client library for interacting with CAs

Downloads

34

Readme

Caf.js

Co-design permanent, active, stateful, reliable cloud proxies with your web app and gadgets.

See http://www.cafjs.com

Client Library

Build Status

This repository contains a Caf.js client library for browser (using browserify and native websockets), cloud, scripting, and gadget (node.js).

The interface is similar to the websocket client API, but Caf.js dynamically extends it with remote methods and local argument checking.

For example, with the CA:

exports.methods = {
    async __ca_init__() {
        this.state.counter = 0;
        return [];
    },
    async increment() {
        this.state.counter = this.state.counter + 1;
        return [null, this.state.counter];
    },
    async decrement() {
        this.state.counter = this.state.counter - 1;
        return [null, this.state.counter];
    }
};

and the client code:

var URL = 'http://root-hello.vcap.me:3000/#from=foo-ca1&ca=foo-ca1';
var s = new caf_cli.Session(URL);
s.onopen = async () => {
    try {
        let counter = await s.increment().getPromise();
        console.log(counter);
        counter = await s.decrement().getPromise();
        console.log('Final count:' + counter);
        s.close();
    } catch (ex) {
        s.close(ex);
    }
}
s.onclose = (err) => {
    if (err) {
        console.log(myUtils.errToPrettyStr(err));
        process.exit(1);
    }
    console.log('Done OK');
};

The methods increment and decrement magically appear in s after we open the session. I love JavaScript!

Remote invocations are always serialized, i.e., the session object locally buffers new requests until the previous ones have been processed. The session properties can be configured in the URL, or in an extra constructor argument. See {@link module:caf_cli/Session} for details.

Errors

There are two types of errors:

  • Application error: propagated in the callback or exception in await, no attempt to recover it, your logic knows best how to handle it.

  • System error: after all the attempts to recover fail, the error is propagated in the onclose handler. The session is no longer usable.

Triggering the onclose with no argument means the session closed normally, i.e., using its close() method.

Note that the onerror handler in the websocket interface is for internal use only. Just use onclose.

Multi-method

In some cases we want to execute multiple methods in a single transaction (see {@link external:caf_ca}). If one fails, we roll back all state changes and (delayed) external interactions.

This is easy in Caf.js, because methods that do not provide a callback are assumed to be multi-method calls:

...
s.onopen = async function() {
    try {
        const counter = await s.increment().decrement().getPromise();
        console.log('Final count:' + counter);
        s.close();
    } catch (err) {
        s.close(err);
    }
}
...

Notifications

The CA can also send notifications to one (or many) client(s), see {@link external:caf_session} for details. Notifications are processed in the onmessage handler:

...
s.onmessage = function(msg) {
    const notif = caf_cli.getMethodArgs(msg)[0];
    console.log('Got notification in client:' + notif);
};
...

See examples/helloworld for full code examples.

Other

Session objects also provide end-to-end encryption and time synchronization. See {@link module:caf_cli/cryptoSession} and {@link module:caf_cli/TimeAdjuster}.