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

@taccisum/egg-utils

v0.3.0

Published

deepexi utils for eggjs

Downloads

3

Readme

egg-utils

NPM version npm download Build Status codecov

CHANGELOG

依赖的插件

  • circuit-fuses v4.x
  • request v2.x

如何使用

添加依赖

$ npm i @taccisum/egg-utils --save

开启插件

// config/plugin.js
exports.utils = {
  enable: true,
  package: 'egg-utils',
};

http utils

简单请求:

const resp = await ctx.helper.http.requestGet('http://www.baidu.com')

sse utils

注意:node长连接默认2分钟会超时,如服务端连接发送事件的间隔需超过2分钟,需服务端自行处理(设置timeout时间 or 在2分钟的间隔内发送无效事件)

使用示例

server端
单次发送消息(如状态需实时返回给前端)
/* router */
app.get('logs', `/${APP_CONTEXT}/api/v2/logs`, app.controller.v1.getLog);
/* router */
const SSEUtils = ctx.helper.sse;
/* controller */
async getLog () {
  const ctx = this.ctx;
  ctx.body = SSEUtils.send({
    setResHeader: resHeaders => {
      ctx.set(resHeaders);
    },
    sendType: 'once',
    onceMsg: 'hello world',
  });
}
其他情况发送消息(非单次,或者其他任意情况)
/* router */
app.get('logs', `/${APP_CONTEXT}/api/v2/logs`, app.controller.v1.getLog);
/* router */
const SSEUtils = ctx.helper.sse;
/* controller */
async getLog () {
  const ctx = this.ctx;
  const msgs = ['hello' 'world'];
  let interval = null
  ctx.body = SSEUtils.send({
    setResHeader: resHeaders => {
      ctx.writeHead(resHeaders);
    },
    sendType: 'other',
    sender: send => {
      let index = 0;
      interval = setInterval(() => {
        const msg = index === msgs.length ? 'sseEnd' : msgs[index];
        send(msg);

        if (msg === 'sseEnd') {
          // 清除事件
          clearInterval(interval);
        }
        index++;
      }, 1000); //每秒发送一次消息
    },
  })
}

API

Methods

| 方法名 | 说明 | 参数 | | :--: | :--: | ---- | | send | 与客户端建立长连接,返回值是 stream对象 | optsions选项options.setResHeader 设置请求头function,requiredoptions.sendType once单次 repeat重复 other其他,默认发送一次options.sender 消息发送者,处理什么时候发送消息和结束发送消息,参数send func,结束需要发送'sseEnd'消息,非一次使用options.onceMsg 单次发送消息主体,默认是''options.retry 长连接发送错误时,重试频率,毫秒, 默认10s options.msgReplace 无法处理消息带换行符的情况,提供替换的正则,默认为'' options.finishCb stream结束时钩子函数 |

crypto utils

使用示例

md5加密
const CryptoUtils = ctx.helper.crypto;
const hashData = CryptoUtils.md5('data', 'salt');
aes加密
const CryptoUtils = ctx.helper.crypto;
const data = 'data';
const secretKey = 'secretKeyDataKey';   // 秘钥必须16位
const iv = 'secretKeyDataKey';          // 初始向量 initial vector 必须16位
CryptoUtils.aesEncrypt(data, secretKey, iv);
aes解密
const CryptoUtils = ctx.helper.crypto;
const data = 'data';
const secretKey = 'secretKeyDataKey';   // 加密时的秘钥 必须16位
const iv = 'secretKeyDataKey';          // 加密时的初始向量 initial vector 必须16位
CryptoUtils.aesDecrypt(data, secretKey, iv);
aes加解密对象
const CryptoUtils = ctx.helper.crypto;
CryptoUtils.getCipheriv(algorithm, secretKey, iv);
CryptoUtils.getDecipheriv(algorithm, secretKey, iv);

License

MIT