From 7f96039cfef17458a1412d18dc53a33b8d2f8df5 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Thu, 26 Oct 2023 14:23:50 +0200 Subject: [PATCH] Add hasDiskAccess config --- README.md | 49 ++++++++++++++++++++++++----------------------- indexes/plugin.js | 15 ++++++++------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 755dcc83..8bdcc0c1 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Over time, this database received more features than ssb-db, and now supports: - Deletion and compaction - Customizable feed formats and encryption formats - You are not tied to classic SSB messages and the classic mode of encryption, - you can use any format you want, or build one yourself, with [ssb-feed-format] - and [ssb-encryption-format] + you can use any format you want, or build one yourself, with [ssb-feed-format] + and [ssb-encryption-format] - By default supports [ssb-classic] - Query language (as composable JS functions) @@ -487,29 +487,24 @@ decrypted values. Example of getting existing post messages together with newly decrypted ones: -``` js - const pull = require('pull-stream') - const cat = require('pull-cat') +```js +const pull = require('pull-stream') +const cat = require('pull-cat') +pull( + cat([ + sbot2.db.query(where(type('post')), toPullStream()), pull( - cat([ - sbot2.db.query( - where(type('post')), - toPullStream() - ), - pull( - sbot2.db.reindexed(), - pull.filter((msg) => { - return msg.value.content.type === 'post' - }) - ) - ]), - pull.drain( - (result) => { - console.log("got a new post", result.value) - } - ) - ) + sbot2.db.reindexed(), + pull.filter((msg) => { + return msg.value.content.type === 'post' + }) + ), + ]), + pull.drain((result) => { + console.log('got a new post', result.value) + }) +) ``` ### add(nativeMsg, cb) @@ -612,7 +607,7 @@ const status = ssb.db2.getStatus() // observeable const listener = (status) => console.log(status.progress) -const unsubscribe = ssb.db2.getStatus(listener) +const unsubscribe = ssb.db2.getStatus(listener) // later unsubscribe() // unsubscribes the listener from status updates @@ -695,6 +690,12 @@ const config = { */ dangerouslyKillFlumeWhenMigrated: false, + /** + * If the environment that db2 runs in has access to a regular disk to write to. Normally detected automatically but in environments like electron you might have to set this manually. + * Default in node: true, default in a browser: false + */ + hasDiskAccess: true + /** * Only try to decrypt box1 messages created after this date * Default: null diff --git a/indexes/plugin.js b/indexes/plugin.js index bbe9b293..a817df28 100644 --- a/indexes/plugin.js +++ b/indexes/plugin.js @@ -11,13 +11,9 @@ const Debug = require('debug') const DeferredPromise = require('p-defer') const { indexesPath } = require('../defaults') -const notInABrowser = typeof window === 'undefined' +let hasDiskAccess let rimraf let mkdirp -if (notInABrowser) { - rimraf = require('rimraf') - mkdirp = require('mkdirp') -} function thenMaybeReportError(err) { if (err) console.error(err) @@ -25,6 +21,11 @@ function thenMaybeReportError(err) { module.exports = class Plugin { constructor(log, dir, name, version, keyEncoding, valueEncoding, configDb2) { + hasDiskAccess = (configDb2 && configDb2.hasDiskAccess) || typeof window === 'undefined' + if (hasDiskAccess) { + rimraf = require('rimraf') + mkdirp = require('mkdirp') + } this.log = log this.name = name this.levelPutListeners = [] @@ -35,7 +36,7 @@ module.exports = class Plugin { this._indexPath = path.join(indexesPath(dir), name) const debug = Debug('ssb:db2:' + name) - if (notInABrowser) mkdirp.sync(this._indexPath) + if (hasDiskAccess) mkdirp.sync(this._indexPath) this.level = Level(this._indexPath) const META = '\x00' @@ -173,7 +174,7 @@ module.exports = class Plugin { * and recreate level. */ clear(cb) { - if (notInABrowser) { + if (hasDiskAccess) { this.level.close(() => { rimraf.sync(this._indexPath) mkdirp.sync(this._indexPath)