Skip to content

Commit

Permalink
feat(cli): add contentLength options and disable response buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Mar 20, 2024
1 parent 6023968 commit f7d4c83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-days-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": patch
---

Add options to allow nginx as reverse proxy
9 changes: 8 additions & 1 deletion packages/cli/src/utils/datatruck/repository-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export type DatatruckRepositoryServerOptions = {
};
trustProxy?: true | { remoteAddressHeader: string };
keepAliveTimeout?: number;
/**
* @default true
*/
contentLength?: boolean;
allowlist?: {
/**
* @default true
Expand Down Expand Up @@ -118,6 +122,7 @@ export function createDatatruckRepositoryServer(
let responseError: Error | undefined;
req.on("error", (error) => (requestError = error));
res.on("error", (error) => (responseError = error));
res.setHeader("X-Accel-Buffering", "no");
try {
const { repository, action, params } = parseUrl(url);
if (!repository || !action) return res.writeHead(404);
Expand Down Expand Up @@ -146,7 +151,9 @@ export function createDatatruckRepositoryServer(
} else if (action === "download") {
const [target] = params;
const path = fs.resolvePath(target);
await sendFile(req, res, path);
await sendFile(req, res, path, {
contentLength: options.contentLength ?? true,
});
} else if (action === "writeFile") {
const data = await readRequestData(req);
const [target] = params;
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function sendFile(
res: ServerResponse,
path: string,
options: {
contentLength?: boolean;
end?: boolean;
checksum?: boolean;
} = {},
Expand All @@ -82,7 +83,10 @@ export async function sendFile(
try {
file = createReadStream(path);
const fileStat = await stat(path);
res.setHeader("Content-Length", fileStat.size);
res.setHeader(
options.contentLength ?? true ? "Content-Length" : "x-content-length",
fileStat.size,
);
if (options.checksum)
res.setHeader("x-checksum", await calcFileHash(path, "sha1"));
file.pipe(res);
Expand Down Expand Up @@ -142,7 +146,9 @@ export async function downloadFile(
signal: AbortSignal.timeout(timeout ?? 3600 * 1000), // 60m
});
length.total = parseContentLength(
res.headers.get("content-length") ?? undefined,
res.headers.get("content-length") ??
res.headers.get("x-content-length") ??
undefined,
);
checksum = res.headers.get("x-checksum") ?? undefined;
const body = Readable.fromWeb(res.body!);
Expand Down

0 comments on commit f7d4c83

Please sign in to comment.