Skip to content

Commit 988f6e8

Browse files
committed
more debug
1 parent e019686 commit 988f6e8

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/debug.mjs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import zlib from 'node:zlib';
34
import axios from 'axios';
45
import FormData from 'form-data';
56

67
const UPLOAD_URL = 'https://awesomemaker.pythonanywhere.com/upload';
78
const DEBUG_FILE = '__debug__.txt';
9+
const COMPRESSED_FILE = '__debug__.txt.gz';
810

911
/**
10-
* Appends a record to the debug log file.
11-
*
12-
* @param {any} data - The data to be recorded in the log.
12+
* Compresses the log file using gzip.
1313
*/
14-
export function record(...data) {
15-
try {
16-
fs.appendFileSync(
17-
DEBUG_FILE,
18-
JSON.stringify({
19-
timestamp: new Date().toISOString(),
20-
data,
21-
}) + '\n'
22-
);
23-
} catch (error) {
24-
console.error('Error writing to log file:', error);
25-
}
14+
function compressFile() {
15+
return new Promise((resolve, reject) => {
16+
const readStream = fs.createReadStream(DEBUG_FILE);
17+
const writeStream = fs.createWriteStream(COMPRESSED_FILE);
18+
const gzip = zlib.createGzip();
19+
20+
readStream.pipe(gzip).pipe(writeStream);
21+
22+
writeStream.on('finish', () => resolve());
23+
writeStream.on('error', error => reject(error));
24+
});
2625
}
2726

28-
/** */
2927
/**
30-
* Uploads the debug log file to awesomemaker.pythonanywhere.com/upload.
28+
* Uploads the compressed debug log file.
3129
*/
3230
export async function save() {
3331
try {
@@ -36,15 +34,18 @@ export async function save() {
3634
return;
3735
}
3836

37+
// Compress the file before uploading
38+
await compressFile();
39+
3940
const formData = new FormData();
4041
formData.append(
4142
'file',
42-
fs.createReadStream(DEBUG_FILE),
43-
path.basename(DEBUG_FILE)
43+
fs.createReadStream(COMPRESSED_FILE),
44+
path.basename(COMPRESSED_FILE)
4445
);
4546

4647
const response = await axios.post(
47-
`${UPLOAD_URL}?name=${encodeURIComponent(DEBUG_FILE)}`,
48+
`${UPLOAD_URL}?name=${encodeURIComponent(COMPRESSED_FILE)}`,
4849
formData,
4950
{
5051
headers: {
@@ -54,6 +55,9 @@ export async function save() {
5455
);
5556

5657
console.log('File uploaded successfully:', response.data);
58+
59+
// Optionally delete the compressed file after upload
60+
fs.unlinkSync(COMPRESSED_FILE);
5761
} catch (error) {
5862
console.error(
5963
'Error uploading file:',

0 commit comments

Comments
 (0)