Skip to content

Commit

Permalink
ENG-0000 - Exploratory Bootstrap Modification
Browse files Browse the repository at this point in the history
Attempting to see if this remedies previously seen failure cases.
Bumps version to 2.1.0-beta.1.
  • Loading branch information
mcnielsen committed Jul 31, 2023
1 parent 4f647c1 commit 0c476ca
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/nucleus/src/common/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class AlError extends Error
*/
public static log( error:AlNetworkResponse|AlError|Error|string|any, commentary?:string, ...details:any[] ) {
let normalized = AlError.normalize( error );
console.log( commentary ? `${commentary}: ${normalized.message}` : normalized.message, details.length > 0 ? details : undefined );
console.log( commentary ? `${commentary}: ${normalized.message}` : normalized.message, details.length > 0 ? details : undefined, error );
}

/**
Expand Down
20 changes: 10 additions & 10 deletions lib/nucleus/src/common/types/al-locator.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ export class AlLocatorMatrix implements AlLocationContext
public locationId?:string;
public accessibleLocationIds?:string[];

public debug = false;

private actingUri:string|undefined;
private actor:AlLocationDescriptor|undefined;

Expand All @@ -249,7 +251,7 @@ export class AlLocatorMatrix implements AlLocationContext
if ( typeof( actingUri ) === 'boolean' || actingUri ) {
this.setActingUrl( actingUri, initialContext );
} else if ( initialContext ) {
this.setContext( initialContext );
this.setContext( initialContext, "Initial" );
}
}

Expand All @@ -264,7 +266,7 @@ export class AlLocatorMatrix implements AlLocationContext
}

public target( context:AlLocationContext ) {
this.setContext( context );
this.setContext( context, "Targeting" );
}

/**
Expand Down Expand Up @@ -464,7 +466,7 @@ export class AlLocatorMatrix implements AlLocationContext
} else {
throw new Error(`Setting context by URL '${actingUri}' failed` );
}
this.setContext( context );
this.setContext( context, "Acting URL" );
}
}

Expand Down Expand Up @@ -570,7 +572,7 @@ export class AlLocatorMatrix implements AlLocationContext
* Sets the acting context (preferred environment, data residency, location attributes).
* This acts as a merge against existing context, so the caller can provide only fragmentary information without borking things.
*/
protected setContext( context:AlLocationContext, debug?:string|boolean ) {
protected setContext( context:AlLocationContext, reason?:string ) {
let notes:string[] = [];
this.nodeCache = {}; // flush lookup cache
this.environment = context && context.environment ? context.environment : this.environment;
Expand All @@ -588,7 +590,7 @@ export class AlLocatorMatrix implements AlLocationContext
if ( this.locationId && this.accessibleLocationIds ) {
if ( ! this.accessibleLocationIds.includes( this.locationId ) ) {
this.locationId = this.accessibleLocationIds[0];
if ( debug ) {
if ( this.debug ) {
notes.push(`accessibility limited to ${this.locationId} `);
}
}
Expand All @@ -602,7 +604,7 @@ export class AlLocatorMatrix implements AlLocationContext
}
this.residency = AlInsightLocations[correctedLocationId].residency || this.residency;
this.locationId = correctedLocationId;
if ( debug ) {
if ( this.debug ) {
notes.push( `overrode location/residency by insight location ${this.locationId}` );
}
} else {
Expand All @@ -611,11 +613,9 @@ export class AlLocatorMatrix implements AlLocationContext
}
}

if ( debug ) {
console.log(`Context set to ${this.environment}/${this.residency} (${this.locationId} in [${this.accessibleLocationIds ? this.accessibleLocationIds.join(",") : ''}]): %s`,
( typeof( debug ) === 'string' ? debug : 'Debug' ) + ( notes.length ? `: ${notes.join(", " )}` : '' ) );
if ( this.debug ) {
console.log(`Context set to ${this.environment}/${this.residency} (${this.locationId} in [${this.accessibleLocationIds ? this.accessibleLocationIds.join(",") : ''}]): %s`, reason ? reason : 'Debug' );
}

}

protected timestamp( defaultValue:number ):number {
Expand Down
3 changes: 2 additions & 1 deletion lib/nucleus/src/context/al-execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export abstract class AlExecutionContext
AlGlobalizer.expose( "al", { "context": this } );
}
this.setOptions( options );
this.sessionInstance = new AlSessionInstance( this );
this.locatorService = new AlLocatorMatrix( AlLocationDictionary, true );
this.sessionInstance = new AlSessionInstance( this );
initializeAlGlobals( this.sessionInstance, this.locator, this.client( RootClient ) );
}

Expand Down
2 changes: 2 additions & 0 deletions lib/nucleus/src/session/al-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
AlCabinet,
ConfigOption,
AlError,
AlStopwatch,
} from "../common";
import { AlExecutionContext } from '../context';
import { AlBeforeNetworkRequest } from '../client';
Expand Down Expand Up @@ -567,6 +568,7 @@ export class AlSessionInstance
protected async restoreSession( session:AIMSSessionDescriptor ) {
try {
this.startDetection();
await AlStopwatch.promise( 1 );
await this.setAuthentication(session);
} catch( e ) {
this.deactivateSession();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@al/core",
"version": "2.0.16",
"version": "2.1.0-beta.1",
"description": "Node Enterprise Packages for Alert Logic (NEPAL) Core Library",
"main": "./bundles/al-core-nucleus.es5.js",
"types": "./types/al-core-nucleus.d.ts",
Expand Down
31 changes: 28 additions & 3 deletions tests/common/al-locator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ describe( 'AlLocatorMatrix', () => {
expect( uri ).toEqual( "https://incidents-pr-9.ui-dev.product.dev.alertlogic.com/#/summary/1" );
} );

/**
* Good test, but relies on window
*/
xit( "should use window.location if the node isn't recognized", () => {
let uri = context.locator.resolveURL( "SomethingUnrecognizable", '/#/arbitrary', { residency: "US", environment: "production" } );
expect( uri ).toEqual( window.location.origin + ( ( window.location.pathname && window.location.pathname.length > 1 ) ? window.location.pathname : '' ) + "/#/arbitrary" );
Expand All @@ -316,4 +313,32 @@ describe( 'AlLocatorMatrix', () => {
} );
} );

describe( "contextual residency awareness", () => {
it("should be maintained as expected", async () => {

/* Starting state: default values */
expect( context.environment ).toEqual( "development" );
expect( context.residency ).toEqual( "US" );
expect( context.locationId ).toEqual("unspecified");
expect( context.accessibleLocationIds ).toEqual( [] );

context.target( "https://console.alertlogic.com/#/search/expert/2");

/* After context is derived from URL: production/US */
expect( context.environment ).toEqual( "production" );
expect( context.residency ).toEqual( "US" );
expect( context.locationId ).toEqual("unspecified");
expect( context.accessibleLocationIds ).toEqual( [] );

await AlTestExecutionContext.setAuthState("2");
context.target( {
locationId: "defender-uk-newport",
accessibleLocationIds: [ "defender-uk-newport", "insight-eu-ireland", "defender-us-denver", "insight-us-virginia" ]
} );

expect( context.environment ).toEqual("production");
expect( context.residency ).toEqual( "EMEA" );
expect( context.locationId ).toEqual( "defender-uk-newport" );
} );
} );
} );

0 comments on commit 0c476ca

Please sign in to comment.