@crombolo/server 0.71.0 → 0.73.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.
- package/package.json +14 -14
- package/src/change-transfer.ts +46 -3
- package/src/config.ts +13 -3
- package/src/content-events.ts +55 -4
- package/src/identity-providers.ts +23 -6
- package/src/principal.ts +8 -1
- package/src/remote-changes.ts +40 -1
- package/src/remote-people.ts +11 -1
- package/src/remote.ts +14 -22
- package/src/served-api.ts +121 -0
- package/src/served-schema.ts +231 -0
- package/src/server.ts +168 -5
- package/src/settings.ts +9 -0
- package/src/webhooks.ts +32 -1
- package/src/workspace-processes.ts +82 -8
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What of the site's schema the served copy holds (story 395), from the changes a
|
|
3
|
+
* host reported applying there. Schema is served when the change that last touched
|
|
4
|
+
* it was reported, or came before the oldest one reported. Only what the served
|
|
5
|
+
* copy does not have at all is held back from making content; a page using
|
|
6
|
+
* anything not yet served names the changes it needs.
|
|
7
|
+
*/
|
|
8
|
+
import { normaliseUuid, problemDetails, problemResponse } from '@crombolo/core'
|
|
9
|
+
import {
|
|
10
|
+
ComponentRepository,
|
|
11
|
+
ContentTypeRepository,
|
|
12
|
+
type Db,
|
|
13
|
+
type FileVersion,
|
|
14
|
+
ServedChangeRepository,
|
|
15
|
+
} from '@crombolo/data'
|
|
16
|
+
|
|
17
|
+
const TYPE_FILES = 'schema/content-types/'
|
|
18
|
+
const COMPONENT_FILES = 'components/'
|
|
19
|
+
|
|
20
|
+
/** What a page needs, by the kind a host is told. */
|
|
21
|
+
export type NeededKind = 'documentType' | 'elementType' | 'template'
|
|
22
|
+
|
|
23
|
+
/** One unserved change a page needs, and what for. */
|
|
24
|
+
export interface NeededChange {
|
|
25
|
+
key: string
|
|
26
|
+
name: string | null
|
|
27
|
+
items: Array<{ kind: NeededKind; alias: string }>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Item {
|
|
31
|
+
kind: NeededKind
|
|
32
|
+
key: string
|
|
33
|
+
alias: string
|
|
34
|
+
made: boolean
|
|
35
|
+
changes: Array<{ key: string; name: string | null }>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What the served copy lacks, when a host has reported anything. */
|
|
39
|
+
export interface ServedView {
|
|
40
|
+
/** Types and element types the served copy does not have. */
|
|
41
|
+
hiddenTypes: ReadonlySet<string>
|
|
42
|
+
/** Templates, by component key, the served copy does not have. */
|
|
43
|
+
hiddenTemplates: ReadonlySet<string>
|
|
44
|
+
/** Everything touched since, made or changed, by its key. */
|
|
45
|
+
pending: ReadonlyMap<string, Item>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const firstLine = (text: string, name: string) =>
|
|
49
|
+
new RegExp(`^${name}\\s*=\\s*"([^"]*)"`, 'm').exec(text)?.[1]
|
|
50
|
+
|
|
51
|
+
const cache = new WeakMap<Db, { revision: string; view: ServedView | undefined }>()
|
|
52
|
+
|
|
53
|
+
/** The served copy's view of the schema, or undefined while nothing is reported. */
|
|
54
|
+
export async function servedView(db: Db): Promise<ServedView | undefined> {
|
|
55
|
+
const served = new ServedChangeRepository(db)
|
|
56
|
+
const revision = await served.revision()
|
|
57
|
+
const known = cache.get(db)
|
|
58
|
+
if (known?.revision === revision) return known.view
|
|
59
|
+
const view = await build(db, served)
|
|
60
|
+
cache.set(db, { revision, view })
|
|
61
|
+
return view
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function build(db: Db, served: ServedChangeRepository): Promise<ServedView | undefined> {
|
|
65
|
+
const reported = await served.keys()
|
|
66
|
+
if (reported.size === 0) return undefined
|
|
67
|
+
const since = (await served.since())?.getTime() ?? Number.POSITIVE_INFINITY
|
|
68
|
+
const isServed = (v: FileVersion) =>
|
|
69
|
+
reported.has(normaliseUuid(v.changeKey)) || v.startedAt.getTime() < since
|
|
70
|
+
const byPath = new Map<string, FileVersion[]>()
|
|
71
|
+
for (const v of await served.fileVersions([TYPE_FILES, COMPONENT_FILES]))
|
|
72
|
+
byPath.set(v.path, [...(byPath.get(v.path) ?? []), v])
|
|
73
|
+
const latest = [...byPath.values()].map((vs) => vs[vs.length - 1] as FileVersion)
|
|
74
|
+
const texts = await served.texts(
|
|
75
|
+
latest.flatMap((v) => (v.afterHash && v.path.startsWith(TYPE_FILES) ? [v.afterHash] : [])),
|
|
76
|
+
)
|
|
77
|
+
const typeKeys = new Map(
|
|
78
|
+
[...(await new ContentTypeRepository(db).aliasesByKey())].map(([key, alias]) => [alias, key]),
|
|
79
|
+
)
|
|
80
|
+
const componentKeys = new Map(
|
|
81
|
+
(await new ComponentRepository(db).all()).map((c) => [c.alias, c.key]),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
const hiddenTypes = new Set<string>()
|
|
85
|
+
const hiddenTemplates = new Set<string>()
|
|
86
|
+
const pending = new Map<string, Item>()
|
|
87
|
+
for (const [path, versions] of byPath) {
|
|
88
|
+
const last = versions[versions.length - 1] as FileVersion
|
|
89
|
+
if (last.removed) continue
|
|
90
|
+
const identity = identityOf(path, last, texts, typeKeys, componentKeys)
|
|
91
|
+
if (!identity) continue
|
|
92
|
+
const unserved = versions.filter((v) => !isServed(v))
|
|
93
|
+
if (unserved.length === 0) continue
|
|
94
|
+
const madeAt = [...versions].reverse().find((v) => v.made)
|
|
95
|
+
const made = madeAt !== undefined && !isServed(madeAt)
|
|
96
|
+
const changes = [
|
|
97
|
+
...new Map(unserved.map((v) => [v.changeKey, { key: v.changeKey, name: v.changeName }])),
|
|
98
|
+
].map(([, c]) => c)
|
|
99
|
+
pending.set(identity.key, { ...identity, made, changes })
|
|
100
|
+
if (made) (identity.kind === 'template' ? hiddenTemplates : hiddenTypes).add(identity.key)
|
|
101
|
+
}
|
|
102
|
+
return { hiddenTypes, hiddenTemplates, pending }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function identityOf(
|
|
106
|
+
path: string,
|
|
107
|
+
last: FileVersion,
|
|
108
|
+
texts: ReadonlyMap<string, string>,
|
|
109
|
+
typeKeys: ReadonlyMap<string, string>,
|
|
110
|
+
componentKeys: ReadonlyMap<string, string>,
|
|
111
|
+
): { kind: NeededKind; key: string; alias: string } | undefined {
|
|
112
|
+
if (path.startsWith(TYPE_FILES)) {
|
|
113
|
+
const text = last.afterHash ? texts.get(last.afterHash) : undefined
|
|
114
|
+
const alias = text ? firstLine(text, 'alias') : undefined
|
|
115
|
+
if (!text || !alias) return undefined
|
|
116
|
+
const key = firstLine(text, 'key') ?? typeKeys.get(alias)
|
|
117
|
+
if (!key) return undefined
|
|
118
|
+
const element = /^is-element\s*=\s*true/m.test(text)
|
|
119
|
+
return { kind: element ? 'elementType' : 'documentType', key: normaliseUuid(key), alias }
|
|
120
|
+
}
|
|
121
|
+
const alias = path
|
|
122
|
+
.slice(COMPONENT_FILES.length)
|
|
123
|
+
.replace(/\.[a-z]+$/i, '')
|
|
124
|
+
.split('/')
|
|
125
|
+
.pop()
|
|
126
|
+
const key = alias ? componentKeys.get(alias) : undefined
|
|
127
|
+
return alias && key ? { kind: 'template', key: normaliseUuid(key), alias } : undefined
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const ELEMENT_KEY = /"contentTypeKey"\s*:\s*"([0-9a-f-]{36})"/gi
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The unserved changes a page needs: those that made or last changed its type, its
|
|
134
|
+
* template, or the element types its blocks use. Empty when it needs none.
|
|
135
|
+
*/
|
|
136
|
+
export function neededBy(
|
|
137
|
+
view: ServedView,
|
|
138
|
+
page: { contentTypeKey: string; componentKey: string | null; values: unknown },
|
|
139
|
+
): NeededChange[] {
|
|
140
|
+
const keys = new Set([normaliseUuid(page.contentTypeKey)])
|
|
141
|
+
if (page.componentKey) keys.add(normaliseUuid(page.componentKey))
|
|
142
|
+
for (const m of JSON.stringify(page.values ?? []).matchAll(ELEMENT_KEY))
|
|
143
|
+
keys.add(normaliseUuid(m[1] as string))
|
|
144
|
+
const out = new Map<string, NeededChange>()
|
|
145
|
+
for (const key of keys) {
|
|
146
|
+
const item = view.pending.get(key)
|
|
147
|
+
if (!item) continue
|
|
148
|
+
for (const change of item.changes) {
|
|
149
|
+
const entry = out.get(change.key) ?? { ...change, items: [] }
|
|
150
|
+
if (!entry.items.some((i) => i.kind === item.kind && i.alias === item.alias))
|
|
151
|
+
entry.items.push({ kind: item.kind, alias: item.alias })
|
|
152
|
+
out.set(change.key, entry)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return [...out.values()]
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Operations that change schema or code, refused outside a named workspace once anything is reported. */
|
|
159
|
+
export function isSchemaWrite(operationId: string, method: string): boolean {
|
|
160
|
+
if (method === 'GET') return false
|
|
161
|
+
if (/Folder|AvailableCompositions|QueryExecute/.test(operationId)) return false
|
|
162
|
+
return /^(Post|Put|Delete|Patch)(DocumentType|MediaType|MemberType|DataType|PartialView|Script|Stylesheet|Template)/.test(
|
|
163
|
+
operationId,
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The refusal of a schema change outside a named workspace. */
|
|
168
|
+
export const schemaInWorkspace = (): Response =>
|
|
169
|
+
problemResponse(
|
|
170
|
+
problemDetails({
|
|
171
|
+
title: 'Build this in a workspace',
|
|
172
|
+
status: 409,
|
|
173
|
+
detail:
|
|
174
|
+
'Types, data types, templates, components, stylesheets and scripts go to the live site with a release here: open a workspace to change them.',
|
|
175
|
+
}),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
/** Operations whose answer offers types for a new page or Library item. */
|
|
179
|
+
export const OFFERS_TYPES: ReadonlySet<string> = new Set([
|
|
180
|
+
'GetDocumentTypeAllowedAtRoot',
|
|
181
|
+
'GetDocumentTypeByIdAllowedChildren',
|
|
182
|
+
'GetDocumentTypeAllowedInLibrary',
|
|
183
|
+
])
|
|
184
|
+
|
|
185
|
+
/** The header the back office sends when someone is making content, rather than schema. */
|
|
186
|
+
export const PURPOSE_HEADER = 'crombolo-purpose'
|
|
187
|
+
|
|
188
|
+
type Json = Record<string, unknown>
|
|
189
|
+
|
|
190
|
+
/** An answer with what the served copy lacks taken out of what it offers. */
|
|
191
|
+
export async function withoutUnserved(
|
|
192
|
+
response: Response,
|
|
193
|
+
operationId: string,
|
|
194
|
+
view: ServedView,
|
|
195
|
+
makingContent: boolean,
|
|
196
|
+
): Promise<Response> {
|
|
197
|
+
const types = OFFERS_TYPES.has(operationId)
|
|
198
|
+
const typeDetail = makingContent && operationId === 'GetDocumentTypeById'
|
|
199
|
+
const dataType = makingContent && operationId === 'GetDataTypeById'
|
|
200
|
+
if (!response.ok || (!types && !typeDetail && !dataType)) return response
|
|
201
|
+
const body = (await response.json()) as Json
|
|
202
|
+
const hidden = (id: unknown) => typeof id === 'string' && view.hiddenTypes.has(normaliseUuid(id))
|
|
203
|
+
if (types && Array.isArray(body.items)) {
|
|
204
|
+
const items = (body.items as Json[]).filter((i) => !hidden(i.id))
|
|
205
|
+
body.total = Math.max(
|
|
206
|
+
0,
|
|
207
|
+
Number(body.total ?? items.length) - (body.items.length - items.length),
|
|
208
|
+
)
|
|
209
|
+
body.items = items
|
|
210
|
+
}
|
|
211
|
+
if (typeDetail) {
|
|
212
|
+
const template = (t: unknown) =>
|
|
213
|
+
typeof (t as Json | null)?.id === 'string' &&
|
|
214
|
+
view.hiddenTemplates.has(normaliseUuid((t as Json).id as string))
|
|
215
|
+
if (Array.isArray(body.allowedTemplates))
|
|
216
|
+
body.allowedTemplates = (body.allowedTemplates as unknown[]).filter((t) => !template(t))
|
|
217
|
+
if (template(body.defaultTemplate)) body.defaultTemplate = null
|
|
218
|
+
}
|
|
219
|
+
if (dataType && Array.isArray(body.values))
|
|
220
|
+
body.values = (body.values as Json[]).map((v) =>
|
|
221
|
+
v.alias === 'blocks' && Array.isArray(v.value)
|
|
222
|
+
? {
|
|
223
|
+
...v,
|
|
224
|
+
value: (v.value as Json[]).filter((b) => !hidden(b.contentElementTypeKey)),
|
|
225
|
+
}
|
|
226
|
+
: v,
|
|
227
|
+
)
|
|
228
|
+
const headers = new Headers(response.headers)
|
|
229
|
+
headers.delete('content-length')
|
|
230
|
+
return new Response(JSON.stringify(body), { status: response.status, headers })
|
|
231
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -116,6 +116,7 @@ import {
|
|
|
116
116
|
UserRepository,
|
|
117
117
|
WorkspaceFileRepository,
|
|
118
118
|
WorkspaceLinkRepository,
|
|
119
|
+
WorkspaceProcessRepository,
|
|
119
120
|
WorkspaceRepository,
|
|
120
121
|
type WritesPaused,
|
|
121
122
|
withAuditContext,
|
|
@@ -271,8 +272,17 @@ import {
|
|
|
271
272
|
import { principalOf } from './principal.ts'
|
|
272
273
|
import { provenanceProblem } from './provenance.ts'
|
|
273
274
|
import { createRemote, may, type RestartOutcome } from './remote.ts'
|
|
275
|
+
import { createChangeRoutes, mayApplyChanges, streamBody } from './remote-changes.ts'
|
|
274
276
|
import { createPeopleExchange } from './remote-people.ts'
|
|
275
277
|
import { createSchemaTransfer, UPLOAD_LIMITS } from './schema-transfer.ts'
|
|
278
|
+
import { createServedApi } from './served-api.ts'
|
|
279
|
+
import {
|
|
280
|
+
isSchemaWrite,
|
|
281
|
+
PURPOSE_HEADER,
|
|
282
|
+
schemaInWorkspace,
|
|
283
|
+
servedView,
|
|
284
|
+
withoutUnserved,
|
|
285
|
+
} from './served-schema.ts'
|
|
276
286
|
import { SiteAssets } from './site-assets.ts'
|
|
277
287
|
import { unknownHostProvided } from './site-file.ts'
|
|
278
288
|
import { PublicFiles } from './site-public.ts'
|
|
@@ -369,10 +379,21 @@ import {
|
|
|
369
379
|
workspaceHosts,
|
|
370
380
|
} from './workspace-access.ts'
|
|
371
381
|
import { createWorkspaceChecks } from './workspace-checks.ts'
|
|
372
|
-
import {
|
|
382
|
+
import {
|
|
383
|
+
createWorkspaceProcesses,
|
|
384
|
+
PROCESS_FAILURES_REPORTED,
|
|
385
|
+
type WorkspaceProcesses,
|
|
386
|
+
type WorkspaceProcessReports,
|
|
387
|
+
workspaceProcessList,
|
|
388
|
+
} from './workspace-processes.ts'
|
|
373
389
|
import { createWorkspaceSchema } from './workspace-schema.ts'
|
|
374
390
|
import { createWorkspaceSites } from './workspace-sites.ts'
|
|
375
|
-
import {
|
|
391
|
+
import {
|
|
392
|
+
createWorkspaceScope,
|
|
393
|
+
createWorkspacesApi,
|
|
394
|
+
OWN_WORKSPACE,
|
|
395
|
+
WORKSPACE_HEADER,
|
|
396
|
+
} from './workspaces.ts'
|
|
376
397
|
|
|
377
398
|
export interface ServerHandle {
|
|
378
399
|
config: CromboloConfig
|
|
@@ -1067,7 +1088,7 @@ export async function createServer(
|
|
|
1067
1088
|
? undefined
|
|
1068
1089
|
: 'the person has no access to the back office'
|
|
1069
1090
|
},
|
|
1070
|
-
signIn: async (key, provider, seconds, ip, actor) => {
|
|
1091
|
+
signIn: async (key, provider, seconds, ip, actor, forApplying) => {
|
|
1071
1092
|
const user = await users.byKey(key)
|
|
1072
1093
|
if (!user) return { refused: 'the person no longer exists' }
|
|
1073
1094
|
const result = await auth.signInByExchange(
|
|
@@ -1076,6 +1097,7 @@ export async function createServer(
|
|
|
1076
1097
|
seconds,
|
|
1077
1098
|
ip,
|
|
1078
1099
|
actor,
|
|
1100
|
+
forApplying,
|
|
1079
1101
|
)
|
|
1080
1102
|
if (result.status === 'success') return { accessToken: result.accessToken }
|
|
1081
1103
|
return {
|
|
@@ -1727,7 +1749,21 @@ export async function createServer(
|
|
|
1727
1749
|
},
|
|
1728
1750
|
onServerEvent: announce,
|
|
1729
1751
|
// Story 375: a request in a workspace, with the workspace held while it runs.
|
|
1730
|
-
|
|
1752
|
+
// Story 395: outside a named one, schema is refused and what is not served is not offered.
|
|
1753
|
+
scope: async (request, principal, operation) => {
|
|
1754
|
+
const named = request.headers.get(WORKSPACE_HEADER)?.trim()
|
|
1755
|
+
const view = named && named.toLowerCase() !== OWN_WORKSPACE ? undefined : await servedView(db)
|
|
1756
|
+
if (view && isSchemaWrite(operation.operationId, request.method)) return schemaInWorkspace()
|
|
1757
|
+
const scope = await workspaceScope(request, principal, operation)
|
|
1758
|
+
if (!view || scope instanceof Response) return scope
|
|
1759
|
+
const making = request.headers.get(PURPOSE_HEADER)?.trim().toLowerCase() === 'content'
|
|
1760
|
+
const filtered = async <T>(answer: T): Promise<T> =>
|
|
1761
|
+
answer instanceof Response
|
|
1762
|
+
? ((await withoutUnserved(answer, operation.operationId, view, making)) as T)
|
|
1763
|
+
: answer
|
|
1764
|
+
if (scope) return { run: async (fn) => filtered(await scope.run(fn)) }
|
|
1765
|
+
return request.method === 'GET' ? { run: async (fn) => filtered(await fn()) } : undefined
|
|
1766
|
+
},
|
|
1731
1767
|
})
|
|
1732
1768
|
|
|
1733
1769
|
// Fixed by the contract and the client's SDK, wherever the editor is mounted.
|
|
@@ -1750,6 +1786,36 @@ export async function createServer(
|
|
|
1750
1786
|
}
|
|
1751
1787
|
}
|
|
1752
1788
|
|
|
1789
|
+
// Story 396: how many workspaces' processes keep failing, across the nodes, for /health.
|
|
1790
|
+
const processLog = logger('workspaces')
|
|
1791
|
+
const processRows = new WorkspaceProcessRepository(db)
|
|
1792
|
+
let failingWorkspaces = 0
|
|
1793
|
+
let failingCheckedAt = 0
|
|
1794
|
+
const refreshFailing = async () => {
|
|
1795
|
+
if (Date.now() - failingCheckedAt < 5000) return
|
|
1796
|
+
failingCheckedAt = Date.now()
|
|
1797
|
+
failingWorkspaces = await processRows
|
|
1798
|
+
.failing(PROCESS_FAILURES_REPORTED)
|
|
1799
|
+
.catch(() => failingWorkspaces)
|
|
1800
|
+
}
|
|
1801
|
+
const processReports: WorkspaceProcessReports = {
|
|
1802
|
+
started: (workspace) => {
|
|
1803
|
+
void processRows.started(schema.nodeId, workspace.key).catch(() => {})
|
|
1804
|
+
},
|
|
1805
|
+
stopped: (workspace) => {
|
|
1806
|
+
void processRows.stopped(schema.nodeId, workspace.key).catch(() => {})
|
|
1807
|
+
},
|
|
1808
|
+
failed: (workspace, reason) => {
|
|
1809
|
+
processLog.warning('workspace.process.failed {workspace} on node {nodeId}: {reason}', {
|
|
1810
|
+
workspace: workspace.key,
|
|
1811
|
+
nodeId: schema.nodeId,
|
|
1812
|
+
reason,
|
|
1813
|
+
})
|
|
1814
|
+
failingCheckedAt = 0
|
|
1815
|
+
void processRows.failed(schema.nodeId, workspace.key, reason).catch(() => {})
|
|
1816
|
+
},
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1753
1819
|
const health = () => {
|
|
1754
1820
|
const paused = poller.paused
|
|
1755
1821
|
const readOnly = schema.compatibilityMode || poller.behind || paused !== undefined
|
|
@@ -1778,6 +1844,8 @@ export async function createServer(
|
|
|
1778
1844
|
// Where jobs run, and on a node that leaves them to a worker whether one
|
|
1779
1845
|
// is running them; reported, never drained on (story 223).
|
|
1780
1846
|
...(config.role === 'web' ? {} : { jobs: core.workerWatch.status() }),
|
|
1847
|
+
// Reported, never drained on: one workspace failing is its owner's to fix.
|
|
1848
|
+
workspaces: { failing: failingWorkspaces },
|
|
1781
1849
|
}
|
|
1782
1850
|
}
|
|
1783
1851
|
|
|
@@ -1837,8 +1905,10 @@ export async function createServer(
|
|
|
1837
1905
|
idleMs: config.workspaceProcessIdleMinutes * 60_000,
|
|
1838
1906
|
memoryMb: config.workspaceProcessMemoryMb,
|
|
1839
1907
|
renderMs: config.workspaceRenderSeconds * 1000,
|
|
1908
|
+
reports: processReports,
|
|
1840
1909
|
})
|
|
1841
1910
|
: undefined
|
|
1911
|
+
if (workspaceProcesses) await processRows.nodeStarted(schema.nodeId).catch(() => {})
|
|
1842
1912
|
const workspacesApi = createWorkspacesApi(db, {
|
|
1843
1913
|
enabled: () => workspacesOn(config),
|
|
1844
1914
|
locked: () => contentLocked(config),
|
|
@@ -1897,6 +1967,50 @@ export async function createServer(
|
|
|
1897
1967
|
schema: () => workspaceSchema,
|
|
1898
1968
|
merge: () => core.operations.merge,
|
|
1899
1969
|
})
|
|
1970
|
+
// Story 394: the remote's change routes, on the Management API for a bearer token.
|
|
1971
|
+
const managementChanges = createChangeRoutes({
|
|
1972
|
+
db,
|
|
1973
|
+
changes: () => (workspacesOn(config) ? changeTransfer : undefined),
|
|
1974
|
+
problem: (status, body) =>
|
|
1975
|
+
Response.json(
|
|
1976
|
+
{
|
|
1977
|
+
message: body.title,
|
|
1978
|
+
...(body.detail ? { detail: body.detail } : {}),
|
|
1979
|
+
...(body.items ? { refusals: body.items } : {}),
|
|
1980
|
+
},
|
|
1981
|
+
{ status },
|
|
1982
|
+
),
|
|
1983
|
+
atLeast: () => true,
|
|
1984
|
+
notAtLevel: () => new Response(null, { status: 403 }),
|
|
1985
|
+
may: (principal, verb) =>
|
|
1986
|
+
verb === RemotePermissions.ApplyChanges
|
|
1987
|
+
? mayApplyChanges(principal, config.applyChangesBy, may)
|
|
1988
|
+
: may(principal, verb),
|
|
1989
|
+
receive: (request, path) => streamBody(request, path, config.remote.maxUploadBytes),
|
|
1990
|
+
})
|
|
1991
|
+
/** The remote's route a Management API change request is, if it is one. */
|
|
1992
|
+
const managementChange = (pathname: string, method: string): string | undefined => {
|
|
1993
|
+
if (pathname === changesPath) return method === 'PUT' ? 'changes' : undefined
|
|
1994
|
+
const bundle = /^\/([0-9a-f-]{36})\/bundle$/i.exec(pathname.slice(changesPath.length))
|
|
1995
|
+
if (pathname.startsWith(`${changesPath}/`) && bundle && method === 'GET')
|
|
1996
|
+
return `changes/${bundle[1]}/bundle`
|
|
1997
|
+
const open = /^\/([0-9a-f-]{36})\/change$/i.exec(pathname.slice(workspacesPath.length))
|
|
1998
|
+
if (pathname.startsWith(`${workspacesPath}/`) && open && method === 'GET')
|
|
1999
|
+
return `workspaces/${open[1]}/change`
|
|
2000
|
+
return undefined
|
|
2001
|
+
}
|
|
2002
|
+
// Story 395: the served copy's changes, reported by a host, and its notices for people.
|
|
2003
|
+
const noticesPath = `${paths.pluginPath}/api/notices`
|
|
2004
|
+
const servedApi = createServedApi(db, {
|
|
2005
|
+
mayApply: async (principal) => mayApplyChanges(principal, config.applyChangesBy, may),
|
|
2006
|
+
})
|
|
2007
|
+
/** A token the caller set in `Authorization`, never the back office's cookie. */
|
|
2008
|
+
const carriesBearer = (request: Request) => {
|
|
2009
|
+
const header = request.headers.get('authorization')
|
|
2010
|
+
const bearer = header?.toLowerCase().startsWith('bearer ') ? header.slice(7).trim() : ''
|
|
2011
|
+
return bearer !== '' && bearer !== REDACTED
|
|
2012
|
+
}
|
|
2013
|
+
|
|
1900
2014
|
// Story 379: each workspace's site, kept on this node while it is looked at.
|
|
1901
2015
|
const workspaceSites = createWorkspaceSites({
|
|
1902
2016
|
db,
|
|
@@ -2040,6 +2154,7 @@ export async function createServer(
|
|
|
2040
2154
|
|
|
2041
2155
|
// Non-contract, unauthenticated: a load balancer drains a node that has fallen behind.
|
|
2042
2156
|
if (pathname === '/health') {
|
|
2157
|
+
await refreshFailing()
|
|
2043
2158
|
const status = health()
|
|
2044
2159
|
return Response.json(status, { status: status.ok ? 200 : 503 })
|
|
2045
2160
|
}
|
|
@@ -3370,6 +3485,53 @@ export async function createServer(
|
|
|
3370
3485
|
)
|
|
3371
3486
|
}
|
|
3372
3487
|
|
|
3488
|
+
// Story 395: what the served copy holds, and notices a host leaves for a person.
|
|
3489
|
+
if (pathname === `${changesPath}/served` || pathname === noticesPath) {
|
|
3490
|
+
if (!carriesBearer(request))
|
|
3491
|
+
return Response.json(
|
|
3492
|
+
{ message: 'A host calls this with a sign-in token in Authorization, not a cookie.' },
|
|
3493
|
+
{ status: 401 },
|
|
3494
|
+
)
|
|
3495
|
+
const principal = await authenticate(request)
|
|
3496
|
+
if (!principal) return new Response('Unauthorized', { status: 401 })
|
|
3497
|
+
return pathname === noticesPath
|
|
3498
|
+
? servedApi.notices(request, principal, '')
|
|
3499
|
+
: ((await servedApi.changes(request, principal, '/served')) as Response)
|
|
3500
|
+
}
|
|
3501
|
+
if (pathname === `${noticesPath}/mine`) {
|
|
3502
|
+
const principal = await authenticate(request)
|
|
3503
|
+
if (!principal) return new Response('Unauthorized', { status: 401 })
|
|
3504
|
+
return servedApi.notices(request, principal, '/mine')
|
|
3505
|
+
}
|
|
3506
|
+
|
|
3507
|
+
// Story 394: a change exported or taken, for a token in Authorization alone.
|
|
3508
|
+
const moving = managementChange(pathname, request.method)
|
|
3509
|
+
if (moving) {
|
|
3510
|
+
if (!carriesBearer(request))
|
|
3511
|
+
return Response.json(
|
|
3512
|
+
{ message: 'Moving a change needs a sign-in token in Authorization, not a cookie.' },
|
|
3513
|
+
{ status: 401 },
|
|
3514
|
+
)
|
|
3515
|
+
const principal = await authenticate(request)
|
|
3516
|
+
if (!principal) return new Response('Unauthorized', { status: 401 })
|
|
3517
|
+
return (
|
|
3518
|
+
(await managementChanges.handle(request, new URL(request.url), principal, moving)) ??
|
|
3519
|
+
new Response(null, { status: 404 })
|
|
3520
|
+
)
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
// Story 396: the workspaces whose processes run, or keep failing, across the nodes.
|
|
3524
|
+
if (pathname === `${workspacesPath}/processes` && request.method === 'GET') {
|
|
3525
|
+
const principal = await authenticate(request)
|
|
3526
|
+
if (!principal) return new Response('Unauthorized', { status: 401 })
|
|
3527
|
+
if (!hasSection(principal, 'settings'))
|
|
3528
|
+
return Response.json(
|
|
3529
|
+
{ message: 'Workspace processes are for people who look after the site.' },
|
|
3530
|
+
{ status: 403 },
|
|
3531
|
+
)
|
|
3532
|
+
return Response.json({ items: await workspaceProcessList(db) })
|
|
3533
|
+
}
|
|
3534
|
+
|
|
3373
3535
|
// Story 375: workspaces, opened, listed, shared and discarded.
|
|
3374
3536
|
if (pathname === workspacesPath || pathname.startsWith(`${workspacesPath}/`)) {
|
|
3375
3537
|
const principal = await authenticate(request)
|
|
@@ -5243,7 +5405,8 @@ export async function createServer(
|
|
|
5243
5405
|
// request is held to the usual one here.
|
|
5244
5406
|
if (
|
|
5245
5407
|
listeningBodyLimit(config) > config.maxRequestBodyBytes &&
|
|
5246
|
-
!remote.isUpload(request, requestUrl)
|
|
5408
|
+
!remote.isUpload(request, requestUrl) &&
|
|
5409
|
+
managementChange(pathname, request.method) !== 'changes'
|
|
5247
5410
|
) {
|
|
5248
5411
|
const length = Number(request.headers.get('content-length') ?? 0)
|
|
5249
5412
|
if (length > config.maxRequestBodyBytes)
|
package/src/settings.ts
CHANGED
|
@@ -590,6 +590,15 @@ const DECLARED = [
|
|
|
590
590
|
description:
|
|
591
591
|
'Whether every change to a page is made in a workspace. What everyone sees then changes only when a workspace is merged; someone who edits without choosing one works in their own. Needs `CROMBOLO_WORKSPACES=true`.',
|
|
592
592
|
},
|
|
593
|
+
{
|
|
594
|
+
env: 'CROMBOLO_APPLY_CHANGES_BY',
|
|
595
|
+
kind: 'enum',
|
|
596
|
+
values: ['any', 'exchange'],
|
|
597
|
+
default: 'any',
|
|
598
|
+
section: 'Schema',
|
|
599
|
+
description:
|
|
600
|
+
"Who may stage or apply a change from another copy of the site. `exchange`: only a sign-in exchanged through an identity provider's `applyAudience`, so a person's own sign-in never can.",
|
|
601
|
+
},
|
|
593
602
|
{
|
|
594
603
|
env: 'CROMBOLO_SYNC_SCHEMA_AT_BOOT',
|
|
595
604
|
kind: 'boolean',
|
package/src/webhooks.ts
CHANGED
|
@@ -240,8 +240,37 @@ export interface WebhookFacts extends RaisedWebhookEvent {
|
|
|
240
240
|
via?: string
|
|
241
241
|
/** Its ancestors' keys, nearest first, for Crombolo's payload (story 208). */
|
|
242
242
|
ancestors?: string[]
|
|
243
|
+
/** The change a publish finished (story 388), who made it, and what it needs (story 395). */
|
|
244
|
+
change?: {
|
|
245
|
+
key: string
|
|
246
|
+
name: string | null
|
|
247
|
+
by?: 'person' | 'tool'
|
|
248
|
+
tool?: { id: string; name: string }
|
|
249
|
+
needs?: Array<{
|
|
250
|
+
key: string
|
|
251
|
+
name: string | null
|
|
252
|
+
items: Array<{ kind: string; alias: string }>
|
|
253
|
+
}>
|
|
254
|
+
}
|
|
243
255
|
}
|
|
244
256
|
|
|
257
|
+
/** The change as a payload names it (story 395). Pure. */
|
|
258
|
+
const changeOf = (change: NonNullable<WebhookFacts['change']>) => ({
|
|
259
|
+
Key: change.key,
|
|
260
|
+
Name: change.name,
|
|
261
|
+
By: change.by ?? 'person',
|
|
262
|
+
...(change.tool ? { Tool: { Id: change.tool.id, Name: change.tool.name } } : {}),
|
|
263
|
+
...(change.needs
|
|
264
|
+
? {
|
|
265
|
+
Needs: change.needs.map((n) => ({
|
|
266
|
+
Key: n.key,
|
|
267
|
+
Name: n.name,
|
|
268
|
+
Items: n.items.map((i) => ({ Kind: i.kind, Alias: i.alias })),
|
|
269
|
+
})),
|
|
270
|
+
}
|
|
271
|
+
: {}),
|
|
272
|
+
})
|
|
273
|
+
|
|
245
274
|
/** An event as the site's own handler receives it (story 298). Pure. */
|
|
246
275
|
export function siteEventOf(facts: WebhookFacts): SiteContentEvent {
|
|
247
276
|
return {
|
|
@@ -258,11 +287,13 @@ export function webhookPayload(
|
|
|
258
287
|
facts: WebhookFacts,
|
|
259
288
|
payload: WebhookDefinition['payload'],
|
|
260
289
|
): Record<string, unknown> {
|
|
261
|
-
|
|
290
|
+
const change = facts.change ? { Change: changeOf(facts.change) } : {}
|
|
291
|
+
if (payload !== 'crombolo') return { Id: facts.key, ...change }
|
|
262
292
|
return {
|
|
263
293
|
Id: facts.key,
|
|
264
294
|
ContentType: facts.contentTypeAlias ?? null,
|
|
265
295
|
Ancestors: facts.ancestors ?? [],
|
|
296
|
+
...change,
|
|
266
297
|
}
|
|
267
298
|
}
|
|
268
299
|
|