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

egg-rpc-generator

v2.0.0

Published

RPC tools for egg framework

Downloads

334

Readme

egg-rpc-generator

NPM version Test coverage npm download

RPC tools for egg framework

Install

$ npm i egg-rpc-generator -g

Usage

1. Command-line interface (cli) usage

$ egg-rpc-generator -h

  Usage: egg-rpc-generator [options]

  Options:

    -b, --base [base]            the base directory of the project
    -p, --plugin [plugin]        the plugins used in generation process
    -f, --framework [framework]  specify framework that can be absolute path or npm package
    -k, --keep-case              keeps field casing instead of converting to camel case
    -h, --help                   output usage information
  • -b, --base the egg project root folder, default is process.cwd()
  • -p, --plugin the plugins will be used in generation process, by default protobuf plugin will be activated
  • -f, --framework specify the custom egg framework name or path
  • -k, --keep-case keeps field casing instead of converting to camel case

run egg-rpc-generator under the egg project root folder

$ egg-rpc-generator

Create /home/admin/project proxy
[ProtoRPCPlugin] found "com.rpc.test.ProtoService" in proto file
[ProtoRPCPlugin] save all proto info into "/home/admin/project/run/proto.json"
------------------------------------------------
All done

2. Usage with package.json scripts

{
  "scripts": {
    "rpc": "egg-rpc-generator"
  }
}

Run arbitrary package scripts

$ npm run rpc

Create /home/admin/project proxy
[ProtoRPCPlugin] found "com.rpc.test.ProtoService" in proto file
[ProtoRPCPlugin] save all proto info into "/home/admin/project/run/proto.json"
------------------------------------------------
All done

Build-in Plugin

Protobuf Plugin

To generate rpc schema and proxy files from *.proto files

  • step 1: put your *.proto files into $base/proto folder
.
├── app
├── config
│   ├── config.default.js
│   └── proxy.js
├── package.json
└── proto
    └── ProtoService.proto

proto/ProtoService.proto

syntax = "proto3";

package com.alipay.sofa.rpc.test;

// 可选
option java_multiple_files = false;

service ProtoService {
  rpc echoObj (EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
  string name = 1;
  Group group = 2;
}

message EchoResponse {
  int32 code = 1;
  string message = 2;
}

enum Group {
  A = 0;
  B = 1;
}
  • step 2: config the config/proxy.js
module.exports = {
  group: 'SOFA',
  errorAsNull: false,
  services: [{
    appName: 'pb',
    responseTimeout: 100,
    api: {
      ProtoService: {
        interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
        version: '1.0',
        method: {
          echoObj: {
            responseTimeout: 3000,
          },
        },
      },
    },
  }],
};
  • step 3: run egg-rpc-generator under the project folder

will generate app/proxy/ProtoService.js and run/proto.json

.
├── app
│   └── proxy
│       └── ProtoService.js
├── config
│   ├── config.default.js
│   └── proxy.js
├── package.json
├── proto
│   └── ProtoService.proto
└── run
    └── proto.json

app/proxy/ProtoService.js

// Don't modified this file, it's auto created by egg-rpc-generator

'use strict';

const path = require('path');

/* eslint-disable */
/* istanbul ignore next */
module.exports = app => {
  const consumer = app.rpcClient.createConsumer({
    interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
    targetAppName: 'pb',
    version: '1.0',
    group: 'SOFA',
    proxyName: 'ProtoService',
    responseTimeout: 100,
  });

  if (!consumer) {
    // `app.config['pb.rpc.service.enable'] = false` will disable this consumer
    return;
  }

  app.beforeStart(async() => {
    await consumer.ready();
  });

  class ProtoService extends app.Proxy {
    constructor(ctx) {
      super(ctx, consumer);
    }

    async echoObj(req) {
      return await consumer.invoke('echoObj', [ req ], {
        ctx: this.ctx,
        responseTimeout: 3000,
      });
    }
  }

  return ProtoService;
};
/* eslint-enable */

Jar2Proxy Plugin

To generate rpc proxy from jar.

  • step 1: config config/proxy.js

Please refer to jar2proxy configuration for detail.

  • step 2: put jar file into $app_root/assembly folder
.
├── app
├── assembly
│   ├── dubbo-demo-api-1.0-SNAPSHOT-sources.jar
│   ├── dubbo-demo-api-1.0-SNAPSHOT.jar
│   ├── jar2proxy-facade-1.0.0-sources.jar
│   └── jar2proxy-facade-1.0.0.jar
├── config
│   └── proxy.js
└── package.json
  • step 3: run egg-rpc-generator under the project folder

will generate following folders:

  • app/proxy all proxy files.
  • app/proxy_class all class definitions
  • app/proxy_enums all enums definitions

Jsdoc2Jar

To generator jave interface definitions(jars) from JavaScript comments.

  • step 1: config config/config.default.js
exports.rpc = {
  server: {
    namespace: 'com.eggjs.xxx',
    // group: 'xxx',
    // version: '1.0.0',
    // pom: {
    //   version: '1.0.0',
    //   groupId: 'com.eggjs.facade',
    //   artifactId: 'your-artifactId',
    // },
  },
};
  • step 2: write rpc service with js comments
// $app_root/rpc/HelloService.js

/**
 * say hello
 * @param {String} name - user name
 * @return {String} hello words
 * @rpc
 */
exports.sayHello = async function (name) {
  return 'hello ' + name;
};
  • step 3: run egg-rpc-generator under the project folder

will generate:

  • $app_root/src: java interface definitions source code
  • $app_root/target: the output of mvn clean install

Write Your Plugin

You can write your own egg-rpc-generator plugin.

  • Write a plugin, and publish it to NPM.
module.exports = async (units, { baseDir }) => {
  // do something
};
  • Use plugin:

install plugin from NPM, then use it with egg-rpc-generator:

$ egg-rpc-generator -p pluginName1,pluginName2,...

License

MIT

Contributors

|gxcsoccer|zhoukk| | :---: | :---: |

This project follows the git-contributor spec, auto updated at Fri Jun 16 2023 21:14:08 GMT+0800.