Backend half
This commit is contained in:
Generated
Vendored
+24
@@ -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,
|
||||
};
|
||||
}
|
||||
Generated
Vendored
+19
@@ -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,
|
||||
};
|
||||
}
|
||||
Generated
Vendored
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+24
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user