@e-mc/core 0.8.28 → 0.8.29

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 +105 -88
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/index.d.ts
12
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/index.d.ts
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, LogStatus } from "./squared";
@@ -165,13 +165,13 @@ interface IPermission {
165
165
 
166
166
  ## References
167
167
 
168
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/squared.d.ts
169
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/core.d.ts
170
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/db.d.ts
171
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/dom.d.ts
172
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/logger.d.ts
173
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/node.d.ts
174
- - https://www.unpkg.com/@e-mc/types@0.8.28/lib/settings.d.ts
168
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/squared.d.ts
169
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/core.d.ts
170
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/db.d.ts
171
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/dom.d.ts
172
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/logger.d.ts
173
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/node.d.ts
174
+ - https://www.unpkg.com/@e-mc/types@0.8.29/lib/settings.d.ts
175
175
 
176
176
  ## LICENSE
177
177
 
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
  }
@@ -228,7 +246,7 @@ function asPosix(value) {
228
246
  return path.normalize(value);
229
247
  }
230
248
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
231
- const expireTime = (value) => Date.now() + value * 1000;
249
+ const expireTime = (value) => Date.now() + Math.trunc(value * 1000);
232
250
  const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000) || 0;
233
251
  class Host extends module_1.default {
234
252
  static async purgeMemory(percent = 1, limit = 0, parent) {
@@ -383,21 +401,7 @@ class Host extends module_1.default {
383
401
  if (HOST.ADMIN_PRIVATE && !username) {
384
402
  return full === true ? { count: -1 } : -1;
385
403
  }
386
- const current = Date.now();
387
- const items = [];
388
- for (const [host, pid] of HOST.DONE) {
389
- if (host.done) {
390
- closeThread(host);
391
- }
392
- else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
393
- host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
394
- host.abort();
395
- closeThread(host);
396
- }
397
- else {
398
- items.push([host, pid]);
399
- }
400
- }
404
+ const items = killThread();
401
405
  const count = items.length;
402
406
  if (full === true) {
403
407
  const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
@@ -474,6 +478,9 @@ class Host extends module_1.default {
474
478
  }
475
479
  }
476
480
  this[kConfig] = Object.freeze(config);
481
+ if (HOST.DONE.size >= HOST.THREAD_LIMIT) {
482
+ killThread();
483
+ }
477
484
  if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
478
485
  HOST.DONE.set(this, HOST.PID++);
479
486
  }
@@ -732,28 +739,30 @@ class ClientDb extends Client {
732
739
  const memory = settings.memory;
733
740
  if ((0, types_1.isPlainObject)(memory)) {
734
741
  for (const name in memory) {
735
- const client = memory[name];
736
- if ((0, types_1.isPlainObject)(client)) {
737
- for (const source in client) {
738
- const item = client[source];
739
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
740
- const key = name + '_' + source;
741
- const stored = CACHE_SOURCE[key];
742
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
743
- if (enabled && limit > 0) {
744
- if ((0, types_1.isString)(percent)) {
745
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
746
- }
747
- else if ((percent = Math.max(percent, 0)) > 1) {
748
- percent /= 100;
742
+ if (name !== 'settings') {
743
+ const client = memory[name];
744
+ if ((0, types_1.isPlainObject)(client)) {
745
+ for (const source in client) {
746
+ const item = client[source];
747
+ if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
748
+ const key = name + '_' + source;
749
+ const stored = CACHE_SOURCE[key];
750
+ let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
751
+ if (enabled && limit > 0) {
752
+ if ((0, types_1.isString)(percent)) {
753
+ percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
754
+ }
755
+ else if ((percent = Math.max(percent, 0)) > 1) {
756
+ percent /= 100;
757
+ }
758
+ if (percent > 0) {
759
+ (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 };
760
+ }
749
761
  }
750
- if (percent > 0) {
751
- (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 };
762
+ else if (stored) {
763
+ delete stored.config;
752
764
  }
753
765
  }
754
- else if (stored) {
755
- delete stored.config;
756
- }
757
766
  }
758
767
  }
759
768
  }
@@ -765,7 +774,7 @@ class ClientDb extends Client {
765
774
  return parent ? super.purgeMemory(percent, limit, parent) : 0;
766
775
  }
767
776
  static getTimeout(value) {
768
- if (value === undefined) {
777
+ if (value == null) {
769
778
  return 0;
770
779
  }
771
780
  let result = 0;
@@ -803,17 +812,19 @@ class ClientDb extends Client {
803
812
  if (!userKey) {
804
813
  return;
805
814
  }
806
- queryString = this.asHash(queryString);
807
- if (timeout > 0) {
815
+ if (timeout > 0 && timeout < Infinity) {
808
816
  const userCache = (_j = CACHE_USER[source]) === null || _j === void 0 ? void 0 : _j[userKey];
809
- let stored;
810
- if (!(userCache && (stored = userCache[queryString]))) {
817
+ if (!userCache) {
818
+ return;
819
+ }
820
+ const stored = userCache[queryString = this.asHash(queryString)];
821
+ if (!stored) {
811
822
  return;
812
823
  }
813
824
  const [expires, result] = stored;
814
825
  if (result) {
815
826
  const current = Date.now();
816
- let valid;
827
+ let valid = false;
817
828
  if (renewCache) {
818
829
  stored[0] = expireTime(timeout);
819
830
  valid = true;
@@ -839,39 +850,28 @@ class ClientDb extends Client {
839
850
  delete userCache[queryString];
840
851
  }
841
852
  else if (sessionKey) {
842
- return (_l = (_k = CACHE_SESSION[source]) === null || _k === void 0 ? void 0 : _k[userKey + sessionKey]) === null || _l === void 0 ? void 0 : _l[queryString];
853
+ return (_l = (_k = CACHE_SESSION[source]) === null || _k === void 0 ? void 0 : _k[userKey + sessionKey]) === null || _l === void 0 ? void 0 : _l[this.asHash(queryString)];
843
854
  }
844
855
  }
845
- static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
856
+ static storeResult(source, credential, queryString, result, cache, sessionKey, sessionExpires) {
846
857
  var _j;
847
858
  if (!credential || !Array.isArray(result)) {
848
859
  return result;
849
860
  }
850
- let cache, cacheDir;
861
+ let cacheDir;
851
862
  if ((0, types_1.isObject)(sessionKey)) {
852
863
  ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
853
864
  }
854
- else if ((0, types_1.isObject)(options) && 'cache' in options) {
855
- ({ cache, cacheDir, sessionKey, sessionExpires } = options);
865
+ else if ((0, types_1.isObject)(cache) && 'cache' in cache) {
866
+ ({ cache, cacheDir, sessionKey, sessionExpires } = cache);
856
867
  }
857
868
  let timeout = 0, userKey, whenEmpty, partition;
858
- if (cache) {
859
- switch (typeof cache) {
860
- case 'number':
861
- timeout = cache;
862
- break;
863
- case 'string':
864
- if (typeof sessionKey === 'number') {
865
- sessionExpires = sessionKey;
866
- sessionKey = cache;
867
- break;
868
- }
869
- timeout = convertSeconds(cache);
870
- break;
871
- case 'object': {
869
+ switch (typeof cache) {
870
+ case 'object':
871
+ if (cache) {
872
872
  let seconds, dir;
873
873
  ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
874
- if ((0, types_1.isString)(dir) && (!cacheDir || module_1.default.isDir(dir))) {
874
+ if (dir && (!cacheDir || module_1.default.isDir(dir))) {
875
875
  cacheDir = dir;
876
876
  partition = true;
877
877
  }
@@ -879,15 +879,25 @@ class ClientDb extends Client {
879
879
  timeout = seconds;
880
880
  }
881
881
  else {
882
- cache.timeout = seconds ? convertSeconds(seconds) : 0;
882
+ cache.timeout = seconds ? timeout = convertSeconds(seconds) : 0;
883
883
  }
884
+ }
885
+ break;
886
+ case 'number':
887
+ timeout = cache;
888
+ break;
889
+ case 'string':
890
+ if (typeof sessionKey === 'number') {
891
+ sessionExpires = sessionKey;
892
+ sessionKey = cache;
884
893
  break;
885
894
  }
886
- }
895
+ timeout = convertSeconds(cache);
896
+ break;
887
897
  }
888
898
  if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) || this.asHash(this.asString(credential)))) {
889
- queryString = this.asHash(queryString);
890
- if (timeout > 0) {
899
+ if (timeout > 0 && timeout < Infinity) {
900
+ queryString = this.asHash(queryString);
891
901
  const item = [expireTime(timeout), result, Date.now(), 0];
892
902
  ((_j = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_j[userKey] = {}))[queryString] = item;
893
903
  addSourceResult(source, item);
@@ -918,7 +928,7 @@ class ClientDb extends Client {
918
928
  sourceData[dbKey] = data;
919
929
  setTimeout(() => delete sourceData[dbKey], sessionExpires * 1000);
920
930
  }
921
- data[queryString] = result;
931
+ data[this.asHash(queryString)] = result;
922
932
  }
923
933
  }
924
934
  return result;
@@ -1059,15 +1069,15 @@ class ClientDb extends Client {
1059
1069
  sessionKey = options;
1060
1070
  break;
1061
1071
  case 'object':
1062
- if (options !== null) {
1063
- ({ value, sessionKey, renewCache, exclusiveOf } = options);
1072
+ if (options) {
1073
+ ({ value, sessionKey, exclusiveOf, renewCache } = options);
1064
1074
  if (Array.isArray(exclusiveOf)) {
1065
1075
  const ignoreCache = exclusiveOf[2];
1066
1076
  if (ignoreCache === 1) {
1067
1077
  return;
1068
1078
  }
1069
1079
  renewCache || (renewCache = ignoreCache === 0);
1070
- queryString += '_' + exclusiveOf.toString();
1080
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1071
1081
  }
1072
1082
  }
1073
1083
  break;
@@ -1094,30 +1104,37 @@ class ClientDb extends Client {
1094
1104
  return result;
1095
1105
  }
1096
1106
  const uuidKey = ClientDb.extractUUID(credential);
1097
- let options;
1098
- if (ClientDb.enabled("memory.settings.users", (_j = this.host) === null || _j === void 0 ? void 0 : _j.username)) {
1099
- let value, exclusiveOf;
1100
- if ((0, types_1.isObject)(sessionKey)) {
1101
- ({ value, sessionKey, exclusiveOf } = sessionKey);
1107
+ let value, exclusiveOf;
1108
+ if ((0, types_1.isObject)(sessionKey)) {
1109
+ ({ value, sessionKey, exclusiveOf } = sessionKey);
1110
+ }
1111
+ if (Array.isArray(exclusiveOf)) {
1112
+ if (isInvalidRange(result, exclusiveOf)) {
1113
+ return result;
1102
1114
  }
1103
- if (Array.isArray(exclusiveOf)) {
1104
- if (isInvalidRange(result, exclusiveOf)) {
1105
- return result;
1106
- }
1107
- queryString += '_' + exclusiveOf.toString();
1115
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1116
+ }
1117
+ if (uuidKey) {
1118
+ const cache = this.settingsKey(uuidKey, 'cache');
1119
+ if (cache !== undefined) {
1120
+ value = cache;
1108
1121
  }
1109
- if (uuidKey) {
1110
- const cache = this.settingsKey(uuidKey, 'cache');
1111
- if (cache !== undefined) {
1112
- value = cache;
1113
- }
1122
+ }
1123
+ const cacheDir = this.cacheDir;
1124
+ value !== null && value !== void 0 ? value : (value = this.settingsOf(source, 'cache'));
1125
+ let options;
1126
+ if (ClientDb.enabled("memory.settings.users", (_j = this.host) === null || _j === void 0 ? void 0 : _j.username)) {
1127
+ if (cacheDir || sessionKey) {
1128
+ options = { cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1114
1129
  }
1115
- options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1116
1130
  }
1117
- else {
1118
- options = { cacheDir: this.cacheDir };
1131
+ else if (cacheDir) {
1132
+ options = { cacheDir };
1133
+ }
1134
+ else if (!(0, types_1.isObject)(value) || !(0, types_1.isString)(value.dir)) {
1135
+ return result;
1119
1136
  }
1120
- return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
1137
+ return value || options ? ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, value, options) : result;
1121
1138
  }
1122
1139
  applyState(items, value, as) {
1123
1140
  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.8.28",
3
+ "version": "0.8.29",
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.8.28",
24
- "@e-mc/types": "0.8.28",
23
+ "@e-mc/module": "0.8.29",
24
+ "@e-mc/types": "0.8.29",
25
25
  "picomatch": "^3.0.1"
26
26
  }
27
27
  }