Skip to content

Commit

Permalink
fix(cli): avoid ECONNRESET errors
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed May 7, 2024
1 parent 40f7071 commit 892714d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eighty-singers-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": patch
---

Avoid `ECONNRESET` errors due to race conditions
6 changes: 5 additions & 1 deletion packages/cli/src/utils/datatruck/repository-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export function createDatatruckRepositoryServer(
const id = counter.next();
let requestError: Error | undefined;
let responseError: Error | undefined;
req.on("error", (error) => (requestError = error));
const requestErrorListener = (error: Error) => (requestError = error);
req.on("error", requestErrorListener);
res.on("error", (error) => (responseError = error));
res.setHeader("X-Accel-Buffering", "no");
try {
Expand All @@ -145,14 +146,17 @@ export function createDatatruckRepositoryServer(
} else if (action === "upload") {
const [target] = params;
const path = fs.resolvePath(target);
req.off("error", requestErrorListener);
await recvFile(req, res, path);
} else if (action === "download") {
const [target] = params;
const path = fs.resolvePath(target);
req.off("error", requestErrorListener);
await sendFile(req, res, path, {
contentLength: options.contentLength ?? true,
});
} else if (action === "writeFile") {
req.off("error", requestErrorListener);
const data = await readRequestData(req);
const [target] = params;
await fs.writeFile(target, data!);
Expand Down

0 comments on commit 892714d

Please sign in to comment.