/** * gets the current UNIX timestamp */ function get_timestamp ( ): int { return Math.floor(Date.now()/1000); } /** * computes a floating point number in the interval [0,1[ out of a string */ function hash_string_to_unit ( x: string ): float { return (x.split("").reduce((x, y) => ((x + y.charCodeAt(0)) % 32), 0) / 32); } /** * encodes a username as a CSS color */ function get_usercolor ( name: string ): string { const hue: float = hash_string_to_unit(name); return `hsl(${(hue*360).toFixed(2)},50%,75%)`; } /** * calls an API action of the backend */ async function backend_call ( conf: type_conf, connection_id: (null | string), action: string, data: any ): Promise { const response: any = await fetch ( `${conf.backend.scheme}://${conf.backend.host}:${conf.backend.port.toFixed(0)}/${conf.backend.path}`, { "method": "POST", "body": JSON.stringify({"id": connection_id, "action": action, "data": data}), } ); if (response.ok) { return response.json(); } else { console.error(response.text()); return Promise.reject(new Error("backend call failed")); } }