Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

docs: publish api docs #69

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 4 additions & 31 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
package-lock.json
yarn.lock
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
.nyc_output

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

dist
docs
.docs
.coverage
package-lock.json
yarn.lock
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-utils.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-utils)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-utils/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-utils/actions/workflows/js-test-and-release.yml)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-utils/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p-utils/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)

> Package to aggregate shared logic and dependencies for the libp2p ecosystem

## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Lead Maintainer](#lead-maintainer)
- [Browser `<script>` tag](#browser-script-tag)
- [Usage](#usage)
- [Contribute](#contribute)
- [API Docs](#api-docs)
- [License](#license)
- [Contribute](#contribute-1)
- [Contribution](#contribution)

## Install

```console
$ npm i @libp2p/utils
```

### Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `Libp2pUtils` in the global namespace.

```html
<script src="https://unpkg.com/@libp2p/utils/dist/index.min.js"></script>
```

The libp2p ecosystem has lots of repos with it comes several problems like:

- Domain logic dedupe - all modules shared a lot of logic like validation, streams handling, etc.
- Dependencies management - it's really easy with so many repos for dependencies to go out of control, they become outdated, different repos use different modules to do the same thing (like merging defaults options), browser bundles ends up with multiple versions of the same package, bumping versions is cumbersome to do because we need to go through several repos, etc.

These problems are the motivation for this package, having shared logic in this package avoids creating cyclic dependencies, centralizes common use modules/functions (exactly like aegir does for the tooling), semantic versioning for 3rd party dependencies is handled in one single place (a good example is going from streams 2 to 3) and maintainers should only care about having `libp2p-utils` updated.

## Lead Maintainer

[Vasco Santos](https://github.com/vasco-santos)

## Usage

Each function should be imported directly.
Expand All @@ -43,13 +47,11 @@ import ipAndPortToMultiaddr from '@libp2p/utils/ip-port-to-multiaddr'
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
```

You can check the [API docs](./API.md).

## Contribute
You can check the [API docs](https://libp2p.github.io/js-libp2p-utils).

Contributions welcome. Please check out [the issues](https://github.com/libp2p/js-libp2p-utils/issues).
## API Docs

Check out our [contributing document](https://github.com/ipfs/community/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
- <https://libp2p.github.io/js-libp2p-utils>

## License

Expand All @@ -58,6 +60,6 @@ Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribute
## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"files": [
"src",
"dist/src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
Expand Down Expand Up @@ -165,6 +165,7 @@
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
"docs": "aegir docs",
"test": "aegir test",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
Expand Down
29 changes: 29 additions & 0 deletions src/address-sort.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/**
* @packageDocumentation
*
* Provides strategies to sort a list of multiaddrs.
*
* @example
*
* ```typescript
* import { publicAddressesFirst } from '@libp2p/utils/address-sort'
* import { multiaddr } from '@multformats/multiaddr'
*
*
* const addresses = [
* multiaddr('/ip4/127.0.0.1/tcp/9000'),
* multiaddr('/ip4/82.41.53.1/tcp/9000')
* ].sort(publicAddressesFirst)
*
* console.info(addresses)
* // ['/ip4/82.41.53.1/tcp/9000', '/ip4/127.0.0.1/tcp/9000']
* ```
*/

import type { Address } from '@libp2p/interface-peer-store'
import { isPrivate } from './multiaddr/is-private.js'

Expand All @@ -24,3 +46,10 @@ export function publicAddressesFirst (a: Address, b: Address): -1 | 0 | 1 {

return 0
}

/**
* A test thing
*/
export async function something (): Promise<Uint8Array> {
return Uint8Array.from([0, 1, 2])
}
19 changes: 19 additions & 0 deletions src/array-equals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/**
* @packageDocumentation
*
* Provides strategies ensure arrays are equivalent.
*
* @example
*
* ```typescript
* import { arrayEquals } from '@libp2p/utils/array-equals'
* import { multiaddr } from '@multformats/multiaddr'
*
* const ma1 = multiaddr('/ip4/127.0.0.1/tcp/9000'),
* const ma2 = multiaddr('/ip4/82.41.53.1/tcp/9000')
*
* console.info(arrayEquals([ma1], [ma1])) // true
* console.info(arrayEquals([ma1], [ma2])) // false
* ```
*/

/**
* Verify if two arrays of non primitive types with the "equals" function are equal.
* Compatible with multiaddr, peer-id and others.
Expand Down
4 changes: 2 additions & 2 deletions src/stream-to-ma-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export interface Timeline {
close?: number
}

interface StreamOptions {
export interface StreamOptions {
signal?: AbortSignal

}

interface StreamProperties {
export interface StreamProperties {
stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>
remoteAddr: Multiaddr
localAddr: Multiaddr
Expand Down