feat: ELECTRON-1204 - add support for ttl (#668)

* ELECTRON-1204: implement ttl logic

* ELECTRON-1204: rebase with master

* ELECTRON-1204: remove redundant comma
This commit is contained in:
Vishwas Shashidhar
2019-06-03 14:19:05 +05:30
committed by GitHub
parent fe0cacad9e
commit 7a94ad31f8
11 changed files with 141 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
const fs = require('fs');
const gulp = require('gulp');
const less = require('gulp-less');
const sourcemaps = require('gulp-sourcemaps');
@@ -11,6 +12,10 @@ gulp.task('clean', function() {
return del('lib');
});
/**
* Compile all typescript files
* and copy to the destination
*/
gulp.task('compile', function() {
return gulp.src(['src/**/*.ts', 'src/**/*.tsx'])
.pipe(tsc({ project: './tsconfig.json' }))
@@ -25,6 +30,9 @@ gulp.task('less', function () {
.pipe(gulp.dest(path.join(__dirname, 'lib/src')));
});
/**
* Copy all assets to JS codebase
*/
gulp.task('copy', function () {
return gulp.src([
'./src/renderer/assets/*',
@@ -35,4 +43,33 @@ gulp.task('copy', function () {
}).pipe(gulp.dest('lib/src'))
});
/**
* Set expiry time for test builds
*/
gulp.task('setExpiry', function (done) {
// Set expiry of 15 days for test builds we create from CI
const expiryDays = 15;
const milliseconds = 24*60*60*1000;
const expiryTime = new Date().getTime() + (expiryDays * milliseconds);
const ttlHandlerFile = path.join(__dirname, 'src/app/ttl-handler.ts');
fs.readFile(ttlHandlerFile, 'utf8', function (err,data) {
if (err) {
console.log(err);
return done(err);
}
// Do a simple search and replace in the `ttl-handler.ts` file
const replacementString = `const ttlExpiryTime = ${expiryTime}`;
var result = data.replace(/const ttlExpiryTime = -1/g, replacementString);
fs.writeFile(ttlHandlerFile, result, 'utf8', function (err) {
if (err) {
return done(err);
}
done();
});
});
});
gulp.task('build', gulp.series('clean', 'compile', 'less', 'copy'));

View File

@@ -18,6 +18,7 @@
"packed-mac": "npm run unpacked-mac && packagesbuild -v installer/mac/symphony-mac-packager.pkgproj",
"prebuild": "npm run compile && npm run rebuild && npm run browserify-preload",
"rebuild": "electron-rebuild -f",
"setexpiry": "gulp setExpiry",
"start": "npm run compile && npm run browserify-preload && cross-env ELECTRON_DEV=true electron .",
"test": "npm run lint && cross-env ELECTRON_QA=true jest --config jest.config.json --runInBand",
"unpacked-mac": "npm run prebuild && npm run test && build --mac --dir",

31
spec/ttl-handler-spec.ts Normal file
View File

@@ -0,0 +1,31 @@
import * as ttlHandler from '../src/app/ttl-handler';
describe('ttl handler', () => {
beforeEach(() => {
jest.resetModules();
});
it('should return -1 for getExpiryTime', () => {
expect(ttlHandler.getExpiryTime()).toBeDefined();
});
it('should return true if build is expired', () => {
const expiryMock = jest.spyOn(ttlHandler, 'getExpiryTime');
expiryMock.mockImplementation(() => Date.now() - 10 * 24 * 60);
expect(ttlHandler.checkIfBuildExpired()).toBeTruthy();
});
it('should return false if build is valid', () => {
const expiryMock = jest.spyOn(ttlHandler, 'getExpiryTime');
expiryMock.mockImplementation(() => Date.now() + 10 * 24 * 60);
expect(ttlHandler.checkIfBuildExpired()).toBeFalsy();
});
it('should return false if ttl is not applicable', () => {
const expiryMock = jest.spyOn(ttlHandler, 'getExpiryTime');
expiryMock.mockImplementation(() => -1);
expect(ttlHandler.checkIfBuildExpired()).toBeFalsy();
});
});

28
src/app/ttl-handler.ts Normal file
View File

@@ -0,0 +1,28 @@
import { logger } from '../common/logger';
const ttlExpiryTime = -1;
export const getExpiryTime = (): number => {
return ttlExpiryTime;
};
/**
* Checks to see if the build is expired against a TTL expiry time
*/
export const checkIfBuildExpired = (): boolean => {
logger.info(`ttl-handler: Checking for build expiry`);
const expiryTime = getExpiryTime();
if (expiryTime <= -1) {
logger.info(`ttl-handler: Expiry not applicable for this build`);
return false;
}
const currentDate: Date = new Date();
const expiryDate: Date = new Date(expiryTime);
logger.info(`ttl-handler: Current Time: ${currentDate.getTime()}, Expiry Time: ${expiryDate.getTime()}`);
const buildExpired = currentDate.getTime() > expiryDate.getTime();
return buildExpired;
};

View File

@@ -15,6 +15,7 @@ import { AppMenu } from './app-menu';
import { handleChildWindow } from './child-window-handler';
import { config, IConfig } from './config-handler';
import { SpellChecker } from './spell-check-handler';
import { checkIfBuildExpired } from './ttl-handler';
import { monitorWindowActions } from './window-actions';
import {
createComponentWindow,
@@ -231,6 +232,8 @@ export class WindowHandler {
// loads the main window with url from config/cmd line
this.mainWindow.loadURL(this.url);
// check for build expiry in case of test builds
this.checkExpiry(this.mainWindow);
this.mainWindow.webContents.on('did-finish-load', async () => {
logger.info(`window-handler: main window web contents finished loading!`);
// early exit if the window has already been destroyed
@@ -794,6 +797,35 @@ export class WindowHandler {
winKey: getGuid(),
};
}
/**
* Check if build is expired and show an error message
* @param browserWindow Focused window instance
*/
private checkExpiry(browserWindow: BrowserWindow) {
logger.info(`window handler: calling ttl handler to check for build expiry!`);
const buildExpired = checkIfBuildExpired();
if (!buildExpired) {
logger.info(`window handler: build not expired, proceeding further!`);
return;
}
logger.info(`window handler: build expired, will inform the user and quit the app!`);
const response = (resp: number) => {
if (resp === 0) {
electron.app.exit();
}
};
const options = {
type: 'error',
title: i18n.t('Build expired')(),
message: i18n.t('Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.')(),
buttons: [ i18n.t('Quit')()],
cancelId: 0,
};
electron.dialog.showMessageBox(browserWindow, options, response);
}
}
const windowHandler = new WindowHandler();

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "Bring All to Front",
"Bring to Front on Notifications": "Bring to Front on Notifications",
"Build expired": "Build expired",
"Certificate Error": "Certificate Error",
"Close": "Close",
"Cancel": "Cancel",
@@ -155,6 +156,7 @@
"Press ": "Press "
},
"Sorry, you are not allowed to access this website": "Sorry, you are not allowed to access this website",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.",
"Speech": "Speech",
"Start Speaking": "Start Speaking",
"Stop Speaking": "Stop Speaking",

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "Bring All to Front",
"Bring to Front on Notifications": "Bring to Front on Notifications",
"Build expired": "Build expired",
"Certificate Error": "Certificate Error",
"Close": "Close",
"Cancel": "Cancel",
@@ -155,6 +156,7 @@
"Press ": "Press "
},
"Sorry, you are not allowed to access this website": "Sorry, you are not allowed to access this website",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.",
"Speech": "Speech",
"Start Speaking": "Start Speaking",
"Stop Speaking": "Stop Speaking",

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "Amener tous au front",
"Bring to Front on Notifications": "Mettre les notifications au premier plan",
"Build expired": "Construit expiré",
"Certificate Error": "Erreur de certificat",
"Close": "Fermer",
"Cancel": "Annuler",
@@ -155,6 +156,7 @@
"Press ": "Appuyer sur "
},
"Sorry, you are not allowed to access this website": "Désolé, vous n'êtes pas autorisé à accéder à ce site.",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "Désolé, ceci est une version de test et elle a expiré. Veuillez contacter votre administrateur pour obtenir une version de production.",
"Speech": "Dictée vocale",
"Start Speaking": "Commencer à dicter",
"Stop Speaking": "Arreter de dicter",

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "Amener tous au front",
"Bring to Front on Notifications": "Mettre les notifications au premier plan",
"Build expired": "Construit expiré",
"Certificate Error": "Erreur de certificat",
"Close": "Fermer",
"Cancel": "Annuler",
@@ -155,6 +156,7 @@
"Press ": "Appuyer sur "
},
"Sorry, you are not allowed to access this website": "Désolé, vous n'êtes pas autorisé à accéder à ce site.",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "Désolé, ceci est une version de test et elle a expiré. Veuillez contacter votre administrateur pour obtenir une version de production.",
"Speech": "Dictée vocale",
"Start Speaking": "Commencer à dicter",
"Stop Speaking": "Arreter de dicter",

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "すべて前面に表示",
"Bring to Front on Notifications": "通知時に前面に表示",
"Build expired": "期限切れのビルド",
"Certificate Error": "証明書のエラー",
"Close": "閉じる",
"Cancel": "キャンセル",
@@ -155,6 +156,7 @@
"Press ": "全画面表示を終了するには "
},
"Sorry, you are not allowed to access this website": "申し訳ありませんが、このウェブサイトへのアクセスは許可されていません",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "申し訳ありませんが、これはテストビルドであり、期限切れです。プロダクションビルドを入手するには、管理者に連絡してください。",
"Speech": "スピーチ",
"Start Speaking": "スピーチを開始",
"Stop Speaking": "スピーチを終了",

View File

@@ -21,6 +21,7 @@
},
"Bring All to Front": "すべて前面に表示",
"Bring to Front on Notifications": "通知時に前面に表示",
"Build expired": "期限切れのビルド",
"Certificate Error": "証明書のエラー",
"Close": "閉じる",
"Cancel": "キャンセル",
@@ -155,6 +156,7 @@
"Press ": "全画面表示を終了するには "
},
"Sorry, you are not allowed to access this website": "申し訳ありませんが、このウェブサイトへのアクセスは許可されていません",
"Sorry, this is a test build and it has expired. Please contact your administrator to get a production build.": "申し訳ありませんが、これはテストビルドであり、期限切れです。プロダクションビルドを入手するには、管理者に連絡してください。",
"Speech": "スピーチ",
"Start Speaking": "スピーチを開始",
"Stop Speaking": "スピーチを終了",