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

Commit

Permalink
feat: convert to typescript (#139)
Browse files Browse the repository at this point in the history
- Converts to typescript
- Only named exports
- No more CJS, only ESM
- Runs tests on all supported environments
- Adds auto-publish
- Adds dependabot

BREAKING CHANGE: switch to named exports, ESM only
  • Loading branch information
achingbrain committed Feb 28, 2022
1 parent a20824a commit 5064483
Show file tree
Hide file tree
Showing 22 changed files with 390 additions and 603 deletions.
8 changes: 8 additions & 0 deletions .aegir.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

/** @type {import('aegir').PartialOptions} */
module.exports = {
build: {
bundlesizeMax: '143KB',
}
}
75 changes: 0 additions & 75 deletions .aegir.js

This file was deleted.

35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
js-libp2p-floodsub
==================
# js-libp2p-floodsub <!-- omit in toc -->

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-floodsub/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-floodsub?branch=master)
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-floodsub.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-floodsub)
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-floodsub.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-floodsub)
[![Build Status](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml/badge.svg?branch=main)](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-floodsub.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-floodsub) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![](https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square)](https://waffle.io/libp2p/js-libp2p-floodsub)

> libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
## Lead Maintainer
## Table of Contents <!-- omit in toc -->

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

## Table of Contents

- [js-libp2p-floodsub](#js-libp2p-floodsub)
- [Lead Maintainer](#lead-maintainer)
- [Table of Contents](#table-of-contents)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Create a floodsub implementation](#create-a-floodsub-implementation)
- [Events](#events)
- [Contribute](#contribute)
- [License](#license)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Create a floodsub implementation](#create-a-floodsub-implementation)
- [Events](#events)
- [Contribute](#contribute)
- [License](#license)

## Install

```sh
> npm install libp2p-floodsub
> npm install @libp2p/floodsub
```

## Usage

```JavaScript
const FloodSub = require('libp2p-floodsub')
import { FloodSub } from '@libp2p/floodsub'

// registrar is provided by libp2p
const fsub = new FloodSub(peerId, registrar, options)
Expand Down Expand Up @@ -94,4 +85,4 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c

## License

Copyright (c) Protocol Labs, Inc. under the **MIT License**. See [LICENSE file](./LICENSE) for details.
[Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) © Protocol Labs
1 change: 0 additions & 1 deletion benchmarks/index.js → benchmarks/index.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict'

const Benchmark = require('benchmark')
const crypto = require('crypto')
Expand Down
File renamed without changes.
71 changes: 29 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "libp2p-floodsub",
"name": "@libp2p/floodsub",
"version": "0.29.1",
"description": "libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).",
"license": "Apache-2.0 OR MIT",
Expand All @@ -23,28 +23,24 @@
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*"
],
"src/*": [
"*",
"dist/*",
"dist/src/*"
]
}
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist"
"dist/src",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs"
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"release": {
"branches": [
Expand Down Expand Up @@ -129,9 +125,10 @@
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
"test": "aegir test",
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f dist/test",
"test:chrome": "npm run test -- -t browser --cov",
"test:chrome-webworker": "npm run test -- -t webworker",
"test:firefox": "npm run test -- -t browser -- --browser firefox",
Expand All @@ -141,30 +138,20 @@
"release": "semantic-release"
},
"dependencies": {
"debug": "^4.2.0",
"libp2p-interfaces": "^4.0.5",
"time-cache": "^0.3.0",
"@libp2p/interfaces": "^1.3.6",
"@libp2p/logger": "^1.0.3",
"@libp2p/pubsub": "^1.2.4",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@chainsafe/libp2p-noise": "^5.0.0",
"@types/debug": "^4.1.5",
"@libp2p/interface-compliance-tests": "^1.0.8",
"@libp2p/peer-id": "^1.1.3",
"@libp2p/peer-id-factory": "^1.0.5",
"@multiformats/multiaddr": "^10.1.5",
"aegir": "^36.1.1",
"benchmark": "^2.1.4",
"buffer": "^6.0.3",
"chai": "^4.3.4",
"ipfs-utils": "^9.0.2",
"it-pair": "^1.0.0",
"libp2p": "libp2p/js-libp2p#feat/async-peerstore",
"libp2p-interfaces-compliance-tests": "^4.0.7",
"libp2p-mplex": "^0.10.3",
"libp2p-websockets": "^0.16.0",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.5",
"os": "^0.1.1",
"p-wait-for": "^3.1.0",
"peer-id": "^0.16.0",
"sinon": "^12.0.1",
"util": "^0.12.3"
"p-wait-for": "^4.1.0",
"sinon": "^13.0.1",
"wherearewe": "^1.0.0"
}
}
1 change: 0 additions & 1 deletion scripts/node-globals.js

This file was deleted.

63 changes: 63 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
interface SimpleTimeCacheOpts {
validityMs: number
}

interface CacheValue<T> {
value: T
validUntilMs: number
}

/**
* This is similar to https://github.com/daviddias/time-cache/blob/master/src/index.js
* for our own need, we don't use lodash throttle to improve performance.
* This gives 4x - 5x performance gain compared to npm TimeCache
*/
export class SimpleTimeCache<T> {
private entries: Map<string, CacheValue<T>>
private readonly validityMs: number
private lastPruneTime = 0

constructor (options: SimpleTimeCacheOpts) {
this.entries = new Map()
this.validityMs = options.validityMs

// allow negative validityMs so that this does not cache anything, spec test compliance.spec.js
// sends duplicate messages and expect peer to receive all. Application likely uses positive validityMs
}

put (key: string, value: T): void {
this.entries.set(key, { value, validUntilMs: Date.now() + this.validityMs })
this.prune()
}

prune (): void {
const now = Date.now()
if (now - this.lastPruneTime < 200) {
return
}
this.lastPruneTime = now

for (const [k, v] of this.entries.entries()) {
if (v.validUntilMs < now) {
this.entries.delete(k)
} else {
// sort by insertion order
break
}
}
}

has (key: string): boolean {
return this.entries.has(key)
}

get (key: string): T | undefined {
const value = this.entries.get(key)
return (value != null && value.validUntilMs >= Date.now()) ? value.value : undefined
}

clear (): void {
this.entries = new Map()
this.lastPruneTime = 0
}
}
11 changes: 0 additions & 11 deletions src/config.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { logger } from '@libp2p/logger'

export const log = logger('libp2p:floodsub')

export const multicodec = '/floodsub/1.0.0'
Loading

0 comments on commit 5064483

Please sign in to comment.