From ef40bbfef160342bc50a867f7aa00435f9d4ef04 Mon Sep 17 00:00:00 2001 From: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> Date: Fri, 10 Apr 2020 13:59:20 +0800 Subject: [PATCH] feat: updates import resolution Updates import resolution to use ES6-style imports and Typescript import/export helpers. Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> --- functions/src/config.example.ts | 10 ++++------ functions/src/firebaseFunctions.ts | 2 +- functions/src/index.ts | 2 +- functions/src/opentrace/getHandshakePin.ts | 7 ++----- functions/src/opentrace/getTempIDs.ts | 10 ++++------ functions/src/opentrace/getUploadToken.ts | 14 ++++++-------- functions/src/opentrace/processUploadedData.ts | 6 ++---- functions/src/opentrace/types/FunctionConfig.ts | 4 +--- functions/src/opentrace/types/HeartBeatEvent.ts | 4 +--- functions/src/opentrace/types/StreetPassRecord.ts | 4 +--- functions/src/opentrace/utils/Authenticator.ts | 4 ++-- functions/src/opentrace/utils/CustomEncrypter.ts | 6 ++---- functions/src/opentrace/utils/PinGenerator.ts | 2 +- functions/src/opentrace/utils/formatTimestamp.ts | 6 ++---- functions/src/opentrace/utils/getEncryptionKey.ts | 6 ++---- functions/test/index.test.ts | 2 +- functions/test/opentrace/config.test.ts | 8 ++++---- functions/tsconfig.json | 8 ++++---- 18 files changed, 41 insertions(+), 64 deletions(-) diff --git a/functions/src/config.example.ts b/functions/src/config.example.ts index c563d1e..7218958 100644 --- a/functions/src/config.example.ts +++ b/functions/src/config.example.ts @@ -1,8 +1,8 @@ -import FunctionConfig from "./opentrace/types/FunctionConfig"; -import Authenticator from "./opentrace/utils/Authenticator"; -import PinGenerator from "./opentrace/utils/PinGenerator"; +import {FunctionConfig} from "./opentrace/types/FunctionConfig"; +import {Authenticator} from "./opentrace/utils/Authenticator"; +import {PinGenerator} from "./opentrace/utils/PinGenerator"; -const config: FunctionConfig = { +export const config: FunctionConfig = { projectId: "", regions: [], utcOffset: 0, @@ -26,5 +26,3 @@ const config: FunctionConfig = { bucketForArchive: "archive-bucket", }, }; - -export default config; diff --git a/functions/src/firebaseFunctions.ts b/functions/src/firebaseFunctions.ts index 9f4db02..d06f538 100644 --- a/functions/src/firebaseFunctions.ts +++ b/functions/src/firebaseFunctions.ts @@ -1,4 +1,4 @@ -import * as functions from "firebase-functions"; +import functions from "firebase-functions"; import config from "./config"; diff --git a/functions/src/index.ts b/functions/src/index.ts index 9544355..fd3aabc 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,4 +1,4 @@ -import * as admin from "firebase-admin"; +import admin from "firebase-admin"; admin.initializeApp(); import * as firebaseFunctions from "./firebaseFunctions"; diff --git a/functions/src/opentrace/getHandshakePin.ts b/functions/src/opentrace/getHandshakePin.ts index 39a6c2c..ccee140 100644 --- a/functions/src/opentrace/getHandshakePin.ts +++ b/functions/src/opentrace/getHandshakePin.ts @@ -1,11 +1,10 @@ -import * as functions from "firebase-functions"; - +import functions from "firebase-functions"; import config from "../config"; /** * Get the handshake pin for a user. */ -const getHandshakePin = async (uid: string) => { +export const getHandshakePin = async (uid: string) => { console.log('getHandshakePin:', 'uid', uid); try { @@ -20,5 +19,3 @@ const getHandshakePin = async (uid: string) => { }; export const getUserHandshakePin = config.upload.pinGenerator.generatePin; - -export default getHandshakePin; diff --git a/functions/src/opentrace/getTempIDs.ts b/functions/src/opentrace/getTempIDs.ts index aefebef..d7679d6 100644 --- a/functions/src/opentrace/getTempIDs.ts +++ b/functions/src/opentrace/getTempIDs.ts @@ -1,8 +1,8 @@ -import * as moment from "moment"; +import moment from "moment"; import config from "../config"; -import CustomEncrypter from "./utils/CustomEncrypter"; -import getEncryptionKey from "./utils/getEncryptionKey"; +import {CustomEncrypter} from "./utils/CustomEncrypter"; +import {getEncryptionKey} from "./utils/getEncryptionKey"; const UID_SIZE = 21; const TIME_SIZE = 4; @@ -11,7 +11,7 @@ const TEMPID_SIZE = UID_SIZE + TIME_SIZE * 2; const IV_SIZE = 16; const AUTHTAG_SIZE = 16; -const getTempIDs = async (uid: string) => { +export const getTempIDs = async (uid: string) => { console.log('getTempIDs:', 'uid', uid); const encryptionKey = await getEncryptionKey(); @@ -76,5 +76,3 @@ export function decryptTempID(tempID: string, encryptionKey: Buffer): { uid: str expiryTime: expiryTime }; } - -export default getTempIDs; diff --git a/functions/src/opentrace/getUploadToken.ts b/functions/src/opentrace/getUploadToken.ts index 34767a5..83e4d0d 100644 --- a/functions/src/opentrace/getUploadToken.ts +++ b/functions/src/opentrace/getUploadToken.ts @@ -1,15 +1,15 @@ -import * as functions from "firebase-functions"; -import * as admin from "firebase-admin"; +import functions from "firebase-functions"; +import admin from "firebase-admin"; import config from "../config"; -import getEncryptionKey from "./utils/getEncryptionKey"; -import CustomEncrypter from "./utils/CustomEncrypter"; -import formatTimestamp from "./utils/formatTimestamp"; +import {getEncryptionKey} from "./utils/getEncryptionKey"; +import {CustomEncrypter} from "./utils/CustomEncrypter"; +import {formatTimestamp} from "./utils/formatTimestamp"; /** * Get upload token by passing in a secret string as `data` */ -const getUploadToken = async (uid: string, data: any, context: functions.https.CallableContext) => { +export const getUploadToken = async (uid: string, data: any, context: functions.https.CallableContext) => { console.log('getUploadToken:', 'uid', uid, 'data', data, 'ip', context.rawRequest.ip); let valid = false; @@ -109,5 +109,3 @@ export function validateToken(token: string, encryptionKey: Buffer, validateToke return {uid: uid, uploadCode: upload}; } - -export default getUploadToken; diff --git a/functions/src/opentrace/processUploadedData.ts b/functions/src/opentrace/processUploadedData.ts index 4bec9d1..048bae8 100644 --- a/functions/src/opentrace/processUploadedData.ts +++ b/functions/src/opentrace/processUploadedData.ts @@ -1,8 +1,6 @@ -import * as functions from "firebase-functions"; +import functions from "firebase-functions"; import {ObjectMetadata} from "firebase-functions/lib/providers/storage"; -const processUploadedData = async (object: ObjectMetadata) => { +export const processUploadedData = async (object: ObjectMetadata) => { throw new functions.https.HttpsError('unimplemented', 'Not implemented yet'); }; - -export default processUploadedData; diff --git a/functions/src/opentrace/types/FunctionConfig.ts b/functions/src/opentrace/types/FunctionConfig.ts index 1815bc9..60e3ddb 100644 --- a/functions/src/opentrace/types/FunctionConfig.ts +++ b/functions/src/opentrace/types/FunctionConfig.ts @@ -4,7 +4,7 @@ import PinGenerator from "../utils/PinGenerator"; // SUPPORTED_REGIONS from function-configuration.d.ts declare type SUPPORTED_REGIONS = "us-central1" | "us-east1" | "us-east4" | "europe-west1" | "europe-west2" | "asia-east2" | "asia-northeast1"; -interface FunctionConfig { +export interface FunctionConfig { projectId: string // Firebase Project ID regions: SUPPORTED_REGIONS[] utcOffset: number | string @@ -28,5 +28,3 @@ interface FunctionConfig { bucketForArchive: string } } - -export default FunctionConfig; diff --git a/functions/src/opentrace/types/HeartBeatEvent.ts b/functions/src/opentrace/types/HeartBeatEvent.ts index f4ae4b6..7425230 100644 --- a/functions/src/opentrace/types/HeartBeatEvent.ts +++ b/functions/src/opentrace/types/HeartBeatEvent.ts @@ -1,8 +1,6 @@ -interface HeartBeatEvent { +export interface HeartBeatEvent { timestamp: number, msg?: string, // enhanced fields: timestampString?: string, } - -export default HeartBeatEvent; diff --git a/functions/src/opentrace/types/StreetPassRecord.ts b/functions/src/opentrace/types/StreetPassRecord.ts index 537629a..33a7cd7 100644 --- a/functions/src/opentrace/types/StreetPassRecord.ts +++ b/functions/src/opentrace/types/StreetPassRecord.ts @@ -8,7 +8,7 @@ * Transmission Power txPower * Organization org */ -interface StreetPassRecord { +export interface StreetPassRecord { timestamp: number, msg?: string, modelC: string, @@ -24,5 +24,3 @@ interface StreetPassRecord { contactIdValidFrom?: number, contactIdValidTo?: number, } - -export default StreetPassRecord; diff --git a/functions/src/opentrace/utils/Authenticator.ts b/functions/src/opentrace/utils/Authenticator.ts index 97eeda6..8367324 100644 --- a/functions/src/opentrace/utils/Authenticator.ts +++ b/functions/src/opentrace/utils/Authenticator.ts @@ -1,7 +1,7 @@ -import * as functions from "firebase-functions"; +import functions from "firebase-functions"; import {CallableContext} from "firebase-functions/lib/providers/https"; -export default class Authenticator { +export class Authenticator { async authenticate(data: any, context: CallableContext): Promise { if (!context.auth) { throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.'); diff --git a/functions/src/opentrace/utils/CustomEncrypter.ts b/functions/src/opentrace/utils/CustomEncrypter.ts index b4fff6b..40ffe0b 100644 --- a/functions/src/opentrace/utils/CustomEncrypter.ts +++ b/functions/src/opentrace/utils/CustomEncrypter.ts @@ -1,7 +1,7 @@ -import * as crypto from "crypto"; +import crypto from "crypto"; import config from "../../config"; -class CustomEncrypter { +export class CustomEncrypter { algorithm: string; key: Buffer; @@ -71,5 +71,3 @@ class CustomEncrypter { return this.decrypt(decodedCipherTextB64, decodedIvB64, decodedAuthTagB64); } } - -export default CustomEncrypter; diff --git a/functions/src/opentrace/utils/PinGenerator.ts b/functions/src/opentrace/utils/PinGenerator.ts index f5a5ee1..99c0e8b 100644 --- a/functions/src/opentrace/utils/PinGenerator.ts +++ b/functions/src/opentrace/utils/PinGenerator.ts @@ -2,7 +2,7 @@ * This class uses a plain substring to generate a pin from user uid. * It should be subclassed with a secure implementation. */ -export default class PinGenerator { +export class PinGenerator { async generatePin(uid: string): Promise { return uid.substring(0, 6).toUpperCase(); } diff --git a/functions/src/opentrace/utils/formatTimestamp.ts b/functions/src/opentrace/utils/formatTimestamp.ts index 2ffd6e0..77980a2 100644 --- a/functions/src/opentrace/utils/formatTimestamp.ts +++ b/functions/src/opentrace/utils/formatTimestamp.ts @@ -2,14 +2,12 @@ * Convert timestamp (expressed in seconds since the Epoch) to "DD-MMM-YYYY HH:mm:ss Z" format * @param timestamp */ -import * as moment from "moment"; +import moment from "moment"; import config from "../../config"; const TIMESTAMP_FORMAT = "DD-MMM-YYYY HH:mm:ss Z"; -function formatTimestamp(timestamp: number) { +export function formatTimestamp(timestamp: number) { return moment.unix(timestamp).utcOffset(config.utcOffset).format(TIMESTAMP_FORMAT); } - -export default formatTimestamp; \ No newline at end of file diff --git a/functions/src/opentrace/utils/getEncryptionKey.ts b/functions/src/opentrace/utils/getEncryptionKey.ts index 3a237c6..56fbde5 100644 --- a/functions/src/opentrace/utils/getEncryptionKey.ts +++ b/functions/src/opentrace/utils/getEncryptionKey.ts @@ -5,7 +5,7 @@ import config from "../../config"; const SECRET_KEY = `projects/${config.projectId}/secrets/${config.encryption.keyPath}`; const SECRET_KEY_DEFAULT_VERSION = `${SECRET_KEY}/versions/${config.encryption.defaultVersion}`; -const getEncryptionKey = async (): Promise => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION); +export const getEncryptionKey = async (): Promise => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION); export const getAllEncryptionKeys = async (): Promise => { const secretManagerClient = new SecretManagerServiceClient(); @@ -25,7 +25,7 @@ export const getAllEncryptionKeys = async (): Promise => { const isDefaultKey = (_: string) => SECRET_KEY_DEFAULT_VERSION.substring(SECRET_KEY_DEFAULT_VERSION.length - 2) === _.substring(_.length - 2); -async function getEncryptionSecret(keyPathIncludingVersion: string): Promise { +export async function getEncryptionSecret(keyPathIncludingVersion: string): Promise { const secretManagerClient = new SecretManagerServiceClient(); console.log("getEncryptionSecret:", `Getting encryption key: ${keyPathIncludingVersion}`); @@ -36,5 +36,3 @@ async function getEncryptionSecret(keyPathIncludingVersion: string): Promise