Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions playgrounds/elysia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# WebDecoy and Elysia

This demo passes Bun's incoming requests through the WebDecoy Fetch guard
before Elysia handles them. The rules run in process and need no WebDecoy
account or API key. The server binds to loopback.

## Run locally

```sh
bun install --frozen-lockfile
bun run start
```

In a second terminal:

```sh
curl -i http://127.0.0.1:3000/
curl -i http://127.0.0.1:3000/demo-decoy
```

The ordinary route returns 200 and logs `ALLOW`. The demo path has no Elysia
route and returns Elysia's usual 404, while the guard logs `DENY` in monitor
mode. The decision changes the response only when enforcement is enabled.

Run `bun test` to verify both modes against a loopback server on an ephemeral
port. The demo uses the socket peer IP and trusts zero forwarding hops.
Configure trusted proxies deliberately if deploying behind a reverse proxy.
50 changes: 50 additions & 0 deletions playgrounds/elysia/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions playgrounds/elysia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "webdecoy-elysia-playground",
"private": true,
"type": "module",
"scripts": {
"start": "bun server.ts",
"test": "bun test"
},
"dependencies": {
"@webdecoy/node": "0.31.0",
"elysia": "1.4.32"
}
}
22 changes: 22 additions & 0 deletions playgrounds/elysia/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test } from 'bun:test';
import { startDemo } from './server';

test.each([
['monitor', 404],
['enforce', 403],
] as const)('%s keeps the ordinary route available', async (mode, decoyStatus) => {
const decisions: Array<[string, string]> = [];
const server = startDemo({ mode, port: 0, onDecision: (path, verdict) => decisions.push([path, verdict]) });
try {
const ordinary = await fetch(server.url);
expect(ordinary.status).toBe(200);
expect(await ordinary.text()).toBe('Hello from Elysia');

const decoy = await fetch(new URL('/demo-decoy', server.url));
expect(decoy.status).toBe(decoyStatus);
await decoy.text();
expect(decisions).toEqual([['/', 'ALLOW'], ['/demo-decoy', 'DENY']]);
} finally {
await server.stop(true);
}
});
32 changes: 32 additions & 0 deletions playgrounds/elysia/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createFetchGuard, tripwire } from '@webdecoy/node';
import { Elysia } from 'elysia';

interface DemoOptions {
port?: number;
mode?: 'monitor' | 'enforce';
onDecision?: (path: string, conclusion: string) => void;
}

export function startDemo({ port = 3000, mode = 'monitor', onDecision }: DemoOptions = {}) {
const app = new Elysia().get('/', () => 'Hello from Elysia');
const guard = createFetchGuard({
mode,
trustProxy: 0,
rules: [tripwire({ paths: ['/demo-decoy'], includeDefaults: false })],
});

return Bun.serve({
hostname: '127.0.0.1',
port,
async fetch(request, server) {
const { decision, response } = await guard.check(request, server.requestIP(request)?.address);
onDecision?.(new URL(request.url).pathname, decision.conclusion);
return response ?? app.handle(request);
},
});
}

if (import.meta.main) {
const server = startDemo({ onDecision: (path, verdict) => console.log(verdict, path) });
console.log(`Elysia demo listening at ${server.url}`);
}
Loading