fantomid 1.0.8 → 1.0.9
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.
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var csprng = require('@lukeed/csprng');
|
|
2
4
|
|
|
3
5
|
const EPOCH = /* @__PURE__ */ new Date("2024-09-08");
|
|
4
6
|
const TIMESTAMP_LENGTH = 36;
|
|
@@ -16,12 +18,12 @@ function fantomid() {
|
|
|
16
18
|
for (let i = 0; i < TIMESTAMP_LENGTH; i++) {
|
|
17
19
|
bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = timestamp >> i & 1;
|
|
18
20
|
}
|
|
19
|
-
const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));
|
|
21
|
+
const randomBytes = csprng.random(Math.ceil(RANDOM_BITS_LENGTH / 8));
|
|
20
22
|
for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {
|
|
21
23
|
bits[RANDOM_BITS_LENGTH - 1 - i] = randomBytes[Math.floor(i / 8)] >> i % 8 & 1;
|
|
22
24
|
}
|
|
23
25
|
return Number("0b" + bits.reduce((id, bit) => id + bit, String()));
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
//# sourceMappingURL=index.
|
|
28
|
+
module.exports = fantomid;
|
|
29
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/fantomid.js"],"sourcesContent":["import { random } from \"@lukeed/csprng\";\n\nconst EPOCH = new Date(\"2024-09-08\");\nconst TIMESTAMP_LENGTH = 36;\nconst RANDOM_BITS_LENGTH = 17;\nconst TIMESTAMP_PRECISION_DIVIDER = 100;\n\nexport function fantomid() {\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (Date.now() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":["random"],"mappings":";;;;AACA,MAAM,KAAK,mBAAmB,IAAI,IAAI,CAAC,YAAY,CAAC;AACpD,MAAM,gBAAgB,GAAG,EAAE;AAC3B,MAAM,kBAAkB,GAAG,EAAE;AAC7B,MAAM,2BAA2B,GAAG,GAAG;AAChC,SAAS,QAAQ,GAAG;AAC3B,EAAE,MAAM,IAAI,GAAG,KAAK,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;AAC3D,EAAE,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK;AAC9B;AACA;AACA,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;AACvB;AACA,IAAI;AACJ,GAAG;AACH,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,EAAE,CAAC,EAAE,EAAE;AAC7C,IAAI,IAAI,CAAC,gBAAgB,GAAG,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,IAAI,CAAC,GAAG,CAAC;AAC5E,EAAE;AACF,EAAE,MAAM,WAAW,GAAGA,aAAM,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;AAC/D,EAAE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,kBAAkB,EAAE,CAAC,EAAE,EAAE;AAC/C,IAAI,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;AAClF,EAAE;AACF,EAAE,OAAO,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,KAAK,EAAE,GAAG,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC;AACpE;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fantomid",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A tiny, universal ID generator compatible with JavaScript integers",
|
|
6
6
|
"repository": "th3rius/fantomid",
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"build": "rimraf dist && pkgroll --sourcemap",
|
|
17
17
|
"test": "node --test --experimental-test-module-mocks **/*.spec.js"
|
|
18
18
|
},
|
|
19
|
-
"main": "./dist/index.
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
20
|
"module": "./dist/index.mjs",
|
|
21
|
-
"types": "./dist/index.d.
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
22
|
"exports": {
|
|
23
23
|
"require": {
|
|
24
|
-
"types": "./dist/index.d.
|
|
25
|
-
"default": "./dist/index.
|
|
24
|
+
"types": "./dist/index.d.cts",
|
|
25
|
+
"default": "./dist/index.cjs"
|
|
26
26
|
},
|
|
27
27
|
"import": {
|
|
28
28
|
"types": "./dist/index.d.mts",
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/fantomid.js"],"sourcesContent":["import { random } from \"@lukeed/csprng\";\n\nconst EPOCH = new Date(\"2024-09-08\");\nconst TIMESTAMP_LENGTH = 36;\nconst RANDOM_BITS_LENGTH = 17;\nconst TIMESTAMP_PRECISION_DIVIDER = 100;\n\nexport function fantomid() {\n const bits = Array(TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH);\n\n // Generates a timestamp to be part of the key. This is\n // inspired by how UUID v7 works: the idea is that this\n // will guarantee keys are generated evenly across time.\n const timestamp = Math.round(\n // Use a more recent base date instead of the Unix Epoch to\n // increase the amount of time we can represent in the timestamp.\n (Date.now() - EPOCH) /\n // Decrease the precision of the timestamp to\n // increase the amount of bits we can fit in a key.\n TIMESTAMP_PRECISION_DIVIDER,\n );\n for (let i = 0; i < TIMESTAMP_LENGTH; i++) {\n bits[TIMESTAMP_LENGTH + RANDOM_BITS_LENGTH - 1 - i] = (timestamp >> i) & 1;\n }\n\n // Generates a randomized part of the key. If multiple machines generate a\n // key at the exact same time (limited by the precision of the timestamp),\n // we are ensured to have a chance of collision of `1` in `2 ** RANDOM_BITS_LENGTH`.\n const randomBytes = random(Math.ceil(RANDOM_BITS_LENGTH / 8));\n for (let i = 0; i < RANDOM_BITS_LENGTH; i++) {\n bits[RANDOM_BITS_LENGTH - 1 - i] =\n (randomBytes[Math.floor(i / 8)] >> i % 8) & 1;\n }\n\n return Number(\"0b\" + bits.reduce((id, bit) => id + bit, String()));\n}\n"],"names":[],"mappings":";;AACA,MAAM,KAAK,mBAAmB,IAAI,IAAI,CAAC,YAAY,CAAC;AACpD,MAAM,gBAAgB,GAAG,EAAE;AAC3B,MAAM,kBAAkB,GAAG,EAAE;AAC7B,MAAM,2BAA2B,GAAG,GAAG;AAChC,SAAS,QAAQ,GAAG;AAC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