@kne/fastify-puppeteer 1.1.2 → 1.1.4

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,25 +1,19 @@
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
8
  const crypto = require('crypto');
9
9
 
10
10
  module.exports = async options => {
11
- options = merge(
12
- {},
13
- {
14
- maxCacheKeys: 1000,
15
- root: path.resolve(process.cwd(), '.puppeteer_cache')
16
- },
17
- options
18
- );
11
+ options = merge({}, {
12
+ maxCacheKeys: 1000, root: path.resolve(process.cwd(), '.puppeteer_cache')
13
+ }, options);
19
14
  await fs.emptyDir(options.root);
20
15
  const cache = new LRUCache({
21
- max: options.maxCacheKeys,
22
- dispose: async value => {
16
+ max: options.maxCacheKeys, dispose: async value => {
23
17
  await fs.remove(path.resolve(options.root, value));
24
18
  }
25
19
  });
@@ -27,26 +21,31 @@ module.exports = async options => {
27
21
  return {
28
22
  setCache: async (object, file) => {
29
23
  const hash = objectHash(object);
30
- const md5 = crypto.createHash('md5').update(file).digest('hex');
31
24
  if (Buffer.isBuffer(file)) {
32
- const { ext } = await fileType.fromBuffer(file);
33
- const filename = md5 + '.' + ext;
25
+ const {ext} = await fileType.fromBuffer(file);
26
+ const filename = crypto.createHash('md5').update(file).digest('hex') + '.' + ext;
34
27
  const filePath = path.resolve(options.root, filename);
35
28
  await fs.writeFile(filePath, file);
36
29
  cache.set(hash, filename);
37
30
  return filename;
38
31
  } else {
39
- const filename = md5 + '.zip';
32
+ const filename = uuidv4() + '.zip';
40
33
  const filePath = path.resolve(options.root, filename);
34
+ const hash = crypto.createHash('md5');
41
35
  await new Promise((resolve, reject) => {
42
- 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);
43
41
  });
44
-
45
- cache.set(hash, filename);
46
- 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;
47
47
  }
48
- },
49
- getCache: async object => {
48
+ }, getCache: async object => {
50
49
  const filename = cache.get(objectHash(object));
51
50
  if (!filename) {
52
51
  return null;
package/libs/services.js CHANGED
@@ -6,22 +6,23 @@ const path = require('path');
6
6
  const createDeferred = require('@kne/create-deferred');
7
7
 
8
8
  module.exports = fp(async (fastify, options) => {
9
- const { maxCacheKeys, maxConcurrency = 10, root } = options;
9
+ const { maxCacheKeys, maxConcurrent, root } = options;
10
10
  const cache = await createCache(options);
11
11
  const puppeteerPage = await setup(options);
12
12
 
13
- const deferred = createDeferred(maxConcurrency);
13
+ const deferred = createDeferred(maxConcurrent);
14
14
 
15
15
  const memoCache = (cacheName, callback) => async props => {
16
- const currentCache = await cache.getCache(Object.assign({}, props, { cacheName }));
17
- if (currentCache) {
16
+ const inputProps = Object.assign({}, props, { cacheName });
17
+ const currentCache = await cache.getCache(inputProps);
18
+ if (currentCache && !inputProps.force) {
18
19
  return currentCache;
19
20
  }
20
21
  const output = await callback(props);
21
22
  if (!output.success) {
22
23
  throw output.error;
23
24
  }
24
- return await cache.setCache(props, output.result);
25
+ return await cache.setCache(inputProps, output.result);
25
26
  };
26
27
 
27
28
  const batchTask = async (list, callback) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-puppeteer",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "用于操作无头浏览器截图保存pdf等",
5
5
  "main": "index.js",
6
6
  "scripts": {