diff --git a/package-lock.json b/package-lock.json index 84f9b3f..e91ec1f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,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" }, @@ -2422,6 +2423,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/idb": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/idb/-/idb-7.1.1.tgz", + "integrity": "sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==" + }, "node_modules/ignore": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz", @@ -6246,6 +6252,11 @@ "has-symbols": "^1.0.2" } }, + "idb": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/idb/-/idb-7.1.1.tgz", + "integrity": "sha512-gchesWBzyvGHRO9W8tzUWFDycow5gwjvFKfyV9FF32Y7F50yZMp7mP+T2mJIWFx49zicqyC4uefHM17o6xKIVQ==" + }, "ignore": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.0.tgz", diff --git a/package.json b/package.json index 0970456..81d1899 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/index.js b/src/index.js index 4729a1a..8dcd443 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { /** @@ -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({}, { @@ -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) { diff --git a/src/storage/index.js b/src/storage/index.js new file mode 100644 index 0000000..a8b52b8 --- /dev/null +++ b/src/storage/index.js @@ -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} get - Retrieves the value associated with the key. + * @property {function(string,any):Promise} set - Sets a new value for the key. + * @property {function(string):Promise} delete - Deletes the value associated with the key. + */ + +export { + indexedDbStorage, + memoryStorage +} diff --git a/src/storage/indexed-db-storage.js b/src/storage/indexed-db-storage.js new file mode 100644 index 0000000..477ff1d --- /dev/null +++ b/src/storage/indexed-db-storage.js @@ -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) + } +} diff --git a/src/storage/memory-storage.js b/src/storage/memory-storage.js new file mode 100644 index 0000000..7a11c8e --- /dev/null +++ b/src/storage/memory-storage.js @@ -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] } + } +}