@h1s97x/request-queue 0.1.1 → 0.1.2

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.
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const tokenBucket = require('@h1s97x/token-bucket');
4
+ const errors = require('@h1s97x/errors');
4
5
 
5
6
  var __defProp = Object.defineProperty;
6
7
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -52,7 +53,10 @@ class RequestQueueManager {
52
53
  const jobId = this.generateId();
53
54
  const waitTimeout = timeout ?? this.options.maxWaitTime;
54
55
  if (this.queue.length >= this.options.maxSize) {
55
- throw new Error(`QUEUE_FULL: Queue is full (${this.options.maxSize})`);
56
+ throw new errors.RateLimitError("Queue is full", {
57
+ code: "QUEUE_FULL",
58
+ context: { maxSize: this.options.maxSize, currentSize: this.queue.length }
59
+ });
56
60
  }
57
61
  this.stats.maxQueueLength = Math.max(
58
62
  this.stats.maxQueueLength,
@@ -66,7 +70,10 @@ class RequestQueueManager {
66
70
  this.stats.waiting--;
67
71
  }
68
72
  this.stats.timeout++;
69
- reject(new Error(`TIMEOUT: Task waited too long (${waitTimeout}ms)`));
73
+ reject(new errors.RateLimitError("Task waited too long", {
74
+ code: "QUEUE_TIMEOUT",
75
+ context: { waitTimeout }
76
+ }));
70
77
  }, waitTimeout);
71
78
  const job = {
72
79
  id: jobId,
@@ -176,7 +183,10 @@ class RequestQueueManager {
176
183
  clearTimeout(job.timeout);
177
184
  job.status = "timeout";
178
185
  this.stats.timeout++;
179
- job.reject(new Error(`CLEANUP: Task evicted after ${this.options.maxWaitTime}ms`));
186
+ job.reject(new errors.RateLimitError("Task evicted from queue", {
187
+ code: "QUEUE_EVICTED",
188
+ context: { maxWaitTime: this.options.maxWaitTime }
189
+ }));
180
190
  }
181
191
  }
182
192
  /**
@@ -195,7 +205,9 @@ class RequestQueueManager {
195
205
  clear() {
196
206
  for (const job of this.queue) {
197
207
  clearTimeout(job.timeout);
198
- job.reject(new Error("CLEARED: Queue has been cleared"));
208
+ job.reject(new errors.RateLimitError("Queue has been cleared", {
209
+ code: "QUEUE_CLEARED"
210
+ }));
199
211
  }
200
212
  this.queue = [];
201
213
  this.stats.waiting = 0;
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { TokenBucketRateLimiter } from '@h1s97x/token-bucket';
2
+ import { RateLimitError } from '@h1s97x/errors';
2
3
 
3
4
  var __defProp = Object.defineProperty;
4
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -50,7 +51,10 @@ class RequestQueueManager {
50
51
  const jobId = this.generateId();
51
52
  const waitTimeout = timeout ?? this.options.maxWaitTime;
52
53
  if (this.queue.length >= this.options.maxSize) {
53
- throw new Error(`QUEUE_FULL: Queue is full (${this.options.maxSize})`);
54
+ throw new RateLimitError("Queue is full", {
55
+ code: "QUEUE_FULL",
56
+ context: { maxSize: this.options.maxSize, currentSize: this.queue.length }
57
+ });
54
58
  }
55
59
  this.stats.maxQueueLength = Math.max(
56
60
  this.stats.maxQueueLength,
@@ -64,7 +68,10 @@ class RequestQueueManager {
64
68
  this.stats.waiting--;
65
69
  }
66
70
  this.stats.timeout++;
67
- reject(new Error(`TIMEOUT: Task waited too long (${waitTimeout}ms)`));
71
+ reject(new RateLimitError("Task waited too long", {
72
+ code: "QUEUE_TIMEOUT",
73
+ context: { waitTimeout }
74
+ }));
68
75
  }, waitTimeout);
69
76
  const job = {
70
77
  id: jobId,
@@ -174,7 +181,10 @@ class RequestQueueManager {
174
181
  clearTimeout(job.timeout);
175
182
  job.status = "timeout";
176
183
  this.stats.timeout++;
177
- job.reject(new Error(`CLEANUP: Task evicted after ${this.options.maxWaitTime}ms`));
184
+ job.reject(new RateLimitError("Task evicted from queue", {
185
+ code: "QUEUE_EVICTED",
186
+ context: { maxWaitTime: this.options.maxWaitTime }
187
+ }));
178
188
  }
179
189
  }
180
190
  /**
@@ -193,7 +203,9 @@ class RequestQueueManager {
193
203
  clear() {
194
204
  for (const job of this.queue) {
195
205
  clearTimeout(job.timeout);
196
- job.reject(new Error("CLEARED: Queue has been cleared"));
206
+ job.reject(new RateLimitError("Queue has been cleared", {
207
+ code: "QUEUE_CLEARED"
208
+ }));
197
209
  }
198
210
  this.queue = [];
199
211
  this.stats.waiting = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h1s97x/request-queue",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "FIFO request queue manager with rate limiting for Node.js and browser",
5
5
  "author": "h1s97x",
6
6
  "license": "MIT",
@@ -20,7 +20,8 @@
20
20
  ],
21
21
  "sideEffects": false,
22
22
  "dependencies": {
23
- "@h1s97x/token-bucket": "0.1.1"
23
+ "@h1s97x/token-bucket": "0.1.1",
24
+ "@h1s97x/errors": "0.2.0"
24
25
  },
25
26
  "devDependencies": {
26
27
  "vitest": "^2.1.9"