Skip to content

Commit

Permalink
Remove URLPattern usage
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Oct 30, 2024
1 parent 35296d8 commit 937b65b
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/actors/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,25 @@ const isEventStreamResponse = (
/**
* The name of the header used to specify the actor ID.
*/
const ACTOR_NAME_PATH_PARAM = "actorName";
const METHOD_NAME_PATH_PARAM = "methodName";
const ACTORS_API_SEGMENT = "actors";
const ACTORS_INVOKE_API_SEGMENT = "invoke";

/**
* The URL pattern for invoking an actor method.
*/
export const actorInvokeUrl = new URLPattern({
pathname:
`/actors/:${ACTOR_NAME_PATH_PARAM}/invoke/:${METHOD_NAME_PATH_PARAM}`,
});
const parseActorInvokeApi = (pathname: string) => {
if (!pathname) {
return null;
}
const normalized = pathname.startsWith("/") ? pathname : `/${pathname}`;
const [_, actorsApiSegment, actorName, invokeApiSegment, methodName] =
normalized
.split("/");
if (
actorsApiSegment !== ACTORS_API_SEGMENT ||
invokeApiSegment !== ACTORS_INVOKE_API_SEGMENT
) {
return null;
}
return { actorName, methodName };
};

// deno-lint-ignore no-explicit-any
type Function = (...args: any) => any;
Expand Down Expand Up @@ -238,13 +247,12 @@ export class ActorRuntime {

this.ensureInitialized(actorId);

const result = actorInvokeUrl.exec(url);
const result = parseActorInvokeApi(url.pathname);
if (!result) {
return new Response(null, { status: 404 });
}
const groups = result?.pathname.groups ?? {};
const actorName = groups[ACTOR_NAME_PATH_PARAM];
const method = groups[METHOD_NAME_PATH_PARAM];
const actorName = result.actorName;
const method = result.methodName;
if (!method || !actorName) {
return new Response(
`method ${method} not found for the actor ${actorName}`,
Expand Down

0 comments on commit 937b65b

Please sign in to comment.