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

Read only option #61

Merged
merged 7 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = {
| **`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) |
| **`write`** | `{Function(cacheKey, data, callback) -> {void}}` | `undefined` | Allows you to override default write cache data to file (e.g. Redis, memcached) |
| **`read`** | `{Function(cacheKey, callback) -> {void}}` | `undefined` | Allows you to override default read cache data from file |
| **`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) |

## Examples

Expand Down
27 changes: 24 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaults = {
cacheIdentifier: `cache-loader:${pkg.version} ${env}`,
cacheKey,
read,
readOnly: false,
write,
};

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

function loader(...args) {
const options = Object.assign({}, defaults, getOptions(this));

validateOptions(schema, options, 'Cache Loader');

const { write: writeFn } = options;
const { readOnly, write: writeFn } = options;

// In case we are under a readOnly mode on cache-loader
// we don't want to write or update any cache file
if (readOnly) {
this.callback(null, ...args);
return;
}

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

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

const { read: readFn, cacheContext, cacheKey: cacheKeyFn } = options;
const {
read: readFn,
readOnly,
cacheContext,
cacheKey: cacheKeyFn,
} = options;

const callback = this.async();
const data = dataInput;
Expand All @@ -150,6 +162,15 @@ function pitch(remainingRequest, prevRequest, dataInput) {
eachCallback(statErr);
return;
}

// When we are under a readOnly config on cache-loader
// we don't want to emit any other error than a
// file stat error
if (readOnly) {
eachCallback();
return;
}

if (stats.mtime.getTime() !== dep.mtime) {
eachCallback(true);
return;
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"read": {
"instanceof": "Function"
},
"readOnly": {
"type": "boolean"
},
"write": {
"instanceof": "Function"
}
Expand Down