@linxin666/dsh-pet 0.2.0 → 0.2.2
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 +15 -5
- package/README.zh.md +15 -5
- package/assets/whale/pet.json +1 -1
- package/assets/whale-refined/pet.json +7 -0
- package/assets/whale-refined/spritesheet.webp +0 -0
- package/lib/client.js +257 -53
- package/lib/client.js.map +1 -1
- package/lib/index.js +235 -83
- package/lib/invariant.js +1 -1
- package/lib/{state-Ch3f4luZ.js → state-DrMX22GL.js} +35 -5
- package/lib/types/chatter.d.ts.map +1 -1
- package/lib/types/chatter.js +167 -77
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +1 -1
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +54 -11
- package/lib/types/client/PluginSettingsCard.d.ts +22 -0
- package/lib/types/client/PluginSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PluginSettingsCard.js +142 -3
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +1 -0
- package/lib/types/client/locales.d.ts +4 -0
- package/lib/types/client/locales.d.ts.map +1 -1
- package/lib/types/client/locales.js +4 -0
- package/lib/types/client/spritesheet.d.ts +1 -3
- package/lib/types/client/spritesheet.d.ts.map +1 -1
- package/lib/types/client/spritesheet.js +1 -5
- package/lib/types/loopback.d.ts +25 -0
- package/lib/types/loopback.d.ts.map +1 -0
- package/lib/types/loopback.js +68 -0
- package/lib/types/registry.d.ts +0 -2
- package/lib/types/registry.d.ts.map +1 -1
- package/lib/types/registry.js +11 -7
- package/lib/types/remarks.d.ts.map +1 -1
- package/lib/types/remarks.js +30 -0
- package/lib/types/routes.d.ts.map +1 -1
- package/lib/types/routes.js +14 -0
- package/lib/types/service.d.ts.map +1 -1
- package/package.json +1 -2
- package/src/chatter.ts +167 -77
- package/src/client/PetDockEntry.test.tsx +93 -0
- package/src/client/PetDockEntry.tsx +1 -0
- package/src/client/PetSprite.test.tsx +72 -11
- package/src/client/PetSprite.tsx +98 -30
- package/src/client/PluginSettingsCard.tsx +219 -18
- package/src/client/index.test.tsx +95 -0
- package/src/client/index.ts +1 -0
- package/src/client/locales.ts +4 -0
- package/src/client/pet-css.test.ts +15 -3
- package/src/client/pet.module.css +101 -71
- package/src/client/settings-card.module.css +90 -1
- package/src/client/spritesheet.ts +1 -6
- package/src/loopback.ts +63 -0
- package/src/registry.test.ts +49 -1
- package/src/registry.ts +11 -7
- package/src/remarks.ts +30 -0
- package/src/routes.ts +11 -0
- package/src/service.ts +0 -1
package/src/routes.ts
CHANGED
|
@@ -17,6 +17,7 @@ import type { WebRoute } from '@deepseek-ai/dsh-host-webserver'
|
|
|
17
17
|
import type { PetService } from './service.ts'
|
|
18
18
|
import type { PetInteraction } from './affinity.ts'
|
|
19
19
|
import { petEntryView, type PetEntry, type PetRegistry } from './registry.ts'
|
|
20
|
+
import { isLoopbackRequest } from './loopback.ts'
|
|
20
21
|
|
|
21
22
|
/** Browser-facing base path of the pet API. */
|
|
22
23
|
export const PET_API_PREFIX = '/api/pet'
|
|
@@ -86,12 +87,20 @@ function readJsonBody(req: IncomingMessage): Promise<unknown> {
|
|
|
86
87
|
})
|
|
87
88
|
}
|
|
88
89
|
|
|
90
|
+
/** Shared route fence: the browser UI is a loopback client; LAN hosts stay out. */
|
|
91
|
+
function guard(req: IncomingMessage, res: ServerResponse): boolean {
|
|
92
|
+
if (isLoopbackRequest(req)) return true
|
|
93
|
+
json(res, 403, { ok: false, error: 'forbidden: loopback-only' })
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
89
97
|
/** Wrap one async service call as a GET JSON route. */
|
|
90
98
|
function getRoute(path: string, run: () => Promise<unknown>): WebRoute {
|
|
91
99
|
return {
|
|
92
100
|
kind: 'exact',
|
|
93
101
|
path,
|
|
94
102
|
handler: (req: IncomingMessage, res: ServerResponse): void => {
|
|
103
|
+
if (!guard(req, res)) return
|
|
95
104
|
if (!requireMethod(req, res, 'GET')) return
|
|
96
105
|
run().then((value) => json(res, 200, value), (error) => {
|
|
97
106
|
json(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
@@ -106,6 +115,7 @@ function postRoute(path: string, run: (body: Record<string, unknown>) => Promise
|
|
|
106
115
|
kind: 'exact',
|
|
107
116
|
path,
|
|
108
117
|
handler: (req: IncomingMessage, res: ServerResponse): Promise<void> => {
|
|
118
|
+
if (!guard(req, res)) return Promise.resolve()
|
|
109
119
|
if (!requireMethod(req, res, 'POST')) return Promise.resolve()
|
|
110
120
|
return readJsonBody(req).then((body) => {
|
|
111
121
|
const record = (typeof body === 'object' && body !== null) ? body as Record<string, unknown> : {}
|
|
@@ -141,6 +151,7 @@ function dirAliases(registry: PetRegistry): Map<string, PetEntry> {
|
|
|
141
151
|
function assetHandler(registry: PetRegistry): WebRoute['handler'] {
|
|
142
152
|
const aliases = dirAliases(registry)
|
|
143
153
|
return (req: IncomingMessage, res: ServerResponse): void => {
|
|
154
|
+
if (!guard(req, res)) return
|
|
144
155
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
145
156
|
res.writeHead(405)
|
|
146
157
|
res.end()
|