Skip to content

Commit d3f3c1d

Browse files
committed
Extract decode method and log correct entries #2
1 parent 599e7b9 commit d3f3c1d

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

index.js

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

45-
function makeAssetCacheName(contentType, contentLength) {
45+
function encodeAssetCacheName(contentType, contentLength) {
4646
return Buffer.from(`${contentType}:${contentLength}`).toString("base64");
4747
}
4848

49+
function decodeAssetCacheName(encodedString) {
50+
const decodedFileName = Buffer.from(encodedString, "base64").toString(
51+
"ascii"
52+
);
53+
return decodedFileName.split(":");
54+
}
55+
4956
const middleWare = (module.exports = function(options) {
5057
return async function(req, res, next) {
5158
options = options || {};
@@ -66,38 +73,36 @@ const middleWare = (module.exports = function(options) {
6673
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1, dir2));
6774

6875
if (fs.existsSync(assetCachePath)) {
76+
const firstFile = fs.readdirSync(assetCachePath)[0];
77+
6978
if (options.logger)
70-
options.logger.debug(`Reading buffer from path ${assetCachePath}`);
79+
options.logger.debug(
80+
`Reading buffer from path ${assetCachePath}/${firstFile}`
81+
);
7182

72-
const firstFile = fs.readdirSync(assetCachePath)[0];
73-
const decodedFileName = Buffer.from("firstFile", "base64").toString(
74-
"ascii"
83+
const [contentType, contentLength] = middleWare.decodeAssetCacheName(
84+
firstFile
7585
);
7686

77-
const [contentType, contentLength] = decodedFileName.split(":");
78-
7987
res.locals.contentLength = contentLength;
8088
res.locals.contentType = contentType;
8189

8290
res.locals.buffer = fs.readFileSync(`${assetCachePath}/${firstFile}`);
8391
} else {
8492
const blob = await (await fetch(res.locals.fetchUrl)).blob();
8593

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

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

91102
res.locals.contentType = blob.type;
92103
res.locals.contentLength = blob.size;
93104

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

103108
next();
@@ -120,4 +125,5 @@ const middleWare = (module.exports = function(options) {
120125

121126
middleWare.makeAssetCachePath = makeAssetCachePath;
122127
middleWare.makeDirIfNotExists = makeDirIfNotExists;
123-
middleWare.makeAssetCacheName = makeAssetCacheName;
128+
middleWare.encodeAssetCacheName = encodeAssetCacheName;
129+
middleWare.decodeAssetCacheName = decodeAssetCacheName;

test/test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,17 @@ describe("Middleware", function() {
134134
});
135135
});
136136

137-
describe("asset cache name generation", function() {
138-
expect(middleware.makeAssetCacheName("image/png", "4096")).to.equal(
139-
"aW1hZ2UvcG5nOjQwOTY="
140-
);
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+
});
141149
});
142150
});

0 commit comments

Comments
 (0)