@kne/fastify-puppeteer 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ const merge = require('lodash/merge');
5
5
  const fileType = require('file-type');
6
6
  const objectHash = require('object-hash');
7
7
  const { v4: uuidv4 } = require('uuid');
8
+ const crypto = require('crypto');
8
9
 
9
10
  module.exports = async options => {
10
11
  options = merge(
@@ -25,20 +26,23 @@ module.exports = async options => {
25
26
  console.log('init cache dir');
26
27
  return {
27
28
  setCache: async (object, file) => {
29
+ const hash = objectHash(object);
30
+ const md5 = crypto.createHash('md5').update(file).digest('hex');
28
31
  if (Buffer.isBuffer(file)) {
29
32
  const { ext } = await fileType.fromBuffer(file);
30
- const filename = uuidv4() + '.' + ext;
33
+ const filename = md5 + '.' + ext;
31
34
  const filePath = path.resolve(options.root, filename);
32
35
  await fs.writeFile(filePath, file);
33
- cache.set(objectHash(object), filename);
36
+ cache.set(hash, filename);
34
37
  return filename;
35
38
  } else {
36
- const filename = uuidv4() + '.zip';
39
+ const filename = md5 + '.zip';
37
40
  const filePath = path.resolve(options.root, filename);
38
41
  await new Promise((resolve, reject) => {
39
42
  file.pipe(fs.createWriteStream(filePath)).on('error', reject).on('finish', resolve);
40
43
  });
41
- cache.set(objectHash(object), filename);
44
+
45
+ cache.set(hash, filename);
42
46
  return filename;
43
47
  }
44
48
  },
package/libs/services.js CHANGED
@@ -3,12 +3,15 @@ const setup = require('./core/setup');
3
3
  const createCache = require('./core/file-cache');
4
4
  const compressing = require('compressing');
5
5
  const path = require('path');
6
+ const createDeferred = require('@kne/create-deferred');
6
7
 
7
8
  module.exports = fp(async (fastify, options) => {
8
- const { maxCacheKeys, root } = options;
9
+ const { maxCacheKeys, maxConcurrency = 10, root } = options;
9
10
  const cache = await createCache(options);
10
11
  const puppeteerPage = await setup(options);
11
12
 
13
+ const deferred = createDeferred(maxConcurrency);
14
+
12
15
  const memoCache = (cacheName, callback) => async props => {
13
16
  const currentCache = await cache.getCache(Object.assign({}, props, { cacheName }));
14
17
  if (currentCache) {
@@ -27,8 +30,11 @@ module.exports = fp(async (fastify, options) => {
27
30
  }
28
31
  const zipStream = new compressing.zip.Stream();
29
32
  const files = [];
33
+
30
34
  for (let target of list) {
31
- files.push(await callback(target));
35
+ await deferred(async () => {
36
+ files.push(await callback(target));
37
+ });
32
38
  }
33
39
  files.forEach(filename => {
34
40
  zipStream.addEntry(path.resolve(root, filename));
@@ -43,7 +49,7 @@ module.exports = fp(async (fastify, options) => {
43
49
  Object.assign(
44
50
  {},
45
51
  {
46
- waitUntil: 'networkidle2',
52
+ waitUntil: 'networkidle0',
47
53
  displayHeaderFooter: true,
48
54
  printBackground: true,
49
55
  format: 'A4'
@@ -63,7 +69,7 @@ module.exports = fp(async (fastify, options) => {
63
69
  const parseHtmlToPhoto = memoCache('parseHtmlToPhoto', async ({ html, options }) => {
64
70
  return await puppeteerPage.task(async ({ page }) => {
65
71
  await page.setContent(html);
66
- return await page.screenshot(Object.assign({}, { waitUntil: 'networkidle2', type: 'png' }, options));
72
+ return await page.screenshot(Object.assign({}, { waitUntil: 'networkidle0', type: 'png' }, options));
67
73
  });
68
74
  });
69
75
 
@@ -76,7 +82,7 @@ module.exports = fp(async (fastify, options) => {
76
82
  const parseUrlToPdf = memoCache('parseUrlToPdf', async ({ url, options = {} }) => {
77
83
  return await puppeteerPage.task(async ({ page }) => {
78
84
  await page.goto(url, {
79
- waitUntil: 'networkidle2'
85
+ waitUntil: 'networkidle0'
80
86
  });
81
87
  await Promise.all(
82
88
  (options.waitForSelectors || []).map(waitForSelector => {
@@ -109,7 +115,7 @@ module.exports = fp(async (fastify, options) => {
109
115
  const parseUrlToPhoto = memoCache('parseUrlToPhoto', async ({ url, selector, options = {} }) => {
110
116
  return await puppeteerPage.task(async ({ page }) => {
111
117
  await page.goto(url, {
112
- waitUntil: 'networkidle2'
118
+ waitUntil: 'networkidle0'
113
119
  });
114
120
  await Promise.all(
115
121
  (options.waitForSelectors || []).map(waitForSelector => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-puppeteer",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "用于操作无头浏览器截图保存pdf等",
5
5
  "main": "index.js",
6
6
  "scripts": {