Skip to content

Commit

Permalink
fix(revisioned): calcFileHash
Browse files Browse the repository at this point in the history
  • Loading branch information
MHuiG committed May 24, 2022
1 parent f5692dd commit 1402b9e
Showing 1 changed file with 80 additions and 71 deletions.
151 changes: 80 additions & 71 deletions scripts/helpers/revisioned.js
Expand Up @@ -74,85 +74,94 @@ const revisioned = (filePath) => {
hexo.extend.helper.register("revisioned", revisioned);

const calcFileHash = async (filePath) => {
const buffer = await stream2buffer(hexo.route.get(filePath));
const fileHash = crypto.createHash("md5").update(buffer).digest('hex').substring(0, 8);
return fileHash;
if (hexo.route.get(filePath)) {
const buffer = await stream2buffer(hexo.route.get(filePath));
const fileHash = crypto.createHash("md5").update(buffer).digest('hex').substring(0, 8);
return fileHash;
} else {
// 随机生成 hash
return crypto.randomBytes(16).toString('hex').substring(0, 8);
}

};

const replaceRevisionPlaceholder = async () => {

if (!hexo.theme.config.cdn_version) {
return;
}

const options = hexo.config.new_revision || {};
const include = options.include || [];

const hashPromiseMap = {};
const hashMap = {};
const doHash = (filePath) =>
calcFileHash(filePath).then((hash) => {
hashMap[filePath] = hash;
});

await Promise.all(
hexo.route.list().map(async (path) => {
const [, , extension] = parseFilePath(path);
if (![".css", ".js", ".html"].includes(extension)) {
return;
}

let fileContent = await readFileAsString(path);

const regexp = /\.!!revision:([^\)]+?)!!/g;
const matchResult = [...fileContent.matchAll(regexp)];
if (matchResult.length) {
const hashTaskList = [];

// 异步获取文件 hash
matchResult.forEach((group) => {
const filePath = group[1];
if (!(filePath in hashPromiseMap)) {
hashPromiseMap[filePath] = doHash(filePath);
}
hashTaskList.push(hashPromiseMap[filePath]);
});

// 等待全部 hash 完成
await Promise.all(hashTaskList);

// 替换 placeholder
fileContent = fileContent.replace(regexp, function (match, filePath) {
if (!(filePath in hashMap)) {
throw new Error("file hash not computed");
try {
const options = hexo.config.new_revision || {};
const include = options.include || [];

const hashPromiseMap = {};
const hashMap = {};
const doHash = (filePath) =>
calcFileHash(filePath).then((hash) => {
hashMap[filePath] = hash;
});

await Promise.all(
hexo.route.list().map(async (path) => {
const [, , extension] = parseFilePath(path);
if (![".css", ".js", ".html"].includes(extension)) {
return;
}

let fileContent = await readFileAsString(path);

const regexp = /\.!!revision:([^\)]+?)!!/g;
const matchResult = [...fileContent.matchAll(regexp)];
if (matchResult.length) {
const hashTaskList = [];

// 异步获取文件 hash
matchResult.forEach((group) => {
const filePath = group[1];
if (!(filePath in hashPromiseMap)) {
hashPromiseMap[filePath] = doHash(filePath);
}
hashTaskList.push(hashPromiseMap[filePath]);
});

// 等待全部 hash 完成
await Promise.all(hashTaskList);

// 替换 placeholder
fileContent = fileContent.replace(regexp, function (match, filePath) {
if (!(filePath in hashMap)) {
throw new Error("file hash not computed");
}
return "." + hashMap[filePath];
});

hexo.route.set(path, fileContent);
}
})
);

await Promise.all(
hexo.route.list().map(async (path) => {
for (let i = 0, len = include.length; i < len; i++) {
if (minimatch(path, include[i])) {
return doHash(path);
}
return "." + hashMap[filePath];
});

hexo.route.set(path, fileContent);
}
})
);

await Promise.all(
hexo.route.list().map(async (path) => {
for (let i = 0, len = include.length; i < len; i++) {
if (minimatch(path, include[i])) {
return doHash(path);
}
}
})
);

await Promise.all(
Object.keys(hashMap).map(async (filePath) => {
hexo.route.set(
getRevisionedFilePath(filePath, hashMap[filePath]),
await readFileAsBuffer(filePath)
);
hexo.route.remove(filePath);
})
);
})
);

await Promise.all(
Object.keys(hashMap).map(async (filePath) => {
hexo.route.set(
getRevisionedFilePath(filePath, hashMap[filePath]),
await readFileAsBuffer(filePath)
);
hexo.route.remove(filePath);
})
);
} catch (error) {

}
};

hexo.extend.filter.register("after_generate", replaceRevisionPlaceholder,999999999999999999999999999999999999);
hexo.extend.filter.register("after_generate", replaceRevisionPlaceholder,9999);

0 comments on commit 1402b9e

Please sign in to comment.