@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,94 @@
1
+ /**
2
+ * The languages a workspace holds (story 392): its copy of `schema/languages.toml`,
3
+ * read and written as the localization port in that workspace. The merge brings the
4
+ * file into Shared with the other definitions. docs/workspaces.md.
5
+ */
6
+ import type { LanguageInput, LocalizationPort } from '@crombolo/api-management/domain'
7
+ import { type Db, WorkspaceFileRepository } from '@crombolo/data'
8
+ import { parseLanguages, type SchemaLanguage, writeLanguages } from '@crombolo/schema'
9
+ import { WORKSPACE_SCHEMA_PREFIX } from './schema-files.ts'
10
+
11
+ /** Where a workspace keeps its languages among its files. */
12
+ export const WORKSPACE_LANGUAGES = `${WORKSPACE_SCHEMA_PREFIX}languages.toml`
13
+
14
+ const toInput = (l: SchemaLanguage): LanguageInput => ({
15
+ isoCode: l.iso,
16
+ name: l.name,
17
+ isDefault: l.default,
18
+ isMandatory: l.mandatory,
19
+ fallbackIsoCode: l.fallback ?? null,
20
+ })
21
+
22
+ const toSchema = (l: LanguageInput): SchemaLanguage => ({
23
+ iso: l.isoCode,
24
+ name: l.name,
25
+ default: l.isDefault,
26
+ mandatory: l.isMandatory,
27
+ ...(l.fallbackIsoCode ? { fallback: l.fallbackIsoCode } : {}),
28
+ })
29
+
30
+ /** The languages as `schema/languages.toml` holds them. */
31
+ export const writeLanguagesFile = (languages: readonly LanguageInput[]): string =>
32
+ writeLanguages(languages.map(toSchema))
33
+
34
+ const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase()
35
+
36
+ /** The localization port, in a workspace: its languages file, else Shared's languages. */
37
+ export function workspaceLocalizationPort(
38
+ db: Db,
39
+ shared: LocalizationPort,
40
+ workspaceId: number,
41
+ ): LocalizationPort {
42
+ const store = new WorkspaceFileRepository(db)
43
+ const own = async () =>
44
+ (await store.files(workspaceId, WORKSPACE_LANGUAGES)).get(WORKSPACE_LANGUAGES)
45
+ const load = async (): Promise<LanguageInput[]> => {
46
+ const text = await own()
47
+ const parsed = text === undefined ? undefined : parseLanguages('languages.toml', text).value
48
+ return parsed ? parsed.map(toInput) : shared.allLanguages()
49
+ }
50
+ const save = async (languages: LanguageInput[]) => {
51
+ const text = writeLanguages(languages.map(toSchema))
52
+ // The workspace took every file Shared had as it opened: without one, Shared had none.
53
+ if ((await own()) !== undefined) await store.write(workspaceId, WORKSPACE_LANGUAGES, text)
54
+ else await store.rebase(workspaceId, WORKSPACE_LANGUAGES, null, text)
55
+ }
56
+ return {
57
+ cultures: () => shared.cultures(),
58
+ allLanguages: load,
59
+ async language(isoCode) {
60
+ return (await load()).find((l) => same(l.isoCode, isoCode))
61
+ },
62
+ async saveLanguage(language) {
63
+ const languages = await load()
64
+ const rest = languages
65
+ .filter((l) => !same(l.isoCode, language.isoCode))
66
+ .map((l) => (language.isDefault ? { ...l, isDefault: false } : l))
67
+ const at = languages.findIndex((l) => same(l.isoCode, language.isoCode))
68
+ const next = [...rest]
69
+ next.splice(at < 0 ? next.length : at, 0, language)
70
+ await save(next)
71
+ },
72
+ async deleteLanguage(isoCode) {
73
+ const languages = await load()
74
+ const found = languages.find((l) => same(l.isoCode, isoCode))
75
+ if (!found) return 'not-found'
76
+ if (found.isDefault) return 'default'
77
+ if (languages.some((l) => l.fallbackIsoCode && same(l.fallbackIsoCode, isoCode)))
78
+ return 'in-use'
79
+ await save(languages.filter((l) => l !== found))
80
+ return 'deleted'
81
+ },
82
+ }
83
+ }
84
+
85
+ /** The languages a workspace holds, where its file reads; undefined where it has none. */
86
+ export async function workspaceLanguages(
87
+ db: Db,
88
+ workspaceId: number,
89
+ ): Promise<LanguageInput[] | undefined> {
90
+ const text = (await new WorkspaceFileRepository(db).files(workspaceId, WORKSPACE_LANGUAGES)).get(
91
+ WORKSPACE_LANGUAGES,
92
+ )
93
+ return text === undefined ? undefined : parseLanguages('languages.toml', text).value?.map(toInput)
94
+ }
@@ -37,6 +37,8 @@ export interface ApprovalsApiContext {
37
37
  /** False on a deployed node with no schema store: a save would not last. */
38
38
  writable: boolean
39
39
  onChanged?: () => Promise<void>
40
+ /** Story 392: `schemaDir` is a workspace's copy, so nothing is synced from it. */
41
+ inWorkspace?: boolean
40
42
  }
41
43
 
42
44
  const json = (body: unknown, status = 200) => Response.json(body, { status })
@@ -50,10 +52,14 @@ export async function syncWorkflowFiles(db: Db, schemaDir: string) {
50
52
  )
51
53
  }
52
54
 
55
+ /** Shared's workflows synced from its files; a workspace's wait for its merge. */
56
+ const syncedFrom = (ctx: ApprovalsApiContext) =>
57
+ ctx.inWorkspace ? Promise.resolve(null) : syncWorkflowFiles(ctx.db, ctx.schemaDir)
58
+
53
59
  /** What saving a workflow would do to the requests in flight under it. */
54
- /** Setting up workflows, which Settings brings (story 322). Deciding on a page is separate. */
60
+ /** Setting up workflows, which Structure brings (story 385). Deciding on a page is separate. */
55
61
  const mayDesignWorkflows = (principal: Principal): boolean =>
56
- principal.isAdmin || hasSection(principal, 'settings')
62
+ principal.isAdmin || hasSection(principal, 'structure')
57
63
 
58
64
  async function changePreview(approvals: Approvals, principal: Principal, workflow: SchemaWorkflow) {
59
65
  const id = workflow.key ?? workflow.alias
@@ -182,9 +188,12 @@ export async function handleApprovalsApi(
182
188
  if (path === 'workflows' && request.method === 'GET') {
183
189
  const loaded = loadSchemaDirectory(ctx.schemaDir)
184
190
  return json({
185
- items: (await readWorkflows(ctx.db)).map((s) => ({
186
- ...s.workflow,
187
- folder: folderIn(loaded.files.get(`workflow:${s.workflow.alias}`)),
191
+ items: (ctx.inWorkspace
192
+ ? (loaded.set.workflows ?? [])
193
+ : (await readWorkflows(ctx.db)).map((s) => s.workflow)
194
+ ).map((workflow) => ({
195
+ ...workflow,
196
+ folder: folderIn(loaded.files.get(`workflow:${workflow.alias}`)),
188
197
  })),
189
198
  folders: foldersIn(ctx.schemaDir, 'workflows'),
190
199
  readOnlyBecause: ctx.writable
@@ -216,7 +225,7 @@ export async function handleApprovalsApi(
216
225
  return json({ ok: false, message: 'There is no such workflow.' }, 404)
217
226
  rmSync(file)
218
227
  await ctx.onChanged?.()
219
- return json({ ok: true, report: await syncWorkflowFiles(ctx.db, ctx.schemaDir) })
228
+ return json({ ok: true, report: await syncedFrom(ctx) })
220
229
  }
221
230
  if (request.method !== 'PUT' && request.method !== 'POST') return undefined
222
231
  const b = await body()
@@ -256,7 +265,7 @@ export async function handleApprovalsApi(
256
265
  if (alias !== workflow.alias && existsSync(file)) rmSync(file)
257
266
  writeFileSync(join(dir, fileNameFor(workflow.alias)), source, 'utf8')
258
267
  await ctx.onChanged?.()
259
- return json({ ok: true, affected, report: await syncWorkflowFiles(ctx.db, ctx.schemaDir) })
268
+ return json({ ok: true, affected, report: await syncedFrom(ctx) })
260
269
  }
261
270
  } catch (error) {
262
271
  if (error instanceof ApprovalRefused)
package/src/bundle-api.ts CHANGED
@@ -116,25 +116,29 @@ export function permissionsFor(method: string, path: string): string[] {
116
116
  .map(([permission]) => permission)
117
117
  }
118
118
 
119
- /** The sections a set of permissions needs, for the API's own section checks. */
120
- const SECTIONS: Readonly<Record<string, string>> = {
121
- content: SECTION_ALIASES.content,
122
- media: SECTION_ALIASES.media,
123
- members: SECTION_ALIASES.members,
124
- users: SECTION_ALIASES.users,
125
- forms: SECTION_ALIASES.forms,
126
- formEntries: SECTION_ALIASES.forms,
127
- dictionary: SECTION_ALIASES.translation,
128
- contentTypes: SECTION_ALIASES.settings,
129
- webhooks: SECTION_ALIASES.settings,
130
- workflows: SECTION_ALIASES.workflow,
131
- redirects: SECTION_ALIASES.content,
119
+ /**
120
+ * The sections a set of permissions needs, for the API's own section checks; the
121
+ * last is the place a person needs to hold it (story 385: pages are Pages', types
122
+ * Structure's).
123
+ */
124
+ const SECTIONS: Readonly<Record<string, readonly string[]>> = {
125
+ content: [SECTION_ALIASES.content, SECTION_ALIASES.pages],
126
+ media: [SECTION_ALIASES.media],
127
+ members: [SECTION_ALIASES.members],
128
+ users: [SECTION_ALIASES.users],
129
+ forms: [SECTION_ALIASES.forms],
130
+ formEntries: [SECTION_ALIASES.forms],
131
+ dictionary: [SECTION_ALIASES.translation],
132
+ contentTypes: [SECTION_ALIASES.structure],
133
+ webhooks: [SECTION_ALIASES.settings],
134
+ workflows: [SECTION_ALIASES.workflow],
135
+ redirects: [SECTION_ALIASES.content, SECTION_ALIASES.pages],
132
136
  }
133
137
 
134
138
  /** The section an area's permission needs, for acting as the person using it (story 325). */
135
139
  export const sectionOfPermission = (permission: string): string | undefined => {
136
140
  const area = permission.split('.')[0] as string
137
- return area === 'identityProviders' ? SECTION_ALIASES.settings : SECTIONS[area]
141
+ return area === 'identityProviders' ? SECTION_ALIASES.settings : SECTIONS[area]?.at(-1)
138
142
  }
139
143
 
140
144
  /** The verbs a set of permissions needs, for the API's own permission checks. */
@@ -169,8 +173,7 @@ export function extensionPrincipal(
169
173
  const set = new Set(granted)
170
174
  const sections = new Set<string>()
171
175
  for (const permission of set) {
172
- const section = SECTIONS[permission.split('.')[0] as string]
173
- if (section) sections.add(section)
176
+ for (const section of SECTIONS[permission.split('.')[0] as string] ?? []) sections.add(section)
174
177
  }
175
178
  const verbs = verbsOf(set)
176
179
  return {
@@ -27,6 +27,7 @@ import {
27
27
  import { ObjectTypes } from '@crombolo/core'
28
28
  import {
29
29
  answersByNode,
30
+ ChangeRepository,
30
31
  type Db,
31
32
  DictionaryRepository,
32
33
  describeRefFailure,
@@ -853,6 +854,8 @@ export async function installBundleContent(
853
854
  result = await db.locks.withLock(Locks.Schema, () =>
854
855
  db.locks.withLock(Locks.ContentTree, () =>
855
856
  db.transaction(async (tx) => {
857
+ // Story 383: replacing a site that holds pages is a change marked as replacing everything.
858
+ const replacing = replace ? await new ChangeRepository(tx).holdsPages() : false
856
859
  const pruned = replace ? await pruneSite(tx, set, replace, config.nodeId) : []
857
860
  if (replace === 'replace-all') await prunePeople(tx, set.people)
858
861
  if (staged?.next.schema) {
@@ -895,6 +898,7 @@ export async function installBundleContent(
895
898
  })
896
899
  if (!imported.runId) throw new InstallRefused([], imported)
897
900
  await recordPruned(tx, imported.runId, pruned)
901
+ if (replacing) await new ChangeRepository(tx).markReplacing(imported.runId)
898
902
  return imported
899
903
  }),
900
904
  ),