|
| 1 | +const { |
| 2 | + getFileStream, getFileStreamAndMetadata, |
| 3 | +} = require("../dist"); |
| 4 | +const fs = require("fs"); |
| 5 | +const path = require("path"); |
| 6 | +const http = require("http"); |
| 7 | +const os = require("os"); |
| 8 | + |
| 9 | +// Helper function to read content from a readable stream |
| 10 | +async function readStreamContent(stream) { |
| 11 | + let content = ""; |
| 12 | + stream.on("data", (chunk) => { |
| 13 | + content += chunk.toString(); |
| 14 | + }); |
| 15 | + |
| 16 | + await new Promise((resolve) => { |
| 17 | + stream.on("end", resolve); |
| 18 | + }); |
| 19 | + |
| 20 | + return content; |
| 21 | +} |
| 22 | + |
| 23 | +// Helper function to wait for stream cleanup by listening to close event |
| 24 | +async function waitForStreamCleanup(stream) { |
| 25 | + return new Promise((resolve) => { |
| 26 | + stream.on("close", resolve); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +describe("file-stream", () => { |
| 31 | + let testFilePath; |
| 32 | + let server; |
| 33 | + const testPort = 3892; |
| 34 | + |
| 35 | + beforeAll(() => { |
| 36 | + // Create a test file |
| 37 | + testFilePath = path.join(__dirname, "test-file.txt"); |
| 38 | + fs.writeFileSync(testFilePath, "test content for file stream"); |
| 39 | + |
| 40 | + // Create a simple HTTP server for testing remote files |
| 41 | + server = http.createServer((req, res) => { |
| 42 | + if (req.url === "/test-file.txt") { |
| 43 | + res.writeHead(200, { |
| 44 | + "Content-Type": "text/plain", |
| 45 | + "Content-Length": "28", |
| 46 | + "Last-Modified": new Date().toUTCString(), |
| 47 | + "ETag": "\"test-etag\"", |
| 48 | + }); |
| 49 | + res.end("test content for file stream"); |
| 50 | + } else if (req.url === "/no-content-length") { |
| 51 | + res.writeHead(200, { |
| 52 | + "Content-Type": "application/json", |
| 53 | + "Last-Modified": new Date().toUTCString(), |
| 54 | + }); |
| 55 | + res.end("{\"test\": \"data\"}"); |
| 56 | + } else if (req.url === "/error") { |
| 57 | + res.writeHead(404, "Not Found"); |
| 58 | + res.end(); |
| 59 | + } else { |
| 60 | + res.writeHead(404, "Not Found"); |
| 61 | + res.end(); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + return new Promise((resolve) => |
| 66 | + server.listen(testPort, resolve)); |
| 67 | + }); |
| 68 | + |
| 69 | + afterAll(() => { |
| 70 | + // Clean up test file |
| 71 | + if (fs.existsSync(testFilePath)) { |
| 72 | + fs.unlinkSync(testFilePath); |
| 73 | + } |
| 74 | + |
| 75 | + if (server) { |
| 76 | + return new Promise((resolve) => |
| 77 | + server.close(resolve)); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + describe("getFileStream", () => { |
| 82 | + it("should return readable stream for local file", async () => { |
| 83 | + const stream = await getFileStream(testFilePath); |
| 84 | + expect(stream).toBeDefined(); |
| 85 | + expect(typeof stream.read).toBe("function"); |
| 86 | + |
| 87 | + const content = await readStreamContent(stream); |
| 88 | + expect(content).toBe("test content for file stream"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should return readable stream for remote URL", async () => { |
| 92 | + const stream = await getFileStream(`http://localhost:${testPort}/test-file.txt`); |
| 93 | + expect(stream).toBeDefined(); |
| 94 | + expect(typeof stream.read).toBe("function"); |
| 95 | + |
| 96 | + const content = await readStreamContent(stream); |
| 97 | + expect(content).toBe("test content for file stream"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should throw error for invalid URL", async () => { |
| 101 | + await expect(getFileStream(`http://localhost:${testPort}/error`)) |
| 102 | + .rejects.toThrow("Failed to fetch"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should throw error for non-existent local file", async () => { |
| 106 | + await expect(getFileStream("/non/existent/file.txt")) |
| 107 | + .rejects.toThrow(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("getFileStreamAndMetadata", () => { |
| 112 | + it("should return stream and metadata for local file", async () => { |
| 113 | + const result = await getFileStreamAndMetadata(testFilePath); |
| 114 | + |
| 115 | + expect(result.stream).toBeDefined(); |
| 116 | + expect(typeof result.stream.read).toBe("function"); |
| 117 | + expect(result.metadata).toMatchObject({ |
| 118 | + size: 28, |
| 119 | + name: "test-file.txt", |
| 120 | + }); |
| 121 | + expect(result.metadata.lastModified.constructor.name).toBe("Date"); |
| 122 | + const content = await readStreamContent(result.stream); |
| 123 | + expect(content).toBe("test content for file stream"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should return stream and metadata for remote file with content-length", async () => { |
| 127 | + const result = await getFileStreamAndMetadata(`http://localhost:${testPort}/test-file.txt`); |
| 128 | + |
| 129 | + expect(result.stream).toBeDefined(); |
| 130 | + expect(typeof result.stream.read).toBe("function"); |
| 131 | + expect(result.metadata).toMatchObject({ |
| 132 | + size: 28, |
| 133 | + contentType: "text/plain", |
| 134 | + name: "test-file.txt", |
| 135 | + etag: "\"test-etag\"", |
| 136 | + }); |
| 137 | + expect(result.metadata.lastModified).toBeInstanceOf(Date); |
| 138 | + const content = await readStreamContent(result.stream); |
| 139 | + expect(content).toBe("test content for file stream"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should handle remote file without content-length", async () => { |
| 143 | + const result = await getFileStreamAndMetadata(`http://localhost:${testPort}/no-content-length`); |
| 144 | + |
| 145 | + expect(result.stream).toBeDefined(); |
| 146 | + expect(typeof result.stream.read).toBe("function"); |
| 147 | + |
| 148 | + expect(result.metadata).toMatchObject({ |
| 149 | + size: 16, // Size determined after download |
| 150 | + contentType: "application/json", |
| 151 | + }); |
| 152 | + expect(result.metadata.lastModified).toBeInstanceOf(Date); |
| 153 | + |
| 154 | + const content = await readStreamContent(result.stream); |
| 155 | + expect(content).toBe("{\"test\": \"data\"}"); |
| 156 | + }); |
| 157 | + |
| 158 | + it("should throw error for invalid remote URL", async () => { |
| 159 | + await expect(getFileStreamAndMetadata(`http://localhost:${testPort}/error`)) |
| 160 | + .rejects.toThrow("Failed to fetch"); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe("temporary file cleanup", () => { |
| 165 | + it("should clean up temporary files after stream ends", async () => { |
| 166 | + const tmpDir = os.tmpdir(); |
| 167 | + const tempFilesBefore = fs.readdirSync(tmpDir); |
| 168 | + const result = await getFileStreamAndMetadata(`http://localhost:${testPort}/no-content-length`); |
| 169 | + |
| 170 | + const content = await readStreamContent(result.stream); |
| 171 | + // Wait for cleanup to complete by listening to close event |
| 172 | + await waitForStreamCleanup(result.stream); |
| 173 | + |
| 174 | + // Check that temp files were cleaned up |
| 175 | + const tempFilesAfter = fs.readdirSync(tmpDir); |
| 176 | + expect(tempFilesAfter.length).toEqual(tempFilesBefore.length); |
| 177 | + expect(content).toBe("{\"test\": \"data\"}"); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should clean up temporary files on stream error", async () => { |
| 181 | + // Check temp files before |
| 182 | + const tmpDir = os.tmpdir(); |
| 183 | + const tempFilesBefore = fs.readdirSync(tmpDir); |
| 184 | + |
| 185 | + const result = await getFileStreamAndMetadata(`http://localhost:${testPort}/no-content-length`); |
| 186 | + |
| 187 | + // Trigger an error and wait for cleanup |
| 188 | + result.stream.destroy(new Error("Test error")); |
| 189 | + await waitForStreamCleanup(result.stream); |
| 190 | + |
| 191 | + // Check that temp files were cleaned up |
| 192 | + const tempFilesAfter = fs.readdirSync(tmpDir); |
| 193 | + expect(tempFilesAfter.length).toEqual(tempFilesBefore.length); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments