1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 04:46:10 +01:00
metamask-extension/app/scripts/lib/migrator/index.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events')
class Migrator extends EventEmitter {
constructor (opts = {}) {
super()
2017-04-27 06:05:45 +02:00
const migrations = opts.migrations || []
// sort migrations by version
this.migrations = migrations.sort((a, b) => a.version - b.version)
// grab migration with highest version
2017-04-27 06:05:45 +02:00
const lastMigration = this.migrations.slice(-1)[0]
// use specified defaultVersion or highest migration version
this.defaultVersion = opts.defaultVersion || (lastMigration && lastMigration.version) || 0
}
// run all pending migrations on meta in place
async migrateData (versionedData = this.generateInitialState()) {
// get all migrations that have not yet been run
const pendingMigrations = this.migrations.filter(migrationIsPending)
// perform each migration
2017-05-28 20:18:07 +02:00
for (const index in pendingMigrations) {
const migration = pendingMigrations[index]
try {
// attempt migration and validate
const migratedData = await migration.migrate(versionedData)
if (!migratedData.data) throw new Error('Migrator - migration returned empty data')
if (migratedData.version !== undefined && migratedData.meta.version !== migration.version) throw new Error('Migrator - Migration did not update version number correctly')
// accept the migration as good
versionedData = migratedData
} catch (err) {
// rewrite error message to add context without clobbering stack
const originalErrorMessage = err.message
err.message = `MetaMask Migration Error #${migration.version}: ${originalErrorMessage}`
console.warn(err.stack)
// emit error instead of throw so as to not break the run (gracefully fail)
this.emit('error', err)
// stop migrating and use state as is
return versionedData
}
}
return versionedData
// migration is "pending" if it has a higher
// version number than currentVersion
2017-04-27 06:05:45 +02:00
function migrationIsPending (migration) {
return migration.version > versionedData.meta.version
}
}
generateInitialState (initState) {
return {
meta: {
version: this.defaultVersion,
},
data: initState,
}
}
}
module.exports = Migrator