Skip to content

Commit cd65d71

Browse files
committed
feat: turn storage into modules
1 parent 327feac commit cd65d71

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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 './utils/storage.js'
6+
import { memoryStorage } from './utils/storage/index.js'
77

88
class Saturn {
99
/**

src/utils/storage/index.js

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

src/utils/storage.js renamed to src/utils/storage/indexed-db-storage.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ const DEFAULT_IDB_VERSION = 1
66
const DEFAULT_IDB_STORAGE_NAME = 'saturn-db'
77
const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'
88

9-
/**
10-
* @typedef {object} Storage
11-
* @property {function(string):Promise<any>} get - Retrieves the value associated with the key.
12-
* @property {function(string,any):Promise<void>} set - Sets a new value for the key.
13-
* @property {function(string):Promise<any>} delete - Deletes the value associated with the key.
14-
*/
15-
169
/**
1710
* @function indexedDbStorage
18-
* @returns {Storage}
11+
* @returns {import('./index.js').Storage}
1912
*/
2013
export function indexedDbStorage () {
2114
const indexedDbExists = (typeof window !== 'undefined') && window?.indexedDB
@@ -34,17 +27,3 @@ export function indexedDbStorage () {
3427
delete: async (key) => indexedDbExists && (await dbPromise).delete(DEFAULT_SATURN_STORAGE_NAME, key)
3528
}
3629
}
37-
38-
/**
39-
* @function memoryStorage
40-
* @returns {Storage}
41-
*/
42-
export function memoryStorage () {
43-
const storageObject = {}
44-
45-
return {
46-
get: async (key) => storageObject[key],
47-
set: async (key, value) => { storageObject[key] = value },
48-
delete: async (key) => { delete storageObject[key] }
49-
}
50-
}

src/utils/storage/memory-storage.js

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

0 commit comments

Comments
 (0)