@e-mc/core 0.10.6 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +14 -12
  3. package/index.js +50 -18
  4. package/package.json +4 -3
package/LICENSE CHANGED
@@ -8,4 +8,4 @@ Redistribution and use in source and binary forms, with or without modification,
8
8
 
9
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
10
 
11
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @e-mc/core
2
2
 
3
- * NodeJS 16
4
- * ES2020
3
+ * NodeJS 16 LTS
4
+ * ES2021
5
5
 
6
6
  ## General Usage
7
7
 
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.10.6/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.11.1/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { DataSource, LogStatus } from "./squared";
@@ -50,6 +50,8 @@ interface IHost extends IModule {
50
50
  get host(): null;
51
51
  get config(): Readonly<HostInitConfig>;
52
52
  get username(): string;
53
+ get ipV4(): string;
54
+ get ipV6(): string;
53
55
  set done(value);
54
56
  get done(): boolean;
55
57
  get queued(): boolean;
@@ -93,7 +95,7 @@ interface IClientDb extends IClient<IHost, ClientModule<ClientDbSettings>> {
93
95
  cacheExpires: number;
94
96
  add(item: DataSource, state?: number): void;
95
97
  hasCache(source: string, sessionKey?: string): boolean;
96
- hasCoerce(source: string, component: keyof DbCoerceSettings, uuidKey: Undef<string>): boolean;
98
+ hasCoerce(source: string, component: keyof DbCoerceSettings, uuidKey: string | undefined): boolean;
97
99
  hasCoerce(source: string, component: keyof DbCoerceSettings, credential?: unknown): boolean;
98
100
  getQueryResult(source: string, credential: unknown, queryString: string, renewCache: boolean): QueryResult | undefined;
99
101
  getQueryResult(source: string, credential: unknown, queryString: string, sessionKey?: string, renewCache?: boolean): QueryResult | undefined;
@@ -230,14 +232,14 @@ NOTE: **@e-mc/core** is mostly a collection of abstract base classes which canno
230
232
 
231
233
  ## References
232
234
 
233
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/squared.d.ts
234
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/core.d.ts
235
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/db.d.ts
236
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/dom.d.ts
237
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/logger.d.ts
238
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/node.d.ts
239
- - https://www.unpkg.com/@e-mc/types@0.10.6/lib/settings.d.ts
235
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/squared.d.ts
236
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/core.d.ts
237
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/db.d.ts
238
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/dom.d.ts
239
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/logger.d.ts
240
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/node.d.ts
241
+ - https://www.unpkg.com/@e-mc/types@0.11.1/lib/settings.d.ts
240
242
 
241
243
  ## LICENSE
242
244
 
243
- BSD 3-Clause
245
+ BSD 3-Clause
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3
3
  exports.Module = exports.Permission = exports.AbortComponent = exports.ClientDb = exports.Client = exports.Host = void 0;
4
- const path = require("path");
5
- const fs = require("fs");
4
+ const path = require("node:path");
5
+ const fs = require("node:fs");
6
6
  const pm = require("picomatch");
7
+ const ipaddr = require("ipaddr.js");
7
8
  const types_1 = require("@e-mc/types");
8
9
  const module_1 = require("@e-mc/module");
9
10
  exports.Module = module_1;
@@ -20,7 +21,7 @@ const kUncRead = Symbol('uncRead');
20
21
  const kUncWrite = Symbol('uncWrite');
21
22
  const CACHE_USER = {};
22
23
  const CACHE_SOURCE = {};
23
- const CACHE_SESSION = {};
24
+ const CACHE_SESSION = Object.create(null);
24
25
  const ABORT_LISTENER = new WeakMap();
25
26
  const HOST = {
26
27
  DONE: new Map(),
@@ -241,6 +242,29 @@ function asPosix(value) {
241
242
  value = path.normalize(value.trim());
242
243
  return module_1.PLATFORM_WIN32 ? value.replace(/(?:^\\|\\+)/g, '/') : value;
243
244
  }
245
+ function parseIp(value, kind) {
246
+ if (!value) {
247
+ return '';
248
+ }
249
+ try {
250
+ let ip = ipaddr.parse(value);
251
+ if (ip.kind() === 'ipv4') {
252
+ if (kind === 6) {
253
+ ip = ip.toIPv4MappedAddress();
254
+ }
255
+ }
256
+ else if (kind === 4) {
257
+ if (!ip.isIPv4MappedAddress()) {
258
+ return '';
259
+ }
260
+ ip = ip.toIPv4Address();
261
+ }
262
+ return ip.toNormalizedString();
263
+ }
264
+ catch {
265
+ }
266
+ return '';
267
+ }
244
268
  const readable = (value) => (0, types_1.isString)(value) || (0, types_1.isArray)(value);
245
269
  const expireTime = (value) => Date.now() + value * 1000;
246
270
  const convertSeconds = (value) => typeof value === 'string' && Math.ceil((0, types_1.parseTime)(value) / 1000) || 0;
@@ -291,16 +315,18 @@ class Host extends module_1 {
291
315
  }
292
316
  }
293
317
  if (proc?.thread) {
294
- let { limit, expires, queue, admin } = proc.thread;
295
- if (limit && (limit = Math.floor(+limit)) > 0) {
296
- HOST.THREAD_LIMIT = limit;
297
- }
298
- if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
299
- HOST.THREAD_EXPIRES = expires;
318
+ const { queue, admin } = proc.thread;
319
+ {
320
+ let { limit, expires } = proc.thread;
321
+ if (limit && (limit = Math.floor(+limit)) > 0) {
322
+ HOST.THREAD_LIMIT = limit;
323
+ }
324
+ if (expires && (expires = (0, types_1.parseTime)(expires)) > 0) {
325
+ HOST.THREAD_EXPIRES = expires;
326
+ }
300
327
  }
301
328
  if (queue) {
302
- let priority;
303
- ({ limit, expires, priority } = queue);
329
+ let { limit, expires, priority } = queue;
304
330
  if (limit !== undefined && (limit = Math.floor(+limit)) >= -1) {
305
331
  HOST.QUEUE_LIMIT = limit >= 0 ? limit : Infinity;
306
332
  }
@@ -377,7 +403,7 @@ class Host extends module_1 {
377
403
  return result;
378
404
  }
379
405
  static getThreadCount(username, iv) {
380
- let full;
406
+ let full = false;
381
407
  switch (typeof username) {
382
408
  case 'boolean':
383
409
  full = username;
@@ -394,7 +420,7 @@ class Host extends module_1 {
394
420
  break;
395
421
  }
396
422
  if (HOST.ADMIN_PRIVATE && !username) {
397
- return full === true ? { count: -1 } : -1;
423
+ return full ? { count: -1 } : -1;
398
424
  }
399
425
  const current = Date.now();
400
426
  const items = [];
@@ -412,7 +438,7 @@ class Host extends module_1 {
412
438
  }
413
439
  }
414
440
  const count = items.length;
415
- if (full === true) {
441
+ if (full) {
416
442
  const result = { count, pending: HOST.QUEUE.length, opened: HOST.DONE.size, closed: HOST.CLOSED, queued: HOST.QUEUED, rejected: HOST.REJECTED, killed: HOST.KILLED };
417
443
  if (count) {
418
444
  if (count > 1) {
@@ -494,7 +520,7 @@ class Host extends module_1 {
494
520
  else if (log !== undefined) {
495
521
  this.ignoreLog(typeof log === 'boolean' ? !log : log);
496
522
  }
497
- this[kConfig] = Object.freeze(config);
523
+ this[kConfig] = Object.freeze({ ...config });
498
524
  if (HOST.DONE.size < HOST.THREAD_LIMIT || this.username && typeof priority === 'number' && priority === HOST.PRIORITY_BYPASS) {
499
525
  HOST.DONE.set(this, HOST.PID++);
500
526
  if (HOST.LOG_PROGRESS) {
@@ -718,6 +744,12 @@ class Host extends module_1 {
718
744
  get username() {
719
745
  return HOST_USERNAME.get(this) || '';
720
746
  }
747
+ get ipV4() {
748
+ return parseIp(this.config.remoteIp, 4);
749
+ }
750
+ get ipV6() {
751
+ return parseIp(this.config.remoteIp, 6);
752
+ }
721
753
  set done(value) {
722
754
  if (value) {
723
755
  this[kDone] = true;
@@ -918,7 +950,7 @@ class ClientDb extends Client {
918
950
  const [expires, result] = stored;
919
951
  if (result) {
920
952
  const current = Date.now();
921
- let valid;
953
+ let valid = false;
922
954
  if (renewCache) {
923
955
  stored[0] = expireTime(timeout);
924
956
  valid = true;
@@ -955,7 +987,7 @@ class ClientDb extends Client {
955
987
  else if ((0, types_1.isObject)(options) && 'cache' in options) {
956
988
  ({ cache, cacheDir, sessionKey, sessionExpires } = options);
957
989
  }
958
- let timeout = 0, userKey, whenEmpty, partition;
990
+ let timeout = 0, userKey, whenEmpty, partition = false;
959
991
  if (cache) {
960
992
  switch (typeof cache) {
961
993
  case 'number':
@@ -1315,7 +1347,7 @@ class Permission {
1315
1347
  if (inherit === false) {
1316
1348
  parent = null;
1317
1349
  }
1318
- let valid;
1350
+ let valid = false;
1319
1351
  if (readable(disk_read) && (!parent || parent.diskRead)) {
1320
1352
  result.setDiskRead(disk_read);
1321
1353
  valid = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/core",
3
- "version": "0.10.6",
3
+ "version": "0.11.1",
4
4
  "description": "Core modules for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,8 +20,9 @@
20
20
  "license": "BSD-3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.10.6",
24
- "@e-mc/types": "0.10.6",
23
+ "@e-mc/module": "0.11.1",
24
+ "@e-mc/types": "0.11.1",
25
+ "ipaddr.js": "^2.2.0",
25
26
  "picomatch": "^4.0.2"
26
27
  }
27
28
  }