Skip to content

Commit

Permalink
feat(cli): add request id
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Feb 21, 2024
1 parent 12c8f19 commit d9faa1b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/cli/src/utils/datatruck/repository-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConfigAction } from "../../actions/ConfigAction";
import { logJson } from "../cli";
import { readRequestData } from "../http";
import { Counter } from "../math";
import { LocalFs } from "../virtual-fs";
import { createReadStream, createWriteStream } from "fs";
import { stat } from "fs/promises";
Expand Down Expand Up @@ -110,14 +111,16 @@ export function createDatatruckRepositoryServer(
configPath?: string;
} = {},
) {
const counter = new Counter();
return createServer(async (req, res) => {
const url = req.url || "";
if (url === "/" || url === "/favicon.ico") return res.end();
const id = counter.next();
let requestError: Error | undefined;
let responseError: Error | undefined;
req.on("error", (error) => (requestError = error));
res.on("error", (error) => (responseError = error));
try {
if (url === "/" || url === "/favicon.ico") return;
const { repository, action, params } = parseUrl(url);
if (!repository || !action) return res.writeHead(404);

Expand All @@ -132,7 +135,7 @@ export function createDatatruckRepositoryServer(
if (!backend) return res.writeHead(401);

if (config.log)
logJson("repository-server", "request", { repository, url });
logJson("repository-server", "request", { id, repository, url });

const fs = new LocalFs({ backend: backend.path });

Expand Down Expand Up @@ -160,20 +163,20 @@ export function createDatatruckRepositoryServer(
const json = await object(...params);
if (json !== undefined) res.write(JSON.stringify(json));
}
if (config.log) logJson("repository-server", "request finished", { url });
if (config.log) logJson("repository-server", "request finished", { id });
} catch (error) {
if (config.log) {
logJson("repository-server", "request failed", { url });
logJson("repository-server", "request failed", { id });
console.error(error);
}
if (!res.headersSent) res.writeHead(500, (error as Error).message);
} finally {
if (requestError) {
logJson("repository-server", "request error", { url });
logJson("repository-server", "request error", { id });
console.error(requestError);
}
if (responseError) {
logJson("repository-server", "response error", { url });
logJson("repository-server", "response error", { id });
console.error(responseError);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ export function progressPercent(total: number, current: number) {
if (total === 0 && current === 0) return 0;
return Number(((current / total) * 100).toFixed(2));
}

export class Counter {
protected value = 0;
constructor(protected maxValue: number = 4_294_967_295) {}
next() {
if (this.maxValue && this.value >= this.maxValue) this.value = 0;
return ++this.value;
}
}
23 changes: 22 additions & 1 deletion packages/cli/test/utils/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { progressPercent } from "../../src/utils/math";
import { Counter, progressPercent } from "../../src/utils/math";
import { describe, expect, test } from "vitest";

describe("progressPercent", () => {
Expand All @@ -9,3 +9,24 @@ describe("progressPercent", () => {
expect(progressPercent(3, 1)).toBe(33.33);
});
});

describe("Counter", () => {
test("increments", () => {
const counter = new Counter();
expect(counter.next()).toBe(1);
expect(counter.next()).toBe(2);
expect(counter.next()).toBe(3);
});
test("resets", () => {
const counter = new Counter(3);
expect(counter.next()).toBe(1);
expect(counter.next()).toBe(2);
expect(counter.next()).toBe(3);
expect(counter.next()).toBe(1);
expect(counter.next()).toBe(2);
expect(counter.next()).toBe(3);
expect(counter.next()).toBe(1);
expect(counter.next()).toBe(2);
expect(counter.next()).toBe(3);
});
});

0 comments on commit d9faa1b

Please sign in to comment.