Skip to content
Open
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ jobs:

- name: Run Tests
shell: bash
env:
NEXT_PUBLIC_STARKNET_NETWORK: goerli-alpha
run: |
if timeout 30s pnpm vitest run --coverage; then
echo "Tests completed within the 30-second limit."
Expand Down
5 changes: 3 additions & 2 deletions src/app/api/admin/feature-flags/evaluate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export async function GET(req: NextRequest) {
const { addHeaders, rateLimitResponse } = withRateLimit(req, 'READ');
if (rateLimitResponse) return rateLimitResponse;

const { searchParams } = new URL(req.url);
const { searchParams } = new URL( req.url);

const id = searchParams.get('id');

if (!id) {
Expand All @@ -33,6 +34,6 @@ export async function GET(req: NextRequest) {
if (key !== 'id') context[key] = value;
});

const isEnabled = evaluateFlag(flag, context);
const isEnabled = await evaluateFlag(flag, context);
return addHeaders(NextResponse.json({ flag, isEnabled, context }));
}
4 changes: 2 additions & 2 deletions src/app/pages/admin/feature-flags/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ function AuditPanel({ flagId, onClose }: { flagId?: string; onClose: () => void
setLoading(false);
}, [flagId]);

useState(() => {
useEffect(() => {
void load();
});
}, [load]);

const ACTION_COLORS: Record<AuditEntry['action'], string> = {
created: 'green',
Expand Down
2 changes: 1 addition & 1 deletion src/lib/feature-flags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* src/lib/feature-flags β€” public barrel
*/
export type { FeatureFlag, TargetingRule, AuditEntry, RolloutStrategy } from './store';
export { flagStore, auditLog, evaluateFlag, createAuditEntry, generateId } from './store';
export { flagStore, auditLog, evaluateFlag, createAuditEntry, generateId, getPercentageBucket } from './store';
22 changes: 15 additions & 7 deletions src/lib/feature-flags/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ export function generateId(prefix = ''): string {
return prefix ? `${prefix}_${uuid}` : uuid;
}

/**
* Deterministic 0–99 bucket for a flag/user pair. Used for percentage
* rollouts so the same user always lands in the same bucket.
*/
export function getPercentageBucket(flagId: string, userId = ''): number {
let hash = 0;
const key = flagId + userId;
for (let i = 0; i < key.length; i++) {
hash = Math.imul(31, hash) + key.charCodeAt(i);
hash |= 0;
}
return Math.abs(hash) % 100;
}

export function createAuditEntry(
action: AuditEntry['action'],
actor: string,
Expand Down Expand Up @@ -145,13 +159,7 @@ export function evaluateFlag(flag: FeatureFlag, context: Record<string, string>
if (flag.percentage >= 100) return true;
if (flag.percentage <= 0) return false;
// Deterministic per-user bucket via userId hash
const userId = context.userId ?? '';
let hash = 0;
for (let i = 0; i < (flag.id + userId).length; i++) {
hash = Math.imul(31, hash) + (flag.id + userId).charCodeAt(i);
hash |= 0;
}
const bucket = Math.abs(hash) % 100;
const bucket = getPercentageBucket(flag.id, context.userId ?? '');
return bucket < flag.percentage;
}

Expand Down
Loading