Skip to content

Commit

Permalink
fix: async function broke delayed responses (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardruiter committed Oct 26, 2022
1 parent 4d28719 commit a31cbd9
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions src/parser/HttpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const Handlebars = getHandlebars();
* 2. From matched directory get .mock file content and generate a response
*/

const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

export interface HttpParserResponse {
status: number;
body: string;
Expand Down Expand Up @@ -137,7 +141,9 @@ export class HttpParser {
}
const newLine = getNewLine(fileResponse);
const fileContent = fileResponse.trim().split(newLine);
fileContent.forEach((line: string, index: number) => {
for (let index = 0; index < fileContent.length; index++) {
const line = fileContent[index];

if (line === "") {
PARSE_BODY = true;
}
Expand Down Expand Up @@ -170,9 +176,8 @@ export class HttpParser {
if (responseBody.includes("camouflage_file_helper")) {
const fileResponse = responseBody.split(";")[1];
if (!fs.existsSync(fileResponse)) this.res.status(404);
setTimeout(() => {
this.res.sendFile(fileResponse);
}, DELAY);
await sleep(DELAY);
this.res.sendFile(fileResponse);
} else {
responseBody = responseBody.replace(/\s+/g, " ").trim();
responseBody = responseBody.replace(/{{{/, "{ {{");
Expand All @@ -191,10 +196,10 @@ export class HttpParser {
...codeResponse["headers"],
};
}
setTimeout(() => {
logger.debug(`Generated Response ${codeResponse["body"]}`);
response.body = codeResponse["body"];
});
await sleep(DELAY);
logger.debug(`Generated Response ${codeResponse["body"]}`);
response.body = codeResponse["body"];
return response;
break;
case "proxy":
/* eslint-disable no-case-declarations */
Expand Down Expand Up @@ -228,43 +233,43 @@ export class HttpParser {
}
break;
default:
setTimeout(async () => {
logger.debug(
`Generated Response ${await template({
request: this.req,
logger: logger,
})}`
);
response.body = await template({
await sleep(DELAY);
logger.debug(
`Generated Response ${await template({
request: this.req,
logger: logger,
});
}, DELAY);
})}`
);

response.body = await template({
request: this.req,
logger: logger,
});
return response;
break;
}
} catch (error) {
logger.warn(error.message);
setTimeout(async () => {
logger.debug(
`Generated Response ${await template({
request: this.req,
logger: logger,
})}`
);
response.body = await template({
await sleep(DELAY);
logger.debug(
`Generated Response ${await template({
request: this.req,
logger: logger,
});
console.log("yo");
}, DELAY);
})}`
);
response.body = await template({
request: this.req,
logger: logger,
});
return response;
}
response.body = responseBody;
}
PARSE_BODY = false;
responseBody = "";
DELAY = 0;
}
});
}
return response;
};
}
Expand Down

0 comments on commit a31cbd9

Please sign in to comment.