-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[PM-19479] Client-Managed SDK state definition #14839
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
Open
dani-garcia
wants to merge
14
commits into
main
Choose a base branch
from
ps/PM-19479-sdk-state-traits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
โ0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
78b7dd5
[PM-19479] Client-Managed SDK state definition
dani-garcia 083fe18
Remove test code
dani-garcia 860ce4f
Merge branch 'main' into ps/PM-19479-sdk-state-traits
dani-garcia 70d0b5c
Update based on latest sdk
dani-garcia db61e90
Merge branch 'main' into ps/PM-19479-sdk-state-traits
coroiu 5802b55
Merge branch 'main' into ps/PM-19479-sdk-state-traits
dani-garcia 7d087a2
Add DB config
dani-garcia e355048
Merge branch 'main' into ps/PM-19479-sdk-state-traits
dani-garcia 2687188
Remove uuid conversion step
dani-garcia 60200bf
Move mapper into separate file
dani-garcia ad61477
Merge branch 'main' into ps/PM-19479-sdk-state-traits
dani-garcia d4968a6
Revert to client managed state
dani-garcia 55af819
Move mapper to Cipher
dani-garcia 2100ced
Typo
dani-garcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
libs/common/src/platform/services/sdk/client-managed-state.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { firstValueFrom, map } from "rxjs"; | ||
|
||
import { UserId } from "@bitwarden/common/types/guid"; | ||
import { CipherRecordMapper } from "@bitwarden/common/vault/models/domain/cipher"; | ||
import { StateClient, Repository } from "@bitwarden/sdk-internal"; | ||
|
||
import { StateProvider, UserKeyDefinition } from "../../state"; | ||
|
||
export async function initializeState( | ||
userId: UserId, | ||
stateClient: StateClient, | ||
stateProvider: StateProvider, | ||
): Promise<void> { | ||
await stateClient.register_cipher_repository( | ||
new RepositoryRecord(userId, stateProvider, new CipherRecordMapper()), | ||
); | ||
} | ||
|
||
export interface SdkRecordMapper<ClientType, SdkType> { | ||
userKeyDefinition(): UserKeyDefinition<Record<string, ClientType>>; | ||
toSdk(value: ClientType): SdkType; | ||
fromSdk(value: SdkType): ClientType; | ||
} | ||
|
||
class RepositoryRecord<ClientType, SdkType> implements Repository<SdkType> { | ||
constructor( | ||
private userId: UserId, | ||
private stateProvider: StateProvider, | ||
private mapper: SdkRecordMapper<ClientType, SdkType>, | ||
) {} | ||
|
||
async get(id: string): Promise<SdkType | null> { | ||
const prov = this.stateProvider.getUser(this.userId, this.mapper.userKeyDefinition()); | ||
const data = await firstValueFrom(prov.state$.pipe(map((data) => data ?? {}))); | ||
const element = data[id]; | ||
if (!element) { | ||
return null; | ||
} | ||
return this.mapper.toSdk(element); | ||
} | ||
|
||
async list(): Promise<SdkType[]> { | ||
const prov = this.stateProvider.getUser(this.userId, this.mapper.userKeyDefinition()); | ||
const elements = await firstValueFrom(prov.state$.pipe(map((data) => data ?? {}))); | ||
return Object.values(elements).map((element) => this.mapper.toSdk(element)); | ||
} | ||
|
||
async set(id: string, value: SdkType): Promise<void> { | ||
const prov = this.stateProvider.getUser(this.userId, this.mapper.userKeyDefinition()); | ||
const elements = await firstValueFrom(prov.state$.pipe(map((data) => data ?? {}))); | ||
elements[id] = this.mapper.fromSdk(value); | ||
await prov.update(() => elements); | ||
} | ||
|
||
async remove(id: string): Promise<void> { | ||
const prov = this.stateProvider.getUser(this.userId, this.mapper.userKeyDefinition()); | ||
const elements = await firstValueFrom(prov.state$.pipe(map((data) => data ?? {}))); | ||
if (!elements[id]) { | ||
return; | ||
} | ||
delete elements[id]; | ||
await prov.update(() => elements); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐จ non blocking and could always be done in the future: I probably would put this into a separate file only to keep this file dedicated to
Cipher
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1