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

@pestras/micro-nats

v1.2.7

Published

pestras microservice plugin for nats messaging server support

Downloads

27

Readme

Pestras Micro Nats

Pestras microservice plugin for nats messaging server support.

install

npm i @pestras/micro @pestras/micro-nats

Plug In

import { SERVICE, Micro } from '@pestras/micro';
import { MicroNats } from '@pestras/micro-nats';

Micro.plugins(new MicroNats());

@SERVICE()
class test {}

Micro.start(Test);

MicroNats class accepts a single optional argument connection.

Name | Type | Default | Description ---- | ----- | ------ | ----- connection | string | number | NatsConnectionOptions | 'localhost:4222' | see Nats Docs

SUBJECT DECORATOR

Used to subscribe to nats server pulished subjects, and also accepts a subject string as a first argument and an optional config object.

Name | Type | Default | Description --- | --- | --- | --- hooks | string[] | [] | hooks methods that should be called before the route handler dataQuota | number | 1024 * 100 | Subject msg data size limit payload | Nats.Payload | Payload.JSON | see Nats Docs options | Nats.SubscriptionOptions | null | see Nats Docs meta | any | extra details that will be passed to the handler, useful for multiple subjects

import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

@SERVICE({ workers: 3 })
class Email {
  
  async auth(nats: Client, msg: NatsMsg, handlerName: string) {
    // if hook failed its purpose should check for msg reply if exists and return false
    if (msg.reply) {
      nats.publish(msg.replay, { error: 'some error' })
      return false
    }

    // otherwise
    return true;
  }

  @SUBJECT('user.insert', {
    hooks: ['auth'],
    options: { queue: 'emailServiceWorker' }
  })
  sendActivationEmail(nats: Client, msg: NatsMsg) {
    let auth = msg.data.auth;
  }

Hooks must return or resolve (async) to true on success or false on failure.

Multible Subjects

Multible subjects can be used on the same handler.

import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

interface MsgInput { id: string; email: string }

@SERVICE()
class Email {

  @SUBJECT('emails.new', { meta: { template: "newEmail" } })
  @SUBJECT('emails.reactivate', { meta: { template: "reactivateEmail" } })
  sendActivataionEmail(client: Client, msg: NatsMsg<MsgInput>, meta: any) {
    // send email
    let emailTemplate = meta.template;
  }
}

Sub Services

// comments.service.ts
import { SUBJECT, NatsEvents } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

export class Comments implements NatsEvents {

  onNatsConnected(client: Client) {
    // ...
  }
  
  validate(client: Client, msg: NatsMsg, handlerName: string) { return true }
  
  @SUBJECT('newComment', {
    // auth hook from the main service
    // validate hook from the local service (sub service)
    hooks: ['auth', 'validate']
  })
  create(client: Client, msg: NatsMsg) {
    
  }
}
// main.ts
import { Micro, SERVICE } from '@pestras/micro';
import { SUBJECT, NATS_HOOK } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

@SERVICE()
class Articles {

  onInit() {    
    Micro.store.someSharedValue = "shared value";
  }
  
  async auth(client: Client, msg: NatsMsg, handlerName: string) {
    return true;
  }
  
  validate(client: Client, msg: NatsMsg, handlerName: string) {
    return true;
  }

  @SUBJECT('newArticle', {
    // both hooks from the main service
    hooks: ['auth', 'validate']
  })
  create(client: Client, msg: NatsMsg) {
    
  }
}

// pass sub services as an array to the second argument of Micro.start method
Micro.start(Articles, [Comments]);
  • Local hooks has the priority over main service hooks.
  • Subservices have their own lifecycle events.

lifecycle Events

onNatsConnected

Called whenever nats driver has a successfull connection

import { SERVICE, Micro } from '@pestras/micro';
import { SUBJECT, NatsMsg, NatsEvents } from '@pestras/micro-nats';
import { Client, Payload} from 'ts-nats';

Micro.plugins(new MicroNats());

interface MsgInput { id: string; email: string }

@SERVICE()
class Email implements NatsEvents {

  onNatsConnected(client: Client) {
    // ...
  }

  @SUBJECT('emails.new')
  sendActivataionEmail(client: Client, msg: NatsMsg<MsgInput>) {
    // send email
  }
}

Thank you