3D engine that AI agents drive
Add a marketplace page for packs and addons (#984)
Nguyen Xuan Anafd57f3de1b0
diff --git a/.gitignore b/.gitignoreindex d1893c9e..429e1aa3 100644--- a/.gitignore+++ b/.gitignore@@ -3,6 +3,7 @@ dist/build/.wrangler/public/releases/+public/packs/public/release.json# Guides copied from docs/ at build timepublic/*.mddiff --git a/changelog.html b/changelog.htmlindex 8331ca66..b4ce901b 100644--- a/changelog.html+++ b/changelog.html@@ -6,7 +6,7 @@<header class="navigation"><a class="brand" href="/">67js</a><span class="version" data-release-version>Loading release</span>- <nav aria-label="Library"><a href="/docs.html">Docs</a><a href="/changelog.html" aria-current="page">Changelog</a></nav>+ <nav aria-label="Library"><a href="/docs.html">Docs</a><a href="/marketplace.html">Marketplace</a><a href="/changelog.html" aria-current="page">Changelog</a></nav><a class="site-nav-cta" href="https://github.com/codepawl/67.js" aria-label="GitHub"><span class="package-logo package-logo-github" aria-hidden="true"></span><span class="site-nav-cta-label">GitHub</span></a></header><main id="content" class="docs docs-page">diff --git a/changes/984-marketplace.md b/changes/984-marketplace.mdnew file mode 100644index 00000000..99bdfef1--- /dev/null+++ b/changes/984-marketplace.md@@ -0,0 +1 @@+- Add a Marketplace page that lists the data packs built from `packs/` (zip, size and SHA-256) and the third-party addons found on npm (#984, #985)diff --git a/docs.html b/docs.htmlindex 84aceb65..76eb884b 100644--- a/docs.html+++ b/docs.html@@ -6,7 +6,7 @@<header class="navigation"><a class="brand" href="/">67js</a><span class="version" data-release-version>Loading release</span>- <nav aria-label="Library"><a href="/docs.html" aria-current="page">Docs</a><a href="/changelog.html">Changelog</a></nav>+ <nav aria-label="Library"><a href="/docs.html" aria-current="page">Docs</a><a href="/marketplace.html">Marketplace</a><a href="/changelog.html">Changelog</a></nav><a class="site-nav-cta" href="https://github.com/codepawl/67.js" aria-label="GitHub"><span class="package-logo package-logo-github" aria-hidden="true"></span><span class="site-nav-cta-label">GitHub</span></a></header><main id="content" class="docs docs-page">diff --git a/docs/packs.md b/docs/packs.mdindex 467dc08f..1b37ccee 100644--- a/docs/packs.md+++ b/docs/packs.md@@ -63,7 +63,7 @@ It runs the same checks as the server and prints `ok` or `FAIL` with the error.## Install-Unzip each pack into its own subfolder of one packs folder and start the server with that folder:+The [Marketplace page](https://67js.codepawl.com/marketplace.html) lists the published packs with a checksum. Unzip each pack into its own subfolder of one packs folder and start the server with that folder:```sh67js-mcp --packs /absolute/path/to/packsdiff --git a/index.html b/index.htmlindex f22a209f..34b0ddd4 100644--- a/index.html+++ b/index.html@@ -70,7 +70,7 @@<a class="brand" href="/" aria-label="67js home">67js</a><span class="version" data-release-version>Loading release</span><nav aria-label="Library">- <a href="/docs.html">Docs</a><a href="/changelog.html">Changelog</a>+ <a href="/docs.html">Docs</a><a href="/marketplace.html">Marketplace</a><a href="/changelog.html">Changelog</a></nav><a class="site-nav-cta" href="https://github.com/codepawl/67.js" aria-label="GitHub"><span class="package-logo package-logo-github" aria-hidden="true"></span><span class="site-nav-cta-label">GitHub</span></a></header>diff --git a/marketplace.html b/marketplace.htmlnew file mode 100644index 00000000..465cbf06--- /dev/null+++ b/marketplace.html@@ -0,0 +1,64 @@+<!doctype html>+<html lang="en" data-theme="afterimage">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="description" content="Download data packs of materials, looks, HDRIs, models and recipes for 67js, and find code addons from other people." />+ <link rel="canonical" href="https://67js.codepawl.com/marketplace.html" />+ <title>Marketplace · 67js</title>+ <link rel="icon" href="/favicon.svg" />+ </head>+ <body class="site marketplace">+ <a class="skip-link" href="#content">Skip to content</a>+ <header class="navigation">+ <a class="brand" href="/" aria-label="67js home">67js</a>+ <nav aria-label="Library">+ <a href="/docs.html">Docs</a><a href="/marketplace.html" aria-current="page">Marketplace</a><a href="/changelog.html">Changelog</a>+ </nav>+ <a class="site-nav-cta" href="https://github.com/codepawl/67.js" aria-label="GitHub"><span class="package-logo package-logo-github" aria-hidden="true"></span><span class="site-nav-cta-label">GitHub</span></a>+ </header>+ <main id="content">+ <h1>Marketplace</h1>+ <section id="packs" aria-labelledby="packs-title">+ <div class="section-heading">+ <h2 id="packs-title">Packs</h2>+ <a href="https://github.com/codepawl/67.js/blob/main/docs/packs.md">Make a pack</a>+ </div>+ <div id="pack-list" class="market-list" aria-live="polite"></div>+ <div class="market-install">+ <h3>Install a pack</h3>+ <ol>+ <li>+ <p>Unzip it into its own folder inside your packs folder.</p>+ <div class="command-row"><code>unzip example-starter-1.0.0.zip -d packs/example-starter</code><button type="button" data-copy="unzip example-starter-1.0.0.zip -d packs/example-starter">Copy</button></div>+ </li>+ <li>+ <p>Start the MCP server with that folder.</p>+ <div class="command-row"><code>67js-mcp --packs /absolute/path/to/packs</code><button type="button" data-copy="67js-mcp --packs /absolute/path/to/packs">Copy</button></div>+ </li>+ <li>+ <p>Check a pack from a 67js checkout.</p>+ <div class="command-row"><code>npm run pack:check -- packs/example-starter</code><button type="button" data-copy="npm run pack:check -- packs/example-starter">Copy</button></div>+ </li>+ </ol>+ </div>+ </section>+ <section id="addons" aria-labelledby="addons-title">+ <div class="section-heading">+ <h2 id="addons-title">Addons</h2>+ <a href="https://github.com/codepawl/67.js/blob/main/docs/addons.md">Write an addon</a>+ </div>+ <p class="market-trust">+ <svg class="market-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="m8 6-6 6 6 6m8-12 6 6-6 6m-3-15-2 18"/></svg>+ <span>An addon runs with the full rights of your page or Node process. Install only addons you trust and read what they do first. The MCP server never loads an addon from a tool call.</span>+ </p>+ <div id="addon-list" class="market-list" aria-live="polite"></div>+ </section>+ </main>+ <footer class="site-footer" aria-label="Site footer">+ <p><a class="brand" href="/" aria-label="67js home">67js</a> by <a href="https://codepawl.com">CodePawl</a></p>+ <nav class="footer-links" aria-label="Project links"><a href="https://github.com/codepawl/67.js"><span class="package-logo package-logo-github" aria-hidden="true"></span>GitHub</a><a href="https://www.npmjs.com/package/@codepawlhq/67js"><span class="package-logo package-logo-npm" aria-hidden="true"></span>npm</a><a href="/changelog.md">Changelog</a><a href="/license.txt">MIT license</a></nav>+ </footer>+ <script type="module" src="/src/marketplace.js"></script>+ </body>+</html>diff --git a/package.json b/package.jsonindex e6f9f50c..1f498b82 100644--- a/package.json+++ b/package.json@@ -150,7 +150,8 @@"prepare": "node scripts/install-git-hooks.mjs","dev": "vite --host 127.0.0.1","pack:check": "node scripts/pack-check.mjs",- "build": "node scripts/prepare-release.mjs && vite build",+ "packs:index": "node scripts/build-pack-index.mjs",+ "build": "node scripts/prepare-release.mjs && node scripts/build-pack-index.mjs && vite build","build:lib": "vite build --config vite.lib.config.js && vite build --config vite.browser.config.js","prepare:release": "node scripts/prepare-release.mjs","prepack": "npm run build:lib",diff --git a/scripts/build-pack-index.mjs b/scripts/build-pack-index.mjsnew file mode 100644index 00000000..42685ee1--- /dev/null+++ b/scripts/build-pack-index.mjs@@ -0,0 +1,174 @@+#!/usr/bin/env node+// Builds the Marketplace downloads: validates every folder under packs/, zips it the same way every time and writes+// public/packs/<name>-<version>.zip, a preview image and public/packs/index.json. A pack that fails the pack check stops the build.+// Usage: node scripts/build-pack-index.mjs [--packs <folder>] [--out <folder>]+import { createHash } from 'node:crypto';+import { copyFile, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises';+import { extname, join, resolve } from 'node:path';+import { fileURLToPath } from 'node:url';+import { crc32, deflateRawSync } from 'node:zlib';+import packageMetadata from '../package.json' with { type: 'json' };+import { PACK_MANIFEST_FILE } from '../src/pack-manifest.js';+import { loadPack } from './agent-packs.mjs';+import { validatePackScene } from './pack-scene-validator.mjs';++const repositoryRoot = fileURLToPath(new URL('..', import.meta.url));+const FIXED_DOS_TIME = 0;+const FIXED_DOS_DATE = 0x0021; // 1980-01-01, the earliest date a zip can hold+const STORED = 0;+const DEFLATED = 8;++async function listFiles(directory, prefix = '') {+ const found = [];+ for (const entry of await readdir(directory, { withFileTypes: true })) {+ const relative = prefix ? `${prefix}/${entry.name}` : entry.name;+ if (entry.isDirectory()) found.push(...await listFiles(join(directory, entry.name), relative));+ else if (entry.isFile()) found.push(relative);+ }+ return found;+}++function compress(bytes) {+ const deflated = deflateRawSync(bytes, { level: 9 });+ return deflated.length < bytes.length ? { method: DEFLATED, data: deflated } : { method: STORED, data: bytes };+}++function localHeader(nameBytes, entry) {+ const header = Buffer.alloc(30);+ header.writeUInt32LE(0x04034b50, 0);+ header.writeUInt16LE(20, 4);+ header.writeUInt16LE(0, 6);+ header.writeUInt16LE(entry.method, 8);+ header.writeUInt16LE(FIXED_DOS_TIME, 10);+ header.writeUInt16LE(FIXED_DOS_DATE, 12);+ header.writeUInt32LE(entry.checksum, 14);+ header.writeUInt32LE(entry.data.length, 18);+ header.writeUInt32LE(entry.size, 22);+ header.writeUInt16LE(nameBytes.length, 26);+ return Buffer.concat([header, nameBytes]);+}++function centralHeader(nameBytes, entry, offset) {+ const header = Buffer.alloc(46);+ header.writeUInt32LE(0x02014b50, 0);+ header.writeUInt16LE(20, 4);+ header.writeUInt16LE(20, 6);+ header.writeUInt16LE(entry.method, 10);+ header.writeUInt16LE(FIXED_DOS_TIME, 12);+ header.writeUInt16LE(FIXED_DOS_DATE, 14);+ header.writeUInt32LE(entry.checksum, 16);+ header.writeUInt32LE(entry.data.length, 20);+ header.writeUInt32LE(entry.size, 24);+ header.writeUInt16LE(nameBytes.length, 28);+ header.writeUInt32LE(0o100644 * 0x10000, 38);+ header.writeUInt32LE(offset, 42);+ return Buffer.concat([header, nameBytes]);+}++/** Zips `files` (relative path to bytes). Sorted paths, fixed timestamps and fixed attributes make equal input give equal bytes. */+export function createZip(files) {+ const names = [...files.keys()].sort((first, second) => Buffer.compare(Buffer.from(first), Buffer.from(second)));+ const localParts = [];+ const centralParts = [];+ let offset = 0;+ for (const name of names) {+ const bytes = files.get(name);+ const nameBytes = Buffer.from(name, 'utf8');+ const entry = { ...compress(bytes), checksum: crc32(bytes), size: bytes.length };+ const local = Buffer.concat([localHeader(nameBytes, entry), entry.data]);+ centralParts.push(centralHeader(nameBytes, entry, offset));+ localParts.push(local);+ offset += local.length;+ }+ const centralDirectory = Buffer.concat(centralParts);+ const end = Buffer.alloc(22);+ end.writeUInt32LE(0x06054b50, 0);+ end.writeUInt16LE(names.length, 8);+ end.writeUInt16LE(names.length, 10);+ end.writeUInt32LE(centralDirectory.length, 12);+ end.writeUInt32LE(offset, 16);+ return Buffer.concat([...localParts, centralDirectory, end]);+}++async function readPackFiles(folder) {+ const files = new Map();+ for (const relative of await listFiles(folder)) files.set(relative, await readFile(join(folder, relative)));+ return files;+}++async function describePack(folder, outputDirectory) {+ const pack = await loadPack(folder, { packageVersion: packageMetadata.version, validateScene: validatePackScene });+ const manifest = JSON.parse(await readFile(join(folder, PACK_MANIFEST_FILE), 'utf8'));+ const stem = `${pack.name}-${pack.version}`;+ const zip = createZip(await readPackFiles(folder));+ await writeFile(join(outputDirectory, `${stem}.zip`), zip);+ const previewFile = Object.values(manifest.contents).flat().find(item => item.preview)?.preview ?? null;+ let preview = null;+ if (previewFile) {+ const previewName = `${stem}-preview${extname(previewFile).toLowerCase()}`;+ await copyFile(join(folder, previewFile), join(outputDirectory, previewName));+ preview = `/packs/${previewName}`;+ }+ const counts = Object.fromEntries(Object.entries(pack.items).filter(([, items]) => items.length > 0).map(([kind, items]) => [kind, items.length]));+ return {+ name: pack.name,+ version: pack.version,+ engines: manifest.engines['67js'],+ license: pack.license,+ author: pack.author,+ description: pack.description,+ items: counts,+ preview,+ zip: `/packs/${stem}.zip`,+ bytes: zip.length,+ sha256: createHash('sha256').update(zip).digest('hex'),+ };+}++/** Validates and zips every pack folder. Throws one error that lists every pack that failed. */+export async function buildPackIndex({ packsDirectory = join(repositoryRoot, 'packs'), outputDirectory = join(repositoryRoot, 'public/packs') } = {}) {+ const source = resolve(packsDirectory);+ const output = resolve(outputDirectory);+ const folders = [];+ for (const entry of await readdir(source, { withFileTypes: true }).catch(() => [])) {+ if (entry.isDirectory() || entry.isSymbolicLink()) folders.push(entry.name);+ }+ folders.sort();+ await rm(output, { recursive: true, force: true });+ await mkdir(output, { recursive: true });+ const packs = [];+ const failures = [];+ for (const name of folders) {+ try {+ packs.push(await describePack(join(source, name), output));+ } catch (error) {+ failures.push(error.message);+ }+ }+ if (failures.length > 0) throw new Error(`Refusing to publish ${failures.length} invalid pack${failures.length === 1 ? '' : 's'}:\n${failures.join('\n')}`);+ const seen = new Set();+ for (const pack of packs) {+ const key = `${pack.name}-${pack.version}`;+ if (seen.has(key)) throw new Error(`Two pack folders publish ${key}. Give each pack its own name or version.`);+ seen.add(key);+ }+ packs.sort((first, second) => first.name.localeCompare(second.name));+ await writeFile(join(output, 'index.json'), `${JSON.stringify({ schemaVersion: 1, packs }, null, 2)}\n`);+ return packs;+}++function optionValue(name) {+ const position = process.argv.indexOf(name);+ return position === -1 ? undefined : process.argv[position + 1];+}++if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ try {+ const packs = await buildPackIndex({ packsDirectory: optionValue('--packs'), outputDirectory: optionValue('--out') });+ for (const pack of packs) console.log(`pack ${pack.name} ${pack.version} ${pack.bytes} bytes sha256 ${pack.sha256.slice(0, 12)}`);+ if (packs.length === 0) console.log('no packs found');+ } catch (error) {+ console.error(error.message);+ process.exit(1);+ }+}diff --git a/showcase.html b/showcase.htmlindex 8332eb3e..47ae1d53 100644--- a/showcase.html+++ b/showcase.html@@ -13,7 +13,7 @@<header class="navigation"><a class="brand" href="/" aria-label="67js home">67js</a><nav aria-label="Library">- <a href="/docs.html">Docs</a><a href="/changelog.html">Changelog</a>+ <a href="/docs.html">Docs</a><a href="/marketplace.html">Marketplace</a><a href="/changelog.html">Changelog</a></nav><a class="site-nav-cta" href="https://github.com/codepawl/67.js" aria-label="GitHub"><span class="package-logo package-logo-github" aria-hidden="true"></span><span class="site-nav-cta-label">GitHub</span></a></header>diff --git a/src/marketplace.css b/src/marketplace.cssnew file mode 100644index 00000000..ecc3d489--- /dev/null+++ b/src/marketplace.css@@ -0,0 +1,62 @@+/* The marketplace page: packs and addons as quiet surfaces, separated by spacing. No rules between rows. */+body.marketplace > main { gap: 40px; }+.marketplace main > section { display: grid; gap: 16px; }+.marketplace main > h1 { font-size: clamp(32px, 4.4vw, 48px); letter-spacing: -1.4px; }+.market-list { display: grid; gap: 12px; }+.market-icon { flex: none; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }++.market-card { display: grid; gap: 16px; padding: 16px; border-radius: var(--panel-radius); background: var(--bg); min-width: 0; }+.market-card h3 { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 10px; font-size: 18px; overflow-wrap: anywhere; }+.marketplace .market-card h3 a:hover { text-decoration: underline; }+.market-version { color: var(--muted); font-family: var(--mono); font-size: 13px; font-weight: 400; }+.market-description { color: var(--muted); font-size: 15px; }+.market-facts { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }+.market-facts li { padding: 3px 10px; border-radius: 999px; background: #ffffff12; color: var(--muted); font-size: 13px; }++.pack-card { grid-template-columns: 168px minmax(0, 1fr); align-items: start; }+.pack-preview { width: 168px; aspect-ratio: 1; object-fit: cover; border-radius: var(--radius); background: var(--window); }+.pack-preview-empty { display: grid; place-items: center; color: var(--muted); }+.pack-preview-empty .market-icon { width: 36px; height: 36px; }+.pack-body { display: grid; gap: 12px; min-width: 0; }+.pack-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; }+.pack-checksum { display: flex; align-items: center; gap: 8px; min-width: 0; color: var(--muted); font-size: 12px; }+.pack-checksum span { white-space: nowrap; }+.pack-checksum code { min-width: 0; overflow-wrap: anywhere; font-size: 12px; user-select: text; }++.marketplace .market-button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: var(--control-height); padding: 8px 18px; border-radius: 999px; background: var(--primary); color: var(--primary-text); font-size: 15px; font-weight: 500; user-select: none; }+.marketplace .market-button:hover { opacity: .88; }+.market-button-icon { width: 17px; height: 17px; }+.marketplace .market-button-quiet { background: #ffffff12; color: var(--text); }+.marketplace .market-button-quiet:hover { opacity: 1; background: #ffffff1f; }+.market-copy { min-height: 30px; padding: 4px 12px; font-size: 13px; }++.addon-card { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }+.addon-body { display: grid; gap: 8px; min-width: 0; }++.market-trust, .market-state { display: flex; align-items: flex-start; gap: 12px; padding: 14px 16px; border-radius: var(--panel-radius); background: var(--bg); color: var(--muted); font-size: 15px; }+.market-trust .market-icon { margin-top: 2px; color: var(--focus); }+.market-state { align-items: center; flex-wrap: wrap; }+.marketplace .market-state a { color: var(--text); text-decoration: underline; }+.market-state-error .market-icon { color: var(--error); }+@media (prefers-reduced-motion: no-preference) {+ .market-state-loading .market-icon { animation: market-spin 1s linear infinite; }+}+@keyframes market-spin { to { transform: rotate(360deg); } }++.market-install { display: grid; gap: 12px; margin-top: 12px; padding: 16px; border-radius: var(--panel-radius); background: var(--bg); }+.market-install ol { display: grid; gap: 16px; margin: 0; padding: 0; list-style: none; counter-reset: step; }+.market-install li { display: grid; gap: 8px; counter-increment: step; }+.market-install li p { color: var(--muted); font-size: 15px; }+.market-install li p::before { content: counter(step) ". "; font-variant-numeric: tabular-nums; }+.market-install .command-row code { user-select: text; }++.marketplace button, .marketplace .market-button, .marketplace .section-heading a { user-select: none; }++@media (max-width: 640px) {+ body.marketplace { --page: calc(100% - 32px); }+ .pack-card { grid-template-columns: minmax(0, 1fr); }+ .pack-preview { width: 100%; max-height: 220px; aspect-ratio: 16 / 9; }+ .addon-card { grid-template-columns: minmax(0, 1fr); align-items: start; }+ .addon-card .market-button { justify-self: start; }+ .pack-actions { align-items: flex-start; }+}diff --git a/src/marketplace.js b/src/marketplace.jsnew file mode 100644index 00000000..06442873--- /dev/null+++ b/src/marketplace.js@@ -0,0 +1,204 @@+import './site.css';+import './brand.js';+import './marketplace.css';++const NPM_SEARCH_URL = 'https://registry.npmjs.org/-/v1/search?text=keywords:67js-addon&size=50';+const NPM_MANIFEST_URL = 'https://registry.npmjs.org/';+const MAXIMUM_RANGE_LOOKUPS = 20;+const CONCURRENT_RANGE_LOOKUPS = 5;+const ADDONS_DOC_URL = 'https://github.com/codepawl/67.js/blob/main/docs/addons.md';+const KIND_LABELS = {+ assets: ['model', 'models'],+ images: ['image', 'images'],+ looks: ['look', 'looks'],+ environments: ['HDRI', 'HDRIs'],+ materials: ['material', 'materials'],+ recipes: ['recipe', 'recipes'],+};+const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';+// One shape per state, so the state reads without the text: a ring, a magnifier, a warning triangle.+const STATE_ICONS = {+ loading: 'M12 3a9 9 0 1 0 9 9',+ empty: 'M10.5 4a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Zm5 11.5L21 21M8 10.5h5',+ error: 'M12 3 2 20h20L12 3Zm0 6v5m0 3h.01',+};++function element(tag, className, text) {+ const node = document.createElement(tag);+ if (className) node.className = className;+ if (text !== undefined) node.textContent = text;+ return node;+}++function icon(path, className = 'market-icon') {+ const svg = document.createElementNS(SVG_NAMESPACE, 'svg');+ svg.setAttribute('viewBox', '0 0 24 24');+ svg.setAttribute('class', className);+ svg.setAttribute('aria-hidden', 'true');+ const shape = document.createElementNS(SVG_NAMESPACE, 'path');+ shape.setAttribute('d', path);+ svg.append(shape);+ return svg;+}++function link(text, href, className) {+ const anchor = element('a', className, text);+ anchor.href = href;+ return anchor;+}++function showState(container, state, message, ...links) {+ const notice = element('p', `market-state market-state-${state}`);+ notice.setAttribute('role', state === 'error' ? 'alert' : 'status');+ notice.append(icon(STATE_ICONS[state]), element('span', '', message), ...links);+ container.replaceChildren(notice);+}++function formatBytes(bytes) {+ if (bytes < 1024) return `${bytes} B`;+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;+}++function countLabel(kind, count) {+ const [singular, plural] = KIND_LABELS[kind] ?? [kind, kind];+ return `${count} ${count === 1 ? singular : plural}`;+}++async function copyText(button, text) {+ const label = button.textContent;+ try {+ await navigator.clipboard.writeText(text);+ button.textContent = 'Copied';+ } catch {+ button.textContent = 'Copy failed';+ }+ setTimeout(() => { button.textContent = label; }, 1800);+}++function copyButton(text, label = 'Copy') {+ const button = element('button', 'market-copy', label);+ button.type = 'button';+ button.addEventListener('click', () => copyText(button, text));+ return button;+}++function packCard(pack) {+ const card = element('article', 'market-card pack-card');+ if (pack.preview) {+ const preview = element('img', 'pack-preview');+ preview.src = pack.preview;+ preview.alt = `Preview of ${pack.name}`;+ preview.loading = 'lazy';+ card.append(preview);+ } else {+ const placeholder = element('div', 'pack-preview pack-preview-empty');+ placeholder.append(icon('M3 7 12 2l9 5v10l-9 5-9-5V7Zm0 0 9 5 9-5M12 12v10'));+ card.append(placeholder);+ }+ const body = element('div', 'pack-body');+ const title = element('h3', '', pack.name);+ title.append(element('span', 'market-version', pack.version));+ const facts = element('ul', 'market-facts');+ const factTexts = [...Object.entries(pack.items).map(([kind, count]) => countLabel(kind, count)), pack.license, `67js ${pack.engines}`, formatBytes(pack.bytes), `by ${pack.author}`];+ for (const text of factTexts) facts.append(element('li', '', text));+ const download = link('Download', pack.zip, 'market-button');+ download.setAttribute('download', '');+ download.prepend(icon('M12 3v12m-5-5 5 5 5-5M4 17v4h16v-4', 'market-icon market-button-icon'));+ const checksum = element('div', 'pack-checksum');+ checksum.append(element('span', '', 'SHA-256'), element('code', '', pack.sha256), copyButton(pack.sha256));+ const actions = element('div', 'pack-actions');+ actions.append(download, checksum);+ body.append(title, element('p', 'market-description', pack.description), facts, actions);+ card.append(body);+ return card;+}++async function loadPacks(container) {+ showState(container, 'loading', 'Loading packs');+ try {+ const response = await fetch('/packs/index.json');+ if (!response.ok) throw new Error(`HTTP ${response.status}`);+ const { packs } = await response.json();+ if (!Array.isArray(packs) || packs.length === 0) {+ showState(container, 'empty', 'No packs are published yet.', link('How to make one', 'https://github.com/codepawl/67.js/blob/main/docs/packs.md'));+ return;+ }+ container.replaceChildren(...packs.map(packCard));+ } catch {+ showState(container, 'error', 'The pack list did not load. Reload the page to try again.');+ }+}++function npmUrl(name) {+ return `https://www.npmjs.com/package/${name}`;+}++function addonCard(addon) {+ const card = element('article', 'market-card addon-card');+ card.dataset.addon = addon.name;+ const title = element('h3');+ title.append(link(addon.name, npmUrl(addon.name)), element('span', 'market-version', addon.version));+ const body = element('div', 'addon-body');+ body.append(title);+ if (addon.description) body.append(element('p', 'market-description', addon.description));+ const facts = element('ul', 'market-facts');+ facts.append(element('li', '', addon.range ? `67js ${addon.range}` : 'Range not stated'));+ body.append(facts);+ const npm = link('npm', npmUrl(addon.name), 'market-button market-button-quiet');+ const logo = element('span', 'package-logo package-logo-npm');+ logo.setAttribute('aria-hidden', 'true');+ npm.prepend(logo);+ npm.setAttribute('aria-label', `${addon.name} on npm`);+ card.append(body, npm);+ return card;+}++async function fetchLatestRange(name) {+ try {+ const response = await fetch(`${NPM_MANIFEST_URL}${name.replace('/', '%2F')}/latest`);+ if (!response.ok) return null;+ const manifest = await response.json();+ return typeof manifest['67js']?.range === 'string' ? manifest['67js'].range : null;+ } catch {+ return null;+ }+}++// The search result carries no `67js` field, so the latest manifest of each listed package is read, a few at a time.+async function fillRanges(addons, container) {+ const queue = addons.slice(0, MAXIMUM_RANGE_LOOKUPS);+ const workers = Array.from({ length: Math.min(CONCURRENT_RANGE_LOOKUPS, queue.length) }, async () => {+ while (queue.length > 0) {+ const addon = queue.shift();+ addon.range = await fetchLatestRange(addon.name);+ const row = container.querySelector(`[data-addon="${CSS.escape(addon.name)}"]`);+ if (row) row.replaceWith(addonCard(addon));+ }+ });+ await Promise.all(workers);+}++async function loadAddons(container) {+ showState(container, 'loading', 'Searching npm for addons');+ try {+ const response = await fetch(NPM_SEARCH_URL);+ if (!response.ok) throw new Error(`HTTP ${response.status}`);+ const { objects } = await response.json();+ const addons = (objects ?? []).map(({ package: entry }) => ({ name: entry.name, version: entry.version, description: entry.description ?? '', range: null }));+ if (addons.length === 0) {+ showState(container, 'empty', 'No addons are published yet.', link('Write the first one', ADDONS_DOC_URL));+ return;+ }+ container.replaceChildren(...addons.map(addonCard));+ await fillRanges(addons, container);+ } catch {+ showState(container, 'error', 'npm did not answer, so the addon list is missing. Search npm for 67js-addon instead.', link('Open the npm search', 'https://www.npmjs.com/search?q=keywords%3A67js-addon'));+ }+}++document.querySelectorAll('[data-copy]').forEach(button => {+ button.addEventListener('click', () => copyText(button, button.dataset.copy));+});+loadPacks(document.querySelector('#pack-list'));+loadAddons(document.querySelector('#addon-list'));diff --git a/tests/build-pack-index.test.js b/tests/build-pack-index.test.jsnew file mode 100644index 00000000..8ef5f50f--- /dev/null+++ b/tests/build-pack-index.test.js@@ -0,0 +1,66 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { createHash } from 'node:crypto';+import { cpSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { inflateRawSync } from 'node:zlib';+import { buildPackIndex, createZip } from '../scripts/build-pack-index.mjs';++const EXAMPLE = new URL('../packs/example-starter', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1');++function packsFolderWithExample(context) {+ const folder = mkdtempSync(join(tmpdir(), '67js-pack-index-'));+ context.after(() => rmSync(folder, { recursive: true, force: true }));+ cpSync(EXAMPLE, join(folder, 'packs/example-starter'), { recursive: true });+ return { packsDirectory: join(folder, 'packs'), outputDirectory: join(folder, 'out') };+}++test('a valid pack gives a zip, a preview and an index entry with the sha256 of the zip', async (context) => {+ const options = packsFolderWithExample(context);+ const packs = await buildPackIndex(options);+ assert.equal(packs.length, 1);+ const [pack] = packs;+ const zip = readFileSync(join(options.outputDirectory, 'example-starter-1.0.0.zip'));+ assert.equal(pack.sha256, createHash('sha256').update(zip).digest('hex'));+ assert.equal(pack.bytes, zip.length);+ assert.deepEqual(pack.items, { looks: 1, materials: 2, recipes: 1 });+ assert.equal(pack.engines, '>=0.19.0');+ assert.equal(pack.license, 'CC0-1.0');+ assert.equal(pack.preview, '/packs/example-starter-1.0.0-preview.png');+ assert.ok(readdirSync(options.outputDirectory).includes('example-starter-1.0.0-preview.png'));+ const index = JSON.parse(readFileSync(join(options.outputDirectory, 'index.json'), 'utf8'));+ assert.deepEqual(index.packs, packs);+});++test('an invalid pack stops the build with its own error', async (context) => {+ const options = packsFolderWithExample(context);+ writeFileSync(join(options.packsDirectory, 'example-starter/run.js'), 'console.log(1)');+ await assert.rejects(buildPackIndex(options), /Refusing to publish 1 invalid pack[\s\S]*example-starter[\s\S]*run\.js/);+ assert.ok(!readdirSync(options.outputDirectory).includes('index.json'));+});++test('two builds give identical bytes', async (context) => {+ const options = packsFolderWithExample(context);+ const readOutputs = () => ['example-starter-1.0.0.zip', 'index.json'].map(name => readFileSync(join(options.outputDirectory, name)));+ await buildPackIndex(options);+ const first = readOutputs();+ await buildPackIndex(options);+ assert.deepEqual(readOutputs(), first);+});++test('the zip sorts its entries and holds the original bytes', () => {+ const content = Buffer.from('hello hello hello hello hello hello');+ const zip = createZip(new Map([['b/second.txt', content], ['a.txt', Buffer.from('x')]]));+ assert.equal(zip.readUInt32LE(0), 0x04034b50);+ const nameLength = zip.readUInt16LE(26);+ assert.equal(zip.subarray(30, 30 + nameLength).toString(), 'a.txt');+ const end = zip.length - 22;+ assert.equal(zip.readUInt32LE(end), 0x06054b50);+ assert.equal(zip.readUInt16LE(end + 10), 2);+ const secondOffset = 30 + nameLength + 1;+ const secondNameLength = zip.readUInt16LE(secondOffset + 26);+ const compressedSize = zip.readUInt32LE(secondOffset + 18);+ const dataStart = secondOffset + 30 + secondNameLength;+ assert.deepEqual(inflateRawSync(zip.subarray(dataStart, dataStart + compressedSize)), content);+});diff --git a/vite.config.js b/vite.config.jsindex 06e7619c..dcff9c6d 100644--- a/vite.config.js+++ b/vite.config.js@@ -56,6 +56,7 @@ export default defineConfig({docs: 'docs.html',changelog: 'changelog.html',showcase: 'showcase.html',+ marketplace: 'marketplace.html',},},},