11import fs from 'node:fs' ;
22import path from 'node:path' ;
3+ import zlib from 'node:zlib' ;
34import axios from 'axios' ;
45import FormData from 'form-data' ;
56
67const UPLOAD_URL = 'https://awesomemaker.pythonanywhere.com/upload' ;
78const 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 */
3230export 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