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

Vanilla #59

Merged
merged 8 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
89 changes: 21 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,49 +70,35 @@ ssbSingleton.setup("/.ssb-example", config, extraModules)

## API

The `SSB` object one gets from the [`singleton`](#ssb-singleton) has
the following:
The `SSB` object one gets from the [`singleton`](#ssb-singleton) is a
[secret-stack] with some extra plugins loadings by default. A few
helper functions are included under helpers:

### db
### connectAndRemember(addr, data)

This is the [ssb-db2] module.

### net

This is the [secret-stack] module with a few extra modules
loaded.

#### id

The public key of the current user

#### rpc:connect event
Will connect and store as to automatically reconnect on
reload. Options are as described in [ssb-conn].

Example:
### getPeer()

```
SSB.net.on('rpc:connect', (rpc) => {
console.log("connected")
rpc.on('closed', () => console.log("bye"))
})
```
Gets one of the connected peers that is not a room server.

#### connectAndRemember(addr, data)
### getGraphForFeed(feed, cb)

Will connect and store as to automatically reconnect on
reload. Options are as described in [ssb-conn].
Returns an object of: following, blocking and extended given the feed.

#### directConnect(addr, cb)
### box

Connect to addr only once. Cb is (err, rpc)
The
[box](https://github.com/ssbc/ssb-keys#boxcontent-recipients--boxed)
method from ssb-keys. Useful for private messages.

#### blobs
### blobs

This is where the `blobs` api can be found. The module implements the
blobs protocol and so can exchange blobs with connection peers. It
The blobs module is a little special compared to default ssb-blobs. It
also contains with the the following extra methods:

##### hash(data, cb)
#### hash(data, cb)

Hashes data and returns the digest or err

Expand All @@ -128,64 +114,31 @@ onFileSelect: function(ev) {
}
```

##### add(blobId, file, cb)
#### add(blobId, file, cb)

Adds the `file` (such as one obtained from ev.target.files when using
a file select) to the blob store using the blobId name. BlobId is & +
hash.

##### remoteURL(blobId)
#### remoteURL(blobId)

Returns a http URL string for the current connection. This is useful
in a browser for images that you don't want to store directly on the
device.

##### privateGet(blobId, unbox, cb)
#### privateGet(blobId, unbox, cb)

Callback with err or a url that works for e.g images that was received
in a private message.

##### localGet(blobId, unbox, cb)
#### localGet(blobId, unbox, cb)

If blob already exists will callback with err or a url that can be
used for images for a blob. Otherwise the blob will get requested and
if size is smaller than the maximum size, the blob will be stored
locally and used for callback, otherwise the callback will return a
`remoteURL` link.

#### ooo

The [ssb-ooo] module

#### Browser specific methods on net

*** Warning: This will be removed in the future ***

For partial replication a special plugin has been created and
implemented in browser core, other clients such as a pub needs to have
the [ssb-partial-replication] plugin installed.

Once a rpc connection has been established, a few extra methods are
available under SSB.net.partialReplication. See plugin for
documentation.

### dir

The path to where the database and blobs are stored.

### getPeer()

Gets one of the connected peers that is not a room server.

### getGraphForFeed(feed, cb)

Returns an object of: following, blocking and extended given the feed.

### box

[box](https://github.com/ssbc/ssb-keys#boxcontent-recipients--boxed)
method from ssb-keys. Useful for private messages.

## SSB Singleton

Several of the libraries we use (such as db2 and
Expand Down
22 changes: 13 additions & 9 deletions core-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ const pull = require('pull-stream')
const raf = require('polyraf')
const path = require('path')

exports.getPeer = function()
{
let connPeers = Array.from(SSB.net.conn.hub().entries())
exports.connectAndRemember = function (addr, data) {
SSB.conn.connect(addr, data, (err, rpc) => {
SSB.conn.remember(addr, Object.assign(data, { autoconnect: true }))
})
}

exports.getPeer = function() {
let connPeers = Array.from(SSB.conn.hub().entries())
connPeers = connPeers.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
var goodPeer = connPeers.find(cp => cp.data.type != 'room')

let peers = Object.values(SSB.net.peers).flat()
let peers = Object.values(SSB.peers).flat()

if (goodPeer) return peers.find(p => p.id == goodPeer.data.key)
else if (peers.length > 0) return peers[0]
else return null
}

function deleteDatabaseFile(filename) {
const path = require('path')
const file = raf(path.join(SSB.dir, filename))
const file = raf(path.join(SSB.config.path, filename))
file.open((err, done) => {
if (err) return console.error(err)
file.destroy()
Expand Down Expand Up @@ -79,7 +83,7 @@ exports.convertHopsIntoGraph = function(hops) {
const feed = feeds[i]
if (hops[feed] == 1)
following.push(feed)
else if (hops[feed] > 0 && hops[feed] <= SSB.net.config.friends.hops)
else if (hops[feed] > 0 && hops[feed] <= SSB.config.friends.hops)
extended.push(feed)
else if (hops[feed] == -1)
blocking.push(feed)
Expand All @@ -89,8 +93,8 @@ exports.convertHopsIntoGraph = function(hops) {
}

exports.getGraphForFeed = function(feedId, cb) {
SSB.net.friends.hops({ start: feedId }, (err, hops) => {
SSB.friends.hops({ start: feedId }, (err, hops) => {
if (err) return cb(err)
else cb(null, exports.convertHopsIntoGraph(hops, feedId == SSB.net.id))
else cb(null, exports.convertHopsIntoGraph(hops, feedId == SSB.id))
})
}
39 changes: 15 additions & 24 deletions core.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
exports.init = function (dir, config, extraModules) {
const EventEmitter = require('events')
SSB = {
events: new EventEmitter(),
dbOperators: require('ssb-db2/operators')
}
SSB.dbOperators.mentions = require('ssb-db2/operators/full-mentions')
SSBLOADER = new EventEmitter()

// init secret stack
const s = require('sodium-browserify')
s.events.on('sodium-browserify:wasm loaded', function() {

s.events.on('sodium-browserify:wasm loaded', () => {
console.log("wasm loaded")

var net = require('./net').init(dir, config, extraModules)

console.log("my id: ", net.id)
SSB = require('./net').init(dir, config, extraModules)
console.log("my id: ", SSB.id)

var helpers = require('./core-helpers')
const helpers = require('./core-helpers')

SSB = Object.assign(SSB, {
db: net.db,
net,
dir,
SSB.helpers = {
box: require('ssb-keys').box,

connectAndRemember: helpers.connectAndRemember,
getPeer: helpers.getPeer,

removeDB: helpers.removeDB,
removeIndexes: helpers.removeIndexes,
removeBlobs: helpers.removeBlobs,

convertHopsIntoGraph: helpers.convertHopsIntoGraph,
getGraphForFeed: helpers.getGraphForFeed,

box: require('ssb-keys').box
})
removeDB: helpers.removeDB,
removeIndexes: helpers.removeIndexes,
removeBlobs: helpers.removeBlobs
}

// delay startup a bit
const startOffline = config && config.core && config.core.startOffline
if (!startOffline) {
setTimeout(() => {
SSB.net.conn.start()
SSB.conn.start()
}, 2500)
}

SSB.events.emit("SSB: loaded")
SSBLOADER.emit("ready")
})
}
Loading