正在完善中……
const str = 'https://qshfu.com/lib?v=1.0'
str.before('?') // 'https://qshfu.com/lib'
str.before('//', '?') // ['https:', 'qshfu.com/lib']
'https://qshfu.com/lib?v=1.0'.after('?') // 'v=1.0'
random() // 6
random(10, 29) // 18
random(10, 199) // 123
'sum(1+1+sum(2+3))+sum(2+4)'
.replace2(/sum\([^]*?\)/g, {m: 'sum', '*': /\+/g})
// 'm(1*1*sum(2*3))+m(2*4)'
'sum(1+1+sum(2+3))+sum(2+4)'
.replace3({prev: 'sum', start: '(', end: ')'}, {m: 'sum', '*': /\+/g})
// 'm(1*1*sum(2*3))+m(2*4)'
[1, 2, 3, 4, 5].forEachReverse(i => i === 2 ? false : console.log(i)) // 5,4,3
[1, 2, 3, 4, 5].forEach2(i => i === 4 ? false : console.log(i)) // 1,2,3
const data = {}, key = {}
data.$set(key, '123')
data.$get(key) // '123'
isEmpty({}) // true
isEmpty({}, {qwe: 123}) // false
new Date('2022/01/01').format('yyyy-MM-dd 周D')
// '2022-01-01 周六'
isObject(undefined) // false
class StatusEnum extends Enum {
RESOLVE = 1
REJECT
constructor() {
super().init()
}
}
const status = new StatusEnum()
status[status.REJECT] // 'REJECT'
class Main extends Singleton {
constructor() {
super()
}
}
new Main() === new Main() // true
// 使用create可能会创建失败,推荐使用异步版本createId
let id
id = create()
id = create()
id = create()
id = await createId()
id = await createId()
id = await createId()
// 递归删除文件夹
rmdir('./tmp')
// 解析url参数
u2o('localhost?qwe=123&asd=456&code') // { qwe: '123', asd: '456', code: 'code' }
// 简单的加解密
const key = createKey('abc123')
const data = encrypt({name: '张三', age: '21'}, key).toString()
console.log(decrypt(data, key))
// 或者
const data = Buffer.from(JSON.stringify({name: '张三', age: 21}))
const key = Buffer.from('abc123')
console.log(XOR(data, key).toString())
console.log(XOR(data, key).toString())
// 封装了加密类
new Cipher
实例文件test.mjs
目录结构
folder ┳ 0.1 ┳ index.mjs // 入口、必需
┃ ┗ ...
┣ ...
┣ 2.1 ┳ index.mjs // 入口、必需
┃ ┗ ...
┣ node_modules // 共用库
┗ package.json
// ./index.mjs 入口文件,需要和版本文件夹同目录,版本文件夹内一定要有index.mjs文件
// import {} from "tools2/version.mjs"; // 没有指定版本会在目录中通过文件夹名找最新版本
import {} from "tools2/version.mjs?v=2.0&e=index.mjs";
// import {} from "tools2/version.mjs?version=2.0&entry=index.mjs";
import {createProxy} from 'min-proxy'
createProxy({
map: {
'/target': 'http://domain/',
'/': '/dir',
'http://domain2/': 'http://domain3/'
},
port: 80,
httpsOptions: {
key,
cert
},
preReq: (req, res) => false,
preRes: (req, res) => false,
})
import {Base, Express} from "tools2/router.mjs";
const app = new Express({
port: 8080,
callback: () => console.log('running...')
})
class Test extends Base {
cache = 0
// 当提供3个参数的函数实现时则加入认证队列
AUTH(req, res, next) {
console.log(req.headers)
next()
}
get path() {
return super.path + '/test'
}
}
class Admin extends Test {
id = 0
AUTH(req, res, next) {
console.log(req.cookies)
console.log(req.query)
next();
}
async GET(req, res) {
res.end(this.view) // new Express({root: 'wwwroot'}) 与 root 参数有关
// 服务器端渲染,每次访问都会重新计算渲染,会增加服务器负担,建议权限控制较高使用不频繁的场景使用
// this.render(this.view, {})
// /<script server>[^]*?<\/script>/g 支持服务器端脚本 <script server>write(`<b>hello~${data.qwe}</b>`)</script>
// /{\/[^]*?}/g 支持服务器端引入 {/com/head.html}
}
async POST(req, res) {
await this.queue() // 必须和this.pass()成对使用
res.end(`id: ${this.id++}`)
this.pass() // 必须和this.queue()成对使用
}
get path() {
return super.path + '/admin'
}
}
app.use((req, res, next) => res.setHeader('Access-Control-Allow-Origin', '*') && next())
app.use(Admin)
// app.use(new Admin())
// app.load('./lib')
1、打包时候node版本要对应
2、使用--experimental-vm-modules模式
> node --experimental-vm-modules index.mjs
root ┬ lib1 ┬ index.mjs
│ ├ data.json
│ └ ...
├ index.mjs // 编译后生成
└ ...
import {build} from 'tools2/module.mjs'
build('lib1', 'index') // 打包
import {} from './lib1/index.mjs' // 开发模式
import {} from './index.mjs' // 生产模式,打包后
let env = 'prod'
// IF DEV
env = 'dev'
// END DEV
console.log(env) // 开发模式:prod;生产模式(打包后):prod
Note:
class Base {
/**
* 定义成员函数,制定规则
* @param {any[]} arg
*/
func(...arg) {
if (this === undefined) return
// init
this.func = (...arg) => {
// todo
}
this.func(...arg)
}
/**
* 定义成员变量,提供工具
* @param {String} key
* @param {Function} init
*/
define(key, init) {
Object.defineProperty(this, key, {
get() {
const value = init()
Object.defineProperty(this, key, {
get() {
return value
}
})
return value
},
configurable: true
})
}
}
import {cpu, mem, sys, node, disk} from 'tools2/info.mjs'
console.log(cpu()) // 处理器信息
console.log(mem()) // 内存信息
console.log(sys()) // 系统信息
console.log(node()) // node版本信息
console.log(disk()) // 硬盘信息
import Query from 'tools2/mysql2ssh2.mjs'
const query = new Query({...}, {...})
query`SELECT * FROM t_test`.then(...)
query.close()
基于tcp或者udp连接
tcp支持集群
import cluster from 'cluster'
import {cpus} from 'os'
if (cluster.isPrimary) {
cpus().forEach(() => cluster.fork())
} else {
// todo
console.log(`Worker ${process.pid} started`)
}
udp
import {udpService, tcpService} from './index.mjs'
const pools = {
s: {test: 'localhost:9000/test'}
}
// 9000端
const s = udpService(pools, {port: 9000, callback(path, data, next) {
next('9000')
}})
// s.close()
// 9001端
const map = new Map
map.set('/test', data => new Promise((resolve, reject) => reject('9001')))
udpService(pools, {port: 9001, map}).s.test().then(r => console.log(r.toString())) // '9000'
tcp
const pools = {
s: {test: 'localhost:9000/test'}
}
// 9000端
const map = new Map
map.set('/test', data => '9000')
const s = tcpService(pools, {port: 9000, map})
// s.close()
// 9001端
tcpService(pools, {port: 9001, callback(path, data, next) {}})
.s.test().then(r => assert.equal(r, '9000')) // '9000'