@e-mc/core 0.7.1 → 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.
- package/LICENSE +10 -10
- package/index.d.ts +12 -12
- package/index.js +122 -117
- package/package.json +3 -3
package/LICENSE
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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
|
-
|
|
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
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.
|
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
|
@@ -218,116 +218,116 @@ function updateHostQueue(host, position, priority, broadcast) {
|
|
|
218
218
|
}
|
|
219
219
|
function asPosix(value) {
|
|
220
220
|
value = path.normalize(value.trim());
|
|
221
|
-
return PLATFORM_WIN32 ? value.replace(
|
|
221
|
+
return PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
|
|
222
222
|
}
|
|
223
223
|
const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
|
|
224
224
|
const expireTime = (value) => Date.now() + value * 1000 /* TIME.S */;
|
|
225
225
|
const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000 /* TIME.S */) || 0;
|
|
226
226
|
class Host extends module_1.default {
|
|
227
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
228
|
-
return parent ? super.purgeMemory(percent, limit) :
|
|
227
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
228
|
+
return parent ? super.purgeMemory(percent, limit) : 0;
|
|
229
229
|
}
|
|
230
230
|
static loadSettings(settings, permission, password) {
|
|
231
231
|
if (typeof permission === 'string') {
|
|
232
232
|
password = permission;
|
|
233
233
|
permission = undefined;
|
|
234
234
|
}
|
|
235
|
-
if (super.loadSettings(settings, password)) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
PERMISSION.setUNCRead(unc_read);
|
|
249
|
-
}
|
|
250
|
-
if (permission.unc_write) {
|
|
251
|
-
PERMISSION.setUNCWrite(unc_write);
|
|
252
|
-
}
|
|
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);
|
|
253
248
|
}
|
|
254
|
-
if (
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
249
|
+
if (permission.unc_read) {
|
|
250
|
+
PERMISSION.setUNCRead(unc_read);
|
|
251
|
+
}
|
|
252
|
+
if (permission.unc_write) {
|
|
253
|
+
PERMISSION.setUNCWrite(unc_write);
|
|
254
|
+
}
|
|
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 };
|
|
258
266
|
}
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
const pkg = require('minimatch');
|
|
262
|
-
MINIMATCH = 'minimatch' in pkg ? pkg.minimatch : pkg;
|
|
263
|
-
MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
|
|
264
|
-
}
|
|
265
|
-
catch {
|
|
266
|
-
}
|
|
267
|
+
catch {
|
|
267
268
|
}
|
|
268
269
|
}
|
|
269
270
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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;
|
|
274
285
|
}
|
|
275
286
|
if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
|
|
276
|
-
HOST.
|
|
287
|
+
HOST.QUEUE_EXPIRES = expires;
|
|
277
288
|
}
|
|
278
|
-
if ((0, types_1.isObject)(
|
|
279
|
-
let priority;
|
|
280
|
-
(
|
|
281
|
-
|
|
282
|
-
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;
|
|
283
293
|
}
|
|
284
|
-
if (
|
|
285
|
-
HOST.
|
|
294
|
+
if ((max = Math.floor(+max)) > 0) {
|
|
295
|
+
HOST.PRIORITY_MAX = max;
|
|
286
296
|
}
|
|
287
|
-
if (
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
|
|
299
|
-
HOST.PRIORITY_BYPASS = bypass;
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
bypass = HOST.PRIORITY_BYPASS;
|
|
303
|
-
}
|
|
304
|
-
if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
|
|
305
|
-
HOST.PRIORITY_BYPASS = undefined;
|
|
306
|
-
}
|
|
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;
|
|
307
308
|
}
|
|
308
309
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
else {
|
|
318
|
-
HOST.CIPHER_ALGORITHM = null;
|
|
319
|
-
HOST.CIPHER_KEY = null;
|
|
320
|
-
HOST.CIPHER_IV = null;
|
|
321
|
-
}
|
|
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;
|
|
322
318
|
}
|
|
323
|
-
|
|
324
|
-
HOST.
|
|
319
|
+
else {
|
|
320
|
+
HOST.CIPHER_ALGORITHM = null;
|
|
321
|
+
HOST.CIPHER_KEY = null;
|
|
322
|
+
HOST.CIPHER_IV = null;
|
|
325
323
|
}
|
|
326
324
|
}
|
|
325
|
+
if (typeof admin.private === 'boolean') {
|
|
326
|
+
HOST.ADMIN_PRIVATE = admin.private;
|
|
327
|
+
}
|
|
327
328
|
}
|
|
328
|
-
return true;
|
|
329
329
|
}
|
|
330
|
-
return
|
|
330
|
+
return true;
|
|
331
331
|
}
|
|
332
332
|
static createPermission(all, freeze) {
|
|
333
333
|
const permission = new Permission(freeze);
|
|
@@ -653,8 +653,8 @@ class Host extends module_1.default {
|
|
|
653
653
|
exports.Host = Host;
|
|
654
654
|
_a = kDone, _b = kQueued, _c = kUsername;
|
|
655
655
|
class Client extends module_1.default {
|
|
656
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
657
|
-
return parent ? super.purgeMemory(percent, limit) :
|
|
656
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
657
|
+
return parent ? super.purgeMemory(percent, limit) : 0;
|
|
658
658
|
}
|
|
659
659
|
constructor(data) {
|
|
660
660
|
super();
|
|
@@ -679,6 +679,11 @@ class Client extends module_1.default {
|
|
|
679
679
|
}
|
|
680
680
|
return this;
|
|
681
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
|
+
}
|
|
682
687
|
set cacheDir(value) {
|
|
683
688
|
if (path.isAbsolute(value) && module_1.default.isDir(value)) {
|
|
684
689
|
this[kCacheDir] = value;
|
|
@@ -711,43 +716,43 @@ class ClientDb extends Client {
|
|
|
711
716
|
static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
|
|
712
717
|
static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
|
|
713
718
|
static loadSettings(settings, password) {
|
|
714
|
-
if (super.loadSettings({ process: settings.process }, password)) {
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
percent /= 100;
|
|
732
|
-
}
|
|
733
|
-
if (percent > 0) {
|
|
734
|
-
(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 };
|
|
735
|
-
}
|
|
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);
|
|
736
736
|
}
|
|
737
|
-
else if (
|
|
738
|
-
|
|
737
|
+
else if ((percent = Math.max(percent, 0)) > 1) {
|
|
738
|
+
percent /= 100;
|
|
739
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;
|
|
740
746
|
}
|
|
741
747
|
}
|
|
742
748
|
}
|
|
743
749
|
}
|
|
744
750
|
}
|
|
745
|
-
return true;
|
|
746
751
|
}
|
|
747
|
-
return
|
|
752
|
+
return true;
|
|
748
753
|
}
|
|
749
|
-
static purgeMemory(percent = 1, limit = 0, parent) {
|
|
750
|
-
return parent ? super.purgeMemory(percent, limit, parent) :
|
|
754
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
755
|
+
return parent ? super.purgeMemory(percent, limit, parent) : 0;
|
|
751
756
|
}
|
|
752
757
|
static getTimeout(value) {
|
|
753
758
|
if (value === undefined) {
|
|
@@ -898,11 +903,11 @@ class ClientDb extends Client {
|
|
|
898
903
|
}
|
|
899
904
|
return result;
|
|
900
905
|
}
|
|
901
|
-
static purgeResult(prefix) {
|
|
906
|
+
static async purgeResult(prefix) {
|
|
902
907
|
const current = Date.now();
|
|
903
908
|
const providers = new Map();
|
|
904
909
|
if (prefix) {
|
|
905
|
-
if (prefix.
|
|
910
|
+
if (!prefix.includes('_')) {
|
|
906
911
|
prefix += '_';
|
|
907
912
|
for (const key in CACHE_USER) {
|
|
908
913
|
if (key.startsWith(prefix)) {
|
|
@@ -942,7 +947,7 @@ class ClientDb extends Client {
|
|
|
942
947
|
}
|
|
943
948
|
}
|
|
944
949
|
}
|
|
945
|
-
return
|
|
950
|
+
return result;
|
|
946
951
|
}
|
|
947
952
|
static setPoolConfig(value) { }
|
|
948
953
|
static getPoolConfig(source) { }
|
|
@@ -1153,9 +1158,9 @@ class AbortComponent {
|
|
|
1153
1158
|
map.splice(index, 1);
|
|
1154
1159
|
}
|
|
1155
1160
|
}
|
|
1156
|
-
abort() {
|
|
1161
|
+
abort(reason) {
|
|
1157
1162
|
if (!this.aborted) {
|
|
1158
|
-
this[kAbortHandler].abort();
|
|
1163
|
+
this[kAbortHandler].abort(reason);
|
|
1159
1164
|
}
|
|
1160
1165
|
}
|
|
1161
1166
|
reset() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/core",
|
|
3
|
-
"version": "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.
|
|
24
|
-
"@e-mc/types": "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
|
}
|