Skip to content

Commit b6889b6

Browse files
authored
Merge pull request #24 from appwrite/fix-gzip
chore: bump version to 0.5.1 and refactor filesystem archive handling
2 parents 7014fa3 + 8c9b88d commit b6889b6

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@appwrite.io/synapse",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "Operating system gateway for remote serverless environments",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/services/filesystem.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -670,17 +670,7 @@ export class Filesystem {
670670
gzipOptions: { level: 9 },
671671
});
672672

673-
// Use a more direct approach with streams
674-
const bufferChunks: Buffer[] = [];
675-
676-
// Set up promise to track completion
677-
const archivePromise = new Promise<Buffer>((resolve, reject) => {
678-
archive.on("data", (chunk: Buffer) => bufferChunks.push(chunk));
679-
archive.on("end", () => resolve(Buffer.concat(bufferChunks)));
680-
archive.on("error", (err: Error) => reject(err));
681-
});
682-
683-
// Recursively add files to archive
673+
// Recursively add files to archive first
684674
const addDirectory = async (dir: string) => {
685675
const entries = await fs.readdir(dir, { withFileTypes: true });
686676

@@ -715,25 +705,45 @@ export class Filesystem {
715705
// Process files
716706
await addDirectory(this.workDir);
717707

718-
// Finalize archive
719-
archive.finalize();
720-
708+
// Handle saving to file and getting buffer
721709
if (saveAs) {
710+
// Save to file
722711
const fullSavePath = this.resolvePath(saveAs);
723712
const writeStream = fsSync.createWriteStream(fullSavePath);
713+
724714
archive.pipe(writeStream);
715+
archive.finalize();
716+
725717
await new Promise<void>((resolve, reject) => {
726718
writeStream.on("finish", () => resolve());
727719
writeStream.on("error", (error) => reject(error));
720+
archive.on("error", (error) => reject(error));
728721
});
729-
}
730722

731-
// Wait for archive to complete and return result
732-
const buffer = await archivePromise;
733-
return {
734-
success: true,
735-
data: { buffer },
736-
};
723+
// Read the file back as buffer
724+
const buffer = await fs.readFile(fullSavePath);
725+
return {
726+
success: true,
727+
data: { buffer },
728+
};
729+
} else {
730+
// Just get buffer without saving to file
731+
const bufferChunks: Buffer[] = [];
732+
733+
const archivePromise = new Promise<Buffer>((resolve, reject) => {
734+
archive.on("data", (chunk: Buffer) => bufferChunks.push(chunk));
735+
archive.on("end", () => resolve(Buffer.concat(bufferChunks)));
736+
archive.on("error", (err: Error) => reject(err));
737+
});
738+
739+
archive.finalize();
740+
741+
const buffer = await archivePromise;
742+
return {
743+
success: true,
744+
data: { buffer },
745+
};
746+
}
737747
} catch (error) {
738748
return {
739749
success: false,

tests/services/filesystem.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ describe("Filesystem", () => {
325325
};
326326
(fsSync.createWriteStream as jest.Mock).mockReturnValue(mockWriteStream);
327327

328+
// Mock the readFile to return a Buffer
329+
(fsp.readFile as jest.Mock).mockResolvedValue(
330+
Buffer.from("test content"),
331+
);
332+
328333
const result = await filesystem.createGzipFile("test.tar.gz");
329334

330335
expect(result.success).toBe(true);

0 commit comments

Comments
 (0)