Skip to content

Commit 599e7b9

Browse files
committed
Resolve content type and size from cache entry #2
1 parent 4f43197 commit 599e7b9

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

index.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ function makeAssetCachePath(cacheDir, cacheKey) {
4242
};
4343
}
4444

45+
function makeAssetCacheName(contentType, contentLength) {
46+
return Buffer.from(`${contentType}:${contentLength}`).toString("base64");
47+
}
48+
4549
const middleWare = (module.exports = function(options) {
4650
return async function(req, res, next) {
4751
options = options || {};
@@ -62,15 +66,20 @@ const middleWare = (module.exports = function(options) {
6266
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1, dir2));
6367

6468
if (fs.existsSync(assetCachePath)) {
65-
const response = await fetch(res.locals.fetchUrl, { method: "HEAD" });
66-
67-
res.locals.contentLength = response.headers.get("content-length");
68-
res.locals.contentType = response.headers.get("content-type");
69-
7069
if (options.logger)
7170
options.logger.debug(`Reading buffer from path ${assetCachePath}`);
7271

73-
res.locals.buffer = fs.readFileSync(assetCachePath);
72+
const firstFile = fs.readdirSync(assetCachePath)[0];
73+
const decodedFileName = Buffer.from("firstFile", "base64").toString(
74+
"ascii"
75+
);
76+
77+
const [contentType, contentLength] = decodedFileName.split(":");
78+
79+
res.locals.contentLength = contentLength;
80+
res.locals.contentType = contentType;
81+
82+
res.locals.buffer = fs.readFileSync(`${assetCachePath}/${firstFile}`);
7483
} else {
7584
const blob = await (await fetch(res.locals.fetchUrl)).blob();
7685

@@ -82,7 +91,13 @@ const middleWare = (module.exports = function(options) {
8291
res.locals.contentType = blob.type;
8392
res.locals.contentLength = blob.size;
8493

85-
fs.writeFileSync(assetCachePath, res.locals.buffer);
94+
fs.writeFileSync(
95+
`${assetCachePath}/${middleWare.makeAssetCacheName(
96+
blob.type,
97+
blob.size
98+
)}`,
99+
res.locals.buffer
100+
);
86101
}
87102

88103
next();
@@ -105,3 +120,4 @@ const middleWare = (module.exports = function(options) {
105120

106121
middleWare.makeAssetCachePath = makeAssetCachePath;
107122
middleWare.makeDirIfNotExists = makeDirIfNotExists;
123+
middleWare.makeAssetCacheName = makeAssetCacheName;

test/test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("Middleware", function() {
5353

5454
expect(this.nextSpy).to.have.been.calledOnce;
5555
expect(writeSpy).to.have.been.calledWith(
56-
"./a1/b2/0123456789abcdef",
56+
"./a1/b2/0123456789abcdef/dW5kZWZpbmVkOnVuZGVmaW5lZA==",
5757
Buffer.from([])
5858
);
5959
});
@@ -65,6 +65,10 @@ describe("Middleware", function() {
6565
.stub(fs, "existsSync")
6666
.withArgs("./a1/b2/0123456789abcdef")
6767
.returns(true);
68+
sinon
69+
.stub(fs, "readdirSync")
70+
.withArgs("./a1/b2/0123456789abcdef")
71+
.returns(["dW5kZWZpbmVkOnVuZGVmaW5lZA=="]);
6872
const mw = middleware({ cacheDir: "." });
6973

7074
await mw(
@@ -75,7 +79,9 @@ describe("Middleware", function() {
7579

7680
expect(this.nextSpy).to.have.been.calledOnce;
7781
expect(readSpy)
78-
.to.have.been.calledWith("./a1/b2/0123456789abcdef")
82+
.to.have.been.calledWith(
83+
"./a1/b2/0123456789abcdef/dW5kZWZpbmVkOnVuZGVmaW5lZA=="
84+
)
7985
.and.returned(Buffer.from([]));
8086
});
8187

@@ -127,4 +133,10 @@ describe("Middleware", function() {
127133
this.makePathSpy.resetHistory();
128134
});
129135
});
136+
137+
describe("asset cache name generation", function() {
138+
expect(middleware.makeAssetCacheName("image/png", "4096")).to.equal(
139+
"aW1hZ2UvcG5nOjQwOTY="
140+
);
141+
});
130142
});

0 commit comments

Comments
 (0)