Skip to content

Commit d9fa794

Browse files
authored
Merge pull request #1 from replane-dev/replication-logs
Replication logs
2 parents d4fdd42 + 646d46d commit d9fa794

5 files changed

Lines changed: 74 additions & 310 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@replanejs/sdk",
3-
"version": "0.5.11",
3+
"version": "0.6.1",
44
"description": "Dynamic configuration SDK for browser and server environments (Node.js, Deno, Bun). Powered by Replane.",
55
"type": "module",
66
"license": "MIT",

src/index.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ class ReplaneRemoteStorage implements ReplaneStorage {
332332
for await (const sseEvent of rawEvents) {
333333
resetInactivityTimer();
334334

335-
if (sseEvent.type === "ping") continue;
335+
if (sseEvent.type === "comment") continue;
336336

337-
const event = JSON.parse(sseEvent.payload);
337+
const event = JSON.parse(sseEvent.data);
338338
if (
339339
typeof event === "object" &&
340340
event !== null &&
@@ -364,7 +364,7 @@ class ReplaneRemoteStorage implements ReplaneStorage {
364364
}
365365
}
366366

367-
export type ReplaneContext = Record<string, unknown>;
367+
export type ReplaneContext = Record<string, string | number | boolean | null | undefined>;
368368

369369
export interface ReplaneClientOptions<T extends Configs> {
370370
/**
@@ -404,7 +404,7 @@ export interface ReplaneClientOptions<T extends Configs> {
404404
/**
405405
* Timeout in ms for SSE connection inactivity.
406406
* If no events (including pings) are received within this time, the connection will be re-established.
407-
* @default 60000
407+
* @default 30000
408408
*/
409409
inactivityTimeoutMs?: number;
410410
/**
@@ -597,7 +597,6 @@ async function _createReplaneClient<T extends Configs = Record<string, unknown>>
597597
currentConfigs: [...configs.values()].map((config) => ({
598598
name: config.name,
599599
overrides: config.overrides,
600-
version: config.version,
601600
value: config.value,
602601
})),
603602
requiredConfigs: sdkOptions.requiredConfigs,
@@ -606,14 +605,11 @@ async function _createReplaneClient<T extends Configs = Record<string, unknown>>
606605

607606
for await (const event of replicationStream) {
608607
const updatedConfigs: ConfigDto[] =
609-
event.type === "config_change" ? [event] : event.configs;
608+
event.type === "config_change" ? [event.config] : event.configs;
610609
for (const config of updatedConfigs) {
611-
if (config.version <= (configs.get(config.name)?.version ?? -1)) continue; // ignore outdated changes
612-
613610
configs.set(config.name, {
614611
name: config.name,
615612
overrides: config.overrides,
616-
version: config.version,
617613
value: config.value,
618614
});
619615
for (const callback of clientSubscriptions) {
@@ -759,7 +755,7 @@ function toFinalOptions<T extends Configs>(defaults: ReplaneClientOptions<T>): R
759755
globalThis.fetch.bind(globalThis),
760756
requestTimeoutMs: defaults.requestTimeoutMs ?? 2000,
761757
initializationTimeoutMs: defaults.initializationTimeoutMs ?? 5000,
762-
inactivityTimeoutMs: defaults.inactivityTimeoutMs ?? 60_000,
758+
inactivityTimeoutMs: defaults.inactivityTimeoutMs ?? 30_000,
763759
logger: defaults.logger ?? console,
764760
retryDelayMs: defaults.retryDelayMs ?? 200,
765761
context: {
@@ -810,7 +806,7 @@ async function fetchWithTimeout(
810806

811807
const SSE_DATA_PREFIX = "data:";
812808

813-
type SseEvent = { type: "ping" } | { type: "data"; payload: string };
809+
type SseEvent = { type: "comment"; comment: string } | { type: "data"; data: string };
814810

815811
async function* fetchSse(params: {
816812
fetchFn: typeof fetch;
@@ -878,13 +874,13 @@ async function* fetchSse(params: {
878874
for (const frame of frames) {
879875
// Parse lines inside a single SSE event frame
880876
const dataLines: string[] = [];
881-
let isPing = false;
877+
let comment: string | null = null;
882878

883879
for (const rawLine of frame.split(/\r?\n/)) {
884880
if (!rawLine) continue;
885881
if (rawLine.startsWith(":")) {
886-
// comment/keepalive - treat as ping
887-
isPing = true;
882+
// comment/keepalive
883+
comment = rawLine.slice(1);
888884
continue;
889885
}
890886

@@ -896,10 +892,10 @@ async function* fetchSse(params: {
896892
}
897893

898894
if (dataLines.length) {
899-
const payload = dataLines.join("\n");
900-
yield { type: "data", payload };
901-
} else if (isPing) {
902-
yield { type: "ping" };
895+
const data = dataLines.join("\n");
896+
yield { type: "data", data };
897+
} else if (comment !== null) {
898+
yield { type: "comment", comment };
903899
}
904900
}
905901
}

src/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ export interface StartReplicationStreamBody {
66
export interface ConfigDto {
77
name: string;
88
overrides: RenderedOverride[];
9-
version: number;
109
value: unknown;
1110
}
1211

1312
export type ReplicationStreamRecord =
1413
| {
1514
type: "config_change";
16-
name: string;
17-
overrides: RenderedOverride[];
18-
version: number;
19-
value: unknown;
15+
config: ConfigDto;
2016
}
2117
| {
2218
type: "init";

0 commit comments

Comments
 (0)