From 31dc945bf46a7ddc7e3add7d9c4b91a5a4e522ae Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 29 Oct 2024 10:24:22 +0000 Subject: [PATCH] chore: lint examples --- examples/go-libp2p-http-proxy/client.js | 58 +++++++++++++++++++ examples/go-libp2p-http-proxy/client.mjs | 57 ------------------ examples/go-libp2p-http-proxy/server.js | 18 ++++++ examples/go-libp2p-http-proxy/server.mjs | 16 ----- .../{client.mjs => client.js} | 16 ++--- .../{server.mjs => server.js} | 24 ++++---- .../client.js | 57 ++++++++++++++++++ .../client.mjs | 57 ------------------ .../server.js | 18 ++++++ .../server.mjs | 16 ----- examples/package.json | 11 ++-- examples/peer-id-auth/{node.mjs => node.js} | 32 +++++----- examples/peer-id-auth/runTest.sh | 4 +- examples/test-runner/main.go | 4 +- .../two-js-peers/{client.mjs => client.js} | 20 ++++--- .../two-js-peers/{server.mjs => server.js} | 13 +++-- 16 files changed, 217 insertions(+), 204 deletions(-) create mode 100644 examples/go-libp2p-http-proxy/client.js delete mode 100644 examples/go-libp2p-http-proxy/client.mjs create mode 100644 examples/go-libp2p-http-proxy/server.js delete mode 100644 examples/go-libp2p-http-proxy/server.mjs rename examples/hono-server-over-libp2p/{client.mjs => client.js} (82%) rename examples/hono-server-over-libp2p/{server.mjs => server.js} (77%) create mode 100644 examples/js-libp2p-client-and-node-server/client.js delete mode 100644 examples/js-libp2p-client-and-node-server/client.mjs create mode 100644 examples/js-libp2p-client-and-node-server/server.js delete mode 100644 examples/js-libp2p-client-and-node-server/server.mjs rename examples/peer-id-auth/{node.mjs => node.js} (74%) rename examples/two-js-peers/{client.mjs => client.js} (81%) rename examples/two-js-peers/{server.mjs => server.js} (85%) diff --git a/examples/go-libp2p-http-proxy/client.js b/examples/go-libp2p-http-proxy/client.js new file mode 100644 index 0000000..8d25612 --- /dev/null +++ b/examples/go-libp2p-http-proxy/client.js @@ -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() +} diff --git a/examples/go-libp2p-http-proxy/client.mjs b/examples/go-libp2p-http-proxy/client.mjs deleted file mode 100644 index 32593a3..0000000 --- a/examples/go-libp2p-http-proxy/client.mjs +++ /dev/null @@ -1,57 +0,0 @@ -import { noise } from '@chainsafe/libp2p-noise' -import { multiaddr } from '@multiformats/multiaddr' -import { yamux } from '@chainsafe/libp2p-yamux' -import { tcp } from '@libp2p/tcp' -import { createLibp2p } from 'libp2p' -import { http } from '@libp2p/http-fetch' - -const node = await createLibp2p({ - // libp2p nodes are started by default, pass false to override this - start: false, - addresses: { - listen: [] - }, - transports: [tcp()], - connectionEncryption: [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() -} - - diff --git a/examples/go-libp2p-http-proxy/server.js b/examples/go-libp2p-http-proxy/server.js new file mode 100644 index 0000000..2f6b1f4 --- /dev/null +++ b/examples/go-libp2p-http-proxy/server.js @@ -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) +}) diff --git a/examples/go-libp2p-http-proxy/server.mjs b/examples/go-libp2p-http-proxy/server.mjs deleted file mode 100644 index 1c9b085..0000000 --- a/examples/go-libp2p-http-proxy/server.mjs +++ /dev/null @@ -1,16 +0,0 @@ -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) -}); diff --git a/examples/hono-server-over-libp2p/client.mjs b/examples/hono-server-over-libp2p/client.js similarity index 82% rename from examples/hono-server-over-libp2p/client.mjs rename to examples/hono-server-over-libp2p/client.js index f2726c6..5e3c4d4 100644 --- a/examples/hono-server-over-libp2p/client.mjs +++ b/examples/hono-server-over-libp2p/client.js @@ -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 @@ -13,7 +15,7 @@ const node = await createLibp2p({ listen: [] }, transports: [tcp()], - connectionEncryption: [noise()], + connectionEncrypters: [noise()], streamMuxers: [yamux()], services: { http: http() } }) @@ -31,7 +33,7 @@ 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. @@ -39,7 +41,7 @@ if (!isHTTPTransport && serverMA.getPeerId() === null) { 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() @@ -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() } diff --git a/examples/hono-server-over-libp2p/server.mjs b/examples/hono-server-over-libp2p/server.js similarity index 77% rename from examples/hono-server-over-libp2p/server.mjs rename to examples/hono-server-over-libp2p/server.js index b75c044..45f7506 100644 --- a/examples/hono-server-over-libp2p/server.mjs +++ b/examples/hono-server-over-libp2p/server.js @@ -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() @@ -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() @@ -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)) @@ -62,4 +63,3 @@ server.close() // stop libp2p node.stop() console.error('libp2p has stopped') - diff --git a/examples/js-libp2p-client-and-node-server/client.js b/examples/js-libp2p-client-and-node-server/client.js new file mode 100644 index 0000000..d54a76a --- /dev/null +++ b/examples/js-libp2p-client-and-node-server/client.js @@ -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() +} diff --git a/examples/js-libp2p-client-and-node-server/client.mjs b/examples/js-libp2p-client-and-node-server/client.mjs deleted file mode 100644 index 32593a3..0000000 --- a/examples/js-libp2p-client-and-node-server/client.mjs +++ /dev/null @@ -1,57 +0,0 @@ -import { noise } from '@chainsafe/libp2p-noise' -import { multiaddr } from '@multiformats/multiaddr' -import { yamux } from '@chainsafe/libp2p-yamux' -import { tcp } from '@libp2p/tcp' -import { createLibp2p } from 'libp2p' -import { http } from '@libp2p/http-fetch' - -const node = await createLibp2p({ - // libp2p nodes are started by default, pass false to override this - start: false, - addresses: { - listen: [] - }, - transports: [tcp()], - connectionEncryption: [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() -} - - diff --git a/examples/js-libp2p-client-and-node-server/server.js b/examples/js-libp2p-client-and-node-server/server.js new file mode 100644 index 0000000..2f6b1f4 --- /dev/null +++ b/examples/js-libp2p-client-and-node-server/server.js @@ -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) +}) diff --git a/examples/js-libp2p-client-and-node-server/server.mjs b/examples/js-libp2p-client-and-node-server/server.mjs deleted file mode 100644 index 1c9b085..0000000 --- a/examples/js-libp2p-client-and-node-server/server.mjs +++ /dev/null @@ -1,16 +0,0 @@ -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) -}); diff --git a/examples/package.json b/examples/package.json index 611ffe3..629fa68 100644 --- a/examples/package.json +++ b/examples/package.json @@ -1,17 +1,18 @@ { "name": "js-libp2p-whatwg-examples", "version": "0.0.1", + "type": "module", "dependencies": { - "@chainsafe/libp2p-noise": "^15.0.0", - "@chainsafe/libp2p-yamux": "^6.0.2", + "@chainsafe/libp2p-noise": "^16.0.0", + "@chainsafe/libp2p-yamux": "^7.0.1", "@hono/node-server": "^1.11.2", "@libp2p/http-fetch": "file:..", - "@libp2p/peer-id": "^4.1.3", - "@libp2p/tcp": "^9.0.26", + "@libp2p/peer-id": "^5.0.7", + "@libp2p/tcp": "^10.0.11", "@multiformats/multiaddr": "^12.3.0", "@multiformats/multiaddr-to-uri": "^10.1.0", "hono": "^4.4.4", - "libp2p": "^1.6.0" + "libp2p": "^2.2.1" }, "license": "Apache-2.0 OR MIT" } diff --git a/examples/peer-id-auth/node.mjs b/examples/peer-id-auth/node.js similarity index 74% rename from examples/peer-id-auth/node.mjs rename to examples/peer-id-auth/node.js index 6482dee..f977543 100644 --- a/examples/peer-id-auth/node.mjs +++ b/examples/peer-id-auth/node.js @@ -1,9 +1,11 @@ +/* eslint-disable no-console */ + +import { serve } from '@hono/node-server' import { unmarshalPrivateKey } from '@libp2p/crypto/keys' -import { WellKnownHandler, WELL_KNOWN_PROTOCOLS } from '@libp2p/http-fetch/well-known-handler.js' import { ClientAuth, HTTPPeerIDAuthProto, ServerAuth } from '@libp2p/http-fetch/auth.js' -import { Hono } from 'hono' -import { serve } from '@hono/node-server' +import { WellKnownHandler, WELL_KNOWN_PROTOCOLS } from '@libp2p/http-fetch/well-known-handler.js' import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import { Hono } from 'hono' const myID = await createEd25519PeerId() const privKey = await unmarshalPrivateKey(myID.privateKey) @@ -13,16 +15,16 @@ const args = process.argv.slice(2) if (args.length === 1 && args[0] === 'client') { // Client mode const client = new ClientAuth(privKey) - const observedPeerID = await client.authenticateServer(fetch, "localhost:8001", "http://localhost:8001/auth") - console.log("Server ID:", observedPeerID.toString()) + const observedPeerID = await client.authenticateServer(fetch, 'localhost:8001', 'http://localhost:8001/auth') + console.log('Server ID:', observedPeerID.toString()) - const authenticatedReq = new Request("http://localhost:8001/log-my-id", { + const authenticatedReq = new Request('http://localhost:8001/log-my-id', { headers: { - Authorization: client.bearerAuthHeader("localhost:8001") + Authorization: client.bearerAuthHeader('localhost:8001') } }) await fetch(authenticatedReq) - console.log("Client ID:", myID.toString()) + console.log('Client ID:', myID.toString()) process.exit(0) } // Server mode @@ -38,27 +40,27 @@ app.get(WELL_KNOWN_PROTOCOLS, async (c) => { app.all('/auth', (c) => { return httpServerAuth.httpHandler(addHeadersProxy(c.req)) }) -wellKnownHandler.registerProtocol(HTTPPeerIDAuthProto, "/auth") +wellKnownHandler.registerProtocol(HTTPPeerIDAuthProto, '/auth') app.all('/log-my-id', async (c) => { try { - const id = await httpServerAuth.unwrapBearerToken("localhost:8001", c.req.header('Authorization')) - console.log("Client ID:", id.toString()) + const id = await httpServerAuth.unwrapBearerToken('localhost:8001', c.req.header('Authorization')) + console.log('Client ID:', id.toString()) } catch (e) { console.error(e) return c.text(e.message, { status: 400 }) } c.status(200) }) -wellKnownHandler.registerProtocol("/log-my-id/1", "/log-my-id") +wellKnownHandler.registerProtocol('/log-my-id/1', '/log-my-id') const server = serve({ fetch: app.fetch, port: 8001, - hostname: '127.0.0.1', + hostname: '127.0.0.1' }) -console.log("Server ID:", myID.toString()) +console.log('Server ID:', myID.toString()) // wait for SIGINT await new Promise(resolve => process.on('SIGINT', resolve)) @@ -70,7 +72,7 @@ server.close() // exposes headers and how hono's Request type exposes headers. // The standard Request type exposes headers as a Headers object, while // hono's Request type has a function `header(name: string): string | null` to get the header -function addHeadersProxy(req) { +function addHeadersProxy (req) { return new Proxy(req, { get: (target, prop) => { if (prop === 'headers') { diff --git a/examples/peer-id-auth/runTest.sh b/examples/peer-id-auth/runTest.sh index 6da6deb..752e5b5 100755 --- a/examples/peer-id-auth/runTest.sh +++ b/examples/peer-id-auth/runTest.sh @@ -27,8 +27,8 @@ trap cleanup EXIT GO_SERVER="./go-node" GO_CLIENT="./go-node client" -NODE_SERVER="node node.mjs" -NODE_CLIENT="node node.mjs client" +NODE_SERVER="node node.js" +NODE_CLIENT="node node.js client" # Define server arrays SERVERS=("$GO_SERVER" "$NODE_SERVER") diff --git a/examples/test-runner/main.go b/examples/test-runner/main.go index 4b8f9d1..48c545e 100644 --- a/examples/test-runner/main.go +++ b/examples/test-runner/main.go @@ -63,7 +63,7 @@ func runTest(hasProxy bool) error { } func runServer(ctx context.Context) (*exec.Cmd, []string, error) { - serverCmd := exec.CommandContext(ctx, "node", "server.mjs") + serverCmd := exec.CommandContext(ctx, "node", "server.js") serverCmd.Stderr = os.Stderr serverCmd.Cancel = func() error { serverCmd.Process.Signal(os.Interrupt) @@ -93,7 +93,7 @@ func runServer(ctx context.Context) (*exec.Cmd, []string, error) { } func runClient(serverMultiaddr string) error { - clientCmd := exec.Command("node", "client.mjs", serverMultiaddr) + clientCmd := exec.Command("node", "client.js", serverMultiaddr) clientCmd.Stderr = os.Stderr return clientCmd.Run() } diff --git a/examples/two-js-peers/client.mjs b/examples/two-js-peers/client.js similarity index 81% rename from examples/two-js-peers/client.mjs rename to examples/two-js-peers/client.js index 53b8e94..08bfa61 100644 --- a/examples/two-js-peers/client.mjs +++ b/examples/two-js-peers/client.js @@ -1,11 +1,13 @@ +/* eslint-disable no-console */ + import { noise } from '@chainsafe/libp2p-noise' -import { multiaddr } from '@multiformats/multiaddr' import { yamux } from '@chainsafe/libp2p-yamux' -import { tcp } from '@libp2p/tcp' -import { createLibp2p } from 'libp2p' import { http } from '@libp2p/http-fetch' -import { sendPing } from '@libp2p/http-fetch/ping.js' +import { sendPing } from '@libp2p/http-fetch/ping.js' import { peerIdFromString } from '@libp2p/peer-id' +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 @@ -14,7 +16,7 @@ const node = await createLibp2p({ listen: [] }, transports: [tcp()], - connectionEncryption: [noise()], + connectionEncrypters: [noise()], streamMuxers: [yamux()], services: { http: http() } }) @@ -32,7 +34,7 @@ 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. @@ -40,13 +42,13 @@ if (!isHTTPTransport && serverMA.getPeerId() === null) { serverMA = serverMA.encapsulate(`/p2p/${conn.remotePeer.toString()}`) } -console.error("Making request to", `${serverMA.toString()}`) +console.error('Making request to', `${serverMA.toString()}`) try { for (let i = 0; i < 2; i++) { const start = new Date().getTime() await sendPing(node, serverMA) const end = new Date().getTime() - console.error("Got response! took", end - start, "ms") + console.error('Got response! took', end - start, 'ms') console.log(end - start) // sleep 1s await new Promise(resolve => setTimeout(resolve, 1000)) @@ -59,7 +61,7 @@ try { const start = new Date().getTime() await sendPing(node, peerIdFromString(serverMA.getPeerId())) const end = new Date().getTime() - console.error("Got response! took", end - start, "ms") + console.error('Got response! took', end - start, 'ms') console.log(end - start) } } finally { diff --git a/examples/two-js-peers/server.mjs b/examples/two-js-peers/server.js similarity index 85% rename from examples/two-js-peers/server.mjs rename to examples/two-js-peers/server.js index d3126b7..aa5234a 100644 --- a/examples/two-js-peers/server.mjs +++ b/examples/two-js-peers/server.js @@ -1,9 +1,11 @@ +/* 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 { http } from '@libp2p/http-fetch' import { PING_PROTOCOL_ID, servePing } from '@libp2p/http-fetch/ping.js' +import { tcp } from '@libp2p/tcp' +import { createLibp2p } from 'libp2p' const node = await createLibp2p({ // libp2p nodes are started by default, pass false to override this @@ -12,9 +14,9 @@ const node = await createLibp2p({ listen: ['/ip4/127.0.0.1/tcp/8000'] }, transports: [tcp()], - connectionEncryption: [noise()], + connectionEncrypters: [noise()], streamMuxers: [yamux()], - services: {http: http()} + services: { http: http() } }) // start libp2p @@ -24,7 +26,7 @@ console.error('libp2p has started') const listenAddrs = node.getMultiaddrs() console.error('libp2p is listening on the following address:') console.log(listenAddrs[0].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) node.services.http.handleHTTPProtocol(PING_PROTOCOL_ID, '/ping', servePing) @@ -34,4 +36,3 @@ await new Promise(resolve => setTimeout(resolve, 100000)) // stop libp2p await node.stop() console.error('libp2p has stopped') -