Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
process.env["DEBUG"] = "*"
import DiscoveryCloudClient from "./Client"
import { Stream } from "stream"
import * as Debug from "debug"
let hypercore = require("hypercore")
let crypto = require("hypercore/lib/crypto")
let ram = require("random-access-memory")
const log1 = Debug("discovery-cloud:test:client1")
const log2 = Debug("discovery-cloud:test:client2")
const url = "wss://discovery-cloud.herokuapp.com"
// const url = "ws://0.0.0.0:8080"
const keys = crypto.keyPair()
const feed1 = hypercore(ram, keys.publicKey, { secretKey: keys.secretKey })
const feed2 = hypercore(ram, keys.publicKey)
const client1 = new DiscoveryCloudClient({
id: crypto.randomBytes(32),
url,
stream: info => feed1.replicate(info),
})
feed1.on("ready", () => {
client1.join(feed1.discoveryKey)
log1("joining")
feed1.append("foo")
feed1.append("bar")
feed1.append("baz")
import { FrontendHandle } from "../../modules/hypermerge/frontend"
import { Doc, AnyEditDoc, EditDoc, AnyDoc } from "automerge/frontend"
import * as Debug from "debug"
const log = Debug("discovery-cloud:test")
let ram = require("random-access-memory")
const hm = new Hypermerge({ storage: ram })
interface Model {
counter1?: number
counter2?: number
}
type Handle = FrontendHandle | null
const buffers = keyPair()
const keys = {
publicKey: Base58.encode(buffers.publicKey),
secretKey: Base58.encode(buffers.secretKey),
}
const input = process.argv[2]
const docId = input || keys.publicKey
log("DOCID", docId)
hm.ready.then(hm => {
swarm(hm, {
id: hm.core.archiver.changes.id,
url: "wss://discovery-cloud.herokuapp.com",
// url: "ws://0.0.0.0:8080",
Archiver.prototype.createFeed = function (key, opts) {
const self = this
opts = opts || {}
if (!key) {
// create key pair
const keyPair = crypto.keyPair()
key = keyPair.publicKey
opts.secretKey = keyPair.secretKey
}
const dk = hypercore.discoveryKey(toBuffer(key, 'hex')).toString('hex')
if (this.feeds[dk]) {
return this.feeds[dk]
}
opts.sparse = this.sparse
const feed = hypercore(storage(key), key, opts)
this.feeds[dk] = feed
this.changes.append({type: 'add', key: key.toString('hex')})
this.emit('add', feed)
createFeed(key, opts = {}) {
this._ensureReady()
log('createFeed', key && key.toString('hex'))
// Create a key pair if we're making a feed from scratch.
if (!key) {
const keyPair = crypto.keyPair()
key = keyPair.publicKey
opts.secretKey = keyPair.secretKey
}
const dk = Hypercore.discoveryKey(toBuffer(key, 'hex')).toString('hex')
if (this.archiver.feeds[dk]) {
return this.archiver.feeds[dk]
}
opts.sparse = this.archiver.sparse
const feed = Hypercore(this._feedStorage(key), key, opts)
this.archiver.feeds[dk] = feed
this.archiver.changes.append({ type: 'add', key: key.toString('hex') })
this.archiver.emit('add', feed)
tape('create with precreated keypair', function (t) {
var crypto = require('hypercore/lib/crypto')
var keyPair = crypto.keyPair()
var db = create.one(keyPair.publicKey, {secretKey: keyPair.secretKey})
db.put('hello', 'world', function (err, node) {
t.same(node.value, 'world')
t.error(err, 'no error')
t.same(db.key, keyPair.publicKey, 'pubkey matches')
db.source._storage.secretKey.read(0, keyPair.secretKey.length, function (err, secretKey) {
t.error(err, 'no error')
t.same(secretKey, keyPair.secretKey, 'secret key is stored')
})
db.get('hello', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'world', 'same value')
t.end()
})
})
t.joinSwarm(client)
//const keys = crypto.keyPair()
//const docId = Base58.encode(keys.publicKey)
interface Foo {
foo: string
bar: string
counter1: number
counter2: number
}
const front: FrontendManager = docId
? t.openDocumentFrontend(docId)
: t.createDocumentFrontend(crypto.keyPair())
let i = 1
front.on("doc", doc => {
log("DOC", doc)
})
if (!docId) {
front.change(doc => {
doc.foo = "bar"
doc.bar = "baz"
doc.counter1 = 0
})
}
setInterval(() => {
front.change(doc => {
doc.counter1 += 1
})
t.joinSwarm(client)
//const keys = crypto.keyPair()
//const docId = Base58.encode(keys.publicKey)
interface Foo {
foo: string
bar: string
counter1: number
counter2: number
}
log("about to open", docId)
const front: FrontendManager = docId
? t.openDocumentFrontend(docId)
: t.createDocumentFrontend(crypto.keyPair())
let i = 1
front.on("doc", doc => {
log("DOC", doc)
})
/*
if (!docId) {
front.change(doc => {
doc.foo = "bar"
doc.bar = "baz"
doc.counter2 = 0
})
}
*/
setInterval(() => {
front.change(doc => {
create(setup: ChangeFn): FrontendManager {
const buffers = keyPair()
const keys = {
publicKey: Base58.encode(buffers.publicKey),
secretKey: Base58.encode(buffers.secretKey),
}
const docId = keys.publicKey
log("create", docId)
this.sendToBackend({
type: "Create",
docId,
keys,
})
const manager = this.manager(docId, docId)
manager.change(setup)
initActorFeed(doc: BackendManager): string {
log("initActorFeed", doc.docId)
const keys = crypto.keyPair()
const actorId = Base58.encode(keys.publicKey)
this.initFeed(doc, keys)
return actorId
}
Corestore.prototype.get = function (key, opts) {
if (typeof key === 'object' && !(key instanceof Buffer)) {
opts = key
key = null
}
opts = opts || {}
opts.seed = opts.seed !== undefined ? opts.seed : true
opts.sparse = opts.sparse !== undefined ? opts.sparse : true
if (!key) opts.valueEncoding = opts.valueEncoding || 'binary'
if (key) {
let existing = this._getCachedCore(key)
if (existing) return existing
} else {
let { publicKey, secretKey } = opts.keyPair || crypto.keyPair()
opts.secretKey = secretKey
opts.writable = true
key = publicKey
}
let core = this._create(key, opts)
opts.writable = core.writable
return core
}