Path: blob/master/src/packages/conat/service/time.ts
1452 views
/*1Time service -- tell me what time you think it is.23This is a global service that is run by hubs.4*/56import { createServiceClient, createServiceHandler } from "./typed";7import { getClient } from "@cocalc/conat/client";89interface TimeApi {10// time in ms since epoch, i.e., Date.now()11time: () => Promise<number>;12}1314const SUBJECT = process.env.COCALC_TEST_MODE ? "time-test" : "time";1516interface User {17account_id?: string;18project_id?: string;19}2021function timeSubject({ account_id, project_id }: User) {22if (account_id) {23return `${SUBJECT}.account-${account_id}.api`;24} else if (project_id) {25return `${SUBJECT}.project-${project_id}.api`;26} else {27return `${SUBJECT}.hub.api`;28}29}3031export function timeClient(user?: User) {32if (user == null) {33user = getClient();34}35const subject = timeSubject(user);36return createServiceClient<TimeApi>({37service: "time",38subject,39});40}4142export async function createTimeService() {43return await createServiceHandler<TimeApi>({44service: "time",45subject: `${SUBJECT}.*.api`,46description: "Time service -- tell me what time you think it is.",47impl: { time: async () => Date.now() },48});49}505152