1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
x2
x16
x4
x232
x232
x232
x232
x232
x240
x240
x240
x232
x232
x232
x232
x284
x284
x284
x232
x265
x265
x265
x265
x232
x248
x248
x248
x232
x309
x309
x309
x309
x232
x248
x248
x248
x232
x243
x243
x243
x243
x232
x247
x247
x247
x232
x4
x232
x232
x696
x232
x232
x8
x2
x4
x4
x4
x4
x232
x232
x232
x232
x232
x4
x2
x4
x4
x4
x4
x4
x2
|
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
|
// Copyright (c) 2025 Anton A Nesterov <an+vski@vski.sh>, VSKI License
//
import { EmulatorWorkflowService } from "./service.ts";
export class EmulatorFetchInterceptor {
constructor(private service: EmulatorWorkflowService) {}
async handleRequest(request: Request): Promise<Response | null> {
const url = new URL(request.url);
const path = url.pathname;
if (!path.startsWith("/api/workflows")) return null;
const db = request.headers.get("x-dbname") || "postgres";
const method = request.method;
try {
// Runs
if (path === "/api/workflows/runs" && method === "POST") {
const data = await request.json();
return this.jsonResponse(await this.service.createRun(db, data));
}
if (
path.startsWith("/api/workflows/runs/") && method === "GET" &&
!path.endsWith("/events")
) {
const id = path.split("/").pop()!;
return this.jsonResponse(await this.service.getRun(db, id));
}
if (path.startsWith("/api/workflows/runs/") && method === "PATCH") {
const id = path.split("/").pop()!;
const data = await request.json();
return this.jsonResponse(await this.service.updateRun(db, id, data));
}
if (path === "/api/workflows/runs" && method === "GET") {
const params = Object.fromEntries(url.searchParams.entries());
return this.jsonResponse(await this.service.listRuns(db, params));
}
// Steps
if (path === "/api/workflows/steps" && method === "POST") {
const data = await request.json();
return this.jsonResponse(
await this.service.createStep(db, data.runId, data),
);
}
if (path.startsWith("/api/workflows/steps/") && method === "GET") {
const parts = path.split("/");
if (parts.length === 6) { // /api/workflows/steps/:runId/:stepId
return this.jsonResponse(
await this.service.getStep(db, parts[4], parts[5]),
);
} else { // /api/workflows/steps/:runId
return this.jsonResponse(await this.service.listSteps(db, parts[4]));
}
}
if (path.startsWith("/api/workflows/steps/") && method === "PATCH") {
const parts = path.split("/");
const data = await request.json();
return this.jsonResponse(
await this.service.updateStep(db, parts[4], parts[5], data),
);
}
// Events
if (path === "/api/workflows/events" && method === "POST") {
const data = await request.json();
return this.jsonResponse(
await this.service.createEvent(db, data.runId, data),
);
}
if (path.startsWith("/api/workflows/runs/") && path.endsWith("/events")) {
const id = path.split("/")[4];
return this.jsonResponse(await this.service.listEvents(db, id));
}
// Hooks
if (path === "/api/workflows/hooks" && method === "POST") {
const data = await request.json();
return this.jsonResponse(
await this.service.createHook(db, data.runId, data),
);
}
if (path === "/api/workflows/hooks" && method === "GET") {
const token = url.searchParams.get("token");
if (token) {
return this.jsonResponse(
await this.service.getHookByToken(db, token),
);
}
}
// Queue
if (path === "/api/workflows/queue" && method === "POST") {
const b = await request.json();
return this.jsonResponse(
await this.service.queue(db, b.queueName, b.message, b.opts),
);
}
if (path === "/api/workflows/queue/ack" && method === "POST") {
const b = await request.json();
return this.jsonResponse(await this.service.ack(db, b.messageId));
}
if (path === "/api/workflows/queue/nack" && method === "POST") {
const b = await request.json();
return this.jsonResponse(await this.service.nack(db, b.messageId));
}
if (path === "/api/workflows/queue/touch" && method === "POST") {
const b = await request.json();
return this.jsonResponse(await this.service.touch(db, b.messageId));
}
if (
path.startsWith("/api/workflows/queue/") && method === "GET" &&
!path.endsWith("/ack") && !path.endsWith("/nack") &&
!path.endsWith("/touch")
) {
const name = path.split("/").pop()!;
return this.jsonResponse(await this.service.poll(db, name));
}
return new Response("Not Found", { status: 404 });
} catch (e: any) {
return this.jsonResponse({ error: e.message }, 500);
}
}
private jsonResponse(data: any, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { "Content-Type": "application/json" },
});
}
private originalFetch: typeof globalThis.fetch | null = null;
install() {
this.originalFetch = globalThis.fetch.bind(globalThis);
globalThis.fetch = async (
input: string | URL | Request,
init?: RequestInit,
) => {
// Optimization: Check URL string before constructing Request object to avoid overhead/side-effects
let urlString: string | undefined;
if (typeof input === "string") urlString = input;
else if (input instanceof URL) urlString = input.toString();
else if (input instanceof Request) urlString = input.url;
// Simple check for the path prefix (handling relative and absolute URLs)
if (urlString && !urlString.includes("/api/workflows")) {
return this.originalFetch!(input, init);
}
const request = new Request(input, init);
const response = await this.handleRequest(request);
if (response) return response;
return this.originalFetch!(input, init);
};
}
uninstall() {
if (this.originalFetch) {
globalThis.fetch = this.originalFetch;
this.originalFetch = null;
}
}
}
|