Skip to content

Commit 3e6d404

Browse files
authored
feat: Apply private property naming standard. Mangle browser private properties. (#620)
1. Update private properties and methods to start with a leading underscore. 2. Update browser terser config to mangle properties that start with an underscore that are not `_meta`. (It would be nice to have a more elegant way to do this, but this is what we have.) 3. Add linting rules for typescript naming conventions. 5. Fix lint issues from rules. Conventions Private properties and methods start with _ and CamelCase case names. Public methods and properties use CamelCase and do not start with an underscore. Public static properties use PascalCase and do not start with an underscore. (This is negotiable, but required fewer changes.) sdk-764
1 parent 794dbfc commit 3e6d404

File tree

121 files changed

+2353
-2231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+2353
-2231
lines changed

.eslintrc.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ module.exports = {
4747
'jest/no-focused-tests': 'error',
4848
'jest/no-identical-title': 'error',
4949
'jest/valid-expect': 'error',
50+
'no-underscore-dangle': ['error', { allowAfterThis: true }],
51+
'@typescript-eslint/naming-convention': [
52+
'error',
53+
{
54+
selector: ['method'],
55+
format: ['camelCase'],
56+
leadingUnderscore: 'forbid',
57+
},
58+
{
59+
selector: ['method'],
60+
format: ['camelCase'],
61+
modifiers: ['private'],
62+
leadingUnderscore: 'require',
63+
},
64+
{
65+
selector: ['classProperty', 'parameterProperty'],
66+
format: ['camelCase'],
67+
leadingUnderscore: 'forbid',
68+
},
69+
{
70+
selector: ['classProperty', 'parameterProperty'],
71+
modifiers: ['static'],
72+
format: ['PascalCase'],
73+
leadingUnderscore: 'forbid',
74+
},
75+
{
76+
selector: ['classProperty', 'parameterProperty'],
77+
modifiers: ['private'],
78+
format: ['camelCase'],
79+
leadingUnderscore: 'require',
80+
},
81+
],
5082
},
5183
globals: {
5284
BigInt: 'readonly',

.github/workflows/release-please.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ jobs:
191191
workspace_path: packages/sdk/browser
192192
aws_assume_role: ${{ vars.AWS_ROLE_ARN }}
193193

194-
195194
release-server-node:
196195
runs-on: ubuntu-latest
197196
needs: ['release-please', 'release-sdk-server']

packages/sdk/akamai-edgekv/src/edgekv/edgeKVProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ type EdgeKVProviderParams = {
88
};
99

1010
export default class EdgeKVProvider implements EdgeProvider {
11-
private edgeKv: EdgeKV;
11+
private _edgeKv: EdgeKV;
1212

1313
constructor({ namespace, group }: EdgeKVProviderParams) {
14-
this.edgeKv = new EdgeKV({ namespace, group } as any);
14+
this._edgeKv = new EdgeKV({ namespace, group } as any);
1515
}
1616

1717
async get(rootKey: string): Promise<string | null | undefined> {
1818
try {
19-
return await this.edgeKv.getText({ item: rootKey } as any);
19+
return await this._edgeKv.getText({ item: rootKey } as any);
2020
} catch (e) {
2121
/* empty */
2222
}

packages/sdk/browser/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-->
1010

1111
# ⛔️⛔️⛔️⛔️
12+
1213
> [!CAUTION]
1314
> This library is a alpha version and should not be considered ready for production use while this message is visible.
1415

packages/sdk/browser/contract-tests/entity/src/ClientEntity.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ function makeDefaultInitialContext() {
7575

7676
export class ClientEntity {
7777
constructor(
78-
private readonly client: LDClient,
79-
private readonly logger: LDLogger,
78+
private readonly _client: LDClient,
79+
private readonly _logger: LDLogger,
8080
) {}
8181

8282
close() {
83-
this.client.close();
84-
this.logger.info('Test ended');
83+
this._client.close();
84+
this._logger.info('Test ended');
8585
}
8686

8787
async doCommand(params: CommandParams) {
88-
this.logger.info(`Received command: ${params.command}`);
88+
this._logger.info(`Received command: ${params.command}`);
8989
switch (params.command) {
9090
case CommandType.EvaluateFlag: {
9191
const evaluationParams = params.evaluate;
@@ -95,23 +95,23 @@ export class ClientEntity {
9595
if (evaluationParams.detail) {
9696
switch (evaluationParams.valueType) {
9797
case ValueType.Bool:
98-
return this.client.boolVariationDetail(
98+
return this._client.boolVariationDetail(
9999
evaluationParams.flagKey,
100100
evaluationParams.defaultValue as boolean,
101101
);
102102
case ValueType.Int: // Intentional fallthrough.
103103
case ValueType.Double:
104-
return this.client.numberVariationDetail(
104+
return this._client.numberVariationDetail(
105105
evaluationParams.flagKey,
106106
evaluationParams.defaultValue as number,
107107
);
108108
case ValueType.String:
109-
return this.client.stringVariationDetail(
109+
return this._client.stringVariationDetail(
110110
evaluationParams.flagKey,
111111
evaluationParams.defaultValue as string,
112112
);
113113
default:
114-
return this.client.variationDetail(
114+
return this._client.variationDetail(
115115
evaluationParams.flagKey,
116116
evaluationParams.defaultValue,
117117
);
@@ -120,42 +120,45 @@ export class ClientEntity {
120120
switch (evaluationParams.valueType) {
121121
case ValueType.Bool:
122122
return {
123-
value: this.client.boolVariation(
123+
value: this._client.boolVariation(
124124
evaluationParams.flagKey,
125125
evaluationParams.defaultValue as boolean,
126126
),
127127
};
128128
case ValueType.Int: // Intentional fallthrough.
129129
case ValueType.Double:
130130
return {
131-
value: this.client.numberVariation(
131+
value: this._client.numberVariation(
132132
evaluationParams.flagKey,
133133
evaluationParams.defaultValue as number,
134134
),
135135
};
136136
case ValueType.String:
137137
return {
138-
value: this.client.stringVariation(
138+
value: this._client.stringVariation(
139139
evaluationParams.flagKey,
140140
evaluationParams.defaultValue as string,
141141
),
142142
};
143143
default:
144144
return {
145-
value: this.client.variation(evaluationParams.flagKey, evaluationParams.defaultValue),
145+
value: this._client.variation(
146+
evaluationParams.flagKey,
147+
evaluationParams.defaultValue,
148+
),
146149
};
147150
}
148151
}
149152

150153
case CommandType.EvaluateAllFlags:
151-
return { state: this.client.allFlags() };
154+
return { state: this._client.allFlags() };
152155

153156
case CommandType.IdentifyEvent: {
154157
const identifyParams = params.identifyEvent;
155158
if (!identifyParams) {
156159
throw malformedCommand;
157160
}
158-
await this.client.identify(identifyParams.user || identifyParams.context);
161+
await this._client.identify(identifyParams.user || identifyParams.context);
159162
return undefined;
160163
}
161164

@@ -164,7 +167,7 @@ export class ClientEntity {
164167
if (!customEventParams) {
165168
throw malformedCommand;
166169
}
167-
this.client.track(
170+
this._client.track(
168171
customEventParams.eventKey,
169172
customEventParams.data,
170173
customEventParams.metricValue,
@@ -173,7 +176,7 @@ export class ClientEntity {
173176
}
174177

175178
case CommandType.FlushEvents:
176-
this.client.flush();
179+
this._client.flush();
177180
return undefined;
178181

179182
default:

packages/sdk/browser/contract-tests/entity/src/TestHarnessWebSocket.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ import { ClientEntity, newSdkClientEntity } from './ClientEntity';
44
import { makeLogger } from './makeLogger';
55

66
export default class TestHarnessWebSocket {
7-
private ws?: WebSocket;
8-
private readonly entities: Record<string, ClientEntity> = {};
9-
private clientCounter = 0;
10-
private logger: LDLogger = makeLogger('TestHarnessWebSocket');
7+
private _ws?: WebSocket;
8+
private readonly _entities: Record<string, ClientEntity> = {};
9+
private _clientCounter = 0;
10+
private _logger: LDLogger = makeLogger('TestHarnessWebSocket');
1111

12-
constructor(private readonly url: string) {}
12+
constructor(private readonly _url: string) {}
1313

1414
connect() {
15-
this.logger.info(`Connecting to web socket.`);
16-
this.ws = new WebSocket(this.url, ['v1']);
17-
this.ws.onopen = () => {
18-
this.logger.info('Connected to websocket.');
15+
this._logger.info(`Connecting to web socket.`);
16+
this._ws = new WebSocket(this._url, ['v1']);
17+
this._ws.onopen = () => {
18+
this._logger.info('Connected to websocket.');
1919
};
20-
this.ws.onclose = () => {
21-
this.logger.info('Websocket closed. Attempting to reconnect in 1 second.');
20+
this._ws.onclose = () => {
21+
this._logger.info('Websocket closed. Attempting to reconnect in 1 second.');
2222
setTimeout(() => {
2323
this.connect();
2424
}, 1000);
2525
};
26-
this.ws.onerror = (err) => {
27-
this.logger.info(`error:`, err);
26+
this._ws.onerror = (err) => {
27+
this._logger.info(`error:`, err);
2828
};
2929

30-
this.ws.onmessage = async (msg) => {
31-
this.logger.info('Test harness message', msg);
30+
this._ws.onmessage = async (msg) => {
31+
this._logger.info('Test harness message', msg);
3232
const data = JSON.parse(msg.data);
3333
const resData: any = { reqId: data.reqId };
3434
switch (data.command) {
@@ -46,33 +46,33 @@ export default class TestHarnessWebSocket {
4646
break;
4747
case 'createClient':
4848
{
49-
resData.resourceUrl = `/clients/${this.clientCounter}`;
49+
resData.resourceUrl = `/clients/${this._clientCounter}`;
5050
resData.status = 201;
5151
const entity = await newSdkClientEntity(data.body);
52-
this.entities[this.clientCounter] = entity;
53-
this.clientCounter += 1;
52+
this._entities[this._clientCounter] = entity;
53+
this._clientCounter += 1;
5454
}
5555
break;
5656
case 'runCommand':
57-
if (Object.prototype.hasOwnProperty.call(this.entities, data.id)) {
58-
const entity = this.entities[data.id];
57+
if (Object.prototype.hasOwnProperty.call(this._entities, data.id)) {
58+
const entity = this._entities[data.id];
5959
const body = await entity.doCommand(data.body);
6060
resData.body = body;
6161
resData.status = body ? 200 : 204;
6262
} else {
6363
resData.status = 404;
64-
this.logger.warn(`Client did not exist: ${data.id}`);
64+
this._logger.warn(`Client did not exist: ${data.id}`);
6565
}
6666

6767
break;
6868
case 'deleteClient':
69-
if (Object.prototype.hasOwnProperty.call(this.entities, data.id)) {
70-
const entity = this.entities[data.id];
69+
if (Object.prototype.hasOwnProperty.call(this._entities, data.id)) {
70+
const entity = this._entities[data.id];
7171
entity.close();
72-
delete this.entities[data.id];
72+
delete this._entities[data.id];
7373
} else {
7474
resData.status = 404;
75-
this.logger.warn(`Could not delete client because it did not exist: ${data.id}`);
75+
this._logger.warn(`Could not delete client because it did not exist: ${data.id}`);
7676
}
7777
break;
7878
default:
@@ -84,10 +84,10 @@ export default class TestHarnessWebSocket {
8484
}
8585

8686
disconnect() {
87-
this.ws?.close();
87+
this._ws?.close();
8888
}
8989

9090
send(data: unknown) {
91-
this.ws?.send(JSON.stringify(data));
91+
this._ws?.send(JSON.stringify(data));
9292
}
9393
}

packages/sdk/browser/rollup.config.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,32 @@ export default [
3333
esmExternals: true,
3434
}),
3535
resolve(),
36-
terser(),
36+
terser({
37+
mangle: {
38+
properties: {
39+
regex: /^_/,
40+
},
41+
},
42+
}),
3743
json(),
3844
// The 'sourcemap' option allows using the minified size, not the size before minification.
3945
visualizer({ sourcemap: true }),
4046
],
4147
},
4248
{
4349
...getSharedConfig('cjs', 'dist/index.cjs.js'),
44-
plugins: [typescript(), common(), resolve(), terser(), json()],
50+
plugins: [
51+
typescript(),
52+
common(),
53+
resolve(),
54+
terser({
55+
mangle: {
56+
properties: {
57+
regex: /^_/,
58+
},
59+
},
60+
}),
61+
json(),
62+
],
4563
},
4664
];

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ export type LDClient = Omit<
8686
};
8787

8888
export class BrowserClient extends LDClientImpl implements LDClient {
89-
private readonly goalManager?: GoalManager;
89+
private readonly _goalManager?: GoalManager;
9090

9191
constructor(
92-
private readonly clientSideId: string,
92+
clientSideId: string,
9393
autoEnvAttributes: AutoEnvAttributes,
9494
options: BrowserOptions = {},
9595
overridePlatform?: Platform,
@@ -174,7 +174,7 @@ export class BrowserClient extends LDClientImpl implements LDClient {
174174
this.setEventSendingEnabled(true, false);
175175

176176
if (validatedBrowserOptions.fetchGoals) {
177-
this.goalManager = new GoalManager(
177+
this._goalManager = new GoalManager(
178178
clientSideId,
179179
platform.requests,
180180
baseUrl,
@@ -215,7 +215,7 @@ export class BrowserClient extends LDClientImpl implements LDClient {
215215
// "waitForGoalsReady", then we would make an async immediately invoked function expression
216216
// which emits the event, and assign its promise to a member. The "waitForGoalsReady" function
217217
// would return that promise.
218-
this.goalManager.initialize();
218+
this._goalManager.initialize();
219219

220220
if (validatedBrowserOptions.automaticBackgroundHandling) {
221221
registerStateDetection(() => this.flush());
@@ -225,7 +225,7 @@ export class BrowserClient extends LDClientImpl implements LDClient {
225225

226226
override async identify(context: LDContext, identifyOptions?: LDIdentifyOptions): Promise<void> {
227227
await super.identify(context, identifyOptions);
228-
this.goalManager?.startTracking();
228+
this._goalManager?.startTracking();
229229
}
230230

231231
setStreaming(streaming?: boolean): void {
@@ -235,7 +235,7 @@ export class BrowserClient extends LDClientImpl implements LDClient {
235235
browserDataManager.setForcedStreaming(streaming);
236236
}
237237

238-
private updateAutomaticStreamingState() {
238+
private _updateAutomaticStreamingState() {
239239
const browserDataManager = this.dataManager as BrowserDataManager;
240240
// This will need changed if support for listening to individual flag change
241241
// events it added.
@@ -244,11 +244,11 @@ export class BrowserClient extends LDClientImpl implements LDClient {
244244

245245
override on(eventName: LDEmitterEventName, listener: Function): void {
246246
super.on(eventName, listener);
247-
this.updateAutomaticStreamingState();
247+
this._updateAutomaticStreamingState();
248248
}
249249

250250
override off(eventName: LDEmitterEventName, listener: Function): void {
251251
super.off(eventName, listener);
252-
this.updateAutomaticStreamingState();
252+
this._updateAutomaticStreamingState();
253253
}
254254
}

0 commit comments

Comments
 (0)