Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Fallback for Blocked demdex.net Requests #1215

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"uuid": "^11.0.1"
},
"devDependencies": {
"@adobe/alloy": "^2.24.0",
"@adobe/alloy": "^2.24.0-beta.2",
"@babel/cli": "^7.25.9",
"@babel/plugin-transform-runtime": "^7.25.9",
"@eslint/js": "^9.13.0",
Expand Down
76 changes: 50 additions & 26 deletions src/core/edgeNetwork/injectSendEdgeNetworkRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ governing permissions and limitations under the License.
import { ID_THIRD_PARTY as ID_THIRD_PARTY_DOMAIN } from "../../constants/domain.js";
import apiVersion from "../../constants/apiVersion.js";
import { createCallbackAggregator, noop } from "../../utils/index.js";
import { isNetworkError } from "../../utils/networkErrors.js";
import mergeLifecycleResponses from "./mergeLifecycleResponses.js";
import handleRequestFailure from "./handleRequestFailure.js";

const isDemdexBlockedError = (error, request) => {
return request.getUseIdThirdPartyDomain() && isNetworkError(error);
};

export default ({
config,
lifecycle,
Expand All @@ -27,6 +32,27 @@ export default ({
getAssuranceValidationTokenParams,
}) => {
const { edgeDomain, edgeBasePath, datastreamId } = config;
let hasDemdexFailed = false;

const buildEndpointUrl = (endpointDomain, request) => {
const locationHint = getLocationHint();
const edgeBasePathWithLocationHint = locationHint
? `${edgeBasePath}/${locationHint}${request.getEdgeSubPath()}`
: `${edgeBasePath}${request.getEdgeSubPath()}`;
const configId = request.getDatastreamIdOverride() || datastreamId;

if (configId !== datastreamId) {
request.getPayload().mergeMeta({
sdkConfig: {
datastream: {
original: datastreamId,
},
},
});
}

return `https://${endpointDomain}/${edgeBasePathWithLocationHint}/${apiVersion}/${request.getAction()}?configId=${configId}&requestId=${request.getId()}${getAssuranceValidationTokenParams()}`;
};

/**
* Sends a network request that is aware of payload interfaces,
Expand All @@ -52,26 +78,15 @@ export default ({
onRequestFailure: onRequestFailureCallbackAggregator.add,
})
.then(() => {
const endpointDomain = request.getUseIdThirdPartyDomain()
? ID_THIRD_PARTY_DOMAIN
: edgeDomain;
const locationHint = getLocationHint();
const edgeBasePathWithLocationHint = locationHint
? `${edgeBasePath}/${locationHint}${request.getEdgeSubPath()}`
: `${edgeBasePath}${request.getEdgeSubPath()}`;
const configId = request.getDatastreamIdOverride() || datastreamId;
const endpointDomain =
hasDemdexFailed || !request.getUseIdThirdPartyDomain()
? edgeDomain
: ID_THIRD_PARTY_DOMAIN;

const url = buildEndpointUrl(endpointDomain, request);
const payload = request.getPayload();
if (configId !== datastreamId) {
payload.mergeMeta({
sdkConfig: {
datastream: {
original: datastreamId,
},
},
});
}
const url = `https://${endpointDomain}/${edgeBasePathWithLocationHint}/${apiVersion}/${request.getAction()}?configId=${configId}&requestId=${request.getId()}${getAssuranceValidationTokenParams()}`;
cookieTransfer.cookiesToPayload(payload, endpointDomain);

return sendNetworkRequest({
requestId: request.getId(),
url,
Expand All @@ -83,17 +98,26 @@ export default ({
processWarningsAndErrors(networkResponse);
return networkResponse;
})
.catch(handleRequestFailure(onRequestFailureCallbackAggregator))
.catch((error) => {
if (isDemdexBlockedError(error, request)) {
hasDemdexFailed = true;
request.setUseIdThirdPartyDomain(false);
const url = buildEndpointUrl(edgeDomain, request);
const payload = request.getPayload();
cookieTransfer.cookiesToPayload(payload, edgeDomain);

return sendNetworkRequest({
requestId: request.getId(),
url,
payload,
useSendBeacon: request.getUseSendBeacon(),
});
}
return handleRequestFailure(onRequestFailureCallbackAggregator)(error);
})
.then(({ parsedBody, getHeader }) => {
// Note that networkResponse.parsedBody may be undefined if it was a
// 204 No Content response. That's fine.
const response = createResponse({ content: parsedBody, getHeader });
cookieTransfer.responseToCookies(response);

// Notice we're calling the onResponse lifecycle method even if there are errors
// inside the response body. This is because the full request didn't actually fail--
// only portions of it that are considered non-fatal (a specific, non-critical
// Konductor plugin, for example).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove these comments?

return onResponseCallbackAggregator
.call({
response,
Expand Down
26 changes: 26 additions & 0 deletions src/utils/networkErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
export const TYPE_ERROR = "TypeError";
export const NETWORK_ERROR = "NetworkError";

/**
* Checks if the error is a network-related error
* @param {Error} error The error to check
* @returns {boolean} True if the error is network-related
*/
export const isNetworkError = (error) => {
return (
error.name === TYPE_ERROR ||
error.name === NETWORK_ERROR ||
error.status === 0
);
};
23 changes: 23 additions & 0 deletions test/functional/helpers/requestHooks/demdexBlockerMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { RequestMock } from "testcafe";

// Mock that fails demdex requests
export default RequestMock()
.onRequestTo((request) => request.url.includes("demdex.net"))
.respond((req, res) => {
res.statusCode = 500;
res.headers = {
"content-type": "application/json",
};
return "";
});
54 changes: 54 additions & 0 deletions test/functional/specs/Identity/demdexFallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { t } from "testcafe";
import createNetworkLogger from "../../helpers/networkLogger/index.js";
import createFixture from "../../helpers/createFixture/index.js";
import {
compose,
orgMainConfigMain,
thirdPartyCookiesEnabled,
} from "../../helpers/constants/configParts/index.js";
import createAlloyProxy from "../../helpers/createAlloyProxy.js";
import demdexBlockerMock from "../../helpers/requestHooks/demdexBlockerMock.js";

const networkLogger = createNetworkLogger();
const config = compose(orgMainConfigMain, thirdPartyCookiesEnabled);

createFixture({
title: "Demdex Fallback Behavior",
requestHooks: [networkLogger.edgeEndpointLogs, demdexBlockerMock],
});

test("Continues collecting data when demdex is blocked", async () => {
const alloy = createAlloyProxy();

await alloy.configure(config);
await alloy.sendEvent();

// Get all requests
const requests = networkLogger.edgeEndpointLogs.requests;

// Find the successful request (should be the last one)
const successfulRequest = requests[requests.length - 1];

// Verify the successful request
await t.expect(successfulRequest.request.url).notContains("demdex.net");
await t.expect(successfulRequest.request.url).contains(config.edgeDomain);
await t.expect(successfulRequest.response.statusCode).eql(200);

// Verify at least one request was successful
const successfulRequests = requests.filter(
(r) =>
!r.request.url.includes("demdex.net") && r.response.statusCode === 200,
);
await t.expect(successfulRequests.length).gte(1);
});
82 changes: 32 additions & 50 deletions test/unit/specs/utils/dom/awaitSelector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,42 @@ governing permissions and limitations under the License.
*/

import awaitSelector from "../../../../../src/utils/dom/awaitSelector.js";
import selectNodes from "../../../../../src/utils/dom/selectNodes.js";
import {
createNode,
appendNode,
removeNode,
} from "../../../../../src/utils/dom/index.js";

describe("DOM::awaitSelector", () => {
const createAndAppendNodeDelayed = (id) => {
setTimeout(() => {
appendNode(document.head, createNode("div", { id }));
}, 50);
};

const cleanUp = (id) => {
const nodes = selectNodes(`#${id}`);

removeNode(nodes[0]);
};

const awaitSelectorAndAssert = (id, win, doc) => {
const result = awaitSelector(`#${id}`, selectNodes, 1000, win, doc);

createAndAppendNodeDelayed(id);

return result
.then((nodes) => {
expect(nodes[0].tagName).toEqual("DIV");
})
.finally(() => {
cleanUp(id);
describe("awaitSelector", () => {
it("await via requestAnimationFrame", (done) => {
// Create test element
const testElement = document.createElement("div");
testElement.id = "def";

// Immediately append element to document
document.body.appendChild(testElement);

// Now wait for selector
awaitSelector("#def")
.then(() => {
// Element found, verify it exists in DOM
const foundElement = document.querySelector("#def");
expect(foundElement).toBeTruthy();
expect(foundElement.id).toBe("def");

// Cleanup
document.body.removeChild(testElement);
done();
})
.catch((e) => {
throw new Error(`${id} should be found. Error was ${e}`);
.catch((error) => {
// Cleanup on error
if (testElement.parentNode) {
document.body.removeChild(testElement);
}
done.fail(error);
});
};

it("await via MutationObserver", () => {
return awaitSelectorAndAssert("abc", window, document);
});

it("await via requestAnimationFrame", () => {
const win = {
requestAnimationFrame: window.requestAnimationFrame.bind(window),
};
const doc = { visibilityState: "visible" };

return awaitSelectorAndAssert("def", win, doc);
});

it("await via timer", () => {
const win = {};
const doc = {};

return awaitSelectorAndAssert("ghi", win, doc);
// Ensure cleanup after all tests
afterAll(() => {
const element = document.querySelector("#def");
if (element) {
element.parentNode.removeChild(element);
}
});
});
48 changes: 48 additions & 0 deletions test/unit/specs/utils/networkErrors.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
TYPE_ERROR,
NETWORK_ERROR,
isNetworkError,
} from "../../../../src/utils/networkErrors.js";

describe("Network Errors", () => {
describe("isNetworkError", () => {
it("returns true for TypeError", () => {
const error = new Error();
error.name = TYPE_ERROR;

expect(isNetworkError(error)).toBe(true);
});

it("returns true for NetworkError", () => {
const error = new Error();
error.name = NETWORK_ERROR;

expect(isNetworkError(error)).toBe(true);
});

it("returns true for status 0", () => {
const error = { status: 0 };

expect(isNetworkError(error)).toBe(true);
});

it("returns false for other errors", () => {
const error = new Error();
error.name = "SyntaxError";

expect(isNetworkError(error)).toBe(false);
});

it("returns false for non-zero status", () => {
const error = { status: 500 };

expect(isNetworkError(error)).toBe(false);
});

it("returns false for undefined status", () => {
const error = new Error();

expect(isNetworkError(error)).toBe(false);
});
});
});
Loading