@temporary-name/server 1.9.3-alpha.6a70825f6d8c3dc99b87e4e5b778aea0fa007417 → 1.9.3-alpha.6a735c9469d995338b7d51335ea70485e18a4dca
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.
- package/dist/adapters/aws-lambda/index.d.mts +5 -7
- package/dist/adapters/aws-lambda/index.d.ts +5 -7
- package/dist/adapters/aws-lambda/index.mjs +4 -6
- package/dist/adapters/fetch/index.d.mts +8 -86
- package/dist/adapters/fetch/index.d.ts +8 -86
- package/dist/adapters/fetch/index.mjs +16 -157
- package/dist/adapters/node/index.d.mts +9 -64
- package/dist/adapters/node/index.d.ts +9 -64
- package/dist/adapters/node/index.mjs +14 -122
- package/dist/{adapters/standard → handler}/index.d.mts +5 -7
- package/dist/{adapters/standard → handler}/index.d.ts +5 -7
- package/dist/handler/index.mjs +8 -0
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +368 -202
- package/dist/index.d.ts +368 -202
- package/dist/index.mjs +462 -176
- package/dist/openapi/index.d.mts +18 -53
- package/dist/openapi/index.d.ts +18 -53
- package/dist/openapi/index.mjs +340 -369
- package/dist/shared/server.BCY45g2x.mjs +160 -0
- package/dist/shared/server.BETu17rq.mjs +319 -0
- package/dist/shared/server.B_oW_rPl.mjs +525 -0
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CjPiuQYH.d.mts +51 -0
- package/dist/shared/server.CjPiuQYH.d.ts +51 -0
- package/dist/shared/server.CunymBH3.d.mts +39 -0
- package/dist/shared/server.DGMnYd7G.d.ts +39 -0
- package/dist/shared/server.xNz5cP2B.mjs +321 -0
- package/dist/shared/server.zsKBRxsz.d.mts +388 -0
- package/dist/shared/server.zsKBRxsz.d.ts +388 -0
- package/package.json +13 -31
- package/dist/adapters/standard/index.mjs +0 -10
- package/dist/plugins/index.d.mts +0 -160
- package/dist/plugins/index.d.ts +0 -160
- package/dist/plugins/index.mjs +0 -288
- package/dist/shared/server.BCcLYvdF.d.mts +0 -56
- package/dist/shared/server.CHV9AQHl.mjs +0 -412
- package/dist/shared/server.CdeqmULw.d.ts +0 -56
- package/dist/shared/server.DHezmW6C.d.mts +0 -23
- package/dist/shared/server.DN9mVGfv.mjs +0 -11
- package/dist/shared/server.DWwaAM-a.mjs +0 -255
- package/dist/shared/server.Da-qLzdU.d.ts +0 -23
- package/dist/shared/server.DecvGKtb.d.mts +0 -242
- package/dist/shared/server.DecvGKtb.d.ts +0 -242
- package/dist/shared/server.JtIZ8YG7.mjs +0 -237
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { HTTPPath, StandardResponse, StandardLazyRequest } from '@temporary-name/shared';
|
|
2
|
+
import { C as Context, l as Router } from './server.zsKBRxsz.js';
|
|
3
|
+
|
|
4
|
+
interface StandardHandleOptions<T extends Context> {
|
|
5
|
+
prefix?: HTTPPath;
|
|
6
|
+
context: T;
|
|
7
|
+
}
|
|
8
|
+
type StandardHandleResult = {
|
|
9
|
+
matched: true;
|
|
10
|
+
response: StandardResponse;
|
|
11
|
+
} | {
|
|
12
|
+
matched: false;
|
|
13
|
+
response: undefined;
|
|
14
|
+
};
|
|
15
|
+
declare class StandardHandler<T extends Context> {
|
|
16
|
+
private readonly matcher;
|
|
17
|
+
constructor(router: Router<T>);
|
|
18
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (object extends T ? {
|
|
22
|
+
context?: T;
|
|
23
|
+
} : {
|
|
24
|
+
context: T;
|
|
25
|
+
});
|
|
26
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
27
|
+
/**
|
|
28
|
+
* {@link https://github.com/unjs/rou3}
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
declare function toRou3Pattern(path: HTTPPath): string;
|
|
33
|
+
/**
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
declare function decodeParams(params: Record<string, string>): Record<string, string>;
|
|
37
|
+
|
|
38
|
+
export { StandardHandler as b, decodeParams as d, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
39
|
+
export type { FriendlyStandardHandleOptions as F, StandardHandleOptions as S, StandardHandleResult as a };
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { isObject, NullProtoObj, fallbackContractConfig, stringifyJSON, isAsyncIteratorObject, toORPCError, isORPCErrorStatus, tryDecodeURIComponent, toHttpPath, runWithSpan, ORPC_NAME, asyncIteratorWithSpan, setSpanError, ORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { c as createProcedureClient } from './server.BCY45g2x.mjs';
|
|
3
|
+
import { mapEventIterator, ErrorEvent } from '@temporary-name/standard-server';
|
|
4
|
+
import { traverseContractProcedures, getLazyMeta, unlazy } from '@temporary-name/server';
|
|
5
|
+
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
6
|
+
import { s as standardizeHTTPPath } from './server.BETu17rq.mjs';
|
|
7
|
+
|
|
8
|
+
function bracketNotationDeserialize(serialized, { maxArrayIndex = 9999 } = {}) {
|
|
9
|
+
if (serialized.length === 0) {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
const arrayPushStyles = /* @__PURE__ */ new WeakSet();
|
|
13
|
+
const ref = { value: [] };
|
|
14
|
+
for (const [path, value] of serialized) {
|
|
15
|
+
const segments = parsePath(path);
|
|
16
|
+
let currentRef = ref;
|
|
17
|
+
let nextSegment = "value";
|
|
18
|
+
segments.forEach((segment, i) => {
|
|
19
|
+
if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
|
|
20
|
+
currentRef[nextSegment] = [];
|
|
21
|
+
}
|
|
22
|
+
if (i !== segments.length - 1) {
|
|
23
|
+
if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment, maxArrayIndex)) {
|
|
24
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
25
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
26
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
27
|
+
} else {
|
|
28
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
33
|
+
if (segment === "") {
|
|
34
|
+
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
35
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
39
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
40
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
41
|
+
} else if (!isValidArrayIndex(segment, maxArrayIndex)) {
|
|
42
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
currentRef = currentRef[nextSegment];
|
|
48
|
+
nextSegment = segment;
|
|
49
|
+
});
|
|
50
|
+
if (Array.isArray(currentRef) && nextSegment === "") {
|
|
51
|
+
arrayPushStyles.add(currentRef);
|
|
52
|
+
currentRef.push(value);
|
|
53
|
+
} else if (nextSegment in currentRef) {
|
|
54
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
55
|
+
currentRef[nextSegment].push(value);
|
|
56
|
+
} else {
|
|
57
|
+
currentRef[nextSegment] = [currentRef[nextSegment], value];
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
currentRef[nextSegment] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return ref.value;
|
|
64
|
+
}
|
|
65
|
+
function parsePath(path) {
|
|
66
|
+
const segments = [];
|
|
67
|
+
let inBrackets = false;
|
|
68
|
+
let currentSegment = "";
|
|
69
|
+
let backslashCount = 0;
|
|
70
|
+
for (let i = 0; i < path.length; i++) {
|
|
71
|
+
const char = path[i];
|
|
72
|
+
const nextChar = path[i + 1];
|
|
73
|
+
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
74
|
+
if (nextChar === void 0) {
|
|
75
|
+
inBrackets = false;
|
|
76
|
+
}
|
|
77
|
+
segments.push(currentSegment);
|
|
78
|
+
currentSegment = "";
|
|
79
|
+
i++;
|
|
80
|
+
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
81
|
+
inBrackets = true;
|
|
82
|
+
segments.push(currentSegment);
|
|
83
|
+
currentSegment = "";
|
|
84
|
+
} else if (char === "\\") {
|
|
85
|
+
backslashCount++;
|
|
86
|
+
} else {
|
|
87
|
+
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
88
|
+
backslashCount = 0;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
92
|
+
}
|
|
93
|
+
function isValidArrayIndex(value, maxIndex) {
|
|
94
|
+
return /^0$|^[1-9]\d*$/.test(value) && Number(value) <= maxIndex;
|
|
95
|
+
}
|
|
96
|
+
function arrayToObject(array) {
|
|
97
|
+
const obj = new NullProtoObj();
|
|
98
|
+
array.forEach((item, i) => {
|
|
99
|
+
obj[i] = item;
|
|
100
|
+
});
|
|
101
|
+
return obj;
|
|
102
|
+
}
|
|
103
|
+
function pushStyleArrayToObject(array) {
|
|
104
|
+
const obj = new NullProtoObj();
|
|
105
|
+
obj[""] = array.length === 1 ? array[0] : array;
|
|
106
|
+
return obj;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function decode(request, pathParams) {
|
|
110
|
+
return {
|
|
111
|
+
path: pathParams ?? {},
|
|
112
|
+
query: bracketNotationDeserialize(Array.from(request.url.searchParams.entries())),
|
|
113
|
+
headers: request.headers,
|
|
114
|
+
body: await request.body() ?? {}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function asyncSafeSerialize(data) {
|
|
118
|
+
if (isAsyncIteratorObject(data)) {
|
|
119
|
+
return mapEventIterator(data, {
|
|
120
|
+
value: async (value) => value,
|
|
121
|
+
error: async (e) => {
|
|
122
|
+
return new ErrorEvent({
|
|
123
|
+
data: toORPCError(e).toJSON(),
|
|
124
|
+
cause: e
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return data;
|
|
130
|
+
}
|
|
131
|
+
function encode(output, procedure) {
|
|
132
|
+
const successStatus = fallbackContractConfig(
|
|
133
|
+
"defaultSuccessStatus",
|
|
134
|
+
procedure["~orpc"].route.successStatus
|
|
135
|
+
);
|
|
136
|
+
const outputStructure = fallbackContractConfig(
|
|
137
|
+
"defaultOutputStructure",
|
|
138
|
+
procedure["~orpc"].route.outputStructure
|
|
139
|
+
);
|
|
140
|
+
if (outputStructure === "compact") {
|
|
141
|
+
return {
|
|
142
|
+
status: successStatus,
|
|
143
|
+
headers: new Headers(),
|
|
144
|
+
body: asyncSafeSerialize(output)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (!isDetailedOutput(output)) {
|
|
148
|
+
throw new Error(`
|
|
149
|
+
Invalid "detailed" output structure:
|
|
150
|
+
\u2022 Expected an object with optional properties:
|
|
151
|
+
- status (number 200-399)
|
|
152
|
+
- headers (Record<string, string | string[]>)
|
|
153
|
+
- body (any)
|
|
154
|
+
\u2022 No extra keys allowed.
|
|
155
|
+
|
|
156
|
+
Actual value:
|
|
157
|
+
${stringifyJSON(output)}
|
|
158
|
+
`);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
status: output.status ?? successStatus,
|
|
162
|
+
headers: output.headers ?? new Headers(),
|
|
163
|
+
body: asyncSafeSerialize(output.body)
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function encodeError(error) {
|
|
167
|
+
return {
|
|
168
|
+
status: error.status,
|
|
169
|
+
headers: new Headers(),
|
|
170
|
+
body: error.toJSON()
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function isDetailedOutput(output) {
|
|
174
|
+
if (!isObject(output)) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (output.headers && !isObject(output.headers)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function resolveFriendlyStandardHandleOptions(options) {
|
|
187
|
+
return {
|
|
188
|
+
...options,
|
|
189
|
+
context: options.context ?? {}
|
|
190
|
+
// Context only optional if all fields are optional
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function toRou3Pattern(path) {
|
|
194
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
195
|
+
}
|
|
196
|
+
function decodeParams(params) {
|
|
197
|
+
return Object.fromEntries(
|
|
198
|
+
Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)])
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
class StandardOpenAPIMatcher {
|
|
203
|
+
tree = createRouter();
|
|
204
|
+
pendingRouters = [];
|
|
205
|
+
init(router, path = []) {
|
|
206
|
+
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
207
|
+
const { path: path2, contract } = traverseOptions;
|
|
208
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
209
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
210
|
+
addRoute(this.tree, method, httpPath, {
|
|
211
|
+
path: path2,
|
|
212
|
+
// cast is safe because router is typed as Router (not ContractRouter)
|
|
213
|
+
procedure: contract,
|
|
214
|
+
router
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
this.pendingRouters.push(
|
|
218
|
+
...laziedOptions.map((option) => ({
|
|
219
|
+
...option,
|
|
220
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
221
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
222
|
+
}))
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
async match(method, pathname) {
|
|
226
|
+
if (this.pendingRouters.length) {
|
|
227
|
+
const newPendingRouters = [];
|
|
228
|
+
for (const pendingRouter of this.pendingRouters) {
|
|
229
|
+
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
230
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
231
|
+
this.init(router, pendingRouter.path);
|
|
232
|
+
} else {
|
|
233
|
+
newPendingRouters.push(pendingRouter);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
this.pendingRouters = newPendingRouters;
|
|
237
|
+
}
|
|
238
|
+
const match = findRoute(this.tree, method, pathname);
|
|
239
|
+
if (!match) {
|
|
240
|
+
return void 0;
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
path: match.data.path,
|
|
244
|
+
procedure: match.data.procedure,
|
|
245
|
+
params: match.params ? decodeParams(match.params) : void 0
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
class StandardHandler {
|
|
251
|
+
matcher;
|
|
252
|
+
constructor(router) {
|
|
253
|
+
this.matcher = new StandardOpenAPIMatcher();
|
|
254
|
+
this.matcher.init(router);
|
|
255
|
+
}
|
|
256
|
+
async handle(request, options) {
|
|
257
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
|
258
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
|
259
|
+
return { matched: false, response: void 0 };
|
|
260
|
+
}
|
|
261
|
+
return runWithSpan({ name: `${request.method} ${request.url.pathname}` }, async (span) => {
|
|
262
|
+
let step;
|
|
263
|
+
try {
|
|
264
|
+
const context = options.context;
|
|
265
|
+
const method = request.method;
|
|
266
|
+
const url = request.url;
|
|
267
|
+
const pathname = prefix ? url.pathname.replace(prefix, "") : url.pathname;
|
|
268
|
+
const match = await runWithSpan(
|
|
269
|
+
{ name: "find_procedure" },
|
|
270
|
+
() => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
|
|
271
|
+
);
|
|
272
|
+
if (!match) {
|
|
273
|
+
return { matched: false, response: void 0 };
|
|
274
|
+
}
|
|
275
|
+
span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
|
|
276
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
277
|
+
span?.setAttribute("rpc.method", match.path.join("."));
|
|
278
|
+
step = "decode_input";
|
|
279
|
+
const input = await runWithSpan({ name: "decode_input" }, () => decode(request, match.params));
|
|
280
|
+
step = void 0;
|
|
281
|
+
if (isAsyncIteratorObject(input.body)) {
|
|
282
|
+
input.body = asyncIteratorWithSpan(
|
|
283
|
+
{ name: "consume_event_iterator_input", signal: request.signal },
|
|
284
|
+
input.body
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const client = createProcedureClient(match.procedure, {
|
|
288
|
+
context,
|
|
289
|
+
path: match.path
|
|
290
|
+
});
|
|
291
|
+
step = "call_procedure";
|
|
292
|
+
const output = await client(input, {
|
|
293
|
+
request,
|
|
294
|
+
signal: request.signal,
|
|
295
|
+
lastEventId: request.headers.get("last-event-id") ?? void 0
|
|
296
|
+
});
|
|
297
|
+
step = void 0;
|
|
298
|
+
const response = encode(output, match.procedure);
|
|
299
|
+
return {
|
|
300
|
+
matched: true,
|
|
301
|
+
response
|
|
302
|
+
};
|
|
303
|
+
} catch (e) {
|
|
304
|
+
if (step !== "call_procedure") {
|
|
305
|
+
setSpanError(span, e);
|
|
306
|
+
}
|
|
307
|
+
const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
|
308
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
309
|
+
cause: e
|
|
310
|
+
}) : toORPCError(e);
|
|
311
|
+
const response = encodeError(error);
|
|
312
|
+
return {
|
|
313
|
+
matched: true,
|
|
314
|
+
response
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export { StandardHandler as S, encodeError as a, StandardOpenAPIMatcher as b, decodeParams as c, decode as d, encode as e, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|