perf(http): resolve ctx.ip on first read instead of on every request
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 34s

Every Bun request called server.requestIP() for ctx.ip (#116), a native
call that builds an object, whether anything read the address or not. On
Mac that measured 7-11% of a small request; recording the server in the
peer map instead costs nothing.

The neutral request now carries a resolver rather than an address. The
handler request and the pipeline seed expose ctx.ip as an accessor that
resolves once on first read, and a step may still assign it. runPipeline
uses the seed it is given instead of spreading it, which would have
evaluated the accessor; both callers build the seed fresh per request.

Inside a stream body ctx.ip is '' unless a step or the handler read it
before the stream was returned: the body runs after the response has
started, when the socket may be gone.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-28 14:31:20 -05:00
1 parent 109297ca4c
commit b86cd605c4
9 files changed
+283 -57

No files matched your search

+97 -5
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest';
import { createApp, Provider, Route, Get, Post, Module, needs, body, ctx } from '../src/index';
import { createApp, Provider, Step, Route, Get, Post, Sse, Module, needs, body, ctx } from '../src/index';
import { countingStream } from './helpers/counting-stream';
import { setPeer } from '../src/http/peer';
@@ -153,7 +153,7 @@ describe('app.fetch trustProxy gates x-forwarded-for', () => {
expect(copy.peerOf(request)).toBe('192.0.2.1');
});
it('T8: only the adapter\'s own Request carries the peer — a copy of it does not', async () => {
it("T8: only the adapter's own Request carries the peer — a copy of it does not", async () => {
const original = new Request('http://x/ip/whoami');
setPeer(original, '127.0.0.1');
@@ -163,6 +163,100 @@ describe('app.fetch trustProxy gates x-forwarded-for', () => {
});
});
// `ctx.ip` resolves on its first read. A peer recorded as a resolver, as Bun records its server, is
// asked only when something reads the address, at most once per request, and not from inside a
// stream body, which runs after the response has started.
@Route('/lazy')
class LazyIpCtl {
@Get('/none') none() {
return { ok: true };
}
@Get('/twice') twice(@ctx() c: any) {
return { ip: c.ip, again: c.ip };
}
@Sse('/stream') stream(@ctx() c: any) {
return (async function* () {
yield c.ip === '' ? 'empty' : c.ip;
})();
}
}
@Module({ mountpoint: '/', controllers: [LazyIpCtl] })
class LazyIpModule {}
@Step({ provides: 'seen' })
class SeesIp {
run(c: any) {
return { seen: c.ip };
}
}
@Route('/early')
class EarlyIpCtl {
@Sse('/stream') stream(@needs('seen') seen: string, @ctx() c: any) {
return (async function* () {
yield `${seen}|${c.ip}`;
})();
}
}
@Module({ mountpoint: '/', steps: [SeesIp], controllers: [EarlyIpCtl] })
class EarlyIpModule {}
@Step({ provides: 'ip' })
class AssignsIp {
run() {
return { ip: '198.51.100.1' };
}
}
@Route('/assigned')
class AssignedIpCtl {
@Get('/') whoami(@needs('ip') ip: string, @ctx() c: any) {
return { ip, ctx: c.ip };
}
}
@Module({ mountpoint: '/', steps: [AssignsIp], controllers: [AssignedIpCtl] })
class AssignedIpModule {}
describe('ctx.ip is resolved only when read', () => {
const resolver = () => {
const source = { calls: 0, requestIP: () => (source.calls++, { address: '203.0.113.9' }) };
return source;
};
const via = async (app: ReturnType<typeof createApp>, path: string) => {
const request = new Request(`http://x${path}`);
const peer = resolver();
setPeer(request, peer);
const res = await app.fetch(request);
return { text: await res.text(), calls: peer.calls };
};
it('a route that never reads ctx.ip never asks for the peer', async () => {
expect((await via(createApp({ modules: [LazyIpModule] }), '/lazy/none')).calls).toBe(0);
});
it('reading ctx.ip twice asks once', async () => {
const { text, calls } = await via(createApp({ modules: [LazyIpModule] }), '/lazy/twice');
expect(JSON.parse(text)).toEqual({ ip: '203.0.113.9', again: '203.0.113.9' });
expect(calls).toBe(1);
});
it('inside a stream body ctx.ip is empty, and the peer is never asked', async () => {
const { text, calls } = await via(createApp({ modules: [LazyIpModule] }), '/lazy/stream');
expect(text).toContain('data: "empty"');
expect(calls).toBe(0);
});
it('a step that reads ctx.ip before the stream starts keeps it for the stream', async () => {
const { text, calls } = await via(createApp({ modules: [EarlyIpModule] }), '/early/stream');
expect(text).toContain('data: "203.0.113.9|203.0.113.9"');
expect(calls).toBe(1);
});
it('a step may assign ctx.ip, and the peer is then never asked', async () => {
const { text, calls } = await via(createApp({ modules: [AssignedIpModule] }), '/assigned');
expect(JSON.parse(text)).toEqual({ ip: '198.51.100.1', ctx: '198.51.100.1' });
expect(calls).toBe(0);
});
});
describe('app.fetch on a mesh app', () => {
// Previously this asserted fetch() *threw* until listen() ran, which made mesh Node-only:
// Deno/Bun/edge serve through app.fetch and can never call listen() (it builds an http.Server).
@@ -276,9 +370,7 @@ describe('app.fetch bounded body reads (#105)', () => {
});
it('T9: a route limit above the server limit wins', async () => {
const res = await limitsApp.fetch(
post('/limits/large', 'x'.repeat(1_500_000), { 'content-type': 'text/plain' }),
);
const res = await limitsApp.fetch(post('/limits/large', 'x'.repeat(1_500_000), { 'content-type': 'text/plain' }));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ length: 1_500_000 });