Skip to content

Commit

Permalink
chore: daily development
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Dec 2, 2024
1 parent 472d437 commit 05c049d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 54 deletions.
2 changes: 1 addition & 1 deletion tools/canyon-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canyon-sdk",
"version": "2.0.0-beta.3",
"version": "2.0.0-beta.5",
"files": [
"dist"
],
Expand Down
23 changes: 23 additions & 0 deletions tools/canyon-sdk/src/helpers/assemblyData.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
17 changes: 17 additions & 0 deletions tools/canyon-sdk/src/helpers/compressDataWithStream.ts
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 7 additions & 53 deletions tools/canyon-sdk/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {compressDataWithStream} from "./helpers/compressDataWithStream";
import {assemblyData} from "./helpers/assemblyData";

// 定义window.__coverage__数据

(async () => {
/*
/coverage/client适配
Expand All @@ -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数据
Expand All @@ -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', () => {
Expand All @@ -99,6 +54,5 @@
console.log('canyon: no coverage data')
}
}

// setTimeout(timerHandler, timeout)
setTimeout(timerHandler, timeout)
})()
28 changes: 28 additions & 0 deletions tools/canyon-sdk/src/window-extensions.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 05c049d

Please sign in to comment.