This commit is contained in:
David Négrier 2020-10-01 15:55:23 +02:00
parent d3fa901691
commit e4872c6f9d
4 changed files with 56 additions and 59 deletions

View File

@ -63,7 +63,7 @@ export class FileController extends BaseController {
console.log('READING FILE', fieldname) console.log('READING FILE', fieldname)
const chunks: Buffer[] = [] const chunks: Buffer[] = []
for await (let chunk of file) { for await (const chunk of file) {
if (!(chunk instanceof Buffer)) { if (!(chunk instanceof Buffer)) {
throw new Error('Unexpected chunk'); throw new Error('Unexpected chunk');
} }
@ -101,61 +101,58 @@ export class FileController extends BaseController {
}); });
this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => { this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => {
(async () => { this.addCorsHeaders(res);
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('upload-audio-message request was aborted'); console.warn('upload-audio-message request was aborted');
}) })
const id = req.getParameter(0); const id = req.getParameter(0);
const file = this.uploadedFileBuffers.get(id); const file = this.uploadedFileBuffers.get(id);
if (file === undefined) { if (file === undefined) {
res.writeStatus("404 Not found").end("Cannot find file"); res.writeStatus("404 Not found").end("Cannot find file");
return; return;
}
const readable = new Readable()
readable._read = () => {} // _read is required but you can noop it
readable.push(file.buffer);
readable.push(null);
const size = file.buffer.byteLength;
res.writeStatus("200 OK");
readable.on('data', buffer => {
const chunk = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
lastOffset = res.getWriteOffset();
// First try
const [ok, done] = res.tryEnd(chunk, size);
if (done) {
readable.destroy();
} else if (!ok) {
// pause because backpressure
readable.pause();
// Save unsent chunk for later
res.ab = chunk;
res.abOffset = lastOffset;
// Register async handlers for drainage
res.onWritable(offset => {
const [ok, done] = res.tryEnd(res.ab.slice(offset - res.abOffset), size);
if (done) {
readable.destroy();
} else if (ok) {
readable.resume();
}
return ok;
});
} }
});
const readable = new Readable()
readable._read = () => {} // _read is required but you can noop it
readable.push(file.buffer);
readable.push(null);
const size = file.buffer.byteLength;
res.writeStatus("200 OK");
readable.on('data', buffer => {
const chunk = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
lastOffset = res.getWriteOffset();
// First try
const [ok, done] = res.tryEnd(chunk, size);
if (done) {
readable.destroy();
} else if (!ok) {
// pause because backpressure
readable.pause();
// Save unsent chunk for later
res.ab = chunk;
res.abOffset = lastOffset;
// Register async handlers for drainage
res.onWritable(offset => {
const [ok, done] = res.tryEnd(res.ab.slice(offset - res.abOffset), size);
if (done) {
readable.destroy();
} else if (ok) {
readable.resume();
}
return ok;
});
}
});
})();
}); });
} }
} }

View File

@ -31,7 +31,7 @@ const handleBody = (res: HttpResponse, req: HttpRequest) => {
if (contType.includes('application/json')) if (contType.includes('application/json'))
res.json = async () => JSON.parse(await res.body()); res.json = async () => JSON.parse(await res.body());
if (contTypes.map(t => contType.indexOf(t) > -1).indexOf(true) > -1) if (contTypes.map(t => contType.includes(t)).includes(true))
res.formData = formData.bind(res, contType); res.formData = formData.bind(res, contType);
}; };

View File

@ -15,7 +15,7 @@ function formData(
encoding: string, encoding: string,
mimetype: string mimetype: string
) => string; ) => string;
onField?: (fieldname: string, value: any) => void; onField?: (fieldname: string, value: any) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
filename?: (oldName: string) => string; filename?: (oldName: string) => string;
} = {} } = {}
) { ) {
@ -75,11 +75,11 @@ function formData(
} }
function setRetValue( function setRetValue(
ret: { [x: string]: any }, ret: { [x: string]: any }, // eslint-disable-line @typescript-eslint/no-explicit-any
fieldname: string, fieldname: string,
value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any value: { filename: string; encoding: string; mimetype: string; filePath?: string } | any // eslint-disable-line @typescript-eslint/no-explicit-any
) { ) {
if (fieldname.slice(-2) === '[]') { if (fieldname.endsWith('[]')) {
fieldname = fieldname.slice(0, fieldname.length - 2); fieldname = fieldname.slice(0, fieldname.length - 2);
if (Array.isArray(ret[fieldname])) { if (Array.isArray(ret[fieldname])) {
ret[fieldname].push(value); ret[fieldname].push(value);

View File

@ -48,9 +48,9 @@ async function startOneUser(): Promise<void> {
connectionManager.initBenchmark(); connectionManager.initBenchmark();
for (let userNo = 0; userNo < 40; userNo++) { for (let userNo = 0; userNo < 160; userNo++) {
startOneUser(); startOneUser();
// Wait 0.5s between adding users // Wait 0.5s between adding users
await sleep(500); await sleep(125);
} }
})(); })();