瀏覽代碼

add id to constructor of peer

ericz 12 年之前
父節點
當前提交
f71557168a
共有 1 個文件被更改,包括 7 次插入3 次删除
  1. 7 3
      lib/peer.js

+ 7 - 3
lib/peer.js

@@ -1,7 +1,11 @@
 /**
  * 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);
   EventEmitter.call(this);
 
@@ -17,14 +21,14 @@ function Peer(options) {
   this._server = options.host + ':' + options.port;
 
   // 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 "-".');
   }
   if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
     throw new Error('API key can only contain alphanumerics, "_", and "-".');
   }
   
-  this.id = options.id;
+  this.id = id;
   // Not used unless using cloud server.
   this._key = options.key;