Skip to content

Commit b28f6ba

Browse files
committed
fix: do not inject scoped service into updater
1 parent 375d6b8 commit b28f6ba

12 files changed

+46
-30
lines changed

e2e/src/my-custom-strategy.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class MyCustomStrategy implements UnleashStrategy {
66
name = 'MyCustomStrategy'
77

88
isEnabled(_parameters: unknown): boolean {
9-
return true
9+
// eslint-disable-next-line no-magic-numbers
10+
return Math.random() < 0.5
1011
}
1112
}

src/unleash-strategies/strategy/application-hostname.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@nestjs/common'
22
import { hostname as osHostname } from 'os'
3+
import { UnleashContext } from '../unleash.context'
34
import { UnleashStrategy } from './strategy.interface'
45

56
export interface HostnameParameters {
@@ -11,7 +12,7 @@ export class ApplicationHostnameStrategy implements UnleashStrategy {
1112
name = 'applicationHostname'
1213
hostname = osHostname()
1314

14-
isEnabled(parameters: HostnameParameters): boolean {
15+
isEnabled(parameters: HostnameParameters, _context: UnleashContext): boolean {
1516
if (!parameters.hostNames) {
1617
return false
1718
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { UnleashContext } from '../unleash.context'
12
import { UnleashStrategy } from './strategy.interface'
23

34
export class DefaultStrategy implements UnleashStrategy {
45
name = 'default'
56

6-
isEnabled(_parameters: never): boolean {
7+
isEnabled(_parameters: never, _context: UnleashContext): boolean {
78
return true
89
}
910
}

src/unleash-strategies/strategy/flexible-rollout.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ export interface FlexibleRolloutParameters {
2020
export class FlexibleRolloutStrategy implements UnleashStrategy {
2121
name = 'flexibleRollout'
2222

23-
constructor(private readonly context: UnleashContext) {}
24-
2523
// eslint-disable-next-line complexity
2624
resolveStickiness(
2725
stickiness: UnleashStickiness | undefined,
26+
context: UnleashContext,
2827
): string | undefined {
29-
const userId = this.context.getUserId()
30-
const sessionId = this.context.getSessionId()
28+
const userId = context.getUserId()
29+
const sessionId = context.getSessionId()
3130

3231
switch (stickiness) {
3332
case UnleashStickiness.userId:
@@ -42,11 +41,14 @@ export class FlexibleRolloutStrategy implements UnleashStrategy {
4241
}
4342

4443
// eslint-disable-next-line complexity
45-
isEnabled(parameters: FlexibleRolloutParameters): boolean {
44+
isEnabled(
45+
parameters: FlexibleRolloutParameters,
46+
context: UnleashContext,
47+
): boolean {
4648
const groupId = parameters.groupId
4749
const percentage = Number(parameters.rollout)
4850
const stickiness = parameters.stickiness || UnleashStickiness.default
49-
const stickinessId = this.resolveStickiness(stickiness)
51+
const stickinessId = this.resolveStickiness(stickiness, context)
5052

5153
if (!stickinessId) {
5254
return false

src/unleash-strategies/strategy/gradual-rollout-random.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@nestjs/common'
2+
import { UnleashContext } from '../unleash.context'
23
import { randomGenerator } from '../util'
34
import { UnleashStrategy } from './strategy.interface'
45

@@ -10,7 +11,10 @@ export interface GradualRolloutRandomParameters {
1011
export class GradualRolloutRandomStrategy implements UnleashStrategy {
1112
name = 'gradualRolloutRandom'
1213

13-
isEnabled(parameters: GradualRolloutRandomParameters): boolean {
14+
isEnabled(
15+
parameters: GradualRolloutRandomParameters,
16+
_context: UnleashContext,
17+
): boolean {
1418
const percentage = parseInt(parameters.percentage, 10)
1519

1620
return percentage >= randomGenerator()

src/unleash-strategies/strategy/gradual-rollout-session-id.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export interface GradualRolloutSessionIdParameters {
1212
export class GradualRolloutSessionIdStrategy implements UnleashStrategy {
1313
name = 'gradualRolloutSessionId'
1414

15-
constructor(private readonly context: UnleashContext) {}
16-
17-
isEnabled(parameters: GradualRolloutSessionIdParameters): boolean {
18-
const sessionId = this.context.getSessionId()
15+
isEnabled(
16+
parameters: GradualRolloutSessionIdParameters,
17+
context: UnleashContext,
18+
): boolean {
19+
const sessionId = context.getSessionId()
1920

2021
if (!sessionId) {
2122
return false

src/unleash-strategies/strategy/gradual-rollout-user-id.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export interface GradualRolloutUserIdParameters {
1212
export class GradualRolloutUserIdStrategy implements UnleashStrategy {
1313
name = 'gradualRolloutUserId'
1414

15-
constructor(private readonly context: UnleashContext) {}
16-
17-
isEnabled(parameters: GradualRolloutUserIdParameters): boolean {
18-
const userId = this.context.getUserId()
15+
isEnabled(
16+
parameters: GradualRolloutUserIdParameters,
17+
context: UnleashContext,
18+
): boolean {
19+
const userId = context.getUserId()
1920

2021
if (!userId) {
2122
return false

src/unleash-strategies/strategy/remote-address.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ export interface RemoteAddressParameters {
1616
export class RemoteAddressStrategy implements UnleashStrategy {
1717
name = 'remoteAddress'
1818

19-
constructor(private readonly context: UnleashContext) {}
20-
2119
// eslint-disable-next-line complexity, sonarjs/cognitive-complexity
22-
isEnabled(parameters: RemoteAddressParameters): boolean {
23-
const remoteAddress = this.context.getRemoteAddress()
20+
isEnabled(
21+
parameters: RemoteAddressParameters,
22+
context: UnleashContext,
23+
): boolean {
24+
const remoteAddress = context.getRemoteAddress()
2425

2526
if (!parameters.IPs || !remoteAddress) {
2627
return false
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { UnleashContext } from '../unleash.context'
2+
13
export interface UnleashStrategy {
24
name: string
35

4-
isEnabled(parameters: unknown): boolean
6+
isEnabled(parameters: unknown, context: UnleashContext): boolean
57
}

src/unleash-strategies/strategy/user-with-id.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export interface UserWithIdParameters {
1010
export class UserWithIdStrategy implements UnleashStrategy {
1111
name = 'userWithId'
1212

13-
constructor(private readonly context: UnleashContext) {}
14-
15-
isEnabled(parameters: UserWithIdParameters): boolean {
16-
const userId = this.context.getUserId()
13+
isEnabled(
14+
parameters: UserWithIdParameters,
15+
context: UnleashContext,
16+
): boolean {
17+
const userId = context.getUserId()
1718

1819
if (!userId) {
1920
return false

src/unleash-strategies/unleash-strategies.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class UnleashStrategiesModule {
3232
...strategies,
3333
{ provide: 'CUSTOM_STRATEGIES', useValue: strategies },
3434
],
35-
exports: [UnleashStrategiesService],
35+
exports: [UnleashStrategiesService, UnleashContext],
3636
}
3737
}
3838
}

src/unleash/unleash.service.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable, Logger, Scope } from '@nestjs/common'
2-
import { UnleashStrategiesService } from '../unleash-strategies'
2+
import { UnleashContext, UnleashStrategiesService } from '../unleash-strategies'
33
import { MetricsService } from './metrics.service'
44
import { ToggleRepository } from './repository/toggle-repository'
55

@@ -11,6 +11,7 @@ export class UnleashService {
1111
private readonly toggles: ToggleRepository,
1212
private readonly strategies: UnleashStrategiesService,
1313
private readonly metrics: MetricsService,
14+
private readonly context: UnleashContext,
1415
) {}
1516

1617
// eslint-disable-next-line sonarjs/cognitive-complexity
@@ -39,7 +40,7 @@ export class UnleashService {
3940
}
4041

4142
try {
42-
const isEnabled = strategy.isEnabled(data.parameters)
43+
const isEnabled = strategy.isEnabled(data.parameters, this.context)
4344
if (isEnabled) {
4445
this.logger.debug(`Strategy "${data.name}" returned true`)
4546
}

0 commit comments

Comments
 (0)