d608636 / anbot.mjs
diff --cc anbot.mjs
index 1be809d,d5aeb07..2f9df70
--- a/anbot.mjs
+++ b/anbot.mjs
@@@ -6,16 -6,21 +6,23 @@@ import { basename, dirname, isAbsolute
import { parseArgs } from 'node:util'
import {
checkoutRoot, clonePath, commitStat, gitEnv, gitOptions, listCommits, listTree,
- publish, readBlob, readCommit, readPushAnnouncements, repositoryName, selectedRepository
- publish, readBlob, readCommit, repositoryName, selectedRepository, verifySelection
++ publish, readBlob, readCommit, readPushAnnouncements, repositoryName, selectedRepository, verifySelection
} from './repository.mjs'
- import { initializeStore, loadIdentity, recordPath, requireThat } from './storage.mjs'
+ import {
+ createFsStore, initializeStore, listVerifiedRecords, loadIdentity, readRecordAuthor,
+ recordPath, requireThat
+ } from './storage.mjs'
+ import {
+ base64Decode, base64Encode, maxManifest, parseJson, utf8Encode, verifyBlock
+ } from './core.mjs'
import { pushAuth, pushWithIdentity } from './push-auth.mjs'
import { receivePush } from './receive.mjs'
--import { publishPost, readFeed } from './social-store.mjs'
++import { listLocalPostRecords, publishPost, readFeed } from './social-store.mjs'
+ import { pull } from './sync.mjs'
import { blobPage, commitPage, feedPage, indexPage, notFoundPage, treePage } from './webui.mjs'
+const feedScript = readFileSync(new URL('./feed-live.js', import.meta.url))
+
const help = `Usage:
node anbot.mjs publish --data DIR --source CHECKOUT --branch BRANCH [--name NAME] [--host HOST] [--port PORT]
node anbot.mjs serve --data DIR [--host HOST] [--port PORT] [--push-allowlist FILE] [--push-origin ORIGIN]
@@@ -98,6 -104,82 +106,83 @@@ function serveHtml(response, status, ht
}).end(html)
}
+ // The poll cursor is opaque to clients: the unpadded Base64url form of the
+ // JSON array [payloadTimestamp, recordId] of the last record served. The
+ // (timestamp, id) pair orders records totally and survives restarts with
+ // no server-side index, because both values come from verified records.
+ const pollPageSize = 100
+
+ function encodeCursor(timestamp, id) {
+ return base64Encode(utf8Encode(JSON.stringify([timestamp, id])), true)
+ }
+
+ function decodeCursor(since) {
+ const decoded = parseJson(base64Decode(since, true))
+ requireThat(Array.isArray(decoded) && decoded.length === 2 &&
+ Number.isSafeInteger(decoded[0]) && typeof decoded[1] === 'string', 'Invalid poll cursor')
+ return decoded
+ }
+
+ function compareRecords(a, b) {
+ return (a.timestamp - b.timestamp) ||
+ (a.record.id < b.record.id ? -1 : a.record.id > b.record.id ? 1 : 0)
+ }
+
+ async function servePoll(root, response, query) {
+ let cursor = null
+ try {
+ const since = new URLSearchParams(query).get('since')
+ if (since) cursor = decodeCursor(since)
+ } catch {
+ response.writeHead(400).end('Invalid poll cursor\n')
+ return
+ }
+ try {
- const verified = await listVerifiedRecords(root)
++ const [records, posts] = await Promise.all([listVerifiedRecords(root), listLocalPostRecords(root)])
++ const verified = [...new Map([...records, ...posts].map(entry => [entry.record.id, entry])).values()]
+ verified.sort(compareRecords)
+ const after = cursor
+ ? verified.filter(({ record, timestamp }) =>
+ timestamp > cursor[0] || (timestamp === cursor[0] && record.id > cursor[1]))
+ : verified
+ const page = after.slice(0, pollPageSize)
+ const last = page.at(-1)
+ response.writeHead(200, {
+ 'Content-Type': 'application/json', 'Cache-Control': 'no-store',
+ 'Access-Control-Allow-Origin': '*'
+ }).end(JSON.stringify({
+ records: page.map(({ record }) => record),
+ nextSince: last ? encodeCursor(last.timestamp, last.record.id) : null
+ }))
+ } catch (error) {
+ console.error(error.message)
+ response.writeHead(503).end('Publication unavailable\n')
+ }
+ }
+
+ async function serveBlock(root, response, hash) {
+ try {
+ const bytes = await createFsStore(root).getBlock(hash, maxManifest)
+ if (bytes === undefined) {
+ response.writeHead(404).end('Unknown block\n')
+ return
+ }
+ requireThat(await verifyBlock(bytes, hash), 'Corrupt AndFS block')
+ response.writeHead(200, {
+ 'Content-Type': 'application/octet-stream',
+ 'Cache-Control': 'public, max-age=31536000, immutable',
+ 'Access-Control-Allow-Origin': '*'
+ }).end(bytes)
+ } catch (error) {
+ if (error.message === 'Invalid encoding') {
+ response.writeHead(404).end('Unknown block\n')
+ return
+ }
+ console.error(error.message)
+ response.writeHead(503).end('Block unavailable\n')
+ }
+ }
+
function parseWebPath(rest) {
const trimmed = rest.replace(/\/+$/, '')
if (!trimmed) return ''
@@@ -143,12 -224,11 +228,12 @@@ async function serveFeed(root, query, r
serveHtml(response, 404, feedPage({ missing: true }))
return
}
- const [social, pushes] = await Promise.all([readFeed(root), readPushAnnouncements(root, identity)])
- const posts = await readFeed(root)
- const post = postId === null ? undefined : posts.find(item => item.id === postId)
- const missing = postId !== null && !post
++ const [social, pushes] = await Promise.all([readFeed(root), readPushAnnouncements(root)])
+ const posts = [...social, ...pushes].sort((a, b) => b.timestamp - a.timestamp || a.id.localeCompare(b.id))
+ const post = posts.find(item => item.id === postId || item.updateId === updateId)
+ const missing = (postId !== null || updateId !== null) && !post
const matching = posts.filter(item =>
- postId !== null ? item.replyTo === postId : author === null || item.author === author)
+ post ? item.replyTo === post.id : author === null || item.author === author)
serveHtml(response, missing ? 404 : 200, feedPage({ posts: matching, author, post, missing }))
}
@@@ -164,15 -245,9 +250,15 @@@ async function runServer(root, identity
const server = createServer(async (request, response) => {
const [pathname, query = ''] = request.url.split('?', 2)
if (request.method === 'GET') {
+ if (pathname === '/feed-live.js') {
+ response.writeHead(200, {
+ 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'no-cache'
+ }).end(feedScript)
+ return
+ }
if (pathname === '/feed') {
try {
- await serveFeed(root, identity, query, response)
+ await serveFeed(root, query, response)
} catch (error) {
console.error(error.message)
response.writeHead(503).end('Feed unavailable\n')
Git ยท Social feed