Skip to content
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: use @helia/verified-fetch #63

Merged
merged 25 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9a080cd
feat: use @helia/verified-fetch
SgtPooki Jan 24, 2024
a27934d
chore: use local tarball so CI runs properly
SgtPooki Jan 24, 2024
b4c61e5
feat: re-enable custom libp2p
SgtPooki Jan 24, 2024
e9f47e5
chore: update fastify
SgtPooki Jan 24, 2024
1c781c4
chore: some minor changes
SgtPooki Jan 24, 2024
daa4fed
fix: fastify/cors patch version
SgtPooki Jan 24, 2024
893a320
chore: package-lock cleared out
SgtPooki Jan 24, 2024
3ad077f
fix: lint issues
SgtPooki Jan 24, 2024
a774085
deps: use @helia/verified-fetch@next
SgtPooki Feb 13, 2024
d9d14e9
feat: content-type-parser for verified-fetch & dep-check fixes
SgtPooki Feb 13, 2024
34afd73
deps: update *-level deps
SgtPooki Feb 13, 2024
7dc2af9
deps: update libp2p deps
SgtPooki Feb 13, 2024
7933118
deps: fix pino-pretty missing error from fastify
SgtPooki Feb 13, 2024
9e2e338
fix: dns-link label decoding
SgtPooki Feb 13, 2024
6564215
chore: remove verified-fetch tarball
SgtPooki Mar 11, 2024
6312031
chore: cleanup console logging and eslint disabling comments
SgtPooki Mar 11, 2024
29c27d6
deps: helia&libp2p deps
SgtPooki Mar 11, 2024
4597384
deps: use full semver strings for deps. remove @types/mime-types
SgtPooki Mar 11, 2024
0630e45
chore: remove unnecessary casting
SgtPooki Mar 11, 2024
a65e3f6
feat: use latest @helia/verified-fetch
SgtPooki Mar 11, 2024
36b2226
chore: set discoverRelays to 0
SgtPooki Mar 11, 2024
4a7a253
chore: address PR comments and cleanup
SgtPooki Mar 11, 2024
a6b08bf
Merge branch 'main' into feat/helia-verified-fetch
SgtPooki Mar 11, 2024
5509431
chore: fix build
SgtPooki Mar 11, 2024
84455c1
chore: disable drand.love from e2e tests
SgtPooki Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/dns-link-labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* For dnslinks see https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header
* DNSLink names include . which means they must be inlined into a single DNS label to provide unique origin and work with wildcard TLS certificates.
*/

/**
* We can receive either a peerId string or dnsLink label string here. PeerId strings do not have dots or dashes.
*/
export function isDnsLabel (label: string): boolean {
return ['-', '.'].some((char) => label.includes(char))
}

/**
* DNSLink label decoding
* * Every standalone - is replaced with .
* * Every remaining -- is replaced with -
*
* @example en-wikipedia--on--ipfs-org.ipns.example.net -> example.net/ipns/en.wikipedia-on-ipfs.org
*/
export function dnsLinkLabelDecoder (linkLabel: string): string {
return linkLabel.replace(/--/g, '-').replace(/-/g, '.')
}

/**
* DNSLink label encoding:
* * Every - is replaced with --
* * Every . is replaced with -
*
* @example example.net/ipns/en.wikipedia-on-ipfs.org → Host: en-wikipedia--on--ipfs-org.ipns.example.net
*/
export function dnsLinkLabelEncoder (linkLabel: string): string {
return linkLabel.replace(/\./g, '-').replace(/-/g, '--')
}
13 changes: 6 additions & 7 deletions src/heliaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createVerifiedFetch } from '@helia/verified-fetch'
import { type FastifyReply, type FastifyRequest, type RouteGenericInterface } from 'fastify'
import { USE_SUBDOMAINS } from './constants.js'
import { contentTypeParser } from './content-type-parser.js'
import { dnsLinkLabelDecoder, isDnsLabel } from './dns-link-labels.js'
import { getCustomHelia } from './getCustomHelia.js'
import { getIpnsAddressDetails } from './ipns-address-utils.js'
import type { PeerId } from '@libp2p/interface'
Expand Down Expand Up @@ -219,13 +220,11 @@ export class HeliaServer {
* Convert HeliaFetchOptions to a URL string compatible with `@helia/verified-fetch`
*/
#getUrlFromArgs ({ address, namespace, relativePath, peerId, cid }: HeliaFetchOptions): string {
// if (peerId != null) {
// // we have a peerId, so we need to pass `ipns://<peerId>/relativePath` to @helia/verified-fetch.
// return `${namespace}://${peerId.toString()}/${relativePath}`
// }
if (relativePath == null || relativePath === '' || relativePath === '/') {
// we have a CID, so we need to pass `ipfs://<cid>` to @helia/verified-fetch.
return `${namespace}://${address}`
// The if a DNSLink returned here needs to be encoded per https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header
if (namespace === 'ipns') {
if (isDnsLabel(address)) {
address = dnsLinkLabelDecoder(address)
}
Copy link
Member

@lidel lidel Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SgtPooki small nit: (unless we check before) we should check if address is a valid CID (with ipns name) first, and apply this normalization only if it is not a valid CID – see some prior art in https://github.com/ipfs/ipfs-companion/blob/7ca6433418909d43ec8801f27e417514614a164c/add-on/src/lib/ipfs-path.js#L71

(better safe than sorry, there could be a multibase encoding which uses . and -)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since helia-verified-fetch handles this entirely now, we should be able to remove this code

}
return `${namespace}://${address}/${relativePath}`
}
Expand Down
Loading