Skip to content

Commit

Permalink
Merge branch 'main' into met-799-clear-out-cached-typegraphs-on-boot
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am authored Jan 11, 2025
2 parents 8d2e2a4 + 0f2c8fa commit 2aaefed
Show file tree
Hide file tree
Showing 21 changed files with 770 additions and 944 deletions.
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 64 additions & 58 deletions src/typegate/src/runtimes/deno/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type {
} from "../../typegraph/types.ts";
import * as ast from "graphql/ast";
import { InternalAuth } from "../../services/auth/protocols/internal.ts";
import { DenoMessenger } from "./deno_messenger.ts";
import type { Task } from "./shared_types.ts";
import { path } from "compress/deps.ts";
import { globalConfig as config } from "../../config.ts";
Expand All @@ -27,6 +26,7 @@ import { PolicyResolverOutput } from "../../engine/planner/policies.ts";
import { getInjectionValues } from "../../engine/planner/injection_utils.ts";
import DynamicInjection from "../../engine/injection/dynamic.ts";
import { getLogger } from "../../log.ts";
import { WorkerManager } from "./worker_manager.ts";

const logger = getLogger(import.meta);

Expand All @@ -37,7 +37,8 @@ const predefinedFuncs: Record<string, Resolver<Record<string, unknown>>> = {
allow: () => "ALLOW" as PolicyResolverOutput,
deny: () => "DENY" as PolicyResolverOutput,
pass: () => "PASS" as PolicyResolverOutput,
internal_policy: ({ _: { context } }) => context.provider === "internal" ? "ALLOW" : "PASS" as PolicyResolverOutput,
internal_policy: ({ _: { context } }) =>
context.provider === "internal" ? "ALLOW" : "PASS" as PolicyResolverOutput,
};

export class DenoRuntime extends Runtime {
Expand All @@ -46,8 +47,7 @@ export class DenoRuntime extends Runtime {
uuid: string,
private tg: TypeGraphDS,
private typegate: Typegate,
private w: DenoMessenger,
private registry: Map<string, number>,
private workerManager: WorkerManager,
private secrets: Record<string, string>,
) {
super(typegraphName, uuid);
Expand Down Expand Up @@ -138,36 +138,24 @@ export class DenoRuntime extends Runtime {
}
}

const w = new DenoMessenger(
name,
{
...(args.permissions ?? {}),
read: [basePath],
} as Deno.PermissionOptionsObject,
false,
ops,
typegate.config.base,
);

if (Deno.env.get("DENO_TESTING") === "true") {
w.disableLazyness();
}
const workerManager = new WorkerManager({
timeout_ms: typegate.config.base.timer_max_timeout_ms,
});

const rt = new DenoRuntime(
typegraphName,
uuid,
tg,
typegate,
w,
registry,
workerManager,
secrets,
);

return rt;
}

async deinit(): Promise<void> {
await this.w.terminate();
// await this.workerManager.deinit();
}

materialize(
Expand Down Expand Up @@ -257,7 +245,10 @@ export class DenoRuntime extends Runtime {
const modMat = this.tg.materializers[mat.data.mod as number];
const entryPoint =
this.tg.meta.artifacts[modMat.data.entryPoint as string];
const op = this.registry.get(entryPoint.hash)!;
const depMetas = (modMat.data.deps as string[]).map((dep) =>
createArtifactMeta(this.typegraphName, this.tg.meta.artifacts[dep])
);
const moduleMeta = createArtifactMeta(this.typegraphName, entryPoint);

return async ({
_: {
Expand All @@ -269,33 +260,33 @@ export class DenoRuntime extends Runtime {
}) => {
const token = await InternalAuth.emit(this.typegate.cryptoKeys);

return await this.w.execute(
op,
// TODO cache??
const entryModulePath = await this.typegate.artifactStore.getLocalPath(
moduleMeta,
depMetas,
);

return await this.workerManager.callFunction(
mat.data.name as string,
entryModulePath,
entryPoint.path,
args,
{
type: "import_func",
args,
internals: {
parent,
context,
secrets,
effect: mat.effect.effect ?? null,
meta: {
url: `${url.protocol}//${url.host}/${this.typegraphName}`,
token,
},
headers,
parent,
context,
secrets,
effect: mat.effect.effect ?? null,
meta: {
url: `${url.protocol}//${url.host}/${this.typegraphName}`,
token,
},
name: mat.data.name as string,
verbose,
headers,
},
[],
pulseCount,
);
};
}

if (mat.name === "function") {
const op = this.registry.get(mat.data.script as string)!;
return async ({
_: {
context,
Expand All @@ -306,26 +297,29 @@ export class DenoRuntime extends Runtime {
}) => {
const token = await InternalAuth.emit(this.typegate.cryptoKeys);

return await this.w.execute(
op,
const modulePath = await this.typegate.artifactStore.getInlineArtifact(
this.typegraphName,
mat.data.script as string,
".ts",
exportInlineFunction("inlineFunction"),
);

return await this.workerManager.callFunction(
"inlineFunction",
modulePath,
"tg",
args,
{
type: "func",
args,
internals: {
parent,
context,
secrets,
effect: mat.effect.effect ?? null,
meta: {
url: `${url.protocol}//${url.host}/${this.typegraphName}`,
token,
},
headers,
parent,
context,
secrets,
effect: mat.effect.effect ?? null,
meta: {
url: `${url.protocol}//${url.host}/${this.typegraphName}`,
token,
},
verbose,
headers,
},
[],
pulseCount,
);
};
}
Expand Down Expand Up @@ -365,6 +359,18 @@ export class DenoRuntime extends Runtime {
}
}

function exportInlineFunction(name = "fn", symbol = "_my_lambda") {
if (!name.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
throw new Error(`Invalid identifier: ${name}`);
}
if (!symbol.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
throw new Error(`Invalid identifier: ${symbol}`);
}
return (code: string) => {
return `${code}\nexport const ${name} = ${symbol};`;
};
}

function getInjectionData(d: InjectionData) {
if ("value" in d) {
return d.value;
Expand Down
74 changes: 0 additions & 74 deletions src/typegate/src/runtimes/deno/deno_messenger.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/typegate/src/runtimes/deno/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

import { TaskContext } from "./shared_types.ts";

export type TaskSpec = {
modulePath: string;
functionName: string;
};

export type DenoMessage = {
type: "CALL";
modulePath: string;
functionName: string;
args: unknown;
internals: TaskContext;
};

export type DenoEvent =
| { type: "SUCCESS"; result: unknown }
| { type: "FAILURE"; error: string; exception: Error | undefined };
Loading

0 comments on commit 2aaefed

Please sign in to comment.