chore: add js-libp2p@v2.2.1 to transport-interop
This commit is contained in:
parent
0168dc5aca
commit
07ad8a847f
|
@ -0,0 +1,117 @@
|
|||
/* eslint-disable no-console */
|
||||
import http from 'http'
|
||||
import { pEvent } from 'p-event'
|
||||
import { createClient } from 'redis'
|
||||
|
||||
const redisAddr = process.env.redis_addr || 'redis:6379'
|
||||
const transport = process.env.transport
|
||||
const isDialer = process.env.is_dialer === 'true'
|
||||
|
||||
/** @type {import('aegir/types').PartialOptions} */
|
||||
export default {
|
||||
test: {
|
||||
browser: {
|
||||
config: {
|
||||
// Ignore self signed certificates
|
||||
browserContextOptions: { ignoreHTTPSErrors: true }
|
||||
}
|
||||
},
|
||||
async before () {
|
||||
// import after build is complete
|
||||
const { createRelay } = await import('./dist/test/fixtures/relay.js')
|
||||
|
||||
let relayNode
|
||||
let relayAddr
|
||||
if (transport === 'webrtc' && !isDialer) {
|
||||
relayNode = await createRelay()
|
||||
|
||||
const sortByNonLocalIp = (a, b) => {
|
||||
if (a.toString().includes('127.0.0.1')) {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
relayAddr = relayNode.getMultiaddrs().sort(sortByNonLocalIp)[0].toString()
|
||||
}
|
||||
|
||||
const redisClient = createClient({
|
||||
url: `redis://${redisAddr}`
|
||||
})
|
||||
redisClient.on('error', (err) => {
|
||||
console.error('Redis client error:', err)
|
||||
})
|
||||
await redisClient.connect()
|
||||
|
||||
const requestListener = async function (req, res) {
|
||||
const requestJSON = await new Promise(resolve => {
|
||||
let body = ''
|
||||
req.on('data', function (data) {
|
||||
body += data
|
||||
})
|
||||
|
||||
req.on('end', function () {
|
||||
resolve(JSON.parse(body))
|
||||
})
|
||||
})
|
||||
|
||||
try {
|
||||
const redisRes = await redisClient.sendCommand(requestJSON)
|
||||
|
||||
if (redisRes == null) {
|
||||
console.error('Redis failure - sent', requestJSON, 'received', redisRes)
|
||||
|
||||
res.writeHead(500, {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
})
|
||||
res.end(JSON.stringify({
|
||||
message: 'Redis sent back null'
|
||||
}))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
res.writeHead(200, {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
})
|
||||
res.end(JSON.stringify(redisRes))
|
||||
} catch (err) {
|
||||
console.error('Error in redis command:', err)
|
||||
res.writeHead(500, {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
})
|
||||
res.end(err.toString())
|
||||
}
|
||||
}
|
||||
|
||||
const proxyServer = http.createServer(requestListener)
|
||||
proxyServer.listen(0)
|
||||
|
||||
await pEvent(proxyServer, 'listening', {
|
||||
signal: AbortSignal.timeout(5000)
|
||||
})
|
||||
|
||||
return {
|
||||
redisClient,
|
||||
relayNode,
|
||||
proxyServer,
|
||||
env: {
|
||||
...process.env,
|
||||
RELAY_ADDR: relayAddr,
|
||||
REDIS_PROXY_PORT: proxyServer.address().port
|
||||
}
|
||||
}
|
||||
},
|
||||
async after (_, { proxyServer, redisClient, relayNode }) {
|
||||
await new Promise(resolve => {
|
||||
proxyServer?.close(() => resolve())
|
||||
})
|
||||
|
||||
try {
|
||||
// We don't care if this fails
|
||||
await redisClient?.disconnect()
|
||||
await relayNode?.stop()
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
# syntax=docker/dockerfile:1
|
||||
|
||||
# Copied since we won't have the repo to use if expanding from cache.
|
||||
|
||||
# Workaround: https://github.com/docker/cli/issues/996
|
||||
ARG BASE_IMAGE=node-js-libp2p-head
|
||||
FROM ${BASE_IMAGE} as js-libp2p-base
|
||||
|
||||
FROM mcr.microsoft.com/playwright
|
||||
|
||||
COPY --from=js-libp2p-base /app/ /app/
|
||||
WORKDIR /app
|
||||
|
||||
# We install browsers here instead of the cached version so that we use the latest browsers at run time.
|
||||
# Ideally this would also be pinned, but playwright controls this, so there isn't much we can do about it.
|
||||
# By installing here, we avoid installing it at test time.
|
||||
RUN npx playwright install-deps
|
||||
RUN npx playwright install
|
||||
|
||||
# Options: chromium, firefox, webkit
|
||||
ARG BROWSER=chromium
|
||||
ENV BROWSER=${BROWSER}
|
||||
|
||||
ENTRYPOINT npm test -- -t browser -- --browser $BROWSER
|
|
@ -0,0 +1,17 @@
|
|||
# Here because we want to fetch the node_modules within docker so that it's
|
||||
# installed on the same platform the test is run. Otherwise tools like `esbuild` will fail to run
|
||||
FROM node:lts
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json .aegir.js tsconfig.json ./
|
||||
COPY src ./src
|
||||
COPY test ./test
|
||||
|
||||
# disable colored output and CLI animation from test runners
|
||||
ENV CI true
|
||||
|
||||
RUN npm ci
|
||||
RUN npm run build
|
||||
|
||||
ENTRYPOINT npm test -- -t node
|
|
@ -0,0 +1,35 @@
|
|||
image_name := js-v1.x
|
||||
|
||||
# TODO Enable webkit once https://github.com/libp2p/js-libp2p/pull/1627 is in
|
||||
all: image.json chromium-image.json firefox-image.json update-lock-file
|
||||
|
||||
# Necessary because multistage builds require a docker image name rather than a digest to be used
|
||||
load-image-json: image.json
|
||||
docker image tag $$(jq -r .imageID image.json) ${image_name}
|
||||
|
||||
chromium-image.json: load-image-json BrowserDockerfile
|
||||
docker build -f BrowserDockerfile --build-arg=BASE_IMAGE=${image_name} --build-arg=BROWSER=chromium -t chromium-${image_name} .
|
||||
docker image inspect chromium-${image_name} -f "{{.Id}}" | \
|
||||
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@
|
||||
|
||||
firefox-image.json: load-image-json BrowserDockerfile
|
||||
docker build -f BrowserDockerfile --build-arg=BASE_IMAGE=${image_name} --build-arg=BROWSER=firefox -t firefox-${image_name} .
|
||||
docker image inspect firefox-${image_name} -f "{{.Id}}" | \
|
||||
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@
|
||||
|
||||
# We update the lock file here so that we make sure we are always using the correct lock file.
|
||||
# If this changes, CI will fail since there are unstaged changes.
|
||||
update-lock-file: image.json
|
||||
CONTAINER_ID=$$(docker create $$(jq -r .imageID image.json)); \
|
||||
docker cp $$CONTAINER_ID:/app/package-lock.json ./package-lock.json; \
|
||||
docker rm $$CONTAINER_ID
|
||||
|
||||
image.json:
|
||||
docker build -t ${image_name} -f ./Dockerfile .
|
||||
docker image inspect ${image_name} -f "{{.Id}}" | \
|
||||
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@
|
||||
|
||||
clean:
|
||||
rm -rf image.json *-image.json
|
||||
|
||||
.PHONY: all clean browser-images load-image-json
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"name": "@libp2p/transport-interop-libp2p-1.x",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"clean": "aegir clean",
|
||||
"build": "aegir build --bundle false",
|
||||
"test": "aegir test",
|
||||
"lint": "aegir lint",
|
||||
"dep-check": "aegir dep-check"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@chainsafe/libp2p-noise": "^15.0.0",
|
||||
"@chainsafe/libp2p-yamux": "^6.0.2",
|
||||
"@libp2p/circuit-relay-v2": "^1.0.24",
|
||||
"@libp2p/identify": "^2.0.2",
|
||||
"@libp2p/interface": "^1.4.0",
|
||||
"@libp2p/mplex": "^10.0.24",
|
||||
"@libp2p/ping": "^1.0.19",
|
||||
"@libp2p/tcp": "^9.0.26",
|
||||
"@libp2p/webrtc": "^4.0.33",
|
||||
"@libp2p/websockets": "^8.0.24",
|
||||
"@libp2p/webtransport": "^4.0.32",
|
||||
"@multiformats/multiaddr": "^12.1.10",
|
||||
"aegir": "^42.2.11",
|
||||
"libp2p": "^2.2.1",
|
||||
"p-event": "^6.0.1",
|
||||
"redis": "^4.6.10"
|
||||
},
|
||||
"browser": {
|
||||
"@libp2p/tcp": false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
// Everything is defined in the test folder
|
||||
|
||||
export { }
|
|
@ -0,0 +1,55 @@
|
|||
/* eslint-disable no-console */
|
||||
/* eslint-env mocha */
|
||||
|
||||
import { multiaddr } from '@multiformats/multiaddr'
|
||||
import { getLibp2p } from './fixtures/get-libp2p.js'
|
||||
import { redisProxy } from './fixtures/redis-proxy.js'
|
||||
import type { Libp2p } from '@libp2p/interface'
|
||||
import type { PingService } from '@libp2p/ping'
|
||||
|
||||
const isDialer: boolean = process.env.is_dialer === 'true'
|
||||
const timeoutSecs: string = process.env.test_timeout_secs ?? '180'
|
||||
|
||||
describe('ping test (dialer)', function () {
|
||||
if (!isDialer) {
|
||||
return
|
||||
}
|
||||
|
||||
// make the default timeout longer than the listener timeout
|
||||
this.timeout((parseInt(timeoutSecs) * 1000) + 30000)
|
||||
let node: Libp2p<{ ping: PingService }>
|
||||
|
||||
beforeEach(async () => {
|
||||
node = await getLibp2p()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
// Shutdown libp2p node
|
||||
try {
|
||||
// We don't care if this fails
|
||||
await node.stop()
|
||||
} catch { }
|
||||
})
|
||||
|
||||
it('should dial and ping', async function () {
|
||||
let [, otherMaStr]: string[] = await redisProxy(['BLPOP', 'listenerAddr', timeoutSecs])
|
||||
|
||||
// Hack until these are merged:
|
||||
// - https://github.com/multiformats/js-multiaddr-to-uri/pull/120
|
||||
otherMaStr = otherMaStr.replace('/tls/ws', '/wss')
|
||||
|
||||
const otherMa = multiaddr(otherMaStr)
|
||||
const handshakeStartInstant = Date.now()
|
||||
|
||||
console.error(`node ${node.peerId.toString()} dials: ${otherMa}`)
|
||||
await node.dial(otherMa)
|
||||
|
||||
console.error(`node ${node.peerId.toString()} pings: ${otherMa}`)
|
||||
const pingRTT = await node.services.ping.ping(multiaddr(otherMa))
|
||||
const handshakePlusOneRTT = Date.now() - handshakeStartInstant
|
||||
console.log(JSON.stringify({
|
||||
handshakePlusOneRTTMillis: handshakePlusOneRTT,
|
||||
pingRTTMilllis: pingRTT
|
||||
}))
|
||||
})
|
||||
})
|
|
@ -0,0 +1,130 @@
|
|||
/* eslint-disable complexity */
|
||||
|
||||
import { noise } from '@chainsafe/libp2p-noise'
|
||||
import { yamux } from '@chainsafe/libp2p-yamux'
|
||||
import { mplex } from '@libp2p/mplex'
|
||||
import { tcp } from '@libp2p/tcp'
|
||||
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import * as filters from '@libp2p/websockets/filters'
|
||||
import { webTransport } from '@libp2p/webtransport'
|
||||
import { type Libp2pOptions, createLibp2p } from 'libp2p'
|
||||
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
|
||||
import { type Identify, identify } from '@libp2p/identify'
|
||||
import { type PingService, ping } from '@libp2p/ping'
|
||||
import type { Libp2p } from '@libp2p/interface'
|
||||
|
||||
const isDialer: boolean = process.env.is_dialer === 'true'
|
||||
|
||||
// Setup libp2p node
|
||||
const TRANSPORT = process.env.transport
|
||||
const SECURE_CHANNEL = process.env.security
|
||||
const MUXER = process.env.muxer
|
||||
const IP = process.env.ip ?? '0.0.0.0'
|
||||
|
||||
export async function getLibp2p (): Promise<Libp2p<{ ping: PingService }>> {
|
||||
const options: Libp2pOptions<{ ping: PingService, identify: Identify }> = {
|
||||
start: true,
|
||||
connectionManager: {
|
||||
minConnections: 0
|
||||
},
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: async () => false
|
||||
},
|
||||
services: {
|
||||
ping: ping(),
|
||||
identify: identify()
|
||||
}
|
||||
}
|
||||
|
||||
switch (TRANSPORT) {
|
||||
case 'tcp':
|
||||
options.transports = [tcp()]
|
||||
options.addresses = {
|
||||
listen: isDialer ? [] : [`/ip4/${IP}/tcp/0`]
|
||||
}
|
||||
break
|
||||
case 'webtransport':
|
||||
options.transports = [webTransport()]
|
||||
if (!isDialer) {
|
||||
throw new Error('WebTransport is not supported as a listener')
|
||||
}
|
||||
break
|
||||
case 'webrtc-direct':
|
||||
options.transports = [webRTCDirect()]
|
||||
options.addresses = {
|
||||
listen: isDialer ? [] : [`/ip4/${IP}/udp/0/webrtc-direct`]
|
||||
}
|
||||
break
|
||||
case 'webrtc':
|
||||
options.transports = [webRTC(),
|
||||
webSockets({ filter: filters.all }), // ws needed to connect to relay
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1
|
||||
}) // needed to use the relay
|
||||
]
|
||||
options.addresses = {
|
||||
listen: isDialer ? [] : ['/webrtc']
|
||||
}
|
||||
break
|
||||
case 'ws':
|
||||
options.transports = [webSockets()]
|
||||
options.addresses = {
|
||||
listen: isDialer ? [] : [`/ip4/${IP}/tcp/0/ws`]
|
||||
}
|
||||
break
|
||||
case 'wss':
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
||||
options.transports = [webSockets()]
|
||||
options.addresses = {
|
||||
listen: isDialer ? [] : [`/ip4/${IP}/tcp/0/wss`]
|
||||
}
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown transport: ${TRANSPORT ?? '???'}`)
|
||||
}
|
||||
|
||||
let skipSecureChannel = false
|
||||
let skipMuxer = false
|
||||
switch (TRANSPORT) {
|
||||
case 'webtransport':
|
||||
case 'webrtc-direct':
|
||||
skipSecureChannel = true
|
||||
skipMuxer = true
|
||||
break
|
||||
case 'webrtc':
|
||||
skipSecureChannel = true
|
||||
skipMuxer = true
|
||||
// Setup yamux and noise to connect to the relay node
|
||||
options.streamMuxers = [yamux()]
|
||||
options.connectionEncryption = [noise()]
|
||||
break
|
||||
default:
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
if (!skipSecureChannel) {
|
||||
switch (SECURE_CHANNEL) {
|
||||
case 'noise':
|
||||
options.connectionEncryption = [noise()]
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown secure channel: ${SECURE_CHANNEL ?? ''}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipMuxer) {
|
||||
switch (MUXER) {
|
||||
case 'mplex':
|
||||
options.streamMuxers = [mplex()]
|
||||
break
|
||||
case 'yamux':
|
||||
options.streamMuxers = [yamux()]
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown muxer: ${MUXER ?? '???'}`)
|
||||
}
|
||||
}
|
||||
|
||||
return createLibp2p(options)
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
export async function redisProxy (commands: any[]): Promise<any> {
|
||||
const res = await fetch(`http://localhost:${process.env.REDIS_PROXY_PORT}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(commands)
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error('Redis command failed')
|
||||
}
|
||||
|
||||
return res.json()
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
import { noise } from '@chainsafe/libp2p-noise'
|
||||
import { yamux } from '@chainsafe/libp2p-yamux'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import * as filters from '@libp2p/websockets/filters'
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
|
||||
import { identify } from '@libp2p/identify'
|
||||
import type { Libp2p } from '@libp2p/interface'
|
||||
|
||||
export async function createRelay (): Promise<Libp2p> {
|
||||
const server = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
},
|
||||
transports: [
|
||||
webSockets({
|
||||
filter: filters.all
|
||||
})
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux()],
|
||||
services: {
|
||||
identify: identify(),
|
||||
relay: circuitRelayServer({
|
||||
reservations: {
|
||||
maxReservations: Infinity,
|
||||
applyDefaultLimit: false
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return server
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/* eslint-disable no-console */
|
||||
/* eslint-env mocha */
|
||||
|
||||
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
||||
import { getLibp2p } from './fixtures/get-libp2p.js'
|
||||
import { redisProxy } from './fixtures/redis-proxy.js'
|
||||
import type { Libp2p } from '@libp2p/interface'
|
||||
import type { PingService } from '@libp2p/ping'
|
||||
|
||||
const isDialer: boolean = process.env.is_dialer === 'true'
|
||||
const timeoutSecs: string = process.env.test_timeout_secs ?? '180'
|
||||
|
||||
describe('ping test (listener)', function () {
|
||||
if (isDialer) {
|
||||
return
|
||||
}
|
||||
|
||||
// make the default timeout longer than the listener timeout
|
||||
this.timeout((parseInt(timeoutSecs) * 1000) + 30000)
|
||||
let node: Libp2p<{ ping: PingService }>
|
||||
|
||||
beforeEach(async () => {
|
||||
node = await getLibp2p()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
// Shutdown libp2p node
|
||||
try {
|
||||
// We don't care if this fails
|
||||
await node.stop()
|
||||
} catch { }
|
||||
})
|
||||
|
||||
it('should listen for ping', async function () {
|
||||
const sortByNonLocalIp = (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 => {
|
||||
if (a.toString().includes('127.0.0.1')) {
|
||||
return 1
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
let multiaddrs = node.getMultiaddrs().sort(sortByNonLocalIp).map(ma => ma.toString())
|
||||
|
||||
const transport = process.env.transport
|
||||
if (transport === 'webrtc') {
|
||||
const relayAddr = process.env.RELAY_ADDR
|
||||
const hasWebrtcMultiaddr = new Promise<string[]>((resolve) => {
|
||||
const abortController = new AbortController()
|
||||
node.addEventListener('self:peer:update', (event) => {
|
||||
const webrtcMas = node.getMultiaddrs().filter(ma => ma.toString().includes('/webrtc'))
|
||||
if (webrtcMas.length > 0) {
|
||||
resolve(webrtcMas.sort(sortByNonLocalIp).map(ma => ma.toString()))
|
||||
}
|
||||
abortController.abort()
|
||||
}, { signal: abortController.signal })
|
||||
})
|
||||
|
||||
if (relayAddr == null || relayAddr === '') {
|
||||
throw new Error('No relayAddr')
|
||||
}
|
||||
// const conn = await node.dial(multiaddr(relayAddr))
|
||||
console.error('dial relay')
|
||||
await node.dial(multiaddr(relayAddr))
|
||||
console.error('wait for relay reservation')
|
||||
multiaddrs = await hasWebrtcMultiaddr
|
||||
}
|
||||
|
||||
console.error('inform redis of dial address')
|
||||
// Send the listener addr over the proxy server so this works on both the Browser and Node
|
||||
await redisProxy(['RPUSH', 'listenerAddr', multiaddrs[0]])
|
||||
// Wait
|
||||
console.error('wait for incoming ping')
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * parseInt(timeoutSecs, 10)))
|
||||
})
|
||||
})
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"extends": "aegir/src/config/tsconfig.aegir.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"test"
|
||||
]
|
||||
}
|
|
@ -310,5 +310,23 @@
|
|||
"mplex",
|
||||
"yamux"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "js-v2.2",
|
||||
"transports": [
|
||||
"tcp",
|
||||
"ws",
|
||||
{
|
||||
"name": "wss",
|
||||
"onlyDial": true
|
||||
}
|
||||
],
|
||||
"secureChannels": [
|
||||
"noise"
|
||||
],
|
||||
"muxers": [
|
||||
"mplex",
|
||||
"yamux"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue