0.0.002
ddat interop with dat js cable interop with cabal js ddat's frontend = dat-thang cable fe = cable-thang dillo support based on cel's dillo-dat bootstrap node xplatform support Signed-off-by: divo <418@cryspace.space>
This commit is contained in:
1 parent
f0f91540be
commit
b735900f2e
323 files changed
+134347
-343
No files matched your search
@@ -0,0 +1,69 @@
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
const hyperdrive = require('../../extra/dillo-dat/node_modules/hyperdrive')
|
||||
const hyperdiscovery = require('../../extra/dillo-dat/node_modules/hyperdiscovery')
|
||||
|
||||
function ready (archive) {
|
||||
return new Promise((resolve, reject) => archive.ready(err => err ? reject(err) : resolve()))
|
||||
}
|
||||
|
||||
function writeFile (archive, name, data) {
|
||||
return new Promise((resolve, reject) => archive.writeFile(name, data, err => err ? reject(err) : resolve()))
|
||||
}
|
||||
|
||||
function readFile (archive, name) {
|
||||
return new Promise((resolve, reject) => archive.readFile(name, (err, data) => err ? reject(err) : resolve(data)))
|
||||
}
|
||||
|
||||
function listening (swarm) {
|
||||
if (swarm.address()) return Promise.resolve(swarm.address())
|
||||
return new Promise(resolve => swarm.once('listening', () => resolve(swarm.address())))
|
||||
}
|
||||
|
||||
function sleep (ms) { return new Promise(resolve => setTimeout(resolve, ms)) }
|
||||
|
||||
async function main () {
|
||||
const root = fs.mkdtempSync(path.join(process.env.DDAT_TEST_DIR || os.tmpdir(), 'ddat-js-p2p-'))
|
||||
let source, replica, sourceSwarm, replicaSwarm
|
||||
try {
|
||||
source = hyperdrive(path.join(root, 'source'))
|
||||
await ready(source)
|
||||
const key = source.key.toString('hex')
|
||||
const address = 'dat://' + key
|
||||
await writeFile(source, 'hello.txt', 'hello from old dillo-dat v1')
|
||||
await writeFile(source, 'hello.txt', 'hello from old dillo-dat v2')
|
||||
if (source.key.toString('hex') !== key) throw new Error('JS archive address changed after update')
|
||||
replica = hyperdrive(path.join(root, 'replica'), source.key, {sparse: true})
|
||||
await ready(replica)
|
||||
|
||||
sourceSwarm = hyperdiscovery(source, {port: 0, discovery: false, dht: false, dns: false, utp: false})
|
||||
replicaSwarm = hyperdiscovery(replica, {port: 0, discovery: false, dht: false, dns: false, utp: false})
|
||||
const sourceAddress = await listening(sourceSwarm)
|
||||
const replicaAddress = await listening(replicaSwarm)
|
||||
const topic = source.discoveryKey
|
||||
sourceSwarm.addPeer(topic, {host: '127.0.0.1', port: replicaAddress.port})
|
||||
replicaSwarm.addPeer(topic, {host: '127.0.0.1', port: sourceAddress.port})
|
||||
|
||||
const deadline = Date.now() + 15000
|
||||
let result
|
||||
while (Date.now() < deadline) {
|
||||
try { result = await readFile(replica, 'hello.txt'); break } catch (_) { await sleep(100) }
|
||||
}
|
||||
if (!result || result.toString() !== 'hello from old dillo-dat v2') throw new Error('old JS replication did not deliver the latest file')
|
||||
process.stdout.write('old Dillo Dat JS create/update/address/xfer: PASS ' + address +
|
||||
' metadata=' + source.metadata.length + ' content=' + source.content.length + '\n')
|
||||
} finally {
|
||||
if (sourceSwarm) sourceSwarm.destroy()
|
||||
if (replicaSwarm) replicaSwarm.destroy()
|
||||
if (source) source.close()
|
||||
if (replica) replica.close()
|
||||
if (process.env.DDAT_TEST_KEEP === '1') {
|
||||
process.stdout.write('kept old JS source and replica at ' + root + '\n')
|
||||
} else {
|
||||
fs.rmSync(root, {recursive: true, force: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(err => { console.error(err); process.exitCode = 1 })
|
||||
Reference in new issue
Block a user