@e-mc/core 0.9.26 → 0.9.28

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 +106 -89
  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.9.26/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.28/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, LogStatus } from "./squared";
@@ -229,13 +229,13 @@ NOTE: **@e-mc/core** is mostly a collection of abstract base classes which canno
229
229
 
230
230
  ## References
231
231
 
232
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/squared.d.ts
233
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/core.d.ts
234
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/db.d.ts
235
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/dom.d.ts
236
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/logger.d.ts
237
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/node.d.ts
238
- - https://www.unpkg.com/@e-mc/types@0.9.26/lib/settings.d.ts
232
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/squared.d.ts
233
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/core.d.ts
234
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/db.d.ts
235
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/dom.d.ts
236
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/logger.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/node.d.ts
238
+ - https://www.unpkg.com/@e-mc/types@0.9.28/lib/settings.d.ts
239
239
 
240
240
  ## LICENSE
241
241
 
package/index.js CHANGED
@@ -99,6 +99,24 @@ function cancelThread(host, hint, rejected) {
99
99
  ++HOST.KILLED;
100
100
  }
101
101
  }
102
+ function killThread() {
103
+ const items = [];
104
+ const current = Date.now();
105
+ for (const [host, pid] of HOST.DONE) {
106
+ if (host.done) {
107
+ closeThread(host);
108
+ }
109
+ else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
110
+ host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
111
+ host.abort();
112
+ closeThread(host);
113
+ }
114
+ else {
115
+ items.push([host, pid]);
116
+ }
117
+ }
118
+ return items;
119
+ }
102
120
  function encryptUsername(data, iv) {
103
121
  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
122
  }
@@ -222,7 +240,7 @@ function asPosix(value) {
222
240
  return path.normalize(value);
223
241
  }
224
242
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
225
- const expireTime = (value) => Date.now() + value * 1000;
243
+ const expireTime = (value) => Date.now() + Math.trunc(value * 1000);
226
244
  const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000) || 0;
227
245
  class Host extends module_1 {
228
246
  static async purgeMemory(percent = 1, limit = 0, parent) {
@@ -377,21 +395,7 @@ class Host extends module_1 {
377
395
  if (HOST.ADMIN_PRIVATE && !username) {
378
396
  return full === true ? { count: -1 } : -1;
379
397
  }
380
- const current = Date.now();
381
- const items = [];
382
- for (const [host, pid] of HOST.DONE) {
383
- if (host.done) {
384
- closeThread(host);
385
- }
386
- else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
387
- host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
388
- host.abort();
389
- closeThread(host);
390
- }
391
- else {
392
- items.push([host, pid]);
393
- }
394
- }
398
+ const items = killThread();
395
399
  const count = items.length;
396
400
  if (full === true) {
397
401
  const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
@@ -473,6 +477,9 @@ class Host extends module_1 {
473
477
  }
474
478
  }
475
479
  this[kConfig] = Object.freeze(config);
480
+ if (HOST.DONE.size >= HOST.THREAD_LIMIT) {
481
+ killThread();
482
+ }
476
483
  if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
477
484
  HOST.DONE.set(this, HOST.PID++);
478
485
  }
@@ -770,28 +777,30 @@ class ClientDb extends Client {
770
777
  const memory = settings.memory;
771
778
  if ((0, types_1.isPlainObject)(memory)) {
772
779
  for (const name in memory) {
773
- const client = memory[name];
774
- if ((0, types_1.isPlainObject)(client)) {
775
- for (const source in client) {
776
- const item = client[source];
777
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
778
- const key = name + '_' + source;
779
- const stored = CACHE_SOURCE[key];
780
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
781
- if (enabled && limit > 0) {
782
- if ((0, types_1.isString)(percent)) {
783
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
784
- }
785
- else if ((percent = Math.max(percent, 0)) > 1) {
786
- percent /= 100;
780
+ if (name !== 'settings') {
781
+ const client = memory[name];
782
+ if ((0, types_1.isPlainObject)(client)) {
783
+ for (const source in client) {
784
+ const item = client[source];
785
+ if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
786
+ const key = name + '_' + source;
787
+ const stored = CACHE_SOURCE[key];
788
+ let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
789
+ if (enabled && limit > 0) {
790
+ if ((0, types_1.isString)(percent)) {
791
+ percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
792
+ }
793
+ else if ((percent = Math.max(percent, 0)) > 1) {
794
+ percent /= 100;
795
+ }
796
+ if (percent > 0) {
797
+ (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 };
798
+ }
787
799
  }
788
- if (percent > 0) {
789
- (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 };
800
+ else if (stored) {
801
+ delete stored.config;
790
802
  }
791
803
  }
792
- else if (stored) {
793
- delete stored.config;
794
- }
795
804
  }
796
805
  }
797
806
  }
@@ -803,7 +812,7 @@ class ClientDb extends Client {
803
812
  return parent ? super.purgeMemory(percent, limit, parent) : 0;
804
813
  }
805
814
  static getTimeout(value) {
806
- if (value === undefined) {
815
+ if (value == null) {
807
816
  return 0;
808
817
  }
809
818
  let result = 0;
@@ -840,17 +849,19 @@ class ClientDb extends Client {
840
849
  if (!userKey) {
841
850
  return;
842
851
  }
843
- queryString = this.asHash(queryString);
844
- if (timeout > 0) {
852
+ if (timeout > 0 && timeout < Infinity) {
845
853
  const userCache = CACHE_USER[source]?.[userKey];
846
- let stored;
847
- if (!(userCache && (stored = userCache[queryString]))) {
854
+ if (!userCache) {
855
+ return;
856
+ }
857
+ const stored = userCache[queryString = this.asHash(queryString)];
858
+ if (!stored) {
848
859
  return;
849
860
  }
850
861
  const [expires, result] = stored;
851
862
  if (result) {
852
863
  const current = Date.now();
853
- let valid;
864
+ let valid = false;
854
865
  if (renewCache) {
855
866
  stored[0] = expireTime(timeout);
856
867
  valid = true;
@@ -876,38 +887,27 @@ class ClientDb extends Client {
876
887
  delete userCache[queryString];
877
888
  }
878
889
  else if (sessionKey) {
879
- return CACHE_SESSION[source]?.[userKey + sessionKey]?.[queryString];
890
+ return CACHE_SESSION[source]?.[userKey + sessionKey]?.[this.asHash(queryString)];
880
891
  }
881
892
  }
882
- static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
893
+ static storeResult(source, credential, queryString, result, cache, sessionKey, sessionExpires) {
883
894
  if (!credential || !Array.isArray(result)) {
884
895
  return result;
885
896
  }
886
- let cache, cacheDir;
897
+ let cacheDir;
887
898
  if ((0, types_1.isObject)(sessionKey)) {
888
899
  ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
889
900
  }
890
- else if ((0, types_1.isObject)(options) && 'cache' in options) {
891
- ({ cache, cacheDir, sessionKey, sessionExpires } = options);
901
+ else if ((0, types_1.isObject)(cache) && 'cache' in cache) {
902
+ ({ cache, cacheDir, sessionKey, sessionExpires } = cache);
892
903
  }
893
904
  let timeout = 0, userKey, whenEmpty, partition;
894
- if (cache) {
895
- switch (typeof cache) {
896
- case 'number':
897
- timeout = cache;
898
- break;
899
- case 'string':
900
- if (typeof sessionKey === 'number') {
901
- sessionExpires = sessionKey;
902
- sessionKey = cache;
903
- break;
904
- }
905
- timeout = convertSeconds(cache);
906
- break;
907
- case 'object': {
905
+ switch (typeof cache) {
906
+ case 'object':
907
+ if (cache) {
908
908
  let seconds, dir;
909
909
  ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
910
- if ((0, types_1.isString)(dir) && (!cacheDir || module_1.isDir(dir))) {
910
+ if (dir && (!cacheDir || module_1.isDir(dir))) {
911
911
  cacheDir = dir;
912
912
  partition = true;
913
913
  }
@@ -915,15 +915,25 @@ class ClientDb extends Client {
915
915
  timeout = seconds;
916
916
  }
917
917
  else {
918
- cache.timeout = seconds ? convertSeconds(seconds) : 0;
918
+ cache.timeout = seconds ? timeout = convertSeconds(seconds) : 0;
919
919
  }
920
+ }
921
+ break;
922
+ case 'number':
923
+ timeout = cache;
924
+ break;
925
+ case 'string':
926
+ if (typeof sessionKey === 'number') {
927
+ sessionExpires = sessionKey;
928
+ sessionKey = cache;
920
929
  break;
921
930
  }
922
- }
931
+ timeout = convertSeconds(cache);
932
+ break;
923
933
  }
924
934
  if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) || this.asHash(this.asString(credential)))) {
925
- queryString = this.asHash(queryString);
926
- if (timeout > 0) {
935
+ if (timeout > 0 && timeout < Infinity) {
936
+ queryString = this.asHash(queryString);
927
937
  const item = [expireTime(timeout), result, Date.now(), 0];
928
938
  ((CACHE_USER[source] ||= {})[userKey] ||= {})[queryString] = item;
929
939
  addSourceResult(source, item);
@@ -954,7 +964,7 @@ class ClientDb extends Client {
954
964
  sourceData[dbKey] = data;
955
965
  setTimeout(() => delete sourceData[dbKey], sessionExpires * 1000);
956
966
  }
957
- data[queryString] = result;
967
+ data[this.asHash(queryString)] = result;
958
968
  }
959
969
  }
960
970
  return result;
@@ -1082,15 +1092,15 @@ class ClientDb extends Client {
1082
1092
  sessionKey = options;
1083
1093
  break;
1084
1094
  case 'object':
1085
- if (options !== null) {
1086
- ({ value, sessionKey, renewCache, exclusiveOf } = options);
1095
+ if (options) {
1096
+ ({ value, sessionKey, exclusiveOf, renewCache } = options);
1087
1097
  if (Array.isArray(exclusiveOf)) {
1088
1098
  const ignoreCache = exclusiveOf[2];
1089
1099
  if (ignoreCache === 1) {
1090
1100
  return;
1091
1101
  }
1092
1102
  renewCache ||= ignoreCache === 0;
1093
- queryString += '_' + exclusiveOf.toString();
1103
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1094
1104
  }
1095
1105
  }
1096
1106
  break;
@@ -1103,7 +1113,7 @@ class ClientDb extends Client {
1103
1113
  }
1104
1114
  }
1105
1115
  const result = ClientDb.findResult(this.moduleName + '_' + source, uuidKey || credential, queryString, value === 0 ? 0 : ClientDb.getTimeout(value || this.settingsOf(source, 'cache')), sessionKey, renewCache);
1106
- if (exclusiveOf && !isInvalidRange(result, exclusiveOf)) {
1116
+ if (exclusiveOf && isInvalidRange(result, exclusiveOf)) {
1107
1117
  return;
1108
1118
  }
1109
1119
  return result;
@@ -1116,30 +1126,37 @@ class ClientDb extends Client {
1116
1126
  return result;
1117
1127
  }
1118
1128
  const uuidKey = ClientDb.extractUUID(credential);
1119
- let options;
1120
- if (ClientDb.enabled("memory.settings.users", this.host?.username)) {
1121
- let value, exclusiveOf;
1122
- if ((0, types_1.isObject)(sessionKey)) {
1123
- ({ value, sessionKey, exclusiveOf } = sessionKey);
1129
+ let value, exclusiveOf;
1130
+ if ((0, types_1.isObject)(sessionKey)) {
1131
+ ({ value, sessionKey, exclusiveOf } = sessionKey);
1132
+ }
1133
+ if (Array.isArray(exclusiveOf)) {
1134
+ if (isInvalidRange(result, exclusiveOf)) {
1135
+ return result;
1124
1136
  }
1125
- if (Array.isArray(exclusiveOf)) {
1126
- if (isInvalidRange(result, exclusiveOf)) {
1127
- return result;
1128
- }
1129
- queryString += '_' + exclusiveOf.toString();
1137
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1138
+ }
1139
+ if (uuidKey) {
1140
+ const cache = this.settingsKey(uuidKey, 'cache');
1141
+ if (cache !== undefined) {
1142
+ value = cache;
1130
1143
  }
1131
- if (uuidKey) {
1132
- const cache = this.settingsKey(uuidKey, 'cache');
1133
- if (cache !== undefined) {
1134
- value = cache;
1135
- }
1144
+ }
1145
+ const cacheDir = this.cacheDir;
1146
+ value ??= this.settingsOf(source, 'cache');
1147
+ let options;
1148
+ if (ClientDb.enabled("memory.settings.users", this.host?.username)) {
1149
+ if (cacheDir || sessionKey) {
1150
+ options = { cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1136
1151
  }
1137
- options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1138
1152
  }
1139
- else {
1140
- options = { cacheDir: this.cacheDir };
1153
+ else if (cacheDir) {
1154
+ options = { cacheDir };
1155
+ }
1156
+ else if (!(0, types_1.isObject)(value) || !(0, types_1.isString)(value.dir)) {
1157
+ return result;
1141
1158
  }
1142
- return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
1159
+ return value || options ? ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, value, options) : result;
1143
1160
  }
1144
1161
  applyState(items, value, as) {
1145
1162
  for (const item of Array.isArray(items) ? items : [items]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.9.26",
3
+ "version": "0.9.28",
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.9.26",
24
- "@e-mc/types": "0.9.26",
23
+ "@e-mc/module": "0.9.28",
24
+ "@e-mc/types": "0.9.28",
25
25
  "picomatch": "^4.0.3"
26
26
  }
27
27
  }