Starting on one-way sync
This commit is contained in:
126
src/com/pullgetlogrequest.ts
Normal file
126
src/com/pullgetlogrequest.ts
Normal file
@ -0,0 +1,126 @@
|
||||
export default interface PullGetLogRequest {
|
||||
limit: number;
|
||||
start: string;
|
||||
}
|
||||
export function serialize_PullGetLogRequest (data: PullGetLogRequest): Uint8Array {
|
||||
let fields: ArrayBuffer[] = [];
|
||||
const addBuffer = (buffer:ArrayBuffer) => fields.push(buffer);
|
||||
const makeField = (payload_length: number, label: number, type: number) => {
|
||||
let b = new ArrayBuffer(3 + payload_length);
|
||||
new DataView(b).setUint16(0, label);
|
||||
new DataView(b).setUint8(2, type);
|
||||
fields.push(b);
|
||||
return new DataView(b, 3);
|
||||
}
|
||||
|
||||
const makeArrayBuffer = (payload_length: number) => {
|
||||
let b = new ArrayBuffer(payload_length);
|
||||
fields.push(b)
|
||||
return new DataView(b)
|
||||
}
|
||||
|
||||
const getAutoGrowLength = (length: number | bigint) => {
|
||||
if (typeof length === "number") length = BigInt(length);
|
||||
const lt1 = length >= BigInt(Math.pow(2, 15)) ? 0x8000 : 0;
|
||||
const lt2 = length >= BigInt(Math.pow(2, 30)) ? 0x8000 : 0;
|
||||
const lt3 = length >= BigInt(Math.pow(2, 45)) ? 0x8000 : 0;
|
||||
const lt4 = length >= BigInt(Math.pow(2, 61));
|
||||
if (lt4) throw new Error("Payload to large!");
|
||||
let blen = BigInt(length);
|
||||
const u16_1 = (Number(blen >> BigInt(0)) & 0x7fff) | lt1;
|
||||
const u16_2 = (Number(blen >> BigInt(15)) & 0x7fff) | lt2;
|
||||
const u16_3 = (Number(blen >> BigInt(30)) & 0x7fff) | lt3;
|
||||
const u16_4 = Number(blen >> BigInt(45)) & 0xffff;
|
||||
const ld: number[] = [u16_1];
|
||||
if (lt1 != 0) ld.push(u16_2);
|
||||
if (lt2 != 0) ld.push(u16_3);
|
||||
if (lt3 != 0) ld.push(u16_4);
|
||||
const dv = new DataView(new ArrayBuffer(ld.length * 2))
|
||||
for(let i = 0; i < ld.length; i++) {
|
||||
dv.setUint16(i * 2, ld[i])
|
||||
}
|
||||
return dv.buffer;
|
||||
}
|
||||
|
||||
let nrOfFields = 0;
|
||||
if(data["limit"] !== null && data["limit"] !== undefined ) {
|
||||
nrOfFields++;
|
||||
const f = makeField(4, 0, 7);
|
||||
f.setUint32(0, data["limit"]);
|
||||
}
|
||||
|
||||
if(data["start"] !== null && data["start"] !== undefined ) {
|
||||
nrOfFields++;
|
||||
const str = new TextEncoder().encode(data["start"]);
|
||||
const lengthBytes = getAutoGrowLength(str.byteLength);
|
||||
const f = makeField(str.byteLength + lengthBytes.byteLength, 1, 21);
|
||||
new Uint8Array(f.buffer, f.byteOffset).set(new Uint8Array(lengthBytes), 0);
|
||||
new Uint8Array(f.buffer, f.byteOffset).set(str, lengthBytes.byteLength);
|
||||
}
|
||||
|
||||
|
||||
const fieldsLength = fields.reduce((a, b) => b.byteLength + a, 0);
|
||||
|
||||
|
||||
const result = new ArrayBuffer(fieldsLength + 2);
|
||||
const resultC = new Uint8Array(result);
|
||||
new DataView(result).setUint16(0, nrOfFields);
|
||||
|
||||
let offset = 2;
|
||||
for (const buf of fields) {
|
||||
resultC.set(new Uint8Array(buf), offset);
|
||||
offset += buf.byteLength;
|
||||
}
|
||||
|
||||
return new Uint8Array(result);
|
||||
}
|
||||
|
||||
export function deserialize_PullGetLogRequest (data: Uint8Array): PullGetLogRequest {
|
||||
const view = new DataView(data.buffer, data.byteOffset)
|
||||
let idx = 0;
|
||||
const readAutoGrowLength = () => {
|
||||
let result = BigInt(0);
|
||||
const v1 = view.getUint16(idx); idx += 2;
|
||||
const u16_1 = v1 & 0x7fff;
|
||||
result = result | (BigInt(u16_1) << BigInt(0));
|
||||
if ((v1 & 0x8000) > 0) {
|
||||
const v2 = view.getUint16(idx); idx += 2;
|
||||
const u16_2 = v2 & 0x7fff;
|
||||
result = result | (BigInt(u16_2) << BigInt(15));
|
||||
if ((v2 & 0x8000) > 0) {
|
||||
const v3 = view.getUint16(idx); idx += 2;
|
||||
const u16_3 = v3 & 0x7fff;
|
||||
result = result | (BigInt(u16_3) << BigInt(30));
|
||||
if ((v3 & 0x8000) > 0) {
|
||||
const v4 = view.getUint16(idx); idx += 2;
|
||||
const u16_4 = v4;
|
||||
result = result | (BigInt(u16_4) << BigInt(45));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Number(result);
|
||||
}
|
||||
let result: any = {}
|
||||
const nrOfFields = view.getUint16(idx); idx += 2;
|
||||
for(let i = 0; i < nrOfFields; i++) {
|
||||
const fieldLabel = view.getUint16(idx); idx += 2;
|
||||
const fieldType = view.getUint8(idx); idx += 1;
|
||||
switch(fieldLabel) {
|
||||
case 0: {
|
||||
result["limit"] = view.getUint32(idx); idx += 4;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
const fieldDataLength = readAutoGrowLength()
|
||||
const fieldDataUint8 = data.slice(idx, idx + fieldDataLength); idx += fieldDataLength;
|
||||
const str = new TextDecoder().decode(fieldDataUint8);
|
||||
result["start"] = str;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error("Invalid label found: " + fieldLabel)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user