@e-mc/core 0.7.0 → 0.8.0

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 (2) hide show
  1. package/index.js +137 -132
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -50,20 +50,6 @@ let PICOMATCH_OPTIONS = { nocase: PLATFORM_WIN32 };
50
50
  let MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32 };
51
51
  let MINIMATCH;
52
52
  let PERMISSION;
53
- function convertPosix(value) {
54
- if (value) {
55
- if ((0, types_1.isString)(value)) {
56
- return asPosix(value);
57
- }
58
- if (Array.isArray(value)) {
59
- const items = value.filter(item => (0, types_1.isString)(item));
60
- if (items.length) {
61
- return items.map(item => asPosix(item));
62
- }
63
- }
64
- }
65
- return '';
66
- }
67
53
  function getSettingsValue(options, name, component) {
68
54
  const result = options[name];
69
55
  return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
@@ -121,7 +107,7 @@ function matchPermission(permission, pathname) {
121
107
  return enabled === true && (0, types_1.isString)(pathname) && (value === '**/*' || value === '**' || Permission.match(pathname, value));
122
108
  }
123
109
  function setPermission(name, pathname, enabled) {
124
- if (typeof enabled === 'boolean' && (!this[kFreeze] || this[name].enabled === null) && (!enabled || (pathname = convertPosix(pathname)))) {
110
+ if (typeof enabled === 'boolean' && (!this[kFreeze] || this[name].enabled === null) && (!enabled || (pathname = Permission.toPosix(pathname)))) {
125
111
  if (enabled && Array.isArray(pathname)) {
126
112
  switch (pathname.length) {
127
113
  case 1:
@@ -232,116 +218,116 @@ function updateHostQueue(host, position, priority, broadcast) {
232
218
  }
233
219
  function asPosix(value) {
234
220
  value = path.normalize(value.trim());
235
- return PLATFORM_WIN32 ? value.replace(/\\/g, '/') : value;
221
+ return PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
236
222
  }
237
223
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
238
224
  const expireTime = (value) => Date.now() + value * 1000 /* TIME.S */;
239
225
  const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000 /* TIME.S */) || 0;
240
226
  class Host extends module_1.default {
241
- static purgeMemory(percent = 1, limit = 0, parent) {
242
- return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
227
+ static async purgeMemory(percent = 1, limit = 0, parent) {
228
+ return parent ? super.purgeMemory(percent, limit) : 0;
243
229
  }
244
230
  static loadSettings(settings, permission, password) {
245
231
  if (typeof permission === 'string') {
246
232
  password = permission;
247
233
  permission = undefined;
248
234
  }
249
- if (super.loadSettings(settings, password)) {
250
- const { permission: perm, process: proc } = settings;
251
- if (perm) {
252
- if (permission && Permission.validate(perm)) {
253
- const { disk_read, disk_write, unc_read, unc_write } = perm;
254
- PERMISSION = new Permission(true);
255
- if (permission.disk_read) {
256
- PERMISSION.setDiskRead(disk_read);
257
- }
258
- if (permission.disk_write) {
259
- PERMISSION.setDiskWrite(disk_write);
260
- }
261
- if (permission.unc_read) {
262
- PERMISSION.setUNCRead(unc_read);
263
- }
264
- if (permission.unc_write) {
265
- PERMISSION.setUNCWrite(unc_write);
266
- }
235
+ if (!super.loadSettings(settings, password)) {
236
+ return false;
237
+ }
238
+ const { permission: perm, process: proc } = settings;
239
+ if (perm) {
240
+ if (permission && Permission.validate(perm)) {
241
+ const { disk_read, disk_write, unc_read, unc_write } = perm;
242
+ PERMISSION = new Permission(true);
243
+ if (permission.disk_read) {
244
+ PERMISSION.setDiskRead(disk_read);
245
+ }
246
+ if (permission.disk_write) {
247
+ PERMISSION.setDiskWrite(disk_write);
248
+ }
249
+ if (permission.unc_read) {
250
+ PERMISSION.setUNCRead(unc_read);
251
+ }
252
+ if (permission.unc_write) {
253
+ PERMISSION.setUNCWrite(unc_write);
267
254
  }
268
- if ((0, types_1.isObject)(perm.settings)) {
269
- const { picomatch: pico, minimatch: mini } = perm.settings;
270
- if ((0, types_1.isPlainObject)(pico)) {
271
- PICOMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...pico };
255
+ }
256
+ if ((0, types_1.isObject)(perm.settings)) {
257
+ const { picomatch: pico, minimatch: mini } = perm.settings;
258
+ if ((0, types_1.isPlainObject)(pico)) {
259
+ PICOMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...pico };
260
+ }
261
+ if ((0, types_1.isPlainObject)(mini)) {
262
+ try {
263
+ const pkg = require('minimatch');
264
+ MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
265
+ MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
272
266
  }
273
- if ((0, types_1.isPlainObject)(mini)) {
274
- try {
275
- const pkg = require('minimatch');
276
- MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
277
- MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
278
- }
279
- catch {
280
- }
267
+ catch {
281
268
  }
282
269
  }
283
270
  }
284
- if (proc && (0, types_1.isObject)(proc.thread)) {
285
- let { limit, expires, queue, admin } = proc.thread;
286
- if (limit && (limit = Math.floor(+limit)) > 0) {
287
- HOST.THREAD_LIMIT = limit;
271
+ }
272
+ if (proc && (0, types_1.isObject)(proc.thread)) {
273
+ let { limit, expires, queue, admin } = proc.thread;
274
+ if (limit && (limit = Math.floor(+limit)) > 0) {
275
+ HOST.THREAD_LIMIT = limit;
276
+ }
277
+ if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
278
+ HOST.THREAD_EXPIRES = expires;
279
+ }
280
+ if ((0, types_1.isObject)(queue)) {
281
+ let priority;
282
+ ({ limit, expires, priority } = queue);
283
+ if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
284
+ HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
288
285
  }
289
286
  if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
290
- HOST.THREAD_EXPIRES = expires;
287
+ HOST.QUEUE_EXPIRES = expires;
291
288
  }
292
- if ((0, types_1.isObject)(queue)) {
293
- let priority;
294
- ({ limit, expires, priority } = queue);
295
- if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
296
- HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
289
+ if ((0, types_1.isObject)(priority)) {
290
+ let { min = -1, max = 0, bypass } = priority;
291
+ if ((min = Math.floor(+min)) >= 0) {
292
+ HOST.PRIORITY_MIN = min;
297
293
  }
298
- if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
299
- HOST.QUEUE_EXPIRES = expires;
294
+ if ((max = Math.floor(+max)) > 0) {
295
+ HOST.PRIORITY_MAX = max;
300
296
  }
301
- if ((0, types_1.isObject)(priority)) {
302
- let { min = -1, max = 0, bypass } = priority;
303
- if ((min = Math.floor(+min)) >= 0) {
304
- HOST.PRIORITY_MIN = min;
305
- }
306
- if ((max = Math.floor(+max)) > 0) {
307
- HOST.PRIORITY_MAX = max;
308
- }
309
- if (HOST.PRIORITY_MAX < HOST.PRIORITY_MIN) {
310
- HOST.PRIORITY_MAX = HOST.PRIORITY_MIN + 1;
311
- }
312
- if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
313
- HOST.PRIORITY_BYPASS = bypass;
314
- }
315
- else {
316
- bypass = HOST.PRIORITY_BYPASS;
317
- }
318
- if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
319
- HOST.PRIORITY_BYPASS = undefined;
320
- }
297
+ if (HOST.PRIORITY_MAX < HOST.PRIORITY_MIN) {
298
+ HOST.PRIORITY_MAX = HOST.PRIORITY_MIN + 1;
299
+ }
300
+ if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
301
+ HOST.PRIORITY_BYPASS = bypass;
302
+ }
303
+ else {
304
+ bypass = HOST.PRIORITY_BYPASS;
305
+ }
306
+ if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
307
+ HOST.PRIORITY_BYPASS = undefined;
321
308
  }
322
309
  }
323
- if (admin) {
324
- const cipher = proc.cipher;
325
- if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
326
- const algorithm = cipher.algorithm || 'aes-256-gcm';
327
- HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
328
- if (HOST.ADMIN_USERS.length) {
329
- HOST.CIPHER_ALGORITHM = algorithm;
330
- }
331
- else {
332
- HOST.CIPHER_ALGORITHM = null;
333
- HOST.CIPHER_KEY = null;
334
- HOST.CIPHER_IV = null;
335
- }
310
+ }
311
+ if (admin) {
312
+ const cipher = proc.cipher;
313
+ if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
314
+ const algorithm = cipher.algorithm || 'aes-256-gcm';
315
+ HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
316
+ if (HOST.ADMIN_USERS.length) {
317
+ HOST.CIPHER_ALGORITHM = algorithm;
336
318
  }
337
- if (typeof admin.private === 'boolean') {
338
- HOST.ADMIN_PRIVATE = admin.private;
319
+ else {
320
+ HOST.CIPHER_ALGORITHM = null;
321
+ HOST.CIPHER_KEY = null;
322
+ HOST.CIPHER_IV = null;
339
323
  }
340
324
  }
325
+ if (typeof admin.private === 'boolean') {
326
+ HOST.ADMIN_PRIVATE = admin.private;
327
+ }
341
328
  }
342
- return true;
343
329
  }
344
- return false;
330
+ return true;
345
331
  }
346
332
  static createPermission(all, freeze) {
347
333
  const permission = new Permission(freeze);
@@ -667,8 +653,8 @@ class Host extends module_1.default {
667
653
  exports.Host = Host;
668
654
  _a = kDone, _b = kQueued, _c = kUsername;
669
655
  class Client extends module_1.default {
670
- static purgeMemory(percent = 1, limit = 0, parent) {
671
- return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
656
+ static async purgeMemory(percent = 1, limit = 0, parent) {
657
+ return parent ? super.purgeMemory(percent, limit) : 0;
672
658
  }
673
659
  constructor(data) {
674
660
  super();
@@ -693,6 +679,11 @@ class Client extends module_1.default {
693
679
  }
694
680
  return this;
695
681
  }
682
+ getUserSettings() {
683
+ const username = this.host?.username;
684
+ let result;
685
+ return username && (0, types_1.isPlainObject)(result = this.settings.users?.[username]) ? result : null;
686
+ }
696
687
  set cacheDir(value) {
697
688
  if (path.isAbsolute(value) && module_1.default.isDir(value)) {
698
689
  this[kCacheDir] = value;
@@ -725,43 +716,43 @@ class ClientDb extends Client {
725
716
  static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
726
717
  static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
727
718
  static loadSettings(settings, password) {
728
- if (super.loadSettings({ process: settings.process }, password)) {
729
- const memory = settings.memory;
730
- if ((0, types_1.isPlainObject)(memory)) {
731
- for (const name in memory) {
732
- const client = memory[name];
733
- if ((0, types_1.isPlainObject)(client)) {
734
- for (const source in client) {
735
- const item = client[source];
736
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
737
- const key = name + '_' + source;
738
- const stored = CACHE_SOURCE[key];
739
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
740
- if (enabled && limit > 0) {
741
- if ((0, types_1.isString)(percent)) {
742
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
743
- }
744
- else if ((percent = Math.max(percent, 0)) > 1) {
745
- percent /= 100;
746
- }
747
- if (percent > 0) {
748
- (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 };
749
- }
719
+ if (!super.loadSettings({ process: settings.process }, password)) {
720
+ return false;
721
+ }
722
+ const memory = settings.memory;
723
+ if ((0, types_1.isPlainObject)(memory)) {
724
+ for (const name in memory) {
725
+ const client = memory[name];
726
+ if ((0, types_1.isPlainObject)(client)) {
727
+ for (const source in client) {
728
+ const item = client[source];
729
+ if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
730
+ const key = name + '_' + source;
731
+ const stored = CACHE_SOURCE[key];
732
+ let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
733
+ if (enabled && limit > 0) {
734
+ if ((0, types_1.isString)(percent)) {
735
+ percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
750
736
  }
751
- else if (stored) {
752
- delete stored.config;
737
+ else if ((percent = Math.max(percent, 0)) > 1) {
738
+ percent /= 100;
753
739
  }
740
+ if (percent > 0) {
741
+ (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 };
742
+ }
743
+ }
744
+ else if (stored) {
745
+ delete stored.config;
754
746
  }
755
747
  }
756
748
  }
757
749
  }
758
750
  }
759
- return true;
760
751
  }
761
- return false;
752
+ return true;
762
753
  }
763
- static purgeMemory(percent = 1, limit = 0, parent) {
764
- return parent ? super.purgeMemory(percent, limit, parent) : Promise.resolve(0);
754
+ static async purgeMemory(percent = 1, limit = 0, parent) {
755
+ return parent ? super.purgeMemory(percent, limit, parent) : 0;
765
756
  }
766
757
  static getTimeout(value) {
767
758
  if (value === undefined) {
@@ -912,11 +903,11 @@ class ClientDb extends Client {
912
903
  }
913
904
  return result;
914
905
  }
915
- static purgeResult(prefix) {
906
+ static async purgeResult(prefix) {
916
907
  const current = Date.now();
917
908
  const providers = new Map();
918
909
  if (prefix) {
919
- if (prefix.indexOf('_') === -1) {
910
+ if (!prefix.includes('_')) {
920
911
  prefix += '_';
921
912
  for (const key in CACHE_USER) {
922
913
  if (key.startsWith(prefix)) {
@@ -956,7 +947,7 @@ class ClientDb extends Client {
956
947
  }
957
948
  }
958
949
  }
959
- return Promise.resolve(result);
950
+ return result;
960
951
  }
961
952
  static setPoolConfig(value) { }
962
953
  static getPoolConfig(source) { }
@@ -1167,9 +1158,9 @@ class AbortComponent {
1167
1158
  map.splice(index, 1);
1168
1159
  }
1169
1160
  }
1170
- abort() {
1161
+ abort(reason) {
1171
1162
  if (!this.aborted) {
1172
- this[kAbortHandler].abort();
1163
+ this[kAbortHandler].abort(reason);
1173
1164
  }
1174
1165
  }
1175
1166
  reset() {
@@ -1259,6 +1250,20 @@ class Permission {
1259
1250
  }
1260
1251
  return pm.isMatch(pathname, pattern, PICOMATCH_OPTIONS);
1261
1252
  }
1253
+ static toPosix(value) {
1254
+ if (value) {
1255
+ if ((0, types_1.isString)(value)) {
1256
+ return asPosix(value);
1257
+ }
1258
+ if (Array.isArray(value)) {
1259
+ const items = value.filter(item => (0, types_1.isString)(item));
1260
+ if (items.length) {
1261
+ return items.map(item => asPosix(item));
1262
+ }
1263
+ }
1264
+ }
1265
+ return '';
1266
+ }
1262
1267
  constructor(freeze = false) {
1263
1268
  this[_f] = { enabled: null, value: '' };
1264
1269
  this[_g] = { enabled: null, value: '' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
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": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.7.0",
24
- "@e-mc/types": "0.7.0",
23
+ "@e-mc/module": "0.8.0",
24
+ "@e-mc/types": "0.8.0",
25
25
  "picomatch": "^3.0.1"
26
26
  }
27
27
  }