@cacheable/net 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +43 -0
- package/dist/index.cjs +85 -0
- package/dist/index.d.cts +37 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +60 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License & © Jared Wray
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
|
5
|
+
deal in the Software without restriction, including without limitation the
|
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
|
|
2
|
+
|
|
3
|
+
> High Performance Network Caching for Node.js with fetch, request, http 1.1, and http 2 support
|
|
4
|
+
|
|
5
|
+
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
6
|
+
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
7
|
+
[](https://www.npmjs.com/package/@cacheable/net)
|
|
8
|
+
[](https://www.npmjs.com/package/@cacheable/net)
|
|
9
|
+
[](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Features:
|
|
13
|
+
* `fetch` from [undici](https://github.com/nodejs/undici) cache enabled via `cacheable`
|
|
14
|
+
* `fetch` quick helpers such as `get`, `post`, `put`, and `delete` for easier development
|
|
15
|
+
* `request` from [undici](https://github.com/nodejs/undici) cache enabled via `cacheable`
|
|
16
|
+
* HTTP/1.1 and HTTP/2 caching support via Node.js `http` and `https` modules
|
|
17
|
+
* [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching for native Node.js HTTP/HTTPS requests
|
|
18
|
+
* Drop in replacement for `http` `https`, `fetch` modules with caching enabled
|
|
19
|
+
* DNS caching for `dns.lookup` and `dns.resolve` methods via `cacheable`
|
|
20
|
+
* WHOIS caching for `whois.lookup` method via `cacheable`
|
|
21
|
+
* Advanced key generation via built in hashing and custom key generation functions
|
|
22
|
+
* Benchmarks for performance comparison
|
|
23
|
+
* All the features of [cacheable](https://npmjs.com/package/cacheable) - layered caching, LRU, expiration, hooks, backed by Keyv, and more!
|
|
24
|
+
* Highly Tested and Maintained on a regular basis with a focus on performance and reliability
|
|
25
|
+
|
|
26
|
+
# Table of Contents
|
|
27
|
+
* [Getting Started](#getting-started)
|
|
28
|
+
* [How to Contribute](#how-to-contribute)
|
|
29
|
+
* [License and Copyright](#license-and-copyright)
|
|
30
|
+
|
|
31
|
+
# Getting Started
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install @cacheable/net
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# How to Contribute
|
|
39
|
+
|
|
40
|
+
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README [https://github.com/jaredwray/cacheable](https://github.com/jaredwray/cacheable). This will talk about how to `Open a Pull Request`, `Ask a Question`, or `Post an Issue`.
|
|
41
|
+
|
|
42
|
+
# License and Copyright
|
|
43
|
+
[MIT © Jared Wray](./LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CacheableNet: () => CacheableNet,
|
|
24
|
+
Net: () => Net,
|
|
25
|
+
fetch: () => fetch
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_cacheable = require("cacheable");
|
|
29
|
+
var import_hookified = require("hookified");
|
|
30
|
+
|
|
31
|
+
// src/fetch.ts
|
|
32
|
+
var import_undici = require("undici");
|
|
33
|
+
async function fetch(url, options) {
|
|
34
|
+
if (!options.cache) {
|
|
35
|
+
throw new Error("Fetch options must include a cache instance or options.");
|
|
36
|
+
}
|
|
37
|
+
const fetchOptions = {
|
|
38
|
+
...options,
|
|
39
|
+
cache: "no-cache"
|
|
40
|
+
};
|
|
41
|
+
return options.cache.getOrSet(url, async () => {
|
|
42
|
+
const response = await (0, import_undici.fetch)(url, fetchOptions);
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
throw new Error(`Fetch failed with status ${response.status}`);
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/index.ts
|
|
51
|
+
var CacheableNet = class extends import_hookified.Hookified {
|
|
52
|
+
_cache = new import_cacheable.Cacheable();
|
|
53
|
+
constructor(options) {
|
|
54
|
+
super(options);
|
|
55
|
+
if (options?.cache) {
|
|
56
|
+
this._cache = options.cache instanceof import_cacheable.Cacheable ? options.cache : new import_cacheable.Cacheable(options.cache);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
get cache() {
|
|
60
|
+
return this._cache;
|
|
61
|
+
}
|
|
62
|
+
set cache(value) {
|
|
63
|
+
this._cache = value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
67
|
+
* @param {string} url The URL to fetch.
|
|
68
|
+
* @param {FetchRequestInit} options Optional request options.
|
|
69
|
+
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
70
|
+
*/
|
|
71
|
+
async fetch(url, options) {
|
|
72
|
+
const fetchOptions = {
|
|
73
|
+
...options,
|
|
74
|
+
cache: this._cache
|
|
75
|
+
};
|
|
76
|
+
return fetch(url, fetchOptions);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var Net = CacheableNet;
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
CacheableNet,
|
|
83
|
+
Net,
|
|
84
|
+
fetch
|
|
85
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Cacheable, CacheableOptions } from 'cacheable';
|
|
2
|
+
import { HookifiedOptions, Hookified } from 'hookified';
|
|
3
|
+
import { Response as Response$1, RequestInit } from 'undici';
|
|
4
|
+
export { RequestInit as FetchRequestInit } from 'undici';
|
|
5
|
+
|
|
6
|
+
type FetchOptions = Omit<RequestInit, "cache"> & {
|
|
7
|
+
cache: Cacheable;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Fetch data from a URL with optional request options.
|
|
11
|
+
* @param {string} url The URL to fetch.
|
|
12
|
+
* @param {FetchOptions} options Optional request options. The `cacheable` property is required and should be an
|
|
13
|
+
* instance of `Cacheable` or a `CacheableOptions` object.
|
|
14
|
+
* @returns {Promise<UndiciResponse>} The response from the fetch.
|
|
15
|
+
*/
|
|
16
|
+
declare function fetch(url: string, options: FetchOptions): Promise<Response$1>;
|
|
17
|
+
type Response = Response$1;
|
|
18
|
+
|
|
19
|
+
type CacheableNetOptions = {
|
|
20
|
+
cache?: Cacheable | CacheableOptions;
|
|
21
|
+
} & HookifiedOptions;
|
|
22
|
+
declare class CacheableNet extends Hookified {
|
|
23
|
+
private _cache;
|
|
24
|
+
constructor(options?: CacheableNetOptions);
|
|
25
|
+
get cache(): Cacheable;
|
|
26
|
+
set cache(value: Cacheable);
|
|
27
|
+
/**
|
|
28
|
+
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
29
|
+
* @param {string} url The URL to fetch.
|
|
30
|
+
* @param {FetchRequestInit} options Optional request options.
|
|
31
|
+
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
32
|
+
*/
|
|
33
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
34
|
+
}
|
|
35
|
+
declare const Net: typeof CacheableNet;
|
|
36
|
+
|
|
37
|
+
export { CacheableNet, type CacheableNetOptions, type FetchOptions, type Response as FetchResponse, Net, fetch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Cacheable, CacheableOptions } from 'cacheable';
|
|
2
|
+
import { HookifiedOptions, Hookified } from 'hookified';
|
|
3
|
+
import { Response as Response$1, RequestInit } from 'undici';
|
|
4
|
+
export { RequestInit as FetchRequestInit } from 'undici';
|
|
5
|
+
|
|
6
|
+
type FetchOptions = Omit<RequestInit, "cache"> & {
|
|
7
|
+
cache: Cacheable;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Fetch data from a URL with optional request options.
|
|
11
|
+
* @param {string} url The URL to fetch.
|
|
12
|
+
* @param {FetchOptions} options Optional request options. The `cacheable` property is required and should be an
|
|
13
|
+
* instance of `Cacheable` or a `CacheableOptions` object.
|
|
14
|
+
* @returns {Promise<UndiciResponse>} The response from the fetch.
|
|
15
|
+
*/
|
|
16
|
+
declare function fetch(url: string, options: FetchOptions): Promise<Response$1>;
|
|
17
|
+
type Response = Response$1;
|
|
18
|
+
|
|
19
|
+
type CacheableNetOptions = {
|
|
20
|
+
cache?: Cacheable | CacheableOptions;
|
|
21
|
+
} & HookifiedOptions;
|
|
22
|
+
declare class CacheableNet extends Hookified {
|
|
23
|
+
private _cache;
|
|
24
|
+
constructor(options?: CacheableNetOptions);
|
|
25
|
+
get cache(): Cacheable;
|
|
26
|
+
set cache(value: Cacheable);
|
|
27
|
+
/**
|
|
28
|
+
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
29
|
+
* @param {string} url The URL to fetch.
|
|
30
|
+
* @param {FetchRequestInit} options Optional request options.
|
|
31
|
+
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
32
|
+
*/
|
|
33
|
+
fetch(url: string, options?: RequestInit): Promise<Response>;
|
|
34
|
+
}
|
|
35
|
+
declare const Net: typeof CacheableNet;
|
|
36
|
+
|
|
37
|
+
export { CacheableNet, type CacheableNetOptions, type FetchOptions, type Response as FetchResponse, Net, fetch };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { Cacheable } from "cacheable";
|
|
3
|
+
import { Hookified } from "hookified";
|
|
4
|
+
|
|
5
|
+
// src/fetch.ts
|
|
6
|
+
import {
|
|
7
|
+
fetch as undiciFetch
|
|
8
|
+
} from "undici";
|
|
9
|
+
async function fetch(url, options) {
|
|
10
|
+
if (!options.cache) {
|
|
11
|
+
throw new Error("Fetch options must include a cache instance or options.");
|
|
12
|
+
}
|
|
13
|
+
const fetchOptions = {
|
|
14
|
+
...options,
|
|
15
|
+
cache: "no-cache"
|
|
16
|
+
};
|
|
17
|
+
return options.cache.getOrSet(url, async () => {
|
|
18
|
+
const response = await undiciFetch(url, fetchOptions);
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
throw new Error(`Fetch failed with status ${response.status}`);
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var CacheableNet = class extends Hookified {
|
|
28
|
+
_cache = new Cacheable();
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super(options);
|
|
31
|
+
if (options?.cache) {
|
|
32
|
+
this._cache = options.cache instanceof Cacheable ? options.cache : new Cacheable(options.cache);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
get cache() {
|
|
36
|
+
return this._cache;
|
|
37
|
+
}
|
|
38
|
+
set cache(value) {
|
|
39
|
+
this._cache = value;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Fetch data from a URL with optional request options. Will use the cache that is already set in the instance.
|
|
43
|
+
* @param {string} url The URL to fetch.
|
|
44
|
+
* @param {FetchRequestInit} options Optional request options.
|
|
45
|
+
* @returns {Promise<FetchResponse>} The response from the fetch.
|
|
46
|
+
*/
|
|
47
|
+
async fetch(url, options) {
|
|
48
|
+
const fetchOptions = {
|
|
49
|
+
...options,
|
|
50
|
+
cache: this._cache
|
|
51
|
+
};
|
|
52
|
+
return fetch(url, fetchOptions);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var Net = CacheableNet;
|
|
56
|
+
export {
|
|
57
|
+
CacheableNet,
|
|
58
|
+
Net,
|
|
59
|
+
fetch
|
|
60
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cacheable/net",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "High Performance Network Caching for Node.js with fetch, request, http 1.1, and http 2 support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/jaredwray/cacheable.git",
|
|
18
|
+
"directory": "packages/cacheable"
|
|
19
|
+
},
|
|
20
|
+
"author": "Jared Wray <me@jaredwray.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"private": false,
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@biomejs/biome": "^2.2.2",
|
|
25
|
+
"@faker-js/faker": "^10.0.0",
|
|
26
|
+
"@types/node": "^24.3.0",
|
|
27
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
28
|
+
"rimraf": "^6.0.1",
|
|
29
|
+
"tsup": "^8.5.0",
|
|
30
|
+
"typescript": "^5.9.2",
|
|
31
|
+
"vitest": "^3.2.4"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"hookified": "^1.12.0",
|
|
35
|
+
"undici": "^7.15.0",
|
|
36
|
+
"cacheable": "^1.10.4"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"cacheable",
|
|
40
|
+
"http caching",
|
|
41
|
+
"fetch caching",
|
|
42
|
+
"request caching",
|
|
43
|
+
"http 1.1 caching",
|
|
44
|
+
"http 2 caching",
|
|
45
|
+
"dns caching",
|
|
46
|
+
"whois caching",
|
|
47
|
+
"high performance",
|
|
48
|
+
"layer 1 caching",
|
|
49
|
+
"layer 2 caching",
|
|
50
|
+
"distributed caching",
|
|
51
|
+
"keyv",
|
|
52
|
+
"expiration",
|
|
53
|
+
"CacheableMemory",
|
|
54
|
+
"distributed sync",
|
|
55
|
+
"secondary store",
|
|
56
|
+
"primary store",
|
|
57
|
+
"cache statistics",
|
|
58
|
+
"layered caching",
|
|
59
|
+
"fault tolerant",
|
|
60
|
+
"in-memory cache",
|
|
61
|
+
"distributed cache",
|
|
62
|
+
"lru",
|
|
63
|
+
"multi-tier cache"
|
|
64
|
+
],
|
|
65
|
+
"files": [
|
|
66
|
+
"dist",
|
|
67
|
+
"LICENSE"
|
|
68
|
+
],
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
|
|
71
|
+
"prepublish": "pnpm build",
|
|
72
|
+
"lint": "biome check --write --error-on-warnings",
|
|
73
|
+
"test": "pnpm lint && vitest run --coverage",
|
|
74
|
+
"test:ci": "biome check --error-on-warnings && vitest run --coverage",
|
|
75
|
+
"clean": "rimraf ./dist ./coverage ./node_modules"
|
|
76
|
+
}
|
|
77
|
+
}
|