Skip to content

Commit

Permalink
feat: updates import resolution
Browse files Browse the repository at this point in the history
Updates import resolution to use ES6-style imports and Typescript import/export helpers.

Signed-off-by: Rifa Achrinza <[email protected]>
  • Loading branch information
achrinza committed Apr 10, 2020
1 parent d4737ff commit ef40bbf
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 64 deletions.
10 changes: 4 additions & 6 deletions functions/src/config.example.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -26,5 +26,3 @@ const config: FunctionConfig = {
bucketForArchive: "archive-bucket",
},
};

export default config;
2 changes: 1 addition & 1 deletion functions/src/firebaseFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as functions from "firebase-functions";
import functions from "firebase-functions";

import config from "./config";

Expand Down
2 changes: 1 addition & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as admin from "firebase-admin";
import admin from "firebase-admin";
admin.initializeApp();

import * as firebaseFunctions from "./firebaseFunctions";
Expand Down
7 changes: 2 additions & 5 deletions functions/src/opentrace/getHandshakePin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -20,5 +19,3 @@ const getHandshakePin = async (uid: string) => {
};

export const getUserHandshakePin = config.upload.pinGenerator.generatePin;

export default getHandshakePin;
10 changes: 4 additions & 6 deletions functions/src/opentrace/getTempIDs.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -76,5 +76,3 @@ export function decryptTempID(tempID: string, encryptionKey: Buffer): { uid: str
expiryTime: expiryTime
};
}

export default getTempIDs;
14 changes: 6 additions & 8 deletions functions/src/opentrace/getUploadToken.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -109,5 +109,3 @@ export function validateToken(token: string, encryptionKey: Buffer, validateToke

return {uid: uid, uploadCode: upload};
}

export default getUploadToken;
6 changes: 2 additions & 4 deletions functions/src/opentrace/processUploadedData.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions functions/src/opentrace/types/FunctionConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,5 +28,3 @@ interface FunctionConfig {
bucketForArchive: string
}
}

export default FunctionConfig;
4 changes: 1 addition & 3 deletions functions/src/opentrace/types/HeartBeatEvent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
interface HeartBeatEvent {
export interface HeartBeatEvent {
timestamp: number,
msg?: string,
// enhanced fields:
timestampString?: string,
}

export default HeartBeatEvent;
4 changes: 1 addition & 3 deletions functions/src/opentrace/types/StreetPassRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Transmission Power txPower
* Organization org
*/
interface StreetPassRecord {
export interface StreetPassRecord {
timestamp: number,
msg?: string,
modelC: string,
Expand All @@ -24,5 +24,3 @@ interface StreetPassRecord {
contactIdValidFrom?: number,
contactIdValidTo?: number,
}

export default StreetPassRecord;
4 changes: 2 additions & 2 deletions functions/src/opentrace/utils/Authenticator.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.');
Expand Down
6 changes: 2 additions & 4 deletions functions/src/opentrace/utils/CustomEncrypter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,5 +71,3 @@ class CustomEncrypter {
return this.decrypt(decodedCipherTextB64, decodedIvB64, decodedAuthTagB64);
}
}

export default CustomEncrypter;
2 changes: 1 addition & 1 deletion functions/src/opentrace/utils/PinGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return uid.substring(0, 6).toUpperCase();
}
Expand Down
6 changes: 2 additions & 4 deletions functions/src/opentrace/utils/formatTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 2 additions & 4 deletions functions/src/opentrace/utils/getEncryptionKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer> => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION);
export const getEncryptionKey = async (): Promise<Buffer> => getEncryptionSecret(SECRET_KEY_DEFAULT_VERSION);

export const getAllEncryptionKeys = async (): Promise<Buffer[]> => {
const secretManagerClient = new SecretManagerServiceClient();
Expand All @@ -25,7 +25,7 @@ export const getAllEncryptionKeys = async (): Promise<Buffer[]> => {

const isDefaultKey = (_: string) => SECRET_KEY_DEFAULT_VERSION.substring(SECRET_KEY_DEFAULT_VERSION.length - 2) === _.substring(_.length - 2);

async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buffer> {
export async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buffer> {
const secretManagerClient = new SecretManagerServiceClient();

console.log("getEncryptionSecret:", `Getting encryption key: ${keyPathIncludingVersion}`);
Expand All @@ -36,5 +36,3 @@ async function getEncryptionSecret(keyPathIncludingVersion: string): Promise<Buf
// @ts-ignore
return Buffer.from(secret.payload.data.toString(), 'base64');
}

export default getEncryptionKey;
2 changes: 1 addition & 1 deletion functions/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as admin from "firebase-admin";
import admin from "firebase-admin";
import config from "../src/config";

if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
Expand Down
8 changes: 4 additions & 4 deletions functions/test/opentrace/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import * as chai from "chai";
import * as crypto from "crypto";
import admin from "firebase-admin";
import functions from "firebase-functions";
import chai from "chai";
import crypto from "crypto";

import config from "../../src/config";
import {FunctionsTestWrapper} from "../index.test";
Expand Down
8 changes: 4 additions & 4 deletions functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2017"
"target": "es2017",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"importHelpers": true
},
"compileOnSave": true,
"include": [
"src"
]
}

0 comments on commit ef40bbf

Please sign in to comment.