From 14ed9c38be6c30ffa61e000e9438f334dfc13e57 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 18 Sep 2026 13:18:49 -0500 Subject: [PATCH] feat(rate-limit): carry the error brand instead of extending core The original extended core's HttpError, which is a value import and makes the published plugin need core at run time. The brand is a registry-wide symbol, so core renders the 429 without this package depending on it, and it survives two copies of core in one tree where instanceof would not. The fields asserted here are exactly the ones HttpErrorLike declares. That core really renders them is proven end to end in the limiter's own tests, which read a 429 and its Retry-After back out of app.fetch. Signed-off-by: Diego Resendez --- packages/rate-limit/src/errors.ts | 33 +++++++++++++++++++++ packages/rate-limit/test/rate-limit.test.ts | 16 ++++++++++ 2 files changed, 49 insertions(+) create mode 100644 packages/rate-limit/src/errors.ts diff --git a/packages/rate-limit/src/errors.ts b/packages/rate-limit/src/errors.ts new file mode 100644 index 0000000..366d8d2 --- /dev/null +++ b/packages/rate-limit/src/errors.ts @@ -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; + + constructor(message: string, retryAfterSeconds: number) { + super(message); + this.name = 'TooManyRequests'; + this.body = { message, status: 429 }; + this.headers = { 'retry-after': String(retryAfterSeconds) }; + } +} diff --git a/packages/rate-limit/test/rate-limit.test.ts b/packages/rate-limit/test/rate-limit.test.ts index 81465b5..ac43e5e 100644 --- a/packages/rate-limit/test/rate-limit.test.ts +++ b/packages/rate-limit/test/rate-limit.test.ts @@ -1,6 +1,7 @@ import { strict as assert } from 'node:assert'; import { test } from 'node:test'; +import { TooManyRequests } from '../src/errors.ts'; import { MemoryStore } from '../src/memory-store.ts'; const sleep = (ms: number): Promise => 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()); }); + +// 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.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); +});