@usenotra/geo
Version:
Request capture SDK for AI traffic attribution and agent feedback collection
96 lines (91 loc) • 3.31 kB
JavaScript
// src/constants.ts
var DEFAULT_ENDPOINT = "https://ingest.usenotra.com";
// src/script/constants.ts
var SCRIPT_HOST = "https://static.usenotra.com";
var SCRIPT_HOST_PATH = "/";
var SCRIPT_PATH = "/api/";
var COLLECT_PATH = "/api/collect";
var SCRIPT_FILE = "notra.js";
var SCRIPT_PLUS_FILE = "notra.plus.js";
var WEB_KEY_PREFIX = "nwk.";
var INVALID_WEB_KEY_MESSAGE = 'Pass your project\'s public web key as `webKey`. It starts with "nwk." and is in the script tag under Settings \u2192 Tracking in your Notra dashboard.';
var SCRIPT_CROSS_ORIGIN = "anonymous";
// src/script/releases.ts
var NOTRA_SCRIPT_RELEASE = {
file: "notra.ce3c872ef9c9.js",
integrity: "sha384-LuUp5Hj+rSQ4/mZp1sWBOrJDHIHKGqflwF1GYIreqmWdCaI1otOQealWJGRj6Toj"
};
var NOTRA_SCRIPT_PLUS_RELEASE = {
file: "notra.plus.d703b9cc5508.js",
integrity: "sha384-MgLdHKufD0WdkO4pxtkT/bbNCKxl6PxOyUJe+M/qwFGfjOdZ5rEnljxTlOVQIzF+"
};
// src/script/attributes.ts
function trimOrigin(value) {
let end = value.length;
while (end > 0 && value[end - 1] === "/") {
end -= 1;
}
return value.slice(0, end);
}
function scriptBase(endpoint) {
return endpoint === void 0 ? `${SCRIPT_HOST}${SCRIPT_HOST_PATH}` : `${trimOrigin(endpoint)}${SCRIPT_PATH}`;
}
function defaultApi(options) {
return options.endpoint === void 0 && options.src === void 0 ? `${DEFAULT_ENDPOINT}${COLLECT_PATH}` : void 0;
}
function resolveSource(options) {
if (options.src) {
return { src: options.src, integrity: options.integrity };
}
const base = scriptBase(options.endpoint);
if (options.pinned === false) {
const file = options.errors ? SCRIPT_PLUS_FILE : SCRIPT_FILE;
return { src: `${base}${file}`, integrity: options.integrity };
}
const release = options.errors ? NOTRA_SCRIPT_PLUS_RELEASE : NOTRA_SCRIPT_RELEASE;
return {
src: `${base}${release.file}`,
integrity: options.integrity ?? release.integrity
};
}
function notraScript(options) {
if (!options.webKey?.startsWith(WEB_KEY_PREFIX)) {
throw new Error(INVALID_WEB_KEY_MESSAGE);
}
const { src, integrity } = resolveSource(options);
const attributes = {
src,
defer: true,
"data-key": options.webKey
};
const api = options.api ?? defaultApi(options);
if (api) {
attributes["data-api"] = api;
}
if (options.hash) {
attributes["data-hash"] = "";
}
if (integrity) {
attributes.integrity = integrity;
attributes.crossOrigin = SCRIPT_CROSS_ORIGIN;
}
return attributes;
}
function escapeAttribute(value) {
return value.replaceAll("&", "&").replaceAll('"', """).replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">");
}
function notraScriptTag(options) {
const attributes = notraScript(options);
let tag = `<script defer src="${escapeAttribute(attributes.src)}" data-key="${escapeAttribute(attributes["data-key"])}"`;
if (attributes["data-api"] !== void 0) {
tag += ` data-api="${escapeAttribute(attributes["data-api"])}"`;
}
if (attributes["data-hash"] !== void 0) {
tag += " data-hash";
}
if (attributes.integrity !== void 0) {
tag += ` integrity="${escapeAttribute(attributes.integrity)}" crossorigin="${SCRIPT_CROSS_ORIGIN}"`;
}
return `${tag}></script>`;
}
export { NOTRA_SCRIPT_PLUS_RELEASE, NOTRA_SCRIPT_RELEASE, notraScript, notraScriptTag };