This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

19
book/node_modules/cacheable-request/LICENSE generated vendored Normal file
View File

@ -0,0 +1,19 @@
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

335
book/node_modules/cacheable-request/README.md generated vendored Normal file
View File

@ -0,0 +1,335 @@
<h1 align="center"><br><img width="380" src="https://cacheable.org/assets/images/cacheable_symbol.svg" alt="cacheable logo"><br><br></h1>
# cacheable-request
> Wrap native HTTP requests with RFC compliant cache support
[![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yaml)
[![codecov](https://codecov.io/gh/jaredwray/cacheable/branch/master/graph/badge.svg?token=LDLaqe4PsI)](https://codecov.io/gh/jaredwray/cacheable)
[![npm](https://img.shields.io/npm/dm/cacheable-request.svg)](https://www.npmjs.com/package/cacheable-request)
[![npm](https://img.shields.io/npm/v/cacheable-request.svg)](https://www.npmjs.com/package/cacheable-request)
[RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching for native Node.js HTTP/HTTPS requests. Caching works out of the box in memory or is easily pluggable with a wide range of storage adapters.
**Note:** This is a low level wrapper around the core HTTP modules, it's not a high level request library.
# Table of Contents
* [Latest Changes](#latest-changes)
* [Features](#features)
* [Install and Usage](#install-and-usage)
* [Storage Adapters](#storage-adapters)
* [API](#api)
* [Using Hooks](#using-hooks)
* [Contributing](#contributing)
* [Ask a Question](#ask-a-question)
* [License](#license) (MIT)
# Latest Changes
## Breaking Changes with v10.0.0
This release contains breaking changes. This is the new way to use this package.
### Usage Before v10
```js
import http from 'http';
import CacheableRequest from 'cacheable-request';
// Then instead of
const req = http.request('http://example.com', cb);
req.end();
// You can do
const cacheableRequest = new CacheableRequest(http.request);
const cacheReq = cacheableRequest('http://example.com', cb);
cacheReq.on('request', req => req.end());
// Future requests to 'example.com' will be returned from cache if still valid
// You pass in any other http.request API compatible method to be wrapped with cache support:
const cacheableRequest = new CacheableRequest(https.request);
const cacheableRequest = new CacheableRequest(electron.net);
```
### Usage After v10.1.0
```js
import CacheableRequest from 'cacheable-request';
// Now You can do
const cacheableRequest = new CacheableRequest(http.request).request();
const cacheReq = cacheableRequest('http://example.com', cb);
cacheReq.on('request', req => req.end());
// Future requests to 'example.com' will be returned from cache if still valid
// You pass in any other http.request API compatible method to be wrapped with cache support:
const cacheableRequest = new CacheableRequest(https.request).request();
const cacheableRequest = new CacheableRequest(electron.net).request();
```
The biggest change is that when you do a `new` CacheableRequest you now want to call `request` method will give you the instance to use.
```diff
- const cacheableRequest = new CacheableRequest(http.request);
+ const cacheableRequest = new CacheableRequest(http.request).request();
```
### ESM Support in version 9 and higher.
We are now using pure esm support in our package. If you need to use commonjs you can use v8 or lower. To learn more about using ESM please read this from `sindresorhus`: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
## Features
- Only stores cacheable responses as defined by RFC 7234
- Fresh cache entries are served directly from cache
- Stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers
- 304 responses from revalidation requests use cached body
- Updates `Age` header on cached responses
- Can completely bypass cache on a per request basis
- In memory cache by default
- Official support for Redis, Memcache, Etcd, MongoDB, SQLite, PostgreSQL and MySQL storage adapters
- Easily plug in your own or third-party storage adapters
- If DB connection fails, cache is automatically bypassed ([disabled by default](#optsautomaticfailover))
- Adds cache support to any existing HTTP code with minimal changes
- Uses [http-cache-semantics](https://github.com/pornel/http-cache-semantics) internally for HTTP RFC 7234 compliance
## Install and Usage
```shell
npm install cacheable-request
```
```js
import http from 'http';
import CacheableRequest from 'cacheable-request';
// Then instead of
const req = http.request('http://example.com', cb);
req.end();
// You can do
const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
const cacheReq = cacheableRequest('http://example.com', cb);
cacheReq.on('request', req => req.end());
// Future requests to 'example.com' will be returned from cache if still valid
// You pass in any other http.request API compatible method to be wrapped with cache support:
const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
```
## Storage Adapters
`cacheable-request` uses [Keyv](https://github.com/jaredwray/keyv) to support a wide range of storage adapters.
For example, to use Redis as a cache backend, you just need to install the official Redis Keyv storage adapter:
```
npm install @keyv/redis
```
And then you can pass `CacheableRequest` your connection string:
```js
const cacheableRequest = new CacheableRequest(http.request, 'redis://user:pass@localhost:6379').createCacheableRequest();
```
[View all official Keyv storage adapters.](https://github.com/jaredwray/keyv#official-storage-adapters)
Keyv also supports anything that follows the Map API so it's easy to write your own storage adapter or use a third-party solution.
e.g The following are all valid storage adapters
```js
const storageAdapter = new Map();
// or
const storageAdapter = require('./my-storage-adapter');
// or
const QuickLRU = require('quick-lru');
const storageAdapter = new QuickLRU({ maxSize: 1000 });
const cacheableRequest = new CacheableRequest(http.request, storageAdapter).createCacheableRequest();
```
View the [Keyv docs](https://github.com/jaredwray/keyv) for more information on how to use storage adapters.
## API
### new cacheableRequest(request, [storageAdapter])
Returns the provided request function wrapped with cache support.
#### request
Type: `function`
Request function to wrap with cache support. Should be [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback) or a similar API compatible request function.
#### storageAdapter
Type: `Keyv storage adapter`<br>
Default: `new Map()`
A [Keyv](https://github.com/jaredwray/keyv) storage adapter instance, or connection string if using with an official Keyv storage adapter.
### Instance
#### cacheableRequest(opts, [cb])
Returns an event emitter.
##### opts
Type: `object`, `string`
- Any of the default request functions options.
- Any [`http-cache-semantics`](https://github.com/kornelski/http-cache-semantics#constructor-options) options.
- Any of the following:
###### opts.cache
Type: `boolean`<br>
Default: `true`
If the cache should be used. Setting this to false will completely bypass the cache for the current request.
###### opts.strictTtl
Type: `boolean`<br>
Default: `false`
If set to `true` once a cached resource has expired it is deleted and will have to be re-requested.
If set to `false` (default), after a cached resource's TTL expires it is kept in the cache and will be revalidated on the next request with `If-None-Match`/`If-Modified-Since` headers.
###### opts.maxTtl
Type: `number`<br>
Default: `undefined`
Limits TTL. The `number` represents milliseconds.
###### opts.automaticFailover
Type: `boolean`<br>
Default: `false`
When set to `true`, if the DB connection fails we will automatically fallback to a network request. DB errors will still be emitted to notify you of the problem even though the request callback may succeed.
###### opts.forceRefresh
Type: `boolean`<br>
Default: `false`
Forces refreshing the cache. If the response could be retrieved from the cache, it will perform a new request and override the cache instead.
##### cb
Type: `function`
The callback function which will receive the response as an argument.
The response can be either a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage) or a [responselike object](https://github.com/lukechilds/responselike). The response will also have a `fromCache` property set with a boolean value.
##### .on('request', request)
`request` event to get the request object of the request.
**Note:** This event will only fire if an HTTP request is actually made, not when a response is retrieved from cache. However, you should always handle the `request` event to end the request and handle any potential request errors.
##### .on('response', response)
`response` event to get the response object from the HTTP request or cache.
##### .on('error', error)
`error` event emitted in case of an error with the cache.
Errors emitted here will be an instance of `CacheableRequest.RequestError` or `CacheableRequest.CacheError`. You will only ever receive a `RequestError` if the request function throws (normally caused by invalid user input). Normal request errors should be handled inside the `request` event.
To properly handle all error scenarios you should use the following pattern:
```js
cacheableRequest('example.com', cb)
.on('error', err => {
if (err instanceof CacheableRequest.CacheError) {
handleCacheError(err); // Cache error
} else if (err instanceof CacheableRequest.RequestError) {
handleRequestError(err); // Request function thrown
}
})
.on('request', req => {
req.on('error', handleRequestError); // Request error emitted
req.end();
});
```
**Note:** Database connection errors are emitted here, however `cacheable-request` will attempt to re-request the resource and bypass the cache on a connection error. Therefore a database connection error doesn't necessarily mean the request won't be fulfilled.
## Using Hooks
Hooks have been implemented since version `v9` and are very useful to modify response before saving it in cache. You can use hooks to modify response before saving it in cache.
### Add Hooks
The hook will pre compute response right before saving it in cache. You can include many hooks and it will run in order you add hook on response object.
```js
import http from 'http';
import CacheableRequest from 'cacheable-request';
const cacheableRequest = new CacheableRequest(request, cache).request();
// adding a hook to decompress response
cacheableRequest.addHook('onResponse', async (value: CacheValue, response: any) => {
const buffer = await pm(gunzip)(value.body);
value.body = buffer.toString();
return value;
});
```
here is also an example of how to add in the remote address
```js
import CacheableRequest, {CacheValue} from 'cacheable-request';
const cacheableRequest = new CacheableRequest(request, cache).request();
cacheableRequest.addHook('onResponse', (value: CacheValue, response: any) => {
if (response.connection) {
value.remoteAddress = response.connection.remoteAddress;
}
return value;
});
```
### Remove Hooks
You can also remove hook by using below
```js
CacheableRequest.removeHook('onResponse');
```
## How to Contribute
Cacheable-Request is an open source package and community driven that is maintained regularly. In addtion we have a couple of other guidelines for review:
* [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) - Our code of conduct
* [CONTRIBUTING.md](CONTRIBUTING.md) - How to contribute to this project
* [SECURITY.md](SECURITY.md) - Security guidelines and supported versions
## Post an Issue
To post an issue, navigate to the "Issues" tab in the main repository, and then select "New Issue." Enter a clear title describing the issue, as well as a description containing additional relevant information. Also select the label that best describes your issue type. For a bug report, for example, create an issue with the label "bug." In the description field, Be sure to include replication steps, as well as any relevant error messages.
If you're reporting a security violation, be sure to check out the project's [security policy](SECURITY.md).
Please also refer to our [Code of Conduct](CODE_OF_CONDUCT.md) for more information on how to report issues.
## Ask a Question
To ask a question, create an issue with the label "question." In the issue description, include the related code and any context that can help us answer your question.
## License
This project is under the [MIT](LICENSE) license.
MIT © Luke Childs 2017-2021 and Jared Wray 2022+

17
book/node_modules/cacheable-request/dist/index.d.ts generated vendored Normal file
View File

@ -0,0 +1,17 @@
import { RequestFn, StorageAdapter, CacheResponse, CacheValue, CacheableOptions, Emitter } from './types.js';
type Function_ = (...arguments_: any[]) => any;
declare class CacheableRequest {
cache: StorageAdapter;
cacheRequest: RequestFn;
hooks: Map<string, Function_>;
constructor(cacheRequest: RequestFn, cacheAdapter?: StorageAdapter | string);
request: () => (options: CacheableOptions, callback?: (response: CacheResponse) => void) => Emitter;
addHook: (name: string, function_: Function_) => void;
removeHook: (name: string) => boolean;
getHook: (name: string) => Function_;
runHook: (name: string, ...arguments_: any[]) => Promise<CacheValue>;
}
export default CacheableRequest;
export * from './types.js';
export declare const onResponse = "onResponse";
//# sourceMappingURL=index.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EACN,SAAS,EAAE,cAAc,EAAE,aAAa,EAAE,UAAU,EAAE,gBAAgB,EAAuC,OAAO,EACpH,MAAM,YAAY,CAAC;AAGpB,KAAK,SAAS,GAAG,CAAC,GAAG,UAAU,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAE/C,cAAM,gBAAgB;IACrB,KAAK,EAAE,cAAc,CAAC;IACtB,YAAY,EAAE,SAAS,CAAC;IACxB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAgC;gBACjD,YAAY,EAAE,SAAS,EAAE,YAAY,CAAC,EAAE,cAAc,GAAG,MAAM;IAmB3E,OAAO,kBAAmB,gBAAgB,wBACnB,aAAa,KAAK,IAAI,KAAG,OAAO,CA2MrD;IAEF,OAAO,SAAU,MAAM,aAAa,SAAS,UAI3C;IAEF,UAAU,SAAU,MAAM,aAA6B;IAEvD,OAAO,SAAU,MAAM,eAA0B;IAEjD,OAAO,SAAgB,MAAM,iBAAiB,GAAG,EAAE,KAAG,QAAQ,UAAU,CAAC,CAA0C;CACnH;AA6CD,eAAe,gBAAgB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,eAAO,MAAM,UAAU,eAAe,CAAC"}

279
book/node_modules/cacheable-request/dist/index.js generated vendored Normal file
View File

@ -0,0 +1,279 @@
import EventEmitter from 'node:events';
import urlLib from 'node:url';
import crypto from 'node:crypto';
import stream, { PassThrough as PassThroughStream } from 'node:stream';
import normalizeUrl from 'normalize-url';
import { getStreamAsBuffer } from 'get-stream';
import CachePolicy from 'http-cache-semantics';
import Response from 'responselike';
import Keyv from 'keyv';
import mimicResponse from 'mimic-response';
import { CacheError, RequestError, } from './types.js';
class CacheableRequest {
constructor(cacheRequest, cacheAdapter) {
this.hooks = new Map();
this.request = () => (options, callback) => {
let url;
if (typeof options === 'string') {
url = normalizeUrlObject(urlLib.parse(options));
options = {};
}
else if (options instanceof urlLib.URL) {
url = normalizeUrlObject(urlLib.parse(options.toString()));
options = {};
}
else {
const [pathname, ...searchParts] = (options.path ?? '').split('?');
const search = searchParts.length > 0
? `?${searchParts.join('?')}`
: '';
url = normalizeUrlObject({ ...options, pathname, search });
}
options = {
headers: {},
method: 'GET',
cache: true,
strictTtl: false,
automaticFailover: false,
...options,
...urlObjectToRequestOptions(url),
};
options.headers = Object.fromEntries(entries(options.headers).map(([key, value]) => [key.toLowerCase(), value]));
const ee = new EventEmitter();
const normalizedUrlString = normalizeUrl(urlLib.format(url), {
stripWWW: false, // eslint-disable-line @typescript-eslint/naming-convention
removeTrailingSlash: false,
stripAuthentication: false,
});
let key = `${options.method}:${normalizedUrlString}`;
// POST, PATCH, and PUT requests may be cached, depending on the response
// cache-control headers. As a result, the body of the request should be
// added to the cache key in order to avoid collisions.
if (options.body && options.method !== undefined && ['POST', 'PATCH', 'PUT'].includes(options.method)) {
if (options.body instanceof stream.Readable) {
// Streamed bodies should completely skip the cache because they may
// or may not be hashable and in either case the stream would need to
// close before the cache key could be generated.
options.cache = false;
}
else {
key += `:${crypto.createHash('md5').update(options.body).digest('hex')}`;
}
}
let revalidate = false;
let madeRequest = false;
const makeRequest = (options_) => {
madeRequest = true;
let requestErrored = false;
let requestErrorCallback = () => { };
const requestErrorPromise = new Promise(resolve => {
requestErrorCallback = () => {
if (!requestErrored) {
requestErrored = true;
resolve();
}
};
});
const handler = async (response) => {
if (revalidate) {
response.status = response.statusCode;
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(options_, response);
if (!revalidatedPolicy.modified) {
response.resume();
await new Promise(resolve => {
// Skipping 'error' handler cause 'error' event should't be emitted for 304 response
response
.once('end', resolve);
});
const headers = convertHeaders(revalidatedPolicy.policy.responseHeaders());
response = new Response({
statusCode: revalidate.statusCode, headers, body: revalidate.body, url: revalidate.url,
});
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new CachePolicy(options_, response, options_);
response.fromCache = false;
}
let clonedResponse;
if (options_.cache && response.cachePolicy.storable()) {
clonedResponse = cloneResponse(response);
(async () => {
try {
const bodyPromise = getStreamAsBuffer(response);
await Promise.race([
requestErrorPromise,
new Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return
new Promise(resolve => response.once('close', resolve)), // eslint-disable-line no-promise-executor-return
]);
const body = await bodyPromise;
let value = {
url: response.url,
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
body,
cachePolicy: response.cachePolicy.toObject(),
};
let ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined;
if (options_.maxTtl) {
ttl = ttl ? Math.min(ttl, options_.maxTtl) : options_.maxTtl;
}
if (this.hooks.size > 0) {
/* eslint-disable no-await-in-loop */
for (const key_ of this.hooks.keys()) {
value = await this.runHook(key_, value, response);
}
/* eslint-enable no-await-in-loop */
}
await this.cache.set(key, value, ttl);
}
catch (error) {
ee.emit('error', new CacheError(error));
}
})();
}
else if (options_.cache && revalidate) {
(async () => {
try {
await this.cache.delete(key);
}
catch (error) {
ee.emit('error', new CacheError(error));
}
})();
}
ee.emit('response', clonedResponse ?? response);
if (typeof callback === 'function') {
callback(clonedResponse ?? response);
}
};
try {
const request_ = this.cacheRequest(options_, handler);
request_.once('error', requestErrorCallback);
request_.once('abort', requestErrorCallback);
request_.once('destroy', requestErrorCallback);
ee.emit('request', request_);
}
catch (error) {
ee.emit('error', new RequestError(error));
}
};
(async () => {
const get = async (options_) => {
await Promise.resolve();
const cacheEntry = options_.cache ? await this.cache.get(key) : undefined;
if (cacheEntry === undefined && !options_.forceRefresh) {
makeRequest(options_);
return;
}
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
if (policy.satisfiesWithoutRevalidation(options_) && !options_.forceRefresh) {
const headers = convertHeaders(policy.responseHeaders());
const response = new Response({
statusCode: cacheEntry.statusCode, headers, body: cacheEntry.body, url: cacheEntry.url,
});
response.cachePolicy = policy;
response.fromCache = true;
ee.emit('response', response);
if (typeof callback === 'function') {
callback(response);
}
}
else if (policy.satisfiesWithoutRevalidation(options_) && Date.now() >= policy.timeToLive() && options_.forceRefresh) {
await this.cache.delete(key);
options_.headers = policy.revalidationHeaders(options_);
makeRequest(options_);
}
else {
revalidate = cacheEntry;
options_.headers = policy.revalidationHeaders(options_);
makeRequest(options_);
}
};
const errorHandler = (error) => ee.emit('error', new CacheError(error));
if (this.cache instanceof Keyv) {
const cachek = this.cache;
cachek.once('error', errorHandler);
ee.on('error', () => cachek.removeListener('error', errorHandler));
ee.on('response', () => cachek.removeListener('error', errorHandler));
}
try {
await get(options);
}
catch (error) {
if (options.automaticFailover && !madeRequest) {
makeRequest(options);
}
ee.emit('error', new CacheError(error));
}
})();
return ee;
};
this.addHook = (name, function_) => {
if (!this.hooks.has(name)) {
this.hooks.set(name, function_);
}
};
this.removeHook = (name) => this.hooks.delete(name);
this.getHook = (name) => this.hooks.get(name);
this.runHook = async (name, ...arguments_) => this.hooks.get(name)?.(...arguments_);
if (cacheAdapter instanceof Keyv) {
this.cache = cacheAdapter;
}
else if (typeof cacheAdapter === 'string') {
this.cache = new Keyv({
uri: cacheAdapter,
namespace: 'cacheable-request',
});
}
else {
this.cache = new Keyv({
store: cacheAdapter,
namespace: 'cacheable-request',
});
}
this.request = this.request.bind(this);
this.cacheRequest = cacheRequest;
}
}
const entries = Object.entries;
const cloneResponse = (response) => {
const clone = new PassThroughStream({ autoDestroy: false });
mimicResponse(response, clone);
return response.pipe(clone);
};
const urlObjectToRequestOptions = (url) => {
const options = { ...url };
options.path = `${url.pathname || '/'}${url.search || ''}`;
delete options.pathname;
delete options.search;
return options;
};
const normalizeUrlObject = (url) =>
// If url was parsed by url.parse or new URL:
// - hostname will be set
// - host will be hostname[:port]
// - port will be set if it was explicit in the parsed string
// Otherwise, url was from request options:
// - hostname or host may be set
// - host shall not have port encoded
({
protocol: url.protocol,
auth: url.auth,
hostname: url.hostname || url.host || 'localhost',
port: url.port,
pathname: url.pathname,
search: url.search,
});
const convertHeaders = (headers) => {
const result = [];
for (const name of Object.keys(headers)) {
result[name.toLowerCase()] = headers[name];
}
return result;
};
export default CacheableRequest;
export * from './types.js';
export const onResponse = 'onResponse';
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

106
book/node_modules/cacheable-request/dist/types.d.ts generated vendored Normal file
View File

@ -0,0 +1,106 @@
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
import { request, RequestOptions, ClientRequest, ServerResponse } from 'node:http';
import { URL } from 'node:url';
import { EventEmitter } from 'node:events';
import { Buffer } from 'node:buffer';
import { Store } from 'keyv';
import ResponseLike from 'responselike';
import { CachePolicyObject } from 'http-cache-semantics';
export type RequestFn = typeof request;
export type RequestFunction = typeof request;
export type CacheResponse = ServerResponse | typeof ResponseLike;
export type CacheableRequestFunction = (options: CacheableOptions, callback?: (response: CacheResponse) => void) => Emitter;
export type CacheableOptions = Options & RequestOptions | string | URL;
export type StorageAdapter = Store<any>;
export interface Options {
/**
* If the cache should be used. Setting this to `false` will completely bypass the cache for the current request.
* @default true
*/
cache?: boolean | undefined;
/**
* If set to `true` once a cached resource has expired it is deleted and will have to be re-requested.
*
* If set to `false`, after a cached resource's TTL expires it is kept in the cache and will be revalidated
* on the next request with `If-None-Match`/`If-Modified-Since` headers.
* @default false
*/
strictTtl?: boolean | undefined;
/**
* Limits TTL. The `number` represents milliseconds.
* @default undefined
*/
maxTtl?: number | undefined;
/**
* When set to `true`, if the DB connection fails we will automatically fallback to a network request.
* DB errors will still be emitted to notify you of the problem even though the request callback may succeed.
* @default false
*/
automaticFailover?: boolean | undefined;
/**
* Forces refreshing the cache. If the response could be retrieved from the cache, it will perform a
* new request and override the cache instead.
* @default false
*/
forceRefresh?: boolean | undefined;
remoteAddress?: boolean | undefined;
url?: string | undefined;
headers?: Record<string, string | string[] | undefined>;
body?: Buffer;
}
export interface CacheValue extends Record<string, any> {
url: string;
statusCode: number;
body: Buffer | string;
cachePolicy: CachePolicyObject;
}
export interface Emitter extends EventEmitter {
addListener(event: 'request', listener: (request: ClientRequest) => void): this;
addListener(event: 'response', listener: (response: CacheResponse) => void): this;
addListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
on(event: 'request', listener: (request: ClientRequest) => void): this;
on(event: 'response', listener: (response: CacheResponse) => void): this;
on(event: 'error', listener: (error: RequestError | CacheError) => void): this;
once(event: 'request', listener: (request: ClientRequest) => void): this;
once(event: 'response', listener: (response: CacheResponse) => void): this;
once(event: 'error', listener: (error: RequestError | CacheError) => void): this;
prependListener(event: 'request', listener: (request: ClientRequest) => void): this;
prependListener(event: 'response', listener: (response: CacheResponse) => void): this;
prependListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
prependOnceListener(event: 'request', listener: (request: ClientRequest) => void): this;
prependOnceListener(event: 'response', listener: (response: CacheResponse) => void): this;
prependOnceListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
removeListener(event: 'request', listener: (request: ClientRequest) => void): this;
removeListener(event: 'response', listener: (response: CacheResponse) => void): this;
removeListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
off(event: 'request', listener: (request: ClientRequest) => void): this;
off(event: 'response', listener: (response: CacheResponse) => void): this;
off(event: 'error', listener: (error: RequestError | CacheError) => void): this;
removeAllListeners(event?: 'request' | 'response' | 'error'): this;
listeners(event: 'request'): Array<(request: ClientRequest) => void>;
listeners(event: 'response'): Array<(response: CacheResponse) => void>;
listeners(event: 'error'): Array<(error: RequestError | CacheError) => void>;
rawListeners(event: 'request'): Array<(request: ClientRequest) => void>;
rawListeners(event: 'response'): Array<(response: CacheResponse) => void>;
rawListeners(event: 'error'): Array<(error: RequestError | CacheError) => void>;
emit(event: 'request', request: ClientRequest): boolean;
emit(event: 'response', response: CacheResponse): boolean;
emit(event: 'error', error: RequestError | CacheError): boolean;
eventNames(): Array<'request' | 'response' | 'error'>;
listenerCount(type: 'request' | 'response' | 'error'): number;
}
export declare class RequestError extends Error {
constructor(error: Error);
}
export declare class CacheError extends Error {
constructor(error: Error);
}
export interface UrlOption {
path: string;
pathname?: string;
search?: string;
}
//# sourceMappingURL=types.d.ts.map

View File

@ -0,0 +1 @@
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;;AASA,OAAO,EACN,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EACtD,MAAM,WAAW,CAAC;AACnB,OAAO,EAAC,GAAG,EAAC,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AACzC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC,OAAO,EAAC,KAAK,EAAC,MAAM,MAAM,CAAC;AAC3B,OAAO,YAAY,MAAM,cAAc,CAAC;AACxC,OAAO,EAAC,iBAAiB,EAAC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EAAE,KAAK;CAIxB;AAED,MAAM,WAAW,SAAS;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB"}

19
book/node_modules/cacheable-request/dist/types.js generated vendored Normal file
View File

@ -0,0 +1,19 @@
// Type definitions for cacheable-request 6.0
// Project: https://github.com/lukechilds/cacheable-request#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Paul Melnikow <https://github.com/paulmelnikow>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
export class RequestError extends Error {
constructor(error) {
super(error.message);
Object.assign(this, error);
}
}
export class CacheError extends Error {
constructor(error) {
super(error.message);
Object.assign(this, error);
}
}
//# sourceMappingURL=types.js.map

File diff suppressed because one or more lines are too long

122
book/node_modules/cacheable-request/package.json generated vendored Normal file
View File

@ -0,0 +1,122 @@
{
"name": "cacheable-request",
"version": "12.0.1",
"description": "Wrap native HTTP requests with RFC compliant cache support",
"license": "MIT",
"repository": "jaredwray/cacheable",
"author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo --fix && NODE_OPTIONS=--experimental-vm-modules jest --coverage ",
"prepare": "npm run build",
"build": "tsc --project tsconfig.build.json",
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite && rm -rf ./dist"
},
"files": [
"dist"
],
"keywords": [
"HTTP",
"HTTPS",
"cache",
"caching",
"layer",
"cacheable",
"RFC 7234",
"RFC",
"7234",
"compliant"
],
"dependenciesComments": {
"@types/http-cache-semantics": "It needs to be in the dependencies list and not devDependencies because otherwise projects that use this one will be getting `Could not find a declaration file for module 'http-cache-semantics'` error when running `tsc`, see https://github.com/jaredwray/cacheable-request/issues/194 for details"
},
"dependencies": {
"@types/http-cache-semantics": "^4.0.4",
"get-stream": "^9.0.1",
"http-cache-semantics": "^4.1.1",
"keyv": "^4.5.4",
"mimic-response": "^4.0.0",
"normalize-url": "^8.0.1",
"responselike": "^3.0.0"
},
"devDependencies": {
"@keyv/sqlite": "^3.6.7",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.30",
"@types/responselike": "^1.0.3",
"@types/sqlite3": "^3.1.11",
"body-parser": "^1.20.2",
"delay": "^6.0.0",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^27.9.0",
"express": "^4.19.1",
"jest": "^29.7.0",
"pify": "^6.1.0",
"sqlite3": "^5.1.7",
"ts-jest": "^29.1.2",
"ts-jest-resolver": "^2.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.3",
"xo": "^0.58.0"
},
"jest": {
"collectCoverageFrom": [
"src/**/*.{ts,js}"
],
"extensionsToTreatAsEsm": [
".ts"
],
"resolver": "ts-jest-resolver",
"moduleFileExtensions": [
"ts",
"js"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "./tsconfig.build.json",
"useESM": true
}
]
},
"testMatch": [
"**/test/*.test.(ts|js)"
],
"testEnvironment": "node"
},
"xo": {
"plugins": [
"jest"
],
"extends": [
"plugin:jest/recommended"
],
"rules": {
"@typescript-eslint/triple-slash-reference": 0,
"@typescript-eslint/no-namespace": 0,
"@typescript-eslint/no-unsafe-assignment": 0,
"@typescript-eslint/no-unsafe-call": 0,
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/restrict-template-expressions": 0,
"@typescript-eslint/no-unsafe-return": 0,
"@typescript-eslint/no-unsafe-argument": 0,
"new-cap": 0,
"unicorn/no-abusive-eslint-disable": 0,
"@typescript-eslint/restrict-plus-operands": 0,
"@typescript-eslint/no-implicit-any-catch": 0,
"@typescript-eslint/consistent-type-imports": 0,
"@typescript-eslint/consistent-type-definitions": 0,
"@typescript-eslint/prefer-nullish-coalescing": 0,
"n/prefer-global/url": 0,
"n/no-deprecated-api": 0,
"unicorn/prefer-event-target": 0,
"@typescript-eslint/no-unnecessary-type-assertion": 0
}
}
}