Skip to content

Commit 939d2cb

Browse files
authored
Merge pull request #5 from boilingdata/add-multi-region-testing
feat: Add multi region testing
2 parents 3e352d3 + 979657c commit 939d2cb

7 files changed

Lines changed: 604 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@boilingdata/node-boilingdata",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
44
"description": "BoilingData client",
55
"main": "dist/cjs/index.js",
66
"types": "dist/cjs/index.d.ts",

src/boilingdata/boilingdata.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface IBDDataQuery {
2525

2626
export interface IBDDataResponse {
2727
messageType: "DATA";
28-
data: Array<string>;
28+
data: Array<any>;
2929
requestId: string;
3030
numOfRecords?: number;
3131
batchSerial?: number;

src/boilingdata/boilingdata.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@ export interface IBDCallbacks {
1919
onSocketClose?: () => void;
2020
}
2121

22+
export type BDAWSRegion =
23+
| "eu-west-1"
24+
| "eu-west-2"
25+
| "eu-west-3"
26+
| "eu-north-1"
27+
| "eu-south-1"
28+
| "eu-central-1"
29+
| "us-east-1"
30+
| "us-east-2"
31+
| "us-west-1"
32+
| "us-west-2"
33+
| "ca-central-1";
34+
2235
export interface IBoilingData {
2336
username: string;
2437
password: string;
2538
logLevel?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; // Match with Bunyan
2639
globalCallbacks?: IBDCallbacks;
40+
region?: BDAWSRegion;
2741
}
2842

2943
export interface IBDQuery {
@@ -74,11 +88,13 @@ export function isDataResponse(data: IBDDataResponse | unknown): data is IBDData
7488

7589
export class BoilingData {
7690
private statusTimer?: NodeJS.Timeout;
91+
private region: BDAWSRegion;
7792
private creds?: BDCredentials;
7893
private socketInstance: ISocketInstance;
7994
private logger = createLogger({ name: "boilingdata", level: this.props.logLevel ?? "info" });
8095

8196
constructor(public props: IBoilingData) {
97+
this.region = this.props.region ? this.props.region : "eu-west-1";
8298
this.socketInstance = {
8399
queries: new Map(), // no queries yet
84100
queryCallbacks: new Map(), // no queries yet, so no query specific callbacks either
@@ -103,9 +119,8 @@ export class BoilingData {
103119
return new Promise((resolve, reject) => {
104120
const sock = this.socketInstance;
105121
const cbs = this.props.globalCallbacks;
106-
getBoilingDataCredentials(this.props.username, this.props.password).then(creds => {
122+
getBoilingDataCredentials(this.props.username, this.props.password, this.region).then(creds => {
107123
this.creds = creds;
108-
this.logger.debug(this.creds);
109124
sock.socket = new WebSocket(this.creds.signedWebsocketUrl);
110125
sock.socket.onclose = () => {
111126
if (cbs?.onSocketClose) cbs.onSocketClose();

src/common/identity.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { CognitoIdentity, CognitoIdentityCredentials } from "aws-sdk";
22
import { CognitoIdToken, CognitoUserPool, CognitoUser, AuthenticationDetails } from "amazon-cognito-identity-js";
33
import { getSignedWssUrl } from "./signature";
4+
import { BDAWSRegion } from "boilingdata/boilingdata";
45

5-
// FIXME: Hard coded
6-
const region = "eu-west-1";
6+
const IDP_REGION = "eu-west-1";
77
const UserPoolId = "eu-west-1_0GLV9KO1p";
8+
const Logins = `cognito-idp.${IDP_REGION}.amazonaws.com/${UserPoolId}`;
89
const IdentityPoolId = "eu-west-1:bce21571-e3a6-47a4-8032-fd015213405f";
9-
const webSocketHost = "m9fhs4t5vh.execute-api.eu-west-1.amazonaws.com";
10-
const Logins = `cognito-idp.${region}.amazonaws.com/${UserPoolId}`;
1110
const poolData = { UserPoolId, ClientId: "6timr8knllr4frovfvq8r2o6oo" };
1211
const Pool = new CognitoUserPool(poolData);
1312

@@ -31,20 +30,30 @@ function getIdToken(Username: string, Password: string): Promise<CognitoIdToken>
3130

3231
async function refreshCredsWithToken(idToken: string): Promise<CognitoIdentityCredentials> {
3332
const idParams = { IdentityPoolId, Logins: { [Logins]: idToken } };
34-
const creds = new CognitoIdentityCredentials(idParams, { region });
33+
const creds = new CognitoIdentityCredentials(idParams, { region: IDP_REGION });
3534
await creds.getPromise();
3635
return creds;
3736
}
3837

39-
export async function getBoilingDataCredentials(username: string, password: string): Promise<BDCredentials> {
38+
function getWsApiDomain(region: string): string {
39+
return `${region}.api.boilingdata.com`;
40+
// return `api.boilingdata.com`;
41+
}
42+
43+
export async function getBoilingDataCredentials(
44+
username: string,
45+
password: string,
46+
region: BDAWSRegion = "eu-west-1",
47+
): Promise<BDCredentials> {
48+
const webSocketHost = getWsApiDomain(region);
4049
const idToken = await getIdToken(username, password);
4150
const creds = await refreshCredsWithToken(idToken.getJwtToken());
4251
const accessKeyId = creds.data?.Credentials?.AccessKeyId;
4352
const secretAccessKey = (<CognitoIdentity.Types.GetCredentialsForIdentityResponse>creds.data)?.Credentials?.SecretKey;
4453
const sessionToken = creds.data?.Credentials?.SessionToken;
4554
if (!accessKeyId || !secretAccessKey) throw new Error("Missing credentials (after refresh)!");
4655
const credentials = { accessKeyId, secretAccessKey, sessionToken };
47-
const signedWebsocketUrl = await getSignedWssUrl(webSocketHost, credentials);
56+
const signedWebsocketUrl = await getSignedWssUrl(webSocketHost, credentials, region, "wss", "");
4857
const cognitoUsername = idToken.decodePayload()["cognito:username"];
4958
return { cognitoUsername, signedWebsocketUrl };
5059
}

src/common/signature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ function getSigner(region: string, credentials: any): SignatureV4 {
1111
export async function getSignedWssUrl(
1212
host: string,
1313
credentials: Credentials | Provider<Credentials>,
14+
region: string,
1415
protocol = "wss",
1516
path = "/dev",
16-
region = "eu-west-1",
1717
): Promise<string> {
1818
const request = new HttpRequest({ protocol, headers: { host }, hostname: host, path });
1919
const fiveMinsS = 5 * 60;

0 commit comments

Comments
 (0)