Skip to content

Commit

Permalink
Add environment detection
Browse files Browse the repository at this point in the history
  • Loading branch information
drzax committed Feb 26, 2021
1 parent 63666a5 commit 2434663
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"start": "tsdx watch",
"build": "tsdx build",
"lint": "tsdx lint src",
"test": "tsdx test",
"prepublishOnly": "npm run build",
"release": "np"
},
Expand Down
41 changes: 28 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export enum TIERS {
PREVIEW = 'preview',
}

type AGTN = APPLICATIONS | GENERATIONS | TIERS | null;
export enum ENVIRONMENTS {
UAT = 'uat',
PROD = 'prod',
}

type AGTNE = APPLICATIONS | GENERATIONS | TIERS | ENVIRONMENTS | null;

export enum DECOY_KEYS {
ARTICLE = 'article',
Expand Down Expand Up @@ -64,9 +69,8 @@ declare global {
}

// Shared constants & functions
const HOSTNAME = window.location.hostname;
const isPartialInHostname = (partialHostname: string): boolean =>
HOSTNAME.indexOf(partialHostname) > -1;
window.location.hostname.indexOf(partialHostname) > -1;
const areAnyPartialsInHostname = (partialHostnames: string[]): boolean =>
!!partialHostnames.find(isPartialInHostname);
const isSelectable = (selector: string): boolean =>
Expand All @@ -75,10 +79,14 @@ const isGeneratedBy = (generatorName: string): boolean =>
isSelectable(`[name="generator"][content="${generatorName}"]`);
const hasIconFrom = (slug: string): boolean =>
isSelectable(`[rel*="icon"][href^="/${slug}/"]`);
const memoize = (fn: () => AGTN) => {
let cached: AGTN;
return () =>
typeof cached === 'undefined' ? ((cached = fn()), cached) : cached;
const memoize = (fn: () => AGTNE) => {
let cached: AGTNE;
return (cache: boolean = true) =>
cache
? typeof cached === 'undefined'
? ((cached = fn()), cached)
: cached
: fn();
};

// Application detection
Expand Down Expand Up @@ -148,16 +156,23 @@ export const getTier = memoize(function _getTier(): TIERS | null {
'presentation-layer.abc',
])
? TIERS.PREVIEW
: areAnyPartialsInHostname([
'www.abc',
'mobile.abc',
'bigted.abc',
'newsapp.abc',
])
: areAnyPartialsInHostname(['www.abc', 'mobile.abc', 'newsapp.abc'])
? TIERS.LIVE
: null;
});

// Environment detection
// * Environments can be detected by matching host names
export const getEnvironment = memoize(
function _getEnvironment(): ENVIRONMENTS | null {
return areAnyPartialsInHostname(['developer.presentation-layer'])
? ENVIRONMENTS.UAT
: getTier() === TIERS.LIVE || getTier() === TIERS.PREVIEW
? ENVIRONMENTS.PROD
: null;
}
);

// Store references to PL decoy activation requests and granted DOM permits
const decoyActivationRequests: DecoyActivationRequests = {};
let domPermitsGranted: DOMPermit[] = [];
Expand Down

0 comments on commit 2434663

Please sign in to comment.