@boxxy/content-hash 0.0.0-stage → 0.0.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 +38 -2
- package/dist/content-hash-schema.d.ts +3 -0
- package/dist/content-hash.d.ts +3 -0
- package/dist/format-canonical-json.d.ts +2 -0
- package/dist/hash-bytes.d.ts +2 -0
- package/dist/hash-tree.d.ts +7 -0
- package/dist/index.d.ts +6 -0
- package/dist/json-value.d.ts +3 -0
- package/package.json +37 -4
- package/src/content-hash-schema.ts +8 -0
- package/src/content-hash.ts +8 -0
- package/src/format-canonical-json.ts +59 -0
- package/src/hash-bytes.ts +7 -0
- package/src/hash-tree.ts +27 -0
- package/src/index.ts +6 -0
- package/src/json-value.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the boxxy authors
|
|
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,39 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>@boxxy/content-hash</h1>
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
<p>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@boxxy/content-hash"><img src="https://img.shields.io/npm/v/@boxxy/content-hash" alt="npm version"></a>
|
|
6
|
+
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license"></a>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p>
|
|
10
|
+
<a href="../../docs/design/boxxy/README.md">Documentation</a>
|
|
11
|
+
</p>
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
Canonical JSON and the SHA-256 content hashes boxxy records.
|
|
15
|
+
|
|
16
|
+
boxxy stores a content hash beside each recipe, rules version and snapshot. A value hashes the same
|
|
17
|
+
whatever the order of its object keys.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
Install the package from [npm](https://www.npmjs.com/package/@boxxy/content-hash) with Bun:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
bun add @boxxy/content-hash
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The package runs on [Bun](https://bun.sh) 1.3 or newer.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
- `formatCanonicalJson`, which writes a JSON value in the canonical form of
|
|
32
|
+
[RFC 8785](https://www.rfc-editor.org/rfc/rfc8785).
|
|
33
|
+
- `contentHash`, the SHA-256 hex of a value's canonical JSON, as a branded `ContentHash`.
|
|
34
|
+
- `hashBytes`, the SHA-256 hex of a file's bytes.
|
|
35
|
+
- `hashTree`, the content hash of a folder's files, whatever order the caller walks them in.
|
|
36
|
+
|
|
37
|
+
## Documentation
|
|
38
|
+
|
|
39
|
+
The [first build spec](../../docs/design/boxxy/slice-1.md#packages) lists the hashes boxxy records.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { contentHash } from './content-hash.ts';
|
|
2
|
+
export { type ContentHash, contentHashSchema } from './content-hash-schema.ts';
|
|
3
|
+
export { formatCanonicalJson } from './format-canonical-json.ts';
|
|
4
|
+
export { hashBytes } from './hash-bytes.ts';
|
|
5
|
+
export { hashTree, type TreeFile } from './hash-tree.ts';
|
|
6
|
+
export type { JsonValue } from './json-value.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boxxy/content-hash",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Canonical JSON and the SHA-256 content hashes boxxy records",
|
|
5
|
+
"homepage": "https://github.com/zgeoff/boxxy/tree/main/libs/content-hash#readme",
|
|
6
|
+
"bugs": "https://github.com/zgeoff/boxxy/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zgeoff/boxxy.git",
|
|
11
|
+
"directory": "libs/content-hash"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/*.test.ts",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./src/index.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "rm -rf dist && tsrs -p tsconfig.build.json",
|
|
30
|
+
"typecheck": "tsrs --noEmit"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"zod": "4.6.7"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"bun": ">=1.3",
|
|
37
|
+
"node": ">=24"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ContentHash } from './content-hash-schema.ts';
|
|
2
|
+
import { formatCanonicalJson } from './format-canonical-json.ts';
|
|
3
|
+
import { hashBytes } from './hash-bytes.ts';
|
|
4
|
+
import type { JsonValue } from './json-value.ts';
|
|
5
|
+
|
|
6
|
+
export function contentHash(value: JsonValue): ContentHash {
|
|
7
|
+
return hashBytes(new TextEncoder().encode(formatCanonicalJson(value)));
|
|
8
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { JsonValue } from './json-value.ts';
|
|
2
|
+
|
|
3
|
+
// ts-pattern's match() fails the type check on the recursive JsonValue type ("instantiations of
|
|
4
|
+
// type 'InvertPatternInternal' appear infinitely circular"), so this branches with typeof
|
|
5
|
+
export function formatCanonicalJson(value: JsonValue): string {
|
|
6
|
+
if (typeof value === 'number') {
|
|
7
|
+
return formatCanonicalNumber(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
return formatCanonicalString(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// RFC 8785 writes the literals as ECMAScript's JSON.stringify does
|
|
15
|
+
if (value === null || typeof value === 'boolean') {
|
|
16
|
+
return JSON.stringify(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (isJsonList(value)) {
|
|
20
|
+
return formatCanonicalList(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const members = Object.entries(value)
|
|
24
|
+
.toSorted(([left], [right]) => (left < right ? -1 : 1))
|
|
25
|
+
.map(([key, member]) => `${formatCanonicalString(key)}:${formatCanonicalJson(member)}`);
|
|
26
|
+
|
|
27
|
+
return `{${members.join(',')}}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function formatCanonicalNumber(number: number): string {
|
|
31
|
+
if (!Number.isFinite(number)) {
|
|
32
|
+
throw new TypeError(`RFC 8785 has no form for the number ${number}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// RFC 8785 writes a number as ECMAScript's Number serialization, which JSON.stringify uses
|
|
36
|
+
return JSON.stringify(number);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function formatCanonicalString(text: string): string {
|
|
40
|
+
// JSON.stringify escapes a lone surrogate, but RFC 8785 rejects a string that holds one
|
|
41
|
+
if (!text.isWellFormed()) {
|
|
42
|
+
throw new TypeError('RFC 8785 has no form for a string with a lone UTF-16 surrogate');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return JSON.stringify(text);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isJsonList(value: JsonValue): value is readonly JsonValue[] {
|
|
49
|
+
return Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function formatCanonicalList(list: readonly JsonValue[]): string {
|
|
53
|
+
// map() skips the holes of a sparse array, which would write `[,]` or hash like a shorter list
|
|
54
|
+
if (Object.keys(list).length !== list.length) {
|
|
55
|
+
throw new TypeError('RFC 8785 has no form for a sparse array');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return `[${list.map((item) => formatCanonicalJson(item)).join(',')}]`;
|
|
59
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type { ContentHash } from './content-hash-schema.ts';
|
|
3
|
+
import { contentHashSchema } from './content-hash-schema.ts';
|
|
4
|
+
|
|
5
|
+
export function hashBytes(bytes: Uint8Array): ContentHash {
|
|
6
|
+
return contentHashSchema.parse(createHash('sha256').update(bytes).digest('hex'));
|
|
7
|
+
}
|
package/src/hash-tree.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ContentHash } from './content-hash-schema.ts';
|
|
2
|
+
import { contentHash } from './content-hash.ts';
|
|
3
|
+
import { hashBytes } from './hash-bytes.ts';
|
|
4
|
+
|
|
5
|
+
export interface TreeFile {
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly bytes: Uint8Array;
|
|
8
|
+
readonly executable: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function hashTree(files: readonly TreeFile[]): ContentHash {
|
|
12
|
+
const entries = files
|
|
13
|
+
.map((file) => ({
|
|
14
|
+
path: file.path,
|
|
15
|
+
sha256: hashBytes(file.bytes),
|
|
16
|
+
executable: file.executable,
|
|
17
|
+
}))
|
|
18
|
+
.toSorted((left, right) => compareUTF8(left.path, right.path));
|
|
19
|
+
|
|
20
|
+
return contentHash(entries);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function compareUTF8(left: string, right: string): number {
|
|
24
|
+
const encoder = new TextEncoder();
|
|
25
|
+
|
|
26
|
+
return Buffer.compare(encoder.encode(left), encoder.encode(right));
|
|
27
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { contentHash } from './content-hash.ts';
|
|
2
|
+
export { type ContentHash, contentHashSchema } from './content-hash-schema.ts';
|
|
3
|
+
export { formatCanonicalJson } from './format-canonical-json.ts';
|
|
4
|
+
export { hashBytes } from './hash-bytes.ts';
|
|
5
|
+
export { hashTree, type TreeFile } from './hash-tree.ts';
|
|
6
|
+
export type { JsonValue } from './json-value.ts';
|