@ohmyapi/relay 0.1.0 → 0.2.0
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/body.d.ts +9 -4
- package/dist/body.js +27 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/body.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* 响应取消和超时信号,要求请求体为对象。返回解析结果和解压后的字节,这些字节用于原样转发。
|
|
4
|
-
*/
|
|
1
|
+
import { type ApiProtocol, type Data } from '@ohmyapi/protocols';
|
|
2
|
+
/** 读取有大小和超时限制的 JSON 对象,并保留解压后的原始字节。 */
|
|
5
3
|
export declare function readJsonBody(req: Request, signal: AbortSignal, stats: {
|
|
6
4
|
bytes: number;
|
|
7
5
|
}, progress: () => void, maxBodyBytes: number): Promise<{
|
|
8
6
|
parsed: Record<string, unknown>;
|
|
9
7
|
bytes: NonSharedBuffer;
|
|
10
8
|
}>;
|
|
9
|
+
/** 协议声明支持上传时接受 multipart,同时保留原始字节和对应的 boundary。 */
|
|
10
|
+
export declare function readProtocolBody(protocol: ApiProtocol, ...args: Parameters<typeof readJsonBody>): Promise<{
|
|
11
|
+
parsed: Data;
|
|
12
|
+
bytes: NonSharedBuffer;
|
|
13
|
+
form?: FormData;
|
|
14
|
+
contentType?: string;
|
|
15
|
+
}>;
|
package/dist/body.js
CHANGED
|
@@ -37,17 +37,11 @@ async function decodeBody(raw, steps, maxBodyBytes) {
|
|
|
37
37
|
}
|
|
38
38
|
return bytes;
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
* 读取 JSON 请求体:按 Content-Encoding 解压(gzip、deflate、br、zstd),限制大小,
|
|
42
|
-
* 响应取消和超时信号,要求请求体为对象。返回解析结果和解压后的字节,这些字节用于原样转发。
|
|
43
|
-
*/
|
|
44
|
-
export async function readJsonBody(req, signal, stats, progress, maxBodyBytes) {
|
|
45
|
-
if (!req.headers.get('content-type')?.toLowerCase().startsWith('application/json'))
|
|
46
|
-
throw new GatewayError(415, 'unsupported_media_type', 'Content-Type must be application/json');
|
|
40
|
+
async function readBodyBytes(req, signal, stats, progress, maxBodyBytes) {
|
|
47
41
|
const steps = contentDecoders(req);
|
|
48
42
|
const reader = req.body?.getReader();
|
|
49
43
|
if (!reader)
|
|
50
|
-
throw new GatewayError(400, 'invalid_request', '
|
|
44
|
+
throw new GatewayError(400, 'invalid_request', 'A request body is required');
|
|
51
45
|
const cancel = () => {
|
|
52
46
|
void reader.cancel().catch(() => { });
|
|
53
47
|
};
|
|
@@ -84,7 +78,13 @@ export async function readJsonBody(req, signal, stats, progress, maxBodyBytes) {
|
|
|
84
78
|
throw new GatewayError(408, 'body_timeout', message);
|
|
85
79
|
throw new GatewayError(400, 'request_cancelled', message);
|
|
86
80
|
}
|
|
87
|
-
|
|
81
|
+
return decodeBody(Buffer.concat(chunks), steps, maxBodyBytes);
|
|
82
|
+
}
|
|
83
|
+
/** 读取有大小和超时限制的 JSON 对象,并保留解压后的原始字节。 */
|
|
84
|
+
export async function readJsonBody(req, signal, stats, progress, maxBodyBytes) {
|
|
85
|
+
if (!req.headers.get('content-type')?.toLowerCase().startsWith('application/json'))
|
|
86
|
+
throw new GatewayError(415, 'unsupported_media_type', 'Content-Type must be application/json');
|
|
87
|
+
const bytes = await readBodyBytes(req, signal, stats, progress, maxBodyBytes);
|
|
88
88
|
let parsed;
|
|
89
89
|
try {
|
|
90
90
|
parsed = JSON.parse(bytes.toString('utf8'));
|
|
@@ -97,3 +97,21 @@ export async function readJsonBody(req, signal, stats, progress, maxBodyBytes) {
|
|
|
97
97
|
throw new GatewayError(400, 'invalid_request', 'A model name and a boolean stream option are required');
|
|
98
98
|
return { parsed, bytes };
|
|
99
99
|
}
|
|
100
|
+
/** 协议声明支持上传时接受 multipart,同时保留原始字节和对应的 boundary。 */
|
|
101
|
+
export async function readProtocolBody(protocol, ...args) {
|
|
102
|
+
const [req] = args;
|
|
103
|
+
const contentType = req.headers.get('content-type') ?? '';
|
|
104
|
+
if (contentType.split(';', 1)[0].trim().toLowerCase() !== 'multipart/form-data')
|
|
105
|
+
return readJsonBody(...args);
|
|
106
|
+
if (!protocol.multipart || !new URL(req.url).pathname.endsWith(protocol.multipart.path))
|
|
107
|
+
throw new GatewayError(415, 'unsupported_media_type', 'This route does not accept multipart/form-data');
|
|
108
|
+
const bytes = await readBodyBytes(...args);
|
|
109
|
+
let form;
|
|
110
|
+
try {
|
|
111
|
+
form = await new Response(bytes, { headers: { 'content-type': contentType } }).formData();
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
throw new GatewayError(400, 'invalid_request', 'Invalid multipart form data');
|
|
115
|
+
}
|
|
116
|
+
return { parsed: protocol.multipart.parse(form), bytes, form, contentType };
|
|
117
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohmyapi/relay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Request lifecycle and response relaying for LLM API gateways",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"node": ">=24.13.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@ohmyapi/protocols": "^0.
|
|
19
|
+
"@ohmyapi/protocols": "^0.2.0"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc -p tsconfig.json",
|