From 05c049d336d138de247db771540ae237541607eb Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Mon, 2 Dec 2024 20:01:22 +0800 Subject: [PATCH] chore: daily development --- tools/canyon-sdk/package.json | 2 +- tools/canyon-sdk/src/helpers/assemblyData.ts | 23 +++++++ .../src/helpers/compressDataWithStream.ts | 17 ++++++ tools/canyon-sdk/src/main.ts | 60 +++---------------- tools/canyon-sdk/src/window-extensions.d.ts | 28 +++++++++ 5 files changed, 76 insertions(+), 54 deletions(-) create mode 100644 tools/canyon-sdk/src/helpers/assemblyData.ts create mode 100644 tools/canyon-sdk/src/helpers/compressDataWithStream.ts create mode 100644 tools/canyon-sdk/src/window-extensions.d.ts diff --git a/tools/canyon-sdk/package.json b/tools/canyon-sdk/package.json index 1d3fc90a..4fee5fab 100644 --- a/tools/canyon-sdk/package.json +++ b/tools/canyon-sdk/package.json @@ -1,6 +1,6 @@ { "name": "canyon-sdk", - "version": "2.0.0-beta.3", + "version": "2.0.0-beta.5", "files": [ "dist" ], diff --git a/tools/canyon-sdk/src/helpers/assemblyData.ts b/tools/canyon-sdk/src/helpers/assemblyData.ts new file mode 100644 index 00000000..83baaf1a --- /dev/null +++ b/tools/canyon-sdk/src/helpers/assemblyData.ts @@ -0,0 +1,23 @@ +export function assemblyData (__coverage__: Window['__coverage__']) { + // 组装数据,确保最小 + + const cov = Object.entries(__coverage__).map(([path, { b, f, s }]) => ({ + path, + b, + f, + s, + })).reduce((acc, { path, b, f, s }) => { + acc[path] = { b, f, s } + return acc + },{}) + + // 获取meta参数 + const { projectID, sha, instrumentCwd,dsn } = Object.values(__coverage__)[0] + return { + coverage: cov, + projectID, + sha, + instrumentCwd, + dsn + } +} diff --git a/tools/canyon-sdk/src/helpers/compressDataWithStream.ts b/tools/canyon-sdk/src/helpers/compressDataWithStream.ts new file mode 100644 index 00000000..c9634bc7 --- /dev/null +++ b/tools/canyon-sdk/src/helpers/compressDataWithStream.ts @@ -0,0 +1,17 @@ +export async function compressDataWithStream (data) { + const textEncoder = new TextEncoder() + const input = textEncoder.encode(data) + + const compressionStream = new CompressionStream('gzip') + const writer = compressionStream.writable.getWriter() + writer.write(input) + writer.close() + + const compressedData = [] + const reader = compressionStream.readable.getReader() + let result + while (!(result = await reader.read()).done) { + compressedData.push(result.value) + } + return new Blob(compressedData) // 压缩后的数据为 Blob +} diff --git a/tools/canyon-sdk/src/main.ts b/tools/canyon-sdk/src/main.ts index 1821cd20..a799e13c 100644 --- a/tools/canyon-sdk/src/main.ts +++ b/tools/canyon-sdk/src/main.ts @@ -1,3 +1,8 @@ +import {compressDataWithStream} from "./helpers/compressDataWithStream"; +import {assemblyData} from "./helpers/assemblyData"; + +// 定义window.__coverage__数据 + (async () => { /* /coverage/client适配 @@ -19,57 +24,10 @@ * * */ const timeout = 500 - - async function compressDataWithStream (data) { - const textEncoder = new TextEncoder() - const input = textEncoder.encode(data) - - const compressionStream = new CompressionStream('gzip') - const writer = compressionStream.writable.getWriter() - writer.write(input) - writer.close() - - const compressedData = [] - const reader = compressionStream.readable.getReader() - let result - while (!(result = await reader.read()).done) { - compressedData.push(result.value) - } - return new Blob(compressedData) // 压缩后的数据为 Blob - } - - function assemblyData () { - // 组装数据,确保最小 - - const cov = Object.entries(window.__coverage__).map(([path, { b, f, s }]) => ({ - path, - b, - f, - s, - })).reduce((acc, { path, b, f, s }) => { - acc[path] = { b, f, s } - return acc - },{}) - - // 获取meta参数 - const { projectID, sha, instrumentCwd,dsn } = Object.values(window.__coverage__)[0] - return { - coverage: cov, - projectID, - sha, - instrumentCwd, - dsn - } - } - async function collectCoverageData (timing) { - - const {coverage,projectID,sha,instrumentCwd,dsn} = assemblyData() + const {coverage,projectID,sha,instrumentCwd,dsn} = assemblyData(window.__coverage__) const fd = new FormData() - - const blob = await compressDataWithStream(JSON.stringify(coverage)) - // 覆盖率数据 fd.append('coverage', blob) // canyon数据 @@ -78,15 +36,12 @@ fd.append('instrumentCwd', instrumentCwd) // 触发时机 fd.append('timing', timing) - // 要发探测数据,id客户端生成 - navigator.sendBeacon( dsn, fd, ) } - const timerHandler = () => { if (window.__coverage__){ window.addEventListener('unload', () => { @@ -99,6 +54,5 @@ console.log('canyon: no coverage data') } } - - // setTimeout(timerHandler, timeout) + setTimeout(timerHandler, timeout) })() diff --git a/tools/canyon-sdk/src/window-extensions.d.ts b/tools/canyon-sdk/src/window-extensions.d.ts new file mode 100644 index 00000000..83a552e5 --- /dev/null +++ b/tools/canyon-sdk/src/window-extensions.d.ts @@ -0,0 +1,28 @@ +// window-extensions.d.ts +// 首先定义一些可能用到的子类型 + +// 表示对象中类似 "s"、"f"、"b" 这种键对应的值的类型 +type CoverageObjectValues = { + [key: string]: number | number[]; +}; + +// 定义 window.__coverage__ 的类型 +interface WindowCoverage { + path: string; + s: CoverageObjectValues; + f: CoverageObjectValues; + b: CoverageObjectValues; + _coverageSchema: string; + hash: string; + sha: string; + projectID: string; + instrumentCwd: string; + dsn: string; +} + +// 扩展Window接口 +interface Window { + __coverage__: { + [key: string]: WindowCoverage; + } +}