-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(canyon-backend): add protobuf compress
- Loading branch information
1 parent
cb8b0b3
commit 55a06a6
Showing
5 changed files
with
10,411 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
syntax = "proto3"; | ||
|
||
message CoverageData { | ||
map<string, FileCoverageData> data = 1; | ||
} | ||
|
||
message FileCoverageData { | ||
string path = 1; | ||
map<string, Range> statementMap = 2; | ||
map<string, FunctionMapping> fnMap = 3; | ||
map<string, BranchMapping> branchMap = 4; | ||
map<string, int32> s = 5; | ||
map<string, int32> f = 6; | ||
map<string, RepeatedInt32> b = 7; | ||
} | ||
|
||
message RepeatedInt32 { | ||
repeated int32 data = 1; | ||
} | ||
|
||
message Range { | ||
Location start = 1; | ||
Location end = 2; | ||
} | ||
message Location { | ||
int32 line = 1; | ||
int32 column = 2; | ||
} | ||
|
||
message FunctionMapping { | ||
string name = 1; | ||
Range loc = 2; | ||
Range decl = 3; | ||
int32 line = 4; | ||
} | ||
|
||
message BranchMapping { | ||
Range loc = 1; | ||
string type = 2; | ||
int32 line = 3; | ||
repeated Range locations = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,110 @@ | ||
// 学习upload里的步骤 | ||
import { compress, decompress } from "@mongodb-js/zstd"; | ||
import {CoverageMapData} from "istanbul-lib-coverage"; | ||
import * as protobuf from "protobufjs"; | ||
|
||
function jianchaJiaZhuanhuan(coverageData) { | ||
// @ts-ignore | ||
return Object.fromEntries(Object.entries(coverageData).map(([key, value]) => [ | ||
key, | ||
{ | ||
// @ts-ignore | ||
...value, | ||
// @ts-ignore | ||
b: Object.fromEntries(Object.entries(value.b).map(([k, v]) => [k, { data: v }])) | ||
} | ||
])); | ||
} | ||
|
||
function fan_jianchaJiaZhuanhuan(fan_coverageData) { | ||
// console.log(fan_coverageData,'fan_coverageData') | ||
// @ts-ignore | ||
return Object.fromEntries(Object.entries(fan_coverageData).map(([key, value]) => [ | ||
key, | ||
{ | ||
// @ts-ignore | ||
...value, | ||
// @ts-ignore | ||
b: Object.fromEntries(Object.entries(value.b).map(([k, v]) => [k, v.data])) | ||
} | ||
])); | ||
} | ||
|
||
export async function compressCoverageData(coverageData: CoverageMapData) { | ||
const time = new Date().getTime() | ||
return new Promise((resolve, reject) => { | ||
// 获取根目录下的coverage.proto文件 | ||
protobuf.load("proto/coverage.proto", function(err, root) { | ||
if (err) | ||
throw err; | ||
|
||
// Obtain a message type | ||
const AwesomeMessage = root.lookupType("CoverageData"); | ||
|
||
// Exemplary payload | ||
const payload = { | ||
data: jianchaJiaZhuanhuan(coverageData) | ||
} | ||
|
||
// Verify the payload if necessary (i.e. when possibly incomplete or invalid) | ||
const errMsg = AwesomeMessage.verify(payload); | ||
if (errMsg) | ||
throw Error(errMsg); | ||
|
||
// Create a new message | ||
const message = AwesomeMessage.create(payload); // or use .fromObject if conversion is necessary | ||
|
||
// Encode a message to an Uint8Array (browser) or Buffer (node) | ||
const buffer2 = AwesomeMessage.encode(message).finish(); | ||
// ... do something with buffer | ||
|
||
// resolve(buffer); | ||
|
||
// console.log(buffer2) | ||
// @ts-ignore | ||
// const res = compress(buffer2); | ||
compress(buffer2).then((res) => { | ||
// console.log(new Date().getTime() - time, 'compress time') | ||
console.log(`压缩耗时: ${new Date().getTime() - time} ms`) | ||
// 压缩率 | ||
console.log(`${JSON.stringify(coverageData).length} b`, `${res.length} b`, (100*res.length / JSON.stringify(coverageData).length).toFixed(2)+'%') | ||
resolve(res); | ||
}); | ||
|
||
|
||
}); | ||
}); | ||
} | ||
|
||
export async function decompressCoverageData(buffer) { | ||
return new Promise((resolve, reject) => { | ||
decompress(buffer).then((res) => { | ||
protobuf.load("proto/coverage.proto", function(err, root) { | ||
if (err) | ||
throw err; | ||
|
||
// Obtain a message type | ||
const AwesomeMessage = root.lookupType("CoverageData"); | ||
|
||
|
||
|
||
var message = AwesomeMessage.decode(res); | ||
// ... do something with message | ||
|
||
// If the application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited. | ||
|
||
// Maybe convert the message back to a plain object | ||
var object = AwesomeMessage.toObject(message, { | ||
longs: String, | ||
enums: String, | ||
bytes: String, | ||
// see ConversionOptions | ||
}); | ||
|
||
|
||
resolve(fan_jianchaJiaZhuanhuan(object.data)); | ||
|
||
} | ||
) | ||
}) | ||
}); | ||
} |
Oops, something went wrong.