Skip to content

Commit

Permalink
Merge pull request #18 from filecoin-saturn/feat/storage-interface
Browse files Browse the repository at this point in the history
feat: add storage interface + implement indexedDb
  • Loading branch information
AmeanAsad authored Oct 11, 2023
2 parents 2570af8 + c6044df commit c93305f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
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 './storage/index.js'

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
16 changes: 16 additions & 0 deletions src/storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @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.
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
*/

export {
indexedDbStorage,
memoryStorage
}
29 changes: 29 additions & 0 deletions src/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @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'

/**
* @function indexedDbStorage
* @returns {import('./index.js').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 {
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)
}
}
15 changes: 15 additions & 0 deletions src/storage/memory-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check

/**
* @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] }
}
}

0 comments on commit c93305f

Please sign in to comment.