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

@tomrpc/core

v2.0.3

Published

My Awesome lib

Downloads

3

Readme

@tomrpc/core

create

const rpc = createServer({
 ...
});

example

import { createServer } from '@tomrpc/core';

const rpc = createServer({
   fn: {
    prefix: '/apk',
  },
});

rpc.fn('a', (a: string) => {
  return a;
});

rpc.fn('a.a', (a: string, ctx: any) => {
  // console.dir(ctx);
  if (ctx.method === 'POST') {
    console.dir('post');
    return {
      a: a,
    // b: b,
    };
  } else {
    console.dir('get' + ctx.method);
    return a;
  }
});

rpc.fn('b', () => {
  console.dir('test b');
});

rpc.add({
  c: (a: string) => {
    return a;
  },
  a: function (a: string, b: string) {
    return `${this.path} , ${a} c2 ${b}`;
  },
});

// https://bobbyhadz.com/blog/typescript-no-overload-matches-this-call
// rpc.dump();
// console.dir(rpc.dump());

// rpc.mount();

rpc.start(3000);

实现插件

import { Plugable } from './src/plugin';
export default class Fn extends Plugable {
  constructor(cfg?) {
    super(cfg);
    this.prefix = '/demo';
    this.name = 'demo';
    const a = this.a();

    this.use(a);
  }
  process() {
    return async (ctx, next) => {
      console.dir('process child');
      if (ctx.path === '/') {
        ctx.body = { '/': 23 };
      } else {
      // ctx.body = { '2323api': 23 };
        await next();
      }
    };
  }
  a() {
    return async (ctx, next) => {
      console.dir('a ' + ctx.path);
      await next();
    };
  }
}

生命周期

  • before:[] 可变数组
    • init:[] 可变数组
    • load:[] 可变数组
    • mount(plugin.prefix, plugin.app) 默认行为,不可操作
      • pre(可选,返回值是 Koa 中间件)
      • process(可选,返回值是 Koa 中间件)
      • post(可选,返回值是 Koa 中间件)
    • config.hooks.default 可在server配置里覆写
  • after:[] 可变数组

生命周期操作方法,在构造函数中,增加到对应的数组即可

  • this.addInit(a);
  • this.addLoad(b);

被mount的中间件,实际上执行的是compose([pre, process, post]);

在plugin构造函数里,执行了this.load.push(this.getMiddleware());

  • pre(可选,返回值是Koa中间件)
  • process(可选,返回值是Koa中间件)
  • post(可选,返回值是Koa中间件)

实现Proxy

import { Proxy } from './src/proxy';
export default class TestProxy extends Proxy {
  constructor(cfg?) {
    super(cfg);

    this.config.proxy.inject = 'before';
    this.config.proxy.before = ['fn'];
  }
  proxy() {
    return async (ctx, next) => {
      console.dir('TestProxy process child' + ctx.path);
      await next();
    };
  }
}

RpcServer

const rpc = new RpcServer({});

const fn = new Fn({});

rpc.plugin(fn);

rpc.start(3000)