Page MenuHomePhorge

D4024.1765062464.diff
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

D4024.1765062464.diff

diff --git a/keyserver/src/cron/backups.js b/keyserver/src/cron/backups.js
--- a/keyserver/src/cron/backups.js
+++ b/keyserver/src/cron/backups.js
@@ -10,32 +10,14 @@
import zlib from 'zlib';
import dbConfig from '../../secrets/db_config';
+import { importJSON } from '../utils/import-json';
const readdir = promisify(fs.readdir);
const lstat = promisify(fs.lstat);
const unlink = promisify(fs.unlink);
-let importedBackupConfig = undefined;
-async function importBackupConfig() {
- if (importedBackupConfig !== undefined) {
- return importedBackupConfig;
- }
- try {
- // $FlowFixMe
- const backupExports = await import('../../facts/backups');
- if (importedBackupConfig === undefined) {
- importedBackupConfig = backupExports.default;
- }
- } catch {
- if (importedBackupConfig === undefined) {
- importedBackupConfig = null;
- }
- }
- return importedBackupConfig;
-}
-
async function backupDB() {
- const backupConfig = await importBackupConfig();
+ const backupConfig = await importJSON('facts/backups');
if (!backupConfig || !backupConfig.enabled) {
return;
}
@@ -190,7 +172,7 @@
}
async function deleteOldestBackup() {
- const backupConfig = await importBackupConfig();
+ const backupConfig = await importJSON('facts/backups');
invariant(backupConfig, 'backupConfig should be non-null');
const files = await readdir(backupConfig.directory);
let oldestFile;
diff --git a/keyserver/src/cron/update-geoip-db.js b/keyserver/src/cron/update-geoip-db.js
--- a/keyserver/src/cron/update-geoip-db.js
+++ b/keyserver/src/cron/update-geoip-db.js
@@ -5,28 +5,10 @@
import geoip from 'geoip-lite';
import { handleAsyncPromise } from '../responders/handlers';
-
-let cachedGeoipLicense = undefined;
-async function getGeoipLicense() {
- if (cachedGeoipLicense !== undefined) {
- return cachedGeoipLicense;
- }
- try {
- // $FlowFixMe
- const geoipLicenseImport = await import('../../secrets/geoip_license');
- if (cachedGeoipLicense === undefined) {
- cachedGeoipLicense = geoipLicenseImport.default;
- }
- } catch {
- if (cachedGeoipLicense === undefined) {
- cachedGeoipLicense = null;
- }
- }
- return cachedGeoipLicense;
-}
+import { importJSON } from '../utils/import-json';
async function updateGeoipDB(): Promise<void> {
- const geoipLicense = await getGeoipLicense();
+ const geoipLicense = await importJSON('secrets/geoip_license');
if (!geoipLicense) {
console.log('no keyserver/secrets/geoip_license.json so skipping update');
return;
diff --git a/keyserver/src/push/providers.js b/keyserver/src/push/providers.js
--- a/keyserver/src/push/providers.js
+++ b/keyserver/src/push/providers.js
@@ -4,6 +4,9 @@
import type { Provider as APNProvider } from '@parse/node-apn';
import fcmAdmin from 'firebase-admin';
import type { FirebaseApp } from 'firebase-admin';
+import invariant from 'invariant';
+
+import { importJSON } from '../utils/import-json';
type APNPushProfile = 'apn_config' | 'comm_apn_config';
function getAPNPushProfileForCodeVersion(codeVersion: ?number): APNPushProfile {
@@ -22,10 +25,10 @@
return provider;
}
try {
- // $FlowFixMe
- const apnConfig = await import(`../../secrets/${profile}`);
+ const apnConfig = await importJSON(`secrets/${profile}`);
+ invariant(apnConfig, `APN config missing for ${profile}`);
if (!cachedAPNProviders.has(profile)) {
- cachedAPNProviders.set(profile, new apn.Provider(apnConfig.default));
+ cachedAPNProviders.set(profile, new apn.Provider(apnConfig));
}
} catch {
if (!cachedAPNProviders.has(profile)) {
@@ -42,14 +45,14 @@
return provider;
}
try {
- // $FlowFixMe
- const fcmConfig = await import(`../../secrets/${profile}`);
+ const fcmConfig = await importJSON(`secrets/${profile}`);
+ invariant(fcmConfig, `FCM config missed for ${profile}`);
if (!cachedFCMProviders.has(profile)) {
cachedFCMProviders.set(
profile,
fcmAdmin.initializeApp(
{
- credential: fcmAdmin.credential.cert(fcmConfig.default),
+ credential: fcmAdmin.credential.cert(fcmConfig),
},
profile,
),
diff --git a/keyserver/src/utils/import-json.js b/keyserver/src/utils/import-json.js
new file mode 100644
--- /dev/null
+++ b/keyserver/src/utils/import-json.js
@@ -0,0 +1,23 @@
+// @flow
+
+const cachedJSON = new Map();
+async function importJSON<T>(path: string): Promise<?T> {
+ const cached = cachedJSON.get(path);
+ if (cached !== undefined) {
+ return cached;
+ }
+ try {
+ // $FlowFixMe
+ const importedJSON = await import(`../../${path}`);
+ if (!cachedJSON.has(path)) {
+ cachedJSON.set(path, importedJSON.default);
+ }
+ } catch {
+ if (!cachedJSON.has(path)) {
+ cachedJSON.set(path, null);
+ }
+ }
+ return cachedJSON.get(path);
+}
+
+export { importJSON };
diff --git a/keyserver/src/utils/urls.js b/keyserver/src/utils/urls.js
--- a/keyserver/src/utils/urls.js
+++ b/keyserver/src/utils/urls.js
@@ -4,6 +4,8 @@
import { values } from 'lib/utils/objects';
+import { importJSON } from './import-json';
+
export type AppURLFacts = {
+baseDomain: string,
+basePath: string,
@@ -20,22 +22,13 @@
const cachedURLFacts = new Map();
async function fetchURLFacts(site: Site): Promise<?AppURLFacts> {
- const cached = cachedURLFacts.get(site);
- if (cached !== undefined) {
- return cached;
- }
- try {
- // $FlowFixMe
- const urlFacts = await import(`../../facts/${site}_url`);
- if (!cachedURLFacts.has(site)) {
- cachedURLFacts.set(site, urlFacts.default);
- }
- } catch {
- if (!cachedURLFacts.has(site)) {
- cachedURLFacts.set(site, null);
- }
+ const existing = cachedURLFacts.get(site);
+ if (existing !== undefined) {
+ return existing;
}
- return cachedURLFacts.get(site);
+ const urlFacts: ?AppURLFacts = await importJSON(`facts/${site}_url`);
+ cachedURLFacts.set(site, urlFacts);
+ return urlFacts;
}
async function prefetchAllURLFacts() {

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 6, 11:07 PM (9 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5841343
Default Alt Text
D4024.1765062464.diff (5 KB)

Event Timeline