Merge branch 'main' into dev-tank-search

This commit is contained in:
TrèsAbhi committed 2024-06-23 20:11:23 -05:00
commit 1d65dc755f
19 files changed
+110 -6453

No files matched your search

+17 -8
View File
@@ -1,9 +1,8 @@
import { crc32 } from 'crc';
import { decodeBlock } from 'lz4';
import { decompressBlock } from 'lz4js';
/**
* Thanks Maddoxkkm! Modernized heavily.
*
* Thanks Maddoxkkm! Modified heavily to be modern.
* https://github.com/Maddoxkkm/dvpl_converter/
*/
export function readDVPL(buffer: Buffer) {
@@ -30,6 +29,7 @@ export function readDVPL(buffer: Buffer) {
if (crc32(targetBlock) !== footerData.crc32) {
throw new TypeError('DVPL CRC32 mismatch');
}
if (footerData.type === 0) {
if (!(footerData.oSize === footerData.cSize && footerData.type === 0)) {
throw new RangeError('DVPL type and size mismatch');
@@ -37,14 +37,23 @@ export function readDVPL(buffer: Buffer) {
return targetBlock;
} else if (footerData.type === 1 || footerData.type === 2) {
const deDVPLBlock = Buffer.alloc(footerData.oSize);
const DecompressedBlockSize = decodeBlock(targetBlock, deDVPLBlock);
const source = new Uint8Array(targetBlock);
const destination = new Uint8Array(footerData.oSize);
const decompressedSize = decompressBlock(
source,
destination,
0,
source.length,
0,
);
if (DecompressedBlockSize !== footerData.oSize) {
throw new RangeError('Decompressed DVPL size mismatch');
if (decompressedSize !== footerData.oSize) {
throw new RangeError(
`Decompressed DVPL size mismatch (${decompressedSize} vs ${footerData.oSize})`,
);
}
return deDVPLBlock;
return Buffer.from(destination);
} else {
throw new SyntaxError('Unknown DVPL format');
}
+11 -3
View File
@@ -1,5 +1,5 @@
import { crc32 } from 'crc';
import { encodeBlockHC } from 'lz4';
import { compressBlock } from 'lz4js';
/**
* Thanks Maddoxkkm! Modernized heavily.
@@ -7,8 +7,16 @@ import { encodeBlockHC } from 'lz4';
* https://github.com/Maddoxkkm/dvpl_converter/
*/
export function writeDVPL(buffer: Buffer) {
let output = Buffer.alloc(buffer.length);
const compressedBlockSize = encodeBlockHC(buffer, output);
const source = new Uint8Array(buffer);
const destination = new Uint8Array(buffer.length);
const compressedBlockSize = compressBlock(
source,
destination,
0,
source.length,
0,
);
let output = Buffer.from(destination);
if (compressedBlockSize === 0 || compressedBlockSize >= buffer.length) {
const footerBuffer = toDVPLFooter(
+23
View File
@@ -0,0 +1,23 @@
export async function patientFetchJSON<Type extends object>(
url: string,
timeout = 1000,
options?: Partial<{ cache: 'no-store' }>,
) {
let patientJSON: Type;
while (patientJSON! === undefined) {
try {
patientJSON = (await fetch(url, options).then((response) =>
response.json(),
)) as Type;
} catch (error) {
console.warn(
`Failed to fetch ${url} as JSON; retrying in ${timeout}ms...`,
error,
);
await new Promise((resolve) => setTimeout(resolve, timeout));
}
}
return patientJSON;
}