Skip to content

Commit

Permalink
fix: handle empty responses (#145)
Browse files Browse the repository at this point in the history
Sometimes nothing is received from the remote so throw an appropriate error in that case.
  • Loading branch information
achingbrain authored Oct 14, 2022
1 parent f5adaaf commit 0dfb823
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
44 changes: 44 additions & 0 deletions packages/libp2p-daemon-client/src/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class DHT {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand All @@ -67,6 +72,11 @@ export class DHT {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down Expand Up @@ -99,6 +109,11 @@ export class DHT {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down Expand Up @@ -135,6 +150,11 @@ export class DHT {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down Expand Up @@ -163,6 +183,10 @@ export class DHT {

let message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

// stream begin message
const response = Response.decode(message)

Expand All @@ -173,6 +197,11 @@ export class DHT {

while (true) {
message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = DHTResponse.decode(message)

// Stream end
Expand Down Expand Up @@ -214,6 +243,11 @@ export class DHT {

// stream begin message
let message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
Expand All @@ -223,6 +257,11 @@ export class DHT {

while (true) {
message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = DHTResponse.decode(message)

// Stream end
Expand Down Expand Up @@ -265,6 +304,11 @@ export class DHT {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down
20 changes: 20 additions & 0 deletions packages/libp2p-daemon-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class Client implements DaemonClient {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
Expand Down Expand Up @@ -141,6 +146,11 @@ class Client implements DaemonClient {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
Expand Down Expand Up @@ -173,6 +183,11 @@ class Client implements DaemonClient {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
Expand Down Expand Up @@ -243,6 +258,11 @@ class Client implements DaemonClient {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down
20 changes: 20 additions & 0 deletions packages/libp2p-daemon-client/src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class Pubsub {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand Down Expand Up @@ -65,6 +70,11 @@ export class Pubsub {
})

const message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

await sh.close()
Expand All @@ -91,6 +101,11 @@ export class Pubsub {
})

let message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

const response = Response.decode(message)

if (response.type !== Response.Type.OK) {
Expand All @@ -100,6 +115,11 @@ export class Pubsub {
// stream messages
while (true) {
message = await sh.read()

if (message == null) {
throw errcode(new Error('Empty response from remote'), 'ERR_EMPTY_RESPONSE')
}

yield PSMessage.decode(message)
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p-daemon-protocol/src/stream-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class StreamHandler {
/**
* Read and decode message
*/
async read () {
async read (): Promise<Uint8Array | undefined> {
// @ts-expect-error decoder is really a generator
const msg = await this.decoder.next()
if (msg.value != null) {
Expand All @@ -52,15 +52,15 @@ export class StreamHandler {
/**
* Return the handshake rest stream and invalidate handler
*/
rest () {
rest (): Duplex<Uint8ArrayList, Uint8Array> {
this.shake.rest()
return this.shake.stream
}

/**
* Close the stream
*/
async close () {
async close (): Promise<void> {
log('closing the stream')
await this.rest().sink([])
}
Expand Down

0 comments on commit 0dfb823

Please sign in to comment.