Skip to content

Commit

Permalink
feat: add http adapter option to bee and bee debug (#860)
Browse files Browse the repository at this point in the history
* feat: add http adapter option to bee and bee debug

* test(fix): change asserted 404 error messages

* ci: remove debug workflow
  • Loading branch information
Cafe137 authored Aug 1, 2023
1 parent 0618bbc commit ff78e0d
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 102 deletions.
47 changes: 0 additions & 47 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,50 +59,3 @@ jobs:
- name: Tests non-Node 18
if: matrix.node-version != '18.x'
run: npm run test

- name: Debug workflow if failed
if: failure()
run: |
KEYS=$(curl -sSf -X POST https://relay.tunshell.com/api/sessions)
curl -sSf -X POST -H "Content-Type: application/json" -d "{\"text\": \"**Bee JS**\nDebug -> \`sh <(curl -sSf https://lets.tunshell.com/init.sh) L $(echo $KEYS | jq -r .peer2_key) \${TUNSHELL_SECRET} relay.tunshell.com\`\"}" https://beehive.ethswarm.org/hooks/${{ secrets.WEBHOOK_KEY }}
echo "Connect to github actions node using"
echo "sh <(curl -sSf https://lets.tunshell.com/init.sh) L $(echo $KEYS | jq -r .peer2_key) \${TUNSHELL_SECRET} relay.tunshell.com"
curl -sSf https://lets.tunshell.com/init.sh | sh /dev/stdin T $(echo $KEYS | jq -r .peer1_key) ${{ secrets.TUNSHELL_SECRET }} relay.tunshell.com
# browser:
# runs-on: ubuntu-latest
#
# strategy:
# matrix:
# node-version: [16.x]
#
# steps:
# - name: Checkout
# uses: actions/checkout@v2
# with:
# fetch-depth: 1
#
# - name: Use Node.js ${{ matrix.node-version }}
# uses: actions/setup-node@v3
# with:
# node-version: ${{ matrix.node-version }}
# cache: 'npm'
#
# - name: Install npm deps
# run: npm ci
#
# # Start Bee Factory environment
# - name: Start Bee Factory environment
# run: npm run bee -- --detach
#
# - name: Run browser tests
# run: npm run test:browser
#
# - name: Debug workflow if failed
# if: failure()
# run: |
# KEYS=$(curl -sSf -X POST https://relay.tunshell.com/api/sessions)
# curl -sSf -X POST -H "Content-Type: application/json" -d "{\"text\": \"**Bee JS**\nDebug -> \`sh <(curl -sSf https://lets.tunshell.com/init.sh) L $(echo $KEYS | jq -r .peer2_key) \${TUNSHELL_SECRET} relay.tunshell.com\`\"}" https://beehive.ethswarm.org/hooks/${{ secrets.WEBHOOK_KEY }}
# echo "Connect to github actions node using"
# echo "sh <(curl -sSf https://lets.tunshell.com/init.sh) L $(echo $KEYS | jq -r .peer2_key) \${TUNSHELL_SECRET} relay.tunshell.com"
# curl -sSf https://lets.tunshell.com/init.sh | sh /dev/stdin T $(echo $KEYS | jq -r .peer1_key) ${{ secrets.TUNSHELL_SECRET }} relay.tunshell.com
10 changes: 5 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@ethersphere/swarm-cid": "^0.1.0",
"@types/readable-stream": "^2.3.13",
"axios": "^1.3.4",
"cafe-utility": "^9.0.1",
"cafe-utility": "^10.8.1",
"elliptic": "^6.5.4",
"fetch-blob": "2.1.2",
"isomorphic-ws": "^4.0.1",
Expand Down
15 changes: 4 additions & 11 deletions src/bee-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,13 @@ export class BeeDebug {
// unnecessary redirects.
this.url = stripLastSlash(url)

const requestOptions: BeeRequestOptions = {
this.requestOptions = {
baseURL: this.url,
timeout: options?.timeout ?? false,
headers: options?.headers,
onRequest: options?.onRequest,
adapter: options?.adapter,
}

if (options?.headers) {
requestOptions.headers = options.headers
}

if (options?.onRequest) {
requestOptions.onRequest = options.onRequest
}

this.requestOptions = requestOptions
}

async getNodeAddresses(options?: BeeRequestOptions): Promise<NodeAddresses> {
Expand Down
19 changes: 6 additions & 13 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Index, IndexBytes, makeFeedReader, makeFeedWriter } from './feed'
import { getJsonData, setJsonData } from './feed/json'
import { areAllSequentialFeedsUpdateRetrievable } from './feed/retrievable'
import { makeTopic, makeTopicFromString } from './feed/topic'
import { assertFeedType, DEFAULT_FEED_TYPE, FeedType } from './feed/type'
import { DEFAULT_FEED_TYPE, FeedType, assertFeedType } from './feed/type'
import * as bytes from './modules/bytes'
import * as bzz from './modules/bzz'
import * as chunk from './modules/chunk'
Expand Down Expand Up @@ -34,9 +34,9 @@ import type {
PssSubscription,
PublicKey,
Reference,
Signer,
SOCReader,
SOCWriter,
Signer,
Tag,
Topic,
UploadOptions,
Expand Down Expand Up @@ -120,20 +120,13 @@ export class Bee {
this.signer = makeSigner(options.signer)
}

const requestOptions: BeeRequestOptions = {
this.requestOptions = {
baseURL: this.url,
timeout: options?.timeout ?? false,
headers: options?.headers,
onRequest: options?.onRequest,
adapter: options?.adapter,
}

if (options?.headers) {
requestOptions.headers = options.headers
}

if (options?.onRequest) {
requestOptions.onRequest = options.onRequest
}

this.requestOptions = requestOptions
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/modules/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function upload(

return {
reference: response.data.reference,
tagUid: makeTagUid(response.headers['swarm-tag']),
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/bzz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function uploadFile(

return {
reference: response.data.reference,
tagUid: makeTagUid(response.headers['swarm-tag']),
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
}
}

Expand Down Expand Up @@ -177,6 +177,6 @@ export async function uploadCollection(

return {
reference: response.data.reference,
tagUid: makeTagUid(response.headers['swarm-tag']),
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
}
}
5 changes: 3 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AxiosAdapter } from 'axios'
import type { Identifier, SingleOwnerChunk } from '../chunk/soc'
import type { FeedUploadOptions } from '../feed'
import type { FeedType } from '../feed/type'
Expand Down Expand Up @@ -96,6 +97,7 @@ export type BeeRequestOptions = {
timeout?: number | false
retry?: number | false
headers?: Record<string, string>
adapter?: AxiosAdapter
onRequest?: (request: BeeRequest) => void
}

Expand All @@ -104,7 +106,6 @@ export interface BeeOptions extends BeeRequestOptions {
* Signer object or private key of the Signer in form of either hex string or Uint8Array that will be default signer for the instance.
*/
signer?: Signer | Uint8Array | string
onRequest?: (request: BeeRequest) => void
}

export interface UploadResultWithCid extends UploadResult {
Expand All @@ -129,7 +130,7 @@ export interface UploadResult {
/**
* Automatically created tag's UID.
*/
tagUid: number
tagUid?: number
}

export interface UploadOptions {
Expand Down
10 changes: 5 additions & 5 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import * as bzz from '../../src/modules/bzz'
import { REFERENCE_HEX_LENGTH } from '../../src/types'
import { makeBytes } from '../../src/utils/bytes'
import { makeEthAddress } from '../../src/utils/eth'
import { bytesToHex, HexString } from '../../src/utils/hex'
import { HexString, bytesToHex } from '../../src/utils/hex'
import {
ERR_TIMEOUT,
FEED_TIMEOUT,
PSS_TIMEOUT,
beeDebugUrl,
beeKyOptions,
beePeerDebugUrl,
Expand All @@ -19,11 +22,8 @@ import {
commonMatchers,
createRandomNodeReadable,
createReadableStream,
ERR_TIMEOUT,
FEED_TIMEOUT,
getPostageBatch,
makeTestTarget,
PSS_TIMEOUT,
randomByteArray,
testChunkPayload,
testIdentity,
Expand Down Expand Up @@ -319,7 +319,7 @@ describe('Bee class', () => {
})

const statusBeforePinning = bee.getPin(result.reference)
await expect(statusBeforePinning).be.rejectedWith('Not Found')
await expect(statusBeforePinning).be.rejectedWith('Request failed with status code 404')

await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong

Expand Down
2 changes: 1 addition & 1 deletion test/integration/modules/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe('modules/bytes', () => {

it('should catch error', async function () {
this.timeout(ERR_TIMEOUT)
await expect(bytes.download(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(bytes.download(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})
})
6 changes: 4 additions & 2 deletions test/integration/modules/bzz.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as bzz from '../../../src/modules/bzz'
import * as tag from '../../../src/modules/tag'
import { Collection, ENCRYPTED_REFERENCE_HEX_LENGTH } from '../../../src/types'
import { makeCollectionFromFS } from '../../../src/utils/collection.node'
import { beeKyOptions, BIG_FILE_TIMEOUT, getPostageBatch, invalidReference, randomByteArray } from '../../utils'
import { BIG_FILE_TIMEOUT, beeKyOptions, getPostageBatch, invalidReference, randomByteArray } from '../../utils'

const BEE_KY_OPTIONS = beeKyOptions()

Expand Down Expand Up @@ -243,7 +243,9 @@ describe('modules/bzz', () => {

it('should catch error', async function () {
this.timeout(BIG_FILE_TIMEOUT)
await expect(bzz.downloadFile(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(bzz.downloadFile(BEE_KY_OPTIONS, invalidReference)).rejectedWith(
'Request failed with status code 404',
)
})

it('should upload bigger file', async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/modules/chunk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ describe('modules/chunk', () => {
it('should catch error', async function () {
this.timeout(ERR_TIMEOUT)

await expect(chunk.download(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(chunk.download(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})
})
4 changes: 2 additions & 2 deletions test/integration/modules/feed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { upload as uploadSOC } from '../../../src/modules/soc'
import type { Topic } from '../../../src/types'
import { HexString, hexToBytes, makeHexString } from '../../../src/utils/hex'
import {
ERR_TIMEOUT,
beeKyOptions,
commonMatchers,
ERR_TIMEOUT,
getPostageBatch,
testIdentity,
tryDeleteChunkFromLocalStorage,
Expand All @@ -32,7 +32,7 @@ describe('modules/feed', () => {
const emptyTopic = '1000000000000000000000000000000000000000000000000000000000000000' as Topic
const feedUpdate = fetchLatestFeedUpdate(BEE_KY_OPTIONS, owner, emptyTopic)

await expect(feedUpdate).rejectedWith('Not Found')
await expect(feedUpdate).rejectedWith('Request failed with status code 404')
})

it('one feed update', async function () {
Expand Down
18 changes: 9 additions & 9 deletions test/integration/modules/pinning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ describe('modules/pin', () => {
it('should not pin a non-existing file', async function () {
this.timeout(ERR_TIMEOUT)

await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should not unpin a non-existing file', async function () {
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})
})

Expand Down Expand Up @@ -68,11 +68,11 @@ describe('modules/pin', () => {
it('should not pin a non-existing collections', async function () {
this.timeout(ERR_TIMEOUT)

await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should not unpin a non-existing collections', async function () {
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})
})

Expand All @@ -92,11 +92,11 @@ describe('modules/pin', () => {
it('should not pin a non-existing data', async function () {
this.timeout(ERR_TIMEOUT)

await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should not unpin a non-existing data', async function () {
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})
})

Expand All @@ -118,11 +118,11 @@ describe('modules/pin', () => {
it('should not pin a non-existing chunk', async function () {
this.timeout(ERR_TIMEOUT)

await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.pin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should not unpin a non-existing chunk', async function () {
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.unpin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should return pinning status of existing chunk', async function () {
Expand All @@ -135,7 +135,7 @@ describe('modules/pin', () => {
})

it('should not return pinning status of non-existing chunk', async function () {
await expect(pinning.getPin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Not Found')
await expect(pinning.getPin(BEE_KY_OPTIONS, invalidReference)).rejectedWith('Request failed with status code 404')
})

it('should return list of pinned chunks', async function () {
Expand Down

0 comments on commit ff78e0d

Please sign in to comment.