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

@indigocore/agent

v0.3.0

Published

NodeJS module that exposes functions to create Stratumn agents using Javascript

Downloads

9

Readme

Indigo agent for NodeJS

This NodeJS module exposes functions to create Indigo agents using Javascript.

npm

Copyright 2017 Stratumn SAS. All rights reserved.

Unless otherwise noted, the Stratumn Agent Javascript Library source files are distributed under the Apache License 2.0 found in the LICENSE file.

Creating an HTTP server for an agent

var express = require('express');
var Agent = require('@indigocore/agent');
var plugins = Agent.plugins;

// Load actions.
// Assumes your actions are in ./lib/actions.
var actions = require('./lib/actions');

// Creates an HTTP store client to save segments.
// Assumes an HTTP store server is available on env.STRATUMN_STORE_URL or http://store:5000.
var storeHttpClient = Agent.storeHttpClient(process.env.STRATUMN_STORE_URL || 'http://store:5000');

// Creates an HTTP fossilizer client to fossilize segments.
// Assumes an HTTP fossilizer server is available on env.STRATUMN_FOSSILIZER_URL or http://fossilizer:6000.
var fossilizerHttpClient = Agent.fossilizerHttpClient(process.env.STRATUMN_FOSSILIZER_URL || 'http://fossilizer:6000');

// Creates an agent
var agent = Agent.create({
    agentUrl: 'http://localhost:3000',               // the agent needs to know its root URL,·
});

// Adds a process from a name, its actions, the store client, and the fossilizer client.
// As many processes as one needs can be added. A different storeHttpClient and fossilizerHttpClient may be used.
agent.addProcess("my_first_process", actions, storeHttpClient, fossilizerHttpClient, {
  plugins: [plugins.localTime]                     // pick any plugins from src/plugins or develop your own - order matters
});

// Creates an HTTP server for the agent with CORS enabled.
var agentHttpServer = Agent.httpServer(agent, { cors: {} });

// Create the Express server.
const app = express();
app.disable('x-powered-by');

// Mount agent on the root path of the server.
app.use('/', agentHttpServer);

// Create server by binding app and websocket connection
const server = Agent.websocketServer(app, storeHttpClient);

// Start the server
server.listen(3000, () => {
  console.log('Listening on ' + server.address().port);
});

// You can also add processes on-the-fly after the server has started listening
agent.addProcess("my_second_process", actions, storeHttpClient, fossilizerHttpClient, {
  plugins: [plugins.localTime]
});

The documentation for the HTTP API is available in doc/swaggerDoc.md. It uses OpenAPI ver. 2 (fka Swagger). You can also use doc/swagger.json with Swagger UI for instance:

docker run -p 8080:8080 -e SWAGGER_JSON=/opt/swagger.json -v $(pwd)/doc:/opt swaggerapi/swagger-ui

Advanced usage

  • create creates an agent instance.
  • storeHttpClient creates an instance to work with stores via HTTP.
  • fossilizerHttpClient creates an instance to work with fossilizers via HTTP.
  • httpServer creates an app for the agent
  • websocketServerbind websocket connection to app and returns server

Plugins

An agent plugin enriches the content of a segment. It may implement four methods:

  • willCreate(link) is called right before a transition function from the agent's actions. It takes the existing link as an argument. It should be updated in-place.

  • didCreateLink(link) is called whenever a link has been created by a transition function. It takes the new link as an argument. It should be updated in-place.

  • filterSegment(segment) is called when segments are retrieved by the agent from the underlying storage. It should return true if the plugins accepts the segment, false otherwise. Filters are applied sequentially in the reverse order they are defined.

All methods are optional. They can either be synchronous or return a Promise.

Available plugins:

  • agentUrl: Saves in segment meta the URL that can be used to retrieve a segment.
  • encryptedState: Encrypts the state before the segment is saved. Filters out segment that cannot be decrypted.
  • localTime: Saves the local timestamp in the link meta information.
  • stateHash: Computes and adds the hash of the state in meta.

Development

To regenerate the HTTP API documentation, run:

$ npm run swagger:generate