Skip to content

Commit

Permalink
feat: No migration op event for empty flag key. (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Sep 27, 2023
1 parent f3481a4 commit dda6b37
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/shared/sdk-server/__tests__/MigrationOpTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ it('does not generate an event if an op is not set', () => {
},
);

tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});

Expand All @@ -22,8 +24,27 @@ it('does not generate an event with missing context keys', () => {
kind: 'FALLTHROUGH',
});

// Set the op otherwise that would prevent an event as well.
// Set the op otherwise/invoked that would prevent an event as well.
tracker.op('write');
tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});

it('does not generate an event with empty flag key', () => {
const tracker = new MigrationOpTracker(
'',
{ key: 'user-key' },
LDMigrationStage.Off,
LDMigrationStage.Off,
{
kind: 'FALLTHROUGH',
},
);

// Set the op/invoked otherwise that would prevent an event as well.
tracker.op('write');
tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/sdk-server/src/MigrationOpTracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { internal, LDEvaluationReason, LDLogger } from '@launchdarkly/js-sdk-common';
import {
internal,
LDEvaluationReason,
LDLogger,
TypeValidators,
} from '@launchdarkly/js-sdk-common';

import { LDMigrationStage, LDMigrationTracker } from './api';
import {
Expand Down Expand Up @@ -82,6 +87,11 @@ export default class MigrationOpTracker implements LDMigrationTracker {
}

createEvent(): LDMigrationOpEvent | undefined {
if (!TypeValidators.String.is(this.flagKey) || this.flagKey === '') {
this.logger?.error('The flag key for a migration operation must be a non-empty string.');
return undefined;
}

if (!this.operation) {
this.logger?.error('The operation must be set using "op" before an event can be created.');
return undefined;
Expand Down

0 comments on commit dda6b37

Please sign in to comment.