Skip to content

Commit 8747dc9

Browse files
Update the event handler code to use the new annotations (#170)
1 parent ecf7675 commit 8747dc9

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

buf.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ deps:
44
- remote: buf.build
55
owner: restatedev
66
repository: proto
7-
commit: 4c536701ef5348ecbf3cd1ef6cf825fc
8-
digest: shake256:0fdebe27d9653dc31f9951623e0c8dc68a161d2b55146cc22c0501d2bccb22d49ab9b2b80c8f4de8827c4ba168119296f14d323eed5162ef63f128803cc64f47
7+
commit: 4de69fd4bcad4d829ebe7643a6f461d1
8+
digest: shake256:d9e8a43e890ecbc53822abd7bff03367697f588f815b9beddc1e34675c886549ef262a1e91c8a678db31a7b18979f88ceb98d7017c79fab0ebb81062740f996e

buf.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: v1
22
deps:
3-
- buf.build/restatedev/proto
3+
- buf.build/restatedev/proto:0.4.0
44
build:
55
excludes:
66
- node_modules

proto/dynrpc.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ service RpcEndpoint {
2121

2222
rpc call(RpcRequest) returns (RpcResponse) {};
2323

24-
rpc handle(dev.restate.StringKeyedEvent) returns (google.protobuf.Empty) {};
24+
rpc handle(KeyedEvent) returns (google.protobuf.Empty) {};
25+
}
26+
27+
message KeyedEvent {
28+
string key = 1 [(dev.restate.ext.field) = KEY];
29+
bytes payload = 3 [(dev.restate.ext.field) = EVENT_PAYLOAD];
30+
map<string, string> attributes = 15 [(dev.restate.ext.field) = EVENT_METADATA];
2531
}
2632

2733
service UnkeyedRpcEndpoint {

src/server/base_restate_server.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
ServiceDiscoveryResponse,
2424
} from "../generated/proto/discovery";
2525
import { Event } from "../types/types";
26-
import { StringKeyedEvent } from "../generated/dev/restate/events";
2726
import {
2827
FileDescriptorProto,
2928
UninterpretedOption,
@@ -43,6 +42,7 @@ import {
4342
RpcResponse,
4443
ProtoMetadata as RpcServiceProtoMetadata,
4544
protoMetadata as rpcServiceProtoMetadata,
45+
KeyedEvent,
4646
} from "../generated/proto/dynrpc";
4747
import { RestateContext, useContext } from "../restate_context";
4848
import { RpcContextImpl } from "../restate_context_impl";
@@ -202,15 +202,15 @@ export abstract class BaseRestateServer {
202202
throw new TerminalError("Unkeyed Event handlers are not yet supported.");
203203
}
204204
const descriptor = createStringKeyedMethodDescriptor(route);
205-
const localMethod = (instance: unknown, input: StringKeyedEvent) => {
205+
const localMethod = (instance: unknown, input: KeyedEvent) => {
206206
const ctx = useContext(instance);
207207
return dispatchKeyedEventHandler(ctx, input, handler);
208208
};
209209

210-
const decoder = StringKeyedEvent.decode;
210+
const decoder = KeyedEvent.decode;
211211
const encoder = (message: Empty) => Empty.encode(message).finish();
212212

213-
const method = new GrpcServiceMethod<StringKeyedEvent, Empty>(
213+
const method = new GrpcServiceMethod<KeyedEvent, Empty>(
214214
route,
215215
route,
216216
localMethod,
@@ -447,19 +447,19 @@ async function dispatchUnkeyedRpcHandler(
447447

448448
async function dispatchKeyedEventHandler(
449449
origCtx: RestateContext,
450-
req: StringKeyedEvent,
450+
req: KeyedEvent,
451451
handler: Function
452452
): Promise<Empty> {
453453
const ctx = new RpcContextImpl(origCtx);
454454
const key = req.key;
455-
if (typeof key !== "string" || key.length === 0) {
455+
if (key === null || key === undefined || key.length === 0) {
456456
// we throw a terminal error here, because this cannot be patched by updating code:
457457
// if the request is wrong (missing a key), the request can never make it
458458
throw new TerminalError(
459-
"Keyed handlers must recieve a non null or empty string key"
459+
"Keyed handlers must receive a non null or empty string key"
460460
);
461461
}
462-
const jsEvent = new Event(key, req.payload, req.source, req.attributes);
462+
const jsEvent = new Event(key, req.payload, req.attributes);
463463
await handler(ctx, jsEvent);
464464
return Empty.create({});
465465
}

src/types/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export class Event {
125125
constructor(
126126
readonly key: string,
127127
readonly payload: Buffer,
128-
readonly source: string,
129128
readonly attributes: Record<string, string>
130129
) {}
131130

0 commit comments

Comments
 (0)