@datadog/browser-core
Advanced tools
Comparing version 4.33.0 to 4.34.0
@@ -9,3 +9,3 @@ "use strict"; | ||
var publicApi = (0, utils_1.assign)({ | ||
version: "4.33.0", | ||
version: "4.34.0", | ||
// This API method is intentionally not monitored, since the only thing executed is the | ||
@@ -12,0 +12,0 @@ // user-provided 'callback'. All SDK usages executed in the callback should be monitored, and |
@@ -22,2 +22,6 @@ import type { CookieOptions } from '../../browser/cookie'; | ||
trackLongTasks?: boolean | undefined; | ||
proxy?: string | undefined; | ||
/** | ||
* @deprecated use `proxy` instead | ||
*/ | ||
proxyUrl?: string | undefined; | ||
@@ -24,0 +28,0 @@ site?: string | undefined; |
@@ -79,5 +79,6 @@ "use strict"; | ||
function serializeConfiguration(configuration) { | ||
var _a; | ||
var _a, _b; | ||
var proxy = (_a = configuration.proxy) !== null && _a !== void 0 ? _a : configuration.proxyUrl; | ||
return { | ||
session_sample_rate: (_a = configuration.sessionSampleRate) !== null && _a !== void 0 ? _a : configuration.sampleRate, | ||
session_sample_rate: (_b = configuration.sessionSampleRate) !== null && _b !== void 0 ? _b : configuration.sampleRate, | ||
telemetry_sample_rate: configuration.telemetrySampleRate, | ||
@@ -88,3 +89,3 @@ telemetry_configuration_sample_rate: configuration.telemetryConfigurationSampleRate, | ||
use_secure_session_cookie: configuration.useSecureSessionCookie, | ||
use_proxy: configuration.proxyUrl !== undefined ? !!configuration.proxyUrl : undefined, | ||
use_proxy: proxy !== undefined ? !!proxy : undefined, | ||
silent_multiple_init: configuration.silentMultipleInit, | ||
@@ -91,0 +92,0 @@ track_session_across_subdomains: configuration.trackSessionAcrossSubdomains, |
@@ -12,4 +12,4 @@ import type { RetryInfo } from '../../transport'; | ||
build(api: 'xhr' | 'fetch' | 'beacon', retry?: RetryInfo | undefined): string; | ||
buildIntakeUrl(): string; | ||
urlPrefix: string; | ||
endpointType: "logs" | "rum" | "sessionReplay"; | ||
}; |
@@ -19,32 +19,9 @@ "use strict"; | ||
function createEndpointBuilder(initConfiguration, endpointType, configurationTags) { | ||
var clientToken = initConfiguration.clientToken; | ||
var host = buildEndpointHost(initConfiguration, endpointType); | ||
var baseUrl = "https://".concat(host, "/api/v2/").concat(INTAKE_TRACKS[endpointType]); | ||
var proxyUrl = initConfiguration.proxyUrl && (0, urlPolyfill_1.normalizeUrl)(initConfiguration.proxyUrl); | ||
var buildUrlWithParameters = createEndpointUrlWithParametersBuilder(initConfiguration, endpointType); | ||
return { | ||
build: function (api, retry) { | ||
var tags = ["sdk_version:".concat("4.33.0"), "api:".concat(api)].concat(configurationTags); | ||
if (retry) { | ||
tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus)); | ||
} | ||
var parameters = [ | ||
'ddsource=browser', | ||
"ddtags=".concat(encodeURIComponent(tags.join(','))), | ||
"dd-api-key=".concat(clientToken), | ||
"dd-evp-origin-version=".concat(encodeURIComponent("4.33.0")), | ||
'dd-evp-origin=browser', | ||
"dd-request-id=".concat((0, utils_1.generateUUID)()), | ||
]; | ||
if (endpointType === 'rum') { | ||
parameters.push("batch_time=".concat((0, timeUtils_1.timeStampNow)())); | ||
} | ||
if (initConfiguration.internalAnalyticsSubdomain) { | ||
parameters.reverse(); | ||
} | ||
var endpointUrl = "".concat(baseUrl, "?").concat(parameters.join('&')); | ||
return proxyUrl ? "".concat(proxyUrl, "?ddforward=").concat(encodeURIComponent(endpointUrl)) : endpointUrl; | ||
var parameters = buildEndpointParameters(initConfiguration, endpointType, configurationTags, api, retry); | ||
return buildUrlWithParameters(parameters); | ||
}, | ||
buildIntakeUrl: function () { | ||
return proxyUrl ? "".concat(proxyUrl, "?ddforward") : baseUrl; | ||
}, | ||
urlPrefix: buildUrlWithParameters(''), | ||
endpointType: endpointType, | ||
@@ -54,2 +31,24 @@ }; | ||
exports.createEndpointBuilder = createEndpointBuilder; | ||
/** | ||
* Create a function used to build a full endpoint url from provided parameters. The goal of this | ||
* function is to pre-compute some parts of the URL to avoid re-computing everything on every | ||
* request, as only parameters are changing. | ||
*/ | ||
function createEndpointUrlWithParametersBuilder(initConfiguration, endpointType) { | ||
var path = "/api/v2/".concat(INTAKE_TRACKS[endpointType]); | ||
var proxy = initConfiguration.proxy, proxyUrl = initConfiguration.proxyUrl; | ||
if (proxy) { | ||
var normalizedProxyUrl_1 = (0, urlPolyfill_1.normalizeUrl)(proxy); | ||
return function (parameters) { return "".concat(normalizedProxyUrl_1, "?ddforward=").concat(encodeURIComponent("".concat(path, "?").concat(parameters))); }; | ||
} | ||
var host = buildEndpointHost(initConfiguration, endpointType); | ||
if (proxy === undefined && proxyUrl) { | ||
// TODO: remove this in a future major. | ||
var normalizedProxyUrl_2 = (0, urlPolyfill_1.normalizeUrl)(proxyUrl); | ||
return function (parameters) { | ||
return "".concat(normalizedProxyUrl_2, "?ddforward=").concat(encodeURIComponent("https://".concat(host).concat(path, "?").concat(parameters))); | ||
}; | ||
} | ||
return function (parameters) { return "https://".concat(host).concat(path, "?").concat(parameters); }; | ||
} | ||
function buildEndpointHost(initConfiguration, endpointType) { | ||
@@ -65,2 +64,28 @@ var _a = initConfiguration.site, site = _a === void 0 ? intakeSites_1.INTAKE_SITE_US1 : _a, internalAnalyticsSubdomain = initConfiguration.internalAnalyticsSubdomain; | ||
} | ||
/** | ||
* Build parameters to be used for an intake request. Parameters should be re-built for each | ||
* request, as they change randomly. | ||
*/ | ||
function buildEndpointParameters(_a, endpointType, configurationTags, api, retry) { | ||
var clientToken = _a.clientToken, internalAnalyticsSubdomain = _a.internalAnalyticsSubdomain; | ||
var tags = ["sdk_version:".concat("4.34.0"), "api:".concat(api)].concat(configurationTags); | ||
if (retry) { | ||
tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus)); | ||
} | ||
var parameters = [ | ||
'ddsource=browser', | ||
"ddtags=".concat(encodeURIComponent(tags.join(','))), | ||
"dd-api-key=".concat(clientToken), | ||
"dd-evp-origin-version=".concat(encodeURIComponent("4.34.0")), | ||
'dd-evp-origin=browser', | ||
"dd-request-id=".concat((0, utils_1.generateUUID)()), | ||
]; | ||
if (endpointType === 'rum') { | ||
parameters.push("batch_time=".concat((0, timeUtils_1.timeStampNow)())); | ||
} | ||
if (internalAnalyticsSubdomain) { | ||
parameters.reverse(); | ||
} | ||
return parameters.join('&'); | ||
} | ||
//# sourceMappingURL=endpointBuilder.js.map |
@@ -11,6 +11,6 @@ "use strict"; | ||
var endpointBuilders = computeEndpointBuilders(initConfiguration, tags); | ||
var intakeEndpoints = (0, utils_1.objectValues)(endpointBuilders).map(function (builder) { return builder.buildIntakeUrl(); }); | ||
var replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeEndpoints, tags); | ||
var intakeUrlPrefixes = (0, utils_1.objectValues)(endpointBuilders).map(function (builder) { return builder.urlPrefix; }); | ||
var replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeUrlPrefixes, tags); | ||
return (0, utils_1.assign)({ | ||
isIntakeUrl: function (url) { return intakeEndpoints.some(function (intakeEndpoint) { return url.indexOf(intakeEndpoint) === 0; }); }, | ||
isIntakeUrl: function (url) { return intakeUrlPrefixes.some(function (intakeEndpoint) { return url.indexOf(intakeEndpoint) === 0; }); }, | ||
replica: replicaConfiguration, | ||
@@ -28,3 +28,3 @@ site: initConfiguration.site || intakeSites_1.INTAKE_SITE_US1, | ||
} | ||
function computeReplicaConfiguration(initConfiguration, intakeEndpoints, tags) { | ||
function computeReplicaConfiguration(initConfiguration, intakeUrlPrefixes, tags) { | ||
if (!initConfiguration.replica) { | ||
@@ -41,5 +41,5 @@ return; | ||
}; | ||
intakeEndpoints.push.apply(intakeEndpoints, (0, utils_1.objectValues)(replicaEndpointBuilders).map(function (builder) { return builder.buildIntakeUrl(); })); | ||
intakeUrlPrefixes.push.apply(intakeUrlPrefixes, (0, utils_1.objectValues)(replicaEndpointBuilders).map(function (builder) { return builder.urlPrefix; })); | ||
return (0, utils_1.assign)({ applicationId: initConfiguration.replica.applicationId }, replicaEndpointBuilders); | ||
} | ||
//# sourceMappingURL=transportConfiguration.js.map |
@@ -47,3 +47,3 @@ "use strict"; | ||
service: telemetryService, | ||
version: "4.33.0", | ||
version: "4.34.0", | ||
source: 'browser', | ||
@@ -50,0 +50,0 @@ _dd: { |
@@ -6,3 +6,3 @@ import { catchUserErrors } from '../tools/catchUserErrors'; | ||
var publicApi = assign({ | ||
version: "4.33.0", | ||
version: "4.34.0", | ||
// This API method is intentionally not monitored, since the only thing executed is the | ||
@@ -9,0 +9,0 @@ // user-provided 'callback'. All SDK usages executed in the callback should be monitored, and |
@@ -22,2 +22,6 @@ import type { CookieOptions } from '../../browser/cookie'; | ||
trackLongTasks?: boolean | undefined; | ||
proxy?: string | undefined; | ||
/** | ||
* @deprecated use `proxy` instead | ||
*/ | ||
proxyUrl?: string | undefined; | ||
@@ -24,0 +28,0 @@ site?: string | undefined; |
@@ -74,5 +74,6 @@ import { getCurrentSite } from '../../browser/cookie'; | ||
export function serializeConfiguration(configuration) { | ||
var _a; | ||
var _a, _b; | ||
var proxy = (_a = configuration.proxy) !== null && _a !== void 0 ? _a : configuration.proxyUrl; | ||
return { | ||
session_sample_rate: (_a = configuration.sessionSampleRate) !== null && _a !== void 0 ? _a : configuration.sampleRate, | ||
session_sample_rate: (_b = configuration.sessionSampleRate) !== null && _b !== void 0 ? _b : configuration.sampleRate, | ||
telemetry_sample_rate: configuration.telemetrySampleRate, | ||
@@ -83,3 +84,3 @@ telemetry_configuration_sample_rate: configuration.telemetryConfigurationSampleRate, | ||
use_secure_session_cookie: configuration.useSecureSessionCookie, | ||
use_proxy: configuration.proxyUrl !== undefined ? !!configuration.proxyUrl : undefined, | ||
use_proxy: proxy !== undefined ? !!proxy : undefined, | ||
silent_multiple_init: configuration.silentMultipleInit, | ||
@@ -86,0 +87,0 @@ track_session_across_subdomains: configuration.trackSessionAcrossSubdomains, |
@@ -12,4 +12,4 @@ import type { RetryInfo } from '../../transport'; | ||
build(api: 'xhr' | 'fetch' | 'beacon', retry?: RetryInfo | undefined): string; | ||
buildIntakeUrl(): string; | ||
urlPrefix: string; | ||
endpointType: "logs" | "rum" | "sessionReplay"; | ||
}; |
@@ -16,35 +16,34 @@ import { timeStampNow } from '../../tools/timeUtils'; | ||
export function createEndpointBuilder(initConfiguration, endpointType, configurationTags) { | ||
var clientToken = initConfiguration.clientToken; | ||
var host = buildEndpointHost(initConfiguration, endpointType); | ||
var baseUrl = "https://".concat(host, "/api/v2/").concat(INTAKE_TRACKS[endpointType]); | ||
var proxyUrl = initConfiguration.proxyUrl && normalizeUrl(initConfiguration.proxyUrl); | ||
var buildUrlWithParameters = createEndpointUrlWithParametersBuilder(initConfiguration, endpointType); | ||
return { | ||
build: function (api, retry) { | ||
var tags = ["sdk_version:".concat("4.33.0"), "api:".concat(api)].concat(configurationTags); | ||
if (retry) { | ||
tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus)); | ||
} | ||
var parameters = [ | ||
'ddsource=browser', | ||
"ddtags=".concat(encodeURIComponent(tags.join(','))), | ||
"dd-api-key=".concat(clientToken), | ||
"dd-evp-origin-version=".concat(encodeURIComponent("4.33.0")), | ||
'dd-evp-origin=browser', | ||
"dd-request-id=".concat(generateUUID()), | ||
]; | ||
if (endpointType === 'rum') { | ||
parameters.push("batch_time=".concat(timeStampNow())); | ||
} | ||
if (initConfiguration.internalAnalyticsSubdomain) { | ||
parameters.reverse(); | ||
} | ||
var endpointUrl = "".concat(baseUrl, "?").concat(parameters.join('&')); | ||
return proxyUrl ? "".concat(proxyUrl, "?ddforward=").concat(encodeURIComponent(endpointUrl)) : endpointUrl; | ||
var parameters = buildEndpointParameters(initConfiguration, endpointType, configurationTags, api, retry); | ||
return buildUrlWithParameters(parameters); | ||
}, | ||
buildIntakeUrl: function () { | ||
return proxyUrl ? "".concat(proxyUrl, "?ddforward") : baseUrl; | ||
}, | ||
urlPrefix: buildUrlWithParameters(''), | ||
endpointType: endpointType, | ||
}; | ||
} | ||
/** | ||
* Create a function used to build a full endpoint url from provided parameters. The goal of this | ||
* function is to pre-compute some parts of the URL to avoid re-computing everything on every | ||
* request, as only parameters are changing. | ||
*/ | ||
function createEndpointUrlWithParametersBuilder(initConfiguration, endpointType) { | ||
var path = "/api/v2/".concat(INTAKE_TRACKS[endpointType]); | ||
var proxy = initConfiguration.proxy, proxyUrl = initConfiguration.proxyUrl; | ||
if (proxy) { | ||
var normalizedProxyUrl_1 = normalizeUrl(proxy); | ||
return function (parameters) { return "".concat(normalizedProxyUrl_1, "?ddforward=").concat(encodeURIComponent("".concat(path, "?").concat(parameters))); }; | ||
} | ||
var host = buildEndpointHost(initConfiguration, endpointType); | ||
if (proxy === undefined && proxyUrl) { | ||
// TODO: remove this in a future major. | ||
var normalizedProxyUrl_2 = normalizeUrl(proxyUrl); | ||
return function (parameters) { | ||
return "".concat(normalizedProxyUrl_2, "?ddforward=").concat(encodeURIComponent("https://".concat(host).concat(path, "?").concat(parameters))); | ||
}; | ||
} | ||
return function (parameters) { return "https://".concat(host).concat(path, "?").concat(parameters); }; | ||
} | ||
function buildEndpointHost(initConfiguration, endpointType) { | ||
@@ -60,2 +59,28 @@ var _a = initConfiguration.site, site = _a === void 0 ? INTAKE_SITE_US1 : _a, internalAnalyticsSubdomain = initConfiguration.internalAnalyticsSubdomain; | ||
} | ||
/** | ||
* Build parameters to be used for an intake request. Parameters should be re-built for each | ||
* request, as they change randomly. | ||
*/ | ||
function buildEndpointParameters(_a, endpointType, configurationTags, api, retry) { | ||
var clientToken = _a.clientToken, internalAnalyticsSubdomain = _a.internalAnalyticsSubdomain; | ||
var tags = ["sdk_version:".concat("4.34.0"), "api:".concat(api)].concat(configurationTags); | ||
if (retry) { | ||
tags.push("retry_count:".concat(retry.count), "retry_after:".concat(retry.lastFailureStatus)); | ||
} | ||
var parameters = [ | ||
'ddsource=browser', | ||
"ddtags=".concat(encodeURIComponent(tags.join(','))), | ||
"dd-api-key=".concat(clientToken), | ||
"dd-evp-origin-version=".concat(encodeURIComponent("4.34.0")), | ||
'dd-evp-origin=browser', | ||
"dd-request-id=".concat(generateUUID()), | ||
]; | ||
if (endpointType === 'rum') { | ||
parameters.push("batch_time=".concat(timeStampNow())); | ||
} | ||
if (internalAnalyticsSubdomain) { | ||
parameters.reverse(); | ||
} | ||
return parameters.join('&'); | ||
} | ||
//# sourceMappingURL=endpointBuilder.js.map |
@@ -8,6 +8,6 @@ import { assign, objectValues } from '../../tools/utils'; | ||
var endpointBuilders = computeEndpointBuilders(initConfiguration, tags); | ||
var intakeEndpoints = objectValues(endpointBuilders).map(function (builder) { return builder.buildIntakeUrl(); }); | ||
var replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeEndpoints, tags); | ||
var intakeUrlPrefixes = objectValues(endpointBuilders).map(function (builder) { return builder.urlPrefix; }); | ||
var replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeUrlPrefixes, tags); | ||
return assign({ | ||
isIntakeUrl: function (url) { return intakeEndpoints.some(function (intakeEndpoint) { return url.indexOf(intakeEndpoint) === 0; }); }, | ||
isIntakeUrl: function (url) { return intakeUrlPrefixes.some(function (intakeEndpoint) { return url.indexOf(intakeEndpoint) === 0; }); }, | ||
replica: replicaConfiguration, | ||
@@ -24,3 +24,3 @@ site: initConfiguration.site || INTAKE_SITE_US1, | ||
} | ||
function computeReplicaConfiguration(initConfiguration, intakeEndpoints, tags) { | ||
function computeReplicaConfiguration(initConfiguration, intakeUrlPrefixes, tags) { | ||
if (!initConfiguration.replica) { | ||
@@ -37,5 +37,5 @@ return; | ||
}; | ||
intakeEndpoints.push.apply(intakeEndpoints, objectValues(replicaEndpointBuilders).map(function (builder) { return builder.buildIntakeUrl(); })); | ||
intakeUrlPrefixes.push.apply(intakeUrlPrefixes, objectValues(replicaEndpointBuilders).map(function (builder) { return builder.urlPrefix; })); | ||
return assign({ applicationId: initConfiguration.replica.applicationId }, replicaEndpointBuilders); | ||
} | ||
//# sourceMappingURL=transportConfiguration.js.map |
@@ -44,3 +44,3 @@ import { ConsoleApiName } from '../../tools/display'; | ||
service: telemetryService, | ||
version: "4.33.0", | ||
version: "4.34.0", | ||
source: 'browser', | ||
@@ -47,0 +47,0 @@ _dd: { |
{ | ||
"name": "@datadog/browser-core", | ||
"version": "4.33.0", | ||
"version": "4.34.0", | ||
"license": "Apache-2.0", | ||
@@ -24,3 +24,3 @@ "main": "cjs/index.js", | ||
}, | ||
"gitHead": "09cba227ece772311f6a00f96fd27705acf5c118" | ||
"gitHead": "e592cccb3f008ad1bffb649b586f6ca802c46558" | ||
} |
@@ -33,2 +33,6 @@ import type { CookieOptions } from '../../browser/cookie' | ||
// transport options | ||
proxy?: string | undefined | ||
/** | ||
* @deprecated use `proxy` instead | ||
*/ | ||
proxyUrl?: string | undefined | ||
@@ -168,2 +172,3 @@ site?: string | undefined | ||
export function serializeConfiguration(configuration: InitConfiguration): Partial<RawTelemetryConfiguration> { | ||
const proxy = configuration.proxy ?? configuration.proxyUrl | ||
return { | ||
@@ -176,3 +181,3 @@ session_sample_rate: configuration.sessionSampleRate ?? configuration.sampleRate, | ||
use_secure_session_cookie: configuration.useSecureSessionCookie, | ||
use_proxy: configuration.proxyUrl !== undefined ? !!configuration.proxyUrl : undefined, | ||
use_proxy: proxy !== undefined ? !!proxy : undefined, | ||
silent_multiple_init: configuration.silentMultipleInit, | ||
@@ -179,0 +184,0 @@ track_session_across_subdomains: configuration.trackSessionAcrossSubdomains, |
import type { BuildEnvWindow } from '../../../test/specHelper' | ||
import { startsWith } from '../../tools/utils' | ||
import type { InitConfiguration } from './configuration' | ||
@@ -39,3 +40,43 @@ import { createEndpointBuilder } from './endpointBuilder' | ||
describe('proxyUrl', () => { | ||
describe('proxy configuration', () => { | ||
it('should replace the intake endpoint by the proxy and set the intake path and parameters in the attribute ddforward', () => { | ||
expect( | ||
createEndpointBuilder({ ...initConfiguration, proxy: 'https://proxy.io/path' }, 'rum', []).build('xhr') | ||
).toMatch( | ||
`https://proxy.io/path\\?ddforward=${encodeURIComponent( | ||
`/api/v2/rum?ddsource=(.*)&ddtags=(.*)&dd-api-key=${clientToken}` + | ||
'&dd-evp-origin-version=(.*)&dd-evp-origin=browser&dd-request-id=(.*)&batch_time=(.*)' | ||
)}` | ||
) | ||
}) | ||
it('normalizes the proxy url', () => { | ||
expect( | ||
startsWith( | ||
createEndpointBuilder({ ...initConfiguration, proxy: '/path' }, 'rum', []).build('xhr'), | ||
`${location.origin}/path?ddforward` | ||
) | ||
).toBeTrue() | ||
}) | ||
it('uses `proxy` over `proxyUrl`', () => { | ||
expect( | ||
createEndpointBuilder( | ||
{ ...initConfiguration, proxy: 'https://proxy.io/path', proxyUrl: 'https://legacy-proxy.io/path' }, | ||
'rum', | ||
[] | ||
).build('xhr') | ||
).toMatch(/^https:\/\/proxy.io\/path\?/) | ||
expect( | ||
createEndpointBuilder( | ||
{ ...initConfiguration, proxy: false as any, proxyUrl: 'https://legacy-proxy.io/path' }, | ||
'rum', | ||
[] | ||
).build('xhr') | ||
).toMatch(/^https:\/\/rum.browser-intake-datadoghq.com\//) | ||
}) | ||
}) | ||
describe('deprecated proxyUrl configuration', () => { | ||
it('should replace the full intake endpoint by the proxyUrl and set it in the attribute ddforward', () => { | ||
@@ -51,2 +92,11 @@ expect( | ||
}) | ||
it('normalizes the proxy url', () => { | ||
expect( | ||
startsWith( | ||
createEndpointBuilder({ ...initConfiguration, proxyUrl: '/path' }, 'rum', []).build('xhr'), | ||
`${location.origin}/path?ddforward` | ||
) | ||
).toBeTrue() | ||
}) | ||
}) | ||
@@ -53,0 +103,0 @@ |
@@ -32,36 +32,10 @@ import type { RetryInfo } from '../../transport' | ||
) { | ||
const { clientToken } = initConfiguration | ||
const buildUrlWithParameters = createEndpointUrlWithParametersBuilder(initConfiguration, endpointType) | ||
const host = buildEndpointHost(initConfiguration, endpointType) | ||
const baseUrl = `https://${host}/api/v2/${INTAKE_TRACKS[endpointType]}` | ||
const proxyUrl = initConfiguration.proxyUrl && normalizeUrl(initConfiguration.proxyUrl) | ||
return { | ||
build(api: 'xhr' | 'fetch' | 'beacon', retry?: RetryInfo) { | ||
const tags = [`sdk_version:${__BUILD_ENV__SDK_VERSION__}`, `api:${api}`].concat(configurationTags) | ||
if (retry) { | ||
tags.push(`retry_count:${retry.count}`, `retry_after:${retry.lastFailureStatus}`) | ||
} | ||
const parameters = [ | ||
'ddsource=browser', | ||
`ddtags=${encodeURIComponent(tags.join(','))}`, | ||
`dd-api-key=${clientToken}`, | ||
`dd-evp-origin-version=${encodeURIComponent(__BUILD_ENV__SDK_VERSION__)}`, | ||
'dd-evp-origin=browser', | ||
`dd-request-id=${generateUUID()}`, | ||
] | ||
if (endpointType === 'rum') { | ||
parameters.push(`batch_time=${timeStampNow()}`) | ||
} | ||
if (initConfiguration.internalAnalyticsSubdomain) { | ||
parameters.reverse() | ||
} | ||
const endpointUrl = `${baseUrl}?${parameters.join('&')}` | ||
return proxyUrl ? `${proxyUrl}?ddforward=${encodeURIComponent(endpointUrl)}` : endpointUrl | ||
const parameters = buildEndpointParameters(initConfiguration, endpointType, configurationTags, api, retry) | ||
return buildUrlWithParameters(parameters) | ||
}, | ||
buildIntakeUrl() { | ||
return proxyUrl ? `${proxyUrl}?ddforward` : baseUrl | ||
}, | ||
urlPrefix: buildUrlWithParameters(''), | ||
endpointType, | ||
@@ -71,2 +45,31 @@ } | ||
/** | ||
* Create a function used to build a full endpoint url from provided parameters. The goal of this | ||
* function is to pre-compute some parts of the URL to avoid re-computing everything on every | ||
* request, as only parameters are changing. | ||
*/ | ||
function createEndpointUrlWithParametersBuilder( | ||
initConfiguration: InitConfiguration, | ||
endpointType: EndpointType | ||
): (parameters: string) => string { | ||
const path = `/api/v2/${INTAKE_TRACKS[endpointType]}` | ||
const { proxy, proxyUrl } = initConfiguration | ||
if (proxy) { | ||
const normalizedProxyUrl = normalizeUrl(proxy) | ||
return (parameters) => `${normalizedProxyUrl}?ddforward=${encodeURIComponent(`${path}?${parameters}`)}` | ||
} | ||
const host = buildEndpointHost(initConfiguration, endpointType) | ||
if (proxy === undefined && proxyUrl) { | ||
// TODO: remove this in a future major. | ||
const normalizedProxyUrl = normalizeUrl(proxyUrl) | ||
return (parameters) => | ||
`${normalizedProxyUrl}?ddforward=${encodeURIComponent(`https://${host}${path}?${parameters}`)}` | ||
} | ||
return (parameters) => `https://${host}${path}?${parameters}` | ||
} | ||
function buildEndpointHost(initConfiguration: InitConfiguration, endpointType: EndpointType) { | ||
@@ -84,1 +87,35 @@ const { site = INTAKE_SITE_US1, internalAnalyticsSubdomain } = initConfiguration | ||
} | ||
/** | ||
* Build parameters to be used for an intake request. Parameters should be re-built for each | ||
* request, as they change randomly. | ||
*/ | ||
function buildEndpointParameters( | ||
{ clientToken, internalAnalyticsSubdomain }: InitConfiguration, | ||
endpointType: EndpointType, | ||
configurationTags: string[], | ||
api: 'xhr' | 'fetch' | 'beacon', | ||
retry: RetryInfo | undefined | ||
) { | ||
const tags = [`sdk_version:${__BUILD_ENV__SDK_VERSION__}`, `api:${api}`].concat(configurationTags) | ||
if (retry) { | ||
tags.push(`retry_count:${retry.count}`, `retry_after:${retry.lastFailureStatus}`) | ||
} | ||
const parameters = [ | ||
'ddsource=browser', | ||
`ddtags=${encodeURIComponent(tags.join(','))}`, | ||
`dd-api-key=${clientToken}`, | ||
`dd-evp-origin-version=${encodeURIComponent(__BUILD_ENV__SDK_VERSION__)}`, | ||
'dd-evp-origin=browser', | ||
`dd-request-id=${generateUUID()}`, | ||
] | ||
if (endpointType === 'rum') { | ||
parameters.push(`batch_time=${timeStampNow()}`) | ||
} | ||
if (internalAnalyticsSubdomain) { | ||
parameters.reverse() | ||
} | ||
return parameters.join('&') | ||
} |
@@ -100,15 +100,42 @@ import { computeTransportConfiguration } from './transportConfiguration' | ||
}) | ||
;[ | ||
{ | ||
proxyConfigurationName: 'proxy' as const, | ||
intakeUrl: '/api/v2/rum', | ||
}, | ||
{ | ||
proxyConfigurationName: 'proxyUrl' as const, | ||
intakeUrl: 'https://rum.browser-intake-datadoghq.com/api/v2/rum', | ||
}, | ||
].forEach(({ proxyConfigurationName, intakeUrl }) => { | ||
describe(`${proxyConfigurationName} configuration`, () => { | ||
it('should detect proxy intake request', () => { | ||
let configuration = computeTransportConfiguration({ | ||
clientToken, | ||
[proxyConfigurationName]: 'https://www.proxy.com', | ||
}) | ||
expect( | ||
configuration.isIntakeUrl(`https://www.proxy.com/?ddforward=${encodeURIComponent(`${intakeUrl}?foo=bar`)}`) | ||
).toBe(true) | ||
it('should detect proxy intake request', () => { | ||
let configuration = computeTransportConfiguration({ clientToken, proxyUrl: 'https://www.proxy.com' }) | ||
expect(configuration.isIntakeUrl('https://www.proxy.com/?ddforward=xxx')).toBe(true) | ||
configuration = computeTransportConfiguration({ | ||
clientToken, | ||
[proxyConfigurationName]: 'https://www.proxy.com/custom/path', | ||
}) | ||
expect( | ||
configuration.isIntakeUrl( | ||
`https://www.proxy.com/custom/path?ddforward=${encodeURIComponent(`${intakeUrl}?foo=bar`)}` | ||
) | ||
).toBe(true) | ||
}) | ||
configuration = computeTransportConfiguration({ clientToken, proxyUrl: 'https://www.proxy.com/custom/path' }) | ||
expect(configuration.isIntakeUrl('https://www.proxy.com/custom/path?ddforward=xxx')).toBe(true) | ||
it('should not detect request done on the same host as the proxy', () => { | ||
const configuration = computeTransportConfiguration({ | ||
clientToken, | ||
[proxyConfigurationName]: 'https://www.proxy.com', | ||
}) | ||
expect(configuration.isIntakeUrl('https://www.proxy.com/foo')).toBe(false) | ||
}) | ||
}) | ||
}) | ||
it('should not detect request done on the same host as the proxy', () => { | ||
const configuration = computeTransportConfiguration({ clientToken, proxyUrl: 'https://www.proxy.com' }) | ||
expect(configuration.isIntakeUrl('https://www.proxy.com/foo')).toBe(false) | ||
}) | ||
;[ | ||
@@ -115,0 +142,0 @@ { site: 'datadoghq.eu' }, |
@@ -27,9 +27,9 @@ import { assign, objectValues } from '../../tools/utils' | ||
const endpointBuilders = computeEndpointBuilders(initConfiguration, tags) | ||
const intakeEndpoints = objectValues(endpointBuilders).map((builder) => builder.buildIntakeUrl()) | ||
const intakeUrlPrefixes = objectValues(endpointBuilders).map((builder) => builder.urlPrefix) | ||
const replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeEndpoints, tags) | ||
const replicaConfiguration = computeReplicaConfiguration(initConfiguration, intakeUrlPrefixes, tags) | ||
return assign( | ||
{ | ||
isIntakeUrl: (url: string) => intakeEndpoints.some((intakeEndpoint) => url.indexOf(intakeEndpoint) === 0), | ||
isIntakeUrl: (url: string) => intakeUrlPrefixes.some((intakeEndpoint) => url.indexOf(intakeEndpoint) === 0), | ||
replica: replicaConfiguration, | ||
@@ -52,3 +52,3 @@ site: initConfiguration.site || INTAKE_SITE_US1, | ||
initConfiguration: InitConfiguration, | ||
intakeEndpoints: string[], | ||
intakeUrlPrefixes: string[], | ||
tags: string[] | ||
@@ -70,5 +70,5 @@ ): ReplicaConfiguration | undefined { | ||
intakeEndpoints.push(...objectValues(replicaEndpointBuilders).map((builder) => builder.buildIntakeUrl())) | ||
intakeUrlPrefixes.push(...objectValues(replicaEndpointBuilders).map((builder) => builder.urlPrefix)) | ||
return assign({ applicationId: initConfiguration.replica.applicationId }, replicaEndpointBuilders) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1031074
20633