From 8ab2b70371094d8672e13795ed003d94b777f686 Mon Sep 17 00:00:00 2001 From: Sarah Shader Date: Fri, 4 Oct 2024 12:01:03 -0400 Subject: [PATCH] Export getFunctionAddress (#30344) We'd like to access this in external code (notably `convex-test`) GitOrigin-RevId: 8ebd69e54bdc11039f346e72321290a2d155869f --- src/server/api.ts | 2 +- src/server/components/index.ts | 8 +++++-- src/server/components/paths.ts | 32 +++++++++++++++++++++++++++ src/server/impl/actions_impl.ts | 33 +--------------------------- src/server/impl/registration_impl.ts | 3 ++- src/server/impl/scheduler_impl.ts | 2 +- src/server/index.ts | 1 + 7 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/server/api.ts b/src/server/api.ts index 91f7fba..f5c1aef 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -8,8 +8,8 @@ import { } from "./registration.js"; import { Expand, UnionToIntersection } from "../type_utils.js"; import { PaginationOptions, PaginationResult } from "./pagination.js"; -import { getFunctionAddress } from "./impl/actions_impl.js"; import { functionName } from "./functionName.js"; +import { getFunctionAddress } from "./components/paths.js"; /** * The type of a Convex function. diff --git a/src/server/components/index.ts b/src/server/components/index.ts index 061a096..3a3aa5a 100644 --- a/src/server/components/index.ts +++ b/src/server/components/index.ts @@ -4,7 +4,6 @@ import { FunctionReference, FunctionType, } from "../api.js"; -import { getFunctionAddress } from "../impl/actions_impl.js"; import { performAsyncSyscall } from "../impl/syscall.js"; import { DefaultFunctionArgs } from "../registration.js"; import { @@ -12,7 +11,12 @@ import { ComponentDefinitionAnalysis, ComponentDefinitionType, } from "./definition.js"; -import { setReferencePath, toReferencePath } from "./paths.js"; +import { + getFunctionAddress, + setReferencePath, + toReferencePath, +} from "./paths.js"; +export { getFunctionAddress } from "./paths.js"; /** * A serializable reference to a Convex function. diff --git a/src/server/components/paths.ts b/src/server/components/paths.ts index 6ce2699..c12b98d 100644 --- a/src/server/components/paths.ts +++ b/src/server/components/paths.ts @@ -1,3 +1,5 @@ +import { functionName } from "../functionName.js"; + export const toReferencePath = Symbol.for("toReferencePath"); // Multiple instances of the same Symbol.for() are equal at runtime but not @@ -14,3 +16,33 @@ export function extractReferencePath(reference: any): string | null { export function isFunctionHandle(s: string): boolean { return s.startsWith("function://"); } + +export function getFunctionAddress(functionReference: any) { + // The `run*` syscalls expect either a UDF path at "name" or a serialized + // reference at "reference". Dispatch on `functionReference` to coerce + // it to one or the other. + let functionAddress; + + // Legacy path for passing in UDF paths directly as function references. + if (typeof functionReference === "string") { + if (isFunctionHandle(functionReference)) { + functionAddress = { functionHandle: functionReference }; + } else { + functionAddress = { name: functionReference }; + } + } + // Path for passing in a `FunctionReference`, either from `api` or directly + // created from a UDF path with `makeFunctionReference`. + else if (functionReference[functionName]) { + functionAddress = { name: functionReference[functionName] }; + } + // Reference to a component's function derived from `app` or `component`. + else { + const referencePath = extractReferencePath(functionReference); + if (!referencePath) { + throw new Error(`${functionReference} is not a functionReference`); + } + functionAddress = { reference: referencePath }; + } + return functionAddress; +} diff --git a/src/server/impl/actions_impl.ts b/src/server/impl/actions_impl.ts index d5aef3d..c3df52b 100644 --- a/src/server/impl/actions_impl.ts +++ b/src/server/impl/actions_impl.ts @@ -3,8 +3,7 @@ import { version } from "../../index.js"; import { performAsyncSyscall } from "./syscall.js"; import { parseArgs } from "../../common/index.js"; import { FunctionReference } from "../../server/api.js"; -import { extractReferencePath, isFunctionHandle } from "../components/paths.js"; -import { functionName } from "../functionName.js"; +import { getFunctionAddress } from "../components/paths.js"; function syscallArgs( requestId: string, @@ -20,36 +19,6 @@ function syscallArgs( }; } -export function getFunctionAddress(functionReference: any) { - // The `run*` syscalls expect either a UDF path at "name" or a serialized - // reference at "reference". Dispatch on `functionReference` to coerce - // it to one or the other. - let functionAddress; - - // Legacy path for passing in UDF paths directly as function references. - if (typeof functionReference === "string") { - if (isFunctionHandle(functionReference)) { - functionAddress = { functionHandle: functionReference }; - } else { - functionAddress = { name: functionReference }; - } - } - // Path for passing in a `FunctionReference`, either from `api` or directly - // created from a UDF path with `makeFunctionReference`. - else if (functionReference[functionName]) { - functionAddress = { name: functionReference[functionName] }; - } - // Reference to a component's function derived from `app` or `component`. - else { - const referencePath = extractReferencePath(functionReference); - if (!referencePath) { - throw new Error(`${functionReference} is not a functionReference`); - } - functionAddress = { reference: referencePath }; - } - return functionAddress; -} - export function setupActionCalls(requestId: string) { return { runQuery: async ( diff --git a/src/server/impl/registration_impl.ts b/src/server/impl/registration_impl.ts index 7d04919..66c226c 100644 --- a/src/server/impl/registration_impl.ts +++ b/src/server/impl/registration_impl.ts @@ -21,7 +21,7 @@ import { RegisteredMutation, RegisteredQuery, } from "../registration.js"; -import { getFunctionAddress, setupActionCalls } from "./actions_impl.js"; +import { setupActionCalls } from "./actions_impl.js"; import { setupActionVectorSearch } from "./vector_search_impl.js"; import { setupAuth } from "./authentication_impl.js"; import { setupReader, setupWriter } from "./database_impl.js"; @@ -38,6 +38,7 @@ import { import { parseArgs } from "../../common/index.js"; import { performAsyncSyscall } from "./syscall.js"; import { asObjectValidator } from "../../values/validator.js"; +import { getFunctionAddress } from "../components/paths.js"; async function invokeMutation< F extends (ctx: GenericMutationCtx, ...args: any) => any, diff --git a/src/server/impl/scheduler_impl.ts b/src/server/impl/scheduler_impl.ts index 34bb398..1c475e3 100644 --- a/src/server/impl/scheduler_impl.ts +++ b/src/server/impl/scheduler_impl.ts @@ -5,7 +5,7 @@ import { parseArgs } from "../../common/index.js"; import { SchedulableFunctionReference, Scheduler } from "../scheduler.js"; import { Id } from "../../values/value.js"; import { validateArg } from "./validate.js"; -import { getFunctionAddress } from "./actions_impl.js"; +import { getFunctionAddress } from "../components/paths.js"; export function setupMutationScheduler(): Scheduler { return { diff --git a/src/server/index.ts b/src/server/index.ts index 3dfb6cb..b84a66f 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -173,6 +173,7 @@ export { * @internal */ export { currentSystemUdfInComponent } from "./components/index.js"; +export { getFunctionAddress } from "./components/index.js"; export type { ComponentDefinition, AnyComponents,