mybase 1.1.51 → 1.2.2

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 (46) hide show
  1. package/ip6addr.d.ts +10 -1
  2. package/jest.config.js +8 -1
  3. package/mybase.d.ts +57 -0
  4. package/mybase.js +401 -684
  5. package/mybase.test.ts +647 -0
  6. package/mybase.ts +397 -0
  7. package/package.json +3 -2
  8. package/ts/funcs/Geoip2Paths.js +7 -5
  9. package/ts/funcs/Geoip2Paths.ts +6 -4
  10. package/ts/funcs/asJSON.d.ts +1 -1
  11. package/ts/funcs/asJSON.js +6 -4
  12. package/ts/funcs/asJSON.ts +8 -5
  13. package/ts/funcs/hash_sha512.d.ts +1 -1
  14. package/ts/funcs/hash_sha512.js +4 -4
  15. package/ts/funcs/hash_sha512.ts +3 -4
  16. package/ts/funcs/isLANIp.d.ts +2 -3
  17. package/ts/funcs/isLANIp.js +14 -15
  18. package/ts/funcs/isLANIp.test.ts +7 -8
  19. package/ts/funcs/isLANIp.ts +25 -28
  20. package/ts/funcs/isLoopbackIP.d.ts +2 -3
  21. package/ts/funcs/isLoopbackIP.js +15 -16
  22. package/ts/funcs/isLoopbackIP.test.ts +7 -7
  23. package/ts/funcs/isLoopbackIP.ts +21 -23
  24. package/ts/funcs/validEmail.d.ts +1 -1
  25. package/ts/funcs/validEmail.js +0 -3
  26. package/ts/funcs/validEmail.ts +1 -3
  27. package/ts/funcs/vaultFill.js +1 -1
  28. package/ts/funcs/vaultFill.ts +1 -1
  29. package/ts/funcs/vaultRead.js +9 -3
  30. package/ts/funcs/vaultRead.ts +8 -3
  31. package/ts/index.d.ts +1 -0
  32. package/ts/index.js +1 -0
  33. package/ts/index.ts +1 -1
  34. package/ts/models/DateIterator.d.ts +33 -0
  35. package/ts/models/DateIterator.js +76 -0
  36. package/ts/models/DateIterator.test.ts +149 -0
  37. package/ts/models/DateIterator.ts +80 -0
  38. package/ts/models/IPAddress.d.ts +13 -13
  39. package/ts/models/IPAddress.ts +4 -4
  40. package/ts/models/OTPGenerator.test.ts +1 -1
  41. package/ts/types.d.ts +35 -0
  42. package/ts/types.js +1 -0
  43. package/ts/types.ts +42 -1
  44. package/tsconfig.jest.json +11 -0
  45. package/tsconfig.json +2 -1
  46. package/types/third-party.d.ts +21 -0
@@ -0,0 +1,80 @@
1
+ import { Unixtime } from "./Unixtime"
2
+
3
+ /**
4
+ * Iterates day-by-day backwards from `start` toward `end` (end-exclusive),
5
+ * emitting each date as a `yyyy{sep}mm{sep}dd` string.
6
+ *
7
+ * @example
8
+ * const it = new DateIterator(Unixtime.now(), Unixtime.now().addDays(-5))
9
+ * it.next() // "20260417"
10
+ * it.next() // "20260416"
11
+ * // ...
12
+ * it.next() // null
13
+ *
14
+ * @example separator
15
+ * new DateIterator(start, end, "-").toArray()
16
+ * // ["2026-04-17", "2026-04-16", ...]
17
+ *
18
+ * @example for..of
19
+ * for (const d of new DateIterator(start, end, "/")) console.log(d)
20
+ */
21
+ export class DateIterator implements Iterable<string> {
22
+ private readonly start: Unixtime
23
+ private readonly end: Unixtime
24
+ private readonly separator: string
25
+ private cursor: Unixtime | null
26
+
27
+ constructor(start: Unixtime, end: Unixtime, separator: string = "") {
28
+ if (start.lessThan(end)) {
29
+ throw new Error('DateIterator: `start` must be newer than or equal to `end`')
30
+ }
31
+ // Defensive clones: callers' instances must not be mutated, and
32
+ // Unixtime.addDays() mutates the underlying Date.
33
+ this.start = start.clone()
34
+ this.end = end.clone()
35
+ this.separator = separator
36
+ this.cursor = this.start.clone()
37
+ }
38
+
39
+ /** Returns the next formatted date going backwards, or `null` when exhausted. */
40
+ next(): string | null {
41
+ if (this.cursor === null) return null
42
+ if (!this.cursor.greaterThan(this.end)) {
43
+ this.cursor = null
44
+ return null
45
+ }
46
+ const value = this.cursor.yyyymmdd(this.separator)
47
+ this.cursor.addDays(-1)
48
+ return value
49
+ }
50
+
51
+ /** Reset the iterator so it can be consumed again from `start`. */
52
+ reset(): this {
53
+ this.cursor = this.start.clone()
54
+ return this
55
+ }
56
+
57
+ /** Collect all remaining dates into an array. */
58
+ toArray(): string[] {
59
+ const out: string[] = []
60
+ let d: string | null
61
+ while ((d = this.next()) !== null) out.push(d)
62
+ return out
63
+ }
64
+
65
+ [Symbol.iterator](): Iterator<string> {
66
+ let cursor: Unixtime | null = this.start.clone()
67
+ const end = this.end
68
+ const sep = this.separator
69
+ return {
70
+ next(): IteratorResult<string> {
71
+ if (cursor === null || !cursor.greaterThan(end)) {
72
+ return { value: undefined as unknown as string, done: true }
73
+ }
74
+ const value = cursor.yyyymmdd(sep)
75
+ cursor.addDays(-1)
76
+ return { value, done: false }
77
+ }
78
+ }
79
+ }
80
+ }
@@ -1,15 +1,15 @@
1
- import ip6addr, { ToStringOpts } from '@7c/node-ip6addr';
1
+ import { CIDR, ToStringOpts } from '@7c/node-ip6addr';
2
2
  export declare class IPAddress {
3
3
  private _ip;
4
- static loopback_cidrs4: ip6addr.CIDR[];
5
- static loopback_cidrs6: ip6addr.CIDR[];
6
- static loopback_cidrs: ip6addr.CIDR[];
7
- static lan_cidrs4: ip6addr.CIDR[];
8
- static lan_cidrs6: ip6addr.CIDR[];
9
- static lan_cidrs: ip6addr.CIDR[];
10
- static local_cidrs: ip6addr.CIDR[];
11
- static local_cidrs4: ip6addr.CIDR[];
12
- static local_cidrs6: ip6addr.CIDR[];
4
+ static loopback_cidrs4: CIDR[];
5
+ static loopback_cidrs6: CIDR[];
6
+ static loopback_cidrs: CIDR[];
7
+ static lan_cidrs4: CIDR[];
8
+ static lan_cidrs6: CIDR[];
9
+ static lan_cidrs: CIDR[];
10
+ static local_cidrs: CIDR[];
11
+ static local_cidrs4: CIDR[];
12
+ static local_cidrs6: CIDR[];
13
13
  constructor(ip: string);
14
14
  abbreviated(): string;
15
15
  toString(opts?: ToStringOpts): string;
@@ -20,13 +20,13 @@ export declare class IPAddress {
20
20
  isLoopbackIP(): boolean;
21
21
  isLANIP(): boolean;
22
22
  isLocalIP(): boolean;
23
- partOfCIDR(cidr_networks: ip6addr.CIDR[]): boolean;
24
- static partOfCIDR(_ip: string, cidr_networks: ip6addr.CIDR[]): boolean;
23
+ partOfCIDR(cidr_networks: CIDR[]): boolean;
24
+ static partOfCIDR(_ip: string, cidr_networks: CIDR[]): boolean;
25
25
  isPublicIP(): boolean;
26
26
  static unserialize(serializedIpString: string): IPAddress;
27
27
  static randomIP4(): IPAddress;
28
28
  static randomIP6(): IPAddress;
29
- static randomCIDRIp(cidr: ip6addr.CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress;
29
+ static randomCIDRIp(cidr: CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress;
30
30
  serialize(): string;
31
31
  kind(): number;
32
32
  equals(secondIp: IPAddress): boolean;
@@ -1,7 +1,7 @@
1
1
  /// <reference path="./../../ip6addr.d.ts" />
2
2
  import debug from 'debug'
3
3
  import { randomIP, randomIP6, } from "./../"
4
- import ip6addr, { Addr, ToStringOpts } from '@7c/node-ip6addr'
4
+ import ip6addr, { Addr, CIDR, ToStringOpts } from '@7c/node-ip6addr'
5
5
  import net from 'net'
6
6
  const ip6 = require('ip6') // "url": "https://github.com/elgs/ip6"
7
7
  const dbg = debug('_IPAddress')
@@ -103,7 +103,7 @@ export class IPAddress {
103
103
  return this.partOfCIDR(IPAddress.local_cidrs)
104
104
  }
105
105
 
106
- partOfCIDR(cidr_networks: ip6addr.CIDR[]): boolean {
106
+ partOfCIDR(cidr_networks: CIDR[]): boolean {
107
107
  const ipNormalized = this.toString()
108
108
  let ipVersion = net.isIPv4(ipNormalized) ? 'ipv4' : 'ipv6'
109
109
  for (let cidr of cidr_networks) {
@@ -116,7 +116,7 @@ export class IPAddress {
116
116
  return false
117
117
  }
118
118
 
119
- static partOfCIDR(_ip: string, cidr_networks: ip6addr.CIDR[]): boolean {
119
+ static partOfCIDR(_ip: string, cidr_networks: CIDR[]): boolean {
120
120
  const ip = IPAddress.try(_ip)
121
121
  if (ip === undefined) return false
122
122
  return ip.partOfCIDR(cidr_networks)
@@ -151,7 +151,7 @@ export class IPAddress {
151
151
  }
152
152
 
153
153
 
154
- public static randomCIDRIp(cidr: ip6addr.CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress {
154
+ public static randomCIDRIp(cidr: CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress {
155
155
  if (cidr === 'loopback') cidr = IPAddress.loopback_cidrs
156
156
  if (cidr === 'loopback4') cidr = IPAddress.loopback_cidrs4
157
157
  if (cidr === 'loopback6') cidr = IPAddress.loopback_cidrs6
@@ -15,7 +15,7 @@ describe('OTPGenerator', () => {
15
15
  it('should generate different OTPs in milliseconds', async () => {
16
16
  const otpGenerator = new OTPGenerator(passkey, 5);
17
17
  const otp1 = otpGenerator.generateOTP()
18
- await wait(1/1000);
18
+ await wait(3/1000);
19
19
  const otp2 = otpGenerator.generateOTP();
20
20
  expect(otp1).not.toBe(otp2);
21
21
  })
package/ts/types.d.ts CHANGED
@@ -1 +1,36 @@
1
1
  export type UnixtimeShort = number;
2
+ export type MysqlConfig = {
3
+ host: string;
4
+ port?: number;
5
+ login: string;
6
+ password: string;
7
+ db: string;
8
+ };
9
+ export type MysqlConnection = {
10
+ connect: (callback: (err: NodeJS.ErrnoException | null) => void) => void;
11
+ ping: (callback: (err?: NodeJS.ErrnoException | Error | null) => void) => void;
12
+ };
13
+ export type MysqlConnector = {
14
+ createConnection: (opts: {
15
+ host: string;
16
+ port: number;
17
+ user: string;
18
+ password: string;
19
+ database: string;
20
+ }) => MysqlConnection;
21
+ };
22
+ export type VaultClient = {
23
+ endpoint?: string;
24
+ read: (key: string) => Promise<{
25
+ data?: unknown;
26
+ }>;
27
+ };
28
+ export type SqlQueryable = {
29
+ query: (q: string, values: unknown[], callback: (err: Error | null, res?: unknown) => void) => void;
30
+ };
31
+ export type Geoip2PathMap = {
32
+ country: string | false;
33
+ city: string | false;
34
+ isp: string | false;
35
+ ct: string | false;
36
+ };
package/ts/types.js CHANGED
@@ -1,3 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ //#endregion
3
4
  //# sourceMappingURL=types.js.map
package/ts/types.ts CHANGED
@@ -1 +1,42 @@
1
- export type UnixtimeShort = number
1
+ export type UnixtimeShort = number
2
+
3
+ //#region mybase
4
+ export type MysqlConfig = {
5
+ host: string
6
+ port?: number
7
+ login: string
8
+ password: string
9
+ db: string
10
+ }
11
+
12
+ export type MysqlConnection = {
13
+ connect: (callback: (err: NodeJS.ErrnoException | null) => void) => void
14
+ ping: (callback: (err?: NodeJS.ErrnoException | Error | null) => void) => void
15
+ }
16
+
17
+ export type MysqlConnector = {
18
+ createConnection: (opts: {
19
+ host: string
20
+ port: number
21
+ user: string
22
+ password: string
23
+ database: string
24
+ }) => MysqlConnection
25
+ }
26
+
27
+ export type VaultClient = {
28
+ endpoint?: string
29
+ read: (key: string) => Promise<{ data?: unknown }>
30
+ }
31
+
32
+ export type SqlQueryable = {
33
+ query: (q: string, values: unknown[], callback: (err: Error | null, res?: unknown) => void) => void
34
+ }
35
+
36
+ export type Geoip2PathMap = {
37
+ country: string | false
38
+ city: string | false
39
+ isp: string | false
40
+ ct: string | false
41
+ }
42
+ //#endregion
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": true,
5
+ "allowJs": true,
6
+ "types": ["jest", "node"],
7
+ "noImplicitAny": false
8
+ },
9
+ "include": ["**/*.test.ts", "mybase.ts", "ts/**/*.ts"],
10
+ "exclude": ["node_modules"]
11
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
- "compilerOptions": {
2
+ "compilerOptions": {
3
+ // "ignoreDeprecations": "6.0",
3
4
  "target": "es2017",
4
5
  "experimentalDecorators": true,
5
6
  "emitDecoratorMetadata": true,
@@ -0,0 +1,21 @@
1
+ declare module '@7c/validurl' {
2
+ function validURL(host: unknown): boolean
3
+ export = validURL
4
+ }
5
+
6
+ declare module 'ip6' {
7
+ export function normalize(a: string): string
8
+ export function abbreviate(a: string): string
9
+ }
10
+
11
+ declare module 'aes-js' {
12
+ const aesjs: {
13
+ utils: {
14
+ utf8: { toBytes(s: string): Uint8Array; fromBytes(b: Uint8Array): string }
15
+ hex: { fromBytes(b: Uint8Array): string; toBytes(s: string): Uint8Array }
16
+ }
17
+ padding: { pkcs7: { pad(b: Uint8Array, n: number): Uint8Array; strip(b: Uint8Array): Uint8Array } }
18
+ ModeOfOperation: { cbc: new (key: Uint8Array) => { encrypt(b: Uint8Array): Uint8Array; decrypt(b: Uint8Array): Uint8Array } }
19
+ }
20
+ export = aesjs
21
+ }