@sproutboat/wire 0.4.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @sproutboat/wire
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Rate limiter (baronunread/sproutboat#69): `env.<NAME>.limit({ key })` now
8
+ returns `{ success, resetAt }`. `resetAt` is epoch milliseconds for when the
9
+ fixed window rolls and the counter clears, returned on every call (hit or
10
+ miss), so a rejection can carry an accurate `Retry-After`:
11
+ `Math.ceil((resetAt - Date.now()) / 1000)`. The `ratelimit.check` op already
12
+ had `windowStart` and `period` in hand; it just stopped discarding them.
13
+ Additive: existing callers reading only `success` are unaffected.
14
+
3
15
  ## 0.4.0
4
16
 
5
17
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sproutboat/wire",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -745,7 +745,11 @@ test("ratelimit: fixed window per key, limit/period from the message (#69)", asy
745
745
  expect((await hit("a")).success).toBe(true); // 1
746
746
  expect((await hit("a")).success).toBe(true); // 2
747
747
  expect((await hit("a")).success).toBe(true); // 3
748
- expect((await hit("a")).success).toBe(false); // 4 — over
748
+ const over = await hit("a"); // 4 — over
749
+ expect(over.success).toBe(false);
750
+ // resetAt is epoch ms at the window edge, so it drives a Retry-After.
751
+ expect(over.resetAt).toBeGreaterThan(Date.now());
752
+ expect(over.resetAt).toBeLessThanOrEqual(Date.now() + 60_000);
749
753
  expect((await hit("b")).success).toBe(true); // a different key has its own count
750
754
 
751
755
  await expect(b.dispatch({ op: "ratelimit.check", name: "NOPE", key: "x", limit: 1, period: 60 })).rejects.toThrow(
package/src/broker.ts CHANGED
@@ -612,7 +612,7 @@ export function createBroker(opts: BrokerOptions = {}): Broker {
612
612
  "window_start = ?3 RETURNING count",
613
613
  )
614
614
  .get(name, msg.key == null ? "" : str(msg.key), windowStart);
615
- return { ok: true, success: (row?.count ?? 1) <= limit };
615
+ return { ok: true, success: (row?.count ?? 1) <= limit, resetAt: (windowStart + period) * 1000 };
616
616
  }
617
617
 
618
618
  case "r2.put": {