mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
chore(server): improve stream read (#10960)
This commit is contained in:
@@ -2,7 +2,7 @@ import { Readable } from 'node:stream';
|
||||
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { BlobQuotaExceeded } from '../../../base';
|
||||
import { readBufferWithLimit } from '../../../base';
|
||||
import { MAX_EMBEDDABLE_SIZE } from './types';
|
||||
|
||||
export class GqlSignal implements AsyncDisposable {
|
||||
@@ -31,27 +31,6 @@ export async function checkEmbeddingAvailable(
|
||||
export function readStream(
|
||||
readable: Readable,
|
||||
maxSize = MAX_EMBEDDABLE_SIZE
|
||||
): Promise<Buffer<ArrayBuffer>> {
|
||||
return new Promise<Buffer<ArrayBuffer>>((resolve, reject) => {
|
||||
const chunks: Uint8Array[] = [];
|
||||
let totalSize = 0;
|
||||
|
||||
readable.on('data', chunk => {
|
||||
totalSize += chunk.length;
|
||||
if (totalSize > maxSize) {
|
||||
reject(new BlobQuotaExceeded());
|
||||
readable.destroy(new BlobQuotaExceeded());
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
|
||||
readable.on('end', () => {
|
||||
resolve(Buffer.concat(chunks, totalSize));
|
||||
});
|
||||
|
||||
readable.on('error', err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
): Promise<Buffer> {
|
||||
return readBufferWithLimit(readable, maxSize);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
CallMetric,
|
||||
Config,
|
||||
type FileUpload,
|
||||
readBuffer,
|
||||
type StorageProvider,
|
||||
StorageProviderFactory,
|
||||
URLHelper,
|
||||
@@ -63,29 +64,7 @@ export class CopilotStorage {
|
||||
throw new BlobQuotaExceeded();
|
||||
}
|
||||
|
||||
const buffer = await new Promise<Buffer>((resolve, reject) => {
|
||||
const stream = blob.createReadStream();
|
||||
const chunks: Uint8Array[] = [];
|
||||
stream.on('data', chunk => {
|
||||
chunks.push(chunk);
|
||||
|
||||
// check size after receive each chunk to avoid unnecessary memory usage
|
||||
const bufferSize = chunks.reduce((acc, cur) => acc + cur.length, 0);
|
||||
if (checkExceeded(bufferSize)) {
|
||||
reject(new BlobQuotaExceeded());
|
||||
}
|
||||
});
|
||||
stream.on('error', reject);
|
||||
stream.on('end', () => {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
|
||||
if (checkExceeded(buffer.length)) {
|
||||
reject(new BlobQuotaExceeded());
|
||||
} else {
|
||||
resolve(buffer);
|
||||
}
|
||||
});
|
||||
});
|
||||
const buffer = await readBuffer(blob.createReadStream(), checkExceeded);
|
||||
|
||||
return {
|
||||
buffer,
|
||||
|
||||
Reference in New Issue
Block a user