browser-credentials.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type { Context } from '@deepseek-ai/cordis'
  2. import type { CredentialProvider, CredentialRecord } from '@deepseek-ai/dsh-credentials'
  3. /** Mutable credential-record double for Connection authentication tests. */
  4. export class RecordCredentials {
  5. record: CredentialRecord | undefined
  6. discardWrites = false
  7. reads = 0
  8. modifies = 0
  9. readRecord(): Promise<CredentialRecord | undefined> {
  10. this.reads += 1
  11. return Promise.resolve(this.record)
  12. }
  13. async modifyRecord(
  14. _key: unknown,
  15. mutate: (current: CredentialRecord | undefined) => Promise<CredentialRecord | undefined>,
  16. ): Promise<CredentialRecord | undefined> {
  17. this.modifies += 1
  18. const next = await mutate(this.record)
  19. if (this.discardWrites) return undefined
  20. if (next !== undefined) this.record = next
  21. return this.record
  22. }
  23. deleteRecord(): Promise<void> {
  24. this.record = undefined
  25. return Promise.resolve()
  26. }
  27. }
  28. /** Provide the record operations Connection needs during authentication setup. */
  29. export function provideBrowserCredentials(ctx: Context): void {
  30. ctx.provide('credentials', new RecordCredentials() as unknown as CredentialProvider)
  31. }