@e-mc/core 0.8.27 → 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 +9 -9
  2. package/index.js +124 -95
  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.27/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";
@@ -61,7 +61,7 @@ interface HostConstructor extends ModuleConstructor {
61
61
  getThreadCount(full: true): ThreadCountStat;
62
62
  getThreadCount(username: string, iv?: BinaryLike): ThreadCountStat;
63
63
  getThreadCount(username?: string | boolean, iv?: BinaryLike): number;
64
- getPermissionFromSettings(): IPermission;
64
+ getPermissionFromSettings(freeze?: boolean): IPermission;
65
65
  readonly prototype: IHost;
66
66
  new(config?: HostInitConfig): IHost;
67
67
  }
@@ -165,13 +165,13 @@ interface IPermission {
165
165
 
166
166
  ## References
167
167
 
168
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/squared.d.ts
169
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/core.d.ts
170
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/db.d.ts
171
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/dom.d.ts
172
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/logger.d.ts
173
- - https://www.unpkg.com/@e-mc/types@0.8.27/lib/node.d.ts
174
- - https://www.unpkg.com/@e-mc/types@0.8.27/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
  }
@@ -218,7 +236,7 @@ function updateHostQueue(host, position, priority, broadcast) {
218
236
  }
219
237
  function isInvalidRange(result, range) {
220
238
  const [lower, upper = 0] = range;
221
- return result.length <= lower || upper > 0 && result.length > upper;
239
+ return Array.isArray(result) && (result.length <= lower || upper > 0 && result.length > upper);
222
240
  }
223
241
  function asPosix(value) {
224
242
  if (PLATFORM_WIN32) {
@@ -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) {
@@ -259,6 +277,7 @@ class Host extends module_1.default {
259
277
  if (permission.unc_write) {
260
278
  PERMISSION.setUNCWrite(unc_write);
261
279
  }
280
+ Object.freeze(PERMISSION);
262
281
  }
263
282
  if ((0, types_1.isObject)(perm.settings)) {
264
283
  const { picomatch: pico, minimatch: mini } = perm.settings;
@@ -382,21 +401,7 @@ class Host extends module_1.default {
382
401
  if (HOST.ADMIN_PRIVATE && !username) {
383
402
  return full === true ? { count: -1 } : -1;
384
403
  }
385
- const current = Date.now();
386
- const items = [];
387
- for (const [host, pid] of HOST.DONE) {
388
- if (host.done) {
389
- closeThread(host);
390
- }
391
- else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
392
- host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
393
- host.abort();
394
- closeThread(host);
395
- }
396
- else {
397
- items.push([host, pid]);
398
- }
399
- }
404
+ const items = killThread();
400
405
  const count = items.length;
401
406
  if (full === true) {
402
407
  const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
@@ -430,8 +435,8 @@ class Host extends module_1.default {
430
435
  }
431
436
  return count;
432
437
  }
433
- static getPermissionFromSettings() {
434
- return PERMISSION;
438
+ static getPermissionFromSettings(freeze = true) {
439
+ return freeze ? PERMISSION : Permission.clone(PERMISSION);
435
440
  }
436
441
  constructor(config = {}) {
437
442
  super();
@@ -473,6 +478,9 @@ class Host extends module_1.default {
473
478
  }
474
479
  }
475
480
  this[kConfig] = Object.freeze(config);
481
+ if (HOST.DONE.size >= HOST.THREAD_LIMIT) {
482
+ killThread();
483
+ }
476
484
  if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
477
485
  HOST.DONE.set(this, HOST.PID++);
478
486
  }
@@ -731,27 +739,29 @@ class ClientDb extends Client {
731
739
  const memory = settings.memory;
732
740
  if ((0, types_1.isPlainObject)(memory)) {
733
741
  for (const name in memory) {
734
- const client = memory[name];
735
- if ((0, types_1.isPlainObject)(client)) {
736
- for (const source in client) {
737
- const item = client[source];
738
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
739
- const key = name + '_' + source;
740
- const stored = CACHE_SOURCE[key];
741
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
742
- if (enabled && limit > 0) {
743
- if ((0, types_1.isString)(percent)) {
744
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
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
+ }
745
761
  }
746
- else if ((percent = Math.max(percent, 0)) > 1) {
747
- percent /= 100;
762
+ else if (stored) {
763
+ delete stored.config;
748
764
  }
749
- if (percent > 0) {
750
- (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 };
751
- }
752
- }
753
- else if (stored) {
754
- delete stored.config;
755
765
  }
756
766
  }
757
767
  }
@@ -764,7 +774,7 @@ class ClientDb extends Client {
764
774
  return parent ? super.purgeMemory(percent, limit, parent) : 0;
765
775
  }
766
776
  static getTimeout(value) {
767
- if (value === undefined) {
777
+ if (value == null) {
768
778
  return 0;
769
779
  }
770
780
  let result = 0;
@@ -802,17 +812,19 @@ class ClientDb extends Client {
802
812
  if (!userKey) {
803
813
  return;
804
814
  }
805
- queryString = this.asHash(queryString);
806
- if (timeout > 0) {
815
+ if (timeout > 0 && timeout < Infinity) {
807
816
  const userCache = (_j = CACHE_USER[source]) === null || _j === void 0 ? void 0 : _j[userKey];
808
- let stored;
809
- if (!(userCache && (stored = userCache[queryString]))) {
817
+ if (!userCache) {
818
+ return;
819
+ }
820
+ const stored = userCache[queryString = this.asHash(queryString)];
821
+ if (!stored) {
810
822
  return;
811
823
  }
812
824
  const [expires, result] = stored;
813
825
  if (result) {
814
826
  const current = Date.now();
815
- let valid;
827
+ let valid = false;
816
828
  if (renewCache) {
817
829
  stored[0] = expireTime(timeout);
818
830
  valid = true;
@@ -838,36 +850,28 @@ class ClientDb extends Client {
838
850
  delete userCache[queryString];
839
851
  }
840
852
  else if (sessionKey) {
841
- 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)];
842
854
  }
843
855
  }
844
- static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
845
- var _j, _k;
846
- let cache, cacheDir;
856
+ static storeResult(source, credential, queryString, result, cache, sessionKey, sessionExpires) {
857
+ var _j;
858
+ if (!credential || !Array.isArray(result)) {
859
+ return result;
860
+ }
861
+ let cacheDir;
847
862
  if ((0, types_1.isObject)(sessionKey)) {
848
863
  ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
849
864
  }
850
- else if ((0, types_1.isObject)(options) && 'cache' in options) {
851
- ({ cache, cacheDir, sessionKey, sessionExpires } = options);
865
+ else if ((0, types_1.isObject)(cache) && 'cache' in cache) {
866
+ ({ cache, cacheDir, sessionKey, sessionExpires } = cache);
852
867
  }
853
868
  let timeout = 0, userKey, whenEmpty, partition;
854
- if (cache) {
855
- switch (typeof cache) {
856
- case 'number':
857
- timeout = cache;
858
- break;
859
- case 'string':
860
- if (typeof sessionKey === 'number') {
861
- sessionExpires = sessionKey;
862
- sessionKey = cache;
863
- break;
864
- }
865
- timeout = convertSeconds(cache);
866
- break;
867
- case 'object': {
869
+ switch (typeof cache) {
870
+ case 'object':
871
+ if (cache) {
868
872
  let seconds, dir;
869
873
  ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
870
- if ((0, types_1.isString)(dir) && (!cacheDir || module_1.default.isDir(dir))) {
874
+ if (dir && (!cacheDir || module_1.default.isDir(dir))) {
871
875
  cacheDir = dir;
872
876
  partition = true;
873
877
  }
@@ -875,15 +879,25 @@ class ClientDb extends Client {
875
879
  timeout = seconds;
876
880
  }
877
881
  else {
878
- cache.timeout = seconds ? convertSeconds(seconds) : 0;
882
+ cache.timeout = seconds ? timeout = convertSeconds(seconds) : 0;
879
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;
880
893
  break;
881
894
  }
882
- }
895
+ timeout = convertSeconds(cache);
896
+ break;
883
897
  }
884
898
  if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) || this.asHash(this.asString(credential)))) {
885
- queryString = this.asHash(queryString);
886
- if (timeout > 0) {
899
+ if (timeout > 0 && timeout < Infinity) {
900
+ queryString = this.asHash(queryString);
887
901
  const item = [expireTime(timeout), result, Date.now(), 0];
888
902
  ((_j = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_j[userKey] = {}))[queryString] = item;
889
903
  addSourceResult(source, item);
@@ -907,8 +921,14 @@ class ClientDb extends Client {
907
921
  }
908
922
  else if (typeof sessionKey === 'string' && sessionExpires && sessionExpires > 0) {
909
923
  const dbKey = userKey + sessionKey;
910
- ((_k = (CACHE_SESSION[source] || (CACHE_SESSION[source] = {})))[dbKey] || (_k[dbKey] = {}))[queryString] = result;
911
- setTimeout(() => delete CACHE_SESSION[dbKey], sessionExpires * 1000);
924
+ const sourceData = CACHE_SESSION[source] || (CACHE_SESSION[source] = {});
925
+ let data = sourceData[dbKey];
926
+ if (!data) {
927
+ data = {};
928
+ sourceData[dbKey] = data;
929
+ setTimeout(() => delete sourceData[dbKey], sessionExpires * 1000);
930
+ }
931
+ data[this.asHash(queryString)] = result;
912
932
  }
913
933
  }
914
934
  return result;
@@ -1049,15 +1069,15 @@ class ClientDb extends Client {
1049
1069
  sessionKey = options;
1050
1070
  break;
1051
1071
  case 'object':
1052
- if (options !== null) {
1053
- ({ value, sessionKey, renewCache, exclusiveOf } = options);
1072
+ if (options) {
1073
+ ({ value, sessionKey, exclusiveOf, renewCache } = options);
1054
1074
  if (Array.isArray(exclusiveOf)) {
1055
1075
  const ignoreCache = exclusiveOf[2];
1056
1076
  if (ignoreCache === 1) {
1057
1077
  return;
1058
1078
  }
1059
1079
  renewCache || (renewCache = ignoreCache === 0);
1060
- queryString += '_' + exclusiveOf.toString();
1080
+ queryString += '_' + exclusiveOf.slice(0, 2).toString();
1061
1081
  }
1062
1082
  }
1063
1083
  break;
@@ -1070,9 +1090,10 @@ class ClientDb extends Client {
1070
1090
  }
1071
1091
  }
1072
1092
  const result = ClientDb.findResult(this.moduleName + '_' + source, uuidKey || credential, queryString, value === 0 ? 0 : ClientDb.getTimeout(value || this.settingsOf(source, 'cache')), sessionKey, renewCache);
1073
- if (!exclusiveOf || !isInvalidRange(result, exclusiveOf)) {
1074
- return result;
1093
+ if (exclusiveOf && isInvalidRange(result, exclusiveOf)) {
1094
+ return;
1075
1095
  }
1096
+ return result;
1076
1097
  }
1077
1098
  setQueryResult(source, credential, queryString, result, sessionKey) {
1078
1099
  var _j;
@@ -1083,30 +1104,37 @@ class ClientDb extends Client {
1083
1104
  return result;
1084
1105
  }
1085
1106
  const uuidKey = ClientDb.extractUUID(credential);
1086
- let options;
1087
- if (ClientDb.enabled("memory.settings.users", (_j = this.host) === null || _j === void 0 ? void 0 : _j.username)) {
1088
- let value, exclusiveOf;
1089
- if ((0, types_1.isObject)(sessionKey)) {
1090
- ({ 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;
1091
1114
  }
1092
- if (Array.isArray(exclusiveOf)) {
1093
- if (isInvalidRange(result, exclusiveOf)) {
1094
- return result;
1095
- }
1096
- 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;
1097
1121
  }
1098
- if (uuidKey) {
1099
- const cache = this.settingsKey(uuidKey, 'cache');
1100
- if (cache !== undefined) {
1101
- value = cache;
1102
- }
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 };
1103
1129
  }
1104
- options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1105
1130
  }
1106
- else {
1107
- 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;
1108
1136
  }
1109
- 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;
1110
1138
  }
1111
1139
  applyState(items, value, as) {
1112
1140
  for (const item of Array.isArray(items) ? items : [items]) {
@@ -1351,3 +1379,4 @@ class Permission {
1351
1379
  exports.Permission = Permission;
1352
1380
  _e = kDiskRead, _f = kDiskWrite, _g = kUncRead, _h = kUncWrite;
1353
1381
  PERMISSION = Host.createPermission(true, true);
1382
+ Object.freeze(PERMISSION);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.8.27",
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.27",
24
- "@e-mc/types": "0.8.27",
23
+ "@e-mc/module": "0.8.29",
24
+ "@e-mc/types": "0.8.29",
25
25
  "picomatch": "^3.0.1"
26
26
  }
27
27
  }