@i18n.site/hash_change 0.0.0-stage → 0.1.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.md +61 -2
- package/changeCheck.js +18 -0
- package/fileHash.js +13 -0
- package/frameUpdate.js +12 -0
- package/hashFile.js +13 -0
- package/hashOf.js +18 -0
- package/package.json +1 -6
- package/walkFile.js +42 -0
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://www.npmjs.com/package/@i18n.site/hash_change) [](https://www.apache.org/licenses/LICENSE-2.0)
|
|
2
|
+
|
|
3
|
+
[](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/en/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/zh/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/fr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/de/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/es/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ja/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ar/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/pt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ru/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/it/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/nl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ko/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/tr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/id/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/zh-TW/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/hi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/pl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ta/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ga/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sv/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/bn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/he/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/th/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/no/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/vi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/mr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ms/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/da/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ro/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ur/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/cs/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/fa/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/te/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/pa/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/kn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/gu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/el/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/fi/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ca/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/gd/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/kk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/tl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sw/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ug/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/hu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/uk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/uz/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ml/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/af/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/hr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sr/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/bg/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/br/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/am/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/cy/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/or/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/si/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ha/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/eu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/szl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/lt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/be/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/zu/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/gl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/my/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sl/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/az/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/as/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/km/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ne/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ku/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/xh/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/lv/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/et/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/tt/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/is/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ka/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/bs/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/sq/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/hy/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/mn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ky/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/mk/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/lo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/tg/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/rw/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/eo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/fil/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/nn/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/ckb/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/wo/README.md) [](https://atomgit.com/i18n-site/rs/blob/main/hash_change/i18n/fy/README.md)
|
|
4
|
+
|
|
5
|
+
# hash_change
|
|
6
|
+
|
|
7
|
+
Content hash based change detection for a set of files.
|
|
8
|
+
|
|
9
|
+
Give it one or more directories plus extra files; it hashes their contents in a
|
|
10
|
+
stable order, compares the digest with the value recorded in `.dist.hash`, and
|
|
11
|
+
tells you whether anything changed. Publish scripts use it to decide whether a
|
|
12
|
+
package needs a new version.
|
|
13
|
+
|
|
14
|
+
Paths are collected with [`@1-/walk`](https://www.npmjs.com/package/@1-/walk);
|
|
15
|
+
`node_modules`, `.git` and dot entries are skipped by default.
|
|
16
|
+
|
|
17
|
+
Run `bun examples/basic.js` inside the package for a live report.
|
|
18
|
+
|
|
19
|
+
- [Usage](#usage)
|
|
20
|
+
- [API](#api)
|
|
21
|
+
- [Hash format](#hash-format)
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import changeCheck, { CHANGED, HASH } from '@i18n.site/hash_change/changeCheck.js'
|
|
27
|
+
import { hashWrite } from '@i18n.site/hash_change/hashFile.js'
|
|
28
|
+
|
|
29
|
+
// Hash src/ plus i18n/en/README.md, compare with .dist.hash
|
|
30
|
+
const [changed, hash] = await changeCheck(dir, ['src'], ['i18n/en/README.md'])
|
|
31
|
+
|
|
32
|
+
if (changed) {
|
|
33
|
+
// bump the version, render the README, then record the new hash
|
|
34
|
+
await hashWrite(dir, hash)
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Only the source of truth is hashed: for a package this is `src/` and
|
|
39
|
+
`i18n/en/README.md`, not the generated `README.md`. Bumping the version in
|
|
40
|
+
`package.json` therefore does not feed back into the next comparison.
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
- `changeCheck(dir, dir_li, file_li, tag)` returns `[changed, hash, rel_li]`.
|
|
45
|
+
- `walkFile(root_dir, dir_li, file_li, skip_li)` returns the sorted relative file list.
|
|
46
|
+
- `fileHash(dir, rel_li, tag)` hashes the listed files with one tag.
|
|
47
|
+
- `hashOf(entry_li)` hashes `[tag, rel_path, bytes]` entries; use it when
|
|
48
|
+
different kinds of content need different tags.
|
|
49
|
+
- `hashRead(dir)` / `hashWrite(dir, hash)` read and write `.dist.hash`.
|
|
50
|
+
|
|
51
|
+
## Hash format
|
|
52
|
+
|
|
53
|
+
Each entry updates the SHA-256 state with
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
tag "\0" rel_path "\0" byte_length "\n"
|
|
57
|
+
bytes
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
so renaming a file, moving bytes between files, or changing a tag all produce a
|
|
61
|
+
different digest even when the concatenated content is identical.
|
|
2
62
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
package/changeCheck.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import walkFile from './walkFile.js'
|
|
2
|
+
import fileHash, { TAG_FILE } from './fileHash.js'
|
|
3
|
+
import { hashRead } from './hashFile.js'
|
|
4
|
+
|
|
5
|
+
export const CHANGED = 0,
|
|
6
|
+
HASH = 1,
|
|
7
|
+
REL_LI = 2
|
|
8
|
+
|
|
9
|
+
// Detect whether the content of dir_li + file_li changed since the recorded
|
|
10
|
+
// hash. Returns [changed, hash, rel_li]; call hashWrite to record the new hash.
|
|
11
|
+
const changeCheck = async (dir, dir_li = [], file_li = [], tag = TAG_FILE) => {
|
|
12
|
+
const rel_li = await walkFile(dir, dir_li, file_li),
|
|
13
|
+
hash = await fileHash(dir, rel_li, tag),
|
|
14
|
+
old_hash = await hashRead(dir)
|
|
15
|
+
return [old_hash !== hash, hash, rel_li]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default changeCheck
|
package/fileHash.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import hashOf from './hashOf.js'
|
|
2
|
+
|
|
3
|
+
export const TAG_FILE = 'FILE'
|
|
4
|
+
|
|
5
|
+
// Hash the bytes of rel_li under dir, in list order.
|
|
6
|
+
const fileHash = async (dir, rel_li, tag = TAG_FILE) => {
|
|
7
|
+
const entry_li = await Promise.all(
|
|
8
|
+
rel_li.map(async (rel) => [tag, rel, await Bun.file(dir + '/' + rel).bytes()])
|
|
9
|
+
)
|
|
10
|
+
return hashOf(entry_li)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default fileHash
|
package/frameUpdate.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Hash one framed entry: a tag, a relative path, the byte length and the bytes.
|
|
2
|
+
//
|
|
3
|
+
// Framing keeps the digest unambiguous: moving bytes between two files, or
|
|
4
|
+
// renaming a file, always produces a different hash even when the total
|
|
5
|
+
// content is identical.
|
|
6
|
+
const frameUpdate = (hasher, tag, rel_path, bytes) => {
|
|
7
|
+
const data = typeof bytes === 'string' ? Buffer.from(bytes) : bytes
|
|
8
|
+
hasher.update(tag + '\0' + rel_path + '\0' + data.length + '\n')
|
|
9
|
+
hasher.update(data)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default frameUpdate
|
package/hashFile.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Default record file, one per package / crate directory.
|
|
2
|
+
export const HASH_FILE = '.dist.hash'
|
|
3
|
+
|
|
4
|
+
// Read the recorded hash, or "" when nothing has been recorded yet.
|
|
5
|
+
export const hashRead = async (dir) => {
|
|
6
|
+
const file = Bun.file(dir + '/' + HASH_FILE)
|
|
7
|
+
return (await file.exists()) ? (await file.text()).trim() : ''
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Persist a hash so the next run can compare against it.
|
|
11
|
+
export const hashWrite = async (dir, hash) => {
|
|
12
|
+
await Bun.write(dir + '/' + HASH_FILE, hash.trim() + '\n')
|
|
13
|
+
}
|
package/hashOf.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import frameUpdate from './frameUpdate.js'
|
|
3
|
+
|
|
4
|
+
export const TAG = 0,
|
|
5
|
+
REL = 1,
|
|
6
|
+
BYTES = 2
|
|
7
|
+
|
|
8
|
+
// Hash an ordered [tag, rel_path, bytes] entry list with SHA-256.
|
|
9
|
+
// The entry order is part of the digest, so callers must sort path lists.
|
|
10
|
+
const hashOf = (entry_li) => {
|
|
11
|
+
const hasher = createHash('sha256')
|
|
12
|
+
for (const entry of entry_li) {
|
|
13
|
+
frameUpdate(hasher, entry[TAG], entry[REL], entry[BYTES])
|
|
14
|
+
}
|
|
15
|
+
return hasher.digest('hex')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default hashOf
|
package/package.json
CHANGED
|
@@ -1,6 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@i18n.site/hash_change",
|
|
3
|
-
"version": "0.0.0-stage",
|
|
4
|
-
"stub": true,
|
|
5
|
-
"description": "Temporary package placeholder for staged publishing"
|
|
6
|
-
}
|
|
1
|
+
{"name":"@i18n.site/hash_change","version":"0.1.2","description":"","keywords":[],"homepage":"https://atomgit.com/i18n-site/rs/tree/main/hash_change","license":"MulanPSL-2.0","author":"i18n-org@googlegroups.com","repository":{"type":"git","url":"git+https://atomgit.com/i18n-site/rs.git"},"files":["./*"],"type":"module","exports":{".":"./_.js","./*":"./*"}}
|
package/walkFile.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join, resolve } from 'node:path'
|
|
3
|
+
import walkRel from '@1-/walk/walkRel.js'
|
|
4
|
+
import { FILE } from '@1-/walk'
|
|
5
|
+
|
|
6
|
+
const BASE_SKIP = ['node_modules', '.git'],
|
|
7
|
+
cleanRel = (rel) => rel.replace(/^\.\//, '')
|
|
8
|
+
|
|
9
|
+
const skipTest = (skip_li) => {
|
|
10
|
+
const skip_set = new Set([...BASE_SKIP, ...skip_li])
|
|
11
|
+
return (rel_path) =>
|
|
12
|
+
rel_path.split('/').some((name) => skip_set.has(name) || name.startsWith('.'))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Collect sorted, deduplicated relative file paths under every dir in dir_li,
|
|
16
|
+
// plus the extra file_li entries that exist. Traversal uses @1-/walk.
|
|
17
|
+
const walkFile = async (root_dir, dir_li = [], file_li = [], skip_li = []) => {
|
|
18
|
+
const root = resolve(root_dir),
|
|
19
|
+
skip = skipTest(skip_li),
|
|
20
|
+
rel_li = []
|
|
21
|
+
|
|
22
|
+
await Promise.all(
|
|
23
|
+
dir_li.map((raw_dir) => {
|
|
24
|
+
const dir = cleanRel(raw_dir),
|
|
25
|
+
abs_dir = join(root, dir)
|
|
26
|
+
if (!existsSync(abs_dir)) return
|
|
27
|
+
return walkRel(abs_dir, (kind, rel) => {
|
|
28
|
+
if (kind === FILE) rel_li.push(dir + '/' + rel)
|
|
29
|
+
if (skip(rel)) return false
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
for (const rel of file_li) {
|
|
35
|
+
const rel_path = cleanRel(rel)
|
|
36
|
+
if (!skip(rel_path) && existsSync(join(root, rel_path))) rel_li.push(rel_path)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return [...new Set(rel_li)].sort()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default walkFile
|