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

crypto_net

v1.1.2

Published

some useful functions

Downloads

27

Readme

crypto_net

crypto_net是一个JavaScript库,可以简单实现加密、解密、消息摘要、签名和验证数据,编码和解码数据,以及网络请求统一拦截等。它还包括用于将数据转换为二进制数据,以及读取Arraybuffer,blob等二进制数据为其他形式数据的方法,以及提供一些与网络加载优化的相关方法,比如延迟加载、预加载和滚动加载等。

特点:

这是一个很小的包,没有臃肿的体积,且支持es6按需加载,帮助你tree-shaking。提供了实用的跟网络请求有关的工具函数。

Installation

通过npm下载:

npm install crypto_net

Usage:

// es module
import {utf16ToBase64,base64ToUtf16,encodeStr,decodeCodePoints} from 'crypto_net'
utf16ToBase64('你好')//5L2g5aW9
base64ToUtf16('5L2g5aW9')//你好
encodeStr('你好', 'utf-16')//Uint16Array(2)[20320, 22909]
decodeCodePoints(new Uint16Array([20320, 22909]), 'utf-16')//你好
encodeStr('你好', 'utf-8')//Uint8Array(6) [ 228, 189, 160, 229, 165, 189 ]
decodeCodePoints(new Uint8Array([228, 189, 160, 229, 165, 189]), 'utf-8')//你好

API:

1. Encoding and Decoding

  • base64ToUtf16(base64String: string): string 将base64字符串转换为utf16编码的字符串。

  • utf16ToBase64(utf16String: string): string 将utf16编码的字符串转换为base64字符串。

  • encodeStr(str:String, encoding:"utf-8"|"utf-16"):Uint8Array|Uint16Array 返回编码后的Uint8Array或者Uint16Array。

  • decodeCodePoints(codePoints:Uint8Array, encoding:"utf-8"|"utf-16"|"windows-1251"|"base64"|"ascii"|"utf-8"|"gbk"等编码类型): string 返回解码后的字符串

2. Encryption and Decryption

  • getEncryKey(method, algorithm, extractable, usages, options):promise promise兑现为生成一个CryptoKey密钥对象。

  • encry(plainText,algorithmn,key,options): promise promise兑现为一个包含密文数据的Arraybuffer。

  • decry(cipherText,algorithmn,key):promise

    promise兑现为一个包含原数据的Arraybuffer。

3. Signing and Verifying

  • sign(data,algorithmn,key):promise 兑现为包含签名的Arraybuffer。

  • generateDigest(algorithmn,data):promise 兑现为包含摘要数据的ArrayBuffer。

  • verify(signedData,signature,algorithmn,key):promise 兑现为表示是否验证通过的boolean。

4. Axios类:

封装了fetch,具有跟axios一样的拦截器功能。

  • 通过Axios.get/post/delete/put/options/patch/connect等方法发起网络请求。
  • 通过Axios.requestInterception(dealConfig,dealError)设置全局请求拦截器。
  • 通过Axios.responseInterception(dealResponse,dealError)设置全局响应拦截器。

4. 处理二进制数据

  • readBlob(blob, type:"url"|"text"|"stream"|"dataUrl"|"arrayBuffer"):promise 兑现为给定类型数据
  • readArrayBuffer(arraybuffer, type:"url"|"string"|"stream"|"dataUrl"|"arrayBuffer"|"blob"|"typedArray",options) 当type为'string'时在options中提供decodeType,为'blob'时在options提供blob的类型blobType。该函数会直接返回对应的数据。
  • encodeToBinary(data,dataType:"blob"|"arrayBuffer"|"typedArray",{type})dataType为blob时在options.type中提供这个blob的类型,为typedArray时options.type提供特定类型如"Uint8Array",该函数会直接返回对应的数据。

5. Utility Functions

优化加载性能的根据函数:

  • preLoadScript(src):void 在浏览器空闲时通过script标签预加载async的方式预加载脚本。

  • prefetchAsset(src, {crossOrigin}) 浏览器空闲时通过link预加载资源,需要指定跨域相关头部。

  • scrollLoad(containerSelector,offset,delay,options):promise 当容器滚到距离底部offset的距离时promise兑现为true

License This package is licensed under the MIT License.