From e4872c6f9d29df5294b3ee1d1cbdec4e73c216ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 1 Oct 2020 15:55:23 +0200 Subject: [PATCH] Fix CI --- back/src/Controller/FileController.ts | 101 +++++++++++++------------- back/src/Server/server/baseapp.ts | 2 +- back/src/Server/server/formdata.ts | 8 +- benchmark/index.ts | 4 +- 4 files changed, 56 insertions(+), 59 deletions(-) diff --git a/back/src/Controller/FileController.ts b/back/src/Controller/FileController.ts index fa0c5f24..889cfa61 100644 --- a/back/src/Controller/FileController.ts +++ b/back/src/Controller/FileController.ts @@ -63,7 +63,7 @@ export class FileController extends BaseController { console.log('READING FILE', fieldname) const chunks: Buffer[] = [] - for await (let chunk of file) { + for await (const chunk of file) { if (!(chunk instanceof Buffer)) { 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) => { - (async () => { - this.addCorsHeaders(res); + this.addCorsHeaders(res); - res.onAborted(() => { - console.warn('upload-audio-message request was aborted'); - }) + res.onAborted(() => { + console.warn('upload-audio-message request was aborted'); + }) - const id = req.getParameter(0); + const id = req.getParameter(0); - const file = this.uploadedFileBuffers.get(id); - if (file === undefined) { - res.writeStatus("404 Not found").end("Cannot find file"); - return; + const file = this.uploadedFileBuffers.get(id); + if (file === undefined) { + res.writeStatus("404 Not found").end("Cannot find file"); + 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; - }); - } - }); - - })(); + }); }); } } diff --git a/back/src/Server/server/baseapp.ts b/back/src/Server/server/baseapp.ts index 9831c860..accd8a99 100644 --- a/back/src/Server/server/baseapp.ts +++ b/back/src/Server/server/baseapp.ts @@ -31,7 +31,7 @@ const handleBody = (res: HttpResponse, req: HttpRequest) => { if (contType.includes('application/json')) 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); }; diff --git a/back/src/Server/server/formdata.ts b/back/src/Server/server/formdata.ts index 7af70dda..9dd08440 100644 --- a/back/src/Server/server/formdata.ts +++ b/back/src/Server/server/formdata.ts @@ -15,7 +15,7 @@ function formData( encoding: string, mimetype: 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; } = {} ) { @@ -75,11 +75,11 @@ function formData( } function setRetValue( - ret: { [x: string]: any }, + ret: { [x: string]: any }, // eslint-disable-line @typescript-eslint/no-explicit-any 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); if (Array.isArray(ret[fieldname])) { ret[fieldname].push(value); diff --git a/benchmark/index.ts b/benchmark/index.ts index 420223cc..c4bda8fc 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -48,9 +48,9 @@ async function startOneUser(): Promise { connectionManager.initBenchmark(); - for (let userNo = 0; userNo < 40; userNo++) { + for (let userNo = 0; userNo < 160; userNo++) { startOneUser(); // Wait 0.5s between adding users - await sleep(500); + await sleep(125); } })();