Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

add support for kv host function #73

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/internal/kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { KVAction } from "@streamdal/protos/protos/shared/sp_shared";
import { KVCommand } from "@streamdal/protos/protos/sp_command";
import { KVInstruction } from "@streamdal/protos/protos/sp_kv";

import { internal } from "./register.js";

export const kvInstruction = (instruction: KVInstruction) => {
switch (instruction.action) {
case KVAction.KV_ACTION_DELETE_ALL: {
internal.kv.clear();
break;
}
case KVAction.KV_ACTION_DELETE: {
instruction.object?.key && internal.kv.delete(instruction.object.key);
break;
}
case KVAction.KV_ACTION_EXISTS: {
return instruction.object?.key && internal.kv.has(instruction.object.key);
}
case KVAction.KV_ACTION_GET: {
return instruction.object?.key && internal.kv.get(instruction.object.key);
}
case KVAction.KV_ACTION_UPDATE:
case KVAction.KV_ACTION_CREATE: {
instruction.object?.key &&
internal.kv.set(instruction.object.key, instruction.object.value);
break;
}
}
};

export const kvCommand = (command: KVCommand) => {
for (const instruction of command.instructions) {
kvInstruction(instruction);
}
};

export const kvExists = (
memory: WebAssembly.Memory,
pointer: number,
length: number
): boolean => {
const bytes = new Uint8Array(memory.buffer, pointer, length);
const key = new TextDecoder().decode(bytes);
return internal.kv.has(key);
};
6 changes: 6 additions & 0 deletions src/internal/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GetAttachCommandsByServiceResponse } from "@streamdal/protos/protos/sp_
import { Pipeline, PipelineStep } from "@streamdal/protos/protos/sp_pipeline";

import { Configs } from "../streamdal.js";
import { kvCommand } from "./kv.js";
import { audienceKey, internal, TailStatus } from "./register.js";
import { instantiateWasm } from "./wasm.js";

Expand Down Expand Up @@ -40,6 +41,11 @@ export const initPipelines = async (configs: Configs) => {
};

export const processResponse = async (response: Command) => {
if (response.command.oneofKind === "kv") {
kvCommand(response.command.kv);
return;
}

if (!response.audience) {
response.command.oneofKind !== "keepAlive" &&
console.debug("command response has no audience, ignoring");
Expand Down
1 change: 1 addition & 0 deletions src/internal/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const internal = {
registered: false,
pipelineInitialized: false,
pipelines: new Map<string, Map<string, InternalPipeline>>(),
kv: new Map<string, Uint8Array>(),
audiences: new Map<
string,
{ audience: Audience; tails: Map<string, TailStatus> }
Expand Down
22 changes: 20 additions & 2 deletions src/internal/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
// eslint-disable-next-line import/no-unresolved
import { WASI } from "wasi";

import { kvExists } from "./kv.js";
import { MAX_PAYLOAD_SIZE } from "./process.js";
import { internal } from "./register.js";

Expand Down Expand Up @@ -34,8 +35,25 @@ export const instantiateWasm = async (
}

const wasm = await WebAssembly.compile(wasmBytes);
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const instantiated = await WebAssembly.instantiate(wasm, importObject);
const instantiated = await WebAssembly.instantiate(wasm, {
wasi_snapshot_preview1: wasi.wasiImport,
env: {
kvExists: (pointer: number, length: number): number => {
const result = instantiated.exports.memory
? kvExists(
instantiated.exports.memory as WebAssembly.Memory,
pointer,
length
)
: false;
//
// Wasm function expects a bigint but our runtime type expects a number
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return BigInt(result ? 1 : 0);
},
},
});
internal.wasmModules.set(wasmId, instantiated);
};

Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
import { Audience } from "@streamdal/protos/protos/sp_common";

import { OperationType, Streamdal, StreamdalConfigs } from "../streamdal.js";
import { QUIET, runPipeline } from "./index.js";
import { runPipeline } from "./index.js";

const serviceBillingConfig: StreamdalConfigs = {
streamdalUrl: "localhost:8082",
Expand Down
Loading