Skip to content

Commit 00a03aa

Browse files
author
Andrea Cosentino
committed
fix: various
1 parent 0a06245 commit 00a03aa

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/utils/plugin.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,8 @@ export const runPlugin = async (req: IncomingMessage, response: ServerResponse,
689689
}
690690
break;
691691
case "MANUALLY_HANDLED":
692-
dataResponse.data = `${error.message} Handled request did not send any response`;
693-
dataResponse.errorMiddlewares = undefined;
692+
settleReject("MANUALLY_HANDLED");
693+
callReject = true;
694694
break;
695695
case "ERROR":
696696
dataResponse.status = error.getCode();
@@ -724,6 +724,10 @@ export const runPlugin = async (req: IncomingMessage, response: ServerResponse,
724724
logger.debug(`runPlugin: runInternalPlugin error`);
725725
if (error === "next") {
726726
next();
727+
} else if (error === "MANUALLY_HANDLED") {
728+
if (!response.writableEnded) {
729+
response.end();
730+
}
727731
} else {
728732
next(error);
729733
}
@@ -797,18 +801,21 @@ export const runWsPlugin = (server: ViteDevServer | PreviewServer, logger: ILogg
797801
const endpointNoPrefix = Utils.request.removeEndpointPrefix(url.pathname, options.endpointPrefix);
798802

799803
let handler: typeof wsHandlers[number] | null = null;
804+
let wsParams: Record<string, string> | null = null;
800805
for (const handle of wsHandlers) {
806+
const candidateParams: Record<string, string> = {};
801807
const matched = options.matcher.doMatch(
802808
Utils.request.addSlash(handle.pattern, "leading"),
803809
Utils.request.addSlash(endpointNoPrefix, "leading"),
804810
true,
805-
null
811+
candidateParams
806812
);
807813
if (matched) {
808814
if (handle.disabled) {
809815
logger.debug("runWsPlugin: matched handler is disabled");
810816
} else {
811817
handler = handle;
818+
wsParams = Object.keys(candidateParams).length > 0 ? candidateParams : null;
812819
break;
813820
}
814821
}
@@ -854,6 +861,7 @@ export const runWsPlugin = (server: ViteDevServer | PreviewServer, logger: ILogg
854861

855862
handlerWss.handleUpgrade(req, socket as any, head, async (ws) => {
856863
logger.debug(`runWsPlugin: connection upgraded for ${url.pathname}`);
864+
(req as any).params = wsParams;
857865
const connection = new WebSocketConnection(
858866
logger,
859867
ws,

src/utils/utils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ function patchWalkPath(target: any, path: string) {
2525

2626
for (let i = 0; i < segments.length - 1; i++) {
2727
const key = segments[i];
28-
if (!(key in current)) {
28+
if (Array.isArray(current)) {
29+
const idx = parseInt(key, 10);
30+
if (isNaN(idx) || idx < 0 || idx >= current.length) {
31+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
32+
}
33+
} else if (typeof current !== "object" || current === null || !(key in current)) {
2934
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
3035
}
3136
current = current[key];
@@ -444,6 +449,10 @@ export const Utils = {
444449
},
445450
async parseRequest(request: UniversalApiRequest<any>, res: ServerResponse, fullUrl: URL, parserRequest: UniversalApiParser, logger: ILogger) {
446451
try {
452+
if (request.body !== null || request.files !== null || request.readableEnded) {
453+
logger.debug("parseRequest: skipped (already parsed or stream ended)");
454+
return;
455+
}
447456
if (parserRequest) {
448457
logger.debug("parseRequest: START");
449458
if (typeof parserRequest === "object") {
@@ -670,8 +679,10 @@ export const Utils = {
670679
}
671680
try {
672681
await next(error);
673-
if (lastError && !res.writableEnded) {
674-
throw lastError;
682+
if (lastError) {
683+
if (!res.writableEnded) {
684+
throw lastError;
685+
}
675686
}
676687
} catch (finalError: any) {
677688
throw new UniversalApiError(finalError, "ERROR_MIDDLEWARE", "", Constants.HTTP_STATUS_CODE.INTERNAL_SERVER_ERROR);
@@ -692,8 +703,8 @@ export const Utils = {
692703
hasPaginationOrFilters(method: UniversalApiRequest<any>["method"], paginationPlugin: UniversalApiOptionsRequired["pagination"], filterPlugin: UniversalApiOptionsRequired["filters"], paginationHandler: UniversalApiRestFsHandler["pagination"], filtersHandler: UniversalApiRestFsHandler["filters"]) {
693704
return (!!paginationHandler && paginationHandler !== "none")
694705
|| (!!filtersHandler && filtersHandler !== "none")
695-
|| (paginationPlugin !== null && (method! in paginationPlugin || "ALL" in paginationPlugin))
696-
|| (filterPlugin !== null && (method! in filterPlugin || "ALL" in filterPlugin))
706+
|| (paginationPlugin != null && (method! in paginationPlugin || "ALL" in paginationPlugin))
707+
|| (filterPlugin != null && (method! in filterPlugin || "ALL" in filterPlugin))
697708
},
698709
getPaginationAndFilters(request: UniversalApiRequest<any>, paginationHandler: UniversalApiRestFsHandler["pagination"], filtersHandler: UniversalApiRestFsHandler["filters"], paginationPlugin: UniversalApiOptions["pagination"], filtersPlugin: UniversalApiOptions["filters"]): { pagination: null | { limit: null | number, skip: null | number, sort: null | string, order: null | string }, filters: null | { key: string, value: any, comparison: string, regexFlags?: string }[] } {
699710
const result: { pagination: null | { limit: null | number, skip: null | number, sort: null | string, order: null | string }, filters: null | { key: string, value: any, comparison: string, regexFlags?: string }[] } = {
@@ -1235,7 +1246,6 @@ export const Utils = {
12351246
res.statusCode = data.status;
12361247
data.headers.forEach(({ name, value }) => res.setHeader(name, value));
12371248
res.setHeader("content-length", size);
1238-
data.headers.push({ name: "content-length", value: size });
12391249
const stream = createReadStream(data.data);
12401250

12411251
const cleanup = (err?: Error) => {

0 commit comments

Comments
 (0)