@contrast/agent 4.32.1 → 4.32.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/lib/hooks/module/helpers.js +44 -10
- package/package.json +1 -1
|
@@ -12,6 +12,8 @@ Copyright: 2023 Contrast Security, Inc
|
|
|
12
12
|
engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
13
|
way not consistent with the End User License Agreement.
|
|
14
14
|
*/
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
15
17
|
const fs = require('fs');
|
|
16
18
|
const os = require('os');
|
|
17
19
|
const makeDir = require('make-dir');
|
|
@@ -64,10 +66,13 @@ module.exports.cache = async function cache(agent, filename, result) {
|
|
|
64
66
|
const cachedFilename = getCachedFilename(agent, filename);
|
|
65
67
|
|
|
66
68
|
try {
|
|
67
|
-
await makeDir(path.dirname(cachedFilename), {
|
|
68
|
-
recursive: true
|
|
69
|
-
});
|
|
69
|
+
await makeDir(path.dirname(cachedFilename), { recursive: true });
|
|
70
70
|
await writeFile(cachedFilename, result, 'utf8');
|
|
71
|
+
|
|
72
|
+
logger.trace('Cache entry created: %o', {
|
|
73
|
+
filename,
|
|
74
|
+
cachedFilename,
|
|
75
|
+
});
|
|
71
76
|
} catch (err) {
|
|
72
77
|
logger.warn('Unable to cache rewrite results for %s: %o', filename, err);
|
|
73
78
|
}
|
|
@@ -84,9 +89,9 @@ module.exports.cache = async function cache(agent, filename, result) {
|
|
|
84
89
|
module.exports.cacheWithSourceMap = async function cacheWithSourceMap(
|
|
85
90
|
agent,
|
|
86
91
|
filename,
|
|
87
|
-
result
|
|
92
|
+
result,
|
|
88
93
|
) {
|
|
89
|
-
if (!agent.config.agent.node.rewrite_cache.enable) return
|
|
94
|
+
if (!agent.config.agent.node.rewrite_cache.enable) return;
|
|
90
95
|
// if this is the result of the estools rewriter, we want to use
|
|
91
96
|
// the normal caching functionality
|
|
92
97
|
if (!result.code) {
|
|
@@ -97,12 +102,18 @@ module.exports.cacheWithSourceMap = async function cacheWithSourceMap(
|
|
|
97
102
|
|
|
98
103
|
try {
|
|
99
104
|
await makeDir(path.dirname(cachedFilename), {
|
|
100
|
-
recursive: true
|
|
105
|
+
recursive: true,
|
|
101
106
|
});
|
|
102
107
|
await Promise.all([
|
|
103
108
|
writeFile(cachedFilename, result.code, 'utf8'),
|
|
104
|
-
writeFile(`${cachedFilename}.map`, JSON.stringify(result.map), 'utf8')
|
|
109
|
+
writeFile(`${cachedFilename}.map`, JSON.stringify(result.map), 'utf8'),
|
|
105
110
|
]);
|
|
111
|
+
|
|
112
|
+
logger.trace('Cache entry created: %o', {
|
|
113
|
+
filename,
|
|
114
|
+
cachedFilename,
|
|
115
|
+
map: true,
|
|
116
|
+
});
|
|
106
117
|
} catch (err) {
|
|
107
118
|
logger.warn('Unable to cache rewrite results for %s: %o', filename, err);
|
|
108
119
|
}
|
|
@@ -121,19 +132,42 @@ module.exports.find = function find(agent, filename) {
|
|
|
121
132
|
const cachedFilename = getCachedFilename(agent, filename);
|
|
122
133
|
|
|
123
134
|
try {
|
|
135
|
+
const time = mtime(filename);
|
|
136
|
+
const cachedTime = mtime(cachedFilename);
|
|
137
|
+
|
|
124
138
|
// if the file being loaded is newer than our cache entry, recompile
|
|
125
|
-
if (
|
|
139
|
+
if (time > cachedTime) {
|
|
140
|
+
logger.trace('Cache invalid, falling back to compiling: %o', {
|
|
141
|
+
sourceFilename: filename,
|
|
142
|
+
cachedFilename,
|
|
143
|
+
sourceTime: time,
|
|
144
|
+
cachedTime,
|
|
145
|
+
});
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
126
149
|
// otherwise we can simply return the content of the cached file
|
|
150
|
+
logger.trace('Cache valid: %o', {
|
|
151
|
+
sourceFilename: filename,
|
|
152
|
+
cachedFilename,
|
|
153
|
+
sourceTime: time,
|
|
154
|
+
cachedTime,
|
|
155
|
+
});
|
|
127
156
|
return fs.readFileSync(cachedFilename, 'utf8');
|
|
128
157
|
} catch (err) {
|
|
129
158
|
// statSync throws when file is not found, any error not ENOENT is bad
|
|
130
|
-
if (err.code !== 'ENOENT')
|
|
159
|
+
if (err.code !== 'ENOENT') {
|
|
131
160
|
logger.error(
|
|
132
161
|
'An unexpected error occurred, falling back to compiling %s: %o',
|
|
133
162
|
filename,
|
|
134
|
-
err
|
|
163
|
+
err,
|
|
135
164
|
);
|
|
165
|
+
}
|
|
136
166
|
|
|
167
|
+
logger.trace('Cache miss, falling back to compiling: %o', {
|
|
168
|
+
filename,
|
|
169
|
+
cachedFilename,
|
|
170
|
+
});
|
|
137
171
|
return undefined;
|
|
138
172
|
}
|
|
139
173
|
};
|