@orpc/standard-server 0.0.0-next.b2e67f7 → 0.0.0-next.c0088c7
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 +68 -0
- package/dist/index.d.mts +99 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.mjs +186 -0
- package/package.json +6 -11
- package/dist/index.js +0 -225
- 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 -16
- package/dist/src/event-source/index.d.ts +0 -6
- package/dist/src/event-source/meta.d.ts +0 -6
- 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 -27
- package/dist/src/utils.d.ts +0 -6
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1></h1>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/standard-server">
|
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fstandard-server?logo=npm" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
|
+
</a>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
|
+
|
|
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, ensuring a smooth and enjoyable developer experience.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Highlights
|
|
29
|
+
|
|
30
|
+
- **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
|
|
31
|
+
- **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
|
|
32
|
+
- **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
|
|
33
|
+
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
|
|
34
|
+
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
|
|
35
|
+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue), Pinia Colada, and more.
|
|
36
|
+
- **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
|
|
37
|
+
- **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
|
|
38
|
+
- **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
|
|
39
|
+
- **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
|
|
40
|
+
- **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
|
|
41
|
+
- **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
|
|
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.
|
|
46
|
+
|
|
47
|
+
## Documentation
|
|
48
|
+
|
|
49
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
|
50
|
+
|
|
51
|
+
## Packages
|
|
52
|
+
|
|
53
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
|
+
- [@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
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
61
|
+
|
|
62
|
+
## `@orpc/standard-server`
|
|
63
|
+
|
|
64
|
+
This package is designed to be used as a base for other server implementations.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
|
2
|
+
|
|
3
|
+
interface EventMessage {
|
|
4
|
+
event: string | undefined;
|
|
5
|
+
id: string | undefined;
|
|
6
|
+
data: string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
9
|
+
*/
|
|
10
|
+
retry: number | undefined;
|
|
11
|
+
comments: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
15
|
+
interface EventDecoderOptions {
|
|
16
|
+
onEvent?: (event: EventMessage) => void;
|
|
17
|
+
}
|
|
18
|
+
declare class EventDecoder {
|
|
19
|
+
private options;
|
|
20
|
+
private incomplete;
|
|
21
|
+
constructor(options?: EventDecoderOptions);
|
|
22
|
+
feed(chunk: string): void;
|
|
23
|
+
end(): void;
|
|
24
|
+
}
|
|
25
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
26
|
+
constructor();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare function assertEventId(id: string): void;
|
|
30
|
+
declare function assertEventName(event: string): void;
|
|
31
|
+
declare function assertEventRetry(retry: number): void;
|
|
32
|
+
declare function assertEventComment(comment: string): void;
|
|
33
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
34
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
35
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
36
|
+
|
|
37
|
+
declare class EventEncoderError extends TypeError {
|
|
38
|
+
}
|
|
39
|
+
declare class EventDecoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
42
|
+
message?: string;
|
|
43
|
+
data?: unknown;
|
|
44
|
+
}
|
|
45
|
+
declare class ErrorEvent extends Error {
|
|
46
|
+
data: unknown;
|
|
47
|
+
constructor(options?: ErrorEventOptions);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
51
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
52
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
53
|
+
|
|
54
|
+
interface StandardHeaders {
|
|
55
|
+
[key: string]: string | string[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
58
|
+
interface StandardRequest {
|
|
59
|
+
method: string;
|
|
60
|
+
url: URL;
|
|
61
|
+
headers: StandardHeaders;
|
|
62
|
+
/**
|
|
63
|
+
* The body has been parsed based on the content-type header.
|
|
64
|
+
*/
|
|
65
|
+
body: StandardBody;
|
|
66
|
+
signal: AbortSignal | undefined;
|
|
67
|
+
}
|
|
68
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
69
|
+
/**
|
|
70
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
71
|
+
*/
|
|
72
|
+
raw: Record<string, unknown>;
|
|
73
|
+
/**
|
|
74
|
+
* The body has been parsed based on the content-type header.
|
|
75
|
+
* This method can safely call multiple times (cached).
|
|
76
|
+
*/
|
|
77
|
+
body: () => Promise<StandardBody>;
|
|
78
|
+
}
|
|
79
|
+
interface StandardResponse {
|
|
80
|
+
status: number;
|
|
81
|
+
headers: StandardHeaders;
|
|
82
|
+
/**
|
|
83
|
+
* The body has been parsed based on the content-type header.
|
|
84
|
+
*/
|
|
85
|
+
body: StandardBody;
|
|
86
|
+
}
|
|
87
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
88
|
+
/**
|
|
89
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
90
|
+
*/
|
|
91
|
+
raw: Record<string, unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* The body has been parsed based on the content-type header.
|
|
94
|
+
* This method can safely call multiple times (cached).
|
|
95
|
+
*/
|
|
96
|
+
body: () => Promise<StandardBody>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { ErrorEvent, type ErrorEventOptions, EventDecoder, EventDecoderError, type EventDecoderOptions, EventDecoderStream, EventEncoderError, type EventMessage, type EventMeta, type StandardBody, type StandardHeaders, type StandardLazyRequest, type StandardLazyResponse, type StandardRequest, type StandardResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, getEventMeta, withEventMeta };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
|
2
|
+
|
|
3
|
+
interface EventMessage {
|
|
4
|
+
event: string | undefined;
|
|
5
|
+
id: string | undefined;
|
|
6
|
+
data: string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The number of milliseconds to wait before retrying the event iterator if error occurs.
|
|
9
|
+
*/
|
|
10
|
+
retry: number | undefined;
|
|
11
|
+
comments: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function decodeEventMessage(encoded: string): EventMessage;
|
|
15
|
+
interface EventDecoderOptions {
|
|
16
|
+
onEvent?: (event: EventMessage) => void;
|
|
17
|
+
}
|
|
18
|
+
declare class EventDecoder {
|
|
19
|
+
private options;
|
|
20
|
+
private incomplete;
|
|
21
|
+
constructor(options?: EventDecoderOptions);
|
|
22
|
+
feed(chunk: string): void;
|
|
23
|
+
end(): void;
|
|
24
|
+
}
|
|
25
|
+
declare class EventDecoderStream extends TransformStream<string, EventMessage> {
|
|
26
|
+
constructor();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare function assertEventId(id: string): void;
|
|
30
|
+
declare function assertEventName(event: string): void;
|
|
31
|
+
declare function assertEventRetry(retry: number): void;
|
|
32
|
+
declare function assertEventComment(comment: string): void;
|
|
33
|
+
declare function encodeEventData(data: string | undefined): string;
|
|
34
|
+
declare function encodeEventComments(comments: string[] | undefined): string;
|
|
35
|
+
declare function encodeEventMessage(message: Partial<EventMessage>): string;
|
|
36
|
+
|
|
37
|
+
declare class EventEncoderError extends TypeError {
|
|
38
|
+
}
|
|
39
|
+
declare class EventDecoderError extends TypeError {
|
|
40
|
+
}
|
|
41
|
+
interface ErrorEventOptions extends ErrorOptions {
|
|
42
|
+
message?: string;
|
|
43
|
+
data?: unknown;
|
|
44
|
+
}
|
|
45
|
+
declare class ErrorEvent extends Error {
|
|
46
|
+
data: unknown;
|
|
47
|
+
constructor(options?: ErrorEventOptions);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id' | 'comments'>>;
|
|
51
|
+
declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
52
|
+
declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
53
|
+
|
|
54
|
+
interface StandardHeaders {
|
|
55
|
+
[key: string]: string | string[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
type StandardBody = undefined | unknown | Blob | URLSearchParams | FormData | AsyncIterator<unknown | void, unknown | void, undefined>;
|
|
58
|
+
interface StandardRequest {
|
|
59
|
+
method: string;
|
|
60
|
+
url: URL;
|
|
61
|
+
headers: StandardHeaders;
|
|
62
|
+
/**
|
|
63
|
+
* The body has been parsed based on the content-type header.
|
|
64
|
+
*/
|
|
65
|
+
body: StandardBody;
|
|
66
|
+
signal: AbortSignal | undefined;
|
|
67
|
+
}
|
|
68
|
+
interface StandardLazyRequest extends Omit<StandardRequest, 'body'> {
|
|
69
|
+
/**
|
|
70
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
71
|
+
*/
|
|
72
|
+
raw: Record<string, unknown>;
|
|
73
|
+
/**
|
|
74
|
+
* The body has been parsed based on the content-type header.
|
|
75
|
+
* This method can safely call multiple times (cached).
|
|
76
|
+
*/
|
|
77
|
+
body: () => Promise<StandardBody>;
|
|
78
|
+
}
|
|
79
|
+
interface StandardResponse {
|
|
80
|
+
status: number;
|
|
81
|
+
headers: StandardHeaders;
|
|
82
|
+
/**
|
|
83
|
+
* The body has been parsed based on the content-type header.
|
|
84
|
+
*/
|
|
85
|
+
body: StandardBody;
|
|
86
|
+
}
|
|
87
|
+
interface StandardLazyResponse extends Omit<StandardResponse, 'body'> {
|
|
88
|
+
/**
|
|
89
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
90
|
+
*/
|
|
91
|
+
raw: Record<string, unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* The body has been parsed based on the content-type header.
|
|
94
|
+
* This method can safely call multiple times (cached).
|
|
95
|
+
*/
|
|
96
|
+
body: () => Promise<StandardBody>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { ErrorEvent, type ErrorEventOptions, EventDecoder, EventDecoderError, type EventDecoderOptions, EventDecoderStream, EventEncoderError, type EventMessage, type EventMeta, type StandardBody, type StandardHeaders, type StandardLazyRequest, type StandardLazyResponse, type StandardRequest, type StandardResponse, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, getEventMeta, withEventMeta };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { isTypescriptObject } from '@orpc/shared';
|
|
2
|
+
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
|
3
|
+
|
|
4
|
+
class EventEncoderError extends TypeError {
|
|
5
|
+
}
|
|
6
|
+
class EventDecoderError extends TypeError {
|
|
7
|
+
}
|
|
8
|
+
class ErrorEvent extends Error {
|
|
9
|
+
data;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super(options?.message ?? "An error event was received", options);
|
|
12
|
+
this.data = options?.data;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function decodeEventMessage(encoded) {
|
|
17
|
+
const lines = encoded.replace(/\n+$/, "").split(/\n/);
|
|
18
|
+
const message = {
|
|
19
|
+
data: void 0,
|
|
20
|
+
event: void 0,
|
|
21
|
+
id: void 0,
|
|
22
|
+
retry: void 0,
|
|
23
|
+
comments: []
|
|
24
|
+
};
|
|
25
|
+
for (const line of lines) {
|
|
26
|
+
const index = line.indexOf(":");
|
|
27
|
+
const key = index === -1 ? line : line.slice(0, index);
|
|
28
|
+
const value = index === -1 ? "" : line.slice(index + 1).replace(/^\s/, "");
|
|
29
|
+
if (index === 0) {
|
|
30
|
+
message.comments.push(value);
|
|
31
|
+
} else if (key === "data") {
|
|
32
|
+
message.data ??= "";
|
|
33
|
+
message.data += `${value}
|
|
34
|
+
`;
|
|
35
|
+
} else if (key === "event") {
|
|
36
|
+
message.event = value;
|
|
37
|
+
} else if (key === "id") {
|
|
38
|
+
message.id = value;
|
|
39
|
+
} else if (key === "retry") {
|
|
40
|
+
const maybeInteger = Number.parseInt(value);
|
|
41
|
+
if (Number.isInteger(maybeInteger) && maybeInteger >= 0 && maybeInteger.toString() === value) {
|
|
42
|
+
message.retry = maybeInteger;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
message.data = message.data?.replace(/\n$/, "");
|
|
47
|
+
return message;
|
|
48
|
+
}
|
|
49
|
+
class EventDecoder {
|
|
50
|
+
constructor(options = {}) {
|
|
51
|
+
this.options = options;
|
|
52
|
+
}
|
|
53
|
+
incomplete = "";
|
|
54
|
+
feed(chunk) {
|
|
55
|
+
this.incomplete += chunk;
|
|
56
|
+
const lastCompleteIndex = this.incomplete.lastIndexOf("\n\n");
|
|
57
|
+
if (lastCompleteIndex === -1) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const completes = this.incomplete.slice(0, lastCompleteIndex).split(/\n\n/);
|
|
61
|
+
this.incomplete = this.incomplete.slice(lastCompleteIndex + 2);
|
|
62
|
+
for (const encoded of completes) {
|
|
63
|
+
const message = decodeEventMessage(`${encoded}
|
|
64
|
+
|
|
65
|
+
`);
|
|
66
|
+
if (this.options.onEvent) {
|
|
67
|
+
this.options.onEvent(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.incomplete = "";
|
|
71
|
+
}
|
|
72
|
+
end() {
|
|
73
|
+
if (this.incomplete) {
|
|
74
|
+
throw new EventDecoderError("Event Iterator ended before complete");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class EventDecoderStream extends TransformStream {
|
|
79
|
+
constructor() {
|
|
80
|
+
let decoder;
|
|
81
|
+
super({
|
|
82
|
+
start(controller) {
|
|
83
|
+
decoder = new EventDecoder({
|
|
84
|
+
onEvent: (event) => {
|
|
85
|
+
controller.enqueue(event);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
transform(chunk) {
|
|
90
|
+
decoder.feed(chunk);
|
|
91
|
+
},
|
|
92
|
+
flush() {
|
|
93
|
+
decoder.end();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function assertEventId(id) {
|
|
100
|
+
if (id.includes("\n")) {
|
|
101
|
+
throw new EventEncoderError("Event's id must not contain a newline character");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function assertEventName(event) {
|
|
105
|
+
if (event.includes("\n")) {
|
|
106
|
+
throw new EventEncoderError("Event's event must not contain a newline character");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function assertEventRetry(retry) {
|
|
110
|
+
if (!Number.isInteger(retry) || retry < 0) {
|
|
111
|
+
throw new EventEncoderError("Event's retry must be a integer and >= 0");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function assertEventComment(comment) {
|
|
115
|
+
if (comment.includes("\n")) {
|
|
116
|
+
throw new EventEncoderError("Event's comment must not contain a newline character");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function encodeEventData(data) {
|
|
120
|
+
const lines = data?.split(/\n/) ?? [];
|
|
121
|
+
let output = "";
|
|
122
|
+
for (const line of lines) {
|
|
123
|
+
output += `data: ${line}
|
|
124
|
+
`;
|
|
125
|
+
}
|
|
126
|
+
return output;
|
|
127
|
+
}
|
|
128
|
+
function encodeEventComments(comments) {
|
|
129
|
+
let output = "";
|
|
130
|
+
for (const comment of comments ?? []) {
|
|
131
|
+
assertEventComment(comment);
|
|
132
|
+
output += `: ${comment}
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
return output;
|
|
136
|
+
}
|
|
137
|
+
function encodeEventMessage(message) {
|
|
138
|
+
let output = "";
|
|
139
|
+
output += encodeEventComments(message.comments);
|
|
140
|
+
if (message.event !== void 0) {
|
|
141
|
+
assertEventName(message.event);
|
|
142
|
+
output += `event: ${message.event}
|
|
143
|
+
`;
|
|
144
|
+
}
|
|
145
|
+
if (message.retry !== void 0) {
|
|
146
|
+
assertEventRetry(message.retry);
|
|
147
|
+
output += `retry: ${message.retry}
|
|
148
|
+
`;
|
|
149
|
+
}
|
|
150
|
+
if (message.id !== void 0) {
|
|
151
|
+
assertEventId(message.id);
|
|
152
|
+
output += `id: ${message.id}
|
|
153
|
+
`;
|
|
154
|
+
}
|
|
155
|
+
output += encodeEventData(message.data);
|
|
156
|
+
output += "\n";
|
|
157
|
+
return output;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
|
161
|
+
function withEventMeta(container, meta) {
|
|
162
|
+
if (meta.id !== void 0) {
|
|
163
|
+
assertEventId(meta.id);
|
|
164
|
+
}
|
|
165
|
+
if (meta.retry !== void 0) {
|
|
166
|
+
assertEventRetry(meta.retry);
|
|
167
|
+
}
|
|
168
|
+
if (meta.comments !== void 0) {
|
|
169
|
+
for (const comment of meta.comments) {
|
|
170
|
+
assertEventComment(comment);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return new Proxy(container, {
|
|
174
|
+
get(target, prop, receiver) {
|
|
175
|
+
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
|
176
|
+
return meta;
|
|
177
|
+
}
|
|
178
|
+
return Reflect.get(target, prop, receiver);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function getEventMeta(container) {
|
|
183
|
+
return isTypescriptObject(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { ErrorEvent, EventDecoder, EventDecoderError, EventDecoderStream, EventEncoderError, assertEventComment, assertEventId, assertEventName, assertEventRetry, decodeEventMessage, encodeEventComments, encodeEventData, encodeEventMessage, getEventMeta, withEventMeta };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/standard-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.c0088c7",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -14,25 +14,20 @@
|
|
|
14
14
|
],
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
|
-
"types": "./dist/
|
|
18
|
-
"import": "./dist/index.
|
|
19
|
-
"default": "./dist/index.
|
|
20
|
-
},
|
|
21
|
-
"./🔒/*": {
|
|
22
|
-
"types": "./dist/src/*.d.ts"
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
23
20
|
}
|
|
24
21
|
},
|
|
25
22
|
"files": [
|
|
26
|
-
"!**/*.map",
|
|
27
|
-
"!**/*.tsbuildinfo",
|
|
28
23
|
"dist"
|
|
29
24
|
],
|
|
30
25
|
"dependencies": {
|
|
31
26
|
"@tinyhttp/content-disposition": "^2.2.2",
|
|
32
|
-
"
|
|
27
|
+
"@orpc/shared": "0.0.0-next.c0088c7"
|
|
33
28
|
},
|
|
34
29
|
"scripts": {
|
|
35
|
-
"build": "
|
|
30
|
+
"build": "unbuild",
|
|
36
31
|
"build:watch": "pnpm run build --watch",
|
|
37
32
|
"type:check": "tsc -b"
|
|
38
33
|
}
|
package/dist/index.js
DELETED
|
@@ -1,225 +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
|
-
var EVENT_SOURCE_META_SYMBOL = Symbol("ORPC_EVENT_SOURCE_META");
|
|
155
|
-
function isEventMetaContainer(value) {
|
|
156
|
-
return !!value && (typeof value === "object" || typeof value === "function");
|
|
157
|
-
}
|
|
158
|
-
function withEventMeta(container, meta) {
|
|
159
|
-
if (meta.id !== void 0) {
|
|
160
|
-
assertEventId(meta.id);
|
|
161
|
-
}
|
|
162
|
-
if (meta.retry !== void 0) {
|
|
163
|
-
assertEventRetry(meta.retry);
|
|
164
|
-
}
|
|
165
|
-
return new Proxy(container, {
|
|
166
|
-
get(target, prop, receiver) {
|
|
167
|
-
if (prop === EVENT_SOURCE_META_SYMBOL) {
|
|
168
|
-
return meta;
|
|
169
|
-
}
|
|
170
|
-
return Reflect.get(target, prop, receiver);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function getEventMeta(container) {
|
|
175
|
-
return isEventMetaContainer(container) ? Reflect.get(container, EVENT_SOURCE_META_SYMBOL) : void 0;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// src/utils.ts
|
|
179
|
-
import { contentDisposition, parse } from "@tinyhttp/content-disposition";
|
|
180
|
-
function once(fn) {
|
|
181
|
-
let cached;
|
|
182
|
-
return () => {
|
|
183
|
-
if (cached) {
|
|
184
|
-
return cached.result;
|
|
185
|
-
}
|
|
186
|
-
const result = fn();
|
|
187
|
-
cached = { result };
|
|
188
|
-
return result;
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
function parseEmptyableJSON(text) {
|
|
192
|
-
if (!text) {
|
|
193
|
-
return void 0;
|
|
194
|
-
}
|
|
195
|
-
return JSON.parse(text);
|
|
196
|
-
}
|
|
197
|
-
function isAsyncIteratorObject(maybe) {
|
|
198
|
-
if (!maybe || typeof maybe !== "object") {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
202
|
-
}
|
|
203
|
-
export {
|
|
204
|
-
ErrorEvent,
|
|
205
|
-
EventDecoder,
|
|
206
|
-
EventDecoderError,
|
|
207
|
-
EventDecoderStream,
|
|
208
|
-
EventEncoderError,
|
|
209
|
-
UnknownEvent,
|
|
210
|
-
assertEventId,
|
|
211
|
-
assertEventName,
|
|
212
|
-
assertEventRetry,
|
|
213
|
-
contentDisposition,
|
|
214
|
-
decodeEventMessage,
|
|
215
|
-
encodeEventData,
|
|
216
|
-
encodeEventMessage,
|
|
217
|
-
getEventMeta,
|
|
218
|
-
isAsyncIteratorObject,
|
|
219
|
-
isEventMetaContainer,
|
|
220
|
-
once,
|
|
221
|
-
parse as parseContentDisposition,
|
|
222
|
-
parseEmptyableJSON,
|
|
223
|
-
withEventMeta
|
|
224
|
-
};
|
|
225
|
-
//# 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,16 +0,0 @@
|
|
|
1
|
-
import type { JsonValue } from 'type-fest';
|
|
2
|
-
export declare class EventEncoderError extends TypeError {
|
|
3
|
-
}
|
|
4
|
-
export declare class EventDecoderError extends TypeError {
|
|
5
|
-
}
|
|
6
|
-
export interface ErrorEventOptions extends ErrorOptions {
|
|
7
|
-
message?: string;
|
|
8
|
-
data?: undefined | JsonValue;
|
|
9
|
-
}
|
|
10
|
-
export declare class ErrorEvent extends Error {
|
|
11
|
-
data: undefined | JsonValue;
|
|
12
|
-
constructor(options?: ErrorEventOptions);
|
|
13
|
-
}
|
|
14
|
-
export declare class UnknownEvent extends ErrorEvent {
|
|
15
|
-
}
|
|
16
|
-
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { EventMessage } from './types';
|
|
2
|
-
export type EventMeta = Partial<Pick<EventMessage, 'retry' | 'id'>>;
|
|
3
|
-
export declare function isEventMetaContainer(value: unknown): value is Record<PropertyKey, unknown>;
|
|
4
|
-
export declare function withEventMeta<T extends object>(container: T, meta: EventMeta): T;
|
|
5
|
-
export declare function getEventMeta(container: unknown): EventMeta | undefined;
|
|
6
|
-
//# 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,27 +0,0 @@
|
|
|
1
|
-
import type { JsonValue } from 'type-fest';
|
|
2
|
-
export type { JsonValue };
|
|
3
|
-
export interface StandardHeaders {
|
|
4
|
-
[key: string]: string | string[] | undefined;
|
|
5
|
-
}
|
|
6
|
-
export type StandardBody = undefined | JsonValue | Blob | URLSearchParams | FormData | AsyncIterator<JsonValue | void, JsonValue | void, undefined>;
|
|
7
|
-
export interface StandardRequest {
|
|
8
|
-
/**
|
|
9
|
-
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
|
10
|
-
*/
|
|
11
|
-
raw: Record<string, unknown>;
|
|
12
|
-
method: string;
|
|
13
|
-
url: URL;
|
|
14
|
-
headers: StandardHeaders;
|
|
15
|
-
/**
|
|
16
|
-
* The body has been parsed base on the content-type header.
|
|
17
|
-
* This method can safely call multiple times (cached).
|
|
18
|
-
*/
|
|
19
|
-
body: () => Promise<StandardBody>;
|
|
20
|
-
signal: AbortSignal | undefined;
|
|
21
|
-
}
|
|
22
|
-
export interface StandardResponse {
|
|
23
|
-
status: number;
|
|
24
|
-
headers: StandardHeaders;
|
|
25
|
-
body: StandardBody;
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/src/utils.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { JsonValue } from 'type-fest';
|
|
2
|
-
export declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
3
|
-
export declare function parseEmptyableJSON(text: string): JsonValue | undefined;
|
|
4
|
-
export declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
5
|
-
export { contentDisposition, parse as parseContentDisposition } from '@tinyhttp/content-disposition';
|
|
6
|
-
//# sourceMappingURL=utils.d.ts.map
|