Skip to content

Commit

Permalink
chore: lint examples
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Oct 29, 2024
1 parent 19b7a56 commit 31dc945
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 204 deletions.
58 changes: 58 additions & 0 deletions examples/go-libp2p-http-proxy/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { http } from '@libp2p/http-fetch'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'

const node = await createLibp2p({
// libp2p nodes are started by default, pass false to override this
start: false,
addresses: {
listen: []
},
transports: [tcp()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
services: { http: http() }
})

// start libp2p
await node.start()
console.error('libp2p has started')

// Read server multiaddr from the command line
const serverAddr = process.argv[2]
if (!serverAddr) {
console.error('Please provide the server multiaddr as an argument')
process.exit(1)
}

let serverMA = multiaddr(serverAddr)

// check if this is an http transport multiaddr
const isHTTPTransport = serverMA.protos().find(p => p.name === 'http')
if (!isHTTPTransport && serverMA.getPeerId() === null) {
// Learn the peer id of the server. This lets us reuse the connection for all our HTTP requests.
// Otherwise js-libp2p will open a new connection for each request.
const conn = await node.dial(serverMA)
serverMA = serverMA.encapsulate(`/p2p/${conn.remotePeer.toString()}`)
}

console.error(`Making request to ${serverMA.toString()}`)
try {
const resp = await node.services.http.fetch(new Request(`multiaddr:${serverMA}`))
const respBody = await resp.text()
if (resp.status !== 200) {
throw new Error(`Unexpected status code: ${resp.status}`)
}
if (respBody !== 'Hello, World!') {
throw new Error(`Unexpected response body: ${respBody}`)
}
console.error('Got response:', respBody)
} finally {
// stop libp2p
await node.stop()
}
57 changes: 0 additions & 57 deletions examples/go-libp2p-http-proxy/client.mjs

This file was deleted.

18 changes: 18 additions & 0 deletions examples/go-libp2p-http-proxy/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-console */

import http from 'http'

const port = 55776

const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, World!')
})

server.listen(port, () => {
console.error(`Server running at: http://localhost:${port}/`)
// Print multiaddr on stdout for test
console.error("Server's multiaddr is:")
console.log(`/dns4/localhost/tcp/${port}/http`)
console.log('') // Empty line to signal we have no more addresses (for test runner)
})
16 changes: 0 additions & 16 deletions examples/go-libp2p-http-proxy/server.mjs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { multiaddr } from '@multiformats/multiaddr'
import { yamux } from '@chainsafe/libp2p-yamux'
import { http } from '@libp2p/http-fetch'
import { sendPing } from '@libp2p/http-fetch/ping.js'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { http } from '@libp2p/http-fetch'
import { sendPing } from '@libp2p/http-fetch/ping.js'

const node = await createLibp2p({
// libp2p nodes are started by default, pass false to override this
Expand All @@ -13,7 +15,7 @@ const node = await createLibp2p({
listen: []
},
transports: [tcp()],
connectionEncryption: [noise()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
services: { http: http() }
})
Expand All @@ -31,15 +33,15 @@ if (!serverAddr) {

let serverMA = multiaddr(serverAddr)

const isHTTPTransport = serverMA.protos().find(p => p.name === "http") // check if this is an http transport multiaddr
const isHTTPTransport = serverMA.protos().find(p => p.name === 'http') // check if this is an http transport multiaddr
if (!isHTTPTransport && serverMA.getPeerId() === null) {
// Learn the peer id of the server. This lets us reuse the connection for all our HTTP requests.
// Otherwise js-libp2p will open a new connection for each request.
const conn = await node.dial(serverMA)
serverMA = serverMA.encapsulate(`/p2p/${conn.remotePeer.toString()}`)
}

console.error("Making request to", `${serverMA.toString()}`)
console.error('Making request to', `${serverMA.toString()}`)
try {
const resp = await node.services.http.fetch(new Request(`multiaddr:${serverMA}` + `/http-path/${encodeURIComponent('my-app')}`))
const respBody = await resp.text()
Expand All @@ -53,7 +55,7 @@ try {
const start = new Date().getTime()
await sendPing(node, serverMA)
const end = new Date().getTime()
console.error("HTTP Ping took", end - start, "ms")
console.error('HTTP Ping took', end - start, 'ms')
} finally {
await node.stop()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import { createLibp2p } from 'libp2p'
import { serve } from '@hono/node-server'
import { WELL_KNOWN_PROTOCOLS, httpCustomServer } from '@libp2p/http-fetch'
import { PING_PROTOCOL_ID, servePing } from '@libp2p/http-fetch/ping.js'
import { tcp } from '@libp2p/tcp'
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { createLibp2p } from 'libp2p'

const app = new Hono()

Expand All @@ -16,22 +18,22 @@ const node = await createLibp2p({
listen: ['/ip4/127.0.0.1/tcp/8000']
},
transports: [tcp()],
connectionEncryption: [noise()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
services: {http: httpCustomServer({customHTTPHandler: app.fetch.bind(app)})}
services: { http: httpCustomServer({ customHTTPHandler: app.fetch.bind(app) }) }
})

app.get(WELL_KNOWN_PROTOCOLS, async (c) => {
return node.services.http.serveWellKnownProtocols(c.req)
})
app.get('/my-app', (c) => c.text('Hono!'))
node.services.http.registerProtocol('/example-app/0.0.1', "/my-app")
node.services.http.registerProtocol('/example-app/0.0.1', '/my-app')

// Register HTTP ping protocol
app.all('/ping', (c) => {
return servePing(c.req)
})
node.services.http.registerProtocol(PING_PROTOCOL_ID, "/ping")
node.services.http.registerProtocol(PING_PROTOCOL_ID, '/ping')

// start libp2p
await node.start()
Expand All @@ -41,17 +43,16 @@ console.error('libp2p has started')
const server = serve({
fetch: app.fetch,
port: 8001,
hostname: '127.0.0.1',
hostname: '127.0.0.1'
})

const listenAddrs = node.getMultiaddrs()
console.error('libp2p is listening on the following addresses:')
console.log(`/ip4/127.0.0.1/tcp/8001/http`)
console.log('/ip4/127.0.0.1/tcp/8001/http')
for (const addr of listenAddrs) {
console.log(addr.toString())
}
console.log("") // Empty line to signal we have no more addresses (for test runner)

console.log('') // Empty line to signal we have no more addresses (for test runner)

// wait for SIGINT
await new Promise(resolve => process.on('SIGINT', resolve))
Expand All @@ -62,4 +63,3 @@ server.close()
// stop libp2p
node.stop()
console.error('libp2p has stopped')

57 changes: 57 additions & 0 deletions examples/js-libp2p-client-and-node-server/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable no-console */

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { http } from '@libp2p/http-fetch'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'

const node = await createLibp2p({
// libp2p nodes are started by default, pass false to override this
start: false,
addresses: {
listen: []
},
transports: [tcp()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
services: { http: http() }
})

// start libp2p
await node.start()
console.error('libp2p has started')

// Read server multiaddr from the command line
const serverAddr = process.argv[2]
if (!serverAddr) {
console.error('Please provide the server multiaddr as an argument')
process.exit(1)
}

let serverMA = multiaddr(serverAddr)

const isHTTPTransport = serverMA.protos().find(p => p.name === 'http') // check if this is an http transport multiaddr
if (!isHTTPTransport && serverMA.getPeerId() === null) {
// Learn the peer id of the server. This lets us reuse the connection for all our HTTP requests.
// Otherwise js-libp2p will open a new connection for each request.
const conn = await node.dial(serverMA)
serverMA = serverMA.encapsulate(`/p2p/${conn.remotePeer.toString()}`)
}

console.error('Making request to', `${serverMA.toString()}`)
try {
const resp = await node.services.http.fetch(new Request(`multiaddr:${serverMA}`))
const respBody = await resp.text()
if (resp.status !== 200) {
throw new Error(`Unexpected status code: ${resp.status}`)
}
if (respBody !== 'Hello, World!') {
throw new Error(`Unexpected response body: ${respBody}`)
}
console.error('Got response:', respBody)
} finally {
// stop libp2p
await node.stop()
}
57 changes: 0 additions & 57 deletions examples/js-libp2p-client-and-node-server/client.mjs

This file was deleted.

Loading

0 comments on commit 31dc945

Please sign in to comment.