@orpc/server 1.10.0 → 1.10.2

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.
@@ -0,0 +1,53 @@
1
+ import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
2
+ import { FastifyRequest, FastifyReply, SendStandardResponseOptions } from '@orpc/standard-server-fastify';
3
+ import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.mjs';
4
+ import { b as StandardHandleOptions, f as StandardHandler } from '../../shared/server.DBCUJijK.mjs';
5
+ import { F as FriendlyStandardHandleOptions } from '../../shared/server.CVKCo60T.mjs';
6
+ import { S as StandardRPCHandlerOptions } from '../../shared/server.Ck-gOLzq.mjs';
7
+ import '@orpc/client';
8
+ import '@orpc/contract';
9
+ import '@orpc/standard-server';
10
+ import '@orpc/client/standard';
11
+ import '../../shared/server.DzV1hr3z.mjs';
12
+
13
+ type FastifyHandleResult = {
14
+ matched: true;
15
+ } | {
16
+ matched: false;
17
+ };
18
+ interface FastifyInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
19
+ request: FastifyRequest;
20
+ reply: FastifyReply;
21
+ sendStandardResponseOptions: SendStandardResponseOptions;
22
+ }
23
+ interface FastifyHandlerOptions<T extends Context> extends SendStandardResponseOptions {
24
+ adapterInterceptors?: Interceptor<FastifyInterceptorOptions<T>, Promise<FastifyHandleResult>>[];
25
+ }
26
+ declare class FastifyHandler<T extends Context> {
27
+ private readonly standardHandler;
28
+ private readonly sendStandardResponseOptions;
29
+ private readonly adapterInterceptors;
30
+ constructor(standardHandler: StandardHandler<T>, options?: NoInfer<FastifyHandlerOptions<T>>);
31
+ handle(request: FastifyRequest, reply: FastifyReply, ...rest: MaybeOptionalOptions<FriendlyStandardHandleOptions<T>>): Promise<FastifyHandleResult>;
32
+ }
33
+
34
+ interface RPCHandlerOptions<T extends Context> extends FastifyHandlerOptions<T>, StandardRPCHandlerOptions<T> {
35
+ /**
36
+ * Enables or disables the StrictGetMethodPlugin.
37
+ *
38
+ * @default true
39
+ */
40
+ strictGetMethodPluginEnabled?: boolean;
41
+ }
42
+ /**
43
+ * RPC Handler for Fastify Server
44
+ *
45
+ * @see {@link https://orpc.unnoq.com/docs/rpc-handler RPC Handler Docs}
46
+ * @see {@link https://orpc.unnoq.com/docs/adapters/http HTTP Adapter Docs}
47
+ */
48
+ declare class RPCHandler<T extends Context> extends FastifyHandler<T> {
49
+ constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
50
+ }
51
+
52
+ export { FastifyHandler, RPCHandler };
53
+ export type { FastifyHandleResult, FastifyHandlerOptions, FastifyInterceptorOptions, RPCHandlerOptions };
@@ -0,0 +1,53 @@
1
+ import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
2
+ import { FastifyRequest, FastifyReply, SendStandardResponseOptions } from '@orpc/standard-server-fastify';
3
+ import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.js';
4
+ import { b as StandardHandleOptions, f as StandardHandler } from '../../shared/server.DPIFWpxG.js';
5
+ import { F as FriendlyStandardHandleOptions } from '../../shared/server.DNtJ-p60.js';
6
+ import { S as StandardRPCHandlerOptions } from '../../shared/server.COL12UTb.js';
7
+ import '@orpc/client';
8
+ import '@orpc/contract';
9
+ import '@orpc/standard-server';
10
+ import '@orpc/client/standard';
11
+ import '../../shared/server.Cb6yD7DZ.js';
12
+
13
+ type FastifyHandleResult = {
14
+ matched: true;
15
+ } | {
16
+ matched: false;
17
+ };
18
+ interface FastifyInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
19
+ request: FastifyRequest;
20
+ reply: FastifyReply;
21
+ sendStandardResponseOptions: SendStandardResponseOptions;
22
+ }
23
+ interface FastifyHandlerOptions<T extends Context> extends SendStandardResponseOptions {
24
+ adapterInterceptors?: Interceptor<FastifyInterceptorOptions<T>, Promise<FastifyHandleResult>>[];
25
+ }
26
+ declare class FastifyHandler<T extends Context> {
27
+ private readonly standardHandler;
28
+ private readonly sendStandardResponseOptions;
29
+ private readonly adapterInterceptors;
30
+ constructor(standardHandler: StandardHandler<T>, options?: NoInfer<FastifyHandlerOptions<T>>);
31
+ handle(request: FastifyRequest, reply: FastifyReply, ...rest: MaybeOptionalOptions<FriendlyStandardHandleOptions<T>>): Promise<FastifyHandleResult>;
32
+ }
33
+
34
+ interface RPCHandlerOptions<T extends Context> extends FastifyHandlerOptions<T>, StandardRPCHandlerOptions<T> {
35
+ /**
36
+ * Enables or disables the StrictGetMethodPlugin.
37
+ *
38
+ * @default true
39
+ */
40
+ strictGetMethodPluginEnabled?: boolean;
41
+ }
42
+ /**
43
+ * RPC Handler for Fastify Server
44
+ *
45
+ * @see {@link https://orpc.unnoq.com/docs/rpc-handler RPC Handler Docs}
46
+ * @see {@link https://orpc.unnoq.com/docs/adapters/http HTTP Adapter Docs}
47
+ */
48
+ declare class RPCHandler<T extends Context> extends FastifyHandler<T> {
49
+ constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
50
+ }
51
+
52
+ export { FastifyHandler, RPCHandler };
53
+ export type { FastifyHandleResult, FastifyHandlerOptions, FastifyInterceptorOptions, RPCHandlerOptions };
@@ -0,0 +1,54 @@
1
+ import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
2
+ import { toStandardLazyRequest, sendStandardResponse } from '@orpc/standard-server-fastify';
3
+ import { r as resolveFriendlyStandardHandleOptions } from '../../shared/server.DZ5BIITo.mjs';
4
+ import '@orpc/standard-server';
5
+ import '@orpc/standard-server/batch';
6
+ import '@orpc/standard-server-fetch';
7
+ import '@orpc/client';
8
+ import { S as StrictGetMethodPlugin } from '../../shared/server.TEVCLCFC.mjs';
9
+ import '@orpc/contract';
10
+ import { b as StandardRPCHandler } from '../../shared/server.Bxx6tqNe.mjs';
11
+ import '@orpc/client/standard';
12
+ import '../../shared/server.Ds4HPpvH.mjs';
13
+
14
+ class FastifyHandler {
15
+ constructor(standardHandler, options = {}) {
16
+ this.standardHandler = standardHandler;
17
+ this.adapterInterceptors = toArray(options.adapterInterceptors);
18
+ this.sendStandardResponseOptions = options;
19
+ }
20
+ sendStandardResponseOptions;
21
+ adapterInterceptors;
22
+ async handle(request, reply, ...rest) {
23
+ return intercept(
24
+ this.adapterInterceptors,
25
+ {
26
+ ...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
27
+ request,
28
+ reply,
29
+ sendStandardResponseOptions: this.sendStandardResponseOptions
30
+ },
31
+ async ({ request: request2, reply: reply2, sendStandardResponseOptions, ...options }) => {
32
+ const standardRequest = toStandardLazyRequest(request2, reply2);
33
+ const result = await this.standardHandler.handle(standardRequest, options);
34
+ if (!result.matched) {
35
+ return { matched: false };
36
+ }
37
+ await sendStandardResponse(reply2, result.response, sendStandardResponseOptions);
38
+ return { matched: true };
39
+ }
40
+ );
41
+ }
42
+ }
43
+
44
+ class RPCHandler extends FastifyHandler {
45
+ constructor(router, options = {}) {
46
+ if (options.strictGetMethodPluginEnabled ?? true) {
47
+ options.plugins ??= [];
48
+ options.plugins.push(new StrictGetMethodPlugin());
49
+ }
50
+ super(new StandardRPCHandler(router, options), options);
51
+ }
52
+ }
53
+
54
+ export { FastifyHandler, RPCHandler };
@@ -1,5 +1,6 @@
1
1
  import { SupportedMessagePort } from '@orpc/client/message-port';
2
- import { MaybeOptionalOptions } from '@orpc/shared';
2
+ import { Value, Promisable, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { DecodedResponseMessage } from '@orpc/standard-server-peer';
3
4
  import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.mjs';
4
5
  import { f as StandardHandler } from '../../shared/server.DBCUJijK.mjs';
5
6
  import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.mjs';
@@ -7,17 +8,41 @@ import { S as StandardRPCHandlerOptions } from '../../shared/server.Ck-gOLzq.mjs
7
8
  import '@orpc/client';
8
9
  import '@orpc/contract';
9
10
  import '@orpc/standard-server';
10
- import '@orpc/standard-server-peer';
11
11
  import '../../shared/server.CVKCo60T.mjs';
12
12
  import '@orpc/client/standard';
13
13
  import '../../shared/server.DzV1hr3z.mjs';
14
14
 
15
+ interface MessagePortHandlerOptions<_T extends Context> {
16
+ /**
17
+ * By default, oRPC serializes request/response messages to string/binary data before sending over message port.
18
+ * If needed, you can define the this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
19
+ * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
20
+ *
21
+ * @remarks
22
+ * - return null | undefined to disable this feature
23
+ *
24
+ * @warning Make sure your message port supports `transfer` before using this feature.
25
+ * @example
26
+ * ```ts
27
+ * experimental_transfer: (message, port) => {
28
+ * const transfer = deepFindTransferableObjects(message) // implement your own logic
29
+ * return transfer.length ? transfer : null // only enable when needed
30
+ * }
31
+ * ```
32
+ *
33
+ * @see {@link https://orpc.unnoq.com/docs/adapters/message-port#transfer Message Port Transfer Docs}
34
+ */
35
+ experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedResponseMessage, port: SupportedMessagePort]>;
36
+ }
15
37
  declare class MessagePortHandler<T extends Context> {
16
38
  private readonly standardHandler;
17
- constructor(standardHandler: StandardHandler<T>);
39
+ private readonly transfer;
40
+ constructor(standardHandler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
18
41
  upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): void;
19
42
  }
20
43
 
44
+ interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T>, MessagePortHandlerOptions<T> {
45
+ }
21
46
  /**
22
47
  * RPC Handler for common message port implementations.
23
48
  *
@@ -25,7 +50,8 @@ declare class MessagePortHandler<T extends Context> {
25
50
  * @see {@link https://orpc.unnoq.com/docs/adapters/message-port Message Port Adapter Docs}
26
51
  */
27
52
  declare class RPCHandler<T extends Context> extends MessagePortHandler<T> {
28
- constructor(router: Router<any, T>, options?: NoInfer<StandardRPCHandlerOptions<T>>);
53
+ constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
29
54
  }
30
55
 
31
56
  export { MessagePortHandler, RPCHandler };
57
+ export type { MessagePortHandlerOptions, RPCHandlerOptions };
@@ -1,5 +1,6 @@
1
1
  import { SupportedMessagePort } from '@orpc/client/message-port';
2
- import { MaybeOptionalOptions } from '@orpc/shared';
2
+ import { Value, Promisable, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { DecodedResponseMessage } from '@orpc/standard-server-peer';
3
4
  import { C as Context, R as Router } from '../../shared/server.B4BGqy3Y.js';
4
5
  import { f as StandardHandler } from '../../shared/server.DPIFWpxG.js';
5
6
  import { HandleStandardServerPeerMessageOptions } from '../standard-peer/index.js';
@@ -7,17 +8,41 @@ import { S as StandardRPCHandlerOptions } from '../../shared/server.COL12UTb.js'
7
8
  import '@orpc/client';
8
9
  import '@orpc/contract';
9
10
  import '@orpc/standard-server';
10
- import '@orpc/standard-server-peer';
11
11
  import '../../shared/server.DNtJ-p60.js';
12
12
  import '@orpc/client/standard';
13
13
  import '../../shared/server.Cb6yD7DZ.js';
14
14
 
15
+ interface MessagePortHandlerOptions<_T extends Context> {
16
+ /**
17
+ * By default, oRPC serializes request/response messages to string/binary data before sending over message port.
18
+ * If needed, you can define the this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
19
+ * such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
20
+ *
21
+ * @remarks
22
+ * - return null | undefined to disable this feature
23
+ *
24
+ * @warning Make sure your message port supports `transfer` before using this feature.
25
+ * @example
26
+ * ```ts
27
+ * experimental_transfer: (message, port) => {
28
+ * const transfer = deepFindTransferableObjects(message) // implement your own logic
29
+ * return transfer.length ? transfer : null // only enable when needed
30
+ * }
31
+ * ```
32
+ *
33
+ * @see {@link https://orpc.unnoq.com/docs/adapters/message-port#transfer Message Port Transfer Docs}
34
+ */
35
+ experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedResponseMessage, port: SupportedMessagePort]>;
36
+ }
15
37
  declare class MessagePortHandler<T extends Context> {
16
38
  private readonly standardHandler;
17
- constructor(standardHandler: StandardHandler<T>);
39
+ private readonly transfer;
40
+ constructor(standardHandler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
18
41
  upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<HandleStandardServerPeerMessageOptions<T>>): void;
19
42
  }
20
43
 
44
+ interface RPCHandlerOptions<T extends Context> extends StandardRPCHandlerOptions<T>, MessagePortHandlerOptions<T> {
45
+ }
21
46
  /**
22
47
  * RPC Handler for common message port implementations.
23
48
  *
@@ -25,7 +50,8 @@ declare class MessagePortHandler<T extends Context> {
25
50
  * @see {@link https://orpc.unnoq.com/docs/adapters/message-port Message Port Adapter Docs}
26
51
  */
27
52
  declare class RPCHandler<T extends Context> extends MessagePortHandler<T> {
28
- constructor(router: Router<any, T>, options?: NoInfer<StandardRPCHandlerOptions<T>>);
53
+ constructor(router: Router<any, T>, options?: NoInfer<RPCHandlerOptions<T>>);
29
54
  }
30
55
 
31
56
  export { MessagePortHandler, RPCHandler };
57
+ export type { MessagePortHandlerOptions, RPCHandlerOptions };
@@ -1,6 +1,6 @@
1
1
  import { postMessagePortMessage, onMessagePortMessage, onMessagePortClose } from '@orpc/client/message-port';
2
- import { resolveMaybeOptionalOptions } from '@orpc/shared';
3
- import { ServerPeer } from '@orpc/standard-server-peer';
2
+ import { value, resolveMaybeOptionalOptions, isObject } from '@orpc/shared';
3
+ import { experimental_ServerPeerWithoutCodec, serializeResponseMessage, encodeResponseMessage, deserializeRequestMessage, decodeRequestMessage } from '@orpc/standard-server-peer';
4
4
  import { c as createServerPeerHandleRequestFn } from '../../shared/server.UVMTOWrk.mjs';
5
5
  import '@orpc/client';
6
6
  import '@orpc/standard-server';
@@ -11,18 +11,34 @@ import '../../shared/server.DZ5BIITo.mjs';
11
11
  import '../../shared/server.Ds4HPpvH.mjs';
12
12
 
13
13
  class MessagePortHandler {
14
- constructor(standardHandler) {
14
+ constructor(standardHandler, options = {}) {
15
15
  this.standardHandler = standardHandler;
16
+ this.transfer = options.experimental_transfer;
16
17
  }
18
+ transfer;
17
19
  upgrade(port, ...rest) {
18
- const peer = new ServerPeer((message) => {
19
- return postMessagePortMessage(port, message);
20
+ const peer = new experimental_ServerPeerWithoutCodec(async (message) => {
21
+ const [id, type, payload] = message;
22
+ const transfer = await value(this.transfer, message, port);
23
+ if (transfer) {
24
+ postMessagePortMessage(port, serializeResponseMessage(id, type, payload), transfer);
25
+ } else {
26
+ postMessagePortMessage(port, await encodeResponseMessage(id, type, payload));
27
+ }
20
28
  });
21
29
  onMessagePortMessage(port, async (message) => {
22
- await peer.message(
23
- message,
24
- createServerPeerHandleRequestFn(this.standardHandler, resolveMaybeOptionalOptions(rest))
25
- );
30
+ const handleFn = createServerPeerHandleRequestFn(this.standardHandler, resolveMaybeOptionalOptions(rest));
31
+ if (isObject(message)) {
32
+ await peer.message(
33
+ deserializeRequestMessage(message),
34
+ handleFn
35
+ );
36
+ } else {
37
+ await peer.message(
38
+ await decodeRequestMessage(message),
39
+ handleFn
40
+ );
41
+ }
26
42
  });
27
43
  onMessagePortClose(port, () => {
28
44
  peer.close();
@@ -32,7 +48,7 @@ class MessagePortHandler {
32
48
 
33
49
  class RPCHandler extends MessagePortHandler {
34
50
  constructor(router, options = {}) {
35
- super(new StandardRPCHandler(router, options));
51
+ super(new StandardRPCHandler(router, options), options);
36
52
  }
37
53
  }
38
54
 
@@ -32,7 +32,7 @@ interface NodeHttpHandlerOptions<T extends Context> extends SendStandardResponse
32
32
  adapterInterceptors?: Interceptor<NodeHttpHandlerInterceptorOptions<T>, Promise<NodeHttpHandleResult>>[];
33
33
  plugins?: NodeHttpHandlerPlugin<T>[];
34
34
  }
35
- declare class NodeHttpHandler<T extends Context> implements NodeHttpHandler<T> {
35
+ declare class NodeHttpHandler<T extends Context> {
36
36
  private readonly standardHandler;
37
37
  private readonly sendStandardResponseOptions;
38
38
  private readonly adapterInterceptors;
@@ -32,7 +32,7 @@ interface NodeHttpHandlerOptions<T extends Context> extends SendStandardResponse
32
32
  adapterInterceptors?: Interceptor<NodeHttpHandlerInterceptorOptions<T>, Promise<NodeHttpHandleResult>>[];
33
33
  plugins?: NodeHttpHandlerPlugin<T>[];
34
34
  }
35
- declare class NodeHttpHandler<T extends Context> implements NodeHttpHandler<T> {
35
+ declare class NodeHttpHandler<T extends Context> {
36
36
  private readonly standardHandler;
37
37
  private readonly sendStandardResponseOptions;
38
38
  private readonly adapterInterceptors;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "1.10.0",
4
+ "version": "1.10.2",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -54,6 +54,11 @@
54
54
  "import": "./dist/adapters/node/index.mjs",
55
55
  "default": "./dist/adapters/node/index.mjs"
56
56
  },
57
+ "./fastify": {
58
+ "types": "./dist/adapters/fastify/index.d.mts",
59
+ "import": "./dist/adapters/fastify/index.mjs",
60
+ "default": "./dist/adapters/fastify/index.mjs"
61
+ },
57
62
  "./aws-lambda": {
58
63
  "types": "./dist/adapters/aws-lambda/index.d.mts",
59
64
  "import": "./dist/adapters/aws-lambda/index.mjs",
@@ -102,20 +107,22 @@
102
107
  },
103
108
  "dependencies": {
104
109
  "cookie": "^1.0.2",
105
- "@orpc/client": "1.10.0",
106
- "@orpc/contract": "1.10.0",
107
- "@orpc/shared": "1.10.0",
108
- "@orpc/interop": "1.10.0",
109
- "@orpc/standard-server": "1.10.0",
110
- "@orpc/standard-server-aws-lambda": "1.10.0",
111
- "@orpc/standard-server-fetch": "1.10.0",
112
- "@orpc/standard-server-node": "1.10.0",
113
- "@orpc/standard-server-peer": "1.10.0"
110
+ "@orpc/client": "1.10.2",
111
+ "@orpc/contract": "1.10.2",
112
+ "@orpc/shared": "1.10.2",
113
+ "@orpc/standard-server": "1.10.2",
114
+ "@orpc/standard-server-fastify": "1.10.2",
115
+ "@orpc/standard-server-aws-lambda": "1.10.2",
116
+ "@orpc/interop": "1.10.2",
117
+ "@orpc/standard-server-fetch": "1.10.2",
118
+ "@orpc/standard-server-node": "1.10.2",
119
+ "@orpc/standard-server-peer": "1.10.2"
114
120
  },
115
121
  "devDependencies": {
116
- "@tanstack/router-core": "^1.133.3",
122
+ "@tanstack/router-core": "^1.133.20",
117
123
  "@types/ws": "^8.18.1",
118
124
  "crossws": "^0.4.1",
125
+ "fastify": "^5.6.1",
119
126
  "next": "^15.5.6",
120
127
  "supertest": "^7.1.4",
121
128
  "ws": "^8.18.3",