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

fix!: conform to Delegated Routing V1 HTTP spec #41

Merged
merged 19 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
feat: add GET /routing/v1/ipns
  • Loading branch information
hacdias committed Oct 13, 2023
commit df60a7fc53f2a86e8a595e85dd8d32d21f17000b
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"@helia/interface": "^2.0.0",
"@libp2p/interface": "^0.1.2",
"fastify": "^4.17.0",
"ipns": "^7.0.1",
"multiformats": "^12.1.1"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getIpnsV1 from './routing/v1/ipns/get.js'
import getPeersV1 from './routing/v1/peers/get.js'
import getProvidersV1 from './routing/v1/providers/get.js'
import type { Helia } from '@helia/interface'
Expand All @@ -6,4 +7,5 @@ import type { FastifyInstance } from 'fastify'
export default function routes (fastify: FastifyInstance, helia: Helia): void {
getProvidersV1(fastify, helia)
getPeersV1(fastify, helia)
getIpnsV1(fastify, helia)
}
47 changes: 47 additions & 0 deletions packages/server/src/routes/routing/v1/ipns/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { peerIdFromCID } from '@libp2p/peer-id'
import { peerIdToRoutingKey } from 'ipns'
import { CID } from 'multiformats/cid'
import type { Helia } from '@helia/interface'
import type { PeerId } from '@libp2p/interface/peer-id'
import type { FastifyInstance } from 'fastify'

interface Params {
name: string
}

export default function getIpnsV1 (fastify: FastifyInstance, helia: Helia): void {
fastify.route<{ Params: Params }>({
method: 'GET',
url: '/routing/v1/ipns/:name',
schema: {
// request needs to have a querystring with a `name` parameter
params: {
type: 'object',
properties: {
name: {
type: 'string'
}
},
required: ['name']
}
},
handler: async (request, reply) => {
let pid: PeerId

try {
// PeerId must be encoded as a Libp2p-key CID.
const { name: cidStr } = request.params
const peerCid = CID.parse(cidStr)
pid = peerIdFromCID(peerCid)
} catch (err) {
// these are .thenables but not .catchables?
reply.code(422).type('text/html').send('Unprocessable Entity') // eslint-disable-line @typescript-eslint/no-floating-promises
return
}

const rawRecord = await helia.libp2p.contentRouting.get(peerIdToRoutingKey(pid))
reply.header('Content-Type', 'application/vnd.ipfs.ipns-record') // eslint-disable-line @typescript-eslint/no-floating-promises
return reply.send(rawRecord)
}
})
}
4 changes: 2 additions & 2 deletions packages/server/src/routes/routing/v1/peers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export default function getPeersV1 (fastify: FastifyInstance, helia: Helia): voi
let pid: PeerId

try {
const { pid: pidStr } = request.params
const peerCid = CID.parse(pidStr)
const { pid: cidStr } = request.params
const peerCid = CID.parse(cidStr)
pid = peerIdFromCID(peerCid)
} catch (err) {
// these are .thenables but not .catchables?
Expand Down