-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-module-hash.ts
132 lines (107 loc) · 3.81 KB
/
create-module-hash.ts
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
131
132
import type { Module, Module_Input, Modules } from "../../proto.js";
import { createHash } from "../../utils/create-hash.js";
import { getModuleOrThrow } from "../../utils/get-module.js";
import { toHex } from "../../utils/to-hex.js";
import { type ModuleGraph, createModuleGraph } from "../graph/create-module-graph.js";
export async function createModuleHashHex(modules: Modules, module: Module, graph?: ModuleGraph): Promise<string>;
export async function createModuleHashHex(modules: Modules, name: string, graph?: ModuleGraph): Promise<string>;
export async function createModuleHashHex(
modules: Modules,
moduleOrName: Module | string,
graph?: ModuleGraph,
): Promise<string>;
export async function createModuleHashHex(
modules: Modules,
moduleOrName: Module | string,
graph = createModuleGraph(modules.modules ?? []),
) {
return toHex(await createModuleHash(modules, moduleOrName, graph));
}
export async function createModuleHash(modules: Modules, module: Module, graph?: ModuleGraph): Promise<Uint8Array>;
export async function createModuleHash(modules: Modules, name: string, graph?: ModuleGraph): Promise<Uint8Array>;
export async function createModuleHash(
modules: Modules,
moduleOrName: Module | string,
graph?: ModuleGraph,
): Promise<Uint8Array>;
export async function createModuleHash(
modules: Modules,
moduleOrName: Module | string,
graph = createModuleGraph(modules.modules ?? []),
) {
const module = typeof moduleOrName === "string" ? getModuleOrThrow(modules.modules, moduleOrName) : moduleOrName;
const encoder = new TextEncoder();
const chunks: Uint8Array[] = [];
chunks.push(encoder.encode("initial_block"));
chunks.push(encodeInitialBlock(module));
chunks.push(encoder.encode("kind"));
chunks.push(encoder.encode(moduleKind(module)));
chunks.push(encoder.encode("binary"));
const binary = modules.binaries[module.binaryIndex];
if (binary !== undefined) {
chunks.push(encoder.encode(binary.type));
chunks.push(binary.content);
}
chunks.push(encoder.encode("inputs"));
for (const input of module.inputs) {
chunks.push(encoder.encode(inputType(input)));
chunks.push(encoder.encode(inputValue(input)));
}
chunks.push(encoder.encode("ancestors"));
for (const ancestor of graph.ancestorsOf(module)) {
chunks.push(await createModuleHash(modules, ancestor, graph));
}
chunks.push(encoder.encode("entrypoint"));
chunks.push(encoder.encode(module.binaryEntrypoint));
const length = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
const array = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
array.set(chunk, offset);
offset += chunk.length;
}
return createHash(array);
}
function encodeInitialBlock(module: Module) {
const array = new Uint8Array(8);
new DataView(array.buffer).setBigUint64(0, module.initialBlock, true);
return array;
}
function moduleKind(module: Module): string {
switch (module.kind.case) {
case "kindMap":
return "map";
case "kindStore":
return "store";
default:
throw new Error(`Invalid module ${module.kind.case}`);
}
}
function inputType(input: Module_Input): string {
switch (input.input.case) {
case "map":
return "map";
case "store":
return "store";
case "source":
return "source";
case "params":
return "params";
default:
throw new Error(`Invalid input ${input}`);
}
}
function inputValue(input: Module_Input): string {
switch (input.input.case) {
case "source":
return input.input.value.type;
case "params":
return input.input.value.value;
case "map":
return ""; // This is accounted for in the `ancestorsOf()` tree.
case "store":
return ""; // This is accounted for in the `ancestorsOf()` tree.
default:
throw new Error(`Invalid input ${input}`);
}
}