Skip to content

Commit 1f42601

Browse files
Removed the caching options. All files are now cached, and the entire cache is reset for each new parse operation.
1 parent 2d5b52c commit 1f42601

File tree

7 files changed

+50
-197
lines changed

7 files changed

+50
-197
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/d
5050

5151
- Use **JSON** or **YAML** schemas — or even a mix of both!
5252
- Fully supports `$ref` pointers to external files and URLs
53-
- Configurable caching of referenced files
5453
- Can [bundle](docs/ref-parser.md#bundlepath-options-callback) multiple files into a single schema that only has _internal_ `$ref` pointers
5554
- Can [dereference](docs/ref-parser.md#dereferencepath-options-callback) your schema, producing a plain-old JavaScript object that's easy to work with
5655
- Supports [circular references](docs/README.md#circular-refs), nested references, back-references, and cross-references between files

docs/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Classes & Methods
2323
- [`circular`](refs.md#circular)
2424
- [`paths()`](refs.md#pathstypes)
2525
- [`values()`](refs.md#valuestypes)
26-
- [`isExpired()`](refs.md#isexpiredref)
27-
- [`expire()`](refs.md#expireref)
2826
- [`exists()`](refs.md#existsref)
2927
- [`get()`](refs.md#getref-options)
3028
- [`set()`](refs.md#setref-value-options)
@@ -50,7 +48,7 @@ var parser = new $RefParser();
5048
parser.bundle("my-schema.json");
5149
```
5250

53-
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function. Also, having a `$RefParser` instance allows you to benefit from **[caching](options.md#cache)**, so the next time you call [`parser.resolve()`](ref-parser.md#resolveschema-options-callback), it won't need to re-download those files again (as long as the cache hasn't expired).
51+
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function.
5452

5553

5654
### Callbacks vs. Promises

docs/options.md

+11-30
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ $RefParser.dereference("my-schema.yaml", {
2323
resolve: {
2424
file: false, // Don't resolve local file references
2525
http: {
26-
cache: 30000, // Cache downloaded files for 30 seconds
2726
timeout: 2000 // 2 second timeout
28-
}
27+
}
2928
},
3029
dereference: {
3130
circular: false // Don't allow circular $refs
@@ -49,7 +48,7 @@ $RefParser.dereference("my-schema.yaml", { parse: { json: false } });
4948
#### `order`
5049
Parsers run in a specific order, relative to other parsers. For example, a parser with `order: 5` will run _before_ a parser with `order: 10`. If a parser is unable to successfully parse a file, then the next parser is tried, until one succeeds or they all fail.
5150

52-
You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add your own custom parser that you want to run _first_.
51+
You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add your own custom parser that you want to run _first_.
5352

5453
```javascript
5554
// Run the plain-text parser first
@@ -64,11 +63,11 @@ Multiple parsers can contain the same file extensions. For example, both the JSO
6463
You can set your own file extensions for any parser. Each extension can be a string or a regular expression to test against the _full file path_. Here's an example:
6564

6665
```javascript
67-
$RefParser.dereference("my-schema.yaml", {
68-
parse: {
69-
text: {
66+
$RefParser.dereference("my-schema.yaml", {
67+
parse: {
68+
text: {
7069
// parse text, html, and readme files as plain-text
71-
ext: [".txt", ".html", /docs\/README/i]
70+
ext: [".txt", ".html", /docs\/README/i]
7271
}
7372
}
7473
});
@@ -103,7 +102,7 @@ Here is a simple example of a custom parser. For more complex examples refer to
103102

104103
// This parser only parses .foo files
105104
myCustomParser.ext = '.foo';
106-
105+
107106
// This parser runs first (before any other parsers)
108107
myCustomParser.order = 1;
109108

@@ -129,28 +128,13 @@ $RefParser.dereference("my-schema.yaml", { resolve: { http: false } });
129128
#### `order`
130129
Resolvers run in a specific order, relative to other resolvers. For example, a resolver with `order: 5` will run _before_ a resolver with `order: 10`. If a resolver is unable to successfully resolve a path, then the next resolver is tried, until one succeeds or they all fail.
131130

132-
You can change the order in which resolvers run, which is useful if you know that most of your file references will be a certain type, or if you add your own custom resolver that you want to run _first_.
131+
You can change the order in which resolvers run, which is useful if you know that most of your file references will be a certain type, or if you add your own custom resolver that you want to run _first_.
133132

134133
```javascript
135134
// Run the HTTP resolver first
136135
$RefParser.dereference("my-schema.yaml", { resolve: { http: { order: 1 } } });
137136
```
138137

139-
#### `cache`
140-
JSON Schema $Ref Parser can automatically cache files, so they don't need to be re-downloaded or re-read every time. You can specify a different cache duration (in milliseconds) for each resolver, or you can disable caching for a given resolver by setting the duration to zero.
141-
142-
```javascript
143-
$RefParser.dereference("my-schema.yaml", {
144-
resolve: {
145-
// Cache downloaded files for 30 seconds
146-
http: { cache: 30000 },
147-
148-
// Don't cache local files (re-read them every time)
149-
file: { cache: 0 }
150-
}
151-
});
152-
```
153-
154138
#### Resolver-specific options
155139
Resolvers can have other options that are specific to that resolver. For example, the [HTTP resolver](../lib/read/http.js) has options that allow you to customize the HTTP headers, timeout, credentials, etc.
156140

@@ -166,13 +150,13 @@ To add your own custom resolver, just define a function that accepts the followi
166150
Here is a simple example of a custom resolver. For more complex examples refer to any of [the built-in resolvers](../lib/read).
167151

168152
```javascript
169-
// A custom resolver that reads from a MongoDB database
153+
// A custom resolver that reads from a MongoDB database
170154
function mongoDb(path, options, callback) {
171155
// If it's not a MongoDB URL, then error-out, so the next resolver can be tried
172156
if (path.substr(0, 10) !== "mongodb://") {
173157
callback("Not a MongoDB URL");
174158
}
175-
159+
176160
mongoClient.connect(path, function(err, db) {
177161
if (err) {
178162
callback(err);
@@ -185,7 +169,7 @@ Here is a simple example of a custom resolver. For more complex examples refer
185169

186170
// This parser only parses .foo files
187171
myCustomParser.ext = '.foo';
188-
172+
189173
// This parser runs first (before any other parsers)
190174
myCustomParser.order = 1;
191175

@@ -203,9 +187,6 @@ Here is a simple example of a custom resolver. For more complex examples refer
203187
|Option |Type |Default |Description
204188
|:---------------------|:--------|:---------|:----------
205189
|`$refs.circular` |bool or "ignore" |true |Determines whether [circular `$ref` pointers](README.md#circular-refs) are allowed. If `false`, then a `ReferenceError` will be thrown if the schema contains a circular reference.<br><br> If set to `"ignore"`, then circular references will _not_ be dereferenced, even when calling [`dereference()`](ref-parser.md#dereferenceschema-options-callback). No error will be thrown, but the [`$Refs.circular`](refs.md#circular) property will still be set to `true`.
206-
|`cache.fs` |number |60 |<a name="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
207-
|`cache.http` |number |300 |The length of time (in seconds) to cache HTTP URLs. The default is five minutes. Setting to zero will cache forever.
208-
|`cache.https` |number |300 |The length of time (in seconds) to cache HTTPS URLs. The default is five minutes. Setting to zero will cache forever.
209190
|`http.headers` |object |`{}` |HTTP to send when downloading files<br> (note: [some headers are protected](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name) and cannot be set)
210191
|`http.timeout` |number |5000 |The HTTP request timeout (in milliseconds)
211192
|`http.redirects` |number |5 |The maximum number of HTTP redirects to follow. To disable automatic following of redirects, set this to zero.

docs/refs.md

-43
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ This object is a map of JSON References and their resolved values. It also has
1212
##### Methods
1313
- [`paths()`](#pathstypes)
1414
- [`values()`](#valuestypes)
15-
- [`isExpired()`](#isexpiredref)
16-
- [`expire()`](#expireref)
1715
- [`exists()`](#existsref)
1816
- [`get()`](#getref)
1917
- [`set()`](#setref-value)
@@ -80,47 +78,6 @@ $RefParser.resolve("my-schema.json")
8078
```
8179

8280

83-
### `isExpired($ref)`
84-
85-
- **$ref** (_required_) - `string`<br>
86-
The JSON Reference path, optionally with a JSON Pointer in the hash
87-
88-
- **Return Value:** `boolean`<br>
89-
Returns `true` if the given JSON reference has expired (or if it doesn't exist); otherwise, returns `false`
90-
91-
```javascript
92-
$RefParser.resolve("my-schema.json")
93-
.then(function($refs) {
94-
// Hasn't expired yet
95-
$refs.isExpired("schemas/places.yaml"); // => false
96-
97-
// Check again after 10 minutes
98-
setTimeout(function() {
99-
$refs.isExpired("schemas/places.yaml"); // => true
100-
}, 600000);
101-
});
102-
```
103-
104-
105-
### `expire($ref)`
106-
107-
- **$ref** (_required_) - `string`<br>
108-
The JSON Reference path, optionally with a JSON Pointer in the hash
109-
110-
Immediately expires the given JSON reference, so the next time you call a method such as [`parse`](ref-parser.md#parseschema-options-callback) or [`dereference`](ref-parser.md#dereferenceschema-options-callback), the file will be refreshed rather than reusing the cached value.
111-
112-
```javascript
113-
$RefParser.resolve("my-schema.json")
114-
.then(function($refs) {
115-
$refs.isExpired("schemas/places.yaml"); // => false
116-
117-
$refs.expire("schemas/places.yaml");
118-
119-
$refs.isExpired("schemas/places.yaml"); // => true
120-
});
121-
```
122-
123-
12481
### `exists($ref)`
12582

12683
- **$ref** (_required_) - `string`<br>

lib/options.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var jsonParser = require('./parsers/json'),
1111
module.exports = $RefParserOptions;
1212

1313
/**
14-
* Options that determine how JSON schemas are parsed, dereferenced, and cached.
14+
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
1515
*
1616
* @param {object|$RefParserOptions} [options] - Overridden options
1717
* @constructor
@@ -39,12 +39,12 @@ $RefParserOptions.defaults = {
3939
* "Empty" includes zero-byte files, as well as JSON/YAML files that
4040
* don't contain any keys.
4141
*/
42-
this.parse = {
42+
parse: {
4343
json: jsonParser,
4444
yaml: yamlParser,
4545
text: textParser,
4646
binary: binaryParser,
47-
};
47+
},
4848

4949
/**
5050
* Determines how JSON References will be resolved.
@@ -56,13 +56,9 @@ $RefParserOptions.defaults = {
5656
*
5757
* order {number} - The order in which the resolver will run
5858
*
59-
* cache {number} - How long to cache files (in milliseconds)
60-
* The default cache duration is different for each resolver.
61-
* Setting the cache duration to zero disables caching for that resolver.
62-
*
6359
* The HTTP resolver has additional options. See read/http.js for details.
6460
*/
65-
this.resolve = {
61+
resolve: {
6662
file: fileResolver,
6763
http: httpResolver,
6864

@@ -74,12 +70,12 @@ $RefParserOptions.defaults = {
7470
* @type {boolean}
7571
*/
7672
external: true,
77-
};
73+
},
7874

7975
/**
8076
* Determines the types of JSON references that are allowed.
8177
*/
82-
this.dereference = {
78+
dereference: {
8379
/**
8480
* Dereference circular (recursive) JSON references?
8581
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
@@ -88,11 +84,11 @@ $RefParserOptions.defaults = {
8884
* @type {boolean|string}
8985
*/
9086
circular: true
91-
};
87+
},
9288
};
9389

9490
/**
95-
* Merges the specified options into the target object.
91+
* Merges the properties of the source object into the target object.
9692
*
9793
* @param {object} target - The object that we're populating
9894
* @param {?object} source - The options that are being merged

lib/ref.js

+12-68
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,14 @@
22

33
module.exports = $Ref;
44

5-
var Pointer = require('./pointer'),
6-
url = require('./util/url');
5+
var Pointer = require('./pointer');
76

87
/**
98
* This class represents a single JSON reference and its resolved value.
109
*
11-
* @param {$Refs} $refs
12-
* @param {string} path
1310
* @constructor
1411
*/
15-
function $Ref($refs, path) {
16-
path = url.stripHash(path);
17-
18-
// Add this $ref to its parent collection
19-
$refs._$refs[path] = this;
20-
21-
/**
22-
* The {@link $Refs} object that contains this {@link $Ref} object.
23-
* @type {$Refs}
24-
*/
25-
this.$refs = $refs;
26-
12+
function $Ref() {
2713
/**
2814
* The file path or URL of the referenced file.
2915
* This path is relative to the path of the main JSON schema file.
@@ -34,13 +20,7 @@ function $Ref($refs, path) {
3420
*
3521
* @type {string}
3622
*/
37-
this.path = path;
38-
39-
/**
40-
* Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
41-
* @type {?string}
42-
*/
43-
this.pathType = undefined;
23+
this.path = undefined;
4424

4525
/**
4626
* The resolved value of the JSON reference.
@@ -50,53 +30,17 @@ function $Ref($refs, path) {
5030
this.value = undefined;
5131

5232
/**
53-
* The date/time that the cached value will expire.
54-
* @type {?Date}
33+
* The {@link $Refs} object that contains this {@link $Ref} object.
34+
* @type {$Refs}
5535
*/
56-
this.expires = undefined;
57-
}
58-
59-
/**
60-
* Determines whether the {@link $Ref#value} has expired.
61-
*
62-
* @returns {boolean}
63-
*/
64-
$Ref.prototype.isExpired = function() {
65-
// If no expiration has been set yet (i.e. `this.expires === undefined`) then it's NOT expried
66-
return !!(this.expires && this.expires <= Date.now());
67-
};
68-
69-
/**
70-
* Immediately expires the {@link $Ref#value}.
71-
*/
72-
$Ref.prototype.expire = function() {
73-
this.expires = new Date();
74-
};
36+
this.$refs = undefined;
7537

76-
/**
77-
* Sets the {@link $Ref#expires}, based on the {@link $Ref#pathType}.
78-
*
79-
* @param {$RefParserOptions} options
80-
*/
81-
$Ref.prototype.setExpiration = function(options) {
82-
var resolver = options.resolve[this.pathType];
83-
if (resolver) {
84-
var cacheDuration = resolver.cache;
85-
if (cacheDuration > 0) {
86-
// Extend the cache expiration
87-
var expires = Date.now() + cacheDuration;
88-
this.expires = new Date(expires);
89-
}
90-
else {
91-
// Expire immediately
92-
this.expires = new Date();
93-
}
94-
}
95-
else {
96-
// Never expire
97-
this.expires = undefined;
98-
}
99-
};
38+
/**
39+
* Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
40+
* @type {?string}
41+
*/
42+
this.pathType = undefined;
43+
}
10044

10145
/**
10246
* Determines whether the given JSON reference exists within this {@link $Ref#value}.

0 commit comments

Comments
 (0)