Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): carry plugin state into the staged tree on update

Yudhi Armyndharisa1a9c35c3b9f
diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.ts
index 59eded9ba..25cb114d3 100644
--- a/src/modules/plugins/plugins.service.spec.ts
+++ b/src/modules/plugins/plugins.service.spec.ts
@@ -161,6 +161,25 @@ describe('PluginsService — install / uninstall (real loader + disk)', () => {
}
});
+ it('has the plugin state in the live directory the moment the update swaps it in', async () => {
+ // Boot recovery drops the backup once a live directory exists, so state that is copied after the swap
+ // is lost by a crash between the two. Observe the live directory right after the staging rename.
+ service.install({ buffer: pkg({ version: '1.0.0' }) });
+ await pluginStorage.createPluginStorage('svc-plg').set('cursor', { lastId: 'msg-42' });
+ const live = path.join(pluginsDir, 'svc-plg');
+ const realRename = fs.renameSync.bind(fs);
+ let atSwap: string[] | undefined;
+ const rename = jest.spyOn(fs, 'renameSync').mockImplementation((from, to) => {
+ realRename(from, to);
+ if (to === live && String(from).endsWith('.svc-plg.new')) atSwap = fs.readdirSync(live);
+ });
+
+ await service.updatePackage('svc-plg', pkg({ version: '2.0.0' }));
+ rename.mockRestore();
+
+ expect(atSwap).toEqual(expect.arrayContaining([expect.stringMatching(/^key-.*\.json$/)]));
+ });
+
it('preserves a legacy (pre-encoding) ctx.storage file across an in-place package update', async () => {
service.install({ buffer: pkg({ version: '1.0.0' }) });
const storage = pluginStorage.createPluginStorage('svc-plg');
diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.ts
index 854232b7e..24b71cb28 100644
--- a/src/modules/plugins/plugins.service.ts
+++ b/src/modules/plugins/plugins.service.ts
@@ -596,6 +596,28 @@ export class PluginsService {
action: 'plugin_update_backup_restored',
});
}
+ // ctx.storage files share the package directory under shipped defaults. Carry them into the staged
+ // tree before the swap, so the live directory is complete the moment it takes the name: a crash after
+ // the swap must not leave state only in a backup that boot recovery drops once a live directory exists.
+ // Everything is carried unless the new package explicitly supplied that exact path: encoded key-* files
+ // and the legacy <key>.json files the storage service still reads (every top-level *.json except the
+ // package's manifest.json/package.json). Copy (rather than move) so the rollback below still has a
+ // complete original directory.
+ const packagePaths = new Set(entries.map(entry => entry.relPath));
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ if (
+ !entry.isFile() ||
+ !entry.name.endsWith('.json') ||
+ entry.name === 'manifest.json' ||
+ entry.name === 'package.json' ||
+ packagePaths.has(entry.name)
+ ) {
+ continue;
+ }
+ const stateFile = path.join(staging, entry.name);
+ fs.copyFileSync(path.join(dir, entry.name), stateFile);
+ fs.chmodSync(stateFile, 0o600);
+ }
fs.rmSync(backup, { recursive: true, force: true });
fs.renameSync(dir, backup);
fs.renameSync(staging, dir);
@@ -622,26 +644,6 @@ export class PluginsService {
});
try {
- // ctx.storage files share the package directory under shipped defaults. Restore state from the
- // backup unless the new package explicitly supplied that exact path: encoded key-* files and the
- // legacy <key>.json files the storage service still reads (every top-level *.json except the
- // package's manifest.json/package.json). Copy (rather than move) so the rollback below still has
- // a complete original directory.
- const packagePaths = new Set(entries.map(entry => entry.relPath));
- for (const entry of fs.readdirSync(backup, { withFileTypes: true })) {
- if (
- !entry.isFile() ||
- !entry.name.endsWith('.json') ||
- entry.name === 'manifest.json' ||
- entry.name === 'package.json' ||
- packagePaths.has(entry.name)
- ) {
- continue;
- }
- const stateFile = path.join(dir, entry.name);
- fs.copyFileSync(path.join(backup, entry.name), stateFile);
- fs.chmodSync(stateFile, 0o600);
- }
this.pluginLoader.loadPlugin(dir);
// Shutdown may have begun while the old version was being stopped: roll back below rather than
// keep the new version untested with its backup deleted.