@nuasite/cms 0.43.0-beta.1 → 0.43.0-beta.2
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/dist/editor.js +2221 -2219
- package/package.json +18 -4
- package/src/admin/entry.tsx +35 -0
- package/src/admin/env.d.ts +15 -0
- package/src/admin/tsconfig.json +15 -0
- package/src/admin/tsconfig.tsbuildinfo +1 -0
- package/src/editor/components/collections-browser.tsx +4 -1
- package/src/editor/components/toolbar.tsx +20 -11
- package/src/editor/signals.ts +4 -2
- package/src/index.ts +128 -55
- package/src/local-admin.ts +232 -0
- package/src/mode.ts +53 -0
- package/src/tsconfig.json +6 -1
- package/src/types.ts +7 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local-mode admin server (cms-headless F7).
|
|
3
|
+
*
|
|
4
|
+
* In `local` mode (`pletivo dev` on a developer machine), `@nuasite/cms` gives the
|
|
5
|
+
* same full-page collections admin as the webmaster tab — reusing the exact
|
|
6
|
+
* `@nuasite/collections-admin` SPA — instead of the cramped in-iframe collection
|
|
7
|
+
* form. This module wires two things into Astro's Vite dev server:
|
|
8
|
+
*
|
|
9
|
+
* 1. an **in-process** cms-sidecar (`@nuasite/cms-sidecar` `createServer` over
|
|
10
|
+
* `createCmsCore(createNodeFs(root))`) mounted at `/_nua/cms-admin-api/*`,
|
|
11
|
+
* forwarding to the sidecar's `/cms/v1` contract;
|
|
12
|
+
* 2. the collections-admin SPA served at `/_nua/admin`, with
|
|
13
|
+
* `apiBase = /_nua/cms-admin-api/cms/v1`.
|
|
14
|
+
*
|
|
15
|
+
* Both are **lazy**: nothing is built on `pletivo dev` startup. The sidecar core
|
|
16
|
+
* is created on the first `/_nua/admin` (or API) hit and reused thereafter, so
|
|
17
|
+
* dev startup stays fast.
|
|
18
|
+
*
|
|
19
|
+
* The sidecar runs in-process (not a child `bunx` process) on purpose: the dev
|
|
20
|
+
* server already runs under Bun/Node, the sidecar's `createServer` is a pure
|
|
21
|
+
* Web-standard `fetch` handler, and `createCmsCore(createNodeFs(root))` is the
|
|
22
|
+
* very same brain the legacy `/_nua/cms` dev API already builds here. In-process
|
|
23
|
+
* avoids a port allocation, a `bunx` cold-start, and a network hop on every
|
|
24
|
+
* request — and keeps `pletivo dev` a single self-contained process.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createCmsCore, createNodeFs } from '@nuasite/cms-core'
|
|
28
|
+
import { type CmsSidecarServer, createServer } from '@nuasite/cms-sidecar'
|
|
29
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
30
|
+
import { getProjectRoot } from './config'
|
|
31
|
+
import { readBody } from './handlers/request-utils'
|
|
32
|
+
import type { MediaStorageAdapter } from './media/types'
|
|
33
|
+
|
|
34
|
+
/** Minimal Vite dev-server surface this module needs (kept loose to dodge Vite version skew). */
|
|
35
|
+
export interface AdminViteServerLike {
|
|
36
|
+
middlewares: {
|
|
37
|
+
use: (middleware: (req: IncomingMessage, res: ServerResponse, next: () => void) => void) => void
|
|
38
|
+
}
|
|
39
|
+
transformIndexHtml: (url: string, html: string) => Promise<string>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface LocalAdminOptions {
|
|
43
|
+
/** Content collections directory, relative to the project root. */
|
|
44
|
+
contentDir: string
|
|
45
|
+
/** Component directories, used by the core for MDX import resolution. */
|
|
46
|
+
componentDirs: string[]
|
|
47
|
+
/** Media adapter for the sidecar's `/media` routes (defaults to `local` in this mode). */
|
|
48
|
+
mediaAdapter?: MediaStorageAdapter
|
|
49
|
+
/** Max upload size in bytes for sidecar media uploads. */
|
|
50
|
+
maxUploadSize: number
|
|
51
|
+
/** Virtual-module id of the SPA entry the HTML shell loads (transformed by Vite). */
|
|
52
|
+
entryModuleId: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** URL the collections-admin SPA is served at. */
|
|
56
|
+
export const ADMIN_ROUTE = '/_nua/admin'
|
|
57
|
+
|
|
58
|
+
/** Local mount of the in-process sidecar. The SPA's `apiBase` targets `${API_PREFIX}/cms/v1`. */
|
|
59
|
+
export const ADMIN_API_PREFIX = '/_nua/cms-admin-api'
|
|
60
|
+
|
|
61
|
+
/** `apiBase` passed to the SPA — the sidecar serves its routes under `/cms/v1`. */
|
|
62
|
+
export const ADMIN_API_BASE = `${ADMIN_API_PREFIX}/cms/v1`
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build the HTML shell for the admin SPA. It loads the virtual entry module
|
|
66
|
+
* (which imports the lib's stylesheet and mounts `<CollectionsAdminApp apiBase={…} />`),
|
|
67
|
+
* and injects the resolved `apiBase` as a window global so the entry needs no
|
|
68
|
+
* build config. The host-agnostic SPA is reused verbatim — only `apiBase` differs
|
|
69
|
+
* from the webmaster tab.
|
|
70
|
+
*/
|
|
71
|
+
function adminShellHtml(entryModuleId: string, apiBase: string): string {
|
|
72
|
+
return `<!doctype html>
|
|
73
|
+
<html lang="en">
|
|
74
|
+
<head>
|
|
75
|
+
<meta charset="utf-8" />
|
|
76
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
77
|
+
<title>Collections — Nua CMS</title>
|
|
78
|
+
<style>
|
|
79
|
+
html, body, #nua-admin-root { height: 100%; margin: 0; }
|
|
80
|
+
body { background: #f8fafc; }
|
|
81
|
+
</style>
|
|
82
|
+
<script>window.__NUA_ADMIN_API_BASE__ = ${JSON.stringify(apiBase)};</script>
|
|
83
|
+
</head>
|
|
84
|
+
<body>
|
|
85
|
+
<div id="nua-admin-root"></div>
|
|
86
|
+
<script type="module" src=${JSON.stringify(entryModuleId)}></script>
|
|
87
|
+
</body>
|
|
88
|
+
</html>
|
|
89
|
+
`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Lazily create the in-process cms-sidecar over the project's `node:fs`. The core
|
|
94
|
+
* is built once on first use and reused; the media adapter mirrors the dev
|
|
95
|
+
* server's selection (local `public/uploads` by default in this mode).
|
|
96
|
+
*/
|
|
97
|
+
function makeLazySidecar(options: LocalAdminOptions): () => CmsSidecarServer {
|
|
98
|
+
let server: CmsSidecarServer | null = null
|
|
99
|
+
return () => {
|
|
100
|
+
if (server) return server
|
|
101
|
+
const root = getProjectRoot()
|
|
102
|
+
const fs = createNodeFs(root)
|
|
103
|
+
const core = createCmsCore(fs, {
|
|
104
|
+
contentDir: options.contentDir,
|
|
105
|
+
media: options.mediaAdapter,
|
|
106
|
+
componentDirs: options.componentDirs,
|
|
107
|
+
})
|
|
108
|
+
server = createServer({
|
|
109
|
+
core,
|
|
110
|
+
fs,
|
|
111
|
+
root,
|
|
112
|
+
// In-process: the core ships with the site's @nuasite/cms version, so the
|
|
113
|
+
// package version is not separately meaningful — report it as local.
|
|
114
|
+
coreVersion: 'local',
|
|
115
|
+
contentDir: options.contentDir,
|
|
116
|
+
maxUploadSize: options.maxUploadSize,
|
|
117
|
+
})
|
|
118
|
+
return server
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Build a Web `Request` from a Node `IncomingMessage` (+ already-buffered body). */
|
|
123
|
+
function toWebRequest(req: IncomingMessage, path: string, body: Buffer | undefined): Request {
|
|
124
|
+
const host = req.headers.host ?? 'localhost'
|
|
125
|
+
const url = `http://${host}${path}`
|
|
126
|
+
const headers = new Headers()
|
|
127
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
128
|
+
if (value === undefined) continue
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
for (const v of value) headers.append(key, v)
|
|
131
|
+
} else {
|
|
132
|
+
headers.set(key, value)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const method = req.method ?? 'GET'
|
|
136
|
+
const init: RequestInit = { method, headers }
|
|
137
|
+
if (method !== 'GET' && method !== 'HEAD' && body !== undefined && body.length > 0) {
|
|
138
|
+
// `Buffer` is not directly a DOM `BodyInit`; copy into a standalone
|
|
139
|
+
// `Uint8Array` (a valid `ArrayBufferView` body) over its own `ArrayBuffer`.
|
|
140
|
+
const bytes = new Uint8Array(body.byteLength)
|
|
141
|
+
bytes.set(body)
|
|
142
|
+
init.body = bytes
|
|
143
|
+
}
|
|
144
|
+
return new Request(url, init)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Write a Web `Response` back to a Node `ServerResponse`. */
|
|
148
|
+
async function writeWebResponse(res: ServerResponse, response: Response): Promise<void> {
|
|
149
|
+
res.statusCode = response.status
|
|
150
|
+
response.headers.forEach((value, key) => {
|
|
151
|
+
res.setHeader(key, value)
|
|
152
|
+
})
|
|
153
|
+
const buffer = Buffer.from(await response.arrayBuffer())
|
|
154
|
+
res.end(buffer)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Register the local-mode admin middleware on the Vite dev server. The caller
|
|
159
|
+
* must only invoke this in `local` mode — in `hosted` mode the managed sandbox
|
|
160
|
+
* sidecar + the webmaster tab own these responsibilities, so the plugin never
|
|
161
|
+
* registers this middleware (verified by the no-op test).
|
|
162
|
+
*/
|
|
163
|
+
export function createLocalAdminMiddleware(server: AdminViteServerLike, options: LocalAdminOptions): void {
|
|
164
|
+
const getSidecar = makeLazySidecar(options)
|
|
165
|
+
const shell = adminShellHtml(options.entryModuleId, ADMIN_API_BASE)
|
|
166
|
+
|
|
167
|
+
// 1. In-process sidecar API. Strip the local mount prefix and forward the rest
|
|
168
|
+
// (which begins with `/cms/v1/...`) to the sidecar's Web `fetch` handler.
|
|
169
|
+
server.middlewares.use((req, res, next) => {
|
|
170
|
+
const rawUrl = req.url ?? ''
|
|
171
|
+
if (!rawUrl.startsWith(`${ADMIN_API_PREFIX}/`) && rawUrl !== ADMIN_API_PREFIX) {
|
|
172
|
+
next()
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
const forwardedPath = rawUrl.slice(ADMIN_API_PREFIX.length) || '/'
|
|
176
|
+
|
|
177
|
+
readBody(req, options.maxUploadSize)
|
|
178
|
+
.then(async (body) => {
|
|
179
|
+
const sidecar = getSidecar()
|
|
180
|
+
const request = toWebRequest(req, forwardedPath, body)
|
|
181
|
+
const response = await sidecar.fetch(request)
|
|
182
|
+
await writeWebResponse(res, response)
|
|
183
|
+
})
|
|
184
|
+
.catch((error) => {
|
|
185
|
+
console.error('[nua-cms] /_nua/admin API error:', error)
|
|
186
|
+
if (!res.headersSent) {
|
|
187
|
+
res.statusCode = 500
|
|
188
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
189
|
+
}
|
|
190
|
+
res.end(JSON.stringify({ error: 'Internal server error', code: 'io_error' }))
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// 2. Admin SPA shell at /_nua/admin (and any sub-path under it — the SPA owns
|
|
195
|
+
// its own internal view-state navigation). Health-check the in-process
|
|
196
|
+
// sidecar before serving so a broken project surfaces a clear error rather
|
|
197
|
+
// than a blank SPA. The HTML is run through Vite's transform so the dev
|
|
198
|
+
// client + the virtual entry module resolve and HMR works.
|
|
199
|
+
server.middlewares.use((req, res, next) => {
|
|
200
|
+
const pathname = (req.url ?? '').split('?')[0] ?? ''
|
|
201
|
+
const isAdmin = pathname === ADMIN_ROUTE || pathname.startsWith(`${ADMIN_ROUTE}/`)
|
|
202
|
+
if (!isAdmin || (req.method !== 'GET' && req.method !== 'HEAD')) {
|
|
203
|
+
next()
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const serve = async () => {
|
|
208
|
+
const sidecar = getSidecar()
|
|
209
|
+
const health = await sidecar.fetch(new Request('http://localhost/health'))
|
|
210
|
+
if (!health.ok) {
|
|
211
|
+
res.statusCode = 503
|
|
212
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8')
|
|
213
|
+
res.end('Nua CMS local sidecar is not healthy.')
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
const html = await server.transformIndexHtml(pathname, shell)
|
|
217
|
+
res.statusCode = 200
|
|
218
|
+
res.setHeader('content-type', 'text/html; charset=utf-8')
|
|
219
|
+
res.setHeader('cache-control', 'no-store')
|
|
220
|
+
res.end(html)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
serve().catch((error) => {
|
|
224
|
+
console.error('[nua-cms] /_nua/admin serve error:', error)
|
|
225
|
+
if (!res.headersSent) {
|
|
226
|
+
res.statusCode = 500
|
|
227
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8')
|
|
228
|
+
}
|
|
229
|
+
res.end('Failed to load Nua CMS admin.')
|
|
230
|
+
})
|
|
231
|
+
})
|
|
232
|
+
}
|
package/src/mode.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CMS run mode — `local` vs `hosted` (cms-headless F7).
|
|
3
|
+
*
|
|
4
|
+
* The marker plugin behaves differently by host:
|
|
5
|
+
*
|
|
6
|
+
* - **`local`** (default for `pletivo dev` on a developer machine): the plugin
|
|
7
|
+
* lazily spawns an in-process cms-sidecar over the project's `node:fs` and
|
|
8
|
+
* serves the `@nuasite/collections-admin` SPA at `/_nua/admin`. The inline
|
|
9
|
+
* widget runs in-page as usual.
|
|
10
|
+
* - **`hosted`** (inside the agent sandbox): the sidecar is a managed sandbox
|
|
11
|
+
* service (cms-headless F2) and the collections admin is a webmaster tab (F3),
|
|
12
|
+
* so the plugin must NOT spawn a sidecar and must NOT serve `/_nua/admin`. It
|
|
13
|
+
* stays a pure marker + CDN-inject plugin (F5).
|
|
14
|
+
*
|
|
15
|
+
* Detection is automatic and zero-config for local dev: the agent runtime sets a
|
|
16
|
+
* sandbox environment variable that the `pletivo dev` process inherits, which
|
|
17
|
+
* flips the mode to `hosted`. An explicit `mode` in the plugin config always
|
|
18
|
+
* wins over auto-detection.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export type CmsMode = 'local' | 'hosted'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Environment signals that mean "this `pletivo dev` runs inside the managed
|
|
25
|
+
* agent sandbox" (⇒ `hosted`):
|
|
26
|
+
*
|
|
27
|
+
* - `SANDBOX_ID` — set by the webmaster sandbox deployer when it starts the agent
|
|
28
|
+
* process (the Contember project id); every service the agent spawns, including
|
|
29
|
+
* `pletivo dev`, inherits it. It is never set on a developer machine. This is
|
|
30
|
+
* the canonical "we are in the sandbox" marker used across the agent runtime.
|
|
31
|
+
* - `CMS_SIDECAR_LOCAL_EDVABE` — set (to `'1'`) by the deployer for the local
|
|
32
|
+
* edvabe sandbox variant; the managed sidecar bundle is bind-mounted there.
|
|
33
|
+
* Also implies a managed sidecar, hence `hosted`.
|
|
34
|
+
*/
|
|
35
|
+
const HOSTED_ENV_KEYS: readonly string[] = ['SANDBOX_ID', 'CMS_SIDECAR_LOCAL_EDVABE']
|
|
36
|
+
|
|
37
|
+
/** Whether any hosted-sandbox env signal is present (and non-empty). */
|
|
38
|
+
export function detectHostedFromEnv(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
39
|
+
return HOSTED_ENV_KEYS.some((key) => {
|
|
40
|
+
const value = env[key]
|
|
41
|
+
return typeof value === 'string' && value.trim() !== ''
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the effective CMS mode. An explicit `override` (from the plugin config)
|
|
47
|
+
* always wins; otherwise the mode is auto-detected from the environment, defaulting
|
|
48
|
+
* to `local` when no sandbox signal is present.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveCmsMode(override?: CmsMode, env: NodeJS.ProcessEnv = process.env): CmsMode {
|
|
51
|
+
if (override !== undefined) return override
|
|
52
|
+
return detectHostedFromEnv(env) ? 'hosted' : 'local'
|
|
53
|
+
}
|
package/src/tsconfig.json
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../tsconfig.settings.json",
|
|
3
|
+
"exclude": [
|
|
4
|
+
"admin"
|
|
5
|
+
],
|
|
3
6
|
"compilerOptions": {
|
|
4
7
|
"outDir": "../dist/src",
|
|
5
8
|
"composite": true,
|
|
@@ -22,6 +25,8 @@
|
|
|
22
25
|
},
|
|
23
26
|
"references": [
|
|
24
27
|
{ "path": "../../cms-core/src" },
|
|
25
|
-
{ "path": "../../cms-types/src" }
|
|
28
|
+
{ "path": "../../cms-types/src" },
|
|
29
|
+
{ "path": "../../collections-admin/src" },
|
|
30
|
+
{ "path": "../../cms-sidecar/src" },
|
|
26
31
|
]
|
|
27
32
|
}
|
package/src/types.ts
CHANGED
|
@@ -527,6 +527,13 @@ export type CmsPostMessage =
|
|
|
527
527
|
|
|
528
528
|
export interface CmsFeatures {
|
|
529
529
|
selectElement?: boolean
|
|
530
|
+
/**
|
|
531
|
+
* Controls the in-preview collection browser/management UI (browse collections,
|
|
532
|
+
* list/open entries). Defaults to enabled. When `false`, the widget hides this
|
|
533
|
+
* UI because collection editing is owned elsewhere (e.g. the webmaster
|
|
534
|
+
* Collections tab); inline text/image/color editing stays unaffected.
|
|
535
|
+
*/
|
|
536
|
+
collectionManagement?: boolean
|
|
530
537
|
}
|
|
531
538
|
|
|
532
539
|
// ============================================================================
|