SerializerCacache.js 955 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const cacache = require('cacache');
  2. class CacacheSerializer {
  3. constructor({ cacheDirPath }) {
  4. this.path = cacheDirPath;
  5. }
  6. read() {
  7. const cache = {};
  8. const promises = [];
  9. return new Promise((resolve, reject) => {
  10. cacache.ls
  11. .stream(this.path)
  12. .on('data', ({ key }) => {
  13. promises.push(
  14. cacache.get(this.path, key).then(({ data }) => {
  15. cache[key] = JSON.parse(data);
  16. }),
  17. );
  18. })
  19. .on('error', reject)
  20. .on('end', () => {
  21. resolve();
  22. });
  23. })
  24. .then(() => Promise.all(promises))
  25. .then(() => cache);
  26. }
  27. write(ops) {
  28. return Promise.all(
  29. ops.map(op => {
  30. if (op.value) {
  31. return cacache.put(this.path, op.key, JSON.stringify(op.value));
  32. } else {
  33. return cacache.rm.entry(this.path, op.key);
  34. }
  35. }),
  36. );
  37. }
  38. }
  39. module.exports = CacacheSerializer;