@imvu/bot 0.0.0-stage → 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,76 @@
1
+ export interface RateLimit {
2
+ /** Messages allowed per window. */
3
+ messages: number;
4
+ /** Window length in milliseconds. */
5
+ perMs: number;
6
+ }
7
+
8
+ /**
9
+ * A sliding-window send queue. Tasks run in order, never more than `messages` per
10
+ * `perMs`; once `maxQueue` tasks are waiting, new ones are rejected rather than
11
+ * piling up behind a flood.
12
+ */
13
+ export class RateLimiter {
14
+ private readonly sentAt: number[] = [];
15
+ private readonly waiting: Array<() => void> = [];
16
+ private draining = false;
17
+ private timer?: ReturnType<typeof setTimeout>;
18
+ private stopped = false;
19
+
20
+ public constructor(
21
+ private readonly limit: RateLimit,
22
+ private readonly maxQueue = 20,
23
+ private readonly now: () => number = Date.now
24
+ ) {
25
+ if (limit.messages < 1 || limit.perMs < 0) throw new RangeError('Invalid rate limit');
26
+ }
27
+
28
+ public get pending(): number {
29
+ return this.waiting.length;
30
+ }
31
+
32
+ public schedule<T>(task: () => Promise<T>): Promise<T> {
33
+ if (this.stopped) return Promise.reject(new Error('The rate limiter has stopped'));
34
+ if (this.waiting.length >= this.maxQueue) {
35
+ return Promise.reject(new Error(`Outgoing queue is full (${this.maxQueue} waiting)`));
36
+ }
37
+
38
+ return new Promise<T>((resolve, reject) => {
39
+ this.waiting.push(() => {
40
+ task().then(resolve, reject);
41
+ });
42
+ this.drain();
43
+ });
44
+ }
45
+
46
+ /** Reject nothing already sent; drop everything still waiting. */
47
+ public stop(): void {
48
+ this.stopped = true;
49
+ this.waiting.length = 0;
50
+ clearTimeout(this.timer);
51
+ }
52
+
53
+ private drain(): void {
54
+ if (this.draining || this.stopped) return;
55
+ this.draining = true;
56
+
57
+ while (this.waiting.length > 0) {
58
+ const now = this.now();
59
+ while (this.sentAt.length > 0 && now - this.sentAt[0] >= this.limit.perMs) this.sentAt.shift();
60
+
61
+ if (this.sentAt.length >= this.limit.messages) {
62
+ const wait = this.limit.perMs - (now - this.sentAt[0]);
63
+ this.timer = setTimeout(() => {
64
+ this.draining = false;
65
+ this.drain();
66
+ }, wait);
67
+ return;
68
+ }
69
+
70
+ this.sentAt.push(now);
71
+ this.waiting.shift()!();
72
+ }
73
+
74
+ this.draining = false;
75
+ }
76
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './Bot.js';
2
+ export * from './parse.js';
3
+ export * from './RateLimiter.js';
package/src/parse.ts ADDED
@@ -0,0 +1,27 @@
1
+ export interface ParsedCommand {
2
+ /** Lower-cased command name, without the prefix. */
3
+ name: string;
4
+ /** Whitespace-separated arguments; "double" or 'single' quotes group words. */
5
+ args: string[];
6
+ /** Everything after the command name, as typed. */
7
+ argText: string;
8
+ }
9
+
10
+ /** Parse `!name arg "two words"`, or return null when `text` is not a command. */
11
+ export function parseCommand(text: string, prefix: string): ParsedCommand | null {
12
+ const trimmed = text.trim();
13
+ if (!prefix || !trimmed.startsWith(prefix)) return null;
14
+
15
+ const body = trimmed.slice(prefix.length);
16
+ const match = /^(\S+)\s*([\s\S]*)$/.exec(body);
17
+ if (!match) return null;
18
+
19
+ return { name: match[1].toLowerCase(), args: splitArgs(match[2]), argText: match[2] };
20
+ }
21
+
22
+ function splitArgs(text: string): string[] {
23
+ const args: string[] = [];
24
+ const pattern = /"([^"]*)"|'([^']*)'|(\S+)/g;
25
+ for (const match of text.matchAll(pattern)) args.push(match[1] ?? match[2] ?? match[3]);
26
+ return args;
27
+ }