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

matrixes-lib

v1.1.4

Published

Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.

Downloads

10

Readme

matrixes-lib

Node.js module for create a gRPC server as Microservices or Koa server as ApiGateway.

Aim

This project was forked from agreatfool/sasdn, and was intended to separate Command Tools and gRpc Server Source Code.

  • other difference
    • Remove bluebird, and use util.promisify
    • The NPM package size is more littler, and install more faster

Install

npm install matrixes-lib -save

How to user

Create gRPC Server

import { RpcApplication } from 'matrixes-lib';
import { registerServices } from './services/Register';

class Server {
    private _initialized: boolean;
    public app: RpcApplication;

    constructor() {
        this._initialized = false;
    }

    public async init(isDev: boolean = false): Promise<any> {
        this.app = new RpcApplication();
        this._initialized = true;

        return Promise.resolve();
    }

    public start(): void {
        if (!this._initialized) {
            return;
        }

        registerServices(this.app);

        const host = '0.0.0.0';
        const port = '8080';
        this.app.bind(`${host}:${port}`).start();
        console.log(`server start, Address: ${host}:${port}!`);
    }
}


const server = new Server();
server.init(process.env.NODE_ENV === 'development')
    .then(() => {
        server.start();
    })
    .catch((error) => {
        console.log(`MicroService init failed error = ${error.stack}`);
    });

process.on('uncaughtException', (error) => {
    console.log(`process on uncaughtException error = ${error.stack}`);
});

process.on('unhandledRejection', (error) => {
    console.log(`process on unhandledRejection error = ${error.stack}`);
});

Create gRPC Client

import * as grpc from 'grpc';
import {Duplex, Readable, Writable} from 'stream';
import {BookServiceClient} from './proto/book/book_grpc_pb';
import {Book, GetBookRequest, GetBookViaAuthorRequest} from './proto/book/book_pb';
import MSBookServiceClient from './clients/book/MSBookServiceClient';

const md = new grpc.Metadata();
md.set('name', 'fengjie');

let bookClient = new MSBookServiceClient('127.0.0.1:8080');

function getBook() {
    const request = new GetBookRequest();
    request.setIsbn(6);

    bookClient.getBook(request, md)
        .then((res) => {
            console.log(`[getBook] response: ${JSON.stringify(res.toObject())}`);
            console.log(`[getBook] done`);
        })
        .catch((err) => {
            console.log(`[getBook] err: ${err.message}`);
            console.log(`[getBook] done`);
        });
}

getBook();

Create API Gateway Server

import {Koa, KoaBodyParser} from 'matrixes-lib';
import RouterLoader from './router/Router';

class Server {
    private _initialized: boolean;
    public app: Koa;

    constructor() {
        this._initialized = false;
    }

    public async init(isDev: boolean = false): Promise<any> {

        await RouterLoader.instance().init();

        this.app = new Koa();
        this.app.use(KoaBodyParser({ formLimit: '2048kb' }));
        this.app.use(RouterLoader.instance().getRouter().routes());
        this._initialized = true;

        return Promise.resolve();
    }

    public start(): void {
        if (!this._initialized) {
            return;
        }

        const host = '0.0.0.0';
        const port = '8081';
        this.app.listen(port, host, () => {
            console.log(`server start, Address: ${host}:${port}!`);
        });
    }
}


const server = new Server();
server.init(process.env.NODE_ENV === 'development')
    .then(() => {
        server.start();
    })
    .catch((error) => {
        console.log(`Gateway init failed error = ${error.stack}`);
    });

process.on('uncaughtException', (error) => {
    console.log(`process on uncaughtException error = ${error.stack}`);
});

process.on('unhandledRejection', (error) => {
    console.log(`process on unhandledRejection error = ${error.stack}`);
});

Test API Gateway

# api 
curl -d "isbn=1" "http://127.0.0.1:8081/v1/getBookUser"

# mock api when SET NODE_ENV=development
curl -d "isbn=1" "http://127.0.0.1:8081/v1/getBookUser?mock=1"

Tool Chain

Simple