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

@evio/nodebase

v1.0.17

Published

nodejs base dev framework

Downloads

20

Readme

Nodebase

彩虹桥,被用作此架构名,意在进入神域的通道。而此架构,是开发所有nodejs应用的主架构。它实现了进程化管理以及微服务插件的概念,具有非常强大的扩展性和丰富的生命周期供开发者创建自己的应用服务。

Install

npm install bifroster --save

Usage

const path = require('path');
const Bifrost = require('bifroster');
const bifrost = new Bifrost({
  cwd: __dirname,
  agents: ['agent']
});

Dir

Bifrost-project
├── package.json
├── index.js
├── app.js (可选)
├── agent.js (可选)
├── plugin.js (可选)
├── app
|   ├── router
│   |   └── index.js
│   ├── controller
│   |   └── home.js
│   ├── service (可选)
│   |   └── user.js
│   ├── middleware (可选)
│   |   └── response_time.js

Bifrost

new Bifrost({ ...options });

Bifrost#options

  • max number 最大启动子进程个数
  • cwd string 项目目录
  • agents array agent进程列表,格式为 agents:['agent']
  • secure object 启动安全协议的配置 {key, cert}组合,使用https协议
  • port number 服务启动端口
  • host string 服务启动ip

Master#Events

  • agent:ready Agent服务准备就绪
  • agent:exit Agent服务关闭
  • exit 整个应用关闭
  • ready 整个应用准备就绪
[
  'agent:ready',
  'ready',
  'agent:exit',
  'exit'
].forEach(name => {
  bifrost.on(name, (...args) => {
    bifrost.console.log(`[${bifrost.type}]:`, name, ...args);
  });
});

IPC-Message

请看这里

Agent

微服务部署核心类。

Create Agent

/agent.js =>

const Bifrost = require('bifroster');

module.exports = agent => {
  [
    'receive:message',
    'master:ready',
    'agent:beforeCreate',
    'agent:created',
    'agent:beforeDestroy',
    'agent:destroyed'
  ].forEach(name => {
    agent.on(name, (...args) => {
      agent.console.log('[agent]:', name, ...args);
    })
  });
}

Agent lifecycles

  • receive:message 自定义接收未进入微服务的消息处理事件
  • agent:beforeCreate Agent被创建前
  • agent:created Agent被创建完毕
  • agent:beforeDestroy Agent开始停止服务前
  • agent:destroyed Agent服务被停止后
  • master:ready 由顶层进程通知过来的关于整个应用准备完毕后调用的生命周期

Agent install plugins

在项目根目录下有一个文件plugin.js

const path = require('path');
module.exports = {
  test: {
    enable: true,
    env: '*',
    path: path.resolve(__dirname, 'plugins', 'test'),
    // package: 'component',
    agent: '*',
    config: {}
  },
  test2: {
    enable: true,
    env: '*',
    path: path.resolve(__dirname, 'plugins', 'test2'),
    // package: 'component',
    agent: '*',
    config: {}
  }
}

参数:

  • enable boolean 是否可用 默认true
  • env array 运行环境 默认全环境
  • path string 插件文件夹 与package互斥
  • package string 插件模块 与path互斥
  • agent array 运行在哪个agent上 默认全agent
  • config 插件配置

每个插件分2个入口:

  • agent.js 运行在agent端入口
  • app.js 运行在app端入口

Make Plugin

微服务中可以理解为为整个一个工程,每条业务线都是流水作业。所以我们通过不断询问流水线负责人来确认是否要进入此微服务来获取数据。如果负责人现在的状态是locked,而且他答应处理(可能由于业务太多忙不过来),那么他会将消息暂时存入堆栈,等到空闲了再处理,相当于任务的调度。

实际情况下,我们采用路由中间件的模式来处理这些请求,从而返回数据。

首先来认识4个生命周期:

  • task:start 任务开始
  • task:done 任务结束
  • server:start 注册服务初始化的函数
  • server:destroy 注册服务将要停止的函数
module.exports = component => {
  [
    'task:start',
    'task:done',
    'server:start',
    'server:destroy'
  ].forEach(name => {
    component.on(name, (...args) => {
      console.log('[agent]:', '[channel]:', name, ...args);
    })
  });

  component.use(async(ctx, next) => {
    ctx.send({
      a: 1,
      b: 2,
      c: ctx.body
    });
    await next();
  })
}

我们可以看到,里面的数据都是通过中间件模式被返回的,这个在使用nodejs的开发者是最清楚如何使用,这里我就不再多说。

注意:这里的ctx.send只有一个参数,意思说明,现在的数据需要按原路返回给请求者。 同样的,ctx.reply就是ctx.send只有一个参数时候的作用

Create application

/app.js

生命周期如下:

  • wroker:beforeStart app被创建前
  • wroker:started app被创建完毕
  • wroker:beforeStop app开始停止服务前
  • wroker:stoped app服务被停止后
  • master:ready 整个服务准备完毕
  • receive:message 接收额外的消息

app.js

module.exports = app => {
  [
    'wroker:beforeStart',
    'wroker:started',
    'wroker:beforeStop',
    'wroker:stoped',
    'master:ready',
    'receive:message'
  ].forEach(name => {
    app.on(name, (...args) => {
      app.console.log('[worker]:', name, ...args);
    })
  });
}

Test

You can see how to write through the examples in the test folder, and you can test the class by using the npm run test command.

License

Bifrost is MIT licensed.