@orpc/standard-server-fetch 0.0.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/LICENSE +21 -0
- package/dist/index.js +228 -0
- package/dist/src/body.d.ts +8 -0
- package/dist/src/event-source.d.ts +4 -0
- package/dist/src/headers.d.ts +12 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/request.d.ts +3 -0
- package/dist/src/response.d.ts +3 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 oRPC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// src/body.ts
|
|
2
|
+
import { contentDisposition, isAsyncIteratorObject, parseContentDisposition, parseEmptyableJSON as parseEmptyableJSON2 } from "@orpc/standard-server";
|
|
3
|
+
|
|
4
|
+
// src/event-source.ts
|
|
5
|
+
import {
|
|
6
|
+
encodeEventMessage,
|
|
7
|
+
ErrorEvent,
|
|
8
|
+
EventDecoderStream,
|
|
9
|
+
getEventMeta,
|
|
10
|
+
isEventMetaContainer,
|
|
11
|
+
parseEmptyableJSON,
|
|
12
|
+
UnknownEvent,
|
|
13
|
+
withEventMeta
|
|
14
|
+
} from "@orpc/standard-server";
|
|
15
|
+
function toEventIterator(stream) {
|
|
16
|
+
const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
|
|
17
|
+
const reader = eventStream.getReader();
|
|
18
|
+
async function* gen() {
|
|
19
|
+
try {
|
|
20
|
+
while (true) {
|
|
21
|
+
const { done, value } = await reader.read();
|
|
22
|
+
if (done) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
switch (value.event) {
|
|
26
|
+
case "message": {
|
|
27
|
+
let message = parseEmptyableJSON(value.data);
|
|
28
|
+
if (isEventMetaContainer(message)) {
|
|
29
|
+
message = withEventMeta(message, value);
|
|
30
|
+
}
|
|
31
|
+
yield message;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case "error": {
|
|
35
|
+
let error = new ErrorEvent({
|
|
36
|
+
data: parseEmptyableJSON(value.data)
|
|
37
|
+
});
|
|
38
|
+
error = withEventMeta(error, value);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
case "done": {
|
|
42
|
+
let done2 = parseEmptyableJSON(value.data);
|
|
43
|
+
if (isEventMetaContainer(done2)) {
|
|
44
|
+
done2 = withEventMeta(done2, value);
|
|
45
|
+
}
|
|
46
|
+
return done2;
|
|
47
|
+
}
|
|
48
|
+
default: {
|
|
49
|
+
let error = new UnknownEvent({
|
|
50
|
+
message: `Unknown event: ${value.event}`,
|
|
51
|
+
data: parseEmptyableJSON(value.data)
|
|
52
|
+
});
|
|
53
|
+
error = withEventMeta(error, value);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
await reader.cancel();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return gen();
|
|
63
|
+
}
|
|
64
|
+
function toEventStream(iterator) {
|
|
65
|
+
const stream = new ReadableStream({
|
|
66
|
+
async pull(controller) {
|
|
67
|
+
try {
|
|
68
|
+
const value = await iterator.next();
|
|
69
|
+
controller.enqueue(encodeEventMessage({
|
|
70
|
+
...getEventMeta(value.value),
|
|
71
|
+
event: value.done ? "done" : "message",
|
|
72
|
+
data: JSON.stringify(value.value)
|
|
73
|
+
}));
|
|
74
|
+
if (value.done) {
|
|
75
|
+
controller.close();
|
|
76
|
+
}
|
|
77
|
+
} catch (err) {
|
|
78
|
+
controller.enqueue(encodeEventMessage({
|
|
79
|
+
...getEventMeta(err),
|
|
80
|
+
event: "error",
|
|
81
|
+
data: err instanceof ErrorEvent ? JSON.stringify(err.data) : void 0
|
|
82
|
+
}));
|
|
83
|
+
controller.close();
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
async cancel(reason) {
|
|
87
|
+
if (reason) {
|
|
88
|
+
await iterator.throw?.(reason);
|
|
89
|
+
} else {
|
|
90
|
+
await iterator.return?.();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}).pipeThrough(new TextEncoderStream());
|
|
94
|
+
return stream;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/body.ts
|
|
98
|
+
async function toStandardBody(re) {
|
|
99
|
+
if (!re.body) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
const contentDisposition2 = re.headers.get("content-disposition");
|
|
103
|
+
if (contentDisposition2) {
|
|
104
|
+
const fileName = parseContentDisposition(contentDisposition2).parameters.filename;
|
|
105
|
+
if (typeof fileName === "string") {
|
|
106
|
+
const blob2 = await re.blob();
|
|
107
|
+
return new File([blob2], fileName, {
|
|
108
|
+
type: blob2.type
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const contentType = re.headers.get("content-type");
|
|
113
|
+
if (!contentType || contentType.startsWith("application/json")) {
|
|
114
|
+
const text = await re.text();
|
|
115
|
+
return parseEmptyableJSON2(text);
|
|
116
|
+
}
|
|
117
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
118
|
+
return await re.formData();
|
|
119
|
+
}
|
|
120
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
121
|
+
const text = await re.text();
|
|
122
|
+
return new URLSearchParams(text);
|
|
123
|
+
}
|
|
124
|
+
if (contentType.startsWith("text/event-stream")) {
|
|
125
|
+
return toEventIterator(re.body);
|
|
126
|
+
}
|
|
127
|
+
if (contentType.startsWith("text/")) {
|
|
128
|
+
return await re.text();
|
|
129
|
+
}
|
|
130
|
+
const blob = await re.blob();
|
|
131
|
+
return new File([blob], "blob", {
|
|
132
|
+
type: blob.type
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function toFetchBody(body, headers) {
|
|
136
|
+
headers.delete("content-type");
|
|
137
|
+
headers.delete("content-disposition");
|
|
138
|
+
if (body === void 0) {
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
if (body instanceof Blob) {
|
|
142
|
+
headers.set("content-type", body.type);
|
|
143
|
+
headers.set("content-length", body.size.toString());
|
|
144
|
+
headers.set(
|
|
145
|
+
"content-disposition",
|
|
146
|
+
contentDisposition(body instanceof File ? body.name : "blob", { type: "inline" })
|
|
147
|
+
);
|
|
148
|
+
return body;
|
|
149
|
+
}
|
|
150
|
+
if (body instanceof FormData) {
|
|
151
|
+
return body;
|
|
152
|
+
}
|
|
153
|
+
if (body instanceof URLSearchParams) {
|
|
154
|
+
return body;
|
|
155
|
+
}
|
|
156
|
+
if (isAsyncIteratorObject(body)) {
|
|
157
|
+
headers.set("content-type", "text/event-stream");
|
|
158
|
+
headers.set("cache-control", "no-cache");
|
|
159
|
+
headers.set("connection", "keep-alive");
|
|
160
|
+
return toEventStream(body);
|
|
161
|
+
}
|
|
162
|
+
headers.set("content-type", "application/json");
|
|
163
|
+
return JSON.stringify(body);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/headers.ts
|
|
167
|
+
function toStandardHeaders(headers, standardHeaders = {}) {
|
|
168
|
+
for (const [key, value] of headers) {
|
|
169
|
+
if (Array.isArray(standardHeaders[key])) {
|
|
170
|
+
standardHeaders[key].push(value);
|
|
171
|
+
} else if (standardHeaders[key] !== void 0) {
|
|
172
|
+
standardHeaders[key] = [standardHeaders[key], value];
|
|
173
|
+
} else {
|
|
174
|
+
standardHeaders[key] = value;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return standardHeaders;
|
|
178
|
+
}
|
|
179
|
+
function toFetchHeaders(headers, fetchHeaders = new Headers()) {
|
|
180
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
181
|
+
if (Array.isArray(value)) {
|
|
182
|
+
for (const v of value) {
|
|
183
|
+
fetchHeaders.append(key, v);
|
|
184
|
+
}
|
|
185
|
+
} else if (value !== void 0) {
|
|
186
|
+
fetchHeaders.append(key, value);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return fetchHeaders;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/request.ts
|
|
193
|
+
import { once } from "@orpc/standard-server";
|
|
194
|
+
function toStandardRequest(request) {
|
|
195
|
+
return {
|
|
196
|
+
raw: { request },
|
|
197
|
+
url: new URL(request.url),
|
|
198
|
+
signal: request.signal,
|
|
199
|
+
method: request.method,
|
|
200
|
+
body: once(() => toStandardBody(request)),
|
|
201
|
+
get headers() {
|
|
202
|
+
const headers = toStandardHeaders(request.headers);
|
|
203
|
+
Object.defineProperty(this, "headers", { value: headers, writable: true });
|
|
204
|
+
return headers;
|
|
205
|
+
},
|
|
206
|
+
set headers(value) {
|
|
207
|
+
Object.defineProperty(this, "headers", { value, writable: true });
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/response.ts
|
|
213
|
+
function toFetchResponse(response) {
|
|
214
|
+
const headers = toFetchHeaders(response.headers);
|
|
215
|
+
const body = toFetchBody(response.body, headers);
|
|
216
|
+
return new Response(body, { headers, status: response.status });
|
|
217
|
+
}
|
|
218
|
+
export {
|
|
219
|
+
toEventIterator,
|
|
220
|
+
toEventStream,
|
|
221
|
+
toFetchBody,
|
|
222
|
+
toFetchHeaders,
|
|
223
|
+
toFetchResponse,
|
|
224
|
+
toStandardBody,
|
|
225
|
+
toStandardHeaders,
|
|
226
|
+
toStandardRequest
|
|
227
|
+
};
|
|
228
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { StandardBody } from '@orpc/standard-server';
|
|
2
|
+
export declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
|
|
3
|
+
/**
|
|
4
|
+
* @param body
|
|
5
|
+
* @param headers - The headers can be changed by the function and effects on the original headers.
|
|
6
|
+
*/
|
|
7
|
+
export declare function toFetchBody(body: StandardBody, headers: Headers): string | Blob | FormData | URLSearchParams | undefined | ReadableStream<Uint8Array>;
|
|
8
|
+
//# sourceMappingURL=body.d.ts.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { JsonValue } from '@orpc/standard-server';
|
|
2
|
+
export declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<JsonValue | void, JsonValue | void, void>;
|
|
3
|
+
export declare function toEventStream(iterator: AsyncIterator<JsonValue | void, JsonValue | void, void>): ReadableStream<Uint8Array>;
|
|
4
|
+
//# sourceMappingURL=event-source.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StandardHeaders } from '@orpc/standard-server';
|
|
2
|
+
/**
|
|
3
|
+
* @param headers
|
|
4
|
+
* @param standardHeaders - The base headers can be changed by the function and effects on the original headers.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toStandardHeaders(headers: Headers, standardHeaders?: StandardHeaders): StandardHeaders;
|
|
7
|
+
/**
|
|
8
|
+
* @param headers
|
|
9
|
+
* @param fetchHeaders - The base headers can be changed by the function and effects on the original headers.
|
|
10
|
+
*/
|
|
11
|
+
export declare function toFetchHeaders(headers: StandardHeaders, fetchHeaders?: Headers): Headers;
|
|
12
|
+
//# sourceMappingURL=headers.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orpc/standard-server-fetch",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://unnoq.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/unnoq/orpc.git",
|
|
10
|
+
"directory": "packages/standard-server-fetch"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"orpc"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/src/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./🔒/*": {
|
|
22
|
+
"types": "./dist/src/*.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"!**/*.map",
|
|
27
|
+
"!**/*.tsbuildinfo",
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@orpc/standard-server": "0.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@hono/node-server": "^1.13.8"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|
|
38
|
+
"build:watch": "pnpm run build --watch",
|
|
39
|
+
"type:check": "tsc -b"
|
|
40
|
+
}
|
|
41
|
+
}
|