@narrativetrace/browser 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,99 @@
1
+ Business Source License 1.1
2
+
3
+ License text copyright © 2017 MariaDB Corporation Ab, All Rights Reserved.
4
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
5
+
6
+ Parameters
7
+
8
+ Licensor: Empower Agile
9
+
10
+ Licensed Work: NarrativeTrace for TypeScript (the @narrativetrace npm packages) version 0.1.0. The Licensed Work is (c) 2026 Empower Agile.
11
+
12
+ Additional Use Grant: You may make production use of the Licensed Work for any
13
+ purpose, including internal use and use in products and
14
+ services you provide to your own customers, provided that
15
+ such production use does not include offering the
16
+ Licensed Work, or a product or service whose value
17
+ derives substantially from the Licensed Work, to third
18
+ parties as a logging, tracing, or code-narrative product
19
+ or service.
20
+
21
+ Change Date: Four years from the date the Licensed Work is published. (2030-09-07 for this version)
22
+
23
+ Change License: Apache License, Version 2.0
24
+
25
+ For information about alternative licensing arrangements for the Licensed
26
+ Work, please contact Empower Agile at hello@narrativetrace.ai.
27
+
28
+ Notice
29
+
30
+ The Business Source License (this document, or the "License") is not an Open
31
+ Source license. However, the Licensed Work will eventually be made available
32
+ under an Open Source License, as stated in this License.
33
+
34
+ Terms
35
+
36
+ The Licensor hereby grants you the right to copy, modify, create derivative
37
+ works, redistribute, and make non-production use of the Licensed Work. The
38
+ Licensor may make an Additional Use Grant, above, permitting limited
39
+ production use.
40
+
41
+ Effective on the Change Date, or the fourth anniversary of the first publicly
42
+ available distribution of a specific version of the Licensed Work under this
43
+ License, whichever comes first, the Licensor hereby grants you rights under
44
+ the terms of the Change License, and the rights granted in the paragraph
45
+ above terminate.
46
+
47
+ If your use of the Licensed Work does not comply with the requirements
48
+ currently in effect as described in this License, you must purchase a
49
+ commercial license from the Licensor, its affiliated entities, or authorized
50
+ resellers, or you must refrain from using the Licensed Work.
51
+
52
+ All copies of the original and modified Licensed Work, and derivative works
53
+ of the Licensed Work, are subject to this License. This License applies
54
+ separately for each version of the Licensed Work and the Change Date may vary
55
+ for each version of the Licensed Work released by Licensor.
56
+
57
+ You must conspicuously display this License on each original or modified copy
58
+ of the Licensed Work. If you receive the Licensed Work in original or
59
+ modified form from a third party, the terms and conditions set forth in this
60
+ License apply to your use of that work.
61
+
62
+ Any use of the Licensed Work in violation of this License will automatically
63
+ terminate your rights under this License for the current and all other
64
+ versions of the Licensed Work.
65
+
66
+ This License does not grant you any right in any trademark or logo of
67
+ Licensor or its affiliates (provided that you may use a trademark or logo of
68
+ Licensor as expressly required by this License).
69
+
70
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
71
+ AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
72
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
73
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
74
+ TITLE.
75
+
76
+ MariaDB hereby grants you permission to use this License’s text to license
77
+ your works, and to refer to it using the trademark “Business Source License”,
78
+ as long as you comply with the Covenants of Licensor below.
79
+
80
+ Covenants of Licensor
81
+
82
+ In consideration of the right to use this License’s text and the “Business
83
+ Source License” name and trademark, Licensor covenants to MariaDB, and to all
84
+ other recipients of the licensed work to be provided by Licensor:
85
+
86
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
87
+ or a license that is compatible with GPL Version 2.0 or a later version,
88
+ where “compatible” means that software provided under the Change License can
89
+ be included in a program with software provided under GPL Version 2.0 or a
90
+ later version. Licensor may specify additional Change Licenses without
91
+ limitation.
92
+
93
+ 2. To either: (a) specify an additional grant of rights to use that does not
94
+ impose any additional restriction on the right granted in this License, as
95
+ the Additional Use Grant; or (b) insert the text “None”.
96
+
97
+ 3. To specify a Change Date.
98
+
99
+ 4. Not to modify this License in any other way.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @narrativetrace/browser
2
+
3
+ Browser rendering and export for NarrativeTrace: print a captured trace to the devtools console, POST it to a collector, or propagate trace headers on `fetch`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @narrativetrace/browser @narrativetrace/core-web
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Use `tracedFetch` to propagate the trace context on requests, `renderToConsole` to inspect a captured tree with collapsible console groups, and `postToCollector` to ship it to a backend:
14
+
15
+ ```ts
16
+ import { renderToConsole, postToCollector, tracedFetch } from "@narrativetrace/browser";
17
+ import type { NarrativeContext } from "@narrativetrace/core-web";
18
+
19
+ async function reportCheckout(ctx: NarrativeContext) {
20
+ const fetchWithTrace = tracedFetch(ctx);
21
+ await fetchWithTrace("/api/checkout", { method: "POST" });
22
+
23
+ const tree = ctx.captureTrace();
24
+ renderToConsole(tree);
25
+ await postToCollector(tree, { scenario: "checkout" }, "https://collector.example.com/traces");
26
+ ctx.reset();
27
+ }
28
+ ```
29
+
30
+ `renderToConsole` prints each call with its parameters and outcome; `postToCollector` serializes the tree with the given metadata and POSTs it as JSON.
31
+
32
+ ## Learn more
33
+
34
+ - [Main README](../../README.md) — the NarrativeTrace model, packages, and quick start.
package/dist/index.cjs ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ postToCollector: () => postToCollector,
24
+ renderToConsole: () => renderToConsole,
25
+ tracedFetch: () => tracedFetch
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/console-renderer.ts
30
+ var import_core_web = require("@narrativetrace/core-web");
31
+ function formatCall(node) {
32
+ const { className, methodName, parameters } = node.signature;
33
+ const params = parameters.map((p) => `${p.name}: ${p.renderedValue}`).join(", ");
34
+ return `${className}.${methodName}(${params})`;
35
+ }
36
+ function formatOutcome(node) {
37
+ if (node.outcome.kind === "threw") {
38
+ const err = node.outcome.error;
39
+ const msg = err instanceof Error ? err.message : String(err);
40
+ return ` \u2717 ${msg}`;
41
+ }
42
+ if (node.outcome.kind === "incomplete") return " \u23F3 (incomplete)";
43
+ const ret = node.outcome.renderedValue;
44
+ if (ret !== null && ret !== "undefined") {
45
+ return ` \u2192 ${ret}`;
46
+ }
47
+ return "";
48
+ }
49
+ function logNarration(node) {
50
+ if (node.signature.narration !== void 0) {
51
+ console.log(` ${node.signature.narration}`);
52
+ }
53
+ }
54
+ function enterNode(node, stop) {
55
+ const label = `${formatCall(node)}${formatOutcome(node)}`;
56
+ if (stop !== void 0) {
57
+ console.group(label);
58
+ logNarration(node);
59
+ console.log(import_core_web.TREE_WALK_MARKER[stop]);
60
+ console.groupEnd();
61
+ return;
62
+ }
63
+ if (node.children.length === 0) {
64
+ console.log(label);
65
+ logNarration(node);
66
+ return;
67
+ }
68
+ console.group(label);
69
+ logNarration(node);
70
+ }
71
+ function exitNode(node) {
72
+ if (node.children.length > 0) console.groupEnd();
73
+ }
74
+ function renderToConsole(tree) {
75
+ const traceId = tree.roots[0]?.spanContext?.traceId;
76
+ if (traceId) {
77
+ console.log(`[${(0, import_core_web.humanName)(traceId)}]`);
78
+ }
79
+ (0, import_core_web.walkPreOrder)(tree.roots, (n) => n.children, enterNode, exitNode);
80
+ }
81
+
82
+ // src/network-export.ts
83
+ var import_core_web2 = require("@narrativetrace/core-web");
84
+ async function postToCollector(tree, metadata, collectorUrl) {
85
+ const body = (0, import_core_web2.exportJson)(tree, metadata);
86
+ return fetch(collectorUrl, {
87
+ method: "POST",
88
+ headers: { "Content-Type": "application/json" },
89
+ body
90
+ });
91
+ }
92
+
93
+ // src/traced-fetch.ts
94
+ var import_core_web3 = require("@narrativetrace/core-web");
95
+ function tracedFetch(ctx) {
96
+ return (input, init) => {
97
+ const base = input instanceof Request ? input.headers : void 0;
98
+ const headers = new Headers(init?.headers ?? base);
99
+ headers.set("traceparent", (0, import_core_web3.formatTraceparent)(ctx.traceId(), (0, import_core_web3.generateSpanId)()));
100
+ return globalThis.fetch(input, { ...init, headers });
101
+ };
102
+ }
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ postToCollector,
106
+ renderToConsole,
107
+ tracedFetch
108
+ });
109
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/console-renderer.ts","../src/network-export.ts","../src/traced-fetch.ts"],"sourcesContent":["// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nexport { renderToConsole } from \"./console-renderer.js\";\nexport { postToCollector } from \"./network-export.js\";\nexport { tracedFetch } from \"./traced-fetch.js\";\n","// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport {\n humanName,\n TREE_WALK_MARKER,\n type TraceId,\n type TraceNode,\n type TraceTree,\n type TreeWalkStop,\n walkPreOrder,\n} from \"@narrativetrace/core-web\";\n\nfunction formatCall(node: TraceNode): string {\n const { className, methodName, parameters } = node.signature;\n const params = parameters.map((p) => `${p.name}: ${p.renderedValue}`).join(\", \");\n return `${className}.${methodName}(${params})`;\n}\n\nfunction formatOutcome(node: TraceNode): string {\n if (node.outcome.kind === \"threw\") {\n const err = node.outcome.error;\n const msg = err instanceof Error ? err.message : String(err);\n return ` \\u2717 ${msg}`;\n }\n if (node.outcome.kind === \"incomplete\") return \" \\u23F3 (incomplete)\";\n const ret = node.outcome.renderedValue;\n if (ret !== null && ret !== \"undefined\") {\n return ` \\u2192 ${ret}`;\n }\n return \"\";\n}\n\nfunction logNarration(node: TraceNode): void {\n if (node.signature.narration !== undefined) {\n console.log(` ${node.signature.narration}`);\n }\n}\n\n// The node's own line prints regardless of the guard (\"contributes itself\"); a stopped node opens\n// and closes its own group inline (walkPreOrder never calls exitNode for it) instead of leaving an\n// unmatched console.group() open.\nfunction enterNode(node: TraceNode, stop: TreeWalkStop | undefined): void {\n const label = `${formatCall(node)}${formatOutcome(node)}`;\n if (stop !== undefined) {\n console.group(label);\n logNarration(node);\n console.log(TREE_WALK_MARKER[stop]);\n console.groupEnd();\n return;\n }\n if (node.children.length === 0) {\n console.log(label);\n logNarration(node);\n return;\n }\n console.group(label);\n logNarration(node);\n}\n\n// Only a node whose group is still open (it had children and was not stopped) needs closing —\n// walkPreOrder calls this exactly once per such node, after all its descendants are visited.\nfunction exitNode(node: TraceNode): void {\n if (node.children.length > 0) console.groupEnd();\n}\n\n/**\n * Logs a trace to the browser/Node console via `console.group`/`console.groupEnd`, one collapsible\n * group per call with children, a plain `console.log` line for a leaf. Bounded and cycle-safe: a\n * hand-built, replayed or deserialized tree — cyclic or merely very deep — logs a\n * \"… (depth limit)\"/\"… (cycle)\" marker line instead of crashing or hanging.\n */\nexport function renderToConsole(tree: TraceTree): void {\n const traceId = tree.roots[0]?.spanContext?.traceId;\n if (traceId) {\n console.log(`[${humanName(traceId as TraceId)}]`);\n }\n walkPreOrder(tree.roots, (n) => n.children, enterNode, exitNode);\n}\n","// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport { exportJson, type TraceMetadata, type TraceTree } from \"@narrativetrace/core-web\";\n\nexport async function postToCollector(\n tree: TraceTree,\n metadata: TraceMetadata,\n collectorUrl: string,\n): Promise<Response> {\n const body = exportJson(tree, metadata);\n return fetch(collectorUrl, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body,\n });\n}\n","// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport { formatTraceparent, generateSpanId, type NarrativeContext } from \"@narrativetrace/core-web\";\n\nexport function tracedFetch(ctx: NarrativeContext): typeof globalThis.fetch {\n return (input, init) => {\n const base = input instanceof Request ? input.headers : undefined;\n const headers = new Headers(init?.headers ?? base);\n headers.set(\"traceparent\", formatTraceparent(ctx.traceId(), generateSpanId()));\n return globalThis.fetch(input, { ...init, headers });\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,sBAQO;AAEP,SAAS,WAAW,MAAyB;AAC3C,QAAM,EAAE,WAAW,YAAY,WAAW,IAAI,KAAK;AACnD,QAAM,SAAS,WAAW,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,KAAK,EAAE,aAAa,EAAE,EAAE,KAAK,IAAI;AAC/E,SAAO,GAAG,SAAS,IAAI,UAAU,IAAI,MAAM;AAC7C;AAEA,SAAS,cAAc,MAAyB;AAC9C,MAAI,KAAK,QAAQ,SAAS,SAAS;AACjC,UAAM,MAAM,KAAK,QAAQ;AACzB,UAAM,MAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC3D,WAAO,WAAW,GAAG;AAAA,EACvB;AACA,MAAI,KAAK,QAAQ,SAAS,aAAc,QAAO;AAC/C,QAAM,MAAM,KAAK,QAAQ;AACzB,MAAI,QAAQ,QAAQ,QAAQ,aAAa;AACvC,WAAO,WAAW,GAAG;AAAA,EACvB;AACA,SAAO;AACT;AAEA,SAAS,aAAa,MAAuB;AAC3C,MAAI,KAAK,UAAU,cAAc,QAAW;AAC1C,YAAQ,IAAI,KAAK,KAAK,UAAU,SAAS,EAAE;AAAA,EAC7C;AACF;AAKA,SAAS,UAAU,MAAiB,MAAsC;AACxE,QAAM,QAAQ,GAAG,WAAW,IAAI,CAAC,GAAG,cAAc,IAAI,CAAC;AACvD,MAAI,SAAS,QAAW;AACtB,YAAQ,MAAM,KAAK;AACnB,iBAAa,IAAI;AACjB,YAAQ,IAAI,iCAAiB,IAAI,CAAC;AAClC,YAAQ,SAAS;AACjB;AAAA,EACF;AACA,MAAI,KAAK,SAAS,WAAW,GAAG;AAC9B,YAAQ,IAAI,KAAK;AACjB,iBAAa,IAAI;AACjB;AAAA,EACF;AACA,UAAQ,MAAM,KAAK;AACnB,eAAa,IAAI;AACnB;AAIA,SAAS,SAAS,MAAuB;AACvC,MAAI,KAAK,SAAS,SAAS,EAAG,SAAQ,SAAS;AACjD;AAQO,SAAS,gBAAgB,MAAuB;AACrD,QAAM,UAAU,KAAK,MAAM,CAAC,GAAG,aAAa;AAC5C,MAAI,SAAS;AACX,YAAQ,IAAI,QAAI,2BAAU,OAAkB,CAAC,GAAG;AAAA,EAClD;AACA,oCAAa,KAAK,OAAO,CAAC,MAAM,EAAE,UAAU,WAAW,QAAQ;AACjE;;;AC3EA,IAAAA,mBAA+D;AAE/D,eAAsB,gBACpB,MACA,UACA,cACmB;AACnB,QAAM,WAAO,6BAAW,MAAM,QAAQ;AACtC,SAAO,MAAM,cAAc;AAAA,IACzB,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,IAC9C;AAAA,EACF,CAAC;AACH;;;ACbA,IAAAC,mBAAyE;AAElE,SAAS,YAAY,KAAgD;AAC1E,SAAO,CAAC,OAAO,SAAS;AACtB,UAAM,OAAO,iBAAiB,UAAU,MAAM,UAAU;AACxD,UAAM,UAAU,IAAI,QAAQ,MAAM,WAAW,IAAI;AACjD,YAAQ,IAAI,mBAAe,oCAAkB,IAAI,QAAQ,OAAG,iCAAe,CAAC,CAAC;AAC7E,WAAO,WAAW,MAAM,OAAO,EAAE,GAAG,MAAM,QAAQ,CAAC;AAAA,EACrD;AACF;","names":["import_core_web","import_core_web"]}
@@ -0,0 +1,15 @@
1
+ import { TraceTree, TraceMetadata, NarrativeContext } from '@narrativetrace/core-web';
2
+
3
+ /**
4
+ * Logs a trace to the browser/Node console via `console.group`/`console.groupEnd`, one collapsible
5
+ * group per call with children, a plain `console.log` line for a leaf. Bounded and cycle-safe: a
6
+ * hand-built, replayed or deserialized tree — cyclic or merely very deep — logs a
7
+ * "… (depth limit)"/"… (cycle)" marker line instead of crashing or hanging.
8
+ */
9
+ declare function renderToConsole(tree: TraceTree): void;
10
+
11
+ declare function postToCollector(tree: TraceTree, metadata: TraceMetadata, collectorUrl: string): Promise<Response>;
12
+
13
+ declare function tracedFetch(ctx: NarrativeContext): typeof globalThis.fetch;
14
+
15
+ export { postToCollector, renderToConsole, tracedFetch };
@@ -0,0 +1,15 @@
1
+ import { TraceTree, TraceMetadata, NarrativeContext } from '@narrativetrace/core-web';
2
+
3
+ /**
4
+ * Logs a trace to the browser/Node console via `console.group`/`console.groupEnd`, one collapsible
5
+ * group per call with children, a plain `console.log` line for a leaf. Bounded and cycle-safe: a
6
+ * hand-built, replayed or deserialized tree — cyclic or merely very deep — logs a
7
+ * "… (depth limit)"/"… (cycle)" marker line instead of crashing or hanging.
8
+ */
9
+ declare function renderToConsole(tree: TraceTree): void;
10
+
11
+ declare function postToCollector(tree: TraceTree, metadata: TraceMetadata, collectorUrl: string): Promise<Response>;
12
+
13
+ declare function tracedFetch(ctx: NarrativeContext): typeof globalThis.fetch;
14
+
15
+ export { postToCollector, renderToConsole, tracedFetch };
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ // src/console-renderer.ts
2
+ import {
3
+ humanName,
4
+ TREE_WALK_MARKER,
5
+ walkPreOrder
6
+ } from "@narrativetrace/core-web";
7
+ function formatCall(node) {
8
+ const { className, methodName, parameters } = node.signature;
9
+ const params = parameters.map((p) => `${p.name}: ${p.renderedValue}`).join(", ");
10
+ return `${className}.${methodName}(${params})`;
11
+ }
12
+ function formatOutcome(node) {
13
+ if (node.outcome.kind === "threw") {
14
+ const err = node.outcome.error;
15
+ const msg = err instanceof Error ? err.message : String(err);
16
+ return ` \u2717 ${msg}`;
17
+ }
18
+ if (node.outcome.kind === "incomplete") return " \u23F3 (incomplete)";
19
+ const ret = node.outcome.renderedValue;
20
+ if (ret !== null && ret !== "undefined") {
21
+ return ` \u2192 ${ret}`;
22
+ }
23
+ return "";
24
+ }
25
+ function logNarration(node) {
26
+ if (node.signature.narration !== void 0) {
27
+ console.log(` ${node.signature.narration}`);
28
+ }
29
+ }
30
+ function enterNode(node, stop) {
31
+ const label = `${formatCall(node)}${formatOutcome(node)}`;
32
+ if (stop !== void 0) {
33
+ console.group(label);
34
+ logNarration(node);
35
+ console.log(TREE_WALK_MARKER[stop]);
36
+ console.groupEnd();
37
+ return;
38
+ }
39
+ if (node.children.length === 0) {
40
+ console.log(label);
41
+ logNarration(node);
42
+ return;
43
+ }
44
+ console.group(label);
45
+ logNarration(node);
46
+ }
47
+ function exitNode(node) {
48
+ if (node.children.length > 0) console.groupEnd();
49
+ }
50
+ function renderToConsole(tree) {
51
+ const traceId = tree.roots[0]?.spanContext?.traceId;
52
+ if (traceId) {
53
+ console.log(`[${humanName(traceId)}]`);
54
+ }
55
+ walkPreOrder(tree.roots, (n) => n.children, enterNode, exitNode);
56
+ }
57
+
58
+ // src/network-export.ts
59
+ import { exportJson } from "@narrativetrace/core-web";
60
+ async function postToCollector(tree, metadata, collectorUrl) {
61
+ const body = exportJson(tree, metadata);
62
+ return fetch(collectorUrl, {
63
+ method: "POST",
64
+ headers: { "Content-Type": "application/json" },
65
+ body
66
+ });
67
+ }
68
+
69
+ // src/traced-fetch.ts
70
+ import { formatTraceparent, generateSpanId } from "@narrativetrace/core-web";
71
+ function tracedFetch(ctx) {
72
+ return (input, init) => {
73
+ const base = input instanceof Request ? input.headers : void 0;
74
+ const headers = new Headers(init?.headers ?? base);
75
+ headers.set("traceparent", formatTraceparent(ctx.traceId(), generateSpanId()));
76
+ return globalThis.fetch(input, { ...init, headers });
77
+ };
78
+ }
79
+ export {
80
+ postToCollector,
81
+ renderToConsole,
82
+ tracedFetch
83
+ };
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/console-renderer.ts","../src/network-export.ts","../src/traced-fetch.ts"],"sourcesContent":["// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport {\n humanName,\n TREE_WALK_MARKER,\n type TraceId,\n type TraceNode,\n type TraceTree,\n type TreeWalkStop,\n walkPreOrder,\n} from \"@narrativetrace/core-web\";\n\nfunction formatCall(node: TraceNode): string {\n const { className, methodName, parameters } = node.signature;\n const params = parameters.map((p) => `${p.name}: ${p.renderedValue}`).join(\", \");\n return `${className}.${methodName}(${params})`;\n}\n\nfunction formatOutcome(node: TraceNode): string {\n if (node.outcome.kind === \"threw\") {\n const err = node.outcome.error;\n const msg = err instanceof Error ? err.message : String(err);\n return ` \\u2717 ${msg}`;\n }\n if (node.outcome.kind === \"incomplete\") return \" \\u23F3 (incomplete)\";\n const ret = node.outcome.renderedValue;\n if (ret !== null && ret !== \"undefined\") {\n return ` \\u2192 ${ret}`;\n }\n return \"\";\n}\n\nfunction logNarration(node: TraceNode): void {\n if (node.signature.narration !== undefined) {\n console.log(` ${node.signature.narration}`);\n }\n}\n\n// The node's own line prints regardless of the guard (\"contributes itself\"); a stopped node opens\n// and closes its own group inline (walkPreOrder never calls exitNode for it) instead of leaving an\n// unmatched console.group() open.\nfunction enterNode(node: TraceNode, stop: TreeWalkStop | undefined): void {\n const label = `${formatCall(node)}${formatOutcome(node)}`;\n if (stop !== undefined) {\n console.group(label);\n logNarration(node);\n console.log(TREE_WALK_MARKER[stop]);\n console.groupEnd();\n return;\n }\n if (node.children.length === 0) {\n console.log(label);\n logNarration(node);\n return;\n }\n console.group(label);\n logNarration(node);\n}\n\n// Only a node whose group is still open (it had children and was not stopped) needs closing —\n// walkPreOrder calls this exactly once per such node, after all its descendants are visited.\nfunction exitNode(node: TraceNode): void {\n if (node.children.length > 0) console.groupEnd();\n}\n\n/**\n * Logs a trace to the browser/Node console via `console.group`/`console.groupEnd`, one collapsible\n * group per call with children, a plain `console.log` line for a leaf. Bounded and cycle-safe: a\n * hand-built, replayed or deserialized tree — cyclic or merely very deep — logs a\n * \"… (depth limit)\"/\"… (cycle)\" marker line instead of crashing or hanging.\n */\nexport function renderToConsole(tree: TraceTree): void {\n const traceId = tree.roots[0]?.spanContext?.traceId;\n if (traceId) {\n console.log(`[${humanName(traceId as TraceId)}]`);\n }\n walkPreOrder(tree.roots, (n) => n.children, enterNode, exitNode);\n}\n","// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport { exportJson, type TraceMetadata, type TraceTree } from \"@narrativetrace/core-web\";\n\nexport async function postToCollector(\n tree: TraceTree,\n metadata: TraceMetadata,\n collectorUrl: string,\n): Promise<Response> {\n const body = exportJson(tree, metadata);\n return fetch(collectorUrl, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body,\n });\n}\n","// SPDX-License-Identifier: BUSL-1.1\n// Licensed under the Business Source License 1.1 (see LICENSE); Change Date: four years from publication; Change License: Apache-2.0\n// Copyright (c) 2026 Empower Agile\nimport { formatTraceparent, generateSpanId, type NarrativeContext } from \"@narrativetrace/core-web\";\n\nexport function tracedFetch(ctx: NarrativeContext): typeof globalThis.fetch {\n return (input, init) => {\n const base = input instanceof Request ? input.headers : undefined;\n const headers = new Headers(init?.headers ?? base);\n headers.set(\"traceparent\", formatTraceparent(ctx.traceId(), generateSpanId()));\n return globalThis.fetch(input, { ...init, headers });\n };\n}\n"],"mappings":";AAGA;AAAA,EACE;AAAA,EACA;AAAA,EAKA;AAAA,OACK;AAEP,SAAS,WAAW,MAAyB;AAC3C,QAAM,EAAE,WAAW,YAAY,WAAW,IAAI,KAAK;AACnD,QAAM,SAAS,WAAW,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,KAAK,EAAE,aAAa,EAAE,EAAE,KAAK,IAAI;AAC/E,SAAO,GAAG,SAAS,IAAI,UAAU,IAAI,MAAM;AAC7C;AAEA,SAAS,cAAc,MAAyB;AAC9C,MAAI,KAAK,QAAQ,SAAS,SAAS;AACjC,UAAM,MAAM,KAAK,QAAQ;AACzB,UAAM,MAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAC3D,WAAO,WAAW,GAAG;AAAA,EACvB;AACA,MAAI,KAAK,QAAQ,SAAS,aAAc,QAAO;AAC/C,QAAM,MAAM,KAAK,QAAQ;AACzB,MAAI,QAAQ,QAAQ,QAAQ,aAAa;AACvC,WAAO,WAAW,GAAG;AAAA,EACvB;AACA,SAAO;AACT;AAEA,SAAS,aAAa,MAAuB;AAC3C,MAAI,KAAK,UAAU,cAAc,QAAW;AAC1C,YAAQ,IAAI,KAAK,KAAK,UAAU,SAAS,EAAE;AAAA,EAC7C;AACF;AAKA,SAAS,UAAU,MAAiB,MAAsC;AACxE,QAAM,QAAQ,GAAG,WAAW,IAAI,CAAC,GAAG,cAAc,IAAI,CAAC;AACvD,MAAI,SAAS,QAAW;AACtB,YAAQ,MAAM,KAAK;AACnB,iBAAa,IAAI;AACjB,YAAQ,IAAI,iBAAiB,IAAI,CAAC;AAClC,YAAQ,SAAS;AACjB;AAAA,EACF;AACA,MAAI,KAAK,SAAS,WAAW,GAAG;AAC9B,YAAQ,IAAI,KAAK;AACjB,iBAAa,IAAI;AACjB;AAAA,EACF;AACA,UAAQ,MAAM,KAAK;AACnB,eAAa,IAAI;AACnB;AAIA,SAAS,SAAS,MAAuB;AACvC,MAAI,KAAK,SAAS,SAAS,EAAG,SAAQ,SAAS;AACjD;AAQO,SAAS,gBAAgB,MAAuB;AACrD,QAAM,UAAU,KAAK,MAAM,CAAC,GAAG,aAAa;AAC5C,MAAI,SAAS;AACX,YAAQ,IAAI,IAAI,UAAU,OAAkB,CAAC,GAAG;AAAA,EAClD;AACA,eAAa,KAAK,OAAO,CAAC,MAAM,EAAE,UAAU,WAAW,QAAQ;AACjE;;;AC3EA,SAAS,kBAAsD;AAE/D,eAAsB,gBACpB,MACA,UACA,cACmB;AACnB,QAAM,OAAO,WAAW,MAAM,QAAQ;AACtC,SAAO,MAAM,cAAc;AAAA,IACzB,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,IAC9C;AAAA,EACF,CAAC;AACH;;;ACbA,SAAS,mBAAmB,sBAA6C;AAElE,SAAS,YAAY,KAAgD;AAC1E,SAAO,CAAC,OAAO,SAAS;AACtB,UAAM,OAAO,iBAAiB,UAAU,MAAM,UAAU;AACxD,UAAM,UAAU,IAAI,QAAQ,MAAM,WAAW,IAAI;AACjD,YAAQ,IAAI,eAAe,kBAAkB,IAAI,QAAQ,GAAG,eAAe,CAAC,CAAC;AAC7E,WAAO,WAAW,MAAM,OAAO,EAAE,GAAG,MAAM,QAAQ,CAAC;AAAA,EACrD;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@narrativetrace/browser",
3
+ "version": "0.1.0",
4
+ "description": "Browser rendering and export for captured traces: console output, fetch header propagation, and posting to a collector",
5
+ "license": "BUSL-1.1",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/narrativetrace/narrativetrace-typescript.git",
9
+ "directory": "packages/browser"
10
+ },
11
+ "publishConfig": {
12
+ "provenance": true,
13
+ "access": "public"
14
+ },
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "peerDependencies": {
32
+ "@narrativetrace/core-web": "0.1.0",
33
+ "@narrativetrace/proxy": "0.1.0"
34
+ },
35
+ "devDependencies": {
36
+ "@narrativetrace/core-web": "0.1.0",
37
+ "@narrativetrace/proxy": "0.1.0"
38
+ },
39
+ "scripts": {
40
+ "build": "tsup",
41
+ "test": "vitest run",
42
+ "coverage": "vitest run --coverage",
43
+ "mutate": "stryker run",
44
+ "build:site": "tsup --config tsup.site.config.ts"
45
+ }
46
+ }