add modern versions

This commit is contained in:
Henry 2021-03-16 09:54:59 +01:00
parent 82cf618279
commit fc7bd1aad5
5 changed files with 190 additions and 0 deletions

View File

@ -227,3 +227,49 @@ func TestLegacyJSServer(t *testing.T) {
ts.wait()
}
// the new ssb-room-client module (2x) against a Go room server
func TestModernJSClient(t *testing.T) {
// defer leakcheck.Check(t)
r := require.New(t)
ts := newRandomSession(t)
// ts := newSession(t, nil)
var allowDB = &mockdb.FakeAllowListService{}
var aliasDB = &mockdb.FakeAliasService{}
srv := ts.startGoServer(allowDB, aliasDB)
alice := ts.startJSClient("alice", "./testscripts/modern_client.js",
srv.Network.GetListenAddr(),
srv.Whoami(),
)
srv.Allow(alice, true)
var roomHandle bytes.Buffer
roomHandle.WriteString("tunnel:")
roomHandle.WriteString(srv.Whoami().Ref())
roomHandle.WriteString(":")
roomHandle.WriteString(alice.Ref())
roomHandle.WriteString("~shs:")
roomHandle.WriteString(base64.StdEncoding.EncodeToString(alice.ID))
// write the handle to the testrun folder of the bot
handleFile := filepath.Join("testrun", t.Name(), "bob", "endpoint_through_room.txt")
os.MkdirAll(filepath.Dir(handleFile), 0700)
err := ioutil.WriteFile(handleFile, roomHandle.Bytes(), 0700)
r.NoError(err)
time.Sleep(1500 * time.Millisecond)
bob := ts.startJSClient("bob", "./testscripts/modern_client_opening_tunnel.js",
srv.Network.GetListenAddr(),
srv.Whoami(),
)
srv.Allow(bob, true)
allowDB.HasFeedReturns(true)
time.Sleep(5 * time.Second)
ts.wait()
}

View File

@ -2382,6 +2382,20 @@
}
}
},
"ssb-room-client": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/ssb-room-client/-/ssb-room-client-0.2.0.tgz",
"integrity": "sha512-yTLxsY4R/enw3gICP3r5Eyf/o/LzL+qhyBS02k54Y3r1P3jQZsEjFx2CVpZ2HfvjlwTF6xo1k3QzwxiliIqqcA==",
"requires": {
"debug": "^4.3.1",
"pull-pair": "^1.1.0",
"pull-stream": "^3.6.14",
"ssb-conn-hub": ">=0.2.0",
"ssb-conn-staging": ">=0.1.0",
"ssb-ref": "^2.14.3",
"ssb-typescript": "^2.2.0"
}
},
"ssb-typescript": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/ssb-typescript/-/ssb-typescript-2.2.0.tgz",

View File

@ -19,6 +19,7 @@
"ssb-keys": "^8.0.0",
"ssb-replicate": "^1.3.2",
"ssb-room": "^1.3.0",
"ssb-room-client": "^0.2.0",
"tape": "^5.0.1"
}
}

View File

@ -0,0 +1,45 @@
const pull = require('pull-stream')
module.exports = {
secretStackPlugins: ['ssb-conn', 'ssb-room-client'],
before: (sbot, ready) => {
ready()
},
after: (sbot, rpc, exit) => {
sbot.on("rpc:connect", (remote, isClient) => {
console.warn("tunneld connection to simple client!")
// leave after 5 seconds
setTimeout(() => {
rpc.tunnel.leave().then((ret) => {
console.warn('left')
console.warn(ret)
console.warn('room left... exiting in 10s')
setTimeout(exit, 10000)
}).catch((err) => {
console.warn('left failed')
throw err
})
}, 5000)
})
// announce ourselves to the room/tunnel
rpc.tunnel.announce().then((ret) => {
console.warn('announced!')
console.warn(ret)
}).catch((err) => {
console.warn('announce failed')
throw err
})
// log all new endpoints
pull(
rpc.tunnel.endpoints(),
pull.drain(el => {
console.warn("from roomsrv:",el)
})
)
}
}

View File

@ -0,0 +1,84 @@
const pull = require('pull-stream')
const { readFileSync } = require('fs')
let newConnections = 0
module.exports = {
secretStackPlugins: ['ssb-conn', 'ssb-room-client'],
before: (client, ready) => {
// nothing to prepare (like publishes messages, or...)
ready()
// let msg = {
// type: 'test',
// }
// client.publish(msg, (err) => {
// if (err) throw err
// })
},
after: (client, roomSrvRpc, exit) => {
newConnections++
console.warn('new connection!', roomSrvRpc.id, 'total:', newConnections)
if (newConnections > 1) {
console.warn('after call 2 - not exiting')
return
}
// now connected to the room server
// log all new endpoints
pull(
roomSrvRpc.tunnel.endpoints(),
pull.drain(el => {
console.warn("from roomsrv:",el)
})
)
roomSrvRpc.tunnel.isRoom().then((yes) => {
if (!yes) throw new Error("expected isRoom to be true!")
console.warn("peer is indeed a room!")
// announce ourselves to the room/tunnel
roomSrvRpc.tunnel.announce().then((ret) => {
console.warn('announced!')
// put there by the go test process
let roomHandle = readFileSync('endpoint_through_room.txt').toString()
console.warn("connecting to room handle:", roomHandle)
client.conn.connect(roomHandle, (err, tunneldRpc) => {
if (err) throw err
console.warn("got tunnel to:", tunneldRpc.id)
// check the tunnel connection works
tunneldRpc.tunnel.ping((err, id) => {
if (err) throw err
console.warn("ping:", id)
// start leaving after 2s
setTimeout(() => {
roomSrvRpc.tunnel.leave().then((ret) => {
console.warn('left room... exiting in 3s')
setTimeout(exit, 3000)
}).catch((err) => {
console.warn('left failed')
throw err
})
}, 2000)
})
})
}).catch((err) => {
console.warn('announce failed')
throw err
})
}).catch((err) => {
console.warn('isRoom failed')
throw err
})
}
}