Merge pull request #18772 from Snuffleupagus/Node-unit-test-fs-promise

Use `fs/promises` in the Node.js unit-tests (PR 17714 follow-up)
This commit is contained in:
Tim van der Meij 2024-09-22 20:21:37 +02:00 committed by GitHub
commit ddd7b63406
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 35 deletions

View File

@ -782,7 +782,7 @@ class Driver {
} }
} }
if (task.skipPages && task.skipPages.includes(task.pageNum)) { if (task.skipPages?.includes(task.pageNum)) {
this._log( this._log(
" Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n" " Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n"
); );

View File

@ -161,7 +161,7 @@ function parseOptions() {
); );
}) })
.check(argv => { .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."); throw new Error("--testfilter and --xfaOnly cannot be used together.");
} }
return true; return true;

View File

@ -45,15 +45,8 @@ class DOMFileReaderFactory {
class NodeFileReaderFactory { class NodeFileReaderFactory {
static async fetch(params) { static async fetch(params) {
return new Promise((resolve, reject) => { const data = await fs.promises.readFile(params.path);
fs.readFile(params.path, (error, data) => { return new Uint8Array(data);
if (error || !data) {
reject(error || new Error(`Empty file for: ${params.path}`));
return;
}
resolve(new Uint8Array(data));
});
});
} }
} }
@ -152,33 +145,34 @@ function createTemporaryNodeServer() {
const server = http const server = http
.createServer((request, response) => { .createServer((request, response) => {
const filePath = process.cwd() + "/test/pdfs" + request.url; const filePath = process.cwd() + "/test/pdfs" + request.url;
fs.lstat(filePath, (error, stat) => { fs.promises.lstat(filePath).then(
if (error) { 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.writeHead(404);
response.end(`File ${request.url} not found!`); 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 */ .listen(0); /* Listen on a random free port */