@orpc/server 1.14.5 → 2.0.0-beta.1

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.
Files changed (78) hide show
  1. package/README.md +75 -136
  2. package/dist/adapters/crossws/index.d.mts +42 -21
  3. package/dist/adapters/crossws/index.d.ts +42 -21
  4. package/dist/adapters/crossws/index.mjs +37 -18
  5. package/dist/adapters/fetch/index.d.mts +83 -67
  6. package/dist/adapters/fetch/index.d.ts +83 -67
  7. package/dist/adapters/fetch/index.mjs +131 -106
  8. package/dist/adapters/message-port/index.d.mts +51 -34
  9. package/dist/adapters/message-port/index.d.ts +51 -34
  10. package/dist/adapters/message-port/index.mjs +73 -38
  11. package/dist/adapters/node/index.d.mts +82 -60
  12. package/dist/adapters/node/index.d.ts +82 -60
  13. package/dist/adapters/node/index.mjs +127 -98
  14. package/dist/adapters/standard/index.d.mts +16 -18
  15. package/dist/adapters/standard/index.d.ts +16 -18
  16. package/dist/adapters/standard/index.mjs +5 -5
  17. package/dist/adapters/standard-peer/index.d.mts +12 -14
  18. package/dist/adapters/standard-peer/index.d.ts +12 -14
  19. package/dist/adapters/standard-peer/index.mjs +2 -21
  20. package/dist/adapters/websocket/index.d.mts +39 -34
  21. package/dist/adapters/websocket/index.d.ts +39 -34
  22. package/dist/adapters/websocket/index.mjs +42 -33
  23. package/dist/extensions/callable.d.mts +10 -0
  24. package/dist/extensions/callable.d.ts +10 -0
  25. package/dist/extensions/callable.mjs +11 -0
  26. package/dist/helpers/index.d.mts +2 -2
  27. package/dist/helpers/index.d.ts +2 -2
  28. package/dist/helpers/index.mjs +1 -1
  29. package/dist/index.d.mts +163 -770
  30. package/dist/index.d.ts +163 -770
  31. package/dist/index.mjs +296 -403
  32. package/dist/plugins/index.d.mts +105 -143
  33. package/dist/plugins/index.d.ts +105 -143
  34. package/dist/plugins/index.mjs +232 -255
  35. package/dist/shared/server.BB_Ik9Ph.d.mts +104 -0
  36. package/dist/shared/server.BL22TloH.d.mts +184 -0
  37. package/dist/shared/server.BL22TloH.d.ts +184 -0
  38. package/dist/shared/server.B_U9y00a.d.mts +66 -0
  39. package/dist/shared/server.BsNNjG5J.d.mts +61 -0
  40. package/dist/shared/server.BwHnWUuN.mjs +222 -0
  41. package/dist/shared/server.CX4vUnDk.mjs +11 -0
  42. package/dist/shared/server.CjOb6ItT.mjs +41 -0
  43. package/dist/shared/server.CrlKQucM.mjs +233 -0
  44. package/dist/shared/server.D_QauotT.mjs +30 -0
  45. package/dist/shared/server.EOHJ3NJr.d.ts +104 -0
  46. package/dist/shared/server.GDpX6Df8.mjs +271 -0
  47. package/dist/shared/server.Pa0F03f_.d.ts +61 -0
  48. package/dist/shared/server.T9F3bzZx.d.ts +66 -0
  49. package/dist/shared/{server.DZ5BIITo.mjs → server.W91HSRkE.mjs} +2 -2
  50. package/package.json +26 -55
  51. package/dist/adapters/aws-lambda/index.d.mts +0 -46
  52. package/dist/adapters/aws-lambda/index.d.ts +0 -46
  53. package/dist/adapters/aws-lambda/index.mjs +0 -40
  54. package/dist/adapters/bun-ws/index.d.mts +0 -36
  55. package/dist/adapters/bun-ws/index.d.ts +0 -36
  56. package/dist/adapters/bun-ws/index.mjs +0 -47
  57. package/dist/adapters/fastify/index.d.mts +0 -53
  58. package/dist/adapters/fastify/index.d.ts +0 -53
  59. package/dist/adapters/fastify/index.mjs +0 -52
  60. package/dist/adapters/ws/index.d.mts +0 -31
  61. package/dist/adapters/ws/index.d.ts +0 -31
  62. package/dist/adapters/ws/index.mjs +0 -37
  63. package/dist/hibernation/index.d.mts +0 -44
  64. package/dist/hibernation/index.d.ts +0 -44
  65. package/dist/hibernation/index.mjs +0 -65
  66. package/dist/shared/server.7cEtMB30.d.ts +0 -74
  67. package/dist/shared/server.B8gYOD5g.d.mts +0 -12
  68. package/dist/shared/server.BqadksTP.d.mts +0 -74
  69. package/dist/shared/server.C8_sRzQB.d.mts +0 -42
  70. package/dist/shared/server.ChUyt5-i.d.mts +0 -32
  71. package/dist/shared/server.ChyoA9XY.d.ts +0 -42
  72. package/dist/shared/server.DEBcqOjg.mjs +0 -418
  73. package/dist/shared/server.EfTOZ2Q7.d.ts +0 -12
  74. package/dist/shared/server.TEVCLCFC.mjs +0 -39
  75. package/dist/shared/server.ZxHCEN1h.mjs +0 -226
  76. package/dist/shared/server.qKsRrdxW.d.mts +0 -193
  77. package/dist/shared/server.qKsRrdxW.d.ts +0 -193
  78. package/dist/shared/server.yoEB3Fx4.d.ts +0 -32
@@ -1,21 +1,19 @@
1
- import { S as StandardCodec, a as StandardParams } from '../../shared/server.BqadksTP.mjs';
2
- export { C as CompositeStandardHandlerPlugin, b as StandardHandleOptions, c as StandardHandleResult, f as StandardHandler, d as StandardHandlerInterceptorOptions, e as StandardHandlerOptions, g as StandardHandlerPlugin, h as StandardMatchResult, i as StandardMatcher } from '../../shared/server.BqadksTP.mjs';
3
- import { ORPCError } from '@orpc/client';
4
- import { StandardRPCSerializer } from '@orpc/client/standard';
5
- import { StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
6
- import { A as AnyProcedure } from '../../shared/server.qKsRrdxW.mjs';
7
- export { a as StandardRPCHandler, S as StandardRPCHandlerOptions, c as StandardRPCMatcher, b as StandardRPCMatcherOptions } from '../../shared/server.ChUyt5-i.mjs';
8
- export { F as FriendlyStandardHandleOptions, r as resolveFriendlyStandardHandleOptions } from '../../shared/server.B8gYOD5g.mjs';
9
- import '@orpc/contract';
1
+ import { b as StandardHandlerHandleOptions } from '../../shared/server.BB_Ik9Ph.mjs';
2
+ export { C as CompositeStandardHandlerPlugin, O as OtelHandlerPlugin, S as StandardHandler, c as StandardHandlerCodec, d as StandardHandlerCodecResolvedProcedure, g as StandardHandlerHandleResult, h as StandardHandlerInterceptor, i as StandardHandlerInterceptorOptions, a as StandardHandlerOptions, e as StandardHandlerPlugin, j as StandardHandlerRoutingInterceptor, f as StandardHandlerRoutingInterceptorOptions } from '../../shared/server.BB_Ik9Ph.mjs';
3
+ export { a as RPCHandlerCodec, R as RPCHandlerCodecOptions, b as RPCMatcher, c as RPCMatcherOptions } from '../../shared/server.B_U9y00a.mjs';
4
+ import { C as Context } from '../../shared/server.BL22TloH.mjs';
5
+ import '@orpc/client';
10
6
  import '@orpc/shared';
11
- import '../../shared/server.C8_sRzQB.mjs';
7
+ import '@standardserver/core';
8
+ import '@orpc/contract';
9
+ import '../../shared/server.BsNNjG5J.mjs';
12
10
 
13
- declare class StandardRPCCodec implements StandardCodec {
14
- private readonly serializer;
15
- constructor(serializer: StandardRPCSerializer);
16
- decode(request: StandardLazyRequest, _params: StandardParams | undefined, _procedure: AnyProcedure): Promise<unknown>;
17
- encode(output: unknown, _procedure: AnyProcedure): StandardResponse;
18
- encodeError(error: ORPCError<any, any>): StandardResponse;
19
- }
11
+ type FriendlyStandardHandlerHandleOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (object extends T ? {
12
+ context?: T;
13
+ } : {
14
+ context: T;
15
+ });
16
+ declare function resolveFriendlyStandardHandlerHandleOptions<T extends Context>(options: FriendlyStandardHandlerHandleOptions<T>): StandardHandlerHandleOptions<T>;
20
17
 
21
- export { StandardCodec, StandardParams, StandardRPCCodec };
18
+ export { StandardHandlerHandleOptions, resolveFriendlyStandardHandlerHandleOptions };
19
+ export type { FriendlyStandardHandlerHandleOptions };
@@ -1,21 +1,19 @@
1
- import { S as StandardCodec, a as StandardParams } from '../../shared/server.7cEtMB30.js';
2
- export { C as CompositeStandardHandlerPlugin, b as StandardHandleOptions, c as StandardHandleResult, f as StandardHandler, d as StandardHandlerInterceptorOptions, e as StandardHandlerOptions, g as StandardHandlerPlugin, h as StandardMatchResult, i as StandardMatcher } from '../../shared/server.7cEtMB30.js';
3
- import { ORPCError } from '@orpc/client';
4
- import { StandardRPCSerializer } from '@orpc/client/standard';
5
- import { StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
6
- import { A as AnyProcedure } from '../../shared/server.qKsRrdxW.js';
7
- export { a as StandardRPCHandler, S as StandardRPCHandlerOptions, c as StandardRPCMatcher, b as StandardRPCMatcherOptions } from '../../shared/server.yoEB3Fx4.js';
8
- export { F as FriendlyStandardHandleOptions, r as resolveFriendlyStandardHandleOptions } from '../../shared/server.EfTOZ2Q7.js';
9
- import '@orpc/contract';
1
+ import { b as StandardHandlerHandleOptions } from '../../shared/server.EOHJ3NJr.js';
2
+ export { C as CompositeStandardHandlerPlugin, O as OtelHandlerPlugin, S as StandardHandler, c as StandardHandlerCodec, d as StandardHandlerCodecResolvedProcedure, g as StandardHandlerHandleResult, h as StandardHandlerInterceptor, i as StandardHandlerInterceptorOptions, a as StandardHandlerOptions, e as StandardHandlerPlugin, j as StandardHandlerRoutingInterceptor, f as StandardHandlerRoutingInterceptorOptions } from '../../shared/server.EOHJ3NJr.js';
3
+ export { a as RPCHandlerCodec, R as RPCHandlerCodecOptions, b as RPCMatcher, c as RPCMatcherOptions } from '../../shared/server.T9F3bzZx.js';
4
+ import { C as Context } from '../../shared/server.BL22TloH.js';
5
+ import '@orpc/client';
10
6
  import '@orpc/shared';
11
- import '../../shared/server.ChyoA9XY.js';
7
+ import '@standardserver/core';
8
+ import '@orpc/contract';
9
+ import '../../shared/server.Pa0F03f_.js';
12
10
 
13
- declare class StandardRPCCodec implements StandardCodec {
14
- private readonly serializer;
15
- constructor(serializer: StandardRPCSerializer);
16
- decode(request: StandardLazyRequest, _params: StandardParams | undefined, _procedure: AnyProcedure): Promise<unknown>;
17
- encode(output: unknown, _procedure: AnyProcedure): StandardResponse;
18
- encodeError(error: ORPCError<any, any>): StandardResponse;
19
- }
11
+ type FriendlyStandardHandlerHandleOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (object extends T ? {
12
+ context?: T;
13
+ } : {
14
+ context: T;
15
+ });
16
+ declare function resolveFriendlyStandardHandlerHandleOptions<T extends Context>(options: FriendlyStandardHandlerHandleOptions<T>): StandardHandlerHandleOptions<T>;
20
17
 
21
- export { StandardCodec, StandardParams, StandardRPCCodec };
18
+ export { StandardHandlerHandleOptions, resolveFriendlyStandardHandlerHandleOptions };
19
+ export type { FriendlyStandardHandlerHandleOptions };
@@ -1,8 +1,8 @@
1
- export { C as CompositeStandardHandlerPlugin, S as StandardHandler, a as StandardRPCCodec, b as StandardRPCHandler, c as StandardRPCMatcher } from '../../shared/server.ZxHCEN1h.mjs';
2
- export { r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DZ5BIITo.mjs';
3
- import '@orpc/client/standard';
1
+ export { C as CompositeStandardHandlerPlugin, O as OtelHandlerPlugin, R as RPCHandlerCodec, a as RPCMatcher, S as StandardHandler } from '../../shared/server.GDpX6Df8.mjs';
2
+ export { r as resolveFriendlyStandardHandlerHandleOptions } from '../../shared/server.W91HSRkE.mjs';
4
3
  import '@orpc/client';
5
4
  import '@orpc/shared';
6
- import '@orpc/standard-server';
7
- import '../../shared/server.DEBcqOjg.mjs';
5
+ import '@standardserver/core';
6
+ import '../../shared/server.CrlKQucM.mjs';
8
7
  import '@orpc/contract';
8
+ import '../../shared/server.BwHnWUuN.mjs';
@@ -1,18 +1,16 @@
1
- import { ServerPeer, EncodedMessage, ServerPeerHandleRequestFn } from '@orpc/standard-server-peer';
2
- import { C as Context } from '../../shared/server.qKsRrdxW.mjs';
3
- import { F as FriendlyStandardHandleOptions } from '../../shared/server.B8gYOD5g.mjs';
4
- import { f as StandardHandler } from '../../shared/server.BqadksTP.mjs';
1
+ import { Value, Promisable } from '@orpc/shared';
2
+ import { StandardLazyRequest, StandardResponse } from '@standardserver/core';
3
+ import { C as Context } from '../../shared/server.BL22TloH.mjs';
4
+ import { b as StandardHandlerHandleOptions, S as StandardHandler } from '../../shared/server.BB_Ik9Ph.mjs';
5
5
  import '@orpc/client';
6
6
  import '@orpc/contract';
7
- import '@orpc/shared';
8
- import '@orpc/standard-server';
9
7
 
10
- type HandleStandardServerPeerMessageOptions<T extends Context> = Omit<FriendlyStandardHandleOptions<T>, 'prefix'>;
11
- /**
12
- * @deprecated Use `createServerPeerRequestHandleFn` instead.
13
- */
14
- declare function handleStandardServerPeerMessage<T extends Context>(handler: StandardHandler<T>, peer: ServerPeer, message: EncodedMessage, options: HandleStandardServerPeerMessageOptions<T>): Promise<void>;
15
- declare function createServerPeerHandleRequestFn<T extends Context>(handler: StandardHandler<T>, options: HandleStandardServerPeerMessageOptions<T>): ServerPeerHandleRequestFn;
8
+ type StandardPeerRequestHandlerOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
9
+ context?: Value<Promisable<T>, [request: StandardLazyRequest]>;
10
+ } : {
11
+ context: Value<Promisable<T>, [request: StandardLazyRequest]>;
12
+ });
13
+ declare function createStandardPeerRequestHandler<T extends Context>(handler: StandardHandler<T>, options: StandardPeerRequestHandlerOptions<T>): (request: StandardLazyRequest) => Promise<StandardResponse>;
16
14
 
17
- export { createServerPeerHandleRequestFn, handleStandardServerPeerMessage };
18
- export type { HandleStandardServerPeerMessageOptions };
15
+ export { createStandardPeerRequestHandler };
16
+ export type { StandardPeerRequestHandlerOptions };
@@ -1,18 +1,16 @@
1
- import { ServerPeer, EncodedMessage, ServerPeerHandleRequestFn } from '@orpc/standard-server-peer';
2
- import { C as Context } from '../../shared/server.qKsRrdxW.js';
3
- import { F as FriendlyStandardHandleOptions } from '../../shared/server.EfTOZ2Q7.js';
4
- import { f as StandardHandler } from '../../shared/server.7cEtMB30.js';
1
+ import { Value, Promisable } from '@orpc/shared';
2
+ import { StandardLazyRequest, StandardResponse } from '@standardserver/core';
3
+ import { C as Context } from '../../shared/server.BL22TloH.js';
4
+ import { b as StandardHandlerHandleOptions, S as StandardHandler } from '../../shared/server.EOHJ3NJr.js';
5
5
  import '@orpc/client';
6
6
  import '@orpc/contract';
7
- import '@orpc/shared';
8
- import '@orpc/standard-server';
9
7
 
10
- type HandleStandardServerPeerMessageOptions<T extends Context> = Omit<FriendlyStandardHandleOptions<T>, 'prefix'>;
11
- /**
12
- * @deprecated Use `createServerPeerRequestHandleFn` instead.
13
- */
14
- declare function handleStandardServerPeerMessage<T extends Context>(handler: StandardHandler<T>, peer: ServerPeer, message: EncodedMessage, options: HandleStandardServerPeerMessageOptions<T>): Promise<void>;
15
- declare function createServerPeerHandleRequestFn<T extends Context>(handler: StandardHandler<T>, options: HandleStandardServerPeerMessageOptions<T>): ServerPeerHandleRequestFn;
8
+ type StandardPeerRequestHandlerOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
9
+ context?: Value<Promisable<T>, [request: StandardLazyRequest]>;
10
+ } : {
11
+ context: Value<Promisable<T>, [request: StandardLazyRequest]>;
12
+ });
13
+ declare function createStandardPeerRequestHandler<T extends Context>(handler: StandardHandler<T>, options: StandardPeerRequestHandlerOptions<T>): (request: StandardLazyRequest) => Promise<StandardResponse>;
16
14
 
17
- export { createServerPeerHandleRequestFn, handleStandardServerPeerMessage };
18
- export type { HandleStandardServerPeerMessageOptions };
15
+ export { createStandardPeerRequestHandler };
16
+ export type { StandardPeerRequestHandlerOptions };
@@ -1,21 +1,2 @@
1
- import { r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DZ5BIITo.mjs';
2
-
3
- async function handleStandardServerPeerMessage(handler, peer, message, options) {
4
- const [id, request] = await peer.message(message);
5
- if (!request) {
6
- return;
7
- }
8
- const handle = createServerPeerHandleRequestFn(handler, options);
9
- await peer.response(id, await handle(request));
10
- }
11
- function createServerPeerHandleRequestFn(handler, options) {
12
- return async (request) => {
13
- const { response } = await handler.handle(
14
- { ...request, body: () => Promise.resolve(request.body) },
15
- resolveFriendlyStandardHandleOptions(options)
16
- );
17
- return response ?? { status: 404, headers: {}, body: "No procedure matched" };
18
- };
19
- }
20
-
21
- export { createServerPeerHandleRequestFn, handleStandardServerPeerMessage };
1
+ export { c as createStandardPeerRequestHandler } from '../../shared/server.CX4vUnDk.mjs';
2
+ import '@orpc/shared';
@@ -1,56 +1,61 @@
1
1
  import { MaybeOptionalOptions } from '@orpc/shared';
2
- import { C as Context, R as Router } from '../../shared/server.qKsRrdxW.mjs';
3
- import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.mjs';
4
- import { f as StandardHandler } from '../../shared/server.BqadksTP.mjs';
5
- import { S as StandardRPCHandlerOptions } from '../../shared/server.ChUyt5-i.mjs';
2
+ import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
3
+ import { C as Context } from '../../shared/server.BL22TloH.mjs';
4
+ import { S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.BB_Ik9Ph.mjs';
5
+ import { StandardPeerRequestHandlerOptions } from '../standard-peer/index.mjs';
6
+ import { R as Router } from '../../shared/server.BsNNjG5J.mjs';
7
+ import { R as RPCHandlerCodecOptions } from '../../shared/server.B_U9y00a.mjs';
6
8
  import '@orpc/client';
7
9
  import '@orpc/contract';
8
- import '@orpc/standard-server-peer';
9
- import '../../shared/server.B8gYOD5g.mjs';
10
- import '@orpc/standard-server';
11
- import '@orpc/client/standard';
12
- import '../../shared/server.C8_sRzQB.mjs';
10
+ import '@standardserver/core';
13
11
 
14
- type MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
12
+ type WebsocketLike = Pick<WebSocket, 'addEventListener' | 'removeEventListener' | 'send'>;
13
+ interface WebsocketHandlerOptions<_T extends Context> {
14
+ /**
15
+ * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
16
+ */
17
+ encodePeerMessage?: EncodePeerMessageOptions | undefined;
18
+ /**
19
+ * Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
20
+ */
21
+ decodePeerMessage?: DecodePeerMessageOptions | undefined;
22
+ }
15
23
  declare class WebsocketHandler<T extends Context> {
16
- #private;
17
- constructor(standardHandler: StandardHandler<T>);
24
+ private readonly handler;
25
+ private readonly peers;
26
+ private readonly encodePeerMessageOptions;
27
+ private readonly decodePeerMessageOptions;
28
+ constructor(handler: StandardHandler<T>, options?: NoInfer<WebsocketHandlerOptions<T>>);
18
29
  /**
19
- * Upgrades a WebSocket to enable handling
20
- *
21
- * This attaches the necessary 'message' and 'close' listeners to the WebSocket
22
- *
23
- * @warning Do not use this method if you're using `.message()` or `.close()`
30
+ * Attaches necessary event listeners to a WebSocket to handle incoming messages and peer management.
24
31
  */
25
- upgrade(ws: MinimalWebsocket, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): void;
32
+ upgrade(ws: WebsocketLike, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
26
33
  /**
27
34
  * Handles a single message received from a WebSocket.
28
35
  *
29
- * @warning Avoid calling this directly if `.upgrade()` is used.
36
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
30
37
  *
31
- * @param ws The WebSocket instance
32
- * @param data The message payload, usually place in `event.data`
38
+ * @param ws The WebSocket instance, require consistent instance across messages for proper peer management
39
+ * @param data The message data received from the WebSocket, can be string, ArrayBuffer, Blob, ...
33
40
  */
34
- message(ws: MinimalWebsocket, data: string | ArrayBuffer | Blob, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): Promise<void>;
41
+ message(ws: WebsocketLike, data: string | ArrayBuffer | Blob | Exclude<ConstructorParameters<typeof Blob>[0], undefined>[0][] | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
42
+ matched: boolean;
43
+ }>;
35
44
  /**
36
- * Closes the WebSocket peer and cleans up.
45
+ * Called when a websocket is closed, to clean up any associated peer state.
46
+ *
47
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
37
48
  *
38
- * @warning Avoid calling this directly if `.upgrade()` is used.
49
+ * @param ws The WebSocket instance to clean up, must be the same instance used in `.message()` calls to properly clean up
39
50
  */
40
- close(ws: MinimalWebsocket): void;
51
+ close(ws: WebsocketLike): Promise<void>;
41
52
  }
42
53
 
43
- interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T> {
54
+ interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebsocketHandlerOptions<T> {
44
55
  }
45
- /**
46
- * RPC Handler for Websocket adapter
47
- *
48
- * @see {@link https://orpc.dev/docs/rpc-handler RPC Handler Docs}
49
- * @see {@link https://orpc.dev/docs/adapters/websocket Websocket Adapter Docs}
50
- */
51
56
  declare class RPCHandler<T extends Context> extends WebsocketHandler<T> {
52
- constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
57
+ constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
53
58
  }
54
59
 
55
60
  export { RPCHandler, WebsocketHandler };
56
- export type { MinimalWebsocket, RPCHandlerOptions };
61
+ export type { RPCHandlerOptions, WebsocketHandlerOptions, WebsocketLike };
@@ -1,56 +1,61 @@
1
1
  import { MaybeOptionalOptions } from '@orpc/shared';
2
- import { C as Context, R as Router } from '../../shared/server.qKsRrdxW.js';
3
- import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.js';
4
- import { f as StandardHandler } from '../../shared/server.7cEtMB30.js';
5
- import { S as StandardRPCHandlerOptions } from '../../shared/server.yoEB3Fx4.js';
2
+ import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
3
+ import { C as Context } from '../../shared/server.BL22TloH.js';
4
+ import { S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.EOHJ3NJr.js';
5
+ import { StandardPeerRequestHandlerOptions } from '../standard-peer/index.js';
6
+ import { R as Router } from '../../shared/server.Pa0F03f_.js';
7
+ import { R as RPCHandlerCodecOptions } from '../../shared/server.T9F3bzZx.js';
6
8
  import '@orpc/client';
7
9
  import '@orpc/contract';
8
- import '@orpc/standard-server-peer';
9
- import '../../shared/server.EfTOZ2Q7.js';
10
- import '@orpc/standard-server';
11
- import '@orpc/client/standard';
12
- import '../../shared/server.ChyoA9XY.js';
10
+ import '@standardserver/core';
13
11
 
14
- type MinimalWebsocket = Pick<WebSocket, 'addEventListener' | 'send'>;
12
+ type WebsocketLike = Pick<WebSocket, 'addEventListener' | 'removeEventListener' | 'send'>;
13
+ interface WebsocketHandlerOptions<_T extends Context> {
14
+ /**
15
+ * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
16
+ */
17
+ encodePeerMessage?: EncodePeerMessageOptions | undefined;
18
+ /**
19
+ * Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
20
+ */
21
+ decodePeerMessage?: DecodePeerMessageOptions | undefined;
22
+ }
15
23
  declare class WebsocketHandler<T extends Context> {
16
- #private;
17
- constructor(standardHandler: StandardHandler<T>);
24
+ private readonly handler;
25
+ private readonly peers;
26
+ private readonly encodePeerMessageOptions;
27
+ private readonly decodePeerMessageOptions;
28
+ constructor(handler: StandardHandler<T>, options?: NoInfer<WebsocketHandlerOptions<T>>);
18
29
  /**
19
- * Upgrades a WebSocket to enable handling
20
- *
21
- * This attaches the necessary 'message' and 'close' listeners to the WebSocket
22
- *
23
- * @warning Do not use this method if you're using `.message()` or `.close()`
30
+ * Attaches necessary event listeners to a WebSocket to handle incoming messages and peer management.
24
31
  */
25
- upgrade(ws: MinimalWebsocket, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): void;
32
+ upgrade(ws: WebsocketLike, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
26
33
  /**
27
34
  * Handles a single message received from a WebSocket.
28
35
  *
29
- * @warning Avoid calling this directly if `.upgrade()` is used.
36
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
30
37
  *
31
- * @param ws The WebSocket instance
32
- * @param data The message payload, usually place in `event.data`
38
+ * @param ws The WebSocket instance, require consistent instance across messages for proper peer management
39
+ * @param data The message data received from the WebSocket, can be string, ArrayBuffer, Blob, ...
33
40
  */
34
- message(ws: MinimalWebsocket, data: string | ArrayBuffer | Blob, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): Promise<void>;
41
+ message(ws: WebsocketLike, data: string | ArrayBuffer | Blob | Exclude<ConstructorParameters<typeof Blob>[0], undefined>[0][] | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
42
+ matched: boolean;
43
+ }>;
35
44
  /**
36
- * Closes the WebSocket peer and cleans up.
45
+ * Called when a websocket is closed, to clean up any associated peer state.
46
+ *
47
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
37
48
  *
38
- * @warning Avoid calling this directly if `.upgrade()` is used.
49
+ * @param ws The WebSocket instance to clean up, must be the same instance used in `.message()` calls to properly clean up
39
50
  */
40
- close(ws: MinimalWebsocket): void;
51
+ close(ws: WebsocketLike): Promise<void>;
41
52
  }
42
53
 
43
- interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T> {
54
+ interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebsocketHandlerOptions<T> {
44
55
  }
45
- /**
46
- * RPC Handler for Websocket adapter
47
- *
48
- * @see {@link https://orpc.dev/docs/rpc-handler RPC Handler Docs}
49
- * @see {@link https://orpc.dev/docs/adapters/websocket Websocket Adapter Docs}
50
- */
51
56
  declare class RPCHandler<T extends Context> extends WebsocketHandler<T> {
52
- constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
57
+ constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
53
58
  }
54
59
 
55
60
  export { RPCHandler, WebsocketHandler };
56
- export type { MinimalWebsocket, RPCHandlerOptions };
61
+ export type { RPCHandlerOptions, WebsocketHandlerOptions, WebsocketLike };
@@ -1,26 +1,24 @@
1
- import { readAsBuffer, resolveMaybeOptionalOptions } from '@orpc/shared';
2
- import { ServerPeer } from '@orpc/standard-server-peer';
3
- import { createServerPeerHandleRequestFn } from '../standard-peer/index.mjs';
4
- import { b as StandardRPCHandler } from '../../shared/server.ZxHCEN1h.mjs';
5
- import '../../shared/server.DZ5BIITo.mjs';
6
- import '@orpc/client/standard';
1
+ import { toStringOrBytes, resolveMaybeOptionalOptions } from '@orpc/shared';
2
+ import { ServerPeer, encodePeerMessage, decodePeerMessage, isClientPeerSendMessage } from '@standardserver/peer';
3
+ import { c as createStandardPeerRequestHandler } from '../../shared/server.CX4vUnDk.mjs';
4
+ import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.GDpX6Df8.mjs';
7
5
  import '@orpc/client';
8
- import '@orpc/standard-server';
9
- import '../../shared/server.DEBcqOjg.mjs';
6
+ import '@standardserver/core';
7
+ import '../../shared/server.CrlKQucM.mjs';
10
8
  import '@orpc/contract';
9
+ import '../../shared/server.BwHnWUuN.mjs';
11
10
 
12
11
  class WebsocketHandler {
13
- #peers = /* @__PURE__ */ new WeakMap();
14
- #handler;
15
- constructor(standardHandler) {
16
- this.#handler = standardHandler;
12
+ constructor(handler, options = {}) {
13
+ this.handler = handler;
14
+ this.encodePeerMessageOptions = options.encodePeerMessage;
15
+ this.decodePeerMessageOptions = options.decodePeerMessage;
17
16
  }
17
+ peers = /* @__PURE__ */ new WeakMap();
18
+ encodePeerMessageOptions;
19
+ decodePeerMessageOptions;
18
20
  /**
19
- * Upgrades a WebSocket to enable handling
20
- *
21
- * This attaches the necessary 'message' and 'close' listeners to the WebSocket
22
- *
23
- * @warning Do not use this method if you're using `.message()` or `.close()`
21
+ * Attaches necessary event listeners to a WebSocket to handle incoming messages and peer management.
24
22
  */
25
23
  upgrade(ws, ...rest) {
26
24
  ws.addEventListener("message", (event) => this.message(ws, event.data, ...rest));
@@ -29,38 +27,49 @@ class WebsocketHandler {
29
27
  /**
30
28
  * Handles a single message received from a WebSocket.
31
29
  *
32
- * @warning Avoid calling this directly if `.upgrade()` is used.
30
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
33
31
  *
34
- * @param ws The WebSocket instance
35
- * @param data The message payload, usually place in `event.data`
32
+ * @param ws The WebSocket instance, require consistent instance across messages for proper peer management
33
+ * @param data The message data received from the WebSocket, can be string, ArrayBuffer, Blob, ...
36
34
  */
37
35
  async message(ws, data, ...rest) {
38
- let peer = this.#peers.get(ws);
36
+ let peer = this.peers.get(ws);
39
37
  if (!peer) {
40
- this.#peers.set(ws, peer = new ServerPeer(ws.send.bind(ws)));
38
+ this.peers.set(ws, peer = new ServerPeer(async (message) => {
39
+ return ws.send(await encodePeerMessage(message, this.encodePeerMessageOptions));
40
+ }));
41
41
  }
42
- const message = data instanceof Blob ? await readAsBuffer(data) : data;
43
- await peer.message(
44
- message,
45
- createServerPeerHandleRequestFn(this.#handler, resolveMaybeOptionalOptions(rest))
46
- );
42
+ const encoded = await toStringOrBytes(data);
43
+ const result = decodePeerMessage(encoded, this.decodePeerMessageOptions);
44
+ if (result.matched && isClientPeerSendMessage(result.message)) {
45
+ await peer.message(
46
+ result.message,
47
+ createStandardPeerRequestHandler(this.handler, resolveMaybeOptionalOptions(rest))
48
+ );
49
+ }
50
+ return result;
47
51
  }
48
52
  /**
49
- * Closes the WebSocket peer and cleans up.
53
+ * Called when a websocket is closed, to clean up any associated peer state.
54
+ *
55
+ * @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
50
56
  *
51
- * @warning Avoid calling this directly if `.upgrade()` is used.
57
+ * @param ws The WebSocket instance to clean up, must be the same instance used in `.message()` calls to properly clean up
52
58
  */
53
- close(ws) {
54
- const peer = this.#peers.get(ws);
59
+ async close(ws) {
60
+ const peer = this.peers.get(ws);
55
61
  if (peer) {
56
- peer.close();
62
+ await peer.close();
63
+ this.peers.delete(ws);
57
64
  }
58
65
  }
59
66
  }
60
67
 
61
68
  class RPCHandler extends WebsocketHandler {
62
69
  constructor(router, options = {}) {
63
- super(new StandardRPCHandler(router, options));
70
+ const codec = new RPCHandlerCodec(router, options);
71
+ const handler = new StandardHandler(codec, options);
72
+ super(handler, options);
64
73
  }
65
74
  }
66
75
 
@@ -0,0 +1,10 @@
1
+ import { AnyORPCError, ClientContext } from '@orpc/client';
2
+ import { AnySchema, ErrorMap } from '@orpc/contract';
3
+ import { MaybeOptionalOptions } from '@orpc/shared';
4
+ import { C as Context, P as ProcedureClientOptions, a as Procedure, b as ProcedureClient } from '../shared/server.BL22TloH.mjs';
5
+
6
+ declare module "../index" {
7
+ interface DecoratedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
8
+ callable<TClientContext extends ClientContext>(...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError> & ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ import { AnyORPCError, ClientContext } from '@orpc/client';
2
+ import { AnySchema, ErrorMap } from '@orpc/contract';
3
+ import { MaybeOptionalOptions } from '@orpc/shared';
4
+ import { C as Context, P as ProcedureClientOptions, a as Procedure, b as ProcedureClient } from '../shared/server.BL22TloH.js';
5
+
6
+ declare module "../index" {
7
+ interface DecoratedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
8
+ callable<TClientContext extends ClientContext>(...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError> & ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
9
+ }
10
+ }
@@ -0,0 +1,11 @@
1
+ import { c as createProcedureClient } from '../shared/server.CrlKQucM.mjs';
2
+ import { D as DecoratedProcedure } from '../shared/server.CjOb6ItT.mjs';
3
+ import '@orpc/client';
4
+ import '@orpc/contract';
5
+ import '@orpc/shared';
6
+
7
+ DecoratedProcedure.prototype.callable = function callable(...rest) {
8
+ const callable2 = createProcedureClient(this, ...rest);
9
+ callable2["~orpc"] = this["~orpc"];
10
+ return callable2;
11
+ };
@@ -1,4 +1,4 @@
1
- import { SerializeOptions, ParseOptions } from 'cookie';
1
+ import { ParseOptions, SerializeOptions } from 'cookie';
2
2
 
3
3
  /**
4
4
  * Encodes a Uint8Array to base64url format
@@ -25,7 +25,7 @@ declare function encodeBase64url(data: Uint8Array): string;
25
25
  * expect(new TextDecoder().decode(decoded)).toEqual(text)
26
26
  * ```
27
27
  */
28
- declare function decodeBase64url(base64url: string | undefined | null): Uint8Array | undefined;
28
+ declare function decodeBase64url(base64url: string | undefined | null): Uint8Array<ArrayBuffer> | undefined;
29
29
 
30
30
  interface SetCookieOptions extends SerializeOptions {
31
31
  /**
@@ -1,4 +1,4 @@
1
- import { SerializeOptions, ParseOptions } from 'cookie';
1
+ import { ParseOptions, SerializeOptions } from 'cookie';
2
2
 
3
3
  /**
4
4
  * Encodes a Uint8Array to base64url format
@@ -25,7 +25,7 @@ declare function encodeBase64url(data: Uint8Array): string;
25
25
  * expect(new TextDecoder().decode(decoded)).toEqual(text)
26
26
  * ```
27
27
  */
28
- declare function decodeBase64url(base64url: string | undefined | null): Uint8Array | undefined;
28
+ declare function decodeBase64url(base64url: string | undefined | null): Uint8Array<ArrayBuffer> | undefined;
29
29
 
30
30
  interface SetCookieOptions extends SerializeOptions {
31
31
  /**
@@ -1,4 +1,4 @@
1
- import { serialize, parse } from 'cookie';
1
+ import { parse, serialize } from 'cookie';
2
2
 
3
3
  function encodeBase64url(data) {
4
4
  const chunkSize = 8192;