@contentbit/studio 1.0.0-rc.0 → 1.0.0-rc.1
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/README.md +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +34 -7
- package/package.json +4 -4
- package/src/server/index.ts +25 -13
- package/src/server/scan.test.ts +68 -0
package/README.md
CHANGED
|
@@ -50,5 +50,7 @@ contentbit studio <globs...> \
|
|
|
50
50
|
|
|
51
51
|
The CLI binds `127.0.0.1` by default, starts at port `4377` and lets Vite choose
|
|
52
52
|
the next free port, and opens the browser unless `--no-open` is passed.
|
|
53
|
+
`--port 0` picks any free port. The CLI warns when `--host` is not a loopback
|
|
54
|
+
address, because Studio is then reachable from your network.
|
|
53
55
|
Studio loads the generic block definitions by default; pass `--no-generic-blocks`
|
|
54
56
|
when your registry should own the full block set.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,MAAM,CAAA;AAIpE,OAAO,KAAK,EAAE,kBAAkB,EAAiB,MAAM,YAAY,CAAA;AAEnE,YAAY,EACV,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,aAAa,EACb,WAAW,EACX,aAAa,GACd,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEhE,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACtB,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IACrB,IAAI,EAAE,aAAa,CAAA;CACpB;AAED,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC,CAyDpF"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// src/server/index.ts
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
3
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { createServer as createNetServer } from "node:net";
|
|
4
6
|
import { dirname, isAbsolute as isAbsolute2, join as join2, resolve } from "node:path";
|
|
5
7
|
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { createServer
|
|
8
|
+
import { createServer } from "vite";
|
|
7
9
|
|
|
8
10
|
// src/server/scan.ts
|
|
9
11
|
import {
|
|
@@ -18,6 +20,7 @@ import {
|
|
|
18
20
|
import { genericBlocks } from "@contentbit/blocks";
|
|
19
21
|
import { createBlockRegistry, createLinkGraphView, discoverContentProject, scanContentProject, summarizeContentProjectFindings } from "@contentbit/core";
|
|
20
22
|
import { createHash } from "node:crypto";
|
|
23
|
+
import { existsSync } from "node:fs";
|
|
21
24
|
import { readFile } from "node:fs/promises";
|
|
22
25
|
import { isAbsolute, join, relative } from "node:path";
|
|
23
26
|
import { pathToFileURL } from "node:url";
|
|
@@ -51,9 +54,12 @@ async function loadRegistry(registryPath, options = {}) {
|
|
|
51
54
|
if (!registryPath)
|
|
52
55
|
return registry;
|
|
53
56
|
const resolvedPath = isAbsolute(registryPath) ? registryPath : join(options.cwd ?? process.cwd(), registryPath);
|
|
57
|
+
if (!existsSync(resolvedPath)) {
|
|
58
|
+
throw new ProjectLoadError(2, `Registry module not found: ${resolvedPath}`);
|
|
59
|
+
}
|
|
54
60
|
let mod;
|
|
55
61
|
try {
|
|
56
|
-
mod = await
|
|
62
|
+
mod = await importFresh(resolvedPath);
|
|
57
63
|
} catch (err) {
|
|
58
64
|
if (err instanceof Error && "code" in err && err.code === "ERR_UNKNOWN_FILE_EXTENSION") {
|
|
59
65
|
throw new Error(`Importing a TypeScript registry needs Node 22.18+ (native type stripping): ${resolvedPath}`);
|
|
@@ -66,6 +72,14 @@ async function loadRegistry(registryPath, options = {}) {
|
|
|
66
72
|
registry.use(mod.default);
|
|
67
73
|
return registry;
|
|
68
74
|
}
|
|
75
|
+
async function importFresh(path) {
|
|
76
|
+
const url = pathToFileURL(path);
|
|
77
|
+
const source = await readFile(path).catch(() => void 0);
|
|
78
|
+
if (source) {
|
|
79
|
+
url.searchParams.set("v", createHash("sha256").update(source).digest("hex").slice(0, 16));
|
|
80
|
+
}
|
|
81
|
+
return import(url.href);
|
|
82
|
+
}
|
|
69
83
|
async function loadContentProject(input) {
|
|
70
84
|
const files = await resolveContentFiles(input.positionals, input.cmd, {
|
|
71
85
|
cwd: input.cwd,
|
|
@@ -369,9 +383,12 @@ function sendJson(res, status, body) {
|
|
|
369
383
|
async function startStudio(options) {
|
|
370
384
|
const root = findPackageRoot();
|
|
371
385
|
const host = options.host ?? "127.0.0.1";
|
|
372
|
-
const port = options.port ?? 4377;
|
|
386
|
+
const port = options.port === 0 ? await freePort(host) : options.port ?? 4377;
|
|
373
387
|
const cwd = options.cwd ?? process.cwd();
|
|
374
|
-
const
|
|
388
|
+
const fontRoot = dirname(
|
|
389
|
+
createRequire(join2(root, "package.json")).resolve("@fontsource-variable/inter/package.json")
|
|
390
|
+
);
|
|
391
|
+
const fsAllow = [root, fontRoot];
|
|
375
392
|
const apiOptions = {
|
|
376
393
|
globs: options.globs,
|
|
377
394
|
cwd,
|
|
@@ -414,7 +431,7 @@ async function startStudio(options) {
|
|
|
414
431
|
async function loadProjectComponents(vite, options) {
|
|
415
432
|
const merged = {};
|
|
416
433
|
for (const candidate of componentCandidates(options)) {
|
|
417
|
-
if (!
|
|
434
|
+
if (!existsSync2(candidate.path)) continue;
|
|
418
435
|
let mod;
|
|
419
436
|
try {
|
|
420
437
|
mod = await vite.ssrLoadModule(candidate.path);
|
|
@@ -465,10 +482,20 @@ function studioApiPlugin(options) {
|
|
|
465
482
|
}
|
|
466
483
|
};
|
|
467
484
|
}
|
|
485
|
+
async function freePort(host) {
|
|
486
|
+
const server = createNetServer();
|
|
487
|
+
await new Promise((resolve2, reject) => {
|
|
488
|
+
server.once("error", reject);
|
|
489
|
+
server.listen(0, host, resolve2);
|
|
490
|
+
});
|
|
491
|
+
const { port } = server.address();
|
|
492
|
+
await new Promise((resolve2) => server.close(resolve2));
|
|
493
|
+
return port;
|
|
494
|
+
}
|
|
468
495
|
function findPackageRoot() {
|
|
469
496
|
let dir = dirname(fileURLToPath(import.meta.url));
|
|
470
497
|
for (; ; ) {
|
|
471
|
-
if (
|
|
498
|
+
if (existsSync2(join2(dir, "package.json"))) return dir;
|
|
472
499
|
const next = dirname(dir);
|
|
473
500
|
if (next === dir) throw new Error("Could not locate @contentbit/studio package root.");
|
|
474
501
|
dir = next;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentbit/studio",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
4
|
"description": "Local read-only content studio for contentbit projects.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"tinyglobby": "^0.2.10",
|
|
49
49
|
"tw-animate-css": "^1.4.0",
|
|
50
50
|
"vite": "^8",
|
|
51
|
-
"@contentbit/blocks": "1.0.0-rc.
|
|
52
|
-
"@contentbit/core": "1.0.0-rc.
|
|
53
|
-
"@contentbit/react": "1.0.0-rc.
|
|
51
|
+
"@contentbit/blocks": "1.0.0-rc.1",
|
|
52
|
+
"@contentbit/core": "1.0.0-rc.1",
|
|
53
|
+
"@contentbit/react": "1.0.0-rc.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@testing-library/dom": "^10.4.1",
|
package/src/server/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
2
|
import { existsSync } from 'node:fs'
|
|
3
|
+
import { createRequire } from 'node:module'
|
|
4
|
+
import { createServer as createNetServer, type AddressInfo } from 'node:net'
|
|
3
5
|
import { dirname, isAbsolute, join, resolve } from 'node:path'
|
|
4
6
|
import { fileURLToPath } from 'node:url'
|
|
5
|
-
import { createServer,
|
|
7
|
+
import { createServer, type Plugin, type ViteDevServer } from 'vite'
|
|
6
8
|
import type { BlockComponent } from '@contentbit/react'
|
|
7
9
|
|
|
8
10
|
import { handleStudioApiRequest } from './api.js'
|
|
@@ -28,19 +30,18 @@ export interface StudioServer {
|
|
|
28
30
|
export async function startStudio(options: StartStudioOptions): Promise<StudioServer> {
|
|
29
31
|
const root = findPackageRoot()
|
|
30
32
|
const host = options.host ?? '127.0.0.1'
|
|
31
|
-
|
|
33
|
+
// Vite treats port 0 as unset and falls back to 5173.
|
|
34
|
+
const port = options.port === 0 ? await freePort(host) : (options.port ?? 4377)
|
|
32
35
|
const cwd = options.cwd ?? process.cwd()
|
|
33
|
-
// Vite serves
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// (notably with `--host`).
|
|
43
|
-
const fsAllow = [...new Set([root, searchForWorkspaceRoot(root), cwd])]
|
|
36
|
+
// Vite serves `/@fs/` files from `fs.allow` and the modules the UI imports.
|
|
37
|
+
// The UI reads the project only through /api/*, and previews and project
|
|
38
|
+
// components render on the server, so the list holds the Studio package and
|
|
39
|
+
// the font its stylesheet loads by url(). A project or workspace root here
|
|
40
|
+
// would serve every file under it, to the network with --host.
|
|
41
|
+
const fontRoot = dirname(
|
|
42
|
+
createRequire(join(root, 'package.json')).resolve('@fontsource-variable/inter/package.json'),
|
|
43
|
+
)
|
|
44
|
+
const fsAllow = [root, fontRoot]
|
|
44
45
|
const apiOptions: StudioOptions = {
|
|
45
46
|
globs: options.globs,
|
|
46
47
|
cwd,
|
|
@@ -161,6 +162,17 @@ function studioApiPlugin(options: StudioOptions): Plugin {
|
|
|
161
162
|
}
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
async function freePort(host: string): Promise<number> {
|
|
166
|
+
const server = createNetServer()
|
|
167
|
+
await new Promise<void>((resolve, reject) => {
|
|
168
|
+
server.once('error', reject)
|
|
169
|
+
server.listen(0, host, resolve)
|
|
170
|
+
})
|
|
171
|
+
const { port } = server.address() as AddressInfo
|
|
172
|
+
await new Promise((resolve) => server.close(resolve))
|
|
173
|
+
return port
|
|
174
|
+
}
|
|
175
|
+
|
|
164
176
|
function findPackageRoot(): string {
|
|
165
177
|
let dir = dirname(fileURLToPath(import.meta.url))
|
|
166
178
|
for (;;) {
|
package/src/server/scan.test.ts
CHANGED
|
@@ -383,6 +383,74 @@ test('startStudio serves read-only local API responses on port 0', async () => {
|
|
|
383
383
|
expect(write.status).toBe(405)
|
|
384
384
|
})
|
|
385
385
|
|
|
386
|
+
test('startStudio with port 0 listens on an OS-assigned port and reports it', async () => {
|
|
387
|
+
const dir = await fixture({ 'content/a.md': '# Alpha\n\nReadable body.\n' })
|
|
388
|
+
const server = await startStudio({ globs: ['content/*.md'], cwd: dir, port: 0, open: false })
|
|
389
|
+
runningServers.push(server)
|
|
390
|
+
|
|
391
|
+
const port = Number(new URL(server.url).port)
|
|
392
|
+
const project = await fetch(new URL('/api/project', server.url))
|
|
393
|
+
|
|
394
|
+
expect(project.status).toBe(200)
|
|
395
|
+
// Operating systems assign port 0 from their ephemeral range (32768 and up
|
|
396
|
+
// on Linux, 49152 and up on macOS and Windows). Vite's own fallback scans up
|
|
397
|
+
// from 5173, and Studio's default starts at 4377.
|
|
398
|
+
expect(port).toBeGreaterThanOrEqual(32768)
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
test('startStudio serves its UI fonts but not other project files', async () => {
|
|
402
|
+
const dir = await fixture({
|
|
403
|
+
'content/a.md': '# Alpha\n\nReadable body.\n',
|
|
404
|
+
'secrets.json': '{"token":"s3cret"}\n',
|
|
405
|
+
})
|
|
406
|
+
const server = await startStudio({ globs: ['content/*.md'], cwd: dir, port: 0, open: false })
|
|
407
|
+
runningServers.push(server)
|
|
408
|
+
|
|
409
|
+
const html = await (await fetch(server.url)).text()
|
|
410
|
+
const stylesheets = [...html.matchAll(/<link rel="stylesheet" href="([^"]+)"/g)].map((m) => m[1])
|
|
411
|
+
const css = await Promise.all(
|
|
412
|
+
stylesheets.map(async (href) => {
|
|
413
|
+
const response = await fetch(new URL(href, server.url), { headers: { accept: 'text/css' } })
|
|
414
|
+
return response.text()
|
|
415
|
+
}),
|
|
416
|
+
)
|
|
417
|
+
const fonts = css.flatMap((text) =>
|
|
418
|
+
[...text.matchAll(/url\(["']?([^"')]+\.woff2)/g)].map((m) => m[1]),
|
|
419
|
+
)
|
|
420
|
+
const fontStatuses = await Promise.all(
|
|
421
|
+
fonts.map(async (font) => (await fetch(new URL(font, server.url))).status),
|
|
422
|
+
)
|
|
423
|
+
const secret = await fetch(new URL(`/@fs${join(dir, 'secrets.json')}`, server.url))
|
|
424
|
+
|
|
425
|
+
expect(fonts.length).toBeGreaterThan(0)
|
|
426
|
+
expect(new Set(fontStatuses)).toEqual(new Set([200]))
|
|
427
|
+
expect(secret.status).toBe(403)
|
|
428
|
+
expect(await secret.text()).not.toContain('s3cret')
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
test('scanProject validates against the registry as it is on disk now', async () => {
|
|
432
|
+
const dir = await fixture({
|
|
433
|
+
'blocks/registry.mjs': 'export default []\n',
|
|
434
|
+
'content/a.md': '# Alpha\n\n:::shout\nHello there.\n:::\n',
|
|
435
|
+
})
|
|
436
|
+
await linkContentbitCore(dir)
|
|
437
|
+
const errors = async () =>
|
|
438
|
+
(
|
|
439
|
+
await scanProject({ globs: ['content/*.md'], cwd: dir, registryPath: 'blocks/registry.mjs' })
|
|
440
|
+
).findings
|
|
441
|
+
.filter((finding) => finding.severity === 'error')
|
|
442
|
+
.map((finding) => finding.code)
|
|
443
|
+
|
|
444
|
+
expect(await errors()).toEqual(['CB_UNKNOWN_BLOCK'])
|
|
445
|
+
await writeFile(
|
|
446
|
+
join(dir, 'blocks/registry.mjs'),
|
|
447
|
+
`import { defineBlock, markdownBody } from '@contentbit/core'
|
|
448
|
+
export default [defineBlock({ name: 'shout', description: 'Shouts.', content: markdownBody() })]
|
|
449
|
+
`,
|
|
450
|
+
)
|
|
451
|
+
expect(await errors()).toEqual([])
|
|
452
|
+
})
|
|
453
|
+
|
|
386
454
|
test('startStudio renders project block components from the registry folder', async () => {
|
|
387
455
|
const dir = await fixture({
|
|
388
456
|
'blocks/registry.mjs': `import { defineBlock, markdownBody } from '@contentbit/core'
|