@orpc/shared 2.0.0-beta.48 → 2.0.0-beta.49
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 +4 -4
- package/dist/args.js +3 -0
- package/dist/array.js +7 -0
- package/dist/bigint.d.ts +9 -0
- package/dist/bigint.js +19 -0
- package/dist/buffer.d.ts +18 -0
- package/dist/buffer.js +77 -0
- package/dist/compare.js +45 -0
- package/dist/consts.js +1 -0
- package/dist/error.js +3 -0
- package/dist/{function.d.mts → function.d.ts} +5 -5
- package/dist/function.js +38 -0
- package/dist/{http.d.mts → http.d.ts} +16 -16
- package/dist/http.js +113 -0
- package/dist/id.d.ts +8 -0
- package/dist/id.js +13 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +32 -0
- package/dist/interceptor.d.ts +60 -0
- package/dist/interceptor.js +139 -0
- package/dist/iterator.d.ts +56 -0
- package/dist/iterator.js +157 -0
- package/dist/object.d.ts +60 -0
- package/dist/object.js +245 -0
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +61 -0
- package/dist/promise.d.ts +17 -0
- package/dist/promise.js +32 -0
- package/dist/proxy.d.ts +12 -0
- package/dist/proxy.js +24 -0
- package/dist/queue.d.ts +16 -0
- package/dist/queue.js +79 -0
- package/dist/signal.d.ts +18 -0
- package/dist/signal.js +85 -0
- package/dist/stream.d.ts +48 -0
- package/dist/stream.js +155 -0
- package/dist/tracing.d.ts +73 -0
- package/dist/tracing.js +94 -0
- package/dist/types.d.ts +26 -0
- package/dist/value.d.ts +2 -0
- package/dist/value.js +6 -0
- package/package.json +16 -18
- package/dist/args.mjs +0 -3
- package/dist/array.mjs +0 -7
- package/dist/bigint.d.mts +0 -9
- package/dist/bigint.mjs +0 -12
- package/dist/buffer.d.mts +0 -18
- package/dist/buffer.mjs +0 -66
- package/dist/compare.mjs +0 -43
- package/dist/consts.mjs +0 -1
- package/dist/error.mjs +0 -3
- package/dist/function.mjs +0 -31
- package/dist/http.mjs +0 -83
- package/dist/id.d.mts +0 -8
- package/dist/id.mjs +0 -7
- package/dist/index.d.mts +0 -25
- package/dist/index.mjs +0 -23
- package/dist/interceptor.d.mts +0 -96
- package/dist/interceptor.mjs +0 -102
- package/dist/iterator.d.mts +0 -82
- package/dist/iterator.mjs +0 -151
- package/dist/object.d.mts +0 -64
- package/dist/object.mjs +0 -221
- package/dist/plugin.d.mts +0 -12
- package/dist/plugin.mjs +0 -58
- package/dist/promise.d.mts +0 -17
- package/dist/promise.mjs +0 -23
- package/dist/proxy.d.mts +0 -15
- package/dist/proxy.mjs +0 -15
- package/dist/queue.d.mts +0 -16
- package/dist/queue.mjs +0 -74
- package/dist/signal.d.mts +0 -13
- package/dist/signal.mjs +0 -74
- package/dist/stream.d.mts +0 -54
- package/dist/stream.mjs +0 -138
- package/dist/tracing.d.mts +0 -73
- package/dist/tracing.mjs +0 -89
- package/dist/types.d.mts +0 -32
- package/dist/value.d.mts +0 -8
- package/dist/value.mjs +0 -6
- /package/dist/{args.d.mts → args.d.ts} +0 -0
- /package/dist/{array.d.mts → array.d.ts} +0 -0
- /package/dist/{compare.d.mts → compare.d.ts} +0 -0
- /package/dist/{consts.d.mts → consts.d.ts} +0 -0
- /package/dist/{error.d.mts → error.d.ts} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/shared">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fshared?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
210
|
<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
211
|
<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>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
package/dist/args.js
ADDED
package/dist/array.js
ADDED
package/dist/bigint.d.ts
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.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default maximum digits, excluding a leading `-`, when parsing a `bigint` from a string.
|
|
3
|
+
*/
|
|
4
|
+
export const DEFAULT_MAX_BIGINT_DIGITS = 4300;
|
|
5
|
+
/**
|
|
6
|
+
* Matches `bigint.toString()` output only.
|
|
7
|
+
*/
|
|
8
|
+
const BIGINT_PATTERN = /^-?(?:0|[1-9]\d*)$/;
|
|
9
|
+
/**
|
|
10
|
+
* Parses a `bigint.toString()` string, or returns `undefined` if it is invalid or has more than `maxDigits` digits.
|
|
11
|
+
* The length is checked first, since `BigInt()` is super-linear in the digit count.
|
|
12
|
+
*/
|
|
13
|
+
export function parseBigIntString(value, maxDigits) {
|
|
14
|
+
const digits = value.startsWith('-') ? value.length - 1 : value.length;
|
|
15
|
+
if (digits > maxDigits || !BIGINT_PATTERN.test(value)) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return BigInt(value);
|
|
19
|
+
}
|
package/dist/buffer.d.ts
ADDED
|
@@ -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.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load Request/Response/Blob/File/.. to a buffer (Uint8Array<ArrayBuffer>).
|
|
3
|
+
*
|
|
4
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
5
|
+
*/
|
|
6
|
+
export async function loadBytes(source) {
|
|
7
|
+
// oxlint-disable-next-line no-restricted-properties
|
|
8
|
+
if (typeof source.bytes === 'function') {
|
|
9
|
+
// oxlint-disable-next-line no-restricted-properties
|
|
10
|
+
return source.bytes();
|
|
11
|
+
}
|
|
12
|
+
return new Uint8Array(await source.arrayBuffer());
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Normalize text or binary-like inputs to either:
|
|
16
|
+
* - the original string value, or
|
|
17
|
+
* - a Uint8Array view over the source bytes.
|
|
18
|
+
*/
|
|
19
|
+
export function toStringOrBytes(source) {
|
|
20
|
+
if (typeof source === 'string') {
|
|
21
|
+
return source;
|
|
22
|
+
}
|
|
23
|
+
if (source instanceof ArrayBuffer) {
|
|
24
|
+
return new Uint8Array(source);
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(source)) {
|
|
27
|
+
return concatBytes(source);
|
|
28
|
+
}
|
|
29
|
+
if (source instanceof Uint8Array) {
|
|
30
|
+
return source;
|
|
31
|
+
}
|
|
32
|
+
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
33
|
+
}
|
|
34
|
+
function toBytes(item) {
|
|
35
|
+
if (typeof item === 'string') {
|
|
36
|
+
return new TextEncoder().encode(item);
|
|
37
|
+
}
|
|
38
|
+
if (item instanceof ArrayBuffer) {
|
|
39
|
+
return new Uint8Array(item);
|
|
40
|
+
}
|
|
41
|
+
if (item instanceof Uint8Array) {
|
|
42
|
+
return item;
|
|
43
|
+
}
|
|
44
|
+
return new Uint8Array(item.buffer, item.byteOffset, item.byteLength);
|
|
45
|
+
}
|
|
46
|
+
function concatBytes(items) {
|
|
47
|
+
const chunks = items.map(toBytes);
|
|
48
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
49
|
+
const result = new Uint8Array(totalLength);
|
|
50
|
+
let offset = 0;
|
|
51
|
+
for (const chunk of chunks) {
|
|
52
|
+
result.set(chunk, offset);
|
|
53
|
+
offset += chunk.byteLength;
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Encodes bytes to standard, padded base64, as data URLs and many protocols require.
|
|
59
|
+
* Prefers the native `toBase64` or `Buffer`, which are far faster for large outputs.
|
|
60
|
+
*/
|
|
61
|
+
export function encodeBase64(bytes) {
|
|
62
|
+
const native = bytes;
|
|
63
|
+
if (typeof native.toBase64 === 'function') {
|
|
64
|
+
return native.toBase64();
|
|
65
|
+
}
|
|
66
|
+
// loaded at runtime, so the package keeps working on runtimes without Node.js built-ins
|
|
67
|
+
const NodeBuffer = globalThis.process?.getBuiltinModule?.('node:buffer')?.Buffer;
|
|
68
|
+
if (NodeBuffer !== undefined) {
|
|
69
|
+
return NodeBuffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('base64');
|
|
70
|
+
}
|
|
71
|
+
const chunkSize = 8192; // 8KB chunks to stay well below call stack limits
|
|
72
|
+
let binaryString = '';
|
|
73
|
+
for (let i = 0; i < bytes.length; i += chunkSize) {
|
|
74
|
+
binaryString += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
75
|
+
}
|
|
76
|
+
return btoa(binaryString);
|
|
77
|
+
}
|
package/dist/compare.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isPlainObject } from './object.js';
|
|
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
|
+
}
|
|
14
|
+
else if (!isPlainObject(a) || !isPlainObject(b)) {
|
|
15
|
+
// Anything else (primitives, Date, RegExp, Map, Set, class instances, ...) is only equal when Object.is above matched:
|
|
16
|
+
// non-plain objects keep their state outside own enumerable keys, so they cannot be compared structurally
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const aRecord = a;
|
|
20
|
+
const bRecord = b;
|
|
21
|
+
const visitedMatches = visited.get(a);
|
|
22
|
+
if (visitedMatches?.has(b)) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (visitedMatches) {
|
|
26
|
+
visitedMatches.add(b);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
visited.set(a, new WeakSet([b]));
|
|
30
|
+
}
|
|
31
|
+
const aKeys = Object.keys(aRecord).filter((k) => aRecord[k] !== undefined);
|
|
32
|
+
const bKeys = Object.keys(bRecord).filter((k) => bRecord[k] !== undefined);
|
|
33
|
+
if (aKeys.length !== bKeys.length) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
for (const key of aKeys) {
|
|
37
|
+
if (!Object.hasOwn(bRecord, key)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (!isDeepEqualInternal(aRecord[key], bRecord[key], visited)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
package/dist/consts.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ORPC_NAME = 'orpc';
|
package/dist/error.js
ADDED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export type AnyFunction = (...args: any[]) => any;
|
|
2
2
|
/**
|
|
3
|
-
* Checks whether a value is an async generator function (`async function*`).
|
|
4
|
-
*/
|
|
3
|
+
* Checks whether a value is an async generator function (`async function*`).
|
|
4
|
+
*/
|
|
5
5
|
export declare function isAsyncGeneratorFunction(value: unknown): value is (...args: any[]) => AsyncGenerator;
|
|
6
6
|
export declare function once<T>(fn: () => T): () => T;
|
|
7
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
|
-
*/
|
|
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
11
|
export declare function defer(callback: () => void, delay?: number): void;
|
|
12
12
|
export declare function tryOrUndefined<T>(fn: () => T): undefined | T;
|
package/dist/function.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const AsyncGeneratorFunction = /* @__PURE__ */ (() => Object.getPrototypeOf(async function* () { }).constructor)();
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a value is an async generator function (`async function*`).
|
|
4
|
+
*/
|
|
5
|
+
export function isAsyncGeneratorFunction(value) {
|
|
6
|
+
return value instanceof AsyncGeneratorFunction;
|
|
7
|
+
}
|
|
8
|
+
export function once(fn) {
|
|
9
|
+
let cached;
|
|
10
|
+
return () => {
|
|
11
|
+
if (cached) {
|
|
12
|
+
return cached.result;
|
|
13
|
+
}
|
|
14
|
+
const result = fn();
|
|
15
|
+
cached = { result };
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Executes the callback function after the current call stack has been cleared,
|
|
21
|
+
* waiting at least `delay` ms when `setTimeout` is available.
|
|
22
|
+
*/
|
|
23
|
+
export function defer(callback, delay = 0) {
|
|
24
|
+
if (typeof setTimeout === 'function') {
|
|
25
|
+
setTimeout(callback, delay);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
void Promise.resolve().then(() => Promise.resolve().then(() => Promise.resolve().then(callback)));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function tryOrUndefined(fn) {
|
|
32
|
+
try {
|
|
33
|
+
return fn();
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -4,26 +4,26 @@ export declare function mergeHttpPath(a: `/${string}`, b: `/${string}`): `/${str
|
|
|
4
4
|
export declare function matchesHttpPathPrefix(url: `/${string}`, prefix: `/${string}`): boolean;
|
|
5
5
|
export declare function matchesHttpPath(url: `/${string}`, path: `/${string}`): boolean;
|
|
6
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
|
-
*/
|
|
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
13
|
export declare function parseAcceptEncodingQualities(header: string | undefined): Map<string, number>;
|
|
14
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
|
-
*/
|
|
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
20
|
export declare function isAcceptableEncoding(qualities: Map<string, number>, coding: string): boolean;
|
|
21
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
|
-
*/
|
|
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
27
|
export declare function varyByAcceptEncoding(vary: string | undefined): string;
|
|
28
28
|
export declare function isNoTransformCacheControl(cacheControl: string | undefined): boolean;
|
|
29
29
|
export declare function isCompressibleContentType(contentType: string | null | undefined): boolean;
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
// order by most common cases for better performance
|
|
21
|
+
return (charAfterPrefix === '/' ||
|
|
22
|
+
charAfterPrefix === '?' ||
|
|
23
|
+
charAfterPrefix === '#' ||
|
|
24
|
+
charAfterPrefix === undefined ||
|
|
25
|
+
prefix[prefix.length - 1] === '/');
|
|
26
|
+
}
|
|
27
|
+
export function matchesHttpPath(url, path) {
|
|
28
|
+
const pathWithoutEndSlash = path.endsWith('/') ? path.slice(0, path.length - 1) : path;
|
|
29
|
+
if (!url.startsWith(pathWithoutEndSlash)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
let charAfterPrefix = url[pathWithoutEndSlash.length];
|
|
33
|
+
if (charAfterPrefix === '/') {
|
|
34
|
+
charAfterPrefix = url[pathWithoutEndSlash.length + 1];
|
|
35
|
+
}
|
|
36
|
+
// order by most common cases for better performance
|
|
37
|
+
return charAfterPrefix === undefined || charAfterPrefix === '?' || charAfterPrefix === '#';
|
|
38
|
+
}
|
|
39
|
+
const ACCEPT_ENCODING_QUALITY_REGEX = /^\s*q=([\d.]+)\s*$/i;
|
|
40
|
+
/**
|
|
41
|
+
* Parse Accept-Encoding into each coding's q-value, where `0` means the coding is explicitly
|
|
42
|
+
* unacceptable. The `*` wildcard is kept under its own key; use {@link isAcceptableEncoding}
|
|
43
|
+
* to decide whether a coding is acceptable.
|
|
44
|
+
*
|
|
45
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-accept-encoding
|
|
46
|
+
*/
|
|
47
|
+
export function parseAcceptEncodingQualities(header) {
|
|
48
|
+
const qualities = new Map();
|
|
49
|
+
for (const part of header?.split(',') ?? []) {
|
|
50
|
+
const [rawCoding, ...params] = part.split(';');
|
|
51
|
+
const coding = rawCoding.trim().toLowerCase();
|
|
52
|
+
if (coding === '') {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const quality = params
|
|
56
|
+
.map((param) => ACCEPT_ENCODING_QUALITY_REGEX.exec(param)?.[1])
|
|
57
|
+
.find((value) => value !== undefined);
|
|
58
|
+
qualities.set(coding, quality === undefined ? 1 : Number(quality));
|
|
59
|
+
}
|
|
60
|
+
return qualities;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether the client accepts a coding, given qualities from {@link parseAcceptEncodingQualities}.
|
|
64
|
+
* An explicit q-value takes precedence over the `*` wildcard, so `gzip;q=0, *` never accepts gzip.
|
|
65
|
+
*
|
|
66
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-accept-encoding
|
|
67
|
+
*/
|
|
68
|
+
export function isAcceptableEncoding(qualities, coding) {
|
|
69
|
+
return (qualities.get(coding) ?? qualities.get('*') ?? 0) > 0;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Add `accept-encoding` to a Vary header value. The encoding is chosen from the request, so a
|
|
73
|
+
* shared cache must key on it or it will hand a compressed body to a client that cannot decode it.
|
|
74
|
+
*
|
|
75
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-vary
|
|
76
|
+
*/
|
|
77
|
+
export function varyByAcceptEncoding(vary) {
|
|
78
|
+
if (vary === undefined) {
|
|
79
|
+
return 'accept-encoding';
|
|
80
|
+
}
|
|
81
|
+
const fields = vary.split(',').map((field) => field.trim().toLowerCase());
|
|
82
|
+
// `*` already forbids reuse across requests, so narrowing it would be a downgrade
|
|
83
|
+
return fields.includes('accept-encoding') || fields.includes('*')
|
|
84
|
+
? vary
|
|
85
|
+
: `${vary}, accept-encoding`;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Whether Cache-Control includes the no-transform directive, which forbids transforming the body.
|
|
89
|
+
*
|
|
90
|
+
* @see https://www.rfc-editor.org/rfc/rfc9111.html#name-no-transform
|
|
91
|
+
*/
|
|
92
|
+
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
|
|
93
|
+
export function isNoTransformCacheControl(cacheControl) {
|
|
94
|
+
if (cacheControl === undefined) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return CACHE_CONTROL_NO_TRANSFORM_REGEX.test(cacheControl);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* inspired from Hono Compression Plugin
|
|
101
|
+
*/
|
|
102
|
+
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;
|
|
103
|
+
// Cap length to skip the regex on pathological inputs.
|
|
104
|
+
const MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH = 1024;
|
|
105
|
+
export function isCompressibleContentType(contentType) {
|
|
106
|
+
if (contentType === null || contentType === undefined) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if (contentType.length > MAX_COMPRESSIBLE_CONTENT_TYPE_LENGTH) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return COMPRESSIBLE_CONTENT_TYPE_REGEX.test(contentType);
|
|
113
|
+
}
|
package/dist/id.d.ts
ADDED
package/dist/id.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compares two sequential IDs.
|
|
3
|
+
* Returns:
|
|
4
|
+
* - negative if `a` < `b`
|
|
5
|
+
* - positive if `a` > `b`
|
|
6
|
+
* - 0 if equal
|
|
7
|
+
*/
|
|
8
|
+
export function compareSequentialIds(a, b) {
|
|
9
|
+
if (a.length !== b.length) {
|
|
10
|
+
return a.length - b.length;
|
|
11
|
+
}
|
|
12
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
13
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * from './args.ts';
|
|
2
|
+
export * from './array.ts';
|
|
3
|
+
export * from './bigint.ts';
|
|
4
|
+
export * from './buffer.ts';
|
|
5
|
+
export * from './compare.ts';
|
|
6
|
+
export * from './consts.ts';
|
|
7
|
+
export * from './error.ts';
|
|
8
|
+
export * from './function.ts';
|
|
9
|
+
export * from './http.ts';
|
|
10
|
+
export * from './id.ts';
|
|
11
|
+
export * from './interceptor.ts';
|
|
12
|
+
export * from './iterator.ts';
|
|
13
|
+
export * from './object.ts';
|
|
14
|
+
export * from './plugin.ts';
|
|
15
|
+
export * from './promise.ts';
|
|
16
|
+
export * from './proxy.ts';
|
|
17
|
+
export * from './queue.ts';
|
|
18
|
+
export * from './signal.ts';
|
|
19
|
+
export * from './stream.ts';
|
|
20
|
+
export * from './tracing.ts';
|
|
21
|
+
export * from './types.ts';
|
|
22
|
+
export * from './value.ts';
|
|
23
|
+
export {
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated Use `isPlainObject` instead.
|
|
26
|
+
*/
|
|
27
|
+
isPlainObject as isObject, } from './object.ts';
|
|
28
|
+
export { AbortError, AsyncIteratorClass, getOrBind, isAsyncIteratorObject, isNonPrimitive,
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `isNonPrimitive` instead.
|
|
31
|
+
*/
|
|
32
|
+
isNonPrimitive as isTypescriptObject, parseEmptyableJSON, safeDecodeURIComponent, safeEncodeURIComponent, sequential, SequentialIdGenerator, sleep, stringifyJSON, throwIfAborted, toArray, } from '@standard-server/shared';
|
|
33
|
+
export type { AsyncCleanupFn, AsyncIteratorClassNextFn } from '@standard-server/shared';
|
|
34
|
+
export type { Arrayable, IsEqual, PartialDeep, Promisable, Writable } from 'type-fest';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export * from './args.js';
|
|
2
|
+
export * from './array.js';
|
|
3
|
+
export * from './bigint.js';
|
|
4
|
+
export * from './buffer.js';
|
|
5
|
+
export * from './compare.js';
|
|
6
|
+
export * from './consts.js';
|
|
7
|
+
export * from './error.js';
|
|
8
|
+
export * from './function.js';
|
|
9
|
+
export * from './http.js';
|
|
10
|
+
export * from './id.js';
|
|
11
|
+
export * from './interceptor.js';
|
|
12
|
+
export * from './iterator.js';
|
|
13
|
+
export * from './object.js';
|
|
14
|
+
export * from './plugin.js';
|
|
15
|
+
export * from './promise.js';
|
|
16
|
+
export * from './proxy.js';
|
|
17
|
+
export * from './queue.js';
|
|
18
|
+
export * from './signal.js';
|
|
19
|
+
export * from './stream.js';
|
|
20
|
+
export * from './tracing.js';
|
|
21
|
+
export * from './types.js';
|
|
22
|
+
export * from './value.js';
|
|
23
|
+
export {
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated Use `isPlainObject` instead.
|
|
26
|
+
*/
|
|
27
|
+
isPlainObject as isObject, } from './object.js';
|
|
28
|
+
export { AbortError, AsyncIteratorClass, getOrBind, isAsyncIteratorObject, isNonPrimitive,
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `isNonPrimitive` instead.
|
|
31
|
+
*/
|
|
32
|
+
isNonPrimitive as isTypescriptObject, parseEmptyableJSON, safeDecodeURIComponent, safeEncodeURIComponent, sequential, SequentialIdGenerator, sleep, stringifyJSON, throwIfAborted, toArray, } from '@standard-server/shared';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Promisable } from 'type-fest';
|
|
2
|
+
import type { PromiseWithError, ThrowableError } from './types.ts';
|
|
3
|
+
export type InterceptableOptions = Record<string, any>;
|
|
4
|
+
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
5
|
+
next(options?: TOptions): TResult;
|
|
6
|
+
};
|
|
7
|
+
export type Interceptor<TOptions extends InterceptableOptions, TResult> = (options: InterceptorOptions<TOptions, TResult>) => TResult;
|
|
8
|
+
/**
|
|
9
|
+
* Can used for interceptors or middlewares
|
|
10
|
+
*/
|
|
11
|
+
export declare function onStart<T, TOptions extends {
|
|
12
|
+
next: () => any;
|
|
13
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
14
|
+
/**
|
|
15
|
+
* Can used for interceptors or middlewares
|
|
16
|
+
*/
|
|
17
|
+
export declare function onSuccess<T, TOptions extends {
|
|
18
|
+
next: () => any;
|
|
19
|
+
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an interceptor or middleware that invokes a callback whenever an error
|
|
22
|
+
* is thrown. The error is rethrown after the callback completes.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
25
|
+
*/
|
|
26
|
+
export declare function onError<T, TOptions extends {
|
|
27
|
+
next: () => any;
|
|
28
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
29
|
+
export type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess: false] | [error: null, data: TResult, isSuccess: true];
|
|
30
|
+
/**
|
|
31
|
+
* Can used for interceptors or middlewares
|
|
32
|
+
*/
|
|
33
|
+
export declare function onFinish<T, TOptions extends {
|
|
34
|
+
next: () => any;
|
|
35
|
+
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a middleware or interceptor that invokes a callback when the returned async
|
|
38
|
+
* iterator object throws an error while being consumed.
|
|
39
|
+
*
|
|
40
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
41
|
+
* initial call (before the interceptor returns the iterator), whereas this
|
|
42
|
+
* callback only fires while consuming the iterator. Use both together to
|
|
43
|
+
* catch all possible errors.
|
|
44
|
+
*/
|
|
45
|
+
export declare function onAsyncIteratorObjectError<T, TOptions extends {
|
|
46
|
+
next: () => any;
|
|
47
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError), options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
48
|
+
/**
|
|
49
|
+
* Creates an interceptor that invokes a callback when the returned readable
|
|
50
|
+
* stream errors while being consumed.
|
|
51
|
+
*
|
|
52
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
53
|
+
* initial call (before the interceptor returns the stream), whereas this
|
|
54
|
+
* callback only fires while consuming the stream. Use both together to catch
|
|
55
|
+
* all possible errors.
|
|
56
|
+
*/
|
|
57
|
+
export declare function onReadableStreamError<T, TOptions extends {
|
|
58
|
+
next: () => any;
|
|
59
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ThrowableError | (ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError), options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
60
|
+
export declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: undefined | Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|