@e-mc/core 0.7.1 → 0.8.1
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.js +150 -124
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -216,118 +216,122 @@ function updateHostQueue(host, position, priority, broadcast) {
|
|
|
216
216
|
host.formatMessage(2 /* LOG_TYPE.NODE */, 'QUEUE', ["Thread limit exceeded" /* ERR_MESSAGE.THREAD */ + ` (position #${position + 1})`, host.username], 'Priority: ' + priority, { type: 2 /* LOG_TYPE.NODE */, ...module_1.default.LOG_STYLE_WARN });
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
|
+
function isInvalidRange(result, range) {
|
|
220
|
+
const [lower, upper = 0] = range;
|
|
221
|
+
return result.length <= lower || upper > 0 && result.length > upper;
|
|
222
|
+
}
|
|
219
223
|
function asPosix(value) {
|
|
220
224
|
value = path.normalize(value.trim());
|
|
221
|
-
return PLATFORM_WIN32 ? value.replace(
|
|
225
|
+
return PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
|
|
222
226
|
}
|
|
223
227
|
const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
|
|
224
228
|
const expireTime = (value) => Date.now() + value * 1000 /* TIME.S */;
|
|
225
229
|
const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000 /* TIME.S */) || 0;
|
|
226
230
|
class Host extends module_1.default {
|
|
227
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
228
|
-
return parent ? super.purgeMemory(percent, limit) :
|
|
231
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
232
|
+
return parent ? super.purgeMemory(percent, limit) : 0;
|
|
229
233
|
}
|
|
230
234
|
static loadSettings(settings, permission, password) {
|
|
231
235
|
if (typeof permission === 'string') {
|
|
232
236
|
password = permission;
|
|
233
237
|
permission = undefined;
|
|
234
238
|
}
|
|
235
|
-
if (super.loadSettings(settings, password)) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
PERMISSION.setDiskWrite(disk_write);
|
|
246
|
-
}
|
|
247
|
-
if (permission.unc_read) {
|
|
248
|
-
PERMISSION.setUNCRead(unc_read);
|
|
249
|
-
}
|
|
250
|
-
if (permission.unc_write) {
|
|
251
|
-
PERMISSION.setUNCWrite(unc_write);
|
|
252
|
-
}
|
|
239
|
+
if (!super.loadSettings(settings, password)) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
const { permission: perm, process: proc } = settings;
|
|
243
|
+
if (perm) {
|
|
244
|
+
if (permission && Permission.validate(perm)) {
|
|
245
|
+
const { disk_read, disk_write, unc_read, unc_write } = perm;
|
|
246
|
+
PERMISSION = new Permission(true);
|
|
247
|
+
if (permission.disk_read) {
|
|
248
|
+
PERMISSION.setDiskRead(disk_read);
|
|
253
249
|
}
|
|
254
|
-
if (
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
250
|
+
if (permission.disk_write) {
|
|
251
|
+
PERMISSION.setDiskWrite(disk_write);
|
|
252
|
+
}
|
|
253
|
+
if (permission.unc_read) {
|
|
254
|
+
PERMISSION.setUNCRead(unc_read);
|
|
255
|
+
}
|
|
256
|
+
if (permission.unc_write) {
|
|
257
|
+
PERMISSION.setUNCWrite(unc_write);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if ((0, types_1.isObject)(perm.settings)) {
|
|
261
|
+
const { picomatch: pico, minimatch: mini } = perm.settings;
|
|
262
|
+
if ((0, types_1.isPlainObject)(pico)) {
|
|
263
|
+
PICOMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...pico };
|
|
264
|
+
}
|
|
265
|
+
if ((0, types_1.isPlainObject)(mini)) {
|
|
266
|
+
try {
|
|
267
|
+
const pkg = require('minimatch');
|
|
268
|
+
MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
|
|
269
|
+
MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
|
|
258
270
|
}
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
const pkg = require('minimatch');
|
|
262
|
-
MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
|
|
263
|
-
MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
|
|
264
|
-
}
|
|
265
|
-
catch {
|
|
266
|
-
}
|
|
271
|
+
catch {
|
|
267
272
|
}
|
|
268
273
|
}
|
|
269
274
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
275
|
+
}
|
|
276
|
+
if (proc && (0, types_1.isObject)(proc.thread)) {
|
|
277
|
+
let { limit, expires, queue, admin } = proc.thread;
|
|
278
|
+
if (limit && (limit = Math.floor(+limit)) > 0) {
|
|
279
|
+
HOST.THREAD_LIMIT = limit;
|
|
280
|
+
}
|
|
281
|
+
if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
|
|
282
|
+
HOST.THREAD_EXPIRES = expires;
|
|
283
|
+
}
|
|
284
|
+
if ((0, types_1.isObject)(queue)) {
|
|
285
|
+
let priority;
|
|
286
|
+
({ limit, expires, priority } = queue);
|
|
287
|
+
if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
|
|
288
|
+
HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
|
|
274
289
|
}
|
|
275
290
|
if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
|
|
276
|
-
HOST.
|
|
291
|
+
HOST.QUEUE_EXPIRES = expires;
|
|
277
292
|
}
|
|
278
|
-
if ((0, types_1.isObject)(
|
|
279
|
-
let priority;
|
|
280
|
-
(
|
|
281
|
-
|
|
282
|
-
HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
|
|
293
|
+
if ((0, types_1.isObject)(priority)) {
|
|
294
|
+
let { min = -1, max = 0, bypass } = priority;
|
|
295
|
+
if ((min = Math.floor(+min)) >= 0) {
|
|
296
|
+
HOST.PRIORITY_MIN = min;
|
|
283
297
|
}
|
|
284
|
-
if (
|
|
285
|
-
HOST.
|
|
298
|
+
if ((max = Math.floor(+max)) > 0) {
|
|
299
|
+
HOST.PRIORITY_MAX = max;
|
|
286
300
|
}
|
|
287
|
-
if (
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
|
|
299
|
-
HOST.PRIORITY_BYPASS = bypass;
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
bypass = HOST.PRIORITY_BYPASS;
|
|
303
|
-
}
|
|
304
|
-
if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
|
|
305
|
-
HOST.PRIORITY_BYPASS = undefined;
|
|
306
|
-
}
|
|
301
|
+
if (HOST.PRIORITY_MAX < HOST.PRIORITY_MIN) {
|
|
302
|
+
HOST.PRIORITY_MAX = HOST.PRIORITY_MIN + 1;
|
|
303
|
+
}
|
|
304
|
+
if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
|
|
305
|
+
HOST.PRIORITY_BYPASS = bypass;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
bypass = HOST.PRIORITY_BYPASS;
|
|
309
|
+
}
|
|
310
|
+
if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
|
|
311
|
+
HOST.PRIORITY_BYPASS = undefined;
|
|
307
312
|
}
|
|
308
313
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
else {
|
|
318
|
-
HOST.CIPHER_ALGORITHM = null;
|
|
319
|
-
HOST.CIPHER_KEY = null;
|
|
320
|
-
HOST.CIPHER_IV = null;
|
|
321
|
-
}
|
|
314
|
+
}
|
|
315
|
+
if (admin) {
|
|
316
|
+
const cipher = proc.cipher;
|
|
317
|
+
if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
|
|
318
|
+
const algorithm = cipher.algorithm || 'aes-256-gcm';
|
|
319
|
+
HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
|
|
320
|
+
if (HOST.ADMIN_USERS.length) {
|
|
321
|
+
HOST.CIPHER_ALGORITHM = algorithm;
|
|
322
322
|
}
|
|
323
|
-
|
|
324
|
-
HOST.
|
|
323
|
+
else {
|
|
324
|
+
HOST.CIPHER_ALGORITHM = null;
|
|
325
|
+
HOST.CIPHER_KEY = null;
|
|
326
|
+
HOST.CIPHER_IV = null;
|
|
325
327
|
}
|
|
326
328
|
}
|
|
329
|
+
if (typeof admin.private === 'boolean') {
|
|
330
|
+
HOST.ADMIN_PRIVATE = admin.private;
|
|
331
|
+
}
|
|
327
332
|
}
|
|
328
|
-
return true;
|
|
329
333
|
}
|
|
330
|
-
return
|
|
334
|
+
return true;
|
|
331
335
|
}
|
|
332
336
|
static createPermission(all, freeze) {
|
|
333
337
|
const permission = new Permission(freeze);
|
|
@@ -653,8 +657,8 @@ class Host extends module_1.default {
|
|
|
653
657
|
exports.Host = Host;
|
|
654
658
|
_a = kDone, _b = kQueued, _c = kUsername;
|
|
655
659
|
class Client extends module_1.default {
|
|
656
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
657
|
-
return parent ? super.purgeMemory(percent, limit) :
|
|
660
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
661
|
+
return parent ? super.purgeMemory(percent, limit) : 0;
|
|
658
662
|
}
|
|
659
663
|
constructor(data) {
|
|
660
664
|
super();
|
|
@@ -679,6 +683,11 @@ class Client extends module_1.default {
|
|
|
679
683
|
}
|
|
680
684
|
return this;
|
|
681
685
|
}
|
|
686
|
+
getUserSettings() {
|
|
687
|
+
const username = this.host?.username;
|
|
688
|
+
let result;
|
|
689
|
+
return username && (0, types_1.isPlainObject)(result = this.settings.users?.[username]) ? result : null;
|
|
690
|
+
}
|
|
682
691
|
set cacheDir(value) {
|
|
683
692
|
if (path.isAbsolute(value) && module_1.default.isDir(value)) {
|
|
684
693
|
this[kCacheDir] = value;
|
|
@@ -711,43 +720,43 @@ class ClientDb extends Client {
|
|
|
711
720
|
static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
|
|
712
721
|
static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
|
|
713
722
|
static loadSettings(settings, password) {
|
|
714
|
-
if (super.loadSettings({ process: settings.process }, password)) {
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
(stored || (CACHE_SOURCE[key] = { items: [] })).config = { limit: limit > 0 ? limit : 0, percent: Math.min(percent, 1), min: min > 0 ? min : 0, max: max > 0 ? max : Infinity };
|
|
735
|
-
}
|
|
723
|
+
if (!super.loadSettings({ process: settings.process }, password)) {
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
const memory = settings.memory;
|
|
727
|
+
if ((0, types_1.isPlainObject)(memory)) {
|
|
728
|
+
for (const name in memory) {
|
|
729
|
+
const client = memory[name];
|
|
730
|
+
if ((0, types_1.isPlainObject)(client)) {
|
|
731
|
+
for (const source in client) {
|
|
732
|
+
const item = client[source];
|
|
733
|
+
if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
|
|
734
|
+
const key = name + '_' + source;
|
|
735
|
+
const stored = CACHE_SOURCE[key];
|
|
736
|
+
let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
|
|
737
|
+
if (enabled && limit > 0) {
|
|
738
|
+
if ((0, types_1.isString)(percent)) {
|
|
739
|
+
percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
|
|
740
|
+
}
|
|
741
|
+
else if ((percent = Math.max(percent, 0)) > 1) {
|
|
742
|
+
percent /= 100;
|
|
736
743
|
}
|
|
737
|
-
|
|
738
|
-
|
|
744
|
+
if (percent > 0) {
|
|
745
|
+
(stored || (CACHE_SOURCE[key] = { items: [] })).config = { limit: limit > 0 ? limit : 0, percent: Math.min(percent, 1), min: min > 0 ? min : 0, max: max > 0 ? max : Infinity };
|
|
739
746
|
}
|
|
740
747
|
}
|
|
748
|
+
else if (stored) {
|
|
749
|
+
delete stored.config;
|
|
750
|
+
}
|
|
741
751
|
}
|
|
742
752
|
}
|
|
743
753
|
}
|
|
744
754
|
}
|
|
745
|
-
return true;
|
|
746
755
|
}
|
|
747
|
-
return
|
|
756
|
+
return true;
|
|
748
757
|
}
|
|
749
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
750
|
-
return parent ? super.purgeMemory(percent, limit, parent) :
|
|
758
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
759
|
+
return parent ? super.purgeMemory(percent, limit, parent) : 0;
|
|
751
760
|
}
|
|
752
761
|
static getTimeout(value) {
|
|
753
762
|
if (value === undefined) {
|
|
@@ -762,7 +771,7 @@ class ClientDb extends Client {
|
|
|
762
771
|
result = convertSeconds(value);
|
|
763
772
|
break;
|
|
764
773
|
case 'object':
|
|
765
|
-
if (value && typeof (result = value.timeout) === 'string') {
|
|
774
|
+
if (value !== null && typeof (result = value.timeout) === 'string') {
|
|
766
775
|
result = convertSeconds(value.timeout);
|
|
767
776
|
value.timeout = result;
|
|
768
777
|
}
|
|
@@ -898,11 +907,11 @@ class ClientDb extends Client {
|
|
|
898
907
|
}
|
|
899
908
|
return result;
|
|
900
909
|
}
|
|
901
|
-
static purgeResult(prefix) {
|
|
910
|
+
static async purgeResult(prefix) {
|
|
902
911
|
const current = Date.now();
|
|
903
912
|
const providers = new Map();
|
|
904
913
|
if (prefix) {
|
|
905
|
-
if (prefix.
|
|
914
|
+
if (!prefix.includes('_')) {
|
|
906
915
|
prefix += '_';
|
|
907
916
|
for (const key in CACHE_USER) {
|
|
908
917
|
if (key.startsWith(prefix)) {
|
|
@@ -942,7 +951,7 @@ class ClientDb extends Client {
|
|
|
942
951
|
}
|
|
943
952
|
}
|
|
944
953
|
}
|
|
945
|
-
return
|
|
954
|
+
return result;
|
|
946
955
|
}
|
|
947
956
|
static setPoolConfig(value) { }
|
|
948
957
|
static getPoolConfig(source) { }
|
|
@@ -1025,7 +1034,7 @@ class ClientDb extends Client {
|
|
|
1025
1034
|
return this.settingsOf(source, 'coerce', component) === true || component === 'options' && this.settingsOf(source, 'coerce') === true;
|
|
1026
1035
|
}
|
|
1027
1036
|
getQueryResult(source, credential, queryString, options, renewCache) {
|
|
1028
|
-
let sessionKey, value;
|
|
1037
|
+
let sessionKey, value, exclusiveOf;
|
|
1029
1038
|
switch (typeof options) {
|
|
1030
1039
|
case 'boolean':
|
|
1031
1040
|
renewCache = options;
|
|
@@ -1034,8 +1043,16 @@ class ClientDb extends Client {
|
|
|
1034
1043
|
sessionKey = options;
|
|
1035
1044
|
break;
|
|
1036
1045
|
case 'object':
|
|
1037
|
-
if (options) {
|
|
1038
|
-
({ value, sessionKey, renewCache } = options);
|
|
1046
|
+
if (options !== null) {
|
|
1047
|
+
({ value, sessionKey, renewCache, exclusiveOf } = options);
|
|
1048
|
+
if (Array.isArray(exclusiveOf)) {
|
|
1049
|
+
const ignoreCache = exclusiveOf[2];
|
|
1050
|
+
if (ignoreCache === 1) {
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
renewCache || (renewCache = ignoreCache === 0);
|
|
1054
|
+
queryString += '_' + exclusiveOf.toString();
|
|
1055
|
+
}
|
|
1039
1056
|
}
|
|
1040
1057
|
break;
|
|
1041
1058
|
}
|
|
@@ -1046,7 +1063,10 @@ class ClientDb extends Client {
|
|
|
1046
1063
|
value = cache;
|
|
1047
1064
|
}
|
|
1048
1065
|
}
|
|
1049
|
-
|
|
1066
|
+
const result = ClientDb.findResult(this.moduleName + '_' + source, uuidKey || credential, queryString, value === 0 ? 0 : ClientDb.getTimeout(value || this.settingsOf(source, 'cache')), sessionKey, renewCache);
|
|
1067
|
+
if (!exclusiveOf || !isInvalidRange(result, exclusiveOf)) {
|
|
1068
|
+
return result;
|
|
1069
|
+
}
|
|
1050
1070
|
}
|
|
1051
1071
|
setQueryResult(source, credential, queryString, result, sessionKey) {
|
|
1052
1072
|
if (!Array.isArray(result)) {
|
|
@@ -1058,9 +1078,15 @@ class ClientDb extends Client {
|
|
|
1058
1078
|
const uuidKey = ClientDb.extractUUID(credential);
|
|
1059
1079
|
let options;
|
|
1060
1080
|
if (ClientDb.enabled("memory.settings.users" /* KEY_NAME.MEMORY_SETTINGS_USERS */, this.host?.username)) {
|
|
1061
|
-
let value;
|
|
1081
|
+
let value, exclusiveOf;
|
|
1062
1082
|
if ((0, types_1.isObject)(sessionKey)) {
|
|
1063
|
-
({ value, sessionKey } = sessionKey);
|
|
1083
|
+
({ value, sessionKey, exclusiveOf } = sessionKey);
|
|
1084
|
+
}
|
|
1085
|
+
if (Array.isArray(exclusiveOf)) {
|
|
1086
|
+
if (isInvalidRange(result, exclusiveOf)) {
|
|
1087
|
+
return result;
|
|
1088
|
+
}
|
|
1089
|
+
queryString += '_' + exclusiveOf.toString();
|
|
1064
1090
|
}
|
|
1065
1091
|
if (uuidKey) {
|
|
1066
1092
|
const cache = this.settingsKey(uuidKey, 'cache');
|
|
@@ -1153,9 +1179,9 @@ class AbortComponent {
|
|
|
1153
1179
|
map.splice(index, 1);
|
|
1154
1180
|
}
|
|
1155
1181
|
}
|
|
1156
|
-
abort() {
|
|
1182
|
+
abort(reason) {
|
|
1157
1183
|
if (!this.aborted) {
|
|
1158
|
-
this[kAbortHandler].abort();
|
|
1184
|
+
this[kAbortHandler].abort(reason);
|
|
1159
1185
|
}
|
|
1160
1186
|
}
|
|
1161
1187
|
reset() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
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.8.1",
|
|
24
|
+
"@e-mc/types": "0.8.1",
|
|
25
25
|
"picomatch": "^3.0.1"
|
|
26
26
|
}
|
|
27
27
|
}
|