Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbundy committed Dec 12, 2018
0 parents commit 0d66dec
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ssb-blob-prune

> Prune unused blobs from Scuttlebutt.
## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Maintainers](#maintainers)
- [Contributing](#contributing)
- [License](#license)

## Install

```sh
npm -g install ssb-prune-blobs
```

## Usage

If you want to test this out *without* installing, use:

```sh
npx ssb-blob-prune
```

If you've already installed above, you can just use:

```sh
ssb-blob-prune
```

## Maintainers

[@fraction](https://github.com/fraction)

## Contributing

PRs accepted.

## License

MIT © 2018 Fraction LLC
81 changes: 81 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node

const pull = require('pull-stream')
const ssbClient = require('ssb-client')
const _ = require('deepdash')(require('lodash'))
const ref = require('ssb-ref')
const multiblob = require('multiblob')
const path = require('path')
const debug = require('debug')('ssb-blob-prune')
const os = require('os')

debug.enabled = true

const config = {
messagesPerDebug: 1000
}

// create a scuttlebot client using default settings
// (server at localhost:8080, using key found at ~/.ssb/secret)
ssbClient(function (err, sbot) {
if (err) {
debug('could not connect to sbot, please ensure that it\'s running')
throw err
}

debug('connected to sbot')

const mentionedBlobs = []
const strayBlobs = []
let messagesConsumed = 0

const debugMessages = debug.extend('messages')

// stream all messages in all feeds, ordered by publish time
pull(
sbot.createFeedStream(),
pull.drain((msg) => {
// on each
messagesConsumed = messagesConsumed + 1
if (messagesConsumed % config.messagesPerDebug === 0) {
debugMessages('%d', messagesConsumed)
}

_.eachDeep(msg, value => {
if (ref.isBlob(value)) {
const sansAmpersand = value.substr(1)
mentionedBlobs.push(sansAmpersand)
}
})
}, () => {
const debugBlobs = debug.extend('blobs')
debugBlobs('enumerating')
// on done
var blobs = multiblob({
dir: path.join(os.homedir(), '.ssb', 'blobs'),
alg: 'sha256'
})

pull(
blobs.ls(),
pull.drain((blob) => {
if (!mentionedBlobs.includes(blob)) {
debugBlobs('stray %s', blob)
strayBlobs.push(blob)
blobs.rm(blob, (err, val) => {
if (err) throw err

if (val) debugBlobs('%s', val)
})
}
}, () => {
debug('previous total: %d', mentionedBlobs.length)
debug('deleted: %d', strayBlobs.length)
const currentTotal = mentionedBlobs.length - strayBlobs.length
debug('current total: %d', currentTotal)
sbot.close()
})
)
})
)
})
Loading

0 comments on commit 0d66dec

Please sign in to comment.