@genesislcap/foundation-utils 15.54.0 → 15.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,61 @@ import { __awaiter } from "tslib";
2
2
  import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
3
3
  const REPORTED_LOG_TYPES = new Set(['error', 'fatal']);
4
4
  const MAX_BUFFERED_LOGS = 100;
5
+ /**
6
+ * Sentry's default integrations that send nothing from the page beyond the error and the browser.
7
+ * Others, such as `Console`, and any a later SDK adds by default, stay off unless the app adds
8
+ * them. Taken from `@sentry/browser` 11.1.0 and matched by name, so a renamed one is dropped.
9
+ */
10
+ const KEPT_DEFAULT_INTEGRATIONS = new Set([
11
+ 'EventFilters',
12
+ 'InboundFilters',
13
+ 'FunctionToString',
14
+ 'BrowserApiErrors',
15
+ 'Breadcrumbs',
16
+ 'GlobalHandlers',
17
+ 'LinkedErrors',
18
+ 'Dedupe',
19
+ 'HttpContext',
20
+ 'CultureContext',
21
+ 'BrowserSession',
22
+ ]);
23
+ /**
24
+ * Console breadcrumbs carry the logged arguments; click and input ones the element's title and
25
+ * labels.
26
+ */
27
+ const DROPPED_BREADCRUMB_CATEGORIES = new Set(['console', 'ui.click', 'ui.input']);
28
+ const BREADCRUMB_URL_KEYS = ['url', 'from', 'to'];
29
+ const SPAN_URL_KEYS = new Set(['url.full', 'http.url']);
30
+ const SPAN_URL_PART_KEYS = new Set(['url.query', 'url.fragment', 'http.query', 'http.fragment']);
31
+ const DEFAULT_DATA_COLLECTION = {
32
+ userInfo: false,
33
+ cookies: false,
34
+ // Sentry reads the browser and OS from User-Agent; Referer can hold the previous page's query.
35
+ httpHeaders: { request: { allow: ['user-agent'] }, response: false },
36
+ httpBodies: [],
37
+ urlQueryParams: false,
38
+ };
39
+ const withoutQuery = (url) => url.split(/[?#]/, 1)[0];
40
+ const withoutUrlQueries = (breadcrumb) => {
41
+ const { data } = breadcrumb;
42
+ const urls = BREADCRUMB_URL_KEYS.flatMap((key) => {
43
+ const value = data === null || data === void 0 ? void 0 : data[key];
44
+ return typeof value === 'string' ? [[key, withoutQuery(value)]] : [];
45
+ });
46
+ return urls.length
47
+ ? Object.assign(Object.assign({}, breadcrumb), { data: Object.assign(Object.assign({}, data), Object.fromEntries(urls)) }) : breadcrumb;
48
+ };
49
+ const withoutSpanUrlParts = (values) => Object.fromEntries(Object.entries(values)
50
+ .filter(([key]) => !SPAN_URL_PART_KEYS.has(key))
51
+ .map(([key, value]) => [
52
+ key,
53
+ SPAN_URL_KEYS.has(key) && typeof value === 'string' ? withoutQuery(value) : value,
54
+ ]));
55
+ const spanOp = ({ op, attributes, data }) => {
56
+ var _a;
57
+ const value = (_a = op !== null && op !== void 0 ? op : attributes === null || attributes === void 0 ? void 0 : attributes['sentry.op']) !== null && _a !== void 0 ? _a : data === null || data === void 0 ? void 0 : data['sentry.op'];
58
+ return typeof value === 'string' ? value : undefined;
59
+ };
5
60
  const LOCAL_HOSTNAMES = new Set(['localhost', '0.0.0.0', 'host.docker.internal']);
6
61
  const LOCAL_HOSTNAME_SUFFIXES = ['.localhost', '.local'];
7
62
  const IPV4 = /^\d{1,3}(\.\d{1,3}){3}$/;
@@ -170,12 +225,18 @@ const bufferLogs = () => {
170
225
  * @remarks
171
226
  * Without a DSN, or when no environment applies (by default on local hosts such as `localhost` or
172
227
  * a private IP address), it does nothing and resolves `false`. The environment defaults to the
173
- * page's hostname, so one image promoted across environments reports each under its own name; pass
174
- * a function to map hostnames to names such as `uat`. Otherwise it:
228
+ * page's hostname, so one image promoted across environments reports each under its own name. To
229
+ * use names such as `uat`, read one the deployment sets rather than listing hostnames, which would
230
+ * show everyone who loads the bundle where the other environments are. Otherwise it:
175
231
  *
176
- * - initialises the client (`client.init`, or `options.init`) with the DSN, environment, a
177
- * `beforeBreadcrumb` that drops the SDK's console breadcrumbs for foundation logger output (they
178
- * carry the logged arguments), and any `initOptions`;
232
+ * - initialises the client (`client.init`, or `options.init`) with the DSN, the environment, any
233
+ * `initOptions`, and defaults that keep page data out of what the SDK collects by itself: Sentry
234
+ * 11's `dataCollection` off for user info, cookies, headers but `User-Agent`, bodies and URL
235
+ * queries; only the default integrations that send nothing from the page (so not `Console`); no
236
+ * console, click or input breadcrumbs; and no query strings or fragments in breadcrumb URLs, the
237
+ * page URL, or span URLs and resource span names when tracing is on. The app's
238
+ * `beforeBreadcrumb`, `beforeSend`, `beforeSendTransaction` and `beforeSendSpan` run after
239
+ * these, and its `integrations` are added;
179
240
  * - registers a global log reporter ({@link createErrorMonitoringReporter}), so errors logged by
180
241
  * every foundation logger, including those Genesis packages create at import time, are reported,
181
242
  * and other logs become breadcrumbs. Only log messages and exceptions are sent, never logged data.
@@ -200,7 +261,8 @@ const bufferLogs = () => {
200
261
  *
201
262
  * setupErrorMonitoring(Sentry, {
202
263
  * dsn: 'https://<key>@<org>.ingest.sentry.io/<project>',
203
- * environment: ({ hostname }) => ({ 'app-uat.example.com': 'uat', 'app.example.com': 'prod' })[hostname],
264
+ * environment: () =>
265
+ * document.querySelector<HTMLMetaElement>('meta[name="error-monitoring-environment"]')?.content,
204
266
  * });
205
267
  * ```
206
268
  *
@@ -227,14 +289,46 @@ export function setupErrorMonitoring(client, options) {
227
289
  try {
228
290
  const appBeforeBreadcrumb = initOptions.beforeBreadcrumb;
229
291
  const beforeBreadcrumb = (breadcrumb, hint) => {
230
- if (isFoundationLoggerConsoleBreadcrumb(breadcrumb)) {
292
+ var _a;
293
+ if (DROPPED_BREADCRUMB_CATEGORIES.has((_a = breadcrumb.category) !== null && _a !== void 0 ? _a : '')) {
231
294
  return null;
232
295
  }
233
- return appBeforeBreadcrumb ? appBeforeBreadcrumb(breadcrumb, hint) : breadcrumb;
296
+ const kept = withoutUrlQueries(breadcrumb);
297
+ return appBeforeBreadcrumb ? appBeforeBreadcrumb(kept, hint) : kept;
298
+ };
299
+ const withPageUrlStripped = (hook) => (event, hint) => {
300
+ var _a;
301
+ const url = (_a = event.request) === null || _a === void 0 ? void 0 : _a.url;
302
+ const kept = typeof url === 'string'
303
+ ? Object.assign(Object.assign({}, event), { request: Object.assign(Object.assign({}, event.request), { url: withoutQuery(url) }) }) : event;
304
+ return (initOptions[hook] ? initOptions[hook](kept, hint) : kept);
305
+ };
306
+ const beforeSendSpan = (span) => {
307
+ var _a;
308
+ const isResource = (_a = spanOp(span)) === null || _a === void 0 ? void 0 : _a.startsWith('resource.');
309
+ const kept = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, span), (span.attributes && { attributes: withoutSpanUrlParts(span.attributes) })), (span.data && { data: withoutSpanUrlParts(span.data) })), (isResource && span.name && { name: withoutQuery(span.name) })), (isResource && span.description && { description: withoutQuery(span.description) }));
310
+ return (initOptions.beforeSendSpan ? initOptions.beforeSendSpan(kept) : kept);
311
+ };
312
+ const isStaticLifecycle = initOptions.traceLifecycle === 'static';
313
+ if (isStaticLifecycle) {
314
+ // Sentry.withStaticSpan's marker; Sentry skips unmarked callbacks in static mode.
315
+ Object.defineProperty(beforeSendSpan, '_static', { value: true });
316
+ }
317
+ const appIntegrations = initOptions.integrations;
318
+ const integrations = (defaults) => {
319
+ const kept = defaults.filter(({ name }) => KEPT_DEFAULT_INTEGRATIONS.has(name));
320
+ if (typeof appIntegrations === 'function') {
321
+ return appIntegrations(kept);
322
+ }
323
+ const added = (appIntegrations !== null && appIntegrations !== void 0 ? appIntegrations : []);
324
+ const addedNames = new Set(added.map(({ name }) => name));
325
+ return [...kept.filter(({ name }) => !addedNames.has(name)), ...added];
234
326
  };
235
- const resolvedInitOptions = Object.assign(Object.assign({}, initOptions), { dsn,
236
- environment,
237
- beforeBreadcrumb });
327
+ const resolvedInitOptions = Object.assign(Object.assign(Object.assign(Object.assign({}, initOptions), { dsn,
328
+ environment, dataCollection: Object.assign(Object.assign({}, DEFAULT_DATA_COLLECTION), initOptions.dataCollection), integrations,
329
+ beforeBreadcrumb, beforeSend: withPageUrlStripped('beforeSend') }), (isStaticLifecycle && {
330
+ beforeSendTransaction: withPageUrlStripped('beforeSendTransaction'),
331
+ })), { beforeSendSpan });
238
332
  try {
239
333
  yield (init ? init(resolvedInitOptions) : (_a = client.init) === null || _a === void 0 ? void 0 : _a.call(client, resolvedInitOptions));
240
334
  }