@nxtedition/lib 19.3.6 → 19.4.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 (2) hide show
  1. package/package.json +2 -1
  2. package/sequence.js +101 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "19.3.6",
3
+ "version": "19.4.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -18,6 +18,7 @@
18
18
  "http.js",
19
19
  "s3.js",
20
20
  "deepstream.js",
21
+ "sequence.js",
21
22
  "logger.js",
22
23
  "mime.js",
23
24
  "proxy.js",
package/sequence.js ADDED
@@ -0,0 +1,101 @@
1
+ import assert from 'node:assert'
2
+
3
+ export const ID_SEP = '~'
4
+
5
+ // https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
6
+ const cyrb53 = (str, seed = 0) => {
7
+ let h1 = 0xdeadbeef ^ seed
8
+ let h2 = 0x41c6ce57 ^ seed
9
+ for (let i = 0, ch; i < str.length; i++) {
10
+ ch = str.charCodeAt(i)
11
+ h1 = Math.imul(h1 ^ ch, 2654435761)
12
+ h2 = Math.imul(h2 ^ ch, 1597334677)
13
+ }
14
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
15
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
16
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
17
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
18
+
19
+ return 4294967296 * (2097151 & h2) + (h1 >>> 0)
20
+ }
21
+
22
+ export class Sequence {
23
+ #value
24
+ #parts = []
25
+ #identity
26
+
27
+ constructor(value) {
28
+ try {
29
+ if (Array.isArray(value)) {
30
+ for (const part of value) {
31
+ if (typeof part === 'string') {
32
+ const [sequenceStr, id] = part.split(ID_SEP)
33
+ const sequence = parseInt(sequenceStr)
34
+ assert(Number.isFinite(sequence) && sequence >= 0)
35
+ this.#parts.push(id, sequence)
36
+ } else {
37
+ assert(Number.isFinite(part.sequence) && part.sequence >= 0)
38
+ assert(typeof part.id === 'string' && part.id.length > 0)
39
+ this.#parts.push(part.id, part.sequence)
40
+ }
41
+ }
42
+ } else if (typeof value === 'string') {
43
+ const [countStr, token] = value.split('-')
44
+ const count = parseInt(countStr)
45
+ assert(Number.isFinite(count) && count >= 0)
46
+ for (const str of token.split('_')) {
47
+ const [sequenceStr, id] = str.split(ID_SEP)
48
+ const sequence = parseInt(sequenceStr)
49
+ assert(Number.isFinite(sequence) && sequence >= 0)
50
+ this.#parts.push(id, sequence)
51
+ }
52
+ this.#value = value
53
+ } else {
54
+ assert(false)
55
+ }
56
+ } catch (err) {
57
+ throw Object.assign(new Error('Invalid sequence value'), { cause: err, data: value })
58
+ }
59
+ }
60
+
61
+ get identity() {
62
+ if (this.#identity == null) {
63
+ let str
64
+ for (let n = 0; n < this.#parts.length; n += 2) {
65
+ str += this.#parts[n]
66
+ }
67
+ this.#identity = cyrb53(str, this.#parts.length)
68
+ }
69
+
70
+ return this.#identity
71
+ }
72
+
73
+ get length() {
74
+ return this.#parts.length / 2
75
+ }
76
+
77
+ at(index) {
78
+ assert(Number.isFinite(index) && index >= 0 && index * 2 < this.#parts.length)
79
+ return this.#parts[index * 2 + 1]
80
+ }
81
+
82
+ set(index, sequence) {
83
+ assert(Number.isFinite(index) && index >= 0 && index < this.#parts.length)
84
+ assert(Number.isFinite(sequence) && sequence >= 0)
85
+ this.#parts[index * 2 + 1] = sequence
86
+ this.#value = undefined
87
+ }
88
+
89
+ toString() {
90
+ if (!this.#value) {
91
+ let count = 0
92
+ let token = ''
93
+ for (let n = 0; n < this.#parts.length; n += 2) {
94
+ count += this.#parts[n + 1]
95
+ token += (n === 0 ? '-' : '_') + this.#parts[n + 1] + ID_SEP + this.#parts[n]
96
+ }
97
+ this.#value = String(count) + token
98
+ }
99
+ return this.#value
100
+ }
101
+ }