Release version 1.0.1
This commit is contained in:
152
dist/chromium/background.js
vendored
152
dist/chromium/background.js
vendored
@@ -18,6 +18,10 @@
|
||||
const OPPTY_PRODUCTS_MAX_PAGES = 250;
|
||||
const OPPTY_PRODUCTS_CONCURRENCY = 4;
|
||||
const CUSTOMER_TOKEN_SERVICE_URL = "https://spa.oracle.com/oalcrm/web/api/g2m-consumer-application/consumerTokenService";
|
||||
const CUSTOMER_TOKEN_SESSION_URL = "https://spa.oracle.com/oalcrm/web/api/g2m-consumer-application/ui/index.html";
|
||||
const CUSTOMER_TOKEN_MAX_ATTEMPTS = 5;
|
||||
const CUSTOMER_TOKEN_RETRY_BASE_DELAY_MS = 750;
|
||||
const CUSTOMER_TOKEN_RETRY_MAX_DELAY_MS = 4000;
|
||||
const ACCOUNT_SUMMARY_URL = "https://spa.oracle.com/oalcrm/web/api/provider-proxy/g2m-account/service/accountsummary";
|
||||
const ACCOUNT_PLANS_URL = "https://spa.oracle.com/oalcrm/web/api/provider-proxy/g2m-consumption/service/v1/account/accountPlans";
|
||||
const CONSUMPTION_TREND_URL = "https://spa.oracle.com/oalcrm/web/api/provider-proxy/g2m-consumption/service/v1/plan/trend";
|
||||
@@ -129,7 +133,7 @@
|
||||
}
|
||||
|
||||
if (message.type === "opportunitiesExtension.requestConsumerTokenService") {
|
||||
requestConsumerTokenService()
|
||||
requestConsumerTokenService(sender)
|
||||
.then(sendResponse)
|
||||
.catch((error) => {
|
||||
sendResponse({
|
||||
@@ -173,20 +177,11 @@
|
||||
return false;
|
||||
});
|
||||
|
||||
async function requestConsumerTokenService() {
|
||||
async function requestConsumerTokenService(sender) {
|
||||
const debugRequests = [];
|
||||
|
||||
try {
|
||||
const tokenResponse = await fetchJson(CUSTOMER_TOKEN_SERVICE_URL, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Cache-Control": "no-cache",
|
||||
Pragma: "no-cache"
|
||||
}
|
||||
}, "requestConsumerTokenService", debugRequests);
|
||||
const tokenResponse = await fetchConsumerTokenServiceUntilJson(sender, debugRequests);
|
||||
const accountSummaryAuthorization = extractTokenServiceAuthorization(
|
||||
tokenResponse,
|
||||
"ACCP-TS",
|
||||
@@ -239,6 +234,126 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchConsumerTokenServiceUntilJson(sender, debugRequests) {
|
||||
let attempt = 0;
|
||||
let sessionTab = null;
|
||||
let receivedValidJson = false;
|
||||
|
||||
try {
|
||||
while (attempt < CUSTOMER_TOKEN_MAX_ATTEMPTS) {
|
||||
attempt += 1;
|
||||
|
||||
try {
|
||||
const tokenResponse = await fetchJson(CUSTOMER_TOKEN_SERVICE_URL, {
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Cache-Control": "no-cache, no-store, max-age=0",
|
||||
Pragma: "no-cache"
|
||||
}
|
||||
}, "requestConsumerTokenService", debugRequests);
|
||||
const debugEntry = debugRequests[debugRequests.length - 1];
|
||||
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.attempt = String(attempt);
|
||||
}
|
||||
|
||||
receivedValidJson = true;
|
||||
return tokenResponse;
|
||||
} catch (error) {
|
||||
const isHtmlFallback = error
|
||||
&& error.code === "INVALID_JSON_RESPONSE"
|
||||
&& error.httpStatus === 200;
|
||||
const debugEntry = debugRequests[debugRequests.length - 1];
|
||||
|
||||
if (!isHtmlFallback || attempt >= CUSTOMER_TOKEN_MAX_ATTEMPTS) {
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.attempt = String(attempt);
|
||||
if (isHtmlFallback) {
|
||||
debugEntry.metadata.retryLimitReached = "true";
|
||||
debugEntry.error = `requestConsumerTokenService returned invalid JSON after ${CUSTOMER_TOKEN_MAX_ATTEMPTS} attempts.`;
|
||||
error.message = debugEntry.error;
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!sessionTab) {
|
||||
try {
|
||||
sessionTab = await openConsumerTokenSessionTab(sender);
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.sessionBootstrapUrl = CUSTOMER_TOKEN_SESSION_URL;
|
||||
debugEntry.metadata.sessionBootstrapState = "opened-inactive-tab";
|
||||
}
|
||||
} catch (sessionError) {
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.sessionBootstrapUrl = CUSTOMER_TOKEN_SESSION_URL;
|
||||
debugEntry.metadata.sessionBootstrapState = `tab-open-failed: ${sessionError.message || "unknown error"}`;
|
||||
}
|
||||
}
|
||||
} else if (debugEntry) {
|
||||
debugEntry.metadata.sessionBootstrapState = "waiting-for-valid-session";
|
||||
}
|
||||
|
||||
const retryDelayMs = getConsumerTokenRetryDelay(attempt);
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.attempt = String(attempt);
|
||||
debugEntry.metadata.retryReason = "HTTP 200 response did not contain valid JSON";
|
||||
debugEntry.metadata.nextRetryDelayMs = String(retryDelayMs);
|
||||
}
|
||||
|
||||
await wait(retryDelayMs);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("requestConsumerTokenService retry loop ended unexpectedly.");
|
||||
} finally {
|
||||
if (sessionTab && Number.isInteger(sessionTab.id)) {
|
||||
const debugEntry = debugRequests[debugRequests.length - 1];
|
||||
|
||||
try {
|
||||
await tabsRemove(sessionTab.id);
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.sessionBootstrapState = receivedValidJson
|
||||
? "closed-after-valid-json"
|
||||
: "closed-after-retry-finished";
|
||||
}
|
||||
} catch (error) {
|
||||
if (debugEntry) {
|
||||
debugEntry.metadata.sessionBootstrapState = `tab-close-failed: ${error.message || "unknown error"}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getConsumerTokenRetryDelay(attempt) {
|
||||
return Math.min(
|
||||
CUSTOMER_TOKEN_RETRY_BASE_DELAY_MS * (2 ** Math.max(attempt - 1, 0)),
|
||||
CUSTOMER_TOKEN_RETRY_MAX_DELAY_MS
|
||||
);
|
||||
}
|
||||
|
||||
function openConsumerTokenSessionTab(sender) {
|
||||
const details = {
|
||||
url: CUSTOMER_TOKEN_SESSION_URL,
|
||||
active: false
|
||||
};
|
||||
|
||||
if (sender && sender.tab && Number.isInteger(sender.tab.windowId)) {
|
||||
details.windowId = sender.tab.windowId;
|
||||
}
|
||||
|
||||
if (supportsFirefoxCookieStoreTabs() && sender && sender.tab && sender.tab.cookieStoreId) {
|
||||
details.cookieStoreId = sender.tab.cookieStoreId;
|
||||
}
|
||||
|
||||
return tabsCreate(details);
|
||||
}
|
||||
|
||||
async function requestAccountPlans(message) {
|
||||
const partyNumber = typeof message.partyNumber === "string" ? message.partyNumber.trim() : "";
|
||||
const accountSummaryAuthorization = normalizeMessageAuthorization(
|
||||
@@ -416,7 +531,14 @@
|
||||
throw new Error(`${requestName} response body could not be read.`);
|
||||
}
|
||||
|
||||
return parseJsonResponse(responsePayload.text, requestName);
|
||||
try {
|
||||
return parseJsonResponse(responsePayload.text, requestName);
|
||||
} catch (error) {
|
||||
error.httpStatus = response.status;
|
||||
debugEntry.status = "failed";
|
||||
debugEntry.error = error.message;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function createBackgroundDebugEntry(requestName, url, options, startedAt) {
|
||||
@@ -491,7 +613,9 @@
|
||||
try {
|
||||
return JSON.parse(normalizedText);
|
||||
} catch (error) {
|
||||
throw new Error(`${requestName} returned an invalid JSON response.`);
|
||||
const invalidJsonError = new Error(`${requestName} returned an invalid JSON response.`);
|
||||
invalidJsonError.code = "INVALID_JSON_RESPONSE";
|
||||
throw invalidJsonError;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user