Skip to content

Commit

Permalink
Merge pull request #425 from ssbc/hasdiskaccess-conf
Browse files Browse the repository at this point in the history
Add hasDiskAccess config
  • Loading branch information
Powersource authored Oct 26, 2023
2 parents c1e9479 + 7f96039 commit de08240
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions indexes/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ 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)
}

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 = []
Expand All @@ -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'
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit de08240

Please sign in to comment.