@metacensus/library-research 0.12.0 → 0.13.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/README.md +21 -23
- package/dist/commands/dev.js +11 -12
- package/dist/commands/init.js +14 -9
- package/dist/data.d.ts +1 -0
- package/dist/data.js +2 -2
- package/dist/dev/host.d.ts +2 -1
- package/dist/dev/host.js +22 -15
- package/dist/host.d.ts +10 -6
- package/dist/host.js +15 -7
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/mount.d.ts +24 -10
- package/dist/mount.js +21 -7
- package/dist/playwright.js +1 -1
- package/dist/react.d.ts +13 -2
- package/dist/react.js +13 -2
- package/dist/reader.d.ts +4 -0
- package/dist/reader.js +2 -0
- package/dist/router.d.ts +4 -3
- package/dist/router.js +7 -8
- package/dist/schema.d.ts +1 -0
- package/dist/schema.js +4 -0
- package/dist/testing.d.ts +9 -5
- package/dist/testing.js +11 -9
- package/dist/types.d.ts +14 -1
- package/dist/vite-config.js +1 -0
- package/dist/vite.d.ts +10 -1
- package/dist/vite.js +29 -5
- package/package.json +1 -1
- package/schema/research.schema.json +13 -0
- package/src/virtual.d.ts +2 -2
package/README.md
CHANGED
|
@@ -12,8 +12,8 @@ corpus.json sha → paper
|
|
|
12
12
|
papers/<sha>.<kind> through LFS
|
|
13
13
|
ui/package.json depends on @metacensus/library-research, which brings @playwright/test; scripts dev, check and test
|
|
14
14
|
ui/tsconfig.json extends @metacensus/library-research/tsconfig.json
|
|
15
|
-
ui/index.tsx default export: (parent
|
|
16
|
-
ui/local/index.tsx optional; the same export for the local-only routes, never shipped; declares no Register
|
|
15
|
+
ui/index.tsx default export: defineProject('<path>', (parent) => the project's TanStack routes)
|
|
16
|
+
ui/local/index.tsx optional; the same export, at the same path, for the local-only routes, never shipped; declares no Register
|
|
17
17
|
ui/*.test.tsx optional; unit tests on testHost from @metacensus/library-research/testing
|
|
18
18
|
ui/playwright.config.ts with browser/; export { default } from '@metacensus/library-research/playwright'
|
|
19
19
|
ui/browser/*.spec.ts optional; test and expect from @metacensus/library-research/playwright
|
|
@@ -21,13 +21,13 @@ ui/browser/*.spec.ts optional; test and expect from @metacensus/library-rese
|
|
|
21
21
|
|
|
22
22
|
From `ui/`:
|
|
23
23
|
|
|
24
|
-
| Command | Does
|
|
25
|
-
| --------------------------------------------------------------------- |
|
|
26
|
-
| `npm run dev -- [--reader NAME] [--port N]` | serves the
|
|
27
|
-
| `npm run check` | gates the project, one line per failure, as a host's build does
|
|
28
|
-
| `npx playwright test` | runs `browser/` in Chromium on a dev server; a CSP or axe violation fails its spec unless `violations` lists it
|
|
29
|
-
| `npx library-research add <file> [--title T] [--citation C] [--open]` | adds a paper to `papers/` and `corpus.json`
|
|
30
|
-
| `npx library-research candidate` | runs `check`, then tags a clean `HEAD` on `origin/main` as `<id>-YYYY-MM-DD[.N]`, pushes it, and prints the service's `make pin` line
|
|
24
|
+
| Command | Does |
|
|
25
|
+
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
26
|
+
| `npm run dev -- [--reader NAME] [--port N]` | serves the project at `/<path>/` and `ui/local/`'s at `/local/<path>/`, live from the files, as reader `NAME` (else `local`) to a context that names none, under the host's CSP |
|
|
27
|
+
| `npm run check` | gates the project, one line per failure, as a host's build does |
|
|
28
|
+
| `npx playwright test` | runs `browser/` in Chromium on a dev server; a CSP or axe violation fails its spec unless `violations` lists it |
|
|
29
|
+
| `npx library-research add <file> [--title T] [--citation C] [--open]` | adds a paper to `papers/` and `corpus.json` |
|
|
30
|
+
| `npx library-research candidate` | runs `check`, then tags a clean `HEAD` on `origin/main` as `<id>-YYYY-MM-DD[.N]`, pushes it, and prints the service's `make pin` line |
|
|
31
31
|
|
|
32
32
|
## Rendering
|
|
33
33
|
|
|
@@ -43,32 +43,30 @@ Each key of research.json's `data` is a role, and `ui/roles.ts` exports `roles`,
|
|
|
43
43
|
|
|
44
44
|
`usePrivate(role, parse)` returns `{ page: parsed }` for the pages the host names for the person the project is viewed as: none, one or several, and a page not yet built is left out. `Object.values(usePrivate('ask', parse))` is that person's questions. The role's schema in `ui/roles.ts` parses one page's file, and `load` returns the role as `{ page: parsed }`. A host's `private(role)` returns the page-to-URL map.
|
|
45
45
|
|
|
46
|
-
In `dev`, a reader's
|
|
46
|
+
In `dev` and `testHost`, `research.json`'s optional `people` maps a reader to their pages, as a project's `people` does in production's people.json: `{ "people": { "dan.hertz": ["hertz"] } }`. `dev --reader dan.hertz` holds `hertz`'s page and another page answers 403; a reader `people` omits holds the page named as them, so `dev --reader hertz` holds `hertz`. `testHost`'s `private: { role: { page: json } }` gives the reader their pages, and its `people` option takes `research.people`. Production ignores `people`.
|
|
47
47
|
|
|
48
48
|
Node loads `ui/roles.ts` by stripping its types: no `enum` or namespace, relative imports end in `.ts`, and `ui/package.json` says `"type": "module"`. Dev tooling reads a project's roles through `load` from `@metacensus/library-research/roles`, which takes the folder holding research.json and returns each role parsed, with `corpus`.
|
|
49
49
|
|
|
50
50
|
## Routes
|
|
51
51
|
|
|
52
|
-
A project exports its
|
|
52
|
+
A project default-exports `defineProject('<path>', routes)`: its first URL segment, matching `^[a-z0-9][a-z0-9-]*$` and not `projects`, `local` or `pdfjs` (dev and the host reserve them), and its routes created against the `parent` it is handed, a route at that path; the host owns the router. `Register` types the project's own links. A link names its path in full and passes `from="/<path>"`, so a wrong path fails to compile:
|
|
53
53
|
|
|
54
54
|
```tsx
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
const dpyd = defineProject('dpyd', (parent) => [
|
|
56
|
+
createRoute({ getParentRoute: () => parent, path: 'task/$key' }),
|
|
57
|
+
])
|
|
58
|
+
export default dpyd
|
|
59
59
|
declare module '@tanstack/react-router' {
|
|
60
60
|
interface Register {
|
|
61
|
-
router: RouterOf<typeof
|
|
61
|
+
router: RouterOf<typeof dpyd>
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
// <Link from="
|
|
64
|
+
// <Link from="/dpyd" to="/dpyd/task/$key" params={{ key }} />
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
A host mounts
|
|
68
|
-
|
|
69
|
-
The route also takes `frame(useHost)`, spread into its options: it provides the `Host` that `useHost` returns, shows a resettable error and an announced loading state. A host sets no `defaultPendingComponent` or `defaultErrorComponent` on its router, which would override the frame's for a project's routes. A host serves `@metacensus/library-research/csp.json` as its Content-Security-Policy (directive → sources, adding its own origins and nonces) and `pdfAssets`, `/pdfjs/`, for `configurePdf({ assets })`.
|
|
67
|
+
A host mounts each project with `mount(parent, project, options)` from `@metacensus/library-research/host`: it creates the route at the path the project names, with `options` as the route's options, adds the project's routes, and returns it. Two projects at one path are refused by TanStack's duplicate-route-id check, in development builds only.
|
|
70
68
|
|
|
71
|
-
`
|
|
69
|
+
`options` takes `frame(useHost)`, spread in: it provides the `Host` that `useHost` returns, shows a resettable error and an announced loading state. A host sets no `defaultPendingComponent` or `defaultErrorComponent` on its router, which would override the frame's for a project's routes. A host serves `@metacensus/library-research/csp.json` as its Content-Security-Policy (directive → sources, adding its own origins and nonces) and `pdfAssets`, `/pdfjs/`, for `configurePdf({ assets })`.
|
|
72
70
|
|
|
73
71
|
## Saving
|
|
74
72
|
|
|
@@ -85,11 +83,11 @@ A project saves JSON documents through its host, as it reads data.
|
|
|
85
83
|
|
|
86
84
|
## Testing
|
|
87
85
|
|
|
88
|
-
`testHost(
|
|
86
|
+
`testHost(project, { data, private, documents, reader, url, sessionLost })` from `@metacensus/library-research/testing` returns `{ element, router, documents }`: the project in the frame and mount a real host uses, at its path, `data` giving each role's JSON, `reader` the username (default `test`), `documents` the project's saved documents as `{ id: { body, writer? } }`, `url` the path below the project (default `/`). Render `element` with any DOM renderer; `router.state.location` says where the project navigated, and `documents()` what it saved. `init` scaffolds `ui/index.test.tsx` on vitest, jsdom and Testing Library, which a project installs itself.
|
|
89
87
|
|
|
90
88
|
```tsx
|
|
91
89
|
render(
|
|
92
|
-
testHost(
|
|
90
|
+
testHost(dpyd, { data: { items }, reader: 'ana', url: '/task/7' }).element,
|
|
93
91
|
)
|
|
94
92
|
```
|
|
95
93
|
|
package/dist/commands/dev.js
CHANGED
|
@@ -9,7 +9,7 @@ import { documentStore } from '../document-store.js';
|
|
|
9
9
|
import { paperPath } from '../papers.js';
|
|
10
10
|
import { project, read, readCorpus } from '../data.js';
|
|
11
11
|
import { pdfAssets } from '../pdf.js';
|
|
12
|
-
import { readerCookie } from '../reader.js';
|
|
12
|
+
import { pagesOf, readerCookie } from '../reader.js';
|
|
13
13
|
import { findRoot } from '../root.js';
|
|
14
14
|
import { kinds } from '../schema.js';
|
|
15
15
|
import { hostConfig } from '../vite-config.js';
|
|
@@ -58,20 +58,20 @@ export default async function dev(args) {
|
|
|
58
58
|
const root = findRoot(process.cwd());
|
|
59
59
|
const local = existsSync(join(root, 'ui/local/index.tsx'));
|
|
60
60
|
const server = await createServer(mergeConfig(hostConfig(root), {
|
|
61
|
-
plugins: [devHost(root, values.reader
|
|
61
|
+
plugins: [devHost(root, values.reader)],
|
|
62
62
|
server: {
|
|
63
63
|
port: values.port === undefined ? undefined : Number(values.port),
|
|
64
64
|
},
|
|
65
65
|
}));
|
|
66
66
|
await server.listen();
|
|
67
67
|
const [url] = server.resolvedUrls.local;
|
|
68
|
-
console.log(
|
|
68
|
+
console.log(url);
|
|
69
69
|
if (local)
|
|
70
70
|
console.log(`${url}local/`);
|
|
71
71
|
}
|
|
72
|
-
function devHost(root, reader
|
|
73
|
-
const mount = local ? /^\/(project|local)\// : /^\/project\//;
|
|
72
|
+
function devHost(root, reader) {
|
|
74
73
|
const real = realpathSync(root);
|
|
74
|
+
const { people } = project(root);
|
|
75
75
|
const store = documentStore();
|
|
76
76
|
return {
|
|
77
77
|
name: 'research:dev',
|
|
@@ -81,17 +81,18 @@ function devHost(root, reader, local) {
|
|
|
81
81
|
resolveId: (id) => ([entry, css].includes(id) ? '\0' + id : undefined),
|
|
82
82
|
load(id) {
|
|
83
83
|
if (id === '\0' + css)
|
|
84
|
-
return '@tailwind base;\n@tailwind components;\n@tailwind utilities;\n';
|
|
84
|
+
return '@layer reset, mc;\n@layer reset {\n @tailwind base;\n}\n@tailwind components;\n@tailwind utilities;\n';
|
|
85
85
|
if (id === '\0' + entry)
|
|
86
86
|
return [
|
|
87
|
+
// First, so `reset` is ordered below library-ui's `mc`, as the host orders them.
|
|
88
|
+
`import '${css}'`,
|
|
87
89
|
`import '@metacensus/library-ui/fonts.css'`,
|
|
88
90
|
`import '@metacensus/library-ui/tokens.css'`,
|
|
89
91
|
`import '@metacensus/library-ui/styles.css'`,
|
|
90
|
-
`import '${css}'`,
|
|
91
92
|
`import { configurePdf } from '@metacensus/library-ui/pdf'`,
|
|
92
93
|
`import { start } from ${JSON.stringify(host)}`,
|
|
93
94
|
`configurePdf({ assets: ${JSON.stringify(pdfAssets)} })`,
|
|
94
|
-
`start(${JSON.stringify(reader)})`,
|
|
95
|
+
`start(${JSON.stringify(reader)}, ${JSON.stringify(people)})`,
|
|
95
96
|
].join('\n');
|
|
96
97
|
},
|
|
97
98
|
configureServer(server) {
|
|
@@ -112,8 +113,6 @@ function devHost(root, reader, local) {
|
|
|
112
113
|
server.middlewares.use((req, res, next) => {
|
|
113
114
|
res.setHeader('Content-Security-Policy', policy);
|
|
114
115
|
const path = req.url?.split('?')[0];
|
|
115
|
-
if (path === '/')
|
|
116
|
-
return res.writeHead(302, { Location: '/project/' }).end();
|
|
117
116
|
// A cookie with an empty value is a signed-out reader; the raw value is
|
|
118
117
|
// compared, as an encoded name is never empty.
|
|
119
118
|
const named = cookie(req, readerCookie);
|
|
@@ -141,7 +140,7 @@ function devHost(root, reader, local) {
|
|
|
141
140
|
catch {
|
|
142
141
|
return notFound(res, `${page}: not a page`);
|
|
143
142
|
}
|
|
144
|
-
if (who
|
|
143
|
+
if (!pagesOf(people, who).includes(wanted))
|
|
145
144
|
return reply(res, 403, { 'Content-Type': 'text/plain' }, 'not yours');
|
|
146
145
|
const file = privateFile(root, wanted, role);
|
|
147
146
|
if (!file)
|
|
@@ -174,7 +173,7 @@ function devHost(root, reader, local) {
|
|
|
174
173
|
});
|
|
175
174
|
return () => server.middlewares.use((req, res, next) => {
|
|
176
175
|
const url = req.originalUrl ?? '';
|
|
177
|
-
if (req.url !== '/index.html' ||
|
|
176
|
+
if (req.url !== '/index.html' || url.startsWith('/projects/'))
|
|
178
177
|
return next();
|
|
179
178
|
server
|
|
180
179
|
.transformIndexHtml(url, page)
|
package/dist/commands/init.js
CHANGED
|
@@ -9,6 +9,11 @@ export default function init(args) {
|
|
|
9
9
|
const title = basename(root);
|
|
10
10
|
if (!/^[\w.-]+$/.test(title))
|
|
11
11
|
throw new Error(`${title}: not a repository name; use letters, digits, ., - or _`);
|
|
12
|
+
// The project's URL path: the title, lower-cased, in letters, digits and hyphens.
|
|
13
|
+
const path = title
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
16
|
+
.replace(/^-|-$/g, '') || 'project';
|
|
12
17
|
const files = {
|
|
13
18
|
'research.json': json({ title, data: {} }),
|
|
14
19
|
'corpus.json': '{}\n',
|
|
@@ -76,11 +81,11 @@ jobs:
|
|
|
76
81
|
'ui/tsconfig.json': json({
|
|
77
82
|
extends: '@metacensus/library-research/tsconfig.json',
|
|
78
83
|
}),
|
|
79
|
-
'ui/index.tsx': `import
|
|
84
|
+
'ui/index.tsx': `import { defineProject, type RouterOf } from '@metacensus/library-research'
|
|
80
85
|
import { createRoute } from '@tanstack/react-router'
|
|
81
86
|
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
const project = defineProject('${path}', (parent) => [
|
|
88
|
+
createRoute({
|
|
84
89
|
getParentRoute: () => parent,
|
|
85
90
|
path: '/',
|
|
86
91
|
component: () => (
|
|
@@ -88,13 +93,13 @@ export default function routes(parent: Mount) {
|
|
|
88
93
|
<h1>${title}</h1>
|
|
89
94
|
</main>
|
|
90
95
|
),
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
}),
|
|
97
|
+
])
|
|
98
|
+
export default project
|
|
94
99
|
|
|
95
100
|
declare module '@tanstack/react-router' {
|
|
96
101
|
interface Register {
|
|
97
|
-
router: RouterOf<typeof
|
|
102
|
+
router: RouterOf<typeof project>
|
|
98
103
|
}
|
|
99
104
|
}
|
|
100
105
|
`,
|
|
@@ -102,12 +107,12 @@ declare module '@tanstack/react-router' {
|
|
|
102
107
|
import { testHost } from '@metacensus/library-research/testing'
|
|
103
108
|
import { cleanup, render, screen } from '@testing-library/react'
|
|
104
109
|
import { afterEach, test } from 'vitest'
|
|
105
|
-
import
|
|
110
|
+
import project from './index.tsx'
|
|
106
111
|
|
|
107
112
|
afterEach(cleanup)
|
|
108
113
|
|
|
109
114
|
test('the root renders', async () => {
|
|
110
|
-
render(testHost(
|
|
115
|
+
render(testHost(project).element)
|
|
111
116
|
await screen.findByRole('heading', { level: 1 })
|
|
112
117
|
})
|
|
113
118
|
`,
|
package/dist/data.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare function project(dir: string): {
|
|
|
4
4
|
title: string;
|
|
5
5
|
roles: Record<string, () => unknown>;
|
|
6
6
|
pages: Record<string, () => Record<string, string>>;
|
|
7
|
+
people: Record<string, string[]> | undefined;
|
|
7
8
|
};
|
|
8
9
|
/** The files a private role's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
9
10
|
export declare function privatePages(dir: string, role: string, glob: string): Record<string, string>;
|
package/dist/data.js
CHANGED
|
@@ -3,7 +3,7 @@ import { join, sep } from 'node:path';
|
|
|
3
3
|
import { Corpus, Research } from './schema.js';
|
|
4
4
|
/** Reads a project's research.json; each role reads its files when called. */
|
|
5
5
|
export function project(dir) {
|
|
6
|
-
const { title, data, private: held = {}, } = read(dir, 'research.json', Research);
|
|
6
|
+
const { title, data, private: held = {}, people, } = read(dir, 'research.json', Research);
|
|
7
7
|
const roles = {
|
|
8
8
|
corpus: () => readCorpus(dir),
|
|
9
9
|
};
|
|
@@ -12,7 +12,7 @@ export function project(dir) {
|
|
|
12
12
|
const pages = {};
|
|
13
13
|
for (const [role, glob] of Object.entries(held))
|
|
14
14
|
pages[role] = () => privatePages(dir, role, glob);
|
|
15
|
-
return { title, roles, pages };
|
|
15
|
+
return { title, roles, pages, people };
|
|
16
16
|
}
|
|
17
17
|
/** The files a private role's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
18
18
|
export function privatePages(dir, role, glob) {
|
package/dist/dev/host.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { type People } from '../reader.js';
|
|
2
|
+
export declare function start(username: string, people?: People): Promise<void>;
|
package/dist/dev/host.js
CHANGED
|
@@ -5,30 +5,28 @@ import { RouterProvider } from '@tanstack/react-router';
|
|
|
5
5
|
import { createContext, useContext, useMemo, useState } from 'react';
|
|
6
6
|
import { createRoot } from 'react-dom/client';
|
|
7
7
|
import projects from 'virtual:research-projects';
|
|
8
|
-
import { readerCookie } from '../reader.js';
|
|
8
|
+
import { pagesOf, readerCookie } from '../reader.js';
|
|
9
9
|
import { projectRouter } from '../router.js';
|
|
10
10
|
const project = projects.project;
|
|
11
|
-
const load = window.location.pathname.startsWith('/local/')
|
|
12
|
-
? project.local
|
|
13
|
-
: project.root;
|
|
14
11
|
const Reader = createContext({ username: '', lost: () => { } });
|
|
15
12
|
function useHost() {
|
|
16
|
-
const { username, lost } = useContext(Reader);
|
|
13
|
+
const { username, people, lost } = useContext(Reader);
|
|
17
14
|
return useMemo(() => ({
|
|
18
15
|
reader: { username },
|
|
19
16
|
data: (role) => `/projects/project/data/${role}.json`,
|
|
20
|
-
private: (role) => (
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
private: (role) => Object.fromEntries(pagesOf(people, username).map((page) => [
|
|
18
|
+
page,
|
|
19
|
+
`/projects/project/private/${encodeURIComponent(page)}/${role}.json`,
|
|
20
|
+
])),
|
|
23
21
|
paper: (sha) => `/projects/project/papers/${sha}`,
|
|
24
22
|
document: (id) => `/projects/project/documents/${id}`,
|
|
25
23
|
documents: (prefix) => `/projects/project/documents?prefix=${prefix}`,
|
|
26
24
|
sessionLost: lost,
|
|
27
|
-
}), [username, lost]);
|
|
25
|
+
}), [username, people, lost]);
|
|
28
26
|
}
|
|
29
|
-
function App({ username, router }) {
|
|
27
|
+
function App({ username, people, router, }) {
|
|
30
28
|
const [lost, setLost] = useState(false);
|
|
31
|
-
const value = useMemo(() => ({ username, lost: () => setLost(true) }), [username]);
|
|
29
|
+
const value = useMemo(() => ({ username, people, lost: () => setLost(true) }), [username, people]);
|
|
32
30
|
if (lost)
|
|
33
31
|
return (_jsx(Notice, { role: "alert", title: "Your session ended", children: _jsx(Button, { onClick: () => window.location.reload(), children: "Reload" }) }));
|
|
34
32
|
return (_jsx(Reader.Provider, { value: value, children: _jsx(RouterProvider, { router: router }) }));
|
|
@@ -42,9 +40,18 @@ function readerOf(username) {
|
|
|
42
40
|
? username
|
|
43
41
|
: decodeURIComponent(named.slice(readerCookie.length + 1));
|
|
44
42
|
}
|
|
45
|
-
export async function start(username) {
|
|
43
|
+
export async function start(username, people) {
|
|
46
44
|
document.title = project.title;
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
45
|
+
const local = window.location.pathname.startsWith('/local/');
|
|
46
|
+
const { default: shown } = await (local ? project.local : project.root)();
|
|
47
|
+
if (local) {
|
|
48
|
+
const { default: root } = await project.root();
|
|
49
|
+
if (shown.path !== root.path)
|
|
50
|
+
throw new Error(`ui/local/index.tsx's path "${shown.path}" is not ui/index.tsx's "${root.path}"`);
|
|
51
|
+
}
|
|
52
|
+
const base = local ? '/local' : '';
|
|
53
|
+
if (window.location.pathname === `${base}/`)
|
|
54
|
+
window.history.replaceState(null, '', `${base}/${shown.path}/`);
|
|
55
|
+
const router = projectRouter(shown, useHost, undefined, base || undefined);
|
|
56
|
+
createRoot(document.getElementById('root')).render(_jsx(QueryClientProvider, { client: new QueryClient(), children: _jsx(App, { username: readerOf(username), people: people, router: router }) }));
|
|
50
57
|
}
|
package/dist/host.d.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import { type AnyRoute, type ErrorComponentProps } from '@tanstack/react-router';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AnyProject } from './mount.js';
|
|
3
|
+
import type { Host } from './types.js';
|
|
3
4
|
export type { Host };
|
|
4
5
|
export { pdfAssets } from './pdf.js';
|
|
5
6
|
declare function Failed({ error, reset }: ErrorComponentProps): import("react").JSX.Element;
|
|
6
7
|
/**
|
|
7
|
-
* The route options of a
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* The route options of a project's mount: the project's `Host` from
|
|
9
|
+
* `useHost`, a resettable error boundary, and a loading state announced to
|
|
10
|
+
* screen readers. Spread into `mount`'s options.
|
|
10
11
|
*/
|
|
11
12
|
export declare function frame(useHost: () => Host): {
|
|
12
13
|
component: () => import("react").JSX.Element;
|
|
13
14
|
errorComponent: typeof Failed;
|
|
14
15
|
pendingComponent: () => import("react").JSX.Element;
|
|
15
16
|
};
|
|
16
|
-
/**
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Mounts `project` under `parent` at the path it names, with `options` as the
|
|
19
|
+
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the route.
|
|
20
|
+
*/
|
|
21
|
+
export declare function mount(parent: AnyRoute, project: AnyProject, options: Partial<Omit<AnyRoute['options'], 'getParentRoute' | 'path'>>): AnyRoute;
|
package/dist/host.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Button, Notice } from '@metacensus/library-ui';
|
|
3
3
|
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
|
|
4
|
-
import { Outlet, } from '@tanstack/react-router';
|
|
4
|
+
import { createRoute, Outlet, } from '@tanstack/react-router';
|
|
5
5
|
import { HostContext } from './context.js';
|
|
6
6
|
export { pdfAssets } from './pdf.js';
|
|
7
7
|
function Failed({ error, reset }) {
|
|
@@ -12,9 +12,9 @@ function Failed({ error, reset }) {
|
|
|
12
12
|
}, children: "Try again" }) }));
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* The route options of a
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* The route options of a project's mount: the project's `Host` from
|
|
16
|
+
* `useHost`, a resettable error boundary, and a loading state announced to
|
|
17
|
+
* screen readers. Spread into `mount`'s options.
|
|
18
18
|
*/
|
|
19
19
|
export function frame(useHost) {
|
|
20
20
|
return {
|
|
@@ -25,7 +25,15 @@ export function frame(useHost) {
|
|
|
25
25
|
pendingComponent: () => _jsx("p", { role: "status", children: "Loading\u2026" }),
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Mounts `project` under `parent` at the path it names, with `options` as the
|
|
30
|
+
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the route.
|
|
31
|
+
*/
|
|
32
|
+
export function mount(parent, project, options) {
|
|
33
|
+
const route = createRoute({
|
|
34
|
+
...options,
|
|
35
|
+
getParentRoute: () => parent,
|
|
36
|
+
path: project.path,
|
|
37
|
+
});
|
|
38
|
+
return route.addChildren(project.routes(route));
|
|
31
39
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export type { Corpus, Doc, Host, Kind, Paper, Mount, Reader, RouterOf,
|
|
1
|
+
export type { AnyProject, Corpus, Doc, Host, Kind, Paper, Mount, Project, Reader, RouterOf, } from './types.js';
|
|
2
|
+
export { defineProject } from './mount.js';
|
|
2
3
|
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, usePrivate, useSaveDocument, } from './react.js';
|
|
3
4
|
export { DocumentError } from './documents.js';
|
package/dist/index.js
CHANGED
package/dist/mount.d.ts
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import { type AnyRoute } from '@tanstack/react-router';
|
|
2
|
-
|
|
3
|
-
export declare function createMount(): {
|
|
2
|
+
declare function mountRoute<P extends string>(path: P): {
|
|
4
3
|
root: import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>;
|
|
5
|
-
|
|
4
|
+
route: import("@tanstack/react-router").Route<unknown, import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/react-router").ResolveFullPath<import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/router-core").RoutePrefix<"/", P>>, string, import("@tanstack/router-core").RoutePrefix<"__root__", P>, undefined, import("@tanstack/react-router").ResolveParams<P>, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>;
|
|
6
5
|
};
|
|
7
|
-
declare function routerOf<C extends readonly AnyRoute[]>(children: C): import("@tanstack/router-core").RouterCore<import("@tanstack/router-core").Route<import("@tanstack/react-router").Register, any, "/", "/", string, "__root__", undefined, {}, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, readonly [import("@tanstack/router-core").Route<unknown, import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, "
|
|
8
|
-
/** The parent a project's routes are created against; they type as
|
|
9
|
-
export type Mount = ReturnType<typeof
|
|
10
|
-
/**
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
declare function routerOf<P extends string, C extends readonly AnyRoute[]>(path: P, children: C): import("@tanstack/router-core").RouterCore<import("@tanstack/router-core").Route<import("@tanstack/react-router").Register, any, "/", "/", string, "__root__", undefined, {}, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, readonly [import("@tanstack/router-core").Route<unknown, import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/react-router").ResolveFullPath<import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/router-core").RoutePrefix<"/", P>>, string, import("@tanstack/router-core").RoutePrefix<"__root__", P>, undefined, import("@tanstack/react-router").ResolveParams<P>, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, C, unknown, unknown, unknown, undefined>], unknown, unknown, unknown, undefined>, "never", false, import("@tanstack/history").RouterHistory, Record<string, any>>;
|
|
7
|
+
/** The parent a project's routes are created against; they type as `/<path>/…`. */
|
|
8
|
+
export type Mount<P extends string> = ReturnType<typeof mountRoute<P>>['route'];
|
|
9
|
+
/** A project's routes under the first path segment it names. */
|
|
10
|
+
export interface Project<P extends string = string, C = readonly AnyRoute[]> {
|
|
11
|
+
readonly path: P;
|
|
12
|
+
routes(parent: Mount<P>): C;
|
|
13
|
+
}
|
|
14
|
+
/** Any project, for a list or a constraint: a `Project<'a'>` is not a `Project<string>`. */
|
|
15
|
+
export interface AnyProject {
|
|
16
|
+
readonly path: string;
|
|
17
|
+
routes(parent: never): readonly AnyRoute[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What `ui/index.tsx` and `ui/local/index.tsx` default-export. `path` is the
|
|
21
|
+
* project's first URL segment, matching `^[a-z0-9][a-z0-9-]*$` and not
|
|
22
|
+
* `projects`, `local` or `pdfjs`, which dev and the host reserve; `routes`
|
|
23
|
+
* creates its routes against `parent`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function defineProject<P extends string, const C>(path: P, routes: (parent: Mount<P>) => C): Project<P, C>;
|
|
26
|
+
/** A project's `Register`: `router: RouterOf<typeof project>`. */
|
|
27
|
+
export type RouterOf<R extends AnyProject> = ReturnType<R['routes']> extends infer C extends readonly AnyRoute[] ? ReturnType<typeof routerOf<R['path'], C>> : never;
|
|
14
28
|
export {};
|
package/dist/mount.js
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
import { createRootRoute, createRoute, createRouter, } from '@tanstack/react-router';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const format = /^[a-z0-9][a-z0-9-]*$/;
|
|
3
|
+
// First segments that dev and the host serve themselves.
|
|
4
|
+
const reserved = ['projects', 'local', 'pdfjs'];
|
|
5
|
+
function mountRoute(path) {
|
|
4
6
|
const root = createRootRoute();
|
|
5
|
-
|
|
6
|
-
return { root, mount };
|
|
7
|
+
return { root, route: createRoute({ getParentRoute: () => root, path }) };
|
|
7
8
|
}
|
|
8
9
|
// Only `typeof routerOf` is read: it names the router a project's routes make.
|
|
9
10
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
|
-
function routerOf(children) {
|
|
11
|
-
const { root,
|
|
11
|
+
function routerOf(path, children) {
|
|
12
|
+
const { root, route } = mountRoute(path);
|
|
12
13
|
return createRouter({
|
|
13
|
-
routeTree: root.addChildren([
|
|
14
|
+
routeTree: root.addChildren([route.addChildren(children)]),
|
|
14
15
|
});
|
|
15
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* What `ui/index.tsx` and `ui/local/index.tsx` default-export. `path` is the
|
|
19
|
+
* project's first URL segment, matching `^[a-z0-9][a-z0-9-]*$` and not
|
|
20
|
+
* `projects`, `local` or `pdfjs`, which dev and the host reserve; `routes`
|
|
21
|
+
* creates its routes against `parent`.
|
|
22
|
+
*/
|
|
23
|
+
export function defineProject(path, routes) {
|
|
24
|
+
if (!format.test(path))
|
|
25
|
+
throw new Error(`project path "${path}" does not match ${format}`);
|
|
26
|
+
if (reserved.includes(path))
|
|
27
|
+
throw new Error(`project path "${path}" is reserved by dev and the host`);
|
|
28
|
+
return { path, routes };
|
|
29
|
+
}
|
package/dist/playwright.js
CHANGED
|
@@ -9,7 +9,7 @@ export default defineConfig({
|
|
|
9
9
|
use: { baseURL: process.env.LIBRARY_RESEARCH_URL },
|
|
10
10
|
webServer: {
|
|
11
11
|
command: `"${process.execPath}" -e "${server}"`,
|
|
12
|
-
wait: { stdout: /(?<library_research_url>http:\S+\/
|
|
12
|
+
wait: { stdout: /(?<library_research_url>http:\S+\/)/ },
|
|
13
13
|
},
|
|
14
14
|
});
|
|
15
15
|
/** Collects a page's CSP and axe violations; the returned function fails on those not listed. */
|
package/dist/react.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { DocumentError } from './documents.js';
|
|
2
2
|
import type { Corpus, Doc, Host } from './types.js';
|
|
3
|
+
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
3
4
|
export declare function useHost(): Host;
|
|
4
5
|
type Role<T = unknown> = readonly [role: string, parse: (json: unknown) => T];
|
|
5
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* One data role's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
8
|
+
* fetch or parse throws to the frame's error notice, naming the role. A 401 or
|
|
9
|
+
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
10
|
+
* role list, not on every render.
|
|
11
|
+
*/
|
|
6
12
|
export declare function useData<T>(role: string, parse: (json: unknown) => T): T;
|
|
7
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* `useData` for several roles at once, fetched in parallel: a `[role, parse]`
|
|
15
|
+
* tuple each, the values returned in the same order and typed by their parsers.
|
|
16
|
+
* Each parser must be pure: it is read once per role list, not on every render.
|
|
17
|
+
*/
|
|
8
18
|
export declare function useDatas<const R extends readonly Role[]>(roles: R): {
|
|
9
19
|
-readonly [K in keyof R]: R[K] extends Role<infer T> ? T : never;
|
|
10
20
|
};
|
|
@@ -15,6 +25,7 @@ export declare function useDatas<const R extends readonly Role[]>(roles: R): {
|
|
|
15
25
|
* `{ json } | null`, is not `useData`'s raw JSON, under the same key prefix.
|
|
16
26
|
*/
|
|
17
27
|
export declare function usePrivate<T>(role: string, parse: (json: unknown) => T): Record<string, T>;
|
|
28
|
+
/** The `corpus` role, parsed by the library's own schema, through `useData`. */
|
|
18
29
|
export declare function useCorpus(): Corpus;
|
|
19
30
|
/**
|
|
20
31
|
* One document of the project, or `null` when none has that id. Suspends while
|
package/dist/react.js
CHANGED
|
@@ -3,17 +3,27 @@ import { useContext, useMemo, useState } from 'react';
|
|
|
3
3
|
import { FetchContext, HostContext } from './context.js';
|
|
4
4
|
import { DocumentError, documentId, documentPrefix } from './documents.js';
|
|
5
5
|
import * as schema from './schema.js';
|
|
6
|
+
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
6
7
|
export function useHost() {
|
|
7
8
|
const host = useContext(HostContext);
|
|
8
9
|
if (!host)
|
|
9
10
|
throw new Error('useHost outside the frame of ./host');
|
|
10
11
|
return host;
|
|
11
12
|
}
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* One data role's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
15
|
+
* fetch or parse throws to the frame's error notice, naming the role. A 401 or
|
|
16
|
+
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
17
|
+
* role list, not on every render.
|
|
18
|
+
*/
|
|
13
19
|
export function useData(role, parse) {
|
|
14
20
|
return useDatas([[role, parse]])[0];
|
|
15
21
|
}
|
|
16
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* `useData` for several roles at once, fetched in parallel: a `[role, parse]`
|
|
24
|
+
* tuple each, the values returned in the same order and typed by their parsers.
|
|
25
|
+
* Each parser must be pure: it is read once per role list, not on every render.
|
|
26
|
+
*/
|
|
17
27
|
export function useDatas(roles) {
|
|
18
28
|
const host = useHost();
|
|
19
29
|
// The parsers and select functions are kept until the role list changes, so
|
|
@@ -108,6 +118,7 @@ export function usePrivate(role, parse) {
|
|
|
108
118
|
combine: (results) => Object.fromEntries(results.flatMap(({ data }, i) => data ? [[pages[i], data.value]] : [])),
|
|
109
119
|
});
|
|
110
120
|
}
|
|
121
|
+
/** The `corpus` role, parsed by the library's own schema, through `useData`. */
|
|
111
122
|
export function useCorpus() {
|
|
112
123
|
return useData('corpus', (json) => schema.Corpus.parse(json));
|
|
113
124
|
}
|
package/dist/reader.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
/** The cookie that names the reader of a browser context on a dev server. */
|
|
2
2
|
export declare const readerCookie = "library-research-reader";
|
|
3
|
+
/** Each reader's page keys, as research.json's `people`. */
|
|
4
|
+
export type People = Record<string, string[]>;
|
|
5
|
+
/** The pages a reader holds in `dev` and `testHost`: those `people` lists for them, else the page named as they are. */
|
|
6
|
+
export declare const pagesOf: (people: People | undefined, reader: string) => string[];
|
package/dist/reader.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
/** The cookie that names the reader of a browser context on a dev server. */
|
|
2
2
|
export const readerCookie = 'library-research-reader';
|
|
3
|
+
/** The pages a reader holds in `dev` and `testHost`: those `people` lists for them, else the page named as they are. */
|
|
4
|
+
export const pagesOf = (people, reader) => people && Object.hasOwn(people, reader) ? people[reader] : [reader];
|
package/dist/router.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type RouterHistory } from '@tanstack/react-router';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { AnyProject } from './mount.js';
|
|
3
|
+
import type { Host } from './types.js';
|
|
4
|
+
/** A project mounted in the frame a real host uses, under `basepath` if given; shared by the dev host and the test host. */
|
|
5
|
+
export declare function projectRouter(project: AnyProject, useHost: () => Host, history?: RouterHistory, basepath?: string): import("@tanstack/router-core").RouterCore<import("@tanstack/router-core").Route<import("@tanstack/react-router").Register, any, "/", "/", string, "__root__", undefined, {}, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, readonly [import("@tanstack/react-router").AnyRoute], unknown, unknown, unknown, undefined>, "preserve", false, RouterHistory, Record<string, any>>;
|
package/dist/router.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Notice } from '@metacensus/library-ui';
|
|
3
|
-
import { createRouter } from '@tanstack/react-router';
|
|
4
|
-
import { frame,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const { root, mount } = createMount();
|
|
9
|
-
mount.update(frame(useHost));
|
|
3
|
+
import { createRootRoute, createRouter, } from '@tanstack/react-router';
|
|
4
|
+
import { frame, mount } from './host.js';
|
|
5
|
+
/** A project mounted in the frame a real host uses, under `basepath` if given; shared by the dev host and the test host. */
|
|
6
|
+
export function projectRouter(project, useHost, history, basepath) {
|
|
7
|
+
const root = createRootRoute();
|
|
10
8
|
return createRouter({
|
|
11
|
-
routeTree: root.addChildren([
|
|
9
|
+
routeTree: root.addChildren([mount(root, project, frame(useHost))]),
|
|
12
10
|
history,
|
|
11
|
+
basepath,
|
|
13
12
|
trailingSlash: 'preserve',
|
|
14
13
|
defaultNotFoundComponent: () => _jsx(Notice, { role: "alert", title: "Not found" }),
|
|
15
14
|
});
|
package/dist/schema.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare const Research: z.ZodObject<{
|
|
|
17
17
|
title: z.ZodString;
|
|
18
18
|
data: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
19
19
|
private: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
20
|
+
people: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
|
|
20
21
|
}, z.core.$strict>;
|
|
21
22
|
export declare const Corpus: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
22
23
|
kind: z.ZodEnum<{
|
package/dist/schema.js
CHANGED
|
@@ -21,6 +21,10 @@ export const Research = z
|
|
|
21
21
|
title: z.string().min(1),
|
|
22
22
|
data: z.record(role, glob),
|
|
23
23
|
private: z.record(role, glob).optional(),
|
|
24
|
+
/** Each reader's private pages in `dev` and `testHost`; production's people.json holds the real mapping. */
|
|
25
|
+
people: z
|
|
26
|
+
.record(z.string(), z.array(z.string().regex(/^[a-z0-9][a-z0-9-]*$/)))
|
|
27
|
+
.optional(),
|
|
24
28
|
})
|
|
25
29
|
.superRefine(({ data, private: held = {} }, ctx) => {
|
|
26
30
|
for (const [name, pattern] of Object.entries(held)) {
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
|
-
import type
|
|
2
|
+
import { type People } from './reader.js';
|
|
3
|
+
import type { AnyProject } from './mount.js';
|
|
4
|
+
/** What `testHost` is given besides the project; every field is optional. */
|
|
3
5
|
export interface TestHostOptions {
|
|
4
6
|
/** Each data role's JSON value, parsed by the project's own schema. */
|
|
5
7
|
data?: Record<string, unknown>;
|
|
@@ -10,6 +12,8 @@ export interface TestHostOptions {
|
|
|
10
12
|
body: unknown;
|
|
11
13
|
writer?: string;
|
|
12
14
|
}>;
|
|
15
|
+
/** Each reader's page keys, as research.json's `people`; a reader it omits holds the page named as them. */
|
|
16
|
+
people?: People;
|
|
13
17
|
/** The reader's username. */
|
|
14
18
|
reader?: string;
|
|
15
19
|
/** The path below the project, with search and hash. */
|
|
@@ -18,12 +22,12 @@ export interface TestHostOptions {
|
|
|
18
22
|
sessionLost?: () => void;
|
|
19
23
|
}
|
|
20
24
|
/**
|
|
21
|
-
* A project
|
|
22
|
-
*
|
|
25
|
+
* A project rendered through the frame and the mount a real host uses, at the
|
|
26
|
+
* path it names. Render `element` with any DOM renderer; `router` says where
|
|
23
27
|
* the project navigated, and `documents()` what it saved, sorted by id.
|
|
24
28
|
*/
|
|
25
|
-
export declare function testHost(
|
|
29
|
+
export declare function testHost(project: AnyProject, { data, documents, private: held, people, reader, url, sessionLost, }?: TestHostOptions): {
|
|
26
30
|
element: ReactElement<any, string | import("react").JSXElementConstructor<any>>;
|
|
27
|
-
router: import("@tanstack/router-core").RouterCore<import("@tanstack/router-core").Route<import("@tanstack/react-router").Register, any, "/", "/", string, "__root__", undefined, {}, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, readonly [import("@tanstack/
|
|
31
|
+
router: import("@tanstack/router-core").RouterCore<import("@tanstack/router-core").Route<import("@tanstack/react-router").Register, any, "/", "/", string, "__root__", undefined, {}, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, readonly [import("@tanstack/react-router").AnyRoute], unknown, unknown, unknown, undefined>, "preserve", false, import("@tanstack/history").RouterHistory, Record<string, any>>;
|
|
28
32
|
documents: () => import("./types.js").Doc<unknown>[];
|
|
29
33
|
};
|
package/dist/testing.js
CHANGED
|
@@ -3,14 +3,15 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
|
3
3
|
import { createMemoryHistory, RouterProvider } from '@tanstack/react-router';
|
|
4
4
|
import { FetchContext } from './context.js';
|
|
5
5
|
import { documentStore } from './document-store.js';
|
|
6
|
+
import { pagesOf } from './reader.js';
|
|
6
7
|
import { projectRouter } from './router.js';
|
|
7
8
|
const origin = 'http://test.invalid';
|
|
8
9
|
/**
|
|
9
|
-
* A project
|
|
10
|
-
*
|
|
10
|
+
* A project rendered through the frame and the mount a real host uses, at the
|
|
11
|
+
* path it names. Render `element` with any DOM renderer; `router` says where
|
|
11
12
|
* the project navigated, and `documents()` what it saved, sorted by id.
|
|
12
13
|
*/
|
|
13
|
-
export function testHost(
|
|
14
|
+
export function testHost(project, { data = {}, documents = {}, private: held = {}, people, reader = 'test', url = '/', sessionLost = () => { }, } = {}) {
|
|
14
15
|
const store = documentStore(documents, reader);
|
|
15
16
|
const memory = (input, init) => store.handle(new Request(new URL(input instanceof Request ? input.url : input, origin), init), reader);
|
|
16
17
|
const host = {
|
|
@@ -20,17 +21,18 @@ export function testHost(routes, { data = {}, documents = {}, private: held = {}
|
|
|
20
21
|
throw new Error(`testHost: no data for role "${role}"`);
|
|
21
22
|
return `data:application/json,${encodeURIComponent(JSON.stringify(data[role]))}`;
|
|
22
23
|
},
|
|
23
|
-
private: (role) => Object.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
:
|
|
24
|
+
private: (role) => Object.fromEntries(pagesOf(people, reader)
|
|
25
|
+
.filter((page) => Object.hasOwn(held[role] ?? {}, page))
|
|
26
|
+
.map((page) => [
|
|
27
|
+
page,
|
|
28
|
+
`data:application/json,${encodeURIComponent(JSON.stringify(held[role][page]))}`,
|
|
29
|
+
])),
|
|
28
30
|
paper: (sha) => `/papers/${sha}`,
|
|
29
31
|
document: (id) => `${origin}/documents/${id}`,
|
|
30
32
|
documents: (prefix) => `${origin}/documents?prefix=${prefix}`,
|
|
31
33
|
sessionLost,
|
|
32
34
|
};
|
|
33
|
-
const router = projectRouter(
|
|
35
|
+
const router = projectRouter(project, () => host, createMemoryHistory({ initialEntries: [`/${project.path}${url}`] }));
|
|
34
36
|
// react-router 1.170.46 reads this in Matches before router-core 1.171.39 sets it.
|
|
35
37
|
// Delete once react-router and router-core are bumped to versions that agree.
|
|
36
38
|
Object.assign(router, { _rendered: [] });
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { Corpus as CorpusSchema, kinds } from './schema.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a host gives a project: `frame` provides it and `useHost` returns it.
|
|
5
|
+
* The methods are called during render, so each must be cheap and return the
|
|
6
|
+
* same URL for the same arguments.
|
|
7
|
+
*/
|
|
3
8
|
export interface Host {
|
|
9
|
+
/** The person the project is viewed as, who may differ from the person signed in. */
|
|
4
10
|
reader: Reader;
|
|
11
|
+
/** The URL of a data role's JSON, `role` being one the project declares; `useData` fetches it. */
|
|
5
12
|
data(role: string): string;
|
|
6
13
|
/** The viewed person's pages of a private role: page key to URL; empty when they hold none. */
|
|
7
14
|
private(role: string): Record<string, string>;
|
|
15
|
+
/** The URL of the paper stored under `sha`, as the corpus names it. */
|
|
8
16
|
paper(sha: string): string;
|
|
9
17
|
/**
|
|
10
18
|
* The URL of one document of this project: a GET reads it, a PUT writes it
|
|
@@ -26,10 +34,15 @@ export interface Doc<T> {
|
|
|
26
34
|
revision: number;
|
|
27
35
|
body: T;
|
|
28
36
|
}
|
|
37
|
+
/** The person a project is viewed as. */
|
|
29
38
|
export interface Reader {
|
|
39
|
+
/** The name the host signs them in as. */
|
|
30
40
|
username: string;
|
|
31
41
|
}
|
|
42
|
+
/** A kind of record the schema allows, one of `kinds`' keys. */
|
|
32
43
|
export type Kind = keyof typeof kinds;
|
|
44
|
+
/** The parsed `corpus` role: every paper by sha. */
|
|
33
45
|
export type Corpus = z.infer<typeof CorpusSchema>;
|
|
46
|
+
/** One paper of the corpus. */
|
|
34
47
|
export type Paper = Corpus[string];
|
|
35
|
-
export type { Mount,
|
|
48
|
+
export type { AnyProject, Mount, Project, RouterOf } from './mount.js';
|
package/dist/vite-config.js
CHANGED
package/dist/vite.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { type Plugin } from 'vite';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Compiles each project, by slug and path from the Vite root, into the host.
|
|
4
|
+
* A build fails, naming the rule, unless the host's config keeps these:
|
|
5
|
+
* - an absolute `base`, not `./` or `''`, so a project's CSS `url()` paths resolve;
|
|
6
|
+
* - the default `build.minify`, which keeps the per-project canary the build checks for;
|
|
7
|
+
* - `build.cssCodeSplit`, so each project's CSS stays apart from the host's;
|
|
8
|
+
* - no `manualChunks`, which can mix host and project modules in one chunk;
|
|
9
|
+
* - no `publicDir` that holds or lies inside a project, which would copy it whole.
|
|
10
|
+
*/
|
|
3
11
|
export declare function research({ projects, }: {
|
|
4
12
|
projects: Record<string, string>;
|
|
5
13
|
}): Plugin[];
|
|
14
|
+
/** The Tailwind `content` globs that scan each project's `ui/`, skipping its `node_modules`; spread into the host's config. */
|
|
6
15
|
export declare function tailwindContent(projects: Record<string, string>): string[];
|
package/dist/vite.js
CHANGED
|
@@ -5,6 +5,7 @@ import { isCSSRequest, preprocessCSS, searchForWorkspaceRoot, } from 'vite';
|
|
|
5
5
|
import { project, read } from './data.js';
|
|
6
6
|
const prefix = 'projects';
|
|
7
7
|
const dataPath = new RegExp(`^${prefix}/([^/]+)/data/([^/]+)\\.json$`);
|
|
8
|
+
const privatePath = new RegExp(`^${prefix}/([^/]+)/private/([^/]+)/([^/]+)\\.json$`);
|
|
8
9
|
const virtual = 'virtual:research-projects';
|
|
9
10
|
const cssImport = /@(?:import|use|forward)\b/;
|
|
10
11
|
const cssComposes = /\bcomposes\s*:[^;}]*?\bfrom\s+(['"])([^'"]+)\1/g;
|
|
@@ -22,7 +23,15 @@ const declared = (file) => {
|
|
|
22
23
|
return undefined;
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Compiles each project, by slug and path from the Vite root, into the host.
|
|
28
|
+
* A build fails, naming the rule, unless the host's config keeps these:
|
|
29
|
+
* - an absolute `base`, not `./` or `''`, so a project's CSS `url()` paths resolve;
|
|
30
|
+
* - the default `build.minify`, which keeps the per-project canary the build checks for;
|
|
31
|
+
* - `build.cssCodeSplit`, so each project's CSS stays apart from the host's;
|
|
32
|
+
* - no `manualChunks`, which can mix host and project modules in one chunk;
|
|
33
|
+
* - no `publicDir` that holds or lies inside a project, which would copy it whole.
|
|
34
|
+
*/
|
|
26
35
|
export function research({ projects, }) {
|
|
27
36
|
let config;
|
|
28
37
|
let dirs = [];
|
|
@@ -254,12 +263,26 @@ export function research({ projects, }) {
|
|
|
254
263
|
configureServer(server) {
|
|
255
264
|
server.middlewares.use((req, res, next) => {
|
|
256
265
|
const path = req.url?.split('?')[0] ?? '';
|
|
257
|
-
const
|
|
258
|
-
path.slice(config.base.length)
|
|
259
|
-
|
|
260
|
-
const
|
|
266
|
+
const local = path.startsWith(config.base)
|
|
267
|
+
? path.slice(config.base.length)
|
|
268
|
+
: '';
|
|
269
|
+
const [, slug, role = ''] = local.match(dataPath) ?? [];
|
|
270
|
+
const [, pslug, page = '', prole = ''] = local.match(privatePath) ?? [];
|
|
271
|
+
const dir = dirs.find(([s]) => s === (slug ?? pslug))?.[1];
|
|
261
272
|
if (!dir)
|
|
262
273
|
return next();
|
|
274
|
+
if (pslug) {
|
|
275
|
+
// Gates nothing: the host's dev server decides who loads which page.
|
|
276
|
+
const { pages } = project(dir);
|
|
277
|
+
const held = Object.hasOwn(pages, prole) ? pages[prole]() : {};
|
|
278
|
+
const file = Object.hasOwn(held, page) ? held[page] : undefined;
|
|
279
|
+
if (!file) {
|
|
280
|
+
res.statusCode = 404;
|
|
281
|
+
return res.end(`${pslug} has no private role "${prole}" page "${page}"`);
|
|
282
|
+
}
|
|
283
|
+
res.setHeader('Content-Type', 'application/json');
|
|
284
|
+
return res.end(JSON.stringify(read(dir, file)));
|
|
285
|
+
}
|
|
263
286
|
const { roles } = project(dir);
|
|
264
287
|
if (!Object.hasOwn(roles, role)) {
|
|
265
288
|
res.statusCode = 404;
|
|
@@ -272,6 +295,7 @@ export function research({ projects, }) {
|
|
|
272
295
|
},
|
|
273
296
|
];
|
|
274
297
|
}
|
|
298
|
+
/** The Tailwind `content` globs that scan each project's `ui/`, skipping its `node_modules`; spread into the host's config. */
|
|
275
299
|
export function tailwindContent(projects) {
|
|
276
300
|
return Object.values(projects).flatMap((path) => [
|
|
277
301
|
`${path}/ui/**/*.{ts,tsx}`,
|
package/package.json
CHANGED
|
@@ -27,6 +27,19 @@
|
|
|
27
27
|
"type": "string",
|
|
28
28
|
"pattern": "^(?!\\/)(?!.*\\.\\.).+$"
|
|
29
29
|
}
|
|
30
|
+
},
|
|
31
|
+
"people": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"propertyNames": {
|
|
34
|
+
"type": "string"
|
|
35
|
+
},
|
|
36
|
+
"additionalProperties": {
|
|
37
|
+
"type": "array",
|
|
38
|
+
"items": {
|
|
39
|
+
"type": "string",
|
|
40
|
+
"pattern": "^[a-z0-9][a-z0-9-]*$"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
30
43
|
}
|
|
31
44
|
},
|
|
32
45
|
"required": [
|
package/src/virtual.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
declare module 'virtual:research-projects' {
|
|
2
|
-
import type {
|
|
2
|
+
import type { AnyProject } from '@metacensus/library-research'
|
|
3
3
|
|
|
4
|
-
type Root = () => Promise<{ default:
|
|
4
|
+
type Root = () => Promise<{ default: AnyProject }>
|
|
5
5
|
/** `local` only under `vite serve`, where `ui/local/index.tsx` exists. */
|
|
6
6
|
const projects: Record<string, { title: string; root: Root; local?: Root }>
|
|
7
7
|
export default projects
|