@metacensus/library-research 0.10.0 → 0.11.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 +15 -3
- package/dist/cli.js +1 -1
- package/dist/commands/add.js +2 -1
- package/dist/commands/candidate.js +12 -1
- package/dist/commands/dev.js +10 -5
- package/dist/commands/init.d.ts +1 -0
- package/dist/commands/init.js +92 -0
- package/dist/dev/host.js +4 -0
- package/dist/papers.d.ts +0 -6
- package/dist/papers.js +8 -21
- package/dist/playwright.js +7 -4
- package/dist/react.d.ts +4 -2
- package/dist/react.js +26 -15
- package/dist/roles.d.ts +6 -0
- package/dist/roles.js +13 -5
- package/dist/types.d.ts +2 -0
- package/dist/vite-config.js +3 -2
- package/dist/vite.js +17 -0
- package/package.json +12 -4
- package/tsconfig.project.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# library-research
|
|
2
2
|
|
|
3
|
-
The research-project contract and
|
|
3
|
+
The research-project contract and its tools
|
|
4
4
|
|
|
5
5
|
## Run a project
|
|
6
6
|
|
|
7
|
-
A project is this layout, each path fixed:
|
|
7
|
+
A project is this layout, each path fixed, which `npx @metacensus/library-research init` writes into an empty repository:
|
|
8
8
|
|
|
9
9
|
```
|
|
10
10
|
research.json title and data roles
|
|
11
11
|
corpus.json sha → paper
|
|
12
12
|
papers/<sha>.<kind> through LFS
|
|
13
|
-
ui/package.json depends on @metacensus/library-research; scripts dev and check
|
|
13
|
+
ui/package.json depends on @metacensus/library-research, which brings @playwright/test; scripts dev and check
|
|
14
14
|
ui/tsconfig.json extends @metacensus/library-research/tsconfig.json
|
|
15
15
|
ui/index.tsx default export: the root
|
|
16
16
|
ui/local/index.tsx optional; default export: the local-only root, never shipped
|
|
@@ -27,3 +27,15 @@ From `ui/`:
|
|
|
27
27
|
| `npx playwright test` | runs `browser/` in Chromium on a dev server; a CSP or axe violation fails its spec unless `violations` lists it |
|
|
28
28
|
| `npx library-research add <file> [--title T] [--citation C] [--open]` | adds a paper to `papers/` and `corpus.json` |
|
|
29
29
|
| `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 |
|
|
30
|
+
|
|
31
|
+
## Release
|
|
32
|
+
|
|
33
|
+
A version reaches npm only from a tag on `origin/main`, as the tarball CI installed; the release workflow refuses any other tag, and the tag sets the version.
|
|
34
|
+
|
|
35
|
+
1. The Publish issue carries the changelog, drafted before the tag: what changed, how a project upgrades, what breaks. Jacob's `designed` on it is the yes.
|
|
36
|
+
2. `make release TYPE=patch|minor|major` from a checkout at `origin/main`.
|
|
37
|
+
3. The approved changelog is the GitHub Release's notes: `gh release create vX.Y.Z --verify-tag --notes-file <file>`.
|
|
38
|
+
|
|
39
|
+
## Code correctness belongs to tests and PR review
|
|
40
|
+
|
|
41
|
+
Tests and pull-request review settle whether code is correct; nothing else does. Content review is the researcher's process: a person reviews what the research says (claims, passages, citations, narrative) as data and signs off on it. Its checks, receipts, sign-offs and fingerprints never cover source code, styles or build config, in library-research or in a project, so a refactor that changes no content never waits on a content reviewer. A code change that could alter what a reader sees gets a test that pins what the reader sees, not a sign-off.
|
package/dist/cli.js
CHANGED
|
@@ -10,7 +10,7 @@ if (name === 'help') {
|
|
|
10
10
|
console.log(commands.join('\n'));
|
|
11
11
|
}
|
|
12
12
|
else if (commands.includes(name)) {
|
|
13
|
-
const command = await import(new URL(`${name}.js`, dir).href);
|
|
13
|
+
const command = (await import(new URL(`${name}.js`, dir).href));
|
|
14
14
|
try {
|
|
15
15
|
await command.default(args);
|
|
16
16
|
}
|
package/dist/commands/add.js
CHANGED
|
@@ -2,7 +2,8 @@ import { createHash } from 'node:crypto';
|
|
|
2
2
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { parseArgs } from 'node:util';
|
|
5
|
-
import { git, kindOf, paperPath
|
|
5
|
+
import { git, kindOf, paperPath } from '../papers.js';
|
|
6
|
+
import { readCorpus } from '../roles.js';
|
|
6
7
|
import { findRoot } from '../root.js';
|
|
7
8
|
import { Corpus, kinds } from '../schema.js';
|
|
8
9
|
export default function add(args) {
|
|
@@ -21,6 +21,17 @@ export default async function candidate(args) {
|
|
|
21
21
|
for (let n = 2; taken.has(tag); n++)
|
|
22
22
|
tag = `${day}.${n}`;
|
|
23
23
|
git(root, 'tag', '--annotate', '--message', tag, tag);
|
|
24
|
-
|
|
24
|
+
try {
|
|
25
|
+
git(root, 'push', '--quiet', 'origin', `refs/tags/${tag}`);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
try {
|
|
29
|
+
git(root, 'tag', '--delete', tag);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// the push's error is the one to report
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
25
36
|
console.log(`make pin PROJECT=${id} TAG=${tag}`);
|
|
26
37
|
}
|
package/dist/commands/dev.js
CHANGED
|
@@ -5,7 +5,8 @@ import { extname, join } from 'node:path';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { parseArgs } from 'node:util';
|
|
7
7
|
import { createServer, mergeConfig } from 'vite';
|
|
8
|
-
import { paperPath
|
|
8
|
+
import { paperPath } from '../papers.js';
|
|
9
|
+
import { readCorpus } from '../roles.js';
|
|
9
10
|
import { findRoot } from '../root.js';
|
|
10
11
|
import { kinds } from '../schema.js';
|
|
11
12
|
import { hostConfig } from '../vite-config.js';
|
|
@@ -30,7 +31,7 @@ const page = `<!doctype html>
|
|
|
30
31
|
</html>
|
|
31
32
|
`;
|
|
32
33
|
const nonce = randomBytes(16).toString('base64');
|
|
33
|
-
// service-ui-research's defaultHeaders, plus the nonce
|
|
34
|
+
// service-ui-research's defaultHeaders, plus the nonce, less the papers origin
|
|
34
35
|
const policy = `default-src 'self'; script-src 'self' 'wasm-unsafe-eval' 'nonce-${nonce}'; style-src 'self' 'nonce-${nonce}'; worker-src 'self'; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'`;
|
|
35
36
|
export default async function dev(args) {
|
|
36
37
|
const { values } = parseArgs({
|
|
@@ -97,6 +98,8 @@ function devHost(root, reader, local) {
|
|
|
97
98
|
if (!kind)
|
|
98
99
|
return notFound(res, `${sha}: not in corpus.json`);
|
|
99
100
|
const file = paperPath(sha, kind);
|
|
101
|
+
if (!existsSync(join(root, file)))
|
|
102
|
+
return notFound(res, `${file}: listed in corpus.json but missing`);
|
|
100
103
|
if (realpathSync(join(root, file)) !== join(real, file))
|
|
101
104
|
return notFound(res, `${file}: leads outside papers/`);
|
|
102
105
|
const bytes = readFileSync(join(root, file));
|
|
@@ -104,12 +107,14 @@ function devHost(root, reader, local) {
|
|
|
104
107
|
return notFound(res, `${file}: an LFS pointer; run git lfs pull`);
|
|
105
108
|
send(req, res, bytes, kinds[kind]);
|
|
106
109
|
});
|
|
107
|
-
return () => server.middlewares.use(
|
|
110
|
+
return () => server.middlewares.use((req, res, next) => {
|
|
108
111
|
const url = req.originalUrl ?? '';
|
|
109
112
|
if (req.url !== '/index.html' || !mount.test(url))
|
|
110
113
|
return next();
|
|
111
|
-
|
|
112
|
-
|
|
114
|
+
server
|
|
115
|
+
.transformIndexHtml(url, page)
|
|
116
|
+
.then((html) => res.writeHead(200, { 'Content-Type': 'text/html' }).end(html))
|
|
117
|
+
.catch(next);
|
|
113
118
|
});
|
|
114
119
|
},
|
|
115
120
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function init(args: string[]): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
const { version } = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
5
|
+
const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
|
6
|
+
export default function init(args) {
|
|
7
|
+
parseArgs({ args });
|
|
8
|
+
const root = process.cwd();
|
|
9
|
+
const title = basename(root);
|
|
10
|
+
if (!/^[\w.-]+$/.test(title))
|
|
11
|
+
throw new Error(`${title}: not a repository name; use letters, digits, ., - or _`);
|
|
12
|
+
const files = {
|
|
13
|
+
'research.json': json({ title, data: {} }),
|
|
14
|
+
'corpus.json': '{}\n',
|
|
15
|
+
'.gitattributes': 'papers/** filter=lfs diff=lfs merge=lfs -text\n',
|
|
16
|
+
'.gitignore': 'node_modules/\ntest-results/\n',
|
|
17
|
+
'.nvmrc': '24\n',
|
|
18
|
+
'README.md': `# ${title}
|
|
19
|
+
|
|
20
|
+
A research project on [\`@metacensus/library-research\`](https://www.npmjs.com/package/@metacensus/library-research), whose README lists every command.
|
|
21
|
+
|
|
22
|
+
From \`ui/\`, run \`npm install\` and commit its lockfile. \`npx library-research candidate\` then names a commit for the service to pin.
|
|
23
|
+
`,
|
|
24
|
+
'.github/workflows/check.yml': `name: Check
|
|
25
|
+
|
|
26
|
+
on:
|
|
27
|
+
pull_request:
|
|
28
|
+
push:
|
|
29
|
+
branches: [main]
|
|
30
|
+
|
|
31
|
+
permissions:
|
|
32
|
+
contents: read
|
|
33
|
+
|
|
34
|
+
concurrency:
|
|
35
|
+
group: \${{ github.workflow }}-\${{ github.ref }}
|
|
36
|
+
cancel-in-progress: true
|
|
37
|
+
|
|
38
|
+
jobs:
|
|
39
|
+
check:
|
|
40
|
+
name: Check
|
|
41
|
+
runs-on: ubuntu-latest
|
|
42
|
+
timeout-minutes: 15
|
|
43
|
+
defaults:
|
|
44
|
+
run:
|
|
45
|
+
working-directory: ui
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/checkout@v7
|
|
48
|
+
- uses: actions/setup-node@v7
|
|
49
|
+
with:
|
|
50
|
+
node-version-file: .nvmrc
|
|
51
|
+
cache: npm
|
|
52
|
+
cache-dependency-path: ui/package-lock.json
|
|
53
|
+
- run: npm ci
|
|
54
|
+
- run: npx playwright install --with-deps chromium
|
|
55
|
+
- run: npm run check
|
|
56
|
+
- run: npx playwright test
|
|
57
|
+
`,
|
|
58
|
+
'ui/package.json': json({
|
|
59
|
+
private: true,
|
|
60
|
+
scripts: { dev: 'library-research dev', check: 'library-research check' },
|
|
61
|
+
dependencies: { '@metacensus/library-research': `^${version}` },
|
|
62
|
+
devEngines: { runtime: { name: 'node', version: '24' } },
|
|
63
|
+
}),
|
|
64
|
+
'ui/tsconfig.json': json({
|
|
65
|
+
extends: '@metacensus/library-research/tsconfig.json',
|
|
66
|
+
}),
|
|
67
|
+
'ui/index.tsx': `export default function Root() {
|
|
68
|
+
return (
|
|
69
|
+
<main>
|
|
70
|
+
<h1>${title}</h1>
|
|
71
|
+
</main>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
`,
|
|
75
|
+
'ui/playwright.config.ts': `export { default } from '@metacensus/library-research/playwright'\n`,
|
|
76
|
+
'ui/browser/page.spec.ts': `import { expect, test } from '@metacensus/library-research/playwright'
|
|
77
|
+
|
|
78
|
+
test('the root renders', async ({ page }) => {
|
|
79
|
+
await page.goto('')
|
|
80
|
+
await expect(page.getByRole('heading', { level: 1 })).toBeVisible()
|
|
81
|
+
})
|
|
82
|
+
`,
|
|
83
|
+
};
|
|
84
|
+
const taken = Object.keys(files).filter((path) => existsSync(join(root, path)));
|
|
85
|
+
if (taken.length)
|
|
86
|
+
throw new Error(taken.map((path) => `${path}: already exists`).join('\n'));
|
|
87
|
+
for (const [path, text] of Object.entries(files)) {
|
|
88
|
+
mkdirSync(dirname(join(root, path)), { recursive: true });
|
|
89
|
+
writeFileSync(join(root, path), text);
|
|
90
|
+
console.log(path);
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/dev/host.js
CHANGED
|
@@ -15,6 +15,7 @@ function here() {
|
|
|
15
15
|
}
|
|
16
16
|
function App({ username }) {
|
|
17
17
|
const [location, setLocation] = useState(here);
|
|
18
|
+
const [lost, setLost] = useState(false);
|
|
18
19
|
useEffect(() => {
|
|
19
20
|
const pop = () => setLocation(here());
|
|
20
21
|
window.addEventListener('popstate', pop);
|
|
@@ -30,7 +31,10 @@ function App({ username }) {
|
|
|
30
31
|
},
|
|
31
32
|
data: (role) => `/projects/project/data/${role}.json`,
|
|
32
33
|
paper: (sha) => `/projects/project/papers/${sha}`,
|
|
34
|
+
sessionLost: () => setLost(true),
|
|
33
35
|
};
|
|
36
|
+
if (lost)
|
|
37
|
+
return (_jsx(Notice, { role: "alert", title: "Your session ended", children: _jsx(Button, { onClick: () => window.location.reload(), children: "Reload" }) }));
|
|
34
38
|
return (_jsx(QueryErrorResetBoundary, { children: ({ reset }) => (_jsx(Boundary, { reset: reset, children: _jsx(Suspense, { fallback: null, children: _jsx(ResearchProvider, { host: host, children: _jsx(Root, {}) }) }) })) }));
|
|
35
39
|
}
|
|
36
40
|
class Boundary extends Component {
|
package/dist/papers.d.ts
CHANGED
|
@@ -2,10 +2,4 @@ import type { Kind } from './types.js';
|
|
|
2
2
|
export declare function git(root: string, ...args: string[]): string;
|
|
3
3
|
export declare function kindOf(path: string): Kind | undefined;
|
|
4
4
|
export declare const paperPath: (sha: string, kind: Kind) => string;
|
|
5
|
-
export declare function readCorpus(root: string): Record<string, {
|
|
6
|
-
kind: "pdf" | "doc" | "docx" | "xls" | "xlsx" | "zip" | "gif" | "jpg" | "tif" | "txt" | "html" | "xml";
|
|
7
|
-
title?: string | undefined;
|
|
8
|
-
citation?: string | undefined;
|
|
9
|
-
open?: true | undefined;
|
|
10
|
-
}>;
|
|
11
5
|
export declare function checkPapers(root: string): string[];
|
package/dist/papers.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Corpus, kinds } from './schema.js';
|
|
2
|
+
import { extname } from 'node:path';
|
|
3
|
+
import { readCorpus } from './roles.js';
|
|
4
|
+
import { kinds } from './schema.js';
|
|
6
5
|
export function git(root, ...args) {
|
|
7
6
|
const { status, stdout, stderr, error } = spawnSync('git', args, {
|
|
8
7
|
cwd: root,
|
|
@@ -10,29 +9,17 @@ export function git(root, ...args) {
|
|
|
10
9
|
});
|
|
11
10
|
if (status === 0)
|
|
12
11
|
return stdout;
|
|
13
|
-
|
|
12
|
+
const why = error?.message ??
|
|
13
|
+
stderr.match(/^ ! \[remote rejected\].*/m)?.[0].trim() ??
|
|
14
|
+
stderr.match(/^(fatal|error): .*/m)?.[0] ??
|
|
15
|
+
stderr.split('\n')[0];
|
|
16
|
+
throw new Error(`git ${args[0]}: ${why}`);
|
|
14
17
|
}
|
|
15
18
|
export function kindOf(path) {
|
|
16
19
|
const extension = extname(path).slice(1).toLowerCase();
|
|
17
20
|
return Object.hasOwn(kinds, extension) ? extension : undefined;
|
|
18
21
|
}
|
|
19
22
|
export const paperPath = (sha, kind) => `papers/${sha}.${kind}`;
|
|
20
|
-
export function readCorpus(root) {
|
|
21
|
-
const file = join(root, 'corpus.json');
|
|
22
|
-
if (!existsSync(file))
|
|
23
|
-
return {};
|
|
24
|
-
try {
|
|
25
|
-
return Corpus.parse(JSON.parse(readFileSync(file, 'utf8')));
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
const why = error instanceof z.ZodError
|
|
29
|
-
? error.issues
|
|
30
|
-
.map(({ path, message }) => `${path.join('.')}: ${message}`)
|
|
31
|
-
.join('; ')
|
|
32
|
-
: error.message;
|
|
33
|
-
throw new Error(`corpus.json: ${why}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
23
|
// git-lfs leaves an empty file as is, so `git lfs ls-files` never lists it.
|
|
37
24
|
const emptySha = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
|
|
38
25
|
export function checkPapers(root) {
|
package/dist/playwright.js
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import AxeBuilder from '@axe-core/playwright';
|
|
2
2
|
import { defineConfig, expect, test as base } from '@playwright/test';
|
|
3
3
|
const report = 'libraryResearchViolation';
|
|
4
|
+
const sent = 'libraryResearchSent';
|
|
5
|
+
const server = `const index = require('node:url').pathToFileURL(require.resolve('@metacensus/library-research')); import(new URL('commands/dev.js', index)).then((dev) => dev.default(['--port', '0']))`;
|
|
4
6
|
export default defineConfig({
|
|
5
7
|
testDir: 'browser',
|
|
6
8
|
use: { baseURL: process.env.LIBRARY_RESEARCH_URL },
|
|
7
9
|
webServer: {
|
|
8
|
-
command:
|
|
10
|
+
command: `"${process.execPath}" -e "${server}"`,
|
|
9
11
|
wait: { stdout: /(?<library_research_url>http:\S+\/project\/)/ },
|
|
10
12
|
},
|
|
11
13
|
});
|
|
12
14
|
export const test = base.extend({
|
|
13
15
|
violations: [[], { option: true }],
|
|
14
|
-
page: async ({ page, violations },
|
|
16
|
+
page: async ({ page, violations }, provide) => {
|
|
15
17
|
const found = [];
|
|
16
18
|
await page.exposeFunction(report, (v) => found.push(v));
|
|
17
|
-
await page.addInitScript(`document.addEventListener('securitypolicyviolation', (e) => ${report}('csp ' + e.effectiveDirective + ' ' + e.blockedURI))`);
|
|
18
|
-
await
|
|
19
|
+
await page.addInitScript(`window.${sent} = []; document.addEventListener('securitypolicyviolation', (e) => ${sent}.push(${report}('csp ' + e.effectiveDirective + ' ' + e.blockedURI)))`);
|
|
20
|
+
await provide(page);
|
|
21
|
+
await page.evaluate(`Promise.all(window.${sent} ?? [])`);
|
|
19
22
|
const axe = await new AxeBuilder({ page }).analyze();
|
|
20
23
|
found.push(...axe.violations.map((v) => `axe ${v.id}`));
|
|
21
24
|
expect(found.filter((v) => !violations.includes(v)), 'unlisted violations').toEqual([]);
|
package/dist/react.d.ts
CHANGED
|
@@ -5,13 +5,15 @@ export declare function ResearchProvider({ host, children, }: PropsWithChildren<
|
|
|
5
5
|
}>): import("react").JSX.Element;
|
|
6
6
|
export declare function useHost(): Host;
|
|
7
7
|
type Role<T = unknown> = readonly [role: string, parse: (json: unknown) => T];
|
|
8
|
+
/** `parse` must be pure: it is read once per role list, not on every render. */
|
|
8
9
|
export declare function useData<T>(role: string, parse: (json: unknown) => T): T;
|
|
10
|
+
/** Each parser must be pure: it is read once per role list, not on every render. */
|
|
9
11
|
export declare function useDatas<const R extends readonly Role[]>(roles: R): {
|
|
10
12
|
-readonly [K in keyof R]: R[K] extends Role<infer T> ? T : never;
|
|
11
13
|
};
|
|
12
14
|
export declare function useCorpus(): Corpus;
|
|
13
|
-
export declare
|
|
15
|
+
export declare const Link: import("react").ForwardRefExoticComponent<{
|
|
14
16
|
to: string;
|
|
15
17
|
replace?: boolean;
|
|
16
|
-
} & Omit<AnchorHTMLAttributes<HTMLAnchorElement>,
|
|
18
|
+
} & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href"> & import("react").RefAttributes<HTMLAnchorElement>>;
|
|
17
19
|
export {};
|
package/dist/react.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useSuspenseQueries } from '@tanstack/react-query';
|
|
3
|
-
import { createContext, useContext, useMemo,
|
|
3
|
+
import { createContext, forwardRef, useContext, useMemo, useState, } from 'react';
|
|
4
4
|
import * as schema from './schema.js';
|
|
5
5
|
const HostContext = createContext(null);
|
|
6
6
|
export function ResearchProvider({ host, children, }) {
|
|
@@ -12,22 +12,28 @@ export function useHost() {
|
|
|
12
12
|
throw new Error('useHost outside a ResearchProvider');
|
|
13
13
|
return host;
|
|
14
14
|
}
|
|
15
|
+
/** `parse` must be pure: it is read once per role list, not on every render. */
|
|
15
16
|
export function useData(role, parse) {
|
|
16
17
|
return useDatas([[role, parse]])[0];
|
|
17
18
|
}
|
|
19
|
+
/** Each parser must be pure: it is read once per role list, not on every render. */
|
|
18
20
|
export function useDatas(roles) {
|
|
19
21
|
const host = useHost();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const
|
|
22
|
+
// The parsers and select functions are kept until the role list changes, so
|
|
23
|
+
// an inline parse does not rerun on every render.
|
|
24
|
+
const key = JSON.stringify(roles.map(([role]) => role));
|
|
25
|
+
const [held, hold] = useState({ key, roles });
|
|
26
|
+
if (held.key !== key)
|
|
27
|
+
hold({ key, roles });
|
|
28
|
+
const names = held.roles.map(([role]) => role);
|
|
29
|
+
const selects = useMemo(() => held.roles.map(([role, parse]) => (json) => {
|
|
24
30
|
try {
|
|
25
|
-
return
|
|
31
|
+
return parse(json);
|
|
26
32
|
}
|
|
27
33
|
catch (cause) {
|
|
28
34
|
return failed(role, cause);
|
|
29
35
|
}
|
|
30
|
-
}), [
|
|
36
|
+
}), [held]);
|
|
31
37
|
return useSuspenseQueries({
|
|
32
38
|
queries: names.map((role, i) => {
|
|
33
39
|
const url = host.data(role);
|
|
@@ -35,6 +41,8 @@ export function useDatas(roles) {
|
|
|
35
41
|
queryKey: ['research', url],
|
|
36
42
|
queryFn: () => fetch(url)
|
|
37
43
|
.then((response) => {
|
|
44
|
+
if (response.status === 401 || response.status === 403)
|
|
45
|
+
host.sessionLost();
|
|
38
46
|
if (!response.ok)
|
|
39
47
|
throw new Error(`${response.status} from ${url}`);
|
|
40
48
|
return response.json();
|
|
@@ -45,17 +53,20 @@ export function useDatas(roles) {
|
|
|
45
53
|
retry: false,
|
|
46
54
|
};
|
|
47
55
|
}),
|
|
48
|
-
|
|
56
|
+
combine: datas,
|
|
57
|
+
});
|
|
49
58
|
}
|
|
59
|
+
// Module-level, so combine reruns only when a result changes.
|
|
60
|
+
const datas = (results) => results.map(({ data }) => data);
|
|
50
61
|
function failed(role, cause) {
|
|
51
|
-
const why = cause instanceof Error ? cause.message : cause;
|
|
62
|
+
const why = cause instanceof Error ? cause.message : String(cause);
|
|
52
63
|
throw new Error(`data role "${role}": ${why}`, { cause });
|
|
53
64
|
}
|
|
54
65
|
export function useCorpus() {
|
|
55
|
-
return useData('corpus', schema.Corpus.parse);
|
|
66
|
+
return useData('corpus', (json) => schema.Corpus.parse(json));
|
|
56
67
|
}
|
|
57
|
-
export function Link({ to, replace, ...a }) {
|
|
58
|
-
const
|
|
68
|
+
export const Link = forwardRef(function Link({ to, replace, ...a }, ref) {
|
|
69
|
+
const host = useHost();
|
|
59
70
|
const onClick = (event) => {
|
|
60
71
|
a.onClick?.(event);
|
|
61
72
|
if (event.defaultPrevented ||
|
|
@@ -67,7 +78,7 @@ export function Link({ to, replace, ...a }) {
|
|
|
67
78
|
(a.target && a.target !== '_self'))
|
|
68
79
|
return;
|
|
69
80
|
event.preventDefault();
|
|
70
|
-
navigate(to, { replace });
|
|
81
|
+
host.navigate(to, { replace });
|
|
71
82
|
};
|
|
72
|
-
return _jsx("a", { ...a, href: base + to, onClick: onClick });
|
|
73
|
-
}
|
|
83
|
+
return _jsx("a", { ...a, ref: ref, href: host.base + to, onClick: onClick });
|
|
84
|
+
});
|
package/dist/roles.d.ts
CHANGED
|
@@ -3,3 +3,9 @@ export declare function project(dir: string): {
|
|
|
3
3
|
title: string;
|
|
4
4
|
roles: Record<string, () => unknown>;
|
|
5
5
|
};
|
|
6
|
+
export declare function readCorpus(dir: string): Record<string, {
|
|
7
|
+
kind: "pdf" | "doc" | "docx" | "xls" | "xlsx" | "zip" | "gif" | "jpg" | "tif" | "txt" | "html" | "xml";
|
|
8
|
+
title?: string | undefined;
|
|
9
|
+
citation?: string | undefined;
|
|
10
|
+
open?: true | undefined;
|
|
11
|
+
}>;
|
package/dist/roles.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { globSync, readFileSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { z } from 'zod';
|
|
1
|
+
import { globSync, lstatSync, readFileSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { join, sep } from 'node:path';
|
|
4
3
|
import { Corpus, Research } from './schema.js';
|
|
5
4
|
/** Reads a project's research.json; each role reads its files when called. */
|
|
6
5
|
export function project(dir) {
|
|
7
6
|
const { title, data } = read(dir, 'research.json', Research);
|
|
8
7
|
const roles = {
|
|
9
|
-
corpus: () =>
|
|
8
|
+
corpus: () => readCorpus(dir),
|
|
10
9
|
};
|
|
11
10
|
for (const [role, glob] of Object.entries(data))
|
|
12
11
|
roles[role] = () => match(dir, glob);
|
|
@@ -23,11 +22,18 @@ function match(dir, glob) {
|
|
|
23
22
|
const key = (file) => file.slice(head, file.length - tail).replace(/^[./]+|[./]+$/g, '');
|
|
24
23
|
return Object.fromEntries(files.map((file) => [key(file), read(dir, file)]));
|
|
25
24
|
}
|
|
25
|
+
export function readCorpus(dir) {
|
|
26
|
+
return lstatSync(join(dir, 'corpus.json'), { throwIfNoEntry: false })
|
|
27
|
+
? read(dir, 'corpus.json', Corpus)
|
|
28
|
+
: {};
|
|
29
|
+
}
|
|
26
30
|
function read(dir, file, schema) {
|
|
27
31
|
const path = join(dir, file);
|
|
28
32
|
const fail = (why) => new Error(`${path}: ${why}`);
|
|
29
33
|
let json;
|
|
30
34
|
try {
|
|
35
|
+
if (!realpathSync(path).startsWith(realpathSync(dir) + sep))
|
|
36
|
+
throw new Error('leads outside the project');
|
|
31
37
|
json = JSON.parse(readFileSync(path, 'utf8'));
|
|
32
38
|
}
|
|
33
39
|
catch (e) {
|
|
@@ -37,6 +43,8 @@ function read(dir, file, schema) {
|
|
|
37
43
|
return json;
|
|
38
44
|
const checked = schema.safeParse(json);
|
|
39
45
|
if (!checked.success)
|
|
40
|
-
throw fail(
|
|
46
|
+
throw fail(checked.error.issues
|
|
47
|
+
.map(({ path, message }) => `${path.join('.')}: ${message}`)
|
|
48
|
+
.join('; '));
|
|
41
49
|
return checked.data;
|
|
42
50
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export interface Host {
|
|
|
11
11
|
}): void;
|
|
12
12
|
data(role: string): string;
|
|
13
13
|
paper(sha: string): string;
|
|
14
|
+
/** Called each time a data request is answered 401 or 403, so several times for one lost session. */
|
|
15
|
+
sessionLost(): void;
|
|
14
16
|
}
|
|
15
17
|
export interface Reader {
|
|
16
18
|
username: string;
|
package/dist/vite-config.js
CHANGED
|
@@ -17,8 +17,9 @@ export function hostConfig(root) {
|
|
|
17
17
|
],
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
|
-
//
|
|
21
|
-
// copy; Vite reads no imports inside node_modules, so theirs are
|
|
20
|
+
// library-research's modules stay unbundled so the host and the project
|
|
21
|
+
// share one copy; Vite reads no imports inside node_modules, so theirs are
|
|
22
|
+
// listed.
|
|
22
23
|
optimizeDeps: {
|
|
23
24
|
include: [
|
|
24
25
|
'@metacensus/library-ui',
|
package/dist/vite.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync, realpathSync, statSync, } from 'node:fs';
|
|
2
2
|
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { satisfies } from 'semver';
|
|
3
4
|
import { isCSSRequest, preprocessCSS, searchForWorkspaceRoot, } from 'vite';
|
|
4
5
|
import { project } from './roles.js';
|
|
5
6
|
const prefix = 'projects';
|
|
@@ -13,6 +14,14 @@ const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
13
14
|
const inside = (path, dir) => (path + sep).startsWith(dir + sep);
|
|
14
15
|
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
15
16
|
const shared = [pkg.name, ...Object.keys(pkg.peerDependencies)];
|
|
17
|
+
const declared = (file) => {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(file, 'utf8')).dependencies?.[pkg.name];
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
16
25
|
/** Compiles each project, by slug and path from the Vite root, into the host. */
|
|
17
26
|
export function research({ projects, }) {
|
|
18
27
|
let config;
|
|
@@ -52,6 +61,14 @@ export function research({ projects, }) {
|
|
|
52
61
|
slug,
|
|
53
62
|
realpathSync(resolve(from, path)),
|
|
54
63
|
]);
|
|
64
|
+
for (const [slug, dir] of dirs) {
|
|
65
|
+
const file = join(dir, 'ui/package.json');
|
|
66
|
+
const range = declared(file);
|
|
67
|
+
if (typeof range !== 'string')
|
|
68
|
+
throw new Error(`${file}: project ${slug} does not declare ${pkg.name}`);
|
|
69
|
+
if (!satisfies(pkg.version, range))
|
|
70
|
+
throw new Error(`${file}: project ${slug} declares ${pkg.name} ${range}, excluding the host's ${pkg.version}`);
|
|
71
|
+
}
|
|
55
72
|
return {
|
|
56
73
|
resolve: { dedupe: shared },
|
|
57
74
|
worker: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metacensus/library-research",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -48,25 +48,33 @@
|
|
|
48
48
|
"@types/react": "^18.3.31",
|
|
49
49
|
"@types/react-dom": "^18.3.7",
|
|
50
50
|
"@vitejs/plugin-react": "^6.3.0",
|
|
51
|
+
"semver": "^7.8.6",
|
|
51
52
|
"tailwindcss": "^3.4.19",
|
|
52
53
|
"typescript": "^6.0.3",
|
|
53
|
-
"vite": "^8.3.8",
|
|
54
54
|
"zod": "^4.6.7"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"@metacensus/library-ui": ">=0.6.0 <1",
|
|
58
58
|
"@tanstack/react-query": "^5",
|
|
59
59
|
"react": "^18.3.1",
|
|
60
|
-
"react-dom": "^18.3.1"
|
|
60
|
+
"react-dom": "^18.3.1",
|
|
61
|
+
"vite": "~8.3.8"
|
|
61
62
|
},
|
|
62
63
|
"devDependencies": {
|
|
64
|
+
"@eslint/js": "^9.39.5",
|
|
63
65
|
"@metacensus/library-ui": "^0.12.0",
|
|
64
66
|
"@tanstack/react-query": "^5.108.0",
|
|
65
67
|
"@types/jsdom": "^30.0.0",
|
|
66
68
|
"@types/node": "^24.19.2",
|
|
69
|
+
"@types/semver": "^7.8.0",
|
|
70
|
+
"eslint": "^9.39.5",
|
|
71
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
67
72
|
"jsdom": "^31.0.1",
|
|
73
|
+
"postcss": "^8.5.33",
|
|
68
74
|
"prettier": "^3.10.1",
|
|
69
75
|
"react": "^18.3.1",
|
|
70
|
-
"react-dom": "^18.3.1"
|
|
76
|
+
"react-dom": "^18.3.1",
|
|
77
|
+
"typescript-eslint": "^8.73.0",
|
|
78
|
+
"vite": "~8.3.8"
|
|
71
79
|
}
|
|
72
80
|
}
|