detritus-utils 0.5.0-beta.2 → 0.5.0-beta.4

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,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generate = generate;
4
- exports.deconstruct = deconstruct;
5
- exports.timestamp = timestamp;
6
- const constants_1 = require("./constants");
7
- const bits = Object.freeze({
8
- timestamp: 42n,
9
- workerId: 5n,
10
- processId: 5n,
11
- sequence: 12n,
12
- });
13
- const shift = Object.freeze({
14
- timestamp: bits.processId + bits.workerId + bits.sequence,
15
- workerId: bits.workerId + bits.sequence,
16
- processId: bits.sequence,
17
- sequence: 0n,
18
- });
19
- const max = Object.freeze({
20
- timestamp: 0x40000000000n,
21
- processId: -1n ^ (-1n << bits.processId),
22
- sequence: -1n ^ (-1n << bits.sequence),
23
- workerId: -1n ^ (-1n << bits.workerId),
24
- });
25
- const cache = {
26
- sequence: 0n,
27
- };
28
- function generate(options = {}) {
29
- options = Object.assign({
30
- epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
31
- processId: 0,
32
- timestamp: Date.now(),
33
- workerId: 0,
34
- }, options);
35
- const epoch = BigInt(options.epoch);
36
- const processId = BigInt(options.processId) & max.processId;
37
- const timestamp = (BigInt(options.timestamp) - epoch) % max.timestamp;
38
- const workerId = BigInt(options.workerId) & max.workerId;
39
- let sequence;
40
- if (options.sequence === undefined) {
41
- sequence = cache.sequence = ++cache.sequence & max.sequence;
42
- }
43
- else {
44
- sequence = BigInt(options.sequence) & max.sequence;
45
- }
46
- const snowflake = {
47
- id: '',
48
- processId: Number(processId),
49
- sequence: Number(sequence),
50
- timestamp: Number(timestamp),
51
- workerId: Number(workerId),
52
- };
53
- snowflake.id = String((timestamp << shift.timestamp) |
54
- (workerId << shift.workerId) |
55
- (processId << shift.processId) |
56
- (sequence << shift.sequence));
57
- return snowflake;
58
- }
59
- function deconstruct(id, options = {}) {
60
- options = Object.assign({
61
- epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
62
- }, options);
63
- const epoch = BigInt(options.epoch);
64
- const snowflake = BigInt(id);
65
- return {
66
- id,
67
- processId: Number((snowflake & 0x1f000n) >> shift.processId),
68
- sequence: Number(snowflake & 0xfffn),
69
- timestamp: Number((snowflake >> shift.timestamp) + epoch),
70
- workerId: Number((snowflake & 0x3e0000n) >> shift.workerId),
71
- };
72
- }
73
- function timestamp(id, options = {}) {
74
- options = Object.assign({
75
- epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
76
- }, options);
77
- const epoch = BigInt(options.epoch);
78
- if (id) {
79
- return Number((BigInt(id) >> shift.timestamp) + epoch);
80
- }
81
- return 0;
82
- }
@@ -1,21 +0,0 @@
1
- export declare class Interval {
2
- shouldRef: boolean;
3
- reference: any;
4
- get hasStarted(): boolean;
5
- hasRef(): boolean;
6
- ref(): void;
7
- unref(): void;
8
- start(milliseconds: number, handler: Function): void;
9
- stop(): void;
10
- }
11
- export declare class Timeout {
12
- shouldRef: boolean;
13
- reference: any;
14
- get hasStarted(): boolean;
15
- hasRef(): boolean;
16
- ref(): void;
17
- unref(): void;
18
- start(milliseconds: number, handler: Function, override?: boolean): void;
19
- stop(): void;
20
- }
21
- export declare function sleep(milliseconds: number): Promise<void>;
package/lib/src/timers.js DELETED
@@ -1,92 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Timeout = exports.Interval = void 0;
4
- exports.sleep = sleep;
5
- class Interval {
6
- shouldRef = true;
7
- reference = null;
8
- get hasStarted() {
9
- return this.reference !== null;
10
- }
11
- hasRef() {
12
- return this.shouldRef;
13
- }
14
- ref() {
15
- this.shouldRef = true;
16
- if (this.reference) {
17
- this.reference.ref();
18
- }
19
- }
20
- unref() {
21
- this.shouldRef = false;
22
- if (this.reference) {
23
- this.reference.unref();
24
- }
25
- }
26
- start(milliseconds, handler) {
27
- this.stop();
28
- this.reference = setInterval(handler, milliseconds);
29
- if (this.shouldRef) {
30
- this.ref();
31
- }
32
- else {
33
- this.unref();
34
- }
35
- }
36
- stop() {
37
- if (this.hasStarted) {
38
- clearInterval(this.reference);
39
- this.reference = null;
40
- }
41
- }
42
- }
43
- exports.Interval = Interval;
44
- class Timeout {
45
- shouldRef = true;
46
- reference = null;
47
- get hasStarted() {
48
- return this.reference !== null;
49
- }
50
- hasRef() {
51
- return this.shouldRef;
52
- }
53
- ref() {
54
- this.shouldRef = true;
55
- if (this.reference) {
56
- this.reference.ref();
57
- }
58
- }
59
- unref() {
60
- this.shouldRef = false;
61
- if (this.reference) {
62
- this.reference.unref();
63
- }
64
- }
65
- start(milliseconds, handler, override = true) {
66
- if (!this.hasStarted || override) {
67
- this.stop();
68
- this.reference = setTimeout(() => {
69
- this.reference = null;
70
- handler();
71
- }, milliseconds);
72
- if (this.shouldRef) {
73
- this.ref();
74
- }
75
- else {
76
- this.unref();
77
- }
78
- }
79
- }
80
- stop() {
81
- if (this.hasStarted) {
82
- clearTimeout(this.reference);
83
- this.reference = null;
84
- }
85
- }
86
- }
87
- exports.Timeout = Timeout;
88
- function sleep(milliseconds) {
89
- return new Promise((resolve) => {
90
- setTimeout(resolve, milliseconds);
91
- });
92
- }
@@ -1,11 +0,0 @@
1
- export declare function normalize(object: {
2
- [key: string]: any;
3
- }): Readonly<{
4
- [key: string]: any;
5
- }>;
6
- export declare function guildIdToShardId(guildId: string, shardCount?: number): number;
7
- export type URIEncodeWrapFunc = (...args: Array<any>) => string;
8
- export type URIEncodeWrapped = {
9
- [key: string]: any;
10
- };
11
- export declare function URIEncodeWrap(unsafe: URIEncodeWrapped): URIEncodeWrapped;
package/lib/src/tools.js DELETED
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalize = normalize;
4
- exports.guildIdToShardId = guildIdToShardId;
5
- exports.URIEncodeWrap = URIEncodeWrap;
6
- function normalize(object) {
7
- for (const key in object) {
8
- object[key] = key;
9
- }
10
- return Object.freeze(object);
11
- }
12
- function guildIdToShardId(guildId, shardCount = 0) {
13
- return Math.round(+(guildId) / (1 << 22)) % shardCount;
14
- }
15
- const safeCharacter = '@';
16
- function URIEncodeWrap(unsafe) {
17
- const safe = {};
18
- for (let key in unsafe) {
19
- const path = unsafe[key];
20
- if (typeof (path) !== 'function') {
21
- safe[key] = path;
22
- continue;
23
- }
24
- safe[key] = ((...args) => {
25
- args = args.map((arg) => {
26
- if (!arg) {
27
- return arg;
28
- }
29
- const value = String(arg);
30
- if (!value.includes(safeCharacter)) {
31
- return encodeURIComponent(value);
32
- }
33
- return value.split('').map((char) => {
34
- return (char === safeCharacter) ? char : encodeURIComponent(char);
35
- }).join('');
36
- });
37
- return path(...args);
38
- });
39
- }
40
- return Object.freeze(safe);
41
- }