@thi.ng/ksuid 2.1.31 → 3.0.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-01-10T15:20:19Z
3
+ - **Last updated**: 2023-02-05T14:42:21Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,29 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ # [3.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/ksuid@3.0.0) (2023-01-17)
13
+
14
+ #### 🛑 Breaking changes
15
+
16
+ - update readme w/ v3.0.0 info ([f8d83c8](https://github.com/thi-ng/umbrella/commit/f8d83c8))
17
+ - BREAKING CHANGE: `epoch` config unified to use milliseconds
18
+ - add new section to readme
19
+ - see [3d73b1766](https://github.com/thi-ng/umbrella/commit/3d73b1766) for code details
20
+
21
+ #### 🚀 Features
22
+
23
+ - fix [#372](https://github.com/thi-ng/umbrella/issues/372), add fromEpoch() methods ([e416e91](https://github.com/thi-ng/umbrella/commit/e416e91))
24
+ - add fromEpoch/fromEpochBinary() to IKSUID interface
25
+ - add impls in AKSUID
26
+ - add tests for all 3 classes
27
+
28
+ #### ♻️ Refactoring
29
+
30
+ - use milliseconds as unified epoch offsets ([3d73b17](https://github.com/thi-ng/umbrella/commit/3d73b17))
31
+ - clarify KSUIDOpts.epoch docs
32
+ - refactor KSUID32 to use milliseconds as `epoch` offset
33
+ - update tests
34
+
12
35
  ## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/ksuid@2.1.0) (2021-11-17)
13
36
 
14
37
  #### 🚀 Features
package/README.md CHANGED
@@ -11,6 +11,7 @@ This project is part of the
11
11
 
12
12
  - [About](#about)
13
13
  - [Status](#status)
14
+ - [Breaking changes](#breaking-changes)
14
15
  - [Related packages](#related-packages)
15
16
  - [Installation](#installation)
16
17
  - [Dependencies](#dependencies)
@@ -58,6 +59,16 @@ support
58
59
 
59
60
  [Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bksuid%5D+in%3Atitle)
60
61
 
62
+ ### Breaking changes
63
+
64
+ Since v3.0.0 all
65
+ [`epoch`](https://docs.thi.ng/umbrella/ksuid/interfaces/KSUIDOpts.html#epoch)
66
+ time-shift config values are to be given in milliseconds. This change is
67
+ unifying this behavior and is only a breaking change if using `KSUID32` and
68
+ specifying custom `epoch` offsets (using defaults is **not** impacted).
69
+ Previously, `KSUID32` used an offset given in seconds, whereas the other
70
+ implementations already used milliseconds.
71
+
61
72
  ## Related packages
62
73
 
63
74
  - [@thi.ng/base-n](https://github.com/thi-ng/umbrella/tree/develop/packages/base-n) - Arbitrary base-n conversions w/ presets for base16/32/36/58/62/64/85, support for arrays & bigints
@@ -84,7 +95,7 @@ For Node.js REPL:
84
95
  const ksuid = await import("@thi.ng/ksuid");
85
96
  ```
86
97
 
87
- Package sizes (brotli'd, pre-treeshake): ESM: 754 bytes
98
+ Package sizes (brotli'd, pre-treeshake): ESM: 769 bytes
88
99
 
89
100
  ## Dependencies
90
101
 
@@ -143,6 +154,8 @@ import { BASE36 } from "@thi.ng/base-n";
143
154
 
144
155
  // no time shift, 64bit random
145
156
  const id36 = defKSUID32({ base: BASE36, epoch: 0, bytes: 8 });
157
+
158
+ id32.next();
146
159
  // '2VOUKH4K59AG0RXR4XH'
147
160
  ```
148
161
 
package/aksuid.d.ts CHANGED
@@ -18,6 +18,8 @@ export declare abstract class AKSUID implements IKSUID {
18
18
  nextBinary(): Uint8Array;
19
19
  timeOnly(epoch?: number): string;
20
20
  abstract timeOnlyBinary(epoch?: number): Uint8Array;
21
+ fromEpoch(epoch?: number): string;
22
+ fromEpochBinary(epoch?: number): Uint8Array;
21
23
  format(buf: Uint8Array): string;
22
24
  abstract parse(id: string): {
23
25
  epoch: number;
package/aksuid.js CHANGED
@@ -28,6 +28,15 @@ export class AKSUID {
28
28
  timeOnly(epoch) {
29
29
  return this.format(this.timeOnlyBinary(epoch));
30
30
  }
31
+ fromEpoch(epoch) {
32
+ return this.format(this.fromEpochBinary(epoch));
33
+ }
34
+ fromEpochBinary(epoch) {
35
+ const buf = this.timeOnlyBinary(epoch);
36
+ return this.rnd
37
+ ? randomBytesFrom(this.rnd, buf, this.epochSize)
38
+ : randomBytes(buf, this.epochSize);
39
+ }
31
40
  format(buf) {
32
41
  this.ensureSize(buf);
33
42
  return this.pad(this.base.encodeBytes(buf));
package/api.d.ts CHANGED
@@ -32,6 +32,20 @@ export interface IKSUID {
32
32
  * @param epoch -
33
33
  */
34
34
  timeOnlyBinary(epoch?: number): Uint8Array;
35
+ /**
36
+ * Returns a new formatted ID, composed from user supplied timestamp
37
+ * (default: current time) and a random payload.
38
+ *
39
+ * @param epoch
40
+ */
41
+ fromEpoch(epoch?: number): string;
42
+ /**
43
+ * Returns a new ID as byte array, composed from user supplied timestamp
44
+ * (default: current time) and a random payload.
45
+ *
46
+ * @param epoch
47
+ */
48
+ fromEpochBinary(epoch?: number): Uint8Array;
35
49
  /**
36
50
  * Returns baseN encoded version of given binary ID (generated via
37
51
  * `.nextBinary()`).
@@ -76,15 +90,15 @@ export interface KSUIDOpts {
76
90
  */
77
91
  bytes: number;
78
92
  /**
79
- * Time offset in seconds, relative to standard Unix epoch. This is used to
80
- * extend the time headroom of IDs into the future.
93
+ * Time offset in milliseconds, relative to standard Unix epoch. This is
94
+ * used to extend the time headroom of IDs into the future.
81
95
  *
82
96
  * @remarks
83
97
  * The default value (for both 32 & 64bit impls) is approx. 2020-09-13,
84
- * meaning this is the T0 epoch for all IDs (providing an additional ~50
85
- * year lifespan compared to the standard 1970-01-01 epoch)
98
+ * meaning this is the `t0` base epoch for all generated IDs (providing an
99
+ * additional ~50 year lifespan compared to the standard 1970-01-01 epoch)
86
100
  *
87
- * @defaultValue 1_600_000_000 or 1_600_000_000_000
101
+ * @defaultValue 1_600_000_000_000
88
102
  */
89
103
  epoch: number;
90
104
  }
package/ksuid32.js CHANGED
@@ -2,14 +2,14 @@ import { AKSUID } from "./aksuid.js";
2
2
  export class KSUID32 extends AKSUID {
3
3
  constructor(opts) {
4
4
  super(4, {
5
- epoch: 1600000000,
5
+ epoch: 1600000000000,
6
6
  bytes: 16,
7
7
  ...opts,
8
8
  });
9
9
  }
10
10
  timeOnlyBinary(epoch = Date.now()) {
11
11
  const buf = new Uint8Array(this.size);
12
- const t = this.ensureTime((epoch / 1000 - this.epoch) | 0);
12
+ const t = this.ensureTime(((epoch - this.epoch) / 1000) | 0);
13
13
  buf.set([t >>> 24, (t >> 16) & 0xff, (t >> 8) & 0xff, t & 0xff]);
14
14
  return buf;
15
15
  }
@@ -17,7 +17,7 @@ export class KSUID32 extends AKSUID {
17
17
  const buf = new Uint8Array(this.size);
18
18
  this.base.decodeBytes(id, buf);
19
19
  return {
20
- epoch: (this.u32(buf) + this.epoch) * 1000,
20
+ epoch: this.u32(buf) * 1000 + this.epoch,
21
21
  id: buf.slice(4),
22
22
  };
23
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/ksuid",
3
- "version": "2.1.31",
3
+ "version": "3.0.1",
4
4
  "description": "Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,18 +35,18 @@
35
35
  "test": "testament test"
36
36
  },
37
37
  "dependencies": {
38
- "@thi.ng/base-n": "^2.3.12",
39
- "@thi.ng/errors": "^2.2.8",
40
- "@thi.ng/random": "^3.3.21",
41
- "@thi.ng/strings": "^3.3.23"
38
+ "@thi.ng/base-n": "^2.3.13",
39
+ "@thi.ng/errors": "^2.2.9",
40
+ "@thi.ng/random": "^3.3.22",
41
+ "@thi.ng/strings": "^3.3.24"
42
42
  },
43
43
  "devDependencies": {
44
- "@microsoft/api-extractor": "^7.33.7",
45
- "@thi.ng/testament": "^0.3.9",
46
- "rimraf": "^3.0.2",
44
+ "@microsoft/api-extractor": "^7.34.2",
45
+ "@thi.ng/testament": "^0.3.10",
46
+ "rimraf": "^4.1.2",
47
47
  "tools": "^0.0.1",
48
- "typedoc": "^0.23.22",
49
- "typescript": "^4.9.4"
48
+ "typedoc": "^0.23.24",
49
+ "typescript": "^4.9.5"
50
50
  },
51
51
  "keywords": [
52
52
  "32bit",
@@ -109,5 +109,5 @@
109
109
  "status": "stable",
110
110
  "year": 2020
111
111
  },
112
- "gitHead": "3f0b3e2a7c82aefc7e46fb4338369836b5e1b8cf\n"
112
+ "gitHead": "50ba9c87676fac60c46d2bc0e4d2c7711a374a68\n"
113
113
  }