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
This commit is contained in:
parent
ea2172e754
commit
0a621ba73a
@ -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,12 +145,8 @@ 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 => {
|
||||||
response.writeHead(404);
|
|
||||||
response.end(`File ${request.url} not found!`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!request.headers.range) {
|
if (!request.headers.range) {
|
||||||
const contentLength = stat.size;
|
const contentLength = stat.size;
|
||||||
const stream = fs.createReadStream(filePath);
|
const stream = fs.createReadStream(filePath);
|
||||||
@ -178,7 +167,12 @@ function createTemporaryNodeServer() {
|
|||||||
});
|
});
|
||||||
stream.pipe(response);
|
stream.pipe(response);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
error => {
|
||||||
|
response.writeHead(404);
|
||||||
|
response.end(`File ${request.url} not found!`);
|
||||||
|
}
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.listen(0); /* Listen on a random free port */
|
.listen(0); /* Listen on a random free port */
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user