Skip to content

Commit

Permalink
Merge pull request #897 from pmcelhaney/fix-proxy
Browse files Browse the repository at this point in the history
fix #721 - .proxy on / .proxy off commands in REPL have no effect
  • Loading branch information
pmcelhaney authored May 7, 2024
2 parents bfb1e72 + 3e93665 commit c9227e9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-dancers-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fixed #721: .proxy on / .proxy off commands in REPL have no effect
1 change: 1 addition & 0 deletions bin/counterfact.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ program
.option(
"--prefix <string>",
"base path from which routes will be served (e.g. /api/v1)",
"",
)
.action(main)
// eslint-disable-next-line sonar/process-argv
Expand Down
11 changes: 10 additions & 1 deletion src/server/koa-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { IncomingHttpHeaders } from "node:http";

import createDebug from "debug";
import type Koa from "koa";
import koaProxy from "koa-proxy";

import type { Config } from "./config.js";
import type { Dispatcher } from "./dispatcher.js";
import type { HttpMethods } from "./registry.js";

const debug = createDebug("counterfact:server:create-koa-app");

const HTTP_STATUS_CODE_OK = 200;

function addCors(ctx: Koa.ExtendableContext, headers?: IncomingHttpHeaders) {
Expand Down Expand Up @@ -50,11 +54,16 @@ function getAuthObject(

export function koaMiddleware(
dispatcher: Dispatcher,
{ proxyEnabled = false, proxyUrl = "", routePrefix = "" } = {},
config: Config,
proxy = koaProxy,
): Koa.Middleware {
// eslint-disable-next-line max-statements
return async function middleware(ctx, next) {
const { proxyEnabled, proxyUrl, routePrefix } = config;

debug("middleware running for path: %s", ctx.request.path);
debug("routePrefix: %s", routePrefix);

if (!ctx.request.path.startsWith(routePrefix)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await next();
Expand Down
5 changes: 3 additions & 2 deletions src/server/page-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export function pageMiddleware(
.join(__dirname, `../client/${templateName}.html.hbs`)
.replaceAll("\\", "/");

debug("pathToHandlebarsTemplate: %s", pathToHandlebarsTemplate);

const render = Handlebars.compile(await readFile(pathToHandlebarsTemplate));

if (ctx.URL.pathname === pathname) {
debug("rendering page: %s", pathname);
debug("locals: %o", locals);

ctx.body = render(locals);

return;
Expand Down
23 changes: 16 additions & 7 deletions test/server/koa-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { Dispatcher } from "../../src/server/dispatcher.js";
import { koaMiddleware } from "../../src/server/koa-middleware.js";
import { Registry } from "../../src/server/registry.js";

const CONFIG = {
basePath: "",
openApiPath: "",
port: 9999,
proxyEnabled: false,
proxyUrl: "",
routePrefix: "",
};

const mockKoaProxy = (options: KoaProxy.Options | undefined) =>
function proxy(ctx: KoaContext) {
ctx.mockProxyHost = options?.host;
Expand All @@ -28,7 +37,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const ctx = {
req: {
Expand Down Expand Up @@ -65,7 +74,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
const ctx = {
request: { headers: {}, method: "GET", path: "/not-modified" },

Expand Down Expand Up @@ -93,7 +102,7 @@ describe("koa middleware", () => {
const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(
dispatcher,
{ proxyEnabled: true, proxyUrl: "https://example.com" },
{ ...CONFIG, proxyEnabled: true, proxyUrl: "https://example.com" },
mockKoaProxy,
);
const ctx = {
Expand Down Expand Up @@ -122,7 +131,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
const ctx = {
body: undefined,

Expand Down Expand Up @@ -172,7 +181,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
const ctx = {
body: undefined,

Expand Down Expand Up @@ -237,7 +246,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
const ctx = {
body: undefined,

Expand Down Expand Up @@ -318,7 +327,7 @@ describe("koa middleware", () => {
});

const dispatcher = new Dispatcher(registry, new ContextRegistry());
const middleware = koaMiddleware(dispatcher);
const middleware = koaMiddleware(dispatcher, CONFIG);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const ctx = {
req: {
Expand Down

0 comments on commit c9227e9

Please sign in to comment.