@orpc/standard-server 0.0.0-next.fea68c1 → 0.0.0-next.ff2168f
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 +40 -27
- package/dist/batch/index.d.mts +30 -0
- package/dist/batch/index.d.ts +30 -0
- package/dist/batch/index.mjs +150 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.mjs +268 -0
- package/dist/shared/standard-server.BD8Sg1A5.d.mts +38 -0
- package/dist/shared/standard-server.BD8Sg1A5.d.ts +38 -0
- package/package.json +12 -13
- package/dist/index.js +0 -196
- package/dist/src/event-source/decoder.d.ts +0 -16
- package/dist/src/event-source/encoder.d.ts +0 -7
- package/dist/src/event-source/errors.d.ts +0 -15
- package/dist/src/event-source/index.d.ts +0 -6
- package/dist/src/event-source/meta.d.ts +0 -5
- package/dist/src/event-source/types.d.ts +0 -10
- package/dist/src/index.d.ts +0 -4
- package/dist/src/types.d.ts +0 -25
- package/dist/src/utils.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,68 +1,81 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<image align="center" src="https://orpc.
|
|
2
|
+
<image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
|
|
3
3
|
</div>
|
|
4
4
|
|
|
5
5
|
<h1></h1>
|
|
6
6
|
|
|
7
7
|
<div align="center">
|
|
8
|
-
<a href="https://codecov.io/gh/
|
|
9
|
-
<img alt="codecov" src="https://codecov.io/gh/
|
|
8
|
+
<a href="https://codecov.io/gh/middleapi/orpc">
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
10
10
|
</a>
|
|
11
11
|
<a href="https://www.npmjs.com/package/@orpc/standard-server">
|
|
12
12
|
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fstandard-server?logo=npm" />
|
|
13
13
|
</a>
|
|
14
|
-
<a href="https://github.com/
|
|
15
|
-
<img alt="MIT License" src="https://img.shields.io/github/license/
|
|
14
|
+
<a href="https://github.com/middleapi/orpc/blob/main/LICENSE">
|
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/middleapi/orpc?logo=open-source-initiative" />
|
|
16
16
|
</a>
|
|
17
17
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
18
|
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
19
|
</a>
|
|
20
|
+
<a href="https://deepwiki.com/middleapi/orpc">
|
|
21
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
22
|
+
</a>
|
|
20
23
|
</div>
|
|
21
24
|
|
|
22
25
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
26
|
|
|
24
|
-
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
27
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
25
28
|
|
|
26
29
|
---
|
|
27
30
|
|
|
28
31
|
## Highlights
|
|
29
32
|
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
- **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
|
|
43
|
-
- **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
|
|
44
|
-
- **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
|
|
45
|
-
- **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
|
|
33
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
34
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
35
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
36
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
37
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
38
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
39
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
40
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
41
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
42
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
43
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
44
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
46
45
|
|
|
47
46
|
## Documentation
|
|
48
47
|
|
|
49
|
-
You can find the full documentation [here](https://orpc.
|
|
48
|
+
You can find the full documentation [here](https://orpc.dev).
|
|
50
49
|
|
|
51
50
|
## Packages
|
|
52
51
|
|
|
53
52
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
53
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
54
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
56
|
-
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
57
|
-
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
58
|
-
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
55
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
56
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
57
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
59
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
60
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
61
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
62
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
60
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
64
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
65
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
66
|
|
|
62
67
|
## `@orpc/standard-server`
|
|
63
68
|
|
|
64
69
|
This package is designed to be used as a base for other server implementations.
|
|
65
70
|
|
|
71
|
+
## Sponsors
|
|
72
|
+
|
|
73
|
+
<p align="center">
|
|
74
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
75
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
76
|
+
</a>
|
|
77
|
+
</p>
|
|
78
|
+
|
|
66
79
|
## License
|
|
67
80
|
|
|
68
|
-
Distributed under the MIT License. See [LICENSE](https://github.com/
|
|
81
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/main/LICENSE) for more information.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.mjs';
|
|
2
|
+
import { Promisable } from '@orpc/shared';
|
|
3
|
+
|
|
4
|
+
interface ToBatchRequestOptions {
|
|
5
|
+
url: URL;
|
|
6
|
+
method: 'GET' | 'POST';
|
|
7
|
+
headers: StandardHeaders;
|
|
8
|
+
requests: readonly StandardRequest[];
|
|
9
|
+
}
|
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
|
12
|
+
|
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
|
15
|
+
index: number;
|
|
16
|
+
}
|
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
|
19
|
+
/**
|
|
20
|
+
* @default 'streaming'
|
|
21
|
+
*/
|
|
22
|
+
mode?: BatchResponseMode;
|
|
23
|
+
}
|
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
|
26
|
+
|
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
28
|
+
|
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { S as StandardHeaders, c as StandardRequest, e as StandardResponse } from '../shared/standard-server.BD8Sg1A5.js';
|
|
2
|
+
import { Promisable } from '@orpc/shared';
|
|
3
|
+
|
|
4
|
+
interface ToBatchRequestOptions {
|
|
5
|
+
url: URL;
|
|
6
|
+
method: 'GET' | 'POST';
|
|
7
|
+
headers: StandardHeaders;
|
|
8
|
+
requests: readonly StandardRequest[];
|
|
9
|
+
}
|
|
10
|
+
declare function toBatchRequest(options: ToBatchRequestOptions): StandardRequest;
|
|
11
|
+
declare function parseBatchRequest(request: StandardRequest): StandardRequest[];
|
|
12
|
+
|
|
13
|
+
type BatchResponseMode = 'streaming' | 'buffered';
|
|
14
|
+
interface BatchResponseBodyItem extends StandardResponse {
|
|
15
|
+
index: number;
|
|
16
|
+
}
|
|
17
|
+
interface ToBatchResponseOptions extends StandardResponse {
|
|
18
|
+
body: AsyncIteratorObject<BatchResponseBodyItem>;
|
|
19
|
+
/**
|
|
20
|
+
* @default 'streaming'
|
|
21
|
+
*/
|
|
22
|
+
mode?: BatchResponseMode;
|
|
23
|
+
}
|
|
24
|
+
declare function toBatchResponse(options: ToBatchResponseOptions): Promisable<StandardResponse>;
|
|
25
|
+
declare function parseBatchResponse(response: StandardResponse): AsyncGenerator<BatchResponseBodyItem>;
|
|
26
|
+
|
|
27
|
+
declare function toBatchAbortSignal(signals: readonly (AbortSignal | undefined)[]): AbortSignal | undefined;
|
|
28
|
+
|
|
29
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
|
30
|
+
export type { BatchResponseBodyItem, BatchResponseMode, ToBatchRequestOptions, ToBatchResponseOptions };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { stringifyJSON, parseEmptyableJSON, AsyncIteratorClass, isAsyncIteratorObject, isObject } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
function toBatchAbortSignal(signals) {
|
|
4
|
+
const realSignals = signals.filter((signal) => signal !== void 0);
|
|
5
|
+
if (realSignals.length === 0 || realSignals.length !== signals.length) {
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
8
|
+
const controller = new AbortController();
|
|
9
|
+
const abortIfAllInputsAborted = () => {
|
|
10
|
+
if (realSignals.every((signal) => signal.aborted)) {
|
|
11
|
+
controller.abort();
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
abortIfAllInputsAborted();
|
|
15
|
+
for (const signal of realSignals) {
|
|
16
|
+
signal.addEventListener("abort", () => {
|
|
17
|
+
abortIfAllInputsAborted();
|
|
18
|
+
}, {
|
|
19
|
+
once: true,
|
|
20
|
+
signal: controller.signal
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return controller.signal;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function toBatchRequest(options) {
|
|
27
|
+
const url = new URL(options.url);
|
|
28
|
+
let body;
|
|
29
|
+
const batchRequestItems = options.requests.map((request) => ({
|
|
30
|
+
body: request.body,
|
|
31
|
+
headers: Object.keys(request.headers).length ? request.headers : void 0,
|
|
32
|
+
method: request.method === options.method ? void 0 : request.method,
|
|
33
|
+
url: request.url
|
|
34
|
+
}));
|
|
35
|
+
if (options.method === "GET") {
|
|
36
|
+
url.searchParams.append("batch", stringifyJSON(batchRequestItems));
|
|
37
|
+
} else if (options.method === "POST") {
|
|
38
|
+
body = batchRequestItems;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
method: options.method,
|
|
42
|
+
url,
|
|
43
|
+
headers: options.headers,
|
|
44
|
+
body,
|
|
45
|
+
signal: toBatchAbortSignal(options.requests.map((request) => request.signal))
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function parseBatchRequest(request) {
|
|
49
|
+
const items = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("batch").at(-1)) : request.body;
|
|
50
|
+
if (!Array.isArray(items)) {
|
|
51
|
+
throw new TypeError("Invalid batch request");
|
|
52
|
+
}
|
|
53
|
+
return items.map((item) => {
|
|
54
|
+
return {
|
|
55
|
+
method: item.method ?? request.method,
|
|
56
|
+
url: new URL(item.url),
|
|
57
|
+
headers: item.headers ?? {},
|
|
58
|
+
body: item.body,
|
|
59
|
+
signal: request.signal
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function toBatchResponse(options) {
|
|
65
|
+
const mode = options.mode ?? "streaming";
|
|
66
|
+
const minifyResponseItem = (item) => {
|
|
67
|
+
return {
|
|
68
|
+
index: item.index,
|
|
69
|
+
status: item.status === options.status ? void 0 : item.status,
|
|
70
|
+
headers: Object.keys(item.headers).length ? item.headers : void 0,
|
|
71
|
+
body: item.body
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
if (mode === "buffered") {
|
|
75
|
+
return (async () => {
|
|
76
|
+
try {
|
|
77
|
+
const body = [];
|
|
78
|
+
for await (const item of options.body) {
|
|
79
|
+
body.push(minifyResponseItem(item));
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
headers: options.headers,
|
|
83
|
+
status: options.status,
|
|
84
|
+
body
|
|
85
|
+
};
|
|
86
|
+
} finally {
|
|
87
|
+
await options.body.return?.();
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
headers: options.headers,
|
|
93
|
+
status: options.status,
|
|
94
|
+
body: new AsyncIteratorClass(
|
|
95
|
+
async () => {
|
|
96
|
+
const { done, value } = await options.body.next();
|
|
97
|
+
if (done) {
|
|
98
|
+
return { done, value };
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
done,
|
|
102
|
+
value: {
|
|
103
|
+
index: value.index,
|
|
104
|
+
status: value.status === options.status ? void 0 : value.status,
|
|
105
|
+
headers: Object.keys(value.headers).length ? value.headers : void 0,
|
|
106
|
+
body: value.body
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
async (reason) => {
|
|
111
|
+
if (reason !== "next") {
|
|
112
|
+
await options.body.return?.();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function parseBatchResponse(response) {
|
|
119
|
+
const body = response.body;
|
|
120
|
+
if (isAsyncIteratorObject(body) || Array.isArray(body)) {
|
|
121
|
+
const iterator = (async function* () {
|
|
122
|
+
for await (const item of body) {
|
|
123
|
+
if (!isObject(item) || !("index" in item) || typeof item.index !== "number") {
|
|
124
|
+
throw new TypeError("Invalid batch response", {
|
|
125
|
+
cause: item
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
yield {
|
|
129
|
+
index: item.index,
|
|
130
|
+
status: item.status ?? response.status,
|
|
131
|
+
headers: item.headers ?? {},
|
|
132
|
+
body: item.body
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
})();
|
|
136
|
+
return new AsyncIteratorClass(
|
|
137
|
+
() => iterator.next(),
|
|
138
|
+
async (reason) => {
|
|
139
|
+
if (reason !== "next" && isAsyncIteratorObject(body)) {
|
|
140
|
+
await body.return?.();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
throw new TypeError("Invalid batch response", {
|
|
146
|
+
cause: response
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export { parseBatchRequest, parseBatchResponse, toBatchAbortSignal, toBatchRequest, toBatchResponse };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@orpc/shared';
|
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.mjs';
|
|
4
|
+
|
|
5
|
+
interface EventMessage {
|
|
6
|
+
event: string | undefined;
|
|
7
|
+
id: string | undefined;
|
|
8
|
+
data: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
11
|
+
*/
|
|
12
|
+
retry: number | undefined;
|
|
13
|
+
comments: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
17
|
+
interface EventDecoderOptions {
|
|
18
|
+
onEvent?: (event: EventMessage) => void;
|
|
19
|
+
}
|
|
20
|
+
declare class EventDecoder {
|
|
21
|
+
private options;
|
|
22
|
+
private incomplete;
|
|
23
|
+
constructor(options?: EventDecoderOptions);
|
|
24
|
+
feed(chunk: string): void;
|
|
25
|
+
end(): void;
|
|
26
|
+
}
|
|
27
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare function assertEventId(id: string): void;
|
|
32
|
+
declare function assertEventName(event: string): void;
|
|
33
|
+
declare function assertEventRetry(retry: number): void;
|
|
34
|
+
declare function assertEventComment(comment: string): void;
|
|
35
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
36
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
37
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
38
|
+
|
|
39
|
+
declare class EventEncoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
declare class EventDecoderError extends TypeError {
|
|
42
|
+
}
|
|
43
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
44
|
+
message?: string;
|
|
45
|
+
data?: unknown;
|
|
46
|
+
}
|
|
47
|
+
declare class ErrorEvent extends Error {
|
|
48
|
+
data: unknown;
|
|
49
|
+
constructor(options?: ErrorEventOptions);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
53
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
54
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
55
|
+
|
|
56
|
+
interface HibernationEventIteratorCallback {
|
|
57
|
+
(id: string): void;
|
|
58
|
+
}
|
|
59
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
|
60
|
+
/**
|
|
61
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
62
|
+
*/
|
|
63
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
|
64
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
declare function generateContentDisposition(filename: string): string;
|
|
68
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
|
69
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
|
70
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
|
71
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
|
72
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
|
73
|
+
|
|
74
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
75
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@orpc/shared';
|
|
2
|
+
import { S as StandardHeaders, a as StandardLazyResponse } from './shared/standard-server.BD8Sg1A5.js';
|
|
3
|
+
export { b as StandardBody, d as StandardLazyRequest, c as StandardRequest, e as StandardResponse } from './shared/standard-server.BD8Sg1A5.js';
|
|
4
|
+
|
|
5
|
+
interface EventMessage {
|
|
6
|
+
event: string | undefined;
|
|
7
|
+
id: string | undefined;
|
|
8
|
+
data: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
11
|
+
*/
|
|
12
|
+
retry: number | undefined;
|
|
13
|
+
comments: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
17
|
+
interface EventDecoderOptions {
|
|
18
|
+
onEvent?: (event: EventMessage) => void;
|
|
19
|
+
}
|
|
20
|
+
declare class EventDecoder {
|
|
21
|
+
private options;
|
|
22
|
+
private incomplete;
|
|
23
|
+
constructor(options?: EventDecoderOptions);
|
|
24
|
+
feed(chunk: string): void;
|
|
25
|
+
end(): void;
|
|
26
|
+
}
|
|
27
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare function assertEventId(id: string): void;
|
|
32
|
+
declare function assertEventName(event: string): void;
|
|
33
|
+
declare function assertEventRetry(retry: number): void;
|
|
34
|
+
declare function assertEventComment(comment: string): void;
|
|
35
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
36
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
37
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
38
|
+
|
|
39
|
+
declare class EventEncoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
declare class EventDecoderError extends TypeError {
|
|
42
|
+
}
|
|
43
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
44
|
+
message?: string;
|
|
45
|
+
data?: unknown;
|
|
46
|
+
}
|
|
47
|
+
declare class ErrorEvent extends Error {
|
|
48
|
+
data: unknown;
|
|
49
|
+
constructor(options?: ErrorEventOptions);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
53
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
54
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
55
|
+
|
|
56
|
+
interface HibernationEventIteratorCallback {
|
|
57
|
+
(id: string): void;
|
|
58
|
+
}
|
|
59
|
+
declare class HibernationEventIterator<T, TReturn = unknown, TNext = unknown> extends AsyncIteratorClass<T, TReturn, TNext> {
|
|
60
|
+
/**
|
|
61
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
62
|
+
*/
|
|
63
|
+
readonly hibernationCallback?: HibernationEventIteratorCallback;
|
|
64
|
+
constructor(hibernationCallback: HibernationEventIteratorCallback);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
declare function generateContentDisposition(filename: string): string;
|
|
68
|
+
declare function getFilenameFromContentDisposition(contentDisposition: string): string | undefined;
|
|
69
|
+
declare function mergeStandardHeaders(a: StandardHeaders, b: StandardHeaders): StandardHeaders;
|
|
70
|
+
declare function flattenHeader(header: string | readonly string[] | undefined): string | undefined;
|
|
71
|
+
declare function replicateStandardLazyResponse(response: StandardLazyResponse, count: number): StandardLazyResponse[];
|
|
72
|
+
declare function isEventIteratorHeaders(headers: StandardHeaders): boolean;
|
|
73
|
+
|
|
74
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, StandardHeaders, StandardLazyResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
75
|
+
export type { ErrorEventOptions, EventDecoderOptions, EventMessage, EventMeta, HibernationEventIteratorCallback };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { isTypescriptObject, AsyncIteratorClass, tryDecodeURIComponent, toArray, once, isAsyncIteratorObject, replicateAsyncIterator } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
class EventEncoderError extends TypeError {
|
|
4
|
+
}
|
|
5
|
+
class EventDecoderError extends TypeError {
|
|
6
|
+
}
|
|
7
|
+
class ErrorEvent extends Error {
|
|
8
|
+
data;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super(options?.message ?? "An error event was received", options);
|
|
11
|
+
this.data = options?.data;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function decodeEventMessage(encoded) {
|
|
16
|
+
const lines = encoded.replace(/\n+$/, "").split(/\n/);
|
|
17
|
+
const message = {
|
|
18
|
+
data: void 0,
|
|
19
|
+
event: void 0,
|
|
20
|
+
id: void 0,
|
|
21
|
+
retry: void 0,
|
|
22
|
+
comments: []
|
|
23
|
+
};
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
const index = line.indexOf(":");
|
|
26
|
+
const key = index === -1 ? line : line.slice(0, index);
|
|
27
|
+
const value = index === -1 ? "" : line.slice(index + 1).replace(/^\s/, "");
|
|
28
|
+
if (index === 0) {
|
|
29
|
+
message.comments.push(value);
|
|
30
|
+
} else if (key === "data") {
|
|
31
|
+
message.data ??= "";
|
|
32
|
+
message.data += `${value}
|
|
33
|
+
`;
|
|
34
|
+
} else if (key === "event") {
|
|
35
|
+
message.event = value;
|
|
36
|
+
} else if (key === "id") {
|
|
37
|
+
message.id = value;
|
|
38
|
+
} else if (key === "retry") {
|
|
39
|
+
const maybeInteger = Number.parseInt(value);
|
|
40
|
+
if (Number.isInteger(maybeInteger) && maybeInteger >= 0 && maybeInteger.toString() === value) {
|
|
41
|
+
message.retry = maybeInteger;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
message.data = message.data?.replace(/\n$/, "");
|
|
46
|
+
return message;
|
|
47
|
+
}
|
|
48
|
+
class EventDecoder {
|
|
49
|
+
constructor(options = {}) {
|
|
50
|
+
this.options = options;
|
|
51
|
+
}
|
|
52
|
+
incomplete = "";
|
|
53
|
+
feed(chunk) {
|
|
54
|
+
this.incomplete += chunk;
|
|
55
|
+
const lastCompleteIndex = this.incomplete.lastIndexOf("\n\n");
|
|
56
|
+
if (lastCompleteIndex === -1) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const completes = this.incomplete.slice(0, lastCompleteIndex).split(/\n\n/);
|
|
60
|
+
this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
|
|
61
|
+
for (const encoded of completes) {
|
|
62
|
+
const message = decodeEventMessage(`${encoded}
|
|
63
|
+
|
|
64
|
+
`);
|
|
65
|
+
if (this.options.onEvent) {
|
|
66
|
+
this.options.onEvent(message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
end() {
|
|
71
|
+
if (this.incomplete) {
|
|
72
|
+
throw new EventDecoderError("Event Iterator ended before complete");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class EventDecoderStream extends TransformStream {
|
|
77
|
+
constructor() {
|
|
78
|
+
let decoder;
|
|
79
|
+
super({
|
|
80
|
+
start(controller) {
|
|
81
|
+
decoder = new EventDecoder({
|
|
82
|
+
onEvent: (event) => {
|
|
83
|
+
controller.enqueue(event);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
transform(chunk) {
|
|
88
|
+
decoder.feed(chunk);
|
|
89
|
+
},
|
|
90
|
+
flush() {
|
|
91
|
+
decoder.end();
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function assertEventId(id) {
|
|
98
|
+
if (id.includes("\n")) {
|
|
99
|
+
throw new EventEncoderError("Event's id must not contain a newline character");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function assertEventName(event) {
|
|
103
|
+
if (event.includes("\n")) {
|
|
104
|
+
throw new EventEncoderError("Event's event must not contain a newline character");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function assertEventRetry(retry) {
|
|
108
|
+
if (!Number.isInteger(retry) || retry < 0) {
|
|
109
|
+
throw new EventEncoderError("Event's retry must be a integer and >= 0");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function assertEventComment(comment) {
|
|
113
|
+
if (comment.includes("\n")) {
|
|
114
|
+
throw new EventEncoderError("Event's comment must not contain a newline character");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function encodeEventData(data) {
|
|
118
|
+
const lines = data?.split(/\n/) ?? [];
|
|
119
|
+
let output = "";
|
|
120
|
+
for (const line of lines) {
|
|
121
|
+
output += `data: ${line}
|
|
122
|
+
`;
|
|
123
|
+
}
|
|
124
|
+
return output;
|
|
125
|
+
}
|
|
126
|
+
function encodeEventComments(comments) {
|
|
127
|
+
let output = "";
|
|
128
|
+
for (const comment of comments ?? []) {
|
|
129
|
+
assertEventComment(comment);
|
|
130
|
+
output += `: ${comment}
|
|
131
|
+
`;
|
|
132
|
+
}
|
|
133
|
+
return output;
|
|
134
|
+
}
|
|
135
|
+
function encodeEventMessage(message) {
|
|
136
|
+
let output = "";
|
|
137
|
+
output += encodeEventComments(message.comments);
|
|
138
|
+
if (message.event !== void 0) {
|
|
139
|
+
assertEventName(message.event);
|
|
140
|
+
output += `event: ${message.event}
|
|
141
|
+
`;
|
|
142
|
+
}
|
|
143
|
+
if (message.retry !== void 0) {
|
|
144
|
+
assertEventRetry(message.retry);
|
|
145
|
+
output += `retry: ${message.retry}
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
if (message.id !== void 0) {
|
|
149
|
+
assertEventId(message.id);
|
|
150
|
+
output += `id: ${message.id}
|
|
151
|
+
`;
|
|
152
|
+
}
|
|
153
|
+
output += encodeEventData(message.data);
|
|
154
|
+
output += "\n";
|
|
155
|
+
return output;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
|
159
|
+
function withEventMeta(container, meta) {
|
|
160
|
+
if (meta.id === void 0 && meta.retry === void 0 && !meta.comments?.length) {
|
|
161
|
+
return container;
|
|
162
|
+
}
|
|
163
|
+
if (meta.id !== void 0) {
|
|
164
|
+
assertEventId(meta.id);
|
|
165
|
+
}
|
|
166
|
+
if (meta.retry !== void 0) {
|
|
167
|
+
assertEventRetry(meta.retry);
|
|
168
|
+
}
|
|
169
|
+
if (meta.comments !== void 0) {
|
|
170
|
+
for (const comment of meta.comments) {
|
|
171
|
+
assertEventComment(comment);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return new Proxy(container, {
|
|
175
|
+
get(target, prop, receiver) {
|
|
176
|
+
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
|
177
|
+
return meta;
|
|
178
|
+
}
|
|
179
|
+
return Reflect.get(target, prop, receiver);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function getEventMeta(container) {
|
|
184
|
+
return isTypescriptObject(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
class HibernationEventIterator extends AsyncIteratorClass {
|
|
188
|
+
/**
|
|
189
|
+
* this property is not transferred to the client, so it should be optional for type safety
|
|
190
|
+
*/
|
|
191
|
+
hibernationCallback;
|
|
192
|
+
constructor(hibernationCallback) {
|
|
193
|
+
super(async () => {
|
|
194
|
+
throw new Error("Cannot iterate over hibernating iterator directly");
|
|
195
|
+
}, async (reason) => {
|
|
196
|
+
if (reason !== "next") {
|
|
197
|
+
throw new Error("Cannot cleanup hibernating iterator directly");
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
this.hibernationCallback = hibernationCallback;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function generateContentDisposition(filename) {
|
|
205
|
+
const escapedFileName = filename.replace(/"/g, '\\"');
|
|
206
|
+
const encodedFilenameStar = encodeURIComponent(filename).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%(7C|60|5E)/g, (str, hex) => String.fromCharCode(Number.parseInt(hex, 16)));
|
|
207
|
+
return `inline; filename="${escapedFileName}"; filename*=utf-8''${encodedFilenameStar}`;
|
|
208
|
+
}
|
|
209
|
+
function getFilenameFromContentDisposition(contentDisposition) {
|
|
210
|
+
const encodedFilenameStarMatch = contentDisposition.match(/filename\*=(UTF-8'')?([^;]*)/i);
|
|
211
|
+
if (encodedFilenameStarMatch && typeof encodedFilenameStarMatch[2] === "string") {
|
|
212
|
+
return tryDecodeURIComponent(encodedFilenameStarMatch[2]);
|
|
213
|
+
}
|
|
214
|
+
const encodedFilenameMatch = contentDisposition.match(/filename="((?:\\"|[^"])*)"/i);
|
|
215
|
+
if (encodedFilenameMatch && typeof encodedFilenameMatch[1] === "string") {
|
|
216
|
+
return encodedFilenameMatch[1].replace(/\\"/g, '"');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function mergeStandardHeaders(a, b) {
|
|
220
|
+
const merged = { ...a };
|
|
221
|
+
for (const key in b) {
|
|
222
|
+
if (Array.isArray(b[key])) {
|
|
223
|
+
merged[key] = [...toArray(merged[key]), ...b[key]];
|
|
224
|
+
} else if (b[key] !== void 0) {
|
|
225
|
+
if (Array.isArray(merged[key])) {
|
|
226
|
+
merged[key] = [...merged[key], b[key]];
|
|
227
|
+
} else if (merged[key] !== void 0) {
|
|
228
|
+
merged[key] = [merged[key], b[key]];
|
|
229
|
+
} else {
|
|
230
|
+
merged[key] = b[key];
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return merged;
|
|
235
|
+
}
|
|
236
|
+
function flattenHeader(header) {
|
|
237
|
+
if (typeof header === "string" || header === void 0) {
|
|
238
|
+
return header;
|
|
239
|
+
}
|
|
240
|
+
if (header.length === 0) {
|
|
241
|
+
return void 0;
|
|
242
|
+
}
|
|
243
|
+
return header.join(", ");
|
|
244
|
+
}
|
|
245
|
+
function replicateStandardLazyResponse(response, count) {
|
|
246
|
+
const replicated = [];
|
|
247
|
+
let bodyPromise;
|
|
248
|
+
let replicatedAsyncIteratorObjects;
|
|
249
|
+
for (let i = 0; i < count; i++) {
|
|
250
|
+
replicated.push({
|
|
251
|
+
...response,
|
|
252
|
+
body: once(async () => {
|
|
253
|
+
const body = await (bodyPromise ??= response.body());
|
|
254
|
+
if (!isAsyncIteratorObject(body)) {
|
|
255
|
+
return body;
|
|
256
|
+
}
|
|
257
|
+
replicatedAsyncIteratorObjects ??= replicateAsyncIterator(body, count);
|
|
258
|
+
return replicatedAsyncIteratorObjects.shift();
|
|
259
|
+
})
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
return replicated;
|
|
263
|
+
}
|
|
264
|
+
function isEventIteratorHeaders(headers) {
|
|
265
|
+
return Boolean(flattenHeader(headers["content-type"])?.startsWith("text/event-stream") && flattenHeader(headers["content-disposition"]) === void 0);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, HibernationEventIterator, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, flattenHeader, generateContentDisposition, getEventMeta, getFilenameFromContentDisposition, isEventIteratorHeaders, mergeStandardHeaders, replicateStandardLazyResponse, withEventMeta };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface StandardHeaders {
|
|
2
|
+
[key: string]: string | string[] | undefined;
|
|
3
|
+
}
|
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
5
|
+
interface StandardRequest {
|
|
6
|
+
method: string;
|
|
7
|
+
url: URL;
|
|
8
|
+
headers: StandardHeaders;
|
|
9
|
+
/**
|
|
10
|
+
* The body has been parsed based on the content-type header.
|
|
11
|
+
*/
|
|
12
|
+
body: StandardBody;
|
|
13
|
+
signal: AbortSignal | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
16
|
+
/**
|
|
17
|
+
* The body has been parsed based on the content-type header.
|
|
18
|
+
* This method can safely call multiple times (cached).
|
|
19
|
+
*/
|
|
20
|
+
body: () => Promise<StandardBody>;
|
|
21
|
+
}
|
|
22
|
+
interface StandardResponse {
|
|
23
|
+
status: number;
|
|
24
|
+
headers: StandardHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* The body has been parsed based on the content-type header.
|
|
27
|
+
*/
|
|
28
|
+
body: StandardBody;
|
|
29
|
+
}
|
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
31
|
+
/**
|
|
32
|
+
* The body has been parsed based on the content-type header.
|
|
33
|
+
* This method can safely call multiple times (cached).
|
|
34
|
+
*/
|
|
35
|
+
body: () => Promise<StandardBody>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface StandardHeaders {
|
|
2
|
+
[key: string]: string | string[] | undefined;
|
|
3
|
+
}
|
|
4
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
5
|
+
interface StandardRequest {
|
|
6
|
+
method: string;
|
|
7
|
+
url: URL;
|
|
8
|
+
headers: StandardHeaders;
|
|
9
|
+
/**
|
|
10
|
+
* The body has been parsed based on the content-type header.
|
|
11
|
+
*/
|
|
12
|
+
body: StandardBody;
|
|
13
|
+
signal: AbortSignal | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
16
|
+
/**
|
|
17
|
+
* The body has been parsed based on the content-type header.
|
|
18
|
+
* This method can safely call multiple times (cached).
|
|
19
|
+
*/
|
|
20
|
+
body: () => Promise<StandardBody>;
|
|
21
|
+
}
|
|
22
|
+
interface StandardResponse {
|
|
23
|
+
status: number;
|
|
24
|
+
headers: StandardHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* The body has been parsed based on the content-type header.
|
|
27
|
+
*/
|
|
28
|
+
body: StandardBody;
|
|
29
|
+
}
|
|
30
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
31
|
+
/**
|
|
32
|
+
* The body has been parsed based on the content-type header.
|
|
33
|
+
* This method can safely call multiple times (cached).
|
|
34
|
+
*/
|
|
35
|
+
body: () => Promise<StandardBody>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type { StandardHeaders as S, StandardLazyResponse as a, StandardBody as b, StandardRequest as c, StandardLazyRequest as d, StandardResponse as e };
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/standard-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.ff2168f",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"homepage": "https://
|
|
6
|
+
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/middleapi/orpc.git",
|
|
10
10
|
"directory": "packages/standard-server"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
@@ -14,25 +14,24 @@
|
|
|
14
14
|
],
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
|
-
"types": "./dist/
|
|
18
|
-
"import": "./dist/index.
|
|
19
|
-
"default": "./dist/index.
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
20
|
},
|
|
21
|
-
"
|
|
22
|
-
"types": "./dist/
|
|
21
|
+
"./batch": {
|
|
22
|
+
"types": "./dist/batch/index.d.mts",
|
|
23
|
+
"import": "./dist/batch/index.mjs",
|
|
24
|
+
"default": "./dist/batch/index.mjs"
|
|
23
25
|
}
|
|
24
26
|
},
|
|
25
27
|
"files": [
|
|
26
|
-
"!**/*.map",
|
|
27
|
-
"!**/*.tsbuildinfo",
|
|
28
28
|
"dist"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@orpc/shared": "0.0.0-next.fea68c1"
|
|
31
|
+
"@orpc/shared": "0.0.0-next.ff2168f"
|
|
33
32
|
},
|
|
34
33
|
"scripts": {
|
|
35
|
-
"build": "
|
|
34
|
+
"build": "unbuild",
|
|
36
35
|
"build:watch": "pnpm run build --watch",
|
|
37
36
|
"type:check": "tsc -b"
|
|
38
37
|
}
|
package/dist/index.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
// src/event-source/errors.ts
|
|
2
|
-
var EventEncoderError = class extends TypeError {
|
|
3
|
-
};
|
|
4
|
-
var EventDecoderError = class extends TypeError {
|
|
5
|
-
};
|
|
6
|
-
var ErrorEvent = class extends Error {
|
|
7
|
-
data;
|
|
8
|
-
constructor(options) {
|
|
9
|
-
super(options?.message ?? "An error event was received", options);
|
|
10
|
-
this.data = options?.data;
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
var UnknownEvent = class extends ErrorEvent {
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// src/event-source/decoder.ts
|
|
17
|
-
function decodeEventMessage(encoded) {
|
|
18
|
-
const lines = encoded.replace(/\n+$/, "").split(/\n/);
|
|
19
|
-
const message = {
|
|
20
|
-
data: "",
|
|
21
|
-
event: void 0,
|
|
22
|
-
id: void 0,
|
|
23
|
-
retry: void 0
|
|
24
|
-
};
|
|
25
|
-
for (const line of lines) {
|
|
26
|
-
const index = line.indexOf(": ");
|
|
27
|
-
if (index === -1) {
|
|
28
|
-
throw new EventDecoderError(`Invalid EventSource message line: ${line}`);
|
|
29
|
-
}
|
|
30
|
-
const key = line.slice(0, index);
|
|
31
|
-
const value = line.slice(index + 2);
|
|
32
|
-
if (key !== "data" && key in message && message[key] !== void 0) {
|
|
33
|
-
throw new EventDecoderError(`Duplicate EventSource message key: ${key}`);
|
|
34
|
-
}
|
|
35
|
-
if (key === "data") {
|
|
36
|
-
message.data += `${value}
|
|
37
|
-
`;
|
|
38
|
-
} else if (key === "event") {
|
|
39
|
-
message.event = value;
|
|
40
|
-
} else if (key === "id") {
|
|
41
|
-
message.id = value;
|
|
42
|
-
} else if (key === "retry") {
|
|
43
|
-
const maybeInteger = Number.parseInt(value);
|
|
44
|
-
if (!Number.isInteger(maybeInteger) || maybeInteger < 0 || maybeInteger.toString() !== value) {
|
|
45
|
-
throw new EventDecoderError(`Invalid EventSource message retry value: ${value}`);
|
|
46
|
-
}
|
|
47
|
-
message.retry = maybeInteger;
|
|
48
|
-
} else {
|
|
49
|
-
throw new EventDecoderError(`Unknown EventSource message key: ${key}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
message.data = message.data.replace(/\n$/, "");
|
|
53
|
-
return message;
|
|
54
|
-
}
|
|
55
|
-
var EventDecoder = class {
|
|
56
|
-
constructor(options = {}) {
|
|
57
|
-
this.options = options;
|
|
58
|
-
}
|
|
59
|
-
incomplete = "";
|
|
60
|
-
feed(chunk) {
|
|
61
|
-
this.incomplete += chunk;
|
|
62
|
-
const lastCompleteIndex = this.incomplete.lastIndexOf("\n\n");
|
|
63
|
-
if (lastCompleteIndex === -1) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const completes = this.incomplete.slice(0, lastCompleteIndex + 2).split(/\n{2,}/);
|
|
67
|
-
this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
|
|
68
|
-
for (const encoded of completes) {
|
|
69
|
-
if (!encoded) {
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
const message = decodeEventMessage(`${encoded}
|
|
73
|
-
|
|
74
|
-
`);
|
|
75
|
-
if (this.options.onEvent) {
|
|
76
|
-
this.options.onEvent(message);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
this.incomplete = "";
|
|
80
|
-
}
|
|
81
|
-
end() {
|
|
82
|
-
this.feed("\n\n");
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
var EventDecoderStream = class extends TransformStream {
|
|
86
|
-
constructor() {
|
|
87
|
-
let decoder;
|
|
88
|
-
super({
|
|
89
|
-
start(controller) {
|
|
90
|
-
decoder = new EventDecoder({
|
|
91
|
-
onEvent: (event) => {
|
|
92
|
-
controller.enqueue(event);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
transform(chunk) {
|
|
97
|
-
decoder.feed(chunk);
|
|
98
|
-
},
|
|
99
|
-
flush() {
|
|
100
|
-
decoder.end();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
// src/event-source/encoder.ts
|
|
107
|
-
function assertEventId(id) {
|
|
108
|
-
if (id.includes("\n")) {
|
|
109
|
-
throw new EventEncoderError("Event-source id must not contain a newline character");
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function assertEventName(event) {
|
|
113
|
-
if (event.includes("\n")) {
|
|
114
|
-
throw new EventEncoderError("Event-source event must not contain a newline character");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
function assertEventRetry(retry) {
|
|
118
|
-
if (!Number.isInteger(retry) || retry < 0) {
|
|
119
|
-
throw new EventEncoderError("Event-source retry must be a integer and >= 0");
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
function encodeEventData(data) {
|
|
123
|
-
const lines = data ? data.split(/\n/) : [""];
|
|
124
|
-
let output = "";
|
|
125
|
-
for (const line of lines) {
|
|
126
|
-
output += `data: ${line}
|
|
127
|
-
`;
|
|
128
|
-
}
|
|
129
|
-
return output;
|
|
130
|
-
}
|
|
131
|
-
function encodeEventMessage(message) {
|
|
132
|
-
let output = "";
|
|
133
|
-
if (message.event !== void 0) {
|
|
134
|
-
assertEventName(message.event);
|
|
135
|
-
output += `event: ${message.event}
|
|
136
|
-
`;
|
|
137
|
-
}
|
|
138
|
-
if (message.retry !== void 0) {
|
|
139
|
-
assertEventRetry(message.retry);
|
|
140
|
-
output += `retry: ${message.retry}
|
|
141
|
-
`;
|
|
142
|
-
}
|
|
143
|
-
if (message.id !== void 0) {
|
|
144
|
-
assertEventId(message.id);
|
|
145
|
-
output += `id: ${message.id}
|
|
146
|
-
`;
|
|
147
|
-
}
|
|
148
|
-
output += encodeEventData(message.data);
|
|
149
|
-
output += "\n";
|
|
150
|
-
return output;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// src/event-source/meta.ts
|
|
154
|
-
import { isTypescriptObject } from "@orpc/shared";
|
|
155
|
-
var EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
|
156
|
-
function withEventMeta(container, meta) {
|
|
157
|
-
if (meta.id !== void 0) {
|
|
158
|
-
assertEventId(meta.id);
|
|
159
|
-
}
|
|
160
|
-
if (meta.retry !== void 0) {
|
|
161
|
-
assertEventRetry(meta.retry);
|
|
162
|
-
}
|
|
163
|
-
return new Proxy(container, {
|
|
164
|
-
get(target, prop, receiver) {
|
|
165
|
-
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
|
166
|
-
return meta;
|
|
167
|
-
}
|
|
168
|
-
return Reflect.get(target, prop, receiver);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
function getEventMeta(container) {
|
|
173
|
-
return isTypescriptObject(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// src/utils.ts
|
|
177
|
-
import { contentDisposition, parse } from "@tinyhttp/content-disposition";
|
|
178
|
-
export {
|
|
179
|
-
ErrorEvent,
|
|
180
|
-
EventDecoder,
|
|
181
|
-
EventDecoderError,
|
|
182
|
-
EventDecoderStream,
|
|
183
|
-
EventEncoderError,
|
|
184
|
-
UnknownEvent,
|
|
185
|
-
assertEventId,
|
|
186
|
-
assertEventName,
|
|
187
|
-
assertEventRetry,
|
|
188
|
-
contentDisposition,
|
|
189
|
-
decodeEventMessage,
|
|
190
|
-
encodeEventData,
|
|
191
|
-
encodeEventMessage,
|
|
192
|
-
getEventMeta,
|
|
193
|
-
parse as parseContentDisposition,
|
|
194
|
-
withEventMeta
|
|
195
|
-
};
|
|
196
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { EventMessage } from './types';
|
|
2
|
-
export declare function decodeEventMessage(encoded: string): EventMessage;
|
|
3
|
-
export interface EventDecoderOptions {
|
|
4
|
-
onEvent?: (event: EventMessage) => void;
|
|
5
|
-
}
|
|
6
|
-
export declare class EventDecoder {
|
|
7
|
-
private options;
|
|
8
|
-
private incomplete;
|
|
9
|
-
constructor(options?: EventDecoderOptions);
|
|
10
|
-
feed(chunk: string): void;
|
|
11
|
-
end(): void;
|
|
12
|
-
}
|
|
13
|
-
export declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
14
|
-
constructor();
|
|
15
|
-
}
|
|
16
|
-
//# sourceMappingURL=decoder.d.ts.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { EventMessage } from './types';
|
|
2
|
-
export declare function assertEventId(id: string): void;
|
|
3
|
-
export declare function assertEventName(event: string): void;
|
|
4
|
-
export declare function assertEventRetry(retry: number): void;
|
|
5
|
-
export declare function encodeEventData(data: string | undefined): string;
|
|
6
|
-
export declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
7
|
-
//# sourceMappingURL=encoder.d.ts.map
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export declare class EventEncoderError extends TypeError {
|
|
2
|
-
}
|
|
3
|
-
export declare class EventDecoderError extends TypeError {
|
|
4
|
-
}
|
|
5
|
-
export interface ErrorEventOptions extends ErrorOptions {
|
|
6
|
-
message?: string;
|
|
7
|
-
data?: unknown;
|
|
8
|
-
}
|
|
9
|
-
export declare class ErrorEvent extends Error {
|
|
10
|
-
data: unknown;
|
|
11
|
-
constructor(options?: ErrorEventOptions);
|
|
12
|
-
}
|
|
13
|
-
export declare class UnknownEvent extends ErrorEvent {
|
|
14
|
-
}
|
|
15
|
-
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { EventMessage } from './types';
|
|
2
|
-
export type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id'>>;
|
|
3
|
-
export declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
4
|
-
export declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
5
|
-
//# sourceMappingURL=meta.d.ts.map
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export interface EventMessage {
|
|
2
|
-
event: string | undefined;
|
|
3
|
-
id: string | undefined;
|
|
4
|
-
data: string;
|
|
5
|
-
/**
|
|
6
|
-
* The number of milliseconds to wait before retrying the event source if error occurs.
|
|
7
|
-
*/
|
|
8
|
-
retry: number | undefined;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/src/index.d.ts
DELETED
package/dist/src/types.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export interface StandardHeaders {
|
|
2
|
-
[key: string]: string | string[] | undefined;
|
|
3
|
-
}
|
|
4
|
-
export type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
5
|
-
export interface StandardRequest {
|
|
6
|
-
/**
|
|
7
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
8
|
-
*/
|
|
9
|
-
raw: Record<string, unknown>;
|
|
10
|
-
method: string;
|
|
11
|
-
url: URL;
|
|
12
|
-
headers: StandardHeaders;
|
|
13
|
-
/**
|
|
14
|
-
* The body has been parsed base on the content-type header.
|
|
15
|
-
* This method can safely call multiple times (cached).
|
|
16
|
-
*/
|
|
17
|
-
body: () => Promise<StandardBody>;
|
|
18
|
-
signal: AbortSignal | undefined;
|
|
19
|
-
}
|
|
20
|
-
export interface StandardResponse {
|
|
21
|
-
status: number;
|
|
22
|
-
headers: StandardHeaders;
|
|
23
|
-
body: StandardBody;
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/src/utils.d.ts
DELETED