Elffuss BraidHub

utopia-ia/elffuss-code public

main · 4.0 KiBRawPermalink
1// Open VSX simulado para las pruebas del gestor de extensiones: búsqueda,
2// metadatos, package.json, suma SHA-256 y el .vsix, servidos con
3// context.route desde paquetes de las pruebas (los .vsix reales fijados de
4// tests/.cache/vsix o manifiestos sintéticos). Apunta cada petición con el
5// marco que la hizo, para comprobar que solo la página de Code habla con
6// Open VSX y nunca el recinto.
7//
8// const mock = await mockOpenVsx(ctx, [{ namespace, name, version, manifest, vsix?, published?, keywords? }])
9// mock.log → [{ url, path, frame, method }]; downloads() (GET of a .vsix) and
10// sizeLookups() (HEAD of a .vsix, for its size before the consent)
11const API = 'https://open-vsx.org';
12
13export async function mockOpenVsx(ctx, packages) {
14 const log = [];
15 const key = p => `${p.namespace}/${p.name}/${p.version}`.toLowerCase();
16 const byKey = new Map(packages.map(p => [key(p), p]));
17 const base = p => `${API}/api/${p.namespace}/${p.name}/${p.version}`;
18 const summary = p => ({
19 namespace: p.namespace, name: p.name, version: p.version, displayName: p.manifest.displayName || p.name,
20 description: p.description || p.manifest.description || '', downloadCount: p.downloadCount || 1, verified: true, files: {},
21 });
22 await ctx.route(`${API}/**`, async route => {
23 const req = route.request();
24 const u = new URL(req.url());
25 let frame = '';
26 try { frame = req.frame()?.url() || ''; } catch { /* de un worker */ }
27 log.push({ url: u.href, path: u.pathname, frame, method: req.method() });
28 const cors = { 'access-control-allow-origin': '*' };
29 const json = (body, status = 200) => route.fulfill({ status, headers: { ...cors, 'content-type': 'application/json' }, body: JSON.stringify(body) });
30 if (u.pathname === '/api/-/search') {
31 const q = (u.searchParams.get('query') || '').toLowerCase();
32 const offset = Number(u.searchParams.get('offset') || 0), size = Number(u.searchParams.get('size') || 20);
33 const hits = packages.filter(p => !q || [p.namespace, p.name, p.manifest.displayName || '', ...(p.keywords || [])].join(' ').toLowerCase().includes(q));
34 return json({ totalSize: hits.length, offset, extensions: hits.slice(offset, offset + size).map(summary) });
35 }
36 const m = /^\/api\/([^/]+)\/([^/]+)\/([^/]+)(?:\/file\/(.+))?$/.exec(u.pathname);
37 const p = m && byKey.get(`${m[1]}/${m[2]}/${m[3]}`.toLowerCase());
38 if (!p) return json({ error: 'not found' }, 404);
39 if (!m[4]) {
40 return json({
41 ...summary(p), license: p.manifest.license || 'MIT',
42 files: {
43 manifest: base(p) + '/file/package.json',
44 ...(p.vsix ? { download: base(p) + `/file/${p.namespace}.${p.name}-${p.version}.vsix` } : {}),
45 ...(p.published !== null && p.vsix ? { sha256: base(p) + `/file/${p.namespace}.${p.name}-${p.version}.sha256` } : {}),
46 },
47 });
48 }
49 if (m[4] === 'package.json') return json(p.manifest);
50 if (m[4].endsWith('.sha256')) {
51 if (p.published === null || p.shaStatus) return route.fulfill({ status: p.shaStatus || 404, headers: cors, body: 'not found' });
52 return route.fulfill({ status: 200, headers: { ...cors, 'content-type': 'text/plain' }, body: `${p.published} ${p.namespace}.${p.name}-${p.version}.vsix` });
53 }
54 if (m[4].endsWith('.vsix') && p.vsix) {
55 // HEAD: solo su tamaño (lo que el consentimiento enseña antes de bajar nada)
56 if (req.method() === 'HEAD') return route.fulfill({ status: 200, headers: { ...cors, 'content-type': 'application/octet-stream', 'content-length': String(p.vsix.length) }, body: '' });
57 return route.fulfill({ status: 200, headers: { ...cors, 'content-type': 'application/octet-stream', 'content-length': String(p.vsix.length) }, body: p.vsix });
58 }
59 return json({ error: 'not found' }, 404);
60 });
61 return { log, downloads: () => log.filter(r => r.path.endsWith('.vsix') && r.method !== 'HEAD').map(r => r.path), sizeLookups: () => log.filter(r => r.path.endsWith('.vsix') && r.method === 'HEAD').map(r => r.path) };
62}