@orpc/standard-server-fetch 1.1.1 → 1.3.0

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
@@ -49,6 +49,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
49
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
50
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
51
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
52
53
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
53
54
  - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
54
55
  - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
2
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
4
4
  interface ToEventStreamOptions {
5
5
  /**
6
6
  * If true, a ping comment is sent periodically to keep the connection alive.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
2
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
3
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
4
4
  interface ToEventStreamOptions {
5
5
  /**
6
6
  * If true, a ping comment is sent periodically to keep the connection alive.
package/dist/index.mjs CHANGED
@@ -1,51 +1,51 @@
1
- import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
2
- import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
1
+ import { createAsyncIteratorObject, parseEmptyableJSON, isTypescriptObject, stringifyJSON, isAsyncIteratorObject, once } from '@orpc/shared';
2
+ import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
3
3
 
4
4
  function toEventIterator(stream) {
5
- const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
- const reader = eventStream.getReader();
7
- async function* gen() {
8
- try {
9
- while (true) {
10
- const { done, value } = await reader.read();
11
- if (done) {
12
- return;
13
- }
14
- switch (value.event) {
15
- case "message": {
16
- let message = parseEmptyableJSON(value.data);
17
- if (isTypescriptObject(message)) {
18
- message = withEventMeta(message, value);
19
- }
20
- yield message;
21
- break;
22
- }
23
- case "error": {
24
- let error = new ErrorEvent({
25
- data: parseEmptyableJSON(value.data)
26
- });
27
- error = withEventMeta(error, value);
28
- throw error;
5
+ const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
+ const reader = eventStream?.getReader();
7
+ return createAsyncIteratorObject(async () => {
8
+ while (true) {
9
+ if (reader === void 0) {
10
+ return { done: true, value: void 0 };
11
+ }
12
+ const { done, value } = await reader.read();
13
+ if (done) {
14
+ return { done: true, value: void 0 };
15
+ }
16
+ switch (value.event) {
17
+ case "message": {
18
+ let message = parseEmptyableJSON(value.data);
19
+ if (isTypescriptObject(message)) {
20
+ message = withEventMeta(message, value);
29
21
  }
30
- case "done": {
31
- let done2 = parseEmptyableJSON(value.data);
32
- if (isTypescriptObject(done2)) {
33
- done2 = withEventMeta(done2, value);
34
- }
35
- return done2;
22
+ return { done: false, value: message };
23
+ }
24
+ case "error": {
25
+ let error = new ErrorEvent({
26
+ data: parseEmptyableJSON(value.data)
27
+ });
28
+ error = withEventMeta(error, value);
29
+ throw error;
30
+ }
31
+ case "done": {
32
+ let done2 = parseEmptyableJSON(value.data);
33
+ if (isTypescriptObject(done2)) {
34
+ done2 = withEventMeta(done2, value);
36
35
  }
36
+ return { done: true, value: done2 };
37
37
  }
38
38
  }
39
- } finally {
40
- await reader.cancel();
41
39
  }
42
- }
43
- return gen();
40
+ }, async () => {
41
+ await reader?.cancel();
42
+ });
44
43
  }
45
44
  function toEventStream(iterator, options = {}) {
46
45
  const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
47
46
  const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
48
47
  const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
48
+ let cancelled = false;
49
49
  let timeout;
50
50
  const stream = new ReadableStream({
51
51
  async pull(controller) {
@@ -59,6 +59,9 @@ function toEventStream(iterator, options = {}) {
59
59
  }
60
60
  const value = await iterator.next();
61
61
  clearInterval(timeout);
62
+ if (cancelled) {
63
+ return;
64
+ }
62
65
  const meta = getEventMeta(value.value);
63
66
  if (!value.done || value.value !== void 0 || meta !== void 0) {
64
67
  controller.enqueue(encodeEventMessage({
@@ -72,6 +75,9 @@ function toEventStream(iterator, options = {}) {
72
75
  }
73
76
  } catch (err) {
74
77
  clearInterval(timeout);
78
+ if (cancelled) {
79
+ return;
80
+ }
75
81
  controller.enqueue(encodeEventMessage({
76
82
  ...getEventMeta(err),
77
83
  event: "error",
@@ -80,21 +86,16 @@ function toEventStream(iterator, options = {}) {
80
86
  controller.close();
81
87
  }
82
88
  },
83
- async cancel(reason) {
84
- if (reason) {
85
- await iterator.throw?.(reason);
86
- } else {
87
- await iterator.return?.();
88
- }
89
+ async cancel() {
90
+ cancelled = true;
91
+ clearInterval(timeout);
92
+ await iterator.return?.();
89
93
  }
90
94
  }).pipeThrough(new TextEncoderStream());
91
95
  return stream;
92
96
  }
93
97
 
94
98
  async function toStandardBody(re) {
95
- if (re.body === null) {
96
- return void 0;
97
- }
98
99
  const contentDisposition = re.headers.get("content-disposition");
99
100
  if (typeof contentDisposition === "string") {
100
101
  const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-fetch",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "1.1.1",
27
- "@orpc/standard-server": "1.1.1"
26
+ "@orpc/shared": "1.3.0",
27
+ "@orpc/standard-server": "1.3.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@hono/node-server": "^1.14.1"