Browse Source

add id to constructor of peer

ericz 12 years ago
parent
commit
f71557168a
1 changed files with 7 additions and 3 deletions
  1. 7 3
      lib/peer.js

+ 7 - 3
lib/peer.js

@@ -1,7 +1,11 @@
 /**
 /**
  * A peer who can initiate connections with other peers.
  * A peer who can initiate connections with other peers.
  */
  */
-function Peer(options) {
+function Peer(id, options) {
+  if (id.constructor == Object) {
+    options = id;
+    id = undefined;
+  }
   if (!(this instanceof Peer)) return new Peer(options);
   if (!(this instanceof Peer)) return new Peer(options);
   EventEmitter.call(this);
   EventEmitter.call(this);
 
 
@@ -17,14 +21,14 @@ function Peer(options) {
   this._server = options.host + ':' + options.port;
   this._server = options.host + ':' + options.port;
 
 
   // Ensure alphanumeric_-
   // Ensure alphanumeric_-
-  if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id)) {
+  if (id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id)) {
     throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
     throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
   }
   }
   if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
   if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
     throw new Error('API key can only contain alphanumerics, "_", and "-".');
     throw new Error('API key can only contain alphanumerics, "_", and "-".');
   }
   }
   
   
-  this.id = options.id;
+  this.id = id;
   // Not used unless using cloud server.
   // Not used unless using cloud server.
   this._key = options.key;
   this._key = options.key;