@orpc/standard-server-fetch 0.0.0-next.b36125c → 0.0.0-next.bf49833

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 />
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
3
3
  </div>
4
4
 
5
5
  <h1></h1>
@@ -0,0 +1,56 @@
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventStreamOptions {
5
+ /**
6
+ * If true, a ping comment is sent periodically to keep the connection alive.
7
+ *
8
+ * @default true
9
+ */
10
+ eventIteratorKeepAliveEnabled?: boolean;
11
+ /**
12
+ * Interval (in milliseconds) between ping comments sent after the last event.
13
+ *
14
+ * @default 5000
15
+ */
16
+ eventIteratorKeepAliveInterval?: number;
17
+ /**
18
+ * The content of the ping comment. Must not include newline characters.
19
+ *
20
+ * @default ''
21
+ */
22
+ eventIteratorKeepAliveComment?: string;
23
+ }
24
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
+
26
+ declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
27
+ interface ToFetchBodyOptions extends ToEventStreamOptions {
28
+ }
29
+ /**
30
+ * @param body
31
+ * @param headers - The headers can be changed by the function and effects on the original headers.
32
+ */
33
+ declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
34
+
35
+ /**
36
+ * @param headers
37
+ * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
38
+ */
39
+ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
40
+ /**
41
+ * @param headers
42
+ * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
43
+ */
44
+ declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
+
46
+ declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
47
+ interface ToFetchRequestOptions extends ToFetchBodyOptions {
48
+ }
49
+ declare function toFetchRequest(request: StandardRequest, options?: ToFetchRequestOptions): Request;
50
+
51
+ interface ToFetchResponseOptions extends ToFetchBodyOptions {
52
+ }
53
+ declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
54
+ declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
55
+
56
+ export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchRequestOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
@@ -0,0 +1,56 @@
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventStreamOptions {
5
+ /**
6
+ * If true, a ping comment is sent periodically to keep the connection alive.
7
+ *
8
+ * @default true
9
+ */
10
+ eventIteratorKeepAliveEnabled?: boolean;
11
+ /**
12
+ * Interval (in milliseconds) between ping comments sent after the last event.
13
+ *
14
+ * @default 5000
15
+ */
16
+ eventIteratorKeepAliveInterval?: number;
17
+ /**
18
+ * The content of the ping comment. Must not include newline characters.
19
+ *
20
+ * @default ''
21
+ */
22
+ eventIteratorKeepAliveComment?: string;
23
+ }
24
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
+
26
+ declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
27
+ interface ToFetchBodyOptions extends ToEventStreamOptions {
28
+ }
29
+ /**
30
+ * @param body
31
+ * @param headers - The headers can be changed by the function and effects on the original headers.
32
+ */
33
+ declare function toFetchBody(body: StandardBody, headers: Headers, options?: ToFetchBodyOptions): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
34
+
35
+ /**
36
+ * @param headers
37
+ * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
38
+ */
39
+ declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
40
+ /**
41
+ * @param headers
42
+ * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
43
+ */
44
+ declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
45
+
46
+ declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
47
+ interface ToFetchRequestOptions extends ToFetchBodyOptions {
48
+ }
49
+ declare function toFetchRequest(request: StandardRequest, options?: ToFetchRequestOptions): Request;
50
+
51
+ interface ToFetchResponseOptions extends ToFetchBodyOptions {
52
+ }
53
+ declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
54
+ declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
55
+
56
+ export { type ToEventStreamOptions, type ToFetchBodyOptions, type ToFetchRequestOptions, type ToFetchResponseOptions, toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
@@ -1,16 +1,6 @@
1
- // src/body.ts
2
- import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2, stringifyJSON as stringifyJSON2 } from "@orpc/shared";
3
- import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
1
+ import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
2
+ import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, parseContentDisposition, contentDisposition } from '@orpc/standard-server';
4
3
 
5
- // src/event-source.ts
6
- import { isTypescriptObject, parseEmptyableJSON, stringifyJSON } from "@orpc/shared";
7
- import {
8
- encodeEventMessage,
9
- ErrorEvent,
10
- EventDecoderStream,
11
- getEventMeta,
12
- withEventMeta
13
- } from "@orpc/standard-server";
14
4
  function toEventIterator(stream) {
15
5
  const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
16
6
  const reader = eventStream.getReader();
@@ -52,11 +42,23 @@ function toEventIterator(stream) {
52
42
  }
53
43
  return gen();
54
44
  }
55
- function toEventStream(iterator) {
45
+ function toEventStream(iterator, options = {}) {
46
+ const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
47
+ const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
48
+ const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
49
+ let timeout;
56
50
  const stream = new ReadableStream({
57
51
  async pull(controller) {
58
52
  try {
53
+ if (keepAliveEnabled) {
54
+ timeout = setInterval(() => {
55
+ controller.enqueue(encodeEventMessage({
56
+ comments: [keepAliveComment]
57
+ }));
58
+ }, keepAliveInterval);
59
+ }
59
60
  const value = await iterator.next();
61
+ clearInterval(timeout);
60
62
  controller.enqueue(encodeEventMessage({
61
63
  ...getEventMeta(value.value),
62
64
  event: value.done ? "done" : "message",
@@ -66,6 +68,7 @@ function toEventStream(iterator) {
66
68
  controller.close();
67
69
  }
68
70
  } catch (err) {
71
+ clearInterval(timeout);
69
72
  controller.enqueue(encodeEventMessage({
70
73
  ...getEventMeta(err),
71
74
  event: "error",
@@ -85,7 +88,6 @@ function toEventStream(iterator) {
85
88
  return stream;
86
89
  }
87
90
 
88
- // src/body.ts
89
91
  async function toStandardBody(re) {
90
92
  if (!re.body) {
91
93
  return void 0;
@@ -103,7 +105,7 @@ async function toStandardBody(re) {
103
105
  const contentType = re.headers.get("content-type");
104
106
  if (!contentType || contentType.startsWith("application/json")) {
105
107
  const text = await re.text();
106
- return parseEmptyableJSON2(text);
108
+ return parseEmptyableJSON(text);
107
109
  }
108
110
  if (contentType.startsWith("multipart/form-data")) {
109
111
  return await re.formData();
@@ -123,7 +125,7 @@ async function toStandardBody(re) {
123
125
  type: blob.type
124
126
  });
125
127
  }
126
- function toFetchBody(body, headers) {
128
+ function toFetchBody(body, headers, options = {}) {
127
129
  headers.delete("content-type");
128
130
  headers.delete("content-disposition");
129
131
  if (body === void 0) {
@@ -148,13 +150,12 @@ function toFetchBody(body, headers) {
148
150
  headers.set("content-type", "text/event-stream");
149
151
  headers.set("cache-control", "no-cache");
150
152
  headers.set("connection", "keep-alive");
151
- return toEventStream(body);
153
+ return toEventStream(body, options);
152
154
  }
153
155
  headers.set("content-type", "application/json");
154
- return stringifyJSON2(body);
156
+ return stringifyJSON(body);
155
157
  }
156
158
 
157
- // src/headers.ts
158
159
  function toStandardHeaders(headers, standardHeaders = {}) {
159
160
  for (const [key, value] of headers) {
160
161
  if (Array.isArray(standardHeaders[key])) {
@@ -180,9 +181,7 @@ function toFetchHeaders(headers, fetchHeaders = new Headers()) {
180
181
  return fetchHeaders;
181
182
  }
182
183
 
183
- // src/request.ts
184
- import { once } from "@orpc/shared";
185
- function toStandardRequest(request) {
184
+ function toStandardLazyRequest(request) {
186
185
  return {
187
186
  raw: { request },
188
187
  url: new URL(request.url),
@@ -199,21 +198,36 @@ function toStandardRequest(request) {
199
198
  }
200
199
  };
201
200
  }
201
+ function toFetchRequest(request, options = {}) {
202
+ const headers = toFetchHeaders(request.headers);
203
+ const body = toFetchBody(request.body, headers, options);
204
+ return new Request(request.url, {
205
+ signal: request.signal,
206
+ method: request.method,
207
+ headers,
208
+ body
209
+ });
210
+ }
202
211
 
203
- // src/response.ts
204
- function toFetchResponse(response) {
212
+ function toFetchResponse(response, options = {}) {
205
213
  const headers = toFetchHeaders(response.headers);
206
- const body = toFetchBody(response.body, headers);
214
+ const body = toFetchBody(response.body, headers, options);
207
215
  return new Response(body, { headers, status: response.status });
208
216
  }
209
- export {
210
- toEventIterator,
211
- toEventStream,
212
- toFetchBody,
213
- toFetchHeaders,
214
- toFetchResponse,
215
- toStandardBody,
216
- toStandardHeaders,
217
- toStandardRequest
218
- };
219
- //# sourceMappingURL=index.js.map
217
+ function toStandardLazyResponse(response) {
218
+ return {
219
+ raw: { response },
220
+ body: once(() => toStandardBody(response)),
221
+ status: response.status,
222
+ get headers() {
223
+ const headers = toStandardHeaders(response.headers);
224
+ Object.defineProperty(this, "headers", { value: headers, writable: true });
225
+ return headers;
226
+ },
227
+ set headers(value) {
228
+ Object.defineProperty(this, "headers", { value, writable: true });
229
+ }
230
+ };
231
+ }
232
+
233
+ export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
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.b36125c",
4
+ "version": "0.0.0-next.bf49833",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -14,28 +14,23 @@
14
14
  ],
15
15
  "exports": {
16
16
  ".": {
17
- "types": "./dist/src/index.d.ts",
18
- "import": "./dist/index.js",
19
- "default": "./dist/index.js"
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
- "@orpc/shared": "0.0.0-next.b36125c",
32
- "@orpc/standard-server": "0.0.0-next.b36125c"
26
+ "@orpc/shared": "0.0.0-next.bf49833",
27
+ "@orpc/standard-server": "0.0.0-next.bf49833"
33
28
  },
34
29
  "devDependencies": {
35
30
  "@hono/node-server": "^1.13.8"
36
31
  },
37
32
  "scripts": {
38
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
33
+ "build": "unbuild",
39
34
  "build:watch": "pnpm run build --watch",
40
35
  "type:check": "tsc -b"
41
36
  }
@@ -1,8 +0,0 @@
1
- import type { StandardBody } from '@orpc/standard-server';
2
- export declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
3
- /**
4
- * @param body
5
- * @param headers - The headers can be changed by the function and effects on the original headers.
6
- */
7
- export declare function toFetchBody(body: StandardBody, headers: Headers): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
8
- //# sourceMappingURL=body.d.ts.map
@@ -1,3 +0,0 @@
1
- export declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
2
- export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>): ReadableStream<Uint8Array>;
3
- //# sourceMappingURL=event-source.d.ts.map
@@ -1,12 +0,0 @@
1
- import type { StandardHeaders } from '@orpc/standard-server';
2
- /**
3
- * @param headers
4
- * @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
5
- */
6
- export declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
7
- /**
8
- * @param headers
9
- * @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
10
- */
11
- export declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
12
- //# sourceMappingURL=headers.d.ts.map
@@ -1,6 +0,0 @@
1
- export * from './body';
2
- export * from './event-source';
3
- export * from './headers';
4
- export * from './request';
5
- export * from './response';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { StandardRequest } from '@orpc/standard-server';
2
- export declare function toStandardRequest(request: Request): StandardRequest;
3
- //# sourceMappingURL=request.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { StandardResponse } from '@orpc/standard-server';
2
- export declare function toFetchResponse(response: StandardResponse): Response;
3
- //# sourceMappingURL=response.d.ts.map