From 51acbef35fe059fec1cee8116b5708c906f27841 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Tue, 8 Jul 2025 18:10:09 -0700 Subject: [PATCH] docs: update readme chore: session timoeouts up feature: started to look at doing something around durations for some forgotten reason, but don't want to just abandon this now :shrug: --- README.md | 26 +++++++++--- public/api/auth/index.ts | 2 +- utils/duration.ts | 91 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 utils/duration.ts diff --git a/README.md b/README.md index fb180da..a354f01 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,24 @@ feature discussions. - [X] refactor login/sessions/totp - [X] chat rooms - [X] chat messages - - [ ] gif support - - [ ] inline image support - - [ ] media uploads - - [ ] local upload support (keep it simple for small instances) - - [ ] S3 support (then self-host with your friends: https://garagehq.deuxfleurs.fr/) + - [ ] chat message processing + - [ ] auto-link urls + - [ ] use this regex?: `(?:(?[a-zA-Z]+):\/\/)?(?:(?(?\S.+)\:(?.+))\@)?(?(?[-a-zA-Z0-9\.]{2,256}\.(?[-a-zA-Z0-9]{2,256}))(?:\:(?[0-9]{1,6}))?)\b(?[-a-zA-Z0-9@:%_{}\[\]<>\(\)\+.~#&//=]*)(?:\?(?[a-zA-Z0-9!$%&<>()*+,-\.\/\:\;\=\?\@_~]+))?(?:#(?[a-zA-Z0-9!$&'()*+,-\.\/\:\;\=\?\@_~]+))?` + - [ ] preview cards for links + - [ ] embedded video for + - [ ] youtube + - [ ] vimeo + - [ ] tiktok + - [ ] embedded audio or audio cards for: + - [ ] tidal + - [ ] spotify + - [ ] youtube (any way to differentiate for yt music?) + - [ ] punycode urls before url extraction? (see: https://stackoverflow.com/a/26618995) + - [ ] gif support + - [ ] start/stop gif control + - [ ] hide control + - [ ] inline image support + - [ ] hide control - [ ] try to select immediate sibling messages from the same user and hide mulitple avatars - [X] user profile page - [X] logout button @@ -41,6 +54,9 @@ feature discussions. - [X] smart forms - [X] use the api for forms so requests will be authenticated - [X] support multiple methods +- [ ] media uploads + - [ ] local upload support (keep it simple for small instances) + - [ ] S3 support (then self-host with your friends: https://garagehq.deuxfleurs.fr/) ## Getting Started Developing diff --git a/public/api/auth/index.ts b/public/api/auth/index.ts index 1bacd9d..edd0657 100644 --- a/public/api/auth/index.ts +++ b/public/api/auth/index.ts @@ -10,7 +10,7 @@ import { get_session, get_user, PRECHECK_TABLE, require_user, SESSION_ID_TOKEN, import * as bcrypt from 'jsr:@da/bcrypt'; import { verifyTotp } from '../../../utils/totp.ts'; -const DEFAULT_SESSION_TIME: number = 60 * 60 * 1_000; // 1 Hour +const DEFAULT_SESSION_TIME: number = 28 * (24 * (60 * (60 * 1_000))); // 28 days export const PRECHECKS: PRECHECK_TABLE = {}; diff --git a/utils/duration.ts b/utils/duration.ts new file mode 100644 index 0000000..1a919ef --- /dev/null +++ b/utils/duration.ts @@ -0,0 +1,91 @@ +enum TimeUnit { + // Plancks = (-5.39 * Math.pow( 10, -44 )), // The amount of time light takes to travel one Planck length. + // quectoseconds = Math.pow( 10, -30), // One nonillionth of a second. + // rontoseconds = Math.pow( 10, -27 ), // One octillionth of a second. + // yoctoseconds = Math.pow( 10, -24 ), // One septillionth of a second. + // zeptoseconds = Math.pow( 10, -21 ), // One sextillionth of a second. Time measurement scale of the NIST and JILA strontium atomic clock. Smallest fragment of time currently measurable is 247 zeptoseconds.[3] + // attoseconds = Math.pow( 10, -18 ), // One quintillionth of a second. + // femtoseconds = Math.pow( 10, -15 ), // One quadrillionth of a second. + // svedbergs = Math.pow( 10, -13 ), // 100 femtoseconds, time unit used for sedimentation rates (usually of proteins). + picoseconds = Math.pow(10, -12), // One trillionth of a second. + nanoseconds = Math.pow(10, -9), // One billionth of a second. Time for molecules to fluoresce. + shakes = Math.pow(10, -8), // 10 nanoseconds, also a casual term for a short period of time. + microseconds = Math.pow(10, -6), // One millionth of a second. Symbol is μs + milliseconds = Math.pow(10, -3), // One thousandth of a second. Shortest time unit used on stopwatches. + centiseconds = Math.pow(10, -2), // One hundredth of a second. + deciseconds = Math.pow(10, -1), // One tenth of a second. + seconds = 1, // SI base unit for time. + decasecond = 10, // Ten seconds (one sixth of a minute) + minutes = 60, // 60 seconds + hectoseconds = 100, // 100 seconds + millidays = 86.4, // 1/1000 d (0.001 d) 1.44 minutes, or 86.4 seconds. Also marketed as a ".beat" by the Swatch corporation. + centidays = 864, // 0.01 d (1 % of a day) 14.4 minutes, or 864 seconds. One-hundredth of a day is 1 cd (centiday), also called "kè" in traditional Chinese timekeeping. The unit was also proposed by Lagrange and endorsed by Rey-Pailhade[5] in the 19th century, named "centijours" (from French centi- 'hundred' and jour 'day'). + kiloseconds = 1000, // About 17 minutes. + hours = 60 * 60, // 60 minutes (60 * 60 seconds) + decidays = 144 * 60, // 0.1 d (10 % of a day) 2.4 hours, or 144 minutes. One-tenth of a day is 1 dd (deciday), also called "gēng" in traditional Chinese timekeeping. + days = 24 * 60 * 60, // 24 h Longest unit used on stopwatches and countdowns. The SI day is exactly 86 400 seconds. + weeks = 7 * 24 * 60 * 60, // 7 d Historically sometimes also called "sennight". + decaday = 10 * 24 * 60 * 60, // 10 d (1 Dd) 10 days. A period of time analogous to the concept of "week", used by different societies around the world: the ancient Egyptian calendar, the ancient Chinese calendar, and also the French Republican calendar (in which it was called a décade). + megaseconds = Math.pow(10, 6), // About 11.6 days. + fortnights = 14 * 24 * 60 * 60, // 2 weeks 14 days + hectodays = 100 * 24 * 60 * 60, // 100 d (1 hd) 100 days, roughly equivalent to 1/4 of a year (91.25 days). In Chinese tradition "bǎi rì" (百日) is the hundredth day after one's birth, also called Baby's 100 Days Celebration. + semesters = 18 * 7 * 24 * 60 * 60, // 18 weeks A division of the academic year.[6] Literally "six months", also used in this sense. + lunaryears = 354.37 * 24 * 60 * 60, // 354.37 d + years = 365 * 24 * 60 * 60, // 12 mo + decades = 10 * 365 * 24 * 60 * 60, // 10 yr + gigaseconds = Math.pow(10, 9), // About 31.7 years. + jubilees = 50 * 365 * 24 * 60 * 60, // 50 yr + centuries = 100 * 365 * 24 * 60 * 60, // 100 yr + millennium = 1000 * 365 * 24 * 60 * 60, // 1000 yr Also called "kiloannum". + teraseconds = Math.pow(10, 12), // About 31,709 years. + petaseconds = Math.pow(10, 15) // About 31709791 years. + // exaseconds = Math.pow( 10, 18 ), // About 31709791983 years. Approximately 2.3 times the current age of the universe. + // zettasecond = Math.pow( 10, 21 ), // About 31709791983764 years. +} + +const UNIT_ABBREVIATIONS: Record = { + 'ps': TimeUnit.picoseconds, + 'ns': TimeUnit.nanoseconds, + 'us': TimeUnit.microseconds, + 'ms': TimeUnit.milliseconds, + 's': TimeUnit.seconds, + 'm': TimeUnit.minutes, + 'h': TimeUnit.hours, + 'd': TimeUnit.days, + 'w': TimeUnit.weeks, + 'y': TimeUnit.years +}; + +export class Duration { + constructor(private length: number = 0, private start: number | undefined = undefined) {} + + public static between(start: Date, end: Date, unit: TimeUnit = TimeUnit.milliseconds) { + const ms_between: number = end.valueOf() - start.valueOf(); + const factor: number = unit / TimeUnit.milliseconds; + return factor * ms_between; + } + + public static of(spec: string | number, unit?: TimeUnit): Duration { + if (unit) { + return new Duration(Number(spec) * unit); + } + + let length_in_ms = 0; + for (const match of `${spec}`.matchAll(/(?([0-9]*[.])?[0-9]+)\W*(?\w+)\W/g)) { + const { len, unit_name } = match.groups ?? {}; + + if (typeof len !== 'string' || typeof unit_name !== 'string') { + throw new Error(`Could not parse duraction spec: ${spec}`); + } + + const time_unit: TimeUnit | undefined = UNIT_ABBREVIATIONS[unit_name] ?? TimeUnit[unit_name as keyof typeof TimeUnit]; + if (!time_unit) { + throw new Error(`Could not determine a time unit for: ${unit_name}`); + } + + length_in_ms += Number(len) * time_unit; + } + + return new Duration(length_in_ms); + } +}