Skip to content

Commit 193cd1e

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 46aa651 commit 193cd1e

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
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

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

3+
const Debug = require('debug');
34
const Fs = require('fs');
45
const GitUrlParse = require('git-url-parse');
56
const Package = require('../package.json');
@@ -9,7 +10,10 @@ const Wreck = require('@hapi/wreck');
910

1011
const Utils = require('./utils');
1112

12-
const internals = {};
13+
const internals = {
14+
cache: new Map(),
15+
log: Debug('detect-node-support-loader')
16+
};
1317

1418

1519
internals.parseRepository = (packument) => {
@@ -85,15 +89,27 @@ internals.createRepositoryLoader = (repository) => {
8589
}
8690

8791
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
92+
internals.log('Loading: %s', url);
93+
94+
if (options === undefined && internals.cache.has(url)) {
95+
internals.log('From cache: %s', url);
96+
return internals.cache[url];
97+
}
8898

8999
try {
90100
const { payload } = await Wreck.get(url, options);
91101

102+
if (options === undefined) {
103+
internals.cache.set(url, payload);
104+
}
105+
106+
internals.log('Loaded: %s', url);
92107
return payload;
93108
}
94109
catch (err) {
95110

96111
if (err.output && err.output.statusCode === 404) {
112+
internals.log('Not found: %s', url);
97113
const error = new Error(`${repository} does not contain a ${filename}`);
98114
error.code = 'ENOENT';
99115
throw error;
@@ -152,3 +168,9 @@ exports.create = ({ path, repository, packageName }) => {
152168

153169
return internals.createPathLoader(path);
154170
};
171+
172+
173+
exports.clearCache = () => {
174+
175+
internals.cache = new Map();
176+
};

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());

0 commit comments

Comments
 (0)