@orpc/server 1.4.5 → 1.5.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.
@@ -8,10 +8,33 @@ import '@orpc/contract';
8
8
  import '@orpc/standard-server';
9
9
  import '@orpc/client/standard';
10
10
 
11
+ type experimental_MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
11
12
  declare class experimental_WebsocketHandler<T extends Context> {
12
- private readonly standardHandler;
13
+ #private;
13
14
  constructor(standardHandler: StandardHandler<T>);
14
- upgrade(ws: Pick<WebSocket, 'addEventListener' | 'send'>, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): void;
15
+ /**
16
+ * Upgrades a WebSocket to enable handling
17
+ *
18
+ * This attaches the necessary 'message' and 'close' listeners to the WebSocket
19
+ *
20
+ * @warning Do not use this method if you're using `.message()` or `.close()`
21
+ */
22
+ upgrade(ws: experimental_MinimalWebsocket, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): void;
23
+ /**
24
+ * Handles a single message received from a WebSocket.
25
+ *
26
+ * @warning Avoid calling this directly if `.upgrade()` is used.
27
+ *
28
+ * @param ws The WebSocket instance
29
+ * @param data The message payload, usually place in `event.data`
30
+ */
31
+ message(ws: experimental_MinimalWebsocket, data: string | ArrayBuffer | Blob, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): Promise<void>;
32
+ /**
33
+ * Closes the WebSocket peer and cleans up.
34
+ *
35
+ * @warning Avoid calling this directly if `.upgrade()` is used.
36
+ */
37
+ close(ws: experimental_MinimalWebsocket): void;
15
38
  }
16
39
 
17
40
  /**
@@ -25,3 +48,4 @@ declare class experimental_RPCHandler<T extends Context> extends experimental_We
25
48
  }
26
49
 
27
50
  export { experimental_RPCHandler, experimental_WebsocketHandler };
51
+ export type { experimental_MinimalWebsocket };
@@ -8,10 +8,33 @@ import '@orpc/contract';
8
8
  import '@orpc/standard-server';
9
9
  import '@orpc/client/standard';
10
10
 
11
+ type experimental_MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
11
12
  declare class experimental_WebsocketHandler<T extends Context> {
12
- private readonly standardHandler;
13
+ #private;
13
14
  constructor(standardHandler: StandardHandler<T>);
14
- upgrade(ws: Pick<WebSocket, 'addEventListener' | 'send'>, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): void;
15
+ /**
16
+ * Upgrades a WebSocket to enable handling
17
+ *
18
+ * This attaches the necessary 'message' and 'close' listeners to the WebSocket
19
+ *
20
+ * @warning Do not use this method if you're using `.message()` or `.close()`
21
+ */
22
+ upgrade(ws: experimental_MinimalWebsocket, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): void;
23
+ /**
24
+ * Handles a single message received from a WebSocket.
25
+ *
26
+ * @warning Avoid calling this directly if `.upgrade()` is used.
27
+ *
28
+ * @param ws The WebSocket instance
29
+ * @param data The message payload, usually place in `event.data`
30
+ */
31
+ message(ws: experimental_MinimalWebsocket, data: string | ArrayBuffer | Blob, ...rest: MaybeOptionalOptions<Omit<FriendlyStandardHandleOptions<T>, 'prefix'>>): Promise<void>;
32
+ /**
33
+ * Closes the WebSocket peer and cleans up.
34
+ *
35
+ * @warning Avoid calling this directly if `.upgrade()` is used.
36
+ */
37
+ close(ws: experimental_MinimalWebsocket): void;
15
38
  }
16
39
 
17
40
  /**
@@ -25,3 +48,4 @@ declare class experimental_RPCHandler<T extends Context> extends experimental_We
25
48
  }
26
49
 
27
50
  export { experimental_RPCHandler, experimental_WebsocketHandler };
51
+ export type { experimental_MinimalWebsocket };
@@ -9,24 +9,54 @@ import '@orpc/client/standard';
9
9
  import '../../shared/server.DG7Tamti.mjs';
10
10
 
11
11
  class experimental_WebsocketHandler {
12
+ #peers = /* @__PURE__ */ new WeakMap();
13
+ #handler;
12
14
  constructor(standardHandler) {
13
- this.standardHandler = standardHandler;
15
+ this.#handler = standardHandler;
14
16
  }
17
+ /**
18
+ * Upgrades a WebSocket to enable handling
19
+ *
20
+ * This attaches the necessary 'message' and 'close' listeners to the WebSocket
21
+ *
22
+ * @warning Do not use this method if you're using `.message()` or `.close()`
23
+ */
15
24
  upgrade(ws, ...rest) {
16
- const peer = new ServerPeer(ws.send.bind(ws));
17
- ws.addEventListener("message", async (event) => {
18
- const message = event.data instanceof Blob ? await event.data.arrayBuffer() : event.data;
19
- const [id, request] = await peer.message(message);
20
- if (!request) {
21
- return;
22
- }
23
- const options = resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest));
24
- const { response } = await this.standardHandler.handle({ ...request, body: () => Promise.resolve(request.body) }, options);
25
- await peer.response(id, response ?? { status: 404, headers: {}, body: "No procedure matched" });
26
- });
27
- ws.addEventListener("close", () => {
25
+ ws.addEventListener("message", (event) => this.message(ws, event.data, ...rest));
26
+ ws.addEventListener("close", () => this.close(ws));
27
+ }
28
+ /**
29
+ * Handles a single message received from a WebSocket.
30
+ *
31
+ * @warning Avoid calling this directly if `.upgrade()` is used.
32
+ *
33
+ * @param ws The WebSocket instance
34
+ * @param data The message payload, usually place in `event.data`
35
+ */
36
+ async message(ws, data, ...rest) {
37
+ let peer = this.#peers.get(ws);
38
+ if (!peer) {
39
+ this.#peers.set(ws, peer = new ServerPeer(ws.send.bind(ws)));
40
+ }
41
+ const message = data instanceof Blob ? await data.arrayBuffer() : data;
42
+ const [id, request] = await peer.message(message);
43
+ if (!request) {
44
+ return;
45
+ }
46
+ const options = resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest));
47
+ const { response } = await this.#handler.handle({ ...request, body: () => Promise.resolve(request.body) }, options);
48
+ await peer.response(id, response ?? { status: 404, headers: {}, body: "No procedure matched" });
49
+ }
50
+ /**
51
+ * Closes the WebSocket peer and cleans up.
52
+ *
53
+ * @warning Avoid calling this directly if `.upgrade()` is used.
54
+ */
55
+ close(ws) {
56
+ const peer = this.#peers.get(ws);
57
+ if (peer) {
28
58
  peer.close();
29
- });
59
+ }
30
60
  }
31
61
  }
32
62
 
@@ -0,0 +1,44 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.Dq8xr7PQ.mjs';
3
+ import { experimental_HibernationEventIterator } from '@orpc/standard-server';
4
+ export { experimental_HibernationEventIterator, experimental_HibernationEventIteratorCallback } from '@orpc/standard-server';
5
+ import { C as Context, R as Router } from '../shared/server.DD2C4ujN.mjs';
6
+ import '@orpc/client';
7
+ import '@orpc/contract';
8
+ import '@orpc/shared';
9
+
10
+ interface experimental_EncodeHibernationRPCEventOptions extends StandardRPCJsonSerializerOptions {
11
+ /**
12
+ * The type of event, each type corresponds a different operation
13
+ *
14
+ * - 'message' = 'yield'
15
+ * - 'error' = 'throw'
16
+ * - 'done' = 'return'
17
+ *
18
+ * @default 'message'
19
+ */
20
+ event?: 'message' | 'error' | 'done';
21
+ }
22
+ /**
23
+ * Encodes a Hibernation RPC Event
24
+ *
25
+ * @see {@link https://orpc.unnoq.com/docs/plugins/hibernation Hibernation Plugin}
26
+ */
27
+ declare function experimental_encodeHibernationRPCEvent(id: number, payload: unknown, options?: experimental_EncodeHibernationRPCEventOptions): string;
28
+
29
+ interface experimental_HibernationContext {
30
+ iterator?: experimental_HibernationEventIterator<any>;
31
+ }
32
+ /**
33
+ * Enable Hibernation APIs
34
+ *
35
+ * @see {@link https://orpc.unnoq.com/docs/plugins/hibernation Hibernation Plugin}
36
+ */
37
+ declare class experimental_HibernationPlugin<T extends Context> implements StandardHandlerPlugin<T> {
38
+ readonly HIBERNATION_CONTEXT_SYMBOL: symbol;
39
+ order: number;
40
+ init(options: StandardHandlerOptions<T>, _router: Router<any, T>): void;
41
+ }
42
+
43
+ export { experimental_HibernationPlugin, experimental_encodeHibernationRPCEvent };
44
+ export type { experimental_EncodeHibernationRPCEventOptions, experimental_HibernationContext };
@@ -0,0 +1,44 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.-ACo36I0.js';
3
+ import { experimental_HibernationEventIterator } from '@orpc/standard-server';
4
+ export { experimental_HibernationEventIterator, experimental_HibernationEventIteratorCallback } from '@orpc/standard-server';
5
+ import { C as Context, R as Router } from '../shared/server.DD2C4ujN.js';
6
+ import '@orpc/client';
7
+ import '@orpc/contract';
8
+ import '@orpc/shared';
9
+
10
+ interface experimental_EncodeHibernationRPCEventOptions extends StandardRPCJsonSerializerOptions {
11
+ /**
12
+ * The type of event, each type corresponds a different operation
13
+ *
14
+ * - 'message' = 'yield'
15
+ * - 'error' = 'throw'
16
+ * - 'done' = 'return'
17
+ *
18
+ * @default 'message'
19
+ */
20
+ event?: 'message' | 'error' | 'done';
21
+ }
22
+ /**
23
+ * Encodes a Hibernation RPC Event
24
+ *
25
+ * @see {@link https://orpc.unnoq.com/docs/plugins/hibernation Hibernation Plugin}
26
+ */
27
+ declare function experimental_encodeHibernationRPCEvent(id: number, payload: unknown, options?: experimental_EncodeHibernationRPCEventOptions): string;
28
+
29
+ interface experimental_HibernationContext {
30
+ iterator?: experimental_HibernationEventIterator<any>;
31
+ }
32
+ /**
33
+ * Enable Hibernation APIs
34
+ *
35
+ * @see {@link https://orpc.unnoq.com/docs/plugins/hibernation Hibernation Plugin}
36
+ */
37
+ declare class experimental_HibernationPlugin<T extends Context> implements StandardHandlerPlugin<T> {
38
+ readonly HIBERNATION_CONTEXT_SYMBOL: symbol;
39
+ order: number;
40
+ init(options: StandardHandlerOptions<T>, _router: Router<any, T>): void;
41
+ }
42
+
43
+ export { experimental_HibernationPlugin, experimental_encodeHibernationRPCEvent };
44
+ export type { experimental_EncodeHibernationRPCEventOptions, experimental_HibernationContext };
@@ -0,0 +1,65 @@
1
+ import { toORPCError } from '@orpc/client';
2
+ import { StandardRPCJsonSerializer } from '@orpc/client/standard';
3
+ import { stringifyJSON } from '@orpc/shared';
4
+ import { getEventMeta, experimental_HibernationEventIterator } from '@orpc/standard-server';
5
+ export { experimental_HibernationEventIterator } from '@orpc/standard-server';
6
+ import { MessageType } from '@orpc/standard-server-peer';
7
+
8
+ function experimental_encodeHibernationRPCEvent(id, payload, options = {}) {
9
+ const { event = "message", ...rest } = options;
10
+ const serializer = new StandardRPCJsonSerializer(rest);
11
+ const data = event === "error" ? toORPCError(payload).toJSON() : payload;
12
+ const [json, meta] = serializer.serialize(data);
13
+ return stringifyJSON({
14
+ i: id,
15
+ t: MessageType.EVENT_ITERATOR,
16
+ p: {
17
+ e: event,
18
+ d: { json, meta },
19
+ m: getEventMeta(payload)
20
+ }
21
+ });
22
+ }
23
+
24
+ class experimental_HibernationPlugin {
25
+ HIBERNATION_CONTEXT_SYMBOL = Symbol("HIBERNATION_CONTEXT");
26
+ order = 2e6;
27
+ // make sure execute after the batch plugin
28
+ init(options, _router) {
29
+ options.interceptors ??= [];
30
+ options.clientInterceptors ??= [];
31
+ options.interceptors.unshift(async (options2) => {
32
+ const hibernationContext = {};
33
+ const result = await options2.next({
34
+ ...options2,
35
+ context: {
36
+ [this.HIBERNATION_CONTEXT_SYMBOL]: hibernationContext,
37
+ ...options2.context
38
+ }
39
+ });
40
+ if (!result.matched || !hibernationContext.iterator) {
41
+ return result;
42
+ }
43
+ return {
44
+ ...result,
45
+ response: {
46
+ ...result.response,
47
+ body: hibernationContext.iterator
48
+ }
49
+ };
50
+ });
51
+ options.clientInterceptors.unshift(async (options2) => {
52
+ const hibernationContext = options2.context[this.HIBERNATION_CONTEXT_SYMBOL];
53
+ if (!hibernationContext) {
54
+ throw new TypeError("[HibernationPlugin] Hibernation context has been corrupted or modified by another plugin or interceptor");
55
+ }
56
+ const output = await options2.next();
57
+ if (output instanceof experimental_HibernationEventIterator) {
58
+ hibernationContext.iterator = output;
59
+ }
60
+ return output;
61
+ });
62
+ }
63
+ }
64
+
65
+ export { experimental_HibernationPlugin, experimental_encodeHibernationRPCEvent };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "1.4.5",
4
+ "version": "1.5.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -24,6 +24,11 @@
24
24
  "import": "./dist/plugins/index.mjs",
25
25
  "default": "./dist/plugins/index.mjs"
26
26
  },
27
+ "./hibernation": {
28
+ "types": "./dist/hibernation/index.d.mts",
29
+ "import": "./dist/hibernation/index.mjs",
30
+ "default": "./dist/hibernation/index.mjs"
31
+ },
27
32
  "./standard": {
28
33
  "types": "./dist/adapters/standard/index.d.mts",
29
34
  "import": "./dist/adapters/standard/index.mjs",
@@ -86,14 +91,14 @@
86
91
  }
87
92
  },
88
93
  "dependencies": {
89
- "@orpc/client": "1.4.5",
90
- "@orpc/contract": "1.4.5",
91
- "@orpc/standard-server": "1.4.5",
92
- "@orpc/standard-server-aws-lambda": "1.4.5",
93
- "@orpc/shared": "1.4.5",
94
- "@orpc/standard-server-fetch": "1.4.5",
95
- "@orpc/standard-server-peer": "1.4.5",
96
- "@orpc/standard-server-node": "1.4.5"
94
+ "@orpc/client": "1.5.0",
95
+ "@orpc/contract": "1.5.0",
96
+ "@orpc/shared": "1.5.0",
97
+ "@orpc/standard-server": "1.5.0",
98
+ "@orpc/standard-server-aws-lambda": "1.5.0",
99
+ "@orpc/standard-server-fetch": "1.5.0",
100
+ "@orpc/standard-server-node": "1.5.0",
101
+ "@orpc/standard-server-peer": "1.5.0"
97
102
  },
98
103
  "devDependencies": {
99
104
  "@types/ws": "^8.18.1",