Skip to content

Commit 3b3a997

Browse files
committed
feat(gate): resolve deno runtime artifacts
1 parent 30188dd commit 3b3a997

File tree

3 files changed

+269
-234
lines changed

3 files changed

+269
-234
lines changed

typegate/src/runtimes/deno/deno.ts

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
22
// SPDX-License-Identifier: Elastic-2.0
33

4-
import { toFileUrl } from "std/path/mod.ts";
54
import { ComputeStage } from "../../engine/query_engine.ts";
65
import { TypeGraphDS, TypeMaterializer } from "../../typegraph/mod.ts";
76
import { Runtime } from "../Runtime.ts";
87
import { Resolver, RuntimeInitParams } from "../../types.ts";
9-
import { DenoRuntimeData } from "../../typegraph/types.ts";
8+
import { Artifact, DenoRuntimeData } from "../../typegraph/types.ts";
109
import * as ast from "graphql/ast";
1110
import { InternalAuth } from "../../services/auth/protocols/internal.ts";
1211
import { DenoMessenger } from "./deno_messenger.ts";
1312
import { Task } from "./shared_types.ts";
14-
import { structureRepr, uncompress } from "../../utils.ts";
1513
import { path } from "compress/deps.ts";
1614
import config from "../../config.ts";
1715
import { getLogger } from "../../log.ts";
@@ -39,8 +37,14 @@ export class DenoRuntime extends Runtime {
3937
static async init(
4038
params: RuntimeInitParams,
4139
): Promise<Runtime> {
42-
const { typegraph: tg, typegraphName, args, materializers, secretManager } =
43-
params as RuntimeInitParams<DenoRuntimeData>;
40+
const {
41+
typegraph: tg,
42+
typegraphName,
43+
args,
44+
materializers,
45+
secretManager,
46+
typegate,
47+
} = params as RuntimeInitParams<DenoRuntimeData>;
4448

4549
const { worker: name } = args as unknown as DenoRuntimeData;
4650
if (name == null) {
@@ -56,6 +60,7 @@ export class DenoRuntime extends Runtime {
5660
}
5761
}
5862

63+
// maps from the module code to the op number/id
5964
const registry = new Map<string, number>();
6065
const ops = new Map<number, Task>();
6166

@@ -64,21 +69,9 @@ export class DenoRuntime extends Runtime {
6469
// => (gate) tmp/scripts/{tgname}/deno/*
6570
const basePath = path.join(
6671
config.tmp_dir,
67-
"scripts",
68-
typegraphName,
69-
uuid,
70-
"deno",
71-
name.replaceAll(" ", "_"), // TODO: improve sanitization
72+
"artifacts", // TODO: improve sanitization
7273
);
7374

74-
try {
75-
// clean up old files
76-
// logger.debug(`removes files at ${basePath}`);
77-
await Deno.remove(basePath, { recursive: true });
78-
} catch {
79-
// ignore non-existent files
80-
}
81-
8275
let registryCount = 0;
8376
for (const mat of materializers) {
8477
if (mat.name === "function") {
@@ -92,29 +85,50 @@ export class DenoRuntime extends Runtime {
9285
registry.set(code, registryCount);
9386
registryCount += 1;
9487
} else if (mat.name === "module") {
95-
const code = mat.data.code as string;
88+
const matData = mat.data;
89+
const denoArtifact = matData.denoArtifact as Artifact;
90+
const depArtifacts = matData.depsMeta as Artifact[];
91+
92+
const moduleMeta = {
93+
typegraphName: typegraphName,
94+
relativePath: denoArtifact.path,
95+
hash: denoArtifact.hash,
96+
sizeInBytes: denoArtifact.size,
97+
};
9698

97-
const repr = await structureRepr(code);
98-
const outDir = path.join(basePath, repr.hashes.entryPoint);
99-
const entries = await uncompress(
100-
outDir,
101-
repr.base64,
99+
const depMetas = depArtifacts.map(
100+
(dep) => {
101+
return {
102+
typegraphName: typegraphName,
103+
relativePath: dep.path,
104+
hash: dep.hash,
105+
sizeInBytes: dep.size,
106+
};
107+
},
102108
);
103109

104-
logger.info(`uncompressed ${entries.join(", ")} at ${outDir}`);
110+
// Note:
111+
// Worker destruction seems to have no effect on the import cache? (deinit() => stop(worker))
112+
// hence the use of contentHash
113+
const entryModulePath = await typegate.artifactStore.getLocalPath(
114+
moduleMeta,
115+
depMetas,
116+
);
117+
118+
logger.info(`Resloved runtimes artifacts at ${basePath}`);
105119

106120
// Note:
107121
// Worker destruction seems to have no effect on the import cache? (deinit() => stop(worker))
108122
// hence the use of contentHash
109123
ops.set(registryCount, {
110124
type: "register_import_func",
111-
modulePath: toFileUrl(
112-
path.resolve(outDir, repr.entryPoint),
113-
).toString() + `?hash=${repr.hashes.content}`,
125+
modulePath: entryModulePath,
114126
op: registryCount,
115127
verbose: config.debug,
116128
});
117-
registry.set(code, registryCount);
129+
130+
// TODO: can a single aritfact be used by multiple materializers?
131+
registry.set(denoArtifact.hash, registryCount);
118132
registryCount += 1;
119133
}
120134
}
@@ -216,7 +230,9 @@ export class DenoRuntime extends Runtime {
216230

217231
if (mat.name === "import_function") {
218232
const modMat = this.tg.materializers[mat.data.mod as number];
219-
const op = this.registry.get(modMat.data.code as string)!;
233+
const denoAritfact = modMat.data.denoArtifact as Artifact;
234+
const op = this.registry.get(denoAritfact.hash)!;
235+
220236
return async (
221237
{ _: { context, parent, info: { url, headers } }, ...args },
222238
) => {

typegate/tests/runtimes/deno/deno_dep.ts

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

7-
export const tg = await typegraph({
8-
name: "test-deno-dep",
7+
export const denoDepTg = await typegraph({
8+
name: "test_deno_dep",
99
}, (g: any) => {
10-
const deno = DenoRuntime();
10+
const deno = new DenoRuntime();
1111
const pub = Policy.public();
1212

1313
g.expose({
@@ -17,6 +17,7 @@ export const tg = await typegraph({
1717
{
1818
module: "ts/dep/main.ts",
1919
name: "doAddition",
20+
deps: ["ts/dep/nested/dep.ts"],
2021
},
2122
).withPolicy(pub),
2223
});

0 commit comments

Comments
 (0)