Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sitecore-cloudsdk/core

Package Overview
Dependencies
Maintainers
3
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sitecore-cloudsdk/core - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1-rc.0

dist/cjs/src/lib/cookie/create-cookies.d.ts

2

dist/cjs/package.json
{
"name": "@sitecore-cloudsdk/core",
"version": "0.3.0",
"version": "0.3.1-rc.0",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js",

@@ -1,3 +0,3 @@

export { createCookie } from './lib/cookie/create-cookie';
export { getBrowserIdFromMiddlewareRequest } from './lib/cookie/get-browser-id-from-middleware-request';
export { createCookies } from './lib/cookie/create-cookies';
export { getCookieValueFromMiddlewareRequest } from './lib/cookie/get-cookie-value-from-middleware-request';
export { getDefaultCookieAttributes } from './lib/cookie/get-default-cookie-attributes';

@@ -15,8 +15,8 @@ export { handleHttpCookie } from './lib/cookie/handle-http-cookie';

export { language, pageName } from './lib/infer/infer';
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request';
export { getCookieValueFromRequest } from './lib/cookie/get-cookie-value-from-request';
export { handleGetSettingsError } from './lib/settings/handle-get-settings-error';
export { generateCorrelationId } from './lib/correlation-id/generate-correlation-id';
export { debug } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER, } from './lib/consts';
export type { EPResponse, Infer } from './lib/interfaces';
export type { CookieSettings, Settings, BrowserSettings, ServerSettings, BasicSettings, } from './lib/settings/interfaces';
export { debug, processDebugResponse } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER } from './lib/consts';
export type { EPResponse, Infer, DebugResponse } from './lib/interfaces';
export type { CookieSettings, Settings, BrowserSettings, ServerSettings, BasicSettings } from './lib/settings/interfaces';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CORRELATION_ID_HEADER = exports.SITECORE_EDGE_URL = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.COOKIE_NAME_PREFIX = exports.API_VERSION = exports.processDebugResponse = exports.debug = exports.generateCorrelationId = exports.handleGetSettingsError = exports.getCookieValueFromRequest = exports.pageName = exports.language = exports.validateSettings = exports.initCoreServer = exports.getSettingsServer = exports.initCore = exports.getSettings = exports.createSettings = exports.getGuestId = exports.fetchBrowserIdFromEdgeProxy = exports.getBrowserId = exports.handleServerCookie = exports.handleNextJsMiddlewareCookie = exports.handleHttpCookie = exports.getDefaultCookieAttributes = exports.getCookieValueFromMiddlewareRequest = exports.createCookies = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
Object.defineProperty(exports, "__esModule", { value: true });
exports.CORRELATION_ID_HEADER = exports.SITECORE_EDGE_URL = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.COOKIE_NAME_PREFIX = exports.API_VERSION = exports.debug = exports.generateCorrelationId = exports.handleGetSettingsError = exports.getBrowserIdFromRequest = exports.pageName = exports.language = exports.validateSettings = exports.initCoreServer = exports.getSettingsServer = exports.initCore = exports.getSettings = exports.createSettings = exports.getGuestId = exports.fetchBrowserIdFromEdgeProxy = exports.getBrowserId = exports.handleServerCookie = exports.handleNextJsMiddlewareCookie = exports.handleHttpCookie = exports.getDefaultCookieAttributes = exports.getBrowserIdFromMiddlewareRequest = exports.createCookie = void 0;
var create_cookie_1 = require("./lib/cookie/create-cookie");
Object.defineProperty(exports, "createCookie", { enumerable: true, get: function () { return create_cookie_1.createCookie; } });
var get_browser_id_from_middleware_request_1 = require("./lib/cookie/get-browser-id-from-middleware-request");
Object.defineProperty(exports, "getBrowserIdFromMiddlewareRequest", { enumerable: true, get: function () { return get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest; } });
var create_cookies_1 = require("./lib/cookie/create-cookies");
Object.defineProperty(exports, "createCookies", { enumerable: true, get: function () { return create_cookies_1.createCookies; } });
var get_cookie_value_from_middleware_request_1 = require("./lib/cookie/get-cookie-value-from-middleware-request");
Object.defineProperty(exports, "getCookieValueFromMiddlewareRequest", { enumerable: true, get: function () { return get_cookie_value_from_middleware_request_1.getCookieValueFromMiddlewareRequest; } });
var get_default_cookie_attributes_1 = require("./lib/cookie/get-default-cookie-attributes");

@@ -36,4 +36,4 @@ Object.defineProperty(exports, "getDefaultCookieAttributes", { enumerable: true, get: function () { return get_default_cookie_attributes_1.getDefaultCookieAttributes; } });

Object.defineProperty(exports, "pageName", { enumerable: true, get: function () { return infer_1.pageName; } });
var get_browser_id_from_request_1 = require("./lib/cookie/get-browser-id-from-request");
Object.defineProperty(exports, "getBrowserIdFromRequest", { enumerable: true, get: function () { return get_browser_id_from_request_1.getBrowserIdFromRequest; } });
var get_cookie_value_from_request_1 = require("./lib/cookie/get-cookie-value-from-request");
Object.defineProperty(exports, "getCookieValueFromRequest", { enumerable: true, get: function () { return get_cookie_value_from_request_1.getCookieValueFromRequest; } });
var handle_get_settings_error_1 = require("./lib/settings/handle-get-settings-error");

@@ -45,2 +45,3 @@ Object.defineProperty(exports, "handleGetSettingsError", { enumerable: true, get: function () { return handle_get_settings_error_1.handleGetSettingsError; } });

Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return debug_1.debug; } });
Object.defineProperty(exports, "processDebugResponse", { enumerable: true, get: function () { return debug_1.processDebugResponse; } });
var consts_1 = require("./lib/consts");

@@ -47,0 +48,0 @@ Object.defineProperty(exports, "API_VERSION", { enumerable: true, get: function () { return consts_1.API_VERSION; } });

@@ -9,4 +9,5 @@ export declare const LIBRARY_VERSION: string;

export declare enum ErrorMessages {
IE_0003 = "[IE-0003] Unable to set the cookie because the browser ID could not be retrieved from the server. Try again later, or use try-catch blocks to handle this error.",
IE_0003 = "[IE-0003] Unable to set the \"sc_{SitecoreEdgeContextId}\" cookie because the browser ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.",
IE_0008 = "[IE-0008] You must first initialize the \"core\" package. Run the \"init\" function.",
IE_0011 = "[IE-0011] Unable to set the \"sc_{SitecoreEdgeContextId}_personalize\" cookie because the guest ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.",
IV_0001 = "[IV-0001] Incorrect value for \"sitecoreEdgeUrl\". Set the value to a valid URL string.",

@@ -13,0 +14,0 @@ MV_0001 = "[MV-0001] \"sitecoreEdgeContextId\" is required.",

@@ -17,6 +17,8 @@ "use strict";

exports.CORRELATION_ID_HEADER = 'x-sc-correlation-id';
/* eslint-disable max-len */
var ErrorMessages;
(function (ErrorMessages) {
ErrorMessages["IE_0003"] = "[IE-0003] Unable to set the cookie because the browser ID could not be retrieved from the server. Try again later, or use try-catch blocks to handle this error.";
ErrorMessages["IE_0003"] = "[IE-0003] Unable to set the \"sc_{SitecoreEdgeContextId}\" cookie because the browser ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
ErrorMessages["IE_0008"] = "[IE-0008] You must first initialize the \"core\" package. Run the \"init\" function.";
ErrorMessages["IE_0011"] = "[IE-0011] Unable to set the \"sc_{SitecoreEdgeContextId}_personalize\" cookie because the guest ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
ErrorMessages["IV_0001"] = "[IV-0001] Incorrect value for \"sitecoreEdgeUrl\". Set the value to a valid URL string.";

@@ -26,1 +28,2 @@ ErrorMessages["MV_0001"] = "[MV-0001] \"sitecoreEdgeContextId\" is required.";

})(ErrorMessages || (exports.ErrorMessages = ErrorMessages = {}));
/* eslint-enable max-len */

@@ -13,4 +13,4 @@ "use strict";

const settings = (0, init_core_1.getSettings)();
return (0, utils_1.getCookieValueClientSide)(settings.cookieSettings.cookieName);
return (0, utils_1.getCookieValueClientSide)(settings.cookieSettings.cookieNames.browserId);
}
exports.getBrowserId = getBrowserId;

@@ -0,1 +1,2 @@

import type { CookieProperties } from '@sitecore-cloudsdk/utils';
/**

@@ -6,3 +7,2 @@ * Gets the default Cookie Attributes

*/
import { CookieProperties } from '@sitecore-cloudsdk/utils';
export declare function getDefaultCookieAttributes(maxAge?: number, cookieDomain?: string): CookieProperties;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultCookieAttributes = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const consts_1 = require("../consts");
/**

@@ -8,6 +11,2 @@ * Gets the default Cookie Attributes

*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultCookieAttributes = void 0;
const consts_1 = require("../consts");
// eslint-disable-next-line max-len
function getDefaultCookieAttributes(maxAge = consts_1.DEFAULT_COOKIE_EXPIRY_DAYS, cookieDomain) {

@@ -19,5 +18,5 @@ return {

sameSite: 'None',
secure: true,
secure: true
};
}
exports.getDefaultCookieAttributes = getDefaultCookieAttributes;
import type { HttpRequest, HttpResponse } from '@sitecore-cloudsdk/utils';
import { Settings } from '../settings/interfaces';
import type { Settings } from '../settings/interfaces';
/**

@@ -4,0 +4,0 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.

@@ -5,5 +5,6 @@ "use strict";

exports.handleHttpCookie = void 0;
const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
const utils_1 = require("@sitecore-cloudsdk/utils");
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy");
const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
const get_guest_id_1 = require("../init/get-guest-id");
/**

@@ -21,20 +22,32 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.

async function handleHttpCookie(request, response, options, timeout) {
const { cookieName } = options.cookieSettings;
const cookieValueFromRequest = request.headers.cookie;
let cookie;
let cookieValue;
if (cookieValueFromRequest) {
cookie = (0, utils_1.getCookieServerSide)(cookieValueFromRequest, cookieName);
if (cookie)
cookieValue = cookie.value;
const { browserId, guestId } = options.cookieSettings.cookieNames;
const browserIdCookie = (0, utils_1.getCookieServerSide)(request.headers.cookie, browserId);
let browserIdCookieValue;
const guestIdCookie = (0, utils_1.getCookieServerSide)(request.headers.cookie, guestId);
let guestIdCookieValue;
const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
if (!browserIdCookie) {
const cookieValues = await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
browserIdCookieValue = cookieValues.browserId;
guestIdCookieValue = cookieValues.guestId;
}
if (!cookieValue)
cookieValue = (await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
.browserId;
const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
const cookieString = (0, utils_1.createCookieString)(cookieName, cookieValue, defaultCookieAttributes);
if (!cookie)
request.headers.cookie = cookieValueFromRequest ? cookieValueFromRequest + '; ' + cookieString : cookieString;
response.setHeader('Set-Cookie', cookieString);
else {
browserIdCookieValue = browserIdCookie.value;
if (!guestIdCookie)
guestIdCookieValue = await (0, get_guest_id_1.getGuestId)(browserIdCookie.value, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
else
guestIdCookieValue = guestIdCookie.value;
}
const browserIdCookieString = (0, utils_1.createCookieString)(browserId, browserIdCookieValue, defaultCookieAttributes);
const guestIdCookieString = (0, utils_1.createCookieString)(guestId, guestIdCookieValue, defaultCookieAttributes);
if (!browserIdCookie)
request.headers.cookie = request.headers.cookie
? request.headers.cookie + '; ' + browserIdCookieString
: browserIdCookieString;
if (!guestIdCookie)
request.headers.cookie = request.headers.cookie
? request.headers.cookie + '; ' + guestIdCookieString
: guestIdCookieString;
response.setHeader('Set-Cookie', [browserIdCookieString, guestIdCookieString]);
}
exports.handleHttpCookie = handleHttpCookie;

@@ -1,3 +0,3 @@

import { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
import { Settings } from '../settings/interfaces';
import type { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
import type { Settings } from '../settings/interfaces';
/**

@@ -13,5 +13,5 @@ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.

*
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
* @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
*
*/
export declare function handleNextJsMiddlewareCookie(request: MiddlewareRequest, response: MiddlewareNextResponse, options: Settings, timeout?: number): Promise<void>;

@@ -5,5 +5,6 @@ "use strict";

exports.handleNextJsMiddlewareCookie = void 0;
const get_browser_id_from_middleware_request_1 = require("./get-browser-id-from-middleware-request");
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy");
const get_cookie_value_from_middleware_request_1 = require("./get-cookie-value-from-middleware-request");
const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy");
const get_guest_id_1 = require("../init/get-guest-id");
/**

@@ -19,15 +20,22 @@ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.

*
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
* @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
*
*/
async function handleNextJsMiddlewareCookie(request, response, options, timeout) {
const { cookieName } = options.cookieSettings;
let cookieValue = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName);
if (!cookieValue)
cookieValue = (await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
.browserId;
const { browserId, guestId } = options.cookieSettings.cookieNames;
let browserIdCookieValue = (0, get_cookie_value_from_middleware_request_1.getCookieValueFromMiddlewareRequest)(request, browserId);
let guestIdCookieValue = (0, get_cookie_value_from_middleware_request_1.getCookieValueFromMiddlewareRequest)(request, guestId);
if (!browserIdCookieValue) {
const cookieValues = await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
browserIdCookieValue = cookieValues.browserId;
guestIdCookieValue = cookieValues.guestId;
}
else if (!guestIdCookieValue)
guestIdCookieValue = await (0, get_guest_id_1.getGuestId)(browserIdCookieValue, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
request.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
response.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
request.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
request.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
response.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
response.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
}
exports.handleNextJsMiddlewareCookie = handleNextJsMiddlewareCookie;

@@ -1,2 +0,2 @@

import { type Request, type MiddlewareNextResponse, type HttpResponse } from '@sitecore-cloudsdk/utils';
import { type HttpResponse, type MiddlewareNextResponse, type Request } from '@sitecore-cloudsdk/utils';
/**

@@ -3,0 +3,0 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.

@@ -7,4 +7,4 @@ "use strict";

const init_core_server_1 = require("../init/init-core-server");
const handle_http_cookie_1 = require("./handle-http-cookie");
const handle_next_js_middleware_cookie_1 = require("./handle-next-js-middleware-cookie");
const handle_http_cookie_1 = require("./handle-http-cookie");
/**

@@ -20,9 +20,7 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.

const settings = (0, init_core_server_1.getSettingsServer)();
if ((0, utils_1.isNextJsMiddlewareRequest)(request) && (0, utils_1.isNextJsMiddlewareResponse)(response)) {
if ((0, utils_1.isNextJsMiddlewareRequest)(request) && (0, utils_1.isNextJsMiddlewareResponse)(response))
await (0, handle_next_js_middleware_cookie_1.handleNextJsMiddlewareCookie)(request, response, settings, timeout);
}
else if ((0, utils_1.isHttpRequest)(request) && (0, utils_1.isHttpResponse)(response)) {
else if ((0, utils_1.isHttpRequest)(request) && (0, utils_1.isHttpResponse)(response))
await (0, handle_http_cookie_1.handleHttpCookie)(request, response, settings, timeout);
}
}
exports.handleServerCookie = handleServerCookie;
import debug from 'debug';
export { debug };
/**
* Extracts debug information from an HTTP response if debugging is enabled.
* @param response - The HTTP response object from fetch.
* @returns An object containing selected response details for debugging purposes.
*/
export declare function processDebugResponse(namespace: string, response: Response): object;

@@ -6,6 +6,6 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.debug = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
exports.processDebugResponse = exports.debug = void 0;
const debug_1 = __importDefault(require("debug"));
exports.debug = debug_1.default;
const utils_1 = require("@sitecore-cloudsdk/utils");
if (process.env &&

@@ -15,4 +15,21 @@ process.env.DEBUG_MULTILINE === 'true' &&

debug_1.default.formatters.o &&
debug_1.default.formatters.O) {
debug_1.default.formatters.O)
debug_1.default.formatters.o = debug_1.default.formatters.O;
/**
* Extracts debug information from an HTTP response if debugging is enabled.
* @param response - The HTTP response object from fetch.
* @returns An object containing selected response details for debugging purposes.
*/
function processDebugResponse(namespace, response) {
let debugResponse = {};
if (debug_1.default.enabled(namespace))
debugResponse = {
headers: (0, utils_1.normalizeHeaders)(response.headers),
redirected: response.redirected,
status: response.status,
statusText: response.statusText,
url: response.url
};
return debugResponse;
}
exports.processDebugResponse = processDebugResponse;

@@ -13,4 +13,5 @@ "use strict";

function constructGetBrowserIdUrl(sitecoreEdgeUrl, sitecoreEdgeContextId) {
// eslint-disable-next-line max-len
return `${sitecoreEdgeUrl}/v1/events/${consts_1.API_VERSION}/browser/create.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=`;
}
exports.constructGetBrowserIdUrl = constructGetBrowserIdUrl;

@@ -1,2 +0,2 @@

import { ProxySettings } from '../interfaces';
import type { ProxySettings } from '../interfaces';
/**

@@ -3,0 +3,0 @@ * Gets the browser ID and Client Key from Sitecore Edge Proxy

@@ -5,4 +5,4 @@ "use strict";

exports.fetchBrowserIdFromEdgeProxy = void 0;
const consts_1 = require("../consts");
const utils_1 = require("@sitecore-cloudsdk/utils");
const consts_1 = require("../consts");
const construct_get_browser_id_url_1 = require("./construct-get-browser-id-url");

@@ -19,7 +19,7 @@ /**

// eslint-disable-next-line @typescript-eslint/naming-convention
headers: { 'X-Library-Version': consts_1.LIBRARY_VERSION },
headers: { 'X-Library-Version': consts_1.LIBRARY_VERSION }
};
const url = (0, construct_get_browser_id_url_1.constructGetBrowserIdUrl)(sitecoreEdgeUrl, sitecoreEdgeContextId);
let payload;
if (timeout !== undefined) {
if (timeout !== undefined)
payload = await (0, utils_1.fetchWithTimeout)(url, timeout, fetchOptions)

@@ -30,18 +30,15 @@ .then((response) => {

.catch((err) => {
if (err.message === utils_1.ErrorMessages.IV_0006 || err.message === utils_1.ErrorMessages.IE_0002) {
if (err.message === utils_1.ErrorMessages.IV_0006 || err.message === utils_1.ErrorMessages.IE_0002)
throw new Error(err.message);
}
return null;
});
}
else {
else
payload = await fetch(url, fetchOptions)
.then((res) => res.json())
.catch(() => undefined);
}
if (!payload?.ref)
throw new Error(consts_1.ErrorMessages.IE_0003);
const { ref: browserId } = payload;
return { browserId };
const { ref: browserId, customer_ref: guestId } = payload;
return { browserId, guestId };
}
exports.fetchBrowserIdFromEdgeProxy = fetchBrowserIdFromEdgeProxy;

@@ -23,4 +23,6 @@ "use strict";

}
if (!data.customer.ref)
throw new Error(consts_1.ErrorMessages.IE_0011);
return data.customer.ref;
}
exports.getGuestId = getGuestId;

@@ -1,3 +0,3 @@

import { HttpResponse, MiddlewareNextResponse, Request } from '@sitecore-cloudsdk/utils';
import { Settings, ServerSettings } from '../settings/interfaces';
import type { Request, Response } from '@sitecore-cloudsdk/utils';
import type { ServerSettings, Settings } from '../settings/interfaces';
export declare function setCoreSettings(settings: Settings): void;

@@ -7,3 +7,4 @@ /**

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -17,3 +18,4 @@ * @param settingsInput - The settings input to configure the core settings.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -25,2 +27,2 @@ * @param settingsInput - The settings input to configure the core settings.

*/
export declare function initCoreServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;
export declare function initCoreServer(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;

@@ -5,7 +5,7 @@ "use strict";

exports.initCoreServer = exports.getSettingsServer = exports.setCoreSettings = void 0;
const handle_server_cookie_1 = require("../cookie/handle-server-cookie");
const namespaces_1 = require("../debug/namespaces");
const consts_1 = require("../consts");
const create_settings_1 = require("../settings/create-settings");
const debug_1 = require("../debug/debug");
const namespaces_1 = require("../debug/namespaces");
const consts_1 = require("../consts");
const handle_server_cookie_1 = require("../cookie/handle-server-cookie");
/**

@@ -24,3 +24,4 @@ * Internal settings object to be used by all functions in module caching.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -31,5 +32,4 @@ * @param settingsInput - The settings input to configure the core settings.

function getSettingsServer() {
if (!coreSettings) {
if (!coreSettings)
throw Error(consts_1.ErrorMessages.IE_0008);
}
return coreSettings;

@@ -41,3 +41,4 @@ }

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -53,6 +54,5 @@ * @param settingsInput - The settings input to configure the core settings.

coreSettings = (0, create_settings_1.createSettings)(settingsInput);
if (settingsInput.enableServerCookie) {
if (settingsInput.enableServerCookie)
await (0, handle_server_cookie_1.handleServerCookie)(request, response, settingsInput.timeout);
}
}
exports.initCoreServer = initCoreServer;

@@ -1,6 +0,7 @@

import { Settings, BrowserSettings } from '../settings/interfaces';
import type { BrowserSettings, Settings } from '../settings/interfaces';
/**
* Retrieves the core settings object.
*
* This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
* This function ensures that the core settings have been initialized and contain essential properties
* like `clientKey`, `cookieSettings`, and `targetURL`.
*

@@ -14,3 +15,4 @@ * @returns The core settings object.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -17,0 +19,0 @@ * @param settingsInput - The settings input to configure the core settings.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCookiePromise = exports.setCoreSettings = exports.initCore = exports.getSettings = void 0;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
const create_cookie_1 = require("../cookie/create-cookie");
const namespaces_1 = require("../debug/namespaces");
const consts_1 = require("../consts");
const create_cookies_1 = require("../cookie/create-cookies");
const create_settings_1 = require("../settings/create-settings");
const debug_1 = require("../debug/debug");
const namespaces_1 = require("../debug/namespaces");
const consts_1 = require("../consts");
/**

@@ -19,3 +18,4 @@ * Internal settings object to be used by all functions in module caching.

*
* This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
* This function ensures that the core settings have been initialized and contain essential properties
* like `clientKey`, `cookieSettings`, and `targetURL`.
*

@@ -26,13 +26,13 @@ * @returns The core settings object.

function getSettings() {
if (!coreSettings) {
if (!coreSettings)
throw Error(consts_1.ErrorMessages.IE_0008);
}
return coreSettings;
}
exports.getSettings = getSettings;
let createCookiePromise = null;
let createCookiesPromise = null;
/**
* Initializes the core settings for browser-based applications.
*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -46,5 +46,5 @@ * @param settingsInput - The settings input to configure the core settings.

coreSettings = (0, create_settings_1.createSettings)(settingsInput);
if (settingsInput.enableBrowserCookie && createCookiePromise === null)
createCookiePromise = (0, create_cookie_1.createCookie)(coreSettings);
await createCookiePromise;
if (settingsInput.enableBrowserCookie && createCookiesPromise === null)
createCookiesPromise = (0, create_cookies_1.createCookies)(coreSettings);
await createCookiesPromise;
}

@@ -60,4 +60,4 @@ exports.initCore = initCore;

function setCookiePromise(promise) {
createCookiePromise = promise;
createCookiesPromise = promise;
}
exports.setCookiePromise = setCookiePromise;

@@ -9,5 +9,7 @@ /**

client_key: string;
customer_ref: string;
}
export interface ProxySettings {
browserId: string;
guestId: string;
}

@@ -21,1 +23,14 @@ /**

}
/**
* Interface for supporting debug object
*/
export interface DebugResponse {
headers?: {
[key: string]: string | string[] | [string, string][] | Record<string, string> | Headers;
};
redirected?: boolean;
status?: number;
statusText?: string;
url?: string;
body?: unknown;
}

@@ -1,2 +0,2 @@

import { Settings, SettingsParams } from './interfaces';
import type { Settings, SettingsParams } from './interfaces';
/**

@@ -3,0 +3,0 @@ * Creates the global settings object, to be used by the library

@@ -19,10 +19,13 @@ "use strict";

cookieExpiryDays: cookieExpiryDays || consts_1.DEFAULT_COOKIE_EXPIRY_DAYS,
cookieName: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`,
cookiePath: cookiePath || '/',
cookieNames: {
browserId: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`,
guestId: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}_personalize`
},
cookiePath: cookiePath || '/'
},
siteName,
sitecoreEdgeContextId,
sitecoreEdgeUrl: sitecoreEdgeUrl ?? consts_1.SITECORE_EDGE_URL,
sitecoreEdgeUrl: sitecoreEdgeUrl ?? consts_1.SITECORE_EDGE_URL
};
}
exports.createSettings = createSettings;

@@ -1,2 +0,2 @@

import { Settings } from './interfaces';
import type { Settings } from './interfaces';
export declare function handleGetSettingsError(getSettingsFn: () => Settings, newError: string): Settings;

@@ -34,3 +34,6 @@ /**

export interface CookieSettings {
cookieName: string;
cookieNames: {
browserId: string;
guestId: string;
};
cookieDomain?: string;

@@ -40,8 +43,2 @@ cookieExpiryDays: number;

}
/**
* Properties for the cookie object
*/
export interface CookieSettingsBrowser extends CookieSettings {
enableBrowserCookie: boolean;
}
export {};

@@ -1,6 +0,7 @@

import { SettingsParams } from './interfaces';
import type { SettingsParams } from './interfaces';
/**
* Validates the core settings to ensure they meet required criteria.
*
* This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
* This function validates the provided core settings object to ensure that essential properties
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
*

@@ -7,0 +8,0 @@ * @param settings - The core settings object to validate.

@@ -9,3 +9,4 @@ "use strict";

*
* This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
* This function validates the provided core settings object to ensure that essential properties
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
*

@@ -21,3 +22,3 @@ * @param settings - The core settings object to validate.

throw new Error(consts_1.ErrorMessages.MV_0002);
if (sitecoreEdgeUrl !== undefined) {
if (sitecoreEdgeUrl !== undefined)
try {

@@ -29,4 +30,3 @@ new URL(sitecoreEdgeUrl);

}
}
}
exports.validateSettings = validateSettings;
{
"name": "@sitecore-cloudsdk/core",
"version": "0.3.0",
"version": "0.3.1-rc.0",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js",

@@ -1,3 +0,3 @@

export { createCookie } from './lib/cookie/create-cookie';
export { getBrowserIdFromMiddlewareRequest } from './lib/cookie/get-browser-id-from-middleware-request';
export { createCookies } from './lib/cookie/create-cookies';
export { getCookieValueFromMiddlewareRequest } from './lib/cookie/get-cookie-value-from-middleware-request';
export { getDefaultCookieAttributes } from './lib/cookie/get-default-cookie-attributes';

@@ -15,8 +15,8 @@ export { handleHttpCookie } from './lib/cookie/handle-http-cookie';

export { language, pageName } from './lib/infer/infer';
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request';
export { getCookieValueFromRequest } from './lib/cookie/get-cookie-value-from-request';
export { handleGetSettingsError } from './lib/settings/handle-get-settings-error';
export { generateCorrelationId } from './lib/correlation-id/generate-correlation-id';
export { debug } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER, } from './lib/consts';
export type { EPResponse, Infer } from './lib/interfaces';
export type { CookieSettings, Settings, BrowserSettings, ServerSettings, BasicSettings, } from './lib/settings/interfaces';
export { debug, processDebugResponse } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER } from './lib/consts';
export type { EPResponse, Infer, DebugResponse } from './lib/interfaces';
export type { CookieSettings, Settings, BrowserSettings, ServerSettings, BasicSettings } from './lib/settings/interfaces';
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
export { createCookie } from './lib/cookie/create-cookie';
export { getBrowserIdFromMiddlewareRequest } from './lib/cookie/get-browser-id-from-middleware-request';
export { createCookies } from './lib/cookie/create-cookies';
export { getCookieValueFromMiddlewareRequest } from './lib/cookie/get-cookie-value-from-middleware-request';
export { getDefaultCookieAttributes } from './lib/cookie/get-default-cookie-attributes';

@@ -16,6 +16,6 @@ export { handleHttpCookie } from './lib/cookie/handle-http-cookie';

export { language, pageName } from './lib/infer/infer';
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request';
export { getCookieValueFromRequest } from './lib/cookie/get-cookie-value-from-request';
export { handleGetSettingsError } from './lib/settings/handle-get-settings-error';
export { generateCorrelationId } from './lib/correlation-id/generate-correlation-id';
export { debug } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER, } from './lib/consts';
export { debug, processDebugResponse } from './lib/debug/debug';
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, CORRELATION_ID_HEADER } from './lib/consts';

@@ -9,4 +9,5 @@ export declare const LIBRARY_VERSION: string;

export declare enum ErrorMessages {
IE_0003 = "[IE-0003] Unable to set the cookie because the browser ID could not be retrieved from the server. Try again later, or use try-catch blocks to handle this error.",
IE_0003 = "[IE-0003] Unable to set the \"sc_{SitecoreEdgeContextId}\" cookie because the browser ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.",
IE_0008 = "[IE-0008] You must first initialize the \"core\" package. Run the \"init\" function.",
IE_0011 = "[IE-0011] Unable to set the \"sc_{SitecoreEdgeContextId}_personalize\" cookie because the guest ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.",
IV_0001 = "[IV-0001] Incorrect value for \"sitecoreEdgeUrl\". Set the value to a valid URL string.",

@@ -13,0 +14,0 @@ MV_0001 = "[MV-0001] \"sitecoreEdgeContextId\" is required.",

@@ -11,6 +11,8 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

export const CORRELATION_ID_HEADER = 'x-sc-correlation-id';
/* eslint-disable max-len */
export var ErrorMessages;
(function (ErrorMessages) {
ErrorMessages["IE_0003"] = "[IE-0003] Unable to set the cookie because the browser ID could not be retrieved from the server. Try again later, or use try-catch blocks to handle this error.";
ErrorMessages["IE_0003"] = "[IE-0003] Unable to set the \"sc_{SitecoreEdgeContextId}\" cookie because the browser ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
ErrorMessages["IE_0008"] = "[IE-0008] You must first initialize the \"core\" package. Run the \"init\" function.";
ErrorMessages["IE_0011"] = "[IE-0011] Unable to set the \"sc_{SitecoreEdgeContextId}_personalize\" cookie because the guest ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
ErrorMessages["IV_0001"] = "[IV-0001] Incorrect value for \"sitecoreEdgeUrl\". Set the value to a valid URL string.";

@@ -20,1 +22,2 @@ ErrorMessages["MV_0001"] = "[MV-0001] \"sitecoreEdgeContextId\" is required.";

})(ErrorMessages || (ErrorMessages = {}));
/* eslint-enable max-len */

@@ -10,3 +10,3 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

const settings = getSettings();
return getCookieValueClientSide(settings.cookieSettings.cookieName);
return getCookieValueClientSide(settings.cookieSettings.cookieNames.browserId);
}

@@ -0,1 +1,2 @@

import type { CookieProperties } from '@sitecore-cloudsdk/utils';
/**

@@ -6,3 +7,2 @@ * Gets the default Cookie Attributes

*/
import { CookieProperties } from '@sitecore-cloudsdk/utils';
export declare function getDefaultCookieAttributes(maxAge?: number, cookieDomain?: string): CookieProperties;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS } from '../consts';
/**

@@ -7,4 +8,2 @@ * Gets the default Cookie Attributes

*/
import { DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS } from '../consts';
// eslint-disable-next-line max-len
export function getDefaultCookieAttributes(maxAge = DEFAULT_COOKIE_EXPIRY_DAYS, cookieDomain) {

@@ -16,4 +15,4 @@ return {

sameSite: 'None',
secure: true,
secure: true
};
}
import type { HttpRequest, HttpResponse } from '@sitecore-cloudsdk/utils';
import { Settings } from '../settings/interfaces';
import type { Settings } from '../settings/interfaces';
/**

@@ -4,0 +4,0 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
import { createCookieString, getCookieServerSide } from '@sitecore-cloudsdk/utils';
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
import { getGuestId } from '../init/get-guest-id';
/**

@@ -17,19 +18,31 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.

export async function handleHttpCookie(request, response, options, timeout) {
const { cookieName } = options.cookieSettings;
const cookieValueFromRequest = request.headers.cookie;
let cookie;
let cookieValue;
if (cookieValueFromRequest) {
cookie = getCookieServerSide(cookieValueFromRequest, cookieName);
if (cookie)
cookieValue = cookie.value;
const { browserId, guestId } = options.cookieSettings.cookieNames;
const browserIdCookie = getCookieServerSide(request.headers.cookie, browserId);
let browserIdCookieValue;
const guestIdCookie = getCookieServerSide(request.headers.cookie, guestId);
let guestIdCookieValue;
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
if (!browserIdCookie) {
const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
browserIdCookieValue = cookieValues.browserId;
guestIdCookieValue = cookieValues.guestId;
}
if (!cookieValue)
cookieValue = (await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
.browserId;
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
const cookieString = createCookieString(cookieName, cookieValue, defaultCookieAttributes);
if (!cookie)
request.headers.cookie = cookieValueFromRequest ? cookieValueFromRequest + '; ' + cookieString : cookieString;
response.setHeader('Set-Cookie', cookieString);
else {
browserIdCookieValue = browserIdCookie.value;
if (!guestIdCookie)
guestIdCookieValue = await getGuestId(browserIdCookie.value, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
else
guestIdCookieValue = guestIdCookie.value;
}
const browserIdCookieString = createCookieString(browserId, browserIdCookieValue, defaultCookieAttributes);
const guestIdCookieString = createCookieString(guestId, guestIdCookieValue, defaultCookieAttributes);
if (!browserIdCookie)
request.headers.cookie = request.headers.cookie
? request.headers.cookie + '; ' + browserIdCookieString
: browserIdCookieString;
if (!guestIdCookie)
request.headers.cookie = request.headers.cookie
? request.headers.cookie + '; ' + guestIdCookieString
: guestIdCookieString;
response.setHeader('Set-Cookie', [browserIdCookieString, guestIdCookieString]);
}

@@ -1,3 +0,3 @@

import { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
import { Settings } from '../settings/interfaces';
import type { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
import type { Settings } from '../settings/interfaces';
/**

@@ -13,5 +13,5 @@ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.

*
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
* @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
*
*/
export declare function handleNextJsMiddlewareCookie(request: MiddlewareRequest, response: MiddlewareNextResponse, options: Settings, timeout?: number): Promise<void>;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { getBrowserIdFromMiddlewareRequest } from './get-browser-id-from-middleware-request';
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
import { getCookieValueFromMiddlewareRequest } from './get-cookie-value-from-middleware-request';
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
import { getGuestId } from '../init/get-guest-id';
/**

@@ -15,14 +16,21 @@ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.

*
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
* @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
*
*/
export async function handleNextJsMiddlewareCookie(request, response, options, timeout) {
const { cookieName } = options.cookieSettings;
let cookieValue = getBrowserIdFromMiddlewareRequest(request, cookieName);
if (!cookieValue)
cookieValue = (await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
.browserId;
const { browserId, guestId } = options.cookieSettings.cookieNames;
let browserIdCookieValue = getCookieValueFromMiddlewareRequest(request, browserId);
let guestIdCookieValue = getCookieValueFromMiddlewareRequest(request, guestId);
if (!browserIdCookieValue) {
const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
browserIdCookieValue = cookieValues.browserId;
guestIdCookieValue = cookieValues.guestId;
}
else if (!guestIdCookieValue)
guestIdCookieValue = await getGuestId(browserIdCookieValue, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
request.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
response.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
request.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
request.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
response.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
response.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
}

@@ -1,2 +0,2 @@

import { type Request, type MiddlewareNextResponse, type HttpResponse } from '@sitecore-cloudsdk/utils';
import { type HttpResponse, type MiddlewareNextResponse, type Request } from '@sitecore-cloudsdk/utils';
/**

@@ -3,0 +3,0 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { isNextJsMiddlewareRequest, isNextJsMiddlewareResponse, isHttpRequest, isHttpResponse, } from '@sitecore-cloudsdk/utils';
import { isHttpRequest, isHttpResponse, isNextJsMiddlewareRequest, isNextJsMiddlewareResponse } from '@sitecore-cloudsdk/utils';
import { getSettingsServer } from '../init/init-core-server';
import { handleHttpCookie } from './handle-http-cookie';
import { handleNextJsMiddlewareCookie } from './handle-next-js-middleware-cookie';
import { handleHttpCookie } from './handle-http-cookie';
/**

@@ -16,8 +16,6 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.

const settings = getSettingsServer();
if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response)) {
if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response))
await handleNextJsMiddlewareCookie(request, response, settings, timeout);
}
else if (isHttpRequest(request) && isHttpResponse(response)) {
else if (isHttpRequest(request) && isHttpResponse(response))
await handleHttpCookie(request, response, settings, timeout);
}
}
import debug from 'debug';
export { debug };
/**
* Extracts debug information from an HTTP response if debugging is enabled.
* @param response - The HTTP response object from fetch.
* @returns An object containing selected response details for debugging purposes.
*/
export declare function processDebugResponse(namespace: string, response: Response): object;

@@ -1,3 +0,3 @@

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import debug from 'debug';
import { normalizeHeaders } from '@sitecore-cloudsdk/utils';
if (process.env &&

@@ -7,5 +7,21 @@ process.env.DEBUG_MULTILINE === 'true' &&

debug.formatters.o &&
debug.formatters.O) {
debug.formatters.O)
debug.formatters.o = debug.formatters.O;
export { debug };
/**
* Extracts debug information from an HTTP response if debugging is enabled.
* @param response - The HTTP response object from fetch.
* @returns An object containing selected response details for debugging purposes.
*/
export function processDebugResponse(namespace, response) {
let debugResponse = {};
if (debug.enabled(namespace))
debugResponse = {
headers: normalizeHeaders(response.headers),
redirected: response.redirected,
status: response.status,
statusText: response.statusText,
url: response.url
};
return debugResponse;
}
export { debug };

@@ -10,3 +10,4 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

export function constructGetBrowserIdUrl(sitecoreEdgeUrl, sitecoreEdgeContextId) {
// eslint-disable-next-line max-len
return `${sitecoreEdgeUrl}/v1/events/${API_VERSION}/browser/create.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=`;
}

@@ -1,2 +0,2 @@

import { ProxySettings } from '../interfaces';
import type { ProxySettings } from '../interfaces';
/**

@@ -3,0 +3,0 @@ * Gets the browser ID and Client Key from Sitecore Edge Proxy

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { fetchWithTimeout, ErrorMessages as UtilsErrorMessages } from '@sitecore-cloudsdk/utils';
import { ErrorMessages, LIBRARY_VERSION } from '../consts';
import { ErrorMessages as UtilsErrorMessages, fetchWithTimeout } from '@sitecore-cloudsdk/utils';
import { constructGetBrowserIdUrl } from './construct-get-browser-id-url';

@@ -15,7 +15,7 @@ /**

// eslint-disable-next-line @typescript-eslint/naming-convention
headers: { 'X-Library-Version': LIBRARY_VERSION },
headers: { 'X-Library-Version': LIBRARY_VERSION }
};
const url = constructGetBrowserIdUrl(sitecoreEdgeUrl, sitecoreEdgeContextId);
let payload;
if (timeout !== undefined) {
if (timeout !== undefined)
payload = await fetchWithTimeout(url, timeout, fetchOptions)

@@ -26,17 +26,14 @@ .then((response) => {

.catch((err) => {
if (err.message === UtilsErrorMessages.IV_0006 || err.message === UtilsErrorMessages.IE_0002) {
if (err.message === UtilsErrorMessages.IV_0006 || err.message === UtilsErrorMessages.IE_0002)
throw new Error(err.message);
}
return null;
});
}
else {
else
payload = await fetch(url, fetchOptions)
.then((res) => res.json())
.catch(() => undefined);
}
if (!payload?.ref)
throw new Error(ErrorMessages.IE_0003);
const { ref: browserId } = payload;
return { browserId };
const { ref: browserId, customer_ref: guestId } = payload;
return { browserId, guestId };
}
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { API_VERSION, LIBRARY_VERSION } from '../consts';
import { API_VERSION, ErrorMessages, LIBRARY_VERSION } from '../consts';
/**

@@ -20,3 +20,5 @@ * A function that gets the guest ref from EP.

}
if (!data.customer.ref)
throw new Error(ErrorMessages.IE_0011);
return data.customer.ref;
}

@@ -1,3 +0,3 @@

import { HttpResponse, MiddlewareNextResponse, Request } from '@sitecore-cloudsdk/utils';
import { Settings, ServerSettings } from '../settings/interfaces';
import type { Request, Response } from '@sitecore-cloudsdk/utils';
import type { ServerSettings, Settings } from '../settings/interfaces';
export declare function setCoreSettings(settings: Settings): void;

@@ -7,3 +7,4 @@ /**

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -17,3 +18,4 @@ * @param settingsInput - The settings input to configure the core settings.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -25,2 +27,2 @@ * @param settingsInput - The settings input to configure the core settings.

*/
export declare function initCoreServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;
export declare function initCoreServer(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { handleServerCookie } from '../cookie/handle-server-cookie';
import { CORE_NAMESPACE } from '../debug/namespaces';
import { ErrorMessages } from '../consts';
import { createSettings } from '../settings/create-settings';
import { debug } from '../debug/debug';
import { CORE_NAMESPACE } from '../debug/namespaces';
import { ErrorMessages } from '../consts';
import { handleServerCookie } from '../cookie/handle-server-cookie';
/**

@@ -19,3 +19,4 @@ * Internal settings object to be used by all functions in module caching.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -26,5 +27,4 @@ * @param settingsInput - The settings input to configure the core settings.

export function getSettingsServer() {
if (!coreSettings) {
if (!coreSettings)
throw Error(ErrorMessages.IE_0008);
}
return coreSettings;

@@ -35,3 +35,4 @@ }

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -47,5 +48,4 @@ * @param settingsInput - The settings input to configure the core settings.

coreSettings = createSettings(settingsInput);
if (settingsInput.enableServerCookie) {
if (settingsInput.enableServerCookie)
await handleServerCookie(request, response, settingsInput.timeout);
}
}

@@ -1,6 +0,7 @@

import { Settings, BrowserSettings } from '../settings/interfaces';
import type { BrowserSettings, Settings } from '../settings/interfaces';
/**
* Retrieves the core settings object.
*
* This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
* This function ensures that the core settings have been initialized and contain essential properties
* like `clientKey`, `cookieSettings`, and `targetURL`.
*

@@ -14,3 +15,4 @@ * @returns The core settings object.

*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -17,0 +19,0 @@ * @param settingsInput - The settings input to configure the core settings.

@@ -1,7 +0,6 @@

// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
import { createCookie } from '../cookie/create-cookie';
import { CORE_NAMESPACE } from '../debug/namespaces';
import { ErrorMessages } from '../consts';
import { createCookies } from '../cookie/create-cookies';
import { createSettings } from '../settings/create-settings';
import { debug } from '../debug/debug';
import { CORE_NAMESPACE } from '../debug/namespaces';
import { ErrorMessages } from '../consts';
/**

@@ -16,3 +15,4 @@ * Internal settings object to be used by all functions in module caching.

*
* This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
* This function ensures that the core settings have been initialized and contain essential properties
* like `clientKey`, `cookieSettings`, and `targetURL`.
*

@@ -23,12 +23,12 @@ * @returns The core settings object.

export function getSettings() {
if (!coreSettings) {
if (!coreSettings)
throw Error(ErrorMessages.IE_0008);
}
return coreSettings;
}
let createCookiePromise = null;
let createCookiesPromise = null;
/**
* Initializes the core settings for browser-based applications.
*
* This function initializes core settings for the application, including creating settings and handling cookies if enabled.
* This function initializes core settings for the application,
* including creating settings and handling cookies if enabled.
*

@@ -42,5 +42,5 @@ * @param settingsInput - The settings input to configure the core settings.

coreSettings = createSettings(settingsInput);
if (settingsInput.enableBrowserCookie && createCookiePromise === null)
createCookiePromise = createCookie(coreSettings);
await createCookiePromise;
if (settingsInput.enableBrowserCookie && createCookiesPromise === null)
createCookiesPromise = createCookies(coreSettings);
await createCookiesPromise;
}

@@ -54,3 +54,3 @@ /**

export function setCookiePromise(promise) {
createCookiePromise = promise;
createCookiesPromise = promise;
}

@@ -9,5 +9,7 @@ /**

client_key: string;
customer_ref: string;
}
export interface ProxySettings {
browserId: string;
guestId: string;
}

@@ -21,1 +23,14 @@ /**

}
/**
* Interface for supporting debug object
*/
export interface DebugResponse {
headers?: {
[key: string]: string | string[] | [string, string][] | Record<string, string> | Headers;
};
redirected?: boolean;
status?: number;
statusText?: string;
url?: string;
body?: unknown;
}

@@ -1,2 +0,2 @@

import { Settings, SettingsParams } from './interfaces';
import type { Settings, SettingsParams } from './interfaces';
/**

@@ -3,0 +3,0 @@ * Creates the global settings object, to be used by the library

@@ -16,9 +16,12 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

cookieExpiryDays: cookieExpiryDays || DEFAULT_COOKIE_EXPIRY_DAYS,
cookieName: `${COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`,
cookiePath: cookiePath || '/',
cookieNames: {
browserId: `${COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`,
guestId: `${COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}_personalize`
},
cookiePath: cookiePath || '/'
},
siteName,
sitecoreEdgeContextId,
sitecoreEdgeUrl: sitecoreEdgeUrl ?? SITECORE_EDGE_URL,
sitecoreEdgeUrl: sitecoreEdgeUrl ?? SITECORE_EDGE_URL
};
}

@@ -1,2 +0,2 @@

import { Settings } from './interfaces';
import type { Settings } from './interfaces';
export declare function handleGetSettingsError(getSettingsFn: () => Settings, newError: string): Settings;

@@ -34,3 +34,6 @@ /**

export interface CookieSettings {
cookieName: string;
cookieNames: {
browserId: string;
guestId: string;
};
cookieDomain?: string;

@@ -40,8 +43,2 @@ cookieExpiryDays: number;

}
/**
* Properties for the cookie object
*/
export interface CookieSettingsBrowser extends CookieSettings {
enableBrowserCookie: boolean;
}
export {};

@@ -1,6 +0,7 @@

import { SettingsParams } from './interfaces';
import type { SettingsParams } from './interfaces';
/**
* Validates the core settings to ensure they meet required criteria.
*
* This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
* This function validates the provided core settings object to ensure that essential properties
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
*

@@ -7,0 +8,0 @@ * @param settings - The core settings object to validate.

@@ -6,3 +6,4 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.

*
* This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
* This function validates the provided core settings object to ensure that essential properties
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
*

@@ -18,3 +19,3 @@ * @param settings - The core settings object to validate.

throw new Error(ErrorMessages.MV_0002);
if (sitecoreEdgeUrl !== undefined) {
if (sitecoreEdgeUrl !== undefined)
try {

@@ -26,3 +27,2 @@ new URL(sitecoreEdgeUrl);

}
}
}
{
"name": "@sitecore-cloudsdk/core",
"version": "0.3.0",
"version": "0.3.1-rc.0",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc