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.

readme.md 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # caching-transform [![Build Status](https://travis-ci.org/istanbuljs/caching-transform.svg?branch=master)](https://travis-ci.org/istanbuljs/caching-transform) [![Coverage Status](https://coveralls.io/repos/github/istanbuljs/caching-transform/badge.svg?branch=master)](https://coveralls.io/github/istanbuljs/caching-transform?branch=master)
  2. > Wraps a transform and provides caching.
  3. Caching transform results can greatly improve performance. `nyc` saw [dramatic performance increases](https://github.com/bcoe/nyc/pull/101#issuecomment-165716069) when we implemented caching.
  4. ## Install
  5. ```
  6. $ npm install caching-transform
  7. ```
  8. ## Usage
  9. ```js
  10. const cachingTransform = require('caching-transform');
  11. const transform = cachingTransform({
  12. cacheDir: '/path/to/cache/directory',
  13. salt: 'hash-salt',
  14. transform: (input, metadata, hash) => {
  15. // ... Expensive operations ...
  16. return transformedResult;
  17. }
  18. });
  19. transform('some input for transpilation')
  20. // => fetch from the cache,
  21. // or run the transform and save to the cache if not found there
  22. ```
  23. ## API
  24. ### cachingTransform(options)
  25. Returns a transform callback that takes two arguments:
  26. - `input` a string to be transformed
  27. - `metadata` an arbitrary data object
  28. Both arguments are passed to the wrapped transform. Results are cached in the cache directory using an `sha256` hash of `input` and an optional `salt` value. If a cache entry already exist for `input`, the wrapped transform function will never be called.
  29. #### options
  30. ##### salt
  31. Type: `string` `Buffer`<br>
  32. Default: `''`
  33. A value that uniquely identifies your transform:
  34. ```js
  35. const pkg = require('my-transform/package.json');
  36. const salt = pkg.name + ':' + pkg.version;
  37. ```
  38. Including the version in the salt ensures existing cache entries will be automatically invalidated when you bump the version of your transform. If your transform relies on additional dependencies, and the transform output might change as those dependencies update, then your salt should incorporate the versions of those dependencies as well.
  39. ##### transform
  40. Type: `Function(input: string|Buffer, metadata: *, hash: string): string|Buffer`
  41. - `input`: The value to be transformed. It is passed through from the wrapper.
  42. - `metadata`: An arbitrary data object passed through from the wrapper. A typical value might be a string filename.
  43. - `hash`: The salted hash of `input`. Useful if you intend to create additional cache entries beyond the transform result (i.e. `nyc` also creates cache entries for source-map data). This value is not available if the cache is disabled, if you still need it, the default can be computed via [`hasha([input, salt])`](https://www.npmjs.com/package/hasha).
  44. The transform function will return a `string` (or Buffer if `encoding === 'buffer'`) containing the result of transforming `input`.
  45. ##### factory
  46. Type: `Function(cacheDir: string): transformFunction`
  47. If the `transform` function is expensive to create, and it is reasonable to expect that it may never be called during the life of the process, you may supply a `factory` function that will be used to create the `transform` function the first time it is needed.
  48. A typical usage would be to prevent eagerly `require`ing expensive dependencies like Babel:
  49. ```js
  50. function factory() {
  51. // Using the factory function, you can avoid loading Babel until you are sure it is needed.
  52. const babel = require('babel-core');
  53. return (code, metadata) => {
  54. return babel.transform(code, {filename: metadata.filename, plugins: [/* ... */]});
  55. };
  56. }
  57. ```
  58. ##### cacheDir
  59. *Required unless caching is disabled*<br>
  60. Type: `string`
  61. The directory where cached transform results will be stored. The directory is automatically created with [`mkdirp`](https://www.npmjs.com/package/mkdirp). You can set `options.createCacheDir = false` if you are certain the directory already exists.
  62. ##### ext
  63. Type: `string`<br>
  64. Default: `''`
  65. An extension that will be appended to the salted hash to create the filename inside your cache directory. It is not required, but recommended if you know the file type. Appending the extension allows you to easily inspect the contents of the cache directory with your file browser.
  66. ##### shouldTransform
  67. Type: `Function(input: string|Buffer, additionalData: *)`<br>
  68. Default: Always transform
  69. A function that examines `input` and `metadata` to determine whether the transform should be applied. Returning `false` means the transform will not be applied and `input` will be returned unmodified.
  70. ##### disableCache
  71. Type: `boolean`<br>
  72. Default: `false`
  73. If `true`, the cache is ignored and the transform is used every time regardless of cache contents.
  74. ##### hashData
  75. Type: `Function(input: string|Buffer, metadata: *): string|Buffer|Array[string|Buffer]`
  76. Provide additional data that should be included in the hash.
  77. One potential use is including the `metadata` in the hash by coercing it to a hashable string or buffer:
  78. ```js
  79. function hashData(input, metadata) {
  80. return JSON.stringify(metadata);
  81. }
  82. ```
  83. (Note that `metadata` is not taken into account otherwise.)
  84. ##### filenamePrefix
  85. Type: `Function(metadata: *): string`
  86. Provide a filename to prefix the cache entry. The return value may not contain any path separators.
  87. ```js
  88. function filenamePrefix(metadata) {
  89. return path.parse(metadata.filename || '').name + '-';
  90. }
  91. ```
  92. ##### onHash
  93. Type: `Function(input: string|Buffer, metadata: *, hash: string)`
  94. Function that is called after input is hashed.
  95. ##### encoding
  96. Type: `string`<br>
  97. Default: `'utf8'`
  98. The encoding to use when writing to / reading from the filesystem. If set it to `buffer`, then buffers will be returned from the cache instead of strings.
  99. ## License
  100. MIT © [James Talmage](https://github.com/jamestalmage)