Skip to content

Commit c93305f

Browse files
authored
Merge pull request #18 from filecoin-saturn/feat/storage-interface
feat: add storage interface + implement indexedDb
2 parents 2570af8 + c6044df commit c93305f

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@ipld/dag-pb": "^2.1.18",
2929
"@multiformats/blake2": "^1.0.11",
3030
"browser-readablestream-to-it": "^2.0.4",
31+
"idb": "^7.1.1",
3132
"ipfs-unixfs-exporter": "https://gitpkg.now.sh/filecoin-saturn/js-ipfs-unixfs/packages/ipfs-unixfs-exporter?build",
3233
"multiformats": "^12.1.1"
3334
},

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CID } from 'multiformats'
33
import { extractVerifiedContent } from './utils/car.js'
44
import { asAsyncIterable, asyncIteratorToBuffer } from './utils/itr.js'
55
import { randomUUID } from './utils/uuid.js'
6+
import { memoryStorage } from './storage/index.js'
67

78
class Saturn {
89
/**
@@ -12,6 +13,7 @@ class Saturn {
1213
* @param {string} [opts.cdnURL=saturn.ms]
1314
* @param {number} [opts.connectTimeout=5000]
1415
* @param {number} [opts.downloadTimeout=0]
16+
* @param {import('./utils/storage.js').Storage} [opts.storage]
1517
*/
1618
constructor (opts = {}) {
1719
this.opts = Object.assign({}, {
@@ -20,9 +22,11 @@ class Saturn {
2022
logURL: 'https://twb3qukm2i654i3tnvx36char40aymqq.lambda-url.us-west-2.on.aws/',
2123
connectTimeout: 5_000,
2224
downloadTimeout: 0
25+
2326
}, opts)
2427

2528
this.logs = []
29+
this.storage = this.opts.storage || memoryStorage()
2630
this.reportingLogs = process?.env?.NODE_ENV !== 'development'
2731
this.hasPerformanceAPI = typeof window !== 'undefined' && window?.performance
2832
if (this.reportingLogs && this.hasPerformanceAPI) {

src/storage/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-check
2+
3+
import { indexedDbStorage } from './indexed-db-storage.js'
4+
import { memoryStorage } from './memory-storage.js'
5+
6+
/**
7+
* @typedef {object} Storage
8+
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key.
9+
* @property {function(string,any):Promise<void>} set - Sets a new value for the key.
10+
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
11+
*/
12+
13+
export {
14+
indexedDbStorage,
15+
memoryStorage
16+
}

src/storage/indexed-db-storage.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @ts-check
2+
3+
import { openDB } from 'idb'
4+
5+
const DEFAULT_IDB_VERSION = 1
6+
const DEFAULT_IDB_STORAGE_NAME = 'saturn-db'
7+
const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'
8+
9+
/**
10+
* @function indexedDbStorage
11+
* @returns {import('./index.js').Storage}
12+
*/
13+
export function indexedDbStorage () {
14+
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
15+
let dbPromise
16+
if (indexedDbExists) {
17+
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
18+
upgrade (db) {
19+
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
20+
}
21+
})
22+
}
23+
24+
return {
25+
get: async (key) => indexedDbExists && (await dbPromise).get(DEFAULT_SATURN_STORAGE_NAME, key),
26+
set: async (key, value) => indexedDbExists && (await dbPromise).put(DEFAULT_SATURN_STORAGE_NAME, value, key),
27+
delete: async (key) => indexedDbExists && (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
28+
}
29+
}

src/storage/memory-storage.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @ts-check
2+
3+
/**
4+
* @function memoryStorage
5+
* @returns {import('./index.js').Storage}
6+
*/
7+
export function memoryStorage () {
8+
const storageObject = {}
9+
10+
return {
11+
get: async (key) => storageObject[key],
12+
set: async (key, value) => { storageObject[key] = value },
13+
delete: async (key) => { delete storageObject[key] }
14+
}
15+
}

0 commit comments

Comments
 (0)