archidocs 1.3.0 → 1.3.1
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/cli/commands.d.ts +8 -8
- package/dist/cli/commands.js +7 -7
- package/dist/cli/dev.d.ts +5 -6
- package/dist/cli/dev.js +12 -14
- package/dist/cli/diff-format.d.ts +1 -5
- package/dist/cli/main.js +2 -324
- package/dist/cli/run.d.ts +18 -0
- package/dist/cli/run.js +283 -0
- package/dist/commands/build.d.ts +51 -0
- package/dist/commands/build.js +39 -0
- package/dist/commands/check.d.ts +8 -0
- package/dist/commands/check.js +5 -0
- package/dist/commands/diff.d.ts +29 -0
- package/dist/commands/diff.js +25 -0
- package/dist/commands/export.d.ts +41 -0
- package/dist/commands/export.js +47 -0
- package/dist/commands/history.d.ts +18 -0
- package/dist/commands/history.js +9 -0
- package/dist/commands/index.d.ts +11 -0
- package/dist/commands/index.js +11 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/load.d.ts +0 -2
- package/dist/core/load.js +5 -6
- package/dist/core/workspace-files.d.ts +13 -0
- package/dist/core/workspace-files.js +20 -0
- package/dist/git.d.ts +2 -0
- package/dist/git.js +14 -4
- package/dist/history/git.d.ts +0 -10
- package/dist/history/git.js +1 -30
- package/dist/history/index.d.ts +15 -31
- package/dist/history/index.js +47 -43
- package/dist/history/points.d.ts +23 -0
- package/dist/history/points.js +28 -0
- package/dist/history/site.d.ts +30 -0
- package/dist/history/site.js +37 -0
- package/dist/mcp/history.d.ts +4 -4
- package/dist/mcp/history.js +56 -116
- package/dist/mcp/server.js +18 -17
- package/dist/mcp/site.d.ts +12 -0
- package/dist/mcp/site.js +46 -0
- package/dist/mcp/source.d.ts +14 -7
- package/dist/mcp/source.js +25 -99
- package/dist/revisions/find.d.ts +64 -0
- package/dist/revisions/find.js +85 -0
- package/dist/revisions/index.d.ts +57 -0
- package/dist/revisions/index.js +24 -0
- package/dist/revisions/site.d.ts +82 -0
- package/dist/revisions/site.js +157 -0
- package/dist/revisions/text.d.ts +8 -0
- package/dist/revisions/text.js +17 -0
- package/dist/revisions/touching.d.ts +7 -0
- package/dist/revisions/touching.js +7 -0
- package/dist/viewer/assets/index-C0lIIm8V.js +138 -0
- package/dist/viewer/assets/{png-CAaXWR-k.js → png-DMwnpRCt.js} +1 -1
- package/dist/viewer/assets/{svg-BPVHfD36.js → svg-DN6lMeZo.js} +1 -1
- package/dist/viewer/index.html +1 -1
- package/dist/workspace/index.d.ts +37 -0
- package/dist/workspace/index.js +52 -0
- package/package.json +1 -1
- package/dist/viewer/assets/index-BXa5R96a.js +0 -138
- /package/dist/{history → revisions}/migrate.d.ts +0 -0
- /package/dist/{history → revisions}/migrate.js +0 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The point grammar: how a tag, a sha (or its prefix), a day or a time names
|
|
3
|
+
* a Revision. One reading for the viewer, MCP on a site and, through ticket
|
|
4
|
+
* 05, a checkout, so a point names the same Revision everywhere.
|
|
5
|
+
*/
|
|
6
|
+
/** The point grammar in one sentence, for every reference that takes a point. */
|
|
7
|
+
export const POINT_GRAMMAR = "a tag, a commit sha or its first 4 or more characters, or a date (`YYYY-MM-DD`) or time (ISO 8601, UTC unless it gives a zone)";
|
|
8
|
+
/** How many candidates or tags a failure carries. */
|
|
9
|
+
const MAX_LISTED = 5;
|
|
10
|
+
const DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
11
|
+
const TIME = /^(\d{4}-\d{2}-\d{2})[Tt ](\d{2}:\d{2})(:\d{2}(?:\.\d+)?)?([Zz]|[+-]\d{2}:?\d{2})?$/;
|
|
12
|
+
const HEX = /^[0-9a-f]{4,64}$/i;
|
|
13
|
+
const DAY_MS = 86_400_000;
|
|
14
|
+
/**
|
|
15
|
+
* The span a day or a time stands for, or undefined when the point is
|
|
16
|
+
* neither. A day (`YYYY-MM-DD`) is the UTC day `[00:00Z, next 00:00Z)`; a
|
|
17
|
+
* time is its one instant, read as UTC when it gives no zone.
|
|
18
|
+
*/
|
|
19
|
+
export function timeSpan(point) {
|
|
20
|
+
const day = DAY.exec(point);
|
|
21
|
+
if (day) {
|
|
22
|
+
const start = Date.parse(`${point}T00:00:00Z`);
|
|
23
|
+
// Date.parse takes 2026-02-30 as March 2nd; a day must read back as itself.
|
|
24
|
+
if (Number.isNaN(start) || new Date(start).toISOString().slice(0, 10) !== point)
|
|
25
|
+
return undefined;
|
|
26
|
+
return { start, end: start + DAY_MS };
|
|
27
|
+
}
|
|
28
|
+
const time = TIME.exec(point);
|
|
29
|
+
if (!time)
|
|
30
|
+
return undefined;
|
|
31
|
+
const [, date, minutes, seconds = ":00", zone = "Z"] = time;
|
|
32
|
+
const offset = zone.toUpperCase() === "Z" ? "Z" : `${zone.slice(0, 3)}:${zone.slice(-2)}`;
|
|
33
|
+
const at = Date.parse(`${date}T${minutes}${seconds}${offset}`);
|
|
34
|
+
if (Number.isNaN(at) || !timeSpan(date))
|
|
35
|
+
return undefined;
|
|
36
|
+
return instant(at);
|
|
37
|
+
}
|
|
38
|
+
const instant = (at) => ({ start: at, end: at + 1 });
|
|
39
|
+
/**
|
|
40
|
+
* The Revision a point names, over Revisions listed newest first. In order:
|
|
41
|
+
* 1. an exact tag: the newest Revision carrying it;
|
|
42
|
+
* 2. a day or a time: the last Revision dated before the span's end;
|
|
43
|
+
* 3. hex of 4 to 64 characters, any case: the one Revision whose sha starts with it.
|
|
44
|
+
* Anything else, or nothing found, is `not_found`; hex that starts several shas is `ambiguous`.
|
|
45
|
+
*/
|
|
46
|
+
export function find(revisions, point) {
|
|
47
|
+
const tagged = revisions.find((r) => r.tags.includes(point));
|
|
48
|
+
if (tagged)
|
|
49
|
+
return { ok: true, revision: tagged };
|
|
50
|
+
const span = timeSpan(point);
|
|
51
|
+
if (span) {
|
|
52
|
+
const last = revisions.find((r) => Date.parse(r.date) < span.end);
|
|
53
|
+
if (last)
|
|
54
|
+
return { ok: true, revision: last };
|
|
55
|
+
return notFound(revisions, revisions.at(-1));
|
|
56
|
+
}
|
|
57
|
+
if (HEX.test(point)) {
|
|
58
|
+
const prefix = point.toLowerCase();
|
|
59
|
+
const matches = revisions.filter((r) => r.sha.toLowerCase().startsWith(prefix));
|
|
60
|
+
if (matches.length === 1)
|
|
61
|
+
return { ok: true, revision: matches[0] };
|
|
62
|
+
if (matches.length > 1)
|
|
63
|
+
return { ok: false, kind: "ambiguous", count: matches.length, candidates: matches.slice(0, MAX_LISTED) };
|
|
64
|
+
}
|
|
65
|
+
return notFound(revisions);
|
|
66
|
+
}
|
|
67
|
+
function notFound(revisions, first) {
|
|
68
|
+
return { ok: false, kind: "not_found", tags: revisions.flatMap((r) => r.tags).slice(0, MAX_LISTED), ...(first ? { first } : {}) };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The time a point spans, for `since`/`until`/`at` filters: a tag or a sha
|
|
72
|
+
* spans its Revision's one instant, a day its whole UTC day, a time its one
|
|
73
|
+
* instant. A day or time spans itself even with no Revision in or before it.
|
|
74
|
+
* Keep a Revision dated `>=` the `since` span's start and `<` the `until` span's end.
|
|
75
|
+
*/
|
|
76
|
+
export function span(revisions, point) {
|
|
77
|
+
const tagged = revisions.find((r) => r.tags.includes(point));
|
|
78
|
+
if (!tagged) {
|
|
79
|
+
const own = timeSpan(point);
|
|
80
|
+
if (own)
|
|
81
|
+
return { ok: true, span: own };
|
|
82
|
+
}
|
|
83
|
+
const found = find(revisions, point);
|
|
84
|
+
return found.ok ? { ok: true, span: instant(Date.parse(found.revision.date)) } : found;
|
|
85
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Revision index and the layout of a published site. Browser-safe: the
|
|
3
|
+
* viewer imports it as MCP and the CLI do, so nothing here may import from
|
|
4
|
+
* `node:`. This file is the module's only entry.
|
|
5
|
+
*/
|
|
6
|
+
import type { ChangeCounts } from "../core/diff.ts";
|
|
7
|
+
/**
|
|
8
|
+
* The Revision index's own version, written under the JSON key
|
|
9
|
+
* `schemaVersion`. It is independent of the Bundle's `SCHEMA_VERSION`,
|
|
10
|
+
* though both are 1 today: either can change without the other.
|
|
11
|
+
*/
|
|
12
|
+
export declare const INDEX_VERSION = 1;
|
|
13
|
+
/** One Revision as the index records it; `bundle: false` marks a Revision whose Bundle was dropped. */
|
|
14
|
+
export interface RevisionEntry {
|
|
15
|
+
sha: string;
|
|
16
|
+
date: string;
|
|
17
|
+
subject: string;
|
|
18
|
+
author: string;
|
|
19
|
+
tags: string[];
|
|
20
|
+
/** Changes against the previous Revision (or an empty Model for the first). */
|
|
21
|
+
counts: ChangeCounts;
|
|
22
|
+
changedIds: string[];
|
|
23
|
+
bundle: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** What a preview site's index adds: the base branch and both sides' shas, so the viewer opens with Compare "base → PR". */
|
|
26
|
+
export interface PreviewMark {
|
|
27
|
+
base: string;
|
|
28
|
+
baseSha: string;
|
|
29
|
+
headSha: string;
|
|
30
|
+
}
|
|
31
|
+
/** The Revision index, as `bundles/index.json` and a checkout's cache hold it. */
|
|
32
|
+
export interface RevisionIndex {
|
|
33
|
+
/** {@link INDEX_VERSION}, not the Bundle's schema version. */
|
|
34
|
+
schemaVersion: number;
|
|
35
|
+
/** Newest first. */
|
|
36
|
+
revisions: RevisionEntry[];
|
|
37
|
+
/** Set by `build --preview`. */
|
|
38
|
+
preview?: PreviewMark;
|
|
39
|
+
}
|
|
40
|
+
/** A site's paths, relative to its root with `/` separators. */
|
|
41
|
+
export declare const LATEST_BUNDLE_PATH = "workspace.bundle.json";
|
|
42
|
+
/** The file that names the Workspace for the viewer's exports. */
|
|
43
|
+
export declare const SITE_FILE_PATH = "site.json";
|
|
44
|
+
/** The folder of past Bundles and the Revision index. */
|
|
45
|
+
export declare const BUNDLES_DIR = "bundles";
|
|
46
|
+
export declare const INDEX_PATH = "bundles/index.json";
|
|
47
|
+
/** A past Bundle's path: `bundles/<sha>.json`. */
|
|
48
|
+
export declare const pastBundlePath: (sha: string) => string;
|
|
49
|
+
/** A past Bundle's file name within `bundles/`. */
|
|
50
|
+
export declare const pastBundleFile: (sha: string) => string;
|
|
51
|
+
/** Whether a file name in `bundles/` is a past Bundle's, named by its full sha. */
|
|
52
|
+
export declare const isPastBundleFile: (name: string) => boolean;
|
|
53
|
+
export { find, span, timeSpan, POINT_GRAMMAR, type Dated, type Found, type Span } from "./find.ts";
|
|
54
|
+
export { touching } from "./touching.ts";
|
|
55
|
+
export { migrateBundle, migrations, type Migration } from "./migrate.ts";
|
|
56
|
+
export { fetchReader, openSite, readBundle, readIndex, type HistoryUnavailable, type Site, type SiteOptions, type SiteRead, type SiteReader, type UnavailableReason, type Validators, } from "./site.ts";
|
|
57
|
+
export { serializeBundle, serializeIndex, serializeSiteFile } from "./text.ts";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Revision index's own version, written under the JSON key
|
|
3
|
+
* `schemaVersion`. It is independent of the Bundle's `SCHEMA_VERSION`,
|
|
4
|
+
* though both are 1 today: either can change without the other.
|
|
5
|
+
*/
|
|
6
|
+
export const INDEX_VERSION = 1;
|
|
7
|
+
/** A site's paths, relative to its root with `/` separators. */
|
|
8
|
+
export const LATEST_BUNDLE_PATH = "workspace.bundle.json";
|
|
9
|
+
/** The file that names the Workspace for the viewer's exports. */
|
|
10
|
+
export const SITE_FILE_PATH = "site.json";
|
|
11
|
+
/** The folder of past Bundles and the Revision index. */
|
|
12
|
+
export const BUNDLES_DIR = "bundles";
|
|
13
|
+
export const INDEX_PATH = `${BUNDLES_DIR}/index.json`;
|
|
14
|
+
/** A past Bundle's path: `bundles/<sha>.json`. */
|
|
15
|
+
export const pastBundlePath = (sha) => `${BUNDLES_DIR}/${pastBundleFile(sha)}`;
|
|
16
|
+
/** A past Bundle's file name within `bundles/`. */
|
|
17
|
+
export const pastBundleFile = (sha) => `${sha}.json`;
|
|
18
|
+
/** Whether a file name in `bundles/` is a past Bundle's, named by its full sha. */
|
|
19
|
+
export const isPastBundleFile = (name) => /^[0-9a-f]{40,64}\.json$/.test(name);
|
|
20
|
+
export { find, span, timeSpan, POINT_GRAMMAR } from "./find.js";
|
|
21
|
+
export { touching } from "./touching.js";
|
|
22
|
+
export { migrateBundle, migrations } from "./migrate.js";
|
|
23
|
+
export { fetchReader, openSite, readBundle, readIndex, } from "./site.js";
|
|
24
|
+
export { serializeBundle, serializeIndex, serializeSiteFile } from "./text.js";
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Bundle } from "../schema/bundle.ts";
|
|
2
|
+
import { type Migration } from "./migrate.ts";
|
|
3
|
+
import { type RevisionEntry, type RevisionIndex } from "./index.ts";
|
|
4
|
+
/**
|
|
5
|
+
* A Bundle read from a site, latest or past: migrated to this Archidocs's
|
|
6
|
+
* schema and parsed. Undefined when it is newer than this Archidocs, a
|
|
7
|
+
* migration is missing, or it is not a Bundle at all.
|
|
8
|
+
*/
|
|
9
|
+
export declare function readBundle(raw: unknown, chain?: Readonly<Record<number, Migration>>): Bundle | undefined;
|
|
10
|
+
/** A Revision index this version can read: {@link INDEX_VERSION} and an array of Revisions. Undefined otherwise. */
|
|
11
|
+
export declare function readIndex(raw: unknown): RevisionIndex | undefined;
|
|
12
|
+
/** What a host gave to tell whether a file has changed: an ETag, a Last-Modified date, or neither. */
|
|
13
|
+
export interface Validators {
|
|
14
|
+
etag?: string;
|
|
15
|
+
lastModified?: string;
|
|
16
|
+
}
|
|
17
|
+
/** A file read from a site: its text, unchanged since the validators sent, or not there (a 404 or ENOENT). */
|
|
18
|
+
export type SiteRead = {
|
|
19
|
+
kind: "text";
|
|
20
|
+
text: string;
|
|
21
|
+
validators: Validators;
|
|
22
|
+
} | {
|
|
23
|
+
kind: "not_modified";
|
|
24
|
+
} | {
|
|
25
|
+
kind: "missing";
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Reads files below a site's root. A failure other than a missing file
|
|
29
|
+
* throws, and its message names where it read from.
|
|
30
|
+
*/
|
|
31
|
+
export interface SiteReader {
|
|
32
|
+
/** Reads `path`, below the root with `/` separators; with validators, may answer "not modified". */
|
|
33
|
+
read(path: string, validators?: Validators): Promise<SiteRead>;
|
|
34
|
+
/** Where `path` is read from, a URL or a file path, for messages. */
|
|
35
|
+
location(path: string): string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A browser-safe {@link SiteReader} over `fetch`, rooted at `base` (a URL
|
|
39
|
+
* or a relative path such as `"./"`). It never uses the HTTP cache: the
|
|
40
|
+
* validators it is given go out as `If-None-Match` and `If-Modified-Since`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function fetchReader(base: string): SiteReader;
|
|
43
|
+
/** Why a site can't give history: a past Bundle dropped by retention, missing, unreadable or unmigratable, or an unreadable index. */
|
|
44
|
+
export type UnavailableReason = "dropped" | "missing" | "unreadable" | "unmigratable" | "index";
|
|
45
|
+
/** History the site can't give, with a reason and a message naming what is wrong. */
|
|
46
|
+
export interface HistoryUnavailable {
|
|
47
|
+
ok: false;
|
|
48
|
+
code: "history_unavailable";
|
|
49
|
+
reason: UnavailableReason;
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
52
|
+
export interface SiteOptions {
|
|
53
|
+
/** How long the latest Bundle and the index are trusted before the site is asked again; `Infinity` asks once. */
|
|
54
|
+
recheckMs: number;
|
|
55
|
+
/** The latest Bundle's path below the root. */
|
|
56
|
+
bundle?: string;
|
|
57
|
+
/** The migration chain; tests only, to migrate Bundles at a made-up older schema. */
|
|
58
|
+
migrations?: Readonly<Record<number, Migration>>;
|
|
59
|
+
}
|
|
60
|
+
/** A published site: its latest Bundle, its Revision index and its past Bundles. */
|
|
61
|
+
export interface Site {
|
|
62
|
+
/** Where the latest Bundle is read from. */
|
|
63
|
+
readonly location: string;
|
|
64
|
+
/** The latest Bundle. Rejects when it is missing, not JSON, or not a Bundle this version can read. */
|
|
65
|
+
latest(): Promise<Bundle>;
|
|
66
|
+
/** The Revision index; `index: undefined` when the site has none, which is no history. */
|
|
67
|
+
index(): Promise<{
|
|
68
|
+
ok: true;
|
|
69
|
+
index: RevisionIndex | undefined;
|
|
70
|
+
} | HistoryUnavailable>;
|
|
71
|
+
/** A past Revision's Bundle, fetched once; a failure is dropped so the next call asks again. */
|
|
72
|
+
bundle(revision: RevisionEntry): Promise<{
|
|
73
|
+
ok: true;
|
|
74
|
+
bundle: Bundle;
|
|
75
|
+
} | HistoryUnavailable>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Opens a published site through `reader`. The latest Bundle and the index
|
|
79
|
+
* are re-checked at most every `recheckMs`, with conditional requests when
|
|
80
|
+
* the host gave validators; past Bundles never change, so each is read once.
|
|
81
|
+
*/
|
|
82
|
+
export declare function openSite(reader: SiteReader, { recheckMs, bundle: latestPath, migrations: chain }: SiteOptions): Site;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { migrateBundle, migrations } from "./migrate.js";
|
|
2
|
+
import { INDEX_PATH, INDEX_VERSION, LATEST_BUNDLE_PATH, pastBundlePath } from "./index.js";
|
|
3
|
+
/**
|
|
4
|
+
* A Bundle read from a site, latest or past: migrated to this Archidocs's
|
|
5
|
+
* schema and parsed. Undefined when it is newer than this Archidocs, a
|
|
6
|
+
* migration is missing, or it is not a Bundle at all.
|
|
7
|
+
*/
|
|
8
|
+
export function readBundle(raw, chain = migrations) {
|
|
9
|
+
return migrateBundle(raw, chain);
|
|
10
|
+
}
|
|
11
|
+
/** A Revision index this version can read: {@link INDEX_VERSION} and an array of Revisions. Undefined otherwise. */
|
|
12
|
+
export function readIndex(raw) {
|
|
13
|
+
if (raw === null || typeof raw !== "object")
|
|
14
|
+
return undefined;
|
|
15
|
+
const { schemaVersion, revisions, preview } = raw;
|
|
16
|
+
if (schemaVersion !== INDEX_VERSION || !Array.isArray(revisions))
|
|
17
|
+
return undefined;
|
|
18
|
+
return {
|
|
19
|
+
schemaVersion,
|
|
20
|
+
revisions: revisions,
|
|
21
|
+
...(typeof preview?.baseSha === "string" ? { preview: preview } : {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A browser-safe {@link SiteReader} over `fetch`, rooted at `base` (a URL
|
|
26
|
+
* or a relative path such as `"./"`). It never uses the HTTP cache: the
|
|
27
|
+
* validators it is given go out as `If-None-Match` and `If-Modified-Since`.
|
|
28
|
+
*/
|
|
29
|
+
export function fetchReader(base) {
|
|
30
|
+
const root = base.endsWith("/") ? base : `${base}/`;
|
|
31
|
+
const location = (path) => `${root}${path}`;
|
|
32
|
+
return {
|
|
33
|
+
location,
|
|
34
|
+
async read(path, validators) {
|
|
35
|
+
const where = location(path);
|
|
36
|
+
const headers = { accept: "application/json" };
|
|
37
|
+
if (validators?.etag)
|
|
38
|
+
headers["if-none-match"] = validators.etag;
|
|
39
|
+
if (validators?.lastModified)
|
|
40
|
+
headers["if-modified-since"] = validators.lastModified;
|
|
41
|
+
const response = await fetch(where, { headers, cache: "no-store" });
|
|
42
|
+
if (response.status === 304)
|
|
43
|
+
return { kind: "not_modified" };
|
|
44
|
+
if (response.status === 404)
|
|
45
|
+
return { kind: "missing" };
|
|
46
|
+
if (!response.ok)
|
|
47
|
+
throw new Error(`${where} answered ${response.status} ${response.statusText}`.trim());
|
|
48
|
+
const etag = response.headers.get("etag");
|
|
49
|
+
const lastModified = response.headers.get("last-modified");
|
|
50
|
+
return { kind: "text", text: await response.text(), validators: { ...(etag ? { etag } : {}), ...(lastModified ? { lastModified } : {}) } };
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const unavailable = (reason, message) => ({ ok: false, code: "history_unavailable", reason, message });
|
|
55
|
+
const errorText = (error) => (error instanceof Error ? error.message : String(error));
|
|
56
|
+
/**
|
|
57
|
+
* One file re-read at most every `recheckMs`, sending the validators of the
|
|
58
|
+
* text it holds. `interpret` turns the text (undefined when missing) into a
|
|
59
|
+
* value; only values `keep` accepts are held, so a failure is asked again.
|
|
60
|
+
*/
|
|
61
|
+
function rechecked(reader, path, recheckMs, interpret, keep = () => true) {
|
|
62
|
+
let held;
|
|
63
|
+
return async () => {
|
|
64
|
+
const now = Date.now();
|
|
65
|
+
if (held && now - held.at < recheckMs)
|
|
66
|
+
return held.value;
|
|
67
|
+
const read = await reader.read(path, held?.validators);
|
|
68
|
+
if (read.kind === "not_modified") {
|
|
69
|
+
if (!held)
|
|
70
|
+
throw new Error(`${reader.location(path)} answered "not modified" to a request with no validators.`);
|
|
71
|
+
held = { ...held, at: now };
|
|
72
|
+
return held.value;
|
|
73
|
+
}
|
|
74
|
+
const value = interpret(read.kind === "text" ? read.text : undefined);
|
|
75
|
+
if (keep(value))
|
|
76
|
+
held = { value, at: now, validators: read.kind === "text" ? read.validators : {} };
|
|
77
|
+
return value;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const parseJson = (text) => {
|
|
81
|
+
try {
|
|
82
|
+
return { ok: true, value: JSON.parse(text) };
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return { ok: false };
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Opens a published site through `reader`. The latest Bundle and the index
|
|
90
|
+
* are re-checked at most every `recheckMs`, with conditional requests when
|
|
91
|
+
* the host gave validators; past Bundles never change, so each is read once.
|
|
92
|
+
*/
|
|
93
|
+
export function openSite(reader, { recheckMs, bundle: latestPath = LATEST_BUNDLE_PATH, migrations: chain = migrations }) {
|
|
94
|
+
const location = reader.location(latestPath);
|
|
95
|
+
const latest = rechecked(reader, latestPath, recheckMs, (text) => {
|
|
96
|
+
if (text === undefined)
|
|
97
|
+
throw new Error(`${location} was not found.`);
|
|
98
|
+
const json = parseJson(text);
|
|
99
|
+
if (!json.ok)
|
|
100
|
+
throw new Error(`${location} is not JSON.`);
|
|
101
|
+
const read = readBundle(json.value, chain);
|
|
102
|
+
if (!read)
|
|
103
|
+
throw new Error(`${location} is not a Workspace Bundle this version can read.`);
|
|
104
|
+
return read;
|
|
105
|
+
});
|
|
106
|
+
const index = rechecked(reader, INDEX_PATH, recheckMs, (text) => {
|
|
107
|
+
if (text === undefined)
|
|
108
|
+
return { ok: true, index: undefined };
|
|
109
|
+
const json = parseJson(text);
|
|
110
|
+
if (!json.ok)
|
|
111
|
+
return unavailable("index", `The site has no readable Revision index (${INDEX_PATH}): ${reader.location(INDEX_PATH)} is not JSON.`);
|
|
112
|
+
const read = readIndex(json.value);
|
|
113
|
+
return read ? { ok: true, index: read } : unavailable("index", `The site's ${INDEX_PATH} holds no Revisions.`);
|
|
114
|
+
}, (value) => value.ok);
|
|
115
|
+
const pending = new Map();
|
|
116
|
+
const past = async (sha) => {
|
|
117
|
+
const short = sha.slice(0, 7);
|
|
118
|
+
let read;
|
|
119
|
+
try {
|
|
120
|
+
read = await reader.read(pastBundlePath(sha));
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return unavailable("unreadable", `The Bundle at ${short} can't be read from the site: ${errorText(error)}`);
|
|
124
|
+
}
|
|
125
|
+
if (read.kind !== "text")
|
|
126
|
+
return unavailable("missing", `The Bundle at ${short} can't be read from the site: ${reader.location(pastBundlePath(sha))} was not found.`);
|
|
127
|
+
const json = parseJson(read.text);
|
|
128
|
+
if (!json.ok)
|
|
129
|
+
return unavailable("unreadable", `The Bundle at ${short} can't be read from the site: ${reader.location(pastBundlePath(sha))} is not JSON.`);
|
|
130
|
+
const bundle = readBundle(json.value, chain);
|
|
131
|
+
return bundle ? { ok: true, bundle } : unavailable("unmigratable", `The Bundle at ${short} has a schema this Archidocs can't read or migrate.`);
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
location,
|
|
135
|
+
latest,
|
|
136
|
+
async index() {
|
|
137
|
+
try {
|
|
138
|
+
return await index();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
return unavailable("index", `The site has no readable Revision index (${INDEX_PATH}): ${errorText(error)}`);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
bundle(revision) {
|
|
145
|
+
if (revision.bundle === false)
|
|
146
|
+
return Promise.resolve(unavailable("dropped", `The Bundle at ${revision.sha.slice(0, 7)} (${revision.date}) was not kept on this site.`));
|
|
147
|
+
let read = pending.get(revision.sha);
|
|
148
|
+
if (!read) {
|
|
149
|
+
read = past(revision.sha);
|
|
150
|
+
pending.set(revision.sha, read);
|
|
151
|
+
// A failure may be a host's hiccup; ask again next time.
|
|
152
|
+
void read.then((r) => !r.ok && pending.delete(revision.sha));
|
|
153
|
+
}
|
|
154
|
+
return read;
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Bundle } from "../schema/bundle.ts";
|
|
2
|
+
import type { RevisionIndex } from "./index.ts";
|
|
3
|
+
/** The Bundle as written to `workspace.bundle.json` (and to `bundles/<sha>.json`): stable key order, two-space indent. */
|
|
4
|
+
export declare function serializeBundle(bundle: Bundle): string;
|
|
5
|
+
/** `site.json`: the Workspace's name, for the viewer's exports. */
|
|
6
|
+
export declare function serializeSiteFile(workspace: string): string;
|
|
7
|
+
/** `bundles/index.json`: `schemaVersion`, then `preview` when set, then the Revisions; two-space indent. */
|
|
8
|
+
export declare function serializeIndex({ schemaVersion, preview, revisions }: RevisionIndex): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Bundle } from "../schema/bundle.js";
|
|
2
|
+
/*
|
|
3
|
+
* The text of a site's small files. Every writer, and `dev`, which serves a
|
|
4
|
+
* site from memory, takes its bytes from here, so all of them agree.
|
|
5
|
+
*/
|
|
6
|
+
/** The Bundle as written to `workspace.bundle.json` (and to `bundles/<sha>.json`): stable key order, two-space indent. */
|
|
7
|
+
export function serializeBundle(bundle) {
|
|
8
|
+
return JSON.stringify(Bundle.parse(bundle), null, 2) + "\n";
|
|
9
|
+
}
|
|
10
|
+
/** `site.json`: the Workspace's name, for the viewer's exports. */
|
|
11
|
+
export function serializeSiteFile(workspace) {
|
|
12
|
+
return JSON.stringify({ workspace }) + "\n";
|
|
13
|
+
}
|
|
14
|
+
/** `bundles/index.json`: `schemaVersion`, then `preview` when set, then the Revisions; two-space indent. */
|
|
15
|
+
export function serializeIndex({ schemaVersion, preview, revisions }) {
|
|
16
|
+
return JSON.stringify(preview ? { schemaVersion, preview, revisions } : { schemaVersion, revisions }, null, 2) + "\n";
|
|
17
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Revisions whose changed ids satisfy `matches`, newest first as the
|
|
3
|
+
* index lists them. `matches` is usually core's `scopeMatcher`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function touching<R extends {
|
|
6
|
+
changedIds: string[];
|
|
7
|
+
}>(revisions: readonly R[], matches: (ids: string[]) => boolean): R[];
|