@metacensus/library-research 0.13.0 → 0.14.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 +17 -8
- package/dist/commands/check.js +1 -1
- package/dist/commands/dev.js +16 -10
- package/dist/data.d.ts +11 -6
- package/dist/data.js +13 -13
- package/dist/datasets.d.ts +7 -0
- package/dist/{roles.js → datasets.js} +24 -25
- package/dist/dev/host.d.ts +7 -1
- package/dist/dev/host.js +15 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +20 -13
- package/dist/react.js +33 -25
- package/dist/reader.d.ts +18 -4
- package/dist/reader.js +25 -2
- package/dist/schema.d.ts +6 -1
- package/dist/schema.js +35 -8
- package/dist/testing.d.ts +10 -5
- package/dist/testing.js +12 -10
- package/dist/types.d.ts +18 -7
- package/dist/vite.js +15 -13
- package/package.json +4 -4
- package/schema/research.schema.json +29 -5
- package/dist/roles.d.ts +0 -7
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ The research-project contract and its tools
|
|
|
7
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
|
-
research.json title,
|
|
10
|
+
research.json title, datasets, private datasets, roles and test people
|
|
11
11
|
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
|
|
@@ -33,19 +33,28 @@ From `ui/`:
|
|
|
33
33
|
|
|
34
34
|
A project renders in the browser only: the host is one single-page app and each project is a route of it, with no static build. A project never calls React's `lazy()`, and `check` fails on it; the PDF reader and charting come from `@metacensus/library-ui`.
|
|
35
35
|
|
|
36
|
-
##
|
|
36
|
+
## Datasets
|
|
37
37
|
|
|
38
|
-
Each key of research.json's `data` is a
|
|
38
|
+
Each key of research.json's `data` is a dataset, and `ui/datasets.ts` exports `datasets`, one Zod schema per dataset over the dataset's whole value; a glob dataset's is a `z.record` keyed by what its wildcard matched. The UI parses with it and `check` fails, one line per record file, on data that does not. A dataset with no schema fails, and `corpus.json` is no dataset: library-research's `Corpus` parses it.
|
|
39
39
|
|
|
40
40
|
## Private data
|
|
41
41
|
|
|
42
|
-
`research.json`'s `private` maps a
|
|
42
|
+
`research.json`'s `private` maps a dataset to a glob holding exactly one `*`, such as `{ "private": { "ask": "research_notes/coauthors/*/questions.json" } }`. What the `*` matches is the page key, and the build emits each match at `/projects/<id>/private/<page>/<dataset>.json`, never among the shared `data/` files. A host serves that path only to the people who hold the page. A page key matches `^[a-z0-9][a-z0-9-]*$` and a dataset is in `data` or `private`, not both; `check` fails otherwise, naming the dataset.
|
|
43
43
|
|
|
44
|
-
`usePrivate(
|
|
44
|
+
`usePrivate(dataset, 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 dataset's schema in `ui/datasets.ts` parses one page's file, and `load` returns the dataset as `{ page: parsed }`. A host's `private(dataset)` returns the page-to-URL map.
|
|
45
45
|
|
|
46
|
-
In `dev` and `testHost`,
|
|
46
|
+
In `dev` and `testHost`, a reader holds the pages [`people`](#people) gives them, and `testHost`'s `private: { dataset: { page: json } }` holds each page's JSON.
|
|
47
47
|
|
|
48
|
-
Node loads `ui/
|
|
48
|
+
Node loads `ui/datasets.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 datasets through `load` from `@metacensus/library-research/datasets`, which takes the folder holding research.json and returns each dataset parsed, with `corpus`.
|
|
49
|
+
|
|
50
|
+
## People
|
|
51
|
+
|
|
52
|
+
`research.json`'s optional `roles` lists the roles a person of the project holds one of, each matching `^[a-z][a-z0-9-]*$`, none repeated: `{ "roles": ["author", "leader"] }`.
|
|
53
|
+
|
|
54
|
+
- `useReader()` returns the host's `reader`, the person the project is viewed as: `{ username, name, role, self }`. `role` is one of `roles`, or null in a project that declares none. `self` is false while the host views the project as a person other than the one signed in.
|
|
55
|
+
- `useMembers()` returns the host's `members`, every person of the project, each `{ username, name, role }`.
|
|
56
|
+
|
|
57
|
+
In `dev` and `testHost`, `research.json`'s optional `people` stands in for production's people.json: it maps a username to `{ name, role, pages }`, `role` and `pages` optional, as `{ "people": { "dan.hertz": { "name": "Hertz", "role": "author", "pages": ["hertz"] } } }`. Every entry is a member. `dev --reader dan.hertz` and `testHost`'s `reader: 'dan.hertz'` serve that person as `reader` with `self` true, holding `hertz`'s page; another page answers 403. In a project that declares `roles`, a reader `people` omits is refused, naming them: by `testHost`, by `dev` before it serves when `--reader` names them, and by a `dev` page read as them, the default `local` included; in one that declares none, such a reader is served with their username as `name`, no role and no pages, and is no member. `check` fails, naming the person, on a role not in `roles`, on a person with no role where the project declares roles, and on a person with a role where it declares none. Production ignores `people`.
|
|
49
58
|
|
|
50
59
|
## Routes
|
|
51
60
|
|
|
@@ -83,7 +92,7 @@ A project saves JSON documents through its host, as it reads data.
|
|
|
83
92
|
|
|
84
93
|
## Testing
|
|
85
94
|
|
|
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
|
|
95
|
+
`testHost(project, { data, private, documents, people, roles, reader, self, 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 dataset's JSON, `people` and `roles` as research.json's, `reader` the username (default `test`) and the writer of every save, `self` false to view the project as `reader` while signed in as another (default true; `testHost` holds the one identity, so saves are still `reader`'s), `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. `membersOf(research.people)` and `readerOf(research, username, self)` from `./testing` build the `members` and `reader` `testHost` serves, so a fixture reads them from research.json rather than copying them. `init` scaffolds `ui/index.test.tsx` on vitest, jsdom and Testing Library, which a project installs itself.
|
|
87
96
|
|
|
88
97
|
```tsx
|
|
89
98
|
render(
|
package/dist/commands/check.js
CHANGED
|
@@ -6,7 +6,7 @@ import { join, sep } from 'node:path';
|
|
|
6
6
|
import { parseArgs, stripVTControlCharacters } from 'node:util';
|
|
7
7
|
import { build, mergeConfig } from 'vite';
|
|
8
8
|
import { checkPapers } from '../papers.js';
|
|
9
|
-
import { load } from '../
|
|
9
|
+
import { load } from '../datasets.js';
|
|
10
10
|
import { findRoot } from '../root.js';
|
|
11
11
|
import { hostConfig } from '../vite-config.js';
|
|
12
12
|
const tsc = createRequire(import.meta.url).resolve('typescript/bin/tsc');
|
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 { pagesOf, readerCookie } from '../reader.js';
|
|
12
|
+
import { pagesOf, readerCookie, readerOf } 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';
|
|
@@ -51,7 +51,7 @@ export default async function dev(args) {
|
|
|
51
51
|
const { values } = parseArgs({
|
|
52
52
|
args,
|
|
53
53
|
options: {
|
|
54
|
-
reader: { type: 'string'
|
|
54
|
+
reader: { type: 'string' },
|
|
55
55
|
port: { type: 'string' },
|
|
56
56
|
},
|
|
57
57
|
});
|
|
@@ -69,9 +69,15 @@ export default async function dev(args) {
|
|
|
69
69
|
if (local)
|
|
70
70
|
console.log(`${url}local/`);
|
|
71
71
|
}
|
|
72
|
-
function devHost(root,
|
|
72
|
+
function devHost(root, named) {
|
|
73
|
+
const reader = named ?? 'local';
|
|
73
74
|
const real = realpathSync(root);
|
|
74
|
-
const { people } = project(root);
|
|
75
|
+
const { people, roles } = project(root);
|
|
76
|
+
// Refuses, before serving, a --reader the project's people cannot serve; the
|
|
77
|
+
// default is refused where a page reads as it, so contexts that each name a
|
|
78
|
+
// reader still start.
|
|
79
|
+
if (named !== undefined)
|
|
80
|
+
readerOf({ people, roles }, named, true);
|
|
75
81
|
const store = documentStore();
|
|
76
82
|
return {
|
|
77
83
|
name: 'research:dev',
|
|
@@ -92,7 +98,7 @@ function devHost(root, reader) {
|
|
|
92
98
|
`import { configurePdf } from '@metacensus/library-ui/pdf'`,
|
|
93
99
|
`import { start } from ${JSON.stringify(host)}`,
|
|
94
100
|
`configurePdf({ assets: ${JSON.stringify(pdfAssets)} })`,
|
|
95
|
-
`start(${JSON.stringify(reader)}, ${JSON.stringify(people)})`,
|
|
101
|
+
`start(${JSON.stringify(reader)}, ${JSON.stringify({ people, roles })})`,
|
|
96
102
|
].join('\n');
|
|
97
103
|
},
|
|
98
104
|
configureServer(server) {
|
|
@@ -119,7 +125,7 @@ function devHost(root, reader) {
|
|
|
119
125
|
if ([data, documents, privates].some((route) => route.test(path ?? '')) &&
|
|
120
126
|
named === '')
|
|
121
127
|
return reply(res, 401, { 'Content-Type': 'text/plain' }, 'signed out');
|
|
122
|
-
const [, page,
|
|
128
|
+
const [, page, dataset = ''] = path?.match(privates) ?? [];
|
|
123
129
|
if (documents.test(path ?? '') || page) {
|
|
124
130
|
// A cookie that does not decode names no reader.
|
|
125
131
|
let who = reader;
|
|
@@ -142,9 +148,9 @@ function devHost(root, reader) {
|
|
|
142
148
|
}
|
|
143
149
|
if (!pagesOf(people, who).includes(wanted))
|
|
144
150
|
return reply(res, 403, { 'Content-Type': 'text/plain' }, 'not yours');
|
|
145
|
-
const file = privateFile(root, wanted,
|
|
151
|
+
const file = privateFile(root, wanted, dataset);
|
|
146
152
|
if (!file)
|
|
147
|
-
return notFound(res, `project has no private
|
|
153
|
+
return notFound(res, `project has no private dataset "${dataset}" page "${page}"`);
|
|
148
154
|
return send(req, res, Buffer.from(JSON.stringify(read(root, file))), 'application/json');
|
|
149
155
|
}
|
|
150
156
|
const asset = path?.startsWith(pdfAssets) && path.slice(pdfAssets.length);
|
|
@@ -183,9 +189,9 @@ function devHost(root, reader) {
|
|
|
183
189
|
},
|
|
184
190
|
};
|
|
185
191
|
}
|
|
186
|
-
function privateFile(root, page,
|
|
192
|
+
function privateFile(root, page, dataset) {
|
|
187
193
|
try {
|
|
188
|
-
return project(root).pages[
|
|
194
|
+
return project(root).pages[dataset]?.()[page];
|
|
189
195
|
}
|
|
190
196
|
catch (error) {
|
|
191
197
|
if (error.message.endsWith(': matches no file'))
|
package/dist/data.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
/** Reads a project's research.json; each
|
|
2
|
+
/** Reads a project's research.json; each dataset reads its files when called. */
|
|
3
3
|
export declare function project(dir: string): {
|
|
4
4
|
title: string;
|
|
5
|
-
|
|
5
|
+
datasets: Record<string, () => unknown>;
|
|
6
6
|
pages: Record<string, () => Record<string, string>>;
|
|
7
|
-
people: Record<string,
|
|
7
|
+
people: Record<string, {
|
|
8
|
+
name: string;
|
|
9
|
+
role?: string | undefined;
|
|
10
|
+
pages?: string[] | undefined;
|
|
11
|
+
}> | undefined;
|
|
12
|
+
roles: string[] | undefined;
|
|
8
13
|
};
|
|
9
|
-
/** The files a private
|
|
10
|
-
export declare function privatePages(dir: string,
|
|
11
|
-
/** The files a
|
|
14
|
+
/** The files a private dataset's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
15
|
+
export declare function privatePages(dir: string, dataset: string, glob: string): Record<string, string>;
|
|
16
|
+
/** The files a dataset's glob matches; a wildcard keys each by what it matched. */
|
|
12
17
|
export declare function files(dir: string, glob: string): {
|
|
13
18
|
keyed: boolean;
|
|
14
19
|
entries: [file: string, key: string][];
|
package/dist/data.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
import { globSync, lstatSync, readFileSync, realpathSync } from 'node:fs';
|
|
2
2
|
import { join, sep } from 'node:path';
|
|
3
3
|
import { Corpus, Research } from './schema.js';
|
|
4
|
-
/** Reads a project's research.json; each
|
|
4
|
+
/** Reads a project's research.json; each dataset reads its files when called. */
|
|
5
5
|
export function project(dir) {
|
|
6
|
-
const { title, data, private: held = {}, people, } = read(dir, 'research.json', Research);
|
|
7
|
-
const
|
|
6
|
+
const { title, data, private: held = {}, people, roles, } = read(dir, 'research.json', Research);
|
|
7
|
+
const datasets = {
|
|
8
8
|
corpus: () => readCorpus(dir),
|
|
9
9
|
};
|
|
10
|
-
for (const [
|
|
11
|
-
|
|
10
|
+
for (const [dataset, glob] of Object.entries(data))
|
|
11
|
+
datasets[dataset] = () => match(dir, files(dir, glob));
|
|
12
12
|
const pages = {};
|
|
13
|
-
for (const [
|
|
14
|
-
pages[
|
|
15
|
-
return { title,
|
|
13
|
+
for (const [dataset, glob] of Object.entries(held))
|
|
14
|
+
pages[dataset] = () => privatePages(dir, dataset, glob);
|
|
15
|
+
return { title, datasets, pages, people, roles };
|
|
16
16
|
}
|
|
17
|
-
/** The files a private
|
|
18
|
-
export function privatePages(dir,
|
|
17
|
+
/** The files a private dataset's glob matches, by the page each names; throws on a key a page cannot have. */
|
|
18
|
+
export function privatePages(dir, dataset, glob) {
|
|
19
19
|
const { entries } = files(dir, glob);
|
|
20
20
|
const bad = entries.filter(([, key]) => !/^[a-z0-9][a-z0-9-]*$/.test(key));
|
|
21
21
|
if (bad.length)
|
|
22
22
|
throw new Error(bad
|
|
23
|
-
.map(([file, key]) => `${join(dir, file)}: private
|
|
23
|
+
.map(([file, key]) => `${join(dir, file)}: private dataset "${dataset}": page "${key}" is not [a-z0-9][a-z0-9-]*`)
|
|
24
24
|
.join('\n'));
|
|
25
25
|
const pages = {};
|
|
26
26
|
for (const [file, key] of entries) {
|
|
27
27
|
if (Object.hasOwn(pages, key))
|
|
28
|
-
throw new Error(`private
|
|
28
|
+
throw new Error(`private dataset "${dataset}": page "${key}" matched by ${pages[key]} and ${file}`);
|
|
29
29
|
pages[key] = file;
|
|
30
30
|
}
|
|
31
31
|
return pages;
|
|
32
32
|
}
|
|
33
|
-
/** The files a
|
|
33
|
+
/** The files a dataset's glob matches; a wildcard keys each by what it matched. */
|
|
34
34
|
export function files(dir, glob) {
|
|
35
35
|
const found = globSync(glob, { cwd: dir }).sort();
|
|
36
36
|
if (!found.length)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads each dataset of the project in `dir`, the folder holding
|
|
3
|
+
* research.json, parsed by its schema in ui/datasets.ts, and its corpus. A
|
|
4
|
+
* private dataset is read as `{ page: parsed }`, its schema parsing one page's
|
|
5
|
+
* file. Throws one line per failure.
|
|
6
|
+
*/
|
|
7
|
+
export declare function load(dir: string): Promise<Record<string, unknown>>;
|
|
@@ -3,8 +3,8 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
import { files, match, privatePages, read, readCorpus } from './data.js';
|
|
5
5
|
import { Research } from './schema.js';
|
|
6
|
-
const schemaFile = 'ui/
|
|
7
|
-
/** The `
|
|
6
|
+
const schemaFile = 'ui/datasets.ts';
|
|
7
|
+
/** The `datasets` ui/datasets.ts exports, which Node loads by stripping its types. */
|
|
8
8
|
async function schemas(dir) {
|
|
9
9
|
const path = join(dir, schemaFile);
|
|
10
10
|
if (!lstatSync(path, { throwIfNoEntry: false }))
|
|
@@ -13,22 +13,21 @@ async function schemas(dir) {
|
|
|
13
13
|
const { type } = JSON.parse(readFileSync(manifest, 'utf8'));
|
|
14
14
|
if (type !== 'module')
|
|
15
15
|
throw new Error(`${manifest}: needs "type": "module" for ${schemaFile}`);
|
|
16
|
-
let
|
|
16
|
+
let datasets;
|
|
17
17
|
try {
|
|
18
|
-
|
|
19
|
-
.roles;
|
|
18
|
+
datasets = (await import(pathToFileURL(path).href)).datasets;
|
|
20
19
|
}
|
|
21
20
|
catch (error) {
|
|
22
21
|
throw new Error(`${path}: ${error.message.split('\n')[0]}`);
|
|
23
22
|
}
|
|
24
|
-
if (typeof
|
|
25
|
-
throw new Error(`${path}: exports no
|
|
26
|
-
return
|
|
23
|
+
if (typeof datasets !== 'object' || !datasets)
|
|
24
|
+
throw new Error(`${path}: exports no datasets object`);
|
|
25
|
+
return datasets;
|
|
27
26
|
}
|
|
28
27
|
/**
|
|
29
|
-
* Reads each
|
|
30
|
-
* research.json, parsed by its schema in ui/
|
|
31
|
-
* private
|
|
28
|
+
* Reads each dataset of the project in `dir`, the folder holding
|
|
29
|
+
* research.json, parsed by its schema in ui/datasets.ts, and its corpus. A
|
|
30
|
+
* private dataset is read as `{ page: parsed }`, its schema parsing one page's
|
|
32
31
|
* file. Throws one line per failure.
|
|
33
32
|
*/
|
|
34
33
|
export async function load(dir) {
|
|
@@ -36,21 +35,21 @@ export async function load(dir) {
|
|
|
36
35
|
const found = await schemas(dir);
|
|
37
36
|
const loaded = {};
|
|
38
37
|
const failures = [];
|
|
39
|
-
for (const
|
|
40
|
-
if (!Object.hasOwn(data,
|
|
41
|
-
failures.push(`${join(dir, schemaFile)}: schema for "${
|
|
42
|
-
for (const [
|
|
38
|
+
for (const dataset of Object.keys(found))
|
|
39
|
+
if (!Object.hasOwn(data, dataset) && !Object.hasOwn(held, dataset))
|
|
40
|
+
failures.push(`${join(dir, schemaFile)}: schema for "${dataset}", which research.json does not name`);
|
|
41
|
+
for (const [dataset, glob] of Object.entries(data)) {
|
|
43
42
|
try {
|
|
44
43
|
const matched = files(dir, glob);
|
|
45
44
|
const value = match(dir, matched);
|
|
46
|
-
const schema = Object.hasOwn(found,
|
|
45
|
+
const schema = Object.hasOwn(found, dataset) ? found[dataset] : undefined;
|
|
47
46
|
if (typeof schema?.safeParse !== 'function') {
|
|
48
|
-
failures.push(`${
|
|
47
|
+
failures.push(`${dataset}: no schema in ${schemaFile}`);
|
|
49
48
|
continue;
|
|
50
49
|
}
|
|
51
50
|
const parsed = schema.safeParse(value);
|
|
52
51
|
if (parsed.success)
|
|
53
|
-
loaded[
|
|
52
|
+
loaded[dataset] = parsed.data;
|
|
54
53
|
else {
|
|
55
54
|
const byPlace = new Map();
|
|
56
55
|
for (const { path, message } of parsed.error.issues) {
|
|
@@ -65,33 +64,33 @@ export async function load(dir) {
|
|
|
65
64
|
]);
|
|
66
65
|
}
|
|
67
66
|
for (const [place, issues] of byPlace)
|
|
68
|
-
failures.push(`${place}: ${
|
|
67
|
+
failures.push(`${place}: ${dataset}: ${issues.join('; ')}`);
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
catch (error) {
|
|
72
71
|
failures.push(error.message);
|
|
73
72
|
}
|
|
74
73
|
}
|
|
75
|
-
for (const [
|
|
74
|
+
for (const [dataset, glob] of Object.entries(held)) {
|
|
76
75
|
try {
|
|
77
|
-
const schema = Object.hasOwn(found,
|
|
76
|
+
const schema = Object.hasOwn(found, dataset) ? found[dataset] : undefined;
|
|
78
77
|
if (typeof schema?.safeParse !== 'function') {
|
|
79
|
-
failures.push(`${
|
|
78
|
+
failures.push(`${dataset}: no schema in ${schemaFile}`);
|
|
80
79
|
continue;
|
|
81
80
|
}
|
|
82
81
|
const pages = {};
|
|
83
|
-
for (const [page, file] of Object.entries(privatePages(dir,
|
|
82
|
+
for (const [page, file] of Object.entries(privatePages(dir, dataset, glob))) {
|
|
84
83
|
const parsed = schema.safeParse(read(dir, file));
|
|
85
84
|
if (parsed.success)
|
|
86
85
|
pages[page] = parsed.data;
|
|
87
86
|
else
|
|
88
|
-
failures.push(`${join(dir, file)}: ${
|
|
87
|
+
failures.push(`${join(dir, file)}: ${dataset}: ${parsed
|
|
89
88
|
.error.issues.map(({ path, message }) => path.length
|
|
90
89
|
? `${path.map(String).join('.')}: ${message}`
|
|
91
90
|
: message)
|
|
92
91
|
.join('; ')}`);
|
|
93
92
|
}
|
|
94
|
-
loaded[
|
|
93
|
+
loaded[dataset] = pages;
|
|
95
94
|
}
|
|
96
95
|
catch (error) {
|
|
97
96
|
failures.push(error.message);
|
package/dist/dev/host.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { type People } from '../reader.js';
|
|
2
|
-
|
|
2
|
+
/** research.json's `people` and `roles`. */
|
|
3
|
+
type Cast = {
|
|
4
|
+
people?: People;
|
|
5
|
+
roles?: string[];
|
|
6
|
+
};
|
|
7
|
+
export declare function start(username: string, cast: Cast): Promise<void>;
|
|
8
|
+
export {};
|
package/dist/dev/host.js
CHANGED
|
@@ -5,34 +5,36 @@ 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 { pagesOf, readerCookie } from '../reader.js';
|
|
8
|
+
import { membersOf, pagesOf, readerCookie, readerOf, } from '../reader.js';
|
|
9
9
|
import { projectRouter } from '../router.js';
|
|
10
10
|
const project = projects.project;
|
|
11
|
-
const Reader = createContext({ username: '', lost: () => { } });
|
|
11
|
+
const Reader = createContext({ username: '', cast: {}, lost: () => { } });
|
|
12
12
|
function useHost() {
|
|
13
|
-
const { username,
|
|
13
|
+
const { username, cast, lost } = useContext(Reader);
|
|
14
14
|
return useMemo(() => ({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
// A signed-out context is no one, refused by its first request's 401.
|
|
16
|
+
reader: readerOf(username ? cast : {}, username, true),
|
|
17
|
+
members: membersOf(cast.people),
|
|
18
|
+
data: (dataset) => `/projects/project/data/${dataset}.json`,
|
|
19
|
+
private: (dataset) => Object.fromEntries(pagesOf(cast.people, username).map((page) => [
|
|
18
20
|
page,
|
|
19
|
-
`/projects/project/private/${encodeURIComponent(page)}/${
|
|
21
|
+
`/projects/project/private/${encodeURIComponent(page)}/${dataset}.json`,
|
|
20
22
|
])),
|
|
21
23
|
paper: (sha) => `/projects/project/papers/${sha}`,
|
|
22
24
|
document: (id) => `/projects/project/documents/${id}`,
|
|
23
25
|
documents: (prefix) => `/projects/project/documents?prefix=${prefix}`,
|
|
24
26
|
sessionLost: lost,
|
|
25
|
-
}), [username,
|
|
27
|
+
}), [username, cast, lost]);
|
|
26
28
|
}
|
|
27
|
-
function App({ username,
|
|
29
|
+
function App({ username, cast, router, }) {
|
|
28
30
|
const [lost, setLost] = useState(false);
|
|
29
|
-
const value = useMemo(() => ({ username,
|
|
31
|
+
const value = useMemo(() => ({ username, cast, lost: () => setLost(true) }), [username, cast]);
|
|
30
32
|
if (lost)
|
|
31
33
|
return (_jsx(Notice, { role: "alert", title: "Your session ended", children: _jsx(Button, { onClick: () => window.location.reload(), children: "Reload" }) }));
|
|
32
34
|
return (_jsx(Reader.Provider, { value: value, children: _jsx(RouterProvider, { router: router }) }));
|
|
33
35
|
}
|
|
34
36
|
/** The reader the browser context names by cookie, else `username`. */
|
|
35
|
-
function
|
|
37
|
+
function readerName(username) {
|
|
36
38
|
const named = document.cookie
|
|
37
39
|
.split('; ')
|
|
38
40
|
.find((cookie) => cookie.startsWith(`${readerCookie}=`));
|
|
@@ -40,7 +42,7 @@ function readerOf(username) {
|
|
|
40
42
|
? username
|
|
41
43
|
: decodeURIComponent(named.slice(readerCookie.length + 1));
|
|
42
44
|
}
|
|
43
|
-
export async function start(username,
|
|
45
|
+
export async function start(username, cast) {
|
|
44
46
|
document.title = project.title;
|
|
45
47
|
const local = window.location.pathname.startsWith('/local/');
|
|
46
48
|
const { default: shown } = await (local ? project.local : project.root)();
|
|
@@ -53,5 +55,5 @@ export async function start(username, people) {
|
|
|
53
55
|
if (window.location.pathname === `${base}/`)
|
|
54
56
|
window.history.replaceState(null, '', `${base}/${shown.path}/`);
|
|
55
57
|
const router = projectRouter(shown, useHost, undefined, base || undefined);
|
|
56
|
-
createRoot(document.getElementById('root')).render(_jsx(QueryClientProvider, { client: new QueryClient(), children: _jsx(App, { username:
|
|
58
|
+
createRoot(document.getElementById('root')).render(_jsx(QueryClientProvider, { client: new QueryClient(), children: _jsx(App, { username: readerName(username), cast: cast, router: router }) }));
|
|
57
59
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { AnyProject, Corpus, Doc, Host, Kind, Paper, Mount, Project, Reader, RouterOf, } from './types.js';
|
|
1
|
+
export type { AnyProject, Corpus, Doc, Host, Kind, Member, Paper, Mount, Project, Reader, RouterOf, } from './types.js';
|
|
2
2
|
export { defineProject } from './mount.js';
|
|
3
|
-
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, usePrivate, useSaveDocument, } from './react.js';
|
|
3
|
+
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, useMembers, usePrivate, useReader, useSaveDocument, } from './react.js';
|
|
4
4
|
export { DocumentError } from './documents.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { defineProject } from './mount.js';
|
|
2
|
-
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, usePrivate, useSaveDocument, } from './react.js';
|
|
2
|
+
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, useMembers, usePrivate, useReader, useSaveDocument, } from './react.js';
|
|
3
3
|
export { DocumentError } from './documents.js';
|
package/dist/react.d.ts
CHANGED
|
@@ -1,31 +1,38 @@
|
|
|
1
1
|
import { DocumentError } from './documents.js';
|
|
2
|
-
import type { Corpus, Doc, Host } from './types.js';
|
|
2
|
+
import type { Corpus, Doc, Host, Member, Reader } from './types.js';
|
|
3
3
|
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
4
4
|
export declare function useHost(): Host;
|
|
5
|
-
|
|
5
|
+
/** The person the project is viewed as; `self` is false while the host views it as another. */
|
|
6
|
+
export declare function useReader(): Reader;
|
|
7
|
+
/** Every person of the project, each with their role. */
|
|
8
|
+
export declare function useMembers(): Member[];
|
|
9
|
+
type Dataset<T = unknown> = readonly [
|
|
10
|
+
dataset: string,
|
|
11
|
+
parse: (json: unknown) => T
|
|
12
|
+
];
|
|
6
13
|
/**
|
|
7
|
-
* One
|
|
8
|
-
* fetch or parse throws to the frame's error notice, naming the
|
|
14
|
+
* One dataset's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
15
|
+
* fetch or parse throws to the frame's error notice, naming the dataset. A 401 or
|
|
9
16
|
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
10
|
-
*
|
|
17
|
+
* dataset list, not on every render.
|
|
11
18
|
*/
|
|
12
|
-
export declare function useData<T>(
|
|
19
|
+
export declare function useData<T>(dataset: string, parse: (json: unknown) => T): T;
|
|
13
20
|
/**
|
|
14
|
-
* `useData` for several
|
|
21
|
+
* `useData` for several datasets at once, fetched in parallel: a `[dataset, parse]`
|
|
15
22
|
* 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
|
|
23
|
+
* Each parser must be pure: it is read once per dataset list, not on every render.
|
|
17
24
|
*/
|
|
18
|
-
export declare function useDatas<const R extends readonly
|
|
19
|
-
-readonly [K in keyof R]: R[K] extends
|
|
25
|
+
export declare function useDatas<const R extends readonly Dataset[]>(datasets: R): {
|
|
26
|
+
-readonly [K in keyof R]: R[K] extends Dataset<infer T> ? T : never;
|
|
20
27
|
};
|
|
21
28
|
/**
|
|
22
|
-
* The viewed person's pages of a private
|
|
29
|
+
* The viewed person's pages of a private dataset, as `{ page: parsed }`: empty
|
|
23
30
|
* when they hold none, a page the host names but the build did not emit left
|
|
24
31
|
* out. `parse` runs on each page's JSON and must be pure. Its cache entry,
|
|
25
32
|
* `{ json } | null`, is not `useData`'s raw JSON, under the same key prefix.
|
|
26
33
|
*/
|
|
27
|
-
export declare function usePrivate<T>(
|
|
28
|
-
/** The `corpus`
|
|
34
|
+
export declare function usePrivate<T>(dataset: string, parse: (json: unknown) => T): Record<string, T>;
|
|
35
|
+
/** The `corpus` dataset, parsed by the library's own schema, through `useData`. */
|
|
29
36
|
export declare function useCorpus(): Corpus;
|
|
30
37
|
/**
|
|
31
38
|
* One document of the project, or `null` when none has that id. Suspends while
|
package/dist/react.js
CHANGED
|
@@ -10,40 +10,48 @@ export function useHost() {
|
|
|
10
10
|
throw new Error('useHost outside the frame of ./host');
|
|
11
11
|
return host;
|
|
12
12
|
}
|
|
13
|
+
/** The person the project is viewed as; `self` is false while the host views it as another. */
|
|
14
|
+
export function useReader() {
|
|
15
|
+
return useHost().reader;
|
|
16
|
+
}
|
|
17
|
+
/** Every person of the project, each with their role. */
|
|
18
|
+
export function useMembers() {
|
|
19
|
+
return useHost().members;
|
|
20
|
+
}
|
|
13
21
|
/**
|
|
14
|
-
* One
|
|
15
|
-
* fetch or parse throws to the frame's error notice, naming the
|
|
22
|
+
* One dataset's JSON, parsed by `parse`. Suspends while it loads; a failed
|
|
23
|
+
* fetch or parse throws to the frame's error notice, naming the dataset. A 401 or
|
|
16
24
|
* 403 also calls `Host.sessionLost`. `parse` must be pure: it is read once per
|
|
17
|
-
*
|
|
25
|
+
* dataset list, not on every render.
|
|
18
26
|
*/
|
|
19
|
-
export function useData(
|
|
20
|
-
return useDatas([[
|
|
27
|
+
export function useData(dataset, parse) {
|
|
28
|
+
return useDatas([[dataset, parse]])[0];
|
|
21
29
|
}
|
|
22
30
|
/**
|
|
23
|
-
* `useData` for several
|
|
31
|
+
* `useData` for several datasets at once, fetched in parallel: a `[dataset, parse]`
|
|
24
32
|
* 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
|
|
33
|
+
* Each parser must be pure: it is read once per dataset list, not on every render.
|
|
26
34
|
*/
|
|
27
|
-
export function useDatas(
|
|
35
|
+
export function useDatas(datasets) {
|
|
28
36
|
const host = useHost();
|
|
29
|
-
// The parsers and select functions are kept until the
|
|
37
|
+
// The parsers and select functions are kept until the dataset list changes, so
|
|
30
38
|
// an inline parse does not rerun on every render.
|
|
31
|
-
const key = JSON.stringify(
|
|
32
|
-
const [held, hold] = useState({ key,
|
|
39
|
+
const key = JSON.stringify(datasets.map(([dataset]) => dataset));
|
|
40
|
+
const [held, hold] = useState({ key, datasets });
|
|
33
41
|
if (held.key !== key)
|
|
34
|
-
hold({ key,
|
|
35
|
-
const names = held.
|
|
36
|
-
const selects = useMemo(() => held.
|
|
42
|
+
hold({ key, datasets });
|
|
43
|
+
const names = held.datasets.map(([dataset]) => dataset);
|
|
44
|
+
const selects = useMemo(() => held.datasets.map(([dataset, parse]) => (json) => {
|
|
37
45
|
try {
|
|
38
46
|
return parse(json);
|
|
39
47
|
}
|
|
40
48
|
catch (cause) {
|
|
41
|
-
return failed(`
|
|
49
|
+
return failed(`dataset "${dataset}"`, cause);
|
|
42
50
|
}
|
|
43
51
|
}), [held]);
|
|
44
52
|
return useSuspenseQueries({
|
|
45
|
-
queries: names.map((
|
|
46
|
-
const url = host.data(
|
|
53
|
+
queries: names.map((dataset, i) => {
|
|
54
|
+
const url = host.data(dataset);
|
|
47
55
|
return {
|
|
48
56
|
queryKey: ['research', url],
|
|
49
57
|
queryFn: () => fetch(url)
|
|
@@ -54,7 +62,7 @@ export function useDatas(roles) {
|
|
|
54
62
|
throw new Error(`${response.status} from ${url}`);
|
|
55
63
|
return response.json();
|
|
56
64
|
})
|
|
57
|
-
.catch((cause) => failed(`
|
|
65
|
+
.catch((cause) => failed(`dataset "${dataset}"`, cause)),
|
|
58
66
|
select: selects[i],
|
|
59
67
|
staleTime: Infinity,
|
|
60
68
|
retry: false,
|
|
@@ -70,17 +78,17 @@ function failed(what, cause) {
|
|
|
70
78
|
throw new Error(`${what}: ${why}`, { cause });
|
|
71
79
|
}
|
|
72
80
|
/**
|
|
73
|
-
* The viewed person's pages of a private
|
|
81
|
+
* The viewed person's pages of a private dataset, as `{ page: parsed }`: empty
|
|
74
82
|
* when they hold none, a page the host names but the build did not emit left
|
|
75
83
|
* out. `parse` runs on each page's JSON and must be pure. Its cache entry,
|
|
76
84
|
* `{ json } | null`, is not `useData`'s raw JSON, under the same key prefix.
|
|
77
85
|
*/
|
|
78
|
-
export function usePrivate(
|
|
86
|
+
export function usePrivate(dataset, parse) {
|
|
79
87
|
const host = useHost();
|
|
80
|
-
const urls = host.private(
|
|
88
|
+
const urls = host.private(dataset);
|
|
81
89
|
const pages = Object.keys(urls);
|
|
82
90
|
const key = pages.join();
|
|
83
|
-
const held = useHeld(
|
|
91
|
+
const held = useHeld(dataset, parse);
|
|
84
92
|
const selects = useMemo(() => pages.map((page) => (found) => {
|
|
85
93
|
if (!found)
|
|
86
94
|
return null;
|
|
@@ -88,7 +96,7 @@ export function usePrivate(role, parse) {
|
|
|
88
96
|
return { value: held(found.json) };
|
|
89
97
|
}
|
|
90
98
|
catch (cause) {
|
|
91
|
-
return failed(`private
|
|
99
|
+
return failed(`private dataset "${dataset}" page "${page}"`, cause);
|
|
92
100
|
}
|
|
93
101
|
}),
|
|
94
102
|
// The pages are named by `key`.
|
|
@@ -109,7 +117,7 @@ export function usePrivate(role, parse) {
|
|
|
109
117
|
throw new Error(`${response.status} from ${url}`);
|
|
110
118
|
return response.json().then((json) => ({ json }));
|
|
111
119
|
})
|
|
112
|
-
.catch((cause) => failed(`private
|
|
120
|
+
.catch((cause) => failed(`private dataset "${dataset}" page "${page}"`, cause)),
|
|
113
121
|
select: selects[i],
|
|
114
122
|
staleTime: Infinity,
|
|
115
123
|
retry: false,
|
|
@@ -118,7 +126,7 @@ export function usePrivate(role, parse) {
|
|
|
118
126
|
combine: (results) => Object.fromEntries(results.flatMap(({ data }, i) => data ? [[pages[i], data.value]] : [])),
|
|
119
127
|
});
|
|
120
128
|
}
|
|
121
|
-
/** The `corpus`
|
|
129
|
+
/** The `corpus` dataset, parsed by the library's own schema, through `useData`. */
|
|
122
130
|
export function useCorpus() {
|
|
123
131
|
return useData('corpus', (json) => schema.Corpus.parse(json));
|
|
124
132
|
}
|
package/dist/reader.d.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { Research } from './schema.js';
|
|
3
|
+
import type { Member, Reader } from './types.js';
|
|
1
4
|
/** The cookie that names the reader of a browser context on a dev server. */
|
|
2
5
|
export declare const readerCookie = "library-research-reader";
|
|
3
|
-
/**
|
|
4
|
-
export type People =
|
|
5
|
-
/**
|
|
6
|
-
|
|
6
|
+
/** research.json's `people`: each person `dev` and `testHost` serve, by username. */
|
|
7
|
+
export type People = NonNullable<z.infer<typeof Research>['people']>;
|
|
8
|
+
/**
|
|
9
|
+
* `username` as `dev` and `testHost` serve them. In a project that declares
|
|
10
|
+
* `roles`, one `people` omits is refused, naming them; elsewhere they are named
|
|
11
|
+
* by their username and hold no role.
|
|
12
|
+
*/
|
|
13
|
+
export declare function readerOf({ people, roles }: {
|
|
14
|
+
people?: People;
|
|
15
|
+
roles?: string[];
|
|
16
|
+
}, username: string, self: boolean): Reader;
|
|
17
|
+
/** Every person of `people`, in its order. */
|
|
18
|
+
export declare const membersOf: (people?: People) => Member[];
|
|
19
|
+
/** The pages `username` holds in `dev` and `testHost`: their `pages` in `people`, else none. */
|
|
20
|
+
export declare const pagesOf: (people: People | undefined, username: string) => string[];
|
package/dist/reader.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
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
|
-
|
|
4
|
-
|
|
3
|
+
const personOf = (people, username) => people && Object.hasOwn(people, username) ? people[username] : undefined;
|
|
4
|
+
/**
|
|
5
|
+
* `username` as `dev` and `testHost` serve them. In a project that declares
|
|
6
|
+
* `roles`, one `people` omits is refused, naming them; elsewhere they are named
|
|
7
|
+
* by their username and hold no role.
|
|
8
|
+
*/
|
|
9
|
+
export function readerOf({ people, roles }, username, self) {
|
|
10
|
+
const person = personOf(people, username);
|
|
11
|
+
if (!person && roles)
|
|
12
|
+
throw new Error(`reader "${username}" is not in research.json's people, which lists every reader of a project that declares roles`);
|
|
13
|
+
return {
|
|
14
|
+
username,
|
|
15
|
+
name: person?.name ?? username,
|
|
16
|
+
role: person?.role ?? null,
|
|
17
|
+
self,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** Every person of `people`, in its order. */
|
|
21
|
+
export const membersOf = (people = {}) => Object.entries(people).map(([username, { name, role = null }]) => ({
|
|
22
|
+
username,
|
|
23
|
+
name,
|
|
24
|
+
role,
|
|
25
|
+
}));
|
|
26
|
+
/** The pages `username` holds in `dev` and `testHost`: their `pages` in `people`, else none. */
|
|
27
|
+
export const pagesOf = (people, username) => personOf(people, username)?.pages ?? [];
|
package/dist/schema.d.ts
CHANGED
|
@@ -17,7 +17,12 @@ 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
|
-
|
|
20
|
+
roles: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
21
|
+
people: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
22
|
+
name: z.ZodString;
|
|
23
|
+
role: z.ZodOptional<z.ZodString>;
|
|
24
|
+
pages: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
25
|
+
}, z.core.$strict>>>;
|
|
21
26
|
}, z.core.$strict>;
|
|
22
27
|
export declare const Corpus: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
23
28
|
kind: z.ZodEnum<{
|
package/dist/schema.js
CHANGED
|
@@ -14,25 +14,52 @@ export const kinds = {
|
|
|
14
14
|
html: 'text/plain',
|
|
15
15
|
xml: 'text/plain',
|
|
16
16
|
};
|
|
17
|
-
const
|
|
17
|
+
const dataset = z.string().regex(/^(?!corpus$)[a-z][A-Za-z0-9]*$/);
|
|
18
18
|
const glob = z.string().regex(/^(?!\/)(?!.*\.\.).+$/);
|
|
19
|
+
const role = z.string().regex(/^[a-z][a-z0-9-]*$/);
|
|
20
|
+
const page = z.string().regex(/^[a-z0-9][a-z0-9-]*$/);
|
|
19
21
|
export const Research = z
|
|
20
22
|
.strictObject({
|
|
21
23
|
title: z.string().min(1),
|
|
22
|
-
data: z.record(
|
|
23
|
-
private: z.record(
|
|
24
|
-
/**
|
|
24
|
+
data: z.record(dataset, glob),
|
|
25
|
+
private: z.record(dataset, glob).optional(),
|
|
26
|
+
/** The roles a person of the project holds one of. */
|
|
27
|
+
roles: z
|
|
28
|
+
.array(role)
|
|
29
|
+
.refine((roles) => new Set(roles).size === roles.length, 'roles repeat')
|
|
30
|
+
.meta({ uniqueItems: true })
|
|
31
|
+
.optional(),
|
|
32
|
+
/** The people `dev` and `testHost` serve, by username; production's people.json holds the real ones. */
|
|
25
33
|
people: z
|
|
26
|
-
.record(z.string(), z.
|
|
34
|
+
.record(z.string(), z.strictObject({
|
|
35
|
+
name: z.string().min(1),
|
|
36
|
+
role: z.string().optional(),
|
|
37
|
+
pages: z.array(page).optional(),
|
|
38
|
+
}))
|
|
27
39
|
.optional(),
|
|
28
40
|
})
|
|
29
|
-
.superRefine(({ data, private: held = {} }, ctx) => {
|
|
41
|
+
.superRefine(({ data, private: held = {}, roles, people = {} }, ctx) => {
|
|
42
|
+
for (const [username, person] of Object.entries(people)) {
|
|
43
|
+
const issue = (message) => ctx.addIssue({
|
|
44
|
+
code: 'custom',
|
|
45
|
+
path: ['people', username, 'role'],
|
|
46
|
+
message: `${username} ${message}`,
|
|
47
|
+
});
|
|
48
|
+
if (!roles) {
|
|
49
|
+
if (person.role !== undefined)
|
|
50
|
+
issue('has a role, and the project declares no roles');
|
|
51
|
+
}
|
|
52
|
+
else if (person.role === undefined)
|
|
53
|
+
issue('has no role, and the project declares roles');
|
|
54
|
+
else if (!roles.includes(person.role))
|
|
55
|
+
issue(`has role "${person.role}", which is not in roles`);
|
|
56
|
+
}
|
|
30
57
|
for (const [name, pattern] of Object.entries(held)) {
|
|
31
58
|
const issue = (message) => ctx.addIssue({ code: 'custom', path: ['private', name], message });
|
|
32
59
|
if (Object.hasOwn(data, name))
|
|
33
|
-
issue(`
|
|
60
|
+
issue(`dataset "${name}" is in data and private`);
|
|
34
61
|
if (pattern.split('*').length !== 2 || /[?[\]{}()!]/.test(pattern))
|
|
35
|
-
issue(`
|
|
62
|
+
issue(`dataset "${name}": glob "${pattern}" must hold exactly one * and no other glob character`);
|
|
36
63
|
}
|
|
37
64
|
});
|
|
38
65
|
export const Corpus = z.record(z.string().regex(/^[0-9a-f]{64}$/), z.strictObject({
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
2
|
import { type People } from './reader.js';
|
|
3
3
|
import type { AnyProject } from './mount.js';
|
|
4
|
+
export { membersOf, readerOf, type People } from './reader.js';
|
|
4
5
|
/** What `testHost` is given besides the project; every field is optional. */
|
|
5
6
|
export interface TestHostOptions {
|
|
6
|
-
/** Each
|
|
7
|
+
/** Each dataset's JSON value, parsed by the project's own schema. */
|
|
7
8
|
data?: Record<string, unknown>;
|
|
8
|
-
/** Each private
|
|
9
|
+
/** Each private dataset's pages, `{ dataset: { page: JSON } }`; the reader sees those `people` gives them. */
|
|
9
10
|
private?: Record<string, Record<string, unknown>>;
|
|
10
11
|
/** Each document the project starts with, by id; `writer` defaults to the reader. */
|
|
11
12
|
documents?: Record<string, {
|
|
12
13
|
body: unknown;
|
|
13
14
|
writer?: string;
|
|
14
15
|
}>;
|
|
15
|
-
/**
|
|
16
|
+
/** The project's people, as research.json's `people`: each is a member, and `reader` holds their `pages`. */
|
|
16
17
|
people?: People;
|
|
17
|
-
/** The
|
|
18
|
+
/** The project's roles, as research.json's `roles`; with them, `reader` must be in `people`. */
|
|
19
|
+
roles?: string[];
|
|
20
|
+
/** The reader's username, and the writer of every save: `testHost` holds this one identity. */
|
|
18
21
|
reader?: string;
|
|
22
|
+
/** False to view the project as `reader` while signed in as another; saves are still written as `reader`. */
|
|
23
|
+
self?: boolean;
|
|
19
24
|
/** The path below the project, with search and hash. */
|
|
20
25
|
url?: string;
|
|
21
26
|
/** Called as `Host.sessionLost` is. */
|
|
@@ -26,7 +31,7 @@ export interface TestHostOptions {
|
|
|
26
31
|
* path it names. Render `element` with any DOM renderer; `router` says where
|
|
27
32
|
* the project navigated, and `documents()` what it saved, sorted by id.
|
|
28
33
|
*/
|
|
29
|
-
export declare function testHost(project: AnyProject, { data, documents, private: held, people, reader, url, sessionLost, }?: TestHostOptions): {
|
|
34
|
+
export declare function testHost(project: AnyProject, { data, documents, private: held, people, roles, reader, self, url, sessionLost, }?: TestHostOptions): {
|
|
30
35
|
element: ReactElement<any, string | import("react").JSXElementConstructor<any>>;
|
|
31
36
|
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>>;
|
|
32
37
|
documents: () => import("./types.js").Doc<unknown>[];
|
package/dist/testing.js
CHANGED
|
@@ -3,29 +3,31 @@ 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
|
+
import { membersOf, pagesOf, readerOf } from './reader.js';
|
|
7
7
|
import { projectRouter } from './router.js';
|
|
8
|
+
export { membersOf, readerOf } from './reader.js';
|
|
8
9
|
const origin = 'http://test.invalid';
|
|
9
10
|
/**
|
|
10
11
|
* A project rendered through the frame and the mount a real host uses, at the
|
|
11
12
|
* path it names. Render `element` with any DOM renderer; `router` says where
|
|
12
13
|
* the project navigated, and `documents()` what it saved, sorted by id.
|
|
13
14
|
*/
|
|
14
|
-
export function testHost(project, { data = {}, documents = {}, private: held = {}, people, reader = 'test', url = '/', sessionLost = () => { }, } = {}) {
|
|
15
|
+
export function testHost(project, { data = {}, documents = {}, private: held = {}, people, roles, reader = 'test', self = true, url = '/', sessionLost = () => { }, } = {}) {
|
|
15
16
|
const store = documentStore(documents, reader);
|
|
16
17
|
const memory = (input, init) => store.handle(new Request(new URL(input instanceof Request ? input.url : input, origin), init), reader);
|
|
17
18
|
const host = {
|
|
18
|
-
reader: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
reader: readerOf({ people, roles }, reader, self),
|
|
20
|
+
members: membersOf(people),
|
|
21
|
+
data(dataset) {
|
|
22
|
+
if (!Object.hasOwn(data, dataset) || data[dataset] === undefined)
|
|
23
|
+
throw new Error(`testHost: no data for dataset "${dataset}"`);
|
|
24
|
+
return `data:application/json,${encodeURIComponent(JSON.stringify(data[dataset]))}`;
|
|
23
25
|
},
|
|
24
|
-
private: (
|
|
25
|
-
.filter((page) => Object.hasOwn(held[
|
|
26
|
+
private: (dataset) => Object.fromEntries(pagesOf(people, reader)
|
|
27
|
+
.filter((page) => Object.hasOwn(held[dataset] ?? {}, page))
|
|
26
28
|
.map((page) => [
|
|
27
29
|
page,
|
|
28
|
-
`data:application/json,${encodeURIComponent(JSON.stringify(held[
|
|
30
|
+
`data:application/json,${encodeURIComponent(JSON.stringify(held[dataset][page]))}`,
|
|
29
31
|
])),
|
|
30
32
|
paper: (sha) => `/papers/${sha}`,
|
|
31
33
|
document: (id) => `${origin}/documents/${id}`,
|
package/dist/types.d.ts
CHANGED
|
@@ -8,10 +8,12 @@ import type { Corpus as CorpusSchema, kinds } from './schema.js';
|
|
|
8
8
|
export interface Host {
|
|
9
9
|
/** The person the project is viewed as, who may differ from the person signed in. */
|
|
10
10
|
reader: Reader;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
/** The
|
|
14
|
-
|
|
11
|
+
/** Every person of the project; `useMembers` returns it. */
|
|
12
|
+
members: Member[];
|
|
13
|
+
/** The URL of a dataset's JSON, `dataset` being one the project declares; `useData` fetches it. */
|
|
14
|
+
data(dataset: string): string;
|
|
15
|
+
/** The viewed person's pages of a private dataset: page key to URL; empty when they hold none. */
|
|
16
|
+
private(dataset: string): Record<string, string>;
|
|
15
17
|
/** The URL of the paper stored under `sha`, as the corpus names it. */
|
|
16
18
|
paper(sha: string): string;
|
|
17
19
|
/**
|
|
@@ -34,14 +36,23 @@ export interface Doc<T> {
|
|
|
34
36
|
revision: number;
|
|
35
37
|
body: T;
|
|
36
38
|
}
|
|
37
|
-
/**
|
|
38
|
-
export interface
|
|
39
|
+
/** A person of the project. */
|
|
40
|
+
export interface Member {
|
|
39
41
|
/** The name the host signs them in as. */
|
|
40
42
|
username: string;
|
|
43
|
+
/** The name a person reads. */
|
|
44
|
+
name: string;
|
|
45
|
+
/** One of research.json's `roles`; null only in a project that declares none. */
|
|
46
|
+
role: string | null;
|
|
47
|
+
}
|
|
48
|
+
/** The person a project is viewed as; `useReader` returns them. */
|
|
49
|
+
export interface Reader extends Member {
|
|
50
|
+
/** False while the host views the project as another person than the one signed in. */
|
|
51
|
+
self: boolean;
|
|
41
52
|
}
|
|
42
53
|
/** A kind of record the schema allows, one of `kinds`' keys. */
|
|
43
54
|
export type Kind = keyof typeof kinds;
|
|
44
|
-
/** The parsed `corpus`
|
|
55
|
+
/** The parsed `corpus` dataset: every paper by sha. */
|
|
45
56
|
export type Corpus = z.infer<typeof CorpusSchema>;
|
|
46
57
|
/** One paper of the corpus. */
|
|
47
58
|
export type Paper = Corpus[string];
|
package/dist/vite.js
CHANGED
|
@@ -169,18 +169,18 @@ export function research({ projects, }) {
|
|
|
169
169
|
enforce: 'post',
|
|
170
170
|
buildStart() {
|
|
171
171
|
for (const [slug, dir] of dirs) {
|
|
172
|
-
const {
|
|
173
|
-
for (const [
|
|
172
|
+
const { datasets, pages } = project(dir);
|
|
173
|
+
for (const [dataset, json] of Object.entries(datasets))
|
|
174
174
|
this.emitFile({
|
|
175
175
|
type: 'asset',
|
|
176
|
-
fileName: `${prefix}/${slug}/data/${
|
|
176
|
+
fileName: `${prefix}/${slug}/data/${dataset}.json`,
|
|
177
177
|
source: JSON.stringify(json()),
|
|
178
178
|
});
|
|
179
|
-
for (const [
|
|
179
|
+
for (const [dataset, held] of Object.entries(pages))
|
|
180
180
|
for (const [page, file] of Object.entries(held()))
|
|
181
181
|
this.emitFile({
|
|
182
182
|
type: 'asset',
|
|
183
|
-
fileName: `${prefix}/${slug}/private/${page}/${
|
|
183
|
+
fileName: `${prefix}/${slug}/private/${page}/${dataset}.json`,
|
|
184
184
|
source: JSON.stringify(read(dir, file)),
|
|
185
185
|
});
|
|
186
186
|
}
|
|
@@ -266,30 +266,32 @@ export function research({ projects, }) {
|
|
|
266
266
|
const local = path.startsWith(config.base)
|
|
267
267
|
? path.slice(config.base.length)
|
|
268
268
|
: '';
|
|
269
|
-
const [, slug,
|
|
270
|
-
const [, pslug, page = '',
|
|
269
|
+
const [, slug, dataset = ''] = local.match(dataPath) ?? [];
|
|
270
|
+
const [, pslug, page = '', pdataset = ''] = local.match(privatePath) ?? [];
|
|
271
271
|
const dir = dirs.find(([s]) => s === (slug ?? pslug))?.[1];
|
|
272
272
|
if (!dir)
|
|
273
273
|
return next();
|
|
274
274
|
if (pslug) {
|
|
275
275
|
// Gates nothing: the host's dev server decides who loads which page.
|
|
276
276
|
const { pages } = project(dir);
|
|
277
|
-
const held = Object.hasOwn(pages,
|
|
277
|
+
const held = Object.hasOwn(pages, pdataset)
|
|
278
|
+
? pages[pdataset]()
|
|
279
|
+
: {};
|
|
278
280
|
const file = Object.hasOwn(held, page) ? held[page] : undefined;
|
|
279
281
|
if (!file) {
|
|
280
282
|
res.statusCode = 404;
|
|
281
|
-
return res.end(`${pslug} has no private
|
|
283
|
+
return res.end(`${pslug} has no private dataset "${pdataset}" page "${page}"`);
|
|
282
284
|
}
|
|
283
285
|
res.setHeader('Content-Type', 'application/json');
|
|
284
286
|
return res.end(JSON.stringify(read(dir, file)));
|
|
285
287
|
}
|
|
286
|
-
const {
|
|
287
|
-
if (!Object.hasOwn(
|
|
288
|
+
const { datasets } = project(dir);
|
|
289
|
+
if (!Object.hasOwn(datasets, dataset)) {
|
|
288
290
|
res.statusCode = 404;
|
|
289
|
-
return res.end(`${slug} has no
|
|
291
|
+
return res.end(`${slug} has no dataset "${dataset}"`);
|
|
290
292
|
}
|
|
291
293
|
res.setHeader('Content-Type', 'application/json');
|
|
292
|
-
res.end(JSON.stringify(
|
|
294
|
+
res.end(JSON.stringify(datasets[dataset]()));
|
|
293
295
|
});
|
|
294
296
|
},
|
|
295
297
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metacensus/library-research",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"types": "./dist/testing.d.ts",
|
|
27
27
|
"default": "./dist/testing.js"
|
|
28
28
|
},
|
|
29
|
-
"./
|
|
30
|
-
"types": "./dist/
|
|
31
|
-
"default": "./dist/
|
|
29
|
+
"./datasets": {
|
|
30
|
+
"types": "./dist/datasets.d.ts",
|
|
31
|
+
"default": "./dist/datasets.js"
|
|
32
32
|
},
|
|
33
33
|
"./csp.json": "./csp.json",
|
|
34
34
|
"./schema": {
|
|
@@ -28,17 +28,41 @@
|
|
|
28
28
|
"pattern": "^(?!\\/)(?!.*\\.\\.).+$"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
|
+
"roles": {
|
|
32
|
+
"type": "array",
|
|
33
|
+
"items": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"pattern": "^[a-z][a-z0-9-]*$"
|
|
36
|
+
},
|
|
37
|
+
"uniqueItems": true
|
|
38
|
+
},
|
|
31
39
|
"people": {
|
|
32
40
|
"type": "object",
|
|
33
41
|
"propertyNames": {
|
|
34
42
|
"type": "string"
|
|
35
43
|
},
|
|
36
44
|
"additionalProperties": {
|
|
37
|
-
"type": "
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
"type": "object",
|
|
46
|
+
"properties": {
|
|
47
|
+
"name": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"minLength": 1
|
|
50
|
+
},
|
|
51
|
+
"role": {
|
|
52
|
+
"type": "string"
|
|
53
|
+
},
|
|
54
|
+
"pages": {
|
|
55
|
+
"type": "array",
|
|
56
|
+
"items": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"pattern": "^[a-z0-9][a-z0-9-]*$"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"required": [
|
|
63
|
+
"name"
|
|
64
|
+
],
|
|
65
|
+
"additionalProperties": false
|
|
42
66
|
}
|
|
43
67
|
}
|
|
44
68
|
},
|
package/dist/roles.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reads each data role of the project in `dir`, the folder holding
|
|
3
|
-
* research.json, parsed by its schema in ui/roles.ts, and its corpus. A
|
|
4
|
-
* private role is read as `{ page: parsed }`, its schema parsing one page's
|
|
5
|
-
* file. Throws one line per failure.
|
|
6
|
-
*/
|
|
7
|
-
export declare function load(dir: string): Promise<Record<string, unknown>>;
|