Skip to content

Commit

Permalink
chore: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Feb 21, 2024
1 parent 9d77f79 commit 2aae3b9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/block-brokers/src/trustless-gateway/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ ProgressOptions<TrustlessGatewayGetBlockProgressEvents>
this.log = components.logger.forComponent('helia:trustless-gateway-block-broker')
this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS)
.map((gw) => {
if(typeof gw === 'string' || gw instanceof URL) {
if (typeof gw === 'string' || gw instanceof URL) {
// backward compatibility defaults to path gateway
return new TrustlessGateway(gw, false)
}

Check warning on line 26 in packages/block-brokers/src/trustless-gateway/broker.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/src/trustless-gateway/broker.ts#L24-L26

Added lines #L24 - L26 were not covered by tests

return new TrustlessGateway(gw.url, gw.isSubdomain)
return new TrustlessGateway(gw.url, gw.supportsSubdomains)
})
}

Expand Down
8 changes: 4 additions & 4 deletions packages/block-brokers/src/trustless-gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import type { ProgressEvent } from 'progress-events'

export const DEFAULT_TRUSTLESS_GATEWAYS: TrustlessGatewayUrl[] = [
// 2024-02-20: IPNS and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
{ url: 'https://trustless-gateway.link', isSubdomain: false },
{ url: 'https://trustless-gateway.link', supportsSubdomains: false },

// 2024-02-20: IPNS and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
{ url: 'https://cloudflare-ipfs.com', isSubdomain: false },
{ url: 'https://cloudflare-ipfs.com', supportsSubdomains: false },

// 2024-02-20: IPNS, Origin, and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
{ url: 'https://4everland.io', isSubdomain: true },
{ url: 'https://4everland.io', supportsSubdomains: true }
]

interface TrustlessGatewayUrl {
url: string | URL
isSubdomain: boolean
supportsSubdomains: boolean
}

export type TrustlessGatewayGetBlockProgressEvents =
Expand Down
22 changes: 10 additions & 12 deletions packages/block-brokers/src/trustless-gateway/trustless-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CID } from 'multiformats/cid'
import { base32 } from 'multiformats/bases/base32'
import type { CID } from 'multiformats/cid'

/**
* A `TrustlessGateway` keeps track of the number of attempts, errors, and
Expand All @@ -13,7 +13,7 @@ export class TrustlessGateway {
/**
* Whether this gateway is a subdomain resolution style gateway
*/
public isSubdomain: boolean
public supportsSubdomains: boolean

/**
* The number of times this gateway has been attempted to be used to fetch a
Expand Down Expand Up @@ -43,26 +43,24 @@ export class TrustlessGateway {
*/
#successes = 0

constructor(url: URL | string, isSubdomain: boolean = false) {
constructor (url: URL | string, supportsSubdomains: boolean = false) {
this.url = url instanceof URL ? url : new URL(url)
this.isSubdomain = isSubdomain
this.supportsSubdomains = supportsSubdomains
}

/**
* Fetch a raw block from `this.url` following the specification defined at
* https://specs.ipfs.tech/http-gateways/trustless-gateway/
*/
async getRawBlock(cid: CID, signal?: AbortSignal): Promise<Uint8Array> {
async getRawBlock (cid: CID, signal?: AbortSignal): Promise<Uint8Array> {
const gwUrl = this.getGwUrl(cid)

Check warning on line 56 in packages/block-brokers/src/trustless-gateway/trustless-gateway.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/src/trustless-gateway/trustless-gateway.ts#L56

Added line #L56 was not covered by tests

// necessary as not every gateway supports dag-cbor, but every should support
// sending raw block as-is
gwUrl.search = '?format=raw'

if (signal?.aborted === true) {
throw new Error(
`Signal to fetch raw block for CID ${cid} from gateway ${this.url} was aborted prior to fetch`,
)
throw new Error(`Signal to fetch raw block for CID ${cid} from gateway ${this.url} was aborted prior to fetch`)
}

try {
Expand All @@ -72,7 +70,7 @@ export class TrustlessGateway {
headers: {
// also set header, just in case ?format= is filtered out by some
// reverse proxy

Check warning on line 72 in packages/block-brokers/src/trustless-gateway/trustless-gateway.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/src/trustless-gateway/trustless-gateway.ts#L71-L72

Added lines #L71 - L72 were not covered by tests
Accept: 'application/vnd.ipld.raw',
Accept: 'application/vnd.ipld.raw'
},
cache: 'force-cache'
})
Expand All @@ -96,10 +94,10 @@ export class TrustlessGateway {
/**
* Construct the Gateway URL for a CID
*/
getGwUrl(cid: CID): URL {
getGwUrl (cid: CID): URL {
const gwUrl = new URL(this.url)

if (this.isSubdomain) {
if (this.supportsSubdomains) {
gwUrl.hostname = `${cid.toString(base32)}.ipfs.${gwUrl.hostname}`
} else {
gwUrl.pathname = `/ipfs/${cid.toString()}`
Expand Down Expand Up @@ -137,7 +135,7 @@ export class TrustlessGateway {
*
* Play around with the below reliability function at https://www.desmos.com/calculator/d6hfhf5ukm
*/
return this.#successes / (this.#attempts + (this.#errors * 3))
return this.#successes / (this.#attempts + this.#errors * 3)

Check warning on line 138 in packages/block-brokers/src/trustless-gateway/trustless-gateway.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/src/trustless-gateway/trustless-gateway.ts#L138

Added line #L138 was not covered by tests
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/block-brokers/test/trustless-gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ describe('trustless-gateway-block-broker', () => {
const pathGw = new TrustlessGateway('http://localhost:8080')
const subdomainGw = new TrustlessGateway('https://dweb.link', true)

expect(pathGw.getGwUrl(blocks[0].cid).hostname).to.equal(`localhost`)
expect(pathGw.getGwUrl(blocks[0].cid).toString()).to.equal(`http://localhost:8080/ipfs/bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq`)
expect(pathGw.getGwUrl(blocks[0].cid).hostname).to.equal('localhost')
expect(pathGw.getGwUrl(blocks[0].cid).toString()).to.equal('http://localhost:8080/ipfs/bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq')
expect(pathGw.getGwUrl(blocks[1].cid).toString()).to.equal(`http://localhost:8080/ipfs/${blocks[1].cid.toString()}`)

expect(subdomainGw.getGwUrl(blocks[0].cid).hostname).to.equal(`bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq.ipfs.dweb.link`)
expect(subdomainGw.getGwUrl(blocks[0].cid).toString()).to.equal(`https://bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq.ipfs.dweb.link/`)
expect(subdomainGw.getGwUrl(blocks[0].cid).hostname).to.equal('bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq.ipfs.dweb.link')
expect(subdomainGw.getGwUrl(blocks[0].cid).toString()).to.equal('https://bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aq.ipfs.dweb.link/')
expect(subdomainGw.getGwUrl(blocks[1].cid).toString()).to.equal(`https://${blocks[1].cid.toString()}.ipfs.dweb.link/`)
})
})

0 comments on commit 2aae3b9

Please sign in to comment.