Compare commits
1 Commits
master
...
mistval-pa
Author | SHA1 | Date | |
---|---|---|---|
|
ea30b6b38e |
@ -1,23 +0,0 @@
|
|||||||
name: CI Pipeline
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-22.04
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
node-version: [20.x]
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version: ${{ matrix.node-version }}
|
|
||||||
registry-url: "https://registry.npmjs.org"
|
|
||||||
- run: npm ci
|
|
||||||
- run: npm publish
|
|
||||||
env:
|
|
||||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
21
.github/workflows/ci.yml
vendored
Normal file
21
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
name: CI Pipeline
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
node-version: [18.x, 20.x, 21.x]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Use Node.js
|
||||||
|
uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: ${{ matrix.node-version }}
|
||||||
|
- run: npm ci
|
||||||
|
- run: npm run lint
|
||||||
|
- run: npm run coverage
|
43
README.md
43
README.md
@ -1,7 +1,5 @@
|
|||||||
# node-fetch-cache
|
# node-fetch-cache
|
||||||
|
|
||||||
Forked from: <https://github.com/mistval/node-fetch-cache>
|
|
||||||
|
|
||||||
node-fetch with caching of responses.
|
node-fetch with caching of responses.
|
||||||
|
|
||||||
The first fetch with any given arguments will result in an HTTP request and any subsequent fetch with the same arguments will read the response from the cache.
|
The first fetch with any given arguments will result in an HTTP request and any subsequent fetch with the same arguments will read the response from the cache.
|
||||||
@ -13,11 +11,11 @@ By default responses are cached in memory, but you can also cache to files on di
|
|||||||
Require it and use it the same way you would use node-fetch:
|
Require it and use it the same way you would use node-fetch:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import fetch from "node-fetch-cache";
|
import fetch from 'node-fetch-cache';
|
||||||
|
|
||||||
fetch("http://google.com")
|
fetch('http://google.com')
|
||||||
.then((response) => response.text())
|
.then(response => response.text())
|
||||||
.then((text) => console.log(text));
|
.then(text => console.log(text));
|
||||||
```
|
```
|
||||||
|
|
||||||
The next time you `fetch('http://google.com')`, the response will be returned from the cache. No HTTP request will be made.
|
The next time you `fetch('http://google.com')`, the response will be returned from the cache. No HTTP request will be made.
|
||||||
@ -35,18 +33,17 @@ This function can be used to eject the response from the cache, so that the next
|
|||||||
This module caches ALL responses, even those with 4xx and 5xx response statuses. You can use this function to uncache such responses if desired. For example:
|
This module caches ALL responses, even those with 4xx and 5xx response statuses. You can use this function to uncache such responses if desired. For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import fetch from "node-fetch-cache";
|
import fetch from 'node-fetch-cache';
|
||||||
|
|
||||||
fetch("http://google.com")
|
fetch('http://google.com')
|
||||||
.then(async (response) => {
|
.then(async response => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
await response.ejectFromCache();
|
await response.ejectFromCache();
|
||||||
throw new Error("Non-okay response from google.com");
|
throw new Error('Non-okay response from google.com');
|
||||||
} else {
|
} else {
|
||||||
return response.text();
|
return response.text();
|
||||||
}
|
}
|
||||||
})
|
}).then(text => console.log(text));
|
||||||
.then((text) => console.log(text));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Cache Customization
|
## Cache Customization
|
||||||
@ -60,7 +57,7 @@ This is the default cache delegate. It caches responses in-process in a POJO.
|
|||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { fetchBuilder, MemoryCache } from "node-fetch-cache";
|
import { fetchBuilder, MemoryCache } from 'node-fetch-cache';
|
||||||
const fetch = fetchBuilder.withCache(new MemoryCache(options));
|
const fetch = fetchBuilder.withCache(new MemoryCache(options));
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -72,7 +69,7 @@ Options:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that by default (if you don't use `withCache()`) a **shared** MemoryCache will be used (you can import this module in multiple files and they will all share the same cache). If you instantiate and provide a `new MemoryCache()` as shown above however, the cache is _NOT_ shared unless you explicitly pass it around and pass it into `withCache()` in each of your source files.
|
Note that by default (if you don't use `withCache()`) a **shared** MemoryCache will be used (you can import this module in multiple files and they will all share the same cache). If you instantiate and provide a `new MemoryCache()` as shown above however, the cache is *NOT* shared unless you explicitly pass it around and pass it into `withCache()` in each of your source files.
|
||||||
|
|
||||||
### FileSystemCache
|
### FileSystemCache
|
||||||
|
|
||||||
@ -81,7 +78,7 @@ Cache to a directory on disk. This allows the cache to survive the process exiti
|
|||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { fetchBuilder, FileSystemCache } from "node-fetch-cache";
|
import { fetchBuilder, FileSystemCache } from 'node-fetch-cache';
|
||||||
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
|
const fetch = fetchBuilder.withCache(new FileSystemCache(options));
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -121,12 +118,10 @@ Streams don't quite play nice with the concept of caching based on request chara
|
|||||||
Requests with the same cache key are queued. For example, you might wonder if making the same request 100 times simultaneously would result in 100 HTTP requests:
|
Requests with the same cache key are queued. For example, you might wonder if making the same request 100 times simultaneously would result in 100 HTTP requests:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import fetch from "node-fetch-cache";
|
import fetch from 'node-fetch-cache';
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Array(100)
|
Array(100).fill().map(() => fetch('https://google.com')),
|
||||||
.fill()
|
|
||||||
.map(() => fetch("https://google.com"))
|
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -137,10 +132,10 @@ The answer is no. Only one request would be made, and 99 of the `fetch()`s will
|
|||||||
The HTTP standard describes a [Cache-Control request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#request_directives) to control certain aspects of cache behavior. Node-fetch ignores these, but node-fetch-cache respects the `Cache-Control: only-if-cached` directive. When `only-if-cached` is specified, node-fetch-cache will return `undefined` if there is no cached response. No HTTP request will be made. For example:
|
The HTTP standard describes a [Cache-Control request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#request_directives) to control certain aspects of cache behavior. Node-fetch ignores these, but node-fetch-cache respects the `Cache-Control: only-if-cached` directive. When `only-if-cached` is specified, node-fetch-cache will return `undefined` if there is no cached response. No HTTP request will be made. For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import fetch from "node-fetch-cache";
|
import fetch from 'node-fetch-cache';
|
||||||
|
|
||||||
const response = await fetch("https://google.com", {
|
const response = await fetch('https://google.com', {
|
||||||
headers: { "Cache-Control": "only-if-cached" },
|
headers: { 'Cache-Control': 'only-if-cached' }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response === undefined) {
|
if (response === undefined) {
|
||||||
@ -155,12 +150,12 @@ Note that this is slightly different from browser fetch, which returns a `504 Ga
|
|||||||
This module exports a `getCacheKey()` function to calculate a cache key string from request parameters, which may be useful for enabling some advanced use cases (especially if you want to call cache functions directly). Call `getCacheKey()` exactly like you would call `fetch()`.
|
This module exports a `getCacheKey()` function to calculate a cache key string from request parameters, which may be useful for enabling some advanced use cases (especially if you want to call cache functions directly). Call `getCacheKey()` exactly like you would call `fetch()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { fetchBuilder, MemoryCache, getCacheKey } from "node-fetch-cache";
|
import { fetchBuilder, MemoryCache, getCacheKey } from 'node-fetch-cache';
|
||||||
|
|
||||||
const cache = new MemoryCache();
|
const cache = new MemoryCache();
|
||||||
const fetch = fetchBuilder.withCache(cache);
|
const fetch = fetchBuilder.withCache(cache);
|
||||||
|
|
||||||
const rawCacheData = await cache.get(getCacheKey("https://google.com"));
|
const rawCacheData = await cache.get(getCacheKey('https://google.com'));
|
||||||
```
|
```
|
||||||
|
|
||||||
## Bugs / Help / Feature Requests / Contributing
|
## Bugs / Help / Feature Requests / Contributing
|
||||||
|
16
package.json
16
package.json
@ -1,13 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "@fascinated/node-fetch-cache",
|
"name": "node-fetch-cache",
|
||||||
"version": "3.1.3",
|
"version": "3.1.3",
|
||||||
"description": "node-fetch with caching.",
|
"description": "node-fetch with caching.",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"publishConfig": {
|
|
||||||
"access": "public",
|
|
||||||
"registry": "https://registry.npmjs.org/"
|
|
||||||
},
|
|
||||||
"exports": {
|
"exports": {
|
||||||
"import": "./src/index.js",
|
"import": "./src/index.js",
|
||||||
"require": "./commonjs/wrapper.cjs"
|
"require": "./commonjs/wrapper.cjs"
|
||||||
@ -16,13 +12,13 @@
|
|||||||
"buildcjs": "rollup src/index.js --file commonjs/index.cjs --format cjs",
|
"buildcjs": "rollup src/index.js --file commonjs/index.cjs --format cjs",
|
||||||
"test": "npm run lintfix && npm run buildcjs && mocha --timeout 10000 --exit",
|
"test": "npm run lintfix && npm run buildcjs && mocha --timeout 10000 --exit",
|
||||||
"coverage": "nyc --reporter=lcov --reporter=text npm test",
|
"coverage": "nyc --reporter=lcov --reporter=text npm test",
|
||||||
"lint": "eslint .",
|
"lint": "./node_modules/.bin/eslint .",
|
||||||
"lintfix": "eslint . --fix",
|
"lintfix": "./node_modules/.bin/eslint . --fix",
|
||||||
"prepublishOnly": "npm test"
|
"prepublishOnly": "npm test"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://git.fascinated.cc/Fascinated/node-fetch-cache.git"
|
"url": "git+https://github.com/mistval/node-fetch-cache.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"node",
|
"node",
|
||||||
@ -34,9 +30,9 @@
|
|||||||
"author": "mistval",
|
"author": "mistval",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://git.fascinated.cc/Fascinated/node-fetch-cache/issues"
|
"url": "https://github.com/mistval/node-fetch-cache/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://git.fascinated.cc/Fascinated/node-fetch-cache#readme",
|
"homepage": "https://github.com/mistval/node-fetch-cache#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.9.0",
|
"eslint": "^8.9.0",
|
||||||
"eslint-config-airbnb-base": "^15.0.0",
|
"eslint-config-airbnb-base": "^15.0.0",
|
||||||
|
Reference in New Issue
Block a user