Skip to content

Commit c458806

Browse files
committed
feat: update to new libp2p interfaces
Updates this module to use the `@libp2p/*` set of modules as part of the typescript port. - Renames imports to add .js suffix - Renames bundled `@types/*.d.ts` to `@types/*.ts` because tsc ignores `.d.ts` which masks errors - Updates project config to output ESM instead of CJS code - Updates all deps to lastest versions including ones that have gone ESM-only - Re-generates protobuf code with the ES6 target N.b. `pbjs` generates invalid imports for bundler-less ESM - the `.js` suffix has to be added to the import manually so it's best not rebuild the protobuf generated code unless you know something has changed. BREAKING CHANGE: This module now outputs ESM code only
1 parent 790b039 commit c458806

38 files changed

+3346
-1516
lines changed

.aegir.js renamed to .aegir.cjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
const path = require('path')
21

32
/** @type {import('aegir').PartialOptions} */
43
const config = {
5-
tsRepo: true,
64
docs: {
75
entryPoint: "src/index.ts"
86
},

benchmarks/benchmark.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
const { Noise } = require('../dist/src/index')
44
const benchmark = require('benchmark')
5-
const DuplexPair = require('it-pair/duplex')
6-
const PeerId = require('peer-id')
5+
const { duplexPair } = require('it-pair/duplex')
6+
const { createFromJSON } = require('@libp2p/peer-id-factory')
77

88
const bench = async function () {
99
console.log('Initializing handshake benchmark')
1010
const initiator = new Noise()
11-
const initiatorPeer = await PeerId.createFromJSON({
11+
const initiatorPeer = await createFromJSON({
1212
id: '12D3KooWH45PiqBjfnEfDfCD6TqJrpqTBJvQDwGHvjGpaWwms46D',
1313
privKey: 'CAESYBtKXrMwawAARmLScynQUuSwi/gGSkwqDPxi15N3dqDHa4T4iWupkMe5oYGwGH3Hyfvd/QcgSTqg71oYZJadJ6prhPiJa6mQx7mhgbAYfcfJ+939ByBJOqDvWhhklp0nqg==',
1414
pubKey: 'CAESIGuE+IlrqZDHuaGBsBh9x8n73f0HIEk6oO9aGGSWnSeq'
1515
})
1616
const responder = new Noise()
17-
const responderPeer = await PeerId.createFromJSON({
17+
const responderPeer = await createFromJSON({
1818
id: '12D3KooWP63uzL78BRMpkQ7augMdNi1h3VBrVWZucKjyhzGVaSi1',
1919
privKey: 'CAESYPxO3SHyfc2578hDmfkGGBY255JjiLuVavJWy+9ivlpsxSyVKf36ipyRGL6szGzHuFs5ceEuuGVrPMg/rW2Ch1bFLJUp/fqKnJEYvqzMbMe4Wzlx4S64ZWs8yD+tbYKHVg==',
2020
pubKey: 'CAESIMUslSn9+oqckRi+rMxsx7hbOXHhLrhlazzIP61tgodW'
@@ -23,7 +23,7 @@ const bench = async function () {
2323
const bench = new benchmark('handshake', {
2424
defer: true,
2525
fn: async function (deffered) {
26-
const [inboundConnection, outboundConnection] = DuplexPair()
26+
const [inboundConnection, outboundConnection] = duplexPair()
2727
await Promise.all([
2828
initiator.secureOutbound(initiatorPeer, outboundConnection, responderPeer),
2929
responder.secureInbound(responderPeer, inboundConnection, initiatorPeer)

package.json

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"name": "@chainsafe/libp2p-noise",
33
"version": "5.0.3",
4-
"main": "dist/src/index.js",
4+
"type": "module",
55
"types": "dist/src/index.d.ts",
6+
"exports": {
7+
".": {
8+
"import": "./dist/src/index.js"
9+
}
10+
},
611
"files": [
712
"dist",
813
"src"
@@ -22,49 +27,48 @@
2227
"bench": "node benchmarks/benchmark.js",
2328
"clean": "rm -rf dist",
2429
"check": "aegir dep-check && aegir ts -p check",
25-
"build": "aegir build",
30+
"build": "tsc",
31+
"postbuild": "cp -R src/proto dist/src",
2632
"lint": "aegir lint",
2733
"lint:fix": "aegir lint --fix",
2834
"pretest": "yarn run check",
29-
"test": "aegir test",
30-
"test:node": "aegir test -t node",
31-
"test:browser": "aegir test -t browser",
35+
"test": "aegir test -f \"./dist/test/**/*.spec.js\"",
36+
"test:node": "npm run test -- -t node",
37+
"test:browser": "npm run test -- -t browser",
3238
"docs": "aegir docs",
33-
"proto:gen": "pbjs -t static-module -r libp2p-noise -o ./src/proto/payload.js ./src/proto/payload.proto && pbts -o ./src/proto/payload.d.ts ./src/proto/payload.js && yarn run lint --fix"
39+
"proto:gen": "pbjs -t static-module -w es6 -r libp2p-noise -o ./src/proto/payload.js ./src/proto/payload.proto && pbts -o ./src/proto/payload.d.ts ./src/proto/payload.js"
3440
},
3541
"browser": {
3642
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",
3743
"util": false
3844
},
39-
"devDependencies": {
40-
"@types/bl": "^5.0.2",
41-
"aegir": "^36.1.1",
42-
"benchmark": "^2.1.4",
43-
"buffer": "^6.0.3",
44-
"chai": "^4.3.4",
45-
"events": "^3.3.0",
46-
"libp2p-crypto": "^0.21.0",
47-
"microtime": "^3.0.0",
48-
"mocha": "^9.0.2",
49-
"sinon": "^12.0.1",
50-
"util": "^0.12.4"
51-
},
5245
"dependencies": {
46+
"@libp2p/interfaces": "^1.3.14",
47+
"@libp2p/logger": "^1.1.2",
48+
"@libp2p/peer-collections": "^1.0.0",
49+
"@libp2p/peer-id": "^1.1.8",
5350
"@stablelib/chacha20poly1305": "^1.0.1",
5451
"@stablelib/hkdf": "^1.0.1",
5552
"@stablelib/sha256": "^1.0.1",
5653
"@stablelib/x25519": "^1.0.1",
57-
"bl": "^5.0.0",
58-
"debug": "^4.3.1",
59-
"it-buffer": "^0.1.3",
60-
"it-length-prefixed": "^5.0.3",
61-
"it-pair": "^1.0.0",
62-
"it-pb-rpc": "^0.2.0",
63-
"it-pipe": "^1.1.0",
64-
"peer-id": "^0.16.0",
54+
"it-length-prefixed": "^7.0.1",
55+
"it-pair": "^2.0.2",
56+
"it-pb-stream": "^1.0.2",
57+
"it-pipe": "^2.0.3",
6558
"protobufjs": "^6.11.2",
59+
"uint8arraylist": "^1.4.0",
6660
"uint8arrays": "^3.0.0"
6761
},
62+
"devDependencies": {
63+
"@libp2p/crypto": "^0.22.9",
64+
"@libp2p/peer-id-factory": "^1.0.8",
65+
"aegir": "^36.1.3",
66+
"benchmark": "^2.1.4",
67+
"events": "^3.3.0",
68+
"microtime": "^3.0.0",
69+
"sinon": "^13.0.1",
70+
"util": "^0.12.4"
71+
},
6872
"resolutions": {
6973
"bn.js": "4.4.0"
7074
},
File renamed without changes.

src/@types/handshake-interface.d.ts renamed to src/@types/handshake-interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { bytes } from './basic'
2-
import { NoiseSession } from './handshake'
3-
import PeerId from 'peer-id'
1+
import type { bytes } from './basic.js'
2+
import type { NoiseSession } from './handshake.js'
3+
import type { PeerId } from '@libp2p/interfaces/peer-id'
44

55
export interface IHandshake {
66
session: NoiseSession

src/@types/handshake.d.ts renamed to src/@types/handshake.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { bytes, bytes32, uint64 } from './basic'
2-
import { KeyPair } from './libp2p'
1+
import type { bytes, bytes32, uint64 } from './basic.js'
2+
import type { KeyPair } from './libp2p.js'
33

44
export type Hkdf = [bytes, bytes, bytes]
55

src/@types/it-length-prefixed/index.d.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/@types/it-pair/index.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/@types/libp2p.d.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/@types/libp2p.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { bytes, bytes32 } from './basic.js'
2+
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
3+
4+
export interface KeyPair {
5+
publicKey: bytes32
6+
privateKey: bytes32
7+
}
8+
9+
export interface INoiseConnection extends ConnectionEncrypter {
10+
remoteEarlyData?: () => bytes
11+
}

0 commit comments

Comments
 (0)