Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 89 additions & 9 deletions apps/cli/lib/seed-ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('node:path')
const { WebSocketServer, createWebSocketStream } = require('ws')
const { MAX_EXTERNAL_FRAME } = require('@varanda/core/lib/seeder')
const { MEDIA_BLOCK_BYTES } = require('@varanda/core/lib/records')
const { feira } = require('@varanda/rpc')
const { feira, garden: gardenRules } = require('@varanda/rpc')
const { renderListingPage, renderNotFound, renderSitemap } = require('./feira-page')
const { renderPostPage, postPreviewData, postPhotos } = require('./post-page')

Expand Down Expand Up @@ -146,17 +146,31 @@ const { renderPostPage, postPreviewData, postPhotos } = require('./post-page')
// (as in /index/post) and `next` the same shape or null; any other
// segment after the owner is 400 'pedido inválido', never 404, so a
// client tells a bad cursor from an unknown owner.
// - /index/garden/<owner> -> Seeder#indexGarden: `{ on: seq | null, state:
// seq | null, helps: [{ author, seq, followSeq }], given: [seq], indexing,
// complete }` (Varandinha; at most GARDEN_HELP_ROWS helps and given): the
// owner's newest live garden block (on or off) and garden-state block,
// the neighbours' garden-help blocks inside the reader window with the
// seq of each helper's live follow of the owner, and the owner's own help
// seqs. /index/garden/<owner>/rua[/<key>] -> Seeder#indexGardenStreet:
// `{ items: [{ key, on, state }], next, indexing, complete }` (at most
// GARDEN_STREET_PAGE rows), the open balconies of the keys the owner
// follows, key order, strictly after the cursor key; `next` is null or a
// key. Any other segment after the owner is 400 'pedido inválido'.
// Pointers only, checked against the exact shape: an extra key, a
// duplicate or an oversized list is the generic 404, never trimmed.
// - /index/profile/<author> -> Seeder#indexProfile: `{ about: seq | null,
// mood: seq | null, fans: { count, indexing }, top: { seq, back:
// { <friend>: seq | null } } | null, visits: seq | null, indexing,
// complete }`, the author's newest live profile-about, mood, top-friends
// and visits blocks (perfil rico, melhores amigos, visitas; visits is a
// pointer only, the node peeks the block and checks on), the claimed number of fas and, per listed friend, the
// { <friend>: seq | null } } | null, visits: seq | null, garden: seq |
// null, indexing, complete }`, the author's newest live profile-about,
// mood, top-friends, visits and garden blocks (perfil rico, melhores
// amigos, visitas, Varandinha; visits and garden are pointers only, the
// node peeks the block and checks on, so an off block still has a seq), the claimed number of fas and, per listed friend, the
// friend's live follow block of the author (at most MAX_TOP_FRIENDS). The
// object is open: later features add pointers and readers ignore keys they
// do not know, so profileBody keeps only the keys it lists. While the
// author's own diary is still being indexed or scanned (fail closed)
// about, mood, top and visits are null, indexing and fans.indexing are true and
// about, mood, top, visits and garden are null, indexing and fans.indexing are true and
// complete is false; otherwise complete is true and indexing is the global
// flag. Pointers only; the node verifies.
// - /index/fans/<author>[/<atMs>.<key>] -> Seeder#indexFans: `{ author,
Expand Down Expand Up @@ -420,6 +434,14 @@ const INDEX_SCRAPS_RE = /^\/index\/scraps\/([0-9a-f]{64})(?:\/([0-9]{1,16}\.[0-9
// Any path below an owner's recados that INDEX_SCRAPS_RE refuses: a
// malformed cursor (400).
const INDEX_SCRAPS_BAD_RE = /^\/index\/scraps\/[0-9a-f]{64}\//
const INDEX_GARDEN_RE = /^\/index\/garden\/([0-9a-f]{64})$/
const INDEX_GARDEN_STREET_RE = /^\/index\/garden\/([0-9a-f]{64})\/rua(?:\/([0-9a-f]{64}))?$/
// Any path below an owner's balcony that the two above refuse (400).
const INDEX_GARDEN_BAD_RE = /^\/index\/garden\/[0-9a-f]{64}\//
const GARDEN_KEYS = ['complete', 'given', 'helps', 'indexing', 'on', 'state']
const GARDEN_HELP_KEYS = ['author', 'followSeq', 'seq']
const GARDEN_STREET_KEYS = ['complete', 'indexing', 'items', 'next']
const GARDEN_STREET_ITEM_KEYS = ['key', 'on', 'state']
const INDEX_PROFILE_RE = /^\/index\/profile\/([0-9a-f]{64})$/
const INDEX_FANS_RE = /^\/index\/fans\/([0-9a-f]{64})(?:\/([0-9]{1,16}\.[0-9a-f]{64}))?$/
const FAN_ROWS_MAX = 20
Expand Down Expand Up @@ -886,6 +908,14 @@ function startSeedWs ({ host = DEFAULT_HOST, port, seeder, origins = null, media
// The atMs of a cursor fits 16 digits but must stay a safe integer.
if (m && (m[2] === undefined || Number.isSafeInteger(Number(m[2].split('.')[0])))) return ['indexScraps', [m[1], m[2] === undefined ? null : m[2]], scrapsBody]
if (m || INDEX_SCRAPS_BAD_RE.test(url)) return BAD_CURSOR
m = INDEX_GARDEN_STREET_RE.exec(url)
if (m) return ['indexGardenStreet', [m[1], m[2] === undefined ? null : m[2]], gardenStreetBody]
m = INDEX_GARDEN_RE.exec(url)
if (m) {
const owner = m[1]
return ['indexGarden', [owner], (answer) => gardenBody(answer, owner)]
}
if (INDEX_GARDEN_BAD_RE.test(url)) return BAD_CURSOR
m = INDEX_PROFILE_RE.exec(url)
if (m) return ['indexProfile', [m[1]], profileBody]
m = INDEX_FOLLOW_RE.exec(url)
Expand Down Expand Up @@ -963,11 +993,59 @@ function startSeedWs ({ host = DEFAULT_HOST, port, seeder, origins = null, media
}
}

// Exactly the Varandinha pointers, checked as the node checks them: a help
// from the owner, a duplicate, an extra key or an oversized list is a 404,
// never a trimmed answer.
function gardenBody (answer, owner) {
if (!exactKeys(answer, GARDEN_KEYS)) throw new Error('index')
const { on, state, helps, given, indexing, complete } = answer
const seqOk = (n) => Number.isSafeInteger(n) && n >= 1
if ((on !== null && !seqOk(on)) || (state !== null && !seqOk(state))) throw new Error('index')
if (!Array.isArray(helps) || helps.length > gardenRules.GARDEN_HELP_ROWS || !Array.isArray(given) || given.length > gardenRules.GARDEN_HELP_ROWS) throw new Error('index')
const refs = new Set()
const rows = helps.map((row) => {
if (!exactKeys(row, GARDEN_HELP_KEYS)) throw new Error('index')
const { author, seq, followSeq } = row
if (typeof author !== 'string' || !HEX64_RE.test(author) || author === owner || !seqOk(seq) || !seqOk(followSeq)) throw new Error('index')
const ref = author + ':' + seq
if (refs.has(ref)) throw new Error('index')
refs.add(ref)
return { author, seq, followSeq }
})
const seqs = new Set()
for (const seq of given) {
if (!seqOk(seq) || seqs.has(seq)) throw new Error('index')
seqs.add(seq)
}
if (typeof indexing !== 'boolean' || typeof complete !== 'boolean') throw new Error('index')
return { on, state, helps: rows, given: [...given], indexing, complete }
}

// Exactly one page of Varandas da rua: a duplicate key, an extra key or an
// oversized page is a 404.
function gardenStreetBody (answer) {
if (!exactKeys(answer, GARDEN_STREET_KEYS)) throw new Error('index')
const { items, next, indexing, complete } = answer
const seqOk = (n) => Number.isSafeInteger(n) && n >= 1
if (!Array.isArray(items) || items.length > gardenRules.GARDEN_STREET_PAGE) throw new Error('index')
const keys = new Set()
const rows = items.map((item) => {
if (!exactKeys(item, GARDEN_STREET_ITEM_KEYS)) throw new Error('index')
const { key, on, state } = item
if (typeof key !== 'string' || !HEX64_RE.test(key) || keys.has(key) || !seqOk(on) || (state !== null && !seqOk(state))) throw new Error('index')
keys.add(key)
return { key, on, state }
})
if (next !== null && (typeof next !== 'string' || !HEX64_RE.test(next))) throw new Error('index')
if (typeof indexing !== 'boolean' || typeof complete !== 'boolean') throw new Error('index')
return { items: rows, next, indexing, complete }
}

// The pointer keys known so far; a new key is added here when its feature
// ships, an unknown one is never echoed.
function profileBody ({ about, mood, fans = null, top = null, visits = null, indexing, complete }) {
function profileBody ({ about, mood, fans = null, top = null, visits = null, garden = null, indexing, complete }) {
const seqOk = (n) => n === null || (Number.isSafeInteger(n) && n >= 1)
if (!seqOk(about) || !seqOk(mood) || !seqOk(visits)) throw new Error('index')
if (!seqOk(about) || !seqOk(mood) || !seqOk(visits) || !seqOk(garden)) throw new Error('index')
let fanCount = null
if (fans !== null) {
if (typeof fans !== 'object' || !Number.isSafeInteger(fans.count) || fans.count < 0) throw new Error('index')
Expand All @@ -985,7 +1063,7 @@ function startSeedWs ({ host = DEFAULT_HOST, port, seeder, origins = null, media
}
pointers = { seq: top.seq, back }
}
return { about, mood, fans: fanCount, top: pointers, visits, indexing: indexing === true, complete: complete === true }
return { about, mood, fans: fanCount, top: pointers, visits, garden, indexing: indexing === true, complete: complete === true }
}

// Only the follower-list fields, checked as the client checks them: a
Expand Down Expand Up @@ -2092,6 +2170,8 @@ module.exports = {
INDEX_FOLLOWS_RE,
INDEX_TESTIMONIALS_RE,
INDEX_SCRAPS_RE,
INDEX_GARDEN_RE,
INDEX_GARDEN_STREET_RE,
INDEX_PROFILE_RE,
INDEX_FANS_RE,
INDEX_POLL_RE,
Expand Down
Loading
Loading