@orpc/server 0.0.0-next.aac73c2 → 0.0.0-next.ab2f831

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 (74) hide show
  1. package/README.md +23 -23
  2. package/dist/adapters/aws-lambda/index.d.mts +46 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +46 -0
  4. package/dist/adapters/aws-lambda/index.mjs +42 -0
  5. package/dist/adapters/bun-ws/index.d.mts +36 -0
  6. package/dist/adapters/bun-ws/index.d.ts +36 -0
  7. package/dist/adapters/bun-ws/index.mjs +47 -0
  8. package/dist/adapters/crossws/index.d.mts +33 -0
  9. package/dist/adapters/crossws/index.d.ts +33 -0
  10. package/dist/adapters/crossws/index.mjs +45 -0
  11. package/dist/adapters/fastify/index.d.mts +53 -0
  12. package/dist/adapters/fastify/index.d.ts +53 -0
  13. package/dist/adapters/fastify/index.mjs +54 -0
  14. package/dist/adapters/fetch/index.d.mts +80 -10
  15. package/dist/adapters/fetch/index.d.ts +80 -10
  16. package/dist/adapters/fetch/index.mjs +179 -8
  17. package/dist/adapters/message-port/index.d.mts +57 -0
  18. package/dist/adapters/message-port/index.d.ts +57 -0
  19. package/dist/adapters/message-port/index.mjs +55 -0
  20. package/dist/adapters/node/index.d.mts +58 -12
  21. package/dist/adapters/node/index.d.ts +58 -12
  22. package/dist/adapters/node/index.mjs +89 -22
  23. package/dist/adapters/standard/index.d.mts +8 -13
  24. package/dist/adapters/standard/index.d.ts +8 -13
  25. package/dist/adapters/standard/index.mjs +5 -3
  26. package/dist/adapters/standard-peer/index.d.mts +18 -0
  27. package/dist/adapters/standard-peer/index.d.ts +18 -0
  28. package/dist/adapters/standard-peer/index.mjs +7 -0
  29. package/dist/adapters/websocket/index.d.mts +56 -0
  30. package/dist/adapters/websocket/index.d.ts +56 -0
  31. package/dist/adapters/websocket/index.mjs +67 -0
  32. package/dist/adapters/ws/index.d.mts +31 -0
  33. package/dist/adapters/ws/index.d.ts +31 -0
  34. package/dist/adapters/ws/index.mjs +37 -0
  35. package/dist/helpers/index.d.mts +149 -0
  36. package/dist/helpers/index.d.ts +149 -0
  37. package/dist/helpers/index.mjs +198 -0
  38. package/dist/hibernation/index.d.mts +44 -0
  39. package/dist/hibernation/index.d.ts +44 -0
  40. package/dist/hibernation/index.mjs +65 -0
  41. package/dist/index.d.mts +584 -72
  42. package/dist/index.d.ts +584 -72
  43. package/dist/index.mjs +143 -14
  44. package/dist/plugins/index.d.mts +151 -9
  45. package/dist/plugins/index.d.ts +151 -9
  46. package/dist/plugins/index.mjs +191 -11
  47. package/dist/shared/{server.CYBq7eu_.d.mts → server.B4BGqy3Y.d.mts} +61 -11
  48. package/dist/shared/{server.CYBq7eu_.d.ts → server.B4BGqy3Y.d.ts} +61 -11
  49. package/dist/shared/server.Bxx6tqNe.mjs +219 -0
  50. package/dist/shared/server.COL12UTb.d.ts +32 -0
  51. package/dist/shared/server.CVKCo60T.d.mts +12 -0
  52. package/dist/shared/server.Cb6yD7DZ.d.ts +42 -0
  53. package/dist/shared/server.Ck-gOLzq.d.mts +32 -0
  54. package/dist/shared/{server.DNoBYd3E.d.ts → server.DBCUJijK.d.mts} +17 -9
  55. package/dist/shared/server.DNtJ-p60.d.ts +12 -0
  56. package/dist/shared/{server.DHBkTokD.d.mts → server.DPIFWpxG.d.ts} +17 -9
  57. package/dist/shared/{server.BVwwTHyO.mjs → server.DZ5BIITo.mjs} +1 -1
  58. package/dist/shared/{server.CjWkNG6l.mjs → server.Ds4HPpvH.mjs} +125 -86
  59. package/dist/shared/server.DzV1hr3z.d.mts +42 -0
  60. package/dist/shared/server.TEVCLCFC.mjs +39 -0
  61. package/dist/shared/server.UVMTOWrk.mjs +26 -0
  62. package/package.json +80 -20
  63. package/dist/adapters/hono/index.d.mts +0 -22
  64. package/dist/adapters/hono/index.d.ts +0 -22
  65. package/dist/adapters/hono/index.mjs +0 -32
  66. package/dist/adapters/next/index.d.mts +0 -29
  67. package/dist/adapters/next/index.d.ts +0 -29
  68. package/dist/adapters/next/index.mjs +0 -29
  69. package/dist/shared/server.B0kTII1X.d.ts +0 -10
  70. package/dist/shared/server.CBh2jYIX.d.mts +0 -10
  71. package/dist/shared/server.D9QduY95.mjs +0 -161
  72. package/dist/shared/server.DatI0acO.d.mts +0 -8
  73. package/dist/shared/server.Et1O6Bm7.mjs +0 -98
  74. package/dist/shared/server.uEHRR3Pg.d.ts +0 -8
@@ -1,29 +0,0 @@
1
- import { FetchHandler } from '../fetch/index.js';
2
- export { BodyLimitPlugin, BodyLimitPluginOptions, FetchHandleResult, FetchHandlerInterceptorOptions, FetchHandlerOptions, FetchHandlerPlugin, RPCHandler } from '../fetch/index.js';
3
- import { Value, MaybeOptionalOptions } from '@orpc/shared';
4
- import { NextRequest } from 'next/server';
5
- import { C as Context } from '../../shared/server.CYBq7eu_.js';
6
- import { S as StandardHandleOptions } from '../../shared/server.DNoBYd3E.js';
7
- import '../../shared/server.B0kTII1X.js';
8
- import '@orpc/standard-server-fetch';
9
- import '../../shared/server.uEHRR3Pg.js';
10
- import '@orpc/client/standard';
11
- import '@orpc/client';
12
- import '@orpc/contract';
13
- import '@orpc/standard-server';
14
-
15
- type ServeOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
16
- context?: Value<T, [NextRequest]>;
17
- } : {
18
- context: Value<T, [NextRequest]>;
19
- });
20
- interface ServeResult {
21
- GET(req: NextRequest): Promise<Response>;
22
- POST(req: NextRequest): Promise<Response>;
23
- PUT(req: NextRequest): Promise<Response>;
24
- PATCH(req: NextRequest): Promise<Response>;
25
- DELETE(req: NextRequest): Promise<Response>;
26
- }
27
- declare function serve<T extends Context>(handler: FetchHandler<T>, ...[options]: MaybeOptionalOptions<ServeOptions<T>>): ServeResult;
28
-
29
- export { FetchHandler, type ServeOptions, type ServeResult, serve };
@@ -1,29 +0,0 @@
1
- export { B as BodyLimitPlugin, F as FetchHandler, R as RPCHandler } from '../../shared/server.Et1O6Bm7.mjs';
2
- import { value } from '@orpc/shared';
3
- import '@orpc/client';
4
- import '@orpc/client/standard';
5
- import '../../shared/server.D9QduY95.mjs';
6
- import '../../shared/server.CjWkNG6l.mjs';
7
- import '@orpc/contract';
8
- import '@orpc/standard-server-fetch';
9
- import '../../shared/server.BVwwTHyO.mjs';
10
-
11
- function serve(handler, ...[options]) {
12
- const main = async (req) => {
13
- const context = await value(options?.context ?? {}, req);
14
- const { matched, response } = await handler.handle(req, { ...options, context });
15
- if (matched) {
16
- return response;
17
- }
18
- return new Response(`Cannot find a matching procedure for ${req.url}`, { status: 404 });
19
- };
20
- return {
21
- GET: main,
22
- POST: main,
23
- PUT: main,
24
- PATCH: main,
25
- DELETE: main
26
- };
27
- }
28
-
29
- export { serve };
@@ -1,10 +0,0 @@
1
- import { C as Context } from './server.CYBq7eu_.js';
2
- import { S as StandardHandleOptions } from './server.DNoBYd3E.js';
3
-
4
- type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
- context?: T;
6
- } : {
7
- context: T;
8
- });
9
-
10
- export type { FriendlyStandardHandleOptions as F };
@@ -1,10 +0,0 @@
1
- import { C as Context } from './server.CYBq7eu_.mjs';
2
- import { S as StandardHandleOptions } from './server.DHBkTokD.mjs';
3
-
4
- type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
- context?: T;
6
- } : {
7
- context: T;
8
- });
9
-
10
- export type { FriendlyStandardHandleOptions as F };
@@ -1,161 +0,0 @@
1
- import { ORPCError, toORPCError } from '@orpc/client';
2
- import { toArray, intercept, parseEmptyableJSON } from '@orpc/shared';
3
- import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.CjWkNG6l.mjs';
4
- import { toHttpPath } from '@orpc/client/standard';
5
-
6
- class StandardHandler {
7
- constructor(router, matcher, codec, options) {
8
- this.matcher = matcher;
9
- this.codec = codec;
10
- for (const plugin of toArray(options.plugins)) {
11
- plugin.init?.(options);
12
- }
13
- this.interceptors = toArray(options.interceptors);
14
- this.clientInterceptors = toArray(options.clientInterceptors);
15
- this.rootInterceptors = toArray(options.rootInterceptors);
16
- this.matcher.init(router);
17
- }
18
- interceptors;
19
- clientInterceptors;
20
- rootInterceptors;
21
- handle(request, options) {
22
- return intercept(
23
- this.rootInterceptors,
24
- { ...options, request },
25
- async (interceptorOptions) => {
26
- let isDecoding = false;
27
- try {
28
- return await intercept(
29
- this.interceptors,
30
- interceptorOptions,
31
- async ({ request: request2, context, prefix }) => {
32
- const method = request2.method;
33
- const url = request2.url;
34
- if (prefix && !url.pathname.startsWith(prefix)) {
35
- return { matched: false, response: void 0 };
36
- }
37
- const pathname = prefix ? url.pathname.replace(prefix, "") : url.pathname;
38
- const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`);
39
- if (!match) {
40
- return { matched: false, response: void 0 };
41
- }
42
- const client = createProcedureClient(match.procedure, {
43
- context,
44
- path: match.path,
45
- interceptors: this.clientInterceptors
46
- });
47
- isDecoding = true;
48
- const input = await this.codec.decode(request2, match.params, match.procedure);
49
- isDecoding = false;
50
- const lastEventId = Array.isArray(request2.headers["last-event-id"]) ? request2.headers["last-event-id"].at(-1) : request2.headers["last-event-id"];
51
- const output = await client(input, { signal: request2.signal, lastEventId });
52
- const response = this.codec.encode(output, match.procedure);
53
- return {
54
- matched: true,
55
- response
56
- };
57
- }
58
- );
59
- } catch (e) {
60
- const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
61
- message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
62
- cause: e
63
- }) : toORPCError(e);
64
- const response = this.codec.encodeError(error);
65
- return {
66
- matched: true,
67
- response
68
- };
69
- }
70
- }
71
- );
72
- }
73
- }
74
-
75
- class StandardRPCCodec {
76
- constructor(serializer) {
77
- this.serializer = serializer;
78
- }
79
- async decode(request, _params, _procedure) {
80
- const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
81
- return this.serializer.deserialize(serialized);
82
- }
83
- encode(output, _procedure) {
84
- return {
85
- status: 200,
86
- headers: {},
87
- body: this.serializer.serialize(output)
88
- };
89
- }
90
- encodeError(error) {
91
- return {
92
- status: error.status,
93
- headers: {},
94
- body: this.serializer.serialize(error.toJSON())
95
- };
96
- }
97
- }
98
-
99
- class StandardRPCMatcher {
100
- tree = {};
101
- pendingRouters = [];
102
- init(router, path = []) {
103
- const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
104
- const httpPath = toHttpPath(path2);
105
- if (isProcedure(contract)) {
106
- this.tree[httpPath] = {
107
- path: path2,
108
- contract,
109
- procedure: contract,
110
- // this mean dev not used contract-first so we can used contract as procedure directly
111
- router
112
- };
113
- } else {
114
- this.tree[httpPath] = {
115
- path: path2,
116
- contract,
117
- procedure: void 0,
118
- router
119
- };
120
- }
121
- });
122
- this.pendingRouters.push(...laziedOptions.map((option) => ({
123
- ...option,
124
- httpPathPrefix: toHttpPath(option.path)
125
- })));
126
- }
127
- async match(_method, pathname) {
128
- if (this.pendingRouters.length) {
129
- const newPendingRouters = [];
130
- for (const pendingRouter of this.pendingRouters) {
131
- if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
132
- const { default: router } = await unlazy(pendingRouter.router);
133
- this.init(router, pendingRouter.path);
134
- } else {
135
- newPendingRouters.push(pendingRouter);
136
- }
137
- }
138
- this.pendingRouters = newPendingRouters;
139
- }
140
- const match = this.tree[pathname];
141
- if (!match) {
142
- return void 0;
143
- }
144
- if (!match.procedure) {
145
- const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
146
- if (!isProcedure(maybeProcedure)) {
147
- throw new Error(`
148
- [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
149
- Ensure that the procedure is correctly defined and matches the expected contract.
150
- `);
151
- }
152
- match.procedure = createContractedProcedure(maybeProcedure, match.contract);
153
- }
154
- return {
155
- path: match.path,
156
- procedure: match.procedure
157
- };
158
- }
159
- }
160
-
161
- export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -1,8 +0,0 @@
1
- import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
- import { C as Context } from './server.CYBq7eu_.mjs';
3
- import { a as StandardHandlerOptions } from './server.DHBkTokD.mjs';
4
-
5
- interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
- }
7
-
8
- export type { StandardRPCHandlerOptions as S };
@@ -1,98 +0,0 @@
1
- import { ORPCError } from '@orpc/client';
2
- import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
3
- import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.D9QduY95.mjs';
4
- import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
5
- import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
6
- import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
7
-
8
- class BodyLimitPlugin {
9
- maxBodySize;
10
- constructor(options) {
11
- this.maxBodySize = options.maxBodySize;
12
- }
13
- initRuntimeAdapter(options) {
14
- options.adapterInterceptors ??= [];
15
- options.adapterInterceptors.push(async (options2) => {
16
- if (!options2.request.body) {
17
- return options2.next();
18
- }
19
- let currentBodySize = 0;
20
- const rawReader = options2.request.body.getReader();
21
- const reader = new ReadableStream({
22
- start: async (controller) => {
23
- try {
24
- if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
25
- controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
26
- return;
27
- }
28
- while (true) {
29
- const { done, value } = await rawReader.read();
30
- if (done) {
31
- break;
32
- }
33
- currentBodySize += value.length;
34
- if (currentBodySize > this.maxBodySize) {
35
- controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
36
- break;
37
- }
38
- controller.enqueue(value);
39
- }
40
- } finally {
41
- controller.close();
42
- }
43
- }
44
- });
45
- const requestInit = { body: reader, duplex: "half" };
46
- return options2.next({
47
- ...options2,
48
- request: new Request(options2.request, requestInit)
49
- });
50
- });
51
- }
52
- }
53
-
54
- class FetchHandler {
55
- constructor(standardHandler, options = {}) {
56
- this.standardHandler = standardHandler;
57
- for (const plugin of toArray(options.plugins)) {
58
- plugin.initRuntimeAdapter?.(options);
59
- }
60
- this.adapterInterceptors = toArray(options.adapterInterceptors);
61
- this.toFetchResponseOptions = options;
62
- }
63
- toFetchResponseOptions;
64
- adapterInterceptors;
65
- async handle(request, ...rest) {
66
- return intercept(
67
- this.adapterInterceptors,
68
- {
69
- ...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
70
- request,
71
- toFetchResponseOptions: this.toFetchResponseOptions
72
- },
73
- async ({ request: request2, toFetchResponseOptions, ...options }) => {
74
- const standardRequest = toStandardLazyRequest(request2);
75
- const result = await this.standardHandler.handle(standardRequest, options);
76
- if (!result.matched) {
77
- return result;
78
- }
79
- return {
80
- matched: true,
81
- response: toFetchResponse(result.response, toFetchResponseOptions)
82
- };
83
- }
84
- );
85
- }
86
- }
87
-
88
- class RPCHandler extends FetchHandler {
89
- constructor(router, options = {}) {
90
- const jsonSerializer = new StandardRPCJsonSerializer(options);
91
- const serializer = new StandardRPCSerializer(jsonSerializer);
92
- const matcher = new StandardRPCMatcher();
93
- const codec = new StandardRPCCodec(serializer);
94
- super(new StandardHandler(router, matcher, codec, options), options);
95
- }
96
- }
97
-
98
- export { BodyLimitPlugin as B, FetchHandler as F, RPCHandler as R };
@@ -1,8 +0,0 @@
1
- import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
- import { C as Context } from './server.CYBq7eu_.js';
3
- import { a as StandardHandlerOptions } from './server.DNoBYd3E.js';
4
-
5
- interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
- }
7
-
8
- export type { StandardRPCHandlerOptions as S };