@e-mc/core 0.9.7 → 0.10.0
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/README.md +11 -10
- package/index.d.ts +12 -12
- package/index.js +239 -159
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { DataSource, LogStatus } from "./squared";
|
|
@@ -34,8 +34,8 @@ interface IHost extends IModule {
|
|
|
34
34
|
willLog(name: string): boolean;
|
|
35
35
|
ignoreLog(values: boolean | string | string[]): void;
|
|
36
36
|
collectLog(level?: boolean): LogStatus<StatusType>[];
|
|
37
|
-
pauseLog(): void;
|
|
38
|
-
resumeLog(): void;
|
|
37
|
+
pauseLog(type?: string): void;
|
|
38
|
+
resumeLog(type?: string): void;
|
|
39
39
|
hasLog(type: string): boolean;
|
|
40
40
|
delayMessage(...args: unknown[]): void;
|
|
41
41
|
willAbort(value: string | IModule): boolean;
|
|
@@ -100,6 +100,7 @@ interface IClientDb extends IClient<IHost, ClientModule<ClientDbSettings>> {
|
|
|
100
100
|
getQueryResult(source: string, credential: unknown, queryString: string, options?: CacheOptions, renewCache?: boolean): QueryResult | undefined;
|
|
101
101
|
setQueryResult(source: string, credential: unknown, queryString: string, result: unknown, sessionKey?: string): QueryResult;
|
|
102
102
|
setQueryResult(source: string, credential: unknown, queryString: string, result: unknown, options?: CacheOptions): QueryResult;
|
|
103
|
+
getCacheResult(source: string, credential: unknown, queryString: string, cacheValue: CacheOptions, ignoreCache?: unknown): QueryResult | undefined;
|
|
103
104
|
applyState(items: DataSource | DataSource[], value: number, as?: boolean): void;
|
|
104
105
|
commit(items?: DataSource[]): Promise<boolean>;
|
|
105
106
|
valueOfKey(credential: unknown, name: keyof DbSourceOptions, component?: keyof DbCoerceSettings): unknown;
|
|
@@ -229,13 +230,13 @@ NOTE: **@e-mc/core** is mostly a collection of abstract base classes which canno
|
|
|
229
230
|
|
|
230
231
|
## References
|
|
231
232
|
|
|
232
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
233
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
234
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
235
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
236
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
237
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
238
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
233
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/squared.d.ts
|
|
234
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/core.d.ts
|
|
235
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/db.d.ts
|
|
236
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/dom.d.ts
|
|
237
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/logger.d.ts
|
|
238
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/node.d.ts
|
|
239
|
+
- https://www.unpkg.com/@e-mc/types@0.10.0/lib/settings.d.ts
|
|
239
240
|
|
|
240
241
|
## LICENSE
|
|
241
242
|
|
package/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { HostConstructor, ModuleConstructor } from '../types/lib';
|
|
2
|
-
import type { AbortComponentConstructor, ClientConstructor, ClientDbConstructor, PermissionConstructor } from '../types/lib/core';
|
|
3
|
-
|
|
4
|
-
declare namespace core {
|
|
5
|
-
const Module: ModuleConstructor;
|
|
6
|
-
const Host: HostConstructor;
|
|
7
|
-
const Client: ClientConstructor;
|
|
8
|
-
const ClientDb: ClientDbConstructor;
|
|
9
|
-
const AbortComponent: AbortComponentConstructor;
|
|
10
|
-
const Permission: PermissionConstructor;
|
|
11
|
-
}
|
|
12
|
-
|
|
1
|
+
import type { HostConstructor, ModuleConstructor } from '../types/lib';
|
|
2
|
+
import type { AbortComponentConstructor, ClientConstructor, ClientDbConstructor, PermissionConstructor } from '../types/lib/core';
|
|
3
|
+
|
|
4
|
+
declare namespace core {
|
|
5
|
+
const Module: ModuleConstructor;
|
|
6
|
+
const Host: HostConstructor;
|
|
7
|
+
const Client: ClientConstructor;
|
|
8
|
+
const ClientDb: ClientDbConstructor;
|
|
9
|
+
const AbortComponent: AbortComponentConstructor;
|
|
10
|
+
const Permission: PermissionConstructor;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
13
|
export = core;
|
package/index.js
CHANGED
|
@@ -4,9 +4,9 @@ exports.Module = exports.Permission = exports.AbortComponent = exports.ClientDb
|
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const fs = require("fs");
|
|
6
6
|
const pm = require("picomatch");
|
|
7
|
-
const types_1 = require("@e-mc/types");
|
|
8
7
|
const module_1 = require("@e-mc/module");
|
|
9
8
|
exports.Module = module_1;
|
|
9
|
+
const types_1 = require("@e-mc/types");
|
|
10
10
|
const kConfig = Symbol('config');
|
|
11
11
|
const kQueued = Symbol('queued');
|
|
12
12
|
const kAbortHandler = Symbol('abortHandler');
|
|
@@ -41,57 +41,45 @@ const HOST = {
|
|
|
41
41
|
THREAD_EXPIRES: 86400000,
|
|
42
42
|
PRIORITY_MIN: 0,
|
|
43
43
|
PRIORITY_MAX: 100,
|
|
44
|
-
PRIORITY_BYPASS: undefined
|
|
44
|
+
PRIORITY_BYPASS: undefined,
|
|
45
|
+
LOG_PROGRESS: null,
|
|
46
|
+
LOG_DELAYED: [],
|
|
47
|
+
LOG_PROGRESS_QUEUE: new WeakSet(),
|
|
48
|
+
LOG_PROGRESS_PAUSED: new Set()
|
|
45
49
|
};
|
|
46
|
-
const PLATFORM_WIN32 = process.platform === 'win32';
|
|
47
50
|
const HOST_USERNAME = new WeakMap();
|
|
48
51
|
let STORE_RESULT_COUNT = 0;
|
|
49
|
-
let PICOMATCH_OPTIONS = { nocase: PLATFORM_WIN32 };
|
|
50
|
-
let MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32 };
|
|
52
|
+
let PICOMATCH_OPTIONS = { nocase: module_1.PLATFORM_WIN32 };
|
|
53
|
+
let MINIMATCH_OPTIONS = { matchBase: true, nocase: module_1.PLATFORM_WIN32 };
|
|
51
54
|
let MINIMATCH;
|
|
52
55
|
let PERMISSION;
|
|
53
|
-
function
|
|
54
|
-
const result = options[name];
|
|
55
|
-
return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
|
|
56
|
-
}
|
|
57
|
-
function closeThread(host) {
|
|
56
|
+
function closeHostThread(host) {
|
|
58
57
|
HOST.DONE.delete(host);
|
|
58
|
+
resumeHostLog(host);
|
|
59
59
|
++HOST.CLOSED;
|
|
60
60
|
const current = Date.now();
|
|
61
61
|
for (let i = 0; i < HOST.QUEUE.length; ++i) {
|
|
62
62
|
const item = HOST.QUEUE[i];
|
|
63
63
|
const { instance, startTime } = item;
|
|
64
64
|
if (startTime + HOST.QUEUE_EXPIRES <= current) {
|
|
65
|
-
|
|
65
|
+
cancelHostThread(instance, 'expired: ' + (0, types_1.formatTime)(startTime, true));
|
|
66
66
|
HOST.QUEUE.splice(i--, 1);
|
|
67
|
-
|
|
67
|
+
resumeHostQueue(item, true);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
if (HOST.QUEUE.length && HOST.DONE.size < HOST.THREAD_LIMIT) {
|
|
70
|
+
if (HOST.QUEUE.length > 0 && HOST.DONE.size < HOST.THREAD_LIMIT) {
|
|
71
71
|
const queue = HOST.QUEUE.shift();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
HOST.QUEUE.forEach((item, index) =>
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
function callResumeThread(item, aborted) {
|
|
78
|
-
if (item.joined) {
|
|
79
|
-
const { instance, args = [], startTime } = item;
|
|
80
|
-
if (!aborted) {
|
|
81
|
-
instance.writeTimeElapsed('JOIN', ['Thread restarting...', HOST.THREAD_LIMIT > 1 ? 'Availability: ' + HOST.DONE.size + ' / ' + HOST.THREAD_LIMIT : ''], startTime, { type: 2, ...module_1.LOG_STYLE_WARN });
|
|
82
|
-
}
|
|
83
|
-
queueMicrotask(() => {
|
|
84
|
-
try {
|
|
85
|
-
instance.resumeThread({ args, startTime, aborted });
|
|
86
|
-
}
|
|
87
|
-
catch {
|
|
88
|
-
}
|
|
72
|
+
addHostThread(queue.instance);
|
|
73
|
+
resumeHostQueue(queue);
|
|
74
|
+
HOST.QUEUE.forEach((item, index) => {
|
|
75
|
+
notifyHostQueue(item.instance, index, item.priority, true);
|
|
89
76
|
});
|
|
90
77
|
}
|
|
91
78
|
}
|
|
92
|
-
function
|
|
79
|
+
function cancelHostThread(host, hint, rejected) {
|
|
93
80
|
host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Thread was killed", hint), { type: 2, fatal: true });
|
|
94
81
|
host.abort();
|
|
82
|
+
resetHostLog(host);
|
|
95
83
|
if (rejected) {
|
|
96
84
|
++HOST.REJECTED;
|
|
97
85
|
}
|
|
@@ -99,6 +87,74 @@ function cancelThread(host, hint, rejected) {
|
|
|
99
87
|
++HOST.KILLED;
|
|
100
88
|
}
|
|
101
89
|
}
|
|
90
|
+
function resetHostLog(host) {
|
|
91
|
+
if (HOST.LOG_PROGRESS === host) {
|
|
92
|
+
HOST.LOG_PROGRESS = null;
|
|
93
|
+
HOST.LOG_DELAYED.length = 0;
|
|
94
|
+
}
|
|
95
|
+
HOST.LOG_PROGRESS_QUEUE.delete(host);
|
|
96
|
+
HOST.LOG_PROGRESS_PAUSED.delete(host);
|
|
97
|
+
}
|
|
98
|
+
function resumeHostLog(host) {
|
|
99
|
+
resetHostLog(host);
|
|
100
|
+
if (!HOST.LOG_PROGRESS) {
|
|
101
|
+
for (const item of HOST.LOG_PROGRESS_PAUSED) {
|
|
102
|
+
item.resumeLog('progress');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function addHostThread(host) {
|
|
107
|
+
HOST.DONE.set(host, HOST.PID++);
|
|
108
|
+
host[kQueued] = false;
|
|
109
|
+
}
|
|
110
|
+
function notifyHostQueue(host, position, priority, broadcast) {
|
|
111
|
+
if (!broadcast || host.broadcastId) {
|
|
112
|
+
host.formatMessage(2, 'QUEUE', ["Thread limit exceeded" + ` (position #${position + 1})`, host.username], 'Priority: ' + priority, { type: 2, ...module_1.LOG_STYLE_WARN });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function addHostQueue(item) {
|
|
116
|
+
HOST.QUEUE.push(item);
|
|
117
|
+
HOST.QUEUE.sort((a, b) => {
|
|
118
|
+
const au = a.instance.username;
|
|
119
|
+
const bu = b.instance.username;
|
|
120
|
+
if (au && !bu) {
|
|
121
|
+
return -1;
|
|
122
|
+
}
|
|
123
|
+
if (!au && bu) {
|
|
124
|
+
return 1;
|
|
125
|
+
}
|
|
126
|
+
return a.priority - b.priority;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function resumeHostQueue(item, aborted) {
|
|
130
|
+
if (!item.joined) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const { instance, args = [], startTime } = item;
|
|
134
|
+
if (!aborted) {
|
|
135
|
+
if (HOST.LOG_PROGRESS) {
|
|
136
|
+
instance.pauseLog('progress');
|
|
137
|
+
}
|
|
138
|
+
else if (HOST.LOG_PROGRESS_QUEUE.has(instance)) {
|
|
139
|
+
HOST.LOG_PROGRESS = instance;
|
|
140
|
+
instance.resumeLog('progress');
|
|
141
|
+
for (const host of HOST.DONE.keys()) {
|
|
142
|
+
if (host !== instance) {
|
|
143
|
+
host.pauseLog('progress');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
HOST.LOG_PROGRESS_QUEUE.delete(instance);
|
|
148
|
+
instance.writeTimeElapsed('JOIN', ['Thread restarting...', HOST.THREAD_LIMIT > 1 ? 'Availability: ' + HOST.DONE.size + ' / ' + HOST.THREAD_LIMIT : ''], startTime, { type: 2, ...module_1.LOG_STYLE_WARN });
|
|
149
|
+
}
|
|
150
|
+
queueMicrotask(() => {
|
|
151
|
+
try {
|
|
152
|
+
instance.resumeThread({ args, startTime, aborted });
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
102
158
|
function encryptUsername(data, iv) {
|
|
103
159
|
return HOST.CIPHER_ALGORITHM && HOST.CIPHER_KEY && (0, types_1.isString)(data) && (0, types_1.encryptUTF8)(HOST.CIPHER_ALGORITHM, HOST.CIPHER_KEY, iv, data) || '';
|
|
104
160
|
}
|
|
@@ -121,35 +177,21 @@ function setPermission(name, pathname, enabled) {
|
|
|
121
177
|
this[name] = Object.freeze({ enabled, value: enabled ? pathname : '' });
|
|
122
178
|
}
|
|
123
179
|
}
|
|
124
|
-
function
|
|
125
|
-
const result = [];
|
|
126
|
-
|
|
127
|
-
let method = null;
|
|
128
|
-
if (typeof value === 'string') {
|
|
129
|
-
method = module_1.parseFunction(value, { external: true, absolute: true, sync: true });
|
|
130
|
-
}
|
|
131
|
-
else if (typeof value === 'function') {
|
|
132
|
-
method = value;
|
|
133
|
-
}
|
|
134
|
-
if (method) {
|
|
135
|
-
result.push(method);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
}
|
|
140
|
-
function clearSourceResult(target) {
|
|
141
|
-
const result = target[1];
|
|
142
|
-
target[1] = null;
|
|
180
|
+
function clearSourceResult(data) {
|
|
181
|
+
const result = data[1];
|
|
182
|
+
data[1] = null;
|
|
143
183
|
if (typeof result === 'string') {
|
|
144
184
|
fs.unlink(result, () => { });
|
|
145
185
|
}
|
|
146
186
|
}
|
|
147
|
-
function addSourceResult(source,
|
|
187
|
+
function addSourceResult(source, data) {
|
|
148
188
|
const { items, config } = CACHE_SOURCE[source] || (CACHE_SOURCE[source] = { items: [] });
|
|
149
189
|
if (config && items.length >= config.limit) {
|
|
150
190
|
const { percent, min } = config;
|
|
151
191
|
if (percent === 1) {
|
|
152
|
-
|
|
192
|
+
for (const item of items) {
|
|
193
|
+
clearSourceResult(item);
|
|
194
|
+
}
|
|
153
195
|
items.length = 0;
|
|
154
196
|
}
|
|
155
197
|
else {
|
|
@@ -164,51 +206,32 @@ function addSourceResult(source, target) {
|
|
|
164
206
|
}
|
|
165
207
|
}
|
|
166
208
|
}
|
|
167
|
-
items.push(
|
|
209
|
+
items.push(data);
|
|
168
210
|
}
|
|
169
|
-
function deleteSourceResult(source,
|
|
211
|
+
function deleteSourceResult(source, data) {
|
|
170
212
|
const stored = CACHE_SOURCE[source];
|
|
171
213
|
if (stored) {
|
|
172
214
|
const items = stored.items;
|
|
173
|
-
const index = items.findIndex(item => item ===
|
|
215
|
+
const index = items.findIndex(item => item === data);
|
|
174
216
|
if (index !== -1) {
|
|
175
217
|
items.splice(index, 1);
|
|
176
218
|
}
|
|
177
219
|
}
|
|
178
220
|
}
|
|
179
|
-
function incrementSourceCount(source,
|
|
180
|
-
const count = ++
|
|
221
|
+
function incrementSourceCount(source, data, cacheData, queryString) {
|
|
222
|
+
const count = ++data[3];
|
|
181
223
|
const stored = CACHE_SOURCE[source];
|
|
182
224
|
if (stored?.config && count >= stored.config.max) {
|
|
183
|
-
clearSourceResult(
|
|
184
|
-
deleteSourceResult(source,
|
|
225
|
+
clearSourceResult(data);
|
|
226
|
+
deleteSourceResult(source, data);
|
|
185
227
|
if (cacheData && queryString) {
|
|
186
228
|
delete cacheData[queryString];
|
|
187
229
|
}
|
|
188
230
|
}
|
|
189
231
|
}
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const au = a.instance.username;
|
|
194
|
-
const bu = b.instance.username;
|
|
195
|
-
if (au && !bu) {
|
|
196
|
-
return -1;
|
|
197
|
-
}
|
|
198
|
-
if (!au && bu) {
|
|
199
|
-
return 1;
|
|
200
|
-
}
|
|
201
|
-
return a.priority - b.priority;
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
function resumeHostQueue(host) {
|
|
205
|
-
HOST.DONE.set(host, HOST.PID++);
|
|
206
|
-
host[kQueued] = false;
|
|
207
|
-
}
|
|
208
|
-
function updateHostQueue(host, position, priority, broadcast) {
|
|
209
|
-
if (!broadcast || host.broadcastId) {
|
|
210
|
-
host.formatMessage(2, 'QUEUE', ["Thread limit exceeded" + ` (position #${position + 1})`, host.username], 'Priority: ' + priority, { type: 2, ...module_1.LOG_STYLE_WARN });
|
|
211
|
-
}
|
|
232
|
+
function getSettingsValue(options, name, component) {
|
|
233
|
+
const result = options[name];
|
|
234
|
+
return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
|
|
212
235
|
}
|
|
213
236
|
function isInvalidRange(result, range) {
|
|
214
237
|
const [lower, upper = 0] = range;
|
|
@@ -216,7 +239,7 @@ function isInvalidRange(result, range) {
|
|
|
216
239
|
}
|
|
217
240
|
function asPosix(value) {
|
|
218
241
|
value = path.normalize(value.trim());
|
|
219
|
-
return PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
|
|
242
|
+
return module_1.PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
|
|
220
243
|
}
|
|
221
244
|
const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
|
|
222
245
|
const expireTime = (value) => Date.now() + value * 1000;
|
|
@@ -251,23 +274,23 @@ class Host extends module_1 {
|
|
|
251
274
|
PERMISSION.setUNCWrite(unc_write);
|
|
252
275
|
}
|
|
253
276
|
}
|
|
254
|
-
if (
|
|
277
|
+
if (perm.settings) {
|
|
255
278
|
const { picomatch: pico, minimatch: mini } = perm.settings;
|
|
256
279
|
if ((0, types_1.isPlainObject)(pico)) {
|
|
257
|
-
PICOMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...pico };
|
|
280
|
+
PICOMATCH_OPTIONS = { matchBase: true, nocase: module_1.PLATFORM_WIN32, ...pico };
|
|
258
281
|
}
|
|
259
282
|
if ((0, types_1.isPlainObject)(mini)) {
|
|
260
283
|
try {
|
|
261
284
|
const pkg = require('minimatch');
|
|
262
285
|
MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
|
|
263
|
-
MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
|
|
286
|
+
MINIMATCH_OPTIONS = { matchBase: true, nocase: module_1.PLATFORM_WIN32, ...mini };
|
|
264
287
|
}
|
|
265
288
|
catch {
|
|
266
289
|
}
|
|
267
290
|
}
|
|
268
291
|
}
|
|
269
292
|
}
|
|
270
|
-
if (proc
|
|
293
|
+
if (proc?.thread) {
|
|
271
294
|
let { limit, expires, queue, admin } = proc.thread;
|
|
272
295
|
if (limit && (limit = Math.floor(+limit)) > 0) {
|
|
273
296
|
HOST.THREAD_LIMIT = limit;
|
|
@@ -275,7 +298,7 @@ class Host extends module_1 {
|
|
|
275
298
|
if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
|
|
276
299
|
HOST.THREAD_EXPIRES = expires;
|
|
277
300
|
}
|
|
278
|
-
if (
|
|
301
|
+
if (queue) {
|
|
279
302
|
let priority;
|
|
280
303
|
({ limit, expires, priority } = queue);
|
|
281
304
|
if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
|
|
@@ -284,7 +307,7 @@ class Host extends module_1 {
|
|
|
284
307
|
if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
|
|
285
308
|
HOST.QUEUE_EXPIRES = expires;
|
|
286
309
|
}
|
|
287
|
-
if (
|
|
310
|
+
if (priority) {
|
|
288
311
|
let { min = -1, max = 0, bypass } = priority;
|
|
289
312
|
if ((min = Math.floor(+min)) >= 0) {
|
|
290
313
|
HOST.PRIORITY_MIN = min;
|
|
@@ -311,7 +334,7 @@ class Host extends module_1 {
|
|
|
311
334
|
if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
|
|
312
335
|
const algorithm = cipher.algorithm || 'aes-256-gcm';
|
|
313
336
|
HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
|
|
314
|
-
if (HOST.ADMIN_USERS.length) {
|
|
337
|
+
if (HOST.ADMIN_USERS.length > 0) {
|
|
315
338
|
HOST.CIPHER_ALGORITHM = algorithm;
|
|
316
339
|
}
|
|
317
340
|
else {
|
|
@@ -345,8 +368,8 @@ class Host extends module_1 {
|
|
|
345
368
|
if (HOST.ADMIN_USERS.includes(encryptUsername(username, iv))) {
|
|
346
369
|
for (const [host, id] of HOST.DONE) {
|
|
347
370
|
if (pid === true || id === pid || Array.isArray(pid) && pid.includes(id)) {
|
|
348
|
-
|
|
349
|
-
|
|
371
|
+
cancelHostThread(host, 'pid: ' + id.toString());
|
|
372
|
+
closeHostThread(host);
|
|
350
373
|
++result;
|
|
351
374
|
}
|
|
352
375
|
}
|
|
@@ -377,12 +400,12 @@ class Host extends module_1 {
|
|
|
377
400
|
const items = [];
|
|
378
401
|
for (const [host, pid] of HOST.DONE) {
|
|
379
402
|
if (host.done) {
|
|
380
|
-
|
|
403
|
+
closeHostThread(host);
|
|
381
404
|
}
|
|
382
405
|
else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
|
|
383
406
|
host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
|
|
384
407
|
host.abort();
|
|
385
|
-
|
|
408
|
+
closeHostThread(host);
|
|
386
409
|
}
|
|
387
410
|
else {
|
|
388
411
|
items.push([host, pid]);
|
|
@@ -421,6 +444,9 @@ class Host extends module_1 {
|
|
|
421
444
|
}
|
|
422
445
|
return count;
|
|
423
446
|
}
|
|
447
|
+
static getLogDelayed() {
|
|
448
|
+
return HOST.LOG_DELAYED;
|
|
449
|
+
}
|
|
424
450
|
static getPermissionFromSettings() {
|
|
425
451
|
return PERMISSION;
|
|
426
452
|
}
|
|
@@ -445,40 +471,47 @@ class Host extends module_1 {
|
|
|
445
471
|
if (broadcastId) {
|
|
446
472
|
this.broadcastId = broadcastId;
|
|
447
473
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
454
|
-
if (exclude) {
|
|
455
|
-
this.ignoreLog(exclude);
|
|
456
|
-
}
|
|
457
|
-
else if (typeof enabled === 'boolean') {
|
|
458
|
-
this.ignoreLog(!enabled);
|
|
459
|
-
}
|
|
460
|
-
if (useColor) {
|
|
461
|
-
this.supports('stripAnsi', false);
|
|
462
|
-
}
|
|
463
|
-
if (showProgress) {
|
|
464
|
-
this._logDelayed = [];
|
|
465
|
-
}
|
|
474
|
+
let showProgress;
|
|
475
|
+
if ((0, types_1.isPlainObject)(log)) {
|
|
476
|
+
const { enabled, level, exclude, useColor, silent } = log;
|
|
477
|
+
if (level !== undefined) {
|
|
478
|
+
this.logLevel = level;
|
|
466
479
|
}
|
|
467
|
-
|
|
468
|
-
this.ignoreLog(
|
|
480
|
+
if (exclude) {
|
|
481
|
+
this.ignoreLog(exclude);
|
|
482
|
+
}
|
|
483
|
+
else if (typeof enabled === 'boolean') {
|
|
484
|
+
this.ignoreLog(!enabled);
|
|
485
|
+
}
|
|
486
|
+
if (useColor) {
|
|
487
|
+
this.supports('stripAnsi', false);
|
|
488
|
+
}
|
|
489
|
+
if (typeof silent === 'boolean') {
|
|
490
|
+
this.silent = silent;
|
|
469
491
|
}
|
|
492
|
+
showProgress = log.showProgress;
|
|
493
|
+
}
|
|
494
|
+
else if (log !== undefined) {
|
|
495
|
+
this.ignoreLog(typeof log === 'boolean' ? !log : log);
|
|
470
496
|
}
|
|
471
497
|
this[kConfig] = Object.freeze(config);
|
|
472
498
|
if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
|
|
473
499
|
HOST.DONE.set(this, HOST.PID++);
|
|
500
|
+
if (HOST.LOG_PROGRESS) {
|
|
501
|
+
this.pauseLog('progress');
|
|
502
|
+
}
|
|
503
|
+
else if (showProgress) {
|
|
504
|
+
HOST.LOG_PROGRESS = this;
|
|
505
|
+
this._logDelayed = [];
|
|
506
|
+
}
|
|
474
507
|
}
|
|
475
508
|
else if (typeof priority === 'number' && priority >= HOST.PRIORITY_MIN && priority <= HOST.PRIORITY_MAX && HOST.QUEUE.length < HOST.QUEUE_LIMIT && typeof this.resumeThread === 'function') {
|
|
476
509
|
this[kQueued] = true;
|
|
477
510
|
addHostQueue({ instance: this, priority, startTime: Date.now() });
|
|
478
|
-
|
|
511
|
+
notifyHostQueue(this, HOST.QUEUE.findIndex(item => item.instance === this), priority);
|
|
479
512
|
}
|
|
480
513
|
else {
|
|
481
|
-
|
|
514
|
+
cancelHostThread(this, 'limit: ' + HOST.THREAD_LIMIT.toString(), true);
|
|
482
515
|
}
|
|
483
516
|
}
|
|
484
517
|
restart(...args) { }
|
|
@@ -492,12 +525,14 @@ class Host extends module_1 {
|
|
|
492
525
|
if (typeof items[0] === 'boolean' && items.shift()) {
|
|
493
526
|
data.clear();
|
|
494
527
|
}
|
|
495
|
-
|
|
528
|
+
for (const item of items) {
|
|
529
|
+
data.add(item);
|
|
530
|
+
}
|
|
496
531
|
return this;
|
|
497
532
|
}
|
|
498
533
|
contains(item, condition) {
|
|
499
534
|
const data = this._usingObjects;
|
|
500
|
-
if (data.size) {
|
|
535
|
+
if (data.size > 0) {
|
|
501
536
|
if (data.has(item)) {
|
|
502
537
|
return true;
|
|
503
538
|
}
|
|
@@ -566,12 +601,12 @@ class Host extends module_1 {
|
|
|
566
601
|
collectLog(level) {
|
|
567
602
|
const result = this.status.slice(0);
|
|
568
603
|
for (const item of this.modules) {
|
|
569
|
-
if (item.status.length) {
|
|
604
|
+
if (item.status.length > 0) {
|
|
570
605
|
result.push(...item.status);
|
|
571
606
|
}
|
|
572
607
|
}
|
|
573
608
|
for (const item of this.subProcesses) {
|
|
574
|
-
if (item.status.length) {
|
|
609
|
+
if (item.status.length > 0) {
|
|
575
610
|
result.push(...item.status);
|
|
576
611
|
}
|
|
577
612
|
}
|
|
@@ -581,40 +616,47 @@ class Host extends module_1 {
|
|
|
581
616
|
hasLog(type) {
|
|
582
617
|
switch (type) {
|
|
583
618
|
case 'progress':
|
|
584
|
-
return this
|
|
619
|
+
return this === HOST.LOG_PROGRESS;
|
|
585
620
|
default:
|
|
586
621
|
return false;
|
|
587
622
|
}
|
|
588
623
|
}
|
|
589
|
-
pauseLog() {
|
|
590
|
-
if (
|
|
591
|
-
this
|
|
624
|
+
pauseLog(type) {
|
|
625
|
+
if (type === 'progress') {
|
|
626
|
+
this._logDelayed || (this._logDelayed = []);
|
|
627
|
+
HOST.LOG_PROGRESS_PAUSED.add(this);
|
|
592
628
|
}
|
|
629
|
+
this[kLogState] = 0;
|
|
593
630
|
}
|
|
594
|
-
resumeLog() {
|
|
631
|
+
resumeLog(type) {
|
|
595
632
|
var _k;
|
|
596
|
-
|
|
597
|
-
if (delayed) {
|
|
633
|
+
if (!type || this.hasLog(type) || type === 'progress' && !HOST.LOG_PROGRESS) {
|
|
598
634
|
this[kLogState] = 1;
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
635
|
+
const log = this._logDelayed;
|
|
636
|
+
if (log?.length) {
|
|
637
|
+
const trailing = {};
|
|
638
|
+
for (const args of log) {
|
|
639
|
+
(_k = args[4]).statusType && (_k.statusType = 0);
|
|
640
|
+
if (typeof args[4].progressBar === 'boolean') {
|
|
641
|
+
trailing[args[1]] = args;
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
this.formatMessage(...args);
|
|
645
|
+
}
|
|
604
646
|
}
|
|
605
|
-
|
|
606
|
-
|
|
647
|
+
for (const title in trailing) {
|
|
648
|
+
const args = trailing[title];
|
|
649
|
+
delete args[4].progressBar;
|
|
650
|
+
this.formatMessage(...args);
|
|
607
651
|
}
|
|
652
|
+
log.length = 0;
|
|
608
653
|
}
|
|
609
|
-
|
|
610
|
-
const args = trailing[title];
|
|
611
|
-
delete args[4].progressBar;
|
|
612
|
-
this.formatMessage(...args);
|
|
613
|
-
}
|
|
654
|
+
resumeHostLog(this);
|
|
614
655
|
}
|
|
615
656
|
}
|
|
616
657
|
delayMessage(args) {
|
|
617
658
|
this._logDelayed?.push(args);
|
|
659
|
+
HOST.LOG_DELAYED.push(args);
|
|
618
660
|
}
|
|
619
661
|
retain(process) {
|
|
620
662
|
this.subProcesses.add(process);
|
|
@@ -622,9 +664,9 @@ class Host extends module_1 {
|
|
|
622
664
|
}
|
|
623
665
|
release(process, log = true) {
|
|
624
666
|
if (this.subProcesses.delete(process)) {
|
|
625
|
-
if (log) {
|
|
667
|
+
if (log && !process.silent) {
|
|
626
668
|
const status = process.status;
|
|
627
|
-
if (status.length) {
|
|
669
|
+
if (status.length > 0) {
|
|
628
670
|
this.status.push(...status);
|
|
629
671
|
status.length = 0;
|
|
630
672
|
}
|
|
@@ -653,7 +695,7 @@ class Host extends module_1 {
|
|
|
653
695
|
}
|
|
654
696
|
if (reject) {
|
|
655
697
|
HOST.QUEUE.splice(index, 1);
|
|
656
|
-
|
|
698
|
+
addHostThread(this);
|
|
657
699
|
return false;
|
|
658
700
|
}
|
|
659
701
|
if (Array.isArray(args)) {
|
|
@@ -680,7 +722,7 @@ class Host extends module_1 {
|
|
|
680
722
|
set done(value) {
|
|
681
723
|
if (value) {
|
|
682
724
|
this[kDone] = true;
|
|
683
|
-
|
|
725
|
+
closeHostThread(this);
|
|
684
726
|
}
|
|
685
727
|
}
|
|
686
728
|
get done() {
|
|
@@ -717,8 +759,8 @@ class Client extends module_1 {
|
|
|
717
759
|
this.permission = result;
|
|
718
760
|
}
|
|
719
761
|
}
|
|
720
|
-
if (
|
|
721
|
-
this._extensions = extensions.slice(0);
|
|
762
|
+
if ((0, types_1.isArray)(extensions)) {
|
|
763
|
+
this._extensions || (this._extensions = extensions.slice(0));
|
|
722
764
|
}
|
|
723
765
|
if (cache_dir) {
|
|
724
766
|
this.cacheDir = cache_dir;
|
|
@@ -730,6 +772,25 @@ class Client extends module_1 {
|
|
|
730
772
|
let result;
|
|
731
773
|
return username && (0, types_1.isPlainObject)(result = this.settings.users?.[username]) ? result : null;
|
|
732
774
|
}
|
|
775
|
+
checkExtensions(values) {
|
|
776
|
+
if (values.every(value => typeof value === 'function')) {
|
|
777
|
+
return values;
|
|
778
|
+
}
|
|
779
|
+
const result = [];
|
|
780
|
+
for (const value of values) {
|
|
781
|
+
let method = null;
|
|
782
|
+
if (typeof value === 'string') {
|
|
783
|
+
method = module_1.parseFunction(value, { external: true, absolute: true, sync: true });
|
|
784
|
+
}
|
|
785
|
+
else if (typeof value === 'function') {
|
|
786
|
+
method = value;
|
|
787
|
+
}
|
|
788
|
+
if (method) {
|
|
789
|
+
result.push(method);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return result;
|
|
793
|
+
}
|
|
733
794
|
set cacheDir(value) {
|
|
734
795
|
if (path.isAbsolute(value) && module_1.isDir(value)) {
|
|
735
796
|
this[kCacheDir] = value;
|
|
@@ -748,19 +809,30 @@ class Client extends module_1 {
|
|
|
748
809
|
}
|
|
749
810
|
}
|
|
750
811
|
get extensions() {
|
|
751
|
-
|
|
752
|
-
return extensions ? this._extensions = getFunctions(extensions) : [];
|
|
812
|
+
return this._extensions ? this._extensions = this.checkExtensions(this._extensions) : [];
|
|
753
813
|
}
|
|
754
814
|
}
|
|
755
815
|
exports.Client = Client;
|
|
756
816
|
_d = kCacheDir;
|
|
757
817
|
class ClientDb extends Client {
|
|
758
|
-
static get TRANSACTION_ACTIVE() {
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
static get
|
|
762
|
-
|
|
763
|
-
|
|
818
|
+
static get TRANSACTION_ACTIVE() {
|
|
819
|
+
return 1;
|
|
820
|
+
}
|
|
821
|
+
static get TRANSACTION_PARTIAL() {
|
|
822
|
+
return 2;
|
|
823
|
+
}
|
|
824
|
+
static get TRANSACTION_COMMIT() {
|
|
825
|
+
return 4;
|
|
826
|
+
}
|
|
827
|
+
static get TRANSACTION_TERMINATE() {
|
|
828
|
+
return 8;
|
|
829
|
+
}
|
|
830
|
+
static get TRANSACTION_ABORT() {
|
|
831
|
+
return 16;
|
|
832
|
+
}
|
|
833
|
+
static get TRANSACTION_FAIL() {
|
|
834
|
+
return 32;
|
|
835
|
+
}
|
|
764
836
|
static loadSettings(settings, password) {
|
|
765
837
|
if (!super.loadSettings({ process: settings.process }, password)) {
|
|
766
838
|
return false;
|
|
@@ -834,11 +906,11 @@ class ClientDb extends Client {
|
|
|
834
906
|
return result > 0 ? result : 0;
|
|
835
907
|
}
|
|
836
908
|
static findResult(source, credential, queryString, timeout, sessionKey, renewCache) {
|
|
837
|
-
const userKey = this.extractUUID(credential) ||
|
|
909
|
+
const userKey = this.extractUUID(credential) || (0, types_1.hashKey)(this.asString(credential));
|
|
838
910
|
if (!userKey) {
|
|
839
911
|
return;
|
|
840
912
|
}
|
|
841
|
-
queryString =
|
|
913
|
+
queryString = (0, types_1.hashKey)(queryString);
|
|
842
914
|
if (timeout > 0) {
|
|
843
915
|
const userCache = CACHE_USER[source]?.[userKey];
|
|
844
916
|
let stored;
|
|
@@ -917,8 +989,8 @@ class ClientDb extends Client {
|
|
|
917
989
|
}
|
|
918
990
|
}
|
|
919
991
|
}
|
|
920
|
-
if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) ||
|
|
921
|
-
queryString =
|
|
992
|
+
if ((result.length > 0 || whenEmpty) && (userKey = this.extractUUID(credential) || (0, types_1.hashKey)(this.asString(credential)))) {
|
|
993
|
+
queryString = (0, types_1.hashKey)(queryString);
|
|
922
994
|
if (timeout > 0) {
|
|
923
995
|
const item = [expireTime(timeout), result, Date.now(), 0];
|
|
924
996
|
((_k = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_k[userKey] = {}))[queryString] = item;
|
|
@@ -1130,6 +1202,12 @@ class ClientDb extends Client {
|
|
|
1130
1202
|
}
|
|
1131
1203
|
return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
|
|
1132
1204
|
}
|
|
1205
|
+
getCacheResult(source, credential, queryString, cacheValue, ignoreCache) {
|
|
1206
|
+
if (ignoreCache !== 1) {
|
|
1207
|
+
cacheValue.renewCache = ignoreCache === 0;
|
|
1208
|
+
return this.getQueryResult(source, credential, queryString, cacheValue);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1133
1211
|
applyState(items, value, as) {
|
|
1134
1212
|
for (const item of Array.isArray(items) ? items : [items]) {
|
|
1135
1213
|
if ((0, types_1.hasBit)(value, 8)) {
|
|
@@ -1193,14 +1271,16 @@ class AbortComponent {
|
|
|
1193
1271
|
ABORT_LISTENER.set(instance, map = []);
|
|
1194
1272
|
}
|
|
1195
1273
|
if (!map.find(item => item.signal === signal)) {
|
|
1196
|
-
const listener = () =>
|
|
1274
|
+
const listener = () => {
|
|
1275
|
+
instance.abort();
|
|
1276
|
+
};
|
|
1197
1277
|
signal.addEventListener('abort', listener, options);
|
|
1198
1278
|
map.push({ signal, listener });
|
|
1199
1279
|
}
|
|
1200
1280
|
}
|
|
1201
1281
|
static detach(instance, signal) {
|
|
1202
1282
|
const map = ABORT_LISTENER.get(instance);
|
|
1203
|
-
let index;
|
|
1283
|
+
let index = 0;
|
|
1204
1284
|
if (map && (index = map.findIndex(item => item.signal === signal)) !== -1) {
|
|
1205
1285
|
const target = map[index];
|
|
1206
1286
|
target.signal.removeEventListener('abort', target.listener);
|
|
@@ -1306,7 +1386,7 @@ class Permission {
|
|
|
1306
1386
|
}
|
|
1307
1387
|
if (Array.isArray(value)) {
|
|
1308
1388
|
const items = value.filter(item => (0, types_1.isString)(item));
|
|
1309
|
-
if (items.length) {
|
|
1389
|
+
if (items.length > 0) {
|
|
1310
1390
|
return items.map(item => asPosix(item));
|
|
1311
1391
|
}
|
|
1312
1392
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Core modules for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.
|
|
24
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/module": "0.10.0",
|
|
24
|
+
"@e-mc/types": "0.10.0",
|
|
25
25
|
"picomatch": "^4.0.2"
|
|
26
26
|
}
|
|
27
27
|
}
|