Skip to content

Commit 620625b

Browse files
Allow passing journal value codec provider, rather than built codec directly. (#582)
1 parent d756bb0 commit 620625b

File tree

8 files changed

+43
-25
lines changed

8 files changed

+43
-25
lines changed

packages/restate-sdk/src/endpoint.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ export interface RestateEndpointBase<E> {
8484
setLogger(logger: LoggerTransport): E;
8585

8686
/**
87-
* Codec to use for journal values. Check {@link JournalValueCodec} for more details
87+
* Provider for the codec to use for journal values. One codec will be instantiated globally for this endpoint.
88+
* Check {@link JournalValueCodec} for more details
8889
*
8990
* @experimental
9091
*/
91-
journalValueCodec(codec: JournalValueCodec): E;
92+
journalValueCodecProvider(codecProvider: () => Promise<JournalValueCodec>): E;
9293
}
9394

9495
/**

packages/restate-sdk/src/endpoint/endpoint.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ export type Endpoint = {
8585
discoveryMetadata: Omit<discovery.Endpoint, "protocolMode">;
8686

8787
/**
88-
* Default codec for journal entries.
88+
* Codec provider to use for journal values.
8989
*/
90-
journalValueCodec?: JournalValueCodec;
90+
journalValueCodec?: Promise<JournalValueCodec>;
9191
};
9292

9393
export class EndpointBuilder {
@@ -100,7 +100,7 @@ export class EndpointBuilder {
100100
private loggerTransport: LoggerTransport = defaultLoggerTransport;
101101
private keySet: string[] = [];
102102
private defaultServiceOptions: DefaultServiceOptions = {};
103-
private journalValueCodec?: JournalValueCodec;
103+
private journalValueCodecProvider?: () => Promise<JournalValueCodec>;
104104

105105
public bind<P extends string, M>(
106106
definition:
@@ -128,8 +128,10 @@ export class EndpointBuilder {
128128
this.loggerTransport = newLogger;
129129
}
130130

131-
public setJournalValueCodec(codec: JournalValueCodec) {
132-
this.journalValueCodec = codec;
131+
public setJournalValueCodecProvider(
132+
codecProvider: () => Promise<JournalValueCodec>
133+
) {
134+
this.journalValueCodecProvider = codecProvider;
133135
}
134136

135137
public build(): Endpoint {
@@ -196,7 +198,9 @@ export class EndpointBuilder {
196198
rlog,
197199
components,
198200
discoveryMetadata,
199-
journalValueCodec: this.journalValueCodec,
201+
journalValueCodec: this.journalValueCodecProvider
202+
? this.journalValueCodecProvider()
203+
: undefined,
200204
};
201205
}
202206
}

packages/restate-sdk/src/endpoint/fetch_endpoint.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ export class FetchEndpointImpl implements FetchEndpoint {
9090
return this;
9191
}
9292

93-
public journalValueCodec(codec: JournalValueCodec): FetchEndpoint {
94-
this.builder.setJournalValueCodec(codec);
93+
public journalValueCodecProvider(
94+
codecProvider: () => Promise<JournalValueCodec>
95+
): FetchEndpoint {
96+
this.builder.setJournalValueCodecProvider(codecProvider);
9597
return this;
9698
}
9799

packages/restate-sdk/src/endpoint/handlers/generic.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ import {
3737
LogSource,
3838
RestateLogLevel,
3939
} from "../../logging/logger_transport.js";
40-
import { millisOrDurationToMillis } from "@restatedev/restate-sdk-core";
40+
import {
41+
type JournalValueCodec,
42+
millisOrDurationToMillis,
43+
} from "@restatedev/restate-sdk-core";
4144
import type { Endpoint } from "../endpoint.js";
4245

4346
export interface Headers {
@@ -236,10 +239,12 @@ export class GenericHandler implements RestateHandler {
236239
abortSignal: AbortSignal,
237240
additionalContext: AdditionalContext
238241
): Promise<RestateResponse> {
239-
const journalValueCodec = this.endpoint.journalValueCodec ?? {
240-
encode: (entry) => entry,
241-
decode: (entry) => Promise.resolve(entry),
242-
};
242+
const journalValueCodec: JournalValueCodec = this.endpoint.journalValueCodec
243+
? await this.endpoint.journalValueCodec
244+
: {
245+
encode: (entry) => entry,
246+
decode: (entry) => Promise.resolve(entry),
247+
};
243248
const loggerId = Math.floor(Math.random() * 4_294_967_295 /* u32::MAX */);
244249

245250
try {

packages/restate-sdk/src/endpoint/lambda_endpoint.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ export class LambdaEndpointImpl implements LambdaEndpoint {
7171
return this;
7272
}
7373

74-
journalValueCodec(codec: JournalValueCodec): LambdaEndpoint {
75-
this.builder.setJournalValueCodec(codec);
74+
public journalValueCodecProvider(
75+
codecProvider: () => Promise<JournalValueCodec>
76+
): LambdaEndpoint {
77+
this.builder.setJournalValueCodecProvider(codecProvider);
7678
return this;
7779
}
7880

packages/restate-sdk/src/endpoint/node_endpoint.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ export class NodeEndpoint implements RestateEndpoint {
6161
return this;
6262
}
6363

64-
public journalValueCodec(codec: JournalValueCodec): RestateEndpoint {
65-
this.builder.setJournalValueCodec(codec);
64+
public journalValueCodecProvider(
65+
codecProvider: () => Promise<JournalValueCodec>
66+
): RestateEndpoint {
67+
this.builder.setJournalValueCodecProvider(codecProvider);
6668
return this;
6769
}
6870

packages/restate-sdk/src/endpoint/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ export interface EndpointOptions {
5858
logger?: LoggerTransport;
5959

6060
/**
61-
* Codec to use for journal values. Check {@link JournalValueCodec} for more details
61+
* Provider for the codec to use for journal values. One codec will be instantiated globally for this endpoint.
62+
* Check {@link JournalValueCodec} for more details
6263
*
6364
* @experimental
6465
*/
65-
journalValueCodec?: JournalValueCodec;
66+
journalValueCodecProvider?: () => Promise<JournalValueCodec>;
6667
}

packages/restate-sdk/src/endpoint/withOptions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function withOptions<E extends RestateEndpointBase<E>>(
88
defaultServiceOptions,
99
logger,
1010
services,
11-
journalValueCodec,
11+
journalValueCodecProvider,
1212
}: EndpointOptions
1313
): E {
1414
let endpointWithOptions = endpoint;
@@ -20,9 +20,10 @@ export function withOptions<E extends RestateEndpointBase<E>>(
2020
defaultServiceOptions
2121
);
2222
}
23-
if (journalValueCodec) {
24-
endpointWithOptions =
25-
endpointWithOptions.journalValueCodec(journalValueCodec);
23+
if (journalValueCodecProvider) {
24+
endpointWithOptions = endpointWithOptions.journalValueCodecProvider(
25+
journalValueCodecProvider
26+
);
2627
}
2728
if (logger) {
2829
endpointWithOptions = endpointWithOptions.setLogger(logger);

0 commit comments

Comments
 (0)