mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2025-01-03 14:17:29 +00:00
This is a follow-up of https://github.com/nativefier/nativefier/pull/1162#discussion_r623766247
PR #1162 introduced a new `generateRandomSuffix` helper function,
used it for its needs (avoiding collisions of injected js/css).
But it also replaced existing appname normalizing logic with it,
introducing randomness in a place that used to be deterministic.
As a result, starting with dd6e15fb5
/ v43.1.0, re-creating an app would cause
the app to use a different appName, thus a different appData folder, thus
losing user data including cookies.
This PR leaves the `--inject` fixes of #1176, but reverts the appName logic
to the pre-#1176 code.
This commit is contained in:
parent
beae6e872b
commit
331fe8db39
11
src/build/prepareElectronApp.test.ts
Normal file
11
src/build/prepareElectronApp.test.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { normalizeAppName } from './prepareElectronApp';
|
||||||
|
|
||||||
|
describe('normalizeAppName', () => {
|
||||||
|
test('it is stable', () => {
|
||||||
|
// Non-determinism / unstability would cause using a different appName
|
||||||
|
// at each app regen, thus a different appData folder, which would cause
|
||||||
|
// losing user state, including login state through cookies.
|
||||||
|
const normalizedTrello = normalizeAppName('Trello', 'https://trello.com');
|
||||||
|
expect(normalizedTrello).toBe('trello-nativefier-679e8e');
|
||||||
|
});
|
||||||
|
});
|
@ -1,3 +1,4 @@
|
|||||||
|
import * as crypto from 'crypto';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { promisify } from 'util';
|
import { promisify } from 'util';
|
||||||
@ -127,9 +128,16 @@ async function maybeCopyScripts(srcs: string[], dest: string): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeAppName(appName: string): string {
|
/**
|
||||||
// use a simple random string to prevent collisions
|
* Use a basic 6-character hash to prevent collisions. The hash is deterministic url & name,
|
||||||
const postFixHash = generateRandomSuffix();
|
* so that an upgrade (same URL) of an app keeps using the same appData folder.
|
||||||
|
* Warning! Changing this normalizing & hashing will change the way appNames are generated,
|
||||||
|
* changing appData folder, and users will get logged out of their apps after an upgrade.
|
||||||
|
*/
|
||||||
|
export function normalizeAppName(appName: string, url: string): string {
|
||||||
|
const hash = crypto.createHash('md5');
|
||||||
|
hash.update(url);
|
||||||
|
const postFixHash = hash.digest('hex').substring(0, 6);
|
||||||
const normalized = appName
|
const normalized = appName
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.replace(/[,:.]/g, '')
|
.replace(/[,:.]/g, '')
|
||||||
@ -137,10 +145,14 @@ function normalizeAppName(appName: string): string {
|
|||||||
return `${normalized}-nativefier-${postFixHash}`;
|
return `${normalized}-nativefier-${postFixHash}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeAppPackageJsonName(appPath: string, name: string): void {
|
function changeAppPackageJsonName(
|
||||||
|
appPath: string,
|
||||||
|
name: string,
|
||||||
|
url: string,
|
||||||
|
): void {
|
||||||
const packageJsonPath = path.join(appPath, '/package.json');
|
const packageJsonPath = path.join(appPath, '/package.json');
|
||||||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
|
||||||
const normalizedAppName = normalizeAppName(name);
|
const normalizedAppName = normalizeAppName(name, url);
|
||||||
packageJson.name = normalizedAppName;
|
packageJson.name = normalizedAppName;
|
||||||
log.debug(`Updating ${packageJsonPath} 'name' field to ${normalizedAppName}`);
|
log.debug(`Updating ${packageJsonPath} 'name' field to ${normalizedAppName}`);
|
||||||
|
|
||||||
@ -184,5 +196,9 @@ export async function prepareElectronApp(
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error('Error copying injection files.', err);
|
log.error('Error copying injection files.', err);
|
||||||
}
|
}
|
||||||
changeAppPackageJsonName(dest, options.packager.name);
|
changeAppPackageJsonName(
|
||||||
|
dest,
|
||||||
|
options.packager.name,
|
||||||
|
options.packager.targetUrl,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user