Skip to content

Commit

Permalink
Export getFunctionAddress (#30344)
Browse files Browse the repository at this point in the history
We'd like to access this in external code (notably `convex-test`)

GitOrigin-RevId: 8ebd69e54bdc11039f346e72321290a2d155869f
  • Loading branch information
sshader authored and Convex, Inc. committed Oct 4, 2024
1 parent a2a7eb7 commit 8ab2b70
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions src/server/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ 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 {
AppDefinitionAnalysis,
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.
Expand Down
32 changes: 32 additions & 0 deletions src/server/components/paths.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
33 changes: 1 addition & 32 deletions src/server/impl/actions_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion src/server/impl/registration_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<GenericDataModel>, ...args: any) => any,
Expand Down
2 changes: 1 addition & 1 deletion src/server/impl/scheduler_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export {
* @internal
*/
export { currentSystemUdfInComponent } from "./components/index.js";
export { getFunctionAddress } from "./components/index.js";
export type {
ComponentDefinition,
AnyComponents,
Expand Down

0 comments on commit 8ab2b70

Please sign in to comment.