Skip to content

Commit 68060c8

Browse files
committed
incorporate PR suggestions
1 parent de10070 commit 68060c8

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

packages/api/src/routers/api/clickhouseProxy.ts

+18-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { validateRequest } from 'zod-express-middleware';
55

66
import { getConnectionById } from '@/controllers/connection';
77
import { getNonNullUserWithTeam } from '@/middleware/auth';
8-
import { validateRequestHeaders } from '@/utils/validation';
8+
import { validateRequestHeaders } from '@/middleware/validation';
99
import { objectIdSchema } from '@/utils/zod';
1010

1111
const router = express.Router();
@@ -59,16 +59,15 @@ router.post(
5959
},
6060
);
6161

62-
function validation() {
63-
return validateRequestHeaders(
64-
z.object({
65-
'x-hyperdx-connection-id': objectIdSchema,
66-
}),
67-
);
68-
}
62+
const hasConnectionId = validateRequestHeaders(
63+
z.object({
64+
'x-hyperdx-connection-id': objectIdSchema,
65+
}),
66+
);
6967

70-
function getConnection(): RequestHandler {
71-
return async (req, res, next) => {
68+
const getConnection: RequestHandler =
69+
// prettier-ignore-next-line
70+
async (req, res, next) => {
7271
try {
7372
const { teamId } = getNonNullUserWithTeam(req);
7473
const connection_id = req.headers['x-hyperdx-connection-id']!; // ! because zod already validated
@@ -101,10 +100,10 @@ function getConnection(): RequestHandler {
101100
next(e);
102101
}
103102
};
104-
}
105103

106-
function proxyMiddleware(): RequestHandler {
107-
return createProxyMiddleware({
104+
const proxyMiddleware: RequestHandler =
105+
// prettier-ignore-next-line
106+
createProxyMiddleware({
108107
target: '', // doesn't matter. it should be overridden by the router
109108
changeOrigin: true,
110109
pathFilter: (path, _req) => {
@@ -122,11 +121,9 @@ function proxyMiddleware(): RequestHandler {
122121
on: {
123122
proxyReq: (proxyReq, _req) => {
124123
const newPath = _req.params[0];
125-
let qparams = '';
126-
const qIdx = _req.url.indexOf('?');
127-
if (qIdx >= 0) {
128-
qparams = _req.url.substring(qIdx);
129-
}
124+
// @ts-expect-error _req.query is type ParamQs, which doesn't play nicely with URLSearchParams. Replace with getting query params from _req.url eventually
125+
const qparams = new URLSearchParams(_req.query);
126+
qparams.delete('hyperdx_connection_id');
130127
if (_req._hdx_connection?.username && _req._hdx_connection?.password) {
131128
proxyReq.setHeader(
132129
'X-ClickHouse-User',
@@ -138,7 +135,7 @@ function proxyMiddleware(): RequestHandler {
138135
// TODO: Use fixRequestBody after this issue is resolved: https://github.com/chimurai/http-proxy-middleware/issues/1102
139136
proxyReq.write(_req.body);
140137
}
141-
proxyReq.path = `/${newPath}${qparams}`;
138+
proxyReq.path = `/${newPath}?${qparams}`;
142139
},
143140
proxyRes: (proxyRes, _req, res) => {
144141
// since clickhouse v24, the cors headers * will be attached to the response by default
@@ -175,9 +172,8 @@ function proxyMiddleware(): RequestHandler {
175172
// logger: console,
176173
// }),
177174
});
178-
}
179175

180-
router.get('/*', validation(), getConnection(), proxyMiddleware());
181-
router.post('/*', validation(), getConnection(), proxyMiddleware());
176+
router.get('/*', hasConnectionId, getConnection, proxyMiddleware);
177+
router.post('/*', hasConnectionId, getConnection, proxyMiddleware);
182178

183179
export default router;

packages/app/pages/api/[...all].ts

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const config = {
77
api: {
88
externalResolver: true,
99
bodyParser: false,
10-
responseLimit: '32mb',
1110
},
1211
};
1312

packages/app/src/sessions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ export function useRRWebEventStream(
370370
metadata,
371371
);
372372

373+
// TODO: Change ClickhouseClient class to use this under the hood,
374+
// and refactor this to use ClickhouseClient.query
373375
const clickhouseClient = createClient({
374376
clickhouse_settings: {
375377
date_time_output_format: 'iso',

packages/common-utils/src/clickhouse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class ClickhouseClient {
333333
headers['x-hyperdx-connection-id'] = connectionId;
334334
}
335335
// https://github.com/ClickHouse/clickhouse-js/blob/1ebdd39203730bb99fad4c88eac35d9a5e96b34a/packages/client-web/src/connection/web_connection.ts#L200C7-L200C23
336-
const response = await fetch(`${this.host}?${searchParams.toString()}`, {
336+
const response = await fetch(`${this.host}/?${searchParams.toString()}`, {
337337
...(includeCredentials ? { credentials: 'include' } : {}),
338338
signal: abort_signal,
339339
method: 'GET',

0 commit comments

Comments
 (0)