-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a565f30
commit 150d15f
Showing
6 changed files
with
625 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ node_modules | |
.output | ||
.data | ||
.env | ||
dist | ||
dist | ||
.ipx-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { listen } from "listhen"; | ||
import express from "express"; | ||
import { | ||
createIPX, | ||
ipxFSStorage, | ||
ipxHttpStorage, | ||
createIPXNodeServer, | ||
} from "ipx"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { Writable, PassThrough } from "stream"; | ||
|
||
const cacheDir = "./.ipx-cache"; | ||
|
||
// Ensure cache directory exists | ||
if (!fs.existsSync(cacheDir)) { | ||
fs.mkdirSync(cacheDir, { recursive: true }); | ||
} | ||
|
||
// Custom Writable Stream to capture response data | ||
class CaptureStream extends Writable { | ||
constructor(options) { | ||
super(options); | ||
this.chunks = []; | ||
} | ||
|
||
_write(chunk, encoding, callback) { | ||
this.chunks.push(chunk); | ||
callback(); | ||
} | ||
|
||
getBuffer() { | ||
return Buffer.concat(this.chunks); | ||
} | ||
} | ||
|
||
// Middleware to check and cache the response | ||
const cacheMiddleware = (req, res, next) => { | ||
const cacheFilePath = path.resolve(path.join(cacheDir, req.originalUrl)); | ||
|
||
console.log("Checking cache", cacheFilePath); | ||
if (fs.existsSync(cacheFilePath)) { | ||
console.log("Cache found", cacheFilePath); | ||
res.sendFile(cacheFilePath); | ||
} else { | ||
console.log("Cache not found", cacheFilePath); | ||
// Create a PassThrough stream to capture the response data | ||
// Create CaptureStream to capture response data | ||
const captureStream = new CaptureStream(); | ||
const passThrough = new PassThrough(); | ||
|
||
passThrough.pipe(captureStream); | ||
|
||
// Override res.write and res.end to use PassThrough stream | ||
const originalWrite = res.write.bind(res); | ||
const originalEnd = res.end.bind(res); | ||
|
||
res.write = (chunk, encoding, callback) => { | ||
console.log("res.write"); | ||
passThrough.write(chunk, encoding, callback); | ||
return originalWrite(chunk, encoding, callback); | ||
}; | ||
|
||
res.end = (chunk, encoding, callback) => { | ||
if (chunk) passThrough.write(chunk, encoding); | ||
passThrough.end(); | ||
return originalEnd(chunk, encoding, callback); | ||
}; | ||
|
||
res.on("finish", () => { | ||
// Create dit if it doesnt exist | ||
if (!fs.existsSync(path.dirname(cacheFilePath))) | ||
fs.mkdirSync(path.dirname(cacheFilePath), { recursive: true }); | ||
|
||
// Write captured data to cache file | ||
console.log("Writing cache: ", cacheFilePath); | ||
fs.writeFile(cacheFilePath, captureStream.getBuffer(), (err) => { | ||
if (err) { | ||
console.error("Error writing cache file:", err); | ||
} | ||
}); | ||
}); | ||
|
||
// Continue with IPX processing | ||
next(); | ||
} | ||
}; | ||
|
||
const ipx = createIPX({ | ||
storage: ipxFSStorage({ dir: "./public" }), | ||
httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }), | ||
}); | ||
|
||
const app = express(); | ||
app.use(cacheMiddleware); | ||
app.use("/", createIPXNodeServer(ipx)); | ||
|
||
listen(app, { port: 4000 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.