mybase 1.1.46 → 1.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ip6addr.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare module 'ip6addr' {
1
+ declare module '@7c/node-ip6addr' {
2
2
  export interface ToStringOpts {
3
3
  format?: "auto" | "v4" | "v4-mapped" | "v6" | undefined;
4
4
  zeroElide?: boolean | undefined;
package/mybase.js CHANGED
@@ -9,7 +9,7 @@ const chalk = require('chalk')
9
9
  const _validURL = require('@7c/validurl')
10
10
  const validator = require('validator')
11
11
  const sha512 = require('js-sha512')
12
- const ip6addr = require('ip6addr')
12
+ const ip6addr = require('@7c/node-ip6addr')
13
13
 
14
14
  const private_network_cidrs = [ip6addr.createCIDR('10.0.0.0/8')
15
15
  ,ip6addr.createCIDR('172.16.0.0/12')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.46",
3
+ "version": "1.1.49",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -9,6 +9,7 @@
9
9
  "author": "",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
+ "@7c/node-ip6addr": "https://github.com/7c/node-ip6addr.git",
12
13
  "@7c/validurl": "0.0.3",
13
14
  "@types/debug": "^4.1.12",
14
15
  "@types/ip6addr": "^0.2.6",
@@ -21,7 +22,6 @@
21
22
  "debug": "^4.3.4",
22
23
  "ip-range-check": "^0.2.0",
23
24
  "ip6": "=0.2.7",
24
- "ip6addr": "github:7c/node-ip6addr#db6d558af1f314bcb0a733fd61611fa85e1a3eff",
25
25
  "js-sha512": "^0.8.0",
26
26
  "knex": "^3.1.0",
27
27
  "mysql": "^2.18.1",
@@ -33,11 +33,11 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@jest/globals": "^29.7.0",
36
- "@types/jest": "^29.5.11",
36
+ "@types/jest": "^29.5.14",
37
37
  "chai": "^4.2.0",
38
38
  "jest": "^29.7.0",
39
- "mocha": "^8.2.1",
39
+ "mocha": "^11.1.0",
40
40
  "should": "^13.2.3",
41
- "ts-jest": "^29.1.1"
41
+ "ts-jest": "^29.2.5"
42
42
  }
43
43
  }
@@ -1,3 +1,3 @@
1
- import ip6addr from 'ip6addr';
1
+ import ip6addr from '@7c/node-ip6addr';
2
2
  export declare const private_network_cidrs: ip6addr.CIDR[];
3
3
  export declare function isLANIp(ip: string): boolean;
@@ -5,14 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.private_network_cidrs = void 0;
7
7
  exports.isLANIp = isLANIp;
8
- const ip6addr_1 = __importDefault(require("ip6addr"));
8
+ const node_ip6addr_1 = __importDefault(require("@7c/node-ip6addr"));
9
9
  const net_1 = __importDefault(require("net"));
10
10
  exports.private_network_cidrs = [
11
- ip6addr_1.default.createCIDR('10.0.0.0/8'),
12
- ip6addr_1.default.createCIDR('172.16.0.0/12'),
13
- ip6addr_1.default.createCIDR('192.168.0.0/16'),
14
- ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
15
- ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
11
+ node_ip6addr_1.default.createCIDR('10.0.0.0/8'),
12
+ node_ip6addr_1.default.createCIDR('172.16.0.0/12'),
13
+ node_ip6addr_1.default.createCIDR('192.168.0.0/16'),
14
+ node_ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
15
+ node_ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
16
16
  ];
17
17
  function isLANIp(ip) {
18
18
  ip = normalizeIp(ip);
@@ -1,4 +1,4 @@
1
- import ip6addr from 'ip6addr'
1
+ import ip6addr from '@7c/node-ip6addr'
2
2
  import net from 'net'
3
3
 
4
4
  export const private_network_cidrs = [
@@ -1,3 +1,3 @@
1
- import ip6addr from 'ip6addr';
1
+ import ip6addr from '@7c/node-ip6addr';
2
2
  export declare const local_network_cidrs: ip6addr.CIDR[];
3
3
  export declare function isLoopbackIP(ip: string): boolean;
@@ -5,11 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.local_network_cidrs = void 0;
7
7
  exports.isLoopbackIP = isLoopbackIP;
8
- const ip6addr_1 = __importDefault(require("ip6addr"));
8
+ const node_ip6addr_1 = __importDefault(require("@7c/node-ip6addr"));
9
9
  const net_1 = __importDefault(require("net"));
10
10
  exports.local_network_cidrs = [
11
- ip6addr_1.default.createCIDR('127.0.0.0/8'),
12
- ip6addr_1.default.createCIDR('::1/128')
11
+ node_ip6addr_1.default.createCIDR('127.0.0.0/8'),
12
+ node_ip6addr_1.default.createCIDR('::1/128')
13
13
  ];
14
14
  function isLoopbackIP(ip) {
15
15
  ip = normalizeIp(ip);
@@ -1,4 +1,4 @@
1
- import ip6addr from 'ip6addr'
1
+ import ip6addr from '@7c/node-ip6addr'
2
2
  import net from 'net'
3
3
 
4
4
  export const local_network_cidrs = [
@@ -1,4 +1,4 @@
1
- import ip6addr, { ToStringOpts } from 'ip6addr';
1
+ import ip6addr, { ToStringOpts } from '@7c/node-ip6addr';
2
2
  export declare class IPAddress {
3
3
  private _ip;
4
4
  static loopback_cidrs4: ip6addr.CIDR[];
@@ -7,7 +7,7 @@ exports.IPAddress = void 0;
7
7
  /// <reference path="./../../ip6addr.d.ts" />
8
8
  const debug_1 = __importDefault(require("debug"));
9
9
  const __1 = require("./../");
10
- const ip6addr_1 = __importDefault(require("ip6addr"));
10
+ const node_ip6addr_1 = __importDefault(require("@7c/node-ip6addr"));
11
11
  const net_1 = __importDefault(require("net"));
12
12
  const ip6 = require('ip6'); // "url": "https://github.com/elgs/ip6"
13
13
  const dbg = (0, debug_1.default)('_IPAddress');
@@ -18,10 +18,10 @@ class IPAddress {
18
18
  if (ip === '-')
19
19
  ip = '0.0.0.0'; // is this really required?
20
20
  try {
21
- this._ip = ip6addr_1.default.parse(ip);
21
+ this._ip = node_ip6addr_1.default.parse(ip);
22
22
  if (this._ip._attrs.ipv4Mapped) {
23
23
  // we wany to convert it to ipv4
24
- this._ip = ip6addr_1.default.parse(this._ip.toString({ format: 'v4' }));
24
+ this._ip = node_ip6addr_1.default.parse(this._ip.toString({ format: 'v4' }));
25
25
  }
26
26
  }
27
27
  catch (err) {
@@ -157,7 +157,7 @@ class IPAddress {
157
157
  const randomBigInt = BigInt(Math.floor(Math.random() * (Number(range) + 1))) + start;
158
158
  // dbg('randomBigInt', randomBigInt)
159
159
  // Convert the BigInt back to an IP address
160
- const randomIp = ip6addr_1.default.parse(randomBigInt);
160
+ const randomIp = node_ip6addr_1.default.parse(randomBigInt);
161
161
  // dbg('randomCIDRIp', randomIp.toString())
162
162
  return new IPAddress(randomIp.toString());
163
163
  }
@@ -233,20 +233,20 @@ class IPAddress {
233
233
  }
234
234
  exports.IPAddress = IPAddress;
235
235
  IPAddress.loopback_cidrs4 = [
236
- ip6addr_1.default.createCIDR('127.0.0.0/8'),
236
+ node_ip6addr_1.default.createCIDR('127.0.0.0/8'),
237
237
  ];
238
238
  IPAddress.loopback_cidrs6 = [
239
- ip6addr_1.default.createCIDR('::1/128') // yes ipv6 has single address
239
+ node_ip6addr_1.default.createCIDR('::1/128') // yes ipv6 has single address
240
240
  ];
241
241
  IPAddress.loopback_cidrs = IPAddress.loopback_cidrs4.concat(IPAddress.loopback_cidrs6);
242
242
  IPAddress.lan_cidrs4 = [
243
- ip6addr_1.default.createCIDR('10.0.0.0/8'),
244
- ip6addr_1.default.createCIDR('172.16.0.0/12'),
245
- ip6addr_1.default.createCIDR('192.168.0.0/16'),
243
+ node_ip6addr_1.default.createCIDR('10.0.0.0/8'),
244
+ node_ip6addr_1.default.createCIDR('172.16.0.0/12'),
245
+ node_ip6addr_1.default.createCIDR('192.168.0.0/16'),
246
246
  ];
247
247
  IPAddress.lan_cidrs6 = [
248
- ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
249
- ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
248
+ node_ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
249
+ node_ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
250
250
  ];
251
251
  IPAddress.lan_cidrs = IPAddress.lan_cidrs4.concat(IPAddress.lan_cidrs6);
252
252
  IPAddress.local_cidrs = IPAddress.loopback_cidrs.concat(IPAddress.lan_cidrs);
@@ -1,4 +1,4 @@
1
- import ip6addr from 'ip6addr'
1
+ import ip6addr from '@7c/node-ip6addr'
2
2
  import { IPAddress } from './IPAddress'
3
3
  import { randomIP, randomIP6 } from '../'
4
4
 
@@ -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 'ip6addr'
4
+ import ip6addr, { Addr, 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')
@@ -9,7 +9,7 @@ describe('OTPGenerator', () => {
9
9
  const otp = otpGenerator.generateOTP();
10
10
  // length should be 5 + 13
11
11
  expect(otp.length).toBe(18);
12
- console.log(otp);
12
+ // console.log(otp);
13
13
  })
14
14
 
15
15
  it('should generate different OTPs in milliseconds', async () => {
@@ -15,6 +15,7 @@ export declare class Timespan {
15
15
  static days(days: number): Timespan;
16
16
  static weeks(weeks: number): Timespan;
17
17
  static months(months: number): Timespan;
18
+ static years(years: number): Timespan;
18
19
  add(timespan: Timespan): Timespan;
19
20
  subtract(timespan: Timespan): Timespan;
20
21
  toJSON(): {
@@ -47,6 +47,9 @@ class Timespan {
47
47
  static months(months) {
48
48
  return new Timespan(months * 30 * 24 * 60 * 60 * 1000);
49
49
  }
50
+ static years(years) {
51
+ return new Timespan(years * 365 * 24 * 60 * 60 * 1000);
52
+ }
50
53
  add(timespan) {
51
54
  return new Timespan(this.ms + timespan.ms);
52
55
  }
@@ -58,6 +58,10 @@ export class Timespan {
58
58
  return new Timespan(months * 30 * 24 * 60 * 60 * 1000)
59
59
  }
60
60
 
61
+ static years(years: number): Timespan {
62
+ return new Timespan(years * 365 * 24 * 60 * 60 * 1000)
63
+ }
64
+
61
65
  add(timespan: Timespan): Timespan {
62
66
  return new Timespan(this.ms + timespan.ms)
63
67
  }
@@ -22,8 +22,8 @@ export declare class Unixtime {
22
22
  addDays(days?: number): Unixtime;
23
23
  addMonths(months?: number): Unixtime;
24
24
  addYears(years?: number): Unixtime;
25
- yyyymmdd(): string;
26
- yyymmddhh(): string;
25
+ yyyymmdd(seperator?: string): string;
26
+ yyyymmddhh(seperator?: string): string;
27
27
  /**
28
28
  @deprecated use 'day' instead
29
29
  */
@@ -42,6 +42,16 @@ export declare class Unixtime {
42
42
  elapsedSeconds(): number;
43
43
  elapsedMinutes(): number;
44
44
  clone(): Unixtime;
45
+ /**
46
+ * @param max maximum timespan to subtract
47
+ * @returns returns random time in the past within given timespan
48
+ */
49
+ static randomPast(max?: Timespan, base?: Unixtime): Unixtime;
50
+ /**
51
+ * @param max maximum timespan to add
52
+ * @returns returns random time in the future within given timespan
53
+ */
54
+ static randomFuture(max?: Timespan, base?: Unixtime): Unixtime;
45
55
  static fromYYYYMMDD(value: string): Unixtime;
46
56
  static try(value: Date | string | number): Unixtime | null;
47
57
  }
@@ -87,23 +87,23 @@ class Unixtime {
87
87
  this.value.setUTCFullYear(this.year + years);
88
88
  return this;
89
89
  }
90
- yyyymmdd() {
90
+ yyyymmdd(seperator = "") {
91
91
  let mm = this.month;
92
92
  let dd = this.day;
93
93
  return [this.value.getFullYear(),
94
94
  (mm > 9 ? '' : '0') + mm,
95
- (dd > 9 ? '' : '0') + dd
96
- ].join('');
95
+ (dd > 9 ? '' : '0') + dd,
96
+ ].join(seperator);
97
97
  }
98
- yyymmddhh() {
98
+ yyyymmddhh(seperator = "") {
99
99
  let mm = this.month;
100
100
  let dd = this.day;
101
101
  let hh = this.hours;
102
102
  return [this.year,
103
103
  (mm > 9 ? '' : '0') + mm,
104
104
  (dd > 9 ? '' : '0') + dd,
105
- (hh > 9 ? '' : '0') + hh
106
- ].join('');
105
+ (hh > 9 ? '' : '0') + hh,
106
+ ].join(seperator);
107
107
  }
108
108
  /**
109
109
  @deprecated use 'day' instead
@@ -139,6 +139,20 @@ class Unixtime {
139
139
  clone() {
140
140
  return new Unixtime(this.toLongUnixtime());
141
141
  }
142
+ /**
143
+ * @param max maximum timespan to subtract
144
+ * @returns returns random time in the past within given timespan
145
+ */
146
+ static randomPast(max = Timespan_1.Timespan.years(1), base = Unixtime.now()) {
147
+ return base.addSeconds(Math.random() * max.seconds * -1);
148
+ }
149
+ /**
150
+ * @param max maximum timespan to add
151
+ * @returns returns random time in the future within given timespan
152
+ */
153
+ static randomFuture(max = Timespan_1.Timespan.years(1), base = Unixtime.now()) {
154
+ return base.addSeconds(Math.random() * max.seconds);
155
+ }
142
156
  static fromYYYYMMDD(value) {
143
157
  if (value.length != 8)
144
158
  throw new Error('Invalid yyyymmdd value');
@@ -1,3 +1,4 @@
1
+ import { Timespan } from './Timespan';
1
2
  import { Unixtime } from './Unixtime';
2
3
 
3
4
  describe('Unixtime', () => {
@@ -123,6 +124,40 @@ describe('Unixtime', () => {
123
124
  expect(unixtime.toISOString()).toEqual('2024-10-11T00:00:00.000Z');
124
125
  })
125
126
 
127
+ it('yyyymmddhh should return correct values',() => {
128
+ const unixtime = new Unixtime("2024-10-11T12:13:14Z");
129
+ expect(unixtime.yyyymmdd()).toEqual('20241011');
130
+ expect(unixtime.yyyymmddhh()).toEqual('2024101112');
131
+ // should also work with optional seperators
132
+ expect(unixtime.yyyymmddhh('-')).toEqual('2024-10-11-12');
133
+ })
134
+
135
+ it('randomPast should return random time in the past', () => {
136
+ const end = Unixtime.now()
137
+ const begin = end.clone().addYears(-1)
138
+ for(let i=0;i<100000;i++) {
139
+ const rand = Unixtime.randomPast(Timespan.years(1))
140
+ expect(rand.toLongUnixtime()).toBeGreaterThan(begin.toLongUnixtime());
141
+ expect(rand.toLongUnixtime()).toBeLessThan(end.toLongUnixtime());
142
+ }
143
+ })
144
+
145
+ it('randomFuture should return random time in the future within given timespan of 1 year', () => {
146
+ const begin = Unixtime.now()
147
+ const end = begin.clone().addYears(1)
148
+ for(let i=0;i<100000;i++) {
149
+ const rand = Unixtime.randomFuture(Timespan.years(1))
150
+ expect(rand.toLongUnixtime()).toBeGreaterThan(begin.toLongUnixtime());
151
+ expect(rand.toLongUnixtime()).toBeLessThan(end.toLongUnixtime());
152
+ }
153
+ })
154
+
155
+ it('yyyymmdd should work as designed',() => {
156
+ const unixtime = new Unixtime("2024-10-11T12:13:14Z");
157
+ expect(unixtime.yyyymmdd()).toEqual('20241011');
158
+ expect(unixtime.yyyymmdd('-')).toEqual('2024-10-11');
159
+ })
160
+
126
161
  it('fromYYYYMMDD should throw error when invalid date', () => {
127
162
  expect(() => Unixtime.fromYYYYMMDD('20243131')).toThrow('Invalid month');
128
163
  expect(() => Unixtime.fromYYYYMMDD('20241233')).toThrow('Invalid day');
@@ -107,24 +107,24 @@ export class Unixtime {
107
107
  return this
108
108
  }
109
109
 
110
- public yyyymmdd(): string {
110
+ public yyyymmdd(seperator: string=""): string {
111
111
  let mm = this.month
112
112
  let dd = this.day
113
113
  return [this.value.getFullYear(),
114
114
  (mm > 9 ? '' : '0') + mm,
115
- (dd > 9 ? '' : '0') + dd
116
- ].join('')
115
+ (dd > 9 ? '' : '0') + dd,
116
+ ].join(seperator)
117
117
  }
118
118
 
119
- public yyymmddhh(): string {
119
+ public yyyymmddhh(seperator: string=""): string {
120
120
  let mm = this.month
121
121
  let dd = this.day
122
122
  let hh = this.hours
123
123
  return [this.year,
124
124
  (mm > 9 ? '' : '0') + mm,
125
125
  (dd > 9 ? '' : '0') + dd,
126
- (hh > 9 ? '' : '0') + hh
127
- ].join('')
126
+ (hh > 9 ? '' : '0') + hh,
127
+ ].join(seperator)
128
128
  }
129
129
 
130
130
  /**
@@ -171,6 +171,21 @@ export class Unixtime {
171
171
  return new Unixtime(this.toLongUnixtime())
172
172
  }
173
173
 
174
+ /**
175
+ * @param max maximum timespan to subtract
176
+ * @returns returns random time in the past within given timespan
177
+ */
178
+ static randomPast(max: Timespan=Timespan.years(1), base: Unixtime=Unixtime.now()): Unixtime {
179
+ return base.addSeconds(Math.random()*max.seconds*-1)
180
+ }
181
+
182
+ /**
183
+ * @param max maximum timespan to add
184
+ * @returns returns random time in the future within given timespan
185
+ */
186
+ static randomFuture(max: Timespan=Timespan.years(1), base: Unixtime=Unixtime.now()): Unixtime {
187
+ return base.addSeconds(Math.random()*max.seconds)
188
+ }
174
189
 
175
190
  static fromYYYYMMDD(value: string): Unixtime {
176
191
  if (value.length != 8) throw new Error('Invalid yyyymmdd value')
@@ -1 +0,0 @@
1
- {"root":["./ip6addr.d.ts","./ts/global.ts","./ts/index.ts","./ts/types.ts","./ts/funcs/geoip2paths.ts","./ts/funcs/maxruntimehours.ts","./ts/funcs/asjson.ts","./ts/funcs/ensurefolder.ts","./ts/funcs/filecacheisvalid.ts","./ts/funcs/getmysql1.ts","./ts/funcs/getmysql2.ts","./ts/funcs/getweeknumber.ts","./ts/funcs/hash_sha512.ts","./ts/funcs/initmysql2pool.ts","./ts/funcs/int2ip.ts","./ts/funcs/ip2int.ts","./ts/funcs/islanip.ts","./ts/funcs/islocal.ts","./ts/funcs/isloopbackip.ts","./ts/funcs/knexconnection.ts","./ts/funcs/promisetimeout.ts","./ts/funcs/randomip.ts","./ts/funcs/randomip6.ts","./ts/funcs/randomstring.ts","./ts/funcs/randomtcpport.ts","./ts/funcs/randomutfstring.ts","./ts/funcs/utcnow.ts","./ts/funcs/validemail.ts","./ts/funcs/validip.ts","./ts/funcs/vaultfill.ts","./ts/funcs/vaultread.ts","./ts/funcs/wait.ts","./ts/models/ipaddress.ts","./ts/models/interfaces.ts","./ts/models/otpgenerator.ts","./ts/models/timespan.ts","./ts/models/unixtime.ts"],"version":"5.7.2"}