-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add first version of divviup client #375
Open
jbr
wants to merge
2
commits into
main
Choose a base branch
from
add-first-version-of-divviup-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export { DAPClient, DAPClient as default } from "./client"; | ||
export { | ||
DAPClient, | ||
DAPClient as default, | ||
KnownVdafSpec, | ||
VdafMeasurement, | ||
} from "./client"; | ||
export { DAPError } from "./errors"; | ||
export type { ReportOptions } from "./client"; | ||
export { TaskId } from "./taskId"; | ||
export { HpkeConfig, HpkeConfigList } from "./hpkeConfig"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "divviup", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"source": "src/index.ts", | ||
"module": "dist/module.js", | ||
"browser": "dist/browser.js", | ||
"type": "module", | ||
"license": "MPL-2.0", | ||
"scripts": { | ||
"clean": "rm -rf dist/*", | ||
"build:clean": "npm run clean && npm run build", | ||
"build": "npm run build:web && npm run build:node", | ||
"build:web": "esbuild browser=src/index.ts --bundle --loader:.wasm=binary --format=esm --outdir=dist --sourcemap --minify", | ||
"build:node": "tsc -p ./tsconfig.json", | ||
"docs": "typedoc src", | ||
"test": "mocha \"src/**/*.spec.ts\"", | ||
"lint": "eslint src --ext .ts && prettier -c src", | ||
"format": "prettier -w src", | ||
"check": "tsc --noEmit -p ./tsconfig.json", | ||
"test:coverage": "c8 npm test" | ||
}, | ||
"dependencies": { | ||
"@divviup/dap": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"hpke": "^0.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import assert from "assert"; | ||
import { inspect } from "node:util"; | ||
import { DivviupClient, sendMeasurement } from "."; | ||
import { HpkeConfigList, HpkeConfig, TaskId } from "@divviup/dap"; | ||
import * as hpke from "hpke"; | ||
|
||
describe("DivviupClient", () => { | ||
it("fetches task from an id", async () => { | ||
const taskId = TaskId.random().toString(); | ||
const client = new DivviupClient(taskId); | ||
const fetch = mockFetch({ | ||
...dapMocks(taskId), | ||
[`https://api.staging.divviup.org/tasks/${taskId}`]: [ | ||
{ | ||
status: 200, | ||
body: JSON.stringify(task(taskId)), | ||
contentType: "application/json", | ||
}, | ||
], | ||
}); | ||
client.fetch = fetch; | ||
await client.sendMeasurement(10); | ||
assert.equal(fetch.calls.length, 4); | ||
assert.deepEqual(fetch.callStrings(), [ | ||
`GET https://api.staging.divviup.org/tasks/${taskId}`, | ||
`GET https://a.example.com/v1/hpke_config?task_id=${taskId}`, | ||
`GET https://b.example.com/dap/hpke_config?task_id=${taskId}`, | ||
`PUT https://a.example.com/v1/tasks/${taskId}/reports`, | ||
]); | ||
}); | ||
|
||
it("fetches task from a task url", async () => { | ||
const taskId = TaskId.random().toString(); | ||
const client = new DivviupClient( | ||
`https://production.divvi.up/v3/different-url/${taskId}.json`, | ||
); | ||
const fetch = mockFetch({ | ||
...dapMocks(taskId), | ||
[`https://production.divvi.up/v3/different-url/${taskId}.json`]: [ | ||
{ | ||
status: 200, | ||
body: JSON.stringify(task(taskId)), | ||
contentType: "application/json", | ||
}, | ||
], | ||
}); | ||
client.fetch = fetch; | ||
await client.sendMeasurement(10); | ||
assert.equal(fetch.calls.length, 4); | ||
assert.deepEqual(fetch.callStrings(), [ | ||
`GET https://production.divvi.up/v3/different-url/${taskId}.json`, | ||
`GET https://a.example.com/v1/hpke_config?task_id=${taskId}`, | ||
`GET https://b.example.com/dap/hpke_config?task_id=${taskId}`, | ||
`PUT https://a.example.com/v1/tasks/${taskId}/reports`, | ||
]); | ||
}); | ||
}); | ||
|
||
describe("sendMeasurement", () => { | ||
it("fetches task from an id", async () => { | ||
const taskId = TaskId.random().toString(); | ||
const fetch = mockFetch({ | ||
...dapMocks(taskId), | ||
[`https://api.staging.divviup.org/tasks/${taskId}`]: [ | ||
{ | ||
status: 200, | ||
body: JSON.stringify(task(taskId)), | ||
contentType: "application/json", | ||
}, | ||
], | ||
}); | ||
|
||
await sendMeasurement(taskId, 10, fetch); | ||
|
||
assert.equal(fetch.calls.length, 4); | ||
assert.deepEqual(fetch.callStrings(), [ | ||
`GET https://api.staging.divviup.org/tasks/${taskId}`, | ||
`GET https://a.example.com/v1/hpke_config?task_id=${taskId}`, | ||
`GET https://b.example.com/dap/hpke_config?task_id=${taskId}`, | ||
`PUT https://a.example.com/v1/tasks/${taskId}/reports`, | ||
]); | ||
}); | ||
}); | ||
|
||
function dapMocks(taskId: string) { | ||
return { | ||
[`https://a.example.com/v1/hpke_config?task_id=${taskId}`]: [ | ||
hpkeConfigResponse(), | ||
], | ||
|
||
[`https://b.example.com/dap/hpke_config?task_id=${taskId}`]: [ | ||
hpkeConfigResponse(), | ||
], | ||
|
||
[`https://api.staging.divviup.org/tasks/${taskId}`]: [ | ||
{ | ||
status: 200, | ||
body: JSON.stringify(task(taskId)), | ||
contentType: "application/json", | ||
}, | ||
], | ||
[`https://a.example.com/v1/tasks/${taskId}/reports`]: [{ status: 201 }], | ||
}; | ||
} | ||
|
||
interface Fetch { | ||
(input: RequestInfo, init?: RequestInit | undefined): Promise<Response>; | ||
calls: [RequestInfo, RequestInit | undefined][]; | ||
callStrings(): string[]; | ||
} | ||
|
||
interface ResponseSpec { | ||
body?: Buffer | Uint8Array | number[] | string; | ||
contentType?: string; | ||
status?: number; | ||
} | ||
|
||
function mockFetch(mocks: { [url: string]: ResponseSpec[] }): Fetch { | ||
function fakeFetch( | ||
input: RequestInfo, | ||
init?: RequestInit | undefined, | ||
): Promise<Response> { | ||
fakeFetch.calls.push([input, init]); | ||
const responseSpec = mocks[input.toString()]; | ||
const response = responseSpec?.shift(); | ||
|
||
if (!response) { | ||
throw new Error( | ||
`received unhandled request.\n\nurl: ${input.toString()}.\n\nmocks: ${inspect( | ||
mocks, | ||
).slice(1, -1)}`, | ||
); | ||
} | ||
|
||
return Promise.resolve( | ||
new Response(Buffer.from(response.body || ""), { | ||
status: response.status || 200, | ||
headers: { "Content-Type": response.contentType || "text/plain" }, | ||
}), | ||
); | ||
} | ||
|
||
fakeFetch.calls = [] as [RequestInfo, RequestInit | undefined][]; | ||
fakeFetch.callStrings = function () { | ||
return this.calls.map((x) => `${x[1]?.method || "GET"} ${x[0].toString()}`); | ||
}; | ||
return fakeFetch; | ||
} | ||
|
||
function task(taskId: string): { | ||
vdaf: { | ||
type: "sum"; | ||
bits: number; | ||
}; | ||
helper: string; | ||
leader: string; | ||
id: string; | ||
time_precision_seconds: number; | ||
} { | ||
return { | ||
vdaf: { | ||
type: "sum", | ||
bits: 16, | ||
}, | ||
leader: "https://a.example.com/v1", | ||
helper: "https://b.example.com/dap/", | ||
id: taskId, | ||
time_precision_seconds: 1, | ||
}; | ||
} | ||
|
||
function hpkeConfigResponse(config = buildHpkeConfigList()): ResponseSpec { | ||
return { | ||
body: config.encode(), | ||
contentType: "application/dap-hpke-config-list", | ||
}; | ||
} | ||
|
||
function buildHpkeConfigList(): HpkeConfigList { | ||
return new HpkeConfigList([ | ||
new HpkeConfig( | ||
Math.floor(Math.random() * 255), | ||
hpke.Kem.DhP256HkdfSha256, | ||
hpke.Kdf.Sha256, | ||
hpke.Aead.AesGcm128, | ||
Buffer.from(new hpke.Keypair(hpke.Kem.DhP256HkdfSha256).public_key), | ||
), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { DAPClient } from "@divviup/dap"; | ||
import { KnownVdafSpec } from "@divviup/dap/dist/client"; | ||
|
||
type Fetch = ( | ||
input: RequestInfo, | ||
init?: RequestInit | undefined, | ||
) => Promise<Response>; | ||
|
||
interface PublicTask { | ||
id: string; | ||
vdaf: KnownVdafSpec; | ||
leader: string; | ||
helper: string; | ||
time_precision_seconds: number; | ||
} | ||
|
||
type AnyMeasurement = number | bigint | boolean; | ||
type GenericDAPClient = DAPClient<KnownVdafSpec, AnyMeasurement>; | ||
|
||
export class DivviupClient { | ||
#baseUrl = new URL("https://api.staging.divviup.org/tasks"); | ||
#fetch: Fetch = globalThis.fetch.bind(globalThis); | ||
#dapClient: null | GenericDAPClient = null; | ||
#taskUrl: URL; | ||
|
||
/** @internal */ | ||
set fetch(fetch: Fetch) { | ||
this.#fetch = fetch; | ||
if (this.#dapClient) this.#dapClient.fetch = fetch; | ||
} | ||
|
||
constructor(urlOrTaskId: string | URL) { | ||
if (typeof urlOrTaskId === "string") { | ||
try { | ||
this.#taskUrl = new URL(urlOrTaskId); | ||
} catch (e) { | ||
this.#taskUrl = new URL(`${this.#baseUrl.toString()}/${urlOrTaskId}`); | ||
} | ||
} else { | ||
this.#taskUrl = urlOrTaskId; | ||
} | ||
} | ||
|
||
private async taskClient(): Promise<GenericDAPClient> { | ||
if (this.#dapClient) return this.#dapClient; | ||
const response = await this.#fetch(this.#taskUrl.toString()); | ||
const task = (await response.json()) as PublicTask; | ||
const { leader, helper, vdaf, id, time_precision_seconds } = task; | ||
const client = new DAPClient({ | ||
taskId: id, | ||
leader, | ||
helper, | ||
id, | ||
timePrecisionSeconds: time_precision_seconds, | ||
...vdaf, | ||
}); | ||
client.fetch = this.#fetch; | ||
this.#dapClient = client; | ||
return client; | ||
} | ||
|
||
async sendMeasurement(measurement: AnyMeasurement) { | ||
const client = await this.taskClient(); | ||
return client.sendMeasurement(measurement); | ||
} | ||
} | ||
|
||
export default DivviupClient; | ||
|
||
export async function sendMeasurement( | ||
urlOrTaskId: string | URL, | ||
measurement: AnyMeasurement, | ||
fetch?: Fetch, | ||
) { | ||
const client = new DivviupClient(urlOrTaskId); | ||
if (fetch) client.fetch = fetch; | ||
return client.sendMeasurement(measurement); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
}, | ||
"include": ["./src/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": ["../../typedoc.base.json"], | ||
"entryPointStrategy": "expand", | ||
"entryPoints": ["src/index.ts"] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unused