Skip to content

Commit 0d0cd92

Browse files
committed
feat!: Migration to Vite
The library is now built with Vite. Importing in the browser is now done in ESM.
1 parent 8676482 commit 0d0cd92

File tree

4 files changed

+663
-21
lines changed

4 files changed

+663
-21
lines changed

README.md

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
# @thaunknown/simple-peer [![coveralls][coveralls-image]][coveralls-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
2-
3-
[coveralls-image]: https://coveralls.io/repos/github/thaunknown/simple-peer/badge.svg?branch=master
4-
[coveralls-url]: https://coveralls.io/github/thaunknown/simple-peer?branch=master
5-
[npm-image]: https://img.shields.io/npm/v/@thaunknown/simple-peer.svg
6-
[npm-url]: https://npmjs.org/package/@thaunknown/simple-peer
7-
[downloads-image]: https://img.shields.io/npm/dm/@thaunknown/simple-peer.svg
8-
[downloads-url]: https://npmjs.org/package/@thaunknown/simple-peer
1+
# @workadventure/simple-peer [![coveralls][coveralls-image]][coveralls-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
2+
3+
[coveralls-image]: https://coveralls.io/repos/github/workadventure/simple-peer/badge.svg?branch=master
4+
[coveralls-url]: https://coveralls.io/github/workadventure/simple-peer?branch=master
5+
[npm-image]: https://img.shields.io/npm/v/@workadventure/simple-peer.svg
6+
[npm-url]: https://npmjs.org/package/@workadventure/simple-peer
7+
[downloads-image]: https://img.shields.io/npm/dm/@workadventure/simple-peer.svg
8+
[downloads-url]: https://npmjs.org/package/@workadventure/simple-peer
99
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
1010
[standard-url]: https://standardjs.com
1111

1212
#### Simple WebRTC video, voice, and data channels
1313

14-
This package is a more up-to-date version of feross's simple-peer, rewritten 100% in Typescript. It uses `EventEmitter` and `Uint8Array` instead of node streams and buffers, making the module lighter and fully compatible with browser bundlers like Vite. It provides `@thaunknown/simple-peer/lite.js` which is the same peer implementation, but without MediaTrack and MediaStream handling, just pure Uint8/String data.
15-
14+
This package is a more up-to-date version of feross's simple-peer, rewritten 100% in Typescript. It uses `EventEmitter` and `Uint8Array` instead of node streams and buffers, making the module lighter and fully compatible with browser bundlers like Vite. It provides `@workadventure/simple-peer/lite.ts` which is the same peer implementation, but without MediaTrack and MediaStream handling, just pure Uint8/String data.
1615

1716
> **Breaking change:** This version no longer extends `Duplex` stream. The `pipe()` method is not available. Use `peer.on('data', ...)` and `peer.send()` / `peer.write()` instead.
1817
@@ -53,7 +52,20 @@ This package is used by [WebTorrent](https://webtorrent.io) and [many others](#w
5352
npm install @workadventure/simple-peer
5453
```
5554

56-
This package works in the browser with a bundler like Vite.
55+
This package works with modern bundlers (Vite, Rollup, Webpack) and also directly in the browser via native ESM.
56+
If you do not use a bundler, include it using a `<script type="module">` and import from a CDN:
57+
58+
```html
59+
<script type="module">
60+
import Peer from 'https://cdn.jsdelivr.net/npm/@workadventure/simple-peer@12/dist/index.js'
61+
// use Peer here
62+
</script>
63+
```
64+
65+
Variants are available as separate entry points:
66+
67+
- Lite: `@workadventure/simple-peer/lite`
68+
- Full: `@workadventure/simple-peer/full`
5769

5870
## usage
5971

@@ -74,9 +86,10 @@ Let's create an html page that lets you manually connect two peers:
7486
<button type="submit">submit</button>
7587
</form>
7688
<pre id="outgoing"></pre>
77-
<script src="simplepeer.min.js"></script>
78-
<script>
79-
const p = new SimplePeer({
89+
<script type="module">
90+
import Peer from 'https://cdn.jsdelivr.net/npm/@workadventure/simple-peer@12/dist/index.js'
91+
92+
const p = new Peer({
8093
initiator: location.hash === '#1',
8194
trickle: false
8295
})
@@ -127,7 +140,7 @@ peer-to-peer connection is established.
127140
### data channels
128141

129142
```js
130-
var Peer = require('simple-peer')
143+
var Peer = require('@workadventure/simple-peer')
131144

132145
var peer1 = new Peer({ initiator: true })
133146
var peer2 = new Peer()
@@ -158,7 +171,7 @@ peer2.on('data', data => {
158171
Video/voice is also super simple! In this example, peer1 sends video to peer2.
159172

160173
```js
161-
var Peer = require('simple-peer')
174+
var Peer = require('@workadventure/simple-peer')
162175

163176
// get video/voice stream
164177
navigator.mediaDevices.getUserMedia({
@@ -203,7 +216,7 @@ It is also possible to establish a data-only connection at first, and later add
203216
a video/voice stream, if desired.
204217

205218
```js
206-
var Peer = require('simple-peer') // create peer without waiting for media
219+
var Peer = require('@workadventure/simple-peer') // create peer without waiting for media
207220

208221
var peer1 = new Peer({ initiator: true }) // you don't need streams here
209222
var peer2 = new Peer()
@@ -245,7 +258,7 @@ navigator.mediaDevices.getUserMedia({
245258
To use this library in node, pass in `opts.wrtc` as a parameter (see [the constructor options](#peer--new-peeropts)):
246259

247260
```js
248-
var Peer = require('simple-peer')
261+
var Peer = require('@workadventure/simple-peer')
249262
var wrtc = require('wrtc')
250263

251264
var peer1 = new Peer({ initiator: true, wrtc: wrtc })

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"typescript": "^5.9.3",
4141
"util": "^0.12.5",
4242
"vite": "^7.2.7",
43+
"vite-plugin-dts": "^4.5.4",
4344
"vitest": "^4.0.15",
4445
"ws": "^8.18.0"
4546
},
@@ -59,19 +60,26 @@
5960
"webrtc stream"
6061
],
6162
"license": "MIT",
62-
"main": "dist/index.js",
63+
"main": "dist/index.cjs",
64+
"module": "dist/index.js",
6365
"types": "dist/index.d.ts",
6466
"exports": {
6567
".": {
6668
"types": "./dist/index.d.ts",
69+
"import": "./dist/index.js",
70+
"require": "./dist/index.cjs",
6771
"default": "./dist/index.js"
6872
},
6973
"./lite": {
7074
"types": "./dist/lite.d.ts",
75+
"import": "./dist/lite.js",
76+
"require": "./dist/lite.cjs",
7177
"default": "./dist/lite.js"
7278
},
7379
"./full": {
7480
"types": "./dist/full.d.ts",
81+
"import": "./dist/full.js",
82+
"require": "./dist/full.cjs",
7583
"default": "./dist/full.js"
7684
}
7785
},
@@ -83,7 +91,7 @@
8391
"url": "git://github.com/workadventure/simple-peer.git"
8492
},
8593
"scripts": {
86-
"build": "tsc",
94+
"build": "vite build",
8795
"prebuild": "rm -rf dist",
8896
"prepublishOnly": "npm run build",
8997
"test": "npm run build && vitest run",

0 commit comments

Comments
 (0)