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

event-streamer

v0.2.4

Published

Simple event-streaming framework

Downloads

9

Readme

Event Streamer

WARNING!

This is a very early release!! It's not intended to be used yet, it still lacks some critical features

Description

Event Streamer is a simple framework for building microservices connected by event streams.

One of the biggest issues with event based systems is having a good documentation of which events are being produced and consumed by different microservices, so this framework takes a self-documenting approach, all actions have metadata available with which events they might produce.

The framework is intended to be used with Typescript, though Javascript should work, you won't be able to take advantage of the type checks.

Installation

npm:

$ npm install event-streamer

yarn:

$ yarn add event-streamer

Usage

You need to initialize the server with a router implementation.

import { Router, BaseServer } from 'event-streamer';

const router = new Router();
const myServerImplementation: BaseServer = new MyCustomServer(router);

You have two types of router available. The default Router will process the actions in parallel and return the output events as soon as they are emitted. There's also a SequentialRouter that will process events in the order they are received (i.e. it will not process the next event until the previous one finishes).

KafkaServer

You can use the KafkaServer implementation to consume/produce from Kafka

import * as config from 'config';
import { KafkaServer } from 'event-streamer';
import { router } from './router';

const server = new KafkaServer(router, config.get('kafka'));
server.start();

The configuration options are:

export interface KafkaConfiguration {
  producer?: {
    'client.id'?: string,
    'metadata.broker.list'?: string,
    'compression.codec'?: string,
    'retry.backoff.ms'?: number,
    'message.send.max.retries'?: number,
    'socket.keepalive.enable'?: boolean,
    'queue.buffering.max.messages'?: number,
    'queue.buffering.max.ms'?: number,
    'batch.num.messages'?: number
  };
  consumer?: {
    'group.id'?: string,
    'metadata.broker.list'?: string
  };
  consumerTopics: string[];
  consumerTopicConfiguration?: {};
  producerTopic?: string;
  rest?: {
    url?: string
  };
}

Then you need to add routes to the router

router.add(AnInputEventClass, AnActionClass);

Events should implement the BaseEvent class:

import { BaseEvent } from 'event-streamer';

export class AnInputEventClass extends BaseEvent {
  someParam: string;
  build(eventArgs: {}) {
    this.someParam = eventArgs.someParam;
  }
}

Actions should implement the Action class.

import { Action } from 'event-streamer';
import { AnOutputEventClass } from './my-output-event';

export class AnActionClass extends Action {
  private emitOutput = this.emitter(AnOutputEventClass);

  async perform(inputEvent: AnInputEventClass) {
    if (inputEvent.someParam === 'whatever') {
      this.emitOutput(new AnOutputEventClass({
        extraParam: `${inputEvent.someParam} output`
      }));
    }
  }
}

The action is considered finished when the perform promise resolves.

Triggering events locally

You can start an action locally by calling the trigger method on the KafkaServer

server.trigger(new CustomEvent(someParams));

Testing

A test server is provided to write functional tests:

import { Router, TestServer, TestEvent } from 'event-streamer';

describe('AnActionClass', () => {
  it('responds with AnOutputEvent to AnInputEvent', async () => {
    const router = new Router();
    const server = new TestServer(router)
    loadRoutes(router);
    server.inputEvent({ code: 'AnOutputEvent', someParam: 'whatever' });
    const published = await server.publishedEvents();
    expect(published[0].extraParam).toEqual('whatever output');
  })
});