Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const hasha = require('hasha');
  5. const makeDir = require('make-dir');
  6. const writeFileAtomic = require('write-file-atomic');
  7. const packageHash = require('package-hash');
  8. let ownHash = '';
  9. function getOwnHash() {
  10. ownHash = packageHash.sync(path.join(__dirname, 'package.json'));
  11. return ownHash;
  12. }
  13. function wrap(opts) {
  14. if (!(opts.factory || opts.transform) || (opts.factory && opts.transform)) {
  15. throw new Error('Specify factory or transform but not both');
  16. }
  17. if (typeof opts.cacheDir !== 'string' && !opts.disableCache) {
  18. throw new Error('cacheDir must be a string');
  19. }
  20. opts = {
  21. ext: '',
  22. salt: '',
  23. hashData: () => [],
  24. filenamePrefix: () => '',
  25. onHash: () => {},
  26. ...opts
  27. };
  28. let transformFn = opts.transform;
  29. const {factory, cacheDir, shouldTransform, disableCache, hashData, onHash, filenamePrefix, ext, salt} = opts;
  30. const cacheDirCreated = opts.createCacheDir === false;
  31. let created = transformFn && cacheDirCreated;
  32. const encoding = opts.encoding === 'buffer' ? undefined : opts.encoding || 'utf8';
  33. function transform(input, metadata, hash) {
  34. if (!created) {
  35. if (!cacheDirCreated && !disableCache) {
  36. makeDir.sync(cacheDir);
  37. }
  38. if (!transformFn) {
  39. transformFn = factory(cacheDir);
  40. }
  41. created = true;
  42. }
  43. return transformFn(input, metadata, hash);
  44. }
  45. return function (input, metadata) {
  46. if (shouldTransform && !shouldTransform(input, metadata)) {
  47. return input;
  48. }
  49. if (disableCache) {
  50. return transform(input, metadata);
  51. }
  52. const data = [
  53. ownHash || getOwnHash(),
  54. input,
  55. salt,
  56. ...[].concat(hashData(input, metadata))
  57. ];
  58. const hash = hasha(data, {algorithm: 'sha256'});
  59. const cachedPath = path.join(cacheDir, filenamePrefix(metadata) + hash + ext);
  60. onHash(input, metadata, hash);
  61. let result;
  62. let retry = 0;
  63. /* eslint-disable-next-line no-constant-condition */
  64. while (true) {
  65. try {
  66. return fs.readFileSync(cachedPath, encoding);
  67. } catch (readError) {
  68. if (!result) {
  69. result = transform(input, metadata, hash);
  70. }
  71. try {
  72. writeFileAtomic.sync(cachedPath, result, {encoding});
  73. return result;
  74. } catch (error) {
  75. /* Likely https://github.com/npm/write-file-atomic/issues/28
  76. * Make up to 3 attempts to read or write the cache. */
  77. retry++;
  78. if (retry > 3) {
  79. throw error;
  80. }
  81. }
  82. }
  83. }
  84. };
  85. }
  86. module.exports = wrap;