@orpc/server 0.0.0-next.0f0c680 → 0.0.0-next.10ace4f

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 +37 -11
  3. package/dist/adapters/fetch/index.d.ts +37 -11
  4. package/dist/adapters/fetch/index.mjs +6 -6
  5. package/dist/adapters/hono/index.d.mts +5 -4
  6. package/dist/adapters/hono/index.d.ts +5 -4
  7. package/dist/adapters/hono/index.mjs +6 -6
  8. package/dist/adapters/next/index.d.mts +5 -4
  9. package/dist/adapters/next/index.d.ts +5 -4
  10. package/dist/adapters/next/index.mjs +6 -6
  11. package/dist/adapters/node/index.d.mts +39 -22
  12. package/dist/adapters/node/index.d.ts +39 -22
  13. package/dist/adapters/node/index.mjs +74 -21
  14. package/dist/adapters/standard/index.d.mts +6 -6
  15. package/dist/adapters/standard/index.d.ts +6 -6
  16. package/dist/adapters/standard/index.mjs +3 -3
  17. package/dist/index.d.mts +57 -44
  18. package/dist/index.d.ts +57 -44
  19. package/dist/index.mjs +27 -10
  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.DKCNBSd-.d.mts → server.B77ImKAP.d.mts} +2 -2
  24. package/dist/shared/{server.Dj89-yLZ.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.DpIMjOfC.d.mts → server.CzxlqYZL.d.mts} +20 -29
  28. package/dist/shared/server.DGnN7q3R.d.mts +10 -0
  29. package/dist/shared/{server.DnmJuN02.d.mts → server.DLt5njUb.d.mts} +9 -10
  30. package/dist/shared/{server.DnmJuN02.d.ts → server.DLt5njUb.d.ts} +9 -10
  31. package/dist/shared/{server.BXfgvxK8.d.ts → server.DUF89eb-.d.ts} +2 -2
  32. package/dist/shared/{server.DisswUB5.mjs → server.Dfx1jV-K.mjs} +30 -27
  33. package/dist/shared/server.T5WmDoWQ.mjs +98 -0
  34. package/dist/shared/{server.B_5ZADvP.mjs → server.e3W6AG3-.mjs} +18 -11
  35. package/package.json +8 -8
  36. package/dist/shared/server.D1vPuPwc.mjs +0 -29
  37. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -1,51 +1,54 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
- import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
- import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
4
- import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.B_5ZADvP.mjs';
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.e3W6AG3-.mjs';
4
+ import { toHttpPath } from '@orpc/client/standard';
5
5
 
6
6
  class StandardHandler {
7
7
  constructor(router, matcher, codec, options) {
8
8
  this.matcher = matcher;
9
9
  this.codec = codec;
10
- this.options = options;
11
- this.plugin = new CompositePlugin(options.plugins);
12
- this.plugin.init(this.options);
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);
13
16
  this.matcher.init(router);
14
17
  }
15
- plugin;
16
- handle(request, ...[options]) {
18
+ interceptors;
19
+ clientInterceptors;
20
+ rootInterceptors;
21
+ handle(request, options) {
17
22
  return intercept(
18
- this.options.rootInterceptors ?? [],
19
- {
20
- request,
21
- ...options,
22
- context: options?.context ?? {}
23
- // context is optional only when all fields are optional so we can safely force it to have a context
24
- },
23
+ this.rootInterceptors,
24
+ { ...options, request },
25
25
  async (interceptorOptions) => {
26
26
  let isDecoding = false;
27
27
  try {
28
28
  return await intercept(
29
- this.options.interceptors ?? [],
29
+ this.interceptors,
30
30
  interceptorOptions,
31
- async (interceptorOptions2) => {
32
- const method = interceptorOptions2.request.method;
33
- const url = interceptorOptions2.request.url;
34
- const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
35
- const match = await this.matcher.match(method, pathname);
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, "")}`);
36
39
  if (!match) {
37
40
  return { matched: false, response: void 0 };
38
41
  }
39
42
  const client = createProcedureClient(match.procedure, {
40
- context: interceptorOptions2.context,
43
+ context,
41
44
  path: match.path,
42
- interceptors: this.options.clientInterceptors
45
+ interceptors: this.clientInterceptors
43
46
  });
44
47
  isDecoding = true;
45
- const input = await this.codec.decode(request, match.params, match.procedure);
48
+ const input = await this.codec.decode(request2, match.params, match.procedure);
46
49
  isDecoding = false;
47
- const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
48
- const output = await client(input, { signal: request.signal, lastEventId });
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 });
49
52
  const response = this.codec.encode(output, match.procedure);
50
53
  return {
51
54
  matched: true,
@@ -54,7 +57,7 @@ class StandardHandler {
54
57
  }
55
58
  );
56
59
  } catch (e) {
57
- const error = isDecoding ? new ORPCError("BAD_REQUEST", {
60
+ const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
58
61
  message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
62
  cause: e
60
63
  }) : toORPCError(e);
@@ -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 = {}) {
@@ -58,6 +58,10 @@ function isProcedure(item) {
58
58
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
59
59
  }
60
60
 
61
+ function mergeCurrentContext(context, other) {
62
+ return { ...context, ...other };
63
+ }
64
+
61
65
  function createORPCErrorConstructorMap(errors) {
62
66
  const proxy = new Proxy(errors, {
63
67
  get(target, code) {
@@ -123,7 +127,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
123
127
  );
124
128
  } catch (e) {
125
129
  if (!(e instanceof ORPCError)) {
126
- throw toError(e);
130
+ throw e;
127
131
  }
128
132
  const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
129
133
  throw validated;
@@ -170,23 +174,30 @@ async function executeProcedureInternal(procedure, options) {
170
174
  let currentInput = options.input;
171
175
  const next = async (...[nextOptions]) => {
172
176
  const index = currentIndex;
177
+ const midContext = nextOptions?.context ?? {};
173
178
  currentIndex += 1;
174
- currentContext = { ...currentContext, ...nextOptions?.context };
179
+ currentContext = mergeCurrentContext(currentContext, midContext);
175
180
  if (index === inputValidationIndex) {
176
181
  currentInput = await validateInput(procedure, currentInput);
177
182
  }
178
183
  const mid = middlewares[index];
179
- 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
+ };
180
191
  if (index === outputValidationIndex) {
181
192
  const validatedOutput = await validateOutput(procedure, result.output);
182
193
  return {
183
- ...result,
194
+ context: result.context,
184
195
  output: validatedOutput
185
196
  };
186
197
  }
187
198
  return result;
188
199
  };
189
- return (await next({})).output;
200
+ return (await next()).output;
190
201
  }
191
202
 
192
203
  const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
@@ -356,8 +367,4 @@ function call(procedure, input, ...rest) {
356
367
  return createProcedureClient(procedure, ...rest)(input);
357
368
  }
358
369
 
359
- function toHttpPath(path) {
360
- return `/${path.map(encodeURIComponent).join("/")}`;
361
- }
362
-
363
- 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, createORPCErrorConstructorMap as j, getLazyMeta as k, lazy as l, middlewareOutputFn 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 };
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.0f0c680",
4
+ "version": "0.0.0-next.10ace4f",
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.0f0c680",
62
- "@orpc/shared": "0.0.0-next.0f0c680",
63
- "@orpc/contract": "0.0.0-next.0f0c680",
64
- "@orpc/standard-server-fetch": "0.0.0-next.0f0c680",
65
- "@orpc/standard-server-node": "0.0.0-next.0f0c680",
66
- "@orpc/standard-server": "0.0.0-next.0f0c680"
61
+ "@orpc/client": "0.0.0-next.10ace4f",
62
+ "@orpc/contract": "0.0.0-next.10ace4f",
63
+ "@orpc/shared": "0.0.0-next.10ace4f",
64
+ "@orpc/standard-server-node": "0.0.0-next.10ace4f",
65
+ "@orpc/standard-server": "0.0.0-next.10ace4f",
66
+ "@orpc/standard-server-fetch": "0.0.0-next.10ace4f"
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,29 +0,0 @@
1
- import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
2
- import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
- import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.DisswUB5.mjs';
4
-
5
- class RPCHandler {
6
- standardHandler;
7
- constructor(router, options = {}) {
8
- const jsonSerializer = new StandardRPCJsonSerializer(options);
9
- const serializer = new StandardRPCSerializer(jsonSerializer);
10
- const matcher = new StandardRPCMatcher();
11
- const codec = new StandardRPCCodec(serializer);
12
- this.standardHandler = new StandardHandler(router, matcher, codec, options);
13
- }
14
- async handle(request, ...[
15
- options = {}
16
- ]) {
17
- const standardRequest = toStandardLazyRequest(request);
18
- const result = await this.standardHandler.handle(standardRequest, options);
19
- if (!result.matched) {
20
- return result;
21
- }
22
- return {
23
- matched: true,
24
- response: toFetchResponse(result.response, options)
25
- };
26
- }
27
- }
28
-
29
- 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 };