@stateforward/hsm.ts 0.1.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/muid.ts"],"sourcesContent":["export type UInt64 = {\n high: number;\n low: number;\n};\n\nexport type MuidConfig = {\n machineID?: UInt64;\n timestampBitLen?: number;\n machineIDBitLen?: number;\n epoch?: UInt64;\n};\n\nconst defaultEpochValue = 1700000000000;\n\nexport const make64 = (high: number, low: number): UInt64 => ({\n high: high >>> 0,\n low: low >>> 0,\n});\n\nexport const from32 = (value: number) => make64(0, value >>> 0);\n\nexport const add64 = (a: UInt64, b: UInt64) => {\n const low = (a.low + b.low) >>> 0;\n const carry = a.low + b.low > 0xffffffff ? 1 : 0;\n const high = (a.high + b.high + carry) >>> 0;\n return make64(high, low);\n};\n\nexport const sub64 = (a: UInt64, b: UInt64) => {\n const low = (a.low - b.low) >>> 0;\n const borrow = a.low < b.low ? 1 : 0;\n const high = (a.high - b.high - borrow) >>> 0;\n return make64(high, low);\n};\n\nexport const shl64 = (value: UInt64, bits: number) => {\n if (bits === 0) {\n return value;\n }\n if (bits >= 64) {\n return make64(0, 0);\n }\n if (bits >= 32) {\n return make64(value.low << (bits - 32), 0);\n }\n\n const high = (value.high << bits) | (value.low >>> (32 - bits));\n const low = value.low << bits;\n return make64(high >>> 0, low >>> 0);\n};\n\nexport const shr64 = (value: UInt64, bits: number) => {\n if (bits === 0) {\n return value;\n }\n if (bits >= 64) {\n return make64(0, 0);\n }\n if (bits >= 32) {\n return make64(0, value.high >>> (bits - 32));\n }\n\n const low = (value.low >>> bits) | (value.high << (32 - bits));\n const high = value.high >>> bits;\n return make64(high >>> 0, low >>> 0);\n};\n\nexport const or64 = (a: UInt64, b: UInt64) =>\n make64((a.high | b.high) >>> 0, (a.low | b.low) >>> 0);\n\nexport const and64 = (a: UInt64, b: UInt64) =>\n make64((a.high & b.high) >>> 0, (a.low & b.low) >>> 0);\n\nexport const cmp64 = (a: UInt64, b: UInt64) => {\n if (a.high < b.high) {\n return -1;\n }\n if (a.high > b.high) {\n return 1;\n }\n if (a.low < b.low) {\n return -1;\n }\n if (a.low > b.low) {\n return 1;\n }\n return 0;\n};\n\nconst gte64 = (a: UInt64, b: UInt64) => cmp64(a, b) >= 0;\n\nconst toHex64 = (value: UInt64) => {\n if (value.high === 0) {\n return value.low.toString(16);\n }\n\n const highHex = value.high.toString(16);\n let lowHex = value.low.toString(16);\n while (lowHex.length < 8) {\n lowHex = `0${lowHex}`;\n }\n return `${highHex}${lowHex}`;\n};\n\nconst toString64 = (value: UInt64) => {\n if (value.high === 0) {\n return value.low.toString();\n }\n\n if (value.high < 0x200000) {\n return (value.high * 0x100000000 + value.low).toString();\n }\n\n return `0x${toHex64(value)}`;\n};\n\nconst toBase32_64 = (value: UInt64) => {\n if (value.high === 0) {\n return value.low.toString(32);\n }\n\n const hex = toHex64(value);\n let num = 0;\n let result = \"\";\n\n for (let index = 0; index < hex.length; index += 1) {\n num = num * 16 + Number.parseInt(hex[index]!, 16);\n if (num >= 32) {\n result += (num % 32).toString(32);\n num = Math.floor(num / 32);\n }\n }\n\n if (num > 0) {\n result = `${num.toString(32)}${result}`;\n }\n\n return result || \"0\";\n};\n\nconst hashString = (value: string) => {\n let hash = make64(0x811c9dc5, 0);\n const prime = from32(0x01000193);\n\n for (let index = 0; index < value.length; index += 1) {\n const char = from32(value.charCodeAt(index));\n hash = and64(or64(hash, char), make64(0, 0xffffffff));\n if (hash.high === 0) {\n hash = from32((hash.low * prime.low) >>> 0);\n }\n }\n\n return hash;\n};\n\nconst getMachineIdentifier = () => {\n if (typeof process !== \"undefined\") {\n const identifier =\n process.env.HOSTNAME ??\n process.env.COMPUTERNAME ??\n process.release?.name;\n if (identifier) {\n return identifier;\n }\n }\n\n if (typeof navigator !== \"undefined\") {\n return `${navigator.userAgent}${navigator.platform}${navigator.hardwareConcurrency ?? \"\"}`;\n }\n\n return `js-${Math.random().toString(36).slice(2)}`;\n};\n\nconst getRandomBytes = (length: number) => {\n const cryptoApi = globalThis.crypto;\n if (cryptoApi?.getRandomValues) {\n const values = new Uint8Array(length);\n cryptoApi.getRandomValues(values);\n return Array.from(values);\n }\n\n return Array.from({ length }, () => Math.floor(Math.random() * 256));\n};\n\nexport const getDefaultConfig = (): Required<MuidConfig> => {\n const epoch = make64(\n Math.floor(defaultEpochValue / 0x100000000),\n defaultEpochValue % 0x100000000,\n );\n const timestampBitLen = 41;\n const machineIDBitLen = 14;\n const maxMachineID = (1 << machineIDBitLen) - 1;\n const identifier = getMachineIdentifier();\n let machineID: UInt64;\n\n if (identifier) {\n const hash = hashString(identifier);\n machineID = from32(hash.low & maxMachineID);\n } else {\n const randomBytes = getRandomBytes(4);\n let randomValue = 0;\n for (let index = 0; index < 4; index += 1) {\n randomValue = (randomValue << 8) | randomBytes[index]!;\n }\n machineID = from32(randomValue & maxMachineID);\n }\n\n return {\n epoch,\n machineID,\n machineIDBitLen,\n timestampBitLen,\n };\n};\n\nexport class MUID {\n readonly value: UInt64;\n\n constructor(value?: UInt64 | number | null) {\n if (typeof value === \"number\") {\n this.value = from32(value);\n return;\n }\n\n if (value && typeof value.high === \"number\" && typeof value.low === \"number\") {\n this.value = make64(value.high, value.low);\n return;\n }\n\n this.value = make64(0, 0);\n }\n\n toString() {\n return toBase32_64(this.value);\n }\n\n toHex() {\n return toHex64(this.value);\n }\n\n toDecimal() {\n return toString64(this.value);\n }\n\n valueOf() {\n return this.value;\n }\n}\n\nexport class Generator {\n readonly timestampBitLen: number;\n readonly machineIDBitLen: number;\n readonly epoch: UInt64;\n readonly shardIndex: number;\n readonly shardBitLen: number;\n readonly counterBitLen: number;\n readonly timestampBitShift: number;\n readonly machineIDShift: number;\n readonly shardIndexShift: number;\n readonly counterBitMask: UInt64;\n readonly machineID: UInt64;\n state: UInt64;\n\n constructor(config: MuidConfig = {}, shardIndex = 0, shardBitLen = 0) {\n this.timestampBitLen = config.timestampBitLen ?? 41;\n this.machineIDBitLen = config.machineIDBitLen ?? 14;\n this.epoch =\n config.epoch ??\n make64(\n Math.floor(defaultEpochValue / 0x100000000),\n defaultEpochValue % 0x100000000,\n );\n this.shardIndex = shardIndex;\n this.shardBitLen = shardBitLen;\n this.counterBitLen =\n 64 - this.timestampBitLen - this.machineIDBitLen - this.shardBitLen;\n this.timestampBitShift =\n this.machineIDBitLen + this.shardBitLen + this.counterBitLen;\n this.machineIDShift = this.shardBitLen + this.counterBitLen;\n this.shardIndexShift = this.counterBitLen;\n this.counterBitMask =\n this.counterBitLen >= 32\n ? make64(0xffffffff, 0xffffffff)\n : sub64(shl64(from32(1), this.counterBitLen), from32(1));\n\n const machineIDMask =\n this.machineIDBitLen >= 32\n ? make64(0xffffffff, 0xffffffff)\n : sub64(shl64(from32(1), this.machineIDBitLen), from32(1));\n\n this.machineID = and64(config.machineID ?? from32(0), machineIDMask);\n this.shardIndex =\n (this.shardIndex & ((1 << Math.min(this.shardBitLen, 31)) - 1)) >>> 0;\n this.state = from32(1);\n }\n\n id() {\n let now = sub64(from32(Date.now() & 0xffffffff), this.epoch);\n const dateNow = Date.now();\n if (dateNow > 0xffffffff) {\n now = sub64(\n make64(Math.floor(dateNow / 0x100000000), dateNow & 0xffffffff),\n this.epoch,\n );\n }\n const lastTimestamp = shr64(this.state, this.counterBitLen);\n let counter = and64(this.state, this.counterBitMask);\n\n if (cmp64(now, lastTimestamp) < 0) {\n now = lastTimestamp;\n }\n\n if (cmp64(now, lastTimestamp) === 0) {\n if (gte64(counter, this.counterBitMask)) {\n now = add64(now, from32(1));\n counter = from32(1);\n } else {\n counter = add64(counter, from32(1));\n }\n } else {\n counter = from32(1);\n }\n\n this.state = or64(shl64(now, this.counterBitLen), counter);\n\n const timestampPart = shl64(now, this.timestampBitShift);\n const machineIDPart = shl64(this.machineID, this.machineIDShift);\n const shardIndexPart = shl64(from32(this.shardIndex), this.shardIndexShift);\n return new MUID(\n or64(or64(or64(timestampPart, machineIDPart), shardIndexPart), counter),\n );\n }\n}\n\nexport class ShardedGenerators {\n readonly pool: Generator[];\n readonly size: number;\n index = 0;\n\n constructor() {\n const numCPU =\n typeof navigator !== \"undefined\" && navigator.hardwareConcurrency\n ? navigator.hardwareConcurrency\n : 4;\n const shardBits =\n numCPU > 1 ? Math.min(Math.ceil(Math.log2(numCPU)), 5) : 0;\n const defaultConfig = getDefaultConfig();\n this.size = 1 << shardBits;\n this.pool = [];\n\n for (let index = 0; index < this.size; index += 1) {\n this.pool.push(new Generator(defaultConfig, index, shardBits));\n }\n }\n\n next() {\n const generator = this.pool[this.index]!;\n this.index = (this.index + 1) % this.size;\n return generator;\n }\n}\n\nconst defaultGenerator = new Generator(getDefaultConfig(), 0, 0);\n\nexport const make = () => defaultGenerator.id();\n\nexport const newGenerator = (\n config: MuidConfig = {},\n shardIndex = 0,\n shardBitLen = 0,\n) =>\n new Generator(\n {\n ...getDefaultConfig(),\n ...config,\n },\n shardIndex,\n shardBitLen,\n );\n\nexport const makeMuid = (prefix: string) => `${prefix}-${make().toString()}`;\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,IAAM,oBAAoB;AAEnB,IAAM,SAAS,CAAC,MAAc,SAAyB;AAAA,EAC5D,MAAM,SAAS;AAAA,EACf,KAAK,QAAQ;AACf;AAEO,IAAM,SAAS,CAAC,UAAkB,OAAO,GAAG,UAAU,CAAC;AAEvD,IAAM,QAAQ,CAAC,GAAW,MAAc;AAC7C,QAAM,MAAO,EAAE,MAAM,EAAE,QAAS;AAChC,QAAM,QAAQ,EAAE,MAAM,EAAE,MAAM,aAAa,IAAI;AAC/C,QAAM,OAAQ,EAAE,OAAO,EAAE,OAAO,UAAW;AAC3C,SAAO,OAAO,MAAM,GAAG;AACzB;AAEO,IAAM,QAAQ,CAAC,GAAW,MAA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AK,QAAQ,KAAK,KAAK;AACrC,WAAO;AAAA,EACT;AACF;AAEA,IAAM,mBAAmB,IAAI,UAAU,iBAAiB,GAAG,GAAG,CAAC;AAExD,IAAM,OAAO,MAAM,iBAAiB,GAAG;AAEvC,IAAM,eAAe,CAC1B,SAAqB,CAAC,GACtB,aAAa,GACb,cAAc,MAEd,IAAI;AAAA,EACF;AAAA,IACE,GAAG,iBAAiB;AAAA,IACpB,GAAG;AAAA,EACL;AAAA,EACA;AAAA,EACA;AACF;AAEK,IAAM,WAAW,CAAC,WAAmB,GAAG,MAAM,IAAI,KAAK,EAAE,SAAS,CAAC;","names":[]}
@@ -0,0 +1,56 @@
1
+ type UInt64 = {
2
+ high: number;
3
+ low: number;
4
+ };
5
+ type MuidConfig = {
6
+ machineID?: UInt64;
7
+ timestampBitLen?: number;
8
+ machineIDBitLen?: number;
9
+ epoch?: UInt64;
10
+ };
11
+ declare const make64: (high: number, low: number) => UInt64;
12
+ declare const from32: (value: number) => UInt64;
13
+ declare const add64: (a: UInt64, b: UInt64) => UInt64;
14
+ declare const sub64: (a: UInt64, b: UInt64) => UInt64;
15
+ declare const shl64: (value: UInt64, bits: number) => UInt64;
16
+ declare const shr64: (value: UInt64, bits: number) => UInt64;
17
+ declare const or64: (a: UInt64, b: UInt64) => UInt64;
18
+ declare const and64: (a: UInt64, b: UInt64) => UInt64;
19
+ declare const cmp64: (a: UInt64, b: UInt64) => 1 | 0 | -1;
20
+ declare const getDefaultConfig: () => Required<MuidConfig>;
21
+ declare class MUID {
22
+ readonly value: UInt64;
23
+ constructor(value?: UInt64 | number | null);
24
+ toString(): string;
25
+ toHex(): string;
26
+ toDecimal(): string;
27
+ valueOf(): UInt64;
28
+ }
29
+ declare class Generator {
30
+ readonly timestampBitLen: number;
31
+ readonly machineIDBitLen: number;
32
+ readonly epoch: UInt64;
33
+ readonly shardIndex: number;
34
+ readonly shardBitLen: number;
35
+ readonly counterBitLen: number;
36
+ readonly timestampBitShift: number;
37
+ readonly machineIDShift: number;
38
+ readonly shardIndexShift: number;
39
+ readonly counterBitMask: UInt64;
40
+ readonly machineID: UInt64;
41
+ state: UInt64;
42
+ constructor(config?: MuidConfig, shardIndex?: number, shardBitLen?: number);
43
+ id(): MUID;
44
+ }
45
+ declare class ShardedGenerators {
46
+ readonly pool: Generator[];
47
+ readonly size: number;
48
+ index: number;
49
+ constructor();
50
+ next(): Generator;
51
+ }
52
+ declare const make: () => MUID;
53
+ declare const newGenerator: (config?: MuidConfig, shardIndex?: number, shardBitLen?: number) => Generator;
54
+ declare const makeMuid: (prefix: string) => string;
55
+
56
+ export { Generator, MUID, type MuidConfig, ShardedGenerators, type UInt64, add64, and64, cmp64, from32, getDefaultConfig, make, make64, makeMuid, newGenerator, or64, shl64, shr64, sub64 };
package/dist/muid.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ type UInt64 = {
2
+ high: number;
3
+ low: number;
4
+ };
5
+ type MuidConfig = {
6
+ machineID?: UInt64;
7
+ timestampBitLen?: number;
8
+ machineIDBitLen?: number;
9
+ epoch?: UInt64;
10
+ };
11
+ declare const make64: (high: number, low: number) => UInt64;
12
+ declare const from32: (value: number) => UInt64;
13
+ declare const add64: (a: UInt64, b: UInt64) => UInt64;
14
+ declare const sub64: (a: UInt64, b: UInt64) => UInt64;
15
+ declare const shl64: (value: UInt64, bits: number) => UInt64;
16
+ declare const shr64: (value: UInt64, bits: number) => UInt64;
17
+ declare const or64: (a: UInt64, b: UInt64) => UInt64;
18
+ declare const and64: (a: UInt64, b: UInt64) => UInt64;
19
+ declare const cmp64: (a: UInt64, b: UInt64) => 1 | 0 | -1;
20
+ declare const getDefaultConfig: () => Required<MuidConfig>;
21
+ declare class MUID {
22
+ readonly value: UInt64;
23
+ constructor(value?: UInt64 | number | null);
24
+ toString(): string;
25
+ toHex(): string;
26
+ toDecimal(): string;
27
+ valueOf(): UInt64;
28
+ }
29
+ declare class Generator {
30
+ readonly timestampBitLen: number;
31
+ readonly machineIDBitLen: number;
32
+ readonly epoch: UInt64;
33
+ readonly shardIndex: number;
34
+ readonly shardBitLen: number;
35
+ readonly counterBitLen: number;
36
+ readonly timestampBitShift: number;
37
+ readonly machineIDShift: number;
38
+ readonly shardIndexShift: number;
39
+ readonly counterBitMask: UInt64;
40
+ readonly machineID: UInt64;
41
+ state: UInt64;
42
+ constructor(config?: MuidConfig, shardIndex?: number, shardBitLen?: number);
43
+ id(): MUID;
44
+ }
45
+ declare class ShardedGenerators {
46
+ readonly pool: Generator[];
47
+ readonly size: number;
48
+ index: number;
49
+ constructor();
50
+ next(): Generator;
51
+ }
52
+ declare const make: () => MUID;
53
+ declare const newGenerator: (config?: MuidConfig, shardIndex?: number, shardBitLen?: number) => Generator;
54
+ declare const makeMuid: (prefix: string) => string;
55
+
56
+ export { Generator, MUID, type MuidConfig, ShardedGenerators, type UInt64, add64, and64, cmp64, from32, getDefaultConfig, make, make64, makeMuid, newGenerator, or64, shl64, shr64, sub64 };
package/dist/muid.js ADDED
@@ -0,0 +1,37 @@
1
+ import {
2
+ Generator,
3
+ MUID,
4
+ ShardedGenerators,
5
+ add64,
6
+ and64,
7
+ cmp64,
8
+ from32,
9
+ getDefaultConfig,
10
+ make,
11
+ make64,
12
+ makeMuid,
13
+ newGenerator,
14
+ or64,
15
+ shl64,
16
+ shr64,
17
+ sub64
18
+ } from "./chunk-R6UCUJT5.js";
19
+ export {
20
+ Generator,
21
+ MUID,
22
+ ShardedGenerators,
23
+ add64,
24
+ and64,
25
+ cmp64,
26
+ from32,
27
+ getDefaultConfig,
28
+ make,
29
+ make64,
30
+ makeMuid,
31
+ newGenerator,
32
+ or64,
33
+ shl64,
34
+ shr64,
35
+ sub64
36
+ };
37
+ //# sourceMappingURL=muid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@stateforward/hsm.ts",
3
+ "version": "0.1.0",
4
+ "description": "Standalone TypeScript hierarchical state machine library",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ },
15
+ "./kind": {
16
+ "types": "./dist/kind.d.ts",
17
+ "import": "./dist/kind.js",
18
+ "require": "./dist/kind.cjs"
19
+ },
20
+ "./muid": {
21
+ "types": "./dist/muid.d.ts",
22
+ "import": "./dist/muid.js",
23
+ "require": "./dist/muid.cjs"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md"
29
+ ],
30
+ "scripts": {
31
+ "benchmark:muid": "tsx scripts/benchmark-muid.ts",
32
+ "build": "tsup",
33
+ "coverage": "npm run build && node --test --experimental-test-coverage --test-coverage-include=dist/index.js --test-coverage-include=dist/kind.js --test-coverage-include=dist/muid.js --test-coverage-lines=100 --test-coverage-functions=100 --test-coverage-branches=100 tests/runtime-ported/*.test.cjs",
34
+ "test:parity": "npm run build && node --test tests/runtime-ported/*.test.cjs",
35
+ "test:types": "for config in tests/types/*/tsconfig.json; do tsc -p \"$config\" --pretty false || exit 1; done",
36
+ "typecheck": "tsc --noEmit",
37
+ "test": "npm run typecheck && npm run test:types && npm run test:parity"
38
+ },
39
+ "keywords": [
40
+ "state-machine",
41
+ "hsm",
42
+ "typescript",
43
+ "fsm",
44
+ "hierarchical"
45
+ ],
46
+ "author": "HSM Team",
47
+ "license": "MIT",
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/stateforward/hsm.ts.git"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/stateforward/hsm.ts/issues"
54
+ },
55
+ "homepage": "https://github.com/stateforward/hsm.ts#readme",
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "engines": {
60
+ "node": ">=18.0.0"
61
+ },
62
+ "devDependencies": {
63
+ "@types/node": "^22.14.1",
64
+ "tsup": "^8.5.0",
65
+ "tsx": "^4.20.6",
66
+ "typescript": "^5.8.3"
67
+ }
68
+ }