Compare commits
3
Commits
1da6bf2f99
...
c9e76fde2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9e76fde2d | ||
|
|
ae9e5529e8 | ||
|
|
5783212377 |
No files matched your search
@@ -148,6 +148,14 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
Without `trustProxy` it is ignored. Only `edgeHandler` reads it: the Node, Deno and Bun adapters, and a
|
Without `trustProxy` it is ignored. Only `edgeHandler` reads it: the Node, Deno and Bun adapters, and a
|
||||||
bare `app.fetch`, ignore it as before.
|
bare `app.fetch`, ignore it as before.
|
||||||
|
|
||||||
|
- **Mesh (alpha): `HEAD` on a remote `GET` route now works** (#125). The teacup resolved `HEAD` to
|
||||||
|
the remote `GET` and `OPTIONS` advertised `HEAD`, but the request went to the teapot as `HEAD`,
|
||||||
|
which has no `HEAD` plan, so it answered `404`. The teacup now forwards it as a `GET` and drops
|
||||||
|
the body, so it answers like a local route: the `GET`'s status and headers, no body, and a
|
||||||
|
`404`/`500` from the route keeps its status. An explicit remote `HEAD` route still takes
|
||||||
|
precedence. The teapot is unchanged, so a new teacup works against an older teapot during a
|
||||||
|
rolling deploy.
|
||||||
|
|
||||||
- **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard
|
- **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard
|
||||||
compared paths lexically, and the file read then followed symlinks, so `public/leak -> /etc/hosts`
|
compared paths lexically, and the file read then followed symlinks, so `public/leak -> /etc/hosts`
|
||||||
was served. Containment now compares real paths: the requested file, and a directory's
|
was served. Containment now compares real paths: the requested file, and a directory's
|
||||||
|
|||||||
+5
-1
@@ -1148,7 +1148,11 @@ async function spliceRemoteScopes(
|
|||||||
// protocol never declared, so a teapot could come to rely on something we never promised.
|
// protocol never declared, so a teapot could come to rely on something we never promised.
|
||||||
handler: async (req) =>
|
handler: async (req) =>
|
||||||
route.handler({
|
route.handler({
|
||||||
method: req.method,
|
// HEAD reaches a remote GET through the implicit-HEAD fallback, and the teapot looks
|
||||||
|
// its plan up by method, so a forwarded HEAD finds none and answers 404. Send it as the
|
||||||
|
// GET it resolved to; dispatch drops the body for HEAD. Fixed teacup-side (D16) so an
|
||||||
|
// unchanged, older teapot still answers it during a rolling deploy.
|
||||||
|
method: req.method === 'HEAD' && route.method === 'GET' ? 'GET' : req.method,
|
||||||
params: req.params,
|
params: req.params,
|
||||||
query: req.query,
|
query: req.query,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
|
|||||||
@@ -112,3 +112,39 @@ test('mesh on Bun: a remote step colliding with a local token fails the splice',
|
|||||||
tServer.stop(true);
|
tServer.stop(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// CARD-20 (#125, D16): HEAD on a remote GET answers like HEAD on a local one.
|
||||||
|
@Route('/remote')
|
||||||
|
class RemoteHeadCtl {
|
||||||
|
@Get('/x', { export: true })
|
||||||
|
x() {
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteHeadCtl] })
|
||||||
|
class RemoteHeadTeapotModule {}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class EmptyTeacupModule {}
|
||||||
|
|
||||||
|
test('mesh on Bun: HEAD on a remote GET answers 200 with the GET headers and no body', async () => {
|
||||||
|
const teapot = createApp({ modules: [RemoteHeadTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||||
|
const tServer = await serveBun(teapot, { port: 0 });
|
||||||
|
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [EmptyTeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
mesh: { teapots: [{ url: controlUrl(tServer.port), secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
const cServer = await serveBun(teacup, { port: 0 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`http://127.0.0.1:${cServer.port}/api/remote/x`, { method: 'HEAD' });
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type') ?? '').toContain('application/json');
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
} finally {
|
||||||
|
await teacup.close();
|
||||||
|
cServer.stop(true);
|
||||||
|
tServer.stop(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -142,3 +142,39 @@ Deno.test('mesh on Deno: a remote step colliding with a local token fails the sp
|
|||||||
await t.server.shutdown();
|
await t.server.shutdown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// CARD-20 (#125, D16): HEAD on a remote GET answers like HEAD on a local one.
|
||||||
|
@Route('/remote')
|
||||||
|
class RemoteHeadCtl {
|
||||||
|
@Get('/x', { export: true })
|
||||||
|
x() {
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteHeadCtl] })
|
||||||
|
class RemoteHeadTeapotModule {}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class EmptyTeacupModule {}
|
||||||
|
|
||||||
|
Deno.test('mesh on Deno: HEAD on a remote GET answers 200 with the GET headers and no body', async () => {
|
||||||
|
const teapot = createApp({ modules: [RemoteHeadTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||||
|
const t = await serve(teapot);
|
||||||
|
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [EmptyTeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
mesh: { teapots: [{ url: controlUrl(t.port), secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
const c = await serve(teacup);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`http://127.0.0.1:${c.port}/api/remote/x`, { method: 'HEAD' });
|
||||||
|
assertEquals(res.status, 200);
|
||||||
|
assertStringIncludes(res.headers.get('content-type') ?? '', 'application/json');
|
||||||
|
assertEquals(await res.text(), '');
|
||||||
|
} finally {
|
||||||
|
await teacup.close();
|
||||||
|
await c.server.shutdown();
|
||||||
|
await t.server.shutdown();
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/**
|
||||||
|
* CARD-20 (#125, D16): `HEAD` on a remote buffered `GET` behaves like `HEAD` on a local one.
|
||||||
|
*
|
||||||
|
* The teacup resolves `HEAD` to the remote `GET` (implicit HEAD) and forwards it to the teapot as a
|
||||||
|
* `GET`, then drops the body. The teapot is unchanged, so an older teapot works too.
|
||||||
|
*/
|
||||||
|
import 'reflect-metadata';
|
||||||
|
import http from 'http';
|
||||||
|
import type { AddressInfo } from 'net';
|
||||||
|
import { describe, it, expect, afterEach } from 'vitest';
|
||||||
|
import { createApp, Route, Get, Head, Module, NotFound, ctx } from '../../src/index';
|
||||||
|
import type { App } from '../../src/app/types';
|
||||||
|
import type { Logger } from '../../src/logger';
|
||||||
|
|
||||||
|
const SECRET = 'head-secret';
|
||||||
|
const quiet: Logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} };
|
||||||
|
|
||||||
|
const seen: string[] = [];
|
||||||
|
|
||||||
|
@Route('/')
|
||||||
|
class RemoteCtl {
|
||||||
|
@Get('/x', { export: true }) x(@ctx() c: { req: { method: string } }) {
|
||||||
|
seen.push(`x ${c.req.method}`);
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
@Get('/missing', { export: true }) missing() {
|
||||||
|
seen.push('missing');
|
||||||
|
throw new NotFound();
|
||||||
|
}
|
||||||
|
@Get('/boom', { export: true }) boom() {
|
||||||
|
seen.push('boom');
|
||||||
|
throw new Error('kaboom');
|
||||||
|
}
|
||||||
|
@Get('/both', { export: true }) bothGet() {
|
||||||
|
seen.push('both GET');
|
||||||
|
return { via: 'get' };
|
||||||
|
}
|
||||||
|
@Head('/both', { export: true }) bothHead(@ctx() c: { req: { method: string } }) {
|
||||||
|
seen.push(`both HEAD as ${c.req.method}`);
|
||||||
|
return { via: 'head' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteCtl] })
|
||||||
|
class TeapotModule {}
|
||||||
|
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class TeacupModule {}
|
||||||
|
|
||||||
|
const open: App[] = [];
|
||||||
|
afterEach(async () => {
|
||||||
|
while (open.length) await open.pop()!.close();
|
||||||
|
seen.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
async function mesh(): Promise<{ teacup: App; port: number }> {
|
||||||
|
const teapot = createApp({ modules: [TeapotModule], experimental: true, logger: quiet, mesh: { secret: SECRET } });
|
||||||
|
open.push(teapot);
|
||||||
|
const tPort = ((await teapot.listen(0)).address() as AddressInfo).port;
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [TeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
logger: quiet,
|
||||||
|
mesh: { teapots: [{ url: `ws://127.0.0.1:${tPort}/__mesh__/control`, secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
open.unshift(teacup); // closed first
|
||||||
|
const port = ((await teacup.listen(0)).address() as AddressInfo).port;
|
||||||
|
return { teacup, port };
|
||||||
|
}
|
||||||
|
|
||||||
|
function nodeRequest(port: number, method: string, path: string) {
|
||||||
|
return new Promise<{ status: number; headers: http.IncomingHttpHeaders; body: string }>((resolve, reject) => {
|
||||||
|
const req = http.request({ host: '127.0.0.1', port, method, path, agent: false }, (res) => {
|
||||||
|
let body = '';
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
res.on('data', (chunk) => (body += chunk));
|
||||||
|
res.on('end', () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body }));
|
||||||
|
});
|
||||||
|
req.on('error', reject);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchVia = (app: App, method: string, path: string) =>
|
||||||
|
app.fetch(new Request(`http://mesh.test${path}`, { method }));
|
||||||
|
|
||||||
|
describe('mesh: HEAD on a remote GET route (CARD-20, D16)', () => {
|
||||||
|
it('T1: HEAD via app.fetch answers 200 with the GET headers and no body', async () => {
|
||||||
|
const { teacup } = await mesh();
|
||||||
|
const get = await fetchVia(teacup, 'GET', '/api/x');
|
||||||
|
const head = await fetchVia(teacup, 'HEAD', '/api/x');
|
||||||
|
expect(head.status).toBe(200);
|
||||||
|
expect(head.headers.get('content-type')).toBe(get.headers.get('content-type'));
|
||||||
|
expect(head.headers.get('content-type')).toContain('application/json');
|
||||||
|
expect(await head.text()).toBe('');
|
||||||
|
expect(await get.json()).toEqual({ a: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T2: HEAD via the Node listener answers 200 with the GET headers and no body', async () => {
|
||||||
|
const { port } = await mesh();
|
||||||
|
const get = await nodeRequest(port, 'GET', '/api/x');
|
||||||
|
const head = await nodeRequest(port, 'HEAD', '/api/x');
|
||||||
|
expect(head.status).toBe(200);
|
||||||
|
expect(head.headers['content-type']).toBe(get.headers['content-type']);
|
||||||
|
expect(head.body).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('OPTIONS still advertises GET, HEAD, OPTIONS', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
expect((await fetchVia(teacup, 'OPTIONS', '/api/x')).headers.get('allow')).toBe('GET, HEAD, OPTIONS');
|
||||||
|
expect((await nodeRequest(port, 'OPTIONS', '/api/x')).headers.allow).toBe('GET, HEAD, OPTIONS');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T3: an explicit remote HEAD route keeps precedence over the implicit fallback', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/both');
|
||||||
|
expect(viaFetch.status).toBe(200);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/both');
|
||||||
|
expect(viaNode.status).toBe(200);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
// The explicit HEAD handler ran, reached as HEAD — the GET handler never did.
|
||||||
|
expect(seen).toEqual(['both HEAD as HEAD', 'both HEAD as HEAD']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T4: a remote GET that throws NotFound answers HEAD with 404 and no body', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/missing');
|
||||||
|
expect(viaFetch.status).toBe(404);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/missing');
|
||||||
|
expect(viaNode.status).toBe(404);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
// The teapot handler ran: the 404 is the route's, not a missing HEAD plan on the teapot.
|
||||||
|
expect(seen).toEqual(['missing', 'missing']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('a remote GET that fails answers HEAD with 500 and no body', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/boom');
|
||||||
|
expect(viaFetch.status).toBe(500);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/boom');
|
||||||
|
expect(viaNode.status).toBe(500);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T5: one HEAD calls the teapot handler once, as a GET', async () => {
|
||||||
|
const { teacup } = await mesh();
|
||||||
|
await fetchVia(teacup, 'HEAD', '/api/x');
|
||||||
|
expect(seen).toEqual(['x GET']);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -206,7 +206,7 @@
|
|||||||
"adapter": "fetch",
|
"adapter": "fetch",
|
||||||
"method": "HEAD",
|
"method": "HEAD",
|
||||||
"path": "/api/x",
|
"path": "/api/x",
|
||||||
"status": 404,
|
"status": 200,
|
||||||
"headers": {
|
"headers": {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
"referrer-policy": "no-referrer",
|
"referrer-policy": "no-referrer",
|
||||||
@@ -472,7 +472,7 @@
|
|||||||
"adapter": "node",
|
"adapter": "node",
|
||||||
"method": "HEAD",
|
"method": "HEAD",
|
||||||
"path": "/api/x",
|
"path": "/api/x",
|
||||||
"status": 404,
|
"status": 200,
|
||||||
"headers": {
|
"headers": {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
"referrer-policy": "no-referrer",
|
"referrer-policy": "no-referrer",
|
||||||
|
|||||||
@@ -141,9 +141,9 @@ class RemoteMixCtl {
|
|||||||
return { from: 'teapot', route: 'POST /api/v/:id' };
|
return { from: 'teapot', route: 'POST /api/v/:id' };
|
||||||
}
|
}
|
||||||
// Remote only: remote routes are `buffer`, so the teacup's router resolves HEAD to it implicitly.
|
// Remote only: remote routes are `buffer`, so the teacup's router resolves HEAD to it implicitly.
|
||||||
// Captured as observed at ed47836: the teacup forwards the request as HEAD, the teapot looks its
|
// Captured at ed47836 as HEAD → 404 (the teacup forwarded HEAD, and the teapot has no HEAD plan).
|
||||||
// plan up by (pattern, method) and has none for HEAD, so HEAD answers 404 while OPTIONS still
|
// CARD-20 (#125, D16) changed that deliberately, with owner approval: the teacup forwards the
|
||||||
// advertises HEAD. That is the mesh proxy's behaviour, not the matcher's; CARD-07 must keep it.
|
// HEAD as a GET and drops the body, so HEAD now answers 200 like a local route.
|
||||||
@Get('/x', { export: true }) x() {
|
@Get('/x', { export: true }) x() {
|
||||||
return { from: 'teapot', route: 'GET /api/x' };
|
return { from: 'teapot', route: 'GET /api/x' };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user