Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Jan 27, 2025
2 parents 1a2928f + 31a15a1 commit 39e84ab
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/auto-tls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ It also requires the Identify protocol.
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { autoTLS } from '@libp2p/auto-tls'
import { autoNAT } from '@libp2p/autonat'
import { identify } from '@libp2p/identify'
import { keychain } from '@libp2p/keychain'
import { webSockets } from '@libp2p/websockets'
Expand All @@ -65,6 +66,7 @@ const node = await createLibp2p({
yamux()
],
services: {
autoNAT: autoNAT(),
autoTLS: autoTLS(),
identify: identify(),
keychain: keychain(),
Expand All @@ -76,7 +78,7 @@ const node = await createLibp2p({

console.info(node.getMultiaddrs())
// includes public WSS address:
// [ '/ip4/123.123.123.123/tcp/12345/wss ]
// [ '/ip4/123.123.123.123/tcp/12345/tls/ws' ]
```

# Install
Expand Down
4 changes: 3 additions & 1 deletion packages/auto-tls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* import { noise } from '@chainsafe/libp2p-noise'
* import { yamux } from '@chainsafe/libp2p-yamux'
* import { autoTLS } from '@libp2p/auto-tls'
* import { autoNAT } from '@libp2p/autonat'
* import { identify } from '@libp2p/identify'
* import { keychain } from '@libp2p/keychain'
* import { webSockets } from '@libp2p/websockets'
Expand All @@ -42,6 +43,7 @@
* yamux()
* ],
* services: {
* autoNAT: autoNAT(),
* autoTLS: autoTLS(),
* identify: identify(),
* keychain: keychain(),
Expand All @@ -53,7 +55,7 @@
*
* console.info(node.getMultiaddrs())
* // includes public WSS address:
* // [ '/ip4/123.123.123.123/tcp/12345/wss ]
* // [ '/ip4/123.123.123.123/tcp/12345/tls/ws ]
* ```
*/

Expand Down
4 changes: 2 additions & 2 deletions packages/interface/src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export interface Ed25519PrivateKey {
readonly publicKey: Ed25519PublicKey

/**
* The raw public key bytes
* The raw private key bytes
*/
readonly raw: Uint8Array

Expand Down Expand Up @@ -221,7 +221,7 @@ export interface Secp256k1PrivateKey {
readonly publicKey: Secp256k1PublicKey

/**
* The raw public key bytes
* The raw private key bytes
*/
readonly raw: Uint8Array

Expand Down
8 changes: 8 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
"types": "./dist/src/ip-port-to-multiaddr.d.ts",
"import": "./dist/src/ip-port-to-multiaddr.js"
},
"./is-async-generator": {
"types": "./dist/src/is-async-generator.d.ts",
"import": "./dist/src/is-async-generator.js"
},
"./is-generator": {
"types": "./dist/src/is-generator.d.ts",
"import": "./dist/src/is-generator.js"
},
"./is-promise": {
"types": "./dist/src/is-promise.d.ts",
"import": "./dist/src/is-promise.js"
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/src/is-async-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function isAsyncGenerator (obj: unknown): obj is AsyncGenerator {
if (obj == null) return false
const asyncIterator = (obj as { [Symbol.asyncIterator]?: unknown })?.[
Symbol.asyncIterator
]
if (typeof asyncIterator !== 'function') return false

const instance = obj as { next?: unknown }
return typeof instance.next === 'function'
}
8 changes: 8 additions & 0 deletions packages/utils/src/is-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function isGenerator (obj: unknown): obj is Generator {
if (obj == null) return false
const iterator = (obj as { [Symbol.iterator]?: unknown })?.[Symbol.iterator]
if (typeof iterator !== 'function') return false

const instance = obj as { next?: unknown }
return typeof instance.next === 'function'
}
41 changes: 41 additions & 0 deletions packages/utils/test/is-async-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'aegir/chai'
import { isAsyncGenerator } from '../src/is-async-generator.js'

describe('is-async-generator', () => {
it('should return true if the value is an async generator', () => {
async function * asyncGen (): AsyncGenerator<number> {
yield 1
}
const asyncGenerator = asyncGen()
expect(isAsyncGenerator(asyncGenerator)).to.be.true()

const asyncGenObj = (async function * () {
yield 1
})()
expect(isAsyncGenerator(asyncGenObj)).to.be.true()
})

it('should return false if the value is not an async generator', () => {
expect(isAsyncGenerator(1)).to.be.false()
expect(isAsyncGenerator('string')).to.be.false()
expect(isAsyncGenerator({})).to.be.false()
expect(isAsyncGenerator([])).to.be.false()
expect(isAsyncGenerator(null)).to.be.false()
expect(isAsyncGenerator(undefined)).to.be.false()
expect(isAsyncGenerator(() => {})).to.be.false()
expect(isAsyncGenerator(async () => {})).to.be.false()
expect(
isAsyncGenerator(function * () {
yield 1
})
).to.be.false()
expect(
isAsyncGenerator(async function * () {
yield 1
})
).to.be.false() // async generator function, not generator
expect(isAsyncGenerator(Promise.resolve())).to.be.false()
expect(isAsyncGenerator({ next: async () => {} })).to.be.false()
expect(isAsyncGenerator({ [Symbol.asyncIterator]: () => {} })).to.be.false()
})
})
41 changes: 41 additions & 0 deletions packages/utils/test/is-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'aegir/chai'
import { isGenerator } from '../src/is-generator.js'

describe('is-generator', () => {
it('should return true if the value is a generator', () => {
function * gen (): Generator<number> {
yield 1
}
const generator = gen()
expect(isGenerator(generator)).to.be.true()

const genObj = (function * () {
yield 1
})()
expect(isGenerator(genObj)).to.be.true()
})

it('should return false if the value is not a generator', () => {
expect(isGenerator(1)).to.be.false()
expect(isGenerator('string')).to.be.false()
expect(isGenerator({})).to.be.false()
expect(isGenerator([])).to.be.false()
expect(isGenerator(null)).to.be.false()
expect(isGenerator(undefined)).to.be.false()
expect(isGenerator(() => {})).to.be.false()
expect(isGenerator(async () => {})).to.be.false()
expect(
isGenerator(function * () {
yield 1
})
).to.be.false() // generator function, not generator
expect(
isGenerator(async function * () {
yield 1
})
).to.be.false()
expect(isGenerator(Promise.resolve())).to.be.false()
expect(isGenerator({ next: () => {} })).to.be.false()
expect(isGenerator({ [Symbol.iterator]: () => {} })).to.be.false()
})
})
28 changes: 28 additions & 0 deletions packages/utils/test/is-promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,32 @@ describe('is-promise', () => {
it('should not detect partial promise', () => {
expect(isPromise({ then: true })).to.be.false()
})

it('should return true if the value is a promise', () => {
expect(isPromise(Promise.resolve())).to.be.true()
expect(isPromise(new Promise(() => {}))).to.be.true()
expect(isPromise(Promise.reject(new Error('test')))).to.be.true()
})

it('should return false if the value is not a promise', () => {
expect(isPromise(1)).to.be.false()
expect(isPromise('string')).to.be.false()
expect(isPromise({})).to.be.false()
expect(isPromise([])).to.be.false()
expect(isPromise(null)).to.be.false()
expect(isPromise(undefined)).to.be.false()
expect(isPromise(() => {})).to.be.false()
expect(isPromise(async () => {})).to.be.false()
expect(
isPromise(function * () {
yield 1
})
).to.be.false()
expect(
isPromise(async function * () {
yield 1
})
).to.be.false()
expect(isPromise({ then: 1 })).to.be.false()
})
})
2 changes: 2 additions & 0 deletions packages/utils/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"./src/filters/index.ts",
"./src/global-unicast-ip.ts",
"./src/ip-port-to-multiaddr.ts",
"./src/is-async-generator.ts",
"./src/is-generator.ts",
"./src/is-promise.ts",
"./src/link-local-ip.ts",
"./src/moving-average.ts",
Expand Down

0 comments on commit 39e84ab

Please sign in to comment.