Skip to content

Commit 374cf80

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 3214250 commit 374cf80

File tree

4 files changed

+664
-25
lines changed

4 files changed

+664
-25
lines changed

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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, 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.
14+
This package is a more up to date version of feross's simple-peer, 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.
1515

1616
> **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.
1717
@@ -49,14 +49,23 @@ This package is used by [WebTorrent](https://webtorrent.io) and [many others](#w
4949
## install
5050

5151
```
52-
npm install simple-peer
52+
npm install @workadventure/simple-peer
5353
```
5454

55-
This package works in the browser with [browserify](https://browserify.org). If
56-
you do not use a bundler, you can use the `simplepeer.min.js` standalone script
57-
directly in a `<script>` tag. This exports a `SimplePeer` constructor on
58-
`window`. Wherever you see `Peer` in the examples below, substitute that with
59-
`SimplePeer`.
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@11/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`
6069

6170
## usage
6271

@@ -77,9 +86,10 @@ Let's create an html page that lets you manually connect two peers:
7786
<button type="submit">submit</button>
7887
</form>
7988
<pre id="outgoing"></pre>
80-
<script src="simplepeer.min.js"></script>
81-
<script>
82-
const p = new SimplePeer({
89+
<script type="module">
90+
import Peer from 'https://cdn.jsdelivr.net/npm/@workadventure/simple-peer@11/dist/index.js'
91+
92+
const p = new Peer({
8393
initiator: location.hash === '#1',
8494
trickle: false
8595
})
@@ -130,7 +140,7 @@ peer-to-peer connection is established.
130140
### data channels
131141

132142
```js
133-
var Peer = require('simple-peer')
143+
var Peer = require('@workadventure/simple-peer')
134144

135145
var peer1 = new Peer({ initiator: true })
136146
var peer2 = new Peer()
@@ -161,7 +171,7 @@ peer2.on('data', data => {
161171
Video/voice is also super simple! In this example, peer1 sends video to peer2.
162172

163173
```js
164-
var Peer = require('simple-peer')
174+
var Peer = require('@workadventure/simple-peer')
165175

166176
// get video/voice stream
167177
navigator.mediaDevices.getUserMedia({
@@ -206,7 +216,7 @@ It is also possible to establish a data-only connection at first, and later add
206216
a video/voice stream, if desired.
207217

208218
```js
209-
var Peer = require('simple-peer') // create peer without waiting for media
219+
var Peer = require('@workadventure/simple-peer') // create peer without waiting for media
210220

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

250260
```js
251-
var Peer = require('simple-peer')
261+
var Peer = require('@workadventure/simple-peer')
252262
var wrtc = require('wrtc')
253263

254264
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)