@metacensus/library-research 0.12.1 → 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 +36 -29
- package/dist/commands/check.js +1 -1
- package/dist/commands/dev.js +23 -18
- package/dist/commands/init.js +14 -9
- package/dist/data.d.ts +11 -5
- 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 +8 -1
- package/dist/dev/host.js +28 -19
- package/dist/host.d.ts +10 -6
- package/dist/host.js +15 -7
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/mount.d.ts +24 -10
- package/dist/mount.js +21 -7
- package/dist/playwright.js +1 -1
- package/dist/react.d.ts +20 -13
- package/dist/react.js +33 -25
- package/dist/reader.d.ts +18 -0
- package/dist/reader.js +25 -0
- package/dist/router.d.ts +4 -3
- package/dist/router.js +7 -8
- package/dist/schema.d.ts +6 -0
- package/dist/schema.js +37 -6
- package/dist/testing.d.ts +17 -9
- package/dist/testing.js +18 -14
- package/dist/types.d.ts +19 -8
- package/dist/vite-config.js +1 -0
- package/dist/vite.js +15 -13
- package/package.json +4 -4
- package/schema/research.schema.json +37 -0
- package/src/virtual.d.ts +2 -2
- package/dist/roles.d.ts +0 -7
package/dist/testing.js
CHANGED
|
@@ -3,34 +3,38 @@ 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 { membersOf, pagesOf, readerOf } from './reader.js';
|
|
6
7
|
import { projectRouter } from './router.js';
|
|
8
|
+
export { membersOf, readerOf } from './reader.js';
|
|
7
9
|
const origin = 'http://test.invalid';
|
|
8
10
|
/**
|
|
9
|
-
* A project
|
|
10
|
-
*
|
|
11
|
+
* A project rendered through the frame and the mount a real host uses, at the
|
|
12
|
+
* path it names. Render `element` with any DOM renderer; `router` says where
|
|
11
13
|
* the project navigated, and `documents()` what it saved, sorted by id.
|
|
12
14
|
*/
|
|
13
|
-
export function testHost(
|
|
15
|
+
export function testHost(project, { data = {}, documents = {}, private: held = {}, people, roles, reader = 'test', self = true, url = '/', sessionLost = () => { }, } = {}) {
|
|
14
16
|
const store = documentStore(documents, reader);
|
|
15
17
|
const memory = (input, init) => store.handle(new Request(new URL(input instanceof Request ? input.url : input, origin), init), reader);
|
|
16
18
|
const host = {
|
|
17
|
-
reader: {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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]))}`;
|
|
22
25
|
},
|
|
23
|
-
private: (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
:
|
|
26
|
+
private: (dataset) => Object.fromEntries(pagesOf(people, reader)
|
|
27
|
+
.filter((page) => Object.hasOwn(held[dataset] ?? {}, page))
|
|
28
|
+
.map((page) => [
|
|
29
|
+
page,
|
|
30
|
+
`data:application/json,${encodeURIComponent(JSON.stringify(held[dataset][page]))}`,
|
|
31
|
+
])),
|
|
28
32
|
paper: (sha) => `/papers/${sha}`,
|
|
29
33
|
document: (id) => `${origin}/documents/${id}`,
|
|
30
34
|
documents: (prefix) => `${origin}/documents?prefix=${prefix}`,
|
|
31
35
|
sessionLost,
|
|
32
36
|
};
|
|
33
|
-
const router = projectRouter(
|
|
37
|
+
const router = projectRouter(project, () => host, createMemoryHistory({ initialEntries: [`/${project.path}${url}`] }));
|
|
34
38
|
// react-router 1.170.46 reads this in Matches before router-core 1.171.39 sets it.
|
|
35
39
|
// Delete once react-router and router-core are bumped to versions that agree.
|
|
36
40
|
Object.assign(router, { _rendered: [] });
|
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,15 +36,24 @@ 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];
|
|
48
|
-
export type { Mount,
|
|
59
|
+
export type { AnyProject, Mount, Project, RouterOf } from './mount.js';
|
package/dist/vite-config.js
CHANGED
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": {
|
|
@@ -27,6 +27,43 @@
|
|
|
27
27
|
"type": "string",
|
|
28
28
|
"pattern": "^(?!\\/)(?!.*\\.\\.).+$"
|
|
29
29
|
}
|
|
30
|
+
},
|
|
31
|
+
"roles": {
|
|
32
|
+
"type": "array",
|
|
33
|
+
"items": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"pattern": "^[a-z][a-z0-9-]*$"
|
|
36
|
+
},
|
|
37
|
+
"uniqueItems": true
|
|
38
|
+
},
|
|
39
|
+
"people": {
|
|
40
|
+
"type": "object",
|
|
41
|
+
"propertyNames": {
|
|
42
|
+
"type": "string"
|
|
43
|
+
},
|
|
44
|
+
"additionalProperties": {
|
|
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
|
|
66
|
+
}
|
|
30
67
|
}
|
|
31
68
|
},
|
|
32
69
|
"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
|
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>>;
|