From 6ca6764f638e068de32be4e86cca9e6ea23fd6db Mon Sep 17 00:00:00 2001 From: MasterPtato <23087326+MasterPtato@users.noreply.github.com> Date: Tue, 26 Nov 2024 02:20:15 +0000 Subject: [PATCH] fix: fix ts types for actor bindings (#1470) Fixes RVT-4214 ## Changes --- .../isolate-v8-runner/js/40_rivet_kv.js | 65 +-- .../client/isolate-v8-runner/src/ext/kv.rs | 23 +- sdks/actors-bridge/src/bridge/40_rivet_kv.ts | 435 ++++++++---------- sdks/actors-bridge/src/bridge/90_rivet_ns.ts | 2 +- .../actors-bridge/src/bridge/99_rivet_main.ts | 1 - sdks/actors-bridge/src/ext/core.d.ts | 4 +- sdks/actors-bridge/src/ext/ops.d.ts | 30 +- sdks/actors-bridge/src/ext/types.d.ts | 24 + sdks/actors-core/src/index.ts | 9 +- sdks/actors-core/src/types | 2 +- 10 files changed, 301 insertions(+), 294 deletions(-) create mode 100644 sdks/actors-bridge/src/ext/types.d.ts diff --git a/packages/infra/client/isolate-v8-runner/js/40_rivet_kv.js b/packages/infra/client/isolate-v8-runner/js/40_rivet_kv.js index d728d72236..0b580f0c6e 100644 --- a/packages/infra/client/isolate-v8-runner/js/40_rivet_kv.js +++ b/packages/infra/client/isolate-v8-runner/js/40_rivet_kv.js @@ -7,20 +7,22 @@ import { core } from "ext:core/mod.js"; /** * Retrieves a value from the key-value store. * - * @param {any|any[]} key - The key to retrieve the value for. + * @param {Key} key - The key to retrieve the value for. * @param {GetOptions} [options] - Options. - * @returns {Promise} The retrieved value, or undefined if the key does not exist. + * @returns {Promise} The retrieved value, or undefined if the key does not exist. */ async function get(key, options) { - let entry = (await op_rivet_kv_get(serializeKey(key))) ?? undefined; + let entry = await op_rivet_kv_get(serializeKey(key)); + if (entry == null) + return null; return deserializeValue(key, entry.value, options?.format); } /** * Retrieves a batch of key-value pairs. * - * @param {string[]} keys - A list of keys to retrieve. + * @param {Key[]} keys - A list of keys to retrieve. * @param {GetBatchOptions} [options] - Options. - * @returns {Promise>} The retrieved values. + * @returns {Promise>} The retrieved values. */ async function getBatch(keys, options) { let entries = await op_rivet_kv_get_batch(keys.map((x) => serializeKey(x))); @@ -35,7 +37,7 @@ async function getBatch(keys, options) { * Retrieves all key-value pairs in the KV store. * * @param {ListOptions} [options] - Options. - * @returns {Promise>} The retrieved values. + * @returns {Promise>} The retrieved values. */ async function list(options) { // Build query @@ -50,10 +52,7 @@ async function list(options) { throw new Error("must set options.end with options.start"); } query = { - rangeInclusive: [ - serializeListKey(options.start), - serializeKey(options.end), - ], + rangeInclusive: [serializeListKey(options.start), serializeKey(options.end)], }; } else if (options?.startAfter) { @@ -61,10 +60,7 @@ async function list(options) { throw new Error("must set options.end with options.startAfter"); } query = { - rangeExclusive: [ - serializeListKey(options.startAfter), - serializeKey(options.end), - ], + rangeExclusive: [serializeListKey(options.startAfter), serializeKey(options.end)], }; } else if (options?.end) { @@ -84,51 +80,56 @@ async function list(options) { /** * Stores a key-value pair in the key-value store. * - * @param {any|any[]} key - The key under which the value will be stored. - * @param {any|ArrayBuffer} value - The value to be stored, which will be serialized. + * @param {Key} key - The key under which the value will be stored. + * @param {Entry | ArrayBuffer} value - The value to be stored, which will be serialized. * @param {PutOptions} [options] - Options. * @returns {Promise} A promise that resolves when the operation is complete. */ -async function put(key, givenValue, options) { - validateType(givenValue, null, options?.format); +async function put(key, value, options) { + validateType(value, null, options?.format); let format = options?.format ?? "value"; - let value; + let serializedValue; if (format == "value") { - value = core.serialize(givenValue, { forStorage: true }); + value = core.serialize(value, { forStorage: true }); } else if (format == "arrayBuffer") { - value = new Uint8Array(givenValue); + if (value instanceof ArrayBuffer) + value = new Uint8Array(value); + else + throw new Error(`value must be of type \`ArrayBuffer\` if format is "arrayBuffer"`); } - await op_rivet_kv_put(serializeKey(key), value); + await op_rivet_kv_put(serializeKey(key), serializedValue); } /** * Asynchronously stores a batch of key-value pairs. * - * @param {Record} obj - An object containing key-value pairs to be stored. + * @param {Record} obj - An object containing key-value pairs to be stored. * @param {PutBatchOptions} [options] - Options. * @returns {Promise} A promise that resolves when the batch operation is complete. */ async function putBatch(obj, options) { let serializedObj = new Map(); let format = options?.format ?? "value"; - for (let key in obj) { - let givenValue = obj[key]; - validateType(givenValue, key, format); - let value; + for (let [key, value] of obj) { + validateType(value, key, format); + let serializedValue; if (format == "value") { - value = core.serialize(givenValue, { forStorage: true }); + value = core.serialize(serializedValue, { forStorage: true }); } else if (format == "arrayBuffer") { - value = new Uint8Array(givenValue); + if (value instanceof ArrayBuffer) + value = new Uint8Array(value); + else + throw new Error(`value in key "${key}" must be of type \`ArrayBuffer\` if format is "arrayBuffer"`); } - serializedObj.set(serializeKey(key), value); + serializedObj.set(serializeKey(key), serializedValue); } await op_rivet_kv_put_batch(serializedObj); } /** * Deletes a key-value pair from the key-value store. * - * @param {string} key - The key of the key-value pair to delete. + * @param {Key} key - The key of the key-value pair to delete. * @returns {Promise} A promise that resolves when the operation is complete. */ async function delete_(key) { @@ -172,7 +173,7 @@ function validateType(value, key, format = "value") { } function serializeKey(key) { if (key instanceof Array) { - return { jsInKey: [key.map((x) => core.serialize(x))] }; + return { jsInKey: key.map((x) => core.serialize(x)) }; } else { return { jsInKey: [core.serialize(key)] }; diff --git a/packages/infra/client/isolate-v8-runner/src/ext/kv.rs b/packages/infra/client/isolate-v8-runner/src/ext/kv.rs index c8df5f9801..a2bebcb87b 100644 --- a/packages/infra/client/isolate-v8-runner/src/ext/kv.rs +++ b/packages/infra/client/isolate-v8-runner/src/ext/kv.rs @@ -41,9 +41,9 @@ enum Key { impl From for Key { fn from(value: actor_kv::key::Key) -> Self { match value { + // actor_kv::key::Key::JsInKey(tuple) => Key::InKey(tuple), // TODO: Currently, JsBuffer cannot be serialized back to v8 as a buffer. We must convert it until // fixed. - // actor_kv::key::Key::JsInKey(tuple) => Key::InKey(tuple), actor_kv::key::Key::JsInKey(tuple) => Key::OutKey( tuple .into_iter() @@ -58,20 +58,37 @@ impl From for Key { } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] struct Entry { - metadata: actor_kv::Metadata, + metadata: Metadata, value: ToJsBuffer, } impl From for Entry { fn from(value: actor_kv::Entry) -> Self { Entry { - metadata: value.metadata, + metadata: value.metadata.into(), value: value.value.into(), } } } +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Metadata { + pub kv_version: ToJsBuffer, + pub create_ts: i64, +} + +impl From for Metadata { + fn from(value: actor_kv::Metadata) -> Self { + Metadata { + kv_version: value.kv_version.into(), + create_ts: value.create_ts, + } + } +} + #[op2(async)] #[serde] pub fn op_rivet_kv_get( diff --git a/sdks/actors-bridge/src/bridge/40_rivet_kv.ts b/sdks/actors-bridge/src/bridge/40_rivet_kv.ts index 3f46f6e666..baf2f54c77 100644 --- a/sdks/actors-bridge/src/bridge/40_rivet_kv.ts +++ b/sdks/actors-bridge/src/bridge/40_rivet_kv.ts @@ -1,229 +1,206 @@ import { - op_rivet_kv_delete, - op_rivet_kv_delete_all, - op_rivet_kv_delete_batch, - op_rivet_kv_get, - op_rivet_kv_get_batch, - op_rivet_kv_list, - op_rivet_kv_put, - op_rivet_kv_put_batch, + op_rivet_kv_delete, + op_rivet_kv_delete_all, + op_rivet_kv_delete_batch, + op_rivet_kv_get, + op_rivet_kv_get_batch, + op_rivet_kv_list, + op_rivet_kv_put, + op_rivet_kv_put_batch, } from "ext:core/ops"; +import type { InKey, OutKey, ListQuery } from "internal_types"; import { core } from "ext:core/mod.js"; +export type Key = any; +export type Entry = any; + /** * Options for the `get` function. */ export interface GetOptions { - format?: "value" | "arrayBuffer"; + format?: "value" | "arrayBuffer"; } /** * Retrieves a value from the key-value store. * - * @param {any|any[]} key - The key to retrieve the value for. + * @param {Key} key - The key to retrieve the value for. * @param {GetOptions} [options] - Options. - * @returns {Promise} The retrieved value, or undefined if the key does not exist. + * @returns {Promise} The retrieved value, or undefined if the key does not exist. */ -async function get( - key: any | any[], - options?: GetOptions, -): Promise { - let entry = (await op_rivet_kv_get(serializeKey(key))) ?? undefined; +async function get(key: Key, options?: GetOptions): Promise { + let entry = await op_rivet_kv_get(serializeKey(key)); + if (entry == null) return null; - return deserializeValue(key, entry.value, options?.format); + return deserializeValue(key, entry.value, options?.format); } /** * Options for the `getBatch` function. */ export interface GetBatchOptions { - format?: "value" | "arrayBuffer"; + format?: "value" | "arrayBuffer"; } /** * Retrieves a batch of key-value pairs. * - * @param {string[]} keys - A list of keys to retrieve. + * @param {Key[]} keys - A list of keys to retrieve. * @param {GetBatchOptions} [options] - Options. - * @returns {Promise>} The retrieved values. + * @returns {Promise>} The retrieved values. */ -async function getBatch( - keys: string[], - options?: GetBatchOptions, -): Promise> { - let entries = await op_rivet_kv_get_batch(keys.map((x) => serializeKey(x))); - - let deserializedValues = new Map(); - - for (let [key, entry] of entries) { - let jsKey = deserializeKey(key); - deserializedValues.set( - jsKey, - deserializeValue(jsKey, entry.value, options?.format), - ); - } - - return deserializedValues; +async function getBatch(keys: Key[], options?: GetBatchOptions): Promise> { + let entries = await op_rivet_kv_get_batch(keys.map((x) => serializeKey(x))); + + let deserializedValues = new Map(); + + for (let [key, entry] of entries) { + let jsKey = deserializeKey(key); + deserializedValues.set(jsKey, deserializeValue(jsKey, entry.value, options?.format)); + } + + return deserializedValues; } /** * Options for the `list` function. */ export interface ListOptions { - format?: "value" | "arrayBuffer"; - start?: string; - startAfter?: string; - end?: string; - prefix?: string; - reverse?: boolean; - limit?: number; + format?: "value" | "arrayBuffer"; + start?: Key; + startAfter?: Key; + end?: Key; + prefix?: Key; + reverse?: boolean; + limit?: number; } /** * Retrieves all key-value pairs in the KV store. * * @param {ListOptions} [options] - Options. - * @returns {Promise>} The retrieved values. + * @returns {Promise>} The retrieved values. */ -async function list(options?: ListOptions): Promise> { - // Build query - let query; - if (options?.prefix) { - query = { - prefix: serializeListKey(options.prefix), - }; - } else if (options?.start) { - if (!options.end) { - throw new Error("must set options.end with options.start"); - } - - query = { - rangeInclusive: [ - serializeListKey(options.start), - serializeKey(options.end), - ], - }; - } else if (options?.startAfter) { - if (!options.end) { - throw new Error("must set options.end with options.startAfter"); - } - - query = { - rangeExclusive: [ - serializeListKey(options.startAfter), - serializeKey(options.end), - ], - }; - } else if (options?.end) { - throw new Error( - "must set options.start or options.startAfter with options.end", - ); - } else { - query = { all: {} }; - } - - let entries = await op_rivet_kv_list( - query, - options?.reverse ?? false, - options?.limit, - ); - let deserializedValues = new Map(); - - for (let [key, entry] of entries) { - let jsKey = deserializeKey(key); - deserializedValues.set( - jsKey, - deserializeValue(jsKey, entry.value, options?.format), - ); - } - - return deserializedValues; +async function list(options?: ListOptions): Promise> { + // Build query + let query: ListQuery; + if (options?.prefix) { + query = { + prefix: serializeListKey(options.prefix), + }; + } else if (options?.start) { + if (!options.end) { + throw new Error("must set options.end with options.start"); + } + + query = { + rangeInclusive: [serializeListKey(options.start), serializeKey(options.end)], + }; + } else if (options?.startAfter) { + if (!options.end) { + throw new Error("must set options.end with options.startAfter"); + } + + query = { + rangeExclusive: [serializeListKey(options.startAfter), serializeKey(options.end)], + }; + } else if (options?.end) { + throw new Error("must set options.start or options.startAfter with options.end"); + } else { + query = { all: {} }; + } + + let entries = await op_rivet_kv_list(query, options?.reverse ?? false, options?.limit); + let deserializedValues = new Map(); + + for (let [key, entry] of entries) { + let jsKey = deserializeKey(key); + deserializedValues.set(jsKey, deserializeValue(jsKey, entry.value, options?.format)); + } + + return deserializedValues; } /** * Options for the `put` function. */ export interface PutOptions { - format?: "value" | "arrayBuffer"; + format?: "value" | "arrayBuffer"; } /** * Stores a key-value pair in the key-value store. * - * @param {any|any[]} key - The key under which the value will be stored. - * @param {any|ArrayBuffer} value - The value to be stored, which will be serialized. + * @param {Key} key - The key under which the value will be stored. + * @param {Entry | ArrayBuffer} value - The value to be stored, which will be serialized. * @param {PutOptions} [options] - Options. * @returns {Promise} A promise that resolves when the operation is complete. */ -async function put( - key: any | any[], - givenValue: any | ArrayBuffer, - options?: PutOptions, -): Promise { - validateType(givenValue, null, options?.format); - let format = options?.format ?? "value"; - - let value; - if (format == "value") { - value = core.serialize(givenValue, { forStorage: true }); - } else if (format == "arrayBuffer") { - value = new Uint8Array(givenValue); - } - - await op_rivet_kv_put(serializeKey(key), value); +async function put(key: Key, value: Entry | ArrayBuffer, options?: PutOptions): Promise { + validateType(value, null, options?.format); + let format = options?.format ?? "value"; + + let serializedValue; + if (format == "value") { + value = core.serialize(value, { forStorage: true }); + } else if (format == "arrayBuffer") { + if (value instanceof ArrayBuffer) value = new Uint8Array(value); + else throw new Error(`value must be of type \`ArrayBuffer\` if format is "arrayBuffer"`); + } + + await op_rivet_kv_put(serializeKey(key), serializedValue); } /** * Options for the `putBatch` function. */ export interface PutBatchOptions { - format?: "value" | "arrayBuffer"; + format?: "value" | "arrayBuffer"; } /** * Asynchronously stores a batch of key-value pairs. * - * @param {Record} obj - An object containing key-value pairs to be stored. + * @param {Record} obj - An object containing key-value pairs to be stored. * @param {PutBatchOptions} [options] - Options. * @returns {Promise} A promise that resolves when the batch operation is complete. */ -async function putBatch( - obj: Record, - options?: PutBatchOptions, -): Promise { - let serializedObj = new Map(); - let format = options?.format ?? "value"; - - for (let key in obj) { - let givenValue = obj[key]; - - validateType(givenValue, key, format); - - let value; - if (format == "value") { - value = core.serialize(givenValue, { forStorage: true }); - } else if (format == "arrayBuffer") { - value = new Uint8Array(givenValue); - } - - serializedObj.set(serializeKey(key), value); - } - - await op_rivet_kv_put_batch(serializedObj); +async function putBatch(obj: Map, options?: PutBatchOptions): Promise { + let serializedObj = new Map(); + let format = options?.format ?? "value"; + + for (let [key, value] of obj) { + validateType(value, key, format); + + let serializedValue; + if (format == "value") { + value = core.serialize(serializedValue, { forStorage: true }); + } else if (format == "arrayBuffer") { + if (value instanceof ArrayBuffer) value = new Uint8Array(value); + else + throw new Error( + `value in key "${key}" must be of type \`ArrayBuffer\` if format is "arrayBuffer"` + ); + } + + serializedObj.set(serializeKey(key), serializedValue!); + } + + await op_rivet_kv_put_batch(serializedObj); } /** * Deletes a key-value pair from the key-value store. * - * @param {string} key - The key of the key-value pair to delete. + * @param {Key} key - The key of the key-value pair to delete. * @returns {Promise} A promise that resolves when the operation is complete. */ -async function delete_(key: string): Promise { - return await op_rivet_kv_delete(serializeKey(key)); +async function delete_(key: Key): Promise { + return await op_rivet_kv_delete(serializeKey(key)); } -async function deleteBatch(keys: string[]): Promise { - return await op_rivet_kv_delete_batch(keys.map((x) => serializeKey(x))); +async function deleteBatch(keys: Key[]): Promise { + return await op_rivet_kv_delete_batch(keys.map((x) => serializeKey(x))); } /** @@ -232,107 +209,99 @@ async function deleteBatch(keys: string[]): Promise { * @returns {Promise} A promise that resolves when the operation is complete. */ async function deleteAll(): Promise { - return await op_rivet_kv_delete_all(); + return await op_rivet_kv_delete_all(); } function validateType( - value: any, - key: any, - format: "value" | "arrayBuffer" = "value", + value: Entry | ArrayBuffer, + key: Key | null, + format: "value" | "arrayBuffer" = "value" ): void { - let keyText = key ? ` in key "{key}"` : ""; - - if (format == "value") { - if (value instanceof Blob) { - throw new Error( - `the type ${value.constructor.name}${keyText} is not serializable in Deno, but you can use a TypedArray instead. See https://github.com/denoland/deno/issues/12067#issuecomment-1975001079.`, - ); - } - if ( - value instanceof CryptoKey || - value instanceof DOMException || - // Not defined in Deno - // value instanceof RTCCertificate || - // We don't load in the canvas ext into the the Deno runtime for Rivet - // value instanceof ImageBitmap || - value instanceof ImageData - ) { - throw new Error( - `the type ${value.constructor.name}${keyText} is not serializable in Deno. See https://github.com/denoland/deno/issues/12067#issuecomment-1975001079.`, - ); - } - } else if (format == "arrayBuffer") { - if (!(value instanceof ArrayBuffer)) { - throw new Error( - `value must be an ArrayBuffer if options.format = "arrayBuffer".`, - ); - } - } else { - throw new Error("unexpected key type from KV driver"); - } + let keyText = key ? ` in key "{key}"` : ""; + + if (format == "value") { + if (value instanceof Blob) { + throw new Error( + `the type ${value.constructor.name}${keyText} is not serializable in Deno, but you can use a TypedArray instead. See https://github.com/denoland/deno/issues/12067#issuecomment-1975001079.` + ); + } + if ( + value instanceof CryptoKey || + value instanceof DOMException || + // Not defined in Deno + // value instanceof RTCCertificate || + // We don't load in the canvas ext into the the Deno runtime for Rivet + // value instanceof ImageBitmap || + value instanceof ImageData + ) { + throw new Error( + `the type ${value.constructor.name}${keyText} is not serializable in Deno. See https://github.com/denoland/deno/issues/12067#issuecomment-1975001079.` + ); + } + } else if (format == "arrayBuffer") { + if (!(value instanceof ArrayBuffer)) { + throw new Error(`value must be an ArrayBuffer if options.format = "arrayBuffer".`); + } + } else { + throw new Error("unexpected key type from KV driver"); + } } -function serializeKey(key: any): any { - if (key instanceof Array) { - return { jsInKey: [key.map((x) => core.serialize(x))] }; - } else { - return { jsInKey: [core.serialize(key)] }; - } +function serializeKey(key: Key): InKey { + if (key instanceof Array) { + return { jsInKey: key.map((x) => core.serialize(x)) }; + } else { + return { jsInKey: [core.serialize(key)] }; + } } -function serializeListKey(key: any): any[] { - if (key instanceof Array) { - return key.map((x) => core.serialize(x)); - } else { - return [core.serialize(key)]; - } +function serializeListKey(key: Key): Uint8Array[] { + if (key instanceof Array) { + return key.map((x) => core.serialize(x)); + } else { + return [core.serialize(key)]; + } } -function deserializeKey(key: any): any { - if ("inKey" in key || "outKey" in key) { - let jsKey = key.inKey ?? key.outKey; +function deserializeKey(key: OutKey): Key { + if ("inKey" in key || "outKey" in key) { + let jsKey = key.inKey ?? key.outKey; - let tuple = jsKey.map((x: any) => core.deserialize(x)); + let tuple = jsKey.map((x) => core.deserialize(x)); - if (tuple.length == 1) return tuple[0]; - else return tuple; - } else { - throw new Error("unexpected key type from KV driver"); - } + if (tuple.length == 1) return tuple[0]; + else return tuple; + } else { + throw new Error("unexpected key type from KV driver"); + } } -function deserializeValue( - key: any, - value: any, - format: "value" | "arrayBuffer" = "value", -): any { - if (value == undefined) return value; - - if (format == "value") { - try { - return core.deserialize(value, { forStorage: true }); - } catch (e) { - throw new Error( - `could not deserialize value in key "${key}". you must use options.format = "arrayBuffer".`, - { cause: e }, - ); - } - } else if (format == "arrayBuffer") { - return value.buffer; - } else { - throw Error( - `invalid format: "${format}". expected "value" or "arrayBuffer".`, - ); - } +function deserializeValue(key: Key, value: Uint8Array, format: "value" | "arrayBuffer" = "value"): Entry { + if (value == undefined) return value; + + if (format == "value") { + try { + return core.deserialize(value, { forStorage: true }); + } catch (e) { + throw new Error( + `could not deserialize value in key "${key}". you must use options.format = "arrayBuffer".`, + { cause: e } + ); + } + } else if (format == "arrayBuffer") { + return value.buffer; + } else { + throw Error(`invalid format: "${format}". expected "value" or "arrayBuffer".`); + } } export const KV_NAMESPACE = { - get, - getBatch, - list, - put, - putBatch, - delete: delete_, - deleteBatch, - deleteAll, + get, + getBatch, + list, + put, + putBatch, + delete: delete_, + deleteBatch, + deleteAll, }; diff --git a/sdks/actors-bridge/src/bridge/90_rivet_ns.ts b/sdks/actors-bridge/src/bridge/90_rivet_ns.ts index 36bfe79be0..0c34a9bd79 100644 --- a/sdks/actors-bridge/src/bridge/90_rivet_ns.ts +++ b/sdks/actors-bridge/src/bridge/90_rivet_ns.ts @@ -1,5 +1,5 @@ import { KV_NAMESPACE } from "ext:rivet_kv/40_rivet_kv.js"; export const RIVET_NAMESPACE = { - kv: KV_NAMESPACE, + kv: KV_NAMESPACE, }; diff --git a/sdks/actors-bridge/src/bridge/99_rivet_main.ts b/sdks/actors-bridge/src/bridge/99_rivet_main.ts index b85637435d..d4c7e369ab 100644 --- a/sdks/actors-bridge/src/bridge/99_rivet_main.ts +++ b/sdks/actors-bridge/src/bridge/99_rivet_main.ts @@ -3,4 +3,3 @@ import { RIVET_NAMESPACE } from "ext:rivet_runtime/90_rivet_ns.js"; const { ObjectDefineProperty } = primordials; ObjectDefineProperty(globalThis, "Rivet", core.propReadOnly(RIVET_NAMESPACE)); - diff --git a/sdks/actors-bridge/src/ext/core.d.ts b/sdks/actors-bridge/src/ext/core.d.ts index 6acbcb6916..beba5aef94 100644 --- a/sdks/actors-bridge/src/ext/core.d.ts +++ b/sdks/actors-bridge/src/ext/core.d.ts @@ -4,8 +4,8 @@ declare module "ext:core/mod.js" { export const core: { - serialize(value: any, options?: { forStorage?: boolean }): any; - deserialize(value: any, options?: { forStorage?: boolean }): any; + serialize(value: any, options?: { forStorage?: boolean }): Uint8Array; + deserialize(value: Uint8Array, options?: { forStorage?: boolean }): any; propReadOnly(value: any): any; }; export const primordials: { diff --git a/sdks/actors-bridge/src/ext/ops.d.ts b/sdks/actors-bridge/src/ext/ops.d.ts index 730bc30034..427225fcaf 100644 --- a/sdks/actors-bridge/src/ext/ops.d.ts +++ b/sdks/actors-bridge/src/ext/ops.d.ts @@ -3,22 +3,18 @@ */ declare module "ext:core/ops" { - export type Key = any; - export type Entry = any; - export type ListQuery = any; + import type { InKey, OutKey, OutEntry, ListQuery } from "internal_types"; - export function op_rivet_kv_get(key: Key): Promise; - export function op_rivet_kv_get_batch(keys: Key[]): Promise>; - export function op_rivet_kv_list( - query: ListQuery, - reverse: boolean, - limit?: number, - ): Promise>; - export function op_rivet_kv_put(key: Key, value: any): Promise; - export function op_rivet_kv_put_batch( - entries: Map, - ): Promise; - export function op_rivet_kv_delete(key: Key): Promise; - export function op_rivet_kv_delete_batch(keys: Key[]): Promise; - export function op_rivet_kv_delete_all(): Promise; + export function op_rivet_kv_get(key: InKey): Promise; + export function op_rivet_kv_get_batch(keys: InKey[]): Promise>; + export function op_rivet_kv_list( + query: ListQuery, + reverse: boolean, + limit?: number + ): Promise>; + export function op_rivet_kv_put(key: InKey, value: any): Promise; + export function op_rivet_kv_put_batch(entries: Map): Promise; + export function op_rivet_kv_delete(key: InKey): Promise; + export function op_rivet_kv_delete_batch(keys: InKey[]): Promise; + export function op_rivet_kv_delete_all(): Promise; } diff --git a/sdks/actors-bridge/src/ext/types.d.ts b/sdks/actors-bridge/src/ext/types.d.ts new file mode 100644 index 0000000000..5dd189f06c --- /dev/null +++ b/sdks/actors-bridge/src/ext/types.d.ts @@ -0,0 +1,24 @@ +declare module "internal_types" { + export type InKey = { + jsInKey: Uint8Array[]; + }; + export type OutKey = { + inKey: Uint8Array[]; + outKey: Uint8Array[]; + }; + export type OutEntry = { + value: Uint8Array; + metadata: Metadata; + }; + export type Metadata = { + kvVersion: Uint8Array; + createTs: number; + }; + export type ListQuery = { + // Empty object + all?: Record; + rangeInclusive?: [Uint8Array[], InKey]; + rangeExclusive?: [Uint8Array[], InKey]; + prefix?: Uint8Array[]; + }; +} diff --git a/sdks/actors-core/src/index.ts b/sdks/actors-core/src/index.ts index 3d8e89f9c7..1ca3463014 100644 --- a/sdks/actors-core/src/index.ts +++ b/sdks/actors-core/src/index.ts @@ -12,11 +12,12 @@ import type { RIVET_NAMESPACE } from "./types/90_rivet_ns.d.ts"; const Rivet = (globalThis as any).Rivet as typeof RIVET_NAMESPACE; + +// TODO: Replace with polyfill if (!Rivet) { - throw new Error( - "Rivet is not defined in the global scope. This likely means this script is not being ran as a Rivet Actor.", - ); + throw new Error( + "Rivet is not defined in the global scope. This likely means this script is not being ran as a Rivet Actor." + ); } export default Rivet; - diff --git a/sdks/actors-core/src/types b/sdks/actors-core/src/types index e5d3ea71bb..e157dc9236 120000 --- a/sdks/actors-core/src/types +++ b/sdks/actors-core/src/types @@ -1 +1 @@ -../../actors-bridge/types/ \ No newline at end of file +../../actors-bridge/src/bridge \ No newline at end of file