@metacensus/library-research 0.13.0 → 0.15.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 +43 -11
- package/dist/commands/add.js +29 -12
- package/dist/commands/check.js +1 -1
- package/dist/commands/dev.js +24 -13
- package/dist/commands/reshape.d.ts +2 -0
- package/dist/commands/reshape.js +21 -0
- package/dist/context.d.ts +3 -1
- package/dist/context.js +2 -0
- package/dist/corpus.d.ts +432 -0
- package/dist/corpus.js +348 -0
- package/dist/data.d.ts +16 -12
- package/dist/data.js +38 -26
- package/dist/datasets.d.ts +8 -0
- package/dist/{roles.js → datasets.js} +35 -29
- package/dist/dev/host.d.ts +7 -1
- package/dist/dev/host.js +15 -13
- package/dist/host.d.ts +3 -1
- package/dist/host.js +6 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/mount.d.ts +7 -3
- package/dist/mount.js +7 -7
- package/dist/papers.js +3 -2
- package/dist/playwright.d.ts +1 -1
- package/dist/playwright.js +24 -6
- package/dist/project-id.d.ts +4 -0
- package/dist/project-id.js +7 -0
- package/dist/react.d.ts +18 -18
- package/dist/react.js +60 -37
- package/dist/reader.d.ts +18 -4
- package/dist/reader.js +25 -2
- package/dist/schema.d.ts +10 -35
- package/dist/schema.js +57 -34
- package/dist/testing.d.ts +11 -6
- package/dist/testing.js +12 -10
- package/dist/types.d.ts +62 -13
- package/dist/vite-config.js +2 -12
- package/dist/vite.d.ts +10 -2
- package/dist/vite.js +71 -19
- package/dist/zod.d.ts +2 -0
- package/dist/zod.js +4 -0
- package/package.json +14 -14
- package/schema/corpus.schema.json +3949 -29
- package/schema/project-id.schema.json +6 -0
- package/schema/research.schema.json +33 -7
- package/dist/roles.d.ts +0 -7
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/host.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { AnyProject } from './mount.js';
|
|
|
3
3
|
import type { Host } from './types.js';
|
|
4
4
|
export type { Host };
|
|
5
5
|
export { pdfAssets } from './pdf.js';
|
|
6
|
+
export { projectIdPattern } from './project-id.js';
|
|
6
7
|
declare function Failed({ error, reset }: ErrorComponentProps): import("react").JSX.Element;
|
|
7
8
|
/**
|
|
8
9
|
* The route options of a project's mount: the project's `Host` from
|
|
@@ -16,6 +17,7 @@ export declare function frame(useHost: () => Host): {
|
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
18
19
|
* Mounts `project` under `parent` at the path it names, with `options` as the
|
|
19
|
-
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the
|
|
20
|
+
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the
|
|
21
|
+
* route. The route's component renders in the project's datasets.
|
|
20
22
|
*/
|
|
21
23
|
export declare function mount(parent: AnyRoute, project: AnyProject, options: Partial<Omit<AnyRoute['options'], 'getParentRoute' | 'path'>>): AnyRoute;
|
package/dist/host.js
CHANGED
|
@@ -2,8 +2,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { Button, Notice } from '@metacensus/library-ui';
|
|
3
3
|
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
|
|
4
4
|
import { createRoute, Outlet, } from '@tanstack/react-router';
|
|
5
|
-
import { HostContext } from './context.js';
|
|
5
|
+
import { DatasetsContext, HostContext } from './context.js';
|
|
6
6
|
export { pdfAssets } from './pdf.js';
|
|
7
|
+
export { projectIdPattern } from './project-id.js';
|
|
7
8
|
function Failed({ error, reset }) {
|
|
8
9
|
const queries = useQueryErrorResetBoundary();
|
|
9
10
|
return (_jsx(Notice, { role: "alert", title: error instanceof Error ? error.message : String(error), children: _jsx(Button, { onClick: () => {
|
|
@@ -27,11 +28,14 @@ export function frame(useHost) {
|
|
|
27
28
|
}
|
|
28
29
|
/**
|
|
29
30
|
* Mounts `project` under `parent` at the path it names, with `options` as the
|
|
30
|
-
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the
|
|
31
|
+
* route's options (`frame(useHost)`, and any `loader` or `head`); returns the
|
|
32
|
+
* route. The route's component renders in the project's datasets.
|
|
31
33
|
*/
|
|
32
34
|
export function mount(parent, project, options) {
|
|
35
|
+
const Inner = (options.component ?? Outlet);
|
|
33
36
|
const route = createRoute({
|
|
34
37
|
...options,
|
|
38
|
+
component: () => (_jsx(DatasetsContext.Provider, { value: project.datasets, children: _jsx(Inner, {}) })),
|
|
35
39
|
getParentRoute: () => parent,
|
|
36
40
|
path: project.path,
|
|
37
41
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export type { AnyProject, Corpus, Doc, Host, Kind, Paper, Mount, Project, Reader, RouterOf, } from './types.js';
|
|
1
|
+
export type { AnyProject, Corpus, Csl, Data, Doc, Edition, Host, Judgment, Kind, Member, Paper, Register, Schema, Mount, Project, Reader, RouterOf, Work, } from './types.js';
|
|
2
|
+
export { papers } from './corpus.js';
|
|
2
3
|
export { defineProject } from './mount.js';
|
|
3
|
-
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, usePrivate, useSaveDocument, } from './react.js';
|
|
4
|
+
export { useCorpus, useData, useDatas, useDocument, useDocuments, useHost, useMembers, usePrivate, useReader, useSaveDocument, } from './react.js';
|
|
4
5
|
export { DocumentError } from './documents.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { papers } from './corpus.js';
|
|
1
2
|
export { defineProject } from './mount.js';
|
|
2
|
-
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';
|
|
3
4
|
export { DocumentError } from './documents.js';
|
package/dist/mount.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type AnyRoute } from '@tanstack/react-router';
|
|
2
|
+
import type { Schema, Schemas } from './types.js';
|
|
2
3
|
declare function mountRoute<P extends string>(path: P): {
|
|
3
4
|
root: import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>;
|
|
4
5
|
route: import("@tanstack/react-router").Route<unknown, import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/react-router").ResolveFullPath<import("@tanstack/react-router").RootRoute<import("@tanstack/react-router").Register, undefined, {}, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>, P, import("@tanstack/router-core").RoutePrefix<"/", P>>, string, import("@tanstack/router-core").RoutePrefix<"__root__", P>, undefined, import("@tanstack/react-router").ResolveParams<P>, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, import("@tanstack/react-router").AnyContext, {}, undefined, unknown, unknown, unknown, unknown, undefined>;
|
|
@@ -9,20 +10,23 @@ export type Mount<P extends string> = ReturnType<typeof mountRoute<P>>['route'];
|
|
|
9
10
|
/** A project's routes under the first path segment it names. */
|
|
10
11
|
export interface Project<P extends string = string, C = readonly AnyRoute[]> {
|
|
11
12
|
readonly path: P;
|
|
13
|
+
readonly datasets: Record<string, Schema>;
|
|
12
14
|
routes(parent: Mount<P>): C;
|
|
13
15
|
}
|
|
14
16
|
/** Any project, for a list or a constraint: a `Project<'a'>` is not a `Project<string>`. */
|
|
15
17
|
export interface AnyProject {
|
|
16
18
|
readonly path: string;
|
|
19
|
+
readonly datasets: Record<string, Schema>;
|
|
17
20
|
routes(parent: never): readonly AnyRoute[];
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
23
|
* What `ui/index.tsx` and `ui/local/index.tsx` default-export. `path` is the
|
|
21
|
-
* project's first URL segment, matching
|
|
24
|
+
* project's first URL segment, matching `projectIdPattern` and not
|
|
22
25
|
* `projects`, `local` or `pdfjs`, which dev and the host reserve; `routes`
|
|
23
|
-
* creates its routes against `parent
|
|
26
|
+
* creates its routes against `parent`; `datasets` is ui/datasets.ts's export,
|
|
27
|
+
* which `useData` and `usePrivate` read the schemas from.
|
|
24
28
|
*/
|
|
25
|
-
export declare function defineProject<P extends string, const C>(path: P, routes: (parent: Mount<P>) => C): Project<P, C>;
|
|
29
|
+
export declare function defineProject<P extends string, const C>(path: P, routes: (parent: Mount<P>) => C, datasets?: Schemas): Project<P, C>;
|
|
26
30
|
/** A project's `Register`: `router: RouterOf<typeof project>`. */
|
|
27
31
|
export type RouterOf<R extends AnyProject> = ReturnType<R['routes']> extends infer C extends readonly AnyRoute[] ? ReturnType<typeof routerOf<R['path'], C>> : never;
|
|
28
32
|
export {};
|
package/dist/mount.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRootRoute, createRoute, createRouter, } from '@tanstack/react-router';
|
|
2
|
-
|
|
2
|
+
import { checkProjectId } from './project-id.js';
|
|
3
3
|
// First segments that dev and the host serve themselves.
|
|
4
4
|
const reserved = ['projects', 'local', 'pdfjs'];
|
|
5
5
|
function mountRoute(path) {
|
|
@@ -16,14 +16,14 @@ function routerOf(path, children) {
|
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* What `ui/index.tsx` and `ui/local/index.tsx` default-export. `path` is the
|
|
19
|
-
* project's first URL segment, matching
|
|
19
|
+
* project's first URL segment, matching `projectIdPattern` and not
|
|
20
20
|
* `projects`, `local` or `pdfjs`, which dev and the host reserve; `routes`
|
|
21
|
-
* creates its routes against `parent
|
|
21
|
+
* creates its routes against `parent`; `datasets` is ui/datasets.ts's export,
|
|
22
|
+
* which `useData` and `usePrivate` read the schemas from.
|
|
22
23
|
*/
|
|
23
|
-
export function defineProject(path, routes) {
|
|
24
|
-
|
|
25
|
-
throw new Error(`project path "${path}" does not match ${format}`);
|
|
24
|
+
export function defineProject(path, routes, datasets) {
|
|
25
|
+
checkProjectId(path, 'project path');
|
|
26
26
|
if (reserved.includes(path))
|
|
27
27
|
throw new Error(`project path "${path}" is reserved by dev and the host`);
|
|
28
|
-
return { path, routes };
|
|
28
|
+
return { path, routes, datasets: datasets ?? {} };
|
|
29
29
|
}
|
package/dist/papers.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import { extname } from 'node:path';
|
|
3
|
+
import { papers } from './corpus.js';
|
|
3
4
|
import { readCorpus } from './data.js';
|
|
4
5
|
import { kinds } from './schema.js';
|
|
5
6
|
export function git(root, ...args) {
|
|
@@ -28,8 +29,8 @@ export function checkPapers(root) {
|
|
|
28
29
|
const lfs = new Map([...git(root, 'lfs', 'ls-files', '--long').matchAll(/^(\w+) [*-] (.+)$/gm)]
|
|
29
30
|
.filter(([, , path]) => index.has(path))
|
|
30
31
|
.map(([, oid, path]) => [path, oid]));
|
|
31
|
-
const listed = new Map(
|
|
32
|
-
paperPath(sha, kind),
|
|
32
|
+
const listed = new Map([...papers(readCorpus(root))].map(([sha, { paper }]) => [
|
|
33
|
+
paperPath(sha, paper.kind),
|
|
33
34
|
sha,
|
|
34
35
|
]));
|
|
35
36
|
const unstored = (path, sha) => {
|
package/dist/playwright.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { expect, type Page } from '@playwright/test';
|
|
2
2
|
declare const _default: import("@playwright/test").PlaywrightTestConfig<{}, {}>;
|
|
3
3
|
export default _default;
|
|
4
|
-
type Violation = `csp ${string} ${string}` | `axe ${string}`;
|
|
4
|
+
type Violation = `csp ${string} ${string}` | `axe ${string}` | `console ${string}` | `origin ${string}`;
|
|
5
5
|
export declare const test: import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & {
|
|
6
6
|
violations: Violation[];
|
|
7
7
|
/** A page in a new context on the dev server, read as `reader`; `null` is signed out. The context takes the project's `contextOptions` and `baseURL`, no other `use` option. */
|
package/dist/playwright.js
CHANGED
|
@@ -12,22 +12,40 @@ export default defineConfig({
|
|
|
12
12
|
wait: { stdout: /(?<library_research_url>http:\S+\/)/ },
|
|
13
13
|
},
|
|
14
14
|
});
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Collects a page's CSP and axe violations, the console errors its own script logs or throws, and its requests to other origins; the returned function fails on those not listed. A request to another origin is aborted whether or not it is listed.
|
|
17
|
+
*/
|
|
18
|
+
async function watch(page, baseURL) {
|
|
17
19
|
const found = [];
|
|
18
20
|
await page.exposeFunction(report, (v) => found.push(v));
|
|
21
|
+
// The browser's own console lines (a CSP refusal, a failed load) carry no arguments and are reported, or left to the spec, elsewhere.
|
|
22
|
+
page.on('console', (message) => {
|
|
23
|
+
if (message.type() === 'error' && message.args().length > 0)
|
|
24
|
+
found.push(`console ${message.text()}`);
|
|
25
|
+
});
|
|
26
|
+
page.on('pageerror', (error) => found.push(`console ${error}`));
|
|
27
|
+
await page.context().route('**/*', (route) => {
|
|
28
|
+
const { protocol, origin } = new URL(route.request().url());
|
|
29
|
+
if (!protocol.startsWith('http') || origin === new URL(baseURL).origin)
|
|
30
|
+
return route.fallback();
|
|
31
|
+
found.push(`origin ${origin}`);
|
|
32
|
+
return route.abort('blockedbyclient');
|
|
33
|
+
});
|
|
19
34
|
await page.addInitScript(`window.${sent} = []; document.addEventListener('securitypolicyviolation', (e) => ${sent}.push(${report}('csp ' + e.effectiveDirective + ' ' + e.blockedURI)))`);
|
|
20
35
|
return async (listed) => {
|
|
21
36
|
await page.evaluate(`Promise.all(window.${sent} ?? [])`);
|
|
22
|
-
const axe = await new AxeBuilder({ page })
|
|
37
|
+
const axe = await new AxeBuilder({ page })
|
|
38
|
+
.withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa'])
|
|
39
|
+
.analyze();
|
|
23
40
|
found.push(...axe.violations.map((v) => `axe ${v.id}`));
|
|
24
41
|
expect(found.filter((v) => !listed.includes(v)), 'unlisted violations').toEqual([]);
|
|
25
42
|
};
|
|
26
43
|
}
|
|
27
44
|
export const test = base.extend({
|
|
45
|
+
/** What a spec excuses, by entry: `axe <rule>`, `csp <directive> <blocked uri>`, `console <message>` for a console error or uncaught error, `origin <origin>` for a request to another origin (still aborted). */
|
|
28
46
|
violations: [[], { option: true }],
|
|
29
|
-
page: async ({ page, violations }, provide) => {
|
|
30
|
-
const finish = await watch(page);
|
|
47
|
+
page: async ({ page, baseURL, violations }, provide) => {
|
|
48
|
+
const finish = await watch(page, baseURL);
|
|
31
49
|
await provide(page);
|
|
32
50
|
await finish(violations);
|
|
33
51
|
},
|
|
@@ -47,7 +65,7 @@ export const test = base.extend({
|
|
|
47
65
|
},
|
|
48
66
|
]);
|
|
49
67
|
const page = await context.newPage();
|
|
50
|
-
finishers.push(await watch(page));
|
|
68
|
+
finishers.push(await watch(page, baseURL));
|
|
51
69
|
return page;
|
|
52
70
|
});
|
|
53
71
|
// Every page is checked, and the first failure is the one thrown.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** What a project's id, its first URL segment and its key in the host's `projects`, matches. */
|
|
2
|
+
export declare const projectIdPattern: RegExp;
|
|
3
|
+
/** Throws unless `id` matches `projectIdPattern`, naming it as `what`. */
|
|
4
|
+
export declare function checkProjectId(id: string, what: string): void;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** What a project's id, its first URL segment and its key in the host's `projects`, matches. */
|
|
2
|
+
export const projectIdPattern = /^[a-z0-9][a-z0-9-]*$/;
|
|
3
|
+
/** Throws unless `id` matches `projectIdPattern`, naming it as `what`. */
|
|
4
|
+
export function checkProjectId(id, what) {
|
|
5
|
+
if (!projectIdPattern.test(id))
|
|
6
|
+
throw new Error(`${what} "${id}" does not match ${projectIdPattern}`);
|
|
7
|
+
}
|
package/dist/react.d.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import { DocumentError } from './documents.js';
|
|
2
|
-
import type { Corpus, Doc, Host } from './types.js';
|
|
2
|
+
import type { Corpus, Data, Doc, Host, Member, Reader, Schemas } 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[];
|
|
6
9
|
/**
|
|
7
|
-
* One
|
|
8
|
-
* fetch or parse throws to the frame's error notice,
|
|
9
|
-
* 403 also calls `Host.sessionLost`.
|
|
10
|
-
* role list, not on every render.
|
|
10
|
+
* One dataset's JSON, parsed by its schema in `ui/datasets.ts`. Suspends while
|
|
11
|
+
* it loads; a failed fetch or parse throws to the frame's error notice,
|
|
12
|
+
* naming the dataset. A 401 or 403 also calls `Host.sessionLost`.
|
|
11
13
|
*/
|
|
12
|
-
export declare function useData<
|
|
14
|
+
export declare function useData<K extends keyof Schemas>(dataset: K): Data<K>;
|
|
13
15
|
/**
|
|
14
|
-
* `useData` for several
|
|
15
|
-
* tuple
|
|
16
|
-
* Each parser must be pure: it is read once per role list, not on every render.
|
|
16
|
+
* `useData` for several datasets at once, fetched in parallel: the names in a
|
|
17
|
+
* tuple, the values returned in the same order and typed by their schemas.
|
|
17
18
|
*/
|
|
18
|
-
export declare function useDatas<const R extends readonly
|
|
19
|
-
-readonly [K in keyof R]: R[K] extends
|
|
19
|
+
export declare function useDatas<const R extends readonly (keyof Schemas)[]>(datasets: R): {
|
|
20
|
+
-readonly [K in keyof R]: R[K] extends keyof Schemas ? Data<R[K]> : never;
|
|
20
21
|
};
|
|
21
22
|
/**
|
|
22
|
-
* The viewed person's pages of a private
|
|
23
|
+
* The viewed person's pages of a private dataset, as `{ page: parsed }`: empty
|
|
23
24
|
* when they hold none, a page the host names but the build did not emit left
|
|
24
|
-
* out.
|
|
25
|
-
*
|
|
25
|
+
* out. Its schema parses each page's JSON. Its cache entry, `{ json } | null`,
|
|
26
|
+
* is not `useData`'s raw JSON, under the same key prefix.
|
|
26
27
|
*/
|
|
27
|
-
export declare function usePrivate<
|
|
28
|
-
/** The `corpus`
|
|
28
|
+
export declare function usePrivate<K extends keyof Schemas>(dataset: K): Record<string, Data<K>>;
|
|
29
|
+
/** The `corpus` dataset, every work by id, parsed by the library's own schema. */
|
|
29
30
|
export declare function useCorpus(): Corpus;
|
|
30
31
|
/**
|
|
31
32
|
* One document of the project, or `null` when none has that id. Suspends while
|
|
@@ -48,4 +49,3 @@ export declare function useSaveDocument(id: string): import("@tanstack/react-que
|
|
|
48
49
|
body: unknown;
|
|
49
50
|
when?: "absent" | number;
|
|
50
51
|
}, unknown>;
|
|
51
|
-
export {};
|
package/dist/react.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useMutation, useQueryClient, useSuspenseQueries, useSuspenseQuery, } from '@tanstack/react-query';
|
|
2
2
|
import { useContext, useMemo, useState } from 'react';
|
|
3
|
-
import { FetchContext, HostContext } from './context.js';
|
|
3
|
+
import { DatasetsContext, FetchContext, HostContext } from './context.js';
|
|
4
4
|
import { DocumentError, documentId, documentPrefix } from './documents.js';
|
|
5
5
|
import * as schema from './schema.js';
|
|
6
6
|
/** The `Host` of the project's frame; throws outside `./host`'s `frame`. Stable for the life of the frame. */
|
|
@@ -10,40 +10,61 @@ 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
|
+
}
|
|
21
|
+
/** The schemas `defineProject` registered, as `parse` functions by dataset name. */
|
|
22
|
+
function useParsers() {
|
|
23
|
+
const schemas = useContext(DatasetsContext);
|
|
24
|
+
return (dataset) => {
|
|
25
|
+
const schema = Object.hasOwn(schemas, dataset)
|
|
26
|
+
? schemas[dataset]
|
|
27
|
+
: undefined;
|
|
28
|
+
if (!schema)
|
|
29
|
+
throw new Error(`dataset "${dataset}": defineProject was given no schema for it`);
|
|
30
|
+
return (json) => schema.parse(json);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
13
33
|
/**
|
|
14
|
-
* One
|
|
15
|
-
* fetch or parse throws to the frame's error notice,
|
|
16
|
-
* 403 also calls `Host.sessionLost`.
|
|
17
|
-
* role list, not on every render.
|
|
34
|
+
* One dataset's JSON, parsed by its schema in `ui/datasets.ts`. Suspends while
|
|
35
|
+
* it loads; a failed fetch or parse throws to the frame's error notice,
|
|
36
|
+
* naming the dataset. A 401 or 403 also calls `Host.sessionLost`.
|
|
18
37
|
*/
|
|
19
|
-
export function useData(
|
|
20
|
-
return useDatas([
|
|
38
|
+
export function useData(dataset) {
|
|
39
|
+
return useDatas([dataset])[0];
|
|
21
40
|
}
|
|
22
41
|
/**
|
|
23
|
-
* `useData` for several
|
|
24
|
-
* tuple
|
|
25
|
-
* Each parser must be pure: it is read once per role list, not on every render.
|
|
42
|
+
* `useData` for several datasets at once, fetched in parallel: the names in a
|
|
43
|
+
* tuple, the values returned in the same order and typed by their schemas.
|
|
26
44
|
*/
|
|
27
|
-
export function useDatas(
|
|
45
|
+
export function useDatas(datasets) {
|
|
46
|
+
const parser = useParsers();
|
|
47
|
+
return useFetched(datasets.map((dataset) => [String(dataset), parser(String(dataset))]));
|
|
48
|
+
}
|
|
49
|
+
/** Fetches each dataset and parses it; the parsers are those of a project's fixed schemas, so they are read once per dataset list. */
|
|
50
|
+
function useFetched(datasets) {
|
|
28
51
|
const host = useHost();
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const [held, hold] = useState({ key, roles });
|
|
33
|
-
if (held.key !== key)
|
|
34
|
-
hold({ key, roles });
|
|
35
|
-
const names = held.roles.map(([role]) => role);
|
|
36
|
-
const selects = useMemo(() => held.roles.map(([role, parse]) => (json) => {
|
|
52
|
+
const key = JSON.stringify(datasets.map(([dataset]) => dataset));
|
|
53
|
+
const names = datasets.map(([dataset]) => dataset);
|
|
54
|
+
const selects = useMemo(() => datasets.map(([dataset, parse]) => (json) => {
|
|
37
55
|
try {
|
|
38
56
|
return parse(json);
|
|
39
57
|
}
|
|
40
58
|
catch (cause) {
|
|
41
|
-
return failed(`
|
|
59
|
+
return failed(`dataset "${dataset}"`, cause);
|
|
42
60
|
}
|
|
43
|
-
}),
|
|
61
|
+
}),
|
|
62
|
+
// The datasets are named by `key`, and their parsers do not change.
|
|
63
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
64
|
+
[key]);
|
|
44
65
|
return useSuspenseQueries({
|
|
45
|
-
queries: names.map((
|
|
46
|
-
const url = host.data(
|
|
66
|
+
queries: names.map((dataset, i) => {
|
|
67
|
+
const url = host.data(dataset);
|
|
47
68
|
return {
|
|
48
69
|
queryKey: ['research', url],
|
|
49
70
|
queryFn: () => fetch(url)
|
|
@@ -54,7 +75,7 @@ export function useDatas(roles) {
|
|
|
54
75
|
throw new Error(`${response.status} from ${url}`);
|
|
55
76
|
return response.json();
|
|
56
77
|
})
|
|
57
|
-
.catch((cause) => failed(`
|
|
78
|
+
.catch((cause) => failed(`dataset "${dataset}"`, cause)),
|
|
58
79
|
select: selects[i],
|
|
59
80
|
staleTime: Infinity,
|
|
60
81
|
retry: false,
|
|
@@ -70,30 +91,31 @@ function failed(what, cause) {
|
|
|
70
91
|
throw new Error(`${what}: ${why}`, { cause });
|
|
71
92
|
}
|
|
72
93
|
/**
|
|
73
|
-
* The viewed person's pages of a private
|
|
94
|
+
* The viewed person's pages of a private dataset, as `{ page: parsed }`: empty
|
|
74
95
|
* when they hold none, a page the host names but the build did not emit left
|
|
75
|
-
* out.
|
|
76
|
-
*
|
|
96
|
+
* out. Its schema parses each page's JSON. Its cache entry, `{ json } | null`,
|
|
97
|
+
* is not `useData`'s raw JSON, under the same key prefix.
|
|
77
98
|
*/
|
|
78
|
-
export function usePrivate(
|
|
99
|
+
export function usePrivate(dataset) {
|
|
100
|
+
const name = String(dataset);
|
|
101
|
+
const parse = useParsers()(name);
|
|
79
102
|
const host = useHost();
|
|
80
|
-
const urls = host.private(
|
|
103
|
+
const urls = host.private(name);
|
|
81
104
|
const pages = Object.keys(urls);
|
|
82
105
|
const key = pages.join();
|
|
83
|
-
const held = useHeld(role, parse);
|
|
84
106
|
const selects = useMemo(() => pages.map((page) => (found) => {
|
|
85
107
|
if (!found)
|
|
86
108
|
return null;
|
|
87
109
|
try {
|
|
88
|
-
return { value:
|
|
110
|
+
return { value: parse(found.json) };
|
|
89
111
|
}
|
|
90
112
|
catch (cause) {
|
|
91
|
-
return failed(`private
|
|
113
|
+
return failed(`private dataset "${name}" page "${page}"`, cause);
|
|
92
114
|
}
|
|
93
115
|
}),
|
|
94
|
-
// The pages are named by `key
|
|
116
|
+
// The pages are named by `key`, and a project's schemas do not change.
|
|
95
117
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
96
|
-
[
|
|
118
|
+
[key]);
|
|
97
119
|
return useSuspenseQueries({
|
|
98
120
|
queries: pages.map((page, i) => {
|
|
99
121
|
const url = urls[page];
|
|
@@ -109,7 +131,7 @@ export function usePrivate(role, parse) {
|
|
|
109
131
|
throw new Error(`${response.status} from ${url}`);
|
|
110
132
|
return response.json().then((json) => ({ json }));
|
|
111
133
|
})
|
|
112
|
-
.catch((cause) => failed(`private
|
|
134
|
+
.catch((cause) => failed(`private dataset "${name}" page "${page}"`, cause)),
|
|
113
135
|
select: selects[i],
|
|
114
136
|
staleTime: Infinity,
|
|
115
137
|
retry: false,
|
|
@@ -118,9 +140,10 @@ export function usePrivate(role, parse) {
|
|
|
118
140
|
combine: (results) => Object.fromEntries(results.flatMap(({ data }, i) => data ? [[pages[i], data.value]] : [])),
|
|
119
141
|
});
|
|
120
142
|
}
|
|
121
|
-
|
|
143
|
+
const corpusDataset = ['corpus', (json) => schema.Corpus.parse(json)];
|
|
144
|
+
/** The `corpus` dataset, every work by id, parsed by the library's own schema. */
|
|
122
145
|
export function useCorpus() {
|
|
123
|
-
return
|
|
146
|
+
return useFetched([corpusDataset])[0];
|
|
124
147
|
}
|
|
125
148
|
/** A fetch whose 401 and 403 are the host's lost session, and whose other failures are a `DocumentError`. */
|
|
126
149
|
function useSend() {
|
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
|
@@ -1,40 +1,15 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
readonly docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
6
|
-
readonly xls: "application/vnd.ms-excel";
|
|
7
|
-
readonly xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
8
|
-
readonly zip: "application/zip";
|
|
9
|
-
readonly gif: "image/gif";
|
|
10
|
-
readonly jpg: "image/jpeg";
|
|
11
|
-
readonly tif: "image/tiff";
|
|
12
|
-
readonly txt: "text/plain";
|
|
13
|
-
readonly html: "text/plain";
|
|
14
|
-
readonly xml: "text/plain";
|
|
15
|
-
};
|
|
1
|
+
import { z } from './zod.js';
|
|
2
|
+
export { Corpus, kinds } from './corpus.js';
|
|
3
|
+
/** A project's id, matching `projectIdPattern`. */
|
|
4
|
+
export declare const ProjectId: z.ZodString;
|
|
16
5
|
export declare const Research: z.ZodObject<{
|
|
17
6
|
title: z.ZodString;
|
|
18
7
|
data: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
19
8
|
private: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
20
|
-
|
|
9
|
+
roles: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
10
|
+
people: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
role: z.ZodOptional<z.ZodString>;
|
|
13
|
+
pages: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
14
|
+
}, z.core.$strict>>>;
|
|
21
15
|
}, z.core.$strict>;
|
|
22
|
-
export declare const Corpus: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
23
|
-
kind: z.ZodEnum<{
|
|
24
|
-
pdf: "pdf";
|
|
25
|
-
doc: "doc";
|
|
26
|
-
docx: "docx";
|
|
27
|
-
xls: "xls";
|
|
28
|
-
xlsx: "xlsx";
|
|
29
|
-
zip: "zip";
|
|
30
|
-
gif: "gif";
|
|
31
|
-
jpg: "jpg";
|
|
32
|
-
tif: "tif";
|
|
33
|
-
txt: "txt";
|
|
34
|
-
html: "html";
|
|
35
|
-
xml: "xml";
|
|
36
|
-
}>;
|
|
37
|
-
title: z.ZodOptional<z.ZodString>;
|
|
38
|
-
citation: z.ZodOptional<z.ZodString>;
|
|
39
|
-
open: z.ZodOptional<z.ZodLiteral<true>>;
|
|
40
|
-
}, z.core.$strict>>;
|