Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/api/__tests__/cache-tmp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jest.mock('find-cache-dir', () => {
return () => undefined;
});

const os = require('os');
const Cache = require('../src/cache');

// Since this test is mocking out the `find-cache-dir` module for a single test, it needs to be run separately from the
// rest of the cache tests, otherwise all of those tests would use this mocked out version.
test('should fallback to an os-level temp directory if a cache directory cannot be determined', () => {
const dir = os.tmpdir();
const cacheStore = new Cache('http://example.com/readme.json');

expect(cacheStore.dir).toMatch(dir);
expect(cacheStore.cacheStore).toMatch(dir);
expect(cacheStore.specsCache).toMatch(dir);
});
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"form-data": "^3.0.0",
"get-stream": "^6.0.0",
"js-yaml": "^3.14.0",
"make-dir": "^3.1.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is already coming in via find-cache-dir, so I'm using that instead of loading in something else like mktemp.

"mimer": "^1.1.0",
"node-fetch": "^2.6.0"
},
Expand Down
30 changes: 20 additions & 10 deletions packages/api/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ const crypto = require('crypto');
const findCacheDir = require('find-cache-dir');
const pkg = require('../package.json');
const fs = require('fs');
const os = require('os');
const path = require('path');

const cacheDir = findCacheDir({ name: pkg.name, thunk: true });
const makeDir = require('make-dir');

let cacheDir = findCacheDir({ name: pkg.name });
if (typeof cacheDir === 'undefined') {
// The `find-cache-dir` module returns `undefined` if the `node_modules/` directory isn't writable, or there's no
// `package.json` in the root-most directory. If this happens, we can instead adhoc create a cache directory in the
// users OS temp directory and store our data there.
//
// @link https://github.com/avajs/find-cache-dir/issues/29
cacheDir = makeDir.sync(path.join(os.tmpdir(), pkg.name));
}

class SdkCache {
constructor(uri) {
this.uri = uri;
this.uriHash = SdkCache.getCacheHash(this.uri);
this.dir = cacheDir();
this.cacheStore = cacheDir('cache.json');
this.specsCache = cacheDir('specs');
this.dir = cacheDir;
this.cacheStore = path.join(this.dir, 'cache.json');
this.specsCache = path.join(this.dir, 'specs');

// This should default to false so we have awareness if we've looked at the cache yet.
this.cached = false;
Expand Down Expand Up @@ -123,18 +133,18 @@ class SdkCache {
}

const cache = self.getCache();
if (!(this.uriHash in cache)) {
if (!(self.uriHash in cache)) {
const saved = JSON.stringify(spec, null, 2);
const jsonHash = crypto.createHash('md5').update(saved).digest('hex');

cache[this.uriHash] = {
path: cacheDir('specs', `${jsonHash}.json`),
original: this.uri,
cache[self.uriHash] = {
path: path.join(self.specsCache, `${jsonHash}.json`),
original: self.uri,
title: 'title' in spec.info ? spec.info.title : undefined,
version: 'version' in spec.info ? spec.info.version : undefined,
};

fs.writeFileSync(cache[this.uriHash].path, saved);
fs.writeFileSync(cache[self.uriHash].path, saved);
fs.writeFileSync(self.cacheStore, JSON.stringify(cache, null, 2));

self.cache = cache;
Expand Down