Skip to content

Commit

Permalink
use async.mapLimit 解决并发请求控制
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin-front committed Mar 20, 2018
1 parent dbee9bd commit 8b3e99b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
19 changes: 18 additions & 1 deletion example/source/_posts/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ categories: [sample]
---

test
![speed improvements in npm@5](https://cdn-images-1.medium.com/max/800/0*K1Wb1ERhtAHLRG0m.)
![speed improvements in npm@5](https://cdn-images-1.medium.com/max/800/0*K1Wb1ERhtAHLRG0m.)

![](/images/masasa/IMG_1429.JPG)


![在调试模式下运行Node.js应用程序](https://cdn-images-1.medium.com/max/800/1*NIVrIoEiniOt6cP-d-0LFQ.png)

接下来,忽略从终端中显示的“`chrome-devtools://`”开头的URL ,而是在Google Chrome中打开“ `about:inspect` ”。

![chrome DevTools](https://cdn-images-1.medium.com/max/800/1*QAmWRyPtdfF5X7VuvDN1zg.png)

最后点击“ Open dedicated DevTools for Node” 开始调试应用程序的代码。

![deloper tools for Node](https://cdn-images-1.medium.com/max/800/1*zQ1uLl_qiRIAJ7Lxw_LFAQ.png)

![coverage](https://cdn-images-1.medium.com/max/800/1*gD8lX40PSemDOZgvT695Mg.png)

![coverage](https://cdn-images-1.medium.com/max/1200/1*1SUTeKlhRee3MyKdxkUGtQ.png)
50 changes: 27 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const gm = require('gm');
const request = require('request');
const crypto = require('crypto');
const url = require('url');
const async = require('async');

const config = hexo.config;
const lazyload = config.lazyload;
Expand All @@ -28,6 +29,7 @@ const addStyle = (src, content) => content + `<link rel="stylesheet" href="${src

const isRemotePath = str => str.indexOf('http') !== -1;

const isImg = str => /\/([^/]+\.(?:png|jpg|jpeg|gif|bmp))/i.test(str);
const getHashFileName = fpath => {
const extname = path.extname(url.parse(fpath).pathname);
const hash = crypto.createHash('md5').update(fpath).digest("hex");
Expand Down Expand Up @@ -71,10 +73,17 @@ const transformHTML = source => {
}
};

let waitForThumb = [];
const transformImg = s => {
const $ = cheerio.load(s);
const $img = typeof s === 'string' ? $('img') : s;
const attr = $img.attr('src');
if (!isImg(attr)) {
return s;
}
if (waitForThumb.indexOf(attr) === -1) {
waitForThumb.push(attr);
}
$img.attr('src', loadingImgPath);
$img.attr('data-original', attr);
$img.attr('data-thumb', path.join('/', thumbPath, getHashFileName(attr)));
Expand All @@ -88,25 +97,10 @@ const transformImg = s => {
return $.html();
}

const iterationImages = async (source) => {
const className = lazyload.className;
let srcArr;
if (className) {
const $ = cheerio.load(source);
srcArr = $(className).map($img => $img.attr('src'));
} else {
srcArr = source.match(/<img([^>]+)?>/igm)||[];
srcArr = srcArr.map(s => {
const $ = cheerio.load(s);
const attr = $('img').attr('src');
return attr;
});
}
return await Promise.all(srcArr.map(generateThumb));
}

const generateThumb = async (originPath) => {
if (!originPath) { return false; }
if (!originPath || !isImg(originPath)) {
return false;
}
const hashFileName = getHashFileName(originPath);
const targetPath = path.resolve(thumbTargetFolder, hashFileName);
const exists = await existFile(targetPath);
Expand All @@ -115,7 +109,7 @@ const generateThumb = async (originPath) => {
};
}

const gmAsync = (originPath, targetPath) => new Promise((resolve, reject) => {
const gmAsync = async (originPath, targetPath) => await new Promise((resolve, reject) => {
gm(getOriginImage(originPath))
.thumbnail(200, 200)
.quality(8)
Expand Down Expand Up @@ -143,12 +137,22 @@ hexo.extend.filter.register('before_post_render', async function (data) {
await mkdir(thumbTargetFolder);
});

hexo.extend.filter.register('after_post_render', async function(data) {
hexo.extend.filter.register('after_post_render', function (data) {
copyAssets();
log('start processing thumb of ' + data.source);
await iterationImages(data.content);
data.content = addScript('/js/lazyload-plugin/lazyload.intersectionObserver.min.js', data.content);
data.content = transformHTML(data.content);
log('success processed thumb of ' + data.source)
return data;
});

hexo.extend.filter.register('after_generate', async function loopPipe() {
log('start processing thumb');
return await async.mapLimit(waitForThumb, 5, async function (url) {
const response = await generateThumb(url);
return response;
}, (err) => {
if (err) throw err;
// results is now an array of the response bodies
// console.log('results', results)
log('All thumb process successed !');
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-lazyload",
"version": "1.2.2",
"version": "1.2.4",
"description": "use IntersectionObserver api to improve lazyload performance",
"main": "index.js",
"scripts": {
Expand All @@ -23,6 +23,7 @@
"author": "robin",
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"cheerio": "^1.0.0-rc.2",
"gm": "^1.23.1",
"mkdirp": "^0.5.1"
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ arrify@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"

async@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.0.tgz#61a29abb6fcc026fea77e56d1c6ec53a795951f4"
dependencies:
lodash "^4.14.0"

babel-code-frame@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
Expand Down Expand Up @@ -613,6 +619,10 @@ lodash@^4.0.0, lodash@^4.15.0, lodash@^4.17.4, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

lodash@^4.14.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

lru-cache@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55"
Expand Down

0 comments on commit 8b3e99b

Please sign in to comment.