Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Actor ID validation #539

Merged
merged 7 commits into from
Nov 28, 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
7 changes: 7 additions & 0 deletions src/actors/ActorId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default class ActorId {
private readonly id: string;

constructor(id: string) {
if (!id) {
throw new Error("ActorId cannot be empty");
}
this.id = id;
}

Expand All @@ -28,6 +31,10 @@ export default class ActorId {
return this.id;
}

getURLSafeId() {
return encodeURIComponent(this.id);
}

toString() {
return this.id;
}
Expand Down
20 changes: 10 additions & 10 deletions src/actors/client/ActorClient/ActorClientHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async invoke(actorType: string, actorId: ActorId, methodName: string, body?: any): Promise<object> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/method/${methodName}`, {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/method/${methodName}`, {
method: "POST", // we always use POST calls for Invoking (ref: https://github.com/dapr/js-sdk/pull/137#discussion_r772636068)
body,
});
Expand All @@ -37,7 +37,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async stateTransaction(actorType: string, actorId: ActorId, operations: OperationType[]): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/state`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -47,7 +47,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async stateGet(actorType: string, actorId: ActorId, key: string): Promise<KeyValueType | string> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state/${key}`);
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/state/${key}`);
return result as any;
}

Expand All @@ -57,7 +57,7 @@ export default class ActorClientHTTP implements IClientActor {
name: string,
reminder: ActorReminderType,
): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -72,18 +72,18 @@ export default class ActorClientHTTP implements IClientActor {
}

async reminderGet(actorType: string, actorId: ActorId, name: string): Promise<object> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`);
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`);
return result as object;
}

async unregisterActorReminder(actorType: string, actorId: ActorId, name: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`, {
method: "DELETE",
});
}

async registerActorTimer(actorType: string, actorId: ActorId, name: string, timer: ActorTimerType): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/timers/${name}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -99,13 +99,13 @@ export default class ActorClientHTTP implements IClientActor {
}

async unregisterActorTimer(actorType: string, actorId: ActorId, name: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/timers/${name}`, {
method: "DELETE",
});
}

async deactivate(actorType: string, actorId: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId}`, {
async deactivate(actorType: string, actorId: ActorId): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}`, {
method: "DELETE",
});
}
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/http/actors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ describe("http/actors", () => {
});
});

describe("actorId", () => {
it("should be able to create an actorId", () => {
const actorId = ActorId.createRandomId();
expect(actorId.getId()).toBeDefined();
expect(actorId.getURLSafeId()).toBeDefined();
expect(actorId.toString()).toBeDefined();
});

it("should not be able to create an actorId with an empty string", () => {
expect(() => new ActorId("")).toThrowError("ActorId cannot be empty");
});

it("should be able to create an actorId with url unsafe characters like '/'", () => {
const actorId = new ActorId("test/actor");
expect(actorId.getURLSafeId()).toEqual("test%2Factor");
expect(actorId.getId()).toEqual("test/actor");
expect(actorId.toString()).toEqual("test/actor");
});
});

describe("actorProxy", () => {
it("should be able to create an actor object through the proxy", async () => {
const builder = new ActorProxyBuilder<DemoActorCounterInterface>(DemoActorCounterImpl, client);
Expand Down
Loading