Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .changeset/optional-bootstrap-rule-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@reflag/browser-sdk": patch
"@reflag/react-sdk": patch
---

Accept v2 bootstrap snapshots without rule results. The client treats omitted results as an empty diagnostic trace and still records the selected variant and its source version. Existing snapshots and live flag responses are unchanged.
10 changes: 8 additions & 2 deletions packages/browser-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import {
} from "./feedback/feedback";
import * as feedbackLib from "./feedback/ui";
import { isValidBootstrapSnapshot } from "./flag/bootstrap";
import { CheckEvent, FlagsClient, OptInFlag, RawFlags } from "./flag/flags";
import {
type BootstrappedFlags,
CheckEvent,
FlagsClient,
OptInFlag,
RawFlags,
} from "./flag/flags";
import { isValidFlagStateVersion } from "./flag/flagStateVersion";
import {
FlagDefaultValue,
Expand Down Expand Up @@ -226,7 +232,7 @@ export type FlagDefinitions = Readonly<Array<string>>;
export type BootstrappedState = {
protocolVersion: 2;
context: ReflagContext;
flags: RawFlags;
flags: BootstrappedFlags;
flagStateVersion?: number;
};

Expand Down
7 changes: 4 additions & 3 deletions packages/browser-sdk/src/flag/flagCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export function parseAPIFlagsResponse(
isFlagValue(flag.value) ||
(bootstrap && flag.resolved === false && flag.value === undefined)
) ||
!Array.isArray(flag.ruleResults) ||
!flag.ruleResults.every(isRuleEvaluationResult) ||
(!(bootstrap && flag.ruleResults === undefined) &&
(!Array.isArray(flag.ruleResults) ||
!flag.ruleResults.every(isRuleEvaluationResult))) ||
!Array.isArray(flag.errors) ||
!flag.errors.every(isEvaluationError) ||
(flag.matchedRuleId !== undefined &&
Expand Down Expand Up @@ -111,7 +112,7 @@ export function parseAPIFlagsResponse(
sourceVersionId: flag.sourceVersionId,
matchedRuleId: flag.matchedRuleId,
allocationIndex: flag.allocationIndex,
ruleResults: flag.ruleResults,
ruleResults: flag.ruleResults ?? [],
errors: flag.errors,
...(flag.optInEnabled === undefined
? {}
Expand Down
11 changes: 8 additions & 3 deletions packages/browser-sdk/src/flag/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ const REFRESH_LIMIT_WINDOW_MS = 60 * 1000;

export type FlagOverrides = Record<string, FlagValue | undefined>;

type BootstrappedFlag = Omit<RawFlag, "ruleResults"> & {
ruleResults?: RawFlag["ruleResults"];
};
export type BootstrappedFlags = Record<string, BootstrappedFlag>;

type BootstrappedState = {
protocolVersion: 2;
flags: RawFlags;
flags: BootstrappedFlags;
flagStateVersion?: number;
};

Expand Down Expand Up @@ -476,7 +481,7 @@ export class FlagsClient {
}

setFetchedFlags(
fetchedFlags: RawFlags,
fetchedFlags: BootstrappedFlags,
triggerEvent = true,
flagStateVersion?: number,
) {
Expand All @@ -495,7 +500,7 @@ export class FlagsClient {
// A newly accepted snapshot supersedes any metadata-only request in flight.
this.optInMetadataFetchVersion++;
this.setOptInMetadata(validated, flagStateVersion);
this.warnMissingFlagContextFields(fetchedFlags);
this.warnMissingFlagContextFields(validated);
this.updateFlags(triggerEvent);
}

Expand Down
29 changes: 27 additions & 2 deletions packages/browser-sdk/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

import type { BulkEvent } from "../src/bulkQueue";
import { BootstrappedState, ReflagClient } from "../src/client";
import { RawFlags } from "../src/flag/flags";
import { flagResponse, selectedFlag } from "./mocks/handlers";
import { server } from "./mocks/server";
import { testLogger } from "./testLogger";
Expand Down Expand Up @@ -40,7 +39,10 @@ function create(
clients.push(client);
return client;
}
function bootstrap(flags: RawFlags, flagStateVersion = 1): BootstrappedState {
function bootstrap(
flags: BootstrappedState["flags"],
flagStateVersion = 1,
): BootstrappedState {
return {
protocolVersion: 2,
context: { user: { id: "alice" }, company: { id: "acme" } },
Expand Down Expand Up @@ -95,6 +97,29 @@ describe("direct values and root checks", () => {
},
);

test("bootstraps and records the selected variant without rule results", async () => {
const client = create({
bootstrappedState: bootstrap({
flagA: {
value: "enhanced",
resolved: true,
variantKey: "enhanced",
sourceVersionId: "bootstrap-version",
errors: [],
},
}),
});
expect(client.getFlagResults().flagA.ruleResults).toEqual([]);
expect(client.getFlag("flagA", "default")).toBe("enhanced");
await client.stop();
expect(checks()[0]).toMatchObject({
key: "flagA",
evalResult: "enhanced",
sourceVersionId: "bootstrap-version",
evalRuleResults: [],
});
});

test("forwards v2 diagnostics without adding initialization errors to bootstrap reads", async () => {
const error = {
code: "MISSING_CONTEXT_FIELD",
Expand Down
24 changes: 18 additions & 6 deletions packages/browser-sdk/test/flagCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ describe("v2 evaluated cache", () => {
expect(parseAPIFlagsResponse(flags)).toEqual(flags);
});

test("accepts omitted rule results only in bootstrap snapshots", () => {
const { ruleResults: _ruleResults, ...flagA } = selectedFlag(true);
expect(parseAPIFlagsResponse({ flagA }, true)).toEqual({
flagA: { ...flagA, ruleResults: [] },
});
expect(parseAPIFlagsResponse({ flagA })).toBeUndefined();
expect(
parseAPIFlagsResponse({ flagA: { ...flagA, ruleResults: null } }, true),
).toBeUndefined();
});

test.each([
null,
true,
{},
{ ...rule, ruleId: null },
{ ...rule, ruleId: 1 },
{ ...rule, matched: undefined },
{ ...rule, matched: 1 },
Expand All @@ -72,12 +84,12 @@ describe("v2 evaluated cache", () => {
{ type: "variant", variantKey: 1 },
].map((destination) => ({ ...rule, destination })),
])("rejects malformed rule diagnostics atomically: %j", (invalidRule) => {
expect(
parseAPIFlagsResponse({
good: selectedFlag(true),
bad: { ...selectedFlag(true), ruleResults: [invalidRule] },
}),
).toBeUndefined();
const flags = {
good: selectedFlag(true),
bad: { ...selectedFlag(true), ruleResults: [invalidRule] },
};
expect(parseAPIFlagsResponse(flags)).toBeUndefined();
expect(parseAPIFlagsResponse(flags, true)).toBeUndefined();
});

test.each([
Expand Down
4 changes: 1 addition & 3 deletions packages/react-sdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export type OptInFlag = Omit<BrowserOptInFlag, "key"> & {
*/
export type RawFlags = BrowserRawFlags;

export type BootstrappedFlags = BrowserBootstrappedState & {
flags: BrowserRawFlags;
};
export type BootstrappedFlags = BrowserBootstrappedState;

const SDK_VERSION = `react-sdk/${version}`;

Expand Down
Loading