@maiyunnet/kebab 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +14 -0
- package/index.js +1 -1
- package/lib/buffer.d.ts +25 -0
- package/lib/captcha.d.ts +12 -0
- package/lib/consistent.d.ts +20 -0
- package/lib/core.d.ts +85 -0
- package/lib/crypto.d.ts +47 -0
- package/lib/db.d.ts +88 -0
- package/lib/dns.d.ts +75 -0
- package/lib/fs.d.ts +49 -0
- package/lib/jwt.d.ts +30 -0
- package/lib/kv.d.ts +111 -0
- package/lib/lan.d.ts +10 -0
- package/lib/net/formdata.d.ts +23 -0
- package/lib/net/request.d.ts +23 -0
- package/lib/net/response.d.ts +14 -0
- package/lib/net.d.ts +65 -0
- package/lib/s3.d.ts +35 -0
- package/lib/scan.d.ts +31 -0
- package/lib/session.d.ts +22 -0
- package/lib/sql.d.ts +57 -0
- package/lib/ssh/sftp.d.ts +40 -0
- package/lib/ssh/shell.d.ts +13 -0
- package/lib/ssh.d.ts +24 -0
- package/lib/text.d.ts +40 -0
- package/lib/time.d.ts +22 -0
- package/lib/ws.d.ts +87 -0
- package/lib/zip.d.ts +40 -0
- package/lib/zlib.d.ts +25 -0
- package/main.d.ts +1 -0
- package/package.json +1 -1
- package/sys/child.d.ts +1 -0
- package/sys/cmd.d.ts +1 -0
- package/sys/ctr.d.ts +96 -0
- package/sys/master.d.ts +1 -0
- package/sys/mod.d.ts +205 -0
- package/sys/route.d.ts +31 -0
- package/tsconfig.json +1 -1
- package/www/example/ctr/main.d.ts +4 -0
- package/www/example/ctr/middle.d.ts +6 -0
- package/www/example/ctr/test.d.ts +94 -0
- package/www/example/mod/test.d.ts +20 -0
- package/www/example/mod/testdata.d.ts +9 -0
- package/www/example/ws/mproxy.d.ts +4 -0
- package/www/example/ws/rproxy.d.ts +4 -0
- package/www/example/ws/test.d.ts +7 -0
- package/index.ts +0 -33
- package/lib/buffer.ts +0 -152
- package/lib/captcha.ts +0 -63
- package/lib/consistent.ts +0 -219
- package/lib/core.ts +0 -880
- package/lib/crypto.ts +0 -384
- package/lib/db.ts +0 -719
- package/lib/dns.ts +0 -405
- package/lib/fs.ts +0 -527
- package/lib/jwt.ts +0 -276
- package/lib/kv.ts +0 -1489
- package/lib/lan.ts +0 -87
- package/lib/net/formdata.ts +0 -166
- package/lib/net/request.ts +0 -150
- package/lib/net/response.ts +0 -59
- package/lib/net.ts +0 -662
- package/lib/s3.ts +0 -235
- package/lib/scan.ts +0 -364
- package/lib/session.ts +0 -230
- package/lib/sql.ts +0 -1149
- package/lib/ssh/sftp.ts +0 -508
- package/lib/ssh/shell.ts +0 -123
- package/lib/ssh.ts +0 -191
- package/lib/text.ts +0 -626
- package/lib/time.ts +0 -254
- package/lib/ws.ts +0 -523
- package/lib/zip.ts +0 -447
- package/lib/zlib.ts +0 -350
- package/main.ts +0 -27
- package/sys/child.ts +0 -678
- package/sys/cmd.ts +0 -225
- package/sys/ctr.ts +0 -904
- package/sys/master.ts +0 -355
- package/sys/mod.ts +0 -1871
- package/sys/route.ts +0 -1113
- package/www/example/ctr/main.ts +0 -9
- package/www/example/ctr/middle.ts +0 -26
- package/www/example/ctr/test.ts +0 -3218
- package/www/example/mod/test.ts +0 -47
- package/www/example/mod/testdata.ts +0 -30
- package/www/example/ws/mproxy.ts +0 -16
- package/www/example/ws/rproxy.ts +0 -14
- package/www/example/ws/test.ts +0 -36
package/lib/consistent.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Project: Kebab, User: JianSuoQiYue
|
|
3
|
-
* Date: 2022-09-12 10:51:16
|
|
4
|
-
* Last: 2022-09-12 10:51:20, 2023-3-17 10:52:04, 2023-11-15 11:45:28
|
|
5
|
-
*/
|
|
6
|
-
import * as crypto from '~/lib/crypto';
|
|
7
|
-
|
|
8
|
-
export class Consistent {
|
|
9
|
-
|
|
10
|
-
/** --- 虚拟节点数量 --- */
|
|
11
|
-
private readonly _vcount: number = 300;
|
|
12
|
-
|
|
13
|
-
/** --- hash 环 --- */
|
|
14
|
-
private readonly _circle: Record<string, string> = {};
|
|
15
|
-
|
|
16
|
-
/** --- circle 的 keys --- */
|
|
17
|
-
private _keys: string[] = [];
|
|
18
|
-
|
|
19
|
-
public constructor(vcount: number) {
|
|
20
|
-
this._vcount = vcount;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* --- 获取当前的虚拟节点数量 ---
|
|
25
|
-
*/
|
|
26
|
-
public getVcount(): number {
|
|
27
|
-
return this._vcount;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 添加节点
|
|
32
|
-
* @param node node 节点名一个或多个
|
|
33
|
-
*/
|
|
34
|
-
public add(node: string | string[]): void {
|
|
35
|
-
addToCircle(this._circle, node, this._vcount);
|
|
36
|
-
this._keys = [];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 移除节点
|
|
41
|
-
* @param node node 节点名
|
|
42
|
-
*/
|
|
43
|
-
public remove(node: string | string[]): void {
|
|
44
|
-
if (typeof node === 'string') {
|
|
45
|
-
node = [node];
|
|
46
|
-
}
|
|
47
|
-
for (const v of node) {
|
|
48
|
-
for (let i = 0; i < this._vcount; i++) {
|
|
49
|
-
delete this._circle[hash(v + i.toString())];
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
this._keys = [];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 获得一个最近的顺时针节点
|
|
57
|
-
* @param key 为给定键取 Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点
|
|
58
|
-
*/
|
|
59
|
-
public find(key: string | number): string | null {
|
|
60
|
-
if (this._keys.length === 0) {
|
|
61
|
-
this._keys = Object.keys(this._circle);
|
|
62
|
-
this._keys.sort((a, b) => {
|
|
63
|
-
return parseFloat(a) - parseFloat(b);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return findInCircle(this._circle, key, this._keys);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* --- 原数据迁移到新地址 ---
|
|
71
|
-
* @param keys 原始数据 key 集
|
|
72
|
-
* @param node 新增的节点一个或多个
|
|
73
|
-
*/
|
|
74
|
-
public migration(keys: string | number | Array<string | number>, node: string | string[]): Record<string, {
|
|
75
|
-
'old': string;
|
|
76
|
-
'new': string;
|
|
77
|
-
}> {
|
|
78
|
-
const rtn: Record<string, {
|
|
79
|
-
'old': string;
|
|
80
|
-
'new': string;
|
|
81
|
-
}> = {};
|
|
82
|
-
if (!Array.isArray(keys)) {
|
|
83
|
-
keys = [keys];
|
|
84
|
-
}
|
|
85
|
-
// --- 获取老的 key 对应的 node ---
|
|
86
|
-
const mapOld: Record<string, string> = {};
|
|
87
|
-
for (const key of keys) {
|
|
88
|
-
const oldNode = this.find(key);
|
|
89
|
-
if (!oldNode) {
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
mapOld[key] = oldNode;
|
|
93
|
-
}
|
|
94
|
-
this.add(node);
|
|
95
|
-
// --- 再逐一检测老的和新的的 node 是否一致 ---
|
|
96
|
-
for (const key of keys) {
|
|
97
|
-
const newNode = this.find(key);
|
|
98
|
-
if (!newNode) {
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (mapOld[key] === newNode) {
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
rtn[key] = {
|
|
105
|
-
'old': mapOld[key],
|
|
106
|
-
'new': newNode
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
return rtn;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function get(vcount = 300): Consistent {
|
|
115
|
-
return new Consistent(vcount);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
*--- 快速查找一个 key 属于哪个 node ---
|
|
120
|
-
* @param key 要查找的key
|
|
121
|
-
* @param nodes node 列表
|
|
122
|
-
* @param vcount 虚拟节点数量
|
|
123
|
-
*/
|
|
124
|
-
export function fast(key: string | number, nodes: string | string[], vcount = 300): string | null {
|
|
125
|
-
const circle: Record<string, string> = {};
|
|
126
|
-
addToCircle(circle, nodes, vcount);
|
|
127
|
-
return findInCircle(circle, key);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* --- hash 函数 ---
|
|
132
|
-
* @param val 要 hash 的值
|
|
133
|
-
*/
|
|
134
|
-
export function hash(val: string | number): number {
|
|
135
|
-
if (typeof val === 'number') {
|
|
136
|
-
val = val.toString();
|
|
137
|
-
}
|
|
138
|
-
const bKey = crypto.hashHmac('md5', val);
|
|
139
|
-
|
|
140
|
-
const res = ((bKey.charCodeAt(3) & 0xFF) << 24) |
|
|
141
|
-
((bKey.charCodeAt(2) & 0xFF) << 16) |
|
|
142
|
-
((bKey.charCodeAt(1) & 0xFF) << 8) |
|
|
143
|
-
(bKey.charCodeAt(0) & 0xFF);
|
|
144
|
-
return res & 0xffffffff;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* --- 获取区间节点系列 ---
|
|
149
|
-
* @param min 最小值(含)
|
|
150
|
-
* @param max 最大值(含)
|
|
151
|
-
* @param pre 前导
|
|
152
|
-
*/
|
|
153
|
-
export function getRange(min: number, max: number, pre: string = ''): string[] {
|
|
154
|
-
const ls: string[] = [];
|
|
155
|
-
for (let i = min; i <= max; ++i) {
|
|
156
|
-
ls.push(pre + i.toString());
|
|
157
|
-
}
|
|
158
|
-
return ls;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* --- 添加到圆环 ---
|
|
163
|
-
* @param circle 圆环
|
|
164
|
-
* @param node node 节点名一个或多个
|
|
165
|
-
* @param vcount 虚拟节点数量
|
|
166
|
-
*/
|
|
167
|
-
export function addToCircle(
|
|
168
|
-
circle: Record<string, string>,
|
|
169
|
-
node: string | string[],
|
|
170
|
-
vcount: number = 300
|
|
171
|
-
): void {
|
|
172
|
-
if (typeof node === 'string') {
|
|
173
|
-
node = [node];
|
|
174
|
-
}
|
|
175
|
-
for (const v of node) {
|
|
176
|
-
for (let i = 0; i < vcount; i++) {
|
|
177
|
-
circle[hash(v + i.toString())] = v;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* --- 获得一个最近的顺时针节点 ---
|
|
184
|
-
* @param circle 圆环
|
|
185
|
-
* @param key 为给定键取 Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点
|
|
186
|
-
* @param keys keys,留空则自动从 circle 上取
|
|
187
|
-
*/
|
|
188
|
-
export function findInCircle(
|
|
189
|
-
circle: Record<string, string>,
|
|
190
|
-
key: string | number,
|
|
191
|
-
keys: string[] = []
|
|
192
|
-
): string | null {
|
|
193
|
-
let count = keys.length;
|
|
194
|
-
if (keys.length === 0) {
|
|
195
|
-
keys = Object.keys(circle);
|
|
196
|
-
count = keys.length;
|
|
197
|
-
keys.sort((a, b) => {
|
|
198
|
-
return parseFloat(a) - parseFloat(b);
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
if (count === 0) {
|
|
202
|
-
return null;
|
|
203
|
-
}
|
|
204
|
-
if (count === 1) {
|
|
205
|
-
return circle[keys[0]];
|
|
206
|
-
}
|
|
207
|
-
const hashv = hash(key);
|
|
208
|
-
if (circle[hashv] !== undefined) {
|
|
209
|
-
return circle[hashv];
|
|
210
|
-
}
|
|
211
|
-
for (const v of keys) {
|
|
212
|
-
if (parseFloat(v) < hashv) {
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
return circle[v];
|
|
216
|
-
}
|
|
217
|
-
// --- 没找到 ---
|
|
218
|
-
return circle[keys[0]];
|
|
219
|
-
}
|