histoire
Version:
A fast story book powered by Vite
146 lines (145 loc) • 8.12 kB
JavaScript
import { getInstalledIndex } from '../inventory/index.js';
import { compatibility } from './compat.js';
import { discoverCatalog } from './discover.js';
import { CatalogNotFoundError } from './errors.js';
import { catalogFacets, filterCatalog } from './facets.js';
import { officialPlugins, officialReleases } from './official.js';
import { registryDetail, registryRelease } from './registry-entry.js';
import { createRegistryClient } from './registry.js';
import { searchCatalog } from './search.js';
import { detectProjectSignals } from './signals.js';
import { catalogQuerySchema, packageName, prefsSchema } from './validation.js';
/** Remove detail-only metadata from list replies to preserve channel capacity. */
function catalogEntry(detail) {
const { manifest: _manifest, versions: _versions, provenance: _provenance, requires: _requires, access: _access, releaseNotes: _notes, ...entry } = detail;
return entry;
}
/** Server-owned catalog with offline official fallback and consent-gated registry reads. */
export function createCatalogService(ctx, options = {}) {
// Resolve registry settings lazily: community-off must work even with a broken registry URL.
let registry;
/** One cached registry client per project context/service. */
const client = () => registry ??= createRegistryClient(ctx.root, options);
/** Apply project compatibility and inventory without mutating bundled or cached metadata. */
function decorate(detail, project, optionalPeers = []) {
const installed = getInstalledIndex(ctx).get(detail.id);
const compatible = compatibility(detail, project, optionalPeers);
return { ...detail, installed, compatible, versions: detail.versions.map(version => version.version === detail.latest
? { ...version, compatible: compatible.ok, ...(!compatible.ok ? { reason: compatible.reason } : {}) }
: version) };
}
/** Read all allowed sources; official index wins every collision. */
async function entries(prefs) {
const merged = new Map(officialPlugins.map(detail => [detail.id, { detail, optionalPeers: [] }]));
let community = 'off';
if (prefs.community) {
try {
const result = await client().catalog();
community = result.stale ? 'stale' : 'ok';
for (const pkg of result.packuments) {
if (merged.has(pkg.name))
continue;
const release = registryDetail(pkg);
if (release)
merged.set(pkg.name, release);
}
}
catch {
community = 'error';
}
}
const releases = [...merged.values()];
const project = await detectProjectSignals(ctx, releases.flatMap(release => Object.keys(release.detail.requires)));
return { entries: releases.map(release => decorate(release.detail, project, release.optionalPeers)), community, project };
}
/** Filter and rank every source entry before pagination, including facet alternatives. */
async function catalog(input) {
const query = catalogQuerySchema.parse(input);
const result = await entries(query.prefs);
const { project } = result;
// Collection membership narrows every facet alternative as well as paged rows.
const collection = query.filters?.collection;
const members = collection ? result.entries.filter(entry => entry.manifest.collections?.includes(collection)) : result.entries;
const ranked = searchCatalog(members, query.query);
const filtered = filterCatalog(ranked, query.filters);
if (query.sort === 'name')
filtered.sort((a, b) => a.displayName.localeCompare(b.displayName) || a.id.localeCompare(b.id));
if (query.sort === 'updated')
filtered.sort((a, b) => (b.updatedAt ?? '').localeCompare(a.updatedAt ?? '') || a.id.localeCompare(b.id));
const offset = Number(query.cursor ?? 0);
const limit = query.limit ?? 30;
const page = {
entries: filtered.slice(offset, offset + limit).map(catalogEntry),
total: filtered.length,
cursor: offset + limit < filtered.length ? String(offset + limit) : undefined,
facets: catalogFacets(ranked, query.filters, project.project.frameworks),
project: project.project,
sources: { official: 'ok', community: result.community },
};
if (query.view === 'discover') {
const installedCategories = new Set(result.entries.filter(entry => entry.installed).map(entry => entry.category));
const discover = discoverCatalog(filtered, project, installedCategories);
page.discover = {
hero: discover.hero ? { ...catalogEntry(discover.hero), reason: discover.hero.reason } : undefined,
picks: discover.picks.map(entry => ({ ...catalogEntry(entry), reason: entry.reason })),
collections: discover.collections.map(collection => ({ ...collection, entries: collection.entries.slice(0, 12).map(catalogEntry) })),
};
}
return page;
}
/** Exact details require explicit community consent unless identity is bundled. */
async function detail(id, prefs) {
packageName.parse(id);
prefsSchema.parse(prefs);
const official = officialPlugins.find(entry => entry.id === id);
if (official) {
// Bundled history carries each version's own requirements, so older rows get real verdicts.
const releases = officialReleases(id);
const project = await detectProjectSignals(ctx, [official, ...releases].flatMap(release => Object.keys(release.requires)));
const output = decorate(official, project);
if (releases.length) {
output.versions = releases.map((release) => {
const verdict = decorate(release, project).compatible;
return { version: release.latest, compatible: verdict.ok, ...(release.updatedAt ? { date: release.updatedAt } : {}), ...(!verdict.ok ? { reason: verdict.reason } : {}) };
});
}
return output;
}
if (!prefs.community)
throw new CatalogNotFoundError();
const { value: pkg } = await client().packument(id);
const release = registryDetail(pkg);
if (!release)
throw new CatalogNotFoundError();
const project = await detectProjectSignals(ctx, [
...Object.keys(release.detail.requires),
...release.detail.versions.flatMap(version => Object.keys(registryRelease(pkg, version.version)?.detail.requires ?? {})),
]);
const output = decorate(release.detail, project, release.optionalPeers);
output.versions = output.versions.map((version) => {
const entry = registryRelease(pkg, version.version);
const verdict = entry ? decorate(entry.detail, project, entry.optionalPeers).compatible : { ok: false, reason: 'Marketplace metadata unavailable' };
return { ...version, compatible: verdict.ok, ...(!verdict.ok ? { reason: verdict.reason } : {}) };
});
return output;
}
return {
catalog,
detail,
/** Exact metadata fetch shared by update checks, migrations and failure advice. */
packument: (id) => client().packument(id),
/** Read-only planning lookup through the same registry configuration and identity guards. */
cachedPackument: (id) => client().cachedPackument(id),
};
}
/** Context lifetime owns registry cache/in-flight sharing across transport consumers. */
const services = new WeakMap();
/** Shared catalog export for inventory, planning and jobs. */
export function getCatalogService(ctx, options = {}) {
let service = services.get(ctx);
if (!service) {
service = createCatalogService(ctx, options);
services.set(ctx, service);
}
return service;
}