@e-mc/core 0.3.3 → 0.4.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 (4) hide show
  1. package/LICENSE +10 -10
  2. package/index.d.ts +12 -12
  3. package/index.js +1314 -1314
  4. package/package.json +3 -3
package/index.js CHANGED
@@ -1,1314 +1,1314 @@
1
- "use strict";
2
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Module = exports.Permission = exports.AbortComponent = exports.ClientDb = exports.Client = exports.Host = void 0;
5
- const path = require("path");
6
- const fs = require("fs");
7
- const pm = require("picomatch");
8
- const types_1 = require("../types");
9
- const module_1 = require("../module");
10
- exports.Module = module_1.default;
11
- const kConfig = Symbol('config');
12
- const kQueued = Symbol('queued');
13
- const kUsername = Symbol('username');
14
- const kAbortHandler = Symbol('abortHandler');
15
- const kFreeze = Symbol('freeze');
16
- const kDone = Symbol('done');
17
- const kCacheDir = Symbol('cacheDir');
18
- const kDiskRead = Symbol('diskRead');
19
- const kDiskWrite = Symbol('diskWrite');
20
- const kUncRead = Symbol('uncRead');
21
- const kUncWrite = Symbol('uncWrite');
22
- const CACHE_USER = {};
23
- const CACHE_SOURCE = {};
24
- const CACHE_SESSION = {};
25
- const ABORT_LISTENER = new WeakMap();
26
- const HOST = {
27
- DONE: new Map(),
28
- QUEUE: [],
29
- QUEUE_LIMIT: Infinity,
30
- QUEUE_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
31
- PID: 1,
32
- CLOSED: 0,
33
- QUEUED: 0,
34
- REJECTED: 0,
35
- KILLED: 0,
36
- CIPHER_ALGORITHM: undefined,
37
- CIPHER_KEY: undefined,
38
- CIPHER_IV: undefined,
39
- ADMIN_USERS: [],
40
- ADMIN_PRIVATE: false,
41
- THREAD_LIMIT: Infinity,
42
- THREAD_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
43
- PRIORITY_MIN: 0,
44
- PRIORITY_MAX: 100,
45
- PRIORITY_BYPASS: undefined
46
- };
47
- const PLATFORM_WIN32 = process.platform === 'win32';
48
- let STORE_RESULT_COUNT = 0;
49
- let PICOMATCH_OPTIONS = { nocase: PLATFORM_WIN32 };
50
- let MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32 };
51
- let MINIMATCH;
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
- function getSettingsValue(options, name, component) {
68
- const result = options[name];
69
- return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
70
- }
71
- function closeThread(host) {
72
- HOST.DONE.delete(host);
73
- ++HOST.CLOSED;
74
- const current = Date.now();
75
- for (let i = 0; i < HOST.QUEUE.length; ++i) {
76
- const item = HOST.QUEUE[i];
77
- const { instance, startTime } = item;
78
- if (startTime + HOST.QUEUE_EXPIRES <= current) {
79
- cancelThread(instance, 'expired: ' + (0, types_1.formatTime)(startTime, true));
80
- HOST.QUEUE.splice(i--, 1);
81
- callResumeThread(item, true);
82
- }
83
- }
84
- if (HOST.QUEUE.length && HOST.DONE.size < HOST.THREAD_LIMIT) {
85
- const queue = HOST.QUEUE.shift();
86
- resumeHostQueue(queue.instance);
87
- callResumeThread(queue);
88
- HOST.QUEUE.forEach((item, index) => updateHostQueue(item.instance, index, item.priority, true));
89
- }
90
- }
91
- function callResumeThread(item, aborted) {
92
- if (item.joined) {
93
- const { instance, args = [], startTime } = item;
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 });
96
- }
97
- queueMicrotask(() => {
98
- try {
99
- instance.resumeThread({ args, startTime, aborted });
100
- }
101
- catch {
102
- }
103
- });
104
- }
105
- }
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 });
108
- host.abort();
109
- if (rejected) {
110
- ++HOST.REJECTED;
111
- }
112
- else {
113
- ++HOST.KILLED;
114
- }
115
- }
116
- function encryptUsername(data, iv) {
117
- return HOST.CIPHER_ALGORITHM && HOST.CIPHER_KEY && (0, types_1.isString)(data) && (0, types_1.encryptUTF8)(HOST.CIPHER_ALGORITHM, HOST.CIPHER_KEY, iv, data) || '';
118
- }
119
- function matchPermission(permission, pathname) {
120
- const { enabled, value } = permission;
121
- return enabled === true && (0, types_1.isString)(pathname) && (value === '**/*' || value === '**' || Permission.match(pathname, value));
122
- }
123
- function setPermission(name, pathname, enabled) {
124
- if (typeof enabled === 'boolean' && (!this[kFreeze] || this[name].enabled === null) && (!enabled || (pathname = convertPosix(pathname)))) {
125
- if (enabled && Array.isArray(pathname)) {
126
- switch (pathname.length) {
127
- case 1:
128
- pathname = pathname[0];
129
- break;
130
- case 0:
131
- enabled = false;
132
- break;
133
- }
134
- }
135
- this[name] = Object.freeze({ enabled, value: enabled ? pathname : '' });
136
- }
137
- }
138
- function getFunctions(values) {
139
- const result = [];
140
- for (const value of values) {
141
- let method = null;
142
- if (typeof value === 'string') {
143
- method = module_1.default.parseFunction(value, { external: true, absolute: true, sync: true });
144
- }
145
- else if (typeof value === 'function') {
146
- method = value;
147
- }
148
- if (method) {
149
- result.push(method);
150
- }
151
- }
152
- return result;
153
- }
154
- function clearSourceResult(target) {
155
- const result = target[1];
156
- target[1] = null;
157
- if (typeof result === 'string') {
158
- fs.unlink(result, () => { });
159
- }
160
- }
161
- function addSourceResult(source, target) {
162
- const { items, config } = CACHE_SOURCE[source] || (CACHE_SOURCE[source] = { items: [] });
163
- if (config && items.length >= config.limit) {
164
- const { percent, min } = config;
165
- if (percent === 1) {
166
- items.forEach(item => clearSourceResult(item));
167
- items.length = 0;
168
- }
169
- else {
170
- items.sort((a, b) => a[2] - b[2]);
171
- for (let i = 0, length = Math.floor(items.length * percent); i < length; ++i) {
172
- const item = items[i];
173
- if (min === 0 || item[3] < min) {
174
- clearSourceResult(item);
175
- items.splice(i--, 1);
176
- --length;
177
- }
178
- }
179
- }
180
- }
181
- items.push(target);
182
- }
183
- function deleteSourceResult(source, target) {
184
- const stored = CACHE_SOURCE[source];
185
- if (stored) {
186
- const items = stored.items;
187
- const index = items.findIndex(item => item === target);
188
- if (index !== -1) {
189
- items.splice(index, 1);
190
- }
191
- }
192
- }
193
- function incrementSourceCount(source, target, cacheData, queryString) {
194
- const count = ++target[3];
195
- const stored = CACHE_SOURCE[source];
196
- if (stored?.config && count >= stored.config.max) {
197
- clearSourceResult(target);
198
- deleteSourceResult(source, target);
199
- if (cacheData && queryString) {
200
- delete cacheData[queryString];
201
- }
202
- }
203
- }
204
- function addHostQueue(item) {
205
- HOST.QUEUE.push(item);
206
- HOST.QUEUE.sort((a, b) => {
207
- const au = a.instance.username;
208
- const bu = b.instance.username;
209
- if (au && !bu) {
210
- return -1;
211
- }
212
- if (!au && bu) {
213
- return 1;
214
- }
215
- if (a.priority < b.priority) {
216
- return -1;
217
- }
218
- if (b.priority < a.priority) {
219
- return 1;
220
- }
221
- return 0;
222
- });
223
- }
224
- function resumeHostQueue(host) {
225
- HOST.DONE.set(host, HOST.PID++);
226
- host[kQueued] = false;
227
- }
228
- function updateHostQueue(host, position, priority, broadcast) {
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 });
231
- }
232
- }
233
- function asPosix(value) {
234
- value = path.normalize(value.trim());
235
- return PLATFORM_WIN32 ? value.replace(/\\/g, '/') : value;
236
- }
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;
240
- class Host extends module_1.default {
241
- static purgeMemory(percent = 1, limit = 0, parent) {
242
- return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
243
- }
244
- static loadSettings(settings, permission, password) {
245
- if (typeof permission === 'string') {
246
- password = permission;
247
- permission = undefined;
248
- }
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
- }
267
- }
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 };
272
- }
273
- if ((0, types_1.isPlainObject)(mini)) {
274
- try {
275
- MINIMATCH = require('minimatch');
276
- MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
277
- }
278
- catch {
279
- }
280
- }
281
- }
282
- }
283
- if (proc && (0, types_1.isObject)(proc.thread)) {
284
- let { limit, expires, queue, admin } = proc.thread;
285
- if (limit && (limit = Math.floor(+limit)) > 0) {
286
- HOST.THREAD_LIMIT = limit;
287
- }
288
- if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
289
- HOST.THREAD_EXPIRES = expires;
290
- }
291
- if ((0, types_1.isObject)(queue)) {
292
- let priority;
293
- ({ limit, expires, priority } = queue);
294
- if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
295
- HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
296
- }
297
- if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
298
- HOST.QUEUE_EXPIRES = expires;
299
- }
300
- if ((0, types_1.isObject)(priority)) {
301
- let { min = -1, max = 0, bypass } = priority;
302
- if ((min = Math.floor(+min)) >= 0) {
303
- HOST.PRIORITY_MIN = min;
304
- }
305
- if ((max = Math.floor(+max)) > 0) {
306
- HOST.PRIORITY_MAX = max;
307
- }
308
- if (HOST.PRIORITY_MAX < HOST.PRIORITY_MIN) {
309
- HOST.PRIORITY_MAX = HOST.PRIORITY_MIN + 1;
310
- }
311
- if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
312
- HOST.PRIORITY_BYPASS = bypass;
313
- }
314
- else {
315
- bypass = HOST.PRIORITY_BYPASS;
316
- }
317
- if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
318
- HOST.PRIORITY_BYPASS = undefined;
319
- }
320
- }
321
- }
322
- if (admin) {
323
- const cipher = proc.cipher;
324
- if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
325
- const algorithm = cipher.algorithm || 'aes-256-gcm';
326
- HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
327
- if (HOST.ADMIN_USERS.length) {
328
- HOST.CIPHER_ALGORITHM = algorithm;
329
- }
330
- else {
331
- HOST.CIPHER_ALGORITHM = null;
332
- HOST.CIPHER_KEY = null;
333
- HOST.CIPHER_IV = null;
334
- }
335
- }
336
- if (typeof admin.private === 'boolean') {
337
- HOST.ADMIN_PRIVATE = admin.private;
338
- }
339
- }
340
- }
341
- return true;
342
- }
343
- return false;
344
- }
345
- static createPermission(all, freeze) {
346
- const permission = new Permission(freeze);
347
- if (all) {
348
- permission.setDiskRead('**/*');
349
- permission.setDiskWrite('**/*');
350
- permission.setUNCRead('**/*');
351
- permission.setUNCWrite('**/*');
352
- }
353
- return permission;
354
- }
355
- static isPermission(value) {
356
- return value instanceof Permission;
357
- }
358
- static kill(username, iv, pid) {
359
- let result = 0;
360
- if (HOST.ADMIN_USERS.includes(encryptUsername(username, iv))) {
361
- for (const [host, id] of HOST.DONE) {
362
- if (pid === true || id === pid || Array.isArray(pid) && pid.includes(id)) {
363
- cancelThread(host, 'pid: ' + id.toString());
364
- closeThread(host);
365
- ++result;
366
- }
367
- }
368
- }
369
- return result;
370
- }
371
- static getThreadCount(username, iv) {
372
- let full;
373
- switch (typeof username) {
374
- case 'boolean':
375
- full = username;
376
- username = undefined;
377
- break;
378
- case 'string':
379
- if (!iv || !HOST.ADMIN_USERS.includes(encryptUsername(username, iv))) {
380
- username = undefined;
381
- }
382
- full = true;
383
- break;
384
- default:
385
- username = undefined;
386
- break;
387
- }
388
- if (HOST.ADMIN_PRIVATE && !username) {
389
- return full === true ? { count: -1 } : -1;
390
- }
391
- const current = Date.now();
392
- const items = [];
393
- for (const [host, pid] of HOST.DONE) {
394
- if (host.done) {
395
- closeThread(host);
396
- }
397
- else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
398
- 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.abort();
400
- closeThread(host);
401
- }
402
- else {
403
- items.push([host, pid]);
404
- }
405
- }
406
- const count = items.length;
407
- if (full === true) {
408
- const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
409
- if (count) {
410
- if (count > 1) {
411
- items.sort((a, b) => a[0].startTime - b[0].startTime);
412
- result.startTime = items[0][0].startTime;
413
- }
414
- result.previousTime = items[count - 1][0].startTime;
415
- if (username) {
416
- result.admin = items.map(([host, pid]) => {
417
- const { startTime, username: user, sessionId, broadcastId } = host;
418
- const thread = { pid, startTime };
419
- if (user) {
420
- thread.username = user;
421
- }
422
- if (sessionId) {
423
- thread.sessionId = sessionId;
424
- }
425
- if (broadcastId) {
426
- thread.broadcastId = broadcastId;
427
- }
428
- return thread;
429
- });
430
- }
431
- }
432
- if (HOST.THREAD_LIMIT !== Infinity) {
433
- result.max = HOST.THREAD_LIMIT;
434
- }
435
- return result;
436
- }
437
- return count;
438
- }
439
- static getPermissionFromSettings() {
440
- return PERMISSION;
441
- }
442
- constructor(config = {}) {
443
- super();
444
- this.restartable = true;
445
- this.modules = new Set();
446
- this.subProcesses = new Set();
447
- this.startTime = Date.now();
448
- this._threadable = true;
449
- this._logExclude = false;
450
- this._logLevel = 0;
451
- this._usingObjects = new Set();
452
- this[_a] = false;
453
- this[_b] = false;
454
- this[_c] = '';
455
- const { broadcastId, log, username, priority } = config;
456
- if ((0, types_1.isString)(username)) {
457
- this[kUsername] = username;
458
- }
459
- if (broadcastId) {
460
- this.broadcastId = broadcastId;
461
- }
462
- if (log !== undefined) {
463
- if ((0, types_1.isPlainObject)(log)) {
464
- const { enabled, level, exclude, useColor } = log;
465
- if (level !== undefined) {
466
- this.logLevel = level;
467
- }
468
- if (exclude) {
469
- this.ignoreLog(exclude);
470
- }
471
- else if (typeof enabled === 'boolean') {
472
- this.ignoreLog(!enabled);
473
- }
474
- if (useColor) {
475
- this.supports('stripAnsi', false);
476
- }
477
- }
478
- else {
479
- this.ignoreLog(typeof log === 'boolean' ? !log : log);
480
- }
481
- }
482
- this[kConfig] = Object.freeze(config);
483
- if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
484
- HOST.DONE.set(this, HOST.PID++);
485
- }
486
- else if (typeof priority === 'number' && priority >= HOST.PRIORITY_MIN && priority <= HOST.PRIORITY_MAX && HOST.QUEUE.length < HOST.QUEUE_LIMIT && typeof this.resumeThread === 'function') {
487
- this[kQueued] = true;
488
- addHostQueue({ instance: this, priority, startTime: Date.now() });
489
- updateHostQueue(this, HOST.QUEUE.findIndex(item => item.instance === this), priority);
490
- }
491
- else {
492
- cancelThread(this, 'limit: ' + HOST.THREAD_LIMIT.toString(), true);
493
- }
494
- }
495
- restart(...args) { }
496
- isFatal(err) {
497
- const fatal = this.config.error?.fatal;
498
- return fatal ?? super.isFatal(err);
499
- }
500
- using(...items) {
501
- const data = this._usingObjects;
502
- if (typeof items[0] === 'boolean' && items.shift()) {
503
- data.clear();
504
- }
505
- items.forEach(item => data.add(item));
506
- return this;
507
- }
508
- contains(item, condition) {
509
- const data = this._usingObjects;
510
- if (data.size) {
511
- if (data.has(item)) {
512
- return true;
513
- }
514
- if (condition) {
515
- for (const other of data) {
516
- if (condition(other, item)) {
517
- return true;
518
- }
519
- }
520
- }
521
- }
522
- return false;
523
- }
524
- find(name) {
525
- for (const instance of this.modules) {
526
- if (instance.moduleName === name) {
527
- return instance;
528
- }
529
- }
530
- for (const instance of this.subProcesses) {
531
- if (instance.moduleName === name) {
532
- return instance;
533
- }
534
- }
535
- }
536
- findAll(name) {
537
- const result = [];
538
- for (const instance of this.modules) {
539
- if (instance.moduleName === name) {
540
- result.push(instance);
541
- }
542
- }
543
- for (const instance of this.subProcesses) {
544
- if (instance.moduleName === name) {
545
- result.push(instance);
546
- }
547
- }
548
- return result;
549
- }
550
- willLog(name) {
551
- const value = this._logExclude;
552
- return !(typeof value === 'boolean' ? value : value.includes(name));
553
- }
554
- ignoreLog(values) {
555
- if (typeof values === 'string') {
556
- values = [values];
557
- }
558
- if (Array.isArray(values)) {
559
- if (!Array.isArray(this._logExclude)) {
560
- this._logExclude = [];
561
- }
562
- this._logExclude.push(...values);
563
- this._logEnabled = this.willLog(this.moduleName);
564
- }
565
- else if (values) {
566
- this._logExclude = true;
567
- this._logEnabled = false;
568
- }
569
- else {
570
- this._logExclude = false;
571
- this._logEnabled = true;
572
- }
573
- }
574
- collectLog(level) {
575
- const result = this.status.slice(0);
576
- for (const item of this.modules) {
577
- if (item.status.length) {
578
- result.push(...item.status);
579
- }
580
- }
581
- for (const item of this.subProcesses) {
582
- if (item.status.length) {
583
- result.push(...item.status);
584
- }
585
- }
586
- result.sort(level ? (a, b) => b.type - a.type : (a, b) => a.timeStamp - b.timeStamp);
587
- return result;
588
- }
589
- retain(process) {
590
- this.subProcesses.add(process);
591
- process.host = this;
592
- }
593
- release(process, log = true) {
594
- if (this.subProcesses.delete(process)) {
595
- if (log) {
596
- const status = process.status;
597
- if (status.length) {
598
- this.status.push(...status);
599
- status.length = 0;
600
- }
601
- process.flushLog();
602
- }
603
- process.host = null;
604
- return true;
605
- }
606
- return false;
607
- }
608
- loadModule(name, ...args) {
609
- for (const instance of this.modules) {
610
- if (instance.moduleName === name) {
611
- return instance;
612
- }
613
- }
614
- return null;
615
- }
616
- joinQueue(options) {
617
- const index = HOST.QUEUE.findIndex(item => item.instance === this);
618
- if (index !== -1) {
619
- const queue = HOST.QUEUE[index];
620
- let args, reject;
621
- if (options) {
622
- ({ args, reject } = options);
623
- }
624
- if (reject) {
625
- HOST.QUEUE.splice(index, 1);
626
- resumeHostQueue(this);
627
- return false;
628
- }
629
- if (Array.isArray(args)) {
630
- queue.args = args;
631
- }
632
- if (!queue.joined) {
633
- queue.joined = true;
634
- ++HOST.QUEUED;
635
- }
636
- return true;
637
- }
638
- return false;
639
- }
640
- set host(value) { }
641
- get host() {
642
- return null;
643
- }
644
- get config() {
645
- return this[kConfig];
646
- }
647
- get username() {
648
- return this[kUsername];
649
- }
650
- set done(value) {
651
- if (value) {
652
- this[kDone] = true;
653
- closeThread(this);
654
- }
655
- }
656
- get done() {
657
- return this[kDone] || this.aborted;
658
- }
659
- get queued() {
660
- return this[kQueued];
661
- }
662
- get errorCount() {
663
- return Array.from(this.modules).reduce((a, b) => a + b.errors.length, this.errors.length);
664
- }
665
- }
666
- exports.Host = Host;
667
- _a = kDone, _b = kQueued, _c = kUsername;
668
- class Client extends module_1.default {
669
- static purgeMemory(percent = 1, limit = 0, parent) {
670
- return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
671
- }
672
- constructor(data) {
673
- super();
674
- this._extensions = null;
675
- this[_d] = '';
676
- this.module = (0, types_1.isPlainObject)(data) ? data : {};
677
- }
678
- init(...args) {
679
- const { permission, extensions } = this.module;
680
- const cache_dir = this.settings.cache_dir;
681
- if (permission) {
682
- const result = Permission.create(permission, permission.inherit !== false ? this.host?.permission : null, true);
683
- if (result) {
684
- this.permission = result;
685
- }
686
- }
687
- if ((0, types_1.isArray)(extensions)) {
688
- this._extensions || (this._extensions = extensions.slice(0));
689
- }
690
- if (cache_dir) {
691
- this.cacheDir = cache_dir;
692
- }
693
- return this;
694
- }
695
- set cacheDir(value) {
696
- if (path.isAbsolute(value) && module_1.default.isDir(value)) {
697
- this[kCacheDir] = value;
698
- }
699
- }
700
- get cacheDir() {
701
- return this[kCacheDir];
702
- }
703
- get settings() {
704
- var _k;
705
- return ((_k = this.module).settings || (_k.settings = {}));
706
- }
707
- set extensions(values) {
708
- if (Array.isArray(values)) {
709
- this._extensions = values;
710
- }
711
- }
712
- get extensions() {
713
- const extensions = this._extensions;
714
- return extensions ? this._extensions = getFunctions(extensions) : [];
715
- }
716
- }
717
- exports.Client = Client;
718
- _d = kCacheDir;
719
- class ClientDb extends Client {
720
- static get TRANSACTION_ACTIVE() { return 1 /* DB_TRANSACTION.ACTIVE */; }
721
- static get TRANSACTION_PARTIAL() { return 2 /* DB_TRANSACTION.PARTIAL */; }
722
- static get TRANSACTION_COMMIT() { return 4 /* DB_TRANSACTION.COMMIT */; }
723
- static get TRANSACTION_TERMINATE() { return 8 /* DB_TRANSACTION.TERMINATE */; }
724
- static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
725
- static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
726
- static loadSettings(settings, password) {
727
- if (super.loadSettings({ process: settings.process }, password)) {
728
- const memory = settings.memory;
729
- if ((0, types_1.isPlainObject)(memory)) {
730
- for (const name in memory) {
731
- const client = memory[name];
732
- if ((0, types_1.isPlainObject)(client)) {
733
- for (const source in client) {
734
- const item = client[source];
735
- if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
736
- const key = name + '_' + source;
737
- const stored = CACHE_SOURCE[key];
738
- let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
739
- if (enabled && limit > 0) {
740
- if ((0, types_1.isString)(percent)) {
741
- percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
742
- }
743
- else if ((percent = Math.max(percent, 0)) > 1) {
744
- percent /= 100;
745
- }
746
- if (percent > 0) {
747
- (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 };
748
- }
749
- }
750
- else if (stored) {
751
- delete stored.config;
752
- }
753
- }
754
- }
755
- }
756
- }
757
- }
758
- return true;
759
- }
760
- return false;
761
- }
762
- static purgeMemory(percent = 1, limit = 0, parent) {
763
- return parent ? super.purgeMemory(percent, limit, parent) : Promise.resolve(0);
764
- }
765
- static getTimeout(value) {
766
- if (value === undefined) {
767
- return 0;
768
- }
769
- let result = 0;
770
- switch (typeof value) {
771
- case 'number':
772
- result = value;
773
- break;
774
- case 'string':
775
- result = convertSeconds(value);
776
- break;
777
- case 'object':
778
- if (value && typeof (result = value.timeout) === 'string') {
779
- result = convertSeconds(value.timeout);
780
- value.timeout = result;
781
- }
782
- break;
783
- }
784
- return result > 0 ? result : 0;
785
- }
786
- static convertTime(value) {
787
- let result = 0;
788
- switch (typeof value) {
789
- case 'number':
790
- result = value * 1000 /* TIME.S */;
791
- break;
792
- case 'string':
793
- result = (0, types_1.parseTime)(value);
794
- break;
795
- }
796
- return result > 0 ? result : 0;
797
- }
798
- static findResult(source, credential, queryString, timeout, sessionKey, renewCache) {
799
- const userKey = this.extractUUID(credential) || this.asHash(this.asString(credential));
800
- if (userKey) {
801
- queryString = this.asHash(queryString);
802
- if (timeout > 0) {
803
- const userCache = CACHE_USER[source]?.[userKey];
804
- let stored;
805
- if (userCache && (stored = userCache[queryString])) {
806
- const [expires, result] = stored;
807
- if (result) {
808
- const current = Date.now();
809
- let valid;
810
- if (renewCache) {
811
- stored[0] = expireTime(timeout);
812
- valid = true;
813
- }
814
- else if (current < expires) {
815
- valid = true;
816
- }
817
- if (valid) {
818
- stored[2] = current;
819
- incrementSourceCount(source, stored, userCache, queryString);
820
- if (Array.isArray(result)) {
821
- return result;
822
- }
823
- try {
824
- return JSON.parse(fs.readFileSync(result, 'utf-8'));
825
- }
826
- catch {
827
- }
828
- }
829
- clearSourceResult(stored);
830
- }
831
- deleteSourceResult(source, stored);
832
- delete userCache[queryString];
833
- }
834
- }
835
- else if (sessionKey) {
836
- return CACHE_SESSION[source]?.[userKey + sessionKey]?.[queryString];
837
- }
838
- }
839
- }
840
- static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
841
- var _k, _l;
842
- let cache, cacheDir;
843
- if ((0, types_1.isObject)(sessionKey)) {
844
- ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
845
- }
846
- else if ((0, types_1.isObject)(options) && 'cache' in options) {
847
- ({ cache, cacheDir, sessionKey, sessionExpires } = options);
848
- }
849
- let timeout = 0, userKey, whenEmpty, partition;
850
- if (cache) {
851
- switch (typeof cache) {
852
- case 'number':
853
- timeout = cache;
854
- break;
855
- case 'string':
856
- if (typeof sessionKey === 'number') {
857
- sessionExpires = sessionKey;
858
- sessionKey = cache;
859
- break;
860
- }
861
- timeout = convertSeconds(cache);
862
- break;
863
- case 'object': {
864
- let seconds, dir;
865
- ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
866
- if ((0, types_1.isString)(dir) && (!cacheDir || module_1.default.isDir(dir))) {
867
- cacheDir = dir;
868
- partition = true;
869
- }
870
- if (typeof seconds === 'number') {
871
- timeout = seconds;
872
- }
873
- else {
874
- cache.timeout = seconds ? convertSeconds(seconds) : 0;
875
- }
876
- break;
877
- }
878
- }
879
- }
880
- if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) || this.asHash(this.asString(credential)))) {
881
- queryString = this.asHash(queryString);
882
- if (timeout > 0) {
883
- const item = [expireTime(timeout), result, Date.now(), 0];
884
- ((_k = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_k[userKey] = {}))[queryString] = item;
885
- addSourceResult(source, item);
886
- if (cacheDir) {
887
- if (partition) {
888
- const subDir = path.join(cacheDir, (STORE_RESULT_COUNT++ % this.STORE_RESULT_PARTITION_SIZE).toString(), queryString.substring(0, this.STORE_RESULT_PARTITION_MULT));
889
- if (this.createDir(subDir)) {
890
- cacheDir = subDir;
891
- }
892
- }
893
- const pathname = path.join(cacheDir, queryString);
894
- fs.writeFile(pathname, JSON.stringify(result), err => {
895
- if (!err) {
896
- item[1] = pathname;
897
- }
898
- });
899
- }
900
- }
901
- else if (typeof sessionKey === 'string' && sessionExpires && sessionExpires > 0) {
902
- const dbKey = userKey + sessionKey;
903
- ((_l = (CACHE_SESSION[source] || (CACHE_SESSION[source] = {})))[dbKey] || (_l[dbKey] = {}))[queryString] = result;
904
- setTimeout(() => delete CACHE_SESSION[dbKey], sessionExpires * 1000 /* TIME.S */);
905
- }
906
- }
907
- return result;
908
- }
909
- static purgeResult(prefix) {
910
- const current = Date.now();
911
- const providers = new Map();
912
- if (prefix) {
913
- if (prefix.indexOf('_') === -1) {
914
- prefix += '_';
915
- for (const key in CACHE_USER) {
916
- if (key.startsWith(prefix)) {
917
- providers.set(key, CACHE_USER[key]);
918
- }
919
- }
920
- }
921
- else {
922
- const source = CACHE_USER[prefix];
923
- if (source) {
924
- providers.set(prefix, source);
925
- }
926
- }
927
- }
928
- else {
929
- for (const key in CACHE_USER) {
930
- providers.set(key, CACHE_USER[key]);
931
- }
932
- }
933
- let result = 0;
934
- for (const [source, data] of providers) {
935
- for (const user in data) {
936
- const queries = data[user];
937
- for (const query in queries) {
938
- const item = queries[query];
939
- if (current >= item[0]) {
940
- ++result;
941
- delete queries[query];
942
- deleteSourceResult(source, item);
943
- if (typeof item[1] === 'string') {
944
- fs.unlink(item[1], () => { });
945
- }
946
- }
947
- }
948
- if (Object.keys(queries).length === 0) {
949
- delete data[user];
950
- }
951
- }
952
- }
953
- return Promise.resolve(result);
954
- }
955
- static setPoolConfig(value) { }
956
- static getPoolConfig(source) { }
957
- static extractUUID(credential) {
958
- if (typeof credential === 'string') {
959
- return (0, types_1.validateUUID)(credential) ? credential : '';
960
- }
961
- let result;
962
- return (0, types_1.isObject)(credential) && (result = credential.uuidKey) && (0, types_1.validateUUID)(result) ? result : '';
963
- }
964
- static keyOfResult(source, credential, uuidOnly) {
965
- const result = this.extractUUID(credential);
966
- if (uuidOnly) {
967
- return result ? source + '_' + result : '';
968
- }
969
- return source + '_' + (result || this.asString(credential, true));
970
- }
971
- constructor(data, database = []) {
972
- super(data);
973
- this.database = database;
974
- this.cacheExpires = 600000 /* VALUES.DB_EXPIRES */;
975
- }
976
- init(config) {
977
- let { cache_dir, session_expires: expires } = this.settings;
978
- if (cache_dir) {
979
- this.cacheDir = cache_dir;
980
- }
981
- if (expires !== undefined) {
982
- if (typeof expires === 'string') {
983
- if (!isNaN(expires = (0, types_1.parseTime)(expires))) {
984
- this.settings.session_expires = expires;
985
- }
986
- else {
987
- delete this.settings.cache_session;
988
- }
989
- }
990
- if (expires >= 0) {
991
- this.cacheExpires = expires;
992
- }
993
- }
994
- return super.init();
995
- }
996
- add(item, state) {
997
- if (state) {
998
- item.transactionState = state;
999
- }
1000
- if (!this.database.includes(item)) {
1001
- this.database.push(item);
1002
- }
1003
- }
1004
- hasCache(source, sessionKey, override) {
1005
- return (0, types_1.isString)(sessionKey) || ClientDb.getTimeout(override ?? this.settingsOf(source, 'cache')) > 0;
1006
- }
1007
- hasCoerce(source, component, override, credential) {
1008
- if (override) {
1009
- if (typeof override === 'string') {
1010
- credential = override;
1011
- override = undefined;
1012
- }
1013
- else if (!credential && (0, types_1.isPlainObject)(override) && 'uuidKey' in override) {
1014
- credential = override.uuidKey;
1015
- override = undefined;
1016
- }
1017
- }
1018
- let uuidKey;
1019
- if (credential && ((0, types_1.validateUUID)(credential) ? uuidKey = credential : uuidKey = ClientDb.extractUUID(credential))) {
1020
- const value = this.settingsKey(uuidKey, 'coerce', component);
1021
- if (value !== undefined) {
1022
- override = value;
1023
- }
1024
- }
1025
- if (override !== undefined) {
1026
- if (typeof override === 'boolean') {
1027
- return override && component === 'options';
1028
- }
1029
- if ((0, types_1.isPlainObject)(override) && (override = override[component]) !== undefined) {
1030
- return override === true;
1031
- }
1032
- }
1033
- return this.settingsOf(source, 'coerce', component) === true || component === 'options' && this.settingsOf(source, 'coerce') === true;
1034
- }
1035
- getQueryResult(source, credential, queryString, options, renewCache) {
1036
- let sessionKey, value;
1037
- switch (typeof options) {
1038
- case 'boolean':
1039
- renewCache = options;
1040
- break;
1041
- case 'string':
1042
- sessionKey = options;
1043
- break;
1044
- case 'object':
1045
- if (options) {
1046
- ({ value, sessionKey, renewCache } = options);
1047
- }
1048
- break;
1049
- }
1050
- const uuidKey = ClientDb.extractUUID(credential);
1051
- if (uuidKey) {
1052
- const cache = this.settingsKey(uuidKey, 'cache');
1053
- if (cache !== undefined) {
1054
- value = cache;
1055
- }
1056
- }
1057
- return ClientDb.findResult(this.moduleName + '_' + source, uuidKey || credential, queryString, value === 0 ? 0 : ClientDb.getTimeout(value || this.settingsOf(source, 'cache')), sessionKey, renewCache);
1058
- }
1059
- setQueryResult(source, credential, queryString, result, sessionKey) {
1060
- if (!Array.isArray(result)) {
1061
- result = result === undefined ? [] : [result];
1062
- }
1063
- if (!(0, types_1.isString)(queryString)) {
1064
- return result;
1065
- }
1066
- const uuidKey = ClientDb.extractUUID(credential);
1067
- let options;
1068
- if (ClientDb.enabled("memory.settings.users" /* KEY_NAME.MEMORY_SETTINGS_USERS */, this.host?.username)) {
1069
- let value;
1070
- if ((0, types_1.isObject)(sessionKey)) {
1071
- ({ value, sessionKey } = sessionKey);
1072
- }
1073
- if (uuidKey) {
1074
- const cache = this.settingsKey(uuidKey, 'cache');
1075
- if (cache !== undefined) {
1076
- value = cache;
1077
- }
1078
- }
1079
- options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1080
- }
1081
- else {
1082
- options = { cacheDir: this.cacheDir };
1083
- }
1084
- return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
1085
- }
1086
- applyState(items, value, as) {
1087
- for (const item of Array.isArray(items) ? items : [items]) {
1088
- if ((0, types_1.hasBit)(value, 8 /* DB_TRANSACTION.TERMINATE */)) {
1089
- const state = item.transactionState || (item.transactionState = 0);
1090
- if ((0, types_1.hasBit)(state, 4 /* DB_TRANSACTION.COMMIT */) || (0, types_1.hasBit)(state, 32 /* DB_TRANSACTION.FAIL */)) {
1091
- item.transactionState |= value;
1092
- }
1093
- }
1094
- else if ((0, types_1.hasBit)(value, 16 /* DB_TRANSACTION.ABORT */)) {
1095
- item.transactionState || (item.transactionState = value);
1096
- }
1097
- else if (as) {
1098
- item.transactionState = value;
1099
- }
1100
- else {
1101
- item.transactionState |= value;
1102
- }
1103
- }
1104
- }
1105
- valueOfKey(credential, name, component) {
1106
- const uuidKey = ClientDb.extractUUID(credential);
1107
- if (uuidKey) {
1108
- return this.settingsKey(uuidKey, name, component);
1109
- }
1110
- }
1111
- settingsOf(source, name, component) {
1112
- const data = this.settings[source];
1113
- if ((0, types_1.isObject)(data)) {
1114
- return getSettingsValue(data, name, component);
1115
- }
1116
- }
1117
- settingsKey(uuidKey, name, component) {
1118
- var _k;
1119
- const data = ((_k = this.settings).user_key || (_k.user_key = {}))[uuidKey];
1120
- if ((0, types_1.isObject)(data)) {
1121
- return getSettingsValue(data, name, component);
1122
- }
1123
- }
1124
- get pending() {
1125
- return this.database.filter(item => {
1126
- const state = item.transactionState || 0;
1127
- return state === 0 || state === 16 /* DB_TRANSACTION.ABORT */;
1128
- });
1129
- }
1130
- get committed() {
1131
- return this.database.filter(item => (item.transactionState & 4 /* DB_TRANSACTION.COMMIT */) === 4 /* DB_TRANSACTION.COMMIT */);
1132
- }
1133
- get failed() {
1134
- return this.database.filter(item => (item.transactionState & 32 /* DB_TRANSACTION.FAIL */) === 32 /* DB_TRANSACTION.FAIL */);
1135
- }
1136
- }
1137
- exports.ClientDb = ClientDb;
1138
- ClientDb.STORE_RESULT_PARTITION_SIZE = 16 /* VALUES.DB_PARTITION_SIZE */;
1139
- ClientDb.STORE_RESULT_PARTITION_MULT = 2 /* VALUES.DB_PARTITION_MULT */;
1140
- class AbortComponent {
1141
- constructor() {
1142
- this[_e] = new AbortController();
1143
- }
1144
- static attach(instance, signal, options) {
1145
- let map = ABORT_LISTENER.get(instance);
1146
- if (!map) {
1147
- ABORT_LISTENER.set(instance, map = []);
1148
- }
1149
- if (!map.find(item => item.signal === signal)) {
1150
- const listener = () => instance.abort();
1151
- signal.addEventListener('abort', listener, options);
1152
- map.push({ signal, listener });
1153
- }
1154
- }
1155
- static detach(instance, signal) {
1156
- const map = ABORT_LISTENER.get(instance);
1157
- let index;
1158
- if (map && (index = map.findIndex(item => item.signal === signal)) !== -1) {
1159
- const target = map[index];
1160
- target.signal.removeEventListener('abort', target.listener);
1161
- map.splice(index, 1);
1162
- }
1163
- }
1164
- abort() {
1165
- if (!this.aborted) {
1166
- this[kAbortHandler].abort();
1167
- }
1168
- }
1169
- reset() {
1170
- if (this.aborted) {
1171
- this[kAbortHandler] = new AbortController();
1172
- }
1173
- }
1174
- get signal() {
1175
- return this[kAbortHandler].signal;
1176
- }
1177
- get aborted() {
1178
- return this.signal.aborted;
1179
- }
1180
- }
1181
- exports.AbortComponent = AbortComponent;
1182
- _e = kAbortHandler;
1183
- class Permission {
1184
- static create(settings, parent, freeze) {
1185
- if (this.validate(settings)) {
1186
- if (typeof parent === 'boolean') {
1187
- freeze = parent;
1188
- parent = null;
1189
- }
1190
- const { inherit, disk_read, disk_write, unc_read, unc_write } = settings;
1191
- const result = new Permission(freeze);
1192
- if (inherit === false) {
1193
- parent = null;
1194
- }
1195
- let valid;
1196
- if (readable(disk_read) && (!parent || parent.diskRead)) {
1197
- result.setDiskRead(disk_read);
1198
- valid = true;
1199
- }
1200
- else if (parent?.diskRead) {
1201
- result.setDiskRead(parent.getDiskRead());
1202
- }
1203
- if (readable(disk_write) && (!parent || parent.diskWrite)) {
1204
- result.setDiskWrite(disk_write);
1205
- valid = true;
1206
- }
1207
- else if (parent?.diskWrite) {
1208
- result.setDiskWrite(parent.getDiskWrite());
1209
- }
1210
- if (readable(unc_read) && (!parent || parent.uncRead)) {
1211
- result.setUNCRead(unc_read);
1212
- valid = true;
1213
- }
1214
- else if (parent?.uncRead) {
1215
- result.setUNCRead(parent.getUNCRead());
1216
- }
1217
- if (readable(unc_write) && (!parent || parent.uncWrite)) {
1218
- result.setUNCWrite(unc_write);
1219
- valid = true;
1220
- }
1221
- else if (parent?.uncWrite) {
1222
- result.setUNCWrite(parent.getUNCWrite());
1223
- }
1224
- if (valid) {
1225
- return result;
1226
- }
1227
- }
1228
- return null;
1229
- }
1230
- static validate(settings) {
1231
- return (0, types_1.isPlainObject)(settings) && (readable(settings.disk_read) || readable(settings.disk_write) || readable(settings.unc_read) || readable(settings.unc_write));
1232
- }
1233
- static clone(permission, freeze) {
1234
- const result = new Permission(freeze);
1235
- if (permission.diskRead) {
1236
- result.setDiskRead(permission.getDiskRead());
1237
- }
1238
- if (permission.diskWrite) {
1239
- result.setDiskWrite(permission.getDiskWrite());
1240
- }
1241
- if (permission.uncRead) {
1242
- result.setUNCRead(permission.getUNCRead());
1243
- }
1244
- if (permission.uncWrite) {
1245
- result.setUNCWrite(permission.getUNCWrite());
1246
- }
1247
- return result;
1248
- }
1249
- static match(pathname, pattern) {
1250
- pathname = asPosix(pathname);
1251
- if (MINIMATCH) {
1252
- return Array.isArray(pattern) ? pattern.some(target => MINIMATCH(pathname, target, MINIMATCH_OPTIONS)) : MINIMATCH(pathname, pattern, MINIMATCH_OPTIONS);
1253
- }
1254
- return pm.isMatch(pathname, pattern, PICOMATCH_OPTIONS);
1255
- }
1256
- constructor(freeze = false) {
1257
- this[_f] = { enabled: null, value: '' };
1258
- this[_g] = { enabled: null, value: '' };
1259
- this[_h] = { enabled: null, value: '' };
1260
- this[_j] = { enabled: null, value: '' };
1261
- this[kFreeze] = freeze;
1262
- }
1263
- setDiskRead(pathname = '', enabled = true) {
1264
- setPermission.call(this, kDiskRead, pathname, enabled);
1265
- }
1266
- setDiskWrite(pathname = '', enabled = true) {
1267
- setPermission.call(this, kDiskWrite, pathname, enabled);
1268
- }
1269
- setUNCRead(pathname = '', enabled = true) {
1270
- setPermission.call(this, kUncRead, pathname, enabled);
1271
- }
1272
- setUNCWrite(pathname = '', enabled = true) {
1273
- setPermission.call(this, kUncWrite, pathname, enabled);
1274
- }
1275
- getDiskRead() {
1276
- return this[kDiskRead].value;
1277
- }
1278
- getDiskWrite() {
1279
- return this[kDiskWrite].value;
1280
- }
1281
- getUNCRead() {
1282
- return this[kUncRead].value;
1283
- }
1284
- getUNCWrite() {
1285
- return this[kUncWrite].value;
1286
- }
1287
- hasDiskRead(pathname) {
1288
- return matchPermission(this[kDiskRead], pathname);
1289
- }
1290
- hasDiskWrite(pathname) {
1291
- return matchPermission(this[kDiskWrite], pathname);
1292
- }
1293
- hasUNCRead(pathname) {
1294
- return matchPermission(this[kUncRead], pathname);
1295
- }
1296
- hasUNCWrite(pathname) {
1297
- return matchPermission(this[kUncWrite], pathname);
1298
- }
1299
- get diskRead() {
1300
- return this[kDiskRead].enabled === true;
1301
- }
1302
- get diskWrite() {
1303
- return this[kDiskWrite].enabled === true;
1304
- }
1305
- get uncRead() {
1306
- return this[kUncRead].enabled === true;
1307
- }
1308
- get uncWrite() {
1309
- return this[kUncWrite].enabled === true;
1310
- }
1311
- }
1312
- exports.Permission = Permission;
1313
- _f = kDiskRead, _g = kDiskWrite, _h = kUncRead, _j = kUncWrite;
1314
- PERMISSION = Host.createPermission(true, true);
1
+ "use strict";
2
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Module = exports.Permission = exports.AbortComponent = exports.ClientDb = exports.Client = exports.Host = void 0;
5
+ const path = require("path");
6
+ const fs = require("fs");
7
+ const pm = require("picomatch");
8
+ const types_1 = require("../types");
9
+ const module_1 = require("../module");
10
+ exports.Module = module_1.default;
11
+ const kConfig = Symbol('config');
12
+ const kQueued = Symbol('queued');
13
+ const kUsername = Symbol('username');
14
+ const kAbortHandler = Symbol('abortHandler');
15
+ const kFreeze = Symbol('freeze');
16
+ const kDone = Symbol('done');
17
+ const kCacheDir = Symbol('cacheDir');
18
+ const kDiskRead = Symbol('diskRead');
19
+ const kDiskWrite = Symbol('diskWrite');
20
+ const kUncRead = Symbol('uncRead');
21
+ const kUncWrite = Symbol('uncWrite');
22
+ const CACHE_USER = {};
23
+ const CACHE_SOURCE = {};
24
+ const CACHE_SESSION = {};
25
+ const ABORT_LISTENER = new WeakMap();
26
+ const HOST = {
27
+ DONE: new Map(),
28
+ QUEUE: [],
29
+ QUEUE_LIMIT: Infinity,
30
+ QUEUE_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
31
+ PID: 1,
32
+ CLOSED: 0,
33
+ QUEUED: 0,
34
+ REJECTED: 0,
35
+ KILLED: 0,
36
+ CIPHER_ALGORITHM: undefined,
37
+ CIPHER_KEY: undefined,
38
+ CIPHER_IV: undefined,
39
+ ADMIN_USERS: [],
40
+ ADMIN_PRIVATE: false,
41
+ THREAD_LIMIT: Infinity,
42
+ THREAD_EXPIRES: 86400000 /* VALUES.HOST_EXPIRES */,
43
+ PRIORITY_MIN: 0,
44
+ PRIORITY_MAX: 100,
45
+ PRIORITY_BYPASS: undefined
46
+ };
47
+ const PLATFORM_WIN32 = process.platform === 'win32';
48
+ let STORE_RESULT_COUNT = 0;
49
+ let PICOMATCH_OPTIONS = { nocase: PLATFORM_WIN32 };
50
+ let MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32 };
51
+ let MINIMATCH;
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
+ function getSettingsValue(options, name, component) {
68
+ const result = options[name];
69
+ return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
70
+ }
71
+ function closeThread(host) {
72
+ HOST.DONE.delete(host);
73
+ ++HOST.CLOSED;
74
+ const current = Date.now();
75
+ for (let i = 0; i < HOST.QUEUE.length; ++i) {
76
+ const item = HOST.QUEUE[i];
77
+ const { instance, startTime } = item;
78
+ if (startTime + HOST.QUEUE_EXPIRES <= current) {
79
+ cancelThread(instance, 'expired: ' + (0, types_1.formatTime)(startTime, true));
80
+ HOST.QUEUE.splice(i--, 1);
81
+ callResumeThread(item, true);
82
+ }
83
+ }
84
+ if (HOST.QUEUE.length && HOST.DONE.size < HOST.THREAD_LIMIT) {
85
+ const queue = HOST.QUEUE.shift();
86
+ resumeHostQueue(queue.instance);
87
+ callResumeThread(queue);
88
+ HOST.QUEUE.forEach((item, index) => updateHostQueue(item.instance, index, item.priority, true));
89
+ }
90
+ }
91
+ function callResumeThread(item, aborted) {
92
+ if (item.joined) {
93
+ const { instance, args = [], startTime } = item;
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 });
96
+ }
97
+ queueMicrotask(() => {
98
+ try {
99
+ instance.resumeThread({ args, startTime, aborted });
100
+ }
101
+ catch {
102
+ }
103
+ });
104
+ }
105
+ }
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 });
108
+ host.abort();
109
+ if (rejected) {
110
+ ++HOST.REJECTED;
111
+ }
112
+ else {
113
+ ++HOST.KILLED;
114
+ }
115
+ }
116
+ function encryptUsername(data, iv) {
117
+ return HOST.CIPHER_ALGORITHM && HOST.CIPHER_KEY && (0, types_1.isString)(data) && (0, types_1.encryptUTF8)(HOST.CIPHER_ALGORITHM, HOST.CIPHER_KEY, iv, data) || '';
118
+ }
119
+ function matchPermission(permission, pathname) {
120
+ const { enabled, value } = permission;
121
+ return enabled === true && (0, types_1.isString)(pathname) && (value === '**/*' || value === '**' || Permission.match(pathname, value));
122
+ }
123
+ function setPermission(name, pathname, enabled) {
124
+ if (typeof enabled === 'boolean' && (!this[kFreeze] || this[name].enabled === null) && (!enabled || (pathname = convertPosix(pathname)))) {
125
+ if (enabled && Array.isArray(pathname)) {
126
+ switch (pathname.length) {
127
+ case 1:
128
+ pathname = pathname[0];
129
+ break;
130
+ case 0:
131
+ enabled = false;
132
+ break;
133
+ }
134
+ }
135
+ this[name] = Object.freeze({ enabled, value: enabled ? pathname : '' });
136
+ }
137
+ }
138
+ function getFunctions(values) {
139
+ const result = [];
140
+ for (const value of values) {
141
+ let method = null;
142
+ if (typeof value === 'string') {
143
+ method = module_1.default.parseFunction(value, { external: true, absolute: true, sync: true });
144
+ }
145
+ else if (typeof value === 'function') {
146
+ method = value;
147
+ }
148
+ if (method) {
149
+ result.push(method);
150
+ }
151
+ }
152
+ return result;
153
+ }
154
+ function clearSourceResult(target) {
155
+ const result = target[1];
156
+ target[1] = null;
157
+ if (typeof result === 'string') {
158
+ fs.unlink(result, () => { });
159
+ }
160
+ }
161
+ function addSourceResult(source, target) {
162
+ const { items, config } = CACHE_SOURCE[source] || (CACHE_SOURCE[source] = { items: [] });
163
+ if (config && items.length >= config.limit) {
164
+ const { percent, min } = config;
165
+ if (percent === 1) {
166
+ items.forEach(item => clearSourceResult(item));
167
+ items.length = 0;
168
+ }
169
+ else {
170
+ items.sort((a, b) => a[2] - b[2]);
171
+ for (let i = 0, length = Math.floor(items.length * percent); i < length; ++i) {
172
+ const item = items[i];
173
+ if (min === 0 || item[3] < min) {
174
+ clearSourceResult(item);
175
+ items.splice(i--, 1);
176
+ --length;
177
+ }
178
+ }
179
+ }
180
+ }
181
+ items.push(target);
182
+ }
183
+ function deleteSourceResult(source, target) {
184
+ const stored = CACHE_SOURCE[source];
185
+ if (stored) {
186
+ const items = stored.items;
187
+ const index = items.findIndex(item => item === target);
188
+ if (index !== -1) {
189
+ items.splice(index, 1);
190
+ }
191
+ }
192
+ }
193
+ function incrementSourceCount(source, target, cacheData, queryString) {
194
+ const count = ++target[3];
195
+ const stored = CACHE_SOURCE[source];
196
+ if (stored?.config && count >= stored.config.max) {
197
+ clearSourceResult(target);
198
+ deleteSourceResult(source, target);
199
+ if (cacheData && queryString) {
200
+ delete cacheData[queryString];
201
+ }
202
+ }
203
+ }
204
+ function addHostQueue(item) {
205
+ HOST.QUEUE.push(item);
206
+ HOST.QUEUE.sort((a, b) => {
207
+ const au = a.instance.username;
208
+ const bu = b.instance.username;
209
+ if (au && !bu) {
210
+ return -1;
211
+ }
212
+ if (!au && bu) {
213
+ return 1;
214
+ }
215
+ if (a.priority < b.priority) {
216
+ return -1;
217
+ }
218
+ if (b.priority < a.priority) {
219
+ return 1;
220
+ }
221
+ return 0;
222
+ });
223
+ }
224
+ function resumeHostQueue(host) {
225
+ HOST.DONE.set(host, HOST.PID++);
226
+ host[kQueued] = false;
227
+ }
228
+ function updateHostQueue(host, position, priority, broadcast) {
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 });
231
+ }
232
+ }
233
+ function asPosix(value) {
234
+ value = path.normalize(value.trim());
235
+ return PLATFORM_WIN32 ? value.replace(/\\/g, '/') : value;
236
+ }
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;
240
+ class Host extends module_1.default {
241
+ static purgeMemory(percent = 1, limit = 0, parent) {
242
+ return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
243
+ }
244
+ static loadSettings(settings, permission, password) {
245
+ if (typeof permission === 'string') {
246
+ password = permission;
247
+ permission = undefined;
248
+ }
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
+ }
267
+ }
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 };
272
+ }
273
+ if ((0, types_1.isPlainObject)(mini)) {
274
+ try {
275
+ MINIMATCH = require('minimatch');
276
+ MINIMATCH_OPTIONS = { matchBase: true, nocase: PLATFORM_WIN32, ...mini };
277
+ }
278
+ catch {
279
+ }
280
+ }
281
+ }
282
+ }
283
+ if (proc && (0, types_1.isObject)(proc.thread)) {
284
+ let { limit, expires, queue, admin } = proc.thread;
285
+ if (limit && (limit = Math.floor(+limit)) > 0) {
286
+ HOST.THREAD_LIMIT = limit;
287
+ }
288
+ if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
289
+ HOST.THREAD_EXPIRES = expires;
290
+ }
291
+ if ((0, types_1.isObject)(queue)) {
292
+ let priority;
293
+ ({ limit, expires, priority } = queue);
294
+ if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
295
+ HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
296
+ }
297
+ if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
298
+ HOST.QUEUE_EXPIRES = expires;
299
+ }
300
+ if ((0, types_1.isObject)(priority)) {
301
+ let { min = -1, max = 0, bypass } = priority;
302
+ if ((min = Math.floor(+min)) >= 0) {
303
+ HOST.PRIORITY_MIN = min;
304
+ }
305
+ if ((max = Math.floor(+max)) > 0) {
306
+ HOST.PRIORITY_MAX = max;
307
+ }
308
+ if (HOST.PRIORITY_MAX < HOST.PRIORITY_MIN) {
309
+ HOST.PRIORITY_MAX = HOST.PRIORITY_MIN + 1;
310
+ }
311
+ if (bypass !== undefined && (bypass = Math.floor(+bypass)) && (bypass < HOST.PRIORITY_MIN || bypass > HOST.PRIORITY_MAX)) {
312
+ HOST.PRIORITY_BYPASS = bypass;
313
+ }
314
+ else {
315
+ bypass = HOST.PRIORITY_BYPASS;
316
+ }
317
+ if (bypass !== undefined && bypass >= HOST.PRIORITY_MIN && bypass <= HOST.PRIORITY_MAX) {
318
+ HOST.PRIORITY_BYPASS = undefined;
319
+ }
320
+ }
321
+ }
322
+ if (admin) {
323
+ const cipher = proc.cipher;
324
+ if ((0, types_1.isArray)(admin.users) && (0, types_1.isPlainObject)(cipher) && (HOST.CIPHER_KEY = cipher.key) && (HOST.CIPHER_IV = cipher.iv)) {
325
+ const algorithm = cipher.algorithm || 'aes-256-gcm';
326
+ HOST.ADMIN_USERS = admin.users.map(value => (0, types_1.encryptUTF8)(algorithm, HOST.CIPHER_KEY, HOST.CIPHER_IV, value)).filter(value => value);
327
+ if (HOST.ADMIN_USERS.length) {
328
+ HOST.CIPHER_ALGORITHM = algorithm;
329
+ }
330
+ else {
331
+ HOST.CIPHER_ALGORITHM = null;
332
+ HOST.CIPHER_KEY = null;
333
+ HOST.CIPHER_IV = null;
334
+ }
335
+ }
336
+ if (typeof admin.private === 'boolean') {
337
+ HOST.ADMIN_PRIVATE = admin.private;
338
+ }
339
+ }
340
+ }
341
+ return true;
342
+ }
343
+ return false;
344
+ }
345
+ static createPermission(all, freeze) {
346
+ const permission = new Permission(freeze);
347
+ if (all) {
348
+ permission.setDiskRead('**/*');
349
+ permission.setDiskWrite('**/*');
350
+ permission.setUNCRead('**/*');
351
+ permission.setUNCWrite('**/*');
352
+ }
353
+ return permission;
354
+ }
355
+ static isPermission(value) {
356
+ return value instanceof Permission;
357
+ }
358
+ static kill(username, iv, pid) {
359
+ let result = 0;
360
+ if (HOST.ADMIN_USERS.includes(encryptUsername(username, iv))) {
361
+ for (const [host, id] of HOST.DONE) {
362
+ if (pid === true || id === pid || Array.isArray(pid) && pid.includes(id)) {
363
+ cancelThread(host, 'pid: ' + id.toString());
364
+ closeThread(host);
365
+ ++result;
366
+ }
367
+ }
368
+ }
369
+ return result;
370
+ }
371
+ static getThreadCount(username, iv) {
372
+ let full;
373
+ switch (typeof username) {
374
+ case 'boolean':
375
+ full = username;
376
+ username = undefined;
377
+ break;
378
+ case 'string':
379
+ if (!iv || !HOST.ADMIN_USERS.includes(encryptUsername(username, iv))) {
380
+ username = undefined;
381
+ }
382
+ full = true;
383
+ break;
384
+ default:
385
+ username = undefined;
386
+ break;
387
+ }
388
+ if (HOST.ADMIN_PRIVATE && !username) {
389
+ return full === true ? { count: -1 } : -1;
390
+ }
391
+ const current = Date.now();
392
+ const items = [];
393
+ for (const [host, pid] of HOST.DONE) {
394
+ if (host.done) {
395
+ closeThread(host);
396
+ }
397
+ else if (host.startTime + HOST.THREAD_EXPIRES >= current) {
398
+ 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.abort();
400
+ closeThread(host);
401
+ }
402
+ else {
403
+ items.push([host, pid]);
404
+ }
405
+ }
406
+ const count = items.length;
407
+ if (full === true) {
408
+ const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
409
+ if (count) {
410
+ if (count > 1) {
411
+ items.sort((a, b) => a[0].startTime - b[0].startTime);
412
+ result.startTime = items[0][0].startTime;
413
+ }
414
+ result.previousTime = items[count - 1][0].startTime;
415
+ if (username) {
416
+ result.admin = items.map(([host, pid]) => {
417
+ const { startTime, username: user, sessionId, broadcastId } = host;
418
+ const thread = { pid, startTime };
419
+ if (user) {
420
+ thread.username = user;
421
+ }
422
+ if (sessionId) {
423
+ thread.sessionId = sessionId;
424
+ }
425
+ if (broadcastId) {
426
+ thread.broadcastId = broadcastId;
427
+ }
428
+ return thread;
429
+ });
430
+ }
431
+ }
432
+ if (HOST.THREAD_LIMIT !== Infinity) {
433
+ result.max = HOST.THREAD_LIMIT;
434
+ }
435
+ return result;
436
+ }
437
+ return count;
438
+ }
439
+ static getPermissionFromSettings() {
440
+ return PERMISSION;
441
+ }
442
+ constructor(config = {}) {
443
+ super();
444
+ this.restartable = true;
445
+ this.modules = new Set();
446
+ this.subProcesses = new Set();
447
+ this.startTime = Date.now();
448
+ this._threadable = true;
449
+ this._logExclude = false;
450
+ this._logLevel = 0;
451
+ this._usingObjects = new Set();
452
+ this[_a] = false;
453
+ this[_b] = false;
454
+ this[_c] = '';
455
+ const { broadcastId, log, username, priority } = config;
456
+ if ((0, types_1.isString)(username)) {
457
+ this[kUsername] = username;
458
+ }
459
+ if (broadcastId) {
460
+ this.broadcastId = broadcastId;
461
+ }
462
+ if (log !== undefined) {
463
+ if ((0, types_1.isPlainObject)(log)) {
464
+ const { enabled, level, exclude, useColor } = log;
465
+ if (level !== undefined) {
466
+ this.logLevel = level;
467
+ }
468
+ if (exclude) {
469
+ this.ignoreLog(exclude);
470
+ }
471
+ else if (typeof enabled === 'boolean') {
472
+ this.ignoreLog(!enabled);
473
+ }
474
+ if (useColor) {
475
+ this.supports('stripAnsi', false);
476
+ }
477
+ }
478
+ else {
479
+ this.ignoreLog(typeof log === 'boolean' ? !log : log);
480
+ }
481
+ }
482
+ this[kConfig] = Object.freeze(config);
483
+ if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
484
+ HOST.DONE.set(this, HOST.PID++);
485
+ }
486
+ else if (typeof priority === 'number' && priority >= HOST.PRIORITY_MIN && priority <= HOST.PRIORITY_MAX && HOST.QUEUE.length < HOST.QUEUE_LIMIT && typeof this.resumeThread === 'function') {
487
+ this[kQueued] = true;
488
+ addHostQueue({ instance: this, priority, startTime: Date.now() });
489
+ updateHostQueue(this, HOST.QUEUE.findIndex(item => item.instance === this), priority);
490
+ }
491
+ else {
492
+ cancelThread(this, 'limit: ' + HOST.THREAD_LIMIT.toString(), true);
493
+ }
494
+ }
495
+ restart(...args) { }
496
+ isFatal(err) {
497
+ const fatal = this.config.error?.fatal;
498
+ return fatal ?? super.isFatal(err);
499
+ }
500
+ using(...items) {
501
+ const data = this._usingObjects;
502
+ if (typeof items[0] === 'boolean' && items.shift()) {
503
+ data.clear();
504
+ }
505
+ items.forEach(item => data.add(item));
506
+ return this;
507
+ }
508
+ contains(item, condition) {
509
+ const data = this._usingObjects;
510
+ if (data.size) {
511
+ if (data.has(item)) {
512
+ return true;
513
+ }
514
+ if (condition) {
515
+ for (const other of data) {
516
+ if (condition(other, item)) {
517
+ return true;
518
+ }
519
+ }
520
+ }
521
+ }
522
+ return false;
523
+ }
524
+ find(name) {
525
+ for (const instance of this.modules) {
526
+ if (instance.moduleName === name) {
527
+ return instance;
528
+ }
529
+ }
530
+ for (const instance of this.subProcesses) {
531
+ if (instance.moduleName === name) {
532
+ return instance;
533
+ }
534
+ }
535
+ }
536
+ findAll(name) {
537
+ const result = [];
538
+ for (const instance of this.modules) {
539
+ if (instance.moduleName === name) {
540
+ result.push(instance);
541
+ }
542
+ }
543
+ for (const instance of this.subProcesses) {
544
+ if (instance.moduleName === name) {
545
+ result.push(instance);
546
+ }
547
+ }
548
+ return result;
549
+ }
550
+ willLog(name) {
551
+ const value = this._logExclude;
552
+ return !(typeof value === 'boolean' ? value : value.includes(name));
553
+ }
554
+ ignoreLog(values) {
555
+ if (typeof values === 'string') {
556
+ values = [values];
557
+ }
558
+ if (Array.isArray(values)) {
559
+ if (!Array.isArray(this._logExclude)) {
560
+ this._logExclude = [];
561
+ }
562
+ this._logExclude.push(...values);
563
+ this._logEnabled = this.willLog(this.moduleName);
564
+ }
565
+ else if (values) {
566
+ this._logExclude = true;
567
+ this._logEnabled = false;
568
+ }
569
+ else {
570
+ this._logExclude = false;
571
+ this._logEnabled = true;
572
+ }
573
+ }
574
+ collectLog(level) {
575
+ const result = this.status.slice(0);
576
+ for (const item of this.modules) {
577
+ if (item.status.length) {
578
+ result.push(...item.status);
579
+ }
580
+ }
581
+ for (const item of this.subProcesses) {
582
+ if (item.status.length) {
583
+ result.push(...item.status);
584
+ }
585
+ }
586
+ result.sort(level ? (a, b) => b.type - a.type : (a, b) => a.timeStamp - b.timeStamp);
587
+ return result;
588
+ }
589
+ retain(process) {
590
+ this.subProcesses.add(process);
591
+ process.host = this;
592
+ }
593
+ release(process, log = true) {
594
+ if (this.subProcesses.delete(process)) {
595
+ if (log) {
596
+ const status = process.status;
597
+ if (status.length) {
598
+ this.status.push(...status);
599
+ status.length = 0;
600
+ }
601
+ process.flushLog();
602
+ }
603
+ process.host = null;
604
+ return true;
605
+ }
606
+ return false;
607
+ }
608
+ loadModule(name, ...args) {
609
+ for (const instance of this.modules) {
610
+ if (instance.moduleName === name) {
611
+ return instance;
612
+ }
613
+ }
614
+ return null;
615
+ }
616
+ joinQueue(options) {
617
+ const index = HOST.QUEUE.findIndex(item => item.instance === this);
618
+ if (index !== -1) {
619
+ const queue = HOST.QUEUE[index];
620
+ let args, reject;
621
+ if (options) {
622
+ ({ args, reject } = options);
623
+ }
624
+ if (reject) {
625
+ HOST.QUEUE.splice(index, 1);
626
+ resumeHostQueue(this);
627
+ return false;
628
+ }
629
+ if (Array.isArray(args)) {
630
+ queue.args = args;
631
+ }
632
+ if (!queue.joined) {
633
+ queue.joined = true;
634
+ ++HOST.QUEUED;
635
+ }
636
+ return true;
637
+ }
638
+ return false;
639
+ }
640
+ set host(value) { }
641
+ get host() {
642
+ return null;
643
+ }
644
+ get config() {
645
+ return this[kConfig];
646
+ }
647
+ get username() {
648
+ return this[kUsername];
649
+ }
650
+ set done(value) {
651
+ if (value) {
652
+ this[kDone] = true;
653
+ closeThread(this);
654
+ }
655
+ }
656
+ get done() {
657
+ return this[kDone] || this.aborted;
658
+ }
659
+ get queued() {
660
+ return this[kQueued];
661
+ }
662
+ get errorCount() {
663
+ return Array.from(this.modules).reduce((a, b) => a + b.errors.length, this.errors.length);
664
+ }
665
+ }
666
+ exports.Host = Host;
667
+ _a = kDone, _b = kQueued, _c = kUsername;
668
+ class Client extends module_1.default {
669
+ static purgeMemory(percent = 1, limit = 0, parent) {
670
+ return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
671
+ }
672
+ constructor(data) {
673
+ super();
674
+ this._extensions = null;
675
+ this[_d] = '';
676
+ this.module = (0, types_1.isPlainObject)(data) ? data : {};
677
+ }
678
+ init(...args) {
679
+ const { permission, extensions } = this.module;
680
+ const cache_dir = this.settings.cache_dir;
681
+ if (permission) {
682
+ const result = Permission.create(permission, permission.inherit !== false ? this.host?.permission : null, true);
683
+ if (result) {
684
+ this.permission = result;
685
+ }
686
+ }
687
+ if ((0, types_1.isArray)(extensions)) {
688
+ this._extensions || (this._extensions = extensions.slice(0));
689
+ }
690
+ if (cache_dir) {
691
+ this.cacheDir = cache_dir;
692
+ }
693
+ return this;
694
+ }
695
+ set cacheDir(value) {
696
+ if (path.isAbsolute(value) && module_1.default.isDir(value)) {
697
+ this[kCacheDir] = value;
698
+ }
699
+ }
700
+ get cacheDir() {
701
+ return this[kCacheDir];
702
+ }
703
+ get settings() {
704
+ var _k;
705
+ return ((_k = this.module).settings || (_k.settings = {}));
706
+ }
707
+ set extensions(values) {
708
+ if (Array.isArray(values)) {
709
+ this._extensions = values;
710
+ }
711
+ }
712
+ get extensions() {
713
+ const extensions = this._extensions;
714
+ return extensions ? this._extensions = getFunctions(extensions) : [];
715
+ }
716
+ }
717
+ exports.Client = Client;
718
+ _d = kCacheDir;
719
+ class ClientDb extends Client {
720
+ static get TRANSACTION_ACTIVE() { return 1 /* DB_TRANSACTION.ACTIVE */; }
721
+ static get TRANSACTION_PARTIAL() { return 2 /* DB_TRANSACTION.PARTIAL */; }
722
+ static get TRANSACTION_COMMIT() { return 4 /* DB_TRANSACTION.COMMIT */; }
723
+ static get TRANSACTION_TERMINATE() { return 8 /* DB_TRANSACTION.TERMINATE */; }
724
+ static get TRANSACTION_ABORT() { return 16 /* DB_TRANSACTION.ABORT */; }
725
+ static get TRANSACTION_FAIL() { return 32 /* DB_TRANSACTION.FAIL */; }
726
+ static loadSettings(settings, password) {
727
+ if (super.loadSettings({ process: settings.process }, password)) {
728
+ const memory = settings.memory;
729
+ if ((0, types_1.isPlainObject)(memory)) {
730
+ for (const name in memory) {
731
+ const client = memory[name];
732
+ if ((0, types_1.isPlainObject)(client)) {
733
+ for (const source in client) {
734
+ const item = client[source];
735
+ if ((0, types_1.isPlainObject)(item) && 'enabled' in item) {
736
+ const key = name + '_' + source;
737
+ const stored = CACHE_SOURCE[key];
738
+ let { enabled, percent = 0, limit = 0, min = 0, max = 0 } = item;
739
+ if (enabled && limit > 0) {
740
+ if ((0, types_1.isString)(percent)) {
741
+ percent = percent.includes('%') ? parseFloat(percent) / 100 : parseFloat(percent);
742
+ }
743
+ else if ((percent = Math.max(percent, 0)) > 1) {
744
+ percent /= 100;
745
+ }
746
+ if (percent > 0) {
747
+ (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 };
748
+ }
749
+ }
750
+ else if (stored) {
751
+ delete stored.config;
752
+ }
753
+ }
754
+ }
755
+ }
756
+ }
757
+ }
758
+ return true;
759
+ }
760
+ return false;
761
+ }
762
+ static purgeMemory(percent = 1, limit = 0, parent) {
763
+ return parent ? super.purgeMemory(percent, limit, parent) : Promise.resolve(0);
764
+ }
765
+ static getTimeout(value) {
766
+ if (value === undefined) {
767
+ return 0;
768
+ }
769
+ let result = 0;
770
+ switch (typeof value) {
771
+ case 'number':
772
+ result = value;
773
+ break;
774
+ case 'string':
775
+ result = convertSeconds(value);
776
+ break;
777
+ case 'object':
778
+ if (value && typeof (result = value.timeout) === 'string') {
779
+ result = convertSeconds(value.timeout);
780
+ value.timeout = result;
781
+ }
782
+ break;
783
+ }
784
+ return result > 0 ? result : 0;
785
+ }
786
+ static convertTime(value) {
787
+ let result = 0;
788
+ switch (typeof value) {
789
+ case 'number':
790
+ result = value * 1000 /* TIME.S */;
791
+ break;
792
+ case 'string':
793
+ result = (0, types_1.parseTime)(value);
794
+ break;
795
+ }
796
+ return result > 0 ? result : 0;
797
+ }
798
+ static findResult(source, credential, queryString, timeout, sessionKey, renewCache) {
799
+ const userKey = this.extractUUID(credential) || this.asHash(this.asString(credential));
800
+ if (userKey) {
801
+ queryString = this.asHash(queryString);
802
+ if (timeout > 0) {
803
+ const userCache = CACHE_USER[source]?.[userKey];
804
+ let stored;
805
+ if (userCache && (stored = userCache[queryString])) {
806
+ const [expires, result] = stored;
807
+ if (result) {
808
+ const current = Date.now();
809
+ let valid;
810
+ if (renewCache) {
811
+ stored[0] = expireTime(timeout);
812
+ valid = true;
813
+ }
814
+ else if (current < expires) {
815
+ valid = true;
816
+ }
817
+ if (valid) {
818
+ stored[2] = current;
819
+ incrementSourceCount(source, stored, userCache, queryString);
820
+ if (Array.isArray(result)) {
821
+ return result;
822
+ }
823
+ try {
824
+ return JSON.parse(fs.readFileSync(result, 'utf-8'));
825
+ }
826
+ catch {
827
+ }
828
+ }
829
+ clearSourceResult(stored);
830
+ }
831
+ deleteSourceResult(source, stored);
832
+ delete userCache[queryString];
833
+ }
834
+ }
835
+ else if (sessionKey) {
836
+ return CACHE_SESSION[source]?.[userKey + sessionKey]?.[queryString];
837
+ }
838
+ }
839
+ }
840
+ static storeResult(source, credential, queryString, result, options, sessionKey, sessionExpires) {
841
+ var _k, _l;
842
+ let cache, cacheDir;
843
+ if ((0, types_1.isObject)(sessionKey)) {
844
+ ({ cacheDir, sessionKey, sessionExpires } = sessionKey);
845
+ }
846
+ else if ((0, types_1.isObject)(options) && 'cache' in options) {
847
+ ({ cache, cacheDir, sessionKey, sessionExpires } = options);
848
+ }
849
+ let timeout = 0, userKey, whenEmpty, partition;
850
+ if (cache) {
851
+ switch (typeof cache) {
852
+ case 'number':
853
+ timeout = cache;
854
+ break;
855
+ case 'string':
856
+ if (typeof sessionKey === 'number') {
857
+ sessionExpires = sessionKey;
858
+ sessionKey = cache;
859
+ break;
860
+ }
861
+ timeout = convertSeconds(cache);
862
+ break;
863
+ case 'object': {
864
+ let seconds, dir;
865
+ ({ timeout: seconds, when_empty: whenEmpty, dir } = cache);
866
+ if ((0, types_1.isString)(dir) && (!cacheDir || module_1.default.isDir(dir))) {
867
+ cacheDir = dir;
868
+ partition = true;
869
+ }
870
+ if (typeof seconds === 'number') {
871
+ timeout = seconds;
872
+ }
873
+ else {
874
+ cache.timeout = seconds ? convertSeconds(seconds) : 0;
875
+ }
876
+ break;
877
+ }
878
+ }
879
+ }
880
+ if ((result.length || whenEmpty) && (userKey = this.extractUUID(credential) || this.asHash(this.asString(credential)))) {
881
+ queryString = this.asHash(queryString);
882
+ if (timeout > 0) {
883
+ const item = [expireTime(timeout), result, Date.now(), 0];
884
+ ((_k = (CACHE_USER[source] || (CACHE_USER[source] = {})))[userKey] || (_k[userKey] = {}))[queryString] = item;
885
+ addSourceResult(source, item);
886
+ if (cacheDir) {
887
+ if (partition) {
888
+ const subDir = path.join(cacheDir, (STORE_RESULT_COUNT++ % this.STORE_RESULT_PARTITION_SIZE).toString(), queryString.substring(0, this.STORE_RESULT_PARTITION_MULT));
889
+ if (this.createDir(subDir)) {
890
+ cacheDir = subDir;
891
+ }
892
+ }
893
+ const pathname = path.join(cacheDir, queryString);
894
+ fs.writeFile(pathname, JSON.stringify(result), err => {
895
+ if (!err) {
896
+ item[1] = pathname;
897
+ }
898
+ });
899
+ }
900
+ }
901
+ else if (typeof sessionKey === 'string' && sessionExpires && sessionExpires > 0) {
902
+ const dbKey = userKey + sessionKey;
903
+ ((_l = (CACHE_SESSION[source] || (CACHE_SESSION[source] = {})))[dbKey] || (_l[dbKey] = {}))[queryString] = result;
904
+ setTimeout(() => delete CACHE_SESSION[dbKey], sessionExpires * 1000 /* TIME.S */);
905
+ }
906
+ }
907
+ return result;
908
+ }
909
+ static purgeResult(prefix) {
910
+ const current = Date.now();
911
+ const providers = new Map();
912
+ if (prefix) {
913
+ if (prefix.indexOf('_') === -1) {
914
+ prefix += '_';
915
+ for (const key in CACHE_USER) {
916
+ if (key.startsWith(prefix)) {
917
+ providers.set(key, CACHE_USER[key]);
918
+ }
919
+ }
920
+ }
921
+ else {
922
+ const source = CACHE_USER[prefix];
923
+ if (source) {
924
+ providers.set(prefix, source);
925
+ }
926
+ }
927
+ }
928
+ else {
929
+ for (const key in CACHE_USER) {
930
+ providers.set(key, CACHE_USER[key]);
931
+ }
932
+ }
933
+ let result = 0;
934
+ for (const [source, data] of providers) {
935
+ for (const user in data) {
936
+ const queries = data[user];
937
+ for (const query in queries) {
938
+ const item = queries[query];
939
+ if (current >= item[0]) {
940
+ ++result;
941
+ delete queries[query];
942
+ deleteSourceResult(source, item);
943
+ if (typeof item[1] === 'string') {
944
+ fs.unlink(item[1], () => { });
945
+ }
946
+ }
947
+ }
948
+ if (Object.keys(queries).length === 0) {
949
+ delete data[user];
950
+ }
951
+ }
952
+ }
953
+ return Promise.resolve(result);
954
+ }
955
+ static setPoolConfig(value) { }
956
+ static getPoolConfig(source) { }
957
+ static extractUUID(credential) {
958
+ if (typeof credential === 'string') {
959
+ return (0, types_1.validateUUID)(credential) ? credential : '';
960
+ }
961
+ let result;
962
+ return (0, types_1.isObject)(credential) && (result = credential.uuidKey) && (0, types_1.validateUUID)(result) ? result : '';
963
+ }
964
+ static keyOfResult(source, credential, uuidOnly) {
965
+ const result = this.extractUUID(credential);
966
+ if (uuidOnly) {
967
+ return result ? source + '_' + result : '';
968
+ }
969
+ return source + '_' + (result || this.asString(credential, true));
970
+ }
971
+ constructor(data, database = []) {
972
+ super(data);
973
+ this.database = database;
974
+ this.cacheExpires = 600000 /* VALUES.DB_EXPIRES */;
975
+ }
976
+ init(config) {
977
+ let { cache_dir, session_expires: expires } = this.settings;
978
+ if (cache_dir) {
979
+ this.cacheDir = cache_dir;
980
+ }
981
+ if (expires !== undefined) {
982
+ if (typeof expires === 'string') {
983
+ if (!isNaN(expires = (0, types_1.parseTime)(expires))) {
984
+ this.settings.session_expires = expires;
985
+ }
986
+ else {
987
+ delete this.settings.cache_session;
988
+ }
989
+ }
990
+ if (expires >= 0) {
991
+ this.cacheExpires = expires;
992
+ }
993
+ }
994
+ return super.init();
995
+ }
996
+ add(item, state) {
997
+ if (state) {
998
+ item.transactionState = state;
999
+ }
1000
+ if (!this.database.includes(item)) {
1001
+ this.database.push(item);
1002
+ }
1003
+ }
1004
+ hasCache(source, sessionKey, override) {
1005
+ return (0, types_1.isString)(sessionKey) || ClientDb.getTimeout(override ?? this.settingsOf(source, 'cache')) > 0;
1006
+ }
1007
+ hasCoerce(source, component, override, credential) {
1008
+ if (override) {
1009
+ if (typeof override === 'string') {
1010
+ credential = override;
1011
+ override = undefined;
1012
+ }
1013
+ else if (!credential && (0, types_1.isPlainObject)(override) && 'uuidKey' in override) {
1014
+ credential = override.uuidKey;
1015
+ override = undefined;
1016
+ }
1017
+ }
1018
+ let uuidKey;
1019
+ if (credential && ((0, types_1.validateUUID)(credential) ? uuidKey = credential : uuidKey = ClientDb.extractUUID(credential))) {
1020
+ const value = this.settingsKey(uuidKey, 'coerce', component);
1021
+ if (value !== undefined) {
1022
+ override = value;
1023
+ }
1024
+ }
1025
+ if (override !== undefined) {
1026
+ if (typeof override === 'boolean') {
1027
+ return override && component === 'options';
1028
+ }
1029
+ if ((0, types_1.isPlainObject)(override) && (override = override[component]) !== undefined) {
1030
+ return override === true;
1031
+ }
1032
+ }
1033
+ return this.settingsOf(source, 'coerce', component) === true || component === 'options' && this.settingsOf(source, 'coerce') === true;
1034
+ }
1035
+ getQueryResult(source, credential, queryString, options, renewCache) {
1036
+ let sessionKey, value;
1037
+ switch (typeof options) {
1038
+ case 'boolean':
1039
+ renewCache = options;
1040
+ break;
1041
+ case 'string':
1042
+ sessionKey = options;
1043
+ break;
1044
+ case 'object':
1045
+ if (options) {
1046
+ ({ value, sessionKey, renewCache } = options);
1047
+ }
1048
+ break;
1049
+ }
1050
+ const uuidKey = ClientDb.extractUUID(credential);
1051
+ if (uuidKey) {
1052
+ const cache = this.settingsKey(uuidKey, 'cache');
1053
+ if (cache !== undefined) {
1054
+ value = cache;
1055
+ }
1056
+ }
1057
+ return ClientDb.findResult(this.moduleName + '_' + source, uuidKey || credential, queryString, value === 0 ? 0 : ClientDb.getTimeout(value || this.settingsOf(source, 'cache')), sessionKey, renewCache);
1058
+ }
1059
+ setQueryResult(source, credential, queryString, result, sessionKey) {
1060
+ if (!Array.isArray(result)) {
1061
+ result = result === undefined ? [] : [result];
1062
+ }
1063
+ if (!(0, types_1.isString)(queryString)) {
1064
+ return result;
1065
+ }
1066
+ const uuidKey = ClientDb.extractUUID(credential);
1067
+ let options;
1068
+ if (ClientDb.enabled("memory.settings.users" /* KEY_NAME.MEMORY_SETTINGS_USERS */, this.host?.username)) {
1069
+ let value;
1070
+ if ((0, types_1.isObject)(sessionKey)) {
1071
+ ({ value, sessionKey } = sessionKey);
1072
+ }
1073
+ if (uuidKey) {
1074
+ const cache = this.settingsKey(uuidKey, 'cache');
1075
+ if (cache !== undefined) {
1076
+ value = cache;
1077
+ }
1078
+ }
1079
+ options = { cache: value === 0 ? 0 : value || this.settingsOf(source, 'cache'), cacheDir: this.cacheDir, sessionKey, sessionExpires: this.cacheExpires / 1000 };
1080
+ }
1081
+ else {
1082
+ options = { cacheDir: this.cacheDir };
1083
+ }
1084
+ return ClientDb.storeResult(this.moduleName + '_' + source, uuidKey || credential, queryString, result, options);
1085
+ }
1086
+ applyState(items, value, as) {
1087
+ for (const item of Array.isArray(items) ? items : [items]) {
1088
+ if ((0, types_1.hasBit)(value, 8 /* DB_TRANSACTION.TERMINATE */)) {
1089
+ const state = item.transactionState || (item.transactionState = 0);
1090
+ if ((0, types_1.hasBit)(state, 4 /* DB_TRANSACTION.COMMIT */) || (0, types_1.hasBit)(state, 32 /* DB_TRANSACTION.FAIL */)) {
1091
+ item.transactionState |= value;
1092
+ }
1093
+ }
1094
+ else if ((0, types_1.hasBit)(value, 16 /* DB_TRANSACTION.ABORT */)) {
1095
+ item.transactionState || (item.transactionState = value);
1096
+ }
1097
+ else if (as) {
1098
+ item.transactionState = value;
1099
+ }
1100
+ else {
1101
+ item.transactionState |= value;
1102
+ }
1103
+ }
1104
+ }
1105
+ valueOfKey(credential, name, component) {
1106
+ const uuidKey = ClientDb.extractUUID(credential);
1107
+ if (uuidKey) {
1108
+ return this.settingsKey(uuidKey, name, component);
1109
+ }
1110
+ }
1111
+ settingsOf(source, name, component) {
1112
+ const data = this.settings[source];
1113
+ if ((0, types_1.isObject)(data)) {
1114
+ return getSettingsValue(data, name, component);
1115
+ }
1116
+ }
1117
+ settingsKey(uuidKey, name, component) {
1118
+ var _k;
1119
+ const data = ((_k = this.settings).user_key || (_k.user_key = {}))[uuidKey];
1120
+ if ((0, types_1.isObject)(data)) {
1121
+ return getSettingsValue(data, name, component);
1122
+ }
1123
+ }
1124
+ get pending() {
1125
+ return this.database.filter(item => {
1126
+ const state = item.transactionState || 0;
1127
+ return state === 0 || state === 16 /* DB_TRANSACTION.ABORT */;
1128
+ });
1129
+ }
1130
+ get committed() {
1131
+ return this.database.filter(item => (item.transactionState & 4 /* DB_TRANSACTION.COMMIT */) === 4 /* DB_TRANSACTION.COMMIT */);
1132
+ }
1133
+ get failed() {
1134
+ return this.database.filter(item => (item.transactionState & 32 /* DB_TRANSACTION.FAIL */) === 32 /* DB_TRANSACTION.FAIL */);
1135
+ }
1136
+ }
1137
+ ClientDb.STORE_RESULT_PARTITION_SIZE = 16 /* VALUES.DB_PARTITION_SIZE */;
1138
+ ClientDb.STORE_RESULT_PARTITION_MULT = 2 /* VALUES.DB_PARTITION_MULT */;
1139
+ exports.ClientDb = ClientDb;
1140
+ class AbortComponent {
1141
+ constructor() {
1142
+ this[_e] = new AbortController();
1143
+ }
1144
+ static attach(instance, signal, options) {
1145
+ let map = ABORT_LISTENER.get(instance);
1146
+ if (!map) {
1147
+ ABORT_LISTENER.set(instance, map = []);
1148
+ }
1149
+ if (!map.find(item => item.signal === signal)) {
1150
+ const listener = () => instance.abort();
1151
+ signal.addEventListener('abort', listener, options);
1152
+ map.push({ signal, listener });
1153
+ }
1154
+ }
1155
+ static detach(instance, signal) {
1156
+ const map = ABORT_LISTENER.get(instance);
1157
+ let index;
1158
+ if (map && (index = map.findIndex(item => item.signal === signal)) !== -1) {
1159
+ const target = map[index];
1160
+ target.signal.removeEventListener('abort', target.listener);
1161
+ map.splice(index, 1);
1162
+ }
1163
+ }
1164
+ abort() {
1165
+ if (!this.aborted) {
1166
+ this[kAbortHandler].abort();
1167
+ }
1168
+ }
1169
+ reset() {
1170
+ if (this.aborted) {
1171
+ this[kAbortHandler] = new AbortController();
1172
+ }
1173
+ }
1174
+ get signal() {
1175
+ return this[kAbortHandler].signal;
1176
+ }
1177
+ get aborted() {
1178
+ return this.signal.aborted;
1179
+ }
1180
+ }
1181
+ exports.AbortComponent = AbortComponent;
1182
+ _e = kAbortHandler;
1183
+ class Permission {
1184
+ static create(settings, parent, freeze) {
1185
+ if (this.validate(settings)) {
1186
+ if (typeof parent === 'boolean') {
1187
+ freeze = parent;
1188
+ parent = null;
1189
+ }
1190
+ const { inherit, disk_read, disk_write, unc_read, unc_write } = settings;
1191
+ const result = new Permission(freeze);
1192
+ if (inherit === false) {
1193
+ parent = null;
1194
+ }
1195
+ let valid;
1196
+ if (readable(disk_read) && (!parent || parent.diskRead)) {
1197
+ result.setDiskRead(disk_read);
1198
+ valid = true;
1199
+ }
1200
+ else if (parent?.diskRead) {
1201
+ result.setDiskRead(parent.getDiskRead());
1202
+ }
1203
+ if (readable(disk_write) && (!parent || parent.diskWrite)) {
1204
+ result.setDiskWrite(disk_write);
1205
+ valid = true;
1206
+ }
1207
+ else if (parent?.diskWrite) {
1208
+ result.setDiskWrite(parent.getDiskWrite());
1209
+ }
1210
+ if (readable(unc_read) && (!parent || parent.uncRead)) {
1211
+ result.setUNCRead(unc_read);
1212
+ valid = true;
1213
+ }
1214
+ else if (parent?.uncRead) {
1215
+ result.setUNCRead(parent.getUNCRead());
1216
+ }
1217
+ if (readable(unc_write) && (!parent || parent.uncWrite)) {
1218
+ result.setUNCWrite(unc_write);
1219
+ valid = true;
1220
+ }
1221
+ else if (parent?.uncWrite) {
1222
+ result.setUNCWrite(parent.getUNCWrite());
1223
+ }
1224
+ if (valid) {
1225
+ return result;
1226
+ }
1227
+ }
1228
+ return null;
1229
+ }
1230
+ static validate(settings) {
1231
+ return (0, types_1.isPlainObject)(settings) && (readable(settings.disk_read) || readable(settings.disk_write) || readable(settings.unc_read) || readable(settings.unc_write));
1232
+ }
1233
+ static clone(permission, freeze) {
1234
+ const result = new Permission(freeze);
1235
+ if (permission.diskRead) {
1236
+ result.setDiskRead(permission.getDiskRead());
1237
+ }
1238
+ if (permission.diskWrite) {
1239
+ result.setDiskWrite(permission.getDiskWrite());
1240
+ }
1241
+ if (permission.uncRead) {
1242
+ result.setUNCRead(permission.getUNCRead());
1243
+ }
1244
+ if (permission.uncWrite) {
1245
+ result.setUNCWrite(permission.getUNCWrite());
1246
+ }
1247
+ return result;
1248
+ }
1249
+ static match(pathname, pattern) {
1250
+ pathname = asPosix(pathname);
1251
+ if (MINIMATCH) {
1252
+ return Array.isArray(pattern) ? pattern.some(target => MINIMATCH(pathname, target, MINIMATCH_OPTIONS)) : MINIMATCH(pathname, pattern, MINIMATCH_OPTIONS);
1253
+ }
1254
+ return pm.isMatch(pathname, pattern, PICOMATCH_OPTIONS);
1255
+ }
1256
+ constructor(freeze = false) {
1257
+ this[_f] = { enabled: null, value: '' };
1258
+ this[_g] = { enabled: null, value: '' };
1259
+ this[_h] = { enabled: null, value: '' };
1260
+ this[_j] = { enabled: null, value: '' };
1261
+ this[kFreeze] = freeze;
1262
+ }
1263
+ setDiskRead(pathname = '', enabled = true) {
1264
+ setPermission.call(this, kDiskRead, pathname, enabled);
1265
+ }
1266
+ setDiskWrite(pathname = '', enabled = true) {
1267
+ setPermission.call(this, kDiskWrite, pathname, enabled);
1268
+ }
1269
+ setUNCRead(pathname = '', enabled = true) {
1270
+ setPermission.call(this, kUncRead, pathname, enabled);
1271
+ }
1272
+ setUNCWrite(pathname = '', enabled = true) {
1273
+ setPermission.call(this, kUncWrite, pathname, enabled);
1274
+ }
1275
+ getDiskRead() {
1276
+ return this[kDiskRead].value;
1277
+ }
1278
+ getDiskWrite() {
1279
+ return this[kDiskWrite].value;
1280
+ }
1281
+ getUNCRead() {
1282
+ return this[kUncRead].value;
1283
+ }
1284
+ getUNCWrite() {
1285
+ return this[kUncWrite].value;
1286
+ }
1287
+ hasDiskRead(pathname) {
1288
+ return matchPermission(this[kDiskRead], pathname);
1289
+ }
1290
+ hasDiskWrite(pathname) {
1291
+ return matchPermission(this[kDiskWrite], pathname);
1292
+ }
1293
+ hasUNCRead(pathname) {
1294
+ return matchPermission(this[kUncRead], pathname);
1295
+ }
1296
+ hasUNCWrite(pathname) {
1297
+ return matchPermission(this[kUncWrite], pathname);
1298
+ }
1299
+ get diskRead() {
1300
+ return this[kDiskRead].enabled === true;
1301
+ }
1302
+ get diskWrite() {
1303
+ return this[kDiskWrite].enabled === true;
1304
+ }
1305
+ get uncRead() {
1306
+ return this[kUncRead].enabled === true;
1307
+ }
1308
+ get uncWrite() {
1309
+ return this[kUncWrite].enabled === true;
1310
+ }
1311
+ }
1312
+ exports.Permission = Permission;
1313
+ _f = kDiskRead, _g = kDiskWrite, _h = kUncRead, _j = kUncWrite;
1314
+ PERMISSION = Host.createPermission(true, true);