@freema/drobek-sdk 0.11.1 → 0.12.1

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.
package/dist/beacon.d.ts CHANGED
@@ -1,35 +1,3 @@
1
- /**
2
- * The browser beacon — runs in the BROWSER on the app's own origin. The
3
- * compiler adds `import "/__drobek/beacon.js?v=<hash>"` at the top of every JS
4
- * entry of an app (unless its drobek.json says `"beacon": false`), so every
5
- * app reports without configuration:
6
- *
7
- * - that the page loaded (once per page load — a count only, nothing about
8
- * the visitor),
9
- * - its uncaught errors and unhandled promise rejections,
10
- * - a script, stylesheet, image or media file that failed to load (the
11
- * `error` event of the element, seen in the capture phase),
12
- * - a request the Content-Security-Policy blocked (`securitypolicyviolation`).
13
- *
14
- * The agent reads them with MCP `get_logs({ kind: 'runtime' })` and the
15
- * per-version counts in `get_app` (`render`).
16
- *
17
- * Contract with the server (`POST /__drobek/v1/_beacon`, @drobek/insights):
18
- * same-origin, JSON `{ version, load?, events: [{ type, message, stack, url,
19
- * ua, ts }] }`, ≤ 20 events and ≤ 8 KiB per POST (over-cap → 413).
20
- * `version` is the app version the page was served from — the app host
21
- * names it in the document's `Server-Timing: drobek-version;desc="<N>"`
22
- * (null when the browser does not expose it: the server then files the
23
- * report under the version the host serves now). The server redacts
24
- * e-mails / tokens and truncates again — the client caps are only there so a
25
- * POST fits. The page `url` and every resource address are only origin +
26
- * path: a query string or fragment can carry one-time codes, tokens or PII
27
- * the redaction cannot recognise (`?code=123456`), so it never leaves the
28
- * browser.
29
- *
30
- * Never throws, never recurses (an error while reporting is dropped), sends
31
- * at most 100 events per page load and at most 3 copies of the same error.
32
- */
33
1
  export declare const BEACON_ENDPOINT = "/__drobek/v1/_beacon";
34
2
  /** The server's hard cap per POST. */
35
3
  export declare const BEACON_MAX_BYTES: number;
@@ -73,6 +41,9 @@ export interface BeaconEnv {
73
41
  document?: {
74
42
  visibilityState?: string;
75
43
  addEventListener?(type: string, listener: Listener): void;
44
+ querySelector?(selector: string): {
45
+ getAttribute(name: string): string | null;
46
+ } | null;
76
47
  };
77
48
  performance?: {
78
49
  getEntriesByType?(type: string): unknown[];
package/dist/beacon.js CHANGED
@@ -30,6 +30,7 @@
30
30
  * Never throws, never recurses (an error while reporting is dropped), sends
31
31
  * at most 100 events per page load and at most 3 copies of the same error.
32
32
  */
33
+ import { appBasePath } from './core.js';
33
34
  export const BEACON_ENDPOINT = '/__drobek/v1/_beacon';
34
35
  /** The server's hard cap per POST. */
35
36
  export const BEACON_MAX_BYTES = 8 * 1024;
@@ -234,15 +235,18 @@ export function installBeacon(env = globalThis) {
234
235
  let timer = false;
235
236
  let busy = false;
236
237
  const now = () => (env.Date ?? Date).now();
238
+ // Path routing: the page's `<meta name="drobek-base">` puts the beacon under the app's base path.
239
+ const doc = env.document;
240
+ const endpoint = `${appBasePath(doc && typeof doc.querySelector === 'function' ? doc : undefined)}${BEACON_ENDPOINT}`;
237
241
  function send(bodyText) {
238
242
  try {
239
243
  const nav = env.navigator;
240
244
  if (nav && typeof nav.sendBeacon === 'function' && typeof Blob !== 'undefined') {
241
- if (nav.sendBeacon(BEACON_ENDPOINT, new Blob([bodyText], { type: 'application/json' })))
245
+ if (nav.sendBeacon(endpoint, new Blob([bodyText], { type: 'application/json' })))
242
246
  return;
243
247
  }
244
248
  if (typeof env.fetch === 'function') {
245
- void Promise.resolve(env.fetch(BEACON_ENDPOINT, {
249
+ void Promise.resolve(env.fetch(endpoint, {
246
250
  method: 'POST',
247
251
  body: bodyText,
248
252
  headers: { 'Content-Type': 'application/json' },
package/dist/core.d.ts CHANGED
@@ -16,6 +16,18 @@
16
16
  export declare const SDK_HEADER = "X-Drobek-SDK";
17
17
  /** Module routes live under `/__drobek/v1/<module>/…` on the app's own host. */
18
18
  export declare const MODULE_API_BASE = "/__drobek/v1";
19
+ /**
20
+ * The app's base path: '' on its own host; in path routing
21
+ * (APPS_ROUTING_MODE=path) the server puts `<meta name="drobek-base"
22
+ * content="/apps/<label>/">` into every HTML page of the app, and every SDK
23
+ * URL goes under it. Read on each call (a page's head is parsed before any
24
+ * module script runs). Only a value of that exact shape counts.
25
+ */
26
+ export declare function appBasePath(doc?: {
27
+ querySelector(selector: string): {
28
+ getAttribute(name: string): string | null;
29
+ } | null;
30
+ } | undefined): string;
19
31
  /**
20
32
  * A failed module call: `status` is the HTTP status, `code` the stable error
21
33
  * code from the module (`{ error, message, details?, hint? }`), `hint` what to
@@ -41,7 +53,7 @@ export interface RequestOptions {
41
53
  export interface SdkCore {
42
54
  /** The module name (`drobek.<module>`). */
43
55
  readonly module: string;
44
- /** `/__drobek/v1/<module><path>?<query>` */
56
+ /** `/__drobek/v1/<module><path>?<query>` (under the app's base path in path routing) */
45
57
  url(path?: string, query?: Record<string, QueryValue>): string;
46
58
  /**
47
59
  * Call a route of this module. Resolves with the parsed JSON body (text for a
package/dist/core.js CHANGED
@@ -16,6 +16,17 @@
16
16
  export const SDK_HEADER = 'X-Drobek-SDK';
17
17
  /** Module routes live under `/__drobek/v1/<module>/…` on the app's own host. */
18
18
  export const MODULE_API_BASE = '/__drobek/v1';
19
+ /**
20
+ * The app's base path: '' on its own host; in path routing
21
+ * (APPS_ROUTING_MODE=path) the server puts `<meta name="drobek-base"
22
+ * content="/apps/<label>/">` into every HTML page of the app, and every SDK
23
+ * URL goes under it. Read on each call (a page's head is parsed before any
24
+ * module script runs). Only a value of that exact shape counts.
25
+ */
26
+ export function appBasePath(doc = typeof document === 'undefined' ? undefined : document) {
27
+ const raw = doc?.querySelector('meta[name="drobek-base"]')?.getAttribute('content') ?? '';
28
+ return /^\/apps\/[a-z0-9-]+\/$/.test(raw) ? raw.slice(0, -1) : '';
29
+ }
19
30
  /**
20
31
  * A failed module call: `status` is the HTTP status, `code` the stable error
21
32
  * code from the module (`{ error, message, details?, hint? }`), `hint` what to
@@ -71,7 +82,7 @@ async function readBody(res) {
71
82
  /** The SDK core for module `module` (the composed sdk.js calls this once per module). */
72
83
  export function createCore(module, fetchImpl) {
73
84
  const doFetch = fetchImpl ?? ((input, init) => fetch(input, init));
74
- const url = (path, query) => `${MODULE_API_BASE}/${module}${joinPath(path)}${queryString(query)}`;
85
+ const url = (path, query) => `${appBasePath()}${MODULE_API_BASE}/${module}${joinPath(path)}${queryString(query)}`;
75
86
  return {
76
87
  module,
77
88
  url,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freema/drobek-sdk",
3
- "version": "0.11.1",
3
+ "version": "0.12.1",
4
4
  "description": "The drobek browser SDK core: the SdkCore a platform module's SDK entry receives, DrobekError, the error beacon.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",