@wooksjs/http-body 0.5.20 → 0.5.25
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.cjs +140 -155
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +116 -153
- package/package.json +11 -5
package/dist/index.cjs
CHANGED
|
@@ -1,165 +1,150 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
4
26
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
27
|
+
//#region packages/http-body/src/utils/body-compressor.ts
|
|
28
|
+
const compressors = { identity: {
|
|
29
|
+
compress: (v) => v,
|
|
30
|
+
uncompress: (v) => v
|
|
31
|
+
} };
|
|
11
32
|
async function uncompressBody(encodings, body) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
return newBody;
|
|
33
|
+
let newBody = body;
|
|
34
|
+
for (const e of encodings.reverse()) {
|
|
35
|
+
const cmp = compressors[e];
|
|
36
|
+
if (!cmp) throw new Error(`Usupported compression type "${e}".`);
|
|
37
|
+
newBody = await cmp.uncompress(body);
|
|
38
|
+
}
|
|
39
|
+
return newBody;
|
|
21
40
|
}
|
|
22
41
|
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region packages/http-body/src/body.ts
|
|
23
44
|
function useBody() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
if (line.toLowerCase().startsWith('content-type:')) {
|
|
123
|
-
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
124
|
-
if (!partContentType) {
|
|
125
|
-
throw new eventHttp.HttpError(eventHttp.EHttpStatusCode.BadRequest, `Could not read content-type: ${line}`);
|
|
126
|
-
}
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
parsePart();
|
|
133
|
-
function parsePart() {
|
|
134
|
-
if (key && partContentType.includes('application/json')) {
|
|
135
|
-
result[key] = JSON.parse(result[key]);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
}
|
|
140
|
-
function urlEncodedParser(v) {
|
|
141
|
-
return new eventHttp.WooksURLSearchParams(v.trim()).toJson();
|
|
142
|
-
}
|
|
143
|
-
return {
|
|
144
|
-
isJson,
|
|
145
|
-
isHtml,
|
|
146
|
-
isXml,
|
|
147
|
-
isText,
|
|
148
|
-
isBinary,
|
|
149
|
-
isFormData,
|
|
150
|
-
isUrlencoded,
|
|
151
|
-
isCompressed,
|
|
152
|
-
contentEncodings,
|
|
153
|
-
parseBody,
|
|
154
|
-
rawBody,
|
|
155
|
-
};
|
|
45
|
+
const { store } = (0, __wooksjs_event_http.useHttpContext)();
|
|
46
|
+
const { init } = store("request");
|
|
47
|
+
const { rawBody } = (0, __wooksjs_event_http.useRequest)();
|
|
48
|
+
const { "content-type": contentType, "content-encoding": contentEncoding } = (0, __wooksjs_event_http.useHeaders)();
|
|
49
|
+
function contentIs(type) {
|
|
50
|
+
return (contentType || "").includes(type);
|
|
51
|
+
}
|
|
52
|
+
const isJson = () => init("isJson", () => contentIs("application/json"));
|
|
53
|
+
const isHtml = () => init("isHtml", () => contentIs("text/html"));
|
|
54
|
+
const isXml = () => init("isXml", () => contentIs("text/xml"));
|
|
55
|
+
const isText = () => init("isText", () => contentIs("text/plain"));
|
|
56
|
+
const isBinary = () => init("isBinary", () => contentIs("application/octet-stream"));
|
|
57
|
+
const isFormData = () => init("isFormData", () => contentIs("multipart/form-data"));
|
|
58
|
+
const isUrlencoded = () => init("isUrlencoded", () => contentIs("application/x-www-form-urlencoded"));
|
|
59
|
+
const isCompressed = () => init("isCompressed", () => {
|
|
60
|
+
const parts = contentEncodings();
|
|
61
|
+
for (const p of parts) if ([
|
|
62
|
+
"deflate",
|
|
63
|
+
"gzip",
|
|
64
|
+
"br"
|
|
65
|
+
].includes(p)) return true;
|
|
66
|
+
return false;
|
|
67
|
+
});
|
|
68
|
+
const contentEncodings = () => init("contentEncodings", () => (contentEncoding || "").split(",").map((p) => p.trim()).filter((p) => !!p));
|
|
69
|
+
const parseBody = () => init("parsed", async () => {
|
|
70
|
+
const body = await uncompressBody(contentEncodings(), (await rawBody()).toString());
|
|
71
|
+
if (isJson()) return jsonParser(body);
|
|
72
|
+
else if (isFormData()) return formDataParser(body);
|
|
73
|
+
else if (isUrlencoded()) return urlEncodedParser(body);
|
|
74
|
+
else if (isBinary()) return textParser(body);
|
|
75
|
+
else return textParser(body);
|
|
76
|
+
});
|
|
77
|
+
function jsonParser(v) {
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(v);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new __wooksjs_event_http.HttpError(400, error.message);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function textParser(v) {
|
|
85
|
+
return v;
|
|
86
|
+
}
|
|
87
|
+
function formDataParser(v) {
|
|
88
|
+
const boundary = `--${(/boundary=([^;]+)(?:;|$)/u.exec(contentType || "") || [, ""])[1]}`;
|
|
89
|
+
if (!boundary) throw new __wooksjs_event_http.HttpError(__wooksjs_event_http.EHttpStatusCode.BadRequest, "form-data boundary not recognized");
|
|
90
|
+
const parts = v.trim().split(boundary);
|
|
91
|
+
const result = {};
|
|
92
|
+
let key = "";
|
|
93
|
+
let partContentType = "text/plain";
|
|
94
|
+
for (const part of parts) {
|
|
95
|
+
parsePart();
|
|
96
|
+
key = "";
|
|
97
|
+
partContentType = "text/plain";
|
|
98
|
+
let valueMode = false;
|
|
99
|
+
const lines = part.trim().split(/\n/u).map((s) => s.trim());
|
|
100
|
+
for (const line of lines) if (valueMode) if (result[key]) result[key] += `\n${line}`;
|
|
101
|
+
else result[key] = line;
|
|
102
|
+
else {
|
|
103
|
+
if (!line || line === "--") {
|
|
104
|
+
valueMode = !!key;
|
|
105
|
+
if (valueMode) key = key.replace(/^["']/u, "").replace(/["']$/u, "");
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (line.toLowerCase().startsWith("content-disposition: form-data;")) {
|
|
109
|
+
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
110
|
+
if (!key) throw new __wooksjs_event_http.HttpError(__wooksjs_event_http.EHttpStatusCode.BadRequest, `Could not read multipart name: ${line}`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (line.toLowerCase().startsWith("content-type:")) {
|
|
114
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
115
|
+
if (!partContentType) throw new __wooksjs_event_http.HttpError(__wooksjs_event_http.EHttpStatusCode.BadRequest, `Could not read content-type: ${line}`);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
parsePart();
|
|
121
|
+
function parsePart() {
|
|
122
|
+
if (key && partContentType.includes("application/json")) result[key] = JSON.parse(result[key]);
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
function urlEncodedParser(v) {
|
|
127
|
+
return new __wooksjs_event_http.WooksURLSearchParams(v.trim()).toJson();
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
isJson,
|
|
131
|
+
isHtml,
|
|
132
|
+
isXml,
|
|
133
|
+
isText,
|
|
134
|
+
isBinary,
|
|
135
|
+
isFormData,
|
|
136
|
+
isUrlencoded,
|
|
137
|
+
isCompressed,
|
|
138
|
+
contentEncodings,
|
|
139
|
+
parseBody,
|
|
140
|
+
rawBody
|
|
141
|
+
};
|
|
156
142
|
}
|
|
157
143
|
function registerBodyCompressor(name, compressor) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
compressors[name] = compressor;
|
|
144
|
+
if (compressors[name]) throw new Error(`Body compressor "${name}" already registered.`);
|
|
145
|
+
compressors[name] = compressor;
|
|
162
146
|
}
|
|
163
147
|
|
|
164
|
-
|
|
165
|
-
exports.
|
|
148
|
+
//#endregion
|
|
149
|
+
exports.registerBodyCompressor = registerBodyCompressor
|
|
150
|
+
exports.useBody = useBody
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ declare function useBody(): {
|
|
|
14
14
|
isCompressed: () => boolean;
|
|
15
15
|
contentEncodings: () => string[];
|
|
16
16
|
parseBody: <T>() => Promise<T>;
|
|
17
|
-
rawBody: () => Promise<Buffer
|
|
17
|
+
rawBody: () => Promise<Buffer<ArrayBufferLike>>;
|
|
18
18
|
};
|
|
19
19
|
declare function registerBodyCompressor(name: string, compressor: TBodyCompressor): void;
|
|
20
20
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,162 +1,125 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EHttpStatusCode, HttpError, WooksURLSearchParams, useHeaders, useHttpContext, useRequest } from "@wooksjs/event-http";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
3
|
+
//#region packages/http-body/src/utils/body-compressor.ts
|
|
4
|
+
const compressors = { identity: {
|
|
5
|
+
compress: (v) => v,
|
|
6
|
+
uncompress: (v) => v
|
|
7
|
+
} };
|
|
9
8
|
async function uncompressBody(encodings, body) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
return newBody;
|
|
9
|
+
let newBody = body;
|
|
10
|
+
for (const e of encodings.reverse()) {
|
|
11
|
+
const cmp = compressors[e];
|
|
12
|
+
if (!cmp) throw new Error(`Usupported compression type "${e}".`);
|
|
13
|
+
newBody = await cmp.uncompress(body);
|
|
14
|
+
}
|
|
15
|
+
return newBody;
|
|
19
16
|
}
|
|
20
17
|
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region packages/http-body/src/body.ts
|
|
21
20
|
function useBody() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
if (line.toLowerCase().startsWith('content-type:')) {
|
|
121
|
-
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
122
|
-
if (!partContentType) {
|
|
123
|
-
throw new HttpError(EHttpStatusCode.BadRequest, `Could not read content-type: ${line}`);
|
|
124
|
-
}
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
parsePart();
|
|
131
|
-
function parsePart() {
|
|
132
|
-
if (key && partContentType.includes('application/json')) {
|
|
133
|
-
result[key] = JSON.parse(result[key]);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
function urlEncodedParser(v) {
|
|
139
|
-
return new WooksURLSearchParams(v.trim()).toJson();
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
isJson,
|
|
143
|
-
isHtml,
|
|
144
|
-
isXml,
|
|
145
|
-
isText,
|
|
146
|
-
isBinary,
|
|
147
|
-
isFormData,
|
|
148
|
-
isUrlencoded,
|
|
149
|
-
isCompressed,
|
|
150
|
-
contentEncodings,
|
|
151
|
-
parseBody,
|
|
152
|
-
rawBody,
|
|
153
|
-
};
|
|
21
|
+
const { store } = useHttpContext();
|
|
22
|
+
const { init } = store("request");
|
|
23
|
+
const { rawBody } = useRequest();
|
|
24
|
+
const { "content-type": contentType, "content-encoding": contentEncoding } = useHeaders();
|
|
25
|
+
function contentIs(type) {
|
|
26
|
+
return (contentType || "").includes(type);
|
|
27
|
+
}
|
|
28
|
+
const isJson = () => init("isJson", () => contentIs("application/json"));
|
|
29
|
+
const isHtml = () => init("isHtml", () => contentIs("text/html"));
|
|
30
|
+
const isXml = () => init("isXml", () => contentIs("text/xml"));
|
|
31
|
+
const isText = () => init("isText", () => contentIs("text/plain"));
|
|
32
|
+
const isBinary = () => init("isBinary", () => contentIs("application/octet-stream"));
|
|
33
|
+
const isFormData = () => init("isFormData", () => contentIs("multipart/form-data"));
|
|
34
|
+
const isUrlencoded = () => init("isUrlencoded", () => contentIs("application/x-www-form-urlencoded"));
|
|
35
|
+
const isCompressed = () => init("isCompressed", () => {
|
|
36
|
+
const parts = contentEncodings();
|
|
37
|
+
for (const p of parts) if ([
|
|
38
|
+
"deflate",
|
|
39
|
+
"gzip",
|
|
40
|
+
"br"
|
|
41
|
+
].includes(p)) return true;
|
|
42
|
+
return false;
|
|
43
|
+
});
|
|
44
|
+
const contentEncodings = () => init("contentEncodings", () => (contentEncoding || "").split(",").map((p) => p.trim()).filter((p) => !!p));
|
|
45
|
+
const parseBody = () => init("parsed", async () => {
|
|
46
|
+
const body = await uncompressBody(contentEncodings(), (await rawBody()).toString());
|
|
47
|
+
if (isJson()) return jsonParser(body);
|
|
48
|
+
else if (isFormData()) return formDataParser(body);
|
|
49
|
+
else if (isUrlencoded()) return urlEncodedParser(body);
|
|
50
|
+
else if (isBinary()) return textParser(body);
|
|
51
|
+
else return textParser(body);
|
|
52
|
+
});
|
|
53
|
+
function jsonParser(v) {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(v);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
throw new HttpError(400, error.message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function textParser(v) {
|
|
61
|
+
return v;
|
|
62
|
+
}
|
|
63
|
+
function formDataParser(v) {
|
|
64
|
+
const boundary = `--${(/boundary=([^;]+)(?:;|$)/u.exec(contentType || "") || [, ""])[1]}`;
|
|
65
|
+
if (!boundary) throw new HttpError(EHttpStatusCode.BadRequest, "form-data boundary not recognized");
|
|
66
|
+
const parts = v.trim().split(boundary);
|
|
67
|
+
const result = {};
|
|
68
|
+
let key = "";
|
|
69
|
+
let partContentType = "text/plain";
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
parsePart();
|
|
72
|
+
key = "";
|
|
73
|
+
partContentType = "text/plain";
|
|
74
|
+
let valueMode = false;
|
|
75
|
+
const lines = part.trim().split(/\n/u).map((s) => s.trim());
|
|
76
|
+
for (const line of lines) if (valueMode) if (result[key]) result[key] += `\n${line}`;
|
|
77
|
+
else result[key] = line;
|
|
78
|
+
else {
|
|
79
|
+
if (!line || line === "--") {
|
|
80
|
+
valueMode = !!key;
|
|
81
|
+
if (valueMode) key = key.replace(/^["']/u, "").replace(/["']$/u, "");
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (line.toLowerCase().startsWith("content-disposition: form-data;")) {
|
|
85
|
+
key = (/name=([^;]+)/.exec(line) || [])[1];
|
|
86
|
+
if (!key) throw new HttpError(EHttpStatusCode.BadRequest, `Could not read multipart name: ${line}`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (line.toLowerCase().startsWith("content-type:")) {
|
|
90
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1];
|
|
91
|
+
if (!partContentType) throw new HttpError(EHttpStatusCode.BadRequest, `Could not read content-type: ${line}`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
parsePart();
|
|
97
|
+
function parsePart() {
|
|
98
|
+
if (key && partContentType.includes("application/json")) result[key] = JSON.parse(result[key]);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
function urlEncodedParser(v) {
|
|
103
|
+
return new WooksURLSearchParams(v.trim()).toJson();
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
isJson,
|
|
107
|
+
isHtml,
|
|
108
|
+
isXml,
|
|
109
|
+
isText,
|
|
110
|
+
isBinary,
|
|
111
|
+
isFormData,
|
|
112
|
+
isUrlencoded,
|
|
113
|
+
isCompressed,
|
|
114
|
+
contentEncodings,
|
|
115
|
+
parseBody,
|
|
116
|
+
rawBody
|
|
117
|
+
};
|
|
154
118
|
}
|
|
155
119
|
function registerBodyCompressor(name, compressor) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
compressors[name] = compressor;
|
|
120
|
+
if (compressors[name]) throw new Error(`Body compressor "${name}" already registered.`);
|
|
121
|
+
compressors[name] = compressor;
|
|
160
122
|
}
|
|
161
123
|
|
|
162
|
-
|
|
124
|
+
//#endregion
|
|
125
|
+
export { registerBodyCompressor, useBody };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-body",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.25",
|
|
4
4
|
"description": "@wooksjs/http-body",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -38,8 +38,14 @@
|
|
|
38
38
|
"bugs": {
|
|
39
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
40
40
|
},
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
|
|
41
|
+
"peerDependencies": {},
|
|
42
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-body#readme",
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "^5.7.3",
|
|
45
|
+
"vitest": "^2.1.8",
|
|
46
|
+
"@wooksjs/event-http": "^0.5.25"
|
|
43
47
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rolldown -c ../../rolldown.config.mjs"
|
|
50
|
+
}
|
|
51
|
+
}
|