Backend half

This commit is contained in:
2025-07-11 19:56:28 +02:00
parent fa868e7c1d
commit 8600fa7c1d
19426 changed files with 3750448 additions and 8108 deletions
@@ -0,0 +1,24 @@
import { Buffer } from "buffer";
export async function* getChunkStream(data, partSize, getNextData) {
let partNumber = 1;
const currentBuffer = { chunks: [], length: 0 };
for await (const datum of getNextData(data)) {
currentBuffer.chunks.push(datum);
currentBuffer.length += datum.byteLength;
while (currentBuffer.length > partSize) {
const dataChunk = currentBuffer.chunks.length > 1 ? Buffer.concat(currentBuffer.chunks) : currentBuffer.chunks[0];
yield {
partNumber,
data: dataChunk.subarray(0, partSize),
};
currentBuffer.chunks = [dataChunk.subarray(partSize)];
currentBuffer.length = currentBuffer.chunks[0].byteLength;
partNumber += 1;
}
}
yield {
partNumber,
data: currentBuffer.chunks.length !== 1 ? Buffer.concat(currentBuffer.chunks) : currentBuffer.chunks[0],
lastPart: true,
};
}
@@ -0,0 +1,19 @@
export async function* getChunkUint8Array(data, partSize) {
let partNumber = 1;
let startByte = 0;
let endByte = partSize;
while (endByte < data.byteLength) {
yield {
partNumber,
data: data.subarray(startByte, endByte),
};
partNumber += 1;
startByte = endByte;
endByte = startByte + partSize;
}
yield {
partNumber,
data: data.subarray(startByte),
lastPart: true,
};
}
@@ -0,0 +1,11 @@
import { Buffer } from "buffer";
export async function* getDataReadable(data) {
for await (const chunk of data) {
if (Buffer.isBuffer(chunk) || chunk instanceof Uint8Array) {
yield chunk;
}
else {
yield Buffer.from(chunk);
}
}
}
@@ -0,0 +1,24 @@
import { Buffer } from "buffer";
export async function* getDataReadableStream(data) {
const reader = data.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
return;
}
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
yield value;
}
else {
yield Buffer.from(value);
}
}
}
catch (e) {
throw e;
}
finally {
reader.releaseLock();
}
}