@orpc/shared 2.0.0-beta.46 → 2.0.0-beta.48
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/README.md +6 -4
- package/dist/args.d.mts +2 -0
- package/dist/args.mjs +3 -0
- package/dist/array.d.mts +2 -0
- package/dist/array.mjs +7 -0
- package/dist/bigint.d.mts +9 -0
- package/dist/bigint.mjs +12 -0
- package/dist/buffer.d.mts +18 -0
- package/dist/buffer.mjs +66 -0
- package/dist/compare.d.mts +1 -0
- package/dist/compare.mjs +43 -0
- package/dist/consts.d.mts +1 -0
- package/dist/consts.mjs +1 -0
- package/dist/error.d.mts +1 -0
- package/dist/error.mjs +3 -0
- package/dist/function.d.mts +12 -0
- package/dist/function.mjs +31 -0
- package/dist/http.d.mts +29 -0
- package/dist/http.mjs +83 -0
- package/dist/id.d.mts +8 -0
- package/dist/id.mjs +7 -0
- package/dist/index.d.mts +25 -492
- package/dist/index.mjs +23 -1117
- package/dist/interceptor.d.mts +96 -0
- package/dist/interceptor.mjs +102 -0
- package/dist/iterator.d.mts +82 -0
- package/dist/iterator.mjs +151 -0
- package/dist/object.d.mts +64 -0
- package/dist/object.mjs +221 -0
- package/dist/plugin.d.mts +12 -0
- package/dist/plugin.mjs +58 -0
- package/dist/promise.d.mts +17 -0
- package/dist/promise.mjs +23 -0
- package/dist/proxy.d.mts +15 -0
- package/dist/proxy.mjs +15 -0
- package/dist/queue.d.mts +16 -0
- package/dist/queue.mjs +74 -0
- package/dist/signal.d.mts +13 -0
- package/dist/signal.mjs +74 -0
- package/dist/stream.d.mts +54 -0
- package/dist/stream.mjs +138 -0
- package/dist/tracing.d.mts +73 -0
- package/dist/tracing.mjs +89 -0
- package/dist/types.d.mts +32 -0
- package/dist/types.mjs +1 -0
- package/dist/value.d.mts +8 -0
- package/dist/value.mjs +6 -0
- package/package.json +3 -4
- package/dist/index.d.ts +0 -492
package/README.md
CHANGED
|
@@ -156,10 +156,11 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
156
156
|
<td align="center"><a href="https://github.com/christ12938?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="167" alt="christ12938"/><br />christ12938</a></td>
|
|
157
157
|
<td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="167" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
158
158
|
<td align="center"><a href="https://github.com/itigoore01?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="167" alt="shota"/><br />shota</a></td>
|
|
159
|
+
<td align="center"><a href="https://github.com/Nic13Gamer?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="167" alt="Nicholas"/><br />Nicholas</a></td>
|
|
159
160
|
<td align="center"><a href="https://github.com/ellis-driscoll?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&v=4" width="167" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
|
|
160
|
-
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
161
161
|
</tr>
|
|
162
162
|
<tr>
|
|
163
|
+
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
163
164
|
<td align="center"><a href="https://opencollective.com/guest-ac41de3b?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Orestis Ioannou"><img src="https://images.opencollective.com/guest-ac41de3b/avatar/460.png" width="167" alt="Orestis Ioannou"/><br />Orestis Ioannou</a></td>
|
|
164
165
|
<td align="center"><a href="https://automatio.ai/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Stefan Smiljkovic"><img src="https://avatars.githubusercontent.com/u/1984909?u=b7bf5bc40ed49df3c22f69d2da7b8d78709c49ed&v=4" width="167" alt="Stefan Smiljkovic"/><br />Stefan Smiljkovic</a></td>
|
|
165
166
|
</tr>
|
|
@@ -190,10 +191,9 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
190
191
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
191
192
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
192
193
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
193
|
-
<td align="center"><a href="https://
|
|
194
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
194
195
|
</tr>
|
|
195
196
|
<tr>
|
|
196
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
197
197
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
198
198
|
</tr>
|
|
199
199
|
</table>
|
|
@@ -206,7 +206,9 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
206
206
|
|
|
207
207
|
<table>
|
|
208
208
|
<tr>
|
|
209
|
-
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude
|
|
209
|
+
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
|
+
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
+
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
210
212
|
</tr>
|
|
211
213
|
</table>
|
|
212
214
|
|
package/dist/args.d.mts
ADDED
package/dist/args.mjs
ADDED
package/dist/array.d.mts
ADDED
package/dist/array.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default maximum digits, excluding a leading `-`, when parsing a `bigint` from a string.
|
|
3
|
+
*/
|
|
4
|
+
export declare const DEFAULT_MAX_BIGINT_DIGITS = 4300;
|
|
5
|
+
/**
|
|
6
|
+
* Parses a `bigint.toString()` string, or returns `undefined` if it is invalid or has more than `maxDigits` digits.
|
|
7
|
+
* The length is checked first, since `BigInt()` is super-linear in the digit count.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseBigIntString(value: string, maxDigits: number): bigint | undefined;
|
package/dist/bigint.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
export const DEFAULT_MAX_BIGINT_DIGITS = 4300;
|
|
3
|
+
|
|
4
|
+
const BIGINT_PATTERN = /^-?(?:0|[1-9]\d*)$/;
|
|
5
|
+
|
|
6
|
+
export function parseBigIntString(value, maxDigits) {
|
|
7
|
+
const digits = value.startsWith("-") ? value.length - 1 : value.length;
|
|
8
|
+
if (digits > maxDigits || !BIGINT_PATTERN.test(value)) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return BigInt(value);
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Arrayable } from "type-fest";
|
|
2
|
+
/**
|
|
3
|
+
* Load Request/Response/Blob/File/.. to a buffer (Uint8Array<ArrayBuffer>).
|
|
4
|
+
*
|
|
5
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
6
|
+
*/
|
|
7
|
+
export declare function loadBytes(source: Pick<Blob, "arrayBuffer" | "bytes">): ReturnType<Blob["bytes"]>;
|
|
8
|
+
/**
|
|
9
|
+
* Normalize text or binary-like inputs to either:
|
|
10
|
+
* - the original string value, or
|
|
11
|
+
* - a Uint8Array view over the source bytes.
|
|
12
|
+
*/
|
|
13
|
+
export declare function toStringOrBytes(source: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, "buffer" | "byteOffset" | "byteLength">>): string | Awaited<ReturnType<Blob["bytes"]>>;
|
|
14
|
+
/**
|
|
15
|
+
* Encodes bytes to standard, padded base64, as data URLs and many protocols require.
|
|
16
|
+
* Prefers the native `toBase64` or `Buffer`, which are far faster for large outputs.
|
|
17
|
+
*/
|
|
18
|
+
export declare function encodeBase64(bytes: Uint8Array): string;
|
package/dist/buffer.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
export async function loadBytes(source) {
|
|
3
|
+
if (typeof source.bytes === "function") {
|
|
4
|
+
|
|
5
|
+
return source.bytes();
|
|
6
|
+
}
|
|
7
|
+
return new Uint8Array(await source.arrayBuffer());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function toStringOrBytes(source) {
|
|
11
|
+
if (typeof source === "string") {
|
|
12
|
+
return source;
|
|
13
|
+
}
|
|
14
|
+
if (source instanceof ArrayBuffer) {
|
|
15
|
+
return new Uint8Array(source);
|
|
16
|
+
}
|
|
17
|
+
if (Array.isArray(source)) {
|
|
18
|
+
return concatBytes(source);
|
|
19
|
+
}
|
|
20
|
+
if (source instanceof Uint8Array) {
|
|
21
|
+
return source;
|
|
22
|
+
}
|
|
23
|
+
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
24
|
+
}
|
|
25
|
+
function toBytes(item) {
|
|
26
|
+
if (typeof item === "string") {
|
|
27
|
+
return new TextEncoder().encode(item);
|
|
28
|
+
}
|
|
29
|
+
if (item instanceof ArrayBuffer) {
|
|
30
|
+
return new Uint8Array(item);
|
|
31
|
+
}
|
|
32
|
+
if (item instanceof Uint8Array) {
|
|
33
|
+
return item;
|
|
34
|
+
}
|
|
35
|
+
return new Uint8Array(item.buffer, item.byteOffset, item.byteLength);
|
|
36
|
+
}
|
|
37
|
+
function concatBytes(items) {
|
|
38
|
+
const chunks = items.map(toBytes);
|
|
39
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
40
|
+
const result = new Uint8Array(totalLength);
|
|
41
|
+
let offset = 0;
|
|
42
|
+
for (const chunk of chunks) {
|
|
43
|
+
result.set(chunk, offset);
|
|
44
|
+
offset += chunk.byteLength;
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function encodeBase64(bytes) {
|
|
50
|
+
const native = bytes;
|
|
51
|
+
if (typeof native.toBase64 === "function") {
|
|
52
|
+
return native.toBase64();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const NodeBuffer = globalThis.process?.getBuiltinModule?.("node:buffer")?.Buffer;
|
|
57
|
+
if (NodeBuffer !== undefined) {
|
|
58
|
+
return NodeBuffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64");
|
|
59
|
+
}
|
|
60
|
+
const chunkSize = 8192;
|
|
61
|
+
let binaryString = "";
|
|
62
|
+
for (let i = 0; i < bytes.length; i += chunkSize) {
|
|
63
|
+
binaryString += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
64
|
+
}
|
|
65
|
+
return btoa(binaryString);
|
|
66
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isDeepEqual(a: unknown, b: unknown): boolean;
|
package/dist/compare.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { isPlainObject } from "./object.mjs";
|
|
2
|
+
export function isDeepEqual(a, b) {
|
|
3
|
+
return isDeepEqualInternal(a, b, new WeakMap());
|
|
4
|
+
}
|
|
5
|
+
function isDeepEqualInternal(a, b, visited) {
|
|
6
|
+
if (Object.is(a, b)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(a)) {
|
|
10
|
+
if (!Array.isArray(b) || a.length !== b.length) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
} else if (!isPlainObject(a) || !isPlainObject(b)) {
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const aRecord = a;
|
|
19
|
+
const bRecord = b;
|
|
20
|
+
const visitedMatches = visited.get(a);
|
|
21
|
+
if (visitedMatches?.has(b)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (visitedMatches) {
|
|
25
|
+
visitedMatches.add(b);
|
|
26
|
+
} else {
|
|
27
|
+
visited.set(a, new WeakSet([b]));
|
|
28
|
+
}
|
|
29
|
+
const aKeys = Object.keys(aRecord).filter((k) => aRecord[k] !== undefined);
|
|
30
|
+
const bKeys = Object.keys(bRecord).filter((k) => bRecord[k] !== undefined);
|
|
31
|
+
if (aKeys.length !== bKeys.length) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
for (const key of aKeys) {
|
|
35
|
+
if (!Object.hasOwn(bRecord, key)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (!isDeepEqualInternal(aRecord[key], bRecord[key], visited)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ORPC_NAME = "orpc";
|
package/dist/consts.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ORPC_NAME = "orpc";
|
package/dist/error.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isAbortError(error: unknown): error is Error;
|
package/dist/error.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any;
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a value is an async generator function (`async function*`).
|
|
4
|
+
*/
|
|
5
|
+
export declare function isAsyncGeneratorFunction(value: unknown): value is (...args: any[]) => AsyncGenerator;
|
|
6
|
+
export declare function once<T>(fn: () => T): () => T;
|
|
7
|
+
/**
|
|
8
|
+
* Executes the callback function after the current call stack has been cleared,
|
|
9
|
+
* waiting at least `delay` ms when `setTimeout` is available.
|
|
10
|
+
*/
|
|
11
|
+
export declare function defer(callback: () => void, delay?: number): void;
|
|
12
|
+
export declare function tryOrUndefined<T>(fn: () => T): undefined | T;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const AsyncGeneratorFunction = /* @__PURE__ */ (() => Object.getPrototypeOf(async function* () {}).constructor)();
|
|
2
|
+
|
|
3
|
+
export function isAsyncGeneratorFunction(value) {
|
|
4
|
+
return value instanceof AsyncGeneratorFunction;
|
|
5
|
+
}
|
|
6
|
+
export function once(fn) {
|
|
7
|
+
let cached;
|
|
8
|
+
return () => {
|
|
9
|
+
if (cached) {
|
|
10
|
+
return cached.result;
|
|
11
|
+
}
|
|
12
|
+
const result = fn();
|
|
13
|
+
cached = { result };
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function defer(callback, delay = 0) {
|
|
19
|
+
if (typeof setTimeout === "function") {
|
|
20
|
+
setTimeout(callback, delay);
|
|
21
|
+
} else {
|
|
22
|
+
Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function tryOrUndefined(fn) {
|
|
26
|
+
try {
|
|
27
|
+
return fn();
|
|
28
|
+
} catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/dist/http.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare function pathToHttpPath(path: readonly string[]): `/${string}`;
|
|
2
|
+
export declare function normalizeHttpPath(path: string): `/${string}`;
|
|
3
|
+
export declare function mergeHttpPath(a: `/${string}`, b: `/${string}`): `/${string}`;
|
|
4
|
+
export declare function matchesHttpPathPrefix(url: `/${string}`, prefix: `/${string}`): boolean;
|
|
5
|
+
export declare function matchesHttpPath(url: `/${string}`, path: `/${string}`): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Parse Accept-Encoding into each coding's q-value, where `0` means the coding is explicitly
|
|
8
|
+
* unacceptable. The `*` wildcard is kept under its own key; use {@link isAcceptableEncoding}
|
|
9
|
+
* to decide whether a coding is acceptable.
|
|
10
|
+
*
|
|
11
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-accept-encoding
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseAcceptEncodingQualities(header: string | undefined): Map<string, number>;
|
|
14
|
+
/**
|
|
15
|
+
* Whether the client accepts a coding, given qualities from {@link parseAcceptEncodingQualities}.
|
|
16
|
+
* An explicit q-value takes precedence over the `*` wildcard, so `gzip;q=0, *` never accepts gzip.
|
|
17
|
+
*
|
|
18
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-accept-encoding
|
|
19
|
+
*/
|
|
20
|
+
export declare function isAcceptableEncoding(qualities: Map<string, number>, coding: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Add `accept-encoding` to a Vary header value. The encoding is chosen from the request, so a
|
|
23
|
+
* shared cache must key on it or it will hand a compressed body to a client that cannot decode it.
|
|
24
|
+
*
|
|
25
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-vary
|
|
26
|
+
*/
|
|
27
|
+
export declare function varyByAcceptEncoding(vary: string | undefined): string;
|
|
28
|
+
export declare function isNoTransformCacheControl(cacheControl: string | undefined): boolean;
|
|
29
|
+
export declare function isCompressibleContentType(contentType: string | null | undefined): boolean;
|
package/dist/http.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { safeDecodeURIComponent, safeEncodeURIComponent } from "@standard-server/shared";
|
|
2
|
+
export function pathToHttpPath(path) {
|
|
3
|
+
return `/${path.map(safeEncodeURIComponent).join("/")}`;
|
|
4
|
+
}
|
|
5
|
+
export function normalizeHttpPath(path) {
|
|
6
|
+
const paths = path.split("/");
|
|
7
|
+
if (paths.at(0) === "") {
|
|
8
|
+
paths.shift();
|
|
9
|
+
}
|
|
10
|
+
return pathToHttpPath(paths.map(safeDecodeURIComponent));
|
|
11
|
+
}
|
|
12
|
+
export function mergeHttpPath(a, b) {
|
|
13
|
+
return `${a.endsWith("/") ? a.slice(0, -1) : a}${b}`;
|
|
14
|
+
}
|
|
15
|
+
export function matchesHttpPathPrefix(url, prefix) {
|
|
16
|
+
if (!url.startsWith(prefix)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const charAfterPrefix = url[prefix.length];
|
|
20
|
+
|
|
21
|
+
return charAfterPrefix === "/" || charAfterPrefix === "?" || charAfterPrefix === "#" || charAfterPrefix === undefined || prefix[prefix.length - 1] === "/";
|
|
22
|
+
}
|
|
23
|
+
export function matchesHttpPath(url, path) {
|
|
24
|
+
const pathWithoutEndSlash = path.endsWith("/") ? path.slice(0, path.length - 1) : path;
|
|
25
|
+
if (!url.startsWith(pathWithoutEndSlash)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
let charAfterPrefix = url[pathWithoutEndSlash.length];
|
|
29
|
+
if (charAfterPrefix === "/") {
|
|
30
|
+
charAfterPrefix = url[pathWithoutEndSlash.length + 1];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return charAfterPrefix === undefined || charAfterPrefix === "?" || charAfterPrefix === "#";
|
|
34
|
+
}
|
|
35
|
+
const ACCEPT_ENCODING_QUALITY_REGEX = /^\s*q=([\d.]+)\s*$/i;
|
|
36
|
+
|
|
37
|
+
export function parseAcceptEncodingQualities(header) {
|
|
38
|
+
const qualities = new Map();
|
|
39
|
+
for (const part of header?.split(",") ?? []) {
|
|
40
|
+
const [rawCoding, ...params] = part.split(";");
|
|
41
|
+
const coding = rawCoding.trim().toLowerCase();
|
|
42
|
+
if (coding === "") {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const quality = params.map((param) => ACCEPT_ENCODING_QUALITY_REGEX.exec(param)?.[1]).find((value) => value !== undefined);
|
|
46
|
+
qualities.set(coding, quality === undefined ? 1 : Number(quality));
|
|
47
|
+
}
|
|
48
|
+
return qualities;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isAcceptableEncoding(qualities, coding) {
|
|
52
|
+
return (qualities.get(coding) ?? qualities.get("*") ?? 0) > 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function varyByAcceptEncoding(vary) {
|
|
56
|
+
if (vary === undefined) {
|
|
57
|
+
return "accept-encoding";
|
|
58
|
+
}
|
|
59
|
+
const fields = vary.split(",").map((field) => field.trim().toLowerCase());
|
|
60
|
+
|
|
61
|
+
return fields.includes("accept-encoding") || fields.includes("*") ? vary : `${vary}, accept-encoding`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
|
|
65
|
+
export function isNoTransformCacheControl(cacheControl) {
|
|
66
|
+
if (cacheControl === undefined) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return CACHE_CONTROL_NO_TRANSFORM_REGEX.test(cacheControl);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
73
|
+
|
|
74
|
+
const MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH = 1024;
|
|
75
|
+
export function isCompressibleContentType(contentType) {
|
|
76
|
+
if (contentType === null || contentType === undefined) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (contentType.length > MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return COMPRESSIBLE_CONTENT_TYPE_REGEX.test(contentType);
|
|
83
|
+
}
|
package/dist/id.d.mts
ADDED