@kne/fastify-puppeteer 1.1.1 → 1.1.3

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.
package/index.js CHANGED
@@ -10,6 +10,7 @@ module.exports = fp(
10
10
  root: path.resolve(process.cwd(), '.puppeteer_cache'),
11
11
  prefix: `/api/v${packageJson.version.split('.')[0]}/puppeteer`,
12
12
  maxCacheKeys: 1000,
13
+ maxConcurrent: 10,
13
14
  maxTaskSize: 100,
14
15
  pageWidth: 1366,
15
16
  pageHeight: 768,
@@ -1,48 +1,51 @@
1
- const { LRUCache } = require('lru-cache');
1
+ const {LRUCache} = require('lru-cache');
2
2
  const path = require('path');
3
3
  const fs = require('fs-extra');
4
4
  const merge = require('lodash/merge');
5
5
  const fileType = require('file-type');
6
6
  const objectHash = require('object-hash');
7
- const { v4: uuidv4 } = require('uuid');
7
+ const {v4: uuidv4} = require('uuid');
8
+ const crypto = require('crypto');
8
9
 
9
10
  module.exports = async options => {
10
- options = merge(
11
- {},
12
- {
13
- maxCacheKeys: 1000,
14
- root: path.resolve(process.cwd(), '.puppeteer_cache')
15
- },
16
- options
17
- );
11
+ options = merge({}, {
12
+ maxCacheKeys: 1000, root: path.resolve(process.cwd(), '.puppeteer_cache')
13
+ }, options);
18
14
  await fs.emptyDir(options.root);
19
15
  const cache = new LRUCache({
20
- max: options.maxCacheKeys,
21
- dispose: async value => {
16
+ max: options.maxCacheKeys, dispose: async value => {
22
17
  await fs.remove(path.resolve(options.root, value));
23
18
  }
24
19
  });
25
20
  console.log('init cache dir');
26
21
  return {
27
22
  setCache: async (object, file) => {
23
+ const hash = objectHash(object);
28
24
  if (Buffer.isBuffer(file)) {
29
- const { ext } = await fileType.fromBuffer(file);
30
- const filename = uuidv4() + '.' + ext;
25
+ const {ext} = await fileType.fromBuffer(file);
26
+ const filename = crypto.createHash('md5').update(file).digest('hex') + '.' + ext;
31
27
  const filePath = path.resolve(options.root, filename);
32
28
  await fs.writeFile(filePath, file);
33
- cache.set(objectHash(object), filename);
29
+ cache.set(hash, filename);
34
30
  return filename;
35
31
  } else {
36
32
  const filename = uuidv4() + '.zip';
37
33
  const filePath = path.resolve(options.root, filename);
34
+ const hash = crypto.createHash('md5');
38
35
  await new Promise((resolve, reject) => {
39
- file.pipe(fs.createWriteStream(filePath)).on('error', reject).on('finish', resolve);
36
+ file
37
+ .on('data', chunk => hash.update(chunk))
38
+ .pipe(fs.createWriteStream(filePath))
39
+ .on('error', reject)
40
+ .on('finish', resolve);
40
41
  });
41
- cache.set(objectHash(object), filename);
42
- return filename;
42
+ const newFilename = hash.digest('hex') + '.zip';
43
+ const newFilePath = path.resolve(options.root, newFilename);
44
+ await fs.move(filePath, newFilePath);
45
+ cache.set(hash, newFilename);
46
+ return newFilename;
43
47
  }
44
- },
45
- getCache: async object => {
48
+ }, getCache: async object => {
46
49
  const filename = cache.get(objectHash(object));
47
50
  if (!filename) {
48
51
  return null;
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, maxConcurrent, root } = options;
9
10
  const cache = await createCache(options);
10
11
  const puppeteerPage = await setup(options);
11
12
 
13
+ const deferred = createDeferred(maxConcurrent);
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.3",
4
4
  "description": "用于操作无头浏览器截图保存pdf等",
5
5
  "main": "index.js",
6
6
  "scripts": {