Repository navigation
Expand file tree
/
Copy pathsource-inventory.ts
More file actions
130 lines (123 loc) · 3.85 KB
/
Copy pathsource-inventory.ts
File metadata and controls
130 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Data, Effect } from "effect";
import { resolve } from "node:path";
export class SourceInventoryError extends Data.TaggedError(
"SourceInventoryError"
)<{
readonly message: string;
}> {}
export interface SourceInventoryEntry {
readonly sourcePath: string;
readonly bytes: number;
readonly sha256: string;
}
export interface SourceInventory {
readonly algorithm: "sha256-path-size-content-v1";
readonly sha256: string;
readonly fileCount: number;
readonly totalBytes: number;
readonly excludedDirectoryNames: ReadonlyArray<string>;
readonly entries: ReadonlyArray<SourceInventoryEntry>;
}
export interface SourceInventoryOptions {
/** Directory base names excluded at every depth. */
readonly excludedDirectoryNames?: ReadonlyArray<string>;
}
export const defaultExcludedDirectoryNames = [
".git",
"node_modules",
".cache",
"target",
] as const;
const fail = (cause: unknown) =>
new SourceInventoryError({
message: cause instanceof Error ? cause.message : String(cause),
});
/**
* Produce a deterministic content inventory without consulting Git metadata.
*
* The aggregate hashes each sorted relative path, byte length, and per-file SHA-256. VCS metadata
* and generated dependency/cache directories are excluded by name; all other regular files are
* conservatively treated as native build inputs.
*/
export const sourceInventory = Effect.fn("sourceInventory")(function* (
sourceDirectory: string,
options: SourceInventoryOptions = {}
) {
const excludedDirectoryNames = [
...(options.excludedDirectoryNames ?? defaultExcludedDirectoryNames),
];
if (
excludedDirectoryNames.length === 0 ||
new Set(excludedDirectoryNames).size !== excludedDirectoryNames.length ||
excludedDirectoryNames.some(
(name) =>
name.length === 0 ||
name === "." ||
name === ".." ||
name.includes("/") ||
name.includes("\\")
)
) {
return yield* new SourceInventoryError({
message:
"excludedDirectoryNames must contain unique, non-empty directory base names",
});
}
const excluded = new Set(excludedDirectoryNames);
const paths = yield* Effect.tryPromise({
try: async () => {
const collected: Array<string> = [];
const glob = new Bun.Glob("**/*");
for await (const path of glob.scan({
cwd: sourceDirectory,
dot: true,
followSymlinks: false,
onlyFiles: true,
})) {
const segments = path.split("/");
// The contract excludes directory base names, not a regular file that
// happens to share one of those names. This matches the Rust verifier.
if (segments.slice(0, -1).some((segment) => excluded.has(segment))) {
continue;
}
collected.push(path);
}
return collected.sort((left, right) =>
Buffer.compare(Buffer.from(left, "utf8"), Buffer.from(right, "utf8"))
);
},
catch: fail,
});
const entries = yield* Effect.forEach(
paths,
(sourcePath) =>
Effect.tryPromise({
try: async () => {
const file = Bun.file(resolve(sourceDirectory, sourcePath));
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(await file.arrayBuffer());
return {
sourcePath,
bytes: file.size,
sha256: hasher.digest("hex"),
} satisfies SourceInventoryEntry;
},
catch: fail,
}),
{ concurrency: 8 }
);
const aggregate = new Bun.CryptoHasher("sha256");
let totalBytes = 0;
for (const entry of entries) {
aggregate.update(`${entry.sourcePath}\0${entry.bytes}\0${entry.sha256}\n`);
totalBytes += entry.bytes;
}
return {
algorithm: "sha256-path-size-content-v1",
sha256: aggregate.digest("hex"),
fileCount: entries.length,
totalBytes,
excludedDirectoryNames,
entries,
} satisfies SourceInventory;
});