trace-kdzs-sdk 0.0.0-stage → 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/README.md +86 -2
- package/dist/common-BKQhi1LZ.cjs +22 -0
- package/dist/common-BKQhi1LZ.cjs.map +1 -0
- package/dist/common-CvLLuxEe.mjs +17 -0
- package/dist/common-CvLLuxEe.mjs.map +1 -0
- package/dist/core.cjs +55 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +2 -0
- package/dist/core.d.mts +2 -0
- package/dist/core.mjs +53 -0
- package/dist/core.mjs.map +1 -0
- package/dist/index-FHW2y4dt.d.cts +42 -0
- package/dist/index-FHW2y4dt.d.mts +42 -0
- package/dist/index.cjs +23 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +19 -0
- package/dist/index.mjs.map +1 -0
- package/dist/web-CDlgH2Jq.mjs +80 -0
- package/dist/web-CDlgH2Jq.mjs.map +1 -0
- package/dist/web-Dbjr9K0w.cjs +85 -0
- package/dist/web-Dbjr9K0w.cjs.map +1 -0
- package/dist/web.cjs +3 -0
- package/dist/web.d.cts +16 -0
- package/dist/web.d.mts +16 -0
- package/dist/web.mjs +1 -0
- package/docs/TRACE-SDK-API.md +68 -0
- package/package.json +54 -4
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
#
|
|
1
|
+
# trace-kdzs-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
每个页面独立保存 `{ flowId, traceId, spanId }`,同域请求自动带上这三个 ID。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install trace-kdzs-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createTrace, web } from 'trace-kdzs-sdk'
|
|
15
|
+
|
|
16
|
+
// 在 app.tsx 等应用入口初始化一次。
|
|
17
|
+
export const trace = createTrace({
|
|
18
|
+
adapter: web,
|
|
19
|
+
// hash 路由需要配置;默认使用 location.pathname。
|
|
20
|
+
pageKey: location => location.hash.slice(1).split('?')[0] || '/',
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
trace.setFlowId('/orders', 'business-flow')
|
|
24
|
+
trace.setTraceId('/orders', 'business-trace')
|
|
25
|
+
trace.getContext('/orders')
|
|
26
|
+
trace.removePage('/orders') // 关闭业务 Tab 时删除状态
|
|
27
|
+
trace.clear() // 账号切换时清空状态
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
第一次打开页面生成 flowId,traceId 默认与它相同。每页 spanId 从 0 开始,请求发起时加 1;fetch 和 XHR 共用该页计数。切到其他页面再切回来,会继续原来的 ID 和序号。
|
|
31
|
+
|
|
32
|
+
## 自定义
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const trace = createTrace({
|
|
36
|
+
adapter: web,
|
|
37
|
+
context: { appId: 'erp' },
|
|
38
|
+
flowId: (ctx, { pageKey }) => `${ctx.appId}_${crypto.randomUUID()}`,
|
|
39
|
+
traceId: (_ctx, { flowId }) => flowId,
|
|
40
|
+
domain: 'https://api.example.com',
|
|
41
|
+
pageKey: location => location.pathname,
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
context 原样传给生成函数,生成函数只在创建页面状态时执行。默认 ID 使用时间、递增号和随机值,用于日志关联;业务可以提供自己的生成方式。
|
|
46
|
+
|
|
47
|
+
domain 默认当前页面 origin,精确比较协议、域名和端口。传完整 origin(如 `https://api.example.com`),支持数组,不自动补协议。配置后替代默认范围,外站请求不添加头、不消耗序号。
|
|
48
|
+
|
|
49
|
+
请求头固定为 `X-Flow-Id`、`X-Trace-Id`、`X-Span-Id`,由 SDK 写入;业务自己的请求头保留。`no-cors` 请求不添加追踪头。显式允许跨域 API 时,服务端需要支持这三个头。
|
|
50
|
+
|
|
51
|
+
## 结构
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
src/
|
|
55
|
+
index.ts # 对外导出
|
|
56
|
+
createTrace.ts # 创建 core,启动选中的 adapter
|
|
57
|
+
types.ts # 创建参数
|
|
58
|
+
core/
|
|
59
|
+
index.ts # TraceCore:页面 Map、ID、请求序号
|
|
60
|
+
types.ts # 核心类型
|
|
61
|
+
adapters/
|
|
62
|
+
common/index.ts # CommonAdapter:公共初始化
|
|
63
|
+
web/
|
|
64
|
+
index.ts # WebAdapter extends CommonAdapter
|
|
65
|
+
pageKey.ts # 页面标识、路由监听
|
|
66
|
+
request.ts # fetch/XHR 拦截、domain 判断、写头
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
createTrace 返回 TraceCore。不传 adapter 时只创建 core;不提供 `.web()`、`stop()`、请求筛选、头名配置或错误上报回调。自定义生成函数的异常直接抛出。
|
|
70
|
+
|
|
71
|
+
按请求发起时的当前页面关联;已发出的请求保存当时的 ID。隐藏 Tab 的后台任务不能自动推断原页面。同路由的多个业务 Tab 需要使用不同的 pageKey。
|
|
72
|
+
|
|
73
|
+
## 开发
|
|
74
|
+
|
|
75
|
+
构建工具需要 Node.js `^22.18.0 || ^24.11.0 || >=26.0.0`;本项目发布验证使用 Node.js 24。
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm ci
|
|
79
|
+
npm run check
|
|
80
|
+
npm test
|
|
81
|
+
npm run test:browser
|
|
82
|
+
npm run pack:check
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
无运行时依赖,提供 ESM/CJS 与类型声明。Web 面向具备原生 fetch、Request、XMLHttpRequest 的浏览器。无 DOM 环境从 `trace-kdzs-sdk/core` 导入 TraceCore/CommonAdapter;WebAdapter 也从 `trace-kdzs-sdk/web` 导出。RN、小程序后续扩展。
|
|
86
|
+
|
|
87
|
+
完整方法说明见 [API](docs/TRACE-SDK-API.md)。
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/adapters/common/index.ts
|
|
2
|
+
var CommonAdapter = class {
|
|
3
|
+
constructor(core) {
|
|
4
|
+
this.core = core;
|
|
5
|
+
this.started = false;
|
|
6
|
+
}
|
|
7
|
+
start() {
|
|
8
|
+
if (this.started) return;
|
|
9
|
+
this.core.ensurePage(this.getPageKey());
|
|
10
|
+
this.interceptRequests();
|
|
11
|
+
this.watchPageChange(() => this.core.ensurePage(this.getPageKey()));
|
|
12
|
+
this.started = true;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "CommonAdapter", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function() {
|
|
18
|
+
return CommonAdapter;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=common-BKQhi1LZ.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-BKQhi1LZ.cjs","names":[],"sources":["../src/adapters/common/index.ts"],"sourcesContent":["import type { TraceCore } from '../../core'\n\nexport abstract class CommonAdapter {\n private started: boolean = false\n\n constructor(protected readonly core: TraceCore) {}\n\n start(): void {\n if (this.started) return\n this.core.ensurePage(this.getPageKey())\n this.interceptRequests()\n // 切换页面只初始化缺失的记录,切回已有页面继续原来的 ID 和序号。\n this.watchPageChange(() => this.core.ensurePage(this.getPageKey()))\n this.started = true\n }\n\n protected abstract getPageKey(): string\n protected abstract watchPageChange(onChange: () => void): void\n protected abstract interceptRequests(): void\n}\n"],"mappings":";AAEA,IAAsB,gBAAtB,MAAoC;CAGlC,YAA+B,MAAiB;EAAjB,KAAA,OAAA;EAF/B,KAAQ,UAAmB;CAEsB;CAEjD,QAAc;EACZ,IAAI,KAAK,SAAS;EAClB,KAAK,KAAK,WAAW,KAAK,WAAW,CAAC;EACtC,KAAK,kBAAkB;EAEvB,KAAK,sBAAsB,KAAK,KAAK,WAAW,KAAK,WAAW,CAAC,CAAC;EAClE,KAAK,UAAU;CACjB;AAKF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/adapters/common/index.ts
|
|
2
|
+
var CommonAdapter = class {
|
|
3
|
+
constructor(core) {
|
|
4
|
+
this.core = core;
|
|
5
|
+
this.started = false;
|
|
6
|
+
}
|
|
7
|
+
start() {
|
|
8
|
+
if (this.started) return;
|
|
9
|
+
this.core.ensurePage(this.getPageKey());
|
|
10
|
+
this.interceptRequests();
|
|
11
|
+
this.watchPageChange(() => this.core.ensurePage(this.getPageKey()));
|
|
12
|
+
this.started = true;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
export { CommonAdapter as t };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=common-CvLLuxEe.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-CvLLuxEe.mjs","names":[],"sources":["../src/adapters/common/index.ts"],"sourcesContent":["import type { TraceCore } from '../../core'\n\nexport abstract class CommonAdapter {\n private started: boolean = false\n\n constructor(protected readonly core: TraceCore) {}\n\n start(): void {\n if (this.started) return\n this.core.ensurePage(this.getPageKey())\n this.interceptRequests()\n // 切换页面只初始化缺失的记录,切回已有页面继续原来的 ID 和序号。\n this.watchPageChange(() => this.core.ensurePage(this.getPageKey()))\n this.started = true\n }\n\n protected abstract getPageKey(): string\n protected abstract watchPageChange(onChange: () => void): void\n protected abstract interceptRequests(): void\n}\n"],"mappings":";AAEA,IAAsB,gBAAtB,MAAoC;CAGlC,YAA+B,MAAiB;EAAjB,KAAA,OAAA;EAF/B,KAAQ,UAAmB;CAEsB;CAEjD,QAAc;EACZ,IAAI,KAAK,SAAS;EAClB,KAAK,KAAK,WAAW,KAAK,WAAW,CAAC;EACtC,KAAK,kBAAkB;EAEvB,KAAK,sBAAsB,KAAK,KAAK,WAAW,KAAK,WAAW,CAAC,CAAC;EAClE,KAAK,UAAU;CACjB;AAKF"}
|
package/dist/core.cjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_common = require("./common-BKQhi1LZ.cjs");
|
|
3
|
+
// src/core/index.ts
|
|
4
|
+
let sequence = 0;
|
|
5
|
+
function generateId() {
|
|
6
|
+
return `${Date.now().toString(36)}-${(++sequence).toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
7
|
+
}
|
|
8
|
+
var TraceCore = class {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
var _options$generateFlow, _options$generateTrac;
|
|
11
|
+
this.pages = /* @__PURE__ */ new Map();
|
|
12
|
+
this.generateFlowId = (_options$generateFlow = options.generateFlowId) !== null && _options$generateFlow !== void 0 ? _options$generateFlow : generateId;
|
|
13
|
+
this.generateTraceId = (_options$generateTrac = options.generateTraceId) !== null && _options$generateTrac !== void 0 ? _options$generateTrac : ((_pageKey, flowId) => flowId);
|
|
14
|
+
}
|
|
15
|
+
ensurePage(pageKey) {
|
|
16
|
+
return { ...this.getOrCreatePage(pageKey) };
|
|
17
|
+
}
|
|
18
|
+
getContext(pageKey) {
|
|
19
|
+
const page = this.pages.get(pageKey);
|
|
20
|
+
return page ? { ...page } : void 0;
|
|
21
|
+
}
|
|
22
|
+
setFlowId(pageKey, flowId) {
|
|
23
|
+
this.getOrCreatePage(pageKey).flowId = flowId;
|
|
24
|
+
}
|
|
25
|
+
setTraceId(pageKey, traceId) {
|
|
26
|
+
this.getOrCreatePage(pageKey).traceId = traceId;
|
|
27
|
+
}
|
|
28
|
+
nextRequestContext(pageKey) {
|
|
29
|
+
const page = this.getOrCreatePage(pageKey);
|
|
30
|
+
page.spanId += 1;
|
|
31
|
+
return { ...page };
|
|
32
|
+
}
|
|
33
|
+
removePage(pageKey) {
|
|
34
|
+
return this.pages.delete(pageKey);
|
|
35
|
+
}
|
|
36
|
+
clear() {
|
|
37
|
+
this.pages.clear();
|
|
38
|
+
}
|
|
39
|
+
getOrCreatePage(pageKey) {
|
|
40
|
+
const existing = this.pages.get(pageKey);
|
|
41
|
+
if (existing) return existing;
|
|
42
|
+
const flowId = this.generateFlowId(pageKey);
|
|
43
|
+
const page = {
|
|
44
|
+
flowId,
|
|
45
|
+
traceId: this.generateTraceId(pageKey, flowId),
|
|
46
|
+
spanId: 0
|
|
47
|
+
};
|
|
48
|
+
this.pages.set(pageKey, page);
|
|
49
|
+
return page;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
exports.CommonAdapter = require_common.CommonAdapter;
|
|
53
|
+
exports.TraceCore = TraceCore;
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=core.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.cjs","names":[],"sources":["../src/core/index.ts"],"sourcesContent":["import type { PageContext, TraceCoreOptions } from './types'\n\nexport type { PageContext, TraceCoreOptions, TraceDomain, FlowIdInput, TraceIdInput } from './types'\nexport { CommonAdapter } from '../adapters/common'\n\nlet sequence = 0\n\n// 默认 ID 用于日志关联;业务可以通过生成器提供自己的 ID。\nfunction generateId(): string {\n return `${Date.now().toString(36)}-${(++sequence).toString(36)}-${Math.random().toString(36).slice(2)}`\n}\n\nexport class TraceCore {\n // 状态属于当前 core 实例;不同应用或不同 SDK 实例互不串数据。\n private readonly pages: Map<string, PageContext> = new Map()\n private readonly generateFlowId: (pageKey: string) => string\n private readonly generateTraceId: (pageKey: string, flowId: string) => string\n\n constructor(options: TraceCoreOptions = {}) {\n this.generateFlowId = options.generateFlowId ?? generateId\n this.generateTraceId = options.generateTraceId ?? ((_pageKey, flowId) => flowId)\n }\n\n ensurePage(pageKey: string): PageContext {\n return { ...this.getOrCreatePage(pageKey) }\n }\n\n getContext(pageKey: string): PageContext | undefined {\n const page = this.pages.get(pageKey)\n return page ? { ...page } : undefined\n }\n\n setFlowId(pageKey: string, flowId: string): void {\n this.getOrCreatePage(pageKey).flowId = flowId\n }\n\n setTraceId(pageKey: string, traceId: string): void {\n this.getOrCreatePage(pageKey).traceId = traceId\n }\n\n nextRequestContext(pageKey: string): PageContext {\n const page = this.getOrCreatePage(pageKey)\n page.spanId += 1\n // 按发起顺序取号,并复制本次请求的 ID;后续 setter 不会改写在途请求。\n return { ...page }\n }\n\n removePage(pageKey: string): boolean {\n return this.pages.delete(pageKey)\n }\n\n clear(): void {\n this.pages.clear()\n }\n\n private getOrCreatePage(pageKey: string): PageContext {\n const existing = this.pages.get(pageKey)\n if (existing) return existing\n const flowId = this.generateFlowId(pageKey)\n const traceId = this.generateTraceId(pageKey, flowId)\n // 两个 ID 都生成后才保存;新页面的请求序号从 0 开始。\n const page: PageContext = { flowId, traceId, spanId: 0 }\n this.pages.set(pageKey, page)\n return page\n }\n}\n"],"mappings":";;;AAKA,IAAI,WAAW;AAGf,SAAS,aAAqB;CAC5B,OAAO,GAAG,KAAK,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,SAAA,CAAU,SAAS,EAAE,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC;AACtG;AAEA,IAAa,YAAb,MAAuB;CAMrB,YAAY,UAA4B,CAAC,GAAG;;EAJ5C,KAAiB,wBAAkC,IAAI,IAAI;EAKzD,KAAK,kBAAA,wBAAiB,QAAQ,oBAAA,QAAA,0BAAA,KAAA,IAAA,wBAAkB;EAChD,KAAK,mBAAA,wBAAkB,QAAQ,qBAAA,QAAA,0BAAA,KAAA,IAAA,0BAAqB,UAAU,WAAW;CAC3E;CAEA,WAAW,SAA8B;EACvC,OAAO,EAAE,GAAG,KAAK,gBAAgB,OAAO,EAAE;CAC5C;CAEA,WAAW,SAA0C;EACnD,MAAM,OAAO,KAAK,MAAM,IAAI,OAAO;EACnC,OAAO,OAAO,EAAE,GAAG,KAAK,IAAI,KAAA;CAC9B;CAEA,UAAU,SAAiB,QAAsB;EAC/C,KAAK,gBAAgB,OAAO,CAAC,CAAC,SAAS;CACzC;CAEA,WAAW,SAAiB,SAAuB;EACjD,KAAK,gBAAgB,OAAO,CAAC,CAAC,UAAU;CAC1C;CAEA,mBAAmB,SAA8B;EAC/C,MAAM,OAAO,KAAK,gBAAgB,OAAO;EACzC,KAAK,UAAU;EAEf,OAAO,EAAE,GAAG,KAAK;CACnB;CAEA,WAAW,SAA0B;EACnC,OAAO,KAAK,MAAM,OAAO,OAAO;CAClC;CAEA,QAAc;EACZ,KAAK,MAAM,MAAM;CACnB;CAEQ,gBAAgB,SAA8B;EACpD,MAAM,WAAW,KAAK,MAAM,IAAI,OAAO;EACvC,IAAI,UAAU,OAAO;EACrB,MAAM,SAAS,KAAK,eAAe,OAAO;EAG1C,MAAM,OAAoB;GAAE;GAAQ,SAFpB,KAAK,gBAAgB,SAAS,MAEJ;GAAG,QAAQ;EAAE;EACvD,KAAK,MAAM,IAAI,SAAS,IAAI;EAC5B,OAAO;CACT;AACF"}
|
package/dist/core.d.cts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as TraceCoreOptions, i as PageContext, n as CommonAdapter, o as TraceDomain, r as FlowIdInput, s as TraceIdInput, t as TraceCore } from "./index-FHW2y4dt.cjs";
|
|
2
|
+
export { CommonAdapter, type FlowIdInput, type PageContext, TraceCore, type TraceCoreOptions, type TraceDomain, type TraceIdInput };
|
package/dist/core.d.mts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as TraceCoreOptions, i as PageContext, n as CommonAdapter, o as TraceDomain, r as FlowIdInput, s as TraceIdInput, t as TraceCore } from "./index-FHW2y4dt.mjs";
|
|
2
|
+
export { CommonAdapter, type FlowIdInput, type PageContext, TraceCore, type TraceCoreOptions, type TraceDomain, type TraceIdInput };
|
package/dist/core.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { t as CommonAdapter } from "./common-CvLLuxEe.mjs";
|
|
2
|
+
// src/core/index.ts
|
|
3
|
+
let sequence = 0;
|
|
4
|
+
function generateId() {
|
|
5
|
+
return `${Date.now().toString(36)}-${(++sequence).toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
6
|
+
}
|
|
7
|
+
var TraceCore = class {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
var _options$generateFlow, _options$generateTrac;
|
|
10
|
+
this.pages = /* @__PURE__ */ new Map();
|
|
11
|
+
this.generateFlowId = (_options$generateFlow = options.generateFlowId) !== null && _options$generateFlow !== void 0 ? _options$generateFlow : generateId;
|
|
12
|
+
this.generateTraceId = (_options$generateTrac = options.generateTraceId) !== null && _options$generateTrac !== void 0 ? _options$generateTrac : ((_pageKey, flowId) => flowId);
|
|
13
|
+
}
|
|
14
|
+
ensurePage(pageKey) {
|
|
15
|
+
return { ...this.getOrCreatePage(pageKey) };
|
|
16
|
+
}
|
|
17
|
+
getContext(pageKey) {
|
|
18
|
+
const page = this.pages.get(pageKey);
|
|
19
|
+
return page ? { ...page } : void 0;
|
|
20
|
+
}
|
|
21
|
+
setFlowId(pageKey, flowId) {
|
|
22
|
+
this.getOrCreatePage(pageKey).flowId = flowId;
|
|
23
|
+
}
|
|
24
|
+
setTraceId(pageKey, traceId) {
|
|
25
|
+
this.getOrCreatePage(pageKey).traceId = traceId;
|
|
26
|
+
}
|
|
27
|
+
nextRequestContext(pageKey) {
|
|
28
|
+
const page = this.getOrCreatePage(pageKey);
|
|
29
|
+
page.spanId += 1;
|
|
30
|
+
return { ...page };
|
|
31
|
+
}
|
|
32
|
+
removePage(pageKey) {
|
|
33
|
+
return this.pages.delete(pageKey);
|
|
34
|
+
}
|
|
35
|
+
clear() {
|
|
36
|
+
this.pages.clear();
|
|
37
|
+
}
|
|
38
|
+
getOrCreatePage(pageKey) {
|
|
39
|
+
const existing = this.pages.get(pageKey);
|
|
40
|
+
if (existing) return existing;
|
|
41
|
+
const flowId = this.generateFlowId(pageKey);
|
|
42
|
+
const page = {
|
|
43
|
+
flowId,
|
|
44
|
+
traceId: this.generateTraceId(pageKey, flowId),
|
|
45
|
+
spanId: 0
|
|
46
|
+
};
|
|
47
|
+
this.pages.set(pageKey, page);
|
|
48
|
+
return page;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export { CommonAdapter, TraceCore };
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=core.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.mjs","names":[],"sources":["../src/core/index.ts"],"sourcesContent":["import type { PageContext, TraceCoreOptions } from './types'\n\nexport type { PageContext, TraceCoreOptions, TraceDomain, FlowIdInput, TraceIdInput } from './types'\nexport { CommonAdapter } from '../adapters/common'\n\nlet sequence = 0\n\n// 默认 ID 用于日志关联;业务可以通过生成器提供自己的 ID。\nfunction generateId(): string {\n return `${Date.now().toString(36)}-${(++sequence).toString(36)}-${Math.random().toString(36).slice(2)}`\n}\n\nexport class TraceCore {\n // 状态属于当前 core 实例;不同应用或不同 SDK 实例互不串数据。\n private readonly pages: Map<string, PageContext> = new Map()\n private readonly generateFlowId: (pageKey: string) => string\n private readonly generateTraceId: (pageKey: string, flowId: string) => string\n\n constructor(options: TraceCoreOptions = {}) {\n this.generateFlowId = options.generateFlowId ?? generateId\n this.generateTraceId = options.generateTraceId ?? ((_pageKey, flowId) => flowId)\n }\n\n ensurePage(pageKey: string): PageContext {\n return { ...this.getOrCreatePage(pageKey) }\n }\n\n getContext(pageKey: string): PageContext | undefined {\n const page = this.pages.get(pageKey)\n return page ? { ...page } : undefined\n }\n\n setFlowId(pageKey: string, flowId: string): void {\n this.getOrCreatePage(pageKey).flowId = flowId\n }\n\n setTraceId(pageKey: string, traceId: string): void {\n this.getOrCreatePage(pageKey).traceId = traceId\n }\n\n nextRequestContext(pageKey: string): PageContext {\n const page = this.getOrCreatePage(pageKey)\n page.spanId += 1\n // 按发起顺序取号,并复制本次请求的 ID;后续 setter 不会改写在途请求。\n return { ...page }\n }\n\n removePage(pageKey: string): boolean {\n return this.pages.delete(pageKey)\n }\n\n clear(): void {\n this.pages.clear()\n }\n\n private getOrCreatePage(pageKey: string): PageContext {\n const existing = this.pages.get(pageKey)\n if (existing) return existing\n const flowId = this.generateFlowId(pageKey)\n const traceId = this.generateTraceId(pageKey, flowId)\n // 两个 ID 都生成后才保存;新页面的请求序号从 0 开始。\n const page: PageContext = { flowId, traceId, spanId: 0 }\n this.pages.set(pageKey, page)\n return page\n }\n}\n"],"mappings":";;AAKA,IAAI,WAAW;AAGf,SAAS,aAAqB;CAC5B,OAAO,GAAG,KAAK,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,SAAA,CAAU,SAAS,EAAE,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC;AACtG;AAEA,IAAa,YAAb,MAAuB;CAMrB,YAAY,UAA4B,CAAC,GAAG;;EAJ5C,KAAiB,wBAAkC,IAAI,IAAI;EAKzD,KAAK,kBAAA,wBAAiB,QAAQ,oBAAA,QAAA,0BAAA,KAAA,IAAA,wBAAkB;EAChD,KAAK,mBAAA,wBAAkB,QAAQ,qBAAA,QAAA,0BAAA,KAAA,IAAA,0BAAqB,UAAU,WAAW;CAC3E;CAEA,WAAW,SAA8B;EACvC,OAAO,EAAE,GAAG,KAAK,gBAAgB,OAAO,EAAE;CAC5C;CAEA,WAAW,SAA0C;EACnD,MAAM,OAAO,KAAK,MAAM,IAAI,OAAO;EACnC,OAAO,OAAO,EAAE,GAAG,KAAK,IAAI,KAAA;CAC9B;CAEA,UAAU,SAAiB,QAAsB;EAC/C,KAAK,gBAAgB,OAAO,CAAC,CAAC,SAAS;CACzC;CAEA,WAAW,SAAiB,SAAuB;EACjD,KAAK,gBAAgB,OAAO,CAAC,CAAC,UAAU;CAC1C;CAEA,mBAAmB,SAA8B;EAC/C,MAAM,OAAO,KAAK,gBAAgB,OAAO;EACzC,KAAK,UAAU;EAEf,OAAO,EAAE,GAAG,KAAK;CACnB;CAEA,WAAW,SAA0B;EACnC,OAAO,KAAK,MAAM,OAAO,OAAO;CAClC;CAEA,QAAc;EACZ,KAAK,MAAM,MAAM;CACnB;CAEQ,gBAAgB,SAA8B;EACpD,MAAM,WAAW,KAAK,MAAM,IAAI,OAAO;EACvC,IAAI,UAAU,OAAO;EACrB,MAAM,SAAS,KAAK,eAAe,OAAO;EAG1C,MAAM,OAAoB;GAAE;GAAQ,SAFpB,KAAK,gBAAgB,SAAS,MAEJ;GAAG,QAAQ;EAAE;EACvD,KAAK,MAAM,IAAI,SAAS,IAAI;EAC5B,OAAO;CACT;AACF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** 页面状态的副本;外部修改不会影响 core 内部保存的状态。 */
|
|
2
|
+
interface PageContext {
|
|
3
|
+
flowId: string;
|
|
4
|
+
traceId: string;
|
|
5
|
+
spanId: number;
|
|
6
|
+
}
|
|
7
|
+
/** 完整 origin,例如 https://api.example.com;支持多个精确匹配项。 */
|
|
8
|
+
type TraceDomain = string | readonly string[];
|
|
9
|
+
interface TraceCoreOptions {
|
|
10
|
+
generateFlowId?: (pageKey: string) => string;
|
|
11
|
+
generateTraceId?: (pageKey: string, flowId: string) => string;
|
|
12
|
+
}
|
|
13
|
+
interface FlowIdInput {
|
|
14
|
+
pageKey: string;
|
|
15
|
+
}
|
|
16
|
+
interface TraceIdInput extends FlowIdInput {
|
|
17
|
+
flowId: string;
|
|
18
|
+
}
|
|
19
|
+
declare abstract class CommonAdapter {
|
|
20
|
+
protected readonly core: TraceCore;
|
|
21
|
+
private started;
|
|
22
|
+
constructor(core: TraceCore);
|
|
23
|
+
start(): void;
|
|
24
|
+
protected abstract getPageKey(): string;
|
|
25
|
+
protected abstract watchPageChange(onChange: () => void): void;
|
|
26
|
+
protected abstract interceptRequests(): void;
|
|
27
|
+
}
|
|
28
|
+
declare class TraceCore {
|
|
29
|
+
private readonly pages;
|
|
30
|
+
private readonly generateFlowId;
|
|
31
|
+
private readonly generateTraceId;
|
|
32
|
+
constructor(options?: TraceCoreOptions);
|
|
33
|
+
ensurePage(pageKey: string): PageContext;
|
|
34
|
+
getContext(pageKey: string): PageContext | undefined;
|
|
35
|
+
setFlowId(pageKey: string, flowId: string): void;
|
|
36
|
+
setTraceId(pageKey: string, traceId: string): void;
|
|
37
|
+
nextRequestContext(pageKey: string): PageContext;
|
|
38
|
+
removePage(pageKey: string): boolean;
|
|
39
|
+
clear(): void;
|
|
40
|
+
private getOrCreatePage;
|
|
41
|
+
}
|
|
42
|
+
export { TraceCoreOptions as a, PageContext as i, CommonAdapter as n, TraceDomain as o, FlowIdInput as r, TraceIdInput as s, TraceCore as t };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** 页面状态的副本;外部修改不会影响 core 内部保存的状态。 */
|
|
2
|
+
interface PageContext {
|
|
3
|
+
flowId: string;
|
|
4
|
+
traceId: string;
|
|
5
|
+
spanId: number;
|
|
6
|
+
}
|
|
7
|
+
/** 完整 origin,例如 https://api.example.com;支持多个精确匹配项。 */
|
|
8
|
+
type TraceDomain = string | readonly string[];
|
|
9
|
+
interface TraceCoreOptions {
|
|
10
|
+
generateFlowId?: (pageKey: string) => string;
|
|
11
|
+
generateTraceId?: (pageKey: string, flowId: string) => string;
|
|
12
|
+
}
|
|
13
|
+
interface FlowIdInput {
|
|
14
|
+
pageKey: string;
|
|
15
|
+
}
|
|
16
|
+
interface TraceIdInput extends FlowIdInput {
|
|
17
|
+
flowId: string;
|
|
18
|
+
}
|
|
19
|
+
declare abstract class CommonAdapter {
|
|
20
|
+
protected readonly core: TraceCore;
|
|
21
|
+
private started;
|
|
22
|
+
constructor(core: TraceCore);
|
|
23
|
+
start(): void;
|
|
24
|
+
protected abstract getPageKey(): string;
|
|
25
|
+
protected abstract watchPageChange(onChange: () => void): void;
|
|
26
|
+
protected abstract interceptRequests(): void;
|
|
27
|
+
}
|
|
28
|
+
declare class TraceCore {
|
|
29
|
+
private readonly pages;
|
|
30
|
+
private readonly generateFlowId;
|
|
31
|
+
private readonly generateTraceId;
|
|
32
|
+
constructor(options?: TraceCoreOptions);
|
|
33
|
+
ensurePage(pageKey: string): PageContext;
|
|
34
|
+
getContext(pageKey: string): PageContext | undefined;
|
|
35
|
+
setFlowId(pageKey: string, flowId: string): void;
|
|
36
|
+
setTraceId(pageKey: string, traceId: string): void;
|
|
37
|
+
nextRequestContext(pageKey: string): PageContext;
|
|
38
|
+
removePage(pageKey: string): boolean;
|
|
39
|
+
clear(): void;
|
|
40
|
+
private getOrCreatePage;
|
|
41
|
+
}
|
|
42
|
+
export { TraceCoreOptions as a, PageContext as i, CommonAdapter as n, TraceDomain as o, FlowIdInput as r, TraceIdInput as s, TraceCore as t };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_core = require("./core.cjs");
|
|
3
|
+
const require_web = require("./web-Dbjr9K0w.cjs");
|
|
4
|
+
const require_common = require("./common-BKQhi1LZ.cjs");
|
|
5
|
+
// src/createTrace.ts
|
|
6
|
+
function createTrace(options = {}) {
|
|
7
|
+
const { context, flowId, traceId, adapter } = options;
|
|
8
|
+
const coreOptions = {};
|
|
9
|
+
if (flowId) coreOptions.generateFlowId = (pageKey) => flowId(context, { pageKey });
|
|
10
|
+
if (traceId) coreOptions.generateTraceId = (pageKey, id) => traceId(context, {
|
|
11
|
+
pageKey,
|
|
12
|
+
flowId: id
|
|
13
|
+
});
|
|
14
|
+
const core = new require_core.TraceCore(coreOptions);
|
|
15
|
+
if (adapter) new adapter(core, options).start();
|
|
16
|
+
return core;
|
|
17
|
+
}
|
|
18
|
+
exports.CommonAdapter = require_common.CommonAdapter;
|
|
19
|
+
exports.TraceCore = require_core.TraceCore;
|
|
20
|
+
exports.createTrace = createTrace;
|
|
21
|
+
exports.web = require_web.WebAdapter;
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["TraceCore"],"sources":["../src/createTrace.ts"],"sourcesContent":["import { TraceCore } from './core'\nimport type { TraceCoreOptions } from './core/types'\nimport type { CreateTraceDefaultOptions, CreateTraceOptions } from './types'\n\nexport function createTrace(options?: CreateTraceDefaultOptions): TraceCore\nexport function createTrace<TContext>(options: CreateTraceOptions<TContext>): TraceCore\nexport function createTrace<TContext>(options: CreateTraceOptions<TContext> | CreateTraceDefaultOptions = {}): TraceCore {\n // 上下文只交给用户自己的生成函数,core 不读取其中的平台或业务字段。\n // 保存原对象引用,允许调用方在后续新页面创建前更新其内容。\n const { context, flowId, traceId, adapter } = options as CreateTraceOptions<TContext>\n const coreOptions: TraceCoreOptions = {}\n if (flowId) coreOptions.generateFlowId = pageKey => flowId(context, { pageKey })\n if (traceId) coreOptions.generateTraceId = (pageKey, id) => traceId(context, { pageKey, flowId: id })\n const core = new TraceCore(coreOptions)\n // 创建时选定适配器并启动,业务始终使用这一份 core。\n if (adapter) new adapter(core, options).start()\n return core\n}\n"],"mappings":";;;;;AAMA,SAAgB,YAAsB,UAAoE,CAAC,GAAc;CAGvH,MAAM,EAAE,SAAS,QAAQ,SAAS,YAAY;CAC9C,MAAM,cAAgC,CAAC;CACvC,IAAI,QAAQ,YAAY,kBAAiB,YAAW,OAAO,SAAS,EAAE,QAAQ,CAAC;CAC/E,IAAI,SAAS,YAAY,mBAAmB,SAAS,OAAO,QAAQ,SAAS;EAAE;EAAS,QAAQ;CAAG,CAAC;CACpG,MAAM,OAAO,IAAIA,aAAAA,UAAU,WAAW;CAEtC,IAAI,SAAS,IAAI,QAAQ,MAAM,OAAO,CAAC,CAAC,MAAM;CAC9C,OAAO;AACT"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { a as TraceCoreOptions, i as PageContext, n as CommonAdapter, o as TraceDomain, r as FlowIdInput, s as TraceIdInput, t as TraceCore } from "./index-FHW2y4dt.cjs";
|
|
2
|
+
import { WebAdapter, WebAdapterOptions } from "./web.cjs";
|
|
3
|
+
type AdapterConstructor = new (core: TraceCore, options?: WebAdapterOptions) => CommonAdapter;
|
|
4
|
+
interface CreateTraceOptions<TContext> extends WebAdapterOptions {
|
|
5
|
+
adapter?: AdapterConstructor;
|
|
6
|
+
context: TContext;
|
|
7
|
+
flowId?: (context: TContext, input: FlowIdInput) => string;
|
|
8
|
+
traceId?: (context: TContext, input: TraceIdInput) => string;
|
|
9
|
+
}
|
|
10
|
+
type CreateTraceDefaultOptions = Omit<CreateTraceOptions<undefined>, "context"> & {
|
|
11
|
+
context?: undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare function createTrace(options?: CreateTraceDefaultOptions): TraceCore;
|
|
14
|
+
export declare function createTrace<TContext>(options: CreateTraceOptions<TContext>): TraceCore;
|
|
15
|
+
export { type AdapterConstructor, CommonAdapter, type CreateTraceDefaultOptions, type CreateTraceOptions, type FlowIdInput, type PageContext, TraceCore, type TraceCoreOptions, type TraceDomain, type TraceIdInput, type WebAdapterOptions, WebAdapter as web };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { a as TraceCoreOptions, i as PageContext, n as CommonAdapter, o as TraceDomain, r as FlowIdInput, s as TraceIdInput, t as TraceCore } from "./index-FHW2y4dt.mjs";
|
|
2
|
+
import { WebAdapter, WebAdapterOptions } from "./web.mjs";
|
|
3
|
+
type AdapterConstructor = new (core: TraceCore, options?: WebAdapterOptions) => CommonAdapter;
|
|
4
|
+
interface CreateTraceOptions<TContext> extends WebAdapterOptions {
|
|
5
|
+
adapter?: AdapterConstructor;
|
|
6
|
+
context: TContext;
|
|
7
|
+
flowId?: (context: TContext, input: FlowIdInput) => string;
|
|
8
|
+
traceId?: (context: TContext, input: TraceIdInput) => string;
|
|
9
|
+
}
|
|
10
|
+
type CreateTraceDefaultOptions = Omit<CreateTraceOptions<undefined>, "context"> & {
|
|
11
|
+
context?: undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare function createTrace(options?: CreateTraceDefaultOptions): TraceCore;
|
|
14
|
+
export declare function createTrace<TContext>(options: CreateTraceOptions<TContext>): TraceCore;
|
|
15
|
+
export { type AdapterConstructor, CommonAdapter, type CreateTraceDefaultOptions, type CreateTraceOptions, type FlowIdInput, type PageContext, TraceCore, type TraceCoreOptions, type TraceDomain, type TraceIdInput, type WebAdapterOptions, WebAdapter as web };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TraceCore } from "./core.mjs";
|
|
2
|
+
import { t as WebAdapter } from "./web-CDlgH2Jq.mjs";
|
|
3
|
+
import { t as CommonAdapter } from "./common-CvLLuxEe.mjs";
|
|
4
|
+
// src/createTrace.ts
|
|
5
|
+
function createTrace(options = {}) {
|
|
6
|
+
const { context, flowId, traceId, adapter } = options;
|
|
7
|
+
const coreOptions = {};
|
|
8
|
+
if (flowId) coreOptions.generateFlowId = (pageKey) => flowId(context, { pageKey });
|
|
9
|
+
if (traceId) coreOptions.generateTraceId = (pageKey, id) => traceId(context, {
|
|
10
|
+
pageKey,
|
|
11
|
+
flowId: id
|
|
12
|
+
});
|
|
13
|
+
const core = new TraceCore(coreOptions);
|
|
14
|
+
if (adapter) new adapter(core, options).start();
|
|
15
|
+
return core;
|
|
16
|
+
}
|
|
17
|
+
export { CommonAdapter, TraceCore, createTrace, WebAdapter as web };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/createTrace.ts"],"sourcesContent":["import { TraceCore } from './core'\nimport type { TraceCoreOptions } from './core/types'\nimport type { CreateTraceDefaultOptions, CreateTraceOptions } from './types'\n\nexport function createTrace(options?: CreateTraceDefaultOptions): TraceCore\nexport function createTrace<TContext>(options: CreateTraceOptions<TContext>): TraceCore\nexport function createTrace<TContext>(options: CreateTraceOptions<TContext> | CreateTraceDefaultOptions = {}): TraceCore {\n // 上下文只交给用户自己的生成函数,core 不读取其中的平台或业务字段。\n // 保存原对象引用,允许调用方在后续新页面创建前更新其内容。\n const { context, flowId, traceId, adapter } = options as CreateTraceOptions<TContext>\n const coreOptions: TraceCoreOptions = {}\n if (flowId) coreOptions.generateFlowId = pageKey => flowId(context, { pageKey })\n if (traceId) coreOptions.generateTraceId = (pageKey, id) => traceId(context, { pageKey, flowId: id })\n const core = new TraceCore(coreOptions)\n // 创建时选定适配器并启动,业务始终使用这一份 core。\n if (adapter) new adapter(core, options).start()\n return core\n}\n"],"mappings":";;;;AAMA,SAAgB,YAAsB,UAAoE,CAAC,GAAc;CAGvH,MAAM,EAAE,SAAS,QAAQ,SAAS,YAAY;CAC9C,MAAM,cAAgC,CAAC;CACvC,IAAI,QAAQ,YAAY,kBAAiB,YAAW,OAAO,SAAS,EAAE,QAAQ,CAAC;CAC/E,IAAI,SAAS,YAAY,mBAAmB,SAAS,OAAO,QAAQ,SAAS;EAAE;EAAS,QAAQ;CAAG,CAAC;CACpG,MAAM,OAAO,IAAI,UAAU,WAAW;CAEtC,IAAI,SAAS,IAAI,QAAQ,MAAM,OAAO,CAAC,CAAC,MAAM;CAC9C,OAAO;AACT"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { t as CommonAdapter } from "./common-CvLLuxEe.mjs";
|
|
2
|
+
// src/adapters/web/pageKey.ts
|
|
3
|
+
/** Web 页面标识默认取 pathname,hash 路由或业务 Tab 可自行映射。 */
|
|
4
|
+
function getPageKey(target, resolve) {
|
|
5
|
+
return resolve ? resolve(target.location) : target.location.pathname;
|
|
6
|
+
}
|
|
7
|
+
/** 把浏览器路由变化转换成统一的页面变更通知。 */
|
|
8
|
+
function watchPageChange(target, onChange) {
|
|
9
|
+
for (const name of ["pushState", "replaceState"]) {
|
|
10
|
+
const original = target.history[name];
|
|
11
|
+
target.history[name] = function(...args) {
|
|
12
|
+
original.apply(this, args);
|
|
13
|
+
onChange();
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
target.addEventListener("popstate", onChange);
|
|
17
|
+
target.addEventListener("hashchange", onChange);
|
|
18
|
+
}
|
|
19
|
+
// src/adapters/web/request.ts
|
|
20
|
+
/** 只做域名判断和写入追踪头;页面 ID 与序号由 core 提供。 */
|
|
21
|
+
function interceptRequests(target, domain = target.location.origin, getContext) {
|
|
22
|
+
const origins = new Set(typeof domain === "string" ? [domain] : domain);
|
|
23
|
+
const isSameDomain = (url) => origins.has(new target.URL(url, target.document.baseURI).origin);
|
|
24
|
+
const originalFetch = target.fetch;
|
|
25
|
+
target.fetch = function(input, init) {
|
|
26
|
+
const request = new target.Request(input, init);
|
|
27
|
+
if (isSameDomain(request.url) && request.mode !== "no-cors") {
|
|
28
|
+
const { flowId, traceId, spanId } = getContext();
|
|
29
|
+
request.headers.set("X-Flow-Id", flowId);
|
|
30
|
+
request.headers.set("X-Trace-Id", traceId);
|
|
31
|
+
request.headers.set("X-Span-Id", String(spanId));
|
|
32
|
+
}
|
|
33
|
+
return originalFetch.call(target, request);
|
|
34
|
+
};
|
|
35
|
+
const prototype = target.XMLHttpRequest.prototype;
|
|
36
|
+
const originalOpen = prototype.open;
|
|
37
|
+
const originalSend = prototype.send;
|
|
38
|
+
const requests = /* @__PURE__ */ new WeakMap();
|
|
39
|
+
prototype.open = function(...args) {
|
|
40
|
+
requests.set(this, String(args[1]));
|
|
41
|
+
Reflect.apply(originalOpen, this, args);
|
|
42
|
+
};
|
|
43
|
+
prototype.send = function(body) {
|
|
44
|
+
const url = requests.get(this);
|
|
45
|
+
if (url !== void 0 && this.readyState === 1) {
|
|
46
|
+
requests.delete(this);
|
|
47
|
+
if (isSameDomain(url)) {
|
|
48
|
+
const { flowId, traceId, spanId } = getContext();
|
|
49
|
+
this.setRequestHeader("X-Flow-Id", flowId);
|
|
50
|
+
this.setRequestHeader("X-Trace-Id", traceId);
|
|
51
|
+
this.setRequestHeader("X-Span-Id", String(spanId));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
originalSend.call(this, body);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// src/adapters/web/index.ts
|
|
58
|
+
/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */
|
|
59
|
+
var WebAdapter = class extends CommonAdapter {
|
|
60
|
+
constructor(core, options = {}) {
|
|
61
|
+
super(core);
|
|
62
|
+
this.options = options;
|
|
63
|
+
}
|
|
64
|
+
getPageKey() {
|
|
65
|
+
return getPageKey(this.getWindow(), this.options.pageKey);
|
|
66
|
+
}
|
|
67
|
+
watchPageChange(onChange) {
|
|
68
|
+
watchPageChange(this.getWindow(), onChange);
|
|
69
|
+
}
|
|
70
|
+
interceptRequests() {
|
|
71
|
+
interceptRequests(this.getWindow(), this.options.domain, () => this.core.nextRequestContext(this.getPageKey()));
|
|
72
|
+
}
|
|
73
|
+
getWindow() {
|
|
74
|
+
var _this$options$window;
|
|
75
|
+
return (_this$options$window = this.options.window) !== null && _this$options$window !== void 0 ? _this$options$window : window;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
export { WebAdapter as t };
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=web-CDlgH2Jq.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-CDlgH2Jq.mjs","names":[],"sources":["../src/adapters/web/pageKey.ts","../src/adapters/web/request.ts","../src/adapters/web/index.ts"],"sourcesContent":["/** Web 页面标识默认取 pathname,hash 路由或业务 Tab 可自行映射。 */\nexport function getPageKey(target: Window, resolve?: (location: Location) => string): string {\n return resolve ? resolve(target.location) : target.location.pathname\n}\n\n/** 把浏览器路由变化转换成统一的页面变更通知。 */\nexport function watchPageChange(target: Window, onChange: () => void): void {\n for (const name of ['pushState', 'replaceState'] as const) {\n const original = target.history[name]\n target.history[name] = function (this: History, ...args): void {\n original.apply(this, args)\n onChange()\n }\n }\n target.addEventListener('popstate', onChange)\n target.addEventListener('hashchange', onChange)\n}\n","import type { PageContext, TraceDomain } from '../../core/types'\nimport type { WebWindow } from './index'\n\n/** 只做域名判断和写入追踪头;页面 ID 与序号由 core 提供。 */\nexport function interceptRequests(\n target: WebWindow,\n domain: TraceDomain = target.location.origin,\n getContext: () => PageContext,\n): void {\n const domains = typeof domain === 'string' ? [domain] : domain\n const origins = new Set(domains)\n // 按协议、域名和端口精确比较,外站请求直接交给原方法。\n const isSameDomain = (url: string) => origins.has(new target.URL(url, target.document.baseURI).origin)\n\n const originalFetch = target.fetch\n target.fetch = function (input, init) {\n // 交给浏览器合并 URL、Request 和 init,保留正文、请求头与取消信号。\n const request = new target.Request(input, init)\n if (isSameDomain(request.url) && request.mode !== 'no-cors') {\n const { flowId, traceId, spanId } = getContext()\n request.headers.set('X-Flow-Id', flowId)\n request.headers.set('X-Trace-Id', traceId)\n request.headers.set('X-Span-Id', String(spanId))\n }\n return originalFetch.call(target, request)\n }\n\n const prototype = target.XMLHttpRequest.prototype\n const originalOpen = prototype.open\n const originalSend = prototype.send\n const requests = new WeakMap<XMLHttpRequest, string>()\n\n prototype.open = function (this: XMLHttpRequest, ...args: [string, string | URL, boolean?, (string | null)?, (string | null)?]): void {\n // open 会触发 OPENED 事件,先记录地址以支持事件回调里立即 send。\n requests.set(this, String(args[1]))\n Reflect.apply(originalOpen, this, args)\n }\n prototype.send = function (this: XMLHttpRequest, body): void {\n const url = requests.get(this)\n if (url !== undefined && this.readyState === 1) {\n // 本次地址只使用一次,重复 send 不再取号;下一次 open 会重新记录。\n requests.delete(this)\n if (isSameDomain(url)) {\n const { flowId, traceId, spanId } = getContext()\n this.setRequestHeader('X-Flow-Id', flowId)\n this.setRequestHeader('X-Trace-Id', traceId)\n this.setRequestHeader('X-Span-Id', String(spanId))\n }\n }\n // 原 send 只调用一次,网络错误按浏览器原行为返回。\n originalSend.call(this, body)\n }\n}\n","import type { TraceCore, TraceDomain } from '../../core'\nimport { CommonAdapter } from '../common'\nimport { getPageKey, watchPageChange } from './pageKey'\nimport { interceptRequests } from './request'\n\nexport type WebWindow = Window & typeof globalThis\n\nexport interface WebAdapterOptions {\n domain?: TraceDomain\n pageKey?: (location: Location) => string\n window?: WebWindow\n}\n\n/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */\nexport class WebAdapter extends CommonAdapter {\n constructor(core: TraceCore, private readonly options: WebAdapterOptions = {}) {\n super(core)\n }\n\n protected getPageKey(): string {\n return getPageKey(this.getWindow(), this.options.pageKey)\n }\n\n protected watchPageChange(onChange: () => void): void {\n watchPageChange(this.getWindow(), onChange)\n }\n\n protected interceptRequests(): void {\n // 发送时读取当前页面,fetch 和 XHR 共用该页的请求序号。\n interceptRequests(this.getWindow(), this.options.domain, () => this.core.nextRequestContext(this.getPageKey()))\n }\n\n private getWindow(): WebWindow {\n return this.options.window ?? window\n }\n}\n"],"mappings":";;;AACA,SAAgB,WAAW,QAAgB,SAAkD;CAC3F,OAAO,UAAU,QAAQ,OAAO,QAAQ,IAAI,OAAO,SAAS;AAC9D;;AAGA,SAAgB,gBAAgB,QAAgB,UAA4B;CAC1E,KAAK,MAAM,QAAQ,CAAC,aAAa,cAAc,GAAY;EACzD,MAAM,WAAW,OAAO,QAAQ;EAChC,OAAO,QAAQ,QAAQ,SAAyB,GAAG,MAAY;GAC7D,SAAS,MAAM,MAAM,IAAI;GACzB,SAAS;EACX;CACF;CACA,OAAO,iBAAiB,YAAY,QAAQ;CAC5C,OAAO,iBAAiB,cAAc,QAAQ;AAChD;;;ACZA,SAAgB,kBACd,QACA,SAAsB,OAAO,SAAS,QACtC,YACM;CAEN,MAAM,UAAU,IAAI,IADJ,OAAO,WAAW,WAAW,CAAC,MAAM,IAAI,MACzB;CAE/B,MAAM,gBAAgB,QAAgB,QAAQ,IAAI,IAAI,OAAO,IAAI,KAAK,OAAO,SAAS,OAAO,CAAC,CAAC,MAAM;CAErG,MAAM,gBAAgB,OAAO;CAC7B,OAAO,QAAQ,SAAU,OAAO,MAAM;EAEpC,MAAM,UAAU,IAAI,OAAO,QAAQ,OAAO,IAAI;EAC9C,IAAI,aAAa,QAAQ,GAAG,KAAK,QAAQ,SAAS,WAAW;GAC3D,MAAM,EAAE,QAAQ,SAAS,WAAW,WAAW;GAC/C,QAAQ,QAAQ,IAAI,aAAa,MAAM;GACvC,QAAQ,QAAQ,IAAI,cAAc,OAAO;GACzC,QAAQ,QAAQ,IAAI,aAAa,OAAO,MAAM,CAAC;EACjD;EACA,OAAO,cAAc,KAAK,QAAQ,OAAO;CAC3C;CAEA,MAAM,YAAY,OAAO,eAAe;CACxC,MAAM,eAAe,UAAU;CAC/B,MAAM,eAAe,UAAU;CAC/B,MAAM,2BAAW,IAAI,QAAgC;CAErD,UAAU,OAAO,SAAgC,GAAG,MAAkF;EAEpI,SAAS,IAAI,MAAM,OAAO,KAAK,EAAE,CAAC;EAClC,QAAQ,MAAM,cAAc,MAAM,IAAI;CACxC;CACA,UAAU,OAAO,SAAgC,MAAY;EAC3D,MAAM,MAAM,SAAS,IAAI,IAAI;EAC7B,IAAI,QAAQ,KAAA,KAAa,KAAK,eAAe,GAAG;GAE9C,SAAS,OAAO,IAAI;GACpB,IAAI,aAAa,GAAG,GAAG;IACrB,MAAM,EAAE,QAAQ,SAAS,WAAW,WAAW;IAC/C,KAAK,iBAAiB,aAAa,MAAM;IACzC,KAAK,iBAAiB,cAAc,OAAO;IAC3C,KAAK,iBAAiB,aAAa,OAAO,MAAM,CAAC;GACnD;EACF;EAEA,aAAa,KAAK,MAAM,IAAI;CAC9B;AACF;;;ACtCA,IAAa,aAAb,cAAgC,cAAc;CAC5C,YAAY,MAAkC,UAA6B,CAAC,GAAG;EAC7E,MAAM,IAAI;EADkC,KAAA,UAAA;CAE9C;CAEU,aAAqB;EAC7B,OAAO,WAAW,KAAK,UAAU,GAAG,KAAK,QAAQ,OAAO;CAC1D;CAEU,gBAAgB,UAA4B;EACpD,gBAAgB,KAAK,UAAU,GAAG,QAAQ;CAC5C;CAEU,oBAA0B;EAElC,kBAAkB,KAAK,UAAU,GAAG,KAAK,QAAQ,cAAc,KAAK,KAAK,mBAAmB,KAAK,WAAW,CAAC,CAAC;CAChH;CAEQ,YAAuB;;EAC7B,QAAA,uBAAO,KAAK,QAAQ,YAAA,QAAA,yBAAA,KAAA,IAAA,uBAAU;CAChC;AACF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const require_common = require("./common-BKQhi1LZ.cjs");
|
|
2
|
+
// src/adapters/web/pageKey.ts
|
|
3
|
+
/** Web 页面标识默认取 pathname,hash 路由或业务 Tab 可自行映射。 */
|
|
4
|
+
function getPageKey(target, resolve) {
|
|
5
|
+
return resolve ? resolve(target.location) : target.location.pathname;
|
|
6
|
+
}
|
|
7
|
+
/** 把浏览器路由变化转换成统一的页面变更通知。 */
|
|
8
|
+
function watchPageChange(target, onChange) {
|
|
9
|
+
for (const name of ["pushState", "replaceState"]) {
|
|
10
|
+
const original = target.history[name];
|
|
11
|
+
target.history[name] = function(...args) {
|
|
12
|
+
original.apply(this, args);
|
|
13
|
+
onChange();
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
target.addEventListener("popstate", onChange);
|
|
17
|
+
target.addEventListener("hashchange", onChange);
|
|
18
|
+
}
|
|
19
|
+
// src/adapters/web/request.ts
|
|
20
|
+
/** 只做域名判断和写入追踪头;页面 ID 与序号由 core 提供。 */
|
|
21
|
+
function interceptRequests(target, domain = target.location.origin, getContext) {
|
|
22
|
+
const origins = new Set(typeof domain === "string" ? [domain] : domain);
|
|
23
|
+
const isSameDomain = (url) => origins.has(new target.URL(url, target.document.baseURI).origin);
|
|
24
|
+
const originalFetch = target.fetch;
|
|
25
|
+
target.fetch = function(input, init) {
|
|
26
|
+
const request = new target.Request(input, init);
|
|
27
|
+
if (isSameDomain(request.url) && request.mode !== "no-cors") {
|
|
28
|
+
const { flowId, traceId, spanId } = getContext();
|
|
29
|
+
request.headers.set("X-Flow-Id", flowId);
|
|
30
|
+
request.headers.set("X-Trace-Id", traceId);
|
|
31
|
+
request.headers.set("X-Span-Id", String(spanId));
|
|
32
|
+
}
|
|
33
|
+
return originalFetch.call(target, request);
|
|
34
|
+
};
|
|
35
|
+
const prototype = target.XMLHttpRequest.prototype;
|
|
36
|
+
const originalOpen = prototype.open;
|
|
37
|
+
const originalSend = prototype.send;
|
|
38
|
+
const requests = /* @__PURE__ */ new WeakMap();
|
|
39
|
+
prototype.open = function(...args) {
|
|
40
|
+
requests.set(this, String(args[1]));
|
|
41
|
+
Reflect.apply(originalOpen, this, args);
|
|
42
|
+
};
|
|
43
|
+
prototype.send = function(body) {
|
|
44
|
+
const url = requests.get(this);
|
|
45
|
+
if (url !== void 0 && this.readyState === 1) {
|
|
46
|
+
requests.delete(this);
|
|
47
|
+
if (isSameDomain(url)) {
|
|
48
|
+
const { flowId, traceId, spanId } = getContext();
|
|
49
|
+
this.setRequestHeader("X-Flow-Id", flowId);
|
|
50
|
+
this.setRequestHeader("X-Trace-Id", traceId);
|
|
51
|
+
this.setRequestHeader("X-Span-Id", String(spanId));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
originalSend.call(this, body);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// src/adapters/web/index.ts
|
|
58
|
+
/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */
|
|
59
|
+
var WebAdapter = class extends require_common.CommonAdapter {
|
|
60
|
+
constructor(core, options = {}) {
|
|
61
|
+
super(core);
|
|
62
|
+
this.options = options;
|
|
63
|
+
}
|
|
64
|
+
getPageKey() {
|
|
65
|
+
return getPageKey(this.getWindow(), this.options.pageKey);
|
|
66
|
+
}
|
|
67
|
+
watchPageChange(onChange) {
|
|
68
|
+
watchPageChange(this.getWindow(), onChange);
|
|
69
|
+
}
|
|
70
|
+
interceptRequests() {
|
|
71
|
+
interceptRequests(this.getWindow(), this.options.domain, () => this.core.nextRequestContext(this.getPageKey()));
|
|
72
|
+
}
|
|
73
|
+
getWindow() {
|
|
74
|
+
var _this$options$window;
|
|
75
|
+
return (_this$options$window = this.options.window) !== null && _this$options$window !== void 0 ? _this$options$window : window;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
Object.defineProperty(exports, "WebAdapter", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function() {
|
|
81
|
+
return WebAdapter;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=web-Dbjr9K0w.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-Dbjr9K0w.cjs","names":["CommonAdapter"],"sources":["../src/adapters/web/pageKey.ts","../src/adapters/web/request.ts","../src/adapters/web/index.ts"],"sourcesContent":["/** Web 页面标识默认取 pathname,hash 路由或业务 Tab 可自行映射。 */\nexport function getPageKey(target: Window, resolve?: (location: Location) => string): string {\n return resolve ? resolve(target.location) : target.location.pathname\n}\n\n/** 把浏览器路由变化转换成统一的页面变更通知。 */\nexport function watchPageChange(target: Window, onChange: () => void): void {\n for (const name of ['pushState', 'replaceState'] as const) {\n const original = target.history[name]\n target.history[name] = function (this: History, ...args): void {\n original.apply(this, args)\n onChange()\n }\n }\n target.addEventListener('popstate', onChange)\n target.addEventListener('hashchange', onChange)\n}\n","import type { PageContext, TraceDomain } from '../../core/types'\nimport type { WebWindow } from './index'\n\n/** 只做域名判断和写入追踪头;页面 ID 与序号由 core 提供。 */\nexport function interceptRequests(\n target: WebWindow,\n domain: TraceDomain = target.location.origin,\n getContext: () => PageContext,\n): void {\n const domains = typeof domain === 'string' ? [domain] : domain\n const origins = new Set(domains)\n // 按协议、域名和端口精确比较,外站请求直接交给原方法。\n const isSameDomain = (url: string) => origins.has(new target.URL(url, target.document.baseURI).origin)\n\n const originalFetch = target.fetch\n target.fetch = function (input, init) {\n // 交给浏览器合并 URL、Request 和 init,保留正文、请求头与取消信号。\n const request = new target.Request(input, init)\n if (isSameDomain(request.url) && request.mode !== 'no-cors') {\n const { flowId, traceId, spanId } = getContext()\n request.headers.set('X-Flow-Id', flowId)\n request.headers.set('X-Trace-Id', traceId)\n request.headers.set('X-Span-Id', String(spanId))\n }\n return originalFetch.call(target, request)\n }\n\n const prototype = target.XMLHttpRequest.prototype\n const originalOpen = prototype.open\n const originalSend = prototype.send\n const requests = new WeakMap<XMLHttpRequest, string>()\n\n prototype.open = function (this: XMLHttpRequest, ...args: [string, string | URL, boolean?, (string | null)?, (string | null)?]): void {\n // open 会触发 OPENED 事件,先记录地址以支持事件回调里立即 send。\n requests.set(this, String(args[1]))\n Reflect.apply(originalOpen, this, args)\n }\n prototype.send = function (this: XMLHttpRequest, body): void {\n const url = requests.get(this)\n if (url !== undefined && this.readyState === 1) {\n // 本次地址只使用一次,重复 send 不再取号;下一次 open 会重新记录。\n requests.delete(this)\n if (isSameDomain(url)) {\n const { flowId, traceId, spanId } = getContext()\n this.setRequestHeader('X-Flow-Id', flowId)\n this.setRequestHeader('X-Trace-Id', traceId)\n this.setRequestHeader('X-Span-Id', String(spanId))\n }\n }\n // 原 send 只调用一次,网络错误按浏览器原行为返回。\n originalSend.call(this, body)\n }\n}\n","import type { TraceCore, TraceDomain } from '../../core'\nimport { CommonAdapter } from '../common'\nimport { getPageKey, watchPageChange } from './pageKey'\nimport { interceptRequests } from './request'\n\nexport type WebWindow = Window & typeof globalThis\n\nexport interface WebAdapterOptions {\n domain?: TraceDomain\n pageKey?: (location: Location) => string\n window?: WebWindow\n}\n\n/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */\nexport class WebAdapter extends CommonAdapter {\n constructor(core: TraceCore, private readonly options: WebAdapterOptions = {}) {\n super(core)\n }\n\n protected getPageKey(): string {\n return getPageKey(this.getWindow(), this.options.pageKey)\n }\n\n protected watchPageChange(onChange: () => void): void {\n watchPageChange(this.getWindow(), onChange)\n }\n\n protected interceptRequests(): void {\n // 发送时读取当前页面,fetch 和 XHR 共用该页的请求序号。\n interceptRequests(this.getWindow(), this.options.domain, () => this.core.nextRequestContext(this.getPageKey()))\n }\n\n private getWindow(): WebWindow {\n return this.options.window ?? window\n }\n}\n"],"mappings":";;;AACA,SAAgB,WAAW,QAAgB,SAAkD;CAC3F,OAAO,UAAU,QAAQ,OAAO,QAAQ,IAAI,OAAO,SAAS;AAC9D;;AAGA,SAAgB,gBAAgB,QAAgB,UAA4B;CAC1E,KAAK,MAAM,QAAQ,CAAC,aAAa,cAAc,GAAY;EACzD,MAAM,WAAW,OAAO,QAAQ;EAChC,OAAO,QAAQ,QAAQ,SAAyB,GAAG,MAAY;GAC7D,SAAS,MAAM,MAAM,IAAI;GACzB,SAAS;EACX;CACF;CACA,OAAO,iBAAiB,YAAY,QAAQ;CAC5C,OAAO,iBAAiB,cAAc,QAAQ;AAChD;;;ACZA,SAAgB,kBACd,QACA,SAAsB,OAAO,SAAS,QACtC,YACM;CAEN,MAAM,UAAU,IAAI,IADJ,OAAO,WAAW,WAAW,CAAC,MAAM,IAAI,MACzB;CAE/B,MAAM,gBAAgB,QAAgB,QAAQ,IAAI,IAAI,OAAO,IAAI,KAAK,OAAO,SAAS,OAAO,CAAC,CAAC,MAAM;CAErG,MAAM,gBAAgB,OAAO;CAC7B,OAAO,QAAQ,SAAU,OAAO,MAAM;EAEpC,MAAM,UAAU,IAAI,OAAO,QAAQ,OAAO,IAAI;EAC9C,IAAI,aAAa,QAAQ,GAAG,KAAK,QAAQ,SAAS,WAAW;GAC3D,MAAM,EAAE,QAAQ,SAAS,WAAW,WAAW;GAC/C,QAAQ,QAAQ,IAAI,aAAa,MAAM;GACvC,QAAQ,QAAQ,IAAI,cAAc,OAAO;GACzC,QAAQ,QAAQ,IAAI,aAAa,OAAO,MAAM,CAAC;EACjD;EACA,OAAO,cAAc,KAAK,QAAQ,OAAO;CAC3C;CAEA,MAAM,YAAY,OAAO,eAAe;CACxC,MAAM,eAAe,UAAU;CAC/B,MAAM,eAAe,UAAU;CAC/B,MAAM,2BAAW,IAAI,QAAgC;CAErD,UAAU,OAAO,SAAgC,GAAG,MAAkF;EAEpI,SAAS,IAAI,MAAM,OAAO,KAAK,EAAE,CAAC;EAClC,QAAQ,MAAM,cAAc,MAAM,IAAI;CACxC;CACA,UAAU,OAAO,SAAgC,MAAY;EAC3D,MAAM,MAAM,SAAS,IAAI,IAAI;EAC7B,IAAI,QAAQ,KAAA,KAAa,KAAK,eAAe,GAAG;GAE9C,SAAS,OAAO,IAAI;GACpB,IAAI,aAAa,GAAG,GAAG;IACrB,MAAM,EAAE,QAAQ,SAAS,WAAW,WAAW;IAC/C,KAAK,iBAAiB,aAAa,MAAM;IACzC,KAAK,iBAAiB,cAAc,OAAO;IAC3C,KAAK,iBAAiB,aAAa,OAAO,MAAM,CAAC;GACnD;EACF;EAEA,aAAa,KAAK,MAAM,IAAI;CAC9B;AACF;;;ACtCA,IAAa,aAAb,cAAgCA,eAAAA,cAAc;CAC5C,YAAY,MAAkC,UAA6B,CAAC,GAAG;EAC7E,MAAM,IAAI;EADkC,KAAA,UAAA;CAE9C;CAEU,aAAqB;EAC7B,OAAO,WAAW,KAAK,UAAU,GAAG,KAAK,QAAQ,OAAO;CAC1D;CAEU,gBAAgB,UAA4B;EACpD,gBAAgB,KAAK,UAAU,GAAG,QAAQ;CAC5C;CAEU,oBAA0B;EAElC,kBAAkB,KAAK,UAAU,GAAG,KAAK,QAAQ,cAAc,KAAK,KAAK,mBAAmB,KAAK,WAAW,CAAC,CAAC;CAChH;CAEQ,YAAuB;;EAC7B,QAAA,uBAAO,KAAK,QAAQ,YAAA,QAAA,yBAAA,KAAA,IAAA,uBAAU;CAChC;AACF"}
|
package/dist/web.cjs
ADDED
package/dist/web.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { n as CommonAdapter, o as TraceDomain, t as TraceCore } from "./index-FHW2y4dt.cjs";
|
|
2
|
+
export type WebWindow = Window & typeof globalThis;
|
|
3
|
+
export interface WebAdapterOptions {
|
|
4
|
+
domain?: TraceDomain;
|
|
5
|
+
pageKey?: (location: Location) => string;
|
|
6
|
+
window?: WebWindow;
|
|
7
|
+
}
|
|
8
|
+
/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */
|
|
9
|
+
export declare class WebAdapter extends CommonAdapter {
|
|
10
|
+
private readonly options;
|
|
11
|
+
constructor(core: TraceCore, options?: WebAdapterOptions);
|
|
12
|
+
protected getPageKey(): string;
|
|
13
|
+
protected watchPageChange(onChange: () => void): void;
|
|
14
|
+
protected interceptRequests(): void;
|
|
15
|
+
private getWindow;
|
|
16
|
+
}
|
package/dist/web.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { n as CommonAdapter, o as TraceDomain, t as TraceCore } from "./index-FHW2y4dt.mjs";
|
|
2
|
+
export type WebWindow = Window & typeof globalThis;
|
|
3
|
+
export interface WebAdapterOptions {
|
|
4
|
+
domain?: TraceDomain;
|
|
5
|
+
pageKey?: (location: Location) => string;
|
|
6
|
+
window?: WebWindow;
|
|
7
|
+
}
|
|
8
|
+
/** Web 只负责把页面标识、路由变化和请求入口接到 core。 */
|
|
9
|
+
export declare class WebAdapter extends CommonAdapter {
|
|
10
|
+
private readonly options;
|
|
11
|
+
constructor(core: TraceCore, options?: WebAdapterOptions);
|
|
12
|
+
protected getPageKey(): string;
|
|
13
|
+
protected watchPageChange(onChange: () => void): void;
|
|
14
|
+
protected interceptRequests(): void;
|
|
15
|
+
private getWindow;
|
|
16
|
+
}
|
package/dist/web.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as WebAdapter } from "./web-CDlgH2Jq.mjs";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Trace SDK API
|
|
2
|
+
|
|
3
|
+
## 创建
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createTrace, web } from 'trace-kdzs-sdk'
|
|
7
|
+
|
|
8
|
+
const trace = createTrace({
|
|
9
|
+
adapter: web,
|
|
10
|
+
context: { appId: 'erp' },
|
|
11
|
+
flowId: ctx => `${ctx.appId}_${crypto.randomUUID()}`,
|
|
12
|
+
traceId: (_ctx, { flowId }) => flowId,
|
|
13
|
+
pageKey: location => location.hash.slice(1).split('?')[0] || '/',
|
|
14
|
+
domain: 'https://api.example.com',
|
|
15
|
+
})
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
createTrace 返回 TraceCore;传入 adapter 时立即实例化并 start。web 是 WebAdapter class 的导出别名。在应用入口初始化一次,不传 adapter 则只创建 core。
|
|
19
|
+
|
|
20
|
+
| 参数 | 说明 |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| adapter | 适配器 class,目前提供 web |
|
|
23
|
+
| context | 任意自定义上下文,保留原引用与类型 |
|
|
24
|
+
| flowId(context, { pageKey }) | 同步生成 flowId;默认使用时间、递增号和随机值 |
|
|
25
|
+
| traceId(context, { pageKey, flowId }) | 同步生成 traceId,默认等于 flowId |
|
|
26
|
+
| domain | 单个 origin 或数组,默认当前页面 origin |
|
|
27
|
+
| pageKey(location) | Web 页面标识,默认 pathname |
|
|
28
|
+
| window | Web 所属窗口,默认当前 window |
|
|
29
|
+
|
|
30
|
+
ID 生成函数只在创建页面记录时执行,不限制业务 ID 的字符集或长度。业务提供可用于 HTTP 请求头的字符串。生成函数异常直接抛出。
|
|
31
|
+
|
|
32
|
+
domain 直接与请求 origin 比较,需传完整 origin,例如 `https://api.example.com`,不补协议、不解析配置。数组替代默认范围,空数组跳过所有请求。domain 由 Web 适配器处理,core 不涉及域名或浏览器对象。
|
|
33
|
+
|
|
34
|
+
## 页面状态
|
|
35
|
+
|
|
36
|
+
每个 TraceCore 实例持有自己的 `Map<pageKey, { flowId, traceId, spanId }>`。
|
|
37
|
+
|
|
38
|
+
| 方法 | 行为 |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| ensurePage(pageKey) | 创建或复用页面,返回状态副本;初始序号 0 |
|
|
41
|
+
| getContext(pageKey) | 返回副本,不存在则 undefined |
|
|
42
|
+
| setFlowId(pageKey, flowId) | 仅修改该页 flowId |
|
|
43
|
+
| setTraceId(pageKey, traceId) | 仅修改该页 traceId |
|
|
44
|
+
| nextRequestContext(pageKey) | 本页序号加 1,返回请求状态副本 |
|
|
45
|
+
| removePage(pageKey) | 删除该页,返回是否存在 |
|
|
46
|
+
| clear() | 删除全部页面 |
|
|
47
|
+
|
|
48
|
+
setter 遇到不存在的页面会先创建记录,不重置序号。切换 Tab 保留状态;真正关闭时 removePage。请求发出后,修改页面 ID 不影响该请求。
|
|
49
|
+
|
|
50
|
+
直接使用 core:`new TraceCore({ generateFlowId, generateTraceId })`。两个生成器分别接收 pageKey 和 (pageKey, flowId)。
|
|
51
|
+
|
|
52
|
+
## 适配器
|
|
53
|
+
|
|
54
|
+
CommonAdapter 位于 `adapters/common/index.ts`。start 初始化当前页面、安装请求拦截、监听页面切换。同一个适配器对象重复 start 不会重复安装。
|
|
55
|
+
|
|
56
|
+
子类实现 `getPageKey()`、`watchPageChange(onChange)`、`interceptRequests()`。页面变化时调用无参数 onChange,基类读取页面标识并初始化记录。
|
|
57
|
+
|
|
58
|
+
WebAdapter 继承 CommonAdapter,按三份文件组织:index.ts 组装,pageKey.ts 读取页面与监听路由,request.ts 拦截 fetch/XHR。
|
|
59
|
+
|
|
60
|
+
## 请求
|
|
61
|
+
|
|
62
|
+
匹配 domain 的请求固定写入 `X-Flow-Id`、`X-Trace-Id`、`X-Span-Id`。fetch 调用时、XHR send 时取得当前页的 ID 并递增序号。请求层用一个 getContext 函数取得发送时的状态,不自行维护页面 Map。
|
|
63
|
+
|
|
64
|
+
Web 使用浏览器原生 fetch、Request 和 XMLHttpRequest。fetch 参数由原生 Request 合并,保留业务请求头、正文和取消信号。追踪头由 SDK 管理,业务通过 core 的 setter 修改 ID。外站和 fetch no-cors 请求不加头。原网络方法只调用一次;HTTP 错误、取消与网络异常按平台行为返回。没有请求筛选、头名配置、错误通知、重试或异常兜底。
|
|
65
|
+
|
|
66
|
+
请求归属取当前页面,无法自动识别隐藏 Tab 的后台任务。hash 路由显式配置 pageKey,同路由的多个业务 Tab 需使用各自的 key。显式放行跨域 API 时,服务端需要允许追踪头;domain 判断针对初始请求地址。
|
|
67
|
+
|
|
68
|
+
不提供 stop、卸载、动态换配置或日志上报。RN、小程序后续扩展。
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trace-kdzs-sdk",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Internal page tracing SDK with class-based core and Web adapter",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./core": {
|
|
23
|
+
"import": { "types": "./dist/core.d.mts", "default": "./dist/core.mjs" },
|
|
24
|
+
"require": { "types": "./dist/core.d.cts", "default": "./dist/core.cjs" }
|
|
25
|
+
},
|
|
26
|
+
"./web": {
|
|
27
|
+
"import": { "types": "./dist/web.d.mts", "default": "./dist/web.mjs" },
|
|
28
|
+
"require": { "types": "./dist/web.d.cts", "default": "./dist/web.cjs" }
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"docs/TRACE-SDK-API.md"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown",
|
|
39
|
+
"dev": "tsdown --watch",
|
|
40
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.web.json --noEmit",
|
|
41
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
42
|
+
"test:browser": "npm run build && node tests/browser-server.mjs",
|
|
43
|
+
"typecheck:exports": "tsc -p tests/types/tsconfig.core.json && tsc -p tests/types/tsconfig.web.json",
|
|
44
|
+
"check": "npm run typecheck && npm run build && npm run typecheck:exports",
|
|
45
|
+
"pack:check": "npm pack --dry-run",
|
|
46
|
+
"prepublishOnly": "npm run check && node --test tests/*.test.mjs"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"registry": "https://registry.npmjs.org/",
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"tsdown": "0.23.7",
|
|
54
|
+
"typescript": "7.0.2"
|
|
55
|
+
}
|
|
56
|
+
}
|