spice-js 2.7.11 → 2.7.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -196,7 +196,12 @@ class RestHelper {
196
196
  ctx.set("Content-Disposition", "attachment; filename=\"" + filename + "\"");
197
197
  ctx.type = "text/csv; charset=utf-8";
198
198
  ctx.status = 200;
199
- ctx.body = fs.createReadStream(filePath);
199
+ var csvStream = fs.createReadStream(filePath); // Delete file after stream is finished
200
+
201
+ csvStream.on('close', () => {
202
+ fs.promises.unlink(filePath).catch(() => {});
203
+ });
204
+ ctx.body = csvStream;
200
205
  return {
201
206
  v: void 0
202
207
  };
@@ -213,7 +218,12 @@ class RestHelper {
213
218
  ctx.set("Content-Disposition", "attachment; filename=\"" + filename + "\"");
214
219
  ctx.type = "application/json; charset=utf-8";
215
220
  ctx.status = 200;
216
- ctx.body = fs.createReadStream(filePath);
221
+ var jsonStream = fs.createReadStream(filePath); // Delete file after stream is finished
222
+
223
+ jsonStream.on('close', () => {
224
+ fs.promises.unlink(filePath).catch(() => {});
225
+ });
226
+ ctx.body = jsonStream;
217
227
  } catch (e) {
218
228
  console.error(e.stack);
219
229
  ctx.status = 400;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.7.11",
3
+ "version": "2.7.12",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -119,18 +119,22 @@ export default class RestHelper {
119
119
  a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" })
120
120
  );
121
121
 
122
- const csv = parse(flatRows, { fields, defaultValue: "", excelStrings: true });
123
-
122
+ const csv = parse(flatRows, { fields, defaultValue: "", excelStrings: true });
123
+
124
124
  makeDirectory(`./storage/exports/csv`);
125
125
  filename = `${RestHelper.makeid(9)}.csv`;
126
126
  filePath = path.resolve(`./storage/exports/csv/${filename}`);
127
127
  await fs.promises.writeFile(filePath, csv, "utf8");
128
-
129
-
128
+
130
129
  ctx.set("Content-Disposition", `attachment; filename="${filename}"`);
131
130
  ctx.type = "text/csv; charset=utf-8";
132
131
  ctx.status = 200;
133
- ctx.body = fs.createReadStream(filePath);
132
+ const csvStream = fs.createReadStream(filePath);
133
+ // Delete file after stream is finished
134
+ csvStream.on('close', () => {
135
+ fs.promises.unlink(filePath).catch(() => {});
136
+ });
137
+ ctx.body = csvStream;
134
138
  return;
135
139
  }
136
140
 
@@ -143,7 +147,12 @@ export default class RestHelper {
143
147
  ctx.set("Content-Disposition", `attachment; filename="${filename}"`);
144
148
  ctx.type = "application/json; charset=utf-8";
145
149
  ctx.status = 200;
146
- ctx.body = fs.createReadStream(filePath);
150
+ const jsonStream = fs.createReadStream(filePath);
151
+ // Delete file after stream is finished
152
+ jsonStream.on('close', () => {
153
+ fs.promises.unlink(filePath).catch(() => {});
154
+ });
155
+ ctx.body = jsonStream;
147
156
 
148
157
  } catch (e) {
149
158
  console.error(e.stack);