feat: rate-limit and metrics plugins #2
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* Core's error brand, as the public protocol rather than as an import.
|
||||||
|
*
|
||||||
|
* `Symbol.for` is registry-wide, so this resolves to the same symbol core registered, in this copy
|
||||||
|
* of the process and in any other. That is what lets a plugin throw something core renders without
|
||||||
|
* depending on core at runtime — and it survives two copies of core in one tree, which `instanceof`
|
||||||
|
* does not, since each copy has its own class identity.
|
||||||
|
*
|
||||||
|
* The string is the contract (design spec 1.2). `unique symbol` is what TypeScript needs for a
|
||||||
|
* computed class field, and a `const` annotated with it satisfies that.
|
||||||
|
*/
|
||||||
|
const HTTP_ERROR: unique symbol = Symbol.for('green-tea.http-error');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when a caller exceeds a rule. Carries `Retry-After` so a client can back off rather than
|
||||||
|
* retry immediately and spend the next window too.
|
||||||
|
*
|
||||||
|
* Extends `Error`, not core's `HttpError`: core reads `status`, `message`, `body` and `headers` off
|
||||||
|
* whatever carries the brand, and never uses `instanceof`.
|
||||||
|
*/
|
||||||
|
export class TooManyRequests extends Error {
|
||||||
|
readonly [HTTP_ERROR] = true;
|
||||||
|
readonly status = 429;
|
||||||
|
readonly body: { message: string; status: number };
|
||||||
|
readonly headers: Record<string, string>;
|
||||||
|
|
||||||
|
constructor(message: string, retryAfterSeconds: number) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'TooManyRequests';
|
||||||
|
this.body = { message, status: 429 };
|
||||||
|
this.headers = { 'retry-after': String(retryAfterSeconds) };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { strict as assert } from 'node:assert';
|
import { strict as assert } from 'node:assert';
|
||||||
import { test } from 'node:test';
|
import { test } from 'node:test';
|
||||||
|
|
||||||
|
import { TooManyRequests } from '../src/errors.ts';
|
||||||
import { MemoryStore } from '../src/memory-store.ts';
|
import { MemoryStore } from '../src/memory-store.ts';
|
||||||
|
|
||||||
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
@@ -66,3 +67,18 @@ test('close is idempotent, because close() is called more than once in real apps
|
|||||||
|
|
||||||
assert.doesNotThrow(() => store.close());
|
assert.doesNotThrow(() => store.close());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Convention rule 8, checked where it bites. The class never extends core's HttpError — it carries a
|
||||||
|
// registry-wide symbol, which is what lets a plugin throw a 429 that core renders without importing
|
||||||
|
// core, and what keeps working when two copies of core are in one tree (`instanceof` does not).
|
||||||
|
test('carries the brand core recognises, and the fields core reads off it', () => {
|
||||||
|
const error = new TooManyRequests('slow down', 30);
|
||||||
|
|
||||||
|
assert.equal((error as unknown as Record<symbol, unknown>)[Symbol.for('green-tea.http-error')], true);
|
||||||
|
assert.equal(error.status, 429);
|
||||||
|
assert.equal(error.message, 'slow down');
|
||||||
|
assert.equal(error.headers['retry-after'], '30');
|
||||||
|
assert.deepEqual(error.body, { message: 'slow down', status: 429 });
|
||||||
|
// Still an Error: a throw that is not one loses the stack, and core rethrows before rendering.
|
||||||
|
assert.ok(error instanceof Error);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user