Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): keep legacy ctx.storage files across a package update

Yudhi Armyndharis480a39abfc29
diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.ts
index ca51128db..fdc9c072c 100644
--- a/src/modules/plugins/plugins.service.spec.ts
+++ b/src/modules/plugins/plugins.service.spec.ts
@@ -122,6 +122,21 @@ describe('PluginsService — install / uninstall (real loader + disk)', () => {
}
});
+ 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');
+ fs.writeFileSync(path.join(pluginsDir, 'svc-plg', 'cursor.json'), JSON.stringify({ lastId: 'msg-7' }));
+
+ await service.updatePackage('svc-plg', pkg({ version: '2.0.0' }));
+
+ expect(await storage.get('cursor')).toEqual({ lastId: 'msg-7' });
+ // The new package's own manifest is never overwritten by the backed-up one.
+ const shipped = JSON.parse(fs.readFileSync(path.join(pluginsDir, 'svc-plg', 'manifest.json'), 'utf8')) as {
+ version: string;
+ };
+ expect(shipped.version).toBe('2.0.0');
+ });
+
it('updatePackage rejects a package whose id does not match', async () => {
service.install({ buffer: pkg() });
await expect(service.updatePackage('svc-plg', pkg({ id: 'other-plg' }))).rejects.toThrow(/does not match/i);
diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.ts
index 1acafa983..db505b6ba 100644
--- a/src/modules/plugins/plugins.service.ts
+++ b/src/modules/plugins/plugins.service.ts
@@ -574,12 +574,20 @@ export class PluginsService {
});
try {
- // ctx.storage files share the package directory under shipped defaults. Restore service-owned
- // state from the backup unless the new package explicitly supplied that exact path. Copy (rather
- // than move) so the rollback below still has a complete original directory.
+ // 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() || !/^key-[A-Za-z0-9_-]+\.json$/.test(entry.name) || packagePaths.has(entry.name)) {
+ 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);