dsh-pluginmarket-plus 0.1.0 → 0.2.1

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/lib/store.js CHANGED
@@ -12,8 +12,9 @@
12
12
  */
13
13
 
14
14
  import { createHash } from 'node:crypto'
15
- import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
15
+ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
16
16
  import { join } from 'node:path'
17
+ import { normalizeIndexGitSource } from './updates.js'
17
18
 
18
19
  /** 状态清单版本;结构不兼容变更时递增。 */
19
20
  const STATE_VERSION = 1
@@ -49,10 +50,15 @@ export class CacheStore {
49
50
  constructor(dir) {
50
51
  this.dir = dir
51
52
  this.state = { version: STATE_VERSION, sources: [], entries: {} }
53
+ /** @type {{signature: string, size: number, names: Map<string, object[]>}|undefined} 上次解析出来的索引查询表。 */
54
+ this.#indexCache = undefined
52
55
  mkdirSync(dir, { recursive: true })
53
56
  this.#load()
54
57
  }
55
58
 
59
+ /** @type {{signature: string, size: number, names: Map<string, object[]>}|undefined} */
60
+ #indexCache
61
+
56
62
  /** 读取清单;损坏或缺失时以空清单开始(缓存是派生数据,可以重建)。 */
57
63
  #load() {
58
64
  try {
@@ -141,6 +147,7 @@ export class CacheStore {
141
147
  const tmp = `${target}.tmp-${String(process.pid)}-${String(Date.now())}`
142
148
  writeFileSync(tmp, bytes)
143
149
  renameSync(tmp, target)
150
+ this.#indexCache = undefined
144
151
  }
145
152
 
146
153
  /**
@@ -155,5 +162,96 @@ export class CacheStore {
155
162
  } catch {
156
163
  // 删除失败不影响后续逻辑;下次写入会覆盖。
157
164
  }
165
+ this.#indexCache = undefined
166
+ }
167
+
168
+ /**
169
+ * 所有已缓存正文的源 id。
170
+ *
171
+ * 先按源列表与状态清单的顺序取,再扫一遍目录补上清单里没记到的正文文件——这样清单损坏或
172
+ * 被别的进程改写时,已下载的索引仍然可用。
173
+ * @returns {string[]} 源 id 列表。
174
+ */
175
+ cachedIds() {
176
+ /** @type {string[]} */
177
+ const ids = []
178
+ const push = (id) => {
179
+ if (!ids.includes(id) && this.hasBody(id)) ids.push(id)
180
+ }
181
+ for (const url of this.state.sources) push(sourceId(url))
182
+ for (const id of Object.keys(this.state.entries)) push(id)
183
+ let names = []
184
+ try {
185
+ names = readdirSync(this.dir)
186
+ } catch {
187
+ names = []
188
+ }
189
+ for (const name of names) {
190
+ const match = /^index-([a-f0-9]{16})\.json$/u.exec(name)
191
+ if (match !== null) push(match[1])
192
+ }
193
+ return ids
194
+ }
195
+
196
+ /**
197
+ * 一份已缓存索引的签名:正文文件的大小与修改时间。
198
+ *
199
+ * 索引动辄几 MB,解析一次要几百毫秒;用签名判断「有没有变过」,没变就直接复用上次解析结果。
200
+ * @returns {string} 形如 `id:bytes:mtime` 的签名串。
201
+ */
202
+ indexSignature() {
203
+ const parts = []
204
+ for (const id of this.cachedIds()) {
205
+ try {
206
+ const stat = statSync(join(this.dir, `index-${id}.json`))
207
+ parts.push(`${id}:${String(stat.size)}:${String(stat.mtimeMs)}`)
208
+ } catch {
209
+ parts.push(`${id}:missing`)
210
+ }
211
+ }
212
+ return parts.join('|')
213
+ }
214
+
215
+ /**
216
+ * 把已缓存的索引读成「包名 → 版本来源」的查询表。
217
+ *
218
+ * 只读取 `plugins[].versions[]` 里更新判断需要的字段(版本号、发布时间、`source`),
219
+ * 正文损坏或格式不符的源直接跳过——缓存是派生数据,坏一份不该影响其它源。
220
+ * @returns {{signature: string, size: number, names: Map<string, object[]>}} 查询表;`size` 是收录的包数量。
221
+ */
222
+ cachedIndex() {
223
+ const signature = this.indexSignature()
224
+ if (this.#indexCache !== undefined && this.#indexCache.signature === signature) return this.#indexCache
225
+ /** @type {Map<string, object[]>} */
226
+ const names = new Map()
227
+ for (const id of this.cachedIds()) {
228
+ let document
229
+ try {
230
+ document = JSON.parse(readFileSync(join(this.dir, `index-${id}.json`), 'utf8'))
231
+ } catch {
232
+ continue
233
+ }
234
+ if (document === null || typeof document !== 'object' || document.format !== 1 || !Array.isArray(document.plugins)) continue
235
+ for (const plugin of document.plugins) {
236
+ if (plugin === null || typeof plugin !== 'object' || typeof plugin.name !== 'string' || !Array.isArray(plugin.versions)) continue
237
+ const versions = names.get(plugin.name) ?? []
238
+ for (const version of plugin.versions) {
239
+ if (version === null || typeof version !== 'object' || typeof version.version !== 'string') continue
240
+ const source = version.source
241
+ if (source === null || typeof source !== 'object') continue
242
+ if (source.type === 'npm') {
243
+ versions.push({ version: version.version, type: 'npm', publishTimestamp: Number(version.publishTimestamp ?? 0) })
244
+ continue
245
+ }
246
+ if (source.type !== 'git') continue
247
+ const git = normalizeIndexGitSource(source)
248
+ if (git === undefined) continue
249
+ versions.push({ version: version.version, type: 'git', publishTimestamp: Number(version.publishTimestamp ?? 0), git })
250
+ }
251
+ names.set(plugin.name, versions)
252
+ }
253
+ }
254
+ this.#indexCache = { signature, size: names.size, names }
255
+ return this.#indexCache
158
256
  }
159
257
  }