Skip to content

Commit c866c74

Browse files
authored
Merge pull request #3 from julianrubisch/2-base64-encode-mime-and-size
Resolve content type and size from cache entry #2
2 parents 55d3eb1 + d3f3c1d commit c866c74

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

index.js

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

45+
function encodeAssetCacheName(contentType, contentLength) {
46+
return Buffer.from(`${contentType}:${contentLength}`).toString("base64");
47+
}
48+
49+
function decodeAssetCacheName(encodedString) {
50+
const decodedFileName = Buffer.from(encodedString, "base64").toString(
51+
"ascii"
52+
);
53+
return decodedFileName.split(":");
54+
}
55+
4556
const middleWare = (module.exports = function(options) {
4657
return async function(req, res, next) {
4758
options = options || {};
@@ -62,27 +73,36 @@ const middleWare = (module.exports = function(options) {
6273
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1, dir2));
6374

6475
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");
76+
const firstFile = fs.readdirSync(assetCachePath)[0];
6977

7078
if (options.logger)
71-
options.logger.debug(`Reading buffer from path ${assetCachePath}`);
79+
options.logger.debug(
80+
`Reading buffer from path ${assetCachePath}/${firstFile}`
81+
);
82+
83+
const [contentType, contentLength] = middleWare.decodeAssetCacheName(
84+
firstFile
85+
);
86+
87+
res.locals.contentLength = contentLength;
88+
res.locals.contentType = contentType;
7289

73-
res.locals.buffer = fs.readFileSync(assetCachePath);
90+
res.locals.buffer = fs.readFileSync(`${assetCachePath}/${firstFile}`);
7491
} else {
7592
const blob = await (await fetch(res.locals.fetchUrl)).blob();
7693

94+
const fileName = middleWare.encodeAssetCacheName(blob.type, blob.size);
7795
if (options.logger)
78-
options.logger.debug(`Writing buffer to path ${assetCachePath}`);
96+
options.logger.debug(
97+
`Writing buffer to path ${assetCachePath}/${fileName}`
98+
);
7999

80100
res.locals.buffer = Buffer.from(await blob.arrayBuffer(), "binary");
81101

82102
res.locals.contentType = blob.type;
83103
res.locals.contentLength = blob.size;
84104

85-
fs.writeFileSync(assetCachePath, res.locals.buffer);
105+
fs.writeFileSync(`${assetCachePath}/${fileName}`, res.locals.buffer);
86106
}
87107

88108
next();
@@ -105,3 +125,5 @@ const middleWare = (module.exports = function(options) {
105125

106126
middleWare.makeAssetCachePath = makeAssetCachePath;
107127
middleWare.makeDirIfNotExists = makeDirIfNotExists;
128+
middleWare.encodeAssetCacheName = encodeAssetCacheName;
129+
middleWare.decodeAssetCacheName = decodeAssetCacheName;

test/test.js

Lines changed: 22 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,18 @@ describe("Middleware", function() {
127133
this.makePathSpy.resetHistory();
128134
});
129135
});
136+
137+
describe("asset cache name en/decoding", function() {
138+
it("encodes contenttype and length to filename", function() {
139+
expect(middleware.encodeAssetCacheName("image/png", "4096")).to.equal(
140+
"aW1hZ2UvcG5nOjQwOTY="
141+
);
142+
});
143+
144+
it("retrieves contenttype and length from filename", function() {
145+
expect(
146+
middleware.decodeAssetCacheName("aW1hZ2UvcG5nOjQwOTY=")
147+
).to.deep.equal(["image/png", "4096"]);
148+
});
149+
});
130150
});

0 commit comments

Comments
 (0)