@spoosh/plugin-throttle 0.1.11 → 0.2.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.js +14 -2
- package/dist/index.mjs +14 -2
- package/package.json +4 -4
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:
|
|
32
|
+
name: PLUGIN_NAME,
|
|
32
33
|
operations: ["read", "infiniteRead"],
|
|
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:
|
|
6
|
+
name: PLUGIN_NAME,
|
|
6
7
|
operations: ["read", "infiniteRead"],
|
|
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.
|
|
3
|
+
"version": "0.2.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.
|
|
36
|
+
"@spoosh/core": ">=0.13.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.
|
|
39
|
+
"@spoosh/core": "0.13.0",
|
|
40
|
+
"@spoosh/test-utils": "0.2.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|