ipfs / helia

An implementation of IPFS in TypeScript
https://helia.io
Other
962 stars 104 forks source link

How to use PubSub on TypeScript ? #546

Open acsses opened 6 months ago

acsses commented 6 months ago

How to use PubSub on TypeScript ?

I want to use PubSub on Typescript and try to use the sample code on this page

https://helia.io/modules/_helia_ipns.html

However when I run the code, Some issues emerged. These error is below .These error happend during compile .

src/main.ts:347:5 - error TS18048: 'libp2pOptions.services' is possibly 'undefined'.

347     libp2pOptions.services.pubsub = gossipsub()
        ~~~~~~~~~~~~~~~~~~~~~~

src/main.ts:363:11 - error TS18046: 'helia.libp2p.services.keychain' is of type 'unknown'.

363     await helia.libp2p.services.keychain.importKey("self_key_pair",exported,user_name)
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/main.ts:368:18 - error TS2345: Argument of type 'HeliaLibp2p<Libp2p<ServiceMap>>' is not assignable to parameter of type 'PubsubRoutingComponents'.
  The types of 'libp2p.services' are incompatible between these types.
    Property 'pubsub' is missing in type 'ServiceMap' but required in type '{ pubsub: PubSub<PubSubEvents>; }'.

368           pubsub(helia)
                     ~~~~~

  node_modules/@helia/ipns/dist/src/routing/pubsub.d.ts:10:13
    10             pubsub: PubSub;
                   ~~~~~~
    'pubsub' is declared here.

src/main.ts:375:9 - error TS2322: Type 'HeliaLibp2p<Libp2p<ServiceMap>>' is not assignable to type 'HeliaLibp2p<Libp2p<DefaultLibp2pServices>>'.
  Type 'Libp2p<ServiceMap>' is not assignable to type 'Libp2p<DefaultLibp2pServices>'.
    Type 'ServiceMap' is missing the following properties from type 'DefaultLibp2pServices': autoNAT, dcutr, delegatedRouting, dht, and 5 more.

375         helia: helia,
            ~~~~~

  src/main.ts:34:5
    34     helia:HeliaLibp2p,
           ~~~~~
    The expected type comes from property 'helia' which is declared here on type 'peer_ctx'

Found 4 errors in the same file, starting at: src/main.ts:347
SgtPooki commented 2 months ago

Is this issue still occuring with the latest? We had an issue with typings a few months ago that I believe is now resolved. Can you try again and share repro code if you are still seeing issues?

github-actions[bot] commented 2 months ago

Oops, seems like we needed more information for this issue, please comment with more details or this issue will be closed in 7 days.

acsses commented 2 months ago

By building Helia node myself, It move.

like:

var libp2p = await createLibp2p({
    services: {
      autoNAT: autoNAT(),
      dcutr: dcutr(),
      delegatedRouting: () => createDelegatedRoutingV1HttpApiClient('https://delegated-ipfs.dev'),
      dht: kadDHT({
        validators: {
          ipns: ipnsValidator
        },
        selectors: {
          ipns: ipnsSelector
        }
      }),
      identify: identify({
          agentVersion
      }),
      ping: ping(),
      relay: circuitRelayServer({
          advertise: true
      }),
      pubsub: gossipsub({ allowPublishToZeroTopicPeers: true }),
      keychain: keychain()
    },
})

const helia = await createHelia({libp2p})

name = ipns(helia,{
    routers: [
      pubsub({
        datastore: helia.datastore,
        libp2p: {
          peerId: helia.libp2p.peerId,
          services: {
            pubsub: helia.libp2p.services.pubsub as PubSub
          }
        }
      })
    ]
})

fs = unixfs(helia)
polus-arcticus commented 3 weeks ago

https://github.com/polus-arcticus/helia-react-components/pull/2 has an attempt to bring pubsub with typescript and i'm seeing some similar type errors as well

src/usePubsubRoom.ts:20:13 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

20       helia.libp2p.addEventListener('connection:open', () => {})
               ~~~~~~

src/usePubsubRoom.ts:21:13 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

21       helia.libp2p.addEventListener('connection:close', () => {})
               ~~~~~~

src/usePubsubRoom.ts:22:13 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

22       helia.libp2p.addEventListener('self:peer:update', () => {})
               ~~~~~~

src/usePubsubRoom.ts:23:13 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

23       helia.libp2p.services.pubsub.addEventListener('message', (event: any) => {
               ~~~~~~

src/usePubsubRoom.ts:30:15 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

30         helia.libp2p.removeEventListener('connection:open', () => {})
                 ~~~~~~

src/usePubsubRoom.ts:31:15 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

31         helia.libp2p.removeEventListener('connection:close', () => {})
                 ~~~~~~

src/usePubsubRoom.ts:32:15 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

32         helia.libp2p.removeEventListener('self:peer:update', () => {})
                 ~~~~~~

src/usePubsubRoom.ts:33:15 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

33         helia.libp2p.services.pubsub.removeEventListener('message', (event: any) => {})
                 ~~~~~~

src/usePubsubRoom.ts:42:30 - error TS2339: Property 'libp2p' does not exist on type 'Helia'.

42       const peerList = helia.libp2p.services.pubsub.getSubscribers(topic)
                                ~~~~~~
polus-arcticus commented 3 weeks ago

https://github.com/ipfs/helia/blob/b723a1d9910c69064eafba965d4d96d753b91297/packages/helia/src/helia-p2p.ts#L14C14-L14C22

seems like the candidate to import, though i cant seem to import { type HeliaP2P} from helia

polus-arcticus commented 3 weeks ago

got it working by referencing this https://github.com/ipfs/helia/blob/b723a1d9910c69064eafba965d4d96d753b91297/packages/interop/src/ipns-pubsub.spec.ts#L36