Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Recovery from bug-723012
// Example how to access preferences that were stored between versions of 1.4 - 1.4.2
var prefs = require('addon-kit/simple-prefs').prefs;
var recovery = require('./recovery');
// If preferences have not being recovered yet and preference was saved in 1.4 ~ 1.4.2, then
// read it from recovery prefs and override current preference value. If nothing saved in
// 1.4 ~ 1.4.2 then take whatever was saved before (will automatically be current preference).
var myPref = (!prefs.recovered && typeof( !== 'undefined') ? = :;
// Same thing for all prefs....
// once recovery is done save that to prefs to avoid overwrites from old prefs each time.
prefs.recovered = true;
'use strict';
const { Loader } = require('@loader');
const options = require('@packaging');
let recovery = {};
// Deep copy all the options.
recovery.options = JSON.parse(JSON.stringify(options));
// Make `jetpackID` as it was in 1.4 - 1.4.2
recovery.options.jetpackID = options.jetpackID.split('@').shift();
// Create a new loader with a recovery options.
recovery.loader =;
recovery.require = Loader.require.bind(recovery.loader, module.path);
// Exports storage and pref as they would have been in 1.4 - 1.4.2 = recovery.require('addon-kit/simple-storage').storage;
exports.prefs = recovery.require('addon-kit/simple-prefs').prefs;
exports.passwords = recovery.require('addon-kit/passwords');
// Example how to access data stored between versions of 1.4 ~ 1.4.2
// storage contains any data saved into it before updating to 1.4.
var storage = require('addon-kit/simple-storage').storage;
var prefs = require('addon-kit/simple-prefs').prefs;
// all the data saved into storage between 1.4 ~ 1.4.2 will not appear in storage,
// but you could access it from ``.
var recovery = require('./recovery');
// lets perform recursive merge. We know that data in is newer
// than in actual `storage` so we could give a priority to the newer data.
// ## Note this merging algorithm is not perfect and you'll be better off writing
// your own that would suite your particular storage model better ##
function recover(current, recent) {
for (var key in recent) {
// if this entry was not present before just copy
if (!(key in current))
current[key] = recent[key];
// if data types are different most likely recent version is more valid.
else if (typeof(current[key]) !== typeof(recent[key]))
current[key] = recent[key];
// If it's not an object we should be probably override.
else if (typeof(recent[key]) !== 'object')
current[key] = recent[key];
// if object then merge those as well
recover(current[key], recent[key]);
if (!prefs.storageRecovered) {
prefs.storageRecovered = true;
CIAvash commented Feb 25, 2012

I think the preferences example is incorrect:

var myPref = !prefs.recovered && ? = :;

If type of a preference is boolean and its value is "false", the "else" part will work that is incorrect.

The code should be something like this:

var myPref = !prefs.recovered && (typeof != 'undefined') ? = :;

Gozala commented Feb 27, 2012

You're right @CIAvash thanks for pointing that out, I'll update a module to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.