Skip to content

Commit

Permalink
feat: node endpoint support (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored Dec 9, 2021
1 parent 649162f commit 11731d3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/bee-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
Ky,
TransactionInfo,
TransactionHash,
NodesInfo,
} from './types'
import { BeeArgumentError } from './utils/error'
import { assertBeeUrl, stripLastSlash } from './utils/url'
Expand Down Expand Up @@ -370,6 +371,15 @@ export class BeeDebug {
return status.getHealth(this.getKy(options))
}

/**
* Get mode information of node
*/
async getNodeInfo(options?: RequestOptions): Promise<NodesInfo> {
assertRequestOptions(options)

return status.getNodeInfo(this.getKy(options))
}

/**
* Connnects to a node and checks if it is a supported Bee version by the bee-js
*
Expand Down
15 changes: 0 additions & 15 deletions src/modules/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,3 @@ export async function download(ky: Ky, hash: string): Promise<Data> {

return wrapBytesWithHelpers(new Uint8Array(response.data))
}

/**
* Download chunk data as a readable stream
*
* @param ky Ky instance for given Bee class instance
* @param hash Bee content reference
*/
export async function downloadReadable(ky: Ky, hash: string): Promise<ReadableStream<Uint8Array>> {
const response = await http<ReadableStream<Uint8Array>>(ky, {
responseType: 'stream',
path: `${endpoint}/${hash}`,
})

return response.data
}
29 changes: 24 additions & 5 deletions src/modules/debug/status.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import { http } from '../../utils/http'
import type { Health } from '../../types/debug'
import type { Health, NodesInfo } from '../../types/debug'
import { engines } from '../../../package.json'
import { Ky } from '../../types'

export const SUPPORTED_BEE_VERSION_EXACT = engines.bee
export const SUPPORTED_BEE_VERSION = engines.bee.substr(0, engines.bee.indexOf('-'))

const NODE_INFO_URL = 'node'
const HEALTH_URL = 'health'

/**
* Get health of node
*
* @param ky Ky debug instance
*/
export async function getHealth(ky: Ky): Promise<Health> | never {
export async function getHealth(ky: Ky): Promise<Health> {
const response = await http<Health>(ky, {
method: 'get',
path: `health`,
path: HEALTH_URL,
responseType: 'json',
})

return response.data
}

/**
* Get information about Bee node
*
* @param ky Ky debug instance
*/
export async function getNodeInfo(ky: Ky): Promise<NodesInfo> {
const response = await http<NodesInfo>(ky, {
method: 'get',
path: NODE_INFO_URL,
responseType: 'json',
})

return response.data
}

/**
* Connnects to a node and checks if it is a supported Bee version by the bee-js
* Connects to a node and checks if it is a supported Bee version by the bee-js
*
* @param ky Ky debug instance
*
* @returns true if the Bee node version is supported
*/
export async function isSupportedVersion(ky: Ky): Promise<boolean> | never {
export async function isSupportedVersion(ky: Ky): Promise<boolean> {
const { version } = await getHealth(ky)

return version === SUPPORTED_BEE_VERSION_EXACT
Expand Down
11 changes: 11 additions & 0 deletions src/types/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ export interface Health {
debugApiVersion: string
}

export enum BeeModes {
FULL = 'full',
LIGHT = 'light',
DEV = 'dev',
}

export interface NodesInfo {
gatewayMode: boolean
beeMode: BeeModes
}

export interface RemovePeerResponse {
message: string
code: 0
Expand Down
11 changes: 11 additions & 0 deletions test/integration/bee-debug-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,15 @@ describe('Bee Debug class', () => {
await expect(beeDebug.createPostageBatch('-1', 17)).rejects.toThrowError(BeeArgumentError)
})
})

describe('modes', () => {
it('should return modes', async () => {
expect(await beeDebug.getNodeInfo()).toEqual(
expect.objectContaining({
beeMode: expect.stringMatching(/^(dev|light|full)$/),
gatewayMode: expect.any(Boolean),
}),
)
})
})
})

0 comments on commit 11731d3

Please sign in to comment.