ericz 12 年之前
父節點
當前提交
62aa5568ee
共有 13 個文件被更改,包括 261 次插入180 次删除
  1. 1 3
      README.md
  2. 5 0
      changelog.md
  3. 4 1
      dist/peer.js
  4. 0 0
      dist/peer.min.js
  5. 31 29
      docs/api.md
  6. 3 0
      lib/connectionmanager.js
  7. 1 1
      package.json
  8. 24 12
      test/connectionmanager.js
  9. 74 54
      test/dataconnection.js
  10. 10 1
      test/peer.js
  11. 18 9
      test/socket.js
  12. 1 1
      test/test.html
  13. 89 69
      test/util.js

+ 1 - 3
README.md

@@ -52,8 +52,6 @@ Each peer simply provides a identifier with which other peers using the same API
 ## In the future
 
 * Tests
-* Firefox support
-* Stream API
-* Video/audio support
+* Video/audio support (in progress)
 
 

+ 5 - 0
changelog.md

@@ -1,5 +1,10 @@
 # PeerJS Changelog
 
+## Version 0.2.8 (1 July 2013)
+* Fix bug, no error on Firefox 24 due to missing error callback.
+* TLS secure PeerServers now supported.
+* Updated version of Reliable shim.
+
 ## Version 0.2.7 (28 May 2013)
 * Fix bug, no error when .disconnect called in before socket connection established.
 * Fix bug, failure to enter debug mode when aborting because browser not supported.

+ 4 - 1
dist/peer.js

@@ -1,4 +1,4 @@
-/*! peerjs.js build:0.2.7, development. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */
+/*! peerjs.js build:0.2.8, development. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */
 (function(exports){
 var binaryFeatures = {};
 binaryFeatures.useBlobBuilder = (function(){
@@ -1943,6 +1943,9 @@ ConnectionManager.prototype._makeOffer = function() {
       self.emit('error', err);
       util.log('Failed to setLocalDescription, ', err);
     });
+  }, function(err) {
+    self.emit('error', err);
+    util.log('Failed to createOffer, ', err);
   });
 };
 

File diff suppressed because it is too large
+ 0 - 0
dist/peer.min.js


+ 31 - 29
docs/api.md

@@ -2,34 +2,34 @@
 
 **Due to browsers' incomplete support of the WebRTC DataChannel specification, many features of PeerJS have caveats.
 [View the status page for full details](http://peerjs.com/status).**
-
-- [Class: peerjs.Peer](#class-peerjspeer)
-  - [new Peer([id], [options])](#new-peerid-options)
-  - [Peer.browser](#peerbrowser)
-  - [peer.id](#peerid)
-  - [peer.connections](#peerconnections)
-  - [peer.connect(id, [options])](#peerconnectid-options)
-  - [peer.destroy()](#peerdestroy)
-  - [peer.disconnect()](#peerdisconnect)
-  - [peer.disconnected](#peerdisconnected)
-  - [peer.destroyed](#peerdestroyed)
-  - [Event: 'connection'](#event-connection)
-  - [Event: 'open'](#event-open)
-  - [Event: 'error'](#event-error)
-  - [Event: 'close'](#event-close)
-- [Class: peerjs.DataConnection](#class-peerjsdataconnection)
-  - [EXPERIMENTAL reliable and large file transfer:](#experimental-reliable-and-large-file-transfer)
-  - [connection.peer](#connectionpeer)
-  - [connection.open](#connectionopen)
-  - [connection.metadata](#connectionmetadata)
-  - [connection.label](#connectionlabel)
-  - [connection.serialization](#connectionserialization)
-  - [connection.send(data)](#connectionsenddata)
-  - [connection.close()](#connectionclose)
-  - [Event: 'data'](#event-data)
-  - [Event: 'open'](#event-open-1)
-  - [Event: 'error'](#event-error-1)
-  - [Event: 'close'](#event-close-1)
+
+- [Class: peerjs.Peer](#class-peerjspeer)
+  - [new Peer([id], [options])](#new-peerid-options)
+  - [Peer.browser](#peerbrowser)
+  - [peer.id](#peerid)
+  - [peer.connections](#peerconnections)
+  - [peer.connect(id, [options])](#peerconnectid-options)
+  - [peer.destroy()](#peerdestroy)
+  - [peer.disconnect()](#peerdisconnect)
+  - [peer.disconnected](#peerdisconnected)
+  - [peer.destroyed](#peerdestroyed)
+  - [Event: 'connection'](#event-connection)
+  - [Event: 'open'](#event-open)
+  - [Event: 'error'](#event-error)
+  - [Event: 'close'](#event-close)
+- [Class: peerjs.DataConnection](#class-peerjsdataconnection)
+  - [EXPERIMENTAL reliable and large file transfer:](#experimental-reliable-and-large-file-transfer)
+  - [connection.peer](#connectionpeer)
+  - [connection.open](#connectionopen)
+  - [connection.metadata](#connectionmetadata)
+  - [connection.label](#connectionlabel)
+  - [connection.serialization](#connectionserialization)
+  - [connection.send(data)](#connectionsenddata)
+  - [connection.close()](#connectionclose)
+  - [Event: 'data'](#event-data)
+  - [Event: 'open'](#event-open-1)
+  - [Event: 'error'](#event-error-1)
+  - [Event: 'close'](#event-close-1)
 
 ## Class: peerjs.Peer
 
@@ -142,7 +142,7 @@ The `error` object also has a `type` parameter that may be helpful in responding
 * `invalid-key`: The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).
 * `unavailable-id`: The ID passed into the Peer constructor is already taken.
 * `firefoxism`: The operation you're trying to perform is not supported in firefox.
-* `ssl-unavailable`: PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.
+* `ssl-unavailable`: PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.
 * Errors types that shouldn't regularly appear:
   * `server-error`: Unable to reach the server.
   * `socket-error`: An error from the underlying socket.
@@ -168,6 +168,8 @@ There is no constructor. A `DataConnection` object must be obtained in the callb
 
 (CHROME ONLY. Firefox has reliable transport built in and reliable transfer is the default option.) Simply pass in `reliable: true` when calling `.connect(...)`. This module is experimental, temporary, and exists here: https://github.com/michellebu/reliable
 
+*Caveat:* With reliable enabled in Chrome, you can no longer customize the serialization format used for data transfer.
+
 ### connection.peer
 
 The id of the remote peer this connection is connected to.

+ 3 - 0
lib/connectionmanager.js

@@ -200,6 +200,9 @@ ConnectionManager.prototype._makeOffer = function() {
       self.emit('error', err);
       util.log('Failed to setLocalDescription, ', err);
     });
+  }, function(err) {
+    self.emit('error', err);
+    util.log('Failed to createOffer, ', err);
   });
 };
 

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "peerjs",
-  "version": "0.2.7",
+  "version": "0.2.8",
   "description": "PeerJS client library",
   "devDependencies": {
     "uglify-js": "~1.3.4",

+ 24 - 12
test/connectionmanager.js

@@ -1,29 +1,41 @@
 describe('ConnectionManager', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
-  it('_setupDataChannel');
+  describe('#_setupDataChannel', function() {
+  });
 
-  it('_makeOffer');
+  describe('#_makeOffer', function() {
+  });
 
-  it('_makeAnswer');
+  describe('#_makeAnswer', function() {
+  });
 
-  it('_cleanup');
+  describe('#_cleanup', function() {
+  });
 
-  it('_attachConnectionListeners');
+  describe('#_attachConnectionListeners', function() {
+  });
 
-  it('handleSDP');
+  describe('#handleSDP', function() {
+  });
 
-  it('handleCandidate');
+  describe('#handleCandidate', function() {
+  });
 
-  it('handleLeave');
+  describe('#handleLeave', function() {
+  });
 
-  it('close');
+  describe('#close', function() {
+  });
 
-  it('connect');
+  describe('#connect', function() {
+  });
 
-  it('update');
+  describe('#update', function() {
+  });
 
 });

+ 74 - 54
test/dataconnection.js

@@ -15,88 +15,108 @@ describe('DataConnection', function() {
       },
       // Only sends to peer's dc.
       send: function(msg) {
-        pdc._dc.onmessage({ data: msg });
+        pdc._handleDataMessage({ data: msg });
       }
     };
 
-    it('constructor', function() {
-      // Test without 'new' keyword.
-      dc = DataConnection('peer', null,
-        { serialization: 'json',
-          metadata: { message: 'I\'m testing!'},
-          reliable: true });
+    describe('constructor', function() {
+      it('should accept options properly', function() {
+        // Test without 'new' keyword.
+        dc = DataConnection('peer', null,
+          { serialization: 'json',
+            metadata: { message: 'I\'m testing!'} });
 
-      expect(dc.peer).to.be('peer');
-      expect(dc.serialization).to.be('json');
-      expect(dc.metadata.message).to.be('I\'m testing!');
+        expect(dc.peer).to.be('peer');
+        expect(dc.serialization).to.be('json');
+        expect(dc.metadata.message).to.be('I\'m testing!');
 
-      expect(dc._dc).to.be(null);
-      dc._dc = new DataChannelStub();
+        expect(dc._dc).to.be(null);
+        dc._dc = new DataChannelStub();
+      });
     });
 
     it('inherits from EventEmitter');
 
-    it('_configureDataChannel', function() {
-      dc._configureDataChannel();
-
-      if (util.browserisms === 'Firefox') {
-        expect(dc._dc.binaryType).to.be('arraybuffer');
-      } else {
-        expect(dc._reliable).not.to.be(undefined);
-      }
+    before(function() {
+      dc = DataConnection('peer', null,
+        { serialization: 'json',
+          metadata: { message: 'I\'m testing!'} });
+      dc._dc = new DataChannelStub();
     });
 
-    it('should fire an `open` event', function(done) {
-      dc.on('open', function() {
-        expect(dc.open).to.be(true)
-        done();
+    describe('#_configureDataChannel', function() {
+      it('should set the correct binaryType', function() {
+        dc._configureDataChannel();
+
+        if (util.browserisms === 'Firefox') {
+          expect(dc._dc.binaryType).to.be('arraybuffer');
+        } else {
+          expect(dc._reliable).to.be(undefined);
+        }
+      });
+
+      it('should fire an `open` event', function(done) {
+        dc.on('open', function() {
+          expect(dc.open).to.be(true)
+          done();
+        });
+        dc._dc.onopen();
       });
-      dc._dc.onopen();
     });
 
-    it('_handleDataMessage', function() {
-       
+    describe('#_handleDataMessage', function() {
+
     });
 
-    it('addDC', function() {
-      pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
-      pdc.addDC(new DataChannelStub());
+    describe('#addDC', function() {
+      it('should add a DataConnection properly', function() {
+        pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
+        pdc.addDC(new DataChannelStub());
 
-      expect(pdc._dc).not.to.be(null);
+        expect(pdc._dc).not.to.be(null);
+      });
     });
 
-    it('send', function(done) {
-      pdc.on('data', function(data) {
-        expect(data.hello).to.be('peer-tester');
-        done();
+    describe('#send', function() {
+      it('should send data to the peer', function(done) {
+        pdc = new DataConnection('ignore', null, { serialization: 'json' });
+        pdc.on('data', function(data) {
+          expect(data.hello).to.be('peer-tester');
+          done();
+        });
+        dc.send({ hello: 'peer-tester' });
       });
-      dc.send({ hello: 'peer-tester' });
     });
 
-    it('_cleanup', function(done) {
-      var first = true;
-      dc.on('close', function() {
-        expect(dc.open).to.be(false)
+    describe('#_cleanup', function() {
+      it('should emit a `close` event', function(done) {
+        var first = true;
+        dc.on('close', function() {
+          expect(dc.open).to.be(false)
 
-        // Calling it twice should be fine.
-        if (first) {
-          first = false;
-          dc._cleanup();
-        }
+          // Calling it twice should be fine.
+          if (first) {
+            first = false;
+            dc._cleanup();
+          }
 
-        done();
-      });
+          done();
+        });
 
-      dc._cleanup();
+        dc._cleanup();
+      });
     });
 
-    it('close', function() {
-      dc._cleanup = function() {
-        throw Error();
-      }
+    // Hacks hacks
+    describe('#close', function() {
+      it('should not call _cleanup', function() {
+        dc._cleanup = function() {
+          throw Error();
+        }
 
-      // Should not call _cleanup again.
-      dc.close();
+        // Should not call _cleanup again.
+        dc.close();
+      });
     });
 
 

+ 10 - 1
test/peer.js

@@ -1,7 +1,16 @@
 describe('Peer', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
+  describe('.browser', function() {
+    it('should be the current browser', function() {
+      var browser = window.mozRTCPeerConnection ? 'Firefox' : 'Unknown';
+      browser = window.webkitRTCPeerConnection ? 'Webkit' : browser;
+      expect(Peer.browser).to.be(browser);
+    });
+  });
+
 });

+ 18 - 9
test/socket.js

@@ -1,23 +1,32 @@
 describe('Socket', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
-  it('start');
+  describe('#start', function() {
+  });
 
-  it('_startWebSocket');
+  describe('#_startWebSocket', function() {
+  });
 
-  it('_startXhrStream');
+  describe('#_startXhrStream', function() {
+  });
 
-  it('_handleStream');
+  describe('#_handleStream', function() {
+  });
 
-  it('_setHTTPTimeout');
+  describe('#_setHTTPTimeout', function() {
+  });
 
-  it('send');
+  describe('#send', function() {
+  });
 
-  it('close');
+  describe('#close', function() {
+  });
 
-  it('_wsOpen');
+  describe('#_wsOpen', function() {
+  });
 
 });

+ 1 - 1
test/test.html

@@ -15,9 +15,9 @@
   <script src="../lib/util.js"></script>
   <script src="../lib/adapter.js"></script>
   <script src="../lib/dataconnection.js"></script>
-  <script src="../lib/connectionmanager.js"></script>
   <script src="../lib/peer.js"></script>
   <script src="../lib/socket.js"></script>
+  <script src="../lib/connectionmanager.js"></script>
   <script src="../deps/reliable/lib/reliable.js"></script>
 
   <script src="../node_modules/expect.js/expect.js"></script>

+ 89 - 69
test/util.js

@@ -11,92 +11,112 @@ describe('util', function() {
     }
   }
 
-  it('inherits', function() {
-    function ctor() {}
-    function superCtor() {}
-    superCtor.prototype.test = function() { return 5; }
-    util.inherits(ctor, superCtor);
-    expect(new ctor()).to.be.a(superCtor);
-    expect(new ctor().test()).to.be.equal(5);
-  })
+  describe('.inherits', function() {
+    it('should make functions inherit properly', function() {
+      function ctor() {}
+      function superCtor() {}
+      superCtor.prototype.test = function() { return 5; }
+      util.inherits(ctor, superCtor);
+      expect(new ctor()).to.be.a(superCtor);
+      expect(new ctor().test()).to.be.equal(5);
+    });
+  });
 
   /*
    *  extend overwrites keys if already exists
    *  leaves existing keys alone otherwise
    */
-  it('extend', function() {
-    var a = {a: 1, b: 2, c: 3, d: 4}
-      , b = {d: 2};
-    util.extend(b, a);
-    expect(b).to.eql(a);
-    expect(b.d).to.be.equal(4);
-    b = {z: 2};
-    util.extend(b, a);
-    expect(b.z).to.be.equal(2);
-  })
+  describe('.extend', function() {
+    it('should copy the properties of b to a', function() {
+      var a = {a: 1, b: 2, c: 3, d: 4}
+        , b = {d: 2};
+      util.extend(b, a);
+      expect(b).to.eql(a);
+      expect(b.d).to.be.equal(4);
+      b = {z: 2};
+      util.extend(b, a);
+      expect(b.z).to.be.equal(2);
+    });
+  });
 
-  it('pack', function() {
-    expect(util.pack).to.be.equal(BinaryPack.pack);
-  })
+  describe('.pack', function() {
+    it('should be BinaryPack\'s `pack` function', function() {
+      expect(util.pack).to.be.equal(BinaryPack.pack);
+    });
+  });
 
-  it('unpack', function() {
-    expect(util.unpack).to.be.equal(BinaryPack.unpack);
-  })
+  describe('.unpack', function() {
+    it('should be BinaryPack\'s `unpack` function', function() {
+      expect(util.unpack).to.be.equal(BinaryPack.unpack);
+    });
+  });
 
   // FF no like
-  it('log', function(done) {
-    var consolelog = console.log;
-    // default is false
-    expect(util.debug).to.be.equal(false);
-    util.debug = true;
-    console.log = function() {
-      var arg = Array.prototype.slice.call(arguments);
-      expect(arg.join(' ')).to.be.equal('PeerJS:  hi');
-      done();
-    }
-    util.log('hi');
-    // reset
-    console.log = consolelog;
-    util.debug = false;
-  })
-
-  it('setZeroTimeout', function(done) {
-    var isdone = false;
-    util.setZeroTimeout(function() {
-      if (isdone) {
+  describe('.log', function() {
+    it('should log with the PeerJS prefix', function(done) {
+      var consolelog = console.log;
+      // default is false
+      expect(util.debug).to.be.equal(false);
+      util.debug = true;
+      console.log = function() {
+        var arg = Array.prototype.slice.call(arguments);
+        expect(arg.join(' ')).to.be.equal('PeerJS:  hi');
         done();
       }
+      util.log('hi');
+      // reset
+      console.log = consolelog;
+      util.debug = false;
     });
-    isdone = true;
-  })
+  });
 
-  it('blobToArrayBuffer', function(done) {
-    var blob = new Blob(['hi']);
-    util.blobToArrayBuffer(blob, function(result) {
-      expect(result.byteLength).to.be.equal(2);
-      expect(result.slice).to.be.a('function');
-      expect(result instanceof ArrayBuffer).to.be.equal(true);
-      done();
+  describe('.setZeroTimeout', function() {
+    it('should call the function after a 0s timeout', function(done) {
+      var isdone = false;
+      util.setZeroTimeout(function() {
+        if (isdone) {
+          done();
+        }
+      });
+      isdone = true;
     });
-  })
+  });
 
-  it('blobToBinaryString', function(done) {
-    var blob = new Blob(['hi']);
-    util.blobToBinaryString(blob, function(result) {
-      expect(result).to.equal('hi');
-      done();
+  describe('.blobToArrayBuffer', function() {
+    it('should convert a blob to an arraybuffer', function(done) {
+      var blob = new Blob(['hi']);
+      util.blobToArrayBuffer(blob, function(result) {
+        expect(result.byteLength).to.be.equal(2);
+        expect(result.slice).to.be.a('function');
+        expect(result instanceof ArrayBuffer).to.be.equal(true);
+        done();
+      });
+    });
+  });
+
+  describe('blobToBinaryString', function() {
+    it('should convert a blob to a binary string', function(done) {
+      var blob = new Blob(['hi']);
+      util.blobToBinaryString(blob, function(result) {
+        expect(result).to.equal('hi');
+        done();
+      });
     });
-  })
+  });
 
-  it('binaryStringToArrayBuffer', function() {
-    var ba = util.binaryStringToArrayBuffer('\0\0');
-    expect(ba.byteLength).to.be.equal(2);
-    expect(ba.slice).to.be.a('function');
-    expect(ba instanceof ArrayBuffer).to.be.equal(true);
-  })
+  describe('.binaryStringToArrayBuffer', function() {
+    it('should convert a binary string to an arraybuffer', function() {
+      var ba = util.binaryStringToArrayBuffer('\0\0');
+      expect(ba.byteLength).to.be.equal(2);
+      expect(ba.slice).to.be.a('function');
+      expect(ba instanceof ArrayBuffer).to.be.equal(true);
+    });
+  });
 
-  it('randomToken', function() {
-    testRandom(util.randomToken);
-  })
+  describe('.randomToken', function() {
+    it('should return a random string', function() {
+      testRandom(util.randomToken);
+    });
+  });
 
 });

Some files were not shown because too many files changed in this diff