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 2 commits
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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@ipld/dag-pb": "^2.1.18",
"@multiformats/blake2": "^1.0.11",
"browser-readablestream-to-it": "^2.0.4",
"idb": "^7.1.1",
"ipfs-unixfs-exporter": "https://gitpkg.now.sh/filecoin-saturn/js-ipfs-unixfs/packages/ipfs-unixfs-exporter?build",
"multiformats": "^12.1.1"
},
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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'
AmeanAsad marked this conversation as resolved.
Show resolved Hide resolved

class Saturn {
/**
Expand All @@ -12,6 +13,7 @@ class Saturn {
* @param {string} [opts.cdnURL=saturn.ms]
* @param {number} [opts.connectTimeout=5000]
* @param {number} [opts.downloadTimeout=0]
* @param {import('./utils/storage.js').Storage} [opts.storage]
*/
constructor (opts = {}) {
this.opts = Object.assign({}, {
Expand All @@ -20,9 +22,11 @@ class Saturn {
logURL: 'https://twb3qukm2i654i3tnvx36char40aymqq.lambda-url.us-west-2.on.aws/',
connectTimeout: 5_000,
downloadTimeout: 0

}, opts)

this.logs = []
this.storage = this.opts.storage || memoryStorage()
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
this.hasPerformanceAPI = typeof window !== 'undefined' && window?.performance
if (this.reportingLogs && this.hasPerformanceAPI) {
Expand Down
59 changes: 59 additions & 0 deletions src/utils/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @ts-check

import { openDB } from 'idb'

const DEFAULT_IDB_VERSION = 1
const DEFAULT_IDB_STORAGE_NAME = 'saturn-db'
const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'

/**
* @typedef {object} Storage
* @property {function():boolean} check - Checks if the provided Storage is accessible
* @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}
*/
export function indexedDbStorage () {
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
let dbPromise
if (indexedDbExists) {
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
upgrade (db) {
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
}
})
}

return {
check: () => Boolean(indexedDbExists),
guanzo marked this conversation as resolved.
Show resolved Hide resolved
get: async (key) => indexedDbExists && (await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key),
set: async (key, value) => indexedDbExists && (await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key),
delete: async (key) => indexedDbExists && (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
}
}

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

return {
check: () => true, // Memory storage is always accessible
get: (key) => Promise.resolve(storageObject[key]),
set: (key, value) => {
storageObject[key] = value
return Promise.resolve()
},
delete: (key) => {
delete storageObject[key]
return Promise.resolve()
}
}
}
AmeanAsad marked this conversation as resolved.
Show resolved Hide resolved
Loading