Skip to content

Commit cd33dc6

Browse files
committed
feat: cache at loader level
This makes the cache global, per process, so that it can be re-used when resolving deps.
1 parent f62fad3 commit cd33dc6

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

lib/deps.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ const Tmp = require('tmp');
99
const Package = require('./package');
1010
const Utils = require('./utils');
1111

12-
const internals = {};
13-
14-
15-
internals.log = Debug('detect-node-support');
12+
const internals = {
13+
log: Debug('detect-node-support')
14+
};
1615

1716

1817
internals.resolve = async ({ packageJson, lockfile }, options) => {

lib/loader.js

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Wreck = require('@hapi/wreck');
1111
const Utils = require('./utils');
1212

1313
const internals = {
14+
cache: new Map(),
1415
log: Debug('detect-node-support:loader'),
1516
error: Debug('detect-node-support:error')
1617
};
@@ -95,9 +96,18 @@ internals.createRepositoryLoader = (repository) => {
9596
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
9697
internals.log('Loading: %s', url);
9798

99+
if (options === undefined && internals.cache.has(url)) {
100+
internals.log('From cache: %s', url);
101+
return internals.cache.get(url);
102+
}
103+
98104
try {
99105
const { payload } = await Wreck.get(url, options);
100106

107+
if (options === undefined) {
108+
internals.cache.set(url, payload);
109+
}
110+
101111
internals.log('Loaded: %s', url);
102112
return payload;
103113
}
@@ -164,3 +174,9 @@ exports.create = ({ path, repository, packageName }) => {
164174

165175
return internals.createPathLoader(path);
166176
};
177+
178+
179+
exports.clearCache = () => {
180+
181+
internals.cache = new Map();
182+
};

lib/travis/imports.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const Debug = require('debug');
43
const Yaml = require('js-yaml');
54

65
const Loader = require('../loader');
@@ -14,9 +13,6 @@ const internals = {
1413
};
1514

1615

17-
internals.log = Debug('detect-node-support');
18-
19-
2016
internals.normalizeImports = (travisYaml, { relativeTo }) => {
2117

2218
return Utils.toArray(travisYaml.import)
@@ -53,12 +49,7 @@ internals.normalizeImports = (travisYaml, { relativeTo }) => {
5349
};
5450

5551

56-
internals.loadSource = async (source, { loadFile, cache }) => {
57-
58-
if (cache[source]) {
59-
internals.log('Returning cached %s', source);
60-
return cache[source];
61-
}
52+
internals.loadSource = async (source, { loadFile }) => {
6253

6354
let path = source;
6455

@@ -70,14 +61,11 @@ internals.loadSource = async (source, { loadFile, cache }) => {
7061
loadFile = loader.loadFile;
7162
}
7263

73-
internals.log('Loading %s', source);
74-
const result = await loadFile(path);
75-
cache[source] = result;
76-
return result;
64+
return loadFile(path);
7765
};
7866

7967

80-
exports.apply = async (yaml, { loadFile, relativeTo, cache = new Map() }) => {
68+
exports.apply = async (yaml, { loadFile, relativeTo }) => {
8169

8270
if (!yaml.import) {
8371
return;
@@ -87,14 +75,14 @@ exports.apply = async (yaml, { loadFile, relativeTo, cache = new Map() }) => {
8775

8876
for (const entry of imports) {
8977

90-
const buffer = await internals.loadSource(entry.source, { loadFile, cache });
78+
const buffer = await internals.loadSource(entry.source, { loadFile });
9179

9280
const imported = Yaml.safeLoad(buffer, {
9381
schema: Yaml.FAILSAFE_SCHEMA,
9482
json: true
9583
});
9684

97-
await exports.apply(imported, { loadFile, relativeTo: entry, cache });
85+
await exports.apply(imported, { loadFile, relativeTo: entry });
9886

9987
delete imported.import;
10088

test/fixtures/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const SimpleGit = require('simple-git/promise');
77
const Sinon = require('sinon');
88
const Tmp = require('tmp');
99

10+
const Loader = require('../../lib/loader');
1011
const Utils = require('../../lib/utils');
1112

1213

@@ -28,6 +29,8 @@ module.exports = class TestContext {
2829

2930
cleanup() {
3031

32+
Loader.clearCache();
33+
3134
Sinon.restore();
3235

3336
this._cleanup.forEach((cleanup) => cleanup());

test/travis.js

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ describe('.travis.yml parsing', () => {
156156
resolved: { '14': '14.3.0' }
157157
}
158158
});
159+
160+
// verify cache returns the same result
161+
const result2 = await NodeSupport.detect({ path: fixture.path });
162+
internals.assertCommit(result2);
163+
expect(result2).to.equal(result);
159164
});
160165

161166
it('resolves and merges (prepend/append)', async () => {

0 commit comments

Comments
 (0)