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

@artusx/plugin-grpc

v1.1.5-5

Published

grpc plugin for artusx

Downloads

768

Readme

@artusx/plugin-grpc

Plugin

export default {
  grpc: {
    enable: true,
    package: '@artusx/plugin-grpc',
  },
};

Config

import path from 'path';
import type { ArtusXGrpcConfig } from '@artusx/plugin-grpc';

export default () => {
  const grpc: ArtusXGrpcConfig = {
    client: {      
      addr: "0.0.0.0:50051",
    },

    server: {      
      addr: "0.0.0.0:50051",
    },

    static: {
      proto: path.resolve(__dirname, '../proto'),
      codegen: path.resolve(__dirname, '../proto-codegen'),
    },

    dynamic: {
      proto: [path.resolve(__dirname, '../echo-module/proto/echo.proto')],
    },
  };

  return {
    grpc,
  };
};

Static

Proto

syntax = "proto3";

package chat_package;

message ServerMessage {
  string user = 1;
  string text = 2;
}

message ClientMessage {
  string user = 1;
  string text = 2;
}

service Chat {
  rpc join(ClientMessage) returns (ServerMessage) {}
  rpc send(ClientMessage) returns (ServerMessage) {}  
}

Server

import * as grpc from '@grpc/grpc-js';
import { GrpcService, GrpcMethod } from '@artusx/plugin-grpc';
import { ClientMessage, ServerMessage, UnimplementedChatService } from '../proto-codegen/chat';

@GrpcService({
  packageName: 'chat_package',
  serviceName: 'Chat',
  definition: UnimplementedChatService.definition,
})
export default class ChatService extends UnimplementedChatService {
  @GrpcMethod({
    enable: true,
  })
  join(
    call: grpc.ServerUnaryCall<ClientMessage, ServerMessage>,
    callback: grpc.requestCallback<ServerMessage>
  ): void {
    console.log('server:Chat:join', call.request.toObject());
    callback(null, new ServerMessage({ user: call.request.user, text: 'method.join.callback' }));
  }

  @GrpcMethod({
    enable: true,
  })
  send(
    call: grpc.ServerUnaryCall<ClientMessage, ServerMessage>,
    callback: grpc.requestCallback<ServerMessage>
  ): void {
    callback(null, new ServerMessage({ user: call.request.user, text: 'method.join.callback' }));
  }
}

Client

import assert from 'assert';
import * as grpc from '@grpc/grpc-js';
import { Inject, ArtusInjectEnum, Injectable, ScopeEnum } from '@artus/core';
import { ChatClient as Client } from '../proto-codegen/chat';

@Injectable({
  scope: ScopeEnum.EXECUTION,
})
export default class ChatClient extends Client {
  constructor(@Inject(ArtusInjectEnum.Config) public config: any) {
    const { addr } = config.grpc?.client || {};
    assert(addr, 'addr is required');
    super(addr,  grpc.credentials.createInsecure());
  }  
}

Invoke

import { Inject } from '@artus/core';
import { Schedule } from '@artusx/plugin-schedule';
import type { ArtusXSchedule } from '@artusx/plugin-schedule';
import { ClientMessage } from '../proto-codegen/chat';
import ChatClient from './chat.client';

@Schedule({
  enable: true,
  cron: '30 * * * * *',
  runOnInit: true,
})
export default class NotifySchedule implements ArtusXSchedule {
  @Inject(ChatClient)
  chatClient: ChatClient;

  private async invokeStatic() {    
    const message = new ClientMessage({
      user: '@client',
      text: 'hello',
    });

    try {
      const response = await this.chatClient.join(message);
      console.log('client:Chat:join', response.toObject());
    } catch (error) {
      console.error('error:', error.details);
    }
  }

  async run() {
    await this.invokeStatic();
  }
}

Dynamic

Proto

syntax = "proto3";

option go_package = "google.golang.org/grpc/examples/features/proto/echo";

package grpc.examples.echo;

// EchoRequest is the request for echo.
message EchoRequest {
  string message = 1;
}

// EchoResponse is the response for echo.
message EchoResponse {
  string message = 1;
}

// Echo is the echo service.
service Echo {
  // UnaryEcho is unary echo.
  rpc UnaryEcho(EchoRequest) returns (EchoResponse) {}
  // ServerStreamingEcho is server side streaming.
  rpc ServerStreamingEcho(EchoRequest) returns (stream EchoResponse) {}
  // ClientStreamingEcho is client side streaming.
  rpc ClientStreamingEcho(stream EchoRequest) returns (EchoResponse) {}
  // BidirectionalStreamingEcho is bidi streaming.
  rpc BidirectionalStreamingEcho(stream EchoRequest) returns (stream EchoResponse) {}
}

Server

import * as grpc from '@grpc/grpc-js';
import { GrpcService, GrpcMethod } from '@artusx/plugin-grpc';

interface EchoRequest {
  message: string;
}

interface EchoResponse {
  message: string;
}

@GrpcService({
  packageName: 'grpc.examples.echo',
  serviceName: 'Echo',
})
export default class EchoService {
  @GrpcMethod({
    enable: true,
  })
  UnaryEcho(
    call: grpc.ServerUnaryCall<EchoRequest, EchoResponse>,
    callback: grpc.requestCallback<EchoResponse>
  ) {
    console.log('server:Echo:UnaryEcho', call.request);
    callback(null, {
      message: 'getMessage: ' + call.request.message,
    });
  }
}

Client

import assert from 'assert';
import { credentials } from '@artusx/plugin-grpc/types';
import { ArtusInjectEnum, Inject, Injectable, ScopeEnum } from '@artus/core';
import { ArtusXInjectEnum } from '@artusx/utils';
import { ArtusXGrpcClient } from '@artusx/plugin-grpc';

@Injectable({
  scope: ScopeEnum.EXECUTION,
})
export default class EchoClient {
  private echoService: any;

  constructor(
    @Inject(ArtusInjectEnum.Config) public config: any,
    @Inject(ArtusXInjectEnum.GRPC) public grpcClient: ArtusXGrpcClient,
  ) {
    const { addr } = config.grpc?.client || {};
    assert(addr, 'addr is required');

    const EchoService = grpcClient.getService('grpc.examples.echo', 'Echo');    
    this.echoService = new EchoService(addr, credentials.createInsecure());
  }

  UnaryEcho(call: any, callback: any) {
    this.echoService.UnaryEcho(call, callback)
  }
}

Invoke

import { Inject } from '@artus/core';
import { Schedule } from '@artusx/plugin-schedule';
import type { ArtusXSchedule } from '@artusx/plugin-schedule';

import EchoClient from './echo.client';

@Schedule({
  enable: true,
  cron: '30 * * * * *',
  runOnInit: true,
})
export default class NotifySchedule implements ArtusXSchedule {
  @Inject(EchoClient)
  echoClient: EchoClient;

  private async invokeDynamic() {    
    this.echoClient.UnaryEcho({ message: 'ping' }, function (_err: Error, response: any) {
      console.log('client:Echo:UnaryEcho', response);
    });
  }

  async run() {
    await this.invokeDynamic();
  }
}