@orpc/standard-server-node 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 CHANGED
@@ -11,7 +11,6 @@ import {
11
11
  ErrorEvent,
12
12
  EventDecoderStream,
13
13
  getEventMeta,
14
- UnknownEvent,
15
14
  withEventMeta
16
15
  } from "@orpc/standard-server";
17
16
  function toEventIterator(stream) {
@@ -47,14 +46,6 @@ function toEventIterator(stream) {
47
46
  }
48
47
  return done2;
49
48
  }
50
- default: {
51
- let error = new UnknownEvent({
52
- message: `Unknown event: ${value.event}`,
53
- data: parseEmptyableJSON(value.data)
54
- });
55
- error = withEventMeta(error, value);
56
- throw error;
57
- }
58
49
  }
59
50
  }
60
51
  } finally {
@@ -63,11 +54,23 @@ function toEventIterator(stream) {
63
54
  }
64
55
  return gen();
65
56
  }
66
- function toEventStream(iterator) {
57
+ function toEventStream(iterator, options = {}) {
58
+ const pingEnabled = options.eventSourcePingEnabled ?? true;
59
+ const pingInterval = options.eventSourcePingInterval ?? 5e3;
60
+ const pingContent = options.eventSourcePingContent ?? "";
61
+ let timeout;
67
62
  const stream = new ReadableStream({
68
63
  async pull(controller) {
69
64
  try {
65
+ if (pingEnabled) {
66
+ timeout = setInterval(() => {
67
+ controller.enqueue(encodeEventMessage({
68
+ comments: [pingContent]
69
+ }));
70
+ }, pingInterval);
71
+ }
70
72
  const value = await iterator.next();
73
+ clearInterval(timeout);
71
74
  controller.enqueue(encodeEventMessage({
72
75
  ...getEventMeta(value.value),
73
76
  event: value.done ? "done" : "message",
@@ -77,6 +80,7 @@ function toEventStream(iterator) {
77
80
  controller.close();
78
81
  }
79
82
  } catch (err) {
83
+ clearInterval(timeout);
80
84
  controller.enqueue(encodeEventMessage({
81
85
  ...getEventMeta(err),
82
86
  event: "error",
@@ -129,7 +133,7 @@ async function toStandardBody(req) {
129
133
  }
130
134
  return _streamToFile(req, "blob", contentType);
131
135
  }
132
- function toNodeHttpBody(body, headers) {
136
+ function toNodeHttpBody(body, headers, options = {}) {
133
137
  delete headers["content-type"];
134
138
  delete headers["content-disposition"];
135
139
  if (body === void 0) {
@@ -157,7 +161,7 @@ function toNodeHttpBody(body, headers) {
157
161
  headers["content-type"] = "text/event-stream";
158
162
  headers["cache-control"] = "no-cache";
159
163
  headers.connection = "keep-alive";
160
- return toEventStream(body);
164
+ return toEventStream(body, options);
161
165
  }
162
166
  headers["content-type"] = "application/json";
163
167
  return stringifyJSON2(body);
@@ -220,12 +224,12 @@ function toStandardRequest(req, res) {
220
224
  }
221
225
 
222
226
  // src/response.ts
223
- function sendStandardResponse(res, standardResponse) {
227
+ function sendStandardResponse(res, standardResponse, options = {}) {
224
228
  return new Promise((resolve, reject) => {
225
229
  res.on("error", reject);
226
230
  res.on("finish", resolve);
227
231
  const resHeaders = standardResponse.headers;
228
- const resBody = toNodeHttpBody(standardResponse.body, resHeaders);
232
+ const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
229
233
  res.writeHead(standardResponse.status, resHeaders);
230
234
  if (resBody === void 0) {
231
235
  res.end(resBody);
@@ -1,10 +1,14 @@
1
1
  import type { StandardBody, StandardHeaders } from '@orpc/standard-server';
2
+ import type { ToEventStreamOptions } from './event-source';
2
3
  import type { NodeHttpRequest } from './types';
3
4
  import { Readable } from 'node:stream';
4
5
  export declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
6
+ export interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
7
+ }
5
8
  /**
6
9
  * @param body
7
- * @param headers - The headers can be changed by the function and effects on the original headers.
10
+ * @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
11
+ * @param options
8
12
  */
9
- export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders): Readable | undefined | string;
13
+ export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
10
14
  //# sourceMappingURL=body.d.ts.map
@@ -1,4 +1,24 @@
1
1
  import { Readable } from 'node:stream';
2
2
  export declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
3
- export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>): Readable;
3
+ export interface ToEventStreamOptions {
4
+ /**
5
+ * If true, a ping comment is sent periodically to keep the connection alive.
6
+ *
7
+ * @default true
8
+ */
9
+ eventSourcePingEnabled?: boolean;
10
+ /**
11
+ * Interval (in milliseconds) between ping comments sent after the last event.
12
+ *
13
+ * @default 5000
14
+ */
15
+ eventSourcePingInterval?: number;
16
+ /**
17
+ * The content of the ping comment. Must not include newline characters.
18
+ *
19
+ * @default ''
20
+ */
21
+ eventSourcePingContent?: string;
22
+ }
23
+ export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
4
24
  //# sourceMappingURL=event-source.d.ts.map
@@ -1,4 +1,7 @@
1
1
  import type { StandardResponse } from '@orpc/standard-server';
2
+ import type { ToNodeHttpBodyOptions } from './body';
2
3
  import type { NodeHttpResponse } from './types';
3
- export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse): Promise<void>;
4
+ export interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
5
+ }
6
+ export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
4
7
  //# sourceMappingURL=response.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-node",
3
3
  "type": "module",
4
- "version": "0.0.0-next.ca29a36",
4
+ "version": "0.0.0-next.d0e429d",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -28,8 +28,8 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@orpc/shared": "0.0.0-next.ca29a36",
32
- "@orpc/standard-server": "0.0.0-next.ca29a36"
31
+ "@orpc/shared": "0.0.0-next.d0e429d",
32
+ "@orpc/standard-server": "0.0.0-next.d0e429d"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.13.1",