45f48c6e5e1bc2c79b683dcf28fc69a56ec76310.svn-base
5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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)
> Wraps a transform and provides caching.
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.
## Install
```
$ npm install caching-transform
```
## Usage
```js
const cachingTransform = require('caching-transform');
const transform = cachingTransform({
cacheDir: '/path/to/cache/directory',
salt: 'hash-salt',
transform: (input, metadata, hash) => {
// ... Expensive operations ...
return transformedResult;
}
});
transform('some input for transpilation')
// => fetch from the cache,
// or run the transform and save to the cache if not found there
```
## API
### cachingTransform(options)
Returns a transform callback that takes two arguments:
- `input` a string to be transformed
- `metadata` an arbitrary data object
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.
#### options
##### salt
Type: `string` `Buffer`<br>
Default: `''`
A value that uniquely identifies your transform:
```js
const pkg = require('my-transform/package.json');
const salt = pkg.name + ':' + pkg.version;
```
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.
##### transform
Type: `Function(input: string|Buffer, metadata: *, hash: string): string|Buffer`
- `input`: The value to be transformed. It is passed through from the wrapper.
- `metadata`: An arbitrary data object passed through from the wrapper. A typical value might be a string filename.
- `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).
The transform function will return a `string` (or Buffer if `encoding === 'buffer'`) containing the result of transforming `input`.
##### factory
Type: `Function(cacheDir: string): transformFunction`
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.
A typical usage would be to prevent eagerly `require`ing expensive dependencies like Babel:
```js
function factory() {
// Using the factory function, you can avoid loading Babel until you are sure it is needed.
const babel = require('babel-core');
return (code, metadata) => {
return babel.transform(code, {filename: metadata.filename, plugins: [/* ... */]});
};
}
```
##### cacheDir
*Required unless caching is disabled*<br>
Type: `string`
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.
##### ext
Type: `string`<br>
Default: `''`
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.
##### shouldTransform
Type: `Function(input: string|Buffer, additionalData: *)`<br>
Default: Always transform
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.
##### disableCache
Type: `boolean`<br>
Default: `false`
If `true`, the cache is ignored and the transform is used every time regardless of cache contents.
##### hashData
Type: `Function(input: string|Buffer, metadata: *): string|Buffer|Array[string|Buffer]`
Provide additional data that should be included in the hash.
One potential use is including the `metadata` in the hash by coercing it to a hashable string or buffer:
```js
function hashData(input, metadata) {
return JSON.stringify(metadata);
}
```
(Note that `metadata` is not taken into account otherwise.)
##### onHash
Type: `Function(input: string|Buffer, metadata: *, hash: string)`
Function that is called after input is hashed.
##### encoding
Type: `string`<br>
Default: `'utf8'`
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.
## License
MIT © [James Talmage](https://github.com/jamestalmage)