koishi-plugin-rzgtboeyndxsklmq-commons 1.0.5 → 1.0.6
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/lib/index.d.ts +2 -1
- package/lib/index.js +161 -118
- package/lib/utils/BeanHelper.d.ts +30 -22
- package/lib/utils/Files.d.ts +3 -0
- package/lib/utils/Strings.d.ts +6 -0
- package/lib/utils/Test.d.ts +4 -2
- package/package.json +1 -1
- package/lib/utils/File.d.ts +0 -1
- package/lib/utils/I18n.d.ts +0 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Context, Schema } from "koishi";
|
|
2
2
|
export * from "./utils/BeanHelper";
|
|
3
|
-
export * from "./utils/
|
|
3
|
+
export * from "./utils/Strings";
|
|
4
|
+
export * from "./utils/Files";
|
|
4
5
|
export * from "./utils/Test";
|
|
5
6
|
export declare const name = "rzgtboeyndxsklmq-commons";
|
|
6
7
|
export interface Config {
|
package/lib/index.js
CHANGED
|
@@ -31,40 +31,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
BeanHelper: () => BeanHelper,
|
|
34
|
-
BeanType: () => BeanType,
|
|
35
34
|
Config: () => Config,
|
|
35
|
+
Files: () => Files,
|
|
36
|
+
Strings: () => Strings,
|
|
37
|
+
Test: () => Test,
|
|
36
38
|
apply: () => apply,
|
|
37
|
-
name: () => name
|
|
38
|
-
readDirFiles: () => readDirFiles,
|
|
39
|
-
test: () => test,
|
|
40
|
-
test2: () => test2
|
|
39
|
+
name: () => name
|
|
41
40
|
});
|
|
42
41
|
module.exports = __toCommonJS(src_exports);
|
|
43
42
|
var import_koishi = require("koishi");
|
|
44
43
|
|
|
45
44
|
// src/utils/BeanHelper.ts
|
|
46
|
-
var BeanType = class {
|
|
47
|
-
constructor(beanHelper) {
|
|
48
|
-
this.beanHelper = beanHelper;
|
|
49
|
-
this.ctx = beanHelper.getByName("ctx");
|
|
50
|
-
this.config = beanHelper.getByName("config");
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
|
-
static {
|
|
54
|
-
__name(this, "BeanType");
|
|
55
|
-
}
|
|
56
|
-
ctx;
|
|
57
|
-
config;
|
|
58
|
-
start() {
|
|
59
|
-
}
|
|
60
|
-
destroy() {
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
45
|
var BeanHelper = class _BeanHelper {
|
|
64
46
|
static {
|
|
65
47
|
__name(this, "BeanHelper");
|
|
66
48
|
}
|
|
67
|
-
static buildLazyProxyHandler(getObj,
|
|
49
|
+
static buildLazyProxyHandler(getObj, options) {
|
|
68
50
|
const handlerMap = {};
|
|
69
51
|
let needInit = true;
|
|
70
52
|
Reflect.ownKeys(Reflect).forEach((key) => {
|
|
@@ -74,17 +56,18 @@ var BeanHelper = class _BeanHelper {
|
|
|
74
56
|
needInit = false;
|
|
75
57
|
Reflect.ownKeys(obj).forEach((k) => target[k] = obj[k]);
|
|
76
58
|
Reflect.setPrototypeOf(target, Reflect.getPrototypeOf(obj));
|
|
59
|
+
target[_BeanHelper.PlaceholderObject] = true;
|
|
77
60
|
}
|
|
78
61
|
if (key === "set") {
|
|
79
|
-
Reflect
|
|
62
|
+
Reflect.set(target, args[0], args[1]);
|
|
80
63
|
} else if (key === "deleteProperty") {
|
|
81
|
-
Reflect
|
|
64
|
+
Reflect.deleteProperty(target, args[0]);
|
|
82
65
|
}
|
|
83
66
|
let res;
|
|
84
67
|
if (key === "get") {
|
|
85
|
-
res = hooks?.get ? hooks.get(obj, args[0]) : Reflect[key].apply(Reflect, [obj, args[0]]);
|
|
86
|
-
if (typeof res === "function") {
|
|
87
|
-
res = res.bind(args[1]);
|
|
68
|
+
res = options?.hooks?.get ? options.hooks.get(obj, args[0]) : Reflect[key].apply(Reflect, [obj, args[0]]);
|
|
69
|
+
if (typeof res === "function" && !options?.notBind) {
|
|
70
|
+
res = res.bind(options?.bindOriginal ? obj : args[1]);
|
|
88
71
|
}
|
|
89
72
|
} else {
|
|
90
73
|
res = Reflect[key].apply(Reflect, [obj, ...args]);
|
|
@@ -94,17 +77,23 @@ var BeanHelper = class _BeanHelper {
|
|
|
94
77
|
});
|
|
95
78
|
return handlerMap;
|
|
96
79
|
}
|
|
97
|
-
static buildLazyProxy(getObj) {
|
|
98
|
-
return new Proxy(
|
|
80
|
+
static buildLazyProxy(getObj, options) {
|
|
81
|
+
return new Proxy(
|
|
82
|
+
{},
|
|
83
|
+
_BeanHelper.buildLazyProxyHandler(getObj, options)
|
|
84
|
+
);
|
|
99
85
|
}
|
|
100
|
-
static buildLazyRevocableProxy(getObj) {
|
|
101
|
-
return Proxy.revocable(
|
|
86
|
+
static buildLazyRevocableProxy(getObj, options) {
|
|
87
|
+
return Proxy.revocable(
|
|
88
|
+
{},
|
|
89
|
+
_BeanHelper.buildLazyProxyHandler(getObj, options)
|
|
90
|
+
);
|
|
102
91
|
}
|
|
103
92
|
classPool = [];
|
|
104
93
|
ctx;
|
|
105
94
|
config;
|
|
106
95
|
constructor() {
|
|
107
|
-
this.proxy(() => this.ctx, "ctx");
|
|
96
|
+
this.proxy(() => this.ctx, "ctx", { notBind: true });
|
|
108
97
|
this.proxy(() => this.config, "config");
|
|
109
98
|
}
|
|
110
99
|
setCtx(ctx, config) {
|
|
@@ -124,21 +113,21 @@ var BeanHelper = class _BeanHelper {
|
|
|
124
113
|
async start() {
|
|
125
114
|
this.touchAll();
|
|
126
115
|
for (const classInfo of this.classPool) {
|
|
127
|
-
if (classInfo.proxy instanceof BeanType) {
|
|
116
|
+
if (classInfo.proxy instanceof _BeanHelper.BeanType) {
|
|
128
117
|
await classInfo.proxy.start();
|
|
129
118
|
}
|
|
130
119
|
}
|
|
131
120
|
}
|
|
132
121
|
async destroy() {
|
|
133
122
|
for (const classInfo of this.classPool) {
|
|
134
|
-
if (classInfo.proxy instanceof BeanType) {
|
|
123
|
+
if (classInfo.proxy instanceof _BeanHelper.BeanType) {
|
|
135
124
|
await classInfo.proxy.destroy();
|
|
136
125
|
}
|
|
137
126
|
classInfo.proxyRevoke?.();
|
|
138
127
|
}
|
|
139
128
|
this.classPool = null;
|
|
140
129
|
}
|
|
141
|
-
instance(clazz) {
|
|
130
|
+
instance(clazz, options) {
|
|
142
131
|
let classInfo = this.classPool.find(
|
|
143
132
|
(classInfo2) => classInfo2.class === clazz
|
|
144
133
|
);
|
|
@@ -157,14 +146,14 @@ var BeanHelper = class _BeanHelper {
|
|
|
157
146
|
classInfo.instance = new classInfo.class(this);
|
|
158
147
|
}
|
|
159
148
|
return classInfo.instance;
|
|
160
|
-
});
|
|
149
|
+
}, options);
|
|
161
150
|
classInfo.proxy = proxyRevocable.proxy;
|
|
162
151
|
classInfo.proxyRevoke = proxyRevocable.revoke;
|
|
163
152
|
this.classPool.push(classInfo);
|
|
164
153
|
return classInfo.proxy;
|
|
165
154
|
}
|
|
166
|
-
proxy(getObj, name2) {
|
|
167
|
-
const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(getObj);
|
|
155
|
+
proxy(getObj, name2, options) {
|
|
156
|
+
const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(getObj, options);
|
|
168
157
|
const classInfo = {
|
|
169
158
|
name: name2,
|
|
170
159
|
proxy: proxyRevocable.proxy,
|
|
@@ -194,90 +183,145 @@ var BeanHelper = class _BeanHelper {
|
|
|
194
183
|
return instance;
|
|
195
184
|
}
|
|
196
185
|
};
|
|
186
|
+
((BeanHelper2) => {
|
|
187
|
+
BeanHelper2.PlaceholderObject = Symbol("PlaceholderObject");
|
|
188
|
+
class BeanType {
|
|
189
|
+
constructor(beanHelper) {
|
|
190
|
+
this.beanHelper = beanHelper;
|
|
191
|
+
this.ctx = beanHelper.getByName("ctx");
|
|
192
|
+
this.config = beanHelper.getByName("config");
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
static {
|
|
196
|
+
__name(this, "BeanType");
|
|
197
|
+
}
|
|
198
|
+
ctx;
|
|
199
|
+
config;
|
|
200
|
+
start() {
|
|
201
|
+
}
|
|
202
|
+
destroy() {
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
BeanHelper2.BeanType = BeanType;
|
|
206
|
+
})(BeanHelper || (BeanHelper = {}));
|
|
207
|
+
|
|
208
|
+
// src/utils/Strings.ts
|
|
209
|
+
var Strings;
|
|
210
|
+
((Strings2) => {
|
|
211
|
+
function isEmpty(str) {
|
|
212
|
+
return !str;
|
|
213
|
+
}
|
|
214
|
+
Strings2.isEmpty = isEmpty;
|
|
215
|
+
__name(isEmpty, "isEmpty");
|
|
216
|
+
function isNotEmpty(str) {
|
|
217
|
+
return !Strings2.isEmpty(str);
|
|
218
|
+
}
|
|
219
|
+
Strings2.isNotEmpty = isNotEmpty;
|
|
220
|
+
__name(isNotEmpty, "isNotEmpty");
|
|
221
|
+
function isBlank(str) {
|
|
222
|
+
return Strings2.isEmpty(str) || str.trim().length === 0;
|
|
223
|
+
}
|
|
224
|
+
Strings2.isBlank = isBlank;
|
|
225
|
+
__name(isBlank, "isBlank");
|
|
226
|
+
function isNotBlank(str) {
|
|
227
|
+
return !Strings2.isBlank(str);
|
|
228
|
+
}
|
|
229
|
+
Strings2.isNotBlank = isNotBlank;
|
|
230
|
+
__name(isNotBlank, "isNotBlank");
|
|
231
|
+
})(Strings || (Strings = {}));
|
|
197
232
|
|
|
198
|
-
// src/utils/
|
|
233
|
+
// src/utils/Files.ts
|
|
199
234
|
var import_promises = __toESM(require("node:fs/promises"));
|
|
200
235
|
var import_node_path = __toESM(require("node:path"));
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
236
|
+
var Files;
|
|
237
|
+
((Files2) => {
|
|
238
|
+
async function readDirFiles(dirPath, needDir = false) {
|
|
239
|
+
const files = await import_promises.default.readdir(dirPath, {
|
|
240
|
+
recursive: true,
|
|
241
|
+
withFileTypes: true
|
|
242
|
+
});
|
|
243
|
+
const paths = [];
|
|
244
|
+
files.forEach((file) => {
|
|
245
|
+
if (file.isDirectory()) {
|
|
246
|
+
if (!needDir) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
paths.push(import_node_path.default.join(file.parentPath, file.name, import_node_path.default.sep));
|
|
250
|
+
} else if (file.isFile()) {
|
|
251
|
+
paths.push(import_node_path.default.join(file.parentPath, file.name));
|
|
211
252
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
__name(readDirFiles, "readDirFiles");
|
|
253
|
+
});
|
|
254
|
+
return paths;
|
|
255
|
+
}
|
|
256
|
+
Files2.readDirFiles = readDirFiles;
|
|
257
|
+
__name(readDirFiles, "readDirFiles");
|
|
258
|
+
})(Files || (Files = {}));
|
|
220
259
|
|
|
221
260
|
// src/utils/Test.ts
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
timeConsumings
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
261
|
+
var Test;
|
|
262
|
+
((Test2) => {
|
|
263
|
+
async function test(name2, count, serial, run) {
|
|
264
|
+
const timeout = setInterval(() => {
|
|
265
|
+
process.stdout.write(".");
|
|
266
|
+
}, 100);
|
|
267
|
+
let timeConsumings = [];
|
|
268
|
+
timeConsumings.length = count;
|
|
269
|
+
const testStartTime = Date.now();
|
|
270
|
+
if (serial) {
|
|
271
|
+
for (let i = 0; i < count; i++) {
|
|
272
|
+
const time = Date.now();
|
|
273
|
+
await run();
|
|
274
|
+
timeConsumings[i] = Date.now() - time;
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
timeConsumings = [...timeConsumings];
|
|
278
|
+
await Promise.all(
|
|
279
|
+
timeConsumings.map((_, i) => {
|
|
280
|
+
return new Promise((resolve) => {
|
|
281
|
+
setImmediate(async () => {
|
|
282
|
+
const time = Date.now();
|
|
283
|
+
await run();
|
|
284
|
+
timeConsumings[i] = Date.now() - time;
|
|
285
|
+
resolve(0);
|
|
286
|
+
});
|
|
245
287
|
});
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
console.log(`
|
|
288
|
+
})
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const testTime = Date.now() - testStartTime;
|
|
292
|
+
clearInterval(timeout);
|
|
293
|
+
if (count < 2) {
|
|
294
|
+
console.log(`
|
|
254
295
|
run: ${name2}; total: ${timeConsumings[0]};`);
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
let total = 0;
|
|
258
|
-
let average = 0;
|
|
259
|
-
let highest = 0;
|
|
260
|
-
let lowest = null;
|
|
261
|
-
for (let i = 1; i < timeConsumings.length; i++) {
|
|
262
|
-
const t = timeConsumings[i];
|
|
263
|
-
if (lowest == null || t < lowest) {
|
|
264
|
-
lowest = t;
|
|
296
|
+
return;
|
|
265
297
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
298
|
+
let total = 0;
|
|
299
|
+
let average = 0;
|
|
300
|
+
let highest = 0;
|
|
301
|
+
let lowest = null;
|
|
302
|
+
for (let i = 1; i < timeConsumings.length; i++) {
|
|
303
|
+
const t = timeConsumings[i];
|
|
304
|
+
if (lowest == null || t < lowest) {
|
|
305
|
+
lowest = t;
|
|
306
|
+
}
|
|
307
|
+
highest = Math.max(t, highest);
|
|
308
|
+
total += t;
|
|
309
|
+
}
|
|
310
|
+
average = total / (count - 1);
|
|
311
|
+
console.log(
|
|
312
|
+
`
|
|
272
313
|
run: ${name2}; serial: ${serial}; count: ${count}; testTime: ${testTime}; total: ${total}; first: ${timeConsumings[0]}; average: ${average}; highest: ${highest}; lowest: ${lowest};`
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
Test2.test = test;
|
|
317
|
+
__name(test, "test");
|
|
318
|
+
async function test2(name2, count, run) {
|
|
319
|
+
await test(name2, count, true, run);
|
|
320
|
+
await test(name2, count, false, run);
|
|
321
|
+
}
|
|
322
|
+
Test2.test2 = test2;
|
|
323
|
+
__name(test2, "test2");
|
|
324
|
+
})(Test || (Test = {}));
|
|
281
325
|
|
|
282
326
|
// src/index.ts
|
|
283
327
|
var name = "rzgtboeyndxsklmq-commons";
|
|
@@ -288,11 +332,10 @@ __name(apply, "apply");
|
|
|
288
332
|
// Annotate the CommonJS export names for ESM import in node:
|
|
289
333
|
0 && (module.exports = {
|
|
290
334
|
BeanHelper,
|
|
291
|
-
BeanType,
|
|
292
335
|
Config,
|
|
336
|
+
Files,
|
|
337
|
+
Strings,
|
|
338
|
+
Test,
|
|
293
339
|
apply,
|
|
294
|
-
name
|
|
295
|
-
readDirFiles,
|
|
296
|
-
test,
|
|
297
|
-
test2
|
|
340
|
+
name
|
|
298
341
|
});
|
|
@@ -1,26 +1,9 @@
|
|
|
1
1
|
import type { Context, Awaitable } from "koishi";
|
|
2
|
-
export declare abstract class BeanType<C extends object> {
|
|
3
|
-
protected beanHelper: BeanHelper<C>;
|
|
4
|
-
protected ctx: Context;
|
|
5
|
-
protected config: C;
|
|
6
|
-
constructor(beanHelper: BeanHelper<C>);
|
|
7
|
-
start(): Awaitable<void>;
|
|
8
|
-
destroy(): Awaitable<void>;
|
|
9
|
-
}
|
|
10
2
|
type Constructor<T = any> = new (...args: any[]) => T;
|
|
11
|
-
export interface ClassInfo<T> {
|
|
12
|
-
name: string;
|
|
13
|
-
class?: Constructor<T>;
|
|
14
|
-
instance?: T;
|
|
15
|
-
proxy?: T;
|
|
16
|
-
proxyRevoke?: () => void;
|
|
17
|
-
}
|
|
18
3
|
export declare class BeanHelper<C extends object> {
|
|
19
|
-
static buildLazyProxyHandler<T extends object>(getObj: () => T,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
static buildLazyProxy<T extends object>(getObj: () => T): T;
|
|
23
|
-
static buildLazyRevocableProxy<T extends object>(getObj: () => T): {
|
|
4
|
+
static buildLazyProxyHandler<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): ProxyHandler<T>;
|
|
5
|
+
static buildLazyProxy<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): T;
|
|
6
|
+
static buildLazyRevocableProxy<T extends object>(getObj: () => T, options?: BeanHelper.ProxyOptions): {
|
|
24
7
|
proxy: T;
|
|
25
8
|
revoke: () => void;
|
|
26
9
|
};
|
|
@@ -32,9 +15,34 @@ export declare class BeanHelper<C extends object> {
|
|
|
32
15
|
private touchAll;
|
|
33
16
|
start(): Promise<void>;
|
|
34
17
|
destroy(): Promise<void>;
|
|
35
|
-
instance<T extends BeanType<C>>(clazz: Constructor<T
|
|
36
|
-
proxy<T extends object>(getObj: () => T, name: string): T;
|
|
18
|
+
instance<T extends BeanHelper.BeanType<C>>(clazz: Constructor<T>, options?: BeanHelper.ProxyOptions): T;
|
|
19
|
+
proxy<T extends object>(getObj: () => T, name: string, options?: BeanHelper.ProxyOptions): T;
|
|
37
20
|
getByName<T>(name: string): T;
|
|
38
21
|
put<T>(instance: T, name?: string, clazz?: Constructor): T;
|
|
39
22
|
}
|
|
23
|
+
export declare namespace BeanHelper {
|
|
24
|
+
const PlaceholderObject: unique symbol;
|
|
25
|
+
abstract class BeanType<C extends object> {
|
|
26
|
+
protected beanHelper: BeanHelper<C>;
|
|
27
|
+
protected ctx: Context;
|
|
28
|
+
protected config: C;
|
|
29
|
+
constructor(beanHelper: BeanHelper<C>);
|
|
30
|
+
start(): Awaitable<void>;
|
|
31
|
+
destroy(): Awaitable<void>;
|
|
32
|
+
}
|
|
33
|
+
interface ClassInfo<T> {
|
|
34
|
+
name: string;
|
|
35
|
+
class?: Constructor<T>;
|
|
36
|
+
instance?: T;
|
|
37
|
+
proxy?: T;
|
|
38
|
+
proxyRevoke?: () => void;
|
|
39
|
+
}
|
|
40
|
+
interface ProxyOptions {
|
|
41
|
+
notBind?: boolean;
|
|
42
|
+
bindOriginal?: boolean;
|
|
43
|
+
hooks?: {
|
|
44
|
+
get: (obj: any, prop: PropertyKey) => any;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
40
48
|
export {};
|
package/lib/utils/Test.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
1
|
+
export declare namespace Test {
|
|
2
|
+
function test(name: string, count: number, serial: boolean, run: () => any): Promise<void>;
|
|
3
|
+
function test2(name: string, count: number, run: () => any): Promise<void>;
|
|
4
|
+
}
|
package/package.json
CHANGED
package/lib/utils/File.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function readDirFiles(dirPath: string, needDir?: boolean): Promise<string[]>;
|
package/lib/utils/I18n.d.ts
DELETED