Skip to content

Commit

Permalink
Merge pull request #2 from meetecho/es-module
Browse files Browse the repository at this point in the history
Make Janode an ES module
  • Loading branch information
atoppi authored Jan 18, 2022
2 parents 8c0d436 + c15232a commit ba0b006
Show file tree
Hide file tree
Showing 40 changed files with 311 additions and 242 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"env": {
"node": true,
"browser": false,
"es2020": true
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest"
},
"rules": {
"no-unused-vars": [
"warn",
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Read the examples [on the git repo](https://github.com/meetecho/janode) to have


```js
const Janode = require('janode');
import Janode from 'janode';
const { Logger } = Janode;
const EchoTestPlugin = require('janode/src/plugins/echotest-plugin');
import EchoTestPlugin from 'janode/src/plugins/echotest-plugin.js';

const connection = await Janode.connect({
is_admin: false,
Expand Down Expand Up @@ -60,7 +60,7 @@ await echoHandle.detach();
## Admin API example

```js
const Janode = require('janode');
import Janode from 'janode';

const admin = await Janode.connect({
is_admin: true,
Expand Down Expand Up @@ -121,9 +121,11 @@ To change the configuration edit `config.js` under `src`.

## Usage in browsers

The core library should work in browsers.
Just create a bundle by using tools like `browserify` and import it in your web app.
Janode should work in browsers.
You need to create a bundle with the core library and the needed plugins using a tool that can:
- shim native node modules (e.g. `EventEmitter`)
- import commonjs modules (some dependencies could still use that format)
- parse the `browser` field in the `package.json`

If you get the code from the repo, there is a sample browser app under `examples/browser/app`.
The provided `bundle.sh` script in `examples/browser` uses `browserify` to bundle an echotest library `bundle.js` in `examples/browser/app`.
The `bundle.js` script exposes a `janodeLib` global object that is used by the web app.
If you get the code from the repo, you can find a `rollup` bundling sample in the `bundle.sh` script under `examples/browser/`.
The output will be a `bundle.js` script that defines an `App` global object with the members `Janode` and `EchoTestPlugin`.
12 changes: 6 additions & 6 deletions examples/audiobridge/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/audiobridge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "janode-audiobridge",
"description": "Janode audiobridge app",
"type": "module",
"keywords": [
"janus",
"webrtc",
Expand All @@ -26,4 +27,4 @@
"build-config": "node -e \"var fs = require('fs');fs.createReadStream('src/config.template.js').pipe(fs.createWriteStream('src/config.js'));\"",
"start": "node src/index.js"
}
}
}
8 changes: 3 additions & 5 deletions examples/audiobridge/src/config.template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {

export default {
janode: {
address: [{
url: 'ws://127.0.0.1:8188/',
Expand All @@ -8,11 +7,10 @@ module.exports = {
// seconds between retries after a connection setup error
retry_time_secs: 10
},

web: {
port: 4443,
bind: '0.0.0.0',
key: __dirname + '/../../../key.pem',
cert: __dirname + '/../../../cert.pem'
key: '/path/to/key.pem',
cert: '/path/to/cert.pem'
}
};
36 changes: 22 additions & 14 deletions examples/audiobridge/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Janode = require('../../../src/janode.js');
const { janode: janodeConfig, web: serverConfig } = require('./config.js');
import { readFileSync } from 'fs';
import Janode from '../../../src/janode.js';
import config from './config.js';
const { janode: janodeConfig, web: serverConfig } = config;

import { fileURLToPath } from 'url';
import { dirname, basename } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const { Logger } = Janode;
const LOG_NS = `[${path.basename(__filename)}]`;
const AudioBridgePlugin = require('../../../src/plugins/audiobridge-plugin');
const LOG_NS = `[${basename(__filename)}]`;
import AudioBridgePlugin from '../../../src/plugins/audiobridge-plugin.js';

const express = require('express');
import express from 'express';
const app = express();
const options = {
key: serverConfig.key ? fs.readFileSync(serverConfig.key) : null,
cert: serverConfig.cert ? fs.readFileSync(serverConfig.cert) : null,
key: serverConfig.key ? readFileSync(serverConfig.key) : null,
cert: serverConfig.cert ? readFileSync(serverConfig.cert) : null,
};
const server = (options.key && options.cert) ? require('https').createServer(options, app) : require('http').createServer(app);
const io = require('socket.io')(server);
import { createServer as createHttpsServer } from 'https';
import { createServer as createHttpServer } from 'http';
const httpServer = (options.key && options.cert) ? createHttpsServer(options, app) : createHttpServer(app);
import { Server } from 'socket.io';
const io = new Server(httpServer);

const scheduleBackEndConnection = (function () {
let task = null;
Expand Down Expand Up @@ -95,7 +103,7 @@ async function initBackEnd() {
}

function initFrontEnd() {
if (server.listening) return Promise.reject(new Error('Server already listening'));
if (httpServer.listening) return Promise.reject(new Error('Server already listening'));

Logger.info(`${LOG_NS} initializing socketio front end...`);

Expand Down Expand Up @@ -421,7 +429,7 @@ function initFrontEnd() {
// http server binding
return new Promise((resolve, reject) => {
// web server binding
server.listen(
httpServer.listen(
serverConfig.port,
serverConfig.bind,
() => {
Expand All @@ -430,7 +438,7 @@ function initFrontEnd() {
}
);

server.on('error', e => reject(e));
httpServer.on('error', e => reject(e));
});
}

Expand Down
5 changes: 3 additions & 2 deletions examples/browser/app/echotest-client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*global document, libJanode, RTCPeerConnection, navigator */
/* global document, App, RTCPeerConnection, navigator */

'use strict';

const { Janode, EchoTestPlugin } = libJanode;
const { Janode, EchoTestPlugin } = App;

const Logger = Janode.Logger;
let janodeConnection;

Expand Down
38 changes: 31 additions & 7 deletions examples/browser/bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,47 @@ set -u
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
BUILD_DIR="$SCRIPT_PATH/app"
BUNDLE_FILENAME="bundle.js"
EXPORTED_OBJECT="libJanode"
EXPORTED_OBJECT="App"
DEPLOY_DIR="/var/www/html"

# bundle with echotest plugin

pushd $SCRIPT_PATH

cat <<EOF > app.js
module.exports = { Janode: require('../../src/janode.js'), EchoTestPlugin: require('../../src/plugins/echotest-plugin.js') };
import Janode from '../../src/janode.js';
import EchoTestPlugin from '../../src/plugins/echotest-plugin.js';
export default {
Janode,
EchoTestPlugin,
}
EOF

cat <<EOF > config.js
import nodePolyfills from 'rollup-plugin-polyfill-node';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
export default {
input: 'app.js',
output: {
file: '$BUILD_DIR/$BUNDLE_FILENAME',
format: 'iife',
name: '$EXPORTED_OBJECT'
},
plugins: [
nodePolyfills(),
nodeResolve({ browser: true }),
commonjs()
]
};
EOF

browserify app.js --standalone $EXPORTED_OBJECT -o $BUILD_DIR/$BUNDLE_FILENAME \
--dg=false \
--ignore "../../src/transport-unix.js"
npm install --no-save rollup-plugin-polyfill-node @rollup/plugin-node-resolve @rollup/plugin-commonjs
rollup --config config.js

cp ./app/*.* $DEPLOY_DIR

rm app.js
rm *.js

popd
12 changes: 6 additions & 6 deletions examples/echotest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/echotest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "janode-echotest",
"description": "Janode echotest app",
"type": "module",
"keywords": [
"janus",
"webrtc",
Expand All @@ -26,4 +27,4 @@
"build-config": "node -e \"var fs = require('fs');fs.createReadStream('src/config.template.js').pipe(fs.createWriteStream('src/config.js'));\"",
"start": "node src/index.js"
}
}
}
8 changes: 3 additions & 5 deletions examples/echotest/src/config.template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {

export default {
janode: {
address: [{
url: 'ws://127.0.0.1:8188/',
Expand All @@ -8,11 +7,10 @@ module.exports = {
// seconds between retries after a connection setup error
retry_time_secs: 10
},

web: {
port: 4443,
bind: '0.0.0.0',
key: __dirname + '/../../../key.pem',
cert: __dirname + '/../../../cert.pem'
key: '/path/to/key.pem',
cert: '/path/to/cert.pem'
}
};
35 changes: 22 additions & 13 deletions examples/echotest/src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
'use strict';

const fs = require('fs');
const Janode = require('../../../src/janode.js');
const { janode: janodeConfig, web: serverConfig } = require('./config.js');
import { readFileSync } from 'fs';
import Janode from '../../../src/janode.js';
import config from './config.js';
const { janode: janodeConfig, web: serverConfig } = config;

import { fileURLToPath } from 'url';
import { dirname, basename } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const { Logger } = Janode;
const LOG_NS = `[${require('path').basename(__filename)}]`;
const EchoTestPlugin = require('../../../src/plugins/echotest-plugin');
const LOG_NS = `[${basename(__filename)}]`;
import EchoTestPlugin from '../../../src/plugins/echotest-plugin.js';

const express = require('express');
import express from 'express';
const app = express();
const options = {
key: serverConfig.key ? fs.readFileSync(serverConfig.key) : null,
cert: serverConfig.cert ? fs.readFileSync(serverConfig.cert) : null,
key: serverConfig.key ? readFileSync(serverConfig.key) : null,
cert: serverConfig.cert ? readFileSync(serverConfig.cert) : null,
};
const server = (options.key && options.cert) ? require('https').createServer(options, app) : require('http').createServer(app);
const io = require('socket.io')(server);
import { createServer as createHttpsServer } from 'https';
import { createServer as createHttpServer } from 'http';
const httpServer = (options.key && options.cert) ? createHttpsServer(options, app) : createHttpServer(app);
import { Server } from 'socket.io';
const io = new Server(httpServer);

const scheduleBackEndConnection = (function () {
let task = null;
Expand Down Expand Up @@ -84,7 +93,7 @@ async function initBackEnd() {
}

function initFrontEnd() {
if (server.listening) return Promise.reject(new Error('Server already listening'));
if (httpServer.listening) return Promise.reject(new Error('Server already listening'));

Logger.info(`${LOG_NS} initializing socketio front end...`);

Expand Down Expand Up @@ -198,7 +207,7 @@ function initFrontEnd() {
// http server binding
return new Promise((resolve, reject) => {
// web server binding
server.listen(
httpServer.listen(
serverConfig.port,
serverConfig.bind,
() => {
Expand All @@ -207,7 +216,7 @@ function initFrontEnd() {
}
);

server.on('error', e => reject(e));
httpServer.on('error', e => reject(e));
});
}

Expand Down
1 change: 1 addition & 0 deletions examples/helloworld/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "janode-helloworld",
"description": "Janode helloworld app",
"type": "module",
"keywords": [
"janus",
"webrtc",
Expand Down
Loading

0 comments on commit ba0b006

Please sign in to comment.