From d9faa1b27ffc20d8d2bbf1737fa218407480b9e1 Mon Sep 17 00:00:00 2001 From: Juanra GM Date: Wed, 21 Feb 2024 13:55:41 +0100 Subject: [PATCH] feat(cli): add request id --- .../src/utils/datatruck/repository-server.ts | 15 +++++++----- packages/cli/src/utils/math.ts | 9 ++++++++ packages/cli/test/utils/math.test.ts | 23 ++++++++++++++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/utils/datatruck/repository-server.ts b/packages/cli/src/utils/datatruck/repository-server.ts index 471623b..e6f6015 100644 --- a/packages/cli/src/utils/datatruck/repository-server.ts +++ b/packages/cli/src/utils/datatruck/repository-server.ts @@ -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"; @@ -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); @@ -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 }); @@ -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); } diff --git a/packages/cli/src/utils/math.ts b/packages/cli/src/utils/math.ts index 0976286..0d0f3a3 100644 --- a/packages/cli/src/utils/math.ts +++ b/packages/cli/src/utils/math.ts @@ -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; + } +} diff --git a/packages/cli/test/utils/math.test.ts b/packages/cli/test/utils/math.test.ts index b9b5843..cda15a9 100644 --- a/packages/cli/test/utils/math.test.ts +++ b/packages/cli/test/utils/math.test.ts @@ -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", () => { @@ -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); + }); +});