@standardserver/fetch 0.0.10 → 0.0.12
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/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +22 -19
- package/package.json +5 -4
package/dist/index.d.mts
CHANGED
|
@@ -35,11 +35,11 @@ interface ToEventStreamOptions {
|
|
|
35
35
|
*/
|
|
36
36
|
initialComment?: string;
|
|
37
37
|
/**
|
|
38
|
-
* If true,
|
|
38
|
+
* If true, a empty `close` event is sent when the iterator completes with `undefined`.
|
|
39
39
|
*
|
|
40
40
|
* @default true
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
emptyCloseEventEnabled?: boolean;
|
|
43
43
|
}
|
|
44
44
|
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
45
45
|
|
package/dist/index.d.ts
CHANGED
|
@@ -35,11 +35,11 @@ interface ToEventStreamOptions {
|
|
|
35
35
|
*/
|
|
36
36
|
initialComment?: string;
|
|
37
37
|
/**
|
|
38
|
-
* If true,
|
|
38
|
+
* If true, a empty `close` event is sent when the iterator completes with `undefined`.
|
|
39
39
|
*
|
|
40
40
|
* @default true
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
emptyCloseEventEnabled?: boolean;
|
|
43
43
|
}
|
|
44
44
|
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
45
45
|
|
package/dist/index.mjs
CHANGED
|
@@ -53,7 +53,7 @@ function toEventStream(iterator, options = {}) {
|
|
|
53
53
|
const keepAliveComment = options.keepAliveComment ?? "";
|
|
54
54
|
const initialCommentEnabled = options.initialCommentEnabled ?? true;
|
|
55
55
|
const initialComment = options.initialComment ?? "";
|
|
56
|
-
const
|
|
56
|
+
const emptyCloseEventEnabled = options.emptyCloseEventEnabled ?? true;
|
|
57
57
|
let cancelled = false;
|
|
58
58
|
let timeout;
|
|
59
59
|
const stream = new ReadableStream({
|
|
@@ -79,7 +79,7 @@ function toEventStream(iterator, options = {}) {
|
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
const [data, meta] = unwrapEventIteratorEvent(result.value);
|
|
82
|
-
if (!
|
|
82
|
+
if (!result.done || data !== void 0 || meta !== void 0 || emptyCloseEventEnabled) {
|
|
83
83
|
const event = result.done ? "close" : "message";
|
|
84
84
|
controller.enqueue(encodeEventStreamMessage({
|
|
85
85
|
...meta,
|
|
@@ -119,9 +119,8 @@ function toEventStream(iterator, options = {}) {
|
|
|
119
119
|
async function toStandardBody(re, options) {
|
|
120
120
|
const hint = re.headers.get("standard-server") ?? options?.hint;
|
|
121
121
|
const mimeType = re.headers.get("content-type")?.split(";")[0]?.trim();
|
|
122
|
-
const contentDisposition = re.headers.get("content-disposition");
|
|
123
122
|
const contentLength = re.headers.get("content-length");
|
|
124
|
-
if (hint === "none") {
|
|
123
|
+
if (hint === "none" || hint === void 0 && mimeType === void 0 && (contentLength === "0" || contentLength === null)) {
|
|
125
124
|
return void 0;
|
|
126
125
|
}
|
|
127
126
|
if (hint === void 0 && re.body === null) {
|
|
@@ -130,21 +129,22 @@ async function toStandardBody(re, options) {
|
|
|
130
129
|
if (re.bodyUsed) {
|
|
131
130
|
throw new TypeError("Failed to read body: body stream already read");
|
|
132
131
|
}
|
|
133
|
-
if (hint === "json" || hint === void 0 &&
|
|
132
|
+
if (hint === "json" || hint === void 0 && mimeType === "application/json") {
|
|
134
133
|
const text = await re.text();
|
|
135
134
|
return parseEmptyableJSON(text);
|
|
136
135
|
}
|
|
137
|
-
if (hint === "form-data" || hint === void 0 &&
|
|
136
|
+
if (hint === "form-data" || hint === void 0 && mimeType === "multipart/form-data") {
|
|
138
137
|
return await re.formData();
|
|
139
138
|
}
|
|
140
|
-
if (hint === "url-search-params" || hint === void 0 &&
|
|
139
|
+
if (hint === "url-search-params" || hint === void 0 && mimeType === "application/x-www-form-urlencoded") {
|
|
141
140
|
const text = await re.text();
|
|
142
141
|
return new URLSearchParams(text);
|
|
143
142
|
}
|
|
144
|
-
if (hint === "event-stream" || hint === void 0 &&
|
|
143
|
+
if (hint === "event-stream" || hint === void 0 && mimeType === "text/event-stream") {
|
|
145
144
|
return toEventIterator(re.body);
|
|
146
145
|
}
|
|
147
|
-
if (hint === "file" || hint === void 0 &&
|
|
146
|
+
if (hint === "file" || hint === void 0 && contentLength !== null) {
|
|
147
|
+
const contentDisposition = re.headers.get("content-disposition");
|
|
148
148
|
const fileName = contentDisposition !== null ? getFilenameFromContentDisposition(contentDisposition) : void 0;
|
|
149
149
|
const blob = await re.blob();
|
|
150
150
|
return new File([blob], fileName ?? "blob", {
|
|
@@ -158,19 +158,26 @@ async function toStandardBody(re, options) {
|
|
|
158
158
|
});
|
|
159
159
|
}
|
|
160
160
|
function toFetchBody(body, headers, options = {}) {
|
|
161
|
-
|
|
161
|
+
const originalContentType = headers["content-type"];
|
|
162
|
+
const originalContentLength = headers["content-length"];
|
|
163
|
+
headers = {
|
|
164
|
+
...headers,
|
|
165
|
+
"standard-server": void 0,
|
|
166
|
+
"content-type": void 0,
|
|
167
|
+
"content-length": void 0
|
|
168
|
+
};
|
|
162
169
|
if (body === void 0) {
|
|
163
|
-
headers["standard-server"] = "none";
|
|
164
170
|
return [void 0, headers];
|
|
165
171
|
}
|
|
166
172
|
if (body instanceof ReadableStream) {
|
|
167
173
|
headers["standard-server"] = "octet-stream";
|
|
168
|
-
headers["content-type"]
|
|
174
|
+
headers["content-type"] = originalContentType ?? "application/octet-stream";
|
|
175
|
+
headers["content-length"] = originalContentLength;
|
|
169
176
|
return [body, headers];
|
|
170
177
|
}
|
|
171
178
|
if (body instanceof Blob) {
|
|
172
179
|
headers["standard-server"] = "file";
|
|
173
|
-
headers["content-type"]
|
|
180
|
+
headers["content-type"] = body.type;
|
|
174
181
|
headers["content-disposition"] ??= generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
175
182
|
if (Number.isNaN(body.size)) {
|
|
176
183
|
return [body.stream(), headers];
|
|
@@ -179,20 +186,16 @@ function toFetchBody(body, headers, options = {}) {
|
|
|
179
186
|
return [body, headers];
|
|
180
187
|
}
|
|
181
188
|
if (body instanceof FormData) {
|
|
182
|
-
headers["standard-server"] = "form-data";
|
|
183
189
|
return [body, headers];
|
|
184
190
|
}
|
|
185
191
|
if (body instanceof URLSearchParams) {
|
|
186
|
-
headers["standard-server"] = "url-search-params";
|
|
187
192
|
return [body, headers];
|
|
188
193
|
}
|
|
189
194
|
if (isAsyncIteratorObject(body)) {
|
|
190
|
-
headers["
|
|
191
|
-
headers["content-type"] ??= "text/event-stream";
|
|
195
|
+
headers["content-type"] = "text/event-stream";
|
|
192
196
|
return [toEventStream(body, options.eventStream), headers];
|
|
193
197
|
}
|
|
194
|
-
headers["
|
|
195
|
-
headers["content-type"] ??= "application/json";
|
|
198
|
+
headers["content-type"] = "application/json";
|
|
196
199
|
return [stringifyJSON(body), headers];
|
|
197
200
|
}
|
|
198
201
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standardserver/fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.12",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://standardserver.dev",
|
|
7
7
|
"repository": {
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"url": "git+https://github.com/standardserver/standardserver.git",
|
|
10
10
|
"directory": "packages/fetch"
|
|
11
11
|
},
|
|
12
|
+
"sideEffects": false,
|
|
12
13
|
"exports": {
|
|
13
14
|
".": {
|
|
14
15
|
"types": "./dist/index.d.mts",
|
|
@@ -20,11 +21,11 @@
|
|
|
20
21
|
"dist"
|
|
21
22
|
],
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@standardserver/
|
|
24
|
-
"@standardserver/
|
|
24
|
+
"@standardserver/core": "0.0.12",
|
|
25
|
+
"@standardserver/shared": "0.0.12"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"@hono/node-server": "^
|
|
28
|
+
"@hono/node-server": "^2.0.1"
|
|
28
29
|
},
|
|
29
30
|
"scripts": {
|
|
30
31
|
"build": "unbuild",
|