@luosuqwq/dsh-diagram 0.1.6 → 0.1.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/index.js +3 -32
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -7,9 +7,7 @@
|
|
|
7
7
|
// image_annotate existing image -> PNG with boxes/arrows/labels
|
|
8
8
|
// and a `diagram_guide` tool that hands over the style guide on demand.
|
|
9
9
|
|
|
10
|
-
import { existsSync, mkdirSync, readFileSync, statSync
|
|
11
|
-
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
12
|
-
import { homedir } from 'node:os'
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, statSync } from 'node:fs'
|
|
13
11
|
import { readFile } from 'node:fs/promises'
|
|
14
12
|
import path from 'node:path'
|
|
15
13
|
import { fileURLToPath } from 'node:url'
|
|
@@ -22,32 +20,6 @@ export const name = 'dsh-diagram'
|
|
|
22
20
|
export const inject = ['tools', 'webServer', 'systemPrompt']
|
|
23
21
|
|
|
24
22
|
const ROUTE = '/plugins/dsh-diagram/asset'
|
|
25
|
-
// URLs carry an HMAC over the absolute path, so only files this plugin handed
|
|
26
|
-
// out can be fetched, while previously returned links keep working after a
|
|
27
|
-
// restart. The key is created once under the DSH home.
|
|
28
|
-
let assetKeyCache = null
|
|
29
|
-
function assetKey() {
|
|
30
|
-
if (assetKeyCache !== null) return assetKeyCache
|
|
31
|
-
const dir = path.join(homedir(), '.dsh', 'plugins', 'dsh-diagram')
|
|
32
|
-
const file = path.join(dir, 'asset.key')
|
|
33
|
-
try {
|
|
34
|
-
const value = readFileSync(file, 'utf8').trim()
|
|
35
|
-
if (value.length >= 32) return (assetKeyCache = value)
|
|
36
|
-
} catch {}
|
|
37
|
-
mkdirSync(dir, { recursive: true })
|
|
38
|
-
assetKeyCache = randomBytes(32).toString('hex')
|
|
39
|
-
writeFileSync(file, assetKeyCache, { mode: 0o600 })
|
|
40
|
-
return assetKeyCache
|
|
41
|
-
}
|
|
42
|
-
function signAssetPath(file) {
|
|
43
|
-
return createHmac('sha256', assetKey()).update(path.resolve(file).toLowerCase()).digest('hex').slice(0, 32)
|
|
44
|
-
}
|
|
45
|
-
function assetSignatureValid(file, provided) {
|
|
46
|
-
if (typeof provided !== 'string' || provided.length !== 32) return false
|
|
47
|
-
const expected = Buffer.from(signAssetPath(file))
|
|
48
|
-
const actual = Buffer.from(provided)
|
|
49
|
-
return expected.length === actual.length && timingSafeEqual(expected, actual)
|
|
50
|
-
}
|
|
51
23
|
const OUTPUT_DIR = ['.dsh', 'diagram']
|
|
52
24
|
const MIME = new Map([
|
|
53
25
|
['.png', 'image/png'],
|
|
@@ -119,7 +91,7 @@ function origin(ctx) {
|
|
|
119
91
|
function assetMarkdown(ctx, file) {
|
|
120
92
|
let version = '0'
|
|
121
93
|
try { version = String(Math.round(statSync(file).mtimeMs)) } catch {}
|
|
122
|
-
return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&
|
|
94
|
+
return `${origin(ctx)}${ROUTE}?file=${encodeURIComponent(file)}&v=${version}`
|
|
123
95
|
}
|
|
124
96
|
|
|
125
97
|
function imageResult(ctx, info, alt) {
|
|
@@ -155,9 +127,8 @@ function serveAsset(req, res) {
|
|
|
155
127
|
try {
|
|
156
128
|
const requestUrl = new URL(req.url ?? ROUTE, 'http://127.0.0.1')
|
|
157
129
|
const file = requestUrl.searchParams.get('file')
|
|
158
|
-
const signature = requestUrl.searchParams.get('s')
|
|
159
130
|
const contentType = MIME.get(path.extname(file ?? '').toLowerCase())
|
|
160
|
-
if (!file || !
|
|
131
|
+
if (!file || !contentType || !existsSync(file) || !statSync(file).isFile()) {
|
|
161
132
|
res.writeHead(404)
|
|
162
133
|
res.end()
|
|
163
134
|
return
|
package/package.json
CHANGED