Skip to content

Commit 282a3fe

Browse files
authored
feat!: Add uuidv4 to platform. (#56)
1 parent 6be132f commit 282a3fe

File tree

11 files changed

+67
-21
lines changed

11 files changed

+67
-21
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/* eslint-disable class-methods-use-this */
22
import { platform } from '@launchdarkly/js-server-sdk-common';
33

4-
import * as crypto from 'crypto';
4+
import { createHash, createHmac, randomUUID } from 'crypto';
55

66
export default class NodeCrypto implements platform.Crypto {
77
createHash(algorithm: string): platform.Hasher {
8-
return crypto.createHash(algorithm);
8+
return createHash(algorithm);
99
}
1010

1111
createHmac(algorithm: string, key: string): platform.Hmac {
12-
return crypto.createHmac(algorithm, key);
12+
return createHmac(algorithm, key);
13+
}
14+
15+
randomUUID() {
16+
return randomUUID();
1317
}
1418
}

packages/shared/common/__tests__/internal/events/EventProcessor.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ describe('given an event processor', () => {
171171
// Not used for this test.
172172
throw new Error('Function not implemented.');
173173
},
174+
randomUUID(): string {
175+
// Not used for this test.
176+
throw new Error(`Function not implemented.`);
177+
},
174178
},
175179
requests: {
176180
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */

packages/shared/common/src/api/platform/Crypto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export interface Hmac extends Hasher {
2828
export interface Crypto {
2929
createHash(algorithm: string): Hasher;
3030
createHmac(algorithm: string, key: string): Hmac;
31+
randomUUID(): string;
3132
}

packages/shared/sdk-server/__tests__/BigSegmentsManager.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const crypto: Crypto = {
4242
// Not used for this test.
4343
throw new Error(`Function not implemented.${algorithm}${key}`);
4444
},
45+
randomUUID(): string {
46+
// Not used for this test.
47+
throw new Error(`Function not implemented.`);
48+
},
4549
};
4650

4751
describe.each(['STALE', 'HEALTHY'])('given a %s store', (status) => {

packages/shared/sdk-server/__tests__/LDClientImpl.bigSegments.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ const crypto: Crypto = {
4545
// Not used for this test.
4646
throw new Error(`Function not implemented.${algorithm}${key}`);
4747
},
48+
randomUUID(): string {
49+
// Not used for this test.
50+
throw new Error(`Function not implemented.`);
51+
},
4852
};
4953

5054
describe('given test data with big segments', () => {

packages/shared/sdk-server/__tests__/evaluation/Evaluator.segments.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ describe('when evaluating user equivalent contexts for segments', () => {
257257
// Not used for this test.
258258
throw new Error(`Function not implemented.${algorithm}${key}`);
259259
},
260+
randomUUID(): string {
261+
// Not used for this test.
262+
throw new Error(`Function not implemented.`);
263+
},
260264
};
261265

262266
const bucketingPlatform = { ...basicPlatform, crypto };

packages/shared/sdk-server/__tests__/evaluation/mocks/hasher.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const hasher: Hasher = {
77
digest: jest.fn(() => '1234567890123456'),
88
};
99

10+
let counter = 0;
11+
1012
export const crypto: Crypto = {
1113
createHash(algorithm: string): Hasher {
1214
expect(algorithm).toEqual('sha1');
@@ -16,4 +18,10 @@ export const crypto: Crypto = {
1618
// Not used for this test.
1719
throw new Error(`Function not implemented.${algorithm}${key}`);
1820
},
21+
randomUUID(): string {
22+
counter += 1;
23+
// Will provide a unique value for tests.
24+
// Very much not a UUID of course.
25+
return `${counter}`;
26+
},
1927
};

packages/shared/sdk-server/__tests__/events/EventProcessor.test.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import { nanoid } from 'nanoid';
32
import {
43
EventSource,
54
EventSourceInitDict,
@@ -13,6 +12,8 @@ import {
1312
ClientContext,
1413
Context,
1514
internal,
15+
Hmac,
16+
Hasher,
1617
} from '@launchdarkly/js-sdk-common';
1718
import DiagnosticsManager from '../../src/events/DiagnosticsManager';
1819
import Configuration from '../../src/options/Configuration';
@@ -29,11 +30,6 @@ interface RequestState {
2930
requestsMade: Array<{ url: string; options: Options }>;
3031
}
3132

32-
// Mock the nanoid module so we can replace the implementation in specific tests.
33-
jest.mock('nanoid', () => ({
34-
nanoid: jest.fn(() => jest.requireActual('nanoid').nanoid()),
35-
}));
36-
3733
function makePlatform(requestState: RequestState) {
3834
const info: Info = {
3935
platformData(): PlatformData {
@@ -114,14 +110,27 @@ function makePlatform(requestState: RequestState) {
114110
throw new Error('Function not implemented.');
115111
},
116112
};
117-
return { info, requests, waitForMessages };
113+
return {
114+
info,
115+
requests,
116+
waitForMessages,
117+
crypto: {
118+
createHash(algorithm: string): Hasher {
119+
// Not used for this test.
120+
throw new Error(`Function not implemented.${algorithm}`);
121+
},
122+
createHmac(algorithm: string, key: string): Hmac {
123+
// Not used for this test.
124+
throw new Error(`Function not implemented.${algorithm}${key}`);
125+
},
126+
randomUUID: () => '9-ypf7NswGfZ3CN2WpTix',
127+
},
128+
};
118129
}
119130

120131
const user = { key: 'userKey', name: 'Red' };
121132

122133
describe('given an event processor with diagnostics manager', () => {
123-
jest.mock('nanoid', () => ({ nanoid: () => '9-ypf7NswGfZ3CN2WpTix' }));
124-
125134
let eventProcessor: internal.EventProcessor;
126135

127136
const requestState: RequestState = {
@@ -145,12 +154,11 @@ describe('given an event processor with diagnostics manager', () => {
145154
let waitForMessages: (count: number) => Promise<number>;
146155

147156
beforeEach(() => {
148-
// @ts-ignore
149-
nanoid.mockImplementation(() => '9-ypf7NswGfZ3CN2WpTix');
150-
151157
const platform = makePlatform(requestState);
158+
152159
info = platform.info;
153160
requests = platform.requests;
161+
const { crypto } = platform;
154162
waitForMessages = platform.waitForMessages;
155163

156164
resetRequestState();
@@ -171,6 +179,7 @@ describe('given an event processor with diagnostics manager', () => {
171179
// Replace info and requests.
172180
info,
173181
requests,
182+
crypto,
174183
},
175184
store
176185
);

packages/shared/sdk-server/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"license": "Apache-2.0",
2323
"dependencies": {
2424
"@launchdarkly/js-sdk-common": "0.1.0",
25-
"nanoid": "^3.0.0",
2625
"semver": "^7.3.8"
2726
},
2827
"devDependencies": {

packages/shared/sdk-server/src/events/DiagnosticsManager.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { nanoid } from 'nanoid';
21
import { Platform } from '@launchdarkly/js-sdk-common';
32
import { LDFeatureStore } from '../api/subsystems';
43
import Configuration, { defaultValues } from '../options/Configuration';
@@ -103,7 +102,7 @@ export default class DiagnosticsManager {
103102
this.startTime = Date.now();
104103
this.dataSinceDate = this.startTime;
105104
this.id = {
106-
diagnosticId: nanoid(),
105+
diagnosticId: platform.crypto.randomUUID(),
107106
sdkKeySuffix: sdkKey.length > 6 ? sdkKey.substring(sdkKey.length - 6) : sdkKey,
108107
};
109108
}

0 commit comments

Comments
 (0)