@orpc/server 0.0.0-next.c0afbea → 0.0.0-next.c0ca4c7

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 (37) hide show
  1. package/README.md +11 -0
  2. package/dist/adapters/fetch/index.d.mts +38 -10
  3. package/dist/adapters/fetch/index.d.ts +38 -10
  4. package/dist/adapters/fetch/index.mjs +6 -6
  5. package/dist/adapters/hono/index.d.mts +6 -3
  6. package/dist/adapters/hono/index.d.ts +6 -3
  7. package/dist/adapters/hono/index.mjs +6 -6
  8. package/dist/adapters/next/index.d.mts +6 -3
  9. package/dist/adapters/next/index.d.ts +6 -3
  10. package/dist/adapters/next/index.mjs +6 -6
  11. package/dist/adapters/node/index.d.mts +40 -21
  12. package/dist/adapters/node/index.d.ts +40 -21
  13. package/dist/adapters/node/index.mjs +77 -23
  14. package/dist/adapters/standard/index.d.mts +11 -10
  15. package/dist/adapters/standard/index.d.ts +11 -10
  16. package/dist/adapters/standard/index.mjs +3 -3
  17. package/dist/index.d.mts +83 -61
  18. package/dist/index.d.ts +83 -61
  19. package/dist/index.mjs +45 -25
  20. package/dist/plugins/index.d.mts +12 -13
  21. package/dist/plugins/index.d.ts +12 -13
  22. package/dist/plugins/index.mjs +9 -5
  23. package/dist/shared/server.B77ImKAP.d.mts +8 -0
  24. package/dist/shared/{server.DmW25ynm.d.ts → server.BHlRCrf_.d.ts} +20 -29
  25. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  26. package/dist/shared/server.Cud5qk0c.d.ts +10 -0
  27. package/dist/shared/{server.CM3tWr3C.d.mts → server.CzxlqYZL.d.mts} +20 -29
  28. package/dist/shared/server.DGnN7q3R.d.mts +10 -0
  29. package/dist/shared/{server.CPteJIPP.d.mts → server.DLt5njUb.d.mts} +19 -19
  30. package/dist/shared/{server.CPteJIPP.d.ts → server.DLt5njUb.d.ts} +19 -19
  31. package/dist/shared/server.DUF89eb-.d.ts +8 -0
  32. package/dist/shared/{server.CSZRzcSW.mjs → server.Dfx1jV-K.mjs} +33 -30
  33. package/dist/shared/server.T5WmDoWQ.mjs +98 -0
  34. package/dist/shared/{server.CMrS28Go.mjs → server.e3W6AG3-.mjs} +37 -13
  35. package/package.json +8 -8
  36. package/dist/shared/server.Cq3B6PoL.mjs +0 -28
  37. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -0,0 +1,98 @@
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.Dfx1jV-K.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,6 +1,6 @@
1
1
  import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
2
2
  import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
3
- import { value, intercept, toError } from '@orpc/shared';
3
+ import { value, intercept } from '@orpc/shared';
4
4
 
5
5
  const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
6
6
  function lazy(loader, meta = {}) {
@@ -21,7 +21,24 @@ function unlazy(lazied) {
21
21
  return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
22
22
  }
23
23
 
24
- function mergeMiddlewares(first, second) {
24
+ function isStartWithMiddlewares(middlewares, compare) {
25
+ if (compare.length > middlewares.length) {
26
+ return false;
27
+ }
28
+ for (let i = 0; i < middlewares.length; i++) {
29
+ if (compare[i] === void 0) {
30
+ return true;
31
+ }
32
+ if (middlewares[i] !== compare[i]) {
33
+ return false;
34
+ }
35
+ }
36
+ return true;
37
+ }
38
+ function mergeMiddlewares(first, second, options) {
39
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
40
+ return second;
41
+ }
25
42
  return [...first, ...second];
26
43
  }
27
44
  function addMiddleware(middlewares, addition) {
@@ -41,6 +58,10 @@ function isProcedure(item) {
41
58
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
42
59
  }
43
60
 
61
+ function mergeCurrentContext(context, other) {
62
+ return { ...context, ...other };
63
+ }
64
+
44
65
  function createORPCErrorConstructorMap(errors) {
45
66
  const proxy = new Proxy(errors, {
46
67
  get(target, code) {
@@ -106,7 +127,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
106
127
  );
107
128
  } catch (e) {
108
129
  if (!(e instanceof ORPCError)) {
109
- throw toError(e);
130
+ throw e;
110
131
  }
111
132
  const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
112
133
  throw validated;
@@ -153,23 +174,30 @@ async function executeProcedureInternal(procedure, options) {
153
174
  let currentInput = options.input;
154
175
  const next = async (...[nextOptions]) => {
155
176
  const index = currentIndex;
177
+ const midContext = nextOptions?.context ?? {};
156
178
  currentIndex += 1;
157
- currentContext = { ...currentContext, ...nextOptions?.context };
179
+ currentContext = mergeCurrentContext(currentContext, midContext);
158
180
  if (index === inputValidationIndex) {
159
181
  currentInput = await validateInput(procedure, currentInput);
160
182
  }
161
183
  const mid = middlewares[index];
162
- const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
184
+ const result = mid ? {
185
+ context: midContext,
186
+ output: (await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn)).output
187
+ } : {
188
+ context: midContext,
189
+ output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput })
190
+ };
163
191
  if (index === outputValidationIndex) {
164
192
  const validatedOutput = await validateOutput(procedure, result.output);
165
193
  return {
166
- ...result,
194
+ context: result.context,
167
195
  output: validatedOutput
168
196
  };
169
197
  }
170
198
  return result;
171
199
  };
172
- return (await next({})).output;
200
+ return (await next()).output;
173
201
  }
174
202
 
175
203
  const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
@@ -239,7 +267,7 @@ function enhanceRouter(router, options) {
239
267
  return accessible;
240
268
  }
241
269
  if (isProcedure(router)) {
242
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
270
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
243
271
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
244
272
  const enhanced2 = new Procedure({
245
273
  ...router["~orpc"],
@@ -339,8 +367,4 @@ function call(procedure, input, ...rest) {
339
367
  return createProcedureClient(procedure, ...rest)(input);
340
368
  }
341
369
 
342
- function toHttpPath(path) {
343
- return `/${path.map(encodeURIComponent).join("/")}`;
344
- }
345
-
346
- export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, unlazyRouter as p, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
370
+ export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "0.0.0-next.c0afbea",
4
+ "version": "0.0.0-next.c0ca4c7",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -58,15 +58,15 @@
58
58
  "next": ">=14.0.0"
59
59
  },
60
60
  "dependencies": {
61
- "@orpc/client": "0.0.0-next.c0afbea",
62
- "@orpc/contract": "0.0.0-next.c0afbea",
63
- "@orpc/shared": "0.0.0-next.c0afbea",
64
- "@orpc/standard-server-node": "0.0.0-next.c0afbea",
65
- "@orpc/standard-server-fetch": "0.0.0-next.c0afbea",
66
- "@orpc/standard-server": "0.0.0-next.c0afbea"
61
+ "@orpc/contract": "0.0.0-next.c0ca4c7",
62
+ "@orpc/shared": "0.0.0-next.c0ca4c7",
63
+ "@orpc/standard-server": "0.0.0-next.c0ca4c7",
64
+ "@orpc/standard-server-fetch": "0.0.0-next.c0ca4c7",
65
+ "@orpc/standard-server-node": "0.0.0-next.c0ca4c7",
66
+ "@orpc/client": "0.0.0-next.c0ca4c7"
67
67
  },
68
68
  "devDependencies": {
69
- "light-my-request": "^6.5.1"
69
+ "supertest": "^7.0.0"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "unbuild",
@@ -1,28 +0,0 @@
1
- import { RPCSerializer } from '@orpc/client/standard';
2
- import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
- import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.CSZRzcSW.mjs';
4
-
5
- class RPCHandler {
6
- standardHandler;
7
- constructor(router, options = {}) {
8
- const serializer = new RPCSerializer();
9
- const matcher = new RPCMatcher();
10
- const codec = new RPCCodec(serializer);
11
- this.standardHandler = new StandardHandler(router, matcher, codec, options);
12
- }
13
- async handle(request, ...[
14
- options = {}
15
- ]) {
16
- const standardRequest = toStandardLazyRequest(request);
17
- const result = await this.standardHandler.handle(standardRequest, options);
18
- if (!result.matched) {
19
- return result;
20
- }
21
- return {
22
- matched: true,
23
- response: toFetchResponse(result.response, options)
24
- };
25
- }
26
- }
27
-
28
- export { RPCHandler as R };
@@ -1,12 +0,0 @@
1
- class CompositePlugin {
2
- constructor(plugins = []) {
3
- this.plugins = plugins;
4
- }
5
- init(options) {
6
- for (const plugin of this.plugins) {
7
- plugin.init?.(options);
8
- }
9
- }
10
- }
11
-
12
- export { CompositePlugin as C };