From f659469454ce4bc157217d9265256fac006807df Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 4 Nov 2015 17:44:13 +0100 Subject: [PATCH] Release 0.5.2 --- .gitignore | 13 +++- dist | 2 +- gulpfile.js | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 53 ++++++++++---- src/WebRTC.js | 94 ++++++++++++++++++++++++ 5 files changed, 341 insertions(+), 17 deletions(-) create mode 100644 gulpfile.js create mode 100644 src/WebRTC.js diff --git a/.gitignore b/.gitignore index a56a7ef..383bd88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,13 @@ node_modules - +bower_components +build +build_test +.directory +.codio +.settings +.jshintignore +.jshintrc +.validate.json +/y.js +/y.js.map +/y-* diff --git a/dist b/dist index bbacd36..0a5a7a6 160000 --- a/dist +++ b/dist @@ -1 +1 @@ -Subproject commit bbacd36b0b0f6ee8ef82305c5bc9b80535199715 +Subproject commit 0a5a7a6f696e29d23255fe7a387d9687bc9a20c4 diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..28e1f5f --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,196 @@ +/* eslint-env node */ + +/** Gulp Commands + + gulp command* + [--export ModuleType] + [--name ModuleName] + [--testport TestPort] + [--testfiles TestFiles] + + Module name (ModuleName): + Compile this to "y.js" (default) + + Supported module types (ModuleType): + - amd + - amdStrict + - common + - commonStrict + - ignore (default) + - system + - umd + - umdStrict + + Test port (TestPort): + Serve the specs on port 8888 (default) + + Test files (TestFiles): + Specify which specs to use! + + Commands: + - build:deploy + Build this library for deployment (es6->es5, minified) + - dev:browser + Watch the ./src directory. + Builds the library on changes. + Starts an http-server and serves the test suite on http://127.0.0.1:8888. + - dev:node + Watch the ./src directory. + Builds and specs the library on changes. + Usefull to run with node-inspector. + `node-debug $(which gulp) dev:node + - test: + Test this library +*/ + +var gulp = require('gulp') +var minimist = require('minimist') +var concat = require('gulp-concat') +var $ = require('gulp-load-plugins')() + +var options = minimist(process.argv.slice(2), { + string: ['export', 'name', 'testport', 'testfiles', 'regenerator'], + default: { + export: 'ignore', + name: 'y-webrtc.js', + testport: '8888', + testfiles: 'src/**/*.js', + regenerator: process.version < 'v0.12' + } +}) + +var polyfills = [ +] + +var concatOrder = [ + 'WebRTC.js' +] + +var files = { + src: polyfills.concat(concatOrder.map(function (f) { + return 'src/' + f + })), + test: [].concat(concatOrder.map(function (f) { + return 'build/' + f + }).concat(['build/**/*.spec.js'])) +} + +if (options.regenerator) { + files.test = polyfills.concat(files.test) +} + +gulp.task('deploy:build', function () { + return gulp.src(files.src) + .pipe($.sourcemaps.init()) + .pipe(concat('y-webrtc.js')) + .pipe($.babel({ + loose: 'all', + modules: 'ignore', + experimental: true + })) + .pipe($.uglify()) + .pipe($.sourcemaps.write('.')) + .pipe(gulp.dest('./dist/')) +}) + +gulp.task('deploy:updateSubmodule', function () { + return $.git.updateSubmodule({ args: '--init' }) +}) + +gulp.task('deploy:copy', function () { + return gulp.src(['README.md'], {base: '.'}) + .pipe(gulp.dest('dist/')) +}) + +gulp.task('deploy:bump', function () { + return gulp.src(['./package.json', './dist/package.json'], {base: '.'}) + .pipe($.bump({type: 'patch'})) + .pipe(gulp.dest('./')) +}) + +gulp.task('deploy', ['deploy:updateSubmodule', 'deploy:bump', 'deploy:build', 'deploy:copy'], function () { + return gulp.src('./package.json', {read: false}) + .pipe($.shell([ + 'standard', + 'echo "Deploying version <%= getVersion(file.path) %>"', + 'git pull', + 'cd ./dist/ && git add -A', + 'cd ./dist/ && git commit -am "Deploy <%= getVersion(file.path) %>" -n', + 'cd ./dist/ && git push', + 'cd ./dist/ && git tag -a v<%= getVersion(file.path) %> -m "Release <%= getVersion(file.path) %>"', + 'cd ./dist/ && git push origin --tags', + 'git commit -am "Release <%= getVersion(file.path) %>" -n', + 'git push' + ], { + templateData: { + getVersion: function (s) { + return require(s).version + } + } + })) +}) + +gulp.task('build:test', function () { + var babelOptions = { + loose: 'all', + modules: 'ignore', + experimental: true + } + if (!options.regenerator) { + babelOptions.blacklist = 'regenerator' + } + gulp.src(files.src) + .pipe($.sourcemaps.init()) + .pipe(concat('y-webrtc.js')) + .pipe($.babel(babelOptions)) + .pipe($.sourcemaps.write()) + .pipe(gulp.dest('.')) + + return gulp.src('src/**/*.js') + .pipe($.sourcemaps.init()) + .pipe($.babel(babelOptions)) + .pipe($.sourcemaps.write()) + .pipe(gulp.dest('build')) +}) + +gulp.task('dev:node', ['test'], function () { + gulp.watch('src/**/*.js', ['test']) +}) + +gulp.task('dev:browser', ['build:test'], function () { + gulp.watch('src/**/*.js', ['build:test']) + + gulp.src(files.test) + .pipe($.watch(['build/**/*.js'])) + .pipe($.jasmineBrowser.specRunner()) + .pipe($.jasmineBrowser.server({port: options.testport})) +}) + +gulp.task('dev', ['build:test'], function () { + gulp.start('dev:browser') + gulp.start('dev:node') +}) + +gulp.task('copy:dist', ['deploy:build'], function () { + return gulp.src(['./dist/y-webrtc.js', './dist/y-webrtc.js.map']) + .pipe(gulp.dest('./dist/Examples/bower_components/yjs/')) +}) + +gulp.task('dev:examples', ['copy:dist'], function () { + gulp.watch('src/**/*.js', ['copy:dist']) + return $.serve('dist/Examples')() +}) + +gulp.task('test', ['build:test'], function () { + var testfiles = files.test + if (typeof Promise === 'undefined') { + testfiles.concat(['src/polyfills.js']) + } + return gulp.src(testfiles) + .pipe($.jasmine({ + verbose: true, + includeStuckTrace: true + })) +}) + +gulp.task('default', ['test']) diff --git a/package.json b/package.json index 5f886dd..5c29d5f 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,23 @@ { "name": "y-webrtc", - "version": "0.5.0", + "version": "0.5.3", "description": "WebRTC Connector for Yjs", "main": "./build/node/y-webrtc.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node --harmony ./node_modules/.bin/gulp test", + "lint": "./node_modules/.bin/standard", + "build": "./node_modules/.bin/gulp build" + }, + "pre-commit": [ + "lint", + "test" + ], + "standard": { + "parser": "babel-eslint", + "ignore": [ + "build/**", + "dist/**" + ] }, "repository": { "type": "git", @@ -19,21 +32,31 @@ "bugs": { "url": "https://github.com/y-js/y-webrtc/issues" }, - "homepage": "https://github.com/y-js/y-webrtc", + "homepage": "http://y-js.org", "dependencies": { - "simplewebrtc": "^1.13.1" + "simplewebrtc": "^1.19.1" }, "devDependencies": { - "coffee-script": "^1.9.0", - "gulp": "^3.8.11", - "gulp-browserify": "^0.5.1", - "gulp-coffee": "^2.3.1", - "gulp-coffeelint": "^0.4.0", - "gulp-ignore": "^1.2.1", - "gulp-jshint": "^1.9.2", - "gulp-plumber": "^0.6.6", - "gulp-rename": "^1.2.0", - "gulp-uglify": "^1.1.0", - "jshint": "^2.6.0" + "babel-eslint": "^4.1.2", + "gulp": "^3.9.0", + "gulp-babel": "^5.2.1", + "gulp-bump": "^1.0.0", + "gulp-concat": "^2.6.0", + "gulp-filter": "^3.0.1", + "gulp-git": "^1.6.0", + "gulp-jasmine": "^2.0.1", + "gulp-jasmine-browser": "^0.2.3", + "gulp-load-plugins": "^1.0.0", + "gulp-serve": "^1.2.0", + "gulp-shell": "^0.5.1", + "gulp-sourcemaps": "^1.5.2", + "gulp-tag-version": "^1.3.0", + "gulp-uglify": "^1.4.1", + "gulp-util": "^3.0.6", + "gulp-watch": "^4.3.5", + "minimist": "^1.2.0", + "pre-commit": "^1.1.1", + "promise-polyfill": "^2.1.0", + "standard": "^5.2.2" } } diff --git a/src/WebRTC.js b/src/WebRTC.js new file mode 100644 index 0000000..91e5ca2 --- /dev/null +++ b/src/WebRTC.js @@ -0,0 +1,94 @@ +/* global Y, SimpleWebRTC */ +'use strict' + +class WebRTC extends Y.AbstractConnector { + constructor (y, options) { + if (options === undefined) { + throw new Error('Options must not be undefined!') + } + if (options.room == null) { + throw new Error('You must define a room name!') + } + options.role = 'slave' + super(y, options) + this.webrtcOptions = { + url: options.url || 'https://yatta.ninja:8888', + room: options.room + } + var swr = new SimpleWebRTC(this.webrtcOptions) + this.swr = swr + var self = this + swr.once('connectionReady', function (userId) { + // SimpleWebRTC (swr) is initialized + swr.joinRoom(self.webrtcOptions.room) + + swr.once('joinedRoom', function () { + self.setUserId(userId) + /* + var i + // notify the connector class about all the users that already + // joined the session + for(i in self.swr.webrtc.peers){ + self.userJoined(self.swr.webrtc.peers[i].id, "master") + }*/ + swr.on('channelMessage', function (peer, room_, message) { + // The client received a message + // Check if the connector is already initialized, + // only then forward the message to the connector class + if (message.type != null) { + self.receiveMessage(peer.id, message.payload) + } + }) + }) + + swr.on('createdPeer', function (peer) { + // a new peer/client joined the session. + // Notify the connector class, if the connector + // is already initialized + self.userJoined(peer.id, 'master') + }) + + swr.on('peerStreamRemoved', function (peer) { + // a client left the session. + // Notify the connector class, if the connector + // is already initialized + self.userLeft(peer.id) + }) + }) + } + disconnect () { + this.swr.leaveRoom() + super.disconnect() + } + reconnect () { + this.swr.joinRoom(this.webrtcOptions.room) + super.reconnect() + } + send (uid, message) { + var self = this + // we have to make sure that the message is sent under all circumstances + var send = function () { + // check if the clients still exists + var peer = self.swr.webrtc.getPeers(uid)[0] + var success + if (peer) { + // success is true, if the message is successfully sent + success = peer.sendDirectly('simplewebrtc', 'yjs', message) + } + if (!success) { + // resend the message if it didn't work + setTimeout(send, 500) + } + } + // try to send the message + send() + } + broadcast (message) { + this.swr.sendDirectlyToAll('simplewebrtc', 'yjs', message) + } + isDisconnected () { + return false + } +} + +Y.WebRTC = WebRTC