Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142// Browser errors: a page script that throws in an owner's browser is reported to the ops log, so// the operator sees errors nobody mentions.//// Only for owners who turned telemetry on (state.preferences[did].telemetry === true): their pages// that run scripts (the others can't throw) load /errors.js, which posts each uncaught error and// unhandled rejection to /client-errors with the page's route template (src/visits.ts), never its// address. The service writes it as an ops event of kind `client`, scrubbed (src/ops.ts), with quoted// text dropped (a parse error can quote what was being parsed); at most ten a minute per session.
import type { OpsEvent } from './ops.ts'
export const CLIENT_ERRORS_PATH = '/client-errors'
/** * Sent from the browser: the message, the stack cut short, and the route template of the page, which * the server writes on the script's own tag (data-route). At most 20 a page. */export const CLIENT_ERRORS_JS = `(()=>{let n=0;const route=(document.currentScript&&document.currentScript.dataset.route)||'';const send=(m,s)=>{if(++n>20)return;try{fetch(${JSON.stringify(CLIENT_ERRORS_PATH)},{method:'POST',headers:{'content-type':'application/json'},credentials:'same-origin',keepalive:true,body:JSON.stringify({message:String(m||'').slice(0,500),stack:String(s||'').slice(0,2000),route})}).catch(()=>{})}catch{}};addEventListener('error',e=>send(e.message,e.error&&e.error.stack));addEventListener('unhandledrejection',e=>{const r=e.reason;send(r&&r.message||r,r&&r.stack)})})();`
const LIMITS = { message: 500, stack: 2000, route: 120 }
/** * The ops event for a browser's report, or undefined when it isn't one. `owner` is the signed-in * owner's DID. Only the report's known fields are kept; OpsLog scrubs them as it does every event. */export function clientErrorEvent(body: unknown, owner: string): Omit<OpsEvent, 'createdAt'> | undefined { const b = body as any const str = (v: unknown, n: number) => (typeof v === 'string' && v.trim() ? unquote(v.trim().slice(0, n)) : undefined) const message = str(b?.message, LIMITS.message) if (!message) return undefined // A route template only: a path of plain words and :placeholders, never an address with ids or a query. const route = str(b?.route, LIMITS.route) const ev: Omit<OpsEvent, 'createdAt'> = { level: 'error', kind: 'client', message, subject: owner } if (route && /^\/[a-z:/-]*$/.test(route)) ev.method = route const stack = str(b?.stack, LIMITS.stack) if (stack) ev.stack = stack.replace(/https?:\/\/[^\s)]+/g, (u) => u.replace(/^https?:\/\/[^/]+/, '').replace(/[?#].*$/, '')) return ev}
/** Text with anything quoted longer than a short identifier replaced: errors can quote the data they failed on. */export const unquote = (s: string) => s.replace(/(["'`])((?:(?!\1).){13,}?)\1/g, '$1…$1')