Skip to content

Commit

Permalink
feat(client): support for HTTP caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed Jun 19, 2020
1 parent e2904a5 commit 3b5c633
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/.vuepress/config.js
Expand Up @@ -33,7 +33,8 @@ module.exports = {
"installation",
"initialization",
"example-usage",
"configuration"
"configuration",
"caching"
]
}
],
Expand Down
43 changes: 43 additions & 0 deletions docs/guide/caching.md
@@ -0,0 +1,43 @@
# Caching

Out of the box, Joodle provides support for [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP caching. Fresh cache entries are served directly from the cache, and stale entries are revalidated with `If-None-Match`/`If-Modified-Since` headers.

## Supported Cache Stores

Joodle uses Keyv internally to support a wide range of storage adapters, including anything that implements the native JavaScript [Map API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).

This means that you can use an in-memory store:

```js
...
const inMemoryStore = new Map();

const joodle = new Joodle(
...,
{
...,
cache: inMemoryStore
}
);
```

Or you can use a more persistent store such as Redis (using [@keyv/redis](https://www.npmjs.com/package/@keyv/redis)):

```bash
$ npm install @keyv/redis
```

```js
...
const KeyvRedis = require("@keyv/redis");

const redis = new KeyvRedis("redis://user:pass@localhost:6379");

const joodle = new Joodle({
...,
{
...,
cache: redis
}
})
```
5 changes: 4 additions & 1 deletion docs/guide/configuration.md
Expand Up @@ -28,7 +28,8 @@ new Joodle(
{
timeout: 2000,
retries: 5,
rejectInvalidSSL: false
rejectInvalidSSL: false,
cache: undefined
}
);
```
Expand All @@ -39,6 +40,8 @@ The `retries` option indicates how many times the client should retry a request

The `rejectInvalidSSL` option indicates whether the client should reject invalid SSL certificates or not. By default, the client will reject invalid SSL certificates.

The `cache` option allows you to enable HTTP response caching. By default, the client will not cache any responses. See the [Caching](/guide/caching.html) section for more information.

::: danger
You should only set the `rejectInvalidSSL` option to `true` when connecting to local Moodle instances (`baseURL: "https://localhost"`). There are severe security implications when accepting invalid SSL certificates from remote Moodle sites.
:::
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -88,6 +88,7 @@
},
"dependencies": {
"@babel/runtime": "^7.9.6",
"cacheable-request": "^7.0.1",
"got": "^11.1.4",
"qs": "^6.9.4"
},
Expand Down
13 changes: 13 additions & 0 deletions src/client.ts
Expand Up @@ -5,6 +5,7 @@
* ---
*/
import got, { Got } from "got";
import CacheableRequest from "cacheable-request";

export interface ClientOptions {
/**
Expand Down Expand Up @@ -47,6 +48,14 @@ export interface HttpOptions {
* so when connecting to a local Moodle instance.
*/
rejectInvalidSSL?: boolean;

/**
* An object that implements the Map API (such as a `new Map()` or a Keyv
* instance) can be supplied here to cache requests. This caching behavior
* is compliant with RFC 7234, and uses the `If-None-Match`/`If-Modified-Since`
* HTTP headers to revalidate stale cache entries.
*/
cache?: string | false | CacheableRequest.StorageAdapter;
}

/**
Expand Down Expand Up @@ -91,6 +100,10 @@ export abstract class Client {
? httpOptions.rejectInvalidSSL
: true,
},
cache:
httpOptions && httpOptions.cache !== undefined
? httpOptions.cache
: undefined,
});
}
}
3 changes: 3 additions & 0 deletions tests/joodle.test.ts
Expand Up @@ -7,6 +7,7 @@ describe("The Joodle client class", () => {
const timeout = 5000;
const retries = 5;
const rejectInvalidSSL = false;
const cache = new Map();
let joodle: Joodle;

beforeAll(() => {
Expand All @@ -19,6 +20,7 @@ describe("The Joodle client class", () => {
timeout,
retries,
rejectInvalidSSL,
cache,
}
);

Expand Down Expand Up @@ -88,6 +90,7 @@ describe("The Joodle client class", () => {
expect(joodle.got.defaults.options.https!.rejectUnauthorized).toBe(
rejectInvalidSSL
);
expect(joodle.got.defaults.options.cache).toBeInstanceOf(Map);
});

it("should throw an error if the baseURL is not provided", () => {
Expand Down

0 comments on commit 3b5c633

Please sign in to comment.