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: update storage initiation #54

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
26 changes: 20 additions & 6 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Saturn {
* @param {number} [config.fallbackLimit]
* @param {boolean} [config.experimental]
* @param {string} [config.format]
* @param {import('./storage/index.js').Storage} [config.storage]
* @param {function():Promise<import('./index.js').Storage>} [config.storage]
*/
constructor (config = {}) {
this.config = Object.assign({}, {
Expand All @@ -60,7 +60,7 @@ export class Saturn {
if (this.reportingLogs && this.hasPerformanceAPI) {
this._monitorPerformanceBuffer()
}
this.storage = this.config.storage || memoryStorage()
this._setStorage()
this.loadNodesPromise = this.config.experimental ? this._loadNodes(this.config) : null
this.authLimiter = pLimit(1)
}
Expand Down Expand Up @@ -226,6 +226,20 @@ export class Saturn {
return { res, controller, log }
}

async _setStorage () {
if (!this.config.storage) {
this.storage = await memoryStorage()
return
}

try {
const getStorage = this.config.storage
this.storage = await getStorage()
} catch (error) {
this.storage = await memoryStorage()
}
}

/**
* @param {Response} res
* @param {object} log
Expand Down Expand Up @@ -607,10 +621,10 @@ export class Saturn {
async _loadNodes (opts) {
let origin = opts.orchURL

let cacheNodesListPromise
if (this.storage) {
cacheNodesListPromise = this.storage.get(Saturn.nodesListKey)
if (!this.storage) {
await this._setStorage()
}
const cacheNodesListPromise = this.storage?.get(Saturn.nodesListKey)

origin = addHttpPrefix(origin)

Expand Down Expand Up @@ -645,7 +659,7 @@ export class Saturn {
nodes = await orchNodesListPromise
nodes = this._sortNodes(nodes)
this.nodes = nodes
this.storage.set(Saturn.nodesListKey, nodes)
this.storage?.set(Saturn.nodesListKey, nodes)
this.hashring = this.createHashring(nodes)
}
}
31 changes: 23 additions & 8 deletions src/storage/indexed-db-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@ const DEFAULT_SATURN_STORAGE_NAME = 'saturn-client'

/**
* @function indexedDbStorage
* @returns {import('./index.js').Storage}
* @returns {Promise<import('./index.js').Storage>}
*/
export function indexedDbStorage () {
const indexedDbExists = (typeof self !== 'undefined') && self?.indexedDB
export async function indexedDbStorage () {
const indexedDbExists = typeof self !== 'undefined' && self?.indexedDB
let dbPromise

if (!indexedDbExists) {
throw Error('Indexed DB is not supported in this environment')
}

if (indexedDbExists) {
AmeanAsad marked this conversation as resolved.
Show resolved Hide resolved
dbPromise = openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
dbPromise = await openDB(DEFAULT_IDB_STORAGE_NAME, DEFAULT_IDB_VERSION, {
upgrade (db) {
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
try {
db.createObjectStore(DEFAULT_SATURN_STORAGE_NAME)
} catch (error) {
throw Error(`Cannot initialize indexed DB Object store, error: ${error}`)
}
}
})
}

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)
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)
}
}
4 changes: 2 additions & 2 deletions src/storage/memory-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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

return {
Expand Down
33 changes: 21 additions & 12 deletions test/fallback.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ describe('Client Fallback', () => {

const expectedNodes = generateNodes(2, TEST_ORIGIN_DOMAIN)

// Mocking storage object
const mockStorage = {
get: async (key) => null,
set: async (key, value) => null,
delete: async (key) => null
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
// Mocking storage object
const storage = async () => mockStorage
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ storage: mockStorage, ...options })
const saturn = new Saturn({ storage, ...options })

// Mocking options
const mockOpts = { orchURL: TEST_DEFAULT_ORCH }

await saturn._loadNodes(mockOpts)

// Assert that all the storage methods were called twice.
Expand Down Expand Up @@ -98,7 +97,8 @@ describe('Client Fallback', () => {
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ storage: mockStorage, ...options })
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })

// Mocking options
const mockOpts = { orchURL: TEST_DEFAULT_ORCH }
Expand Down Expand Up @@ -137,7 +137,8 @@ describe('Client Fallback', () => {
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ storage: mockStorage, ...options })
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })

const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4')

Expand Down Expand Up @@ -167,10 +168,13 @@ describe('Client Fallback', () => {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}

t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ ...options })
const storage = async () => mockStorage

const saturn = new Saturn({ storage, ...options })

const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4', { raceNodes: true })

Expand Down Expand Up @@ -203,7 +207,9 @@ describe('Client Fallback', () => {
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ ...options })
const storage = async () => mockStorage

const saturn = new Saturn({ storage, ...options })

await saturn.loadNodesPromise

Expand Down Expand Up @@ -242,7 +248,8 @@ describe('Client Fallback', () => {
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ storage: mockStorage, ...options })
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })

const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4', { raceNodes: true })

Expand Down Expand Up @@ -336,7 +343,9 @@ describe('Client Fallback', () => {
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')

const saturn = new Saturn({ storage: mockStorage, customerFallbackURL: TEST_CUSTOMER_ORIGIN, ...options })
const storage = async () => mockStorage

const saturn = new Saturn({ storage, customerFallbackURL: TEST_CUSTOMER_ORIGIN, ...options })

const cid = saturn.fetchContentWithFallback(TEST_CUSTOMER_ORIGIN, { raceNodes: true })

Expand Down
Loading