universal-plugin-hub 1.0.45 → 1.1.0

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "title": "Universal Plugin Hub",
4
4
  "displayName": "Universal Plugin Hub",
5
5
  "description": "在 DSH 中使用 Claude Code、Codex 等软件生态的插件,一站式管理插件、Skills 与 MCP。支持添加 Git 插件源和 marketplace、上传插件和 Skills、添加 MCP 服务器,自动扫描并开关本地 Skills,接入插件提供的子代理、MCP、Hooks 与 LSP。",
6
- "version": "1.0.45",
6
+ "version": "1.1.0",
7
7
  "engines": {
8
8
  "dsh": ">=0.1.5-rc.1"
9
9
  },
@@ -34,7 +34,8 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@modelcontextprotocol/sdk": "1.32.1",
37
- "js-yaml": "^5.3.0"
37
+ "js-yaml": "^5.3.0",
38
+ "pngjs": "7.0.0"
38
39
  },
39
40
  "peerDependencies": {
40
41
  "@deepseek-ai/cordis": ">=4.0.1 <5.0.0"
package/src/icons.js CHANGED
@@ -1,7 +1,8 @@
1
- import { statSync, realpathSync, readFileSync, readdirSync, openSync, readSync, closeSync } from 'node:fs'
1
+ import { statSync, realpathSync, readFileSync, readdirSync, openSync, readSync, closeSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'
2
2
  import { mkdir, readFile, writeFile, stat } from 'node:fs/promises'
3
3
  import { join, relative, isAbsolute, posix } from 'node:path'
4
4
  import { createHash } from 'node:crypto'
5
+ import { PNG } from 'pngjs'
5
6
  import { marketRoot } from './store.js'
6
7
  import { pluginPath } from './market.js'
7
8
 
@@ -160,17 +161,22 @@ function discoverIconFiles(dir, pluginName) {
160
161
  return found.sort((a, b) => b.score - a.score || a.path.localeCompare(b.path))
161
162
  }
162
163
 
163
- // A plugin-local icon is at most a few hundred KB and sits on a local disk, so
164
- // carrying its bytes in the row costs one decode and removes the request the
165
- // first paint would otherwise wait on. Remote avatars (GitHub, favicons) stay
166
- // behind the proxy: they are not ours to inline and would be far too big.
167
- //
168
- // The inline set is a budget, not a rule: every icon carried in the list is
169
- // paid again on each /state fetch, and the card renders at 38px. At 12KB the
170
- // whole marketplace payload stays under 1MB and 67 rows still paint with no
171
- // request; the rest keep the served URL, which is a local disk read.
164
+ // Small local artwork can travel in the row directly. Larger PNGs use a
165
+ // bounded 96px preview, cached separately from the original. Remote images
166
+ // only contribute a preview when bytes already exist in the proxy cache;
167
+ // parsing a catalog never waits on an upstream image request.
172
168
 
173
169
  const INLINE_LIMIT = 12 * 1024
170
+ // A preview is carried once per URL rather than duplicated in both theme arrays.
171
+ // This covers ordinary raster logos while keeping large artwork out of list JSON.
172
+ const PREVIEW_LIMIT = 64 * 1024
173
+ // Decoding runs synchronously inside catalog parsing, once per row, so the
174
+ // bound has to cover the decode itself and not just the file size.
175
+ const PREVIEW_MAX_PIXELS = 262144
176
+ const PREVIEWS_DIR = 'previews'
177
+ const PREVIEW_CACHE_KEEP = 200
178
+ const previewMemory = new Map()
179
+ let lastPreviewPrune = 0
174
180
 
175
181
  /** A local file's bytes as a data URL, or null when it is too big or unreadable. */
176
182
  function inlineFile(file) {
@@ -183,6 +189,94 @@ function inlineFile(file) {
183
189
  } catch { return null }
184
190
  }
185
191
 
192
+ function inlinePreview(buffer) {
193
+ const contentType = imageContentType(buffer)
194
+ if (!contentType) return null
195
+ if (contentType === 'image/png' && buffer.length > INLINE_LIMIT) {
196
+ if (buffer.length < 24) return null
197
+ const w = buffer.readUInt32BE(16), h = buffer.readUInt32BE(20)
198
+ // Reject before decoding: the decode is the expensive part.
199
+ if (!w || !h || w * h > PREVIEW_MAX_PIXELS) return null
200
+ // Version the derived cache by content and algorithm, so replacing artwork
201
+ // cannot leave an old thumbnail behind. Originals are never overwritten.
202
+ const key = createHash('sha256').update(buffer).digest('hex')
203
+ const directory = join(marketRoot(), 'icons', PREVIEWS_DIR)
204
+ const file = join(directory, `96-v1-${key}.png`)
205
+ try {
206
+ const saved = readFileSync(file)
207
+ if (saved.length <= INLINE_LIMIT && imageContentType(saved) === 'image/png') return `data:image/png;base64,${saved.toString('base64')}`
208
+ } catch {}
209
+ let image
210
+ try { image = PNG.sync.read(buffer) } catch { return null }
211
+ const ratio = Math.min(1, 96 / Math.max(w, h))
212
+ const width = Math.max(1, Math.round(w * ratio)), height = Math.max(1, Math.round(h * ratio))
213
+ const data = Buffer.alloc(width * height * 4)
214
+ // Area sampling with premultiplied alpha preserves fine edges on both
215
+ // themes; simply averaging transparent RGB creates dark logo fringes.
216
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
217
+ const left = x * w / width, right = (x + 1) * w / width
218
+ const top = y * h / height, bottom = (y + 1) * h / height
219
+ let red = 0, green = 0, blue = 0, alpha = 0
220
+ for (let sy = Math.floor(top); sy < Math.ceil(bottom); sy++) for (let sx = Math.floor(left); sx < Math.ceil(right); sx++) {
221
+ const weight = (Math.min(right, sx + 1) - Math.max(left, sx)) * (Math.min(bottom, sy + 1) - Math.max(top, sy))
222
+ const i = (sy * w + sx) * 4, a = image.data[i + 3] / 255 * weight
223
+ alpha += a; red += image.data[i] * a; green += image.data[i + 1] * a; blue += image.data[i + 2] * a
224
+ }
225
+ const i = (y * width + x) * 4
226
+ if (alpha) { data[i] = Math.round(red / alpha); data[i + 1] = Math.round(green / alpha); data[i + 2] = Math.round(blue / alpha) }
227
+ data[i + 3] = Math.round(255 * alpha / ((right - left) * (bottom - top)))
228
+ }
229
+ const thumbnail = PNG.sync.write({ width, height, data }, { deflateLevel: 9, deflateStrategy: 0 })
230
+ if (thumbnail.length > INLINE_LIMIT) return null
231
+ try {
232
+ mkdirSync(directory, { recursive: true })
233
+ writeFileSync(file, thumbnail)
234
+ prunePreviewCache(directory)
235
+ } catch {}
236
+ return `data:image/png;base64,${thumbnail.toString('base64')}`
237
+ }
238
+ return buffer.length <= PREVIEW_LIMIT ? `data:${contentType};base64,${buffer.toString('base64')}` : null
239
+ }
240
+
241
+ /** Content-addressed thumbnails are never overwritten, so they need pruning. */
242
+ function prunePreviewCache(directory) {
243
+ const now = Date.now()
244
+ if (now - lastPreviewPrune < 60 * 60 * 1000) return
245
+ lastPreviewPrune = now
246
+ try {
247
+ const files = readdirSync(directory)
248
+ .map((entry) => {
249
+ try { const info = statSync(join(directory, entry)); return info.isFile() ? { entry, at: info.mtimeMs } : null } catch { return null }
250
+ })
251
+ .filter(Boolean)
252
+ .sort((a, b) => b.at - a.at)
253
+ for (const stale of files.slice(PREVIEW_CACHE_KEEP)) {
254
+ try { rmSync(join(directory, stale.entry), { force: true }) } catch {}
255
+ }
256
+ } catch {}
257
+ }
258
+
259
+ function previewFile(file) {
260
+ try {
261
+ const info = statSync(file)
262
+ if (!info.isFile() || info.size > MAX_BYTES) return null
263
+ const key = `${file}:${info.mtimeMs}:${info.size}`
264
+ if (previewMemory.has(key)) return previewMemory.get(key)
265
+ const preview = inlinePreview(readFileSync(file))
266
+ if (previewMemory.size >= 256) previewMemory.delete(previewMemory.keys().next().value)
267
+ previewMemory.set(key, preview)
268
+ return preview
269
+ } catch { return null }
270
+ }
271
+
272
+ function cachedIconPreview(url) {
273
+ const cached = memory.get(url)
274
+ if (cached) {
275
+ try { return inlinePreview(cached.buffer) } catch { return null }
276
+ }
277
+ return previewFile(iconCacheFile(url))
278
+ }
279
+
186
280
  /**
187
281
  * Carry manifest assets through marketplace rows without exposing filesystem paths.
188
282
  *
@@ -197,6 +291,7 @@ export function buildPluginIcons(meta, { sourceId, name, dir, source } = {}) {
197
291
  // Local files are stat'd once here and the same File is reused for the inline
198
292
  // copy, so discovering artwork and inlining it is one pass over the disk.
199
293
  const files = new Map()
294
+ const previews = {}
200
295
  const toUrl = (value) => {
201
296
  if (typeof value !== 'string' || !value.trim()) return null
202
297
  if (/^https?:\/\//i.test(value)) return proxy(value)
@@ -238,18 +333,41 @@ export function buildPluginIcons(meta, { sourceId, name, dir, source } = {}) {
238
333
  : [ui.iconLight, ui.logoLight, meta?.iconLight, ...values('light'), ...plain, ...shade]
239
334
  const urls = [...new Set(declared.map(toUrl).filter(Boolean))]
240
335
  // The inline copy leads, so the first candidate needs no request at all.
241
- // Only the local files this call already stat'd are considered; a remote
242
- // candidate never reaches the disk and so can never be inlined.
336
+ // Only local files lead with their bytes. The separately carried preview
337
+ // never changes candidate priority or replaces the normal refresh request.
243
338
  const inline = urls.map((url) => files.get(url) && inlineFile(files.get(url))).find(Boolean)
339
+ if (!inline && urls[0]) {
340
+ const first = urls[0]
341
+ const remote = first.startsWith('/universal-plugin-hub/api/icon?') ? new URL(first, 'http://localhost').searchParams.get('u') : null
342
+ const preview = files.has(first) ? previewFile(files.get(first)) : remote && cachedIconPreview(remote)
343
+ if (preview) previews[first] = preview
344
+ }
244
345
  return inline ? [inline, ...urls.filter((url) => url !== inline)] : urls
245
346
  }
246
347
  const served = (paths) => [...new Set(paths.map(toUrl).filter(Boolean))]
247
348
  // Reported apart from the tile candidates so the client can try them after
248
349
  // the brand artwork it knows about, instead of leading every row with them.
350
+ const light = build('light')
351
+ const dark = build('dark')
352
+ // Avatar-only rows already have reusable bytes in the proxy's disk cache.
353
+ // Expose those as a preview without starting network work during parsing;
354
+ // the normal image request still refreshes expired cache entries.
355
+ if (!light.length || !dark.length) {
356
+ const githubOwner = (url) => typeof url === 'string' ? url.match(/github\.com\/([^/]+)/i)?.[1] : null
357
+ let owner = [source?.url || meta?.source?.url, meta?.homepage].map(githubOwner).find((value) => value && !value.toLowerCase().includes('anthropics'))
358
+ const author = (typeof meta?.author === 'string' ? meta.author : meta?.author?.name || '').trim().toLowerCase()
359
+ if (!owner && /^[a-z0-9_-]+$/i.test(author) && author !== 'anthropic') owner = author
360
+ if (owner) {
361
+ const url = `https://github.com/${encodeURIComponent(owner)}.png?size=80`
362
+ const preview = cachedIconPreview(url)
363
+ if (preview) previews[proxy(url)] = preview
364
+ }
365
+ }
249
366
  return {
250
- light: build('light'),
251
- dark: build('dark'),
367
+ light,
368
+ dark,
252
369
  portrait: { light: served(portrait), dark: served(portrait) },
370
+ ...(Object.keys(previews).length ? { previews } : {}),
253
371
  }
254
372
  }
255
373
 
package/src/index.js CHANGED
@@ -26,7 +26,7 @@ export function apply(ctx) {
26
26
  // Everything below that resolves a profile path has to run *after* this, so
27
27
  // it lives inside the injection rather than beside it.
28
28
  let provisioned = Promise.resolve()
29
- ctx.inject(['profileContext'], (hostCtx) => {
29
+ ctx.inject(['profileContext'], async (hostCtx) => {
30
30
  const name = hostCtx.profileContext?.name
31
31
  if (typeof name === 'string' && name.trim()) process.env.DSH_PROFILE = name.trim()
32
32
 
@@ -34,7 +34,7 @@ export function apply(ctx) {
34
34
  try {
35
35
  restoreUploadedSkills()
36
36
 
37
- syncAllInstalledSkillsToDsh()
37
+ await syncAllInstalledSkillsToDsh()
38
38
  } catch (err) {
39
39
  ctx.logger?.warn?.(`[universal-plugin-hub] skill sync error: ${err.message}`)
40
40
  }