Skip to content

feat: Scaffold browser client. #579

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

Merged
merged 32 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
53af412
feat: Implement support for browser requests.
kinyoklion Sep 10, 2024
bf164dd
feat: Add support for conditional event source capabilities.
kinyoklion Sep 10, 2024
8307b03
Add more comments.
kinyoklion Sep 10, 2024
c23bf45
Implement for edge SDKs.
kinyoklion Sep 10, 2024
a084b9a
Lint
kinyoklion Sep 10, 2024
6acc5f4
Add getEventSourceCapabilities to mocks.
kinyoklion Sep 10, 2024
b89e14e
Test updates.
kinyoklion Sep 10, 2024
5becb5b
More test updates.
kinyoklion Sep 10, 2024
8f63e45
More tests.
kinyoklion Sep 10, 2024
1c5450f
More test updates.
kinyoklion Sep 10, 2024
f0a51c6
feat: Implement browser platform.
kinyoklion Sep 10, 2024
39355b8
Merge branch 'rlamb/sc-254415/support-event-source-capabilities' into…
kinyoklion Sep 10, 2024
d259791
Rename browser event source.
kinyoklion Sep 10, 2024
679b813
Add backoff tests.
kinyoklion Sep 10, 2024
0434103
Enable encoding.
kinyoklion Sep 10, 2024
7c66b7b
Move to rollup.
kinyoklion Sep 11, 2024
a9ae9ac
Merge branch 'main' into rlamb/sc-254415/implement-browser-requests
kinyoklion Sep 11, 2024
fd8d7d6
Validate package.json imports.
kinyoklion Sep 11, 2024
61a3631
Configurable reset time.
kinyoklion Sep 11, 2024
0cb22bf
feat: Scaffold browser client.
kinyoklion Sep 11, 2024
af341cb
Implements platform.
kinyoklion Sep 11, 2024
f70e901
Merge branch 'rlamb/sc-254415/implement-browser-requests' into rlamb/…
kinyoklion Sep 11, 2024
3900a13
feat: Scaffold browser client.
kinyoklion Sep 11, 2024
ff1dc23
Correct fetch.
kinyoklion Sep 11, 2024
e64cc34
Merge branch 'rlamb/sc-254415/implement-browser-requests' into rlamb/…
kinyoklion Sep 11, 2024
a1ad827
feat: Allow using custom user-agent name.
kinyoklion Sep 12, 2024
cf3abae
Merge branch 'main' into rlamb/sc-255716/support-x-launchdarkly-user-…
kinyoklion Sep 12, 2024
745e994
Remove unused imports.
kinyoklion Sep 12, 2024
5126f93
Event processor should use the default headers.
kinyoklion Sep 12, 2024
b58fc09
Merge branch 'rlamb/sc-255716/support-x-launchdarkly-user-agent' into…
kinyoklion Sep 12, 2024
a6e1126
Set user agent header name.
kinyoklion Sep 12, 2024
ba134f1
Merge branch 'main' into rlamb/sc-255762/scaffold-browser-client
kinyoklion Sep 13, 2024
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
43 changes: 43 additions & 0 deletions packages/sdk/browser/src/BrowserClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
AutoEnvAttributes,
base64UrlEncode,
LDClient as CommonClient,
LDClientImpl,
LDContext,
LDOptions,
} from '@launchdarkly/js-client-sdk-common';

import BrowserPlatform from './platform/BrowserPlatform';

/**
* We are not supporting dynamically setting the connection mode on the LDClient.
*/
export type LDClient = Omit<CommonClient, 'setConnectionMode'>;

export class BrowserClient extends LDClientImpl {
constructor(
private readonly clientSideId: string,
autoEnvAttributes: AutoEnvAttributes,
options: LDOptions = {},
) {
super(clientSideId, autoEnvAttributes, new BrowserPlatform(options), options, {
analyticsEventPath: `/events/bulk/${clientSideId}`,
diagnosticEventPath: `/events/diagnostic/${clientSideId}`,
includeAuthorizationHeader: false,
highTimeoutThreshold: 5,
userAgentHeaderName: 'x-launchdarkly-user-agent',
});
}

private encodeContext(context: LDContext) {
return base64UrlEncode(JSON.stringify(context), this.platform.encoding!);
}

override createStreamUriPath(context: LDContext) {
return `/eval/${this.clientSideId}/${this.encodeContext(context)}`;
}

override createPollUriPath(context: LDContext): string {
return `/sdk/evalx/${this.clientSideId}/contexts/${this.encodeContext(context)}`;
}
}
42 changes: 34 additions & 8 deletions packages/sdk/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import BrowserInfo from './platform/BrowserInfo';
import DefaultBrowserEventSource from './platform/DefaultBrowserEventSource';
import {
AutoEnvAttributes,
LDContext,
LDContextCommon,
LDContextMeta,
LDFlagSet,
LDLogger,
LDLogLevel,
LDMultiKindContext,
LDOptions,
LDSingleKindContext,
} from '@launchdarkly/js-client-sdk-common';

// Temporary exports for testing in a browser.
export { DefaultBrowserEventSource, BrowserInfo };
export * from '@launchdarkly/js-client-sdk-common';
import { BrowserClient, LDClient } from './BrowserClient';

export function Hello() {
// eslint-disable-next-line no-console
console.log('HELLO');
// TODO: Export and use browser specific options.
export {
LDClient,
AutoEnvAttributes,
LDOptions,
LDFlagSet,
LDContext,
LDContextCommon,
LDContextMeta,
LDMultiKindContext,
LDSingleKindContext,
LDLogLevel,
LDLogger,
};

export function init(
clientSideId: string,
autoEnvAttributes: AutoEnvAttributes,
options?: LDOptions,
): LDClient {
return new BrowserClient(clientSideId, autoEnvAttributes, options);
}