Skip to content

Commit

Permalink
Regression: Fix app storage migration (#23286)
Browse files Browse the repository at this point in the history
  • Loading branch information
thassiov authored Sep 27, 2021
1 parent c595a3d commit b72a4be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@storybook/addon-postcss": "^2.0.0",
"@storybook/addons": "^6.3.6",
"@storybook/react": "^6.3.8",
"@types/adm-zip": "^0.4.34",
"@types/agenda": "^2.0.9",
"@types/bad-words": "^3.0.1",
"@types/bcrypt": "^5.0.0",
Expand Down
30 changes: 29 additions & 1 deletion server/startup/migrations/v238.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import AdmZip from 'adm-zip';
import { AppManager } from '@rocket.chat/apps-engine/server/AppManager';

import { addMigration } from '../../lib/migrations';
import { Apps } from '../../../app/apps/server';

function isPreCompilerRemoval(app: any): boolean {
const fileNames = Object.keys(app.compiled);
return fileNames.some((file) => file.endsWith('$ts'));
}

function repackageAppZip(app: any): Buffer {
const zip = new AdmZip();

const sourceFiles: string[] = [];

Object.entries(app.compiled).forEach(([key, value]) => {
const actualFileName = key.endsWith('$ts') ? key.replace(/\$ts$/, '.js') : key;
sourceFiles.push(actualFileName);
zip.addFile(actualFileName, Buffer.from(value as string, 'utf8'));
});

const zipToRead = new AdmZip(Buffer.from(app.zip, 'base64'));

zipToRead.getEntries().forEach((entry: any) => {
if (!sourceFiles.includes(entry.entryName)) {
zip.addFile(entry.entryName, entry.getData());
}
});

return zip.toBuffer();
}

addMigration({
version: 238,
up() {
Expand All @@ -11,7 +39,7 @@ addMigration({
const apps = Apps._model.find().fetch();

for (const app of apps) {
const zipFile = Buffer.from(app.zip, 'base64');
const zipFile = isPreCompilerRemoval(app) ? repackageAppZip(app) : Buffer.from(app.zip, 'base64');
Promise.await((Apps._manager as AppManager).update(zipFile, app.permissionsGranted, { loadApp: false }));
Promise.await(Apps._model.update({ id: app.id }, { $unset: { zip: 1, compiled: 1 } }));
}
Expand Down

0 comments on commit b72a4be

Please sign in to comment.