Skip to content

Commit

Permalink
updates and event
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 19, 2024
1 parent b852f0c commit 26b5cf9
Show file tree
Hide file tree
Showing 20 changed files with 4,648 additions and 2,414 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 55 additions & 4 deletions iroh-js/__test__/blob.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { Iroh, SetTagOption, Hash, Collection } from '../index.js'
import { Iroh, SetTagOption, Hash, Collection, BlobDownloadOptions } from '../index.js'
import { cwd } from 'process'
import { randomBytes } from 'crypto'

Expand Down Expand Up @@ -109,11 +109,62 @@ test('share', async (t) => {
const res = await node.blobs.addBytes(Array.from(Buffer.from('hello')))
const ticket = await node.blobs.share(res.hash, res.format, 'RelayAndAddresses')

const nodeAddr = await node.node.nodeAddr()

console.log(res, ticket)
const nodeAddr = await node.net.nodeAddr()

t.is(ticket.format, res.format)
t.is(ticket.hash, res.hash)
t.deepEqual(ticket.nodeAddr, nodeAddr)
})

test('provide events', async (t) => {
const node1 = await Iroh.memory()

// Do not use Promise.withResovlers it is buggy
let resolve0
let reject0
const promise0 = new Promise((res, rej) => {
resolve0 = res
reject0 = rej
})

let resolve1
let reject1
const promise1 = new Promise((res, rej) => {
resolve1 = res
reject1 = rej
})

let events = []
const node2 = await Iroh.memory({ blobEvents: (err, event) => {
if (err != null) {
return reject0(err)
}

events.push(event)

if (event.transferCompleted != null) {
return resolve0()
}
}})

const res = await node2.blobs.addBytes(Array.from(Buffer.from('hello')))

t.truthy(res.hash)
const node2Addr = await node2.net.nodeAddr()

const opts = new BlobDownloadOptions(res.format, node2Addr, SetTagOption.auto())
await node1.blobs.download(res.hash, opts, (err, event) => {
if (err != null) {
return reject1(err)
}

if (event.allDone != null) {
return resolve1(event)
}
})

await promise0
await promise1

t.is(events.length, 4)
})
12 changes: 6 additions & 6 deletions iroh-js/__test__/gossip.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ test('gossip basic', async (t) => {
rawTopic.fill(0, 1, 32)
const topic = Array.from(rawTopic)

const n1Id = await n1.node.nodeId()
const n1Addr = await n1.node.nodeAddr()
await n0.node.addNodeAddr(n1Addr)
const n1Id = await n1.net.nodeId()
const n1Addr = await n1.net.nodeAddr()
await n0.net.addNodeAddr(n1Addr)

// Do not use Promise.withResovlers it is buggy
let resolve0;
Expand All @@ -39,9 +39,9 @@ test('gossip basic', async (t) => {
}
})

const n0Id = await n0.node.nodeId()
const n0Addr = await n0.node.nodeAddr()
await n1.node.addNodeAddr(n0Addr)
const n0Id = await n0.net.nodeId()
const n0Addr = await n0.net.nodeAddr()
await n1.net.addNodeAddr(n0Addr)

const sink1 = await n1.gossip.subscribe(topic, [n0Id], (error, event) => {
if (error != null) {
Expand Down
10 changes: 10 additions & 0 deletions iroh-js/__test__/net.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from 'ava'

import { Iroh } from '../index.js'

test('node status', async (t) => {
const iroh = await Iroh.memory()

const nodeId = await iroh.net.nodeId()
t.truthy(nodeId)
})
2 changes: 1 addition & 1 deletion iroh-js/__test__/ticket.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Iroh, Hash, BlobTicket } from '../index.js'

test('ticket creation and encoding', async (t) => {
const node = await Iroh.memory()
const nodeId = await node.node.nodeId()
const nodeId = await node.net.nodeId()

const hash1 = new Hash(Array.from(new Uint8Array([1, 2, 3])))
const hash2 = new Hash(Array.from(new Uint8Array([1, 2, 3, 4])))
Expand Down
Loading

0 comments on commit 26b5cf9

Please sign in to comment.