@crombolo/server 0.69.1 → 0.71.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 (51) 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/users.ts +17 -12
  10. package/src/adapters/workspace-dictionary.ts +287 -0
  11. package/src/adapters/workspace-file-port.ts +160 -0
  12. package/src/adapters/workspace-languages.ts +94 -0
  13. package/src/approvals-api.ts +16 -7
  14. package/src/bundle-api.ts +19 -16
  15. package/src/bundle-install.ts +4 -0
  16. package/src/change-transfer.ts +676 -0
  17. package/src/changes-api.ts +258 -0
  18. package/src/component-templates-api.ts +2 -2
  19. package/src/config.ts +195 -21
  20. package/src/content-events.ts +65 -1
  21. package/src/content-operations.ts +22 -3
  22. package/src/core.ts +2 -0
  23. package/src/environment-values-api.ts +161 -0
  24. package/src/external-sign-in.ts +9 -2
  25. package/src/index.ts +1 -0
  26. package/src/managed-users.ts +122 -0
  27. package/src/node-operations.ts +150 -2
  28. package/src/ports.ts +174 -52
  29. package/src/principal.ts +1 -0
  30. package/src/remote-changes.ts +279 -0
  31. package/src/remote-schema.ts +126 -0
  32. package/src/remote-workspaces.ts +82 -0
  33. package/src/remote.ts +289 -192
  34. package/src/schema-transfer.ts +240 -41
  35. package/src/schema.ts +7 -2
  36. package/src/seed/compile.ts +13 -1
  37. package/src/seed/values.ts +3 -0
  38. package/src/server.ts +965 -364
  39. package/src/settings.ts +137 -5
  40. package/src/site-sections.ts +1 -1
  41. package/src/user-admin.ts +2 -0
  42. package/src/workspace-access.ts +152 -0
  43. package/src/workspace-checks.ts +299 -0
  44. package/src/workspace-losses.ts +95 -0
  45. package/src/workspace-processes.ts +306 -0
  46. package/src/workspace-render-child.ts +168 -0
  47. package/src/workspace-schema-dir.ts +100 -0
  48. package/src/workspace-schema.ts +598 -0
  49. package/src/workspace-sites.ts +179 -0
  50. package/src/workspace-views.ts +121 -0
  51. package/src/workspaces.ts +1031 -0
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The remote protocol's routes for the site's schema files (story 374): `schema`
3
+ * reads them, and `schema/edits` writes and removes them by the definition and
4
+ * applies the change at once, as a type saved in the back office is. Reading
5
+ * takes Pull; changing takes Push and ApplyStructure, at the `content` level.
6
+ */
7
+ import type { Principal } from '@crombolo/api-management/domain'
8
+ import { RemotePermissions, type RemoteVerb } from '@crombolo/core'
9
+ import type { createSchemaTransfer, SchemaEdit, SchemaWrites } from './schema-transfer.ts'
10
+
11
+ interface Problem {
12
+ code: string
13
+ title: string
14
+ detail?: string
15
+ items?: string[]
16
+ }
17
+
18
+ export interface SchemaRouteOptions {
19
+ transfer: () => Pick<ReturnType<typeof createSchemaTransfer>, 'files' | 'edit'>
20
+ problem(status: number, body: Problem): Response
21
+ /** Whether the site's remote access level is at least this. */
22
+ atLeast(level: 'read' | 'content'): boolean
23
+ /** A refusal naming the level a thing needs. */
24
+ notAtLevel(level: 'read' | 'content', what: string): Response
25
+ may(principal: Principal, verb: RemoteVerb): boolean
26
+ /** Push, and PushProtected where the environment is protected. */
27
+ mayChange(principal: Principal): boolean
28
+ }
29
+
30
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
31
+ typeof value === 'object' && value !== null && !Array.isArray(value)
32
+
33
+ /** An edit as a request names it, or undefined when it is not one. */
34
+ export function schemaEditOf(asked: unknown): SchemaEdit | undefined {
35
+ if (!isRecord(asked) || !isRecord(asked.edits)) return undefined
36
+ const edits: SchemaWrites = {}
37
+ for (const [path, text] of Object.entries(asked.edits)) {
38
+ if (typeof text !== 'string' && text !== null) return undefined
39
+ edits[path] = text
40
+ }
41
+ let base: Record<string, string | null> | undefined
42
+ if (asked.base !== undefined) {
43
+ if (!isRecord(asked.base)) return undefined
44
+ base = {}
45
+ for (const [path, hash] of Object.entries(asked.base)) {
46
+ if (typeof hash !== 'string' && hash !== null) return undefined
47
+ base[path] = hash
48
+ }
49
+ }
50
+ return {
51
+ edits,
52
+ ...(base ? { base } : {}),
53
+ retire: asked.retire === true,
54
+ dryRun: asked.dryRun === true,
55
+ }
56
+ }
57
+
58
+ export function createSchemaRoutes(options: SchemaRouteOptions) {
59
+ const { problem, atLeast, notAtLevel, may, mayChange } = options
60
+
61
+ return {
62
+ /** The capabilities these routes add to discovery, at the site's level. */
63
+ capabilities(): string[] {
64
+ return [
65
+ ...(atLeast('read') ? ['schema-files'] : []),
66
+ ...(atLeast('content') ? ['schema-edit'] : []),
67
+ ]
68
+ },
69
+
70
+ /** Whether the person may change the schema here. */
71
+ mayEdit(principal: Principal): boolean {
72
+ return (
73
+ atLeast('content') &&
74
+ mayChange(principal) &&
75
+ may(principal, RemotePermissions.ApplyStructure)
76
+ )
77
+ },
78
+
79
+ /** Answers one of these routes, or undefined when `what` is none of them. */
80
+ async handle(
81
+ request: Request,
82
+ principal: Principal,
83
+ what: string,
84
+ ): Promise<Response | undefined> {
85
+ if (what === 'schema') {
86
+ if (request.method !== 'GET') return new Response(null, { status: 405 })
87
+ if (!atLeast('read')) return notAtLevel('read', 'reading the schema')
88
+ if (!may(principal, RemotePermissions.Pull))
89
+ return problem(403, { code: 'forbidden', title: 'You may not read this site’s schema' })
90
+ return Response.json(options.transfer().files(), {
91
+ headers: { 'cache-control': 'private, no-store' },
92
+ })
93
+ }
94
+ if (what !== 'schema/edits') return undefined
95
+ if (request.method !== 'POST') return new Response(null, { status: 405 })
96
+ if (!atLeast('content')) return notAtLevel('content', 'changes to the schema')
97
+ if (!mayChange(principal))
98
+ return problem(403, { code: 'forbidden', title: 'You may not change this environment' })
99
+ if (!may(principal, RemotePermissions.ApplyStructure))
100
+ return problem(403, {
101
+ code: 'forbidden-structure',
102
+ title: 'You may not change this site’s schema',
103
+ detail: 'Changing content types and data types needs Crombolo.Remote.ApplyStructure.',
104
+ })
105
+ const asked = schemaEditOf(await request.json().catch(() => undefined))
106
+ if (!asked)
107
+ return problem(400, {
108
+ code: 'bad-request',
109
+ title: 'That is not a schema edit',
110
+ detail: 'Send { edits: { <path>: <text> | null }, base?, retire?, dryRun? }.',
111
+ })
112
+ const outcome = await options
113
+ .transfer()
114
+ .edit({ id: principal.id, name: principal.name, ipAddress: null }, asked)
115
+ if (outcome.ok) {
116
+ const { ok: _, ...result } = outcome
117
+ return Response.json(result)
118
+ }
119
+ return problem(outcome.status, {
120
+ code: outcome.code ?? 'refused',
121
+ title: outcome.message,
122
+ ...(outcome.problems ? { items: outcome.problems } : {}),
123
+ })
124
+ },
125
+ }
126
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The remote protocol's workspace routes (stories 380, 386): a host merging one, and
3
+ * the command line working in them. `workspaces` lists and opens; `workspaces/{key}`
4
+ * and what is under it (members, diff, update, conflicts, files, check, pages, errors,
5
+ * render, open, links, merge) are the workspace routes' own, at the `content` level,
6
+ * as the person, by the same rules as the back office.
7
+ */
8
+ import type { Principal } from '@crombolo/api-management/domain'
9
+
10
+ interface Problem {
11
+ code: string
12
+ title: string
13
+ detail?: string
14
+ items?: string[]
15
+ }
16
+
17
+ export interface WorkspaceRouteOptions {
18
+ /** The workspace routes, as `<plugin>/api/workspaces` answers them. */
19
+ workspaces: () =>
20
+ | {
21
+ handle(
22
+ request: Request,
23
+ principal: Principal,
24
+ rest: string,
25
+ client: { ipAddress: string | null; forwardedFor: string | null },
26
+ ): Promise<Response>
27
+ }
28
+ | undefined
29
+ enabled: () => boolean
30
+ problem(status: number, body: Problem): Response
31
+ atLeast(level: 'content'): boolean
32
+ notAtLevel(level: 'content', what: string): Response
33
+ }
34
+
35
+ const MERGE_PATH = /^workspaces\/([0-9a-f-]{36})\/merge$/i
36
+ const UNDER = /^workspaces(?:\/(.*))?$/
37
+
38
+ export function createWorkspaceRoutes(options: WorkspaceRouteOptions) {
39
+ return {
40
+ capabilities(): string[] {
41
+ return options.enabled() && options.atLeast('content') && options.workspaces()
42
+ ? ['workspace-merge', 'workspaces']
43
+ : []
44
+ },
45
+
46
+ /** Answers the route, or undefined when `what` is not it. */
47
+ async handle(
48
+ request: Request,
49
+ principal: Principal,
50
+ what: string,
51
+ ): Promise<Response | undefined> {
52
+ const routes = options.workspaces()
53
+ const under = UNDER.exec(what)
54
+ if (!under || !routes) return undefined
55
+ const key = what.match(MERGE_PATH)?.[1]
56
+ if (!options.atLeast('content'))
57
+ return options.notAtLevel('content', key ? 'merging a workspace' : 'workspaces')
58
+ if (!options.enabled())
59
+ return options.problem(404, {
60
+ code: 'not-enabled',
61
+ title: 'Workspaces are not turned on for this site',
62
+ })
63
+ const answered = await routes.handle(request, principal, `/${under[1] ?? ''}`, {
64
+ ipAddress: null,
65
+ forwardedFor: null,
66
+ })
67
+ if (answered.ok || answered.status === 204) return answered
68
+ const body = (await answered.json().catch(() => ({}))) as {
69
+ message?: string
70
+ blockers?: string[]
71
+ refusals?: string[]
72
+ }
73
+ const items = body.blockers ?? body.refusals
74
+ return options.problem(answered.status, {
75
+ code:
76
+ answered.status === 409 ? 'conflict' : answered.status === 403 ? 'forbidden' : 'refused',
77
+ title: body.message ?? (key ? 'The workspace cannot be merged' : 'It was refused'),
78
+ ...(items ? { items } : {}),
79
+ })
80
+ },
81
+ }
82
+ }