@temporary-name/server 1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e → 1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43

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 (33) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +12 -6
  2. package/dist/adapters/aws-lambda/index.d.ts +12 -6
  3. package/dist/adapters/aws-lambda/index.mjs +14 -4
  4. package/dist/adapters/fetch/index.d.mts +12 -6
  5. package/dist/adapters/fetch/index.d.ts +12 -6
  6. package/dist/adapters/fetch/index.mjs +11 -8
  7. package/dist/adapters/node/index.d.mts +12 -6
  8. package/dist/adapters/node/index.d.ts +12 -6
  9. package/dist/adapters/node/index.mjs +11 -8
  10. package/dist/adapters/standard/index.d.mts +85 -13
  11. package/dist/adapters/standard/index.d.ts +85 -13
  12. package/dist/adapters/standard/index.mjs +7 -97
  13. package/dist/index.d.mts +6 -108
  14. package/dist/index.d.ts +6 -108
  15. package/dist/index.mjs +2 -3
  16. package/dist/openapi/index.d.mts +221 -0
  17. package/dist/openapi/index.d.ts +221 -0
  18. package/dist/openapi/index.mjs +784 -0
  19. package/dist/plugins/index.d.mts +2 -2
  20. package/dist/plugins/index.d.ts +2 -2
  21. package/dist/shared/{server.C1YnHvvf.d.ts → server.BKSOrA6h.d.mts} +2 -2
  22. package/dist/shared/{server.C1YnHvvf.d.mts → server.BKSOrA6h.d.ts} +2 -2
  23. package/dist/shared/{server.BEQrAa3A.mjs → server.BKh8I1Ny.mjs} +34 -2
  24. package/dist/shared/server.BeuTpcmO.d.mts +23 -0
  25. package/dist/shared/{server.Btxrgkj5.d.ts → server.C1fnTLq0.d.mts} +8 -24
  26. package/dist/shared/{server.Bo94xDTv.d.mts → server.CQyYNJ1H.d.ts} +8 -24
  27. package/dist/shared/server.DhdDYN-Z.mjs +261 -0
  28. package/dist/shared/server.SLLuK6_v.d.ts +23 -0
  29. package/dist/shared/server._YqJjI50.mjs +297 -0
  30. package/package.json +15 -9
  31. package/dist/shared/server.D6K9uoPI.mjs +0 -35
  32. package/dist/shared/server.DZ5BIITo.mjs +0 -9
  33. package/dist/shared/server.X0YaZxSJ.mjs +0 -13
@@ -0,0 +1,297 @@
1
+ import { isObject, stringifyJSON, isORPCErrorStatus, tryDecodeURIComponent, value, toHttpPath, toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
2
+ import { flattenHeader } from '@temporary-name/standard-server';
3
+ import { c as createProcedureClient } from './server.BKh8I1Ny.mjs';
4
+ import { s as standardizeHTTPPath, S as StandardBracketNotationSerializer, a as StandardOpenAPISerializer, b as StandardOpenAPIJsonSerializer } from './server.DhdDYN-Z.mjs';
5
+ import { fallbackContractConfig } from '@temporary-name/contract';
6
+ import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@temporary-name/server';
7
+ import { createRouter, addRoute, findRoute } from 'rou3';
8
+
9
+ class StandardOpenAPICodec {
10
+ constructor(serializer) {
11
+ this.serializer = serializer;
12
+ }
13
+ async decode(request, params, procedure) {
14
+ const inputStructure = fallbackContractConfig(
15
+ "defaultInputStructure",
16
+ procedure["~orpc"].route.inputStructure
17
+ );
18
+ if (inputStructure === "compact") {
19
+ const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
20
+ if (data === void 0) {
21
+ return params;
22
+ }
23
+ if (isObject(data)) {
24
+ return {
25
+ ...params,
26
+ ...data
27
+ };
28
+ }
29
+ return data;
30
+ }
31
+ const deserializeSearchParams = () => {
32
+ return this.serializer.deserialize(request.url.searchParams);
33
+ };
34
+ return {
35
+ params,
36
+ get query() {
37
+ const value = deserializeSearchParams();
38
+ Object.defineProperty(this, "query", { value, writable: true });
39
+ return value;
40
+ },
41
+ set query(value) {
42
+ Object.defineProperty(this, "query", { value, writable: true });
43
+ },
44
+ headers: request.headers,
45
+ body: this.serializer.deserialize(await request.body())
46
+ };
47
+ }
48
+ encode(output, procedure) {
49
+ const successStatus = fallbackContractConfig(
50
+ "defaultSuccessStatus",
51
+ procedure["~orpc"].route.successStatus
52
+ );
53
+ const outputStructure = fallbackContractConfig(
54
+ "defaultOutputStructure",
55
+ procedure["~orpc"].route.outputStructure
56
+ );
57
+ if (outputStructure === "compact") {
58
+ return {
59
+ status: successStatus,
60
+ headers: {},
61
+ body: this.serializer.serialize(output)
62
+ };
63
+ }
64
+ if (!this.#isDetailedOutput(output)) {
65
+ throw new Error(`
66
+ Invalid "detailed" output structure:
67
+ \u2022 Expected an object with optional properties:
68
+ - status (number 200-399)
69
+ - headers (Record<string, string | string[]>)
70
+ - body (any)
71
+ \u2022 No extra keys allowed.
72
+
73
+ Actual value:
74
+ ${stringifyJSON(output)}
75
+ `);
76
+ }
77
+ return {
78
+ status: output.status ?? successStatus,
79
+ headers: output.headers ?? {},
80
+ body: this.serializer.serialize(output.body)
81
+ };
82
+ }
83
+ encodeError(error) {
84
+ return {
85
+ status: error.status,
86
+ headers: {},
87
+ body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
88
+ };
89
+ }
90
+ #isDetailedOutput(output) {
91
+ if (!isObject(output)) {
92
+ return false;
93
+ }
94
+ if (output.headers && !isObject(output.headers)) {
95
+ return false;
96
+ }
97
+ if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
98
+ return false;
99
+ }
100
+ return true;
101
+ }
102
+ }
103
+
104
+ function resolveFriendlyStandardHandleOptions(options) {
105
+ return {
106
+ ...options,
107
+ context: options.context ?? {}
108
+ // Context only optional if all fields are optional
109
+ };
110
+ }
111
+ function toRou3Pattern(path) {
112
+ return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
113
+ }
114
+ function decodeParams(params) {
115
+ return Object.fromEntries(
116
+ Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)])
117
+ );
118
+ }
119
+
120
+ class StandardOpenAPIMatcher {
121
+ tree = createRouter();
122
+ pendingRouters = [];
123
+ init(router, path = []) {
124
+ const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
125
+ if (!value(true, traverseOptions)) {
126
+ return;
127
+ }
128
+ const { path: path2, contract } = traverseOptions;
129
+ const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
130
+ const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
131
+ if (isProcedure(contract)) {
132
+ addRoute(this.tree, method, httpPath, {
133
+ path: path2,
134
+ contract,
135
+ procedure: contract,
136
+ // this mean dev not used contract-first so we can used contract as procedure directly
137
+ router
138
+ });
139
+ } else {
140
+ addRoute(this.tree, method, httpPath, {
141
+ path: path2,
142
+ contract,
143
+ procedure: void 0,
144
+ router
145
+ });
146
+ }
147
+ });
148
+ this.pendingRouters.push(
149
+ ...laziedOptions.map((option) => ({
150
+ ...option,
151
+ httpPathPrefix: toHttpPath(option.path),
152
+ laziedPrefix: getLazyMeta(option.router).prefix
153
+ }))
154
+ );
155
+ }
156
+ async match(method, pathname) {
157
+ if (this.pendingRouters.length) {
158
+ const newPendingRouters = [];
159
+ for (const pendingRouter of this.pendingRouters) {
160
+ if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
161
+ const { default: router } = await unlazy(pendingRouter.router);
162
+ this.init(router, pendingRouter.path);
163
+ } else {
164
+ newPendingRouters.push(pendingRouter);
165
+ }
166
+ }
167
+ this.pendingRouters = newPendingRouters;
168
+ }
169
+ const match = findRoute(this.tree, method, pathname);
170
+ if (!match) {
171
+ return void 0;
172
+ }
173
+ if (!match.data.procedure) {
174
+ const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
175
+ if (!isProcedure(maybeProcedure)) {
176
+ throw new Error(`
177
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
178
+ Ensure that the procedure is correctly defined and matches the expected contract.
179
+ `);
180
+ }
181
+ match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
182
+ }
183
+ return {
184
+ path: match.data.path,
185
+ procedure: match.data.procedure,
186
+ params: match.params ? decodeParams(match.params) : void 0
187
+ };
188
+ }
189
+ }
190
+
191
+ class CompositeStandardHandlerPlugin {
192
+ plugins;
193
+ constructor(plugins = []) {
194
+ this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
195
+ }
196
+ init(options, router) {
197
+ for (const plugin of this.plugins) {
198
+ plugin.init?.(options, router);
199
+ }
200
+ }
201
+ }
202
+
203
+ class StandardHandler {
204
+ interceptors;
205
+ clientInterceptors;
206
+ rootInterceptors;
207
+ matcher;
208
+ codec;
209
+ constructor(router, options) {
210
+ const jsonSerializer = new StandardOpenAPIJsonSerializer();
211
+ const bracketNotationSerializer = new StandardBracketNotationSerializer();
212
+ const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
213
+ this.matcher = new StandardOpenAPIMatcher();
214
+ this.codec = new StandardOpenAPICodec(serializer);
215
+ const plugins = new CompositeStandardHandlerPlugin(options.plugins);
216
+ plugins.init(options, router);
217
+ this.interceptors = toArray(options.interceptors);
218
+ this.clientInterceptors = toArray(options.clientInterceptors);
219
+ this.rootInterceptors = toArray(options.rootInterceptors);
220
+ this.matcher.init(router);
221
+ }
222
+ async handle(request, options) {
223
+ const prefix = options.prefix?.replace(/\/$/, "") || void 0;
224
+ if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
225
+ return { matched: false, response: void 0 };
226
+ }
227
+ return intercept(this.rootInterceptors, { ...options, request, prefix }, async (interceptorOptions) => {
228
+ return runWithSpan({ name: `${request.method} ${request.url.pathname}` }, async (span) => {
229
+ let step;
230
+ try {
231
+ return await intercept(
232
+ this.interceptors,
233
+ interceptorOptions,
234
+ async ({ request: request2, context, prefix: prefix2 }) => {
235
+ const method = request2.method;
236
+ const url = request2.url;
237
+ const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
238
+ const match = await runWithSpan(
239
+ { name: "find_procedure" },
240
+ () => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
241
+ );
242
+ if (!match) {
243
+ return { matched: false, response: void 0 };
244
+ }
245
+ span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
246
+ span?.setAttribute("rpc.system", ORPC_NAME);
247
+ span?.setAttribute("rpc.method", match.path.join("."));
248
+ step = "decode_input";
249
+ let input = await runWithSpan(
250
+ { name: "decode_input" },
251
+ () => this.codec.decode(request2, match.params, match.procedure)
252
+ );
253
+ step = void 0;
254
+ if (isAsyncIteratorObject(input)) {
255
+ input = asyncIteratorWithSpan(
256
+ { name: "consume_event_iterator_input", signal: request2.signal },
257
+ input
258
+ );
259
+ }
260
+ const client = createProcedureClient(match.procedure, {
261
+ context,
262
+ path: match.path,
263
+ interceptors: this.clientInterceptors
264
+ });
265
+ step = "call_procedure";
266
+ const output = await client(input, {
267
+ signal: request2.signal,
268
+ lastEventId: flattenHeader(request2.headers["last-event-id"])
269
+ });
270
+ step = void 0;
271
+ const response = this.codec.encode(output, match.procedure);
272
+ return {
273
+ matched: true,
274
+ response
275
+ };
276
+ }
277
+ );
278
+ } catch (e) {
279
+ if (step !== "call_procedure") {
280
+ setSpanError(span, e);
281
+ }
282
+ const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
283
+ message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
284
+ cause: e
285
+ }) : toORPCError(e);
286
+ const response = this.codec.encodeError(error);
287
+ return {
288
+ matched: true,
289
+ response
290
+ };
291
+ }
292
+ });
293
+ });
294
+ }
295
+ }
296
+
297
+ export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardOpenAPICodec as a, StandardOpenAPIMatcher as b, decodeParams as d, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@temporary-name/server",
3
3
  "type": "module",
4
- "version": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
4
+ "version": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
5
5
  "license": "MIT",
6
6
  "homepage": "https://www.stainless.com/",
7
7
  "repository": {
@@ -47,6 +47,11 @@
47
47
  "types": "./dist/adapters/aws-lambda/index.d.mts",
48
48
  "import": "./dist/adapters/aws-lambda/index.mjs",
49
49
  "default": "./dist/adapters/aws-lambda/index.mjs"
50
+ },
51
+ "./openapi": {
52
+ "types": "./dist/openapi/index.d.mts",
53
+ "import": "./dist/openapi/index.mjs",
54
+ "default": "./dist/openapi/index.mjs"
50
55
  }
51
56
  },
52
57
  "files": [
@@ -67,17 +72,18 @@
67
72
  "dependencies": {
68
73
  "cookie": "^1.0.2",
69
74
  "@standard-schema/spec": "^1.0.0",
75
+ "rou3": "^0.7.7",
70
76
  "zod": "^4.1.12",
71
- "@temporary-name/interop": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
72
- "@temporary-name/contract": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
73
- "@temporary-name/openapi": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
74
- "@temporary-name/shared": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
75
- "@temporary-name/standard-server-fetch": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
76
- "@temporary-name/standard-server-node": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
77
- "@temporary-name/standard-server": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e",
78
- "@temporary-name/standard-server-aws-lambda": "1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e"
77
+ "@temporary-name/contract": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
78
+ "@temporary-name/interop": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
79
+ "@temporary-name/standard-server": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
80
+ "@temporary-name/standard-server-aws-lambda": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
81
+ "@temporary-name/standard-server-fetch": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
82
+ "@temporary-name/standard-server-node": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43",
83
+ "@temporary-name/shared": "1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43"
79
84
  },
80
85
  "devDependencies": {
86
+ "@types/supertest": "^6.0.3",
81
87
  "@types/ws": "^8.18.1",
82
88
  "supertest": "^7.1.4"
83
89
  },
@@ -1,35 +0,0 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
- import 'zod';
3
- import * as z4 from 'zod/v4/core';
4
-
5
- const gatingContext = new AsyncLocalStorage();
6
- function withoutGatedFields(data, schema, isGateEnabled) {
7
- const filtered = { ...data };
8
- const gatedFields = getGatedFields(schema);
9
- for (const [fieldName, gate] of gatedFields) {
10
- if (!isGateEnabled(gate)) {
11
- delete filtered[fieldName];
12
- }
13
- }
14
- return filtered;
15
- }
16
- function getGatedFields(schema) {
17
- if (!schema || schema["~standard"].vendor !== "zod") {
18
- return [];
19
- }
20
- const gatedFields = [];
21
- const zodDef = schema._zod.def;
22
- if (zodDef.type === "object") {
23
- const shape = zodDef.shape;
24
- for (const fieldName in shape) {
25
- const fieldSchema = shape[fieldName];
26
- const gate = z4.globalRegistry.get(fieldSchema)?.gate;
27
- if (gate) {
28
- gatedFields.push([fieldName, gate]);
29
- }
30
- }
31
- }
32
- return gatedFields;
33
- }
34
-
35
- export { gatingContext as g, withoutGatedFields as w };
@@ -1,9 +0,0 @@
1
- function resolveFriendlyStandardHandleOptions(options) {
2
- return {
3
- ...options,
4
- context: options.context ?? {}
5
- // Context only optional if all fields are optional
6
- };
7
- }
8
-
9
- export { resolveFriendlyStandardHandleOptions as r };
@@ -1,13 +0,0 @@
1
- class CompositeStandardHandlerPlugin {
2
- plugins;
3
- constructor(plugins = []) {
4
- this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
5
- }
6
- init(options, router) {
7
- for (const plugin of this.plugins) {
8
- plugin.init?.(options, router);
9
- }
10
- }
11
- }
12
-
13
- export { CompositeStandardHandlerPlugin as C };