GitHub PullRequestReviewCommentEvent at 2014-06-03 20:53:24
Repository: makedrive
Actor: gideonthomas
Payload: {"comment":{"url":"https://api.github.com/repos/humphd/makedrive/pulls/comments/13358436","id":13358436,"diff_hunk":"@@ -0,0 +1,164 @@\n+/**\n+ * Sync() module for creating and managing syncs\n+ * between client and server\n+ */\n+\n+// Deps\n+var Filer = require( 'filer' ),\n+    env = require( '../environment' ),\n+    Provider = require( env.get( 'FILER_PROVIDER' ) || 'filer-fs' );\n+\n+// Expose internals\n+var rsync = require( '../lib/rsync' );\n+\n+// Static private variables\n+var syncTable = {},\n+    // TODO: Examine these. Are they what we need?\n+    rsyncOptions = {\n+      size: 5,\n+      links: false,\n+      recursive: true\n+    };\n+\n+/**\n+ * Helper functions\n+ */\n+function getUserBySyncId( syncId ) {\n+  for ( var u in syncTable ) {\n+    if ( syncTable[ u ].syncId == syncId ) {\n+      return u;\n+    }\n+  }\n+  return null;\n+}\n+\n+function checkUser( username ) {\n+  return syncTable[ username ] ? true : false;\n+}\n+\n+function isSyncSession( username, id ) {\n+  return checkUser( username ) && syncTable[ username ].syncId == id;\n+}\n+\n+function Sync( username, id ) {\n+  this.syncId = id;\n+  this.username = username;\n+\n+  syncTable[ username ] = {\n+    syncId: this.syncId\n+  };\n+}\n+\n+// Plug into this user's server-side filesystem,\n+// formally starting the sync process\n+Sync.prototype.start = function( username, callback ) {\n+  this.fs = new Filer.FileSystem({\n+    provider: new Provider({\n+      keyPrefix: this.username\n+    })\n+  });\n+\n+  // TODO: Decide what our root path will be (currently /projects)\n+  this.fs.mkdir('/projects', function(err, data) {\n+    if (err) {\n+      return callback( \"Error creating the user's root directory: \" + err );\n+    }\n+\n+    syncTable[username] = {\n+      syncId: this.id,\n+      fs: this.fs\n+    };\n+    callback( null, this.id );\n+  });\n+};\n+\n+// Store the sync's path & srcList values\n+Sync.prototype.hydrate = function( path, srcList, callback ){\n+  // TODO: Add path and srcList validation logic\n+\n+  // If invalid, return callback(\"Invalid path\");\n+  this.path = path;\n+\n+  // If invalid, return callback(\"Invalid srcList\");\n+  this.srcList = srcList;\n+\n+  callback();\n+};\n+\n+// Generate the checksums for the rsync source (client)\n+Sync.prototype.generateChecksums = function( callback ) {\n+  rsync.checksums(this.fs, this.path, this.sourceList, rsyncOptions, callback);\n+};\n+\n+// Apply the diffs from the source (client)\n+Sync.prototype.patch = function( diffs, callback ) {\n+  var that = this;\n+\n+  // Fail loudly if the server allows this method to be called\n+  // without passing diffs\n+  // TODO: Add diff validation logic\n+  if ( !diffs ) {\n+    throw( \"Diffs must be passed\" );\n+  }\n+\n+  this.diffs = diffs;\n+\n+  // TODO: Swap this out for Ali's solution to this problem\n+  // Parse JSON diffs to Uint8Array\n+  for (var i = 0; i < diffs.length; i++) {\n+    if(diffs[i].contents) {\n+      for (var j = 0; j < diffs[i].contents.length; j++) {\n+        for (var k = 0; k < diffs[i].contents[j].diff.length; k++) {\n+          if (diffs[i].contents[j].diff[k].data) {\n+            diffs[i].contents[j].diff[k].data = diffs[i].contents[j].diff[k].data;\n+            // Deal with special-cased flattened typed arrays in WebSQL (see put() below)\n+            if (diffs[i].contents[j].diff[k].data.__isUint8Array) {\n+              diffs[i].contents[j].diff[k].data = new Uint8Array(diffs[i].contents[j].diff[k].data.__array);\n+            }\n+          }\n+        }\n+      }\n+    } else {\n+      for (var k = 0; k < diffs[i].diff.length; k++) {\n+        if (diffs[i].diff[k].data) {\n+          diffs[i].diff[k].data = diffs[i].diff[k].data;\n+          // Deal with special-cased flattened typed arrays in WebSQL (see put() below)\n+          if (diffs[i].diff[k].data.__isUint8Array) {\n+            diffs[i].diff[k].data = new Uint8Array(diffs[i].diff[k].data.__array);\n+          }\n+        }\n+      }\n+    }\n+  }\n+\n+  rsync.patch( this.fs, this.path, this.diffs, rsyncOptions, function ( err, data ) {\n+    if ( err ) {\n+      return callback( err );\n+    }\n+\n+    callback();","path":"server/bin/sync.js","position":139,"original_position":139,"commit_id":"52c9422fc62a3abb5a2157eb9a34de79dca994fc","original_commit_id":"52c9422fc62a3abb5a2157eb9a34de79dca994fc","user":{"login":"gideonthomas","id":5816130,"avatar_url":"https://avatars.githubusercontent.com/u/5816130?","gravatar_id":"b258eac8f9bbbd1cc58948ed37ef4f55","url":"https://api.github.com/users/gideonthomas","html_url":"https://github.com/gideonthomas","followers_url":"https://api.github.com/users/gideonthomas/followers","following_url":"https://api.github.com/users/gideonthomas/following{/other_user}","gists_url":"https://api.github.com/users/gideonthomas/gists{/gist_id}","starred_url":"https://api.github.com/users/gideonthomas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gideonthomas/subscriptions","organizations_url":"https://api.github.com/users/gideonthomas/orgs","repos_url":"https://api.github.com/users/gideonthomas/repos","events_url":"https://api.github.com/users/gideonthomas/events{/privacy}","received_events_url":"https://api.github.com/users/gideonthomas/received_events","type":"User","site_admin":false},"body":"return callback();","created_at":"2014-06-03T20:53:24Z","updated_at":"2014-06-03T20:53:24Z","html_url":"https://github.com/humphd/makedrive/pull/9#discussion_r13358436","pull_request_url":"https://api.github.com/repos/humphd/makedrive/pulls/9","_links":{"self":{"href":"https://api.github.com/repos/humphd/makedrive/pulls/comments/13358436"},"html":{"href":"https://github.com/humphd/makedrive/pull/9#discussion_r13358436"},"pull_request":{"href":"https://api.github.com/repos/humphd/makedrive/pulls/9"}}}}