index.js
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, copyDefault, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
return (module2, temp) => {
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
};
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var __privateWrapper = (obj, member, setter, getter) => {
return {
set _(value) {
__privateSet(obj, member, value, setter);
},
get _() {
return __privateGet(obj, member, getter);
}
};
};
// src/index.ts
var src_exports = {};
__export(src_exports, {
DiscordSnowflake: () => DiscordSnowflake,
Snowflake: () => Snowflake,
TwitterSnowflake: () => TwitterSnowflake
});
// src/lib/Snowflake.ts
var ProcessId = 1n;
var WorkerId = 0n;
var _increment, _epoch;
var Snowflake = class {
constructor(epoch) {
__privateAdd(this, _increment, 0n);
__privateAdd(this, _epoch, void 0);
__publicField(this, "decode", this.deconstruct);
__privateSet(this, _epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
}
get epoch() {
return __privateGet(this, _epoch);
}
generate({ increment, timestamp = Date.now(), workerId = WorkerId, processId = ProcessId } = {}) {
if (timestamp instanceof Date)
timestamp = BigInt(timestamp.getTime());
else if (typeof timestamp === "number")
timestamp = BigInt(timestamp);
else if (typeof timestamp !== "bigint") {
throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
}
if (typeof increment === "bigint" && increment >= 4095n)
increment = 0n;
else {
increment = __privateWrapper(this, _increment)._++;
if (__privateGet(this, _increment) >= 4095n)
__privateSet(this, _increment, 0n);
}
return timestamp - __privateGet(this, _epoch) << 22n | (workerId & 0b11111n) << 17n | (processId & 0b11111n) << 12n | increment;
}
deconstruct(id) {
const bigIntId = BigInt(id);
return {
id: bigIntId,
timestamp: (bigIntId >> 22n) + __privateGet(this, _epoch),
workerId: bigIntId >> 17n & 0b11111n,
processId: bigIntId >> 12n & 0b11111n,
increment: bigIntId & 0b111111111111n,
epoch: __privateGet(this, _epoch)
};
}
timestampFrom(id) {
return Number((BigInt(id) >> 22n) + __privateGet(this, _epoch));
}
};
__name(Snowflake, "Snowflake");
_increment = new WeakMap();
_epoch = new WeakMap();
// src/lib/DiscordSnowflake.ts
var DiscordSnowflake = new Snowflake(1420070400000n);
// src/lib/TwitterSnowflake.ts
var TwitterSnowflake = new Snowflake(1142974214000n);
module.exports = __toCommonJS(src_exports);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DiscordSnowflake,
Snowflake,
TwitterSnowflake
});
//# sourceMappingURL=index.js.map