summaryrefslogtreecommitdiff
path: root/u/server/request
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-06-29 17:31:30 -0700
committerElizabeth Hunt <me@liz.coffee>2025-06-29 17:31:30 -0700
commit58be1809c46cbe517a18d86d0af52179dcc5cbf6 (patch)
tree9ccc678b3fd48c1a52fe501600dd2c2051740a55 /u/server/request
parentd4791f3d357634daf506fb8f91cc5332a794c421 (diff)
downloadci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.tar.gz
ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.zip
Move to nodejs and also lots of significant refactoring that should've been broken up but idgaf
Diffstat (limited to 'u/server/request')
-rw-r--r--u/server/request/index.ts18
-rw-r--r--u/server/request/pengueno.ts44
2 files changed, 62 insertions, 0 deletions
diff --git a/u/server/request/index.ts b/u/server/request/index.ts
new file mode 100644
index 0000000..41d59b7
--- /dev/null
+++ b/u/server/request/index.ts
@@ -0,0 +1,18 @@
+import { HttpMethod } from '@emprespresso/pengueno';
+
+export interface BaseRequest {
+ url: string;
+ method: HttpMethod;
+
+ header(): Record<string, string>;
+
+ formData(): Promise<FormData>;
+ json(): Promise<unknown>;
+ text(): Promise<string>;
+
+ param(key: string): string | undefined;
+ query(): Record<string, string>;
+ queries(): Record<string, string[]>;
+}
+
+export * from './pengueno.js';
diff --git a/u/server/request/pengueno.ts b/u/server/request/pengueno.ts
new file mode 100644
index 0000000..31563e9
--- /dev/null
+++ b/u/server/request/pengueno.ts
@@ -0,0 +1,44 @@
+import { BaseRequest, ITraceable, ServerTrace } from '@emprespresso/pengueno';
+
+const greetings = ['hewwo :D', 'hiya cutie', 'boop!', 'sending virtual hugs!', 'stay pawsitive'];
+const penguenoGreeting = () => greetings[Math.floor(Math.random() * greetings.length)];
+
+export class PenguenoRequest {
+ private constructor(
+ public readonly req: BaseRequest,
+ private readonly id: string,
+ private readonly at: Date,
+ ) {}
+
+ public elapsedTimeMs(after = () => Date.now()): number {
+ return after() - this.at.getTime();
+ }
+
+ public getResponseHeaders(): Record<string, string> {
+ const RequestId = this.id;
+ const RequestReceivedUnix = this.at.getTime();
+ const RequestHandleUnix = Date.now();
+ const DeltaUnix = this.elapsedTimeMs(() => RequestHandleUnix);
+ const Hai = penguenoGreeting();
+
+ return Object.entries({
+ RequestId,
+ RequestReceivedUnix,
+ RequestHandleUnix,
+ DeltaUnix,
+ Hai,
+ }).reduce((acc, [key, val]) => ({ ...acc, [key]: val!.toString() }), {});
+ }
+
+ public static from(request: ITraceable<BaseRequest, ServerTrace>): ITraceable<PenguenoRequest, ServerTrace> {
+ const id = crypto.randomUUID();
+ return request.bimap((tRequest) => {
+ const request = tRequest.get();
+ const url = new URL(request.url);
+ const { pathname } = url;
+ const trace = `RequestId = ${id}, Method = ${request.method}, Path = ${pathname}`;
+
+ return { item: new PenguenoRequest(request, id, new Date()), trace };
+ });
+ }
+}