utilitas 1989.9.46 → 1989.9.47

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/lib/shot.mjs CHANGED
@@ -1,9 +1,14 @@
1
+ import { promises as fs } from 'fs';
2
+ import { sha256 } from './encryption.mjs';
1
3
  import * as fileType from 'file-type';
2
4
  import * as storage from './storage.mjs';
3
5
  import * as utilitas from './utilitas.mjs';
4
6
  import fetch from 'node-fetch';
7
+ import path from 'path';
5
8
 
6
9
  const defFetchOpt = { redirect: 'follow', follow: 3, timeout: 1000 * 10 };
10
+ const fakeRsp = { status: 200, statusText: 'OK', headers: { get() { }, raw() { } } };
11
+ const buf2utf = buf => buf.toString('utf8');
7
12
 
8
13
  const getVersionOnNpm = async (packName) => {
9
14
  utilitas.assert(packName, 'Package name is required.', 400);
@@ -44,11 +49,23 @@ const get = async (url, options) => {
44
49
  utilitas.assert(url, 'URL is required.');
45
50
  options = options || {};
46
51
  options.encode = utilitas.ensureString(options.encode, { case: 'UP' });
47
- const r = await fetch(url, { defFetchOpt, ...options.fetch || {} });
48
- const ts = (b) => { return b.toString('utf8'); };
52
+ const requestHash = sha256(JSON.stringify({ url, ...options }));
53
+ const cachePath = options.cache?.tmp || process.env.TMPDIR;
54
+ const cacheFile = cachePath ? path.join(cachePath, requestHash) : null;
55
+ const cacheable = options.cache && cacheFile;
56
+ let cacheAction, content;
57
+ const cache = cacheable && await utilitas.ignoreErrFunc(async () => {
58
+ const resp = await fs.readFile(cacheFile);
59
+ cacheAction = 'READ';
60
+ return resp;
61
+ });
62
+ const r = cache
63
+ ? { ...fakeRsp, async buffer() { return cache } }
64
+ : await fetch(url, { defFetchOpt, ...options.fetch || {} });
49
65
  const [htpMime, buffer] = [r.headers.get('content-type'), await r.buffer()];
50
66
  const bufMime = utilitas.extract(await fileType.fileTypeFromBuffer(buffer), 'mime');
51
- let [mimeType, content] = [bufMime || htpMime, null];
67
+ const mimeType = bufMime || htpMime;
68
+ const length = buffer.length;
52
69
  switch (options.encode) {
53
70
  case 'BUFFER':
54
71
  content = buffer;
@@ -60,18 +77,25 @@ const get = async (url, options) => {
60
77
  content = storage.encodeBase64DataURL(mimeType, buffer);
61
78
  break;
62
79
  case 'JSON':
63
- try { content = JSON.parse(ts(buffer)); } catch (e) { }
80
+ try { content = JSON.parse(buf2utf(buffer)); } catch (e) { }
64
81
  break;
65
82
  case 'TEXT':
66
- content = ts(buffer);
83
+ content = buf2utf(buffer);
67
84
  break;
68
85
  default:
69
86
  utilitas.assert(!options.encode, 'Invalid encoding.', 400);
70
- content = ts(buffer);
87
+ content = buf2utf(buffer);
71
88
  }
89
+ cacheable && !cache && length && r.status === 200
90
+ && await utilitas.ignoreErrFunc(async () => {
91
+ const resp = await fs.writeFile(cacheFile, buffer);
92
+ cacheAction = 'WRITE';
93
+ return resp;
94
+ });
72
95
  return {
73
- statusCode: r.status, statusText: r.statusText, length: buffer.length,
96
+ statusCode: r.status, statusText: r.statusText, length,
74
97
  mimeType, content, headers: r.headers.raw(), response: r,
98
+ cache: cacheable && { filename: cacheFile, action: cacheAction },
75
99
  };
76
100
  }
77
101
 
package/lib/utilitas.mjs CHANGED
@@ -591,8 +591,8 @@ const checkInterval = (itv, sed) => {
591
591
  return !((Math.round(Date.now() / 1000) + ensureInt(sed)) % ensureInt(itv));
592
592
  };
593
593
 
594
- const ignoreErrFunc = async (f, o) => {
595
- try { return await f(); } catch (e) { return o?.log ? console.error(e) : e }
594
+ const ignoreErrFunc = async (func, options) => {
595
+ try { return await func(); } catch (e) { options?.log && console.error(e); }
596
596
  };
597
597
 
598
598
  const tryUntil = async (fnTry, options) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for Node.js.",
4
- "version": "1989.9.46",
4
+ "version": "1989.9.47",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",