Skip to content

Commit

Permalink
feat: add option to check whether bee api is available (#127)
Browse files Browse the repository at this point in the history
* feat: add option to check whether bee api is available

* chore: add missing definition
  • Loading branch information
tomicvladan authored Jun 30, 2022
1 parent 2ce4f00 commit 34719b0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
23 changes: 23 additions & 0 deletions src/background/feeder/web2-helper.feeder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBeeApiAvailable } from '../../utils/bee-js'
import { InterceptorReqMessageFormat, ResponseMessageFormat } from '../../utils/message/message-handler'
import { BeeApiListener } from '../listener/bee-api.listener'

Expand All @@ -19,7 +20,29 @@ export class Web2HelperFeeder {
answer: this.beeApiListener.beeApiUrl,
}
sendResponse(response)
} else if (message.key === 'isBeeApiAvailable') {
console.log('Web2HelperFeeder:beeApiAvailable got aimed message from content script', message)

this.checkConnection(message, sendResponse)

return true
}
})
}

private async checkConnection(
message: InterceptorReqMessageFormat<string>,
sendResponse: (response?: ResponseMessageFormat) => void,
) {
const answer = await isBeeApiAvailable(this.beeApiListener.beeApiUrl)

const response: ResponseMessageFormat = {
key: message.key,
sender: 'background',
target: 'content',
answer,
}

sendResponse(response)
}
}
39 changes: 28 additions & 11 deletions src/contentscript/swarm-library/web2-helper.content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@ import { appendSwarmSessionIdToUrl } from '../../utils/swarm-session-id'
import { MessengerInpage } from './messenger.inpage'

export class Web2HelperContent extends MessengerInpage implements IWeb2HelperMessage {
/** The real Bee API address that shouldn't be called directly by dApps */
public beeApiUrl(): Promise<string> {
const message: InpageReqMessageFormat<undefined> = {
key: 'beeApiUrl',
eventId: nanoid(),
sender: 'inpage',
target: 'content',
}

return new Promise<string>((resolve, reject) => {
const handler = (response: MessageEvent<InterceptorResMessageFormat<string>>) => {
private messageHandler<Response>(message: InpageReqMessageFormat<unknown>) {
return new Promise<Response>((resolve, reject) => {
const handler = (response: MessageEvent<InterceptorResMessageFormat<Response>>) => {
if (!this.validMessage(response, message.eventId)) return

// Remove listener since this was the response that we were looking for at this point
Expand All @@ -43,6 +35,31 @@ export class Web2HelperContent extends MessengerInpage implements IWeb2HelperMes
window.postMessage(message, origin)
})
}
/** The real Bee API address that shouldn't be called directly by dApps */
public beeApiUrl(): Promise<string> {
const message: InpageReqMessageFormat<undefined> = {
key: 'beeApiUrl',
eventId: nanoid(),
sender: 'inpage',
target: 'content',
}

return this.messageHandler<string>(message)
}

/**
* Checks whether configured Bee API is available
*/
public isBeeApiAvailable(): Promise<boolean> {
const message: InpageReqMessageFormat<undefined> = {
key: 'isBeeApiAvailable',
eventId: nanoid(),
sender: 'inpage',
target: 'content',
}

return this.messageHandler<boolean>(message)
}

/**
* Fake URL which will be forwarded to `bzz` endpoint of the Bee client
Expand Down
14 changes: 13 additions & 1 deletion src/utils/bee-js.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { BeeDebug, PostageBatch } from '@ethersphere/bee-js'
import { Bee, BeeDebug, PostageBatch } from '@ethersphere/bee-js'

export function getPostageBatches(beeDebugApiUrl: string): Promise<PostageBatch[]> {
const bee = new BeeDebug(beeDebugApiUrl)

return bee.getAllPostageBatch()
}

export async function isBeeApiAvailable(beeApiAddress: string): Promise<boolean> {
try {
const bee = new Bee(beeApiAddress)

await bee.checkConnection()

return true
} catch (error) {
return false
}
}
2 changes: 2 additions & 0 deletions src/utils/message/web2-helper/web2-helper.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
// Key: Message name; Parameter: payload data from sender side, Return Value: Feeder functions emit
export interface IWeb2HelperMessage {
beeApiUrl: () => Promise<string>

isBeeApiAvailable(): Promise<boolean>
}
10 changes: 10 additions & 0 deletions test/bzz-protocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ describe('BZZ protocol', () => {
done()
})

test('Check Real Bee API is available', async done => {
await page.click('#button-check-real-bee-api-available')
const placeHolderSelector = '#bee-api-available-placeholder[complete="true"]'
await page.waitForSelector(placeHolderSelector)
const value = await page.$eval(placeHolderSelector, e => e.innerHTML)
expect(value).toBe('true') //default value of Bee API URL in the extension

done()
})

test('Allow Global Postage Stamp ID', async done => {
const extensionPage = await openExtensionPage()
const stamp = getStamp()
Expand Down
5 changes: 5 additions & 0 deletions test/bzz-test-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ <h4>Fetch Real Bee API URL</h4>
<button id="button-fetch-real-bee-api-url" onclick="fetchBeeApiUrl()">Fetch</button>
<label id="bee-api-url-placeholder">[Bee API URL Placeholder]</label>
</div>
<div>
<h4>Check if Real Bee API is available</h4>
<button id="button-check-real-bee-api-available" onclick="checkBeeApiAvailable()">Fetch</button>
<label id="bee-api-available-placeholder">[Bee API Available Placeholder]</label>
</div>
<div>
<h4>Link to other page via BZZ protocol</h4>
<a id="bzz-ext-ref" is="swarm-a" href="bzz://82de75aa2e4e27eefc3c00f5cdc7a2cb787402906a73609803de0ee25602b4f5">Jinn page link 1</a>
Expand Down
8 changes: 8 additions & 0 deletions test/bzz-test-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ function fetchBeeApiUrl() {
web2Helper.beeApiUrl().then(url => (document.getElementById('bee-api-url-placeholder').innerHTML = url))
}

function checkBeeApiAvailable() {
web2Helper.isBeeApiAvailable().then(available => {
const placeholderElement = document.getElementById('bee-api-available-placeholder')
placeholderElement.innerHTML = available
placeholderElement.setAttribute('complete', 'true')
})
}

function fetchJinnImage() {
const bzzContentAddress = '82de75aa2e4e27eefc3c00f5cdc7a2cb787402906a73609803de0ee25602b4f5/images/jinn.png'
const jinnFakeUrl = web2Helper.fakeBzzAddress(bzzContentAddress)
Expand Down

0 comments on commit 34719b0

Please sign in to comment.