@e-mc/core 0.6.0 → 0.6.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/LICENSE CHANGED
@@ -1,11 +1,7 @@
1
- Copyright 2023 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
11
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ Copyright 2023 Studio Trigger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
- ### @e-mc/core
1
+ # @e-mc/core
2
2
 
3
- ### LICENSE
3
+ PEP 402 - Forever Ryuko
4
4
 
5
- BSD 3-Clause
5
+ ## LICENSE
6
+
7
+ MIT
package/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- import type { HostConstructor, ModuleConstructor } from '../types/lib';
2
- import type { AbortComponentConstructor, ClientConstructor, ClientDbConstructor, PermissionConstructor } from '../types/lib/core';
3
-
4
- declare namespace core {
5
- const Module: ModuleConstructor;
6
- const Host: HostConstructor;
7
- const Client: ClientConstructor;
8
- const ClientDb: ClientDbConstructor;
9
- const AbortComponent: AbortComponentConstructor;
10
- const Permission: PermissionConstructor;
11
- }
12
-
1
+ import type { HostConstructor, ModuleConstructor } from '../types/lib';
2
+ import type { AbortComponentConstructor, ClientConstructor, ClientDbConstructor, PermissionConstructor } from '../types/lib/core';
3
+
4
+ declare namespace core {
5
+ const Module: ModuleConstructor;
6
+ const Host: HostConstructor;
7
+ const Client: ClientConstructor;
8
+ const ClientDb: ClientDbConstructor;
9
+ const AbortComponent: AbortComponentConstructor;
10
+ const Permission: PermissionConstructor;
11
+ }
12
+
13
13
  export = core;
package/index.js CHANGED
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2
+ var _a, _b, _c, _d, _e, _f, _g, _h;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Module = exports.Permission = exports.AbortComponent = exports.ClientDb = exports.Client = exports.Host = void 0;
5
5
  const path = require("path");
6
6
  const fs = require("fs");
7
7
  const pm = require("picomatch");
8
- const types_1 = require("../types");
9
- const module_1 = require("../module");
8
+ const types_1 = require("@e-mc/types");
9
+ const module_1 = require("@e-mc/module");
10
10
  exports.Module = module_1.default;
11
11
  const kConfig = Symbol('config');
12
12
  const kQueued = Symbol('queued');
13
- const kUsername = Symbol('username');
14
13
  const kAbortHandler = Symbol('abortHandler');
15
14
  const kFreeze = Symbol('freeze');
16
15
  const kDone = Symbol('done');
@@ -27,7 +26,7 @@ const HOST = {
27
26
  DONE: new Map(),
28
27
  QUEUE: [],
29
28
  QUEUE_LIMIT: Infinity,
30
- QUEUE_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
29
+ QUEUE_EXPIRES: 86400000,
31
30
  PID: 1,
32
31
  CLOSED: 0,
33
32
  QUEUED: 0,
@@ -39,12 +38,13 @@ const HOST = {
39
38
  ADMIN_USERS: [],
40
39
  ADMIN_PRIVATE: false,
41
40
  THREAD_LIMIT: Infinity,
42
- THREAD_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
41
+ THREAD_EXPIRES: 86400000,
43
42
  PRIORITY_MIN: 0,
44
43
  PRIORITY_MAX: 100,
45
44
  PRIORITY_BYPASS: undefined
46
45
  };
47
46
  const PLATFORM_WIN32 = process.platform === 'win32';
47
+ const HOST_USERNAME = new WeakMap();
48
48
  let STORE_RESULT_COUNT = 0;
49
49
  let PICOMATCH_OPTIONS = { nocase: PLATFORM_WIN32 };
50
50
  let MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32 };
@@ -92,7 +92,7 @@ function callResumeThread(item, aborted) {
92
92
  if (item.joined) {
93
93
  const { instance, args = [], startTime } = item;
94
94
  if (!aborted) {
95
- instance.writeTimeElapsed('JOIN', ['Thread restarting...', HOST.THREAD_LIMIT > 1 ? 'Availability: ' + HOST.DONE.size + ' / ' + HOST.THREAD_LIMIT : ''], startTime, { type: 2 /* LOG_TYPE.NODE */, ...module_1.default.LOG_STYLE_WARN });
95
+ instance.writeTimeElapsed('JOIN', ['Thread restarting...', HOST.THREAD_LIMIT > 1 ? 'Availability: ' + HOST.DONE.size + ' / ' + HOST.THREAD_LIMIT : ''], startTime, { type: 2, ...module_1.default.LOG_STYLE_WARN });
96
96
  }
97
97
  queueMicrotask(() => {
98
98
  try {
@@ -104,7 +104,7 @@ function callResumeThread(item, aborted) {
104
104
  }
105
105
  }
106
106
  function cancelThread(host, hint, rejected) {
107
- host.writeFail(["Transaction was cancelled" /* ERR_MESSAGE.CANCELLED */, host.username], (0, types_1.errorValue)("Thread was killed" /* ERR_MESSAGE.THREAD_KILLED */, hint), { type: 2 /* LOG_TYPE.NODE */, fatal: true });
107
+ host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Thread was killed", hint), { type: 2, fatal: true });
108
108
  host.abort();
109
109
  if (rejected) {
110
110
  ++HOST.REJECTED;
@@ -227,7 +227,7 @@ function resumeHostQueue(host) {
227
227
  }
228
228
  function updateHostQueue(host, position, priority, broadcast) {
229
229
  if (!broadcast || host.broadcastId) {
230
- 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 });
230
+ host.formatMessage(2, 'QUEUE', ["Thread limit exceeded" + ` (position #${position + 1})`, host.username], 'Priority: ' + priority, { type: 2, ...module_1.default.LOG_STYLE_WARN });
231
231
  }
232
232
  }
233
233
  function asPosix(value) {
@@ -235,8 +235,8 @@ function asPosix(value) {
235
235
  return PLATFORM_WIN32 ? value.replace(/\\/g, '/') : value;
236
236
  }
237
237
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
238
- const expireTime = (value) => Date.now() + value * 1000 /* TIME.S */;
239
- const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000 /* TIME.S */) || 0;
238
+ const expireTime = (value) => Date.now() + value * 1000;
239
+ const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000) || 0;
240
240
  class Host extends module_1.default {
241
241
  static purgeMemory(percent = 1, limit = 0, parent) {
242
242
  return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
@@ -396,7 +396,7 @@ class Host extends module_1.default {
396
396
  closeThread(host);
397
397
  }
398
398
  else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
399
- host.writeFail(["Transaction was cancelled" /* ERR_MESSAGE.CANCELLED */, host.username], (0, types_1.errorValue)("Timeout was exceeded" /* ERR_MESSAGE.TIMEOUT */, (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
399
+ host.writeFail(["Transaction was cancelled", host.username], (0, types_1.errorValue)("Timeout was exceeded", (0, types_1.formatTime)(HOST.THREAD_EXPIRES)), { fatal: true });
400
400
  host.abort();
401
401
  closeThread(host);
402
402
  }
@@ -452,10 +452,9 @@ class Host extends module_1.default {
452
452
  this._usingObjects = new Set();
453
453
  this[_a] = false;
454
454
  this[_b] = false;
455
- this[_c] = '';
456
455
  const { broadcastId, log, username, priority } = config;
457
456
  if ((0, types_1.isString)(username)) {
458
- this[kUsername] = username;
457
+ HOST_USERNAME.set(this, username);
459
458
  }
460
459
  if (broadcastId) {
461
460
  this.broadcastId = broadcastId;
@@ -646,7 +645,7 @@ class Host extends module_1.default {
646
645
  return this[kConfig];
647
646
  }
648
647
  get username() {
649
- return this[kUsername];
648
+ return HOST_USERNAME.get(this) || '';
650
649
  }
651
650
  set done(value) {
652
651
  if (value) {
@@ -665,7 +664,7 @@ class Host extends module_1.default {
665
664
  }
666
665
  }
667
666
  exports.Host = Host;
668
- _a = kDone, _b = kQueued, _c = kUsername;
667
+ _a = kDone, _b = kQueued;
669
668
  class Client extends module_1.default {
670
669
  static purgeMemory(percent = 1, limit = 0, parent) {
671
670
  return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
@@ -673,7 +672,7 @@ class Client extends module_1.default {
673
672
  constructor(data) {
674
673
  super();
675
674
  this._extensions = null;
676
- this[_d] = '';
675
+ this[_c] = '';
677
676
  this.module = (0, types_1.isPlainObject)(data) ? data : {};
678
677
  }
679
678
  init(...args) {
@@ -702,8 +701,8 @@ class Client extends module_1.default {
702
701
  return this[kCacheDir];
703
702
  }
704
703
  get settings() {
705
- var _k;
706
- return ((_k = this.module).settings || (_k.settings = {}));
704
+ var _j;
705
+ return ((_j = this.module).settings || (_j.settings = {}));
707
706
  }
708
707
  set extensions(values) {
709
708
  if (Array.isArray(values)) {
@@ -716,14 +715,14 @@ class Client extends module_1.default {
716
715
  }
717
716
  }
718
717
  exports.Client = Client;
719
- _d = kCacheDir;
718
+ _c = kCacheDir;
720
719
  class ClientDb extends Client {
721
- static get TRANSACTION_ACTIVE() { return 1 /* DB_TRANSACTION.ACTIVE */; }
722
- static get TRANSACTION_PARTIAL() { return 2 /* DB_TRANSACTION.PARTIAL */; }
723
- static get TRANSACTION_COMMIT() { return 4 /* DB_TRANSACTION.COMMIT */; }
724
- static get TRANSACTION_TERMINATE() { return 8 /* DB_TRANSACTION.TERMINATE */; }
725
- static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
726
- static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
720
+ static get TRANSACTION_ACTIVE() { return 1; }
721
+ static get TRANSACTION_PARTIAL() { return 2; }
722
+ static get TRANSACTION_COMMIT() { return 4; }
723
+ static get TRANSACTION_TERMINATE() { return 8; }
724
+ static get TRANSACTION_ABORT() { return 16; }
725
+ static get TRANSACTION_FAIL() { return 32; }
727
726
  static loadSettings(settings, password) {
728
727
  if (super.loadSettings({ process: settings.process }, password)) {
729
728
  const memory = settings.memory;
@@ -788,7 +787,7 @@ class ClientDb extends Client {
788
787
  let result = 0;
789
788
  switch (typeof value) {
790
789
  case 'number':
791
- result = value * 1000 /* TIME.S */;
790
+ result = value * 1000;
792
791
  break;
793
792
  case 'string':
794
793
  result = (0, types_1.parseTime)(value);
@@ -841,7 +840,7 @@ class ClientDb extends Client {
841
840
  }
842
841
  }
843
842
  static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
844
- var _k, _l;
843
+ var _j, _k;
845
844
  let cache, cacheDir;
846
845
  if ((0, types_1.isObject)(sessionKey)) {
847
846
  ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
@@ -884,7 +883,7 @@ class ClientDb extends Client {
884
883
  queryString = this.asHash(queryString);
885
884
  if (timeout > 0) {
886
885
  const item = [expireTime(timeout), result, Date.now(), 0];
887
- ((_k = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_k[userKey] = {}))[queryString] = item;
886
+ ((_j = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_j[userKey] = {}))[queryString] = item;
888
887
  addSourceResult(source, item);
889
888
  if (cacheDir) {
890
889
  if (partition) {
@@ -906,17 +905,17 @@ class ClientDb extends Client {
906
905
  }
907
906
  else if (typeof sessionKey === 'string' && sessionExpires && sessionExpires > 0) {
908
907
  const dbKey = userKey + sessionKey;
909
- ((_l = (CACHE_SESSION[source] || (CACHE_SESSION[source] = {})))[dbKey] || (_l[dbKey] = {}))[queryString] = result;
910
- setTimeout(() => delete CACHE_SESSION[dbKey], sessionExpires * 1000 /* TIME.S */);
908
+ ((_k = (CACHE_SESSION[source] || (CACHE_SESSION[source] = {})))[dbKey] || (_k[dbKey] = {}))[queryString] = result;
909
+ setTimeout(() => delete CACHE_SESSION[dbKey], sessionExpires * 1000);
911
910
  }
912
911
  }
913
912
  return result;
914
913
  }
915
- static purgeResult(prefix) {
914
+ static async purgeResult(prefix) {
916
915
  const current = Date.now();
917
916
  const providers = new Map();
918
917
  if (prefix) {
919
- if (prefix.indexOf('_') === -1) {
918
+ if (!prefix.includes('_')) {
920
919
  prefix += '_';
921
920
  for (const key in CACHE_USER) {
922
921
  if (key.startsWith(prefix)) {
@@ -977,7 +976,7 @@ class ClientDb extends Client {
977
976
  constructor(data, database = []) {
978
977
  super(data);
979
978
  this.database = database;
980
- this.cacheExpires = 600000 /* VALUES.DB_EXPIRES */;
979
+ this.cacheExpires = 600000;
981
980
  }
982
981
  init(config) {
983
982
  let { cache_dir, session_expires: expires } = this.settings;
@@ -1071,7 +1070,7 @@ class ClientDb extends Client {
1071
1070
  }
1072
1071
  const uuidKey = ClientDb.extractUUID(credential);
1073
1072
  let options;
1074
- if (ClientDb.enabled("memory.settings.users" /* KEY_NAME.MEMORY_SETTINGS_USERS */, this.host?.username)) {
1073
+ if (ClientDb.enabled("memory.settings.users", this.host?.username)) {
1075
1074
  let value;
1076
1075
  if ((0, types_1.isObject)(sessionKey)) {
1077
1076
  ({ value, sessionKey } = sessionKey);
@@ -1091,13 +1090,13 @@ class ClientDb extends Client {
1091
1090
  }
1092
1091
  applyState(items, value, as) {
1093
1092
  for (const item of Array.isArray(items) ? items : [items]) {
1094
- if ((0, types_1.hasBit)(value, 8 /* DB_TRANSACTION.TERMINATE */)) {
1093
+ if ((0, types_1.hasBit)(value, 8)) {
1095
1094
  const state = item.transactionState || (item.transactionState = 0);
1096
- if ((0, types_1.hasBit)(state, 4 /* DB_TRANSACTION.COMMIT */) || (0, types_1.hasBit)(state, 32 /* DB_TRANSACTION.FAIL */)) {
1095
+ if ((0, types_1.hasBit)(state, 4) || (0, types_1.hasBit)(state, 32)) {
1097
1096
  item.transactionState |= value;
1098
1097
  }
1099
1098
  }
1100
- else if ((0, types_1.hasBit)(value, 16 /* DB_TRANSACTION.ABORT */)) {
1099
+ else if ((0, types_1.hasBit)(value, 16)) {
1101
1100
  item.transactionState || (item.transactionState = value);
1102
1101
  }
1103
1102
  else if (as) {
@@ -1121,8 +1120,8 @@ class ClientDb extends Client {
1121
1120
  }
1122
1121
  }
1123
1122
  settingsKey(uuidKey, name, component) {
1124
- var _k;
1125
- const data = ((_k = this.settings).user_key || (_k.user_key = {}))[uuidKey];
1123
+ var _j;
1124
+ const data = ((_j = this.settings).user_key || (_j.user_key = {}))[uuidKey];
1126
1125
  if ((0, types_1.isObject)(data)) {
1127
1126
  return getSettingsValue(data, name, component);
1128
1127
  }
@@ -1130,22 +1129,22 @@ class ClientDb extends Client {
1130
1129
  get pending() {
1131
1130
  return this.database.filter(item => {
1132
1131
  const state = item.transactionState || 0;
1133
- return state === 0 || state === 16 /* DB_TRANSACTION.ABORT */;
1132
+ return state === 0 || state === 16;
1134
1133
  });
1135
1134
  }
1136
1135
  get committed() {
1137
- return this.database.filter(item => (item.transactionState & 4 /* DB_TRANSACTION.COMMIT */) === 4 /* DB_TRANSACTION.COMMIT */);
1136
+ return this.database.filter(item => (item.transactionState & 4) === 4);
1138
1137
  }
1139
1138
  get failed() {
1140
- return this.database.filter(item => (item.transactionState & 32 /* DB_TRANSACTION.FAIL */) === 32 /* DB_TRANSACTION.FAIL */);
1139
+ return this.database.filter(item => (item.transactionState & 32) === 32);
1141
1140
  }
1142
1141
  }
1143
1142
  exports.ClientDb = ClientDb;
1144
- ClientDb.STORE_RESULT_PARTITION_SIZE = 16 /* CACHE_SIZE.CORE_CLIENTDB_PARTITION_SIZE */;
1145
- ClientDb.STORE_RESULT_PARTITION_MULT = 2 /* CACHE_SIZE.CORE_CLIENTDB_PARTITION_MULT */;
1143
+ ClientDb.STORE_RESULT_PARTITION_SIZE = 16;
1144
+ ClientDb.STORE_RESULT_PARTITION_MULT = 2;
1146
1145
  class AbortComponent {
1147
1146
  constructor() {
1148
- this[_e] = new AbortController();
1147
+ this[_d] = new AbortController();
1149
1148
  }
1150
1149
  static attach(instance, signal, options) {
1151
1150
  let map = ABORT_LISTENER.get(instance);
@@ -1185,7 +1184,7 @@ class AbortComponent {
1185
1184
  }
1186
1185
  }
1187
1186
  exports.AbortComponent = AbortComponent;
1188
- _e = kAbortHandler;
1187
+ _d = kAbortHandler;
1189
1188
  class Permission {
1190
1189
  static create(settings, parent, freeze) {
1191
1190
  if (this.validate(settings)) {
@@ -1260,10 +1259,10 @@ class Permission {
1260
1259
  return pm.isMatch(pathname, pattern, PICOMATCH_OPTIONS);
1261
1260
  }
1262
1261
  constructor(freeze = false) {
1262
+ this[_e] = { enabled: null, value: '' };
1263
1263
  this[_f] = { enabled: null, value: '' };
1264
1264
  this[_g] = { enabled: null, value: '' };
1265
1265
  this[_h] = { enabled: null, value: '' };
1266
- this[_j] = { enabled: null, value: '' };
1267
1266
  this[kFreeze] = freeze;
1268
1267
  }
1269
1268
  setDiskRead(pathname = '', enabled = true) {
@@ -1316,5 +1315,5 @@ class Permission {
1316
1315
  }
1317
1316
  }
1318
1317
  exports.Permission = Permission;
1319
- _f = kDiskRead, _g = kDiskWrite, _h = kUncRead, _j = kUncWrite;
1318
+ _e = kDiskRead, _f = kDiskWrite, _g = kUncRead, _h = kUncWrite;
1320
1319
  PERMISSION = Host.createPermission(true, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Core modules for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -17,11 +17,11 @@
17
17
  "squared-functions"
18
18
  ],
19
19
  "author": "An Pham <anpham6@gmail.com>",
20
- "license": "BSD 3-Clause",
20
+ "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.6.0",
24
- "@e-mc/types": "0.6.0",
23
+ "@e-mc/module": "0.6.1",
24
+ "@e-mc/types": "0.6.1",
25
25
  "picomatch": "^2.3.1"
26
26
  }
27
27
  }