Skip to content

Commit aa8ffb8

Browse files
authored
fix: remove @helia/interface dep (#59)
No need to require the entire Helia api
1 parent 7e2bb0b commit aa8ffb8

File tree

8 files changed

+46
-79
lines changed

8 files changed

+46
-79
lines changed

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
},
152152
"dependencies": {
153153
"@fastify/cors": "^8.3.0",
154-
"@helia/interface": "^2.0.0",
155154
"@libp2p/interface": "^0.1.2",
156155
"@libp2p/peer-id": "^3.0.3",
157156
"fastify": "^4.17.0",
@@ -160,6 +159,7 @@
160159
"raw-body": "^2.5.2"
161160
},
162161
"devDependencies": {
162+
"@helia/interface": "^2.0.0",
163163
"@libp2p/peer-id-factory": "^3.0.5",
164164
"@multiformats/multiaddr": "^12.1.3",
165165
"@types/sinon": "^17.0.0",

packages/server/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import fastify, {
5757
type FastifyInstance
5858
} from 'fastify'
5959
import routes from './routes/index.js'
60-
import type { Helia } from '@helia/interface'
60+
import type { Libp2p } from '@libp2p/interface'
6161

6262
export interface ServerInit {
6363
fastify?: FastifyInstance
@@ -67,15 +67,15 @@ export interface ServerInit {
6767
/**
6868
* Create and return a Routing V1 HTTP API server
6969
*/
70-
export async function createDelegatedRoutingV1HttpApiServer (helia: Helia, init: ServerInit = {}): Promise<FastifyInstance> {
70+
export async function createDelegatedRoutingV1HttpApiServer (helia: { libp2p: Libp2p }, init: ServerInit = {}): Promise<FastifyInstance> {
7171
const server = init.fastify ?? fastify()
7272
await server.register(cors, {
7373
origin: '*',
7474
methods: ['GET', 'OPTIONS'],
7575
strictPreflight: false
7676
})
7777

78-
routes(server, helia)
78+
routes(server, helia.libp2p)
7979

8080
await server.listen(init.listen ?? {
8181
port: 0

packages/server/src/routes/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ import getIpnsV1 from './routing/v1/ipns/get.js'
3838
import putIpnsV1 from './routing/v1/ipns/put.js'
3939
import getPeersV1 from './routing/v1/peers/get.js'
4040
import getProvidersV1 from './routing/v1/providers/get.js'
41-
import type { Helia } from '@helia/interface'
41+
import type { Libp2p } from '@libp2p/interface'
4242
import type { FastifyInstance } from 'fastify'
4343

44-
export default function routes (fastify: FastifyInstance, helia: Helia): void {
45-
getProvidersV1(fastify, helia)
46-
getPeersV1(fastify, helia)
47-
getIpnsV1(fastify, helia)
48-
putIpnsV1(fastify, helia)
44+
export default function routes (fastify: FastifyInstance, libp2p: Libp2p): void {
45+
getProvidersV1(fastify, libp2p)
46+
getPeersV1(fastify, libp2p)
47+
getIpnsV1(fastify, libp2p)
48+
putIpnsV1(fastify, libp2p)
4949
}

packages/server/src/routes/routing/v1/ipns/get.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { peerIdFromCID } from '@libp2p/peer-id'
22
import { peerIdToRoutingKey } from 'ipns'
33
import { CID } from 'multiformats/cid'
4-
import type { Helia } from '@helia/interface'
4+
import type { Libp2p } from '@libp2p/interface'
55
import type { PeerId } from '@libp2p/interface/peer-id'
66
import type { FastifyInstance } from 'fastify'
77

88
interface Params {
99
name: string
1010
}
1111

12-
export default function getIpnsV1 (fastify: FastifyInstance, helia: Helia): void {
12+
export default function getIpnsV1 (fastify: FastifyInstance, libp2p: Libp2p): void {
1313
fastify.route<{ Params: Params }>({
1414
method: 'GET',
1515
url: '/routing/v1/ipns/:name',
@@ -43,7 +43,7 @@ export default function getIpnsV1 (fastify: FastifyInstance, helia: Helia): void
4343
return reply.code(422).type('text/html').send('Unprocessable Entity')
4444
}
4545

46-
const rawRecord = await helia.libp2p.contentRouting.get(peerIdToRoutingKey(peerId), {
46+
const rawRecord = await libp2p.contentRouting.get(peerIdToRoutingKey(peerId), {
4747
signal: controller.signal
4848
})
4949

packages/server/src/routes/routing/v1/ipns/put.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { peerIdToRoutingKey } from 'ipns'
33
import { ipnsValidator } from 'ipns/validator'
44
import { CID } from 'multiformats/cid'
55
import getRawBody from 'raw-body'
6-
import type { Helia } from '@helia/interface'
6+
import type { Libp2p } from '@libp2p/interface'
77
import type { PeerId } from '@libp2p/interface/peer-id'
88
import type { FastifyInstance } from 'fastify'
99

1010
interface Params {
1111
name: string
1212
}
1313

14-
export default function putIpnsV1 (fastify: FastifyInstance, helia: Helia): void {
14+
export default function putIpnsV1 (fastify: FastifyInstance, libp2p: Libp2p): void {
1515
fastify.addContentTypeParser('application/vnd.ipfs.ipns-record', function (request, payload, done) {
1616
getRawBody(payload)
1717
.then(buff => { done(null, buff) })
@@ -55,7 +55,7 @@ export default function putIpnsV1 (fastify: FastifyInstance, helia: Helia): void
5555
const body: Uint8Array = request.body
5656
await ipnsValidator(peerIdToRoutingKey(peerId), body)
5757

58-
await helia.libp2p.contentRouting.put(peerIdToRoutingKey(peerId), body, {
58+
await libp2p.contentRouting.put(peerIdToRoutingKey(peerId), body, {
5959
signal: controller.signal
6060
})
6161

packages/server/src/routes/routing/v1/peers/get.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { PassThrough } from 'node:stream'
22
import { peerIdFromCID } from '@libp2p/peer-id'
33
import { CID } from 'multiformats/cid'
4-
import type { Helia } from '@helia/interface'
4+
import type { Libp2p } from '@libp2p/interface'
55
import type { PeerId } from '@libp2p/interface/peer-id'
66
import type { FastifyInstance } from 'fastify'
77

88
interface Params {
99
peerId: string
1010
}
1111

12-
export default function getPeersV1 (fastify: FastifyInstance, helia: Helia): void {
12+
export default function getPeersV1 (fastify: FastifyInstance, libp2p: Libp2p): void {
1313
fastify.route<{ Params: Params }>({
1414
method: 'GET',
1515
url: '/routing/v1/peers/:peerId',
@@ -42,7 +42,7 @@ export default function getPeersV1 (fastify: FastifyInstance, helia: Helia): voi
4242
return reply.code(422).type('text/html').send('Unprocessable Entity')
4343
}
4444

45-
const peerInfo = await helia.libp2p.peerRouting.findPeer(peerId, {
45+
const peerInfo = await libp2p.peerRouting.findPeer(peerId, {
4646
signal: controller.signal
4747
})
4848
const peerRecord = {

packages/server/src/routes/routing/v1/providers/get.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PassThrough } from 'node:stream'
22
import { CID } from 'multiformats/cid'
3-
import type { Helia } from '@helia/interface'
4-
import type { AbortOptions } from '@libp2p/interface'
3+
import type { AbortOptions, Libp2p } from '@libp2p/interface'
54
import type { FastifyInstance } from 'fastify'
65

76
interface Params {
@@ -21,7 +20,7 @@ interface Providers {
2120

2221
const MAX_PROVIDERS = 100
2322

24-
export default function getProvidersV1 (fastify: FastifyInstance, helia: Helia): void {
23+
export default function getProvidersV1 (fastify: FastifyInstance, libp2p: Libp2p): void {
2524
fastify.route<{ Params: Params }>({
2625
method: 'GET',
2726
url: '/routing/v1/providers/:cid',
@@ -57,7 +56,7 @@ export default function getProvidersV1 (fastify: FastifyInstance, helia: Helia):
5756
const stream = new PassThrough()
5857

5958
// wait until we have the first result
60-
const iterable = streamingHandler(cid, helia, {
59+
const iterable = streamingHandler(cid, libp2p, {
6160
signal: controller.signal
6261
})
6362
const result = await iterable.next()
@@ -84,7 +83,7 @@ export default function getProvidersV1 (fastify: FastifyInstance, helia: Helia):
8483
.send(stream)
8584
}
8685
} else {
87-
const result = await nonStreamingHandler(cid, helia, {
86+
const result = await nonStreamingHandler(cid, libp2p, {
8887
signal: controller.signal
8988
})
9089

@@ -98,10 +97,10 @@ export default function getProvidersV1 (fastify: FastifyInstance, helia: Helia):
9897
})
9998
}
10099

101-
async function * streamingHandler (cid: CID, helia: Helia, options?: AbortOptions): AsyncGenerator<PeerRecord, void, unknown> {
100+
async function * streamingHandler (cid: CID, libp2p: Libp2p, options?: AbortOptions): AsyncGenerator<PeerRecord, void, unknown> {
102101
let provs = 0
103102

104-
for await (const prov of helia.libp2p.contentRouting.findProviders(cid, options)) {
103+
for await (const prov of libp2p.contentRouting.findProviders(cid, options)) {
105104
yield {
106105
Schema: 'peer',
107106
ID: prov.id.toString(),
@@ -116,11 +115,11 @@ async function * streamingHandler (cid: CID, helia: Helia, options?: AbortOption
116115
}
117116
}
118117

119-
async function nonStreamingHandler (cid: CID, helia: Helia, options?: AbortOptions): Promise<Providers> {
118+
async function nonStreamingHandler (cid: CID, libp2p: Libp2p, options?: AbortOptions): Promise<Providers> {
120119
const providers = []
121120

122121
try {
123-
for await (const prov of helia.libp2p.contentRouting.findProviders(cid, options)) {
122+
for await (const prov of libp2p.contentRouting.findProviders(cid, options)) {
124123
providers.push({
125124
Schema: 'peer',
126125
ID: prov.id.toString(),

packages/server/test/index.spec.ts

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CID } from 'multiformats'
88
import { stubInterface } from 'sinon-ts'
99
import { createDelegatedRoutingV1HttpApiServer } from '../src/index.js'
1010
import type { Helia } from '@helia/interface'
11+
import type { Libp2p } from '@libp2p/interface'
1112
import type { PeerInfo } from '@libp2p/interface/peer-info'
1213
import type { FastifyInstance } from 'fastify'
1314
import type { StubbedInstance } from 'sinon-ts'
@@ -18,7 +19,9 @@ describe('delegated-routing-v1-http-api-server', () => {
1819
let url: URL
1920

2021
beforeEach(async () => {
21-
helia = stubInterface<Helia>()
22+
helia = stubInterface<Helia>({
23+
libp2p: stubInterface<Libp2p>()
24+
})
2225
server = await createDelegatedRoutingV1HttpApiServer(helia, {
2326
listen: {
2427
host: '127.0.0.1',
@@ -66,12 +69,7 @@ describe('delegated-routing-v1-http-api-server', () => {
6669
})
6770

6871
it('GET providers returns 404 if no providers are found', async () => {
69-
helia.libp2p = {
70-
// @ts-expect-error incomplete implementation
71-
contentRouting: {
72-
findProviders: async function * () {}
73-
}
74-
}
72+
helia.libp2p.contentRouting.findProviders = async function * () {}
7573

7674
const res = await fetch(`${url}routing/v1/providers/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn`, {
7775
method: 'GET'
@@ -81,12 +79,7 @@ describe('delegated-routing-v1-http-api-server', () => {
8179
})
8280

8381
it('GET providers returns 404 if no providers are found when streaming', async () => {
84-
helia.libp2p = {
85-
// @ts-expect-error incomplete implementation
86-
contentRouting: {
87-
findProviders: async function * () {}
88-
}
89-
}
82+
helia.libp2p.contentRouting.findProviders = async function * () {}
9083

9184
const res = await fetch(`${url}routing/v1/providers/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn`, {
9285
method: 'GET',
@@ -114,14 +107,9 @@ describe('delegated-routing-v1-http-api-server', () => {
114107
protocols: []
115108
}
116109

117-
helia.libp2p = {
118-
// @ts-expect-error incomplete implementation
119-
contentRouting: {
120-
findProviders: async function * () {
121-
yield provider1
122-
yield provider2
123-
}
124-
}
110+
helia.libp2p.contentRouting.findProviders = async function * () {
111+
yield provider1
112+
yield provider2
125113
}
126114

127115
const res = await fetch(`${url}routing/v1/providers/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn`, {
@@ -156,14 +144,9 @@ describe('delegated-routing-v1-http-api-server', () => {
156144
protocols: []
157145
}
158146

159-
helia.libp2p = {
160-
// @ts-expect-error incomplete implementation
161-
contentRouting: {
162-
findProviders: async function * () {
163-
yield provider1
164-
yield provider2
165-
}
166-
}
147+
helia.libp2p.contentRouting.findProviders = async function * () {
148+
yield provider1
149+
yield provider2
167150
}
168151

169152
const res = await fetch(`${url}routing/v1/providers/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn`, {
@@ -213,13 +196,8 @@ describe('delegated-routing-v1-http-api-server', () => {
213196
protocols: ['transport-bitswap']
214197
}
215198

216-
helia.libp2p = {
217-
// @ts-expect-error incomplete implementation
218-
peerRouting: {
219-
findPeer: async function () {
220-
return peer
221-
}
222-
}
199+
helia.libp2p.peerRouting.findPeer = async function () {
200+
return peer
223201
}
224202

225203
const res = await fetch(`${url}routing/v1/peers/${peer.id.toCID().toString()}`, {
@@ -254,13 +232,8 @@ describe('delegated-routing-v1-http-api-server', () => {
254232
const cid = CID.parse('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4')
255233
const record = await createIpnsRecord(peerId, cid, 0, 1000)
256234

257-
helia.libp2p = {
258-
// @ts-expect-error incomplete implementation
259-
contentRouting: {
260-
get: async function () {
261-
return marshalIpnsRecord(record)
262-
}
263-
}
235+
helia.libp2p.contentRouting.get = async function () {
236+
return marshalIpnsRecord(record)
264237
}
265238

266239
const res = await fetch(`${url}routing/v1/ipns/${peerId.toCID().toString()}`, {
@@ -284,14 +257,9 @@ describe('delegated-routing-v1-http-api-server', () => {
284257
let putKey: Uint8Array = new Uint8Array()
285258
let putValue: Uint8Array = new Uint8Array()
286259

287-
helia.libp2p = {
288-
// @ts-expect-error incomplete implementation
289-
contentRouting: {
290-
put: async function (key: Uint8Array, value: Uint8Array) {
291-
putKey = key
292-
putValue = value
293-
}
294-
}
260+
helia.libp2p.contentRouting.put = async function (key: Uint8Array, value: Uint8Array) {
261+
putKey = key
262+
putValue = value
295263
}
296264

297265
const res = await fetch(`${url}routing/v1/ipns/${peerId.toCID().toString()}`, {

0 commit comments

Comments
 (0)