1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.cache = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const fs = require("fs");
|
13 | const os = require("os");
|
14 | const path = require("path");
|
15 | const zlib = require("zlib");
|
16 | const crypto = require("crypto");
|
17 | const findCacheDir = require("find-cache-dir");
|
18 | const makeDir = require("make-dir");
|
19 | const util_1 = require("util");
|
20 | const _1 = require(".");
|
21 |
|
22 | let defaultCacheDirectory = null;
|
23 | const readFile = (0, util_1.promisify)(fs.readFile);
|
24 | const writeFile = (0, util_1.promisify)(fs.writeFile);
|
25 | const gunzip = (0, util_1.promisify)(zlib.gunzip);
|
26 | const gzip = (0, util_1.promisify)(zlib.gzip);
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const read = async function (filename, compress) {
|
35 | const data = await readFile(filename + (compress ? ".gz" : ""));
|
36 | const content = compress ? await gunzip(data) : data;
|
37 | return JSON.parse(content.toString());
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | const write = async function (filename, compress, result) {
|
48 | const content = JSON.stringify(result);
|
49 | const data = compress ? await gzip(content) : content;
|
50 | return await writeFile(filename + (compress ? ".gz" : ""), data);
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | const filename = function (source, identifier) {
|
61 | const hash = crypto.createHash("md4");
|
62 | const contents = JSON.stringify({ source, identifier });
|
63 | hash.update(contents);
|
64 | return hash.digest("hex") + ".json";
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const handleCache = async function (directory, cacheOptions, params) {
|
73 | const { cacheIdentifier, cacheDirectory, cacheCompression } = cacheOptions;
|
74 | const file = path.join(directory, filename(params.resourcePath, cacheIdentifier));
|
75 | try {
|
76 |
|
77 |
|
78 | return await read(file, cacheCompression);
|
79 | }
|
80 | catch (err) {
|
81 |
|
82 | }
|
83 | const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
|
84 |
|
85 | try {
|
86 | await makeDir(directory);
|
87 | }
|
88 | catch (err) {
|
89 | if (fallback) {
|
90 | return handleCache(os.tmpdir(), cacheOptions, params);
|
91 | }
|
92 | throw err;
|
93 | }
|
94 |
|
95 |
|
96 | const result = await (0, _1.transform)(params);
|
97 | try {
|
98 | await write(file, cacheCompression, result);
|
99 | }
|
100 | catch (err) {
|
101 | if (fallback) {
|
102 |
|
103 | return handleCache(os.tmpdir(), cacheOptions, params);
|
104 | }
|
105 | throw err;
|
106 | }
|
107 | return result;
|
108 | };
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | async function cache(cacheOptions, transformParams) {
|
118 | let directory;
|
119 | if (typeof cacheOptions.cacheDirectory === "string") {
|
120 | directory = cacheOptions.cacheDirectory;
|
121 | }
|
122 | else {
|
123 | if (defaultCacheDirectory === null) {
|
124 | defaultCacheDirectory = findCacheDir({ name: "responsive-loader" }) || os.tmpdir();
|
125 | }
|
126 | directory = defaultCacheDirectory;
|
127 | }
|
128 | return await handleCache(directory, cacheOptions, transformParams);
|
129 | }
|
130 | exports.cache = cache;
|