Skip to content

Commit beaf94e

Browse files
committed
avoid file system writes and return image data directly
This should speed up the rendering process, resulting in lower latency for image generation requests.
1 parent e9ac3ad commit beaf94e

File tree

5 files changed

+14
-33
lines changed

5 files changed

+14
-33
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Since [Plotly.js](https://plotly.com/javascript/) is the main dependency of this
1010
application, major version changes in Plotly.js will also trigger a major
1111
version change in this application.
1212

13+
## Version 5.9.0-pre (2024-11-25)
14+
15+
* __[improvement]__
16+
The image rendering process is simplified and does now use less I/O operations.
17+
1318
## Version 5.8.16 (2024-11-22)
1419

1520
* __[maintenance]__

export-server/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

export-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "plotly-node-export-server",
3-
"version": "5.8.16",
3+
"version": "5.9.0-pre",
44
"description": "Plotly.js Node.js export server",
55
"repository": {
66
"url": "https://gitlab.com/striezel/plotly-node-export-server.git",

export-server/server.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Plotly.js offline image export server with Node.js
3-
Copyright (C) 2018, 2020, 2021, 2022, 2023 Dirk Stolle
3+
Copyright (C) 2018, 2020, 2021, 2022, 2023, 2024 Dirk Stolle
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -124,27 +124,8 @@ const server = http.createServer(function(req, res) {
124124
const result = await ssr.render(body, filename, req.headers["x-image-width"], req.headers["x-image-height"]);
125125
if (result.success) {
126126
res.statusCode = 200; // 200 == OK
127-
var stream = fs.createReadStream(result.filename);
128-
stream.on('open', function() {
129-
res.setHeader('Content-Type', 'image/svg+xml');
130-
stream.pipe(res);
131-
});
132-
stream.on('close', function() {
133-
res.end();
134-
fs.unlink(result.filename, function(err) {
135-
if (err) {
136-
console.error('Failed to unlink file ' + result.filename + '! '
137-
+ err.toString());
138-
}
139-
});
140-
});
141-
stream.on('error', function(err) {
142-
res.statusCode = 500; // 500 == Internal Server Error
143-
res.setHeader('Content-Type', 'text/plain');
144-
res.end('Failed to serve file due to I/O error.\n');
145-
console.error('Failed to serve file ' + result.filename + ': '
146-
+ err.toString());
147-
});
127+
res.setHeader('Content-Type', 'image/svg+xml');
128+
res.end(result.data);
148129
} else {
149130
res.statusCode = 500; // 500 == Internal Server Error
150131
res.setHeader('Content-Type', 'application/json');

export-server/ssr.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ const { v4: uuidv4 } = require('uuid');
3131
Returns:
3232
object that contains two members:
3333
success - (boolean) indicates success of the rendering process
34-
filename - (string) filename of the output, only present after successful
35-
rendering
34+
data - (string) SVG data, only present after successful rendering
3635
failure - (string) reason for render failure; only present after failed
3736
rendering, may be cryptic and is not necessarily human-friendly
3837
*/
@@ -93,12 +92,8 @@ exports.render = async function(jsonData, filename, width, height) {
9392
.then(win.eval)
9493
.then(() => win.Plotly.toImage({data: array_data, layout: layout, config: config},
9594
{ format: 'svg', imageDataOnly: true }))
96-
.then(function(data) {
97-
fs.promises.writeFile(filename, data);
98-
})
99-
.then(() => {
100-
result = { success: true, filename: filename };
101-
return result;
95+
.then(function(img_data) {
96+
return { success: true, data: img_data };
10297
})
10398
.catch(function(error) {
10499
console.log("Error occurred: " + error);

0 commit comments

Comments
 (0)