Skip to content

Commit

Permalink
feat: support typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
zhensherlock committed Apr 27, 2023
1 parent 1a3c1bb commit 71ad915
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 12 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
<p align="center">
<a href="https://npmjs.com/package/webrtc-streamer-helper"><img src="https://badgen.net/npm/v/webrtc-streamer-helper" alt="npm package"></a>
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/webrtc-streamer-helper">
<img alt="GitHub" src="https://img.shields.io/github/license/zhensherlock/webrtc-streamer-helper">
</p>

# webrtc-streamer-helper
webrtc streamer helper

> This is a webrtc streamer helper.
## Installing

```bash
# or pnpm or yarn
npm install webrtc-streamer-helper
```

## Usage

```ts
import { WebRTCStreamer } from 'webrtc-streamer-helper'

const webRtcServer = new WebRTCStreamer({
url: 'http://10.57.2.244:8000',
element: 'div-1'
})

webRtcServer.connect(
videoUrl,
audioUrl,
'rtptransport=tcp&timeout=60&width=320&height=0',
null
)()
```

## License

[MIT](LICENSE).
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "webrtc streamer helper",
"scripts": {
"dev": "concurrently \"npm run src:dev\"",
"src:dev": "rollup -c --watch --bundleConfigAsCjs"
"src:dev": "rollup -c --watch --bundleConfigAsCjs",
"build": "rollup -c --bundleConfigAsCjs --environment NODE_ENV:production"
},
"repository": {
"type": "git",
Expand All @@ -16,6 +17,15 @@
"url": "https://github.com/zhensherlock/webrtc-streamer-helper/issues"
},
"homepage": "https://github.com/zhensherlock/webrtc-streamer-helper#readme",
"keywords": [
"webrtc-streamer",
"webrtc-streamer-helper"
],
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"browser": "dist/index.browser.js",
"unpkg": "",
"types": "dist/types/main.d.ts",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-eslint": "^9.0.3",
Expand Down
28 changes: 21 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,49 @@ import cssnano from 'cssnano'
const output = [
{
format: 'esm',
file: 'dist/webrtc-streamer-helper.esm.js'
file: 'dist/index.esm.js',
sourcemap: true
},
{
format: 'umd',
name: 'WebrtcStreamerHelper',
file: 'dist/webrtc-streamer-helper.umd.js'
file: 'dist/index.umd.js',
sourcemap: true
},
{
format: 'iife',
name: 'WebrtcStreamerHelper',
file: 'dist/webrtc-streamer-helper.browser.js'
file: 'dist/index.browser.js',
sourcemap: true
},
{
format: 'cjs',
file: 'dist/index.cjs.js',
sourcemap: true
},
// min
{
format: 'esm',
file: 'dist/webrtc-streamer-helper.esm.min.js',
file: 'dist/index.esm.min.js',
plugins: [terser()]
},
{
format: 'umd',
name: 'WebrtcStreamerHelper',
file: 'dist/webrtc-streamer-helper.umd.min.js',
file: 'dist/index.umd.min.js',
plugins: [terser()]
},
{
format: 'iife',
name: 'WebrtcStreamerHelper',
file: 'dist/webrtc-streamer-helper.browser.min.js',
file: 'dist/index.browser.min.js',
plugins: [terser()]
}
},
{
format: 'cjs',
file: 'dist/index.cjs.min.js',
plugins: [terser()]
},
]

export default [
Expand Down
11 changes: 11 additions & 0 deletions src/core/webRTCStreamer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { WebRTCStreamerOptions } from '../types'
import { initialOptions } from '../utils/initialization'

/**
* Interface with WebRTC-streamer API
*/
export default class WebRTCStreamer {
private element?: Element
private options: WebRTCStreamerOptions
Expand All @@ -12,8 +15,16 @@ export default class WebRTCStreamer {
private earlyCandidates: RTCIceCandidate[] = []
private srcObject: any

/**
* Instantiate object
* @constructor
* @param args
*/
constructor (args: Partial<WebRTCStreamerOptions> = {}) {
this.options = Object.assign({}, initialOptions, args)
if (!this.options.url) {
this.options.url = `${window.location.protocol}//${window.location.hostname}:${window.location.port}`
}
this.changeElement(this.options.element)
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import WebRTCStreamer from './core/webRTCStreamer'
import './style'

export default WebRTCStreamer
export {
WebRTCStreamer
}
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export interface WebRTCStreamerOptions {
/**
* id of the video element tag
*/
element: Element | string;
/**
* url of webrtc-streamer (default is current location)
*/
url: string;
}
2 changes: 1 addition & 1 deletion src/utils/initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import type { WebRTCStreamerOptions } from '../types'

export const initialOptions: WebRTCStreamerOptions = {
element: '',
url: `${location.protocol}//${location.hostname}:${location.port}`
url: ''
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "es5",
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "./dist",
"outDir": "./dist/types",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
Expand Down

0 comments on commit 71ad915

Please sign in to comment.