notes
All repositories: gitoria
4.7 KB
// lib/api.hl — THE FUNCTION ROUTES (notes mission 002, code order): thin wrappers — check the input and the session,// call the topic (lib/notes.hl, lib/users.hl), answer.//// THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"): /login/callback?ident_code=&next= → the user// (lib/users.hl userOfLoginCode) → the session is signed in → back to `next` (lib/api-helpers.hl safePath). A FAILED// LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed.// These two write the session: project.hl hands them the request and the session store BY REFERENCE (&req, &sessions);// a by-value copy would lose the write (tickets mission 010, gitoria mission 002).//// THE JSON API (ticket notes#2; since notes#4 the page syncs through its faces, components/notes.hl). JSON, the session cookie is the login.// GET /api/me → { user } (an opaque id of the signed-in user, or null)// GET /api/notes → { notes: [{ id, edited, gone }] } every note of the user, tombstones included// GET /api/notes?id=<id> → { id, text, edited, created }// POST /api/notes { op: 'save', id, local, text, base, at } → { id, edited, text, copy? }// POST /api/notes { op: 'delete', id, base } → { ok } | { kept, id, edited, text }// Invalid JSON is refused by lib/jsoncheck.hl before JSON.parse sees it (hybriel #6); unknown/mistyped fields → 400.import { Response } from 'hl:http1'import { randomBytes } from 'hl:crypto'import { userOfLoginCode, userIdOfSession } from './users.hl'import { metaRows, noteView, pushNote, removeNote } from './notes.hl'import { jsonErrorAt } from './jsoncheck.hl'import { reply, isText, isTime, safePath } from './api-helpers.hl'static loginFailed = (&req, &sessions, why) => {let s = req.sessionfresh = s == nullif (fresh) { s = sessions.mint() }s.data.loginError = whysessions.save(s)res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }return res}// the function route gets the cookie's session as req.session (hybriel #11); none yet → minted herestatic loginCallback = (route, &req, &sessions) => {if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }q = req.query != null ? req.query : {}code = q.ident_codeif (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }x = userOfLoginCode(code)if (x.error != null) { return loginFailed(&req, &sessions, x.error) }let s = req.sessionfresh = s == nullif (fresh) { s = sessions.mint() }s.user = { id = x.user.id }s.data.tag = randomBytes(16)s.data.loginError = nullsessions.save(s)res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }return res}static apiMe = (route, req) => {u = userIdOfSession(req.session)return reply(200, { user = u })}static apiNotes = (route, req) => {u = userIdOfSession(req.session)if (u == null) { return reply(401, { error = 'log in with ident first' }) }q = req.query != null ? req.query : {}if (req.method == 'GET') {if (q.id != null) {v = noteView(u, q.id)if (v == null) { return reply(404, { error = 'no such note' }) }return reply(200, v)}return reply(200, { notes = metaRows(u) })}if (req.method != 'POST') { return reply(405, { error = 'GET or POST only' }) }if (req.body == null || req.body == '') { return reply(400, { error = 'a JSON body is needed' }) }at = jsonErrorAt(req.body)if (at >= 0) { return reply(400, { error = 'invalid JSON at character ' + at }) }b = JSON.parse(req.body)if (b == null || hlTypeName(b) != 'Hybrid') { return reply(400, { error = 'the body must be a JSON object' }) }if (b.op == 'save') {if (!isText(b.id) || !isText(b.local) || !isText(b.text) || !isTime(b.base) || !isTime(b.at)) { return reply(400, { error = 'save needs id, local, text (strings) and base, at (numbers)' }) }if (b.local.length > 64 || b.id.length > 64) { return reply(400, { error = 'id / local too long' }) }r = pushNote(u, b.id, b.local, b.text, b.base, b.at, '')if (r.error != null) { return reply(400, r) }return reply(200, r)}if (b.op == 'delete') {if (!isText(b.id) || !isTime(b.base)) { return reply(400, { error = 'delete needs id (string) and base (number)' }) }return reply(200, removeNote(u, b.id, b.base, '', ''))}return reply(400, { error = "op must be 'save' or 'delete'" })}
Branches
Latest commits
- 8a8ad9banotes mission 007: report, LOG, STATUSmre
- 2cce9c96code order: plain declarations where never reassigned (letcount 0 never); gate: scan every IndexedDB at log-out, soft mode for old-code runs, drop a check that could not failmre
- 7f6a4090README: offline in Hybriel (hl:web offline, continuation, known md-editor gap); gate waits report instead of crash; realdata-offline checkmre
- 632223f2notes#4: delete notes-offline.js and sw.js (hl:web's generated worker and kept state replace them)mre
- 52a2acadgate: offline checks on hl:web's worker/queue, Chrome offline via CDP (reload offline first), one-offline conflict, log-out forgets the kept copymre
- e5785b03notes#4: the notes page in Hybriel — hl:web offline = [Notes], queued faces notesPut/notesDrop, push notesChanged, value-form notesPull on a tick; notes-offline.js/sw.js no longer loaded or routedmre
- 510aff75notes#4: server continues a run of saves of one login (by + since), loser keeps writing into its copy; notesFor listmre
- 0176c4b5mission brief 007mre
- 06cfa870notes mission 006: re-vendor hybriel 0f396652 (static), reportmre
- 6c07bd91mission brief w6mre
- cf64d3d5notes mission 005: stopped at a Hybriel gap (for list repainted during hl:web boot throws, rowKidless); report, LOG, STATUSmre
- a43b5d2dmission brief 005mre
- 04a625edmission 004: re-vendor hybriel a832f73b (native bin + plugins as tickets), compose HL_CACHE_DIR volume + tmpfs /tmp, deploy URL wait 120 s, tests wait 120 s per start; gate 57/0 old and new; real-data same output. Not deployed.mre
- aefb9d44mission brief w4mre
- 202098b3docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
- 304f6a9dnotes mission 003 (#5): report, STATUS, LOGmre
- 9acc27f8notes mission 003 (#5): GitHub pipe tables in md-editor (parse, render, type, write) + gate checksmre
- 2149e902notes mission 002 (4/4): code order — README file map + same-output test, STATUS, LOG, report; tests/letcount.py, tests/realdata-baseline.mjs, tests/realdata-compare.pymre
- 47db4fc1notes mission 002 (3/4): code order — let only where reassigned (58 dropped; 34 left: 19 reassigned, 15 loop-bound); gates 50/0 + 18/0, live-data run = step 2mre
- 3f8cb383notes mission 002 (2/4): code order — topics, map, thin wrappers: lib/util.hl, lib/notes(-helpers).hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login routes take &req/&sessions (failed-login reason now kept); dead notes#1 functions removed; gates 50/0 + 18/0mre