@pi-chamber/web 1.0.8-rc.1 → 1.0.8-rc.3
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/assets/{AgentsSurface-BmHImX8m.js → AgentsSurface-wsW9VCRp.js} +1 -1
- package/dist/assets/{ChoicesArtifact-C71fGOm2.js → ChoicesArtifact-EwCf4Yxu.js} +1 -1
- package/dist/assets/{DiagramView-BZQxoiZu.js → DiagramView-BX6N99cQ.js} +1 -1
- package/dist/assets/{DiffView-C70pbgk-.js → DiffView-CI0eSnPP.js} +1 -1
- package/dist/assets/{FileSaveConflictDialog-Cu8c3gBV.js → FileSaveConflictDialog-GLj2W7VE.js} +1 -1
- package/dist/assets/{FilesView-nFJYEdRw.js → FilesView-CI0tfZQL.js} +1 -1
- package/dist/assets/{GitHubDetailScaffold-Jvv_tgbl.js → GitHubDetailScaffold-B85tZ9m7.js} +1 -1
- package/dist/assets/{GitHubLinkPicker-BXD2ODRi.js → GitHubLinkPicker-Y6AuNiGl.js} +1 -1
- package/dist/assets/{GitHubListPrimitives-BWI706F0.js → GitHubListPrimitives-BJHBud6i.js} +1 -1
- package/dist/assets/{GitView-MIDG5RYW.js → GitView-DueE69kW.js} +5 -5
- package/dist/assets/{IssuesSurface-uNlozeu0.js → IssuesSurface-Cg-xkdIu.js} +3 -3
- package/dist/assets/{JsonTreeView-CSLsTriz.js → JsonTreeView-Cvg6lc40.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-Cs-7BfWi.js → MarkdownRendererImpl-DExPx-QM.js} +1 -1
- package/dist/assets/{PatchDiff-axbsnbTJ.js → PatchDiff-_c3HZ34I.js} +1 -1
- package/dist/assets/{PierreDiffViewer-lwn6w_5w.js → PierreDiffViewer-CZnaqO-_.js} +1 -1
- package/dist/assets/{PullRequestsSurface-o_AXDjbO.js → PullRequestsSurface-C9AYPiwo.js} +2 -2
- package/dist/assets/{SessionSwitcherContent-C4CMlcbv.js → SessionSwitcherContent-DOpnN-8S.js} +1 -1
- package/dist/assets/{SessionSwitcherDropdown-DCBVuCoo.js → SessionSwitcherDropdown-CmHMPZ69.js} +2 -2
- package/dist/assets/{SessionUnreadDot-DyeIrsro.js → SessionUnreadDot-CZCQ4t1J.js} +1 -1
- package/dist/assets/SettingsView-D5PO5M3z.js +11 -0
- package/dist/assets/{SettingsWindow-Cb4st5y5.js → SettingsWindow-DXV5UOE8.js} +1 -1
- package/dist/assets/{SubagentsArtifact-BzeMEoFP.js → SubagentsArtifact-CO1ZiI1x.js} +1 -1
- package/dist/assets/{ToolOutputDialog-BP2mOWdV.js → ToolOutputDialog-GOwNLj-y.js} +1 -1
- package/dist/assets/{ToolPartDiffPreview-D49XBR9b.js → ToolPartDiffPreview-BoaiZji6.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-CLgDONEQ.js → VirtualizedFileDiff-Chq1X8nw.js} +1 -1
- package/dist/assets/VisualFrame-u7aRcX7O.js +22 -0
- package/dist/assets/VisualsArtifact-D9vPjihW.js +1 -0
- package/dist/assets/{collapsible-BeyKtQGB.js → collapsible-BjpDEohv.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-DmO6H8u6.js → computeVirtualFileMetrics-DmPCxbok.js} +1 -1
- package/dist/assets/{contextFileOpenGuard-DOK251YW.js → contextFileOpenGuard-CukNcUeY.js} +1 -1
- package/dist/assets/{fileTreeStatus-CnzR7kxA.js → fileTreeStatus-CDyfNPYO.js} +1 -1
- package/dist/assets/{gitStatusPredicates-4kPg2t6j.js → gitStatusPredicates-uI7Y7L5k.js} +1 -1
- package/dist/assets/{githubLink-QvaLfNNT.js → githubLink-B9Mxac4R.js} +1 -1
- package/dist/assets/{index-DMX9wT1Z.js → index-BWq2ibCw.js} +1 -1
- package/dist/assets/index-BxKPJ9uG.js +11 -0
- package/dist/assets/index-C5JgjS20.css +1 -0
- package/dist/assets/{main-BirYxac2.js → main-CWfC-9xT.js} +4 -4
- package/dist/assets/{main-BW9J3Y72.js → main-ckQ0ciIN.js} +2 -2
- package/dist/assets/{markdownCore-CxmcPur8.js → markdownCore-p2q2Kxwo.js} +1 -1
- package/dist/assets/{miniChat-BRB11eEs.js → miniChat-x2TrwRYA.js} +2 -2
- package/dist/assets/{mobile-DJZYYCuB.js → mobile-B0x2G8h1.js} +2 -2
- package/dist/assets/{nativeFileSave-B6QpoPRn.js → nativeFileSave-D542OJlG.js} +1 -1
- package/dist/assets/{pi-session-focus-preference-PWoh3LNA.js → pi-session-focus-preference-Q_RZUCnt.js} +1 -1
- package/dist/assets/{renderDiffWithHighlighter-C48T6YFJ.js → renderDiffWithHighlighter-CSFGSknx.js} +1 -1
- package/dist/assets/{renderElectronMiniChatApp-N-BQdJl0.js → renderElectronMiniChatApp-CFIdesZT.js} +2 -2
- package/dist/assets/{renderFileWithHighlighter-8Kt2MyTN.js → renderFileWithHighlighter-C5JE0mRX.js} +1 -1
- package/dist/assets/renderMobileApp-B1ybjcGM.js +11 -0
- package/dist/assets/runtimeEndpointReset-DyZSKCYM.js +66 -0
- package/dist/assets/{runtimeSurface-kIEoYsPP.js → runtimeSurface-BR_dhe7_.js} +1 -1
- package/dist/assets/{selection-BTEdLRVK.js → selection-CfOpW4rA.js} +1 -1
- package/dist/assets/useAppFontEffects-COMvkffC.js +226 -0
- package/dist/assets/{useGitHubRemoteSearch-DIBHNu8r.js → useGitHubRemoteSearch-M1AE2YuX.js} +1 -1
- package/dist/assets/{useSwitcherItems-DfLbfX37.js → useSwitcherItems-D0i2BYYP.js} +1 -1
- package/dist/assets/{web-components-C_DsTJis.js → web-components-wOQkOurQ.js} +1 -1
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +1 -1
- package/plugins/featured.json +12 -0
- package/plugins/subagents/package.json +1 -1
- package/plugins/visuals/extension.js +8 -5
- package/plugins/visuals/package.json +1 -1
- package/server/index.js +90 -27
- package/server/lib/git/DOCUMENTATION.md +4 -0
- package/server/lib/git/service.js +70 -5
- package/server/lib/git/service.test.js +179 -0
- package/server/lib/github/DOCUMENTATION.md +2 -0
- package/server/lib/marketplace/DOCUMENTATION.md +95 -0
- package/server/lib/marketplace/featured.js +176 -0
- package/server/lib/marketplace/featured.test.js +207 -0
- package/server/lib/marketplace/npm-registry.js +255 -0
- package/server/lib/marketplace/npm-registry.test.js +227 -0
- package/server/lib/marketplace/routes.js +64 -0
- package/server/lib/marketplace/routes.test.js +119 -0
- package/server/lib/marketplace/service.js +225 -0
- package/server/lib/marketplace/service.test.js +174 -0
- package/server/lib/notifications/DOCUMENTATION.md +10 -4
- package/server/lib/notifications/pi-notification-watcher.js +92 -33
- package/server/lib/notifications/pi-notification-watcher.test.js +79 -0
- package/server/lib/open-url/DOCUMENTATION.md +1 -0
- package/server/lib/pi/merged-event-stream.js +316 -0
- package/server/lib/pi/merged-event-stream.test.js +95 -0
- package/server/lib/pi/routes-daemon-pool.test.js +348 -0
- package/server/lib/pi/routes-merged-events.test.js +327 -0
- package/server/lib/pi/routes-sandbox.test.js +1684 -0
- package/server/lib/pi/routes.js +491 -90
- package/server/lib/pi/routes.test.js +87 -2
- package/server/lib/pi/sandbox/DOCUMENTATION.md +68 -0
- package/server/lib/pi/sandbox/launcher.js +122 -0
- package/server/lib/pi/sandbox/launcher.test.js +91 -0
- package/server/lib/pi/sandbox/nono-launcher.js +455 -0
- package/server/lib/pi/sandbox/nono-launcher.test.js +371 -0
- package/server/lib/pi/sandbox/nono-profile-macos.json +20 -0
- package/server/lib/pi/sandbox/nono-profile-wsl2.json +21 -0
- package/server/lib/pi/sandbox/nono-profile.json +22 -0
- package/server/lib/pi/sandbox/project-sandbox-manager.js +1461 -0
- package/server/lib/pi/sandbox/project-sandbox-setting.js +166 -0
- package/server/lib/pi/sandbox/project-sandbox.js +207 -0
- package/server/lib/pi/sandbox/sandbox-routes.js +110 -0
- package/server/lib/pi/sandbox/sandboxed-folders.js +100 -0
- package/server/lib/pi/sandbox/sandboxed-folders.test.js +65 -0
- package/server/lib/pi/sandbox/trust-ask.js +135 -0
- package/server/lib/pi/sandbox/trust-ask.test.js +54 -0
- package/server/lib/pi/sandbox/worktree-ids.js +96 -0
- package/server/lib/pi/sandbox/worktree-ids.test.js +73 -0
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +26 -6
- package/server/lib/pi/session-daemon/daemon-pool.js +157 -0
- package/server/lib/pi/session-daemon/daemon-process.js +10 -1
- package/server/lib/pi/session-daemon/extension-config.js +206 -23
- package/server/lib/pi/session-daemon/extension-config.test.js +180 -0
- package/server/lib/pi/session-daemon/session-daemon-read-persisted.test.js +167 -0
- package/server/lib/pi/session-daemon/session-daemon.js +368 -56
- package/server/lib/pi/session-daemon/session-daemon.test.js +227 -0
- package/server/lib/pi/session-daemon/session-idle-residency.test.js +98 -1
- package/server/lib/pi/session-daemon/session-jsonl.js +49 -10
- package/server/lib/pi/session-daemon/session-jsonl.test.js +11 -0
- package/server/lib/pi/session-daemon/session-lease.js +93 -15
- package/server/lib/pi/session-daemon/session-lease.test.js +107 -1
- package/server/lib/pi/session-daemon/supervisor-launcher-stop.test.js +295 -0
- package/server/lib/pi/session-daemon/supervisor-sandbox-nono.e2e.test.js +301 -0
- package/server/lib/pi/session-daemon/supervisor-sandbox.test.js +444 -0
- package/server/lib/pi/session-daemon/supervisor.js +386 -35
- package/server/lib/pi/session-daemon/supervisor.test.js +28 -0
- package/server/lib/pi/ui-settings-store.js +90 -3
- package/server/lib/pi/ui-settings-store.test.js +118 -0
- package/server/lib/plugins/DOCUMENTATION.md +17 -5
- package/server/lib/plugins/daemon-sync.js +16 -6
- package/server/lib/plugins/daemon-sync.test.js +12 -0
- package/server/lib/plugins/impact.test.js +2 -1
- package/server/lib/plugins/manifest.js +25 -0
- package/server/lib/plugins/manifest.test.js +9 -0
- package/server/lib/plugins/routes.test.js +28 -0
- package/server/lib/plugins/service.js +24 -2
- package/server/lib/server/cross-process-lock.js +28 -3
- package/server/lib/wallpaper/DOCUMENTATION.md +25 -0
- package/server/lib/wallpaper/routes.js +63 -0
- package/server/lib/wallpaper/routes.test.js +109 -0
- package/server/lib/wallpaper/store.js +87 -0
- package/dist/assets/SettingsView-BHBpt2Um.js +0 -7
- package/dist/assets/VisualFrame-CJrAvWSo.js +0 -22
- package/dist/assets/VisualsArtifact-DqLGgU-Q.js +0 -1
- package/dist/assets/index-B2v0_mez.js +0 -11
- package/dist/assets/index-D3F4JrPN.css +0 -1
- package/dist/assets/renderMobileApp-B7cT7m5b.js +0 -11
- package/dist/assets/runtimeEndpointReset-BC6yGnxN.js +0 -62
- package/dist/assets/useAppFontEffects-EcTZ_I_I.js +0 -225
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createNpmRegistry,
|
|
5
|
+
isValidNpmName,
|
|
6
|
+
safeLink,
|
|
7
|
+
summarizeManifest,
|
|
8
|
+
summarizePackage,
|
|
9
|
+
} from './npm-registry.js';
|
|
10
|
+
|
|
11
|
+
const jsonResponse = (body, { status = 200, contentLength } = {}) => ({
|
|
12
|
+
status,
|
|
13
|
+
ok: status >= 200 && status < 300,
|
|
14
|
+
headers: {
|
|
15
|
+
get: (name) => (String(name).toLowerCase() === 'content-length' && contentLength !== undefined
|
|
16
|
+
? String(contentLength)
|
|
17
|
+
: null),
|
|
18
|
+
},
|
|
19
|
+
text: async () => (typeof body === 'string' ? body : JSON.stringify(body)),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const errorResponse = (status) => ({
|
|
23
|
+
status,
|
|
24
|
+
ok: false,
|
|
25
|
+
headers: { get: () => null },
|
|
26
|
+
text: async () => '{}',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const streamResponse = (chunks) => ({
|
|
30
|
+
status: 200,
|
|
31
|
+
ok: true,
|
|
32
|
+
headers: { get: () => null },
|
|
33
|
+
body: {
|
|
34
|
+
getReader: () => {
|
|
35
|
+
let index = 0;
|
|
36
|
+
return {
|
|
37
|
+
read: async () => (index < chunks.length
|
|
38
|
+
? { done: false, value: chunks[index++] }
|
|
39
|
+
: { done: true, value: undefined }),
|
|
40
|
+
cancel: async () => {},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('isValidNpmName', () => {
|
|
47
|
+
it('accepts plain and scoped names', () => {
|
|
48
|
+
expect(isValidNpmName('pi-lens')).toBe(true);
|
|
49
|
+
expect(isValidNpmName('@scope/x')).toBe(true);
|
|
50
|
+
expect(isValidNpmName('a'.repeat(214))).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('rejects names npm would not take for a new package', () => {
|
|
54
|
+
expect(isValidNpmName('Uppercase')).toBe(false);
|
|
55
|
+
expect(isValidNpmName('../x')).toBe(false);
|
|
56
|
+
expect(isValidNpmName('foo@1.0.0')).toBe(false);
|
|
57
|
+
expect(isValidNpmName('npm:foo')).toBe(false);
|
|
58
|
+
expect(isValidNpmName('')).toBe(false);
|
|
59
|
+
expect(isValidNpmName('a'.repeat(215))).toBe(false);
|
|
60
|
+
expect(isValidNpmName(undefined)).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('safeLink', () => {
|
|
65
|
+
it('normalizes git urls and strips .git for github', () => {
|
|
66
|
+
const link = safeLink('git+https://github.com/user/repo.git');
|
|
67
|
+
expect(link).toContain('https://github.com/user/repo');
|
|
68
|
+
expect(link).not.toContain('.git');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('expands the github: shorthand', () => {
|
|
72
|
+
expect(safeLink('github:user/repo')).toContain('https://github.com/user/repo');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('strips credentials', () => {
|
|
76
|
+
const link = safeLink('https://user:pass@example.com/x');
|
|
77
|
+
expect(link).toContain('https://example.com/x');
|
|
78
|
+
expect(link).not.toContain('user');
|
|
79
|
+
expect(link).not.toContain('pass');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('rejects dangerous and non-http schemes', () => {
|
|
83
|
+
expect(safeLink('javascript:alert(1)')).toBeUndefined();
|
|
84
|
+
expect(safeLink('file:///etc/passwd')).toBeUndefined();
|
|
85
|
+
expect(safeLink('not a url')).toBeUndefined();
|
|
86
|
+
expect(safeLink('')).toBeUndefined();
|
|
87
|
+
expect(safeLink(undefined)).toBeUndefined();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('summarizePackage', () => {
|
|
92
|
+
const valid = {
|
|
93
|
+
name: 'pi-lens',
|
|
94
|
+
version: '1.2.3',
|
|
95
|
+
description: 'A lens for pi.',
|
|
96
|
+
keywords: ['pi-package', 'pichamber-plugin'],
|
|
97
|
+
maintainers: [{ username: 'alice' }],
|
|
98
|
+
publisher: { username: 'GitHub Actions' },
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it('summarizes a well-formed entry', () => {
|
|
102
|
+
const summary = summarizePackage(valid, { weeklyDownloads: 42 });
|
|
103
|
+
expect(summary).toMatchObject({
|
|
104
|
+
name: 'pi-lens',
|
|
105
|
+
version: '1.2.3',
|
|
106
|
+
publisher: 'alice',
|
|
107
|
+
weeklyDownloads: 42,
|
|
108
|
+
pichamberKeyword: true,
|
|
109
|
+
});
|
|
110
|
+
expect(summary.links.npm).toContain('pi-lens');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('drops entries without a usable name', () => {
|
|
114
|
+
expect(summarizePackage({ ...valid, name: 'Bad Name' })).toBeUndefined();
|
|
115
|
+
expect(summarizePackage({ ...valid, name: '' })).toBeUndefined();
|
|
116
|
+
expect(summarizePackage(null)).toBeUndefined();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('strips control characters and caps the description', () => {
|
|
120
|
+
const summary = summarizePackage({ ...valid, description: 'a\x00\x01b' });
|
|
121
|
+
expect(summary.description).toBe('a b');
|
|
122
|
+
const long = summarizePackage({ ...valid, description: 'x'.repeat(500) });
|
|
123
|
+
expect(long.description.length).toBeLessThanOrEqual(300);
|
|
124
|
+
expect(long.description).toContain('…');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('prefers maintainers[0] over a publisher named GitHub Actions', () => {
|
|
128
|
+
expect(summarizePackage(valid).publisher).toBe('alice');
|
|
129
|
+
expect(summarizePackage({ ...valid, maintainers: undefined }).publisher).toBeUndefined();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('reads pichamberKeyword from keywords and floors downloads', () => {
|
|
133
|
+
expect(summarizePackage({ ...valid, keywords: ['pi-package'] }).pichamberKeyword).toBe(false);
|
|
134
|
+
expect(summarizePackage(valid, { weeklyDownloads: 42.9 }).weeklyDownloads).toBe(42);
|
|
135
|
+
expect(summarizePackage(valid).weeklyDownloads).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('summarizeManifest', () => {
|
|
140
|
+
it('reads contains from the pi block', () => {
|
|
141
|
+
const summary = summarizeManifest({
|
|
142
|
+
name: 'pi-lens',
|
|
143
|
+
version: '1.0.0',
|
|
144
|
+
keywords: ['pi-package'],
|
|
145
|
+
license: 'MIT',
|
|
146
|
+
pi: { extensions: ['./extension.js'], skills: [] },
|
|
147
|
+
pichamber: { apiVersion: 1 },
|
|
148
|
+
});
|
|
149
|
+
expect(summary.contains).toEqual(['extensions']);
|
|
150
|
+
expect(summary.pichamber).toBe(true);
|
|
151
|
+
expect(summary.license).toBe('MIT');
|
|
152
|
+
expect(summary.isPiPackage).toBe(true);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('requires an object pichamber block with a numeric apiVersion', () => {
|
|
156
|
+
const base = { name: 'pi-lens', keywords: ['pi-package'] };
|
|
157
|
+
expect(summarizeManifest({ ...base, pichamber: { apiVersion: 1 } }).pichamber).toBe(true);
|
|
158
|
+
expect(summarizeManifest({ ...base, pichamber: { apiVersion: '1' } }).pichamber).toBe(false);
|
|
159
|
+
expect(summarizeManifest({ ...base, pichamber: true }).pichamber).toBe(false);
|
|
160
|
+
expect(summarizeManifest(base).pichamber).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('drops licenses outside the pattern', () => {
|
|
164
|
+
const summary = summarizeManifest({ name: 'pi-lens', keywords: [], license: 'MIT; rm -rf' });
|
|
165
|
+
expect(summary.license).toBeUndefined();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('createNpmRegistry', () => {
|
|
170
|
+
it('sends search params with the keyword qualifier, size, and from', async () => {
|
|
171
|
+
const fetchImpl = vi.fn(async () => jsonResponse({ total: 0, objects: [] }));
|
|
172
|
+
const registry = createNpmRegistry({ fetchImpl });
|
|
173
|
+
await registry.searchRaw({ text: 'lens', keywords: ['pi-package'], from: 5, size: 20 });
|
|
174
|
+
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
|
175
|
+
const [url, init] = fetchImpl.mock.calls[0];
|
|
176
|
+
const parsed = new URL(String(url));
|
|
177
|
+
expect(parsed.pathname).toBe('/-/v1/search');
|
|
178
|
+
expect(parsed.searchParams.get('text')).toContain('keywords:pi-package');
|
|
179
|
+
expect(parsed.searchParams.get('text')).toContain('lens');
|
|
180
|
+
expect(parsed.searchParams.get('size')).toBe('20');
|
|
181
|
+
expect(parsed.searchParams.get('from')).toBe('5');
|
|
182
|
+
expect(init.headers['user-agent']).toBe('PiChamber');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('answers 404 on latest as not found', async () => {
|
|
186
|
+
const registry = createNpmRegistry({ fetchImpl: async () => errorResponse(404) });
|
|
187
|
+
await expect(registry.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_PACKAGE_NOT_FOUND' });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('maps network throws and bad statuses to unavailable', async () => {
|
|
191
|
+
const throwing = createNpmRegistry({ fetchImpl: async () => { throw new Error('down'); } });
|
|
192
|
+
await expect(throwing.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
193
|
+
const fiveHundred = createNpmRegistry({ fetchImpl: async () => errorResponse(500) });
|
|
194
|
+
await expect(fiveHundred.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('maps malformed JSON to unavailable', async () => {
|
|
198
|
+
const registry = createNpmRegistry({ fetchImpl: async () => jsonResponse('{oops') });
|
|
199
|
+
await expect(registry.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('rejects an oversized body by content-length', async () => {
|
|
203
|
+
const registry = createNpmRegistry({
|
|
204
|
+
fetchImpl: async () => jsonResponse({ total: 0, objects: [] }, { contentLength: 5 * 1024 * 1024 }),
|
|
205
|
+
});
|
|
206
|
+
await expect(registry.searchRaw({ text: '', keywords: [] })).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('rejects a streamed body over the cap', async () => {
|
|
210
|
+
const registry = createNpmRegistry({
|
|
211
|
+
fetchImpl: async () => streamResponse([new Uint8Array(300 * 1024), new Uint8Array(300 * 1024)]),
|
|
212
|
+
});
|
|
213
|
+
await expect(registry.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('rejects a latest body whose name differs', async () => {
|
|
217
|
+
const registry = createNpmRegistry({ fetchImpl: async () => jsonResponse({ name: 'other' }) });
|
|
218
|
+
await expect(registry.latest('pi-lens')).rejects.toMatchObject({ code: 'MARKETPLACE_UNAVAILABLE' });
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('URL-encodes the slash of a scoped name', async () => {
|
|
222
|
+
const fetchImpl = vi.fn(async () => jsonResponse({ name: '@scope/x' }));
|
|
223
|
+
const registry = createNpmRegistry({ fetchImpl });
|
|
224
|
+
await registry.latest('@scope/x');
|
|
225
|
+
expect(String(fetchImpl.mock.calls[0][0])).toContain('@scope%2Fx/latest');
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Authenticated marketplace routes (the global `/api` auth gate applies).
|
|
2
|
+
// They read npm and the featured list only; installing goes through the
|
|
3
|
+
// daemon (`POST /api/pi/extensions/packages`, see ../pi/routes.js).
|
|
4
|
+
|
|
5
|
+
const STATUS_BY_CODE = {
|
|
6
|
+
INVALID_MARKETPLACE_REQUEST: 400,
|
|
7
|
+
MARKETPLACE_PACKAGE_NOT_FOUND: 404,
|
|
8
|
+
MARKETPLACE_UNAVAILABLE: 502,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const sendError = (res, error) => {
|
|
12
|
+
const code = Object.hasOwn(STATUS_BY_CODE, error?.code) ? error.code : 'MARKETPLACE_UNAVAILABLE';
|
|
13
|
+
if (code === 'MARKETPLACE_UNAVAILABLE') console.warn('[marketplace] request failed', error?.message ?? error);
|
|
14
|
+
res.status(STATUS_BY_CODE[code]).json({ error: { code } });
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const badRequest = (res) => res.status(400).json({ error: { code: 'INVALID_MARKETPLACE_REQUEST' } });
|
|
18
|
+
|
|
19
|
+
const optionalString = (value, max) => value === undefined || (typeof value === 'string' && value.length <= max);
|
|
20
|
+
|
|
21
|
+
export const registerMarketplaceRoutes = (app, { service }) => {
|
|
22
|
+
// `?q=` search text, `?from=` npm offset, `?pichamber=1` PiChamber-ready only.
|
|
23
|
+
app.get('/api/pi/marketplace/search', async (req, res) => {
|
|
24
|
+
const { q, from, pichamber } = req.query ?? {};
|
|
25
|
+
if (!optionalString(q, 500) || !optionalString(from, 8) || !optionalString(pichamber, 1)) return badRequest(res);
|
|
26
|
+
const offset = from === undefined ? 0 : Number(from);
|
|
27
|
+
if (!Number.isInteger(offset) || offset < 0) return badRequest(res);
|
|
28
|
+
try {
|
|
29
|
+
res.json(await service.search({ query: q ?? '', from: offset, pichamberOnly: pichamber === '1' }));
|
|
30
|
+
} catch (error) {
|
|
31
|
+
sendError(res, error);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
app.get('/api/pi/marketplace/featured', async (_req, res) => {
|
|
36
|
+
try {
|
|
37
|
+
res.json(await service.featured());
|
|
38
|
+
} catch (error) {
|
|
39
|
+
sendError(res, error);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// A query parameter, since scoped names carry a slash.
|
|
44
|
+
app.get('/api/pi/marketplace/package', async (req, res) => {
|
|
45
|
+
const { name } = req.query ?? {};
|
|
46
|
+
if (typeof name !== 'string' || name.length > 214) return badRequest(res);
|
|
47
|
+
try {
|
|
48
|
+
res.json(await service.packageDetail(name));
|
|
49
|
+
} catch (error) {
|
|
50
|
+
sendError(res, error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// `?names=a,b`: latest versions, for update checks on installed packages.
|
|
55
|
+
app.get('/api/pi/marketplace/versions', async (req, res) => {
|
|
56
|
+
const { names } = req.query ?? {};
|
|
57
|
+
if (typeof names !== 'string' || names.length > 12_000) return badRequest(res);
|
|
58
|
+
try {
|
|
59
|
+
res.json(await service.versions(names.length === 0 ? [] : names.split(',')));
|
|
60
|
+
} catch (error) {
|
|
61
|
+
sendError(res, error);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
|
|
4
|
+
import { registerMarketplaceRoutes } from './routes.js';
|
|
5
|
+
|
|
6
|
+
const listen = (app) => new Promise((resolve, reject) => {
|
|
7
|
+
const server = app.listen(0, '127.0.0.1', () => resolve(server));
|
|
8
|
+
server.once('error', reject);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('marketplace routes', () => {
|
|
12
|
+
let server;
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await new Promise((resolve) => (server ? server.close(resolve) : resolve()));
|
|
16
|
+
server = undefined;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const setup = async (service) => {
|
|
20
|
+
const app = express();
|
|
21
|
+
app.use(express.json());
|
|
22
|
+
registerMarketplaceRoutes(app, { service });
|
|
23
|
+
server = await listen(app);
|
|
24
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
25
|
+
const call = async (method, path, body) => {
|
|
26
|
+
const response = await fetch(`${base}${path}`, {
|
|
27
|
+
method,
|
|
28
|
+
headers: body === undefined ? {} : { 'content-type': 'application/json' },
|
|
29
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
30
|
+
});
|
|
31
|
+
return { status: response.status, body: await response.json() };
|
|
32
|
+
};
|
|
33
|
+
return { call };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
it('rejects bad query params with 400', async () => {
|
|
37
|
+
const service = {
|
|
38
|
+
search: vi.fn(async () => ({ packages: [], nextFrom: null })),
|
|
39
|
+
packageDetail: vi.fn(),
|
|
40
|
+
versions: vi.fn(async () => ({ versions: {} })),
|
|
41
|
+
};
|
|
42
|
+
const { call } = await setup(service);
|
|
43
|
+
expect(await call('GET', '/api/pi/marketplace/search?from=abc'))
|
|
44
|
+
.toEqual({ status: 400, body: { error: { code: 'INVALID_MARKETPLACE_REQUEST' } } });
|
|
45
|
+
expect(await call('GET', '/api/pi/marketplace/search?from=1.5'))
|
|
46
|
+
.toEqual({ status: 400, body: { error: { code: 'INVALID_MARKETPLACE_REQUEST' } } });
|
|
47
|
+
expect(await call('GET', '/api/pi/marketplace/search?pichamber=yes'))
|
|
48
|
+
.toEqual({ status: 400, body: { error: { code: 'INVALID_MARKETPLACE_REQUEST' } } });
|
|
49
|
+
expect(await call('GET', '/api/pi/marketplace/package'))
|
|
50
|
+
.toEqual({ status: 400, body: { error: { code: 'INVALID_MARKETPLACE_REQUEST' } } });
|
|
51
|
+
expect(service.search).not.toHaveBeenCalled();
|
|
52
|
+
expect(service.packageDetail).not.toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('maps service errors to 404 and 502', async () => {
|
|
56
|
+
const notFound = { code: 'MARKETPLACE_PACKAGE_NOT_FOUND' };
|
|
57
|
+
const unavailable = { code: 'MARKETPLACE_UNAVAILABLE' };
|
|
58
|
+
const service = {
|
|
59
|
+
search: vi.fn(async () => { throw notFound; }),
|
|
60
|
+
featured: vi.fn(async () => { throw unavailable; }),
|
|
61
|
+
packageDetail: vi.fn(async () => { throw notFound; }),
|
|
62
|
+
versions: vi.fn(async () => { throw unavailable; }),
|
|
63
|
+
};
|
|
64
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
65
|
+
try {
|
|
66
|
+
const { call } = await setup(service);
|
|
67
|
+
expect(await call('GET', '/api/pi/marketplace/search')).toEqual({ status: 404, body: { error: notFound } });
|
|
68
|
+
expect(await call('GET', '/api/pi/marketplace/featured')).toEqual({ status: 502, body: { error: unavailable } });
|
|
69
|
+
expect(await call('GET', '/api/pi/marketplace/package?name=pi-lens')).toEqual({ status: 404, body: { error: notFound } });
|
|
70
|
+
expect(await call('GET', '/api/pi/marketplace/versions?names=a')).toEqual({ status: 502, body: { error: unavailable } });
|
|
71
|
+
} finally {
|
|
72
|
+
warn.mockRestore();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('answers unknown errors as unavailable with 502', async () => {
|
|
77
|
+
const service = {
|
|
78
|
+
search: vi.fn(async () => { throw new Error('boom'); }),
|
|
79
|
+
featured: vi.fn(),
|
|
80
|
+
packageDetail: vi.fn(),
|
|
81
|
+
versions: vi.fn(),
|
|
82
|
+
};
|
|
83
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
84
|
+
try {
|
|
85
|
+
const { call } = await setup(service);
|
|
86
|
+
expect(await call('GET', '/api/pi/marketplace/search')).toEqual({
|
|
87
|
+
status: 502,
|
|
88
|
+
body: { error: { code: 'MARKETPLACE_UNAVAILABLE' } },
|
|
89
|
+
});
|
|
90
|
+
} finally {
|
|
91
|
+
warn.mockRestore();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('passes pichamber=1 through as pichamberOnly', async () => {
|
|
96
|
+
const service = {
|
|
97
|
+
search: vi.fn(async () => ({ packages: [], nextFrom: null })),
|
|
98
|
+
featured: vi.fn(async () => ({ packages: [], updated: null, source: 'builtin' })),
|
|
99
|
+
packageDetail: vi.fn(async (name) => ({ name })),
|
|
100
|
+
versions: vi.fn(async (names) => ({ versions: Object.fromEntries(names.map((name) => [name, '1.0.0'])) })),
|
|
101
|
+
};
|
|
102
|
+
const { call } = await setup(service);
|
|
103
|
+
expect(await call('GET', '/api/pi/marketplace/search?q=lens&pichamber=1&from=20')).toEqual({
|
|
104
|
+
status: 200,
|
|
105
|
+
body: { packages: [], nextFrom: null },
|
|
106
|
+
});
|
|
107
|
+
expect(service.search).toHaveBeenCalledWith({ query: 'lens', from: 20, pichamberOnly: true });
|
|
108
|
+
expect(await call('GET', '/api/pi/marketplace/package?name=pi-lens')).toEqual({
|
|
109
|
+
status: 200,
|
|
110
|
+
body: { name: 'pi-lens' },
|
|
111
|
+
});
|
|
112
|
+
expect(service.packageDetail).toHaveBeenCalledWith('pi-lens');
|
|
113
|
+
expect(await call('GET', '/api/pi/marketplace/versions?names=a,b')).toEqual({
|
|
114
|
+
status: 200,
|
|
115
|
+
body: { versions: { a: '1.0.0', b: '1.0.0' } },
|
|
116
|
+
});
|
|
117
|
+
expect(service.versions).toHaveBeenCalledWith(['a', 'b']);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// The marketplace: npm search over Pi packages, the curated featured list,
|
|
2
|
+
// one package's details, and latest versions for update checks. Answers are
|
|
3
|
+
// cached in memory and shared by every device on this server; identical
|
|
4
|
+
// requests in flight share one npm call. See DOCUMENTATION.md.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
MarketplaceError,
|
|
8
|
+
PICHAMBER_KEYWORD,
|
|
9
|
+
PI_PACKAGE_KEYWORD,
|
|
10
|
+
isValidNpmName,
|
|
11
|
+
summarizeManifest,
|
|
12
|
+
summarizePackage,
|
|
13
|
+
} from './npm-registry.js';
|
|
14
|
+
|
|
15
|
+
const PAGE_SIZE = 20;
|
|
16
|
+
const MAX_QUERY_LENGTH = 100;
|
|
17
|
+
const MAX_FROM = 5_000;
|
|
18
|
+
const MAX_VERSION_NAMES = 50;
|
|
19
|
+
const SEARCH_TTL_MS = 5 * 60 * 1000;
|
|
20
|
+
const PACKAGE_TTL_MS = 10 * 60 * 1000;
|
|
21
|
+
const MAX_CACHE_ENTRIES = 300;
|
|
22
|
+
const CONCURRENCY = 6;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A small TTL cache that also shares a running load. A failed load is not
|
|
26
|
+
* kept, so the next request tries again.
|
|
27
|
+
*/
|
|
28
|
+
const createCache = ({ ttlMs, now }) => {
|
|
29
|
+
const entries = new Map();
|
|
30
|
+
return (key, load) => {
|
|
31
|
+
const hit = entries.get(key);
|
|
32
|
+
if (hit && (hit.pending || now() - hit.at < ttlMs)) {
|
|
33
|
+
entries.delete(key);
|
|
34
|
+
entries.set(key, hit);
|
|
35
|
+
return hit.value;
|
|
36
|
+
}
|
|
37
|
+
const value = load();
|
|
38
|
+
const entry = { at: now(), value, pending: true };
|
|
39
|
+
entries.set(key, entry);
|
|
40
|
+
value.then(
|
|
41
|
+
() => {
|
|
42
|
+
entry.pending = false;
|
|
43
|
+
entry.at = now();
|
|
44
|
+
},
|
|
45
|
+
() => {
|
|
46
|
+
if (entries.get(key) === entry) entries.delete(key);
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
while (entries.size > MAX_CACHE_ENTRIES) entries.delete(entries.keys().next().value);
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Run `task` over `items` with at most `limit` at once, keeping order. */
|
|
55
|
+
const mapLimited = async (items, limit, task) => {
|
|
56
|
+
const results = new Array(items.length);
|
|
57
|
+
let next = 0;
|
|
58
|
+
const worker = async () => {
|
|
59
|
+
while (next < items.length) {
|
|
60
|
+
const index = next;
|
|
61
|
+
next += 1;
|
|
62
|
+
results[index] = await task(items[index], index);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
66
|
+
return results;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Search text as npm should see it: one line, no `keywords:` or other
|
|
71
|
+
* qualifiers (the keyword filter is ours to set), length capped.
|
|
72
|
+
*/
|
|
73
|
+
export const normalizeQuery = (value) => {
|
|
74
|
+
if (typeof value !== 'string') return '';
|
|
75
|
+
return value
|
|
76
|
+
// eslint-disable-next-line no-control-regex
|
|
77
|
+
.replace(/[\u0000-\u001f\u007f]+/g, ' ')
|
|
78
|
+
.replace(/\b[a-z]+:\S*/gi, ' ')
|
|
79
|
+
.replace(/\s+/g, ' ')
|
|
80
|
+
.trim()
|
|
81
|
+
.slice(0, MAX_QUERY_LENGTH);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const queryTokens = (query) => query.toLowerCase().split(/[\s,]+/).filter((token) => token.length >= 2);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* npm ranks by the text but still returns every package with the keyword,
|
|
88
|
+
* the popular ones last. A hit counts when a word of the query is in its
|
|
89
|
+
* name, description, or keywords.
|
|
90
|
+
*/
|
|
91
|
+
const matches = (summary, tokens) => {
|
|
92
|
+
if (tokens.length === 0) return true;
|
|
93
|
+
const haystack = [summary.name, summary.description ?? '', ...summary.keywords].join(' ').toLowerCase();
|
|
94
|
+
return tokens.some((token) => haystack.includes(token));
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const createMarketplaceService = ({
|
|
98
|
+
registry,
|
|
99
|
+
featuredSource,
|
|
100
|
+
bundledPackageNames = async () => [],
|
|
101
|
+
now = () => Date.now(),
|
|
102
|
+
}) => {
|
|
103
|
+
const searchCache = createCache({ ttlMs: SEARCH_TTL_MS, now });
|
|
104
|
+
const packageCache = createCache({ ttlMs: PACKAGE_TTL_MS, now });
|
|
105
|
+
const downloadsCache = createCache({ ttlMs: PACKAGE_TTL_MS, now });
|
|
106
|
+
|
|
107
|
+
/** Names never shown: the hidden list and PiChamber's own plugins. */
|
|
108
|
+
const excludedNames = async () => {
|
|
109
|
+
const [featured, bundled] = await Promise.all([featuredSource.get(), bundledPackageNames().catch(() => [])]);
|
|
110
|
+
return { featured, excluded: new Set([...featured.hidden, ...bundled]) };
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const latest = (name) => packageCache(name, () => registry.latest(name));
|
|
114
|
+
|
|
115
|
+
const weeklyDownloads = (name) => downloadsCache(name, () => registry.weeklyDownloads(name))
|
|
116
|
+
.catch(() => undefined);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One page. `from` is npm's offset; the answer's `nextFrom` is the offset
|
|
120
|
+
* of the next page, or `null` at the end.
|
|
121
|
+
*/
|
|
122
|
+
const search = async ({ query, from = 0, pichamberOnly = false } = {}) => {
|
|
123
|
+
const text = normalizeQuery(query);
|
|
124
|
+
const offset = Number.isInteger(from) && from >= 0 && from <= MAX_FROM ? from : 0;
|
|
125
|
+
// npm filters on one keyword reliably; with two it loosens. Ask for the
|
|
126
|
+
// rarer one and check both here.
|
|
127
|
+
const keywords = pichamberOnly ? [PI_PACKAGE_KEYWORD, PICHAMBER_KEYWORD] : [PI_PACKAGE_KEYWORD];
|
|
128
|
+
const key = JSON.stringify([text, offset, pichamberOnly]);
|
|
129
|
+
const page = await searchCache(key, () => registry.searchRaw({
|
|
130
|
+
text,
|
|
131
|
+
keywords: [pichamberOnly ? PICHAMBER_KEYWORD : PI_PACKAGE_KEYWORD],
|
|
132
|
+
from: offset,
|
|
133
|
+
size: PAGE_SIZE,
|
|
134
|
+
}));
|
|
135
|
+
const { excluded } = await excludedNames();
|
|
136
|
+
const tokens = queryTokens(text);
|
|
137
|
+
let unrelated = 0;
|
|
138
|
+
const packages = [];
|
|
139
|
+
for (const object of page.objects) {
|
|
140
|
+
const summary = summarizePackage(object?.package, { weeklyDownloads: object?.downloads?.weekly });
|
|
141
|
+
if (!summary) continue;
|
|
142
|
+
// Pi's gallery needs `pi-package`; the filter needs ours as well.
|
|
143
|
+
if (!keywords.every((keyword) => summary.keywords.includes(keyword))) continue;
|
|
144
|
+
if (!matches(summary, tokens)) {
|
|
145
|
+
unrelated += 1;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (excluded.has(summary.name)) continue;
|
|
149
|
+
packages.push(summary);
|
|
150
|
+
}
|
|
151
|
+
const consumed = offset + page.objects.length;
|
|
152
|
+
// Past the last hit npm pads with unrelated packages: stop there.
|
|
153
|
+
const more = page.objects.length === PAGE_SIZE && unrelated === 0 && consumed < MAX_FROM
|
|
154
|
+
&& (page.total === undefined || consumed < page.total);
|
|
155
|
+
return {
|
|
156
|
+
packages,
|
|
157
|
+
...(tokens.length === 0 && page.total !== undefined ? { total: page.total } : {}),
|
|
158
|
+
nextFrom: more ? consumed : null,
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The featured packages in their listed order. One that npm cannot answer
|
|
164
|
+
* for is still listed, marked `unavailable`, with its note.
|
|
165
|
+
*/
|
|
166
|
+
const featured = async () => {
|
|
167
|
+
const { featured: list, excluded } = await excludedNames();
|
|
168
|
+
const shown = list.featured.filter((entry) => !excluded.has(entry.name));
|
|
169
|
+
const packages = await mapLimited(shown, CONCURRENCY, async (entry) => {
|
|
170
|
+
try {
|
|
171
|
+
const [manifest, downloads] = await Promise.all([latest(entry.name), weeklyDownloads(entry.name)]);
|
|
172
|
+
const summary = summarizeManifest(manifest);
|
|
173
|
+
if (!summary) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE');
|
|
174
|
+
return {
|
|
175
|
+
...summary,
|
|
176
|
+
...(downloads !== undefined ? { weeklyDownloads: downloads } : {}),
|
|
177
|
+
...(entry.note ? { note: entry.note } : {}),
|
|
178
|
+
};
|
|
179
|
+
} catch {
|
|
180
|
+
return {
|
|
181
|
+
name: entry.name,
|
|
182
|
+
keywords: [],
|
|
183
|
+
pichamberKeyword: false,
|
|
184
|
+
links: { npm: `https://www.npmjs.com/package/${entry.name}` },
|
|
185
|
+
...(entry.note ? { note: entry.note } : {}),
|
|
186
|
+
unavailable: true,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return { packages, updated: list.updated, source: list.source };
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** One package's page. Hidden and bundled names answer not found. */
|
|
194
|
+
const packageDetail = async (name) => {
|
|
195
|
+
if (!isValidNpmName(name)) throw new MarketplaceError('INVALID_MARKETPLACE_REQUEST');
|
|
196
|
+
const { excluded } = await excludedNames();
|
|
197
|
+
if (excluded.has(name)) throw new MarketplaceError('MARKETPLACE_PACKAGE_NOT_FOUND');
|
|
198
|
+
const [manifest, downloads] = await Promise.all([latest(name), weeklyDownloads(name)]);
|
|
199
|
+
const summary = summarizeManifest(manifest);
|
|
200
|
+
if (!summary) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE');
|
|
201
|
+
return { ...summary, ...(downloads !== undefined ? { weeklyDownloads: downloads } : {}) };
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Latest versions for update checks. A name npm cannot answer for is left
|
|
206
|
+
* out, which means unknown, not "no update".
|
|
207
|
+
*/
|
|
208
|
+
const versions = async (names) => {
|
|
209
|
+
if (!Array.isArray(names) || names.length > MAX_VERSION_NAMES || !names.every(isValidNpmName)) {
|
|
210
|
+
throw new MarketplaceError('INVALID_MARKETPLACE_REQUEST');
|
|
211
|
+
}
|
|
212
|
+
const unique = [...new Set(names)];
|
|
213
|
+
const found = await mapLimited(unique, CONCURRENCY, async (name) => {
|
|
214
|
+
try {
|
|
215
|
+
const version = summarizeManifest(await latest(name))?.version;
|
|
216
|
+
return version ? [name, version] : null;
|
|
217
|
+
} catch {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
return { versions: Object.fromEntries(found.filter(Boolean)) };
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
return { search, featured, packageDetail, versions };
|
|
225
|
+
};
|