Skip to content

Commit d035425

Browse files
committed
chore(sdk, test): change file hash impl and modify wasmedge test
1 parent e635571 commit d035425

File tree

5 files changed

+81
-50
lines changed

5 files changed

+81
-50
lines changed

typegate/tests/runtimes/wasmedge/wasmedge.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
55
import { WasmEdgeRuntime } from "@typegraph/sdk/runtimes/wasmedge.js";
66

7-
typegraph("wasmedge_ts", async (g: any) => {
7+
export const tg = await typegraph("wasmedge_ts", async (g: any) => {
88
const pub = Policy.public();
99
const wasmedge = new WasmEdgeRuntime();
1010

@@ -18,7 +18,7 @@ typegraph("wasmedge_ts", async (g: any) => {
1818
t.integer(),
1919
{
2020
func: "add",
21-
wasm: "artifacts/rust.wasm",
21+
wasm: "typegate/tests/artifacts/rust.wasm",
2222
},
2323
);
2424
g.expose({
Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
22
// SPDX-License-Identifier: Elastic-2.0
33

4+
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";
45
import { gql, Meta } from "../../utils/mod.ts";
6+
import { testDir } from "test-utils/dir.ts";
7+
import { tg } from "./wasmedge.ts";
58

6-
Meta.test("WasmEdge runtime", async (t) => {
7-
const e = await t.engine("runtimes/wasmedge/wasmedge.py", { deploy: true });
9+
const port = 7698;
10+
const gate = `http://localhost:${port}`;
11+
const cwdDir = testDir;
12+
const auth = new BasicAuth("admin", "password");
813

9-
await t.should("works", async () => {
10-
await gql`
11-
query {
12-
test(a: 1, b: 2)
13-
}
14-
`
15-
.expectData({
16-
test: 3,
17-
})
18-
.on(e);
19-
});
20-
});
14+
// Meta.test("WasmEdge runtime", async (t) => {
15+
// const e = await t.engine("runtimes/wasmedge/wasmedge.py", {}, { port });
16+
17+
// await t.should("works", async () => {
18+
// await gql`
19+
// query {
20+
// test(a: 1, b: 2)
21+
// }
22+
// `
23+
// .expectData({
24+
// test: 3,
25+
// })
26+
// .on(e);
27+
// });
28+
// }, { port: port });
2129

2230
Meta.test("WasmEdge Runtime typescript sdk", async (metaTest) => {
23-
const engine = await metaTest.engine("runtimes/wasmedge/wasmedge.ts", {
24-
deploy: true,
31+
const { serialized, typegate: _gateResponseAdd } = await tgDeploy(tg, {
32+
baseUrl: gate,
33+
auth,
34+
artifactsConfig: {
35+
prismaMigration: {
36+
globalAction: {
37+
create: true,
38+
reset: false,
39+
},
40+
migrationDir: "prisma-migrations",
41+
},
42+
dir: cwdDir,
43+
},
2544
});
2645

27-
await metaTest.should("work", async () => {
46+
const engine = await metaTest.engineFromDeployed(serialized);
47+
48+
await metaTest.should("work after deploying artifact", async () => {
2849
await gql`
2950
query {
3051
test_wasi_ts(a: 11, b: 2)
@@ -35,4 +56,5 @@ Meta.test("WasmEdge Runtime typescript sdk", async (metaTest) => {
3556
})
3657
.on(engine);
3758
});
38-
});
59+
await engine.terminate();
60+
}, { port: port, systemTypegraphs: true });

typegate/tests/utils/test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ export class MetaTest {
142142
return engine;
143143
}
144144

145+
async engineFromDeployed(tgString: string): Promise<QueryEngine> {
146+
const tg = await TypeGraph.parseJson(tgString);
147+
const { engine, response } = await this.typegate.pushTypegraph(
148+
tg,
149+
{},
150+
this.introspection,
151+
);
152+
153+
if (engine == null) {
154+
throw response.failure!;
155+
}
156+
157+
return engine;
158+
}
159+
145160
async unregister(engine: QueryEngine) {
146161
await Promise.all(
147162
this.register

typegraph/node/sdk/src/tg_deploy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export async function tgDeploy(
107107
body: byteArray,
108108
});
109109

110+
const _ = await artifactUploadResponse.json();
110111
if (!artifactUploadResponse.ok) {
111112
throw new Error(
112113
`Failed to upload artifact ${filePath} to typegate: ${artifactUploadResponse.status} ${artifactUploadResponse.statusText}`,

typegraph/node/sdk/src/utils/file_utils.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,37 @@ import * as fs from "node:fs";
55
import * as crypto from "node:crypto";
66
import { wit_utils } from "../wit.js";
77

8-
export function getFileHash(filePath: string): Promise<string> {
9-
return new Promise((resolve, reject) => {
10-
const currDir = wit_utils.getCwd();
11-
filePath = `${currDir}/${filePath}`;
12-
const hash = crypto.createHash("sha256");
13-
const fileDescriptor = fs.openSync(filePath, "r");
8+
export async function getFileHash(filePath: string): Promise<string> {
9+
const cwd = wit_utils.getCwd();
10+
filePath = `${cwd}/${filePath}`;
11+
const hash = crypto.createHash("sha256");
12+
let file;
13+
14+
try {
15+
await fs.promises.access(filePath, fs.constants.F_OK);
16+
file = await fs.promises.open(filePath, "r");
1417

1518
const CHUNK_SIZE = 4096;
1619
let buffer = new Uint8Array(CHUNK_SIZE);
1720
let bytesRead = 0;
18-
19-
function readChunk() {
20-
fs.read(
21-
fileDescriptor,
21+
let numBytesRead = 0;
22+
do {
23+
const { bytesRead: numBytesRead } = await file.read(
2224
buffer,
2325
0,
2426
CHUNK_SIZE,
2527
bytesRead,
26-
(err, numBytesRead) => {
27-
if (err) {
28-
fs.closeSync(fileDescriptor);
29-
reject(err);
30-
return;
31-
}
32-
33-
if (numBytesRead === 0) {
34-
fs.closeSync(fileDescriptor);
35-
resolve(hash.digest("hex"));
36-
return;
37-
}
38-
39-
bytesRead += numBytesRead;
40-
hash.update(buffer.subarray(0, numBytesRead));
41-
readChunk();
42-
},
4328
);
44-
}
29+
bytesRead += numBytesRead;
30+
hash.update(buffer.subarray(0, numBytesRead));
31+
} while (numBytesRead > 0);
4532

46-
readChunk();
47-
});
33+
return hash.digest("hex");
34+
} catch (err) {
35+
throw new Error(`Failed to calculate hash for ${filePath}: \n${err}`);
36+
} finally {
37+
if (file !== undefined) {
38+
await file.close(); // Ensure file closure
39+
}
40+
}
4841
}

0 commit comments

Comments
 (0)