Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds ping stream support #624

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
76 changes: 50 additions & 26 deletions packages/sdk/browser/__tests__/BrowserDataManager.test.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: Updating paths to avoid confusing future developers about usage of /msdk

Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,24 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
browserConfig,
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/contexts/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/meval/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
baseHeaders,
Expand All @@ -176,18 +182,24 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
validateOptions({ streaming: true }, logger),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/contexts/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/meval/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
baseHeaders,
Expand All @@ -214,18 +226,24 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
validateOptions({ streaming: true }, logger),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/contexts/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/meval/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
baseHeaders,
Expand All @@ -241,7 +259,7 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
await dataManager.identify(identifyResolve, identifyReject, context, identifyOptions);

expect(platform.requests.createEventSource).toHaveBeenCalledWith(
'/meval/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlciJ9?h=potato&withReasons=true',
'/path/get/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlciJ9?h=potato&withReasons=true',
expect.anything(),
);
});
Expand All @@ -255,18 +273,24 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
validateOptions({ streaming: false }, logger),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/contexts/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
return `/meval/${base64UrlEncode(_plainContextString, encoding)}`;
return `/path/get/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(encoding: Encoding, _plainContextString: string): string {
return `/path/report/${base64UrlEncode(_plainContextString, encoding)}`;
},
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
pathPing(encoding: Encoding, _plainContextString: string): string {
return `/path/ping/${base64UrlEncode(_plainContextString, encoding)}`;
},
}),
baseHeaders,
Expand All @@ -282,7 +306,7 @@ describe('given a BrowserDataManager with mocked dependencies', () => {
await dataManager.identify(identifyResolve, identifyReject, context, identifyOptions);

expect(platform.requests.fetch).toHaveBeenCalledWith(
'/msdk/evalx/contexts/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlciJ9?withReasons=true&h=potato',
'/path/get/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlciJ9?withReasons=true&h=potato',
expect.anything(),
);
});
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/browser/src/BrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ export class BrowserClient extends LDClientImpl implements LDClient {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/sdk/evalx/${clientSideId}/context`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
// Note: if you are seeing this error, it is a coding error. This DataSourcePaths implementation is for polling endpoints. /ping is not currently
// used in a polling situation. It is probably the case that this was called by streaming logic erroneously.
throw new Error('Ping for polling unsupported.');
},
tanderson-ld marked this conversation as resolved.
Show resolved Hide resolved
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
Expand All @@ -148,6 +153,9 @@ export class BrowserClient extends LDClientImpl implements LDClient {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/eval/${clientSideId}`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
return `/ping/${clientSideId}`;
},
}),
baseHeaders,
emitter,
Expand Down
75 changes: 40 additions & 35 deletions packages/sdk/browser/src/BrowserDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
DataSourcePaths,
DataSourceState,
FlagManager,
getPollingUri,
internal,
LDEmitter,
LDHeaders,
LDIdentifyOptions,
makeRequestor,
Platform,
Requestor,
} from '@launchdarkly/js-client-sdk-common';

import { readFlagsFromBootstrap } from './bootstrap';
Expand Down Expand Up @@ -92,23 +91,35 @@ export default class BrowserDataManager extends BaseDataManager {
if (await this.flagManager.loadCached(context)) {
this._debugLog('Identify - Flags loaded from cache. Continuing to initialize via a poll.');
}
const plainContextString = JSON.stringify(Context.toLDContext(context));
const requestor = this._getRequestor(plainContextString);
await this._finishIdentifyFromPoll(requestor, context, identifyResolve, identifyReject);
}

await this._finishIdentifyFromPoll(context, identifyResolve, identifyReject);
}
this._updateStreamingState();
}

private async _finishIdentifyFromPoll(
requestor: Requestor,
context: Context,
identifyResolve: () => void,
identifyReject: (err: Error) => void,
) {
try {
this.dataSourceStatusManager.requestStateUpdate(DataSourceState.Initializing);
const payload = await requestor.requestPayload();

const plainContextString = JSON.stringify(Context.toLDContext(context));
const pollingRequestor = makeRequestor(
plainContextString,
this.config.serviceEndpoints,
this.getPollingPaths(),
this.platform.requests,
this.platform.encoding!,
this.baseHeaders,
[],
this.config.withReasons,
this.config.useReport,
this._secureModeHash,
);

const payload = await pollingRequestor.requestPayload();
try {
const listeners = this.createStreamListeners(context, identifyResolve);
const putListener = listeners.get('put');
Expand Down Expand Up @@ -196,35 +207,29 @@ export default class BrowserDataManager extends BaseDataManager {
const rawContext = Context.toLDContext(context)!;

this.updateProcessor?.close();
this.createStreamingProcessor(rawContext, context, identifyResolve, identifyReject);

this.updateProcessor!.start();
}

private _getRequestor(plainContextString: string): Requestor {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers: _getRequestor got commonized to be a helper method in the Requestor file, the generated diff is confusing.

const paths = this.getPollingPaths();
const path = this.config.useReport
? paths.pathReport(this.platform.encoding!, plainContextString)
: paths.pathGet(this.platform.encoding!, plainContextString);

const parameters: { key: string; value: string }[] = [];
if (this.config.withReasons) {
parameters.push({ key: 'withReasons', value: 'true' });
}
if (this._secureModeHash) {
parameters.push({ key: 'h', value: this._secureModeHash });
}
const plainContextString = JSON.stringify(Context.toLDContext(context));
const pollingRequestor = makeRequestor(
plainContextString,
this.config.serviceEndpoints,
this.getPollingPaths(), // note: this is the polling path because the requestor is only used to make polling requests.
tanderson-ld marked this conversation as resolved.
Show resolved Hide resolved
this.platform.requests,
this.platform.encoding!,
this.baseHeaders,
[],
this.config.withReasons,
this.config.useReport,
this._secureModeHash,
);

const headers: { [key: string]: string } = { ...this.baseHeaders };
let body;
let method = 'GET';
if (this.config.useReport) {
method = 'REPORT';
headers['content-type'] = 'application/json';
body = plainContextString; // context is in body for REPORT
}
this.createStreamingProcessor(
rawContext,
context,
pollingRequestor,
identifyResolve,
identifyReject,
);

const uri = getPollingUri(this.config.serviceEndpoints, path, parameters);
return new Requestor(this.platform.requests, uri, headers, method, body);
this.updateProcessor!.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ describe('given a MobileDataManager with mocked dependencies', () => {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
throw new Error('Ping for polling unsupported.');
tanderson-ld marked this conversation as resolved.
Show resolved Hide resolved
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
Expand All @@ -140,6 +143,9 @@ describe('given a MobileDataManager with mocked dependencies', () => {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
return `/mping`;
},
}),
baseHeaders,
emitter,
Expand Down
30 changes: 28 additions & 2 deletions packages/sdk/react-native/src/MobileDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
LDEmitter,
LDHeaders,
LDIdentifyOptions,
makeRequestor,
Platform,
} from '@launchdarkly/js-client-sdk-common';

Expand Down Expand Up @@ -95,13 +96,38 @@ export default class MobileDataManager extends BaseDataManager {
) {
const rawContext = Context.toLDContext(context)!;

const plainContextString = JSON.stringify(Context.toLDContext(context));
const requestor = makeRequestor(
plainContextString,
this.config.serviceEndpoints,
this.getPollingPaths(), // note: this is the polling path because the requestor is only used to make polling requests.
this.platform.requests,
this.platform.encoding!,
this.baseHeaders,
[],
this.config.useReport,
this.config.withReasons,
);

this.updateProcessor?.close();
switch (this.connectionMode) {
case 'streaming':
this.createStreamingProcessor(rawContext, context, identifyResolve, identifyReject);
this.createStreamingProcessor(
rawContext,
context,
requestor,
identifyResolve,
identifyReject,
);
break;
case 'polling':
this.createPollingProcessor(rawContext, context, identifyResolve, identifyReject);
this.createPollingProcessor(
rawContext,
context,
requestor,
identifyResolve,
identifyReject,
);
break;
default:
break;
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/react-native/src/ReactNativeLDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export default class ReactNativeLDClient extends LDClientImpl {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/msdk/evalx/context`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
// Note: if you are seeing this error, it is a coding error. This DataSourcePaths implementation is for polling endpoints. /ping is not currently
// used in a polling situation. It is probably the case that this was called by streaming logic erroneously.
throw new Error('Ping for polling unsupported.');
},
}),
() => ({
pathGet(encoding: Encoding, _plainContextString: string): string {
Expand All @@ -97,6 +102,9 @@ export default class ReactNativeLDClient extends LDClientImpl {
pathReport(_encoding: Encoding, _plainContextString: string): string {
return `/meval`;
},
pathPing(_encoding: Encoding, _plainContextString: string): string {
return `/mping`;
},
}),
baseHeaders,
emitter,
Expand Down
5 changes: 3 additions & 2 deletions packages/shared/sdk-client/__tests__/LDClientImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ describe('sdk-client object', () => {
const carContext: LDContext = { kind: 'car', key: 'test-car' };

await expect(ldc.identify(carContext)).rejects.toThrow('test-error');
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(expect.stringMatching(/^error:.*test-error/));
expect(logger.error).toHaveBeenCalledTimes(2);
expect(logger.error).toHaveBeenNthCalledWith(1, expect.stringMatching(/^error:.*test-error/));
expect(logger.error).toHaveBeenNthCalledWith(2, expect.stringContaining('Received error 404'));
});

test('identify change and error listeners', async () => {
Expand Down
Loading
Loading