
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@davestewart/extension-migrations
Advanced tools
Simple migration tool for browser extensions
Extension Migrations is a simple library designed to run migrations in browser extensions when the version is changed.
Use cases might be:
The library supports two installation methods.
The file in migrations.js is standalone JavaScript with JSDoc types.
If your project is small, and missing a compile step, just copy the file or the code and import.
Install from NPM like any other package:
npm i @davestewart/extension-migrations
Migrations should be modelled as a hash of version: migration pairs, each with up and down methods:
const migrations = {
'0.1': {
up () {
// one time setup
}
},
'0.5': {
up () {
// do some action
},
down () {
// undo some action
},
}
}
The up methods are called when a higher version is installed, and down migrations when a lower version is installed.
Note that migration keys must exactly match the version strings in your extension's manifest, i.e. 2.0 is different from 2.0.0.
To run a migration, import and call the migrate() function:
import { migrate } from '@davestewart/extension-migrations'
const result = await migrate({ ... }, fromVersion, toVersion)
You should run the migration in the onInstalled listener:
chrome.runtime.onInstalled.addListener(async (event) => {
// variables
const { version } = chrome.runtime.getManifest()
const { previousVersion } = event
// run migrations
const { type, versions } = await migrate(migrations, previousVersion, version)
// log
console.log('migration complete:', type, versions)
})
Note that:
The function returns the following data:
{
versions: ['1.0', '1.1', ...],
type: 'all',
}
The versions property will be an array of the versions that were run.
The type property will be one of:
skip: there were no migrations to runall: the extension was installednone: the extension was reloadedup: the extension was upgradeddown: the extension was downgraded (likely only in development)To handle errors, wrap the migration in a try/catch:
// migrations
const migrations = {
'2.0': {
up (version) {
errVersion = version
throw new Error('Could not create database')
},
down () {}
},
}
// track errors
let errVersion
// run migrations
try {
await migrate(migrations, previousVersion, version)
}
catch (err) {
console.log(`${err.message} for version ${errVersion}`)
}
FAQs
Simple migration tool for browser extensions
We found that @davestewart/extension-migrations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.