Skip to content

Commit 824cb6b

Browse files
committed
chore(sdk): add hash compute function and field to store it
1 parent 8ade4c2 commit 824cb6b

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

libs/common/src/typegraph/runtimes/wasmedge.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
1010
pub struct WasiMatData {
1111
pub wasm: String,
1212
pub func: String,
13+
pub artifact_hash: String,
1314
}
1415

1516
#[cfg_attr(feature = "codegen", derive(JsonSchema))]

typegraph/core/wit/typegraph.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ interface runtimes {
360360
record materializer-wasi {
361361
func-name: string,
362362
module: string,
363+
artifact-hash: string,
363364
}
364365

365366
register-wasmedge-runtime: func() -> result<runtime-id, error>

typegraph/node/sdk/src/runtimes/wasmedge.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { runtimes } from "../wit.js";
66
import { Effect } from "../gen/interfaces/metatype-typegraph-runtimes.js";
77
import { Materializer, Runtime } from "./mod.js";
88
import { fx } from "../index.js";
9+
import { getFileHash } from "../utils/file_utils.js";
910

1011
interface WasiMat extends Materializer {
1112
module: string;
@@ -30,6 +31,13 @@ export class WasmEdgeRuntime extends Runtime {
3031
effect?: Effect;
3132
},
3233
): t.Func<I, O, WasiMat> {
34+
let fileHash = "";
35+
getFileHash(wasm).then((hash) => {
36+
fileHash = hash;
37+
}).catch((err) => {
38+
console.error(err);
39+
});
40+
3341
const matId = runtimes.fromWasiModule(
3442
{
3543
runtime: this._id,
@@ -38,6 +46,7 @@ export class WasmEdgeRuntime extends Runtime {
3846
{
3947
module: `file:${wasm}`,
4048
funcName: func,
49+
artifactHash: fileHash,
4150
},
4251
);
4352

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
import * as fs from "fs";
5+
import * as crypto from "crypto";
6+
7+
export function getFileHash(filePath: string): Promise<string> {
8+
return new Promise((resolve, reject) => {
9+
const hash = crypto.createHash("sha256");
10+
const fileDescriptor = fs.openSync(filePath, "r");
11+
12+
const CHUNK_SIZE = 4096;
13+
let buffer = Buffer.alloc(CHUNK_SIZE);
14+
let bytesRead = 0;
15+
16+
function readChunk() {
17+
fs.read(
18+
fileDescriptor,
19+
buffer,
20+
0,
21+
CHUNK_SIZE,
22+
bytesRead,
23+
(err, bytesRead) => {
24+
if (err) {
25+
fs.closeSync(fileDescriptor);
26+
reject(err);
27+
return;
28+
}
29+
30+
if (bytesRead === 0) {
31+
fs.closeSync(fileDescriptor);
32+
resolve(hash.digest("hex"));
33+
return;
34+
}
35+
36+
hash.update(buffer.subarray(0, bytesRead));
37+
readChunk();
38+
},
39+
);
40+
}
41+
42+
readChunk();
43+
});
44+
}

typegraph/python/typegraph/runtimes/wasmedge.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from typegraph.gen.types import Err
1414
from typegraph.runtimes.base import Materializer, Runtime
15+
from typegraph.utils import get_file_hash
1516
from typegraph.wit import runtimes, store
1617

1718

@@ -29,12 +30,13 @@ def wasi(
2930
effect: Optional[Effect] = None,
3031
):
3132
effect = effect or EffectRead()
33+
file_hash = get_file_hash(wasm)
3234
wasm = f"file:{wasm}"
3335

3436
mat_id = runtimes.from_wasi_module(
3537
store,
3638
BaseMaterializer(runtime=self.id.value, effect=effect),
37-
MaterializerWasi(module=wasm, func_name=func),
39+
MaterializerWasi(module=wasm, func_name=func, artifact_hash=file_hash),
3840
)
3941

4042
if isinstance(mat_id, Err):

typegraph/python/typegraph/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
22
# SPDX-License-Identifier: MPL-2.0
33

4+
import hashlib
45
import json
6+
import os
7+
import sys
58
from functools import reduce
69
from typing import Dict, List, Union, Tuple, Optional, Any
710
from typegraph.injection import InheritDef
@@ -77,3 +80,17 @@ def build_reduce_data(node: Any, paths: List[ReducePath], curr_path: List[str]):
7780

7881
def unpack_tarb64(tar_b64: str, dest: str):
7982
return wit_utils.unpack_tarb64(store, tar_b64, dest)
83+
84+
85+
def get_file_hash(file_path: str) -> str:
86+
sha256_hasher = hashlib.sha256()
87+
88+
curr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
89+
file_dir = os.path.join(curr_dir, file_path)
90+
91+
with open(file_dir, "rb") as file:
92+
chunk = 0
93+
while chunk := file.read(4096):
94+
sha256_hasher.update(chunk)
95+
96+
return sha256_hasher.hexdigest()

0 commit comments

Comments
 (0)