uniweb 1.34.11 → 1.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,142 @@
1
+ /**
2
+ * What `uniweb dev --published` reads before serving a working copy against the site as published.
3
+ *
4
+ * A published site's services — its own `backend`, its `records`, its forms — are answered by its
5
+ * host, at addresses the host chooses and states in the page it serves. So this asks the backend you
6
+ * are logged in to where the site is published (its status, the address `uniweb status` shows),
7
+ * reads the published page, and takes the host's services block from the data the page boots from
8
+ * (`__DATA__`, `content.config.services`). The dev server is handed it in `UNIWEB_PUBLISHED`
9
+ * (`@uniweb/build/site`'s `published.js` has the contract) and answers each service by forwarding
10
+ * to it.
11
+ *
12
+ * ⭐ Read, never composed: the addresses are the ones the published page carries, so whatever
13
+ * serves the site — on this machine or not — is what the working copy talks to.
14
+ *
15
+ * ⛔ Nothing here is kept. The answer lives as long as the dev server, and no file a push, pull or
16
+ * build reads gets an address from it.
17
+ */
18
+ import { gunzipSync } from 'node:zlib'
19
+ import { readBackendState } from '@uniweb/build/uwx'
20
+ import { BackendClient } from './client.js'
21
+ import { resolveWorkspace } from './workspace.js'
22
+ import { liveAddress } from './live-address.js'
23
+ import { syncedBackends } from '../utils/site-identity.js'
24
+
25
+ /**
26
+ * What the dev server of `siteDir` is handed under `--published`.
27
+ *
28
+ * @param {object} p
29
+ * @param {string} p.siteDir
30
+ * @param {string[]} [p.args]
31
+ * @param {BackendClient} [p.client] - injected in tests
32
+ * @param {typeof fetch} [p.fetchPage] - injected in tests
33
+ * @returns {Promise<{ published: { site: string, backend: string, base: string|null, services: object } }
34
+ * | { refused: string[] }>}
35
+ */
36
+ export async function readSitePublished({ siteDir, args = [], client = null, fetchPage = globalThis.fetch }) {
37
+ client = client || new BackendClient({ args, command: 'Testing against' })
38
+ const uuid = readBackendState(siteDir, client.origin).site?.uuid
39
+ if (!uuid) {
40
+ const known = syncedBackends(siteDir)
41
+ return {
42
+ refused: [
43
+ '`--published` serves this copy against the site as published from the backend you are logged in to, and the site is not on it.',
44
+ known.length
45
+ ? `The site is on ${known.join(', ')}, not on ${client.origin}. To use it: uniweb login --server <url>`
46
+ : `Publish it there first: uniweb publish`
47
+ ]
48
+ }
49
+ }
50
+
51
+ const ws = await resolveWorkspace({ client, args })
52
+ if (ws.refused) return { refused: ['`--published` reads the site in one workspace, and none is chosen.', ws.reason] }
53
+ client.setWorkspace(ws.workspace, { source: ws.source })
54
+
55
+ let status
56
+ try {
57
+ status = await client.siteStatus(uuid)
58
+ } catch (err) {
59
+ return { refused: [err.message] }
60
+ }
61
+ if (!status) return { refused: [`Could not read this site's status on ${client.origin}, so its published address is unknown.`] }
62
+ const address = liveAddress(status.visitor_url, status.last_published_url)
63
+ if (status.published === false || !address) {
64
+ return { refused: ['This site is not published, so it has no published services to test against.', 'Publish it first: uniweb publish'] }
65
+ }
66
+
67
+ let page
68
+ try {
69
+ page = await readPublishedPage(address, fetchPage)
70
+ } catch (err) {
71
+ return { refused: [`Could not read the site as published at ${address}: ${err.message}`] }
72
+ }
73
+ const config = page.data?.content?.config
74
+ const services = config?.services
75
+ if (!services || typeof services !== 'object' || Array.isArray(services)) {
76
+ return {
77
+ refused: [
78
+ `The site as published at ${page.url} offers no services, so there is nothing to test against.`,
79
+ 'Without --published, uniweb dev answers them itself.'
80
+ ]
81
+ }
82
+ }
83
+ return {
84
+ published: {
85
+ site: page.url,
86
+ backend: client.origin,
87
+ base: typeof config.base === 'string' ? config.base : null,
88
+ services
89
+ }
90
+ }
91
+ }
92
+
93
+ /**
94
+ * The published page at `address`, and the data it boots from.
95
+ *
96
+ * @param {string} address
97
+ * @param {typeof fetch} fetchPage
98
+ * @returns {Promise<{ url: string, data: object }>} `url` is the page's address after redirects,
99
+ * which its own relative addresses are read against
100
+ */
101
+ async function readPublishedPage(address, fetchPage) {
102
+ const res = await fetchPage(address, { headers: { accept: 'text/html' }, redirect: 'follow' })
103
+ if (!res.ok) throw new Error(`HTTP ${res.status}`)
104
+ const data = readDataScript(await res.text())
105
+ if (!data) throw new Error('the page carries no site data (`__DATA__`)')
106
+ return { url: res.url || address, data }
107
+ }
108
+
109
+ /**
110
+ * The boot data a host writes into a page's shell — `<script id="__DATA__">`, as JSON, or as
111
+ * base64 gzip when its type says so (the runtime's `decodeData` reads both).
112
+ *
113
+ * @param {string} html
114
+ * @returns {object|null}
115
+ */
116
+ export function readDataScript(html) {
117
+ for (const [, attributes, text] of html.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/gi)) {
118
+ if (!/\bid\s*=\s*["']__DATA__["']/i.test(attributes)) continue
119
+ const type = /\btype\s*=\s*["']([^"']+)["']/i.exec(attributes)?.[1] ?? 'application/json'
120
+ const body = text.trim()
121
+ return type === 'application/json'
122
+ ? JSON.parse(body)
123
+ : JSON.parse(gunzipSync(Buffer.from(body, 'base64')).toString('utf8'))
124
+ }
125
+ return null
126
+ }
127
+
128
+ /**
129
+ * Whether an address is on this machine — what decides if `uniweb dev --published` warns that
130
+ * the data it works with is real.
131
+ *
132
+ * @param {string} address
133
+ * @returns {boolean}
134
+ */
135
+ export function isOnThisMachine(address) {
136
+ try {
137
+ const { hostname } = new URL(address)
138
+ return hostname === 'localhost' || hostname.endsWith('.localhost') || hostname === '127.0.0.1' || hostname === '[::1]'
139
+ } catch {
140
+ return false
141
+ }
142
+ }
@@ -86,7 +86,8 @@ import { keepEditedTranslations, rowsWantTranslation, formatKeptTranslations } f
86
86
  // Deliberately NOT pull.js's `readPullDocuments`: that one is tolerant of JSON
87
87
  // envelopes for a lane whose shape may change, and importing it here would be a
88
88
  // cycle (pull.js already imports this module). Both lanes here are always ZIPs.
89
- function entityDocFromUwx(buf) {
89
+ // `uniweb promote` reads the site's pages from it, as pushed.
90
+ export function entityDocFromUwx(buf) {
90
91
  if (!buf || buf.length < 2 || buf[0] !== 0x50 || buf[1] !== 0x4b) return null
91
92
  for (const [name, data] of readZip(buf)) {
92
93
  if (name === 'manifest.json' || !name.endsWith('.json')) continue
@@ -15,6 +15,15 @@
15
15
  * uniweb dev --port <n> On that port, or not at all
16
16
  * uniweb dev --status Print the running dev server's address
17
17
  * uniweb dev --stop Stop the running dev server
18
+ * uniweb dev --published This copy, against the services of the site as published
19
+ *
20
+ * ⭐ `--published` SERVES THE WORKING COPY AGAINST THE SITE AS PUBLISHED. Its foundation and pages
21
+ * are this copy's, with live reload; its services — its own backend, its records, its forms — are
22
+ * the published site's, read from the page the host serves and forwarded by the dev server, so a
23
+ * member signs in to the real site backend and a page shows the records a visitor sees, the
24
+ * entries its public queries open included (`backend/site-published.js`). A flag, not a key in
25
+ * `site.yml`, because it needs a published site, and a committed key would make every copy's
26
+ * `uniweb dev` need one.
18
27
  *
19
28
  * ⭐ THE DEV SERVER SAYS WHERE IT IS; NOTHING GUESSES. It takes a free port (Vite moves past a
20
29
  * busy one), and once it listens, the server `dev` starts — only that one — records the address
@@ -88,7 +97,7 @@ const VALUE_FLAGS = ['--site', '--port', '--org']
88
97
  * whether to report on the running server or stop it rather than start one.
89
98
  *
90
99
  * @param {string[]} args
91
- * @returns {{ site: string|null, port: number|null, status: boolean, stop: boolean, error?: string }}
100
+ * @returns {{ site: string|null, port: number|null, status: boolean, stop: boolean, published: boolean, error?: string }}
92
101
  */
93
102
  export function readDevArgs(args) {
94
103
  let positional = null
@@ -102,7 +111,8 @@ export function readDevArgs(args) {
102
111
  site: typeof siteFlag === 'string' ? siteFlag : positional,
103
112
  port: null,
104
113
  status: args.includes('--status'),
105
- stop: args.includes('--stop')
114
+ stop: args.includes('--stop'),
115
+ published: args.includes('--published')
106
116
  }
107
117
  const port = readFlagValue(args, '--port')
108
118
  if (port !== undefined) {
@@ -115,6 +125,9 @@ export function readDevArgs(args) {
115
125
  if (wanted.status && wanted.stop) {
116
126
  return { ...wanted, error: '`--status` and `--stop` ask for different things; pass one.' }
117
127
  }
128
+ if (wanted.published && (wanted.status || wanted.stop)) {
129
+ return { ...wanted, error: '`--published` starts a dev server, and `--status` and `--stop` ask about the running one; pass one.' }
130
+ }
118
131
  return wanted
119
132
  }
120
133
 
@@ -217,7 +230,11 @@ export async function dev(args = []) {
217
230
  process.exit(1)
218
231
  }
219
232
  console.error(`${GREEN}✓${RESET} ${site.name} is already being served at ${CYAN}${running.url}${RESET} (pid ${running.pid}).`)
220
- console.error(' Stop it with: uniweb dev --stop')
233
+ console.error(
234
+ wanted.published
235
+ ? ' To serve it with --published instead, stop it first: uniweb dev --stop'
236
+ : ' Stop it with: uniweb dev --stop'
237
+ )
221
238
  return
222
239
  }
223
240
 
@@ -253,6 +270,28 @@ export async function dev(args = []) {
253
270
  `${DIM}→ Previewing with ${asked.preview.foundation.ref}, served by ${asked.preview.backend}${RESET}`
254
271
  )
255
272
  }
273
+ // ⭐ `--published`: the services of the site as published, read from the page its host serves,
274
+ // for this dev server alone (`backend/site-published.js`).
275
+ if (wanted.published) {
276
+ const { readSitePublished, isOnThisMachine } = await import('../backend/site-published.js')
277
+ const got = await readSitePublished({ siteDir: sitePath, args })
278
+ if (got.refused) {
279
+ console.error(`${RED}✗${RESET} ${got.refused[0]}`)
280
+ for (const line of got.refused.slice(1)) console.error(` ${line}`)
281
+ process.exit(1)
282
+ }
283
+ const { PUBLISHED_ENV, encodePublished } = await import('@uniweb/build/site')
284
+ env = { ...env, [PUBLISHED_ENV]: encodePublished(got.published) }
285
+ const names = Object.keys(got.published.services)
286
+ console.error(
287
+ `${DIM}→ This copy, against the site as published at ${got.published.site}: ${names.join(', ') || 'no services'}${RESET}`
288
+ )
289
+ if (!isOnThisMachine(got.published.site)) {
290
+ console.error(
291
+ `${YELLOW}⚠${RESET} That site is not on this machine, so what you do here reads and changes its real data.`
292
+ )
293
+ }
294
+ }
256
295
  // That port or none: the dev server refuses to move past it (`dev-server.js`).
257
296
  if (wanted.port !== null) env = { ...env, [DEV_PORT_ENV]: String(wanted.port) }
258
297
 
@@ -1424,8 +1424,11 @@ export async function checkVideoTools({ sitePath, siteName, issues, tools }) {
1424
1424
  * cannot parse is reported as that, never as a page with no description. A page made from a record
1425
1425
  * (`[slug]`) takes its image and title from the record, which no file here describes, so it is left
1426
1426
  * out. An image at an address of its own is left alone: doctor reads files, it fetches nothing.
1427
+ *
1428
+ * `route` checks that one page, by the same rules: the page a post or an ad links to
1429
+ * (`uniweb promote check <route>`), whose card a network draws from its head.
1427
1430
  */
1428
- export async function checkLinkPreviews({ sitePath, siteName, issues }) {
1431
+ export async function checkLinkPreviews({ sitePath, siteName, issues, route = null }) {
1429
1432
  let content
1430
1433
  let failure = null
1431
1434
  // The collect reports its progress on the console; this report is doctor's.
@@ -1453,7 +1456,17 @@ export async function checkLinkPreviews({ sitePath, siteName, issues }) {
1453
1456
  }
1454
1457
 
1455
1458
  // A folder with no content of its own only redirects (to its first page), so a link never lands on it.
1456
- const pages = (content.pages || []).filter((p) => !p.isDynamic && p.hasContent !== false)
1459
+ let pages = (content.pages || []).filter((p) => !p.isDynamic && p.hasContent !== false)
1460
+ if (route !== null) {
1461
+ const key = (r) => String(r).replace(/\/+$/, '') || '/'
1462
+ pages = pages.filter((p) => key(p.route) === key(route))
1463
+ if (pages.length === 0) {
1464
+ const id = 'page-unknown'
1465
+ issues.push({ id, type: 'warn', site: siteName, message: `The site's files have no page ${route}` })
1466
+ warn(`[${id}] The site's files have no page ${route}, or it only leads to the page under it`)
1467
+ return
1468
+ }
1469
+ }
1457
1470
  if (pages.length === 0) return
1458
1471
  const site = content.config || {}
1459
1472
  const siteImage = site.seo?.image || null
@@ -1463,11 +1476,14 @@ export async function checkLinkPreviews({ sitePath, siteName, issues }) {
1463
1476
  if (without.length > 0) {
1464
1477
  const id = 'share-image-none'
1465
1478
  const n = without.length
1466
- const message = n === pages.length ? 'Links to this site show no image' : `Links to ${n} page${n === 1 ? '' : 's'} show no image`
1479
+ const message = route
1480
+ ? `Links to ${route} show no image`
1481
+ : n === pages.length ? 'Links to this site show no image' : `Links to ${n} page${n === 1 ? '' : 's'} show no image`
1467
1482
  issues.push({ id, type: 'info', site: siteName, message })
1468
1483
  info(`[${id}] ${message}`)
1469
1484
  if (without.length < pages.length) listSome(without.map((p) => p.route))
1470
- log(` Set ${colors.green}seo.image${colors.reset} in site.yml — every page without one of its own shows it. A ${CARD.width}×${CARD.height} JPEG or PNG fills a card; ${colors.green}uniweb snapshot --share${colors.reset} makes one from the site.`)
1485
+ if (route) log(` Give it ${colors.green}seo.image${colors.reset} in its page.yml, a card of its own — or set one in site.yml, which every page without one shows. A ${CARD.width}×${CARD.height} JPEG or PNG fills a card.`)
1486
+ else log(` Set ${colors.green}seo.image${colors.reset} in site.yml — every page without one of its own shows it. A ${CARD.width}×${CARD.height} JPEG or PNG fills a card; ${colors.green}uniweb snapshot --share${colors.reset} makes one from the site.`)
1471
1487
  }
1472
1488
 
1473
1489
  // Each image once, with the pages that show it.
@@ -1480,7 +1496,7 @@ export async function checkLinkPreviews({ sitePath, siteName, issues }) {
1480
1496
  }
1481
1497
  let readImageSize = null
1482
1498
  for (const [image, routes] of shownBy) {
1483
- const shownOn = routes.length === pages.length ? 'every page' : routes.length === 1 ? routes[0] : `${routes.length} pages`
1499
+ const shownOn = route ?? (routes.length === pages.length ? 'every page' : routes.length === 1 ? routes[0] : `${routes.length} pages`)
1484
1500
  const file = siteFile(sitePath, image)
1485
1501
  if (!file) {
1486
1502
  const id = 'share-image-missing'