-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: re-enable js-libp2p under node.js
- Loading branch information
1 parent
eb21984
commit 0c5a863
Showing
6 changed files
with
1,448 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
NPM_PACKAGE_VERSION := 1.1.6 | ||
NPM_PACKAGE := @libp2p/perf | ||
NPM_PACKAGE_NAME := $(NPM_PACKAGE)@$(NPM_PACKAGE_VERSION) | ||
|
||
SOURCE_DIR := js-libp2p-protocol-perf | ||
|
||
DOCKER_IMAGE := node:18.17.1 | ||
DOCKER_RUN := docker run --rm -v "$(shell pwd)/$(SOURCE_DIR)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE) | ||
DOCKER_IMAGE := node:20-alpine | ||
DOCKER_RUN := docker run --rm -v "$(shell pwd)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE) | ||
|
||
all: perf | ||
|
||
perf: | ||
mkdir -p $(SOURCE_DIR) | ||
$(DOCKER_RUN) npm install $(NPM_PACKAGE_NAME) | ||
$(DOCKER_RUN) npm ci | ||
|
||
clean: | ||
rm -rf js-libp2p-* | ||
rm -rf node_modules | ||
|
||
.PHONY: all clean perf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys' | ||
import { createFromPrivKey } from '@libp2p/peer-id-factory' | ||
import { tcp } from '@libp2p/tcp' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { perfService } from '@libp2p/perf' | ||
import { parseArgs } from 'node:util' | ||
|
||
const testPrivKey = 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaVjLQL3J8TWH6wKBgQDSChzItkqWX11CNstJ9zJyUE20I7LrpyBJNgG1gtvz3ZMUQCn3PxxHtQzN9n1P0mSSYs+jBKPuoSyYLt1wwe10/lpgL4rkKWU3/m1Myt0tveJ9WcqHh6tzcAbb/fXpUFT/o4SWDimWkPkuCb+8j//2yiXk0a/T2f36zKMuZvujqQKBgC6B7BAQDG2H2B/ijofp12ejJU36nL98gAZyqOfpLJ+FeMz4TlBDQ+phIMhnHXA5UkdDapQ+zA3SrFk+6yGk9Vw4Hf46B+82SvOrSbmnMa+PYqKYIvUzR4gg34rL/7AhwnbEyD5hXq4dHwMNsIDq+l2elPjwm/U9V0gdAl2+r50HAoGALtsKqMvhv8HucAMBPrLikhXP/8um8mMKFMrzfqZ+otxfHzlhI0L08Bo3jQrb0Z7ByNY6M8epOmbCKADsbWcVre/AAY0ZkuSZK/CaOXNX/AhMKmKJh8qAOPRY02LIJRBCpfS4czEdnfUhYV/TYiFNnKRj57PPYZdTzUsxa/yVTmECgYBr7slQEjb5Onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw==' | ||
|
||
const argv = parseArgs({ | ||
options: { | ||
'run-server': { | ||
type: 'boolean', | ||
default: false | ||
}, | ||
'server-address': { | ||
type: 'string' | ||
}, | ||
transport: { | ||
type: 'string', | ||
default: 'tcp' | ||
}, | ||
'upload-bytes': { | ||
type: 'string', | ||
default: '0' | ||
}, | ||
'download-bytes': { | ||
type: 'string', | ||
default: '0' | ||
} | ||
} | ||
}) | ||
|
||
/** | ||
* @param {boolean} runServer | ||
* @param {string} serverIpAddress | ||
* @param {string} transport | ||
* @param {number} uploadBytes | ||
* @param {number} downloadBytes | ||
*/ | ||
export async function main (runServer, serverIpAddress, transport, uploadBytes, downloadBytes) { | ||
const listenAddrs = [] | ||
|
||
const { host, port } = splitHostPort(serverIpAddress) | ||
// #TODO: right now we only support tcp | ||
const tcpMultiaddr = multiaddr(`/ip4/${host}/tcp/${port}`) | ||
|
||
const config = { | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncryption: [ | ||
noise() | ||
], | ||
services: { | ||
perf: perfService() | ||
} | ||
} | ||
|
||
const encoded = uint8ArrayFromString(testPrivKey, 'base64pad') | ||
const privateKey = await unmarshalPrivateKey(encoded) | ||
const peerId = await createFromPrivKey(privateKey) | ||
const tcpMultiaddrAddress = `${tcpMultiaddr.toString()}/p2p/${peerId.toString()}` | ||
|
||
if (runServer) { | ||
listenAddrs.push(tcpMultiaddrAddress) | ||
|
||
Object.assign(config, { | ||
peerId, | ||
addresses: { | ||
listen: listenAddrs | ||
} | ||
}) | ||
} | ||
|
||
const node = await createLibp2p(config) | ||
|
||
await node.start() | ||
|
||
if (!runServer) { | ||
for await (const output of node.services.perf.measurePerformance(multiaddr(tcpMultiaddrAddress), uploadBytes, downloadBytes)) { | ||
// eslint-disable-next-line no-console | ||
console.log(JSON.stringify(output)) | ||
} | ||
|
||
await node.stop() | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} address | ||
* @returns { host: string, port?: string } | ||
*/ | ||
function splitHostPort (address) { | ||
try { | ||
const parts = address.split(':') | ||
const host = parts[0] | ||
const port = parts[1] | ||
return { | ||
host, | ||
port | ||
} | ||
} catch (error) { | ||
throw Error('Invalid server address') | ||
} | ||
} | ||
|
||
main(argv['run-server'], argv['server-address'], argv.transport, Number(argv['upload-bytes']), Number(argv['download-bytes'])).catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
process.exit(1) | ||
}) |
Oops, something went wrong.