@spoosh/plugin-throttle 0.1.11 → 0.3.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.
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ interface ThrottleReadOptions {
4
4
  /** Throttle requests to max 1 per X milliseconds. Extras return cached data. */
5
5
  throttle?: number;
6
6
  }
7
- type ThrottleInfiniteReadOptions = ThrottleReadOptions;
7
+ type ThrottlePagesOptions = ThrottleReadOptions;
8
8
  type ThrottleWriteOptions = object;
9
9
  type ThrottleReadResult = object;
10
10
  type ThrottleWriteResult = object;
@@ -39,9 +39,9 @@ type ThrottleWriteResult = object;
39
39
  declare function throttlePlugin(): SpooshPlugin<{
40
40
  readOptions: ThrottleReadOptions;
41
41
  writeOptions: ThrottleWriteOptions;
42
- infiniteReadOptions: ThrottleInfiniteReadOptions;
42
+ pagesOptions: ThrottlePagesOptions;
43
43
  readResult: ThrottleReadResult;
44
44
  writeResult: ThrottleWriteResult;
45
45
  }>;
46
46
 
47
- export { type ThrottleInfiniteReadOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
47
+ export { type ThrottlePagesOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ interface ThrottleReadOptions {
4
4
  /** Throttle requests to max 1 per X milliseconds. Extras return cached data. */
5
5
  throttle?: number;
6
6
  }
7
- type ThrottleInfiniteReadOptions = ThrottleReadOptions;
7
+ type ThrottlePagesOptions = ThrottleReadOptions;
8
8
  type ThrottleWriteOptions = object;
9
9
  type ThrottleReadResult = object;
10
10
  type ThrottleWriteResult = object;
@@ -39,9 +39,9 @@ type ThrottleWriteResult = object;
39
39
  declare function throttlePlugin(): SpooshPlugin<{
40
40
  readOptions: ThrottleReadOptions;
41
41
  writeOptions: ThrottleWriteOptions;
42
- infiniteReadOptions: ThrottleInfiniteReadOptions;
42
+ pagesOptions: ThrottlePagesOptions;
43
43
  readResult: ThrottleReadResult;
44
44
  writeResult: ThrottleWriteResult;
45
45
  }>;
46
46
 
47
- export { type ThrottleInfiniteReadOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
47
+ export { type ThrottlePagesOptions, type ThrottleReadOptions, type ThrottleReadResult, type ThrottleWriteOptions, type ThrottleWriteResult, throttlePlugin };
package/dist/index.js CHANGED
@@ -25,27 +25,39 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/plugin.ts
28
+ var PLUGIN_NAME = "spoosh:throttle";
28
29
  function throttlePlugin() {
29
30
  const lastFetchTime = /* @__PURE__ */ new Map();
30
31
  return {
31
- name: "spoosh:throttle",
32
- operations: ["read", "infiniteRead"],
32
+ name: PLUGIN_NAME,
33
+ operations: ["read", "pages"],
33
34
  priority: 100,
34
35
  middleware: async (context, next) => {
36
+ const t = context.tracer?.(PLUGIN_NAME);
37
+ const et = context.eventTracer?.(PLUGIN_NAME);
35
38
  const pluginOptions = context.pluginOptions;
36
39
  const throttleMs = pluginOptions?.throttle;
37
40
  if (!throttleMs || throttleMs <= 0) {
41
+ t?.skip("No throttle configured");
38
42
  return next();
39
43
  }
40
- const { path, method } = context;
44
+ const { path, method, queryKey } = context;
41
45
  const stableKey = `${path}:${method}`;
42
46
  const now = Date.now();
43
47
  const lastTime = lastFetchTime.get(stableKey) ?? 0;
44
48
  const elapsed = now - lastTime;
45
49
  if (elapsed < throttleMs) {
50
+ const remaining = throttleMs - elapsed;
51
+ et?.emit(`Request blocked (${remaining}ms remaining)`, {
52
+ queryKey,
53
+ color: "warning",
54
+ meta: { throttle: throttleMs, elapsed, remaining }
55
+ });
56
+ t?.return("Throttled", { color: "warning" });
46
57
  return { data: void 0, status: 0 };
47
58
  }
48
59
  lastFetchTime.set(stableKey, now);
60
+ t?.log("Request allowed");
49
61
  return next();
50
62
  }
51
63
  };
package/dist/index.mjs CHANGED
@@ -1,25 +1,37 @@
1
1
  // src/plugin.ts
2
+ var PLUGIN_NAME = "spoosh:throttle";
2
3
  function throttlePlugin() {
3
4
  const lastFetchTime = /* @__PURE__ */ new Map();
4
5
  return {
5
- name: "spoosh:throttle",
6
- operations: ["read", "infiniteRead"],
6
+ name: PLUGIN_NAME,
7
+ operations: ["read", "pages"],
7
8
  priority: 100,
8
9
  middleware: async (context, next) => {
10
+ const t = context.tracer?.(PLUGIN_NAME);
11
+ const et = context.eventTracer?.(PLUGIN_NAME);
9
12
  const pluginOptions = context.pluginOptions;
10
13
  const throttleMs = pluginOptions?.throttle;
11
14
  if (!throttleMs || throttleMs <= 0) {
15
+ t?.skip("No throttle configured");
12
16
  return next();
13
17
  }
14
- const { path, method } = context;
18
+ const { path, method, queryKey } = context;
15
19
  const stableKey = `${path}:${method}`;
16
20
  const now = Date.now();
17
21
  const lastTime = lastFetchTime.get(stableKey) ?? 0;
18
22
  const elapsed = now - lastTime;
19
23
  if (elapsed < throttleMs) {
24
+ const remaining = throttleMs - elapsed;
25
+ et?.emit(`Request blocked (${remaining}ms remaining)`, {
26
+ queryKey,
27
+ color: "warning",
28
+ meta: { throttle: throttleMs, elapsed, remaining }
29
+ });
30
+ t?.return("Throttled", { color: "warning" });
20
31
  return { data: void 0, status: 0 };
21
32
  }
22
33
  lastFetchTime.set(stableKey, now);
34
+ t?.log("Request allowed");
23
35
  return next();
24
36
  }
25
37
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-throttle",
3
- "version": "0.1.11",
3
+ "version": "0.3.0",
4
4
  "description": "Request throttling plugin for Spoosh - limits request frequency",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,11 +33,11 @@
33
33
  }
34
34
  },
35
35
  "peerDependencies": {
36
- "@spoosh/core": ">=0.12.1"
36
+ "@spoosh/core": ">=0.15.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@spoosh/core": "0.12.1",
40
- "@spoosh/test-utils": "0.1.8"
39
+ "@spoosh/core": "0.15.0",
40
+ "@spoosh/test-utils": "0.3.0"
41
41
  },
42
42
  "scripts": {
43
43
  "dev": "tsup --watch",