-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: flat-cache removed own custom implementation no support for circ…
…ularJSON
- Loading branch information
Showing
6 changed files
with
226 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//This is our own implementation of flat-cache without the use of flattened as we do not need cicular JSON support | ||
//and the executable for flattened was broken | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
export class Cache { | ||
public visited = {}; | ||
public persisted = {}; | ||
public pathToFile = ''; | ||
constructor(docId: string, cacheDir?: any) { | ||
this.pathToFile = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId); | ||
if (fs.existsSync(this.pathToFile)) { | ||
this.persisted = tryParse(this.pathToFile, {}); | ||
} | ||
} | ||
|
||
public save(noPrune: boolean = false): void { | ||
!noPrune && this.prune(); | ||
writeJSON(this.pathToFile, this.persisted); | ||
} | ||
|
||
public getKey(key: string): any { | ||
this.visited[key] = true; | ||
return this.persisted[key]; | ||
} | ||
|
||
public setKey(key: string, value: any): void { | ||
this.visited[key] = true; | ||
this.persisted[key] = value; | ||
} | ||
private prune() { | ||
let obj = {}; | ||
|
||
const keys = Object.keys(this.visited); | ||
|
||
// no keys visited for either get or set value | ||
if (keys.length === 0) { | ||
return; | ||
} | ||
|
||
keys.forEach(key => { | ||
obj[key] = this.persisted[key]; | ||
}); | ||
|
||
this.visited = {}; | ||
this.persisted = obj; | ||
} | ||
} | ||
|
||
function writeJSON(filePath: string, data: any): void { | ||
fs.mkdirSync(path.dirname(filePath), { | ||
recursive: true, | ||
}); | ||
fs.writeFileSync(filePath, JSON.stringify(data)); | ||
} | ||
function tryParse(filePath: string, defaultValue: any): JSON { | ||
let result; | ||
try { | ||
result = readJSON(filePath); | ||
} catch (ex) { | ||
result = defaultValue; | ||
} | ||
return result; | ||
} | ||
|
||
export function readJSON(filePath: string): JSON { | ||
return JSON.parse( | ||
fs.readFileSync(filePath, { | ||
encoding: 'utf8', | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Requests to public API test successful workflow with and without linters 1`] = ` | ||
Object { | ||
"2": Array [ | ||
Object { | ||
"cols": Array [ | ||
8, | ||
27, | ||
], | ||
"fingerprints": Array [ | ||
Object { | ||
"fingerprint": "f8e3391465a47f6586489cffd1f44ae47a1c4885c722de596d6eb931fe43bb16", | ||
"version": 0, | ||
}, | ||
], | ||
"markers": Array [], | ||
"rows": Array [ | ||
5, | ||
5, | ||
], | ||
}, | ||
], | ||
"3": Array [ | ||
Object { | ||
"cols": Array [ | ||
6, | ||
25, | ||
], | ||
"fingerprints": Array [ | ||
Object { | ||
"fingerprint": "3e40a81739245db8fff4903a7e28e08bffa03486a677e7c91594cfdf15fb5a1d", | ||
"version": 0, | ||
}, | ||
Object { | ||
"fingerprint": "57664a44.2c254dac.98501263.9e345555.da547a36.9509b717.a713c1c8.45d76bdf.57664a44.2c254dac.98501263.9e345555.da547a36.9509b717.a713c1c8.45d76bdf", | ||
"version": 1, | ||
}, | ||
], | ||
"markers": Array [ | ||
Object { | ||
"msg": Array [ | ||
25, | ||
36, | ||
], | ||
"pos": Array [ | ||
Object { | ||
"cols": Array [ | ||
7, | ||
14, | ||
], | ||
"file": "/AnnotatorTest.cpp", | ||
"rows": Array [ | ||
8, | ||
8, | ||
], | ||
}, | ||
], | ||
}, | ||
Object { | ||
"msg": Array [ | ||
45, | ||
57, | ||
], | ||
"pos": Array [ | ||
Object { | ||
"cols": Array [ | ||
6, | ||
25, | ||
], | ||
"file": "/AnnotatorTest.cpp", | ||
"rows": Array [ | ||
10, | ||
10, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
"rows": Array [ | ||
10, | ||
10, | ||
], | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import path from 'path'; | ||
import write from 'write'; | ||
import fs from 'fs'; | ||
import { Cache, readJSON } from '../src/cache'; | ||
describe('Cache', () => { | ||
afterAll(() => { | ||
const dir = path.resolve(__dirname, '../fixtures'); | ||
fs.rmdir(dir, { recursive: true }, err => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
it('should not crash if the cache file exists but it is an empty string', function () { | ||
const cachePath = path.resolve(__dirname, '../fixtures/.cache2'); | ||
write.sync(path.join(cachePath, 'someId'), ''); | ||
|
||
expect(function () { | ||
const cache = new Cache('someId', cachePath); | ||
expect(cache.persisted).toEqual({}); | ||
}).not.toThrow(Error); | ||
}); | ||
|
||
it('should not crash if the cache file exists but it is an invalid JSON string', function () { | ||
const cachePath = path.resolve(__dirname, '../fixtures/.cache2'); | ||
write.sync(path.join(cachePath, 'someId'), '{ "foo": "fookey", "bar" '); | ||
|
||
expect(function () { | ||
const cache = new Cache('someId', cachePath); | ||
expect(cache.persisted).toEqual({}); | ||
}).not.toThrow(Error); | ||
}); | ||
|
||
describe('loading an existing cache custom directory', function () { | ||
beforeEach(function () { | ||
const cache = new Cache('someId', path.resolve(__dirname, '../fixtures/.cache2')); | ||
cache.setKey('foo', { | ||
bar: 'baz', | ||
}); | ||
cache.setKey('bar', { | ||
foo: 'baz', | ||
}); | ||
cache.save(); | ||
}); | ||
|
||
it('should load an existing cache', function () { | ||
const cache = new Cache('someId', path.resolve(__dirname, '../fixtures/.cache2')); | ||
expect(readJSON(path.resolve(__dirname, '../fixtures/.cache2/someId'))).toEqual(cache.persisted); | ||
}); | ||
|
||
it('should return the same structure if load called twice with the same docId', function () { | ||
const cache = new Cache('someId', path.resolve(__dirname, '../fixtures/.cache2')); | ||
const cache2 = new Cache('someId', path.resolve(__dirname, '../fixtures/.cache2')); | ||
|
||
expect(cache.persisted).toEqual(cache2.persisted); | ||
}); | ||
}); | ||
}); |