From 0a621ba73a974d1052fc9a6f796632be3c9b975e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 22 Sep 2024 12:57:23 +0200 Subject: [PATCH 1/2] Use `fs/promises` in the Node.js unit-tests (PR 17714 follow-up) This is available in all Node.js versions that we currently support, and using it allows us to remove callback-functions; please see https://nodejs.org/docs/latest-v18.x/api/fs.html#promises-api --- test/unit/test_utils.js | 60 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index bc1d839d5eadd..507fc15649f48 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -45,15 +45,8 @@ class DOMFileReaderFactory { class NodeFileReaderFactory { static async fetch(params) { - return new Promise((resolve, reject) => { - fs.readFile(params.path, (error, data) => { - if (error || !data) { - reject(error || new Error(`Empty file for: ${params.path}`)); - return; - } - resolve(new Uint8Array(data)); - }); - }); + const data = await fs.promises.readFile(params.path); + return new Uint8Array(data); } } @@ -152,33 +145,34 @@ function createTemporaryNodeServer() { const server = http .createServer((request, response) => { const filePath = process.cwd() + "/test/pdfs" + request.url; - fs.lstat(filePath, (error, stat) => { - if (error) { + fs.promises.lstat(filePath).then( + stat => { + if (!request.headers.range) { + const contentLength = stat.size; + const stream = fs.createReadStream(filePath); + response.writeHead(200, { + "Content-Type": "application/pdf", + "Content-Length": contentLength, + "Accept-Ranges": "bytes", + }); + stream.pipe(response); + } else { + const [start, end] = request.headers.range + .split("=")[1] + .split("-") + .map(x => Number(x)); + const stream = fs.createReadStream(filePath, { start, end }); + response.writeHead(206, { + "Content-Type": "application/pdf", + }); + stream.pipe(response); + } + }, + error => { response.writeHead(404); response.end(`File ${request.url} not found!`); - return; - } - if (!request.headers.range) { - const contentLength = stat.size; - const stream = fs.createReadStream(filePath); - response.writeHead(200, { - "Content-Type": "application/pdf", - "Content-Length": contentLength, - "Accept-Ranges": "bytes", - }); - stream.pipe(response); - } else { - const [start, end] = request.headers.range - .split("=")[1] - .split("-") - .map(x => Number(x)); - const stream = fs.createReadStream(filePath, { start, end }); - response.writeHead(206, { - "Content-Type": "application/pdf", - }); - stream.pipe(response); } - }); + ); }) .listen(0); /* Listen on a random free port */ From 1a1dfe60bfe0f029fa90a5b5c11f3a29d391e3b6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 22 Sep 2024 13:00:57 +0200 Subject: [PATCH 2/2] Add more optional chaining in the `test/` directory --- test/driver.js | 2 +- test/test.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/driver.js b/test/driver.js index c79c30601380e..8ba30b0bfd3ce 100644 --- a/test/driver.js +++ b/test/driver.js @@ -782,7 +782,7 @@ class Driver { } } - if (task.skipPages && task.skipPages.includes(task.pageNum)) { + if (task.skipPages?.includes(task.pageNum)) { this._log( " Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n" ); diff --git a/test/test.mjs b/test/test.mjs index c6d3ccf44308e..4d16c8ea1e656 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -161,7 +161,7 @@ function parseOptions() { ); }) .check(argv => { - if (argv.testfilter && argv.testfilter.length > 0 && argv.xfaOnly) { + if (argv.testfilter?.length > 0 && argv.xfaOnly) { throw new Error("--testfilter and --xfaOnly cannot be used together."); } return true;