Services/Sync/Snippets: Difference between revisions

From MozillaWiki
< Services‎ | Sync
Jump to navigation Jump to search
No edit summary
No edit summary
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
For want of a better place, here are some handy snippets for investigating Sync issues.
Content has been moved to https://developer.mozilla.org/en-US/docs/Firefox_Sync/Code_snippets
 
For most of these your Sync credentials and keys need to have been loaded before running (otherwise you might get errors about there being no key for a collection). Run a sync first.
 
=== Partially corrupt a server ===
 
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://services-sync/resource.js");
function deletePath(path) {
  let resource = new Resource(Weave.Service.storageURL + path);
  resource.setHeader("X-Confirm-Delete", "1");
  return res.delete();
}
// Delete meta/global:
deletePath("meta/global");
// Delete keys:
deletePath("crypto/keys");
 
=== Generate new keys ===
 
// Clients always wipe the server when they generate new keys.
Components.utils.import("resource://services-sync/main.js");
Weave.Service._freshStart();
// If you want to do it without wiping the server (which will cause corruption!):
Weave.Service.generateNewSymmetricKeys();
 
 
=== Print out a list of large bookmark records ===
 
Change '1000' as appropriate.
 
  Components.utils.import("resource://services-sync/engines.js");
  Components.utils.import("resource://services-sync/engines/bookmarks.js");
  let bme = Engines.get("bookmarks");
  let ids = Object.keys(bme._store.getAllIDs());
  for each (let id in ids) {
    let record = bme._store.createRecord(id, "bookmarks");
    let len = record.toString().length;
    if (len > 1000) {
      console.log("ID: " + id + ", len = " + len + ", " + record.title);
    }
  }
 
=== Print an alphabetically sorted list of members of a collection. ===
 
  Components.utils.import("resource://services-sync/main.js");
  Components.utils.import("resource://services-sync/resource.js");
  let ids = JSON.parse(new Resource(Weave.Service.storageURL + "bookmarks").get());
  for each (let id in ids.sort()) {
    console.log("  " + id);
  }
 
=== Get a count of the number of members of a collection on the server. ===
 
let collection = "passwords";
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://services-sync/resource.js");
JSON.parse(new Resource(Weave.Service.storageURL + collection).get()).length;
 
=== Dump the cleartext of each record in a collection to the console. ===
 
let collection = "forms";
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://services-sync/record.js");
let recordType = Weave.Engines.get(collection)._recordObj;
let coll = new Collection(Weave.Service.storageURL + collection, recordType);
coll.full = true;
coll.recordHandler = function(item) {
  item.collection = collection;
  item.decrypt();
  console.log(item.cleartext);
};
coll.get();
 
=== Get a log from XUL Fennec ===
 
* View about:sync-log.
* Long-tap the log in question. Choose "Save Link".
* Open a mail client. Click Attach.
* Choose "File Manager" as the handler.
* Navigate to "Downloads", pick correct file.
 
=== Watch live Sync logs ===
 
* Set services.sync.log.appender.console to Trace.
 
=== Bump meta/global's modified time ===
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://services-sync/resource.js");
let r = new Resource(Weave.Service.storageURL + "meta/global");
let g = r.get();
r.put(g);
 
=== Delete and restore a record ===
 
Components.utils.import("resource://services-sync/main.js");
Components.utils.import("resource://services-sync/resource.js");
Components.utils.import("resource://services-sync/record.js");
// For example:
let id = "iASOkUOZpIxZ"
let collection = "bookmarks";
let resource = new Resource(Weave.Service.storageURL + collection + "/" + id);
let del = new CryptoWrapper(collection, id);
del.deleted = true;
del.encrypt();
// Save the old value.
let old = resource.get();
// Delete.
resource.put(del);
// Restore the old value.
resource.put(old);

Latest revision as of 05:33, 13 January 2014