refactor: remove unused DISPATCHER interface and related code
This commit is contained in:
@@ -2,12 +2,7 @@ export interface FetcherLike {
|
||||
fetch(request: Request): Promise<Response>;
|
||||
}
|
||||
|
||||
export interface DispatchNamespaceLike {
|
||||
get(name: string, scriptArgs?: Record<string, unknown>, options?: Record<string, unknown>): FetcherLike;
|
||||
}
|
||||
|
||||
export interface Env {
|
||||
AUTH: FetcherLike;
|
||||
DISPATCHER?: DispatchNamespaceLike;
|
||||
DEFAULT_WORKER_NAME: string;
|
||||
}
|
||||
|
||||
17
src/index.ts
17
src/index.ts
@@ -18,21 +18,10 @@ app.all("*", async (c) => {
|
||||
return c.env.AUTH.fetch(c.req.raw);
|
||||
}
|
||||
|
||||
if (!c.env.DISPATCHER) {
|
||||
return c.json({
|
||||
ok: true,
|
||||
routedTo: target.workerName,
|
||||
});
|
||||
}
|
||||
|
||||
const worker = c.env.DISPATCHER.get(target.workerName, {}, {
|
||||
limits: {
|
||||
cpuMs: 20,
|
||||
subRequests: 20,
|
||||
},
|
||||
return c.json({
|
||||
ok: true,
|
||||
routedTo: target.workerName,
|
||||
});
|
||||
|
||||
return worker.fetch(c.req.raw);
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -43,7 +43,7 @@ describe("cfw-gateway worker", () => {
|
||||
expect(auth.fetch).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("routes non-auth requests without consulting auth service when dispatcher is absent", async () => {
|
||||
it("routes non-auth requests without consulting auth service", async () => {
|
||||
const auth = createAuthService(401, { authenticated: false });
|
||||
const env: Env = {
|
||||
AUTH: auth,
|
||||
@@ -58,39 +58,4 @@ describe("cfw-gateway worker", () => {
|
||||
});
|
||||
expect(auth.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("dispatches non-auth requests without injecting identity headers", async () => {
|
||||
const dispatchedFetch = vi.fn(async (request: Request) => {
|
||||
return Response.json({
|
||||
authorization: request.headers.get("authorization"),
|
||||
requestId: request.headers.get("x-request-id"),
|
||||
});
|
||||
});
|
||||
const auth = createAuthService(401, { authenticated: false });
|
||||
const env: Env = {
|
||||
AUTH: auth,
|
||||
DISPATCHER: {
|
||||
get: vi.fn(() => ({ fetch: dispatchedFetch })),
|
||||
},
|
||||
DEFAULT_WORKER_NAME: "customer-worker-1",
|
||||
};
|
||||
|
||||
const response = await worker.fetch(
|
||||
new Request("http://gateway.local/app", {
|
||||
headers: {
|
||||
authorization: "Bearer real-client-token",
|
||||
"x-request-id": "client-request-1",
|
||||
},
|
||||
}),
|
||||
env,
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
await expect(response.json()).resolves.toEqual({
|
||||
authorization: "Bearer real-client-token",
|
||||
requestId: "client-request-1",
|
||||
});
|
||||
expect(auth.fetch).not.toHaveBeenCalled();
|
||||
expect(dispatchedFetch).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
"$schema": "node_modules/wrangler/config-schema.json",
|
||||
"name": "cfw-gateway",
|
||||
"main": "src/index.ts",
|
||||
"account_id": "67720b647ff2b55cf37ba3ef9e677083",
|
||||
"compatibility_date": "2026-06-10",
|
||||
"compatibility_flags": ["nodejs_compat"],
|
||||
"compatibility_flags": [
|
||||
"nodejs_compat"
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"pattern": "cfw-gateway.bowong.cc",
|
||||
@@ -18,11 +21,5 @@
|
||||
"binding": "AUTH",
|
||||
"service": "cfw-auth"
|
||||
}
|
||||
],
|
||||
"dispatch_namespaces": [
|
||||
{
|
||||
"binding": "DISPATCHER",
|
||||
"namespace": "platform-workers"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user