@linxin666/dsh-pet 0.4.6 → 0.4.7
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/README.i18n.yaml +2 -2
- package/README.md +2 -1
- package/README.zh.md +2 -1
- package/lib/client.js +103 -24
- package/lib/client.js.map +1 -1
- package/lib/index.js +454 -1
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/client/index.js +1 -1
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- package/package.json +26 -30
- package/src/client/index.ts +1 -1
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- package/src/registry.test.ts +3 -2
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
* - Canvas bitmap buffer (default where createImageBitmap/fetch/2D context
|
|
15
15
|
* exist): frames are decoded once into an ImageBitmap on demand, with a
|
|
16
16
|
* bounded look-ahead window over the playing track, and drawn onto one
|
|
17
|
-
* <canvas
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* <canvas>. Steady-state playback issues zero DOM mutations and re-decodes
|
|
18
|
+
* only what the retention budget evicted: decoded frames are held under
|
|
19
|
+
* MAX_RETAINED_BYTES, so a long-lived pet that plays every track of every
|
|
20
|
+
* skin cannot pin every bitmap for the life of the page (measured hotspot:
|
|
21
|
+
* swapping <img>.src per frame drove image decode + invalidation every tick).
|
|
20
22
|
* - Classic <img> fallback (jsdom/tests or missing APIs): identical to the
|
|
21
23
|
* historical behavior - cache-warm Image elements plus guarded src swaps,
|
|
22
24
|
* so environments without modern decoding keep working unchanged.
|
|
@@ -78,6 +80,20 @@ export interface Frames2dRendererHandle extends PetRendererHandle {
|
|
|
78
80
|
/** Stall watchdog period: a looping track stuck longer re-kicks its chain. */
|
|
79
81
|
const WATCHDOG_MS = 1200
|
|
80
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Retention budget for decoded frame bitmaps, in bytes.
|
|
85
|
+
*
|
|
86
|
+
* The budget is expressed in bytes rather than in frames because a frame's
|
|
87
|
+
* decoded footprint is `width * height * 4` and published pets differ by more
|
|
88
|
+
* than an order of magnitude (a 512x683 frame is 1.33 MB, a 128x128 frame is
|
|
89
|
+
* 64 KB): a frame count would be either useless for small atlases or too tight
|
|
90
|
+
* for large ones. 128 MiB holds this repository's longest shipped track (78
|
|
91
|
+
* frames, 104 MB) in full, so a running loop still re-decodes nothing, while
|
|
92
|
+
* capping a pet that played every track of every skin at roughly a tenth of
|
|
93
|
+
* the ~1.7 GB the unbounded memo reached.
|
|
94
|
+
*/
|
|
95
|
+
const MAX_RETAINED_BYTES = 128 * 1024 * 1024
|
|
96
|
+
|
|
81
97
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
82
98
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
83
99
|
}
|
|
@@ -160,6 +176,32 @@ interface DecodedFrame {
|
|
|
160
176
|
height: number
|
|
161
177
|
}
|
|
162
178
|
|
|
179
|
+
/**
|
|
180
|
+
* One memoized decode job. `frame` and `bytes` appear only once the job
|
|
181
|
+
* resolved with a frame — exactly the set the retention budget may release;
|
|
182
|
+
* pending and failed jobs carry neither. The owning map's insertion order is
|
|
183
|
+
* the LRU order (re-inserting an entry marks it most recently used).
|
|
184
|
+
*/
|
|
185
|
+
interface DecodeEntry {
|
|
186
|
+
job: Promise<DecodedFrame | undefined>
|
|
187
|
+
frame?: DecodedFrame
|
|
188
|
+
bytes?: number
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Release one decoded frame. `close()` is browser-only and idempotent, so the
|
|
193
|
+
* call is guarded for exotic hosts and for frames that were already released;
|
|
194
|
+
* the `<img>` fallback has no close, where dropping the reference is the
|
|
195
|
+
* release.
|
|
196
|
+
*/
|
|
197
|
+
function releaseFrame(frame: DecodedFrame | undefined): void {
|
|
198
|
+
if (frame === undefined) return
|
|
199
|
+
try {
|
|
200
|
+
const maybeClose = (frame.source as { close?: () => void }).close
|
|
201
|
+
if (typeof maybeClose === 'function') maybeClose.call(frame.source)
|
|
202
|
+
} catch { /* already released */ }
|
|
203
|
+
}
|
|
204
|
+
|
|
163
205
|
export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
164
206
|
id: 'frames2d',
|
|
165
207
|
apiVersion: PET_RENDERER_API_VERSION,
|
|
@@ -209,11 +251,18 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
209
251
|
ctx.container.appendChild(img)
|
|
210
252
|
}
|
|
211
253
|
|
|
212
|
-
// Decode cache: one concurrent decode per URL,
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
254
|
+
// Decode cache: one concurrent decode per URL, retained under a decoded
|
|
255
|
+
// byte budget. The historical map memoized every decoded frame forever, so
|
|
256
|
+
// a pet that had played all of its tracks (every skin) kept every bitmap
|
|
257
|
+
// alive for the life of the page: a shipped three-skin pet measures 1276
|
|
258
|
+
// frames at 512x683 RGBA, about 1.7 GB, growing monotonically. Entries now
|
|
259
|
+
// carry their decoded footprint, sit in Map insertion order (delete + set
|
|
260
|
+
// is an LRU touch) and the least recently used ones outside the playing
|
|
261
|
+
// window are closed once MAX_RETAINED_BYTES is crossed. A failed decode
|
|
262
|
+
// still resolves undefined and leaves the memo, so a transient fetch error
|
|
263
|
+
// retries instead of sticking.
|
|
264
|
+
const decoding = new Map<string, DecodeEntry>()
|
|
265
|
+
let retainedBytes = 0
|
|
217
266
|
// All frame fetches funnel through a small pool. Full-library warm passes
|
|
218
267
|
// on large pets fire 1100+ requests at once, which trips the browser's
|
|
219
268
|
// in-flight request limit (net::ERR_INSUFFICIENT_RESOURCES) and fails
|
|
@@ -263,27 +312,93 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
263
312
|
if (index > 0) frameQueue.unshift(frameQueue.splice(index, 1)[0]!)
|
|
264
313
|
}
|
|
265
314
|
const cached = decoding.get(url)
|
|
266
|
-
if (cached !== undefined)
|
|
315
|
+
if (cached !== undefined) {
|
|
316
|
+
// LRU touch: a hit means playback just asked for this frame, so it must
|
|
317
|
+
// not be the next victim. Re-inserting the same entry only reorders.
|
|
318
|
+
decoding.delete(url)
|
|
319
|
+
decoding.set(url, cached)
|
|
320
|
+
return cached.job
|
|
321
|
+
}
|
|
267
322
|
let release!: () => void
|
|
268
323
|
const gate = new Promise<void>((resolve) => { release = resolve })
|
|
269
324
|
const job: Promise<DecodedFrame | undefined> = gate.then(() => (disposed ? undefined : decodeFrame(url)))
|
|
325
|
+
const entry: DecodeEntry = { job }
|
|
270
326
|
job.then(
|
|
271
|
-
(frame) => {
|
|
327
|
+
(frame) => {
|
|
328
|
+
if (frame === undefined) {
|
|
329
|
+
decoding.delete(url)
|
|
330
|
+
return
|
|
331
|
+
}
|
|
332
|
+
// A decode that lands after dispose is closed here: nothing can close
|
|
333
|
+
// it later, and the map was already drained.
|
|
334
|
+
if (disposed) {
|
|
335
|
+
releaseFrame(frame)
|
|
336
|
+
decoding.delete(url)
|
|
337
|
+
return
|
|
338
|
+
}
|
|
339
|
+
entry.frame = frame
|
|
340
|
+
entry.bytes = frame.width * frame.height * 4
|
|
341
|
+
retainedBytes += entry.bytes
|
|
342
|
+
// Only a resolved frame is evictable, and its decoded size is known
|
|
343
|
+
// only now, so the budget is enforced here.
|
|
344
|
+
enforceRetentionBudget()
|
|
345
|
+
},
|
|
272
346
|
() => decoding.delete(url),
|
|
273
347
|
)
|
|
274
348
|
void job.finally(() => {
|
|
275
349
|
activeFrames -= 1
|
|
276
350
|
pumpFrames()
|
|
277
351
|
})
|
|
278
|
-
decoding.set(url,
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
else frameQueue.push(entry)
|
|
352
|
+
decoding.set(url, entry)
|
|
353
|
+
const queued = { url, release }
|
|
354
|
+
if (jump) frameQueue.unshift(queued)
|
|
355
|
+
else frameQueue.push(queued)
|
|
283
356
|
pumpFrames()
|
|
284
357
|
return job
|
|
285
358
|
}
|
|
286
359
|
|
|
360
|
+
/**
|
|
361
|
+
* Release decoded bitmaps until the retained footprint is back under
|
|
362
|
+
* MAX_RETAINED_BYTES. Two properties keep this from breaking playback:
|
|
363
|
+
*
|
|
364
|
+
* - The playing track's look-ahead window, plus the frame a draw is waiting
|
|
365
|
+
* on, are never released. Releasing one would hand a closed bitmap to
|
|
366
|
+
* `drawImage`, and re-decoding it would stall the next tick.
|
|
367
|
+
* - Frames of any other track go first. That is what keeps a running loop
|
|
368
|
+
* warm under a budget smaller than two whole tracks: eviction reclaims the
|
|
369
|
+
* track the pet left instead of eating the cycle it is playing.
|
|
370
|
+
*
|
|
371
|
+
* When the protected window alone exceeds the budget nothing is released —
|
|
372
|
+
* a pet whose frames are that large cannot be bounded further without
|
|
373
|
+
* breaking playback, and the overshoot is then exactly one window.
|
|
374
|
+
*/
|
|
375
|
+
function enforceRetentionBudget(): void {
|
|
376
|
+
if (retainedBytes <= MAX_RETAINED_BYTES) return
|
|
377
|
+
const def = config.tracks[track]
|
|
378
|
+
const playing = new Set<string>(def === undefined ? [] : def.frames)
|
|
379
|
+
const protectedUrls = new Set<string>()
|
|
380
|
+
if (pinned !== undefined) protectedUrls.add(pinned)
|
|
381
|
+
if (def !== undefined) {
|
|
382
|
+
const end = Math.min(def.frames.length, frameIndex + 1 + PREFETCH_AHEAD)
|
|
383
|
+
for (let index = frameIndex; index < end; index += 1) {
|
|
384
|
+
const url = def.frames[index]
|
|
385
|
+
if (url !== undefined) protectedUrls.add(url)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
// Pass 1 reclaims frames of tracks the pet is not playing; pass 2 is the
|
|
389
|
+
// backstop for a single track larger than the whole budget.
|
|
390
|
+
for (const foreignOnly of [true, false]) {
|
|
391
|
+
for (const [url, entry] of decoding) {
|
|
392
|
+
if (retainedBytes <= MAX_RETAINED_BYTES) return
|
|
393
|
+
if (entry.bytes === undefined || protectedUrls.has(url)) continue
|
|
394
|
+
if (foreignOnly && playing.has(url)) continue
|
|
395
|
+
releaseFrame(entry.frame)
|
|
396
|
+
retainedBytes -= entry.bytes
|
|
397
|
+
decoding.delete(url)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
287
402
|
/**
|
|
288
403
|
* Bounded look-ahead window: decode only the frames playback is about to
|
|
289
404
|
* need. The historical warm pass decoded every frame of every track up
|
|
@@ -318,6 +433,9 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
318
433
|
let baseIdle: string = config.phases.idle
|
|
319
434
|
let drawToken = 0
|
|
320
435
|
let lastDrawnUrl: string | undefined
|
|
436
|
+
// The frame a draw is currently awaiting. Eviction must not release it:
|
|
437
|
+
// the callback below holds the bitmap and would draw a closed one.
|
|
438
|
+
let pinned: string | undefined
|
|
321
439
|
|
|
322
440
|
const trackForPhase = (phase: ActivityPhase): string => {
|
|
323
441
|
const mapped = config.phases[phase]
|
|
@@ -330,7 +448,9 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
330
448
|
const paintCanvas = (url: string): void => {
|
|
331
449
|
if (context2d === null || canvas === null) return
|
|
332
450
|
const myToken = ++drawToken
|
|
451
|
+
pinned = url
|
|
333
452
|
void loadFrame(url, true).then((frame) => {
|
|
453
|
+
if (pinned === url) pinned = undefined
|
|
334
454
|
if (disposed || frame === undefined || myToken !== drawToken) return
|
|
335
455
|
if (lastDrawnUrl === url) return
|
|
336
456
|
lastDrawnUrl = url
|
|
@@ -342,7 +462,7 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
342
462
|
context2d.clearRect(0, 0, canvas.width, canvas.height)
|
|
343
463
|
}
|
|
344
464
|
context2d.drawImage(frame.source as CanvasImageSource, 0, 0)
|
|
345
|
-
}).catch(() => { /* keep last frame */ })
|
|
465
|
+
}).catch(() => { if (pinned === url) pinned = undefined /* keep last frame */ })
|
|
346
466
|
}
|
|
347
467
|
|
|
348
468
|
const show = (trackId: string, index: number): void => {
|
|
@@ -433,21 +553,14 @@ export const frames2dRenderer: PetRenderer<PetFrames2dConfig> = {
|
|
|
433
553
|
unsubscribe()
|
|
434
554
|
if (timer !== undefined) clearTimeout(timer)
|
|
435
555
|
if (watchdog !== undefined) clearInterval(watchdog)
|
|
436
|
-
// Release
|
|
437
|
-
//
|
|
438
|
-
//
|
|
556
|
+
// Release every retained bitmap and drain the pool. Queued-but-unstarted
|
|
557
|
+
// frames resolve as no-ops (the gate skips decodeFrame once disposed), and
|
|
558
|
+
// a decode that lands after this loop is closed by the resolution guard
|
|
559
|
+
// above, so teardown leaves nothing pending and nothing retained.
|
|
439
560
|
for (const queued of frameQueue.splice(0)) queued.release()
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
try {
|
|
444
|
-
const maybeClose = (frame?.source as { close?: () => void } | undefined)?.close
|
|
445
|
-
if (typeof maybeClose === 'function' && frame !== undefined) maybeClose.call(frame.source)
|
|
446
|
-
} catch { /* already released */ }
|
|
447
|
-
}).catch(() => { /* never rejects anyway */ })
|
|
448
|
-
}
|
|
449
|
-
decoding.clear()
|
|
450
|
-
})
|
|
561
|
+
for (const entry of decoding.values()) releaseFrame(entry.frame)
|
|
562
|
+
decoding.clear()
|
|
563
|
+
retainedBytes = 0
|
|
451
564
|
canvas?.remove()
|
|
452
565
|
img?.remove()
|
|
453
566
|
}
|
package/src/registry.test.ts
CHANGED
|
@@ -315,8 +315,8 @@ describe('loadPetRegistry', () => {
|
|
|
315
315
|
})
|
|
316
316
|
|
|
317
317
|
// The repo checkout also resolves doro and miku (frames2d gameplay pets),
|
|
318
|
-
// jyn and jyn-
|
|
319
|
-
// and starry-doll (community sprite2d pet) from
|
|
318
|
+
// jyn, jyn-foxtail and jyn-loli (frames2d gameplay pets), long-niang
|
|
319
|
+
// (sprite2d pet) and starry-doll (community sprite2d pet) from
|
|
320
320
|
// assets/; the npm files whitelist excludes them (Workshop delivery), so
|
|
321
321
|
// npm installs see the atlas pets until a Workshop install lands them
|
|
322
322
|
// under $DSH_HOME/pets. blue-throated-bee-eater ships bundled alongside
|
|
@@ -326,6 +326,7 @@ describe('loadPetRegistry', () => {
|
|
|
326
326
|
'doro',
|
|
327
327
|
'jyn',
|
|
328
328
|
'jyn-foxtail',
|
|
329
|
+
'jyn-loli',
|
|
329
330
|
'long-niang',
|
|
330
331
|
'miku',
|
|
331
332
|
'ouo-neko',
|
package/lib/invariant.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { a as AFFINITY_MAX, n as animationForPhase, o as AFFINITY_RANKS, u as defaultAffinityConfig } from "./state-NvDEoCln.js";
|
|
2
|
-
//#region src/invariant.ts
|
|
3
|
-
/**
|
|
4
|
-
* Package invariants — cheap structural checks run at import time on the
|
|
5
|
-
* host side. Mirrors the pattern used by other dsh plugin packages.
|
|
6
|
-
* @module @linxin666/dsh-pet/invariant
|
|
7
|
-
*/
|
|
8
|
-
/** Assert a condition; throws a descriptive Error when violated. */
|
|
9
|
-
function invariant(condition, message) {
|
|
10
|
-
if (!condition) throw new Error(`[dsh-pet] ${message}`);
|
|
11
|
-
}
|
|
12
|
-
/** Run every package invariant once; throws on the first violation. */
|
|
13
|
-
function runPetInvariants() {
|
|
14
|
-
invariant(AFFINITY_MAX > 0, "AFFINITY_MAX must be positive");
|
|
15
|
-
invariant(AFFINITY_RANKS.length > 0 && AFFINITY_RANKS[0].min === 0, "AFFINITY_RANKS must start at 0");
|
|
16
|
-
invariant(defaultAffinityConfig.turnReward > 0, "turnReward must be positive");
|
|
17
|
-
invariant(defaultAffinityConfig.feedCooldownMs > defaultAffinityConfig.petCooldownMs, "feed cooldown must exceed pet cooldown");
|
|
18
|
-
for (const phase of [
|
|
19
|
-
"idle",
|
|
20
|
-
"waiting",
|
|
21
|
-
"thinking",
|
|
22
|
-
"tool",
|
|
23
|
-
"done"
|
|
24
|
-
]) invariant([
|
|
25
|
-
"idle",
|
|
26
|
-
"running",
|
|
27
|
-
"running-right",
|
|
28
|
-
"waiting",
|
|
29
|
-
"jumping"
|
|
30
|
-
].includes(animationForPhase(phase)), `phase ${phase} maps outside the animation contract`);
|
|
31
|
-
}
|
|
32
|
-
runPetInvariants();
|
|
33
|
-
//#endregion
|
|
34
|
-
export { invariant, runPetInvariants };
|