dsh-hashcheck 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +13 -2
- package/bin/cli.js +54 -0
- package/cordis.patch.yml +4 -0
- package/package.json +35 -4
- package/src/index.js +142 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zjukop3
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-hashcheck
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A manifest records the SHA-256 of every file you delivered. Verification says which bytes moved since — a changed file, a missing file, and a file that was delivered but never listed are three different failures, and only the first two get caught by eyeballing a folder.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx dsh-hashcheck build ./delivery --out delivery/hashcheck.json --ignore node_modules
|
|
7
|
+
npx dsh-hashcheck verify delivery/hashcheck.json --dir ./delivery
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Exit codes: `0` clean, `1` findings, `2` usage or read error. In CI, gate on the exit code; in DSH, the same two functions are behind `POST /hashcheck/api` with `{"action":"build"|"verify"}` so the panel and the pipeline cannot disagree.
|
|
11
|
+
|
|
12
|
+
Deterministic by construction: paths are sorted and normalized to forward slashes, no timestamps or mtimes enter the digest, so the same tree produces the same manifest on macOS, Linux and Windows.
|
|
13
|
+
|
|
14
|
+
MIT.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Two verbs, both deterministic: build writes a manifest, verify compares one.
|
|
3
|
+
// Exit codes: 0 clean, 1 findings, 2 usage or read error.
|
|
4
|
+
|
|
5
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
6
|
+
import { buildManifest, serializeManifest, verifyManifest } from '../src/index.js'
|
|
7
|
+
|
|
8
|
+
const args = process.argv.slice(2)
|
|
9
|
+
const command = args[0]
|
|
10
|
+
const target = args[1]
|
|
11
|
+
|
|
12
|
+
function usage(message) {
|
|
13
|
+
if (message) process.stderr.write(`${message}\n`)
|
|
14
|
+
process.stderr.write([
|
|
15
|
+
'usage:',
|
|
16
|
+
' dsh-hashcheck build <directory> [--out hashcheck.json] [--ignore node_modules]',
|
|
17
|
+
' dsh-hashcheck verify <hashcheck.json> [--dir <directory>] [--quiet]',
|
|
18
|
+
'',
|
|
19
|
+
].join('\n'))
|
|
20
|
+
process.exit(2)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function option(name, fallback) {
|
|
24
|
+
const at = args.indexOf(name)
|
|
25
|
+
return at === -1 ? fallback : args[at + 1]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!command || !['build', 'verify'].includes(command)) usage()
|
|
29
|
+
if (!target) usage(`${command} needs a path`)
|
|
30
|
+
|
|
31
|
+
const ignores = args.reduce((list, value, index) => (args[index - 1] === '--ignore' ? [...list, value] : list), [])
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
if (command === 'build') {
|
|
35
|
+
const manifest = await buildManifest(target, { ignore: ignores })
|
|
36
|
+
const out = option('--out', `${target.replace(/\/$/, '')}/hashcheck.json`)
|
|
37
|
+
await writeFile(out, serializeManifest(manifest))
|
|
38
|
+
const count = Object.keys(manifest.files).length
|
|
39
|
+
process.stdout.write(`${out}: ${count} file${count === 1 ? '' : 's'}, sha256\n`)
|
|
40
|
+
process.exit(0)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const manifest = JSON.parse(await readFile(target, 'utf8'))
|
|
44
|
+
const directory = option('--dir', target.includes('/') ? target.slice(0, target.lastIndexOf('/')) || '.' : '.')
|
|
45
|
+
const result = await verifyManifest(manifest, directory, { reportExtra: !args.includes('--no-extra') })
|
|
46
|
+
if (!args.includes('--quiet')) {
|
|
47
|
+
for (const finding of result.findings) process.stdout.write(`${finding.level} ${finding.rule} ${finding.message}\n`)
|
|
48
|
+
}
|
|
49
|
+
process.stdout.write(`${result.ok ? 'ok' : 'fail'}: ${result.checked} checked, ${result.errors} errors, ${result.warnings} warnings\n`)
|
|
50
|
+
process.exit(result.ok ? 0 : 1)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
process.stderr.write(`${error.message}\n`)
|
|
53
|
+
process.exit(2)
|
|
54
|
+
}
|
package/cordis.patch.yml
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-hashcheck",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SHA-256 manifests for delivered files: build once, verify anywhere, and say exactly which bytes moved instead of 'looks fine'.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"dsh-hashcheck": "./bin/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"dsh": {
|
|
15
|
+
"bundle": {
|
|
16
|
+
"patch": "./cordis.patch.yml"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.19.0"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"bin",
|
|
25
|
+
"cordis.patch.yml",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"keywords": ["dsh", "dsh-plugin", "deepseek-harness", "sha256", "hash-manifest", "delivery-verification", "交付校验", "文件哈希"],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "zjukop3",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/zjukop3/dsh-verify-kit.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/zjukop3/dsh-verify-kit/tree/main/packages/dsh-hashcheck"
|
|
37
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// A manifest says what the delivered bytes were; verification says whether they
|
|
2
|
+
// still are. Everything here is deterministic: no timestamps inside the digest,
|
|
3
|
+
// no dependency on mtime, so the same tree hashes the same on any machine.
|
|
4
|
+
|
|
5
|
+
import { createHash } from 'node:crypto'
|
|
6
|
+
import { readdir, readFile, stat } from 'node:fs/promises'
|
|
7
|
+
import { join, relative, sep } from 'node:path'
|
|
8
|
+
|
|
9
|
+
export const name = 'hashcheck'
|
|
10
|
+
export const inject = ['webServer']
|
|
11
|
+
|
|
12
|
+
export const MANIFEST_VERSION = 1
|
|
13
|
+
|
|
14
|
+
export function digestOf(bytes) {
|
|
15
|
+
return createHash('sha256').update(bytes).digest('hex')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Directory order is not stable across filesystems, so the manifest is written
|
|
19
|
+
// in sorted path order and with forward slashes: the file is meant to be diffed.
|
|
20
|
+
export function normalizePath(path) {
|
|
21
|
+
return path.split(sep).join('/')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function listFiles(directory, { ignore = [], recursive = true } = {}) {
|
|
25
|
+
const found = []
|
|
26
|
+
const walk = async current => {
|
|
27
|
+
const entries = await readdir(current, { withFileTypes: true })
|
|
28
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
29
|
+
const full = join(current, entry.name)
|
|
30
|
+
const path = normalizePath(relative(directory, full))
|
|
31
|
+
if (ignore.some(pattern => path === pattern || path.startsWith(`${pattern}/`))) continue
|
|
32
|
+
if (entry.isDirectory()) {
|
|
33
|
+
if (recursive) await walk(full)
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
if (entry.isFile()) found.push(path)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
await walk(directory)
|
|
40
|
+
return found.sort()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function buildManifest(directory, options = {}) {
|
|
44
|
+
const files = await listFiles(directory, options)
|
|
45
|
+
const entries = {}
|
|
46
|
+
for (const path of files) {
|
|
47
|
+
const full = join(directory, path)
|
|
48
|
+
const [bytes, info] = await Promise.all([readFile(full), stat(full)])
|
|
49
|
+
entries[path] = { size: info.size, sha256: digestOf(bytes) }
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
version: MANIFEST_VERSION,
|
|
53
|
+
algorithm: 'sha256',
|
|
54
|
+
// The ignore list travels with the manifest: a verify that does not know
|
|
55
|
+
// what was excluded would report every excluded file as unlisted.
|
|
56
|
+
ignore: [...(options.ignore || [])].sort(),
|
|
57
|
+
files: entries,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function serializeManifest(manifest) {
|
|
62
|
+
return `${JSON.stringify(manifest, null, 2)}\n`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Comparison reports the three states that matter: a file in the manifest that
|
|
66
|
+
// is gone, a file whose bytes changed, and a file delivered but never listed.
|
|
67
|
+
// The third one is the one people forget to check.
|
|
68
|
+
export async function verifyManifest(manifest, directory, { reportExtra = true, ignore } = {}) {
|
|
69
|
+
const findings = []
|
|
70
|
+
const listed = Object.keys(manifest.files || {}).sort()
|
|
71
|
+
for (const path of listed) {
|
|
72
|
+
const expected = manifest.files[path]
|
|
73
|
+
let bytes
|
|
74
|
+
try {
|
|
75
|
+
bytes = await readFile(join(directory, path))
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (error.code === 'ENOENT') {
|
|
78
|
+
findings.push({ level: 'error', rule: 'missing', path, message: `${path} is in the manifest and not on disk` })
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
throw error
|
|
82
|
+
}
|
|
83
|
+
const actual = digestOf(bytes)
|
|
84
|
+
if (actual !== expected.sha256) {
|
|
85
|
+
findings.push({
|
|
86
|
+
level: 'error',
|
|
87
|
+
rule: 'changed',
|
|
88
|
+
path,
|
|
89
|
+
expected: expected.sha256,
|
|
90
|
+
actual,
|
|
91
|
+
message: `${path} changed: expected ${expected.sha256.slice(0, 12)}…, found ${actual.slice(0, 12)}…`,
|
|
92
|
+
})
|
|
93
|
+
} else if (bytes.length !== expected.size) {
|
|
94
|
+
findings.push({
|
|
95
|
+
level: 'warn',
|
|
96
|
+
rule: 'size-mismatch',
|
|
97
|
+
path,
|
|
98
|
+
message: `${path} hashes the same but the manifest records ${expected.size} bytes and the file has ${bytes.length}`,
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (reportExtra) {
|
|
103
|
+
const present = await listFiles(directory, { ignore: ignore ?? manifest.ignore ?? [] })
|
|
104
|
+
const extra = present.filter(path => !(path in (manifest.files || {})))
|
|
105
|
+
for (const path of extra) {
|
|
106
|
+
findings.push({ level: 'warn', rule: 'unlisted', path, message: `${path} is on disk but not in the manifest` })
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const errors = findings.filter(finding => finding.level === 'error').length
|
|
110
|
+
return { ok: errors === 0, checked: listed.length, errors, warnings: findings.length - errors, findings }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The host route is a thin wrapper: the CLI and the route call the same
|
|
114
|
+
// functions, so a check that passes in CI cannot fail in the panel.
|
|
115
|
+
export function apply(ctx, config = {}) {
|
|
116
|
+
const prefix = config.path || '/hashcheck/api'
|
|
117
|
+
const handler = async (req, res) => {
|
|
118
|
+
const send = (status, body) => {
|
|
119
|
+
res.statusCode = status
|
|
120
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
121
|
+
res.end(JSON.stringify(body))
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
if (req.method !== 'POST') return send(405, { error: 'POST only' })
|
|
125
|
+
const chunks = []
|
|
126
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
127
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')
|
|
128
|
+
if (body.action === 'build') {
|
|
129
|
+
const manifest = await buildManifest(String(body.directory || '.'), { ignore: body.ignore || [] })
|
|
130
|
+
return send(200, { manifest, serialized: serializeManifest(manifest) })
|
|
131
|
+
}
|
|
132
|
+
if (body.action === 'verify') {
|
|
133
|
+
if (!body.manifest || typeof body.manifest !== 'object') return send(400, { error: 'manifest is required' })
|
|
134
|
+
return send(200, await verifyManifest(body.manifest, String(body.directory || '.')))
|
|
135
|
+
}
|
|
136
|
+
return send(400, { error: 'action must be build or verify' })
|
|
137
|
+
} catch (error) {
|
|
138
|
+
return send(400, { error: error.message || 'request failed' })
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: prefix, handler }))
|
|
142
|
+
}
|