Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
don-frazier committed Nov 14, 2024
1 parent efe6a9f commit f44315e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/storage/session/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface EventMap<Keys> {
updated: KeyEvent<Keys>
deleted: KeyEvent<Keys>
expired: KeyEvent<Keys>
persisting: KeyEvent<Keys>
}

export type EventTypes<Keys> = keyof EventMap<Keys>
Expand All @@ -62,6 +63,7 @@ export class SessionKeystore<Keys = string> {
#emitter: Emitter
#store: Map<Keys, ExpirableKeyV1>
#timeouts: Map<Keys, any>
readonly id: number

// --

Expand All @@ -74,6 +76,7 @@ export class SessionKeystore<Keys = string> {
}
}) {
this.name = opts.name || 'default'
this.id = Math.floor(Math.random() * 1000000)
this.#storageKey = `session-keystore:${this.name}`
this.#emitter = mitt()
this.#store = new Map()
Expand Down Expand Up @@ -102,6 +105,7 @@ export class SessionKeystore<Keys = string> {
// API --

set(key: Keys, value: string, expiresAt?: Date | number) {
console.log('key', key)
let d: number | undefined
if (expiresAt !== undefined) {
d = typeof expiresAt === 'number' ? expiresAt : expiresAt.valueOf()
Expand All @@ -111,20 +115,24 @@ export class SessionKeystore<Keys = string> {
value,
expiresAt: d
}
console.log('newItem', newItem)
const oldItem = this.#store.get(key)
console.log('oldItem', oldItem)
this.#store.set(key, newItem)
if (this._setTimeout(key) === 'expired') {
return // Don't call created or updated
}
if (!oldItem) {
this.#emitter.emit('created', { name: key })
this.#emitter.emit('created', { name: key, value: newItem.value })
} else if (oldItem.value !== newItem.value) {
this.#emitter.emit('updated', { name: key })
}
}

get(key: Keys, now = Date.now()) {
console.log('get', key)
const item = this.#store.get(key)
console.log('item', item)
if (!item) {
return null
}
Expand All @@ -149,6 +157,7 @@ export class SessionKeystore<Keys = string> {
// --

persist() {
this.#emitter.emit('persisting', { name: this.name });
/* istanbul ignore next */
if (typeof window === 'undefined') {
throw new Error(
Expand Down
3 changes: 1 addition & 2 deletions src/storage/session/utility.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import crypto from 'crypto' // Node.js crypto module
import { encodeBase64url, utf8Decoder, utf8Encoder } from '../../utils'
import { decodeBase64url, encodeBase64url, utf8Decoder, utf8Encoder } from '../../utils'
import { base64UrlToBytes, bytesToBase64url } from '../../utils/encoding';
import { decodeBase64url } from 'did-jwt/lib/util';

const randomBytes = (length: number): Uint8Array => {
if (typeof window === 'undefined') {
Expand Down

0 comments on commit f44315e

Please sign in to comment.