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: add storage interface + implement indexedDb #18

Merged
merged 8 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CID } from 'multiformats'
import { extractVerifiedContent } from './utils/car.js'
import { asAsyncIterable, asyncIteratorToBuffer } from './utils/itr.js'
import { randomUUID } from './utils/uuid.js'
import { memoryStorage } from './utils/storage.js'
import { memoryStorage } from './utils/storage/index.js'
guanzo marked this conversation as resolved.
Show resolved Hide resolved

class Saturn {
/**
Expand Down
17 changes: 17 additions & 0 deletions src/utils/storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// @ts-check

import { indexedDbStorage } from './indexed-db-storage.js'
import { memoryStorage } from './memory-storage.js'

/**
* @typedef {object} Storage
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key.
* @property {function(string,any):Promise<void>} set - Sets a new value for the key.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can it be any value? Is it serialised automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its not serialised automatically. I guess it could be any value but we have to add some extra handling for some storage implementations. For example localStorage only accepts strings as keys and values so what ever gets passed has to be serialisable as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gonna merge then, if we need to add support for non-string storage keys we can do that.

* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
*/

export {
indexedDbStorage,
memoryStorage
}
23 changes: 1 addition & 22 deletions src/utils/storage.js → src/utils/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ const DEFAULT_IDB_VERSION = 1
const DEFAULT_IDB_STORAGE_NAME = 'saturn-db'
const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'

/**
* @typedef {object} Storage
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key.
* @property {function(string,any):Promise<void>} set - Sets a new value for the key.
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
*/

/**
* @function indexedDbStorage
* @returns {Storage}
* @returns {import('./index.js').Storage}
*/
export function indexedDbStorage () {
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
Expand All @@ -34,17 +27,3 @@ export function indexedDbStorage () {
delete: async (key) => indexedDbExists && (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
}
}

/**
* @function memoryStorage
* @returns {Storage}
*/
export function memoryStorage () {
const storageObject = {}

return {
get: async (key) => storageObject[key],
set: async (key, value) => { storageObject[key] = value },
delete: async (key) => { delete storageObject[key] }
}
}
14 changes: 14 additions & 0 deletions src/utils/storage/memory-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

/**
* @function memoryStorage
* @returns {import('./index.js').Storage}
*/
export function memoryStorage () {
const storageObject = {}

return {
get: async (key) => storageObject[key],
set: async (key, value) => { storageObject[key] = value },
delete: async (key) => { delete storageObject[key] }
}
}
Loading