@insmind/cli 1.5.1 → 1.5.3
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/contracts/operations/auth.status/output.schema.json +3 -4
- package/dist/bin/insmind-cli.js +12 -7
- package/dist/bin/postinstall.js +7 -5
- package/dist/bin/{agent-worker.js → task-worker.js} +5 -4
- package/dist/src/analytics/device.js +23 -0
- package/dist/src/analytics/wind.js +54 -0
- package/dist/src/bootstrap/create-cli.js +33 -19
- package/dist/src/bootstrap/create-runtime.js +45 -16
- package/dist/src/bootstrap/{run-agent-worker.js → run-task-worker.js} +20 -10
- package/dist/src/bootstrap/validators.js +10 -5
- package/dist/src/cli/action-binding.js +14 -0
- package/dist/src/cli/agent-commands.js +9 -4
- package/dist/src/cli/auth-commands.js +5 -3
- package/dist/src/cli/credits-command.js +2 -2
- package/dist/src/cli/dam-commands.js +51 -52
- package/dist/src/cli/editor-commands.js +13 -15
- package/dist/src/cli/errors.js +97 -44
- package/dist/src/cli/model-commands.js +6 -4
- package/dist/src/cli/presenter.js +13 -10
- package/dist/src/cli/task-commands.js +13 -5
- package/dist/src/cli/tool-commands.js +11 -5
- package/dist/src/features/agent/use-cases.js +11 -5
- package/dist/src/features/auth/access-policy.js +2 -0
- package/dist/src/features/auth/credential-store.js +2 -2
- package/dist/src/features/auth/sso-service.js +5 -4
- package/dist/src/features/auth/state.js +11 -1
- package/dist/src/features/auth/use-cases.js +43 -40
- package/dist/src/features/credits/use-cases.js +2 -0
- package/dist/src/features/dam/file-downloader.js +38 -16
- package/dist/src/features/dam/object-storage.js +23 -11
- package/dist/src/features/dam/operation-executor.js +46 -37
- package/dist/src/features/dam/registered-uploader.js +39 -29
- package/dist/src/features/dam/storage-upload.js +5 -4
- package/dist/src/features/dam/use-cases.js +81 -60
- package/dist/src/features/editor/bridge-client.js +6 -6
- package/dist/src/features/editor/bridge-process.js +2 -1
- package/dist/src/features/editor/session-state.js +6 -4
- package/dist/src/features/editor/session.js +26 -8
- package/dist/src/features/editor/use-cases.js +10 -3
- package/dist/src/features/org/use-cases.js +2 -1
- package/dist/src/features/task/store.js +4 -0
- package/dist/src/features/task/{agent-background.js → task-background.js} +5 -4
- package/dist/src/features/task/use-cases.js +123 -28
- package/dist/src/features/tool/catalog.js +11 -5
- package/dist/src/features/tool/dynamic-schema.js +9 -7
- package/dist/src/features/tool/mns-catalog-adapter.js +1 -0
- package/dist/src/features/tool/tool-api-adapter.js +8 -7
- package/dist/src/features/tool/use-cases.js +8 -4
- package/dist/src/features/update/update-notification.js +75 -70
- package/dist/src/features/update/update-service.js +24 -20
- package/dist/src/platform/file-lock.js +36 -9
- package/dist/src/platform/interruption.js +13 -0
- package/dist/src/platform/json-input.js +12 -9
- package/dist/src/platform/local-json-file.js +8 -4
- package/dist/src/platform/redact.js +7 -1
- package/dist/src/platform/remote-protocol.js +2 -2
- package/dist/src/platform/safe-upload-file.js +2 -1
- package/dist/src/platform/schema-issues.js +8 -0
- package/dist/src/platform/signed-http-transport.js +8 -7
- package/dist/src/platform/text-output.js +33 -0
- package/dist/src/telemetry/invocation.js +19 -4
- package/dist/src/telemetry/process-errors.js +14 -4
- package/dist/src/telemetry/sanitize.js +4 -2
- package/dist/src/telemetry/sls-sink.js +36 -20
- package/dist/vendor/README.md +10 -0
- package/dist/vendor/gd-tracker-node.mjs +1175 -0
- package/package.json +4 -4
|
@@ -0,0 +1,1175 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { execFile } from 'child_process';
|
|
5
|
+
import { promisify } from 'util';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* CLI/Node 模式稳定错误码。
|
|
9
|
+
* @see proposal/cli-mode-proposal.md §5.5
|
|
10
|
+
*/
|
|
11
|
+
const ERROR_CODES = {
|
|
12
|
+
/** mode 配置非法(node 入口只接受 'cli') */
|
|
13
|
+
INVALID_MODE: 'GD_TRACKER_INVALID_MODE',
|
|
14
|
+
/** deviceId 缺失、空白或不符合长度/字符集规则 */
|
|
15
|
+
DEVICE_ID_REQUIRED: 'GD_TRACKER_DEVICE_ID_REQUIRED',
|
|
16
|
+
/** region 不是 CHINA/US */
|
|
17
|
+
REGION_REQUIRED: 'GD_TRACKER_REGION_REQUIRED',
|
|
18
|
+
/** endpoint 与 region 不一致(预留,当前由 SDK 内部映射 endpoint) */
|
|
19
|
+
REGION_ENDPOINT_MISMATCH: 'GD_TRACKER_REGION_ENDPOINT_MISMATCH',
|
|
20
|
+
/** 任何 AB 相关配置 */
|
|
21
|
+
AB_UNSUPPORTED: 'GD_TRACKER_AB_UNSUPPORTED',
|
|
22
|
+
/** 浏览器专属/会绕过 region 约束的 option */
|
|
23
|
+
UNSUPPORTED_OPTION: 'GD_TRACKER_UNSUPPORTED_OPTION',
|
|
24
|
+
/** setup 中未识别的 option(非阻断诊断) */
|
|
25
|
+
UNKNOWN_OPTION: 'GD_TRACKER_UNKNOWN_OPTION',
|
|
26
|
+
/** CLI 白名单之外的插件 */
|
|
27
|
+
UNSUPPORTED_PLUGIN: 'GD_TRACKER_UNSUPPORTED_PLUGIN',
|
|
28
|
+
/** 适配器缺失或形态不完整 */
|
|
29
|
+
ADAPTER_REQUIRED: 'GD_TRACKER_ADAPTER_REQUIRED',
|
|
30
|
+
/** 传输失败(网络/超时/非 2xx) */
|
|
31
|
+
TRANSPORT_ERROR: 'GD_TRACKER_TRANSPORT_ERROR',
|
|
32
|
+
/** flush 在预算时间内未完成 */
|
|
33
|
+
FLUSH_TIMEOUT: 'GD_TRACKER_FLUSH_TIMEOUT',
|
|
34
|
+
/** setup 完成前调用,或 shutdown 之后调用 */
|
|
35
|
+
NOT_READY: 'GD_TRACKER_NOT_READY',
|
|
36
|
+
/** 事件详情不合法(如缺 event_id) */
|
|
37
|
+
INVALID_EVENT: 'GD_TRACKER_INVALID_EVENT'
|
|
38
|
+
};
|
|
39
|
+
class TrackerError extends Error {
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} code ERROR_CODES 中的稳定错误码
|
|
42
|
+
* @param {string} message 人读的错误说明
|
|
43
|
+
*/
|
|
44
|
+
constructor(code, message) {
|
|
45
|
+
super(`[${code}] ${message}`);
|
|
46
|
+
this.name = 'TrackerError';
|
|
47
|
+
this.code = code;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const execFileAsync = promisify(execFile);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Darwin 内核主版本 → macOS 市场版本。
|
|
55
|
+
* 未知版本回退为 Darwin 原始版本号,不伪造。映射表维护见方案 §4.3。
|
|
56
|
+
*/
|
|
57
|
+
const DARWIN_MAJOR_TO_MACOS = {
|
|
58
|
+
19: '10.15',
|
|
59
|
+
20: '11',
|
|
60
|
+
21: '12',
|
|
61
|
+
22: '13',
|
|
62
|
+
23: '14',
|
|
63
|
+
24: '15',
|
|
64
|
+
25: '26'
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// 拼法基准待 Phase 0 与数据侧确认(服务端 bitwalker 落库形如 "Mac OS X"),此处保持可一处修改。
|
|
68
|
+
const OS_NAMES = {
|
|
69
|
+
darwin: 'macOS',
|
|
70
|
+
win32: 'Windows',
|
|
71
|
+
linux: 'Linux'
|
|
72
|
+
};
|
|
73
|
+
function getOsName(platform = os.platform()) {
|
|
74
|
+
return OS_NAMES[platform];
|
|
75
|
+
}
|
|
76
|
+
function getOsVersion(platform = os.platform(), release = os.release()) {
|
|
77
|
+
if (platform === 'darwin') {
|
|
78
|
+
const major = parseInt(release, 10);
|
|
79
|
+
return DARWIN_MAJOR_TO_MACOS[major] || `Darwin ${release}`;
|
|
80
|
+
}
|
|
81
|
+
// Windows 返回 os.release()(如 10.0.22631);Linux 返回内核版本(不等同于发行版版本)。
|
|
82
|
+
return OS_NAMES[platform] ? release : undefined;
|
|
83
|
+
}
|
|
84
|
+
function getArch() {
|
|
85
|
+
return os.arch();
|
|
86
|
+
}
|
|
87
|
+
function readDmiField(file) {
|
|
88
|
+
try {
|
|
89
|
+
const value = fs.readFileSync(file, 'utf8').trim();
|
|
90
|
+
return value || undefined;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function readWindowsCimField(field) {
|
|
96
|
+
return execFileAsync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', `(Get-CimInstance Win32_ComputerSystem).${field}`], {
|
|
97
|
+
timeout: 800
|
|
98
|
+
}).then(result => (result.stdout || '').trim() || undefined).catch(() => undefined);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 机型/厂商为 best-effort:任何失败都返回 undefined,且整个进程只采集一次。
|
|
102
|
+
let deviceModelPromise = null;
|
|
103
|
+
let manufacturerPromise = null;
|
|
104
|
+
function collectDeviceModel(platform = os.platform()) {
|
|
105
|
+
try {
|
|
106
|
+
if (platform === 'darwin') {
|
|
107
|
+
return execFileAsync('sysctl', ['-n', 'hw.model'], {
|
|
108
|
+
timeout: 800
|
|
109
|
+
}).then(result => (result.stdout || '').trim() || undefined).catch(() => undefined);
|
|
110
|
+
}
|
|
111
|
+
if (platform === 'linux') {
|
|
112
|
+
return Promise.resolve(readDmiField('/sys/devices/virtual/dmi/id/product_name'));
|
|
113
|
+
}
|
|
114
|
+
if (platform === 'win32') {
|
|
115
|
+
return readWindowsCimField('Model');
|
|
116
|
+
}
|
|
117
|
+
return Promise.resolve(undefined);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return Promise.resolve(undefined);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function collectManufacturer(platform = os.platform()) {
|
|
123
|
+
try {
|
|
124
|
+
if (platform === 'darwin') {
|
|
125
|
+
return Promise.resolve('Apple');
|
|
126
|
+
}
|
|
127
|
+
if (platform === 'linux') {
|
|
128
|
+
return Promise.resolve(readDmiField('/sys/devices/virtual/dmi/id/board_vendor'));
|
|
129
|
+
}
|
|
130
|
+
if (platform === 'win32') {
|
|
131
|
+
return readWindowsCimField('Manufacturer');
|
|
132
|
+
}
|
|
133
|
+
return Promise.resolve(undefined);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return Promise.resolve(undefined);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function getDeviceModel() {
|
|
139
|
+
if (!deviceModelPromise) {
|
|
140
|
+
deviceModelPromise = collectDeviceModel();
|
|
141
|
+
}
|
|
142
|
+
return deviceModelPromise;
|
|
143
|
+
}
|
|
144
|
+
function getManufacturer() {
|
|
145
|
+
if (!manufacturerPromise) {
|
|
146
|
+
manufacturerPromise = collectManufacturer();
|
|
147
|
+
}
|
|
148
|
+
return manufacturerPromise;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const DEFAULT_DELIVERY_BUDGET_MS = 2000;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 与浏览器 xhr/beacon 传输一致的 multipart 表单编码,不依赖全局 FormData。
|
|
155
|
+
*/
|
|
156
|
+
function encodeMultipart(params) {
|
|
157
|
+
const boundary = `----gdtrackercli${Math.random().toString(16).slice(2)}`;
|
|
158
|
+
const parts = Object.keys(params).map(name => `--${boundary}\r\nContent-Disposition: form-data; name="${name}"\r\n\r\n${String(params[name])}\r\n`).join('');
|
|
159
|
+
return {
|
|
160
|
+
contentType: `multipart/form-data; boundary=${boundary}`,
|
|
161
|
+
body: `${parts}--${boundary}--\r\n`
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 创建 Node 运行时适配器。
|
|
167
|
+
* @param {Object} [options]
|
|
168
|
+
* @param {Function} [options.fetch] 注入的 fetch 实现,缺省使用 globalThis.fetch(Node >= 18)
|
|
169
|
+
* @param {number} [options.deliveryBudgetMs=2000] 单次请求的投递预算,超时即中止且不重试
|
|
170
|
+
*/
|
|
171
|
+
function createNodeAdapters({
|
|
172
|
+
fetch = globalThis.fetch,
|
|
173
|
+
deliveryBudgetMs = DEFAULT_DELIVERY_BUDGET_MS
|
|
174
|
+
} = {}) {
|
|
175
|
+
if (typeof fetch !== 'function') {
|
|
176
|
+
throw new TrackerError(ERROR_CODES.ADAPTER_REQUIRED, 'Node >= 18 提供全局 fetch,或通过 createNodeAdapters({ fetch }) 注入实现。');
|
|
177
|
+
}
|
|
178
|
+
if (!(deliveryBudgetMs > 0)) {
|
|
179
|
+
throw new TrackerError(ERROR_CODES.ADAPTER_REQUIRED, 'deliveryBudgetMs 必须为正数。');
|
|
180
|
+
}
|
|
181
|
+
const environment = {
|
|
182
|
+
now: () => Date.now(),
|
|
183
|
+
randomUUID: () => randomUUID(),
|
|
184
|
+
runtime: () => ({
|
|
185
|
+
os: getOsName(),
|
|
186
|
+
os_version: getOsVersion(),
|
|
187
|
+
arch: getArch()
|
|
188
|
+
}),
|
|
189
|
+
// device_model/manufacturer 采集失败即缺省(方案 §4.3:不进入事件发送关键路径)。
|
|
190
|
+
runtimeAsync: async () => {
|
|
191
|
+
const [deviceModel, manufacturer] = await Promise.all([getDeviceModel(), getManufacturer()]);
|
|
192
|
+
return {
|
|
193
|
+
...environment.runtime(),
|
|
194
|
+
...(deviceModel ? {
|
|
195
|
+
device_model: deviceModel
|
|
196
|
+
} : {}),
|
|
197
|
+
...(manufacturer ? {
|
|
198
|
+
manufacturer
|
|
199
|
+
} : {})
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const transport = {
|
|
204
|
+
/**
|
|
205
|
+
* @param {{ url: string, params: Object }} payload 由 tracker 组装的请求
|
|
206
|
+
* @param {AbortSignal} [signal] flush/shutdown 超时时由 tracker 触发中止
|
|
207
|
+
* @returns {Promise<{accepted: boolean, retryable: boolean, status?: number, errorCode?: string, message?: string}>}
|
|
208
|
+
*/
|
|
209
|
+
async send(payload, signal) {
|
|
210
|
+
const controller = new AbortController();
|
|
211
|
+
const onExternalAbort = () => controller.abort(signal && signal.reason);
|
|
212
|
+
if (signal) {
|
|
213
|
+
if (signal.aborted) {
|
|
214
|
+
controller.abort(signal.reason);
|
|
215
|
+
} else {
|
|
216
|
+
signal.addEventListener('abort', onExternalAbort, {
|
|
217
|
+
once: true
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const timer = setTimeout(() => controller.abort(new Error('delivery budget exceeded')), deliveryBudgetMs);
|
|
222
|
+
const {
|
|
223
|
+
contentType,
|
|
224
|
+
body
|
|
225
|
+
} = encodeMultipart(payload.params);
|
|
226
|
+
try {
|
|
227
|
+
const response = await fetch(payload.url, {
|
|
228
|
+
method: 'POST',
|
|
229
|
+
headers: {
|
|
230
|
+
'Content-Type': contentType
|
|
231
|
+
},
|
|
232
|
+
body,
|
|
233
|
+
// Region is selected by the SDK and must never be bypassed by a 3xx target.
|
|
234
|
+
redirect: 'error',
|
|
235
|
+
signal: controller.signal
|
|
236
|
+
});
|
|
237
|
+
const status = response.status;
|
|
238
|
+
if (status >= 200 && status < 300) {
|
|
239
|
+
return {
|
|
240
|
+
accepted: true,
|
|
241
|
+
retryable: false,
|
|
242
|
+
status
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
accepted: false,
|
|
247
|
+
retryable: status === 429 || status >= 500,
|
|
248
|
+
status
|
|
249
|
+
};
|
|
250
|
+
} catch (error) {
|
|
251
|
+
const aborted = error && error.name === 'AbortError' || signal && signal.aborted;
|
|
252
|
+
if (aborted) {
|
|
253
|
+
return {
|
|
254
|
+
accepted: false,
|
|
255
|
+
retryable: false,
|
|
256
|
+
errorCode: ERROR_CODES.TRANSPORT_ERROR,
|
|
257
|
+
message: 'request aborted (timeout or shutdown)'
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
accepted: false,
|
|
262
|
+
retryable: true,
|
|
263
|
+
errorCode: ERROR_CODES.TRANSPORT_ERROR,
|
|
264
|
+
message: String(error && error.message || error)
|
|
265
|
+
};
|
|
266
|
+
} finally {
|
|
267
|
+
clearTimeout(timer);
|
|
268
|
+
if (signal) {
|
|
269
|
+
signal.removeEventListener('abort', onExternalAbort);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
return {
|
|
275
|
+
environment,
|
|
276
|
+
transport
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function _defineProperty(e, r, t) {
|
|
281
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
282
|
+
value: t,
|
|
283
|
+
enumerable: !0,
|
|
284
|
+
configurable: !0,
|
|
285
|
+
writable: !0
|
|
286
|
+
}) : e[r] = t, e;
|
|
287
|
+
}
|
|
288
|
+
function _toPrimitive(t, r) {
|
|
289
|
+
if ("object" != typeof t || !t) return t;
|
|
290
|
+
var e = t[Symbol.toPrimitive];
|
|
291
|
+
if (void 0 !== e) {
|
|
292
|
+
var i = e.call(t, r || "default");
|
|
293
|
+
if ("object" != typeof i) return i;
|
|
294
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
295
|
+
}
|
|
296
|
+
return ("string" === r ? String : Number)(t);
|
|
297
|
+
}
|
|
298
|
+
function _toPropertyKey(t) {
|
|
299
|
+
var i = _toPrimitive(t, "string");
|
|
300
|
+
return "symbol" == typeof i ? i : i + "";
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** @module */
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 打印错误信息到控制台。
|
|
307
|
+
* @param {string} msg 错误信息。
|
|
308
|
+
*/
|
|
309
|
+
function error(msg) {
|
|
310
|
+
console && console.error(`[gd-tracker error]: ${msg}`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 无垠数据层级类型。
|
|
315
|
+
* @typedef {('basic'|'common'|'event')} Scope
|
|
316
|
+
* @see {@link SCOPE}
|
|
317
|
+
*/
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 无垠数据层级枚举。
|
|
321
|
+
* @enum {Scope}
|
|
322
|
+
* @readonly
|
|
323
|
+
*/
|
|
324
|
+
const SCOPE = {
|
|
325
|
+
/** 基础公共数据层 */
|
|
326
|
+
basic: 'basic',
|
|
327
|
+
/** 公共业务数据层 */
|
|
328
|
+
common: 'common',
|
|
329
|
+
/** 业务事件数据层 */
|
|
330
|
+
event: 'event'
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 时区偏移量
|
|
335
|
+
* @returns string
|
|
336
|
+
*/
|
|
337
|
+
const getTimezoneOffset = () => {
|
|
338
|
+
const offset = new Date().getTimezoneOffset();
|
|
339
|
+
const operator = offset >= 0 ? '-' : '+';
|
|
340
|
+
const _offset = Math.abs(offset);
|
|
341
|
+
const h = Math.floor(_offset / 60);
|
|
342
|
+
const m = Math.floor(_offset % 60);
|
|
343
|
+
const _h = h < 10 ? `0${h}` : h;
|
|
344
|
+
const _m = m < 10 ? `0${m}` : m;
|
|
345
|
+
return `${operator}${_h}:${_m}`;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* 获取 UTC 时间+时区偏移量
|
|
350
|
+
* @param {number}} now
|
|
351
|
+
* @returns string
|
|
352
|
+
*/
|
|
353
|
+
const getUTCTime = now => {
|
|
354
|
+
const utc = new Date(now).toISOString();
|
|
355
|
+
return utc.replace('Z', getTimezoneOffset());
|
|
356
|
+
};
|
|
357
|
+
/**
|
|
358
|
+
* 代表无垠数据的类。
|
|
359
|
+
* @see {@link https://doc.huanleguang.com/pages/viewpage.action?pageId=43206813|无垠数据-数据格式概览}
|
|
360
|
+
*/
|
|
361
|
+
class Store {
|
|
362
|
+
/**
|
|
363
|
+
* 创建无垠数据类。
|
|
364
|
+
* @param {!Object} [basicFields={}] 初始无垠数据基础公共数据。
|
|
365
|
+
* @param {!Object} [commonFields={}] 初始无垠数据公共业务数据。
|
|
366
|
+
* @param {!Object} [eventFields={}] 初始无垠数据业务事件数据。
|
|
367
|
+
*/
|
|
368
|
+
constructor(basicFields, commonFields, eventFields) {
|
|
369
|
+
/**
|
|
370
|
+
* {@link https://doc.huanleguang.com/pages/viewpage.action?pageId=43206823|无垠数据-基础公共数据层}
|
|
371
|
+
* @type {Object}
|
|
372
|
+
* @private
|
|
373
|
+
*/
|
|
374
|
+
_defineProperty(this, "basicFields", null);
|
|
375
|
+
/**
|
|
376
|
+
* {@link https://doc.huanleguang.com/pages/viewpage.action?pageId=43206834|无垠数据-公共业务数据层}
|
|
377
|
+
* @type {Object}
|
|
378
|
+
* @private
|
|
379
|
+
*/
|
|
380
|
+
_defineProperty(this, "commonFields", null);
|
|
381
|
+
/**
|
|
382
|
+
* {@link https://doc.huanleguang.com/pages/viewpage.action?pageId=43206841|无垠数据-业务事件数据层}
|
|
383
|
+
* @type {Object}
|
|
384
|
+
* @private
|
|
385
|
+
*/
|
|
386
|
+
_defineProperty(this, "eventFields", null);
|
|
387
|
+
this.basicFields = basicFields;
|
|
388
|
+
this.commonFields = commonFields;
|
|
389
|
+
this.eventFields = eventFields;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* 设置对应层级的无垠数据。
|
|
394
|
+
* 如果字段存在则覆盖。
|
|
395
|
+
* @param {Scope} scope 无垠数据层级。
|
|
396
|
+
* @param {!Object} fieldsObject 无垠数据字段对象。
|
|
397
|
+
*/
|
|
398
|
+
set(scope, fieldsObject) {
|
|
399
|
+
if (!SCOPE[scope]) {
|
|
400
|
+
error('parameter "scope" is required.');
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const key = `${scope}Fields`;
|
|
404
|
+
for (let field in fieldsObject) {
|
|
405
|
+
const fieldValue = fieldsObject[field];
|
|
406
|
+
if ([undefined, null, NaN].includes(fieldValue)) {
|
|
407
|
+
this[key][field] && delete this[key][field];
|
|
408
|
+
} else {
|
|
409
|
+
this[key][field] = fieldValue;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* 获取对应层级的无垠数据。
|
|
416
|
+
* 如果没有传入 fields 参数,默认返回 scope 指定的所有数据,否则返回包含 fields 的字段对象。
|
|
417
|
+
* 若 field 不存在,则不返回该 field 指定的字段值。
|
|
418
|
+
* @param {Scope} scope 无垠数据层级。
|
|
419
|
+
* @param {...string} [fields] 待获取的字段。
|
|
420
|
+
* @return {!Object}
|
|
421
|
+
*/
|
|
422
|
+
get(scope, ...fields) {
|
|
423
|
+
if (!SCOPE[scope]) {
|
|
424
|
+
error('parameter "scope" is required.');
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const key = `${scope}Fields`;
|
|
428
|
+
const scopeFields = this[key];
|
|
429
|
+
if (!fields || !fields.length) {
|
|
430
|
+
return {
|
|
431
|
+
...scopeFields
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
return Object.keys(scopeFields).reduce((fieldsObject, key) => {
|
|
435
|
+
if (fields.includes(key)) {
|
|
436
|
+
fieldsObject[key] = scopeFields[key]; // 浅拷贝
|
|
437
|
+
}
|
|
438
|
+
return fieldsObject;
|
|
439
|
+
}, {});
|
|
440
|
+
}
|
|
441
|
+
toJSON() {
|
|
442
|
+
const basicFields = this.get('basic');
|
|
443
|
+
const commonFields = this.get('common');
|
|
444
|
+
const eventFields = this.get('event');
|
|
445
|
+
const now = Date.now();
|
|
446
|
+
const utc = getUTCTime(now);
|
|
447
|
+
const data = basicFields;
|
|
448
|
+
basicFields.report_time = now;
|
|
449
|
+
basicFields.data = commonFields;
|
|
450
|
+
commonFields.attributes = eventFields;
|
|
451
|
+
commonFields.timestamp = now;
|
|
452
|
+
commonFields.utc_time = utc;
|
|
453
|
+
return data;
|
|
454
|
+
}
|
|
455
|
+
deepByJSON() {
|
|
456
|
+
return JSON.parse(JSON.stringify(this.toJSON()));
|
|
457
|
+
}
|
|
458
|
+
clone() {
|
|
459
|
+
const basicFields = this.get('basic');
|
|
460
|
+
const commonFields = this.get('common');
|
|
461
|
+
const eventFields = this.get('event');
|
|
462
|
+
return new Store(basicFields, commonFields, eventFields);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* 创建无垠数据类。
|
|
468
|
+
* @param {!Object} [basicFields={}] 初始无垠数据基础公共数据。
|
|
469
|
+
* @param {!Object} [commonFields={}] 初始无垠数据公共业务数据。
|
|
470
|
+
* @param {!Object} [eventFields={}] 初始无垠数据业务事件数据。
|
|
471
|
+
* @return {Store}
|
|
472
|
+
*/
|
|
473
|
+
function createStore(basicFields, commonFields, eventFields) {
|
|
474
|
+
return new Store(basicFields, commonFields, eventFields);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const GUEST_USER_ID = '0';
|
|
478
|
+
const EMPTY_DEVICE_ID = '';
|
|
479
|
+
function sanitizeOrganization(organization) {
|
|
480
|
+
if (organization && typeof organization === 'object') {
|
|
481
|
+
delete organization.tenant_id;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function sanitizeAttributes(attributes) {
|
|
485
|
+
if (attributes && typeof attributes === 'object') {
|
|
486
|
+
delete attributes.tenant_id;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function isNonZeroDeviceId(deviceId) {
|
|
490
|
+
if ([undefined, null, EMPTY_DEVICE_ID].includes(deviceId)) {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
return String(deviceId) !== GUEST_USER_ID;
|
|
494
|
+
}
|
|
495
|
+
function normalizeGuestUserId(data) {
|
|
496
|
+
if (!isNonZeroDeviceId(data.device_id)) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (String(data.user_id) === String(data.device_id)) {
|
|
500
|
+
data.user_id = GUEST_USER_ID;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function sanitizeTrackData(trackData = {}) {
|
|
504
|
+
const {
|
|
505
|
+
data
|
|
506
|
+
} = trackData;
|
|
507
|
+
if (!data || typeof data !== 'object') {
|
|
508
|
+
return trackData;
|
|
509
|
+
}
|
|
510
|
+
sanitizeOrganization(data.organization);
|
|
511
|
+
sanitizeAttributes(data.attributes);
|
|
512
|
+
normalizeGuestUserId(data);
|
|
513
|
+
return trackData;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const DETAIL_ALIAS_FIELDS = [['page_name', 'page_nm'], ['sub_page_name', 'sub_page_nm'], ['button_name', 'button_nm']];
|
|
517
|
+
function normalizeDetailAliases(detail = {}) {
|
|
518
|
+
const normalizedDetail = Object.assign({}, detail);
|
|
519
|
+
DETAIL_ALIAS_FIELDS.forEach(([primaryField, aliasField]) => {
|
|
520
|
+
const primaryValue = normalizedDetail[primaryField];
|
|
521
|
+
const aliasValue = normalizedDetail[aliasField];
|
|
522
|
+
if (typeof primaryValue !== 'undefined' && typeof aliasValue === 'undefined') {
|
|
523
|
+
normalizedDetail[aliasField] = primaryValue;
|
|
524
|
+
} else if (typeof aliasValue !== 'undefined' && typeof primaryValue === 'undefined') {
|
|
525
|
+
normalizedDetail[primaryField] = aliasValue;
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
return normalizedDetail;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 支持 Unicode 的 base64 encode。
|
|
533
|
+
* @param {string} input 目标字符串
|
|
534
|
+
* @return {string}
|
|
535
|
+
*/
|
|
536
|
+
function base64encode(input) {
|
|
537
|
+
const UTF8Input = strToUTF8Arr(input);
|
|
538
|
+
return base64EncArr(UTF8Input);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/* Base64 string to array encoding */
|
|
542
|
+
|
|
543
|
+
function uint6ToB64(nUint6) {
|
|
544
|
+
return nUint6 < 26 ? nUint6 + 65 : nUint6 < 52 ? nUint6 + 71 : nUint6 < 62 ? nUint6 - 4 : nUint6 === 62 ? 43 : nUint6 === 63 ? 47 : 65;
|
|
545
|
+
}
|
|
546
|
+
function base64EncArr(aBytes) {
|
|
547
|
+
var eqLen = (3 - aBytes.length % 3) % 3;
|
|
548
|
+
var sB64Enc = '';
|
|
549
|
+
for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
|
|
550
|
+
nMod3 = nIdx % 3;
|
|
551
|
+
/* Uncomment the following line in order to split the output in lines 76-character long: */
|
|
552
|
+
/*
|
|
553
|
+
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
|
|
554
|
+
*/
|
|
555
|
+
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
|
|
556
|
+
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
|
|
557
|
+
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
|
|
558
|
+
nUint24 = 0;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? '=' : '==');
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/* DOMString to UTF-8 array */
|
|
565
|
+
function strToUTF8Arr(sDOMStr) {
|
|
566
|
+
var aBytes;
|
|
567
|
+
var nChr;
|
|
568
|
+
var nStrLen = sDOMStr.length;
|
|
569
|
+
var nArrLen = 0;
|
|
570
|
+
|
|
571
|
+
/* mapping... */
|
|
572
|
+
|
|
573
|
+
for (var nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
|
|
574
|
+
nChr = sDOMStr.charCodeAt(nMapIdx);
|
|
575
|
+
nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
|
|
576
|
+
}
|
|
577
|
+
aBytes = new Uint8Array(nArrLen);
|
|
578
|
+
|
|
579
|
+
/* transcription... */
|
|
580
|
+
|
|
581
|
+
for (var nIdx = 0, nChrIdx = 0; nIdx < nArrLen; nChrIdx++) {
|
|
582
|
+
nChr = sDOMStr.charCodeAt(nChrIdx);
|
|
583
|
+
if (nChr < 128) {
|
|
584
|
+
/* one byte */
|
|
585
|
+
aBytes[nIdx++] = nChr;
|
|
586
|
+
} else if (nChr < 0x800) {
|
|
587
|
+
/* two bytes */
|
|
588
|
+
aBytes[nIdx++] = 192 + (nChr >>> 6);
|
|
589
|
+
aBytes[nIdx++] = 128 + (nChr & 63);
|
|
590
|
+
} else if (nChr < 0x10000) {
|
|
591
|
+
/* three bytes */
|
|
592
|
+
aBytes[nIdx++] = 224 + (nChr >>> 12);
|
|
593
|
+
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
|
|
594
|
+
aBytes[nIdx++] = 128 + (nChr & 63);
|
|
595
|
+
} else if (nChr < 0x200000) {
|
|
596
|
+
/* four bytes */
|
|
597
|
+
aBytes[nIdx++] = 240 + (nChr >>> 18);
|
|
598
|
+
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
|
|
599
|
+
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
|
|
600
|
+
aBytes[nIdx++] = 128 + (nChr & 63);
|
|
601
|
+
} else if (nChr < 0x4000000) {
|
|
602
|
+
/* five bytes */
|
|
603
|
+
aBytes[nIdx++] = 248 + (nChr >>> 24);
|
|
604
|
+
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
|
|
605
|
+
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
|
|
606
|
+
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
|
|
607
|
+
aBytes[nIdx++] = 128 + (nChr & 63);
|
|
608
|
+
} else /* if (nChr <= 0x7fffffff) */{
|
|
609
|
+
/* six bytes */
|
|
610
|
+
aBytes[nIdx++] = 252 + (nChr >>> 30);
|
|
611
|
+
aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
|
|
612
|
+
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
|
|
613
|
+
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
|
|
614
|
+
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
|
|
615
|
+
aBytes[nIdx++] = 128 + (nChr & 63);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return aBytes;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function isObject(value) {
|
|
622
|
+
const type = typeof value;
|
|
623
|
+
return value != null && (type === 'object' || type === 'function');
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function isFunction(value) {
|
|
627
|
+
const result = typeof value === 'function';
|
|
628
|
+
return result;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const mergeStore = (store, option, params) => {
|
|
632
|
+
const merge = Object.assign({}, option);
|
|
633
|
+
const hasBasic = isFunction(merge.basic);
|
|
634
|
+
const hasCommon = isFunction(merge.common);
|
|
635
|
+
const hasEvent = isFunction(merge.event);
|
|
636
|
+
const hasDetail = isFunction(merge.detail);
|
|
637
|
+
const basic = Object.assign({}, store.get('basic'));
|
|
638
|
+
const common = Object.assign({}, store.get('common'));
|
|
639
|
+
const event = Object.assign({}, store.get('event'));
|
|
640
|
+
const detail = Object.assign({}, event.detail);
|
|
641
|
+
const basicCopy = Object.assign({}, basic);
|
|
642
|
+
const commonCopy = Object.assign({}, common);
|
|
643
|
+
const eventCopy = Object.assign({}, event);
|
|
644
|
+
const detailCopy = Object.assign({}, detail);
|
|
645
|
+
delete basicCopy.data;
|
|
646
|
+
delete commonCopy.attributes;
|
|
647
|
+
delete eventCopy.detail;
|
|
648
|
+
const mergeBasic = !hasBasic ? basic : merge.basic(basicCopy, Object.assign({}, params)) || basic;
|
|
649
|
+
const mergeCommon = !hasCommon ? common : merge.common(commonCopy, Object.assign({}, params)) || common;
|
|
650
|
+
const mergeEvent = !hasEvent ? event : merge.event(eventCopy, Object.assign({}, params)) || event;
|
|
651
|
+
const mergeDetail = !hasDetail ? detail : merge.detail(detailCopy, Object.assign({}, params)) || detail;
|
|
652
|
+
mergeEvent.detail = mergeDetail;
|
|
653
|
+
return {
|
|
654
|
+
basic: mergeBasic,
|
|
655
|
+
common: mergeCommon,
|
|
656
|
+
event: mergeEvent
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* 支持业务方在每个上报的数据里自动统一合并一些额外的数据,再将合并后的数据统一上报
|
|
662
|
+
*/
|
|
663
|
+
class DataMergeTracker {
|
|
664
|
+
constructor(tracker, options) {
|
|
665
|
+
const defaultOptions = {
|
|
666
|
+
basic: null,
|
|
667
|
+
common: null,
|
|
668
|
+
event: null,
|
|
669
|
+
detail: null
|
|
670
|
+
};
|
|
671
|
+
const obj = isObject(options) ? options : {};
|
|
672
|
+
this.options = Object.assign(defaultOptions, obj);
|
|
673
|
+
tracker.beforeEach((store, next) => {
|
|
674
|
+
const source = store.deepByJSON();
|
|
675
|
+
const {
|
|
676
|
+
basic,
|
|
677
|
+
common,
|
|
678
|
+
event
|
|
679
|
+
} = mergeStore(store, this.options, {
|
|
680
|
+
source
|
|
681
|
+
});
|
|
682
|
+
store.set('basic', basic);
|
|
683
|
+
store.set('common', common);
|
|
684
|
+
store.set('event', event);
|
|
685
|
+
next(store);
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
var version = "2.21.0-beta.5";
|
|
691
|
+
|
|
692
|
+
// SDK_VERSION 来自包元数据;不要读取调用方的 VERSION 环境变量,避免载荷被运行环境篡改。
|
|
693
|
+
|
|
694
|
+
// region → Wind endpoint 由 SDK 内部映射,业务不能通过 serverUrl 绕过。
|
|
695
|
+
// 与浏览器存量契约一致(tracker.js:221,269):region 缺省 CHINA(国内),仅 'US' 走 insmind 海外。
|
|
696
|
+
const DEFAULT_SERVER_HOST = 'https://wind.hlgdata.com';
|
|
697
|
+
const DEFAULT_SERVER_HOST_US = 'https://wind-data.insmind.com';
|
|
698
|
+
const REGION_VALUES = ['CHINA', 'US'];
|
|
699
|
+
function resolveServerUrl(region) {
|
|
700
|
+
const serverHost = region === 'US' ? DEFAULT_SERVER_HOST_US : DEFAULT_SERVER_HOST;
|
|
701
|
+
return `${serverHost}/wind`;
|
|
702
|
+
}
|
|
703
|
+
const DEFAULT_COORDINATES = {
|
|
704
|
+
region_id: '1',
|
|
705
|
+
biz_code: '1'
|
|
706
|
+
};
|
|
707
|
+
const ENVS = ['dev', 'stage', 'prod'];
|
|
708
|
+
|
|
709
|
+
// 会绕过 region endpoint 约束或依赖浏览器链路的存量选项,CLI 一律 fail fast。
|
|
710
|
+
const BROWSER_ONLY_OPTIONS = ['serverUrl', 'serverHost', 'debug', 'transport', 'isValidate', 'validateURL', 'login', 'needLogin', 'batch', 'cross', 'sendPayload', 'resetup', 'gTagId'];
|
|
711
|
+
const AB_OPTIONS = ['abProvider', 'abtest', 'abtestOptions', 'assignment'];
|
|
712
|
+
const SUPPORTED_OPTIONS = new Set(['mode', 'region', 'project', 'version', 'env', 'deviceId', 'userAgent', 'reportMode', 'deviceIdTimestamp', 'isFirstDay', 'coordinates', 'organization', 'plugins', 'adapters', 'showLog', 'onDiagnostic', ...BROWSER_ONLY_OPTIONS, ...AB_OPTIONS]);
|
|
713
|
+
|
|
714
|
+
// CLI 插件白名单:只保留无浏览器依赖的纯逻辑插件(方案 §3.5)。
|
|
715
|
+
const ALLOWED_PLUGINS = ['data-merge-tracker'];
|
|
716
|
+
|
|
717
|
+
// 业务传入 device ID 的校验规则(方案 §4.1 规则 13,是否收紧为 UUID v4 待 Phase 0)。
|
|
718
|
+
const DEVICE_ID_PATTERN = /^[A-Za-z0-9_-]{8,64}$/;
|
|
719
|
+
const MAX_PAYLOAD_BYTES = 512 * 1024;
|
|
720
|
+
const STATES = {
|
|
721
|
+
DETACHED: 'detached',
|
|
722
|
+
INITIALIZING: 'initializing',
|
|
723
|
+
READY: 'ready',
|
|
724
|
+
SHUTDOWN: 'shutdown'
|
|
725
|
+
};
|
|
726
|
+
const IMMUTABLE_FIELDS = Object.freeze({
|
|
727
|
+
basic: ['project', 'sdk_version', 'runtime'],
|
|
728
|
+
common: ['device_id', 'project_ver', 'session_id', 'env', 'user_agent', 'os_ver', 'runtime_type', 'region', 'event_id', 'event']
|
|
729
|
+
});
|
|
730
|
+
function cloneValue(value) {
|
|
731
|
+
return value === undefined ? value : JSON.parse(JSON.stringify(value));
|
|
732
|
+
}
|
|
733
|
+
function pickImmutableFields(scope, fields) {
|
|
734
|
+
return IMMUTABLE_FIELDS[scope].reduce((result, field) => {
|
|
735
|
+
if (Object.prototype.hasOwnProperty.call(fields, field)) {
|
|
736
|
+
result[field] = cloneValue(fields[field]);
|
|
737
|
+
}
|
|
738
|
+
return result;
|
|
739
|
+
}, {});
|
|
740
|
+
}
|
|
741
|
+
function assertPlainObject(value, name) {
|
|
742
|
+
if (value !== undefined && (value === null || typeof value !== 'object' || Array.isArray(value))) {
|
|
743
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, `${name} 必须是对象。`);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
class CliWindTracker {
|
|
747
|
+
constructor() {
|
|
748
|
+
this.state = STATES.DETACHED;
|
|
749
|
+
this.adapters = null;
|
|
750
|
+
this.store = null;
|
|
751
|
+
this.serverUrl = '';
|
|
752
|
+
this.sessionId = '';
|
|
753
|
+
this.showLog = false;
|
|
754
|
+
this.onDiagnostic = null;
|
|
755
|
+
this.beforeEachFns = [];
|
|
756
|
+
this.inflight = new Set();
|
|
757
|
+
this.setupPromise = null;
|
|
758
|
+
this.protectedFields = null;
|
|
759
|
+
}
|
|
760
|
+
diagnostic(code, message) {
|
|
761
|
+
const payload = {
|
|
762
|
+
code,
|
|
763
|
+
message,
|
|
764
|
+
sdk: 'gd-tracker',
|
|
765
|
+
mode: 'cli'
|
|
766
|
+
};
|
|
767
|
+
if (typeof this.onDiagnostic === 'function') {
|
|
768
|
+
this.onDiagnostic(payload);
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
if (this.showLog) {
|
|
772
|
+
console.error('[gd-tracker cli]', JSON.stringify(payload)); // eslint-disable-line no-console
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
assertReady(action) {
|
|
776
|
+
if (this.state === STATES.SHUTDOWN) {
|
|
777
|
+
throw new TrackerError(ERROR_CODES.NOT_READY, `shutdown 之后不能再调用 ${action}。`);
|
|
778
|
+
}
|
|
779
|
+
if (this.state !== STATES.READY) {
|
|
780
|
+
throw new TrackerError(ERROR_CODES.NOT_READY, `必须先 await trackerWind.setup(...) 再调用 ${action}。`);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
validateOptions(options) {
|
|
784
|
+
if (!options || typeof options !== 'object') {
|
|
785
|
+
throw new TrackerError(ERROR_CODES.INVALID_MODE, 'setup(options) 需要 options 对象。');
|
|
786
|
+
}
|
|
787
|
+
if (options.mode !== 'cli') {
|
|
788
|
+
throw new TrackerError(ERROR_CODES.INVALID_MODE, `node 入口只支持 mode: 'cli',收到 ${JSON.stringify(options.mode)}。`);
|
|
789
|
+
}
|
|
790
|
+
const unknown = Object.keys(options).filter(name => !SUPPORTED_OPTIONS.has(name));
|
|
791
|
+
if (unknown.length) {
|
|
792
|
+
this.diagnostic(ERROR_CODES.UNKNOWN_OPTION, `CLI setup 忽略未知选项:${unknown.join(', ')}。`);
|
|
793
|
+
}
|
|
794
|
+
const conflict = BROWSER_ONLY_OPTIONS.filter(name => options[name] !== undefined);
|
|
795
|
+
if (conflict.length) {
|
|
796
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, `CLI 模式不支持选项:${conflict.join(', ')}。`);
|
|
797
|
+
}
|
|
798
|
+
const abConflict = AB_OPTIONS.filter(name => options[name] !== undefined);
|
|
799
|
+
if (abConflict.length) {
|
|
800
|
+
throw new TrackerError(ERROR_CODES.AB_UNSUPPORTED, `CLI 首期不支持 AB:${abConflict.join(', ')}。`);
|
|
801
|
+
}
|
|
802
|
+
if (options.reportMode !== undefined && options.reportMode !== 'wind') {
|
|
803
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, `CLI 只支持 reportMode: 'wind',收到 ${JSON.stringify(options.reportMode)}。`);
|
|
804
|
+
}
|
|
805
|
+
if (options.region !== undefined && !REGION_VALUES.includes(options.region)) {
|
|
806
|
+
throw new TrackerError(ERROR_CODES.REGION_REQUIRED, "region 仅支持 'CHINA'(缺省,国内)或 'US'(insmind 海外)。");
|
|
807
|
+
}
|
|
808
|
+
if (typeof options.project !== 'string' || !options.project.trim()) {
|
|
809
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, 'project 必填。');
|
|
810
|
+
}
|
|
811
|
+
if (typeof options.version !== 'string' || !options.version.trim()) {
|
|
812
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, 'version 必填。');
|
|
813
|
+
}
|
|
814
|
+
if (!ENVS.includes(options.env)) {
|
|
815
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, `env 必须是 ${ENVS.join('/')},收到 ${JSON.stringify(options.env)}。`);
|
|
816
|
+
}
|
|
817
|
+
if (typeof options.deviceId !== 'string' || !DEVICE_ID_PATTERN.test(options.deviceId)) {
|
|
818
|
+
throw new TrackerError(ERROR_CODES.DEVICE_ID_REQUIRED, 'deviceId 必填(8-64 位,字符集 A-Za-z0-9_-),由业务生成并持久化。');
|
|
819
|
+
}
|
|
820
|
+
if (options.userAgent !== undefined && (typeof options.userAgent !== 'string' || !options.userAgent.trim())) {
|
|
821
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, 'userAgent 必须是非空字符串。');
|
|
822
|
+
}
|
|
823
|
+
assertPlainObject(options.coordinates, 'coordinates');
|
|
824
|
+
assertPlainObject(options.organization, 'organization');
|
|
825
|
+
if (options.deviceIdTimestamp !== undefined && typeof options.deviceIdTimestamp !== 'number') {
|
|
826
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, 'deviceIdTimestamp 必须是数字时间戳。');
|
|
827
|
+
}
|
|
828
|
+
if (options.isFirstDay !== undefined && ![0, 1].includes(options.isFirstDay)) {
|
|
829
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, 'isFirstDay 只能是 0 或 1(由业务根据 device ID 创建时间传入)。');
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
validateAdapters(adapters) {
|
|
833
|
+
if (!adapters || !adapters.environment || typeof adapters.environment.now !== 'function' || typeof adapters.environment.randomUUID !== 'function' || typeof adapters.environment.runtime !== 'function' || !adapters.transport || typeof adapters.transport.send !== 'function') {
|
|
834
|
+
throw new TrackerError(ERROR_CODES.ADAPTER_REQUIRED, 'adapters 需要提供 environment(now/randomUUID/runtime) 与 transport(send)。');
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
async resolveRuntime(adapters) {
|
|
838
|
+
const syncRuntime = () => adapters.environment.runtime();
|
|
839
|
+
if (typeof adapters.environment.runtimeAsync !== 'function') {
|
|
840
|
+
return syncRuntime();
|
|
841
|
+
}
|
|
842
|
+
// device_model/manufacturer 采集限时:失败或超时即用同步字段缺省,不阻塞 setup 关键路径。
|
|
843
|
+
let fallbackTimer;
|
|
844
|
+
const fallbackRuntime = new Promise(resolve => {
|
|
845
|
+
fallbackTimer = setTimeout(() => resolve(syncRuntime()), 1000);
|
|
846
|
+
});
|
|
847
|
+
try {
|
|
848
|
+
const runtime = await Promise.race([adapters.environment.runtimeAsync().catch(() => syncRuntime()), fallbackRuntime]);
|
|
849
|
+
return runtime;
|
|
850
|
+
} finally {
|
|
851
|
+
clearTimeout(fallbackTimer);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
resolvePlugins(plugins) {
|
|
855
|
+
if (plugins === undefined) {
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
if (!Array.isArray(plugins)) {
|
|
859
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_PLUGIN, 'plugins 必须是数组,元素为 [name, options] 或 name。');
|
|
860
|
+
}
|
|
861
|
+
plugins.forEach(plugin => {
|
|
862
|
+
const name = Array.isArray(plugin) ? plugin[0] : plugin;
|
|
863
|
+
const pluginOptions = Array.isArray(plugin) ? plugin[1] : undefined;
|
|
864
|
+
if (!ALLOWED_PLUGINS.includes(name)) {
|
|
865
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_PLUGIN, `CLI 白名单外插件:${JSON.stringify(name)}(浏览器专属插件在 CLI 不可用,白名单:${ALLOWED_PLUGINS.join('/')})。`);
|
|
866
|
+
}
|
|
867
|
+
if (name === 'data-merge-tracker') {
|
|
868
|
+
new DataMergeTracker(this, pluginOptions); // eslint-disable-line no-new
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* 初始化 CLI tracker。幂等:重复调用返回同一个初始化结果;失败不残留半初始化状态。
|
|
875
|
+
*/
|
|
876
|
+
setup(options) {
|
|
877
|
+
if (this.state === STATES.READY) {
|
|
878
|
+
return Promise.resolve();
|
|
879
|
+
}
|
|
880
|
+
if (this.state === STATES.INITIALIZING) {
|
|
881
|
+
return this.setupPromise;
|
|
882
|
+
}
|
|
883
|
+
this.state = STATES.INITIALIZING;
|
|
884
|
+
this.setupPromise = this.initialize(options).catch(error => {
|
|
885
|
+
this.state = STATES.DETACHED;
|
|
886
|
+
this.store = null;
|
|
887
|
+
this.beforeEachFns = [];
|
|
888
|
+
this.protectedFields = null;
|
|
889
|
+
this.showLog = false;
|
|
890
|
+
this.onDiagnostic = null;
|
|
891
|
+
this.setupPromise = null;
|
|
892
|
+
throw error;
|
|
893
|
+
});
|
|
894
|
+
return this.setupPromise;
|
|
895
|
+
}
|
|
896
|
+
async initialize(options) {
|
|
897
|
+
// 配置诊断回调后再校验,以便未知选项等非阻断告警也能被业务收到。
|
|
898
|
+
this.showLog = Boolean(options && options.showLog);
|
|
899
|
+
this.onDiagnostic = options && typeof options.onDiagnostic === 'function' ? options.onDiagnostic : null;
|
|
900
|
+
this.validateOptions(options);
|
|
901
|
+
const adapters = options.adapters || createNodeAdapters();
|
|
902
|
+
this.validateAdapters(adapters);
|
|
903
|
+
const runtime = await this.resolveRuntime(adapters);
|
|
904
|
+
this.adapters = adapters;
|
|
905
|
+
this.showLog = Boolean(options.showLog);
|
|
906
|
+
this.onDiagnostic = typeof options.onDiagnostic === 'function' ? options.onDiagnostic : null;
|
|
907
|
+
this.serverUrl = resolveServerUrl(options.region);
|
|
908
|
+
this.sessionId = adapters.environment.randomUUID();
|
|
909
|
+
|
|
910
|
+
// 与浏览器字段结构对齐;CLI 缺省 path/screen/referrer/starter(方案 §4.4)。
|
|
911
|
+
const basicFields = {
|
|
912
|
+
project: options.project,
|
|
913
|
+
sdk_version: version,
|
|
914
|
+
runtime
|
|
915
|
+
};
|
|
916
|
+
const commonFields = {
|
|
917
|
+
device_id: options.deviceId,
|
|
918
|
+
project_ver: options.version,
|
|
919
|
+
session_id: this.sessionId,
|
|
920
|
+
env: options.env,
|
|
921
|
+
user_id: '',
|
|
922
|
+
is_login: 0,
|
|
923
|
+
user_agent: options.userAgent || `gd-cli/${options.version}`,
|
|
924
|
+
// os_ver 放在 common(data)层:服务端 UA 富化在 data.os_ver 存在时短路,不再用 bitwalker 解析覆盖 runtime(方案 §5.4)。
|
|
925
|
+
os_ver: runtime.os_version,
|
|
926
|
+
runtime_type: 'cli',
|
|
927
|
+
region: options.region === 'US' ? 'US' : 'CHINA'
|
|
928
|
+
};
|
|
929
|
+
if (options.deviceIdTimestamp !== undefined) {
|
|
930
|
+
commonFields.device_id_timestamp = options.deviceIdTimestamp;
|
|
931
|
+
}
|
|
932
|
+
if (options.isFirstDay !== undefined) {
|
|
933
|
+
commonFields.is_first_day = options.isFirstDay;
|
|
934
|
+
}
|
|
935
|
+
if (options.organization) {
|
|
936
|
+
commonFields.organization = options.organization;
|
|
937
|
+
}
|
|
938
|
+
const eventFields = {
|
|
939
|
+
...DEFAULT_COORDINATES,
|
|
940
|
+
...(options.coordinates && options.coordinates.region_id ? {
|
|
941
|
+
region_id: String(options.coordinates.region_id)
|
|
942
|
+
} : {}),
|
|
943
|
+
...(options.coordinates && options.coordinates.biz_code ? {
|
|
944
|
+
biz_code: String(options.coordinates.biz_code)
|
|
945
|
+
} : {}),
|
|
946
|
+
...(options.coordinates && options.coordinates.endpoint ? {
|
|
947
|
+
endpoint: String(options.coordinates.endpoint)
|
|
948
|
+
} : {})
|
|
949
|
+
};
|
|
950
|
+
this.resolvePlugins(options.plugins);
|
|
951
|
+
this.store = createStore(basicFields, commonFields, eventFields);
|
|
952
|
+
this.protectedFields = {
|
|
953
|
+
basic: pickImmutableFields('basic', basicFields),
|
|
954
|
+
common: pickImmutableFields('common', commonFields)
|
|
955
|
+
};
|
|
956
|
+
this.state = STATES.READY;
|
|
957
|
+
}
|
|
958
|
+
restoreProtectedFields(store, eventIdentity) {
|
|
959
|
+
store.set('basic', cloneValue(this.protectedFields.basic));
|
|
960
|
+
store.set('common', cloneValue(this.protectedFields.common));
|
|
961
|
+
store.set('common', eventIdentity);
|
|
962
|
+
}
|
|
963
|
+
assertMutableFields(scope, fieldsObject) {
|
|
964
|
+
const protectedFields = IMMUTABLE_FIELDS[scope] || [];
|
|
965
|
+
const blocked = Object.keys(fieldsObject || {}).filter(field => protectedFields.includes(field));
|
|
966
|
+
if (blocked.length) {
|
|
967
|
+
throw new TrackerError(ERROR_CODES.UNSUPPORTED_OPTION, `${scope} 层字段不可覆盖:${blocked.join(', ')}。`);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/**
|
|
972
|
+
* 上报事件。完成脱敏与序列化后立即实时发送(无批量队列)。
|
|
973
|
+
* @returns {Promise<Object>} 结构化投递结果;遥测失败不抛异常,由调用方决定是否关注
|
|
974
|
+
*/
|
|
975
|
+
trackEvent(event, details = {}, scopeObject) {
|
|
976
|
+
if (this.state === STATES.SHUTDOWN) {
|
|
977
|
+
this.diagnostic(ERROR_CODES.NOT_READY, `shutdown 之后调用 trackEvent(${String(event)}),事件已丢弃。`);
|
|
978
|
+
return Promise.resolve({
|
|
979
|
+
accepted: false,
|
|
980
|
+
retryable: false,
|
|
981
|
+
dropped: true,
|
|
982
|
+
errorCode: ERROR_CODES.NOT_READY
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
if (this.state !== STATES.READY) {
|
|
986
|
+
return Promise.reject(new TrackerError(ERROR_CODES.NOT_READY, '必须先 await trackerWind.setup(...) 再调用 trackEvent。'));
|
|
987
|
+
}
|
|
988
|
+
if (typeof event !== 'string' || !event.trim()) {
|
|
989
|
+
return Promise.reject(new TrackerError(ERROR_CODES.INVALID_EVENT, 'event 必须是非空字符串。'));
|
|
990
|
+
}
|
|
991
|
+
if (!details || typeof details !== 'object') {
|
|
992
|
+
return Promise.reject(new TrackerError(ERROR_CODES.INVALID_EVENT, 'details 必须是对象,且 event_id 必填。'));
|
|
993
|
+
}
|
|
994
|
+
const {
|
|
995
|
+
event_id,
|
|
996
|
+
...restDetails
|
|
997
|
+
} = details;
|
|
998
|
+
if (!event_id) {
|
|
999
|
+
return Promise.reject(new TrackerError(ERROR_CODES.INVALID_EVENT, 'details.event_id 必填。'));
|
|
1000
|
+
}
|
|
1001
|
+
const scope = scopeObject || {};
|
|
1002
|
+
const store = this.store.clone();
|
|
1003
|
+
store.set('basic', scope.basic || {});
|
|
1004
|
+
store.set('common', {
|
|
1005
|
+
event_id,
|
|
1006
|
+
event,
|
|
1007
|
+
...(scope.common || {})
|
|
1008
|
+
});
|
|
1009
|
+
store.set('event', {
|
|
1010
|
+
...(scope.event || {})
|
|
1011
|
+
});
|
|
1012
|
+
const normalizedRest = normalizeDetailAliases(restDetails);
|
|
1013
|
+
if (Object.keys(normalizedRest).length) {
|
|
1014
|
+
store.set('event', {
|
|
1015
|
+
detail: normalizedRest
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
const eventIdentity = {
|
|
1019
|
+
event_id,
|
|
1020
|
+
event
|
|
1021
|
+
};
|
|
1022
|
+
|
|
1023
|
+
// beforeEach 钩子(data-merge-tracker 等纯逻辑插件)→ 终止节点序列化发送。
|
|
1024
|
+
let sendPromise = null;
|
|
1025
|
+
const terminating = currentStore => {
|
|
1026
|
+
// 插件和 scope 只允许补充业务字段,不能改变 setup 绑定的身份、项目或地域。
|
|
1027
|
+
this.restoreProtectedFields(currentStore, eventIdentity);
|
|
1028
|
+
const storeData = sanitizeTrackData(currentStore.toJSON());
|
|
1029
|
+
const params = {
|
|
1030
|
+
gzip: 0,
|
|
1031
|
+
use_client_time: false,
|
|
1032
|
+
data: base64encode(JSON.stringify(storeData))
|
|
1033
|
+
};
|
|
1034
|
+
if (Buffer.byteLength(params.data, 'utf8') > MAX_PAYLOAD_BYTES) {
|
|
1035
|
+
this.diagnostic(ERROR_CODES.TRANSPORT_ERROR, `事件载荷超过 ${MAX_PAYLOAD_BYTES} 字节,已丢弃:${String(event)}`);
|
|
1036
|
+
sendPromise = Promise.resolve({
|
|
1037
|
+
accepted: false,
|
|
1038
|
+
retryable: false,
|
|
1039
|
+
dropped: true,
|
|
1040
|
+
errorCode: ERROR_CODES.TRANSPORT_ERROR
|
|
1041
|
+
});
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
sendPromise = this.dispatch(params);
|
|
1045
|
+
};
|
|
1046
|
+
const fns = [...this.beforeEachFns];
|
|
1047
|
+
const run = (index, currentStore) => {
|
|
1048
|
+
if (index < fns.length) {
|
|
1049
|
+
fns[index](currentStore, () => run(index + 1, currentStore));
|
|
1050
|
+
} else {
|
|
1051
|
+
terminating(currentStore);
|
|
1052
|
+
}
|
|
1053
|
+
};
|
|
1054
|
+
try {
|
|
1055
|
+
run(0, store);
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
return Promise.reject(error);
|
|
1058
|
+
}
|
|
1059
|
+
// 终止节点已同步触发发送;无论是否有插件拦截,都返回终止节点的投递结果。
|
|
1060
|
+
return sendPromise || Promise.resolve({
|
|
1061
|
+
accepted: false,
|
|
1062
|
+
retryable: false,
|
|
1063
|
+
dropped: true,
|
|
1064
|
+
errorCode: ERROR_CODES.NOT_READY
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
dispatch(params) {
|
|
1068
|
+
const controller = new AbortController();
|
|
1069
|
+
const entry = {
|
|
1070
|
+
controller
|
|
1071
|
+
};
|
|
1072
|
+
const remove = () => this.inflight.delete(entry);
|
|
1073
|
+
entry.promise = this.adapters.transport.send({
|
|
1074
|
+
url: this.serverUrl,
|
|
1075
|
+
params
|
|
1076
|
+
}, controller.signal).then(result => {
|
|
1077
|
+
if (!result || !result.accepted) {
|
|
1078
|
+
this.diagnostic(ERROR_CODES.TRANSPORT_ERROR, `Wind 上报未成功:${JSON.stringify(result)}`);
|
|
1079
|
+
}
|
|
1080
|
+
return result;
|
|
1081
|
+
}).finally(remove);
|
|
1082
|
+
this.inflight.add(entry);
|
|
1083
|
+
return entry.promise;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
/**
|
|
1087
|
+
* 注册发送前钩子(内部供纯逻辑插件使用)。
|
|
1088
|
+
*/
|
|
1089
|
+
beforeEach(fn) {
|
|
1090
|
+
this.beforeEachFns.push(fn);
|
|
1091
|
+
}
|
|
1092
|
+
set(scope, fieldsObject) {
|
|
1093
|
+
this.assertReady('set');
|
|
1094
|
+
this.assertMutableFields(scope, fieldsObject);
|
|
1095
|
+
this.store.set(scope, fieldsObject);
|
|
1096
|
+
}
|
|
1097
|
+
get(scope, ...fields) {
|
|
1098
|
+
this.assertReady('get');
|
|
1099
|
+
return this.store.get(scope, ...fields);
|
|
1100
|
+
}
|
|
1101
|
+
drain(timeoutMs) {
|
|
1102
|
+
const entries = Array.from(this.inflight);
|
|
1103
|
+
if (!entries.length) {
|
|
1104
|
+
return Promise.resolve({
|
|
1105
|
+
flushed: true,
|
|
1106
|
+
pending: 0
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
return new Promise(resolve => {
|
|
1110
|
+
const timer = setTimeout(() => {
|
|
1111
|
+
entries.forEach(entry => entry.controller.abort(new Error('flush/shutdown timeout')));
|
|
1112
|
+
resolve({
|
|
1113
|
+
flushed: false,
|
|
1114
|
+
pending: entries.length,
|
|
1115
|
+
errorCode: ERROR_CODES.FLUSH_TIMEOUT
|
|
1116
|
+
});
|
|
1117
|
+
}, timeoutMs);
|
|
1118
|
+
Promise.allSettled(entries.map(entry => entry.promise)).then(() => {
|
|
1119
|
+
clearTimeout(timer);
|
|
1120
|
+
resolve({
|
|
1121
|
+
flushed: true,
|
|
1122
|
+
pending: 0
|
|
1123
|
+
});
|
|
1124
|
+
});
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* 等待当前进程内已发起的实时请求完成,不读取磁盘、不重放历史事件。
|
|
1130
|
+
*/
|
|
1131
|
+
async flush({
|
|
1132
|
+
timeoutMs = 2000
|
|
1133
|
+
} = {}) {
|
|
1134
|
+
this.assertReady('flush');
|
|
1135
|
+
return this.drain(timeoutMs);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* 停止接收新事件,等待 in-flight 请求,超时后中止剩余请求并返回失败摘要。
|
|
1140
|
+
*/
|
|
1141
|
+
async shutdown({
|
|
1142
|
+
timeoutMs = 2000
|
|
1143
|
+
} = {}) {
|
|
1144
|
+
if (this.state === STATES.DETACHED || this.state === STATES.INITIALIZING) {
|
|
1145
|
+
throw new TrackerError(ERROR_CODES.NOT_READY, 'setup 尚未完成,无需 shutdown。');
|
|
1146
|
+
}
|
|
1147
|
+
if (this.state === STATES.SHUTDOWN) {
|
|
1148
|
+
return {
|
|
1149
|
+
flushed: false,
|
|
1150
|
+
pending: 0
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
this.state = STATES.SHUTDOWN;
|
|
1154
|
+
const result = await this.drain(timeoutMs);
|
|
1155
|
+
// 超时后 transport 可能暂时不响应 abort;保留未结束 entry,直到 finally 实际移除。
|
|
1156
|
+
// 这样 inflight 反映真实状态,也避免 shutdown 的清理动作制造虚假的“已结束”。
|
|
1157
|
+
if (result.flushed) {
|
|
1158
|
+
this.inflight.clear();
|
|
1159
|
+
}
|
|
1160
|
+
this.store = null;
|
|
1161
|
+
this.beforeEachFns = [];
|
|
1162
|
+
this.protectedFields = null;
|
|
1163
|
+
return result;
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
const trackerWind = new CliWindTracker();
|
|
1167
|
+
|
|
1168
|
+
/**
|
|
1169
|
+
* @gaoding/gd-tracker/node —— CLI(Node) 模式入口。
|
|
1170
|
+
*
|
|
1171
|
+
* 只包含 Node 运行时实现,不访问 window/document/navigator/localStorage 等浏览器全局。
|
|
1172
|
+
* 使用方式见 proposal/cli-mode-proposal.md §4.2 与 README「CLI(Node)模式」章节。
|
|
1173
|
+
*/
|
|
1174
|
+
|
|
1175
|
+
export { CliWindTracker, ERROR_CODES, TrackerError, createNodeAdapters, trackerWind };
|