@standardserver/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/LICENCE +21 -0
- package/dist/index.d.mts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.mjs +273 -0
- package/package.json +33 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Standard Server
|
|
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.d.mts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { StandardBodyHint, StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse, StandardLazyResponse, StandardUrl } from '@standardserver/core';
|
|
2
|
+
import { AsyncIteratorClass } from '@standardserver/shared';
|
|
3
|
+
|
|
4
|
+
declare function toEventIterator(stream: ReadableStream<Uint8Array<ArrayBuffer>> | null): AsyncIteratorClass<unknown>;
|
|
5
|
+
interface ToEventStreamOptions {
|
|
6
|
+
/**
|
|
7
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
8
|
+
*
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
keepAliveEnabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
14
|
+
*
|
|
15
|
+
* @default 5000
|
|
16
|
+
*/
|
|
17
|
+
keepAliveInterval?: number;
|
|
18
|
+
/**
|
|
19
|
+
* The content of the ping comment. Must not include newline characters.
|
|
20
|
+
*
|
|
21
|
+
* @default ''
|
|
22
|
+
*/
|
|
23
|
+
keepAliveComment?: string;
|
|
24
|
+
/**
|
|
25
|
+
* If true, an initial comment is sent immediately upon stream start to flush headers.
|
|
26
|
+
* This allows the receiving side to establish the connection without waiting for the first event.
|
|
27
|
+
*
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
initialCommentEnabled?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The content of the initial comment sent upon stream start. Must not include newline characters.
|
|
33
|
+
*
|
|
34
|
+
* @default ''
|
|
35
|
+
*/
|
|
36
|
+
initialComment?: string;
|
|
37
|
+
/**
|
|
38
|
+
* If false, a 'close' event is only sent if the iterator returns a non-empty value (undefined).
|
|
39
|
+
* By default, a 'close' event is always sent when the iterator completes.
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
alwaysSendCloseEvent?: boolean;
|
|
44
|
+
}
|
|
45
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
46
|
+
|
|
47
|
+
interface ToStandardBodyOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Hints on how the body should be parsed.
|
|
50
|
+
*/
|
|
51
|
+
hint?: StandardBodyHint | undefined;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert a fetch request or response to a standard body.
|
|
55
|
+
*/
|
|
56
|
+
declare function toStandardBody(re: Request | Response, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
57
|
+
interface ToFetchBodyOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Options for the event iterator, like keep-alive settings, initial comment, etc.
|
|
60
|
+
*/
|
|
61
|
+
eventIterator?: ToEventStreamOptions;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convert a standard body to a fetch body.
|
|
65
|
+
*/
|
|
66
|
+
declare function toFetchBody(body: StandardBody, headers: StandardHeaders, options?: ToFetchBodyOptions): [
|
|
67
|
+
body: undefined | string | FormData | URLSearchParams | Blob | ReadableStream<Uint8Array<ArrayBuffer>>,
|
|
68
|
+
headers: StandardHeaders
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Convert fetch headers to standard headers.
|
|
73
|
+
*/
|
|
74
|
+
declare function toStandardHeaders(headers: Headers): StandardHeaders;
|
|
75
|
+
/**
|
|
76
|
+
* Convert standard headers to fetch headers.
|
|
77
|
+
*/
|
|
78
|
+
declare function toFetchHeaders(standardHeaders: StandardHeaders): Headers;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Convert a fetch request to a standard request.
|
|
82
|
+
*/
|
|
83
|
+
declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
|
|
84
|
+
|
|
85
|
+
interface ToFetchResponseOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Options for body conversion, like event iterator options, etc.
|
|
88
|
+
*/
|
|
89
|
+
body?: ToFetchBodyOptions;
|
|
90
|
+
}
|
|
91
|
+
declare function toFetchResponse(standardResponse: StandardResponse, options?: ToFetchResponseOptions): Response;
|
|
92
|
+
declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
|
|
93
|
+
|
|
94
|
+
declare function toStandardUrl(url: URL): StandardUrl;
|
|
95
|
+
|
|
96
|
+
export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse, toStandardUrl };
|
|
97
|
+
export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchResponseOptions, ToStandardBodyOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { StandardBodyHint, StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse, StandardLazyResponse, StandardUrl } from '@standardserver/core';
|
|
2
|
+
import { AsyncIteratorClass } from '@standardserver/shared';
|
|
3
|
+
|
|
4
|
+
declare function toEventIterator(stream: ReadableStream<Uint8Array<ArrayBuffer>> | null): AsyncIteratorClass<unknown>;
|
|
5
|
+
interface ToEventStreamOptions {
|
|
6
|
+
/**
|
|
7
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
8
|
+
*
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
keepAliveEnabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
14
|
+
*
|
|
15
|
+
* @default 5000
|
|
16
|
+
*/
|
|
17
|
+
keepAliveInterval?: number;
|
|
18
|
+
/**
|
|
19
|
+
* The content of the ping comment. Must not include newline characters.
|
|
20
|
+
*
|
|
21
|
+
* @default ''
|
|
22
|
+
*/
|
|
23
|
+
keepAliveComment?: string;
|
|
24
|
+
/**
|
|
25
|
+
* If true, an initial comment is sent immediately upon stream start to flush headers.
|
|
26
|
+
* This allows the receiving side to establish the connection without waiting for the first event.
|
|
27
|
+
*
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
initialCommentEnabled?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The content of the initial comment sent upon stream start. Must not include newline characters.
|
|
33
|
+
*
|
|
34
|
+
* @default ''
|
|
35
|
+
*/
|
|
36
|
+
initialComment?: string;
|
|
37
|
+
/**
|
|
38
|
+
* If false, a 'close' event is only sent if the iterator returns a non-empty value (undefined).
|
|
39
|
+
* By default, a 'close' event is always sent when the iterator completes.
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
alwaysSendCloseEvent?: boolean;
|
|
44
|
+
}
|
|
45
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
46
|
+
|
|
47
|
+
interface ToStandardBodyOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Hints on how the body should be parsed.
|
|
50
|
+
*/
|
|
51
|
+
hint?: StandardBodyHint | undefined;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert a fetch request or response to a standard body.
|
|
55
|
+
*/
|
|
56
|
+
declare function toStandardBody(re: Request | Response, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
57
|
+
interface ToFetchBodyOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Options for the event iterator, like keep-alive settings, initial comment, etc.
|
|
60
|
+
*/
|
|
61
|
+
eventIterator?: ToEventStreamOptions;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convert a standard body to a fetch body.
|
|
65
|
+
*/
|
|
66
|
+
declare function toFetchBody(body: StandardBody, headers: StandardHeaders, options?: ToFetchBodyOptions): [
|
|
67
|
+
body: undefined | string | FormData | URLSearchParams | Blob | ReadableStream<Uint8Array<ArrayBuffer>>,
|
|
68
|
+
headers: StandardHeaders
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Convert fetch headers to standard headers.
|
|
73
|
+
*/
|
|
74
|
+
declare function toStandardHeaders(headers: Headers): StandardHeaders;
|
|
75
|
+
/**
|
|
76
|
+
* Convert standard headers to fetch headers.
|
|
77
|
+
*/
|
|
78
|
+
declare function toFetchHeaders(standardHeaders: StandardHeaders): Headers;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Convert a fetch request to a standard request.
|
|
82
|
+
*/
|
|
83
|
+
declare function toStandardLazyRequest(request: Request): StandardLazyRequest;
|
|
84
|
+
|
|
85
|
+
interface ToFetchResponseOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Options for body conversion, like event iterator options, etc.
|
|
88
|
+
*/
|
|
89
|
+
body?: ToFetchBodyOptions;
|
|
90
|
+
}
|
|
91
|
+
declare function toFetchResponse(standardResponse: StandardResponse, options?: ToFetchResponseOptions): Response;
|
|
92
|
+
declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
|
|
93
|
+
|
|
94
|
+
declare function toStandardUrl(url: URL): StandardUrl;
|
|
95
|
+
|
|
96
|
+
export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse, toStandardUrl };
|
|
97
|
+
export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchResponseOptions, ToStandardBodyOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { getFilenameFromContentDisposition, generateContentDisposition } from '@standardserver/core';
|
|
2
|
+
import { AsyncIteratorClass, parseEmptyableJSON, isTypescriptObject, stringifyJSON, AbortError, isAsyncIteratorObject } from '@standardserver/shared';
|
|
3
|
+
import { EventStreamDecoderStream, withEventIteratorEventMeta, EventIteratorErrorEvent, encodeEventStreamMessage, resolveEventIteratorEvent, getEventIteratorEventMeta } from '@standardserver/core/event-stream';
|
|
4
|
+
|
|
5
|
+
function toEventIterator(stream) {
|
|
6
|
+
const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventStreamDecoderStream());
|
|
7
|
+
const reader = eventStream?.getReader();
|
|
8
|
+
let isCancelled = false;
|
|
9
|
+
return new AsyncIteratorClass(async () => {
|
|
10
|
+
while (true) {
|
|
11
|
+
if (reader === void 0) {
|
|
12
|
+
return { done: true, value: void 0 };
|
|
13
|
+
}
|
|
14
|
+
const { done, value } = await reader.read();
|
|
15
|
+
if (done) {
|
|
16
|
+
if (isCancelled) {
|
|
17
|
+
throw new AbortError("Stream was cancelled");
|
|
18
|
+
}
|
|
19
|
+
return { done: true, value: void 0 };
|
|
20
|
+
}
|
|
21
|
+
switch (value.event) {
|
|
22
|
+
case "message": {
|
|
23
|
+
let message = parseEmptyableJSON(value.data);
|
|
24
|
+
if (isTypescriptObject(message)) {
|
|
25
|
+
message = withEventIteratorEventMeta(message, value);
|
|
26
|
+
}
|
|
27
|
+
return { done: false, value: message };
|
|
28
|
+
}
|
|
29
|
+
case "error": {
|
|
30
|
+
let error = new EventIteratorErrorEvent(parseEmptyableJSON(value.data));
|
|
31
|
+
error = withEventIteratorEventMeta(error, value);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
case "close": {
|
|
35
|
+
let close = parseEmptyableJSON(value.data);
|
|
36
|
+
if (isTypescriptObject(close)) {
|
|
37
|
+
close = withEventIteratorEventMeta(close, value);
|
|
38
|
+
}
|
|
39
|
+
return { done: true, value: close };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, async (isCompleted) => {
|
|
44
|
+
if (!isCompleted) {
|
|
45
|
+
isCancelled = true;
|
|
46
|
+
}
|
|
47
|
+
await reader?.cancel();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function toEventStream(iterator, options = {}) {
|
|
51
|
+
const keepAliveEnabled = options.keepAliveEnabled ?? true;
|
|
52
|
+
const keepAliveInterval = options.keepAliveInterval ?? 5e3;
|
|
53
|
+
const keepAliveComment = options.keepAliveComment ?? "";
|
|
54
|
+
const initialCommentEnabled = options.initialCommentEnabled ?? true;
|
|
55
|
+
const initialComment = options.initialComment ?? "";
|
|
56
|
+
const alwaysSendCloseEvent = options.alwaysSendCloseEvent ?? true;
|
|
57
|
+
let cancelled = false;
|
|
58
|
+
let timeout;
|
|
59
|
+
const stream = new ReadableStream({
|
|
60
|
+
start(controller) {
|
|
61
|
+
if (initialCommentEnabled) {
|
|
62
|
+
controller.enqueue(encodeEventStreamMessage({
|
|
63
|
+
comments: [initialComment]
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
async pull(controller) {
|
|
68
|
+
try {
|
|
69
|
+
if (keepAliveEnabled) {
|
|
70
|
+
timeout = setInterval(() => {
|
|
71
|
+
controller.enqueue(encodeEventStreamMessage({
|
|
72
|
+
comments: [keepAliveComment]
|
|
73
|
+
}));
|
|
74
|
+
}, keepAliveInterval);
|
|
75
|
+
}
|
|
76
|
+
const result = await iterator.next();
|
|
77
|
+
clearInterval(timeout);
|
|
78
|
+
if (cancelled) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const [data, meta] = resolveEventIteratorEvent(result.value);
|
|
82
|
+
if (alwaysSendCloseEvent || !result.done || data !== void 0 || meta !== void 0) {
|
|
83
|
+
const event = result.done ? "close" : "message";
|
|
84
|
+
controller.enqueue(encodeEventStreamMessage({
|
|
85
|
+
...meta,
|
|
86
|
+
event,
|
|
87
|
+
data: stringifyJSON(data)
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
if (result.done) {
|
|
91
|
+
controller.close();
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
clearInterval(timeout);
|
|
95
|
+
if (cancelled) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (err instanceof EventIteratorErrorEvent) {
|
|
99
|
+
controller.enqueue(encodeEventStreamMessage({
|
|
100
|
+
...getEventIteratorEventMeta(err),
|
|
101
|
+
event: "error",
|
|
102
|
+
data: stringifyJSON(err.data)
|
|
103
|
+
}));
|
|
104
|
+
controller.close();
|
|
105
|
+
} else {
|
|
106
|
+
controller.error(err);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
async cancel() {
|
|
111
|
+
cancelled = true;
|
|
112
|
+
clearInterval(timeout);
|
|
113
|
+
await iterator.return?.();
|
|
114
|
+
}
|
|
115
|
+
}).pipeThrough(new TextEncoderStream());
|
|
116
|
+
return stream;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function toStandardBody(re, options) {
|
|
120
|
+
const hint = re.headers.get("standard-server") ?? options?.hint;
|
|
121
|
+
const mimeType = re.headers.get("content-type")?.split(";")[0]?.trim();
|
|
122
|
+
const contentDisposition = re.headers.get("content-disposition");
|
|
123
|
+
const contentLength = re.headers.get("content-length");
|
|
124
|
+
if (hint === "none") {
|
|
125
|
+
return void 0;
|
|
126
|
+
}
|
|
127
|
+
if (hint === void 0 && re.body === null) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
if (re.bodyUsed) {
|
|
131
|
+
throw new TypeError("Failed to read body: body stream already read");
|
|
132
|
+
}
|
|
133
|
+
if (hint === "json" || hint === void 0 && contentDisposition === null && mimeType === "application/json") {
|
|
134
|
+
const text = await re.text();
|
|
135
|
+
return parseEmptyableJSON(text);
|
|
136
|
+
}
|
|
137
|
+
if (hint === "form-data" || hint === void 0 && contentDisposition === null && mimeType === "multipart/form-data") {
|
|
138
|
+
return await re.formData();
|
|
139
|
+
}
|
|
140
|
+
if (hint === "url-search-params" || hint === void 0 && contentDisposition === null && mimeType === "application/x-www-form-urlencoded") {
|
|
141
|
+
const text = await re.text();
|
|
142
|
+
return new URLSearchParams(text);
|
|
143
|
+
}
|
|
144
|
+
if (hint === "event-stream" || hint === void 0 && contentDisposition === null && mimeType === "text/event-stream") {
|
|
145
|
+
return toEventIterator(re.body);
|
|
146
|
+
}
|
|
147
|
+
if (hint === "file" || hint === void 0 && (contentDisposition !== null || contentLength !== null)) {
|
|
148
|
+
const fileName = contentDisposition !== null ? getFilenameFromContentDisposition(contentDisposition) : void 0;
|
|
149
|
+
const blob = await re.blob();
|
|
150
|
+
return new File([blob], fileName ?? "blob", {
|
|
151
|
+
type: blob.type
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return re.body ?? new ReadableStream({
|
|
155
|
+
start(controller) {
|
|
156
|
+
controller.close();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function toFetchBody(body, headers, options = {}) {
|
|
161
|
+
headers = { ...headers };
|
|
162
|
+
if (body === void 0) {
|
|
163
|
+
headers["standard-server"] = "none";
|
|
164
|
+
return [void 0, headers];
|
|
165
|
+
}
|
|
166
|
+
if (body instanceof ReadableStream) {
|
|
167
|
+
headers["standard-server"] = "octet-stream";
|
|
168
|
+
headers["content-type"] ??= "application/octet-stream";
|
|
169
|
+
return [body, headers];
|
|
170
|
+
}
|
|
171
|
+
if (body instanceof Blob) {
|
|
172
|
+
headers["standard-server"] = "file";
|
|
173
|
+
headers["content-type"] ??= body.type;
|
|
174
|
+
headers["content-disposition"] ??= generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
175
|
+
if (Number.isNaN(body.size)) {
|
|
176
|
+
return [body.stream(), headers];
|
|
177
|
+
}
|
|
178
|
+
headers["content-length"] ??= body.size.toString();
|
|
179
|
+
return [body, headers];
|
|
180
|
+
}
|
|
181
|
+
if (body instanceof FormData) {
|
|
182
|
+
headers["standard-server"] = "form-data";
|
|
183
|
+
return [body, headers];
|
|
184
|
+
}
|
|
185
|
+
if (body instanceof URLSearchParams) {
|
|
186
|
+
headers["standard-server"] = "url-search-params";
|
|
187
|
+
return [body, headers];
|
|
188
|
+
}
|
|
189
|
+
if (isAsyncIteratorObject(body)) {
|
|
190
|
+
headers["standard-server"] = "event-stream";
|
|
191
|
+
headers["content-type"] ??= "text/event-stream";
|
|
192
|
+
return [toEventStream(body, options.eventIterator), headers];
|
|
193
|
+
}
|
|
194
|
+
headers["standard-server"] = "json";
|
|
195
|
+
headers["content-type"] ??= "application/json";
|
|
196
|
+
return [stringifyJSON(body), headers];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function toStandardHeaders(headers) {
|
|
200
|
+
const standardHeaders = {};
|
|
201
|
+
headers.forEach((value, key) => {
|
|
202
|
+
if (Array.isArray(standardHeaders[key])) {
|
|
203
|
+
standardHeaders[key].push(value);
|
|
204
|
+
} else if (standardHeaders[key] !== void 0) {
|
|
205
|
+
standardHeaders[key] = [standardHeaders[key], value];
|
|
206
|
+
} else {
|
|
207
|
+
standardHeaders[key] = value;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return standardHeaders;
|
|
211
|
+
}
|
|
212
|
+
function toFetchHeaders(standardHeaders) {
|
|
213
|
+
const headers = new Headers();
|
|
214
|
+
for (const [key, value] of Object.entries(standardHeaders)) {
|
|
215
|
+
if (Array.isArray(value)) {
|
|
216
|
+
for (const v of value) {
|
|
217
|
+
headers.append(key, v);
|
|
218
|
+
}
|
|
219
|
+
} else if (value !== void 0) {
|
|
220
|
+
headers.append(key, value);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return headers;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function toStandardUrl(url) {
|
|
227
|
+
const pathname = `${url.pathname.startsWith("/") ? "" : "/"}${url.pathname}`;
|
|
228
|
+
return `${pathname}${url.search}${url.hash}`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function toStandardLazyRequest(request) {
|
|
232
|
+
const url = new URL(request.url);
|
|
233
|
+
return {
|
|
234
|
+
url: toStandardUrl(url),
|
|
235
|
+
method: request.method,
|
|
236
|
+
get headers() {
|
|
237
|
+
const headers = toStandardHeaders(request.headers);
|
|
238
|
+
Object.defineProperty(this, "headers", { value: headers, writable: true });
|
|
239
|
+
return headers;
|
|
240
|
+
},
|
|
241
|
+
set headers(value) {
|
|
242
|
+
Object.defineProperty(this, "headers", { value, writable: true });
|
|
243
|
+
},
|
|
244
|
+
resolveBody: (hint) => toStandardBody(request, { hint }),
|
|
245
|
+
signal: request.signal
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function toFetchResponse(standardResponse, options = {}) {
|
|
250
|
+
const [body, standardHeaders] = toFetchBody(standardResponse.body, standardResponse.headers, options.body);
|
|
251
|
+
const response = new Response(body, {
|
|
252
|
+
headers: toFetchHeaders(standardHeaders),
|
|
253
|
+
status: standardResponse.status
|
|
254
|
+
});
|
|
255
|
+
void response.body;
|
|
256
|
+
return response;
|
|
257
|
+
}
|
|
258
|
+
function toStandardLazyResponse(response) {
|
|
259
|
+
return {
|
|
260
|
+
resolveBody: (hint) => toStandardBody(response, { hint }),
|
|
261
|
+
status: response.status,
|
|
262
|
+
get headers() {
|
|
263
|
+
const headers = toStandardHeaders(response.headers);
|
|
264
|
+
Object.defineProperty(this, "headers", { value: headers, writable: true });
|
|
265
|
+
return headers;
|
|
266
|
+
},
|
|
267
|
+
set headers(value) {
|
|
268
|
+
Object.defineProperty(this, "headers", { value, writable: true });
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse, toStandardUrl };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@standardserver/fetch",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://standardserver.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/standardserver/standardserver.git",
|
|
10
|
+
"directory": "packages/fetch"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"default": "./dist/index.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@standardserver/core": "0.0.0",
|
|
24
|
+
"@standardserver/shared": "0.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@hono/node-server": "^1.19.7"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "unbuild",
|
|
31
|
+
"type:check": "tsc -b"
|
|
32
|
+
}
|
|
33
|
+
}
|