@crombolo/server 0.69.0 → 0.70.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.
Files changed (46) hide show
  1. package/package.json +15 -14
  2. package/src/adapters/blueprints.ts +51 -5
  3. package/src/adapters/change-recording.ts +140 -0
  4. package/src/adapters/content.ts +132 -21
  5. package/src/adapters/documents.ts +26 -5
  6. package/src/adapters/elements.ts +3 -0
  7. package/src/adapters/media.ts +7 -1
  8. package/src/adapters/schema-files.ts +89 -1
  9. package/src/adapters/workspace-dictionary.ts +287 -0
  10. package/src/adapters/workspace-file-port.ts +160 -0
  11. package/src/adapters/workspace-languages.ts +94 -0
  12. package/src/approvals-api.ts +16 -7
  13. package/src/bundle-api.ts +19 -16
  14. package/src/bundle-install.ts +4 -0
  15. package/src/change-transfer.ts +676 -0
  16. package/src/changes-api.ts +258 -0
  17. package/src/component-templates-api.ts +2 -2
  18. package/src/config.ts +162 -22
  19. package/src/content-events.ts +65 -1
  20. package/src/content-operations.ts +22 -3
  21. package/src/core.ts +2 -0
  22. package/src/environment-values-api.ts +161 -0
  23. package/src/host-token.ts +30 -1
  24. package/src/node-operations.ts +150 -2
  25. package/src/ports.ts +174 -52
  26. package/src/remote-changes.ts +279 -0
  27. package/src/remote-schema.ts +126 -0
  28. package/src/remote-workspaces.ts +82 -0
  29. package/src/remote.ts +289 -192
  30. package/src/schema-transfer.ts +240 -41
  31. package/src/schema.ts +7 -2
  32. package/src/seed/compile.ts +13 -1
  33. package/src/seed/values.ts +3 -0
  34. package/src/server.ts +971 -364
  35. package/src/settings.ts +123 -5
  36. package/src/site-sections.ts +1 -1
  37. package/src/workspace-access.ts +152 -0
  38. package/src/workspace-checks.ts +299 -0
  39. package/src/workspace-losses.ts +95 -0
  40. package/src/workspace-processes.ts +306 -0
  41. package/src/workspace-render-child.ts +168 -0
  42. package/src/workspace-schema-dir.ts +100 -0
  43. package/src/workspace-schema.ts +598 -0
  44. package/src/workspace-sites.ts +179 -0
  45. package/src/workspace-views.ts +121 -0
  46. package/src/workspaces.ts +1031 -0
package/src/remote.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  * the media store so any node can serve the download. A node that stops leaves
11
11
  * its job's heartbeat to go stale, and the job is then reported failed.
12
12
  */
13
+ import { AsyncLocalStorage } from 'node:async_hooks'
13
14
  import { createHash } from 'node:crypto'
14
15
  import { createWriteStream } from 'node:fs'
15
16
  import { mkdir, rm } from 'node:fs/promises'
@@ -65,20 +66,28 @@ import {
65
66
  type SectionFile,
66
67
  swappedSections,
67
68
  } from './bundle-sections.ts'
68
- import { type CromboloConfig, REMOTE_ACCESS_LEVELS, type RemoteAccess } from './config.ts'
69
+ import {
70
+ type CromboloConfig,
71
+ REMOTE_ACCESS_LEVELS,
72
+ type RemoteAccess,
73
+ workspacesOn,
74
+ } from './config.ts'
69
75
  import { answerContentQuery, contentQueryScope } from './content-query.ts'
70
76
  import { MediaFileStore, mediaStoreFor } from './media-files.ts'
71
77
  import { type MediaStore, streamOf } from './media-store.ts'
72
78
  import { siteMemberPorts } from './member-admin.ts'
73
79
  import { describeProvenance, provenanceProblem, verifyProvenance } from './provenance.ts'
74
80
  import { createBackgroundJobRoutes } from './remote-background-jobs.ts'
81
+ import { type ChangeRouteOptions, createChangeRoutes } from './remote-changes.ts'
75
82
  import { createCiSignIn, readRuleInput, trustedIssuers, unverifiedIssuer } from './remote-ci.ts'
76
83
  import { createExtensionActivityRoutes } from './remote-extension-activity.ts'
77
84
  import { createMediaLibraryRoutes } from './remote-media-library.ts'
78
85
  import { createMemberRoutes } from './remote-members.ts'
79
86
  import type { PeopleExchange } from './remote-people.ts'
87
+ import { createSchemaRoutes, type SchemaRouteOptions } from './remote-schema.ts'
80
88
  import { createUndoRoutes } from './remote-undo.ts'
81
89
  import { createUserRoutes } from './remote-users.ts'
90
+ import { createWorkspaceRoutes, type WorkspaceRouteOptions } from './remote-workspaces.ts'
82
91
  import { openSecret, sealSecret, usableSecretKey } from './secrets.ts'
83
92
  import { exportSite } from './site-file.ts'
84
93
  import { REMOTE_DISCOVERY_PATH } from './site-public.ts'
@@ -128,6 +137,14 @@ export interface RemoteOptions {
128
137
  activity?: { flush(): Promise<void>; days: number }
129
138
  /** Told after a media item's permissions change here (story 355), for this node's media cache. */
130
139
  onMediaAccessChange?(key: string): void | Promise<void>
140
+ /** The site's schema files, read and changed by the definition (story 374); absent, those routes are not offered. */
141
+ schema?: SchemaRouteOptions['transfer']
142
+ /** The workspace routes, for a host to merge one (story 380); absent, that route is not offered. */
143
+ workspaces?: WorkspaceRouteOptions['workspaces']
144
+ /** Moving changes between sites (story 383): absent where workspaces are off. */
145
+ changes?: ChangeRouteOptions['changes']
146
+ /** A change's detail and revert (story 386). */
147
+ changeRoutes?: ChangeRouteOptions['changeRoutes']
131
148
  }
132
149
 
133
150
  /** What a supervised restart came to: the new server serving, or why the old one still is. */
@@ -139,6 +156,8 @@ export interface RestartOutcome {
139
156
  export interface Remote {
140
157
  /** Answers a remote path, or undefined when the path is not one. */
141
158
  handle(request: Request, url: URL): Promise<Response | undefined>
159
+ /** A request as someone already known, on this site, as a local command makes it (story 386). */
160
+ handleAs(request: Request, url: URL, principal: Principal): Promise<Response | undefined>
142
161
  /** Whether a request is an upload the remote takes beyond the usual body limit. */
143
162
  isUpload(request: Request, url: URL): boolean
144
163
  /**
@@ -210,7 +229,10 @@ export function createRemote(options: RemoteOptions): Remote {
210
229
 
211
230
  /** What the site takes (story 167): each level the ones before it and more. */
212
231
  const level: RemoteAccess = settings.access ?? (settings.enabled ? 'all' : 'off')
232
+ // A local command's own request (story 386): every level, with the remote off as well.
233
+ const local = new AsyncLocalStorage<boolean>()
213
234
  const atLeast = (wanted: RemoteAccess) =>
235
+ local.getStore() === true ||
214
236
  REMOTE_ACCESS_LEVELS.indexOf(level) >= REMOTE_ACCESS_LEVELS.indexOf(wanted)
215
237
  const notAtLevel = (wanted: RemoteAccess, what: string) =>
216
238
  problem(403, {
@@ -260,6 +282,9 @@ export function createRemote(options: RemoteOptions): Remote {
260
282
  ...backgroundJobRoutes.capabilities,
261
283
  ...(activityRoutes?.capabilities ?? []),
262
284
  ...(mediaLibraryRoutes?.capabilities ?? []),
285
+ ...(schemaRoutes?.capabilities() ?? []),
286
+ ...(workspaceRoutes?.capabilities() ?? []),
287
+ ...(changeRoutes?.capabilities() ?? []),
263
288
  ...(settings.approvals ? ['approvals'] : []),
264
289
  ...(options.bundles && atLeast('read') ? ['bundle-manage'] : []),
265
290
  ...(options.restart && atLeast('all') ? ['restart'] : []),
@@ -305,6 +330,40 @@ export function createRemote(options: RemoteOptions): Remote {
305
330
  })
306
331
  : undefined
307
332
 
333
+ const schemaRoutes = options.schema
334
+ ? createSchemaRoutes({
335
+ transfer: options.schema,
336
+ problem,
337
+ atLeast,
338
+ notAtLevel,
339
+ may,
340
+ mayChange,
341
+ })
342
+ : undefined
343
+
344
+ const workspaceRoutes = options.workspaces
345
+ ? createWorkspaceRoutes({
346
+ workspaces: options.workspaces,
347
+ enabled: () => workspacesOn(config),
348
+ problem,
349
+ atLeast,
350
+ notAtLevel,
351
+ })
352
+ : undefined
353
+
354
+ const changeRoutes = options.changes
355
+ ? createChangeRoutes({
356
+ db,
357
+ changes: options.changes,
358
+ ...(options.changeRoutes ? { changeRoutes: options.changeRoutes } : {}),
359
+ problem,
360
+ atLeast,
361
+ notAtLevel,
362
+ may,
363
+ receive: (request, path) => receive(request, path),
364
+ })
365
+ : undefined
366
+
308
367
  const memberRoutes = createMemberRoutes({
309
368
  db,
310
369
  ports: () => siteMemberPorts(db, config, options.nodeState),
@@ -1522,6 +1581,227 @@ export function createRemote(options: RemoteOptions): Remote {
1522
1581
  }
1523
1582
  }
1524
1583
 
1584
+ /** Every route but discovery and the token exchange, for someone already known. */
1585
+ async function routed(
1586
+ request: Request,
1587
+ url: URL,
1588
+ principal: Principal,
1589
+ ): Promise<Response | undefined> {
1590
+ const { pathname } = url
1591
+ const rest = pathname.slice(REMOTE_API_PATH.length + 1)
1592
+
1593
+ // Who this is, for `crombolo whoami`: a person, or CI acting by a rule.
1594
+ if (rest === 'me' && request.method === 'GET')
1595
+ return Response.json({
1596
+ name: principal.name,
1597
+ kind: principal.ci ? 'ci' : 'person',
1598
+ ...(principal.ci ? { rule: principal.ci.rule, run: principal.ci.claims } : {}),
1599
+ permissions: Object.values(RemotePermissions).filter((verb) => may(principal, verb)),
1600
+ })
1601
+ if (rest === 'trust' || rest.startsWith('trust/')) return trustRoute(request, principal, rest)
1602
+
1603
+ if (rest === 'environments' && request.method === 'GET')
1604
+ return Response.json([
1605
+ {
1606
+ ...environment(),
1607
+ may: {
1608
+ pull: atLeast('read') && may(principal, RemotePermissions.Pull),
1609
+ pullFull: atLeast('read') && may(principal, RemotePermissions.PullFull),
1610
+ // A whole-site push changes structure, so it needs that right and the top level.
1611
+ // A push replaces templates too, which are code (story 336).
1612
+ push:
1613
+ atLeast('all') &&
1614
+ mayChange(principal) &&
1615
+ may(principal, RemotePermissions.ApplyStructure) &&
1616
+ may(principal, RemotePermissions.InstallCode),
1617
+ apply: atLeast('content') && mayChange(principal),
1618
+ applyStructure:
1619
+ atLeast('all') &&
1620
+ mayChange(principal) &&
1621
+ may(principal, RemotePermissions.ApplyStructure),
1622
+ audit: mayReadAudit(principal),
1623
+ backgroundJobs: backgroundJobRoutes.may(principal),
1624
+ extensionActivity: activityRoutes?.may(principal) ?? false,
1625
+ mediaLibrary: mediaLibraryRoutes?.mayMedia(principal) ?? false,
1626
+ contentShow: mediaLibraryRoutes?.mayContent(principal) ?? false,
1627
+ schemaEdit: schemaRoutes?.mayEdit(principal) ?? false,
1628
+ applyChanges:
1629
+ Boolean(changeRoutes?.capabilities().length) &&
1630
+ atLeast('content') &&
1631
+ may(principal, RemotePermissions.ApplyChanges),
1632
+ },
1633
+ },
1634
+ ])
1635
+
1636
+ const job = /^jobs\/([0-9a-f-]{36})(?:\/(download|approve|decline))?$/.exec(rest)
1637
+ if (job) {
1638
+ const [, id, action] = job as unknown as [string, string, string | undefined]
1639
+ if (!action && request.method === 'GET') {
1640
+ const found = await jobs.get(id)
1641
+ // A job is its requester's, and an approver's to look at.
1642
+ if (
1643
+ !found ||
1644
+ (found.requestedBy !== principal.id && !may(principal, RemotePermissions.PushProtected))
1645
+ )
1646
+ return problem(404, { code: 'not-found', title: 'There is no such job' })
1647
+ return Response.json(await describe(found, url.origin))
1648
+ }
1649
+ if (action === 'download' && request.method === 'GET') return download(id, principal)
1650
+ if ((action === 'approve' || action === 'decline') && request.method === 'POST')
1651
+ return decide(request, url, principal, id, action === 'approve')
1652
+ return new Response(null, { status: 405 })
1653
+ }
1654
+
1655
+ const env = /^environments\/([a-z][a-z0-9-]*)\/(.+)$/.exec(rest)
1656
+ if (!env) return problem(404, { code: 'not-found', title: 'There is nothing here' })
1657
+ const [, envId, what] = env as unknown as [string, string, string]
1658
+ if (envId !== settings.environment)
1659
+ return problem(404, {
1660
+ code: 'no-such-environment',
1661
+ title: `There is no environment "${envId}"`,
1662
+ })
1663
+
1664
+ if (what === 'audit' && request.method === 'GET') return readAudit(url, principal)
1665
+ // Finding pages by their facts (story 183), as the signed-in user, as in the back office.
1666
+ if (what === 'content/query' && request.method === 'POST') {
1667
+ if (!hasSection(principal, 'content'))
1668
+ return problem(403, { code: 'forbidden', title: 'You may not search content here' })
1669
+ return answerContentQuery(
1670
+ db,
1671
+ contentQueryScope(principal),
1672
+ await request.json().catch(() => undefined),
1673
+ )
1674
+ }
1675
+ if (what.startsWith('member') || what.startsWith('content/')) {
1676
+ const answered = await memberRoutes.handle(request, url, principal, what)
1677
+ if (answered) return answered
1678
+ }
1679
+ if (what.startsWith('background-jobs')) {
1680
+ const answered = await backgroundJobRoutes.handle(request, url, principal, what)
1681
+ if (answered) return answered
1682
+ }
1683
+ if (what.startsWith('user') || what.startsWith('sign-ins')) {
1684
+ const answered = await userRoutes.handle(request, url, principal, what)
1685
+ if (answered) return answered
1686
+ }
1687
+ if (
1688
+ undo &&
1689
+ (what.startsWith('content/') || what === 'recycle-bin' || what.startsWith('recycle-bin/'))
1690
+ ) {
1691
+ const answered = await undo.handle(request, url, principal, what)
1692
+ if (answered) return answered
1693
+ }
1694
+ if (
1695
+ mediaLibraryRoutes &&
1696
+ (what.startsWith('media-items') || what.startsWith('content-items/'))
1697
+ ) {
1698
+ const answered = await mediaLibraryRoutes.handle(request, url, principal, what)
1699
+ if (answered) return answered
1700
+ }
1701
+ if (
1702
+ changeRoutes &&
1703
+ (what === 'changes' || what.startsWith('changes/') || what.startsWith('workspaces/'))
1704
+ ) {
1705
+ const answered = await changeRoutes.handle(request, url, principal, what)
1706
+ if (answered) return answered
1707
+ }
1708
+ if (workspaceRoutes && (what === 'workspaces' || what.startsWith('workspaces/'))) {
1709
+ const answered = await workspaceRoutes.handle(request, principal, what)
1710
+ if (answered) return answered
1711
+ }
1712
+ if (schemaRoutes && what.startsWith('schema')) {
1713
+ const answered = await schemaRoutes.handle(request, principal, what)
1714
+ if (answered) return answered
1715
+ }
1716
+ if (what === 'restart' && request.method === 'POST') return restartSite(request, principal)
1717
+ if (activityRoutes && what.startsWith('installed/')) {
1718
+ const answered = await activityRoutes.handle(request, url, principal, what)
1719
+ if (answered) return answered
1720
+ }
1721
+ if (what === 'installed' || what.startsWith('installed/'))
1722
+ return manageBundles(request, principal, what)
1723
+ // Story 167: the site's level first, then the person's permissions.
1724
+ const pulling =
1725
+ what === 'pulls' ||
1726
+ what === 'media/links' ||
1727
+ (what.startsWith('media/') && request.method === 'GET')
1728
+ const pushing =
1729
+ what === 'pushes' ||
1730
+ what === 'pushes/check' ||
1731
+ what === 'media/have' ||
1732
+ (what.startsWith('media/') && request.method === 'PUT')
1733
+ if (pulling && !atLeast('read')) return notAtLevel('read', 'pulls')
1734
+ if (pushing && !atLeast('all')) return notAtLevel('all', 'whole-site pushes')
1735
+ // Push and protection are refused by their own routes first, as before story 167.
1736
+ if (pushing && mayChange(principal) && !may(principal, RemotePermissions.ApplyStructure))
1737
+ return problem(403, {
1738
+ code: 'forbidden-structure',
1739
+ title: 'You may not push a whole site here',
1740
+ detail:
1741
+ "A push replaces the site's content types, views and code, which needs Crombolo.Remote.ApplyStructure.",
1742
+ })
1743
+ // Its templates are code, and what a push will change is not known until it is uploaded (story 336).
1744
+ if (pushing && mayChange(principal) && !may(principal, RemotePermissions.InstallCode))
1745
+ return problem(403, {
1746
+ code: 'forbidden-code',
1747
+ title: 'You may not push a whole site here',
1748
+ detail:
1749
+ 'A push replaces the site\'s templates, which are code, so it needs "Install code on this site" (Crombolo.Remote.InstallCode).',
1750
+ })
1751
+ if (what === 'bundles' && request.method === 'PUT') return applyBundle(request, principal)
1752
+ if (what === 'runs' && request.method === 'GET') return listRuns(url, principal)
1753
+ const revert = /^runs\/([0-9a-f-]{36})\/revert$/.exec(what)
1754
+ if (revert && request.method === 'POST')
1755
+ return revertBundleRun(request, principal, revert[1] as string)
1756
+ if (what === 'pulls' && request.method === 'POST') return startPull(request, url, principal)
1757
+ if (what === 'pushes' && request.method === 'PUT') return startPush(request, url, principal)
1758
+ if (what === 'pushes/check' && request.method === 'POST') {
1759
+ if (
1760
+ !may(principal, RemotePermissions.Push) ||
1761
+ (settings.protected && !may(principal, RemotePermissions.PushProtected))
1762
+ )
1763
+ return problem(403, { code: 'forbidden', title: 'You may not push to this environment' })
1764
+ return pushCheck(request)
1765
+ }
1766
+ if (what === 'media/links' && request.method === 'POST') {
1767
+ if (!may(principal, RemotePermissions.Pull))
1768
+ return problem(403, {
1769
+ code: 'forbidden',
1770
+ title: 'You may not pull from this environment',
1771
+ })
1772
+ return mediaLinks(request, url)
1773
+ }
1774
+ if (what === 'media/have' && request.method === 'POST') {
1775
+ if (!may(principal, RemotePermissions.Push))
1776
+ return problem(403, { code: 'forbidden', title: 'You may not push to this environment' })
1777
+ return mediaHave(request)
1778
+ }
1779
+ if (what.startsWith('media/')) {
1780
+ const key = mediaKeyOf(what.slice('media/'.length))
1781
+ if (!key) return problem(400, { code: 'bad-key', title: 'That is not a media key' })
1782
+ if (request.method === 'GET') {
1783
+ if (!may(principal, RemotePermissions.Pull))
1784
+ return problem(403, {
1785
+ code: 'forbidden',
1786
+ title: 'You may not pull from this environment',
1787
+ })
1788
+ return mediaGet(key)
1789
+ }
1790
+ if (request.method === 'PUT') {
1791
+ if (
1792
+ !may(principal, RemotePermissions.Push) ||
1793
+ (settings.protected && !may(principal, RemotePermissions.PushProtected))
1794
+ )
1795
+ return problem(403, {
1796
+ code: 'forbidden',
1797
+ title: 'You may not push to this environment',
1798
+ })
1799
+ return mediaPut(request, key)
1800
+ }
1801
+ }
1802
+ return new Response(null, { status: 405 })
1803
+ }
1804
+
1525
1805
  return {
1526
1806
  trust: trustRoute,
1527
1807
 
@@ -1574,198 +1854,15 @@ export function createRemote(options: RemoteOptions): Remote {
1574
1854
  title: 'Sign in first',
1575
1855
  detail: 'Sign in with the device grant the discovery document names.',
1576
1856
  })
1577
- const rest = pathname.slice(REMOTE_API_PATH.length + 1)
1578
-
1579
- // Who this is, for `crombolo whoami`: a person, or CI acting by a rule.
1580
- if (rest === 'me' && request.method === 'GET')
1581
- return Response.json({
1582
- name: principal.name,
1583
- kind: principal.ci ? 'ci' : 'person',
1584
- ...(principal.ci ? { rule: principal.ci.rule, run: principal.ci.claims } : {}),
1585
- permissions: Object.values(RemotePermissions).filter((verb) => may(principal, verb)),
1586
- })
1587
- if (rest === 'trust' || rest.startsWith('trust/')) return trustRoute(request, principal, rest)
1588
-
1589
- if (rest === 'environments' && request.method === 'GET')
1590
- return Response.json([
1591
- {
1592
- ...environment(),
1593
- may: {
1594
- pull: atLeast('read') && may(principal, RemotePermissions.Pull),
1595
- pullFull: atLeast('read') && may(principal, RemotePermissions.PullFull),
1596
- // A whole-site push changes structure, so it needs that right and the top level.
1597
- // A push replaces templates too, which are code (story 336).
1598
- push:
1599
- atLeast('all') &&
1600
- mayChange(principal) &&
1601
- may(principal, RemotePermissions.ApplyStructure) &&
1602
- may(principal, RemotePermissions.InstallCode),
1603
- apply: atLeast('content') && mayChange(principal),
1604
- applyStructure:
1605
- atLeast('all') &&
1606
- mayChange(principal) &&
1607
- may(principal, RemotePermissions.ApplyStructure),
1608
- audit: mayReadAudit(principal),
1609
- backgroundJobs: backgroundJobRoutes.may(principal),
1610
- extensionActivity: activityRoutes?.may(principal) ?? false,
1611
- mediaLibrary: mediaLibraryRoutes?.mayMedia(principal) ?? false,
1612
- contentShow: mediaLibraryRoutes?.mayContent(principal) ?? false,
1613
- },
1614
- },
1615
- ])
1616
-
1617
- const job = /^jobs\/([0-9a-f-]{36})(?:\/(download|approve|decline))?$/.exec(rest)
1618
- if (job) {
1619
- const [, id, action] = job as unknown as [string, string, string | undefined]
1620
- if (!action && request.method === 'GET') {
1621
- const found = await jobs.get(id)
1622
- // A job is its requester's, and an approver's to look at.
1623
- if (
1624
- !found ||
1625
- (found.requestedBy !== principal.id && !may(principal, RemotePermissions.PushProtected))
1626
- )
1627
- return problem(404, { code: 'not-found', title: 'There is no such job' })
1628
- return Response.json(await describe(found, url.origin))
1629
- }
1630
- if (action === 'download' && request.method === 'GET') return download(id, principal)
1631
- if ((action === 'approve' || action === 'decline') && request.method === 'POST')
1632
- return decide(request, url, principal, id, action === 'approve')
1633
- return new Response(null, { status: 405 })
1634
- }
1635
-
1636
- const env = /^environments\/([a-z][a-z0-9-]*)\/(.+)$/.exec(rest)
1637
- if (!env) return problem(404, { code: 'not-found', title: 'There is nothing here' })
1638
- const [, envId, what] = env as unknown as [string, string, string]
1639
- if (envId !== settings.environment)
1640
- return problem(404, {
1641
- code: 'no-such-environment',
1642
- title: `There is no environment "${envId}"`,
1643
- })
1857
+ return routed(request, url, principal)
1858
+ },
1644
1859
 
1645
- if (what === 'audit' && request.method === 'GET') return readAudit(url, principal)
1646
- // Finding pages by their facts (story 183), as the signed-in user, as in the back office.
1647
- if (what === 'content/query' && request.method === 'POST') {
1648
- if (!hasSection(principal, 'content'))
1649
- return problem(403, { code: 'forbidden', title: 'You may not search content here' })
1650
- return answerContentQuery(
1651
- db,
1652
- contentQueryScope(principal),
1653
- await request.json().catch(() => undefined),
1654
- )
1655
- }
1656
- if (what.startsWith('member') || what.startsWith('content/')) {
1657
- const answered = await memberRoutes.handle(request, url, principal, what)
1658
- if (answered) return answered
1659
- }
1660
- if (what.startsWith('background-jobs')) {
1661
- const answered = await backgroundJobRoutes.handle(request, url, principal, what)
1662
- if (answered) return answered
1663
- }
1664
- if (what.startsWith('user') || what.startsWith('sign-ins')) {
1665
- const answered = await userRoutes.handle(request, url, principal, what)
1666
- if (answered) return answered
1667
- }
1668
- if (
1669
- undo &&
1670
- (what.startsWith('content/') || what === 'recycle-bin' || what.startsWith('recycle-bin/'))
1671
- ) {
1672
- const answered = await undo.handle(request, url, principal, what)
1673
- if (answered) return answered
1674
- }
1675
- if (
1676
- mediaLibraryRoutes &&
1677
- (what.startsWith('media-items') || what.startsWith('content-items/'))
1678
- ) {
1679
- const answered = await mediaLibraryRoutes.handle(request, url, principal, what)
1680
- if (answered) return answered
1681
- }
1682
- if (what === 'restart' && request.method === 'POST') return restartSite(request, principal)
1683
- if (activityRoutes && what.startsWith('installed/')) {
1684
- const answered = await activityRoutes.handle(request, url, principal, what)
1685
- if (answered) return answered
1686
- }
1687
- if (what === 'installed' || what.startsWith('installed/'))
1688
- return manageBundles(request, principal, what)
1689
- // Story 167: the site's level first, then the person's permissions.
1690
- const pulling =
1691
- what === 'pulls' ||
1692
- what === 'media/links' ||
1693
- (what.startsWith('media/') && request.method === 'GET')
1694
- const pushing =
1695
- what === 'pushes' ||
1696
- what === 'pushes/check' ||
1697
- what === 'media/have' ||
1698
- (what.startsWith('media/') && request.method === 'PUT')
1699
- if (pulling && !atLeast('read')) return notAtLevel('read', 'pulls')
1700
- if (pushing && !atLeast('all')) return notAtLevel('all', 'whole-site pushes')
1701
- // Push and protection are refused by their own routes first, as before story 167.
1702
- if (pushing && mayChange(principal) && !may(principal, RemotePermissions.ApplyStructure))
1703
- return problem(403, {
1704
- code: 'forbidden-structure',
1705
- title: 'You may not push a whole site here',
1706
- detail:
1707
- "A push replaces the site's content types, views and code, which needs Crombolo.Remote.ApplyStructure.",
1708
- })
1709
- // Its templates are code, and what a push will change is not known until it is uploaded (story 336).
1710
- if (pushing && mayChange(principal) && !may(principal, RemotePermissions.InstallCode))
1711
- return problem(403, {
1712
- code: 'forbidden-code',
1713
- title: 'You may not push a whole site here',
1714
- detail:
1715
- 'A push replaces the site\'s templates, which are code, so it needs "Install code on this site" (Crombolo.Remote.InstallCode).',
1716
- })
1717
- if (what === 'bundles' && request.method === 'PUT') return applyBundle(request, principal)
1718
- if (what === 'runs' && request.method === 'GET') return listRuns(url, principal)
1719
- const revert = /^runs\/([0-9a-f-]{36})\/revert$/.exec(what)
1720
- if (revert && request.method === 'POST')
1721
- return revertBundleRun(request, principal, revert[1] as string)
1722
- if (what === 'pulls' && request.method === 'POST') return startPull(request, url, principal)
1723
- if (what === 'pushes' && request.method === 'PUT') return startPush(request, url, principal)
1724
- if (what === 'pushes/check' && request.method === 'POST') {
1725
- if (
1726
- !may(principal, RemotePermissions.Push) ||
1727
- (settings.protected && !may(principal, RemotePermissions.PushProtected))
1728
- )
1729
- return problem(403, { code: 'forbidden', title: 'You may not push to this environment' })
1730
- return pushCheck(request)
1731
- }
1732
- if (what === 'media/links' && request.method === 'POST') {
1733
- if (!may(principal, RemotePermissions.Pull))
1734
- return problem(403, {
1735
- code: 'forbidden',
1736
- title: 'You may not pull from this environment',
1737
- })
1738
- return mediaLinks(request, url)
1739
- }
1740
- if (what === 'media/have' && request.method === 'POST') {
1741
- if (!may(principal, RemotePermissions.Push))
1742
- return problem(403, { code: 'forbidden', title: 'You may not push to this environment' })
1743
- return mediaHave(request)
1744
- }
1745
- if (what.startsWith('media/')) {
1746
- const key = mediaKeyOf(what.slice('media/'.length))
1747
- if (!key) return problem(400, { code: 'bad-key', title: 'That is not a media key' })
1748
- if (request.method === 'GET') {
1749
- if (!may(principal, RemotePermissions.Pull))
1750
- return problem(403, {
1751
- code: 'forbidden',
1752
- title: 'You may not pull from this environment',
1753
- })
1754
- return mediaGet(key)
1755
- }
1756
- if (request.method === 'PUT') {
1757
- if (
1758
- !may(principal, RemotePermissions.Push) ||
1759
- (settings.protected && !may(principal, RemotePermissions.PushProtected))
1760
- )
1761
- return problem(403, {
1762
- code: 'forbidden',
1763
- title: 'You may not push to this environment',
1764
- })
1765
- return mediaPut(request, key)
1766
- }
1767
- }
1768
- return new Response(null, { status: 405 })
1860
+ /**
1861
+ * A request as `principal`, on this site, as a command run in its folder makes it
1862
+ * (story 386): the same routes, at every level, with the remote off as well.
1863
+ */
1864
+ handleAs(request, url, principal) {
1865
+ return local.run(true, () => routed(request, url, principal))
1769
1866
  },
1770
1867
  }
1771
1868
  }