Repository navigation
Expand file tree
/
Copy pathfake-server.ts
More file actions
296 lines (270 loc) · 9.54 KB
/
Copy pathfake-server.ts
File metadata and controls
296 lines (270 loc) · 9.54 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* Deterministic in-process fake GitHub server (warren-33aa).
*
* Implements the same `GithubTransport` contract as the production
* transport, so client tests run without sockets, wall time, or entropy.
* It records every request (method, path, query, redacted headers), can
* paginate collections, emit duplicate node ids (caller data), reorder
* events (caller data), serve conditional 304s from versioned ETags,
* and simulate primary and secondary rate limits. Like the production
* transport it fails hard on any non-GET/HEAD method.
*/
import type { Clock, IdGenerator } from "../clock.ts";
import { SequentialIdGenerator, SystemClock } from "../clock.ts";
import { assertReadMethod } from "./http-transport.ts";
import { redactHeaders } from "./redact.ts";
import type { GithubHttpResponse, GithubReadRequest, GithubTransport } from "./types.ts";
/** One recorded request, as exposed to tests — headers already redacted. */
export interface RecordedGithubRequest {
method: string;
/** Path without the query string. */
path: string;
query: Record<string, string>;
/** Request headers with the credential redacted. */
headers: Record<string, string>;
}
/** Primary rate-limit simulation knobs. */
export interface FakeRateLimitState {
limit: number;
remaining: number;
resetEpochSeconds: number;
}
/** Secondary (abuse detection) rate-limit simulation knobs. */
export interface FakeAbuseState {
retryAfterSeconds: number;
}
interface FakeResource {
/** Raw JSON-able body. Arrays on paginated routes are the full collection. */
body: unknown;
version: number;
paginated: boolean;
/** Pinned modification time (ms) — bumps with the version. */
lastModifiedMs: number;
}
export interface FakeGithubServerOptions {
clock?: Clock;
idGenerator?: IdGenerator;
/** Secret used to redact recorded headers (usually the token under test). */
redactionSecret?: string;
/** Default per-page size when the request omits `per_page`. */
defaultPerPage?: number;
}
interface RecordedInternal {
method: string;
path: string;
query: Record<string, string>;
rawHeaders: Record<string, string>;
}
export class FakeGithubServer implements GithubTransport {
private readonly resources = new Map<string, FakeResource>();
private readonly recorded: RecordedInternal[] = [];
private readonly clock: Clock;
/** Deterministic id source for tests building stable node ids. */
readonly ids: IdGenerator;
private readonly secret: string | undefined;
private readonly defaultPerPage: number;
private rateLimit: FakeRateLimitState | null = null;
private abuse: FakeAbuseState | null = null;
constructor(options: FakeGithubServerOptions = {}) {
this.clock = options.clock ?? new SystemClock();
this.ids = options.idGenerator ?? new SequentialIdGenerator();
this.secret = options.redactionSecret;
this.defaultPerPage = options.defaultPerPage ?? 30;
}
/** Register (or replace) a single resource at an exact path. */
setResource(path: string, body: unknown): this {
this.resources.set(normalize(path), {
body,
version: 1,
paginated: false,
lastModifiedMs: this.clock.nowMs(),
});
return this;
}
/**
* Register a paginated collection. The stored array is the full
* collection; `read` slices it by `page`/`per_page` and emits a
* `Link: rel="next"` header while more pages remain. Items may contain
* duplicate node ids — that is the caller simulating GitHub's
* at-least-once delivery.
*/
setPaginatedCollection(path: string, items: unknown[]): this {
this.resources.set(normalize(path), {
body: items,
version: 1,
paginated: true,
lastModifiedMs: this.clock.nowMs(),
});
return this;
}
/** Change a resource's body, bumping its ETag/Last-Modified version. */
mutateResource(path: string, body: unknown): this {
const key = normalize(path);
const prior = this.resources.get(key);
this.resources.set(key, {
body,
version: (prior?.version ?? 0) + 1,
paginated: prior?.paginated ?? false,
lastModifiedMs: this.clock.nowMs(),
});
return this;
}
/** Remove a registered resource so subsequent reads answer 404. */
deleteResource(path: string): this {
this.resources.delete(normalize(path));
return this;
}
/** Bump a resource's version without changing its body. */
bumpResource(path: string): this {
const prior = this.resources.get(normalize(path));
if (prior) {
prior.version += 1;
prior.lastModifiedMs = this.clock.nowMs();
}
return this;
}
/** Simulate an exhausted primary rate limit on every response. */
setRateLimit(state: FakeRateLimitState): this {
this.rateLimit = state;
return this;
}
/** Simulate an abuse-detection (secondary) rate limit. */
setAbuseDetection(state: FakeAbuseState): this {
this.abuse = state;
return this;
}
/** Clear both rate-limit simulations. */
clearRateLimits(): this {
this.rateLimit = null;
this.abuse = null;
return this;
}
/** All recorded requests, newest last, with headers redacted. */
recordedRequests(): RecordedGithubRequest[] {
return this.recorded.map((entry) => ({
method: entry.method,
path: entry.path,
query: entry.query,
headers: redactHeaders(entry.rawHeaders, this.secret),
}));
}
/** Total requests served (attempts that passed the method guard). */
get requestCount(): number {
return this.recorded.length;
}
/** Serve one request. Non-GET/HEAD fails hard and is never recorded. */
async read(request: GithubReadRequest): Promise<GithubHttpResponse> {
assertReadMethod(request.method, request.path);
const [rawPath, queryString = ""] = request.path.split("?");
const query = Object.fromEntries(new URLSearchParams(queryString));
const headers = lowercaseHeaders(request.headers ?? {});
this.recorded.push({
method: request.method,
path: normalize(rawPath ?? "/"),
query,
rawHeaders: headers,
});
const baseHeaders = this.baseHeaders();
if (this.abuse) {
return jsonError(403, "You have triggered an abuse detection mechanism and must wait", {
...baseHeaders,
"retry-after": String(this.abuse.retryAfterSeconds),
});
}
if (this.rateLimit && this.rateLimit.remaining <= 0) {
return jsonError(403, "API rate limit exceeded for this installation", baseHeaders);
}
const resource = this.resources.get(normalize(rawPath ?? "/"));
if (!resource) {
return jsonError(404, "Not Found", baseHeaders);
}
const etag = `W/"fake-v${resource.version}"`;
const lastModified = new Date(resource.lastModifiedMs).toISOString();
const validators = { etag, "last-modified": lastModified, ...baseHeaders };
if (isNotModified(headers, etag, resource.lastModifiedMs)) {
return { status: 304, headers: validators, body: null };
}
if (request.method === "HEAD") {
return { status: 200, headers: validators, body: null };
}
if (resource.paginated) {
return paginateResource(resource, query, rawPath ?? "/", this.defaultPerPage, validators);
}
return { status: 200, headers: validators, body: JSON.stringify(resource.body) };
}
/** Common headers, including the simulated rate-limit snapshot. */
private baseHeaders(): Record<string, string> {
const headers: Record<string, string> = { "content-type": "application/json" };
if (this.rateLimit) {
headers["x-ratelimit-limit"] = String(this.rateLimit.limit);
headers["x-ratelimit-remaining"] = String(this.rateLimit.remaining);
headers["x-ratelimit-reset"] = String(this.rateLimit.resetEpochSeconds);
}
return headers;
}
}
function normalize(path: string): string {
const trimmed = path.startsWith("/") ? path : `/${path}`;
return trimmed.length > 1 && trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
}
function lowercaseHeaders(headers: Record<string, string>): Record<string, string> {
const out: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
out[key.toLowerCase()] = value;
}
return out;
}
function jsonError(
status: number,
message: string,
headers: Record<string, string>,
): GithubHttpResponse {
return { status, headers, body: JSON.stringify({ message }) };
}
function isNotModified(
requestHeaders: Record<string, string>,
currentEtag: string,
lastModifiedMs: number,
): boolean {
const ifNoneMatch = requestHeaders["if-none-match"];
if (ifNoneMatch !== undefined) {
return ifNoneMatch
.split(",")
.some((candidate) => candidate.trim() === currentEtag || candidate.trim() === "*");
}
const ifModifiedSince = requestHeaders["if-modified-since"];
if (ifModifiedSince !== undefined) {
const since = Date.parse(ifModifiedSince);
return Number.isFinite(since) && lastModifiedMs <= since;
}
return false;
}
function paginateResource(
resource: FakeResource,
query: Record<string, string>,
rawPath: string,
defaultPerPage: number,
headers: Record<string, string>,
): GithubHttpResponse {
const items = Array.isArray(resource.body) ? resource.body : [];
const perPage = clampInt(query.per_page, defaultPerPage, 1, 100);
const page = clampInt(query.page, 1, 1, Number.MAX_SAFE_INTEGER);
const start = (page - 1) * perPage;
const slice = items.slice(start, start + perPage);
const nextExists = start + perPage < items.length;
const link = nextExists
? `<${rawPath}?page=${page + 1}&per_page=${perPage}>; rel="next"`
: undefined;
const responseHeaders = link === undefined ? headers : { ...headers, link };
return { status: 200, headers: responseHeaders, body: JSON.stringify(slice) };
}
function clampInt(raw: string | undefined, fallback: number, min: number, max: number): number {
if (raw === undefined) {
return fallback;
}
const parsed = Number.parseInt(raw, 10);
if (!Number.isFinite(parsed)) {
return fallback;
}
return Math.min(Math.max(parsed, min), max);
}