@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
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A workspace's site (story 379): its pages as it has them published, rendered
|
|
3
|
+
* with its own views where it changed any, in a process of its own (story 378),
|
|
4
|
+
* from a published cache of its own.
|
|
5
|
+
* Built when first asked for, kept fresh by the workspace's revision (its newest
|
|
6
|
+
* event, its newest waiting change, its changed files), read with every request;
|
|
7
|
+
* nothing goes on the cache instruction stream. At most so many are kept on a
|
|
8
|
+
* node, and one left alone long enough is dropped.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
import type { Db, NodeSchemaState, WorkspaceScope } from '@crombolo/data'
|
|
12
|
+
import {
|
|
13
|
+
PublishedCache,
|
|
14
|
+
type PublishedCacheOptions,
|
|
15
|
+
Renderer,
|
|
16
|
+
type ViewSnapshots,
|
|
17
|
+
} from '@crombolo/render'
|
|
18
|
+
import { createPublishedContentSource } from './adapters/documents.ts'
|
|
19
|
+
import type { CromboloConfig } from './config.ts'
|
|
20
|
+
import type { WorkspaceProcesses } from './workspace-processes.ts'
|
|
21
|
+
import { changedViews, createWorkspaceViews } from './workspace-views.ts'
|
|
22
|
+
|
|
23
|
+
interface Site {
|
|
24
|
+
workspace: WorkspaceScope
|
|
25
|
+
cache: PublishedCache
|
|
26
|
+
shared: Renderer
|
|
27
|
+
revision: string
|
|
28
|
+
usedAt: number
|
|
29
|
+
/** Its renderer in its process, made when it first changed a view. */
|
|
30
|
+
own?: Renderer
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What the workspace holds now, as one string: it moves with every write there. */
|
|
34
|
+
export async function workspaceRevision(db: Db, workspaceId: number): Promise<string> {
|
|
35
|
+
const events = await db.q
|
|
36
|
+
.selectFrom('content_version')
|
|
37
|
+
.select((eb) => eb.fn.max('id').as('n'))
|
|
38
|
+
.where('workspace_id', '=', workspaceId)
|
|
39
|
+
.execute()
|
|
40
|
+
const pending = await db.q
|
|
41
|
+
.selectFrom('workspace_pending')
|
|
42
|
+
.select((eb) => eb.fn.max('id').as('n'))
|
|
43
|
+
.where('workspace_id', '=', workspaceId)
|
|
44
|
+
.execute()
|
|
45
|
+
const files = await db.q
|
|
46
|
+
.selectFrom('workspace_file')
|
|
47
|
+
.select(['path', 'hash'])
|
|
48
|
+
.where('workspace_id', '=', workspaceId)
|
|
49
|
+
.orderBy('path')
|
|
50
|
+
.execute()
|
|
51
|
+
const digest = createHash('sha256')
|
|
52
|
+
for (const f of files) digest.update(`${f.path}:${f.hash ?? ''}\n`)
|
|
53
|
+
return `${events[0]?.n ?? 0}|${pending[0]?.n ?? 0}|${digest.digest('hex').slice(0, 16)}`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createWorkspaceSites(options: {
|
|
57
|
+
db: Db
|
|
58
|
+
nodeState?: NodeSchemaState
|
|
59
|
+
/** The environment whose own values its pages show (story 387). */
|
|
60
|
+
environment?: string
|
|
61
|
+
cacheOptions: Omit<PublishedCacheOptions, 'onInvalidate' | 'onRead'>
|
|
62
|
+
/**
|
|
63
|
+
* Where a workspace that changed its views renders. Without them (a database in
|
|
64
|
+
* memory, which no other process can open) it renders here, as story 377 did.
|
|
65
|
+
*/
|
|
66
|
+
processes?: WorkspaceProcesses
|
|
67
|
+
componentsCacheDir: string
|
|
68
|
+
componentsDir: string
|
|
69
|
+
snapshots?: ViewSnapshots
|
|
70
|
+
renderTimeoutMs?: number
|
|
71
|
+
/** How many a node keeps. */
|
|
72
|
+
limit: number
|
|
73
|
+
/** How long one is kept unused. */
|
|
74
|
+
idleMs: number
|
|
75
|
+
}) {
|
|
76
|
+
const sites = new Map<string, Site>()
|
|
77
|
+
const here = options.processes
|
|
78
|
+
? undefined
|
|
79
|
+
: createWorkspaceViews({
|
|
80
|
+
db: options.db,
|
|
81
|
+
config: {
|
|
82
|
+
componentsCacheDir: options.componentsCacheDir,
|
|
83
|
+
renderTimeoutMs: options.renderTimeoutMs,
|
|
84
|
+
} as CromboloConfig,
|
|
85
|
+
cache: undefined as never,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const sweep = (now: number, keep: string) => {
|
|
89
|
+
for (const [key, site] of sites)
|
|
90
|
+
if (key !== keep && now - site.usedAt > options.idleMs) sites.delete(key)
|
|
91
|
+
while (sites.size > Math.max(1, options.limit)) {
|
|
92
|
+
const oldest = [...sites.entries()]
|
|
93
|
+
.filter(([key]) => key !== keep)
|
|
94
|
+
.sort((a, b) => a[1].usedAt - b[1].usedAt)[0]
|
|
95
|
+
if (!oldest) break
|
|
96
|
+
sites.delete(oldest[0])
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
/** The workspace's cache and renderer, brought up to what it holds. */
|
|
102
|
+
async siteFor(workspace: WorkspaceScope & { name?: string }): Promise<{
|
|
103
|
+
cache: PublishedCache
|
|
104
|
+
renderer: Renderer
|
|
105
|
+
workspace: WorkspaceScope
|
|
106
|
+
views: 'workspace' | 'shared'
|
|
107
|
+
}> {
|
|
108
|
+
const revision = await workspaceRevision(options.db, workspace.id)
|
|
109
|
+
let site = sites.get(workspace.key)
|
|
110
|
+
if (!site) {
|
|
111
|
+
const cache = new PublishedCache(
|
|
112
|
+
createPublishedContentSource(options.db, {
|
|
113
|
+
nodeState: options.nodeState,
|
|
114
|
+
workspace: workspace.id,
|
|
115
|
+
...(options.environment ? { environment: options.environment } : {}),
|
|
116
|
+
}),
|
|
117
|
+
options.cacheOptions,
|
|
118
|
+
)
|
|
119
|
+
site = {
|
|
120
|
+
workspace,
|
|
121
|
+
cache,
|
|
122
|
+
revision,
|
|
123
|
+
usedAt: 0,
|
|
124
|
+
shared: new Renderer({
|
|
125
|
+
cache,
|
|
126
|
+
componentsDir: options.componentsDir,
|
|
127
|
+
snapshots: options.snapshots,
|
|
128
|
+
renderTimeoutMs: options.renderTimeoutMs,
|
|
129
|
+
}),
|
|
130
|
+
}
|
|
131
|
+
sites.set(workspace.key, site)
|
|
132
|
+
} else if (site.revision !== revision) {
|
|
133
|
+
site.cache.invalidate()
|
|
134
|
+
site.revision = revision
|
|
135
|
+
}
|
|
136
|
+
site.usedAt = Date.now()
|
|
137
|
+
sweep(site.usedAt, workspace.key)
|
|
138
|
+
if (!(await changedViews(options.db, workspace.id)))
|
|
139
|
+
return { cache: site.cache, renderer: site.shared, workspace, views: 'shared' }
|
|
140
|
+
if (!options.processes) {
|
|
141
|
+
const own = (await here?.rendererFor(workspace, site.cache)) as Renderer
|
|
142
|
+
return { cache: site.cache, renderer: own, workspace, views: 'workspace' }
|
|
143
|
+
}
|
|
144
|
+
const current = site
|
|
145
|
+
if (!current.own) {
|
|
146
|
+
const name =
|
|
147
|
+
workspace.name ??
|
|
148
|
+
(
|
|
149
|
+
await options.db.q
|
|
150
|
+
.selectFrom('workspace')
|
|
151
|
+
.select('name')
|
|
152
|
+
.where('id', '=', workspace.id)
|
|
153
|
+
.executeTakeFirst()
|
|
154
|
+
)?.name ??
|
|
155
|
+
workspace.key
|
|
156
|
+
current.own = options.processes.rendererFor(
|
|
157
|
+
{ id: workspace.id, key: workspace.key, name },
|
|
158
|
+
current.cache,
|
|
159
|
+
current.shared,
|
|
160
|
+
() => current.revision,
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
return { cache: site.cache, renderer: current.own as Renderer, workspace, views: 'workspace' }
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
/** Forgets a workspace's site: it was merged or discarded. */
|
|
167
|
+
forget(key: string): void {
|
|
168
|
+
sites.delete(key)
|
|
169
|
+
here?.forget(key)
|
|
170
|
+
options.processes?.stop(key)
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
get size(): number {
|
|
174
|
+
return sites.size
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export type WorkspaceSites = ReturnType<typeof createWorkspaceSites>
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rendering with a workspace's views (story 377), inside the workspace's own
|
|
3
|
+
* process (story 378). A workspace that changed a component has a tree of its
|
|
4
|
+
* own: its full copy of the components, written from its file list into a folder
|
|
5
|
+
* of its own, with a snapshot over it, so each save is read fresh at a new path,
|
|
6
|
+
* as Shared's views are. One that changed none renders with Shared's, on the node.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { dirname, join, resolve } from 'node:path'
|
|
10
|
+
import { type Db, fileHash, WorkspaceFileRepository, type WorkspaceScope } from '@crombolo/data'
|
|
11
|
+
import { type PublishedCache, Renderer, ViewSnapshots } from '@crombolo/render'
|
|
12
|
+
import type { CromboloConfig } from './config.ts'
|
|
13
|
+
|
|
14
|
+
const PREFIX = 'components/'
|
|
15
|
+
|
|
16
|
+
interface Tree {
|
|
17
|
+
renderer: Renderer
|
|
18
|
+
snapshots: ViewSnapshots
|
|
19
|
+
root: string
|
|
20
|
+
written: Map<string, string>
|
|
21
|
+
usedAt: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Whether a workspace changed any view: only then does it render in a process of its own. */
|
|
25
|
+
export async function changedViews(db: Db, workspace: number): Promise<boolean> {
|
|
26
|
+
return (await new WorkspaceFileRepository(db).changes(workspace, PREFIX)).length > 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createWorkspaceViews(options: {
|
|
30
|
+
db: Db
|
|
31
|
+
config: CromboloConfig
|
|
32
|
+
cache: PublishedCache
|
|
33
|
+
}) {
|
|
34
|
+
const trees = new Map<string, Tree>()
|
|
35
|
+
const base = resolve(`${options.config.componentsCacheDir}-workspaces`)
|
|
36
|
+
const store = new WorkspaceFileRepository(options.db)
|
|
37
|
+
|
|
38
|
+
const drop = (key: string) => {
|
|
39
|
+
const tree = trees.get(key)
|
|
40
|
+
if (!tree) return
|
|
41
|
+
trees.delete(key)
|
|
42
|
+
rmSync(tree.root, { recursive: true, force: true })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The workspace's renderer, its tree brought up to its files; undefined when it
|
|
47
|
+
* changed no view. `cache` is what it renders from: the workspace's site's own
|
|
48
|
+
* (story 379), or Shared's.
|
|
49
|
+
*/
|
|
50
|
+
async function rendererFor(
|
|
51
|
+
workspace: WorkspaceScope,
|
|
52
|
+
cache: PublishedCache = options.cache,
|
|
53
|
+
): Promise<Renderer | undefined> {
|
|
54
|
+
if ((await store.changes(workspace.id, PREFIX)).length === 0) return undefined
|
|
55
|
+
const files = await store.files(workspace.id, PREFIX)
|
|
56
|
+
let tree = trees.get(workspace.key)
|
|
57
|
+
const created = !tree
|
|
58
|
+
if (!tree) {
|
|
59
|
+
const root = join(base, workspace.key)
|
|
60
|
+
const src = join(root, 'src')
|
|
61
|
+
mkdirSync(src, { recursive: true })
|
|
62
|
+
const snapshots = new ViewSnapshots({ sourceDir: src, cacheDir: join(root, 'gen') })
|
|
63
|
+
tree = {
|
|
64
|
+
root,
|
|
65
|
+
snapshots,
|
|
66
|
+
written: new Map(),
|
|
67
|
+
usedAt: 0,
|
|
68
|
+
renderer: new Renderer({
|
|
69
|
+
cache,
|
|
70
|
+
componentsDir: src,
|
|
71
|
+
snapshots,
|
|
72
|
+
renderTimeoutMs: options.config.renderTimeoutMs,
|
|
73
|
+
}),
|
|
74
|
+
}
|
|
75
|
+
trees.set(workspace.key, tree)
|
|
76
|
+
}
|
|
77
|
+
const src = join(tree.root, 'src')
|
|
78
|
+
let changed = false
|
|
79
|
+
for (const [key, text] of files) {
|
|
80
|
+
const hash = fileHash(text)
|
|
81
|
+
if (tree.written.get(key) === hash) continue
|
|
82
|
+
const target = join(src, ...key.slice(PREFIX.length).split('/'))
|
|
83
|
+
mkdirSync(dirname(target), { recursive: true })
|
|
84
|
+
writeFileSync(target, text)
|
|
85
|
+
tree.written.set(key, hash)
|
|
86
|
+
changed = true
|
|
87
|
+
}
|
|
88
|
+
for (const key of [...tree.written.keys()])
|
|
89
|
+
if (!files.has(key)) {
|
|
90
|
+
rmSync(join(src, ...key.slice(PREFIX.length).split('/')), { force: true })
|
|
91
|
+
tree.written.delete(key)
|
|
92
|
+
changed = true
|
|
93
|
+
}
|
|
94
|
+
if (created) await tree.snapshots.prepare()
|
|
95
|
+
else if (changed) await tree.snapshots.refresh()
|
|
96
|
+
tree.usedAt = Date.now()
|
|
97
|
+
return tree.renderer
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
rendererFor,
|
|
102
|
+
|
|
103
|
+
/** Forgets a workspace's tree: it was merged or discarded. */
|
|
104
|
+
forget(key: string): void {
|
|
105
|
+
drop(key)
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
/** How many trees this process holds now. */
|
|
109
|
+
get size(): number {
|
|
110
|
+
return trees.size
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/** Removes every tree, as the process stops. */
|
|
114
|
+
clear(): void {
|
|
115
|
+
for (const key of [...trees.keys()]) drop(key)
|
|
116
|
+
if (existsSync(base)) rmSync(base, { recursive: true, force: true })
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type WorkspaceViews = ReturnType<typeof createWorkspaceViews>
|