Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Commit 4fb8478

Browse files
authored
Read only option (#61)
* docs(na): add new readOnly option to the readme file * docs(na): reapply default configs for readme on tests * chore(na): add readOnly option to the schema-utils validate function * feat(na): added readOnly option defaults * feat(na): enable readOnly mode for cache loader * docs(na): explanation for readOnly option under loader function * docs(na): explanation for readOnly option under pitch function
1 parent e764087 commit 4fb8478

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
| **`cacheIdentifier`** | `{String}` | `cache-loader:{version} {process.env.NODE_ENV}` | Provide an invalidation identifier which is used to generate the hashes. You can use it for extra dependencies of loaders (used for default read/write implementation) |
5555
| **`write`** | `{Function(cacheKey, data, callback) -> {void}}` | `undefined` | Allows you to override default write cache data to file (e.g. Redis, memcached) |
5656
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
57+
| **`readOnly`** | `{Boolean}` | `false` | Allows you to override default value and make the cache read only (useful for some environments where you don't want the cache to be updated, only read from it) |
5758

5859
## Examples
5960

src/index.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const defaults = {
2323
cacheIdentifier: `cache-loader:${pkg.version} ${env}`,
2424
cacheKey,
2525
read,
26+
readOnly: false,
2627
write,
2728
};
2829

@@ -46,10 +47,16 @@ function pathWithCacheContext(cacheContext, originalPath) {
4647

4748
function loader(...args) {
4849
const options = Object.assign({}, defaults, getOptions(this));
49-
5050
validateOptions(schema, options, 'Cache Loader');
5151

52-
const { write: writeFn } = options;
52+
const { readOnly, write: writeFn } = options;
53+
54+
// In case we are under a readOnly mode on cache-loader
55+
// we don't want to write or update any cache file
56+
if (readOnly) {
57+
this.callback(null, ...args);
58+
return;
59+
}
5360

5461
const callback = this.async();
5562
const { data } = this;
@@ -124,7 +131,12 @@ function pitch(remainingRequest, prevRequest, dataInput) {
124131

125132
validateOptions(schema, options, 'Cache Loader (Pitch)');
126133

127-
const { read: readFn, cacheContext, cacheKey: cacheKeyFn } = options;
134+
const {
135+
read: readFn,
136+
readOnly,
137+
cacheContext,
138+
cacheKey: cacheKeyFn,
139+
} = options;
128140

129141
const callback = this.async();
130142
const data = dataInput;
@@ -150,6 +162,15 @@ function pitch(remainingRequest, prevRequest, dataInput) {
150162
eachCallback(statErr);
151163
return;
152164
}
165+
166+
// When we are under a readOnly config on cache-loader
167+
// we don't want to emit any other error than a
168+
// file stat error
169+
if (readOnly) {
170+
eachCallback();
171+
return;
172+
}
173+
153174
if (stats.mtime.getTime() !== dep.mtime) {
154175
eachCallback(true);
155176
return;

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"read": {
1717
"instanceof": "Function"
1818
},
19+
"readOnly": {
20+
"type": "boolean"
21+
},
1922
"write": {
2023
"instanceof": "Function"
2124
}

0 commit comments

Comments
 (0)