@linxin666/dsh-pet 0.4.10 → 0.4.12

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.
Files changed (58) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +1 -1
  3. package/README.zh.md +4 -4
  4. package/lib/client.js +1137 -352
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +32 -0
  7. package/lib/types/client/PetPreview.d.ts +12 -0
  8. package/lib/types/client/PetPreview.d.ts.map +1 -0
  9. package/lib/types/client/PetPreview.js +32 -0
  10. package/lib/types/client/PetSettingsCard.d.ts +46 -3
  11. package/lib/types/client/PetSettingsCard.d.ts.map +1 -1
  12. package/lib/types/client/PetSettingsCard.js +96 -11
  13. package/lib/types/client/PluginSettingsCard.d.ts +131 -17
  14. package/lib/types/client/PluginSettingsCard.d.ts.map +1 -1
  15. package/lib/types/client/PluginSettingsCard.js +158 -37
  16. package/lib/types/client/RangeBubble.d.ts +47 -0
  17. package/lib/types/client/RangeBubble.d.ts.map +1 -0
  18. package/lib/types/client/RangeBubble.js +107 -0
  19. package/lib/types/client/SegmentGroup.d.ts +29 -0
  20. package/lib/types/client/SegmentGroup.d.ts.map +1 -0
  21. package/lib/types/client/SegmentGroup.js +81 -0
  22. package/lib/types/client/index.d.ts.map +1 -1
  23. package/lib/types/client/index.js +9 -28
  24. package/lib/types/client/locales.d.ts +36 -44
  25. package/lib/types/client/locales.d.ts.map +1 -1
  26. package/lib/types/client/locales.js +36 -44
  27. package/lib/types/client/settings-form.d.ts +79 -41
  28. package/lib/types/client/settings-form.d.ts.map +1 -1
  29. package/lib/types/client/settings-form.js +96 -36
  30. package/lib/types/client/settings-registration.d.ts +15 -0
  31. package/lib/types/client/settings-registration.d.ts.map +1 -0
  32. package/lib/types/client/settings-registration.js +24 -0
  33. package/lib/types/client/web-extension-contract.d.ts +32 -0
  34. package/lib/types/client/web-extension-contract.d.ts.map +1 -0
  35. package/lib/types/client/web-extension-contract.js +6 -0
  36. package/lib/types/client/web-extension-page.d.ts +51 -0
  37. package/lib/types/client/web-extension-page.d.ts.map +1 -0
  38. package/lib/types/client/web-extension-page.js +122 -0
  39. package/lib/types/registry.d.ts.map +1 -1
  40. package/lib/types/registry.js +35 -0
  41. package/package.json +1 -1
  42. package/src/client/PetPreview.tsx +51 -0
  43. package/src/client/PetSettingsCard.test.tsx +78 -22
  44. package/src/client/PetSettingsCard.tsx +252 -115
  45. package/src/client/PluginSettingsCard.tsx +369 -108
  46. package/src/client/RangeBubble.tsx +163 -0
  47. package/src/client/SegmentGroup.tsx +92 -0
  48. package/src/client/index.ts +9 -27
  49. package/src/client/locales.ts +36 -44
  50. package/src/client/settings-card.module.css +493 -91
  51. package/src/client/settings-form.ts +116 -49
  52. package/src/client/settings-registration.test.ts +79 -0
  53. package/src/client/settings-registration.ts +34 -0
  54. package/src/client/settings-section.module.css +182 -0
  55. package/src/client/web-extension-contract.ts +35 -0
  56. package/src/client/web-extension-page.ts +150 -0
  57. package/src/registry.test.ts +121 -1
  58. package/src/registry.ts +37 -0
@@ -0,0 +1,150 @@
1
+ /**
2
+ * One settings page's seat in either the Web Extensions parent or the legacy
3
+ * first-level settings navigation.
4
+ *
5
+ * The parent supplies the page contract; a child does not know which slot the
6
+ * parent uses internally. While `webUiExtensions` is live the page is handed
7
+ * to its service, which owns keyed mounting and disposal. Without the parent,
8
+ * the same component registers as its original first-level settings section,
9
+ * so a standalone install keeps a working entry.
10
+ */
11
+
12
+ /** The parent service face this helper consumes. */
13
+ export interface WebExtensionPageService {
14
+ readonly sectionId: string
15
+ registerPage(page: WebExtensionPageSeat): () => void
16
+ }
17
+
18
+ /** Seat-independent page contribution. */
19
+ export interface WebExtensionPageSeat {
20
+ key: string
21
+ order: number
22
+ locale: string
23
+ label(): string
24
+ inject(): object
25
+ component: unknown
26
+ children?: Record<string, { kind: 'keyed' | 'list'; scope: 'root' }>
27
+ }
28
+
29
+ /** The slot-registry slice used for the standalone fallback. */
30
+ export interface WebExtensionPageSlots {
31
+ register(options: never, component: never): unknown
32
+ spec?(key: string): unknown
33
+ entries?(key: string): readonly { component?: unknown; options: Record<string, unknown> }[]
34
+ }
35
+
36
+ /** The context slice needed by the migration. */
37
+ export interface WebExtensionPageContext {
38
+ slots: WebExtensionPageSlots
39
+ get?(name: string): unknown
40
+ on?(event: string, listener: () => void): unknown
41
+ }
42
+
43
+ const SETTINGS_SECTION = 'settings.section'
44
+ const WEB_EXTENSION_SLOT = 'web-ui.extension'
45
+
46
+ function serviceOf(ctx: WebExtensionPageContext): WebExtensionPageService | undefined {
47
+ if (typeof ctx.get !== 'function') return undefined
48
+ try {
49
+ const candidate = ctx.get('webUiExtensions') as Partial<WebExtensionPageService> | undefined
50
+ return candidate !== null
51
+ && typeof candidate === 'object'
52
+ && typeof candidate.sectionId === 'string'
53
+ && typeof candidate.registerPage === 'function'
54
+ ? candidate as WebExtensionPageService
55
+ : undefined
56
+ } catch {
57
+ return undefined
58
+ }
59
+ }
60
+
61
+ function warn(seat: string, error: unknown): void {
62
+ try {
63
+ console.warn(`[dsh-web-settings] page "${seat}" could not be mounted`, error)
64
+ } catch {
65
+ // A restricted console must not drop the remaining entry.
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Keep exactly one reachable page contribution while the parent loads or
71
+ * unloads. The parent service owns integrated mounting; only the fallback uses
72
+ * this plugin's own slot registry. Slot disposal is synchronous, so the latch
73
+ * prevents a move from recursively registering a duplicate.
74
+ */
75
+ export function installWebExtensionPage(
76
+ ctx: WebExtensionPageContext,
77
+ page: WebExtensionPageSeat,
78
+ ): () => void {
79
+ let dispose: (() => void) | undefined
80
+ let current: 'integrated' | 'legacy' | undefined
81
+ let reconciling = false
82
+ let disposed = false
83
+
84
+ const present = (seat: 'integrated' | 'legacy'): boolean => {
85
+ if (typeof ctx.slots.entries !== 'function') return current === seat
86
+ const slot = seat === 'integrated' ? WEB_EXTENSION_SLOT : SETTINGS_SECTION
87
+ return ctx.slots.entries(slot).some(entry =>
88
+ entry.component === page.component
89
+ && (seat === 'integrated' ? entry.options.key : entry.options.id) === page.key)
90
+ }
91
+
92
+ const registerSeat = (seat: 'integrated' | 'legacy'): (() => void) => {
93
+ if (seat === 'integrated') {
94
+ const service = serviceOf(ctx)
95
+ if (service === undefined) throw new Error('webUiExtensions service is unavailable')
96
+ return service.registerPage(page)
97
+ }
98
+ return ctx.slots.register({
99
+ name: SETTINGS_SECTION,
100
+ id: page.key,
101
+ order: page.order,
102
+ label: page.label,
103
+ locale: page.locale,
104
+ inject: page.inject as never,
105
+ ...(page.children === undefined ? {} : { children: page.children }),
106
+ } as never, page.component as never) as () => void
107
+ }
108
+
109
+ const reconcile = (): void => {
110
+ if (disposed || reconciling) return
111
+ const target: 'integrated' | 'legacy' = serviceOf(ctx) === undefined ? 'legacy' : 'integrated'
112
+ if (current === target && present(target)) return
113
+ const slot = target === 'integrated' ? WEB_EXTENSION_SLOT : SETTINGS_SECTION
114
+ if (typeof ctx.slots.spec === 'function' && ctx.slots.spec(slot) === undefined) return
115
+ reconciling = true
116
+ const previous = current
117
+ const disposePrevious = dispose
118
+ current = undefined
119
+ dispose = undefined
120
+ disposePrevious?.()
121
+ try {
122
+ dispose = registerSeat(target)
123
+ current = target
124
+ } catch (error) {
125
+ warn(target, error)
126
+ if (previous !== undefined && previous !== target) {
127
+ try {
128
+ dispose = registerSeat(previous)
129
+ current = previous
130
+ } catch (restoreError) {
131
+ warn(previous, restoreError)
132
+ }
133
+ }
134
+ } finally {
135
+ reconciling = false
136
+ }
137
+ }
138
+
139
+ if (typeof ctx.on === 'function') {
140
+ try { ctx.on('slots/changed', reconcile) } catch { /* the initial entry remains usable */ }
141
+ }
142
+ reconcile()
143
+ return () => {
144
+ if (disposed) return
145
+ disposed = true
146
+ dispose?.()
147
+ dispose = undefined
148
+ current = undefined
149
+ }
150
+ }
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { closeSync, existsSync, ftruncateSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { closeSync, existsSync, ftruncateSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmdirSync, rmSync, unlinkSync, writeFileSync } from 'node:fs'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join } from 'node:path'
5
5
  import {
@@ -1040,3 +1040,123 @@ describe('status decorations (pet-center M5, #567)', () => {
1040
1040
  }
1041
1041
  })
1042
1042
  })
1043
+
1044
+ describe('interrupted install staging leftovers (issue #1952)', () => {
1045
+ /** Write one pet fixture directory named 'name' under 'dir'. */
1046
+ function writePet(dir: string, name: string, manifest: Record<string, unknown>): void {
1047
+ mkdirSync(join(dir, name), { recursive: true })
1048
+ writeFileSync(join(dir, name, 'pet.json'), JSON.stringify(manifest), 'utf8')
1049
+ }
1050
+
1051
+ // The staging name the market installer builds before it renames the
1052
+ // directory onto its id: '<id>.install-<base36 ms>-<base36 random>'.
1053
+ const stagingName = 'jyn-loli.install-mvo23x51-thmrh2'
1054
+
1055
+ /**
1056
+ * Undo exactly what writePet wrote, one known path at a time: the pet.json
1057
+ * files this block created, then the directories they leave empty. Nothing
1058
+ * here deletes a tree it did not name, so a stray file in the fixture makes
1059
+ * rmdirSync fail loudly instead of disappearing with it.
1060
+ */
1061
+ function removeFixtures(root: string, names: readonly string[]): void {
1062
+ const petsDir = join(root, 'pets')
1063
+ for (const name of names) {
1064
+ const manifest = join(petsDir, name, 'pet.json')
1065
+ if (existsSync(manifest)) unlinkSync(manifest)
1066
+ const dir = join(petsDir, name)
1067
+ if (existsSync(dir)) rmdirSync(dir)
1068
+ }
1069
+ if (existsSync(petsDir)) rmdirSync(petsDir)
1070
+ if (existsSync(root)) rmdirSync(root)
1071
+ }
1072
+
1073
+ it('keeps the finished pet when a leftover staging directory carries its id', () => {
1074
+ const root = tempDir()
1075
+ try {
1076
+ const petsDir = join(root, 'pets')
1077
+ writePet(petsDir, 'jyn-loli', { id: 'jyn-loli', displayName: 'Jyn Loli', spritesheetPath: 'spritesheet.webp' })
1078
+ // A partial install far enough along to hold a valid manifest: before the
1079
+ // fix it sorted after 'jyn-loli' and won the id collision, so the board
1080
+ // rendered the half-installed pet.
1081
+ writePet(petsDir, stagingName, { id: 'jyn-loli', displayName: 'half installed', spritesheetPath: 'spritesheet.webp' })
1082
+
1083
+ const registry = loadPetRegistry({ packageRoot: root, petsDir, dshPetsDir: '' })
1084
+
1085
+ expect(registry.entries.map(entry => entry.id)).toEqual(['jyn-loli'])
1086
+ expect(registry.byId('jyn-loli')!.displayName).toBe('Jyn Loli')
1087
+ expect(registry.byId('jyn-loli')!.dir).toBe(join(petsDir, 'jyn-loli'))
1088
+
1089
+ const leftover = registry.diagnostics.filter(diagnostic => diagnostic.source.includes(stagingName))
1090
+ expect(leftover).toHaveLength(1)
1091
+ expect(leftover[0]!.level).toBe('warning')
1092
+ expect(leftover[0]!.source).toBe(join(petsDir, stagingName))
1093
+ expect(leftover[0]!.message).toContain(stagingName)
1094
+ expect(registry.warnings.some(warning => warning.includes(stagingName))).toBe(true)
1095
+
1096
+ // Reported, never cleaned up: the scan stays read-only.
1097
+ expect(existsSync(join(petsDir, stagingName))).toBe(true)
1098
+ } finally {
1099
+ removeFixtures(root, ['jyn-loli', stagingName])
1100
+ }
1101
+ })
1102
+
1103
+ it('reports a staging leftover even when it holds no pet.json yet', () => {
1104
+ const root = tempDir()
1105
+ try {
1106
+ const petsDir = join(root, 'pets')
1107
+ writePet(petsDir, 'jyn-loli', { id: 'jyn-loli', displayName: 'Jyn Loli', spritesheetPath: 'spritesheet.webp' })
1108
+ // The shape is filtered before the manifest is looked for, so a leftover
1109
+ // that failed on its first file is still diagnosable.
1110
+ mkdirSync(join(petsDir, stagingName), { recursive: true })
1111
+
1112
+ const registry = loadPetRegistry({ packageRoot: root, petsDir, dshPetsDir: '' })
1113
+
1114
+ expect(registry.entries.map(entry => entry.id)).toEqual(['jyn-loli'])
1115
+ expect(registry.diagnostics.some(diagnostic => diagnostic.source === join(petsDir, stagingName))).toBe(true)
1116
+ } finally {
1117
+ removeFixtures(root, ['jyn-loli', stagingName])
1118
+ }
1119
+ })
1120
+
1121
+ it('leaves a dot-prefixed staging directory to the existing hidden-entry rule', () => {
1122
+ const root = tempDir()
1123
+ try {
1124
+ const petsDir = join(root, 'pets')
1125
+ writePet(petsDir, 'jyn-loli', { id: 'jyn-loli', displayName: 'Jyn Loli', spritesheetPath: 'spritesheet.webp' })
1126
+ const hidden = '.' + stagingName
1127
+ writePet(petsDir, hidden, { id: 'jyn-loli', displayName: 'half installed', spritesheetPath: 'spritesheet.webp' })
1128
+
1129
+ const registry = loadPetRegistry({ packageRoot: root, petsDir, dshPetsDir: '' })
1130
+
1131
+ expect(registry.entries.map(entry => entry.id)).toEqual(['jyn-loli'])
1132
+ expect(registry.byId('jyn-loli')!.displayName).toBe('Jyn Loli')
1133
+ // The dot prefix already drops the directory silently; the staging rule
1134
+ // must not add a second, noisier report for it.
1135
+ expect(registry.diagnostics.some(diagnostic => diagnostic.source.includes(hidden))).toBe(false)
1136
+ expect(registry.warnings.some(warning => warning.includes(hidden))).toBe(false)
1137
+ } finally {
1138
+ removeFixtures(root, ['jyn-loli', '.' + stagingName])
1139
+ }
1140
+ })
1141
+
1142
+ it('keeps a pet directory whose name only mentions .install- outside the staging shape', () => {
1143
+ const root = tempDir()
1144
+ try {
1145
+ const petsDir = join(root, 'pets')
1146
+ writePet(petsDir, 'jyn-loli', { id: 'jyn-loli', displayName: 'Jyn Loli', spritesheetPath: 'spritesheet.webp' })
1147
+ // One base36 segment, not the '<ms>-<random>' pair the installer writes.
1148
+ writePet(petsDir, 'jyn-loli.install-guide', { id: 'jyn-loli-guide', displayName: 'guide', spritesheetPath: 'spritesheet.webp' })
1149
+
1150
+ const registry = loadPetRegistry({ packageRoot: root, petsDir, dshPetsDir: '' })
1151
+
1152
+ // The directory is scanned as an ordinary pet: listed, and carrying
1153
+ // whatever diagnostics its own manifest earns. The staging rule itself
1154
+ // stays silent, so the shape match is exact rather than a prefix test.
1155
+ expect(registry.entries.map(entry => entry.id)).toEqual(['jyn-loli', 'jyn-loli-guide'])
1156
+ expect(registry.warnings.some(warning => warning.includes('install staging leftover'))).toBe(false)
1157
+ expect(registry.diagnostics.some(diagnostic => diagnostic.message.includes('install staging leftover'))).toBe(false)
1158
+ } finally {
1159
+ removeFixtures(root, ['jyn-loli', 'jyn-loli.install-guide'])
1160
+ }
1161
+ })
1162
+ })
package/src/registry.ts CHANGED
@@ -837,6 +837,29 @@ function resolveFrames2dEntry(
837
837
  }
838
838
  }
839
839
 
840
+ /**
841
+ * Directory-name suffix the market installer carries while it downloads:
842
+ * '<id>.install-<base36 ms>-<base36 suffix>'
843
+ * (`installAsset` in dsh-web `packages/dsh-market/src/core/installer.ts` builds
844
+ * it as `dest + '.install-' + Date.now().toString(36) + '-' +
845
+ * Math.random().toString(36).slice(2, 8)`). Success renames the staging
846
+ * directory onto its id, and the failure path removes it, so such a directory
847
+ * surviving on disk means an install was interrupted and what it holds is a
848
+ * partial pet.
849
+ *
850
+ * The bounds are the producer's real range rather than a guess: base36 of
851
+ * `Date.now()` is 8 digits today and stays under 11 for several millennia,
852
+ * and `.slice(2, 8)` always yields exactly 6 base36 characters. A directory
853
+ * whose name merely mentions '.install-' outside that shape stays an ordinary
854
+ * pet directory.
855
+ */
856
+ const PET_INSTALL_STAGING_NAME = /\.install-[0-9a-z]{1,11}-[0-9a-z]{1,11}$/
857
+
858
+ /** True when a pet directory name is an interrupted-install staging leftover. */
859
+ function isPetInstallStagingName(name: string): boolean {
860
+ return PET_INSTALL_STAGING_NAME.test(name)
861
+ }
862
+
840
863
  /** Scan one directory of pet folders; entries come back in name order. */
841
864
  function scanPetDir(dir: string, options: { assetPrefix?: string; warnings?: string[]; diagnostics?: PetRegistryDiagnostic[] }): PetEntry[] {
842
865
  if (!existsSync(dir)) return []
@@ -849,6 +872,20 @@ function scanPetDir(dir: string, options: { assetPrefix?: string; warnings?: str
849
872
  names.sort()
850
873
  const entries: PetEntry[] = []
851
874
  for (const name of names) {
875
+ // An interrupted market install leaves '<id>.install-<base36>-<base36>'
876
+ // behind holding a partial pet.json. Because a directory sorts just before
877
+ // its own staging suffixes and later entries win the id collision below,
878
+ // the leftover would override the finished pet with the same id. Drop it
879
+ // before the manifest is read, and only report it: cleanup belongs to the
880
+ // installer, and a scan must stay read-only.
881
+ if (isPetInstallStagingName(name)) {
882
+ const leftover = join(dir, name)
883
+ const message = 'skipping install staging leftover ' + name + ' (' + leftover
884
+ + '): an interrupted install left it behind, and it is ignored so it cannot override the finished pet with the same id'
885
+ options.warnings?.push(message)
886
+ options.diagnostics?.push({ level: 'warning', source: leftover, message })
887
+ continue
888
+ }
852
889
  const manifestFile = join(dir, name, 'pet.json')
853
890
  if (!existsSync(manifestFile)) continue
854
891
  const parsed = readPetJson(manifestFile, options)