@crombolo/server 0.69.1 → 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.
- package/package.json +15 -14
- package/src/adapters/blueprints.ts +51 -5
- package/src/adapters/change-recording.ts +140 -0
- package/src/adapters/content.ts +132 -21
- package/src/adapters/documents.ts +26 -5
- package/src/adapters/elements.ts +3 -0
- package/src/adapters/media.ts +7 -1
- package/src/adapters/schema-files.ts +89 -1
- package/src/adapters/workspace-dictionary.ts +287 -0
- package/src/adapters/workspace-file-port.ts +160 -0
- package/src/adapters/workspace-languages.ts +94 -0
- package/src/approvals-api.ts +16 -7
- package/src/bundle-api.ts +19 -16
- package/src/bundle-install.ts +4 -0
- package/src/change-transfer.ts +676 -0
- package/src/changes-api.ts +258 -0
- package/src/component-templates-api.ts +2 -2
- package/src/config.ts +161 -21
- package/src/content-events.ts +65 -1
- package/src/content-operations.ts +22 -3
- package/src/core.ts +2 -0
- package/src/environment-values-api.ts +161 -0
- package/src/node-operations.ts +150 -2
- package/src/ports.ts +174 -52
- package/src/remote-changes.ts +279 -0
- package/src/remote-schema.ts +126 -0
- package/src/remote-workspaces.ts +82 -0
- package/src/remote.ts +289 -192
- package/src/schema-transfer.ts +240 -41
- package/src/schema.ts +7 -2
- package/src/seed/compile.ts +13 -1
- package/src/seed/values.ts +3 -0
- package/src/server.ts +948 -364
- package/src/settings.ts +123 -5
- package/src/site-sections.ts +1 -1
- package/src/workspace-access.ts +152 -0
- package/src/workspace-checks.ts +299 -0
- package/src/workspace-losses.ts +95 -0
- package/src/workspace-processes.ts +306 -0
- package/src/workspace-render-child.ts +168 -0
- package/src/workspace-schema-dir.ts +100 -0
- package/src/workspace-schema.ts +598 -0
- package/src/workspace-sites.ts +179 -0
- package/src/workspace-views.ts +121 -0
- package/src/workspaces.ts +1031 -0
package/src/settings.ts
CHANGED
|
@@ -450,13 +450,131 @@ const DECLARED = [
|
|
|
450
450
|
description: 'Whether the back office may change the schema files.',
|
|
451
451
|
},
|
|
452
452
|
{
|
|
453
|
-
env: '
|
|
453
|
+
env: 'CROMBOLO_CODE',
|
|
454
454
|
kind: 'enum',
|
|
455
|
-
values: ['
|
|
456
|
-
default: '
|
|
455
|
+
values: ['editable', 'locked'],
|
|
456
|
+
default: 'editable',
|
|
457
457
|
section: 'Schema',
|
|
458
458
|
description:
|
|
459
|
-
'
|
|
459
|
+
'Locked, the back office cannot change templates, stylesheets and scripts, for a site whose files come from its repository.',
|
|
460
|
+
},
|
|
461
|
+
{
|
|
462
|
+
env: 'CROMBOLO_ENVIRONMENT',
|
|
463
|
+
kind: 'string',
|
|
464
|
+
default: 'local in development, otherwise production',
|
|
465
|
+
section: 'Schema',
|
|
466
|
+
description:
|
|
467
|
+
'Which copy of the site this is. A property that varies by environment shows this environment’s own value where it has one, and the shared value otherwise.',
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
env: 'CROMBOLO_ENVIRONMENT_VALUES_READONLY',
|
|
471
|
+
kind: 'boolean',
|
|
472
|
+
default: 'false',
|
|
473
|
+
section: 'Schema',
|
|
474
|
+
description:
|
|
475
|
+
'Whether values set for one environment can only arrive here by a change, rather than being set in this copy of the site.',
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
env: 'CROMBOLO_CONTENT',
|
|
479
|
+
kind: 'enum',
|
|
480
|
+
values: ['editable', 'locked'],
|
|
481
|
+
default: 'editable',
|
|
482
|
+
section: 'Schema',
|
|
483
|
+
description:
|
|
484
|
+
'Locked, pages, media, the Library, types, data types and code cannot be changed here: they arrive only by a change applied from the site where they are made. Members, form entries and what visitors send are written as before.',
|
|
485
|
+
},
|
|
486
|
+
{
|
|
487
|
+
env: 'CROMBOLO_CONTENT_EDITED_AT',
|
|
488
|
+
kind: 'string',
|
|
489
|
+
default: '',
|
|
490
|
+
section: 'Schema',
|
|
491
|
+
description:
|
|
492
|
+
"Where a locked site's content is edited: the address of the other site's back office, which an item opened here links to.",
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
env: 'CROMBOLO_WORKSPACES',
|
|
496
|
+
kind: 'boolean',
|
|
497
|
+
default: 'false',
|
|
498
|
+
section: 'Schema',
|
|
499
|
+
description:
|
|
500
|
+
'Whether people may open workspaces, to change pages apart from what everyone else sees and look after them there. Off, every workspace route is refused, and nothing else changes.',
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
env: 'CROMBOLO_PURGE_AFTER_DAYS',
|
|
504
|
+
kind: 'number',
|
|
505
|
+
default: '0',
|
|
506
|
+
section: 'Schema',
|
|
507
|
+
description:
|
|
508
|
+
'The fewest days between removing a property from a type and deleting its values for good. A purge sooner than that is refused.',
|
|
509
|
+
},
|
|
510
|
+
{
|
|
511
|
+
env: 'CROMBOLO_WORKSPACES_HOST',
|
|
512
|
+
kind: 'string',
|
|
513
|
+
section: 'Schema',
|
|
514
|
+
description:
|
|
515
|
+
'Where a workspace is looked at before it is merged: a host with `{workspace}` in it, such as `{workspace}.preview.example.com`, and `{host}` for one of the site’s own hosts. Unset, a workspace is opened in a browser on the site’s own address.',
|
|
516
|
+
},
|
|
517
|
+
{
|
|
518
|
+
env: 'CROMBOLO_WORKSPACE_LINK_MAX_DAYS',
|
|
519
|
+
kind: 'number',
|
|
520
|
+
default: '30',
|
|
521
|
+
section: 'Schema',
|
|
522
|
+
description:
|
|
523
|
+
'The longest a link to a workspace, made for someone to look at it, may last, in days. 0 turns such links off.',
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
env: 'CROMBOLO_WORKSPACE_SITES',
|
|
527
|
+
kind: 'number',
|
|
528
|
+
default: '3',
|
|
529
|
+
section: 'Schema',
|
|
530
|
+
description: 'How many workspaces one server keeps ready to be looked at at once.',
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
env: 'CROMBOLO_WORKSPACE_SITE_IDLE_MINUTES',
|
|
534
|
+
kind: 'number',
|
|
535
|
+
default: '15',
|
|
536
|
+
section: 'Schema',
|
|
537
|
+
description: 'How long a workspace is kept ready after the last time it was looked at.',
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
env: 'CROMBOLO_WORKSPACE_PROCESSES',
|
|
541
|
+
kind: 'number',
|
|
542
|
+
default: '10',
|
|
543
|
+
section: 'Schema',
|
|
544
|
+
description:
|
|
545
|
+
'How many workspaces with changed templates one server shows at once, each kept apart from the site. Past it, the one least recently looked at is stopped, and started again when next asked for.',
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
env: 'CROMBOLO_WORKSPACE_PROCESS_IDLE_MINUTES',
|
|
549
|
+
kind: 'number',
|
|
550
|
+
default: '30',
|
|
551
|
+
section: 'Schema',
|
|
552
|
+
description:
|
|
553
|
+
'How long a workspace with changed templates is kept running after the last time it was looked at.',
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
env: 'CROMBOLO_WORKSPACE_PROCESS_MEMORY_MB',
|
|
557
|
+
kind: 'number',
|
|
558
|
+
default: '1024',
|
|
559
|
+
section: 'Schema',
|
|
560
|
+
description:
|
|
561
|
+
'How much memory a workspace with changed templates may use before it is stopped, to start again when next asked for.',
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
env: 'CROMBOLO_WORKSPACE_RENDER_SECONDS',
|
|
565
|
+
kind: 'number',
|
|
566
|
+
default: '30',
|
|
567
|
+
section: 'Schema',
|
|
568
|
+
description:
|
|
569
|
+
'How long a page of a workspace with changed templates may take to show before it is stopped and the page answers with an error.',
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
env: 'CROMBOLO_WORKSPACES_REQUIRED',
|
|
573
|
+
kind: 'boolean',
|
|
574
|
+
default: 'false',
|
|
575
|
+
section: 'Schema',
|
|
576
|
+
description:
|
|
577
|
+
'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`.',
|
|
460
578
|
},
|
|
461
579
|
{
|
|
462
580
|
env: 'CROMBOLO_SYNC_SCHEMA_AT_BOOT',
|
|
@@ -961,7 +1079,7 @@ const DECLARED = [
|
|
|
961
1079
|
{
|
|
962
1080
|
env: 'CROMBOLO_REMOTE_ENVIRONMENT',
|
|
963
1081
|
kind: 'string',
|
|
964
|
-
default: '
|
|
1082
|
+
default: "the site's environment, CROMBOLO_ENVIRONMENT",
|
|
965
1083
|
section: 'Remote',
|
|
966
1084
|
description:
|
|
967
1085
|
'The name this site goes by for pull and push: lower case letters, digits and dashes.',
|
package/src/site-sections.ts
CHANGED
|
@@ -82,7 +82,7 @@ function schemaEntries(set: SchemaSet, from: SectionPicks): BundleFile[] {
|
|
|
82
82
|
)
|
|
83
83
|
return entries
|
|
84
84
|
|
|
85
|
-
put('schema.toml', writeSchemaVersion(set.version))
|
|
85
|
+
put('schema.toml', writeSchemaVersion(set.version, undefined, set.environments))
|
|
86
86
|
for (const type of contentTypes)
|
|
87
87
|
put(join('content-types', fileNameFor(type.alias)), writeContentType(type))
|
|
88
88
|
for (const type of mediaTypes)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may look at a workspace's site, and which workspace a request is for
|
|
3
|
+
* (story 379). A workspace is named by the host a request arrives on, from the
|
|
4
|
+
* site's pattern (`CROMBOLO_WORKSPACES_HOST`, such as
|
|
5
|
+
* `{workspace}.preview.example.com`, the workspace's key as 32 hex digits), or,
|
|
6
|
+
* where no pattern is set, by a cookie on the site's own host.
|
|
7
|
+
*
|
|
8
|
+
* A member gets in by a single-use handoff: "Open" answers a link carrying a
|
|
9
|
+
* signed token, used once within five minutes and swapped for a host-only
|
|
10
|
+
* cookie that names them, checked again on every request. Anyone else gets in by
|
|
11
|
+
* a link a member made: reusable until it expires or is withdrawn, its secret in
|
|
12
|
+
* the address once (then a cookie), or in `Crombolo-Workspace-Link` on a Delivery
|
|
13
|
+
* API call.
|
|
14
|
+
*/
|
|
15
|
+
import { createHash, createHmac, timingSafeEqual } from 'node:crypto'
|
|
16
|
+
|
|
17
|
+
export const WORKSPACE_HANDOFF_PARAM = 'crombolo-workspace'
|
|
18
|
+
export const WORKSPACE_LINK_PARAM = 'crombolo-workspace-link'
|
|
19
|
+
export const WORKSPACE_LINK_HEADER = 'crombolo-workspace-link'
|
|
20
|
+
export const WORKSPACE_COOKIE = 'crombolo_workspace'
|
|
21
|
+
export const WORKSPACE_HANDOFF_ISSUER = 'crombolo:workspace-handoff'
|
|
22
|
+
export const WORKSPACE_HANDOFF_SECONDS = 5 * 60
|
|
23
|
+
/** How long a member's cookie lasts before they open the workspace again. */
|
|
24
|
+
export const WORKSPACE_COOKIE_SECONDS = 8 * 60 * 60
|
|
25
|
+
|
|
26
|
+
/** What the cookie carries: a member, or a link. */
|
|
27
|
+
export type WorkspaceAccess =
|
|
28
|
+
| { workspace: string; user: number; stamp: string; expires: number }
|
|
29
|
+
| { workspace: string; link: number; expires: number }
|
|
30
|
+
|
|
31
|
+
const hmac = (key: string, text: string) =>
|
|
32
|
+
createHmac('sha256', key).update(text).digest('base64url')
|
|
33
|
+
|
|
34
|
+
const keyFor = (secret: string, purpose: 'handoff' | 'cookie') =>
|
|
35
|
+
hmac(secret, `crombolo/workspace-${purpose}`)
|
|
36
|
+
|
|
37
|
+
function sign(secret: string, purpose: 'handoff' | 'cookie', claims: object): string {
|
|
38
|
+
const payload = Buffer.from(JSON.stringify(claims)).toString('base64url')
|
|
39
|
+
return `${payload}.${hmac(keyFor(secret, purpose), payload)}`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function read(
|
|
43
|
+
secret: string,
|
|
44
|
+
purpose: 'handoff' | 'cookie',
|
|
45
|
+
token: string,
|
|
46
|
+
now: number,
|
|
47
|
+
): Record<string, unknown> | undefined {
|
|
48
|
+
const [payload, signature, ...rest] = token.split('.')
|
|
49
|
+
if (!payload || !signature || rest.length > 0) return undefined
|
|
50
|
+
const expected = Buffer.from(hmac(keyFor(secret, purpose), payload))
|
|
51
|
+
const given = Buffer.from(signature)
|
|
52
|
+
if (expected.length !== given.length || !timingSafeEqual(expected, given)) return undefined
|
|
53
|
+
try {
|
|
54
|
+
const claims = JSON.parse(Buffer.from(payload, 'base64url').toString()) as Record<
|
|
55
|
+
string,
|
|
56
|
+
unknown
|
|
57
|
+
>
|
|
58
|
+
if (typeof claims.workspace !== 'string' || typeof claims.expires !== 'number') return undefined
|
|
59
|
+
return claims.expires > now ? claims : undefined
|
|
60
|
+
} catch {
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A member's handoff: their user id and stamp, the workspace, and a single-use id. */
|
|
66
|
+
export function signHandoff(
|
|
67
|
+
secret: string,
|
|
68
|
+
claims: { workspace: string; user: number; stamp: string; id: string; expires: number },
|
|
69
|
+
): string {
|
|
70
|
+
return sign(secret, 'handoff', claims)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function readHandoff(secret: string, token: string, now = Date.now()) {
|
|
74
|
+
const claims = read(secret, 'handoff', token, now)
|
|
75
|
+
return claims &&
|
|
76
|
+
typeof claims.user === 'number' &&
|
|
77
|
+
typeof claims.stamp === 'string' &&
|
|
78
|
+
typeof claims.id === 'string'
|
|
79
|
+
? (claims as { workspace: string; user: number; stamp: string; id: string; expires: number })
|
|
80
|
+
: undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function signAccess(secret: string, access: WorkspaceAccess): string {
|
|
84
|
+
return sign(secret, 'cookie', access)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function readAccess(secret: string, value: string, now = Date.now()) {
|
|
88
|
+
const claims = read(secret, 'cookie', value, now)
|
|
89
|
+
if (!claims) return undefined
|
|
90
|
+
if (typeof claims.user === 'number' && typeof claims.stamp === 'string')
|
|
91
|
+
return claims as unknown as WorkspaceAccess
|
|
92
|
+
if (typeof claims.link === 'number') return claims as unknown as WorkspaceAccess
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The `Set-Cookie` value for access, host-only, or for clearing it. */
|
|
97
|
+
export function accessCookie(value: string | null, secure: boolean): string {
|
|
98
|
+
const age = value === null ? 0 : WORKSPACE_COOKIE_SECONDS
|
|
99
|
+
return `${WORKSPACE_COOKIE}=${value ?? ''}; Path=/; Max-Age=${age}; HttpOnly; SameSite=Lax${secure ? '; Secure' : ''}`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A link's secret as it is stored: its SHA-256, so the table never holds one that opens anything. */
|
|
103
|
+
export const linkSecretHash = (secret: string): string =>
|
|
104
|
+
createHash('sha256').update(secret).digest('hex')
|
|
105
|
+
|
|
106
|
+
/** A workspace key as a host label: its 32 hex digits. */
|
|
107
|
+
export const hostLabel = (key: string): string => key.replaceAll('-', '').toLowerCase()
|
|
108
|
+
|
|
109
|
+
/** The key back from its label. */
|
|
110
|
+
export function keyFromLabel(label: string): string | undefined {
|
|
111
|
+
if (!/^[0-9a-f]{32}$/i.test(label)) return undefined
|
|
112
|
+
const l = label.toLowerCase()
|
|
113
|
+
return `${l.slice(0, 8)}-${l.slice(8, 12)}-${l.slice(12, 16)}-${l.slice(16, 20)}-${l.slice(20)}`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The site's host pattern as a matcher: the workspace's label from a host, and a
|
|
118
|
+
* host for a workspace. `{host}` stands for a site host the pattern lays the
|
|
119
|
+
* workspace over, so a site with several hostnames keeps each.
|
|
120
|
+
*/
|
|
121
|
+
export function workspaceHosts(pattern: string | undefined) {
|
|
122
|
+
if (!pattern) return undefined
|
|
123
|
+
// Each token a group, everything else as written.
|
|
124
|
+
let source = '^'
|
|
125
|
+
const order: Array<'workspace' | 'host'> = []
|
|
126
|
+
for (const part of pattern.split(/(\{workspace\}|\{host\})/)) {
|
|
127
|
+
if (part === '{workspace}') {
|
|
128
|
+
source += '([0-9a-f]{32})'
|
|
129
|
+
order.push('workspace')
|
|
130
|
+
} else if (part === '{host}') {
|
|
131
|
+
source += '(.+)'
|
|
132
|
+
order.push('host')
|
|
133
|
+
} else source += part.replace(/[.*+?^$()|[\]\\{}]/g, '\\$&')
|
|
134
|
+
}
|
|
135
|
+
const matcher = new RegExp(`${source}$`, 'i')
|
|
136
|
+
return {
|
|
137
|
+
/** The workspace a request's host names, and the site host under it, if any. */
|
|
138
|
+
parse(host: string): { key: string; host: string | undefined } | undefined {
|
|
139
|
+
const bare = host.replace(/:\d+$/, '')
|
|
140
|
+
const match = matcher.exec(bare)
|
|
141
|
+
if (!match) return undefined
|
|
142
|
+
const at = (name: 'workspace' | 'host') => match[order.indexOf(name) + 1]
|
|
143
|
+
const key = keyFromLabel(at('workspace') ?? '')
|
|
144
|
+
if (!key) return undefined
|
|
145
|
+
return { key, host: pattern.includes('{host}') ? at('host') : undefined }
|
|
146
|
+
},
|
|
147
|
+
/** The host a workspace is served at, over one of the site's hosts. */
|
|
148
|
+
hostFor(key: string, siteHost?: string): string {
|
|
149
|
+
return pattern.replace('{workspace}', hostLabel(key)).replace('{host}', siteHost ?? '')
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a command line, or an agent, asks of a workspace before merging it (story
|
|
3
|
+
* 386): how it differs from Shared or from where it began, by property for pages and
|
|
4
|
+
* types and by line for files; each conflict with the other side's change, who made it
|
|
5
|
+
* and what it is called; and the checks: each affected page rendered, every file's
|
|
6
|
+
* problems, what cannot be known until the merge. docs/workspaces.md.
|
|
7
|
+
*/
|
|
8
|
+
import { normaliseUuid } from '@crombolo/core'
|
|
9
|
+
import {
|
|
10
|
+
ChangeRepository,
|
|
11
|
+
type Db,
|
|
12
|
+
UserRepository,
|
|
13
|
+
type Workspace,
|
|
14
|
+
WorkspaceFileRepository,
|
|
15
|
+
WorkspaceRepository,
|
|
16
|
+
} from '@crombolo/data'
|
|
17
|
+
import type { WorkspaceMerging } from './node-operations.ts'
|
|
18
|
+
import type { WorkspaceSchema } from './workspace-schema.ts'
|
|
19
|
+
|
|
20
|
+
export interface WorkspaceRendered {
|
|
21
|
+
status: string
|
|
22
|
+
html?: string
|
|
23
|
+
error?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WorkspaceCheckOptions {
|
|
27
|
+
db: Db
|
|
28
|
+
merge: () => WorkspaceMerging | undefined
|
|
29
|
+
schema: () => WorkspaceSchema | undefined
|
|
30
|
+
/** One page of the workspace's site, by its address. */
|
|
31
|
+
render: (workspace: Workspace, path: string) => Promise<WorkspaceRendered>
|
|
32
|
+
/** A page's address on the workspace's site. */
|
|
33
|
+
urlOf: (workspace: Workspace, key: string) => Promise<string | undefined>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** One affected page, as it rendered. */
|
|
37
|
+
export interface PageResult {
|
|
38
|
+
key: string
|
|
39
|
+
name: string
|
|
40
|
+
url: string | null
|
|
41
|
+
status: string
|
|
42
|
+
ok: boolean
|
|
43
|
+
ms: number
|
|
44
|
+
error?: string
|
|
45
|
+
/** Why it was looked at: it changed, or its type or template did. */
|
|
46
|
+
because: 'changed' | 'type' | 'template'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What a workspace's process logged, newest last, per workspace on this node. */
|
|
50
|
+
const ERRORS_KEPT = 200
|
|
51
|
+
|
|
52
|
+
export function createWorkspaceChecks(options: WorkspaceCheckOptions) {
|
|
53
|
+
const { db } = options
|
|
54
|
+
const errors = new Map<string, Array<{ at: string; page: string; says: string }>>()
|
|
55
|
+
|
|
56
|
+
const note = (workspace: Workspace, page: string, says: string) => {
|
|
57
|
+
const list = errors.get(workspace.key) ?? []
|
|
58
|
+
list.push({ at: new Date().toISOString(), page, says })
|
|
59
|
+
if (list.length > ERRORS_KEPT) list.splice(0, list.length - ERRORS_KEPT)
|
|
60
|
+
errors.set(workspace.key, list)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function renderOne(workspace: Workspace, path: string) {
|
|
64
|
+
const started = performance.now()
|
|
65
|
+
let result: WorkspaceRendered
|
|
66
|
+
try {
|
|
67
|
+
result = await options.render(workspace, path)
|
|
68
|
+
} catch (error) {
|
|
69
|
+
result = { status: 'error', error: String(error) }
|
|
70
|
+
}
|
|
71
|
+
const ms = Math.round(performance.now() - started)
|
|
72
|
+
const ok = result.status === 'ok' || result.status === 'redirect' || result.status === 'outcome'
|
|
73
|
+
if (!ok) note(workspace, path, result.error ?? `answered ${result.status}`)
|
|
74
|
+
return { ...result, ok, ms }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The pages a workspace affects: those it changed, and a sample of each changed type's and template's. */
|
|
78
|
+
async function affected(workspace: Workspace, all: boolean) {
|
|
79
|
+
const out = new Map<string, { key: string; name: string; because: PageResult['because'] }>()
|
|
80
|
+
for (const change of await new WorkspaceRepository(db).changes(workspace.id))
|
|
81
|
+
if (change.kind === 'changed' || change.kind === 'made')
|
|
82
|
+
out.set(change.key, { key: change.key, name: change.name, because: 'changed' })
|
|
83
|
+
const files = await new WorkspaceFileRepository(db).changes(workspace.id)
|
|
84
|
+
// A type by the alias its file says; a template by its file's name, as the site names it.
|
|
85
|
+
const types = files
|
|
86
|
+
.filter((f) => f.path.startsWith('schema/content-types/'))
|
|
87
|
+
.map((f) => /^alias\s*=\s*"([^"]+)"/m.exec(f.now ?? f.base ?? '')?.[1])
|
|
88
|
+
.filter((a): a is string => Boolean(a))
|
|
89
|
+
const templates = files
|
|
90
|
+
.map((f) => /^components\/([^/]+)\.tsx?$/.exec(f.path)?.[1])
|
|
91
|
+
.filter((a): a is string => Boolean(a))
|
|
92
|
+
const sample = all ? 100_000 : 3
|
|
93
|
+
const add = (
|
|
94
|
+
rows: Array<{ unique_id: string; text: string | null }>,
|
|
95
|
+
because: 'type' | 'template',
|
|
96
|
+
) => {
|
|
97
|
+
for (const row of rows) {
|
|
98
|
+
const key = normaliseUuid(String(row.unique_id))
|
|
99
|
+
if (!out.has(key)) out.set(key, { key, name: String(row.text ?? key), because })
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
for (const alias of types)
|
|
103
|
+
add(
|
|
104
|
+
await db.q
|
|
105
|
+
.selectFrom('node as n')
|
|
106
|
+
.innerJoin('content as c', 'c.node_id', 'n.id')
|
|
107
|
+
.innerJoin('content_type as t', 't.node_id', 'c.content_type_id')
|
|
108
|
+
.select(['n.unique_id', 'n.text'])
|
|
109
|
+
.where('n.workspace_id', 'is', null)
|
|
110
|
+
.where('t.alias', '=', alias)
|
|
111
|
+
.limit(sample)
|
|
112
|
+
.execute(),
|
|
113
|
+
'type',
|
|
114
|
+
)
|
|
115
|
+
for (const alias of templates)
|
|
116
|
+
add(
|
|
117
|
+
await db.q
|
|
118
|
+
.selectFrom('node as n')
|
|
119
|
+
.innerJoin('content_version as cv', 'cv.node_id', 'n.id')
|
|
120
|
+
.innerJoin('document_version as dv', 'dv.id', 'cv.id')
|
|
121
|
+
.innerJoin('template as tp', 'tp.node_id', 'dv.template_id')
|
|
122
|
+
.select(['n.unique_id', 'n.text'])
|
|
123
|
+
.where('n.workspace_id', 'is', null)
|
|
124
|
+
.where('cv.current', '=', db.dialect.boolValue(true) as boolean)
|
|
125
|
+
.where('tp.alias', '=', alias)
|
|
126
|
+
.limit(sample)
|
|
127
|
+
.execute(),
|
|
128
|
+
'template',
|
|
129
|
+
)
|
|
130
|
+
return [...out.values()]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Each affected page rendered on the workspace's site. */
|
|
134
|
+
async function pages(workspace: Workspace, all = false): Promise<PageResult[]> {
|
|
135
|
+
const results: PageResult[] = []
|
|
136
|
+
for (const page of await affected(workspace, all)) {
|
|
137
|
+
const url = (await options.urlOf(workspace, page.key)) ?? null
|
|
138
|
+
if (!url) {
|
|
139
|
+
results.push({ ...page, url: null, status: 'unpublished', ok: true, ms: 0 })
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
const rendered = await renderOne(workspace, url)
|
|
143
|
+
results.push({
|
|
144
|
+
...page,
|
|
145
|
+
url,
|
|
146
|
+
status: rendered.status,
|
|
147
|
+
ok: rendered.ok,
|
|
148
|
+
ms: rendered.ms,
|
|
149
|
+
...(rendered.error ? { error: rendered.error } : {}),
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
return results
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The person and name of the change that last touched an item in Shared. */
|
|
156
|
+
async function lastChangeOf(item: { node?: string; path?: string }) {
|
|
157
|
+
const changes = new ChangeRepository(db)
|
|
158
|
+
let changeId: string | undefined
|
|
159
|
+
if (item.path) changeId = (await changes.versions(item.path)).at(-1)?.changeId
|
|
160
|
+
if (!changeId && item.node) {
|
|
161
|
+
const rows = await db.q
|
|
162
|
+
.selectFrom('content_transfer_change as c')
|
|
163
|
+
.innerJoin('content_transfer_run as r', 'r.id', 'c.run_id')
|
|
164
|
+
.select('r.id')
|
|
165
|
+
.where('c.node_key', '=', normaliseUuid(item.node))
|
|
166
|
+
.where('r.status', '=', 'applied')
|
|
167
|
+
.orderBy('c.event_id', 'desc')
|
|
168
|
+
.limit(1)
|
|
169
|
+
.execute()
|
|
170
|
+
changeId = rows[0] ? String(rows[0].id) : undefined
|
|
171
|
+
}
|
|
172
|
+
const change = changeId ? await changes.byId(changeId) : undefined
|
|
173
|
+
if (!change) return null
|
|
174
|
+
const by = change.madeBy ? await new UserRepository(db).byKey(change.madeBy) : undefined
|
|
175
|
+
return {
|
|
176
|
+
id: change.id,
|
|
177
|
+
name: change.name,
|
|
178
|
+
kind: change.kind,
|
|
179
|
+
by: by?.name ?? null,
|
|
180
|
+
at: change.startedAt.toISOString(),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
pages,
|
|
186
|
+
|
|
187
|
+
/** Every check: the pages rendered, the files' problems, and what waits for the merge. */
|
|
188
|
+
async check(workspace: Workspace, all = false) {
|
|
189
|
+
const rendered = await pages(workspace, all)
|
|
190
|
+
const plan = await options.schema()?.plan(workspace.id)
|
|
191
|
+
const problems = (plan?.problems ?? []).map((says) => ({ kind: 'file', says }))
|
|
192
|
+
const failures = [
|
|
193
|
+
...rendered
|
|
194
|
+
.filter((p) => !p.ok)
|
|
195
|
+
.map((p) => ({
|
|
196
|
+
kind: 'page',
|
|
197
|
+
item: p.url ?? p.key,
|
|
198
|
+
says: p.error ?? `It answered ${p.status}.`,
|
|
199
|
+
})),
|
|
200
|
+
...problems.map((p) => ({ kind: p.kind, item: '', says: String(p.says) })),
|
|
201
|
+
]
|
|
202
|
+
const siteFiles = Object.keys(plan?.writes ?? {}).filter((path) => path.startsWith('site/'))
|
|
203
|
+
return {
|
|
204
|
+
ok: failures.length === 0,
|
|
205
|
+
pages: rendered,
|
|
206
|
+
failures,
|
|
207
|
+
// Not known until it is merged: so a clean result is not read as more than it is.
|
|
208
|
+
notChecked: [
|
|
209
|
+
...siteFiles.map((path) => `${path.slice('site/'.length)} takes effect at the merge`),
|
|
210
|
+
'dependencies and routes the site reads as it starts',
|
|
211
|
+
],
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
/** What the workspace's renders on this node said went wrong. */
|
|
216
|
+
errors(workspace: Workspace, since?: Date) {
|
|
217
|
+
return (errors.get(workspace.key) ?? []).filter((e) => !since || new Date(e.at) >= since)
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* How the workspace differs: `shared`, what a merge would change; `base`, what it
|
|
222
|
+
* changed since it opened. Pages by property, files by their texts.
|
|
223
|
+
*/
|
|
224
|
+
async diff(workspace: Workspace, against: 'shared' | 'base') {
|
|
225
|
+
const merging = options.merge()
|
|
226
|
+
const plan = merging ? await merging.preview(workspace) : undefined
|
|
227
|
+
const files = await new WorkspaceFileRepository(db).changes(workspace.id)
|
|
228
|
+
const filePlan = await options.schema()?.plan(workspace.id)
|
|
229
|
+
const pagesOut = (plan?.pages ?? []).map((p) => ({
|
|
230
|
+
key: p.page.key,
|
|
231
|
+
name: p.name,
|
|
232
|
+
fields: p.page.fields
|
|
233
|
+
.filter((f) =>
|
|
234
|
+
against === 'shared'
|
|
235
|
+
? JSON.stringify(f.mine ?? null) !== JSON.stringify(f.theirs ?? null)
|
|
236
|
+
: JSON.stringify(f.mine ?? null) !== JSON.stringify(f.base ?? null),
|
|
237
|
+
)
|
|
238
|
+
.map((f) => ({
|
|
239
|
+
field: f.field,
|
|
240
|
+
base: f.base ?? null,
|
|
241
|
+
mine: f.mine ?? null,
|
|
242
|
+
theirs: f.theirs ?? null,
|
|
243
|
+
})),
|
|
244
|
+
}))
|
|
245
|
+
const shared = (filePlan?.writes ?? {}) as Record<string, string | null>
|
|
246
|
+
const filesOut = files.map((f) => ({
|
|
247
|
+
path: f.path,
|
|
248
|
+
base: f.base,
|
|
249
|
+
mine: f.now,
|
|
250
|
+
...(against === 'shared' ? { merged: shared[f.path] ?? null } : {}),
|
|
251
|
+
}))
|
|
252
|
+
return {
|
|
253
|
+
against,
|
|
254
|
+
pages: pagesOut.filter((p) => p.fields.length > 0),
|
|
255
|
+
pending: (plan?.pending ?? []).map((p) => ({
|
|
256
|
+
key: p.pending.key,
|
|
257
|
+
name: p.pending.name,
|
|
258
|
+
kind: p.pending.kind,
|
|
259
|
+
})),
|
|
260
|
+
files: filesOut,
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
/** Each conflict, by part, with base, mine and theirs, and the other side's change. */
|
|
265
|
+
async conflicts(workspace: Workspace) {
|
|
266
|
+
const merging = options.merge()
|
|
267
|
+
const plan = merging ? await merging.preview(workspace) : undefined
|
|
268
|
+
const filePlan = await options.schema()?.plan(workspace.id)
|
|
269
|
+
const out = []
|
|
270
|
+
for (const c of plan?.conflicts ?? []) {
|
|
271
|
+
const page = plan?.pages.find((p) => p.page.key === c.key)
|
|
272
|
+
const field = page?.page.fields.find((f) => f.field === c.field)
|
|
273
|
+
out.push({
|
|
274
|
+
kind: 'page',
|
|
275
|
+
item: `${c.key}#${c.field}`,
|
|
276
|
+
name: page?.name ?? c.key,
|
|
277
|
+
base: field?.base ?? null,
|
|
278
|
+
mine: field?.mine ?? null,
|
|
279
|
+
theirs: field?.theirs ?? null,
|
|
280
|
+
other: await lastChangeOf({ node: c.key }),
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
for (const f of filePlan?.conflicts ?? []) {
|
|
284
|
+
out.push({
|
|
285
|
+
kind: 'file',
|
|
286
|
+
item: f.path,
|
|
287
|
+
name: f.path,
|
|
288
|
+
base: f.base,
|
|
289
|
+
mine: f.mine,
|
|
290
|
+
theirs: f.theirs,
|
|
291
|
+
other: await lastChangeOf({ path: f.path }),
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
return out
|
|
295
|
+
},
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export type WorkspaceChecks = ReturnType<typeof createWorkspaceChecks>
|