-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add static http gateway routing #515
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { uriToMultiaddr } from '@multiformats/uri-to-multiaddr' | ||
import { CID } from 'multiformats/cid' | ||
import { identity } from 'multiformats/hashes/identity' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
import type { Provider, Routing, RoutingOptions } from '@helia/interface' | ||
import { peerIdSymbol, type PeerId, type PeerInfo } from '@libp2p/interface' | ||
import type { MultihashDigest, Version } from 'multiformats' | ||
|
||
export interface HTTPGatwayRouterInit { | ||
gateways?: Array<URL | string> | ||
} | ||
|
||
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv | ||
const LIBP2P_KEY_CODE = 0x72 | ||
const inspect = Symbol.for('nodejs.util.inspect.custom') | ||
|
||
class URLPeerId { | ||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readonly type = 'url' | ||
readonly multihash: MultihashDigest | ||
readonly privateKey?: Uint8Array | ||
readonly publicKey?: Uint8Array | ||
readonly url: string | ||
|
||
constructor (url: URL) { | ||
this.url = url.toString() | ||
this.multihash = identity.digest(uint8ArrayFromString(this.url)) | ||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[inspect] (): string { | ||
return `PeerId(${this.url})` | ||
} | ||
|
||
readonly [peerIdSymbol] = true | ||
|
||
toString (): string { | ||
return this.toCID().toString() | ||
} | ||
|
||
toCID (): CID { | ||
return CID.createV1(LIBP2P_KEY_CODE, this.multihash) | ||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
toBytes (): Uint8Array { | ||
return this.toCID().bytes | ||
} | ||
|
||
equals (other?: PeerId | Uint8Array | string): boolean { | ||
if (other == null) { | ||
return false | ||
} | ||
|
||
if (other instanceof Uint8Array) { | ||
other = uint8ArrayToString(other) | ||
} | ||
|
||
return other.toString() === this.toString() | ||
} | ||
} | ||
|
||
function toPeerInfo (url: string | URL): PeerInfo { | ||
url = url.toString() | ||
|
||
return { | ||
id: new URLPeerId(new URL(url)), | ||
multiaddrs: [ | ||
uriToMultiaddr(url) | ||
] | ||
} | ||
} | ||
|
||
class HTTPGatwayRouter implements Partial<Routing> { | ||
private readonly gateways: PeerInfo[] | ||
|
||
constructor (init: HTTPGatwayRouterInit = {}) { | ||
this.gateways = (init.gateways ?? []).map(url => toPeerInfo(url)) | ||
} | ||
|
||
async * findProviders (cid: CID<unknown, number, number, Version>, options?: RoutingOptions | undefined): AsyncIterable<Provider> { | ||
yield * this.gateways.map(info => ({ | ||
...info, | ||
protocols: ['transport-ipfs-gateway-http'] | ||
})) | ||
} | ||
} | ||
|
||
/** | ||
* Returns a static list of HTTP Gateways as providers | ||
*/ | ||
export function httpGatewayRouting (init: HTTPGatwayRouterInit = {}): Partial<Routing> { | ||
return new HTTPGatwayRouter(init) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expect } from 'aegir/chai' | ||
import all from 'it-all' | ||
import { CID } from 'multiformats' | ||
import { httpGatewayRouting } from '../src/http-gateway-routing.js' | ||
|
||
describe('http-gateway-routing', () => { | ||
it('should find providers', async () => { | ||
const gateway = 'https://example.com' | ||
const routing = httpGatewayRouting({ | ||
gateways: [ | ||
gateway | ||
] | ||
}) | ||
|
||
const cid = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae') | ||
|
||
const providers = await all(routing.findProviders?.(cid) ?? []) | ||
|
||
expect(providers).to.have.lengthOf(1) | ||
expect(providers).to.have.nested.property('[0].protocols').that.includes('transport-ipfs-gateway-http') | ||
expect(providers[0].multiaddrs.map(ma => ma.toString())).to.include('/dns4/example.com/tcp/443/https') | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to use the code for
transport-ipfs-gateway-http
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I have no strong preference here, really it just needs to be unique to allow comparison between peer identifiers.
Reading the PR that added that code the intention was for numbers to be used instead of peer protocol strings so I don't think there'll be any collision?