|
| 1 | +// The browser client of a site on agentenv_protocol.website: JSON calls to the site's API, the universe's time from |
| 2 | +// X-Universe-Now, and the signed-in user. It has no dependencies. Paths are relative to the page, so a site works at |
| 3 | +// "/" and under the gateway's "/website/<name>/" alike: a leading "/" is dropped. |
| 4 | + |
| 5 | +let offsetMs = 0; |
| 6 | + |
| 7 | +// An answer other than success: `status` (0 when the server couldn't be reached), and the site's `code` and `error`. |
| 8 | +export class ApiError extends Error { |
| 9 | + constructor(status, code, error, body) { |
| 10 | + super(error); |
| 11 | + this.name = "ApiError"; |
| 12 | + this.status = status; |
| 13 | + this.code = code; |
| 14 | + this.error = error; |
| 15 | + this.body = body; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// The universe's time: the X-Universe-Now of the latest answer, extrapolated at real speed since it came. An idle |
| 20 | +// page keeps it right with followUniverseClock(). |
| 21 | +export function universeNow() { |
| 22 | + return new Date(Date.now() + offsetMs); |
| 23 | +} |
| 24 | + |
| 25 | +// Read X-Universe-Now from `path`, the site's health path, every `everyMs`, so universeNow() follows a clock that |
| 26 | +// stops or runs at another speed while the page makes no calls. Returns a function that stops it. |
| 27 | +export function followUniverseClock({ path = "api/health", everyMs = 5000 } = {}) { |
| 28 | + const id = setInterval(() => fetch(relative(path), { credentials: "same-origin" }).then(follow).catch(() => {}), everyMs); |
| 29 | + return () => clearInterval(id); |
| 30 | +} |
| 31 | + |
| 32 | +function follow(response) { |
| 33 | + const at = Date.parse(response.headers.get("X-Universe-Now") || ""); |
| 34 | + if (!Number.isNaN(at)) offsetMs = at - Date.now(); |
| 35 | +} |
| 36 | + |
| 37 | +function relative(path) { |
| 38 | + return String(path).replace(/^\/+/, ""); |
| 39 | +} |
| 40 | + |
| 41 | +// Call the site's API: `body` is sent as JSON; the answer's JSON is returned, or an ApiError thrown. |
| 42 | +export async function api(path, { method = "GET", body, headers = {} } = {}) { |
| 43 | + const url = relative(path); |
| 44 | + let response; |
| 45 | + try { |
| 46 | + response = await fetch(url, { |
| 47 | + method, |
| 48 | + credentials: "same-origin", |
| 49 | + headers: body === undefined ? headers : { "Content-Type": "application/json", ...headers }, |
| 50 | + body: body === undefined ? undefined : JSON.stringify(body), |
| 51 | + }); |
| 52 | + } catch (cause) { |
| 53 | + throw new ApiError(0, "network_error", `${method} ${url}: ${cause?.message || cause}`, null); |
| 54 | + } |
| 55 | + follow(response); |
| 56 | + const text = await response.text(); |
| 57 | + let data = null; |
| 58 | + try { |
| 59 | + data = text ? JSON.parse(text) : null; |
| 60 | + } catch { |
| 61 | + data = text; |
| 62 | + } |
| 63 | + if (!response.ok) { |
| 64 | + const error = data && typeof data === "object" && data.error ? data.error : `${method} ${url}: ${response.status}`; |
| 65 | + const code = data && typeof data === "object" && data.code ? data.code : "http_error"; |
| 66 | + throw new ApiError(response.status, code, error, data); |
| 67 | + } |
| 68 | + return data; |
| 69 | +} |
| 70 | + |
| 71 | +// Who the browser is signed in as: {user, role, signed_in}. A browser with no session is signed in as the site's |
| 72 | +// acting user. |
| 73 | +export function session() { |
| 74 | + return api("api/session"); |
| 75 | +} |
| 76 | + |
| 77 | +// Sign in as `user` through the site's own sign-in route, `api/sign-in` unless `path` names another. |
| 78 | +export function signIn(user, { path = "api/sign-in" } = {}) { |
| 79 | + return api(path, { method: "POST", body: { user } }); |
| 80 | +} |
| 81 | + |
| 82 | +export function signOut() { |
| 83 | + return api("api/session", { method: "DELETE" }); |
| 84 | +} |
0 commit comments