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