Download tasks/linkedin/solution/site/implementation/clone/db/http-d1.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/db/http-d1.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/solution/site/implementation/clone/db/http-d1.ts
-
curl -L -o http-d1.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/db/http-d1.ts
2.99 kB
| type BoundValue = bigint | boolean | null | number | string; | |
| const databases = new Map<string, D1Database>(); | |
| function jsonValue(value: BoundValue) { | |
| if (typeof value === "bigint") { | |
| if (!Number.isSafeInteger(Number(value))) throw new TypeError("D1 integer is outside the safe range."); | |
| return Number(value); | |
| } | |
| return value; | |
| } | |
| class HttpD1Statement { | |
| private readonly endpoint: string; | |
| private readonly query: string; | |
| private readonly values: BoundValue[]; | |
| constructor(endpoint: string, query: string, values: BoundValue[] = []) { | |
| this.endpoint = endpoint; | |
| this.query = query; | |
| this.values = values; | |
| } | |
| bind(...values: BoundValue[]) { | |
| return new HttpD1Statement(this.endpoint, this.query, values); | |
| } | |
| private async execute(operation: "all" | "first" | "raw" | "run", column?: string) { | |
| const response = await fetch(this.endpoint, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| protocol: "linkedin.d1.v1", | |
| operation, | |
| query: this.query, | |
| values: this.values.map(jsonValue), | |
| ...(column ? { column } : {}), | |
| }), | |
| }); | |
| const payload = await response.json().catch(() => null) as { error?: string; result?: unknown } | null; | |
| if (!response.ok || !payload || !("result" in payload)) { | |
| throw new Error(`LinkedIn D1 proxy refused ${operation}: ${payload?.error || response.status}`); | |
| } | |
| return payload.result; | |
| } | |
| async first(column?: string) { | |
| return this.execute("first", column); | |
| } | |
| async run() { | |
| return this.execute("run"); | |
| } | |
| async all() { | |
| return this.execute("all"); | |
| } | |
| async raw() { | |
| return this.execute("raw"); | |
| } | |
| envelope() { | |
| return { query: this.query, values: this.values.map(jsonValue) }; | |
| } | |
| } | |
| class HttpD1Database { | |
| constructor(private readonly endpoint: string) {} | |
| prepare(query: string) { | |
| return new HttpD1Statement(this.endpoint, query); | |
| } | |
| async batch(statements: HttpD1Statement[]) { | |
| const response = await fetch(this.endpoint, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| protocol: "linkedin.d1.v1", | |
| operation: "batch", | |
| statements: statements.map((statement) => statement.envelope()), | |
| }), | |
| }); | |
| const payload = await response.json().catch(() => null) as { error?: string; result?: unknown } | null; | |
| if (!response.ok || !payload || !("result" in payload)) { | |
| throw new Error(`LinkedIn D1 proxy refused batch: ${payload?.error || response.status}`); | |
| } | |
| return payload.result; | |
| } | |
| async exec(query: string) { | |
| return new HttpD1Statement(this.endpoint, query).run(); | |
| } | |
| } | |
| export function getHttpD1Database(endpoint: string) { | |
| let database = databases.get(endpoint); | |
| if (!database) { | |
| database = new HttpD1Database(endpoint) as unknown as D1Database; | |
| databases.set(endpoint, database); | |
| } | |
| return database; | |
| } | |