@e-mc/core 0.10.18 → 0.10.19

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.
Files changed (3) hide show
  1. package/README.md +8 -8
  2. package/index.js +123 -98
  3. 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.10.18/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.10.19/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, LogStatus } from "./squared";
@@ -230,13 +230,13 @@ NOTE: **@e-mc/core** is mostly a collection of abstract base classes which canno
230
230
 
231
231
  ## References
232
232
 
233
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/squared.d.ts
234
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/core.d.ts
235
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/db.d.ts
236
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/dom.d.ts
237
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/logger.d.ts
238
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/node.d.ts
239
- - https://www.unpkg.com/@e-mc/types@0.10.18/lib/settings.d.ts
233
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/squared.d.ts
234
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/core.d.ts
235
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/db.d.ts
236
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/dom.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/logger.d.ts
238
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/node.d.ts
239
+ - https://www.unpkg.com/@e-mc/types@0.10.19/lib/settings.d.ts
240
240
 
241
241
  ## LICENSE
242
242
 
package/index.js CHANGED
@@ -67,10 +67,17 @@ function closeHostThread(host) {
67
67
  resumeHostQueue(item, true);
68
68
  }
69
69
  }
70
- if (HOST.QUEUE.length > 0 && HOST.DONE.size < HOST.THREAD_LIMIT) {
71
- const queue = HOST.QUEUE.shift();
72
- addHostThread(queue.instance);
73
- resumeHostQueue(queue);
70
+ const length = HOST.QUEUE.length;
71
+ if (length > 0 && HOST.DONE.size < HOST.THREAD_LIMIT) {
72
+ for (let i = 0; i < length; ++i) {
73
+ const queue = HOST.QUEUE[i];
74
+ if (queue.joined) {
75
+ addHostThread(queue.instance);
76
+ resumeHostQueue(queue);
77
+ HOST.QUEUE.splice(i, 1);
78
+ break;
79
+ }
80
+ }
74
81
  HOST.QUEUE.forEach((item, index) => {
75
82
  notifyHostQueue(item.instance, index, item.priority, true);
76
83
  });
@@ -144,16 +151,32 @@ function resumeHostQueue(item, aborted) {
144
151
  }
145
152
  }
146
153
  }
147
- HOST.LOG_PROGRESS_QUEUE.delete(instance);
148
154
  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
155
  }
150
- queueMicrotask(() => {
151
- try {
156
+ HOST.LOG_PROGRESS_QUEUE.delete(instance);
157
+ if (typeof instance.resumeThread === 'function') {
158
+ queueMicrotask(() => {
152
159
  instance.resumeThread({ args, startTime, aborted });
160
+ });
161
+ }
162
+ }
163
+ function killHostThread() {
164
+ const items = [];
165
+ const current = Date.now();
166
+ for (const [host, pid] of HOST.DONE) {
167
+ if (host.done) {
168
+ closeHostThread(host);
153
169
  }
154
- catch {
170
+ else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
171
+ host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
172
+ host.abort();
173
+ closeHostThread(host);
155
174
  }
156
- });
175
+ else {
176
+ items.push([host, pid]);
177
+ }
178
+ }
179
+ return items;
157
180
  }
158
181
  function encryptUsername(data, iv) {
159
182
  return HOST.CIPHER_ALGORITHM && HOST.CIPHER_KEY && (0, types_1.isString)(data) && (0, types_1.encryptUTF8)(HOST.CIPHER_ALGORITHM, HOST.CIPHER_KEY, iv, data) || '';
@@ -245,7 +268,7 @@ function asPosix(value) {
245
268
  return path.normalize(value);
246
269
  }
247
270
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
248
- const expireTime = (value) => Date.now() + value * 1000;
271
+ const expireTime = (value) => Date.now() + Math.trunc(value * 1000);
249
272
  const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000) || 0;
250
273
  class Host extends module_1 {
251
274
  static async purgeMemory(percent = 1, limit = 0, parent) {
@@ -400,21 +423,7 @@ class Host extends module_1 {
400
423
  if (HOST.ADMIN_PRIVATE && !username) {
401
424
  return full === true ? { count: -1 } : -1;
402
425
  }
403
- const current = Date.now();
404
- const items = [];
405
- for (const [host, pid] of HOST.DONE) {
406
- if (host.done) {
407
- closeHostThread(host);
408
- }
409
- else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
410
- host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
411
- host.abort();
412
- closeHostThread(host);
413
- }
414
- else {
415
- items.push([host, pid]);
416
- }
417
- }
426
+ const items = killHostThread();
418
427
  const count = items.length;
419
428
  if (full === true) {
420
429
  const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
@@ -499,10 +508,16 @@ class Host extends module_1 {
499
508
  this.ignoreLog(typeof log === 'boolean' ? !log : log);
500
509
  }
501
510
  this[kConfig] = Object.freeze(config);
511
+ if (HOST.DONE.size >= HOST.THREAD_LIMIT) {
512
+ killHostThread();
513
+ }
502
514
  if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
503
515
  HOST.DONE.set(this, HOST.PID++);
504
516
  if (HOST.LOG_PROGRESS) {
505
517
  this.pauseLog('progress');
518
+ if (showProgress) {
519
+ HOST.LOG_PROGRESS_QUEUE.add(this);
520
+ }
506
521
  }
507
522
  else if (showProgress) {
508
523
  HOST.LOG_PROGRESS = this;
@@ -701,10 +716,10 @@ class Host extends module_1 {
701
716
  addHostThread(this);
702
717
  return false;
703
718
  }
704
- if (Array.isArray(args)) {
705
- queue.args = args;
706
- }
707
719
  if (!queue.joined) {
720
+ if (Array.isArray(args)) {
721
+ queue.args = args;
722
+ }
708
723
  queue.joined = true;
709
724
  ++HOST.QUEUED;
710
725
  }
@@ -842,27 +857,29 @@ class ClientDb extends Client {
842
857
  const memory = settings.memory;
843
858
  if ((0, types_1.isPlainObject)(memory)) {
844
859
  for (const name in memory) {
845
- const client = memory[name];
846
- if ((0, types_1.isPlainObject)(client)) {
847
- for (const source in client) {
848
- const item = client[source];
849
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
850
- const key = name + '_' + source;
851
- const stored = CACHE_SOURCE[key];
852
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
853
- if (enabled && limit > 0) {
854
- if ((0, types_1.isString)(percent)) {
855
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
860
+ if (name !== 'settings') {
861
+ const client = memory[name];
862
+ if ((0, types_1.isPlainObject)(client)) {
863
+ for (const source in client) {
864
+ const item = client[source];
865
+ if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
866
+ const key = name + '_' + source;
867
+ const stored = CACHE_SOURCE[key];
868
+ let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
869
+ if (enabled && limit > 0) {
870
+ if ((0, types_1.isString)(percent)) {
871
+ percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
872
+ }
873
+ else if ((percent = Math.max(percent, 0)) > 1) {
874
+ percent /= 100;
875
+ }
876
+ if (percent > 0) {
877
+ (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 };
878
+ }
856
879
  }
857
- else if ((percent = Math.max(percent, 0)) > 1) {
858
- percent /= 100;
880
+ else if (stored) {
881
+ delete stored.config;
859
882
  }
860
- if (percent > 0) {
861
- (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 };
862
- }
863
- }
864
- else if (stored) {
865
- delete stored.config;
866
883
  }
867
884
  }
868
885
  }
@@ -875,7 +892,7 @@ class ClientDb extends Client {
875
892
  return parent ? super.purgeMemory(percent, limit, parent) : 0;
876
893
  }
877
894
  static getTimeout(value) {
878
- if (value === undefined) {
895
+ if (value == null) {
879
896
  return 0;
880
897
  }
881
898
  let result = 0;
@@ -912,17 +929,19 @@ class ClientDb extends Client {
912
929
  if (!userKey) {
913
930
  return;
914
931
  }
915
- queryString = (0, types_1.hashKey)(queryString);
916
- if (timeout > 0) {
932
+ if (timeout > 0 && timeout < Infinity) {
917
933
  const userCache = CACHE_USER[source]?.[userKey];
918
- let stored;
919
- if (!(userCache && (stored = userCache[queryString]))) {
934
+ if (!userCache) {
935
+ return;
936
+ }
937
+ const stored = userCache[queryString = (0, types_1.hashKey)(queryString)];
938
+ if (!stored) {
920
939
  return;
921
940
  }
922
941
  const [expires, result] = stored;
923
942
  if (result) {
924
943
  const current = Date.now();
925
- let valid;
944
+ let valid = false;
926
945
  if (renewCache) {
927
946
  stored[0] = expireTime(timeout);
928
947
  valid = true;
@@ -948,38 +967,27 @@ class ClientDb extends Client {
948
967
  delete userCache[queryString];
949
968
  }
950
969
  else if (sessionKey) {
951
- return CACHE_SESSION[source]?.[userKey + sessionKey]?.[queryString];
970
+ return CACHE_SESSION[source]?.[userKey + sessionKey]?.[(0, types_1.hashKey)(queryString)];
952
971
  }
953
972
  }
954
- static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
973
+ static storeResult(source, credential, queryString, result, cache, sessionKey, sessionExpires) {
955
974
  if (!credential || !Array.isArray(result)) {
956
975
  return result;
957
976
  }
958
- let cache, cacheDir;
977
+ let cacheDir;
959
978
  if ((0, types_1.isObject)(sessionKey)) {
960
979
  ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
961
980
  }
962
- else if ((0, types_1.isObject)(options) && 'cache' in options) {
963
- ({ cache, cacheDir, sessionKey, sessionExpires } = options);
981
+ else if ((0, types_1.isObject)(cache) && 'cache' in cache) {
982
+ ({ cache, cacheDir, sessionKey, sessionExpires } = cache);
964
983
  }
965
984
  let timeout = 0, userKey, whenEmpty, partition;
966
- if (cache) {
967
- switch (typeof cache) {
968
- case 'number':
969
- timeout = cache;
970
- break;
971
- case 'string':
972
- if (typeof sessionKey === 'number') {
973
- sessionExpires = sessionKey;
974
- sessionKey = cache;
975
- break;
976
- }
977
- timeout = convertSeconds(cache);
978
- break;
979
- case 'object': {
985
+ switch (typeof cache) {
986
+ case 'object':
987
+ if (cache) {
980
988
  let seconds, dir;
981
989
  ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
982
- if ((0, types_1.isString)(dir) && (!cacheDir || module_1.isDir(dir))) {
990
+ if (dir && (!cacheDir || module_1.isDir(dir))) {
983
991
  cacheDir = dir;
984
992
  partition = true;
985
993
  }
@@ -987,15 +995,25 @@ class ClientDb extends Client {
987
995
  timeout = seconds;
988
996
  }
989
997
  else {
990
- cache.timeout = seconds ? convertSeconds(seconds) : 0;
998
+ cache.timeout = seconds ? timeout = convertSeconds(seconds) : 0;
991
999
  }
1000
+ }
1001
+ break;
1002
+ case 'number':
1003
+ timeout = cache;
1004
+ break;
1005
+ case 'string':
1006
+ if (typeof sessionKey === 'number') {
1007
+ sessionExpires = sessionKey;
1008
+ sessionKey = cache;
992
1009
  break;
993
1010
  }
994
- }
1011
+ timeout = convertSeconds(cache);
1012
+ break;
995
1013
  }
996
1014
  if ((result.length > 0 || whenEmpty) && (userKey = this.extractUUID(credential) || (0, types_1.hashKey)(this.asString(credential)))) {
997
- queryString = (0, types_1.hashKey)(queryString);
998
- if (timeout > 0) {
1015
+ if (timeout > 0 && timeout < Infinity) {
1016
+ queryString = (0, types_1.hashKey)(queryString);
999
1017
  const item = [expireTime(timeout), result, Date.now(), 0];
1000
1018
  ((CACHE_USER[source] ||= {})[userKey] ||= {})[queryString] = item;
1001
1019
  addSourceResult(source, item);
@@ -1026,7 +1044,7 @@ class ClientDb extends Client {
1026
1044
  sourceData[dbKey] = data;
1027
1045
  setTimeout(() => delete sourceData[dbKey], sessionExpires * 1000);
1028
1046
  }
1029
- data[queryString] = result;
1047
+ data[(0, types_1.hashKey)(queryString)] = result;
1030
1048
  }
1031
1049
  }
1032
1050
  return result;
@@ -1154,7 +1172,7 @@ class ClientDb extends Client {
1154
1172
  sessionKey = options;
1155
1173
  break;
1156
1174
  case 'object':
1157
- if (options !== null) {
1175
+ if (options) {
1158
1176
  ({ value, sessionKey, exclusiveOf, renewCache } = options);
1159
1177
  if (Array.isArray(exclusiveOf)) {
1160
1178
  const ignoreCache = exclusiveOf[2];
@@ -1188,30 +1206,37 @@ class ClientDb extends Client {
1188
1206
  return result;
1189
1207
  }
1190
1208
  const uuidKey = ClientDb.extractUUID(credential);
1191
- let options;
1192
- if (ClientDb.enabled("memory.settings.users", this.host?.username)) {
1193
- let value, exclusiveOf;
1194
- if ((0, types_1.isObject)(sessionKey)) {
1195
- ({ value, sessionKey, exclusiveOf } = sessionKey);
1209
+ let value, exclusiveOf;
1210
+ if ((0, types_1.isObject)(sessionKey)) {
1211
+ ({ value, sessionKey, exclusiveOf } = sessionKey);
1212
+ }
1213
+ if (Array.isArray(exclusiveOf)) {
1214
+ if (isInvalidRange(result, exclusiveOf)) {
1215
+ return result;
1196
1216
  }
1197
- if (Array.isArray(exclusiveOf)) {
1198
- if (isInvalidRange(result, exclusiveOf)) {
1199
- return result;
1200
- }
1201
- queryString += '_' + exclusiveOf.slice(0, 2).toString();
1217
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1218
+ }
1219
+ if (uuidKey) {
1220
+ const cache = this.settingsKey(uuidKey, 'cache');
1221
+ if (cache !== undefined) {
1222
+ value = cache;
1202
1223
  }
1203
- if (uuidKey) {
1204
- const cache = this.settingsKey(uuidKey, 'cache');
1205
- if (cache !== undefined) {
1206
- value = cache;
1207
- }
1224
+ }
1225
+ const cacheDir = this.cacheDir;
1226
+ value ??= this.settingsOf(source, 'cache');
1227
+ let options;
1228
+ if (ClientDb.enabled("memory.settings.users", this.host?.username)) {
1229
+ if (cacheDir || sessionKey) {
1230
+ options = { cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1208
1231
  }
1209
- options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1210
1232
  }
1211
- else {
1212
- options = { cacheDir: this.cacheDir };
1233
+ else if (cacheDir) {
1234
+ options = { cacheDir };
1235
+ }
1236
+ else if (!(0, types_1.isObject)(value) || !(0, types_1.isString)(value.dir)) {
1237
+ return result;
1213
1238
  }
1214
- return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
1239
+ return value || options ? ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, value, options) : result;
1215
1240
  }
1216
1241
  getCacheResult(source, credential, queryString, cacheValue, ignoreCache) {
1217
1242
  if (ignoreCache !== 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.10.18",
3
+ "version": "0.10.19",
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": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.10.18",
24
- "@e-mc/types": "0.10.18",
23
+ "@e-mc/module": "0.10.19",
24
+ "@e-mc/types": "0.10.19",
25
25
  "picomatch": "^4.0.3"
26
26
  }
27
27
  }