@orpc/standard-server-fetch 0.0.0-next.ca29a36 → 0.0.0-next.d0e429d
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/dist/index.js +18 -14
- package/dist/src/body.d.ts +4 -1
- package/dist/src/event-source.d.ts +21 -1
- package/dist/src/response.d.ts +4 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
ErrorEvent,
|
|
10
10
|
EventDecoderStream,
|
|
11
11
|
getEventMeta,
|
|
12
|
-
UnknownEvent,
|
|
13
12
|
withEventMeta
|
|
14
13
|
} from "@orpc/standard-server";
|
|
15
14
|
function toEventIterator(stream) {
|
|
@@ -45,14 +44,6 @@ function toEventIterator(stream) {
|
|
|
45
44
|
}
|
|
46
45
|
return done2;
|
|
47
46
|
}
|
|
48
|
-
default: {
|
|
49
|
-
let error = new UnknownEvent({
|
|
50
|
-
message: `Unknown event: ${value.event}`,
|
|
51
|
-
data: parseEmptyableJSON(value.data)
|
|
52
|
-
});
|
|
53
|
-
error = withEventMeta(error, value);
|
|
54
|
-
throw error;
|
|
55
|
-
}
|
|
56
47
|
}
|
|
57
48
|
}
|
|
58
49
|
} finally {
|
|
@@ -61,11 +52,23 @@ function toEventIterator(stream) {
|
|
|
61
52
|
}
|
|
62
53
|
return gen();
|
|
63
54
|
}
|
|
64
|
-
function toEventStream(iterator) {
|
|
55
|
+
function toEventStream(iterator, options = {}) {
|
|
56
|
+
const pingEnabled = options.eventSourcePingEnabled ?? true;
|
|
57
|
+
const pingInterval = options.eventSourcePingInterval ?? 5e3;
|
|
58
|
+
const pingContent = options.eventSourcePingContent ?? "";
|
|
59
|
+
let timeout;
|
|
65
60
|
const stream = new ReadableStream({
|
|
66
61
|
async pull(controller) {
|
|
67
62
|
try {
|
|
63
|
+
if (pingEnabled) {
|
|
64
|
+
timeout = setInterval(() => {
|
|
65
|
+
controller.enqueue(encodeEventMessage({
|
|
66
|
+
comments: [pingContent]
|
|
67
|
+
}));
|
|
68
|
+
}, pingInterval);
|
|
69
|
+
}
|
|
68
70
|
const value = await iterator.next();
|
|
71
|
+
clearInterval(timeout);
|
|
69
72
|
controller.enqueue(encodeEventMessage({
|
|
70
73
|
...getEventMeta(value.value),
|
|
71
74
|
event: value.done ? "done" : "message",
|
|
@@ -75,6 +78,7 @@ function toEventStream(iterator) {
|
|
|
75
78
|
controller.close();
|
|
76
79
|
}
|
|
77
80
|
} catch (err) {
|
|
81
|
+
clearInterval(timeout);
|
|
78
82
|
controller.enqueue(encodeEventMessage({
|
|
79
83
|
...getEventMeta(err),
|
|
80
84
|
event: "error",
|
|
@@ -132,7 +136,7 @@ async function toStandardBody(re) {
|
|
|
132
136
|
type: blob.type
|
|
133
137
|
});
|
|
134
138
|
}
|
|
135
|
-
function toFetchBody(body, headers) {
|
|
139
|
+
function toFetchBody(body, headers, options = {}) {
|
|
136
140
|
headers.delete("content-type");
|
|
137
141
|
headers.delete("content-disposition");
|
|
138
142
|
if (body === void 0) {
|
|
@@ -157,7 +161,7 @@ function toFetchBody(body, headers) {
|
|
|
157
161
|
headers.set("content-type", "text/event-stream");
|
|
158
162
|
headers.set("cache-control", "no-cache");
|
|
159
163
|
headers.set("connection", "keep-alive");
|
|
160
|
-
return toEventStream(body);
|
|
164
|
+
return toEventStream(body, options);
|
|
161
165
|
}
|
|
162
166
|
headers.set("content-type", "application/json");
|
|
163
167
|
return stringifyJSON2(body);
|
|
@@ -210,9 +214,9 @@ function toStandardRequest(request) {
|
|
|
210
214
|
}
|
|
211
215
|
|
|
212
216
|
// src/response.ts
|
|
213
|
-
function toFetchResponse(response) {
|
|
217
|
+
function toFetchResponse(response, options = {}) {
|
|
214
218
|
const headers = toFetchHeaders(response.headers);
|
|
215
|
-
const body = toFetchBody(response.body, headers);
|
|
219
|
+
const body = toFetchBody(response.body, headers, options);
|
|
216
220
|
return new Response(body, { headers, status: response.status });
|
|
217
221
|
}
|
|
218
222
|
export {
|
package/dist/src/body.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { StandardBody } from '@orpc/standard-server';
|
|
2
|
+
import type { ToEventStreamOptions } from './event-source';
|
|
2
3
|
export declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
|
|
4
|
+
export interface ToFetchBodyOptions extends ToEventStreamOptions {
|
|
5
|
+
}
|
|
3
6
|
/**
|
|
4
7
|
* @param body
|
|
5
8
|
* @param headers - The headers can be changed by the function and effects on the original headers.
|
|
6
9
|
*/
|
|
7
|
-
export declare function toFetchBody(body: StandardBody, headers: Headers): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
|
|
10
|
+
export declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
|
|
8
11
|
//# sourceMappingURL=body.d.ts.map
|
|
@@ -1,3 +1,23 @@
|
|
|
1
1
|
export declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
|
|
2
|
-
export
|
|
2
|
+
export interface ToEventStreamOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
5
|
+
*
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
eventSourcePingEnabled?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
11
|
+
*
|
|
12
|
+
* @default 5000
|
|
13
|
+
*/
|
|
14
|
+
eventSourcePingInterval?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The content of the ping comment. Must not include newline characters.
|
|
17
|
+
*
|
|
18
|
+
* @default ''
|
|
19
|
+
*/
|
|
20
|
+
eventSourcePingContent?: string;
|
|
21
|
+
}
|
|
22
|
+
export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
|
|
3
23
|
//# sourceMappingURL=event-source.d.ts.map
|
package/dist/src/response.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { StandardResponse } from '@orpc/standard-server';
|
|
2
|
-
|
|
2
|
+
import type { ToFetchBodyOptions } from './body';
|
|
3
|
+
export interface ToFetchResponseOptions extends ToFetchBodyOptions {
|
|
4
|
+
}
|
|
5
|
+
export declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
|
|
3
6
|
//# sourceMappingURL=response.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/standard-server-fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.d0e429d",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"dist"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@orpc/shared": "0.0.0-next.
|
|
32
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
31
|
+
"@orpc/shared": "0.0.0-next.d0e429d",
|
|
32
|
+
"@orpc/standard-server": "0.0.0-next.d0e429d"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@hono/node-server": "^1.13.8"
|