@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/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's routes rendered through the frame and the `/$project` mount a
10
- * real host uses. Render `element` with any DOM renderer; `router` says where
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(routes, { data = {}, documents = {}, private: held = {}, reader = 'test', url = '/', sessionLost = () => { }, } = {}) {
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: { username: reader },
18
- data(role) {
19
- if (!Object.hasOwn(data, role) || data[role] === undefined)
20
- throw new Error(`testHost: no data for role "${role}"`);
21
- return `data:application/json,${encodeURIComponent(JSON.stringify(data[role]))}`;
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: (role) => Object.hasOwn(held[role] ?? {}, reader)
24
- ? {
25
- [reader]: `data:application/json,${encodeURIComponent(JSON.stringify(held[role][reader]))}`,
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(routes, () => host, createMemoryHistory({ initialEntries: [`/project${url}`] }));
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
- /** The URL of a data role's JSON, `role` being one the project declares; `useData` fetches it. */
12
- data(role: string): string;
13
- /** The viewed person's pages of a private role: page key to URL; empty when they hold none. */
14
- private(role: string): Record<string, string>;
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
- /** The person a project is viewed as. */
38
- export interface Reader {
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` role: every paper by sha. */
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, RouterOf, Routes } from './mount.js';
59
+ export type { AnyProject, Mount, Project, RouterOf } from './mount.js';
@@ -24,6 +24,7 @@ export function hostConfig(root) {
24
24
  include: [
25
25
  '@metacensus/library-ui',
26
26
  '@metacensus/library-ui/pdf',
27
+ '@metacensus/library-ui > pdfjs-dist',
27
28
  '@tanstack/react-query',
28
29
  '@tanstack/react-router',
29
30
  'react-dom/client',
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 { roles, pages } = project(dir);
173
- for (const [role, json] of Object.entries(roles))
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/${role}.json`,
176
+ fileName: `${prefix}/${slug}/data/${dataset}.json`,
177
177
  source: JSON.stringify(json()),
178
178
  });
179
- for (const [role, held] of Object.entries(pages))
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}/${role}.json`,
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, role = ''] = local.match(dataPath) ?? [];
270
- const [, pslug, page = '', prole = ''] = local.match(privatePath) ?? [];
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, prole) ? pages[prole]() : {};
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 role "${prole}" page "${page}"`);
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 { roles } = project(dir);
287
- if (!Object.hasOwn(roles, role)) {
288
+ const { datasets } = project(dir);
289
+ if (!Object.hasOwn(datasets, dataset)) {
288
290
  res.statusCode = 404;
289
- return res.end(`${slug} has no role "${role}"`);
291
+ return res.end(`${slug} has no dataset "${dataset}"`);
290
292
  }
291
293
  res.setHeader('Content-Type', 'application/json');
292
- res.end(JSON.stringify(roles[role]()));
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.12.1",
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
- "./roles": {
30
- "types": "./dist/roles.d.ts",
31
- "default": "./dist/roles.js"
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 { Routes } from '@metacensus/library-research'
2
+ import type { AnyProject } from '@metacensus/library-research'
3
3
 
4
- type Root = () => Promise<{ default: Routes }>
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>>;