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

shuke.authlib

v0.0.5

Published

将工程的 Release 发布包 shuke.authlib.tgz

Downloads

1

Readme

树课平台验证库 shuke.authlib

安装

将工程的 Release 发布包 shuke.authlib.tgz

下载至要安装的项目目录 (如deps) 下,

然后执行

npm i ./deps/shuke.authlib.tgz

即可完成安装

远程用法

无论与树课服务器是否在同一个内网, 都可以使用远程验证器。

远程验证器与本地验证器共同实现了同一个接口。

import * as AuthLib from "shuke.authlib";

const authHelper = new AuthLib.ShukeAuthHelper("jigsaw://127.0.0.1/shuke.auth");

authHelper.on("ready",async ()=>{
    const sess_info = await authHelper.auth({ token : "IAMTOKEN" })
    console.log(sess_info);

});

本地用法

若你的应用程序与树课服务器在同一个内网, 可考虑使用本地验证器加速验证.

import * as AuthLib from "shuke.authlib";

const mysql_config = {
    host:"127.0.0.1",
    port:3306,
    user:"root",
    password:"PASSWORD",
    database:"testdb"
};

const authHelper = new AuthLib.AuthHelper(mysql_config);

authHelper.on("ready",async ()=>{
    const sess_info = await authHelper.auth({ token : "IAMTOKEN" })
    console.log(sess_info);
});

错误处理

目前总共有三种类型的错误以及两种已经编码了的错误,在使用验证器的验证方法时会发生.

6001: UnAuthError 代表验证信息有误, 无法找到与该验证信息相关的会话信息
6002: UnRegisterError 代表该账户还未注册或手机号还未验证

其它错误: 其它错误将不会是上面所述的代码,通常是网络超时,参数错误等.

通常的错误处理方式:


const helper = new AuthLib.ShukeAuthHelper();

...

try{
    await helper.auth({ token : "IAMTOKEN" });
}catch(err){
    if(err.code == 6001){
        ctx.status = 403;
        ctx.body = "验证失败, 请重新登录";
    }else if(err.code == 6002){
        ctx.status = 403;
        ctx.body = "您的账户还未验证手机号, 请先通过验证.";
    }else{
        ctx.status = 500;
        ctx.body = `未预料的内部错误,发生了: ${err.message}`;
    }
}

良好的 TypeScript 支持

虽然本库完美支持 JavaScript 的引用,您可以这样使用:

const { ShukeAuthHelper } = require("shuke.authlib");

这样你仍然可以在 vscode 等代码编辑器获得到基于 TS 的代码提示, 但是你不会受到任何类型的约束

使用 TypeScript 构建工程并这样引入本库:

import { ShukeAuthHelper } from "shuke.authlib";

你将获得完全的 TypeScript 声明支持与类型约束.

中间件

你可以使用使用本库提供的中间件, 也可以自己自制中间件。 此中间件同时支持koa与jigsaw-rpc

import * as AuthLib from "shuke.authlib";

const authHelper = new AuthLib.AuthHelper("jigsaw://127.0.0.1/shuke.auth");
const ware = new AuthLib.Useware(authHelper);

//构造器的第一个参数可以是任意一个实现了IAuthHelper接口的实例

...

koa.use(ware.handle())

koa.use(async (ctx,next)=>{

    console.log(ctx.shuke_sessinfo);
})
...

//或者
jigsaw.use(ware.handle())

jigsaw.use(async (ctx.next)=>{

    console.log(ctx.shuke_sessinfo);
});