diff --git a/.github/prettifier.yml b/.github/prettifier.yml new file mode 100644 index 000000000..b4ba869cc --- /dev/null +++ b/.github/prettifier.yml @@ -0,0 +1,17 @@ +# Template for commit messages from Prettifier. +# Placeholders are: +# - {{commitSha}}: the SHA of your commit that Prettifier formats +commitMessage: "AutoFormat {{commitSha}}" + +fixByCommit: true + +fixByPullRequest: false + +prettified-message: > + I have adjusted the formatting of this pull request for you. + + To stop seeing this message, please install Prettier on your machine and run + `meteor npm run format` to format your changes before submitting them. + +# Whether to only check branches that have an active pull request. +pulls-only: true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..53c68e726 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: Unit tests + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Use Node.js 16.x + uses: actions/setup-node@v2 + with: + node-version: 16.x + cache: "npm" + - run: npm i + - name: Run tsc-check + run: npm run tsc-check + - name: Run tests + run: npm test diff --git a/.gitignore b/.gitignore index baa2d2f1d..1366e77d4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ npm-debug.log test/output .tscache test/public -.vscode/ \ No newline at end of file +.vscode/ +dist diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..1c2097332 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,6 @@ +dist +docs +package-json.lock + +# semantic-release +CHANGELOG.md \ No newline at end of file diff --git a/.prettierrc.toml b/.prettierrc.toml new file mode 100644 index 000000000..437054d3b --- /dev/null +++ b/.prettierrc.toml @@ -0,0 +1,3 @@ +trailingComma = "all" +semi = true +useTabs = true \ No newline at end of file diff --git a/changelog.md b/CHANGELOG.md similarity index 77% rename from changelog.md rename to CHANGELOG.md index 2e692570c..d88d35853 100644 --- a/changelog.md +++ b/CHANGELOG.md @@ -2,10 +2,100 @@ All notable changes will be documented in this file. -## vNEXT +## vNext ... + + +## v2.0.0 (upcoming) + +The main goals of the second version: + +- modernize the code base: use `es6` and latest tools; +- target to the latest browsers API(you can use webrtc adapter to shim old browsers); +- shrink the package size; +- NodeJS support. + +### Breaking changes + +#### Drop ES5 supports + +If you need to support es5, you can recompile the lib from the sources or use the latest version from the v1 branch like `npm i peerjs@1`. + +#### Remove `webrtc-adapter` package from `dependencies` + +The modern browsers mostly works good without any additional libs like webrtc-adapter. + +You can increase cross browsers support by manually add `webrtc-adapter` package to your project: + +`npm install webrtc-adapter` + +Then import it as soon as possible. For example, you have an entry point for your application in the file `index.tsx`. So, lets add the import: + +``` +import 'webrtc-adapter'; // as soon as possible + +// +//... your startup code +// + +``` + +See more: https://github.com/webrtcHacks/adapter + +#### Remove browser detection features from the library. + +The `browser` field was removed from the `util` class +and the `OFFER` packet. + +#### The `main` field of `package.json` now points to the non minified version. + +Today, most projects use bundlers with a minification step. Sometimes, double minification can lead to issues. + +If you want to use the minified version, use it directly from the `dist` folder like: + +``` +import Peer from 'peerjs/dist/peerjs.min.js'; + +... +``` + +#### Remove `dist` folder from git + +Please, try to host the package by yourself. It is more securely and you don't depend on third party. + +In any case, you can use a CDN like `unpkg.com`: + +```html + +``` + +#### Replace `util.supports` to `Peer.getFeatures()` + +You can check the possible features of WebRTC by static method `Peer.getFeatures()` +before instantiating a `Peer` object. + +#### Use `ESBuild` as bundler. + +Parcel 1 was a good choice years ago, but now esbuild seems like simple and good solution! + +### New + +#### NodeJS support + +Yeah! You can use `peerjs` in NodeJS! + +See details in https://github.com/peers/peerjs/pull/928 + +### Another changes + +- update `devDependencies` +- apply `Prettier` for the code style +- rename `util` to `Utils` +- remove `peerjs` export. +- fix #864 + ## 1.3.2 (2021-03-11) @@ -61,7 +151,7 @@ All notable changes will be documented in this file. ## 1.0.3 (2019-08-21) - add pingInterval option - + ## 1.0.2 (2019-07-20) @@ -97,6 +187,7 @@ Almost all project was refactored!!! - changed: fetch api instead of xhr ### Features + - added: heartbeat #502 ### Bug Fixes diff --git a/README.md b/README.md index ecf2576ab..2ddeb5cba 100644 --- a/README.md +++ b/README.md @@ -1,83 +1,109 @@ -# PeerJS: Simple peer-to-peer with WebRTC # +# PeerJS: Simple peer-to-peer with WebRTC ### https://t.me/joinchat/VWI0UBxnG7f7_DV7 [![Backers on Open Collective](https://opencollective.com/peer/backers/badge.svg)](#backers) - [![Sponsors on Open Collective](https://opencollective.com/peer/sponsors/badge.svg)](#sponsors) +[![Sponsors on Open Collective](https://opencollective.com/peer/sponsors/badge.svg)](#sponsors) PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC, supporting both data channels and media streams. +## Features + +- TypeScript 100% +- respects CommonJS, ES Module +- NodeJS support +- Simple API +- DataChannel + MediaStream + ## Live Example Here's an example application that uses both media and data connections: https://glitch.com/~peerjs-video. The example also uses its own [PeerServer](https://github.com/peers/peerjs-server). ## Setup +**Install a dependency** + +with npm: + +`npm install peerjs` + +with yarn: + +`yarn add peerjs` **Include the library** - with npm: - `npm install peerjs` - - with yarn: - `yarn add peerjs` - ```js - // The usage - - import Peer from 'peerjs'; - ``` +```js +import { Peer } from "peerjs"; +``` +**Create a Peer** -**Create a Peer** ```javascript -const peer = new Peer('pick-an-id'); +const peer = new Peer("pick-an-id"); // You can pick your own id or omit the id if you want to get a random one from the server. ``` ## Data connections + **Connect** + ```javascript -const conn = peer.connect('another-peers-id'); -conn.on('open', () => { - conn.send('hi!'); +const conn = peer.connect("another-peers-id"); +conn.on("open", () => { + conn.send("hi!"); }); ``` + **Receive** + ```javascript -peer.on('connection', (conn) => { - conn.on('data', (data) => { - // Will print 'hi!' - console.log(data); - }); - conn.on('open', () => { - conn.send('hello!'); - }); +peer.on("connection", (conn) => { + conn.on("data", (data) => { + // Will print 'hi!' + console.log(data); + }); + conn.on("open", () => { + conn.send("hello!"); + }); }); ``` ## Media calls + **Call** -```javascript -navigator.mediaDevices.getUserMedia({video: true, audio: true}, (stream) => { - const call = peer.call('another-peers-id', stream); - call.on('stream', (remoteStream) => { - // Show stream in some