@wooksjs/http-body 0.5.20 → 0.6.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/index.cjs +143 -158
- package/dist/index.d.ts +2 -10
- package/dist/index.mjs +120 -156
- 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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return newBody;
|
|
27
|
+
//#region packages/http-body/src/utils/safe-json.ts
|
|
28
|
+
const ILLEGAL_KEYS = [
|
|
29
|
+
"__proto__",
|
|
30
|
+
"constructor",
|
|
31
|
+
"prototype"
|
|
32
|
+
];
|
|
33
|
+
const illigalKeySet = new Set(ILLEGAL_KEYS);
|
|
34
|
+
function safeJsonParse(src) {
|
|
35
|
+
return JSON.parse(src, (key, value) => {
|
|
36
|
+
assertKey(key);
|
|
37
|
+
return value;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function assertKey(k) {
|
|
41
|
+
if (illigalKeySet.has(k)) throw new __wooksjs_event_http.HttpError(400, `Illegal key name "${k}"`);
|
|
21
42
|
}
|
|
22
43
|
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region packages/http-body/src/body.ts
|
|
23
46
|
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
|
-
|
|
123
|
-
|
|
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
|
-
};
|
|
156
|
-
}
|
|
157
|
-
function registerBodyCompressor(name, compressor) {
|
|
158
|
-
if (compressors[name]) {
|
|
159
|
-
throw new Error(`Body compressor "${name}" already registered.`);
|
|
160
|
-
}
|
|
161
|
-
compressors[name] = compressor;
|
|
47
|
+
const { store } = (0, __wooksjs_event_http.useHttpContext)();
|
|
48
|
+
const { init } = store("request");
|
|
49
|
+
const { rawBody } = (0, __wooksjs_event_http.useRequest)();
|
|
50
|
+
const { "content-type": contentType } = (0, __wooksjs_event_http.useHeaders)();
|
|
51
|
+
function contentIs(type) {
|
|
52
|
+
return (contentType || "").includes(type);
|
|
53
|
+
}
|
|
54
|
+
const isJson = () => init("isJson", () => contentIs("application/json"));
|
|
55
|
+
const isHtml = () => init("isHtml", () => contentIs("text/html"));
|
|
56
|
+
const isXml = () => init("isXml", () => contentIs("text/xml"));
|
|
57
|
+
const isText = () => init("isText", () => contentIs("text/plain"));
|
|
58
|
+
const isBinary = () => init("isBinary", () => contentIs("application/octet-stream"));
|
|
59
|
+
const isFormData = () => init("isFormData", () => contentIs("multipart/form-data"));
|
|
60
|
+
const isUrlencoded = () => init("isUrlencoded", () => contentIs("application/x-www-form-urlencoded"));
|
|
61
|
+
const parseBody = () => init("parsed", async () => {
|
|
62
|
+
const body = await rawBody();
|
|
63
|
+
const sBody = body.toString();
|
|
64
|
+
if (isJson()) return jsonParser(sBody);
|
|
65
|
+
else if (isFormData()) return formDataParser(sBody);
|
|
66
|
+
else if (isUrlencoded()) return urlEncodedParser(sBody);
|
|
67
|
+
else if (isBinary()) return textParser(sBody);
|
|
68
|
+
else return textParser(sBody);
|
|
69
|
+
});
|
|
70
|
+
function jsonParser(v) {
|
|
71
|
+
try {
|
|
72
|
+
return safeJsonParse(v);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new __wooksjs_event_http.HttpError(400, error.message);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function textParser(v) {
|
|
78
|
+
return v;
|
|
79
|
+
}
|
|
80
|
+
function formDataParser(v) {
|
|
81
|
+
const MAX_PARTS = 255;
|
|
82
|
+
const MAX_KEY_LENGTH = 100;
|
|
83
|
+
const MAX_VALUE_LENGTH = 102400;
|
|
84
|
+
const boundary = `--${(/boundary=([^;]+)(?:;|$)/u.exec(contentType || "") || [, ""])[1]}`;
|
|
85
|
+
if (!boundary) throw new __wooksjs_event_http.HttpError(__wooksjs_event_http.EHttpStatusCode.BadRequest, "form-data boundary not recognized");
|
|
86
|
+
const parts = v.trim().split(boundary);
|
|
87
|
+
const result = Object.create(null);
|
|
88
|
+
let key = "";
|
|
89
|
+
let partContentType = "text/plain";
|
|
90
|
+
let partCount = 0;
|
|
91
|
+
for (const part of parts) {
|
|
92
|
+
parsePart();
|
|
93
|
+
key = "";
|
|
94
|
+
partContentType = "text/plain";
|
|
95
|
+
if (!part.trim() || part.trim() === "--") continue;
|
|
96
|
+
partCount++;
|
|
97
|
+
if (partCount > MAX_PARTS) throw new __wooksjs_event_http.HttpError(413, "Too many form fields");
|
|
98
|
+
let valueMode = false;
|
|
99
|
+
const lines = part.trim().split(/\n/u).map((l) => l.trim());
|
|
100
|
+
for (const line of lines) {
|
|
101
|
+
if (valueMode) {
|
|
102
|
+
if (line.length + String(result[key] ?? "").length > MAX_VALUE_LENGTH) throw new __wooksjs_event_http.HttpError(413, `Field "${key}" is too large`);
|
|
103
|
+
result[key] = (result[key] ? `${result[key]}\n` : "") + line;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (!line) {
|
|
107
|
+
valueMode = !!key;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (line.toLowerCase().startsWith("content-disposition: form-data;")) {
|
|
111
|
+
key = (/name=([^;]+)/.exec(line) || [])[1].replace(/^["']|["']$/g, "") ?? "";
|
|
112
|
+
if (!key) throw new __wooksjs_event_http.HttpError(400, `Could not read multipart name: ${line}`);
|
|
113
|
+
if (key.length > MAX_KEY_LENGTH) throw new __wooksjs_event_http.HttpError(413, "Field name too long");
|
|
114
|
+
if ([
|
|
115
|
+
"__proto__",
|
|
116
|
+
"constructor",
|
|
117
|
+
"prototype"
|
|
118
|
+
].includes(key)) throw new __wooksjs_event_http.HttpError(400, `Illegal key name "${key}"`);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (line.toLowerCase().startsWith("content-type:")) {
|
|
122
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1] ?? "";
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
parsePart();
|
|
128
|
+
return result;
|
|
129
|
+
function parsePart() {
|
|
130
|
+
if (key && partContentType.includes("application/json") && typeof result[key] === "string") result[key] = safeJsonParse(result[key]);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function urlEncodedParser(v) {
|
|
134
|
+
return new __wooksjs_event_http.WooksURLSearchParams(v.trim()).toJson();
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
isJson,
|
|
138
|
+
isHtml,
|
|
139
|
+
isXml,
|
|
140
|
+
isText,
|
|
141
|
+
isBinary,
|
|
142
|
+
isFormData,
|
|
143
|
+
isUrlencoded,
|
|
144
|
+
parseBody,
|
|
145
|
+
rawBody
|
|
146
|
+
};
|
|
162
147
|
}
|
|
163
148
|
|
|
164
|
-
|
|
165
|
-
exports.useBody = useBody
|
|
149
|
+
//#endregion
|
|
150
|
+
exports.useBody = useBody
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
interface TBodyCompressor {
|
|
2
|
-
compress: (data: string) => string | Promise<string>;
|
|
3
|
-
uncompress: (data: string) => string | Promise<string>;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
1
|
declare function useBody(): {
|
|
7
2
|
isJson: () => boolean;
|
|
8
3
|
isHtml: () => boolean;
|
|
@@ -11,11 +6,8 @@ declare function useBody(): {
|
|
|
11
6
|
isBinary: () => boolean;
|
|
12
7
|
isFormData: () => boolean;
|
|
13
8
|
isUrlencoded: () => boolean;
|
|
14
|
-
isCompressed: () => boolean;
|
|
15
|
-
contentEncodings: () => string[];
|
|
16
9
|
parseBody: <T>() => Promise<T>;
|
|
17
|
-
rawBody: () => Promise<Buffer
|
|
10
|
+
rawBody: () => Promise<Buffer<ArrayBufferLike>>;
|
|
18
11
|
};
|
|
19
|
-
declare function registerBodyCompressor(name: string, compressor: TBodyCompressor): void;
|
|
20
12
|
|
|
21
|
-
export {
|
|
13
|
+
export { useBody };
|
package/dist/index.mjs
CHANGED
|
@@ -1,162 +1,126 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EHttpStatusCode, HttpError, WooksURLSearchParams, useHeaders, useHttpContext, useRequest } from "@wooksjs/event-http";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return newBody;
|
|
3
|
+
//#region packages/http-body/src/utils/safe-json.ts
|
|
4
|
+
const ILLEGAL_KEYS = [
|
|
5
|
+
"__proto__",
|
|
6
|
+
"constructor",
|
|
7
|
+
"prototype"
|
|
8
|
+
];
|
|
9
|
+
const illigalKeySet = new Set(ILLEGAL_KEYS);
|
|
10
|
+
function safeJsonParse(src) {
|
|
11
|
+
return JSON.parse(src, (key, value) => {
|
|
12
|
+
assertKey(key);
|
|
13
|
+
return value;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function assertKey(k) {
|
|
17
|
+
if (illigalKeySet.has(k)) throw new HttpError(400, `Illegal key name "${k}"`);
|
|
19
18
|
}
|
|
20
19
|
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region packages/http-body/src/body.ts
|
|
21
22
|
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
|
-
|
|
121
|
-
|
|
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
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function registerBodyCompressor(name, compressor) {
|
|
156
|
-
if (compressors[name]) {
|
|
157
|
-
throw new Error(`Body compressor "${name}" already registered.`);
|
|
158
|
-
}
|
|
159
|
-
compressors[name] = compressor;
|
|
23
|
+
const { store } = useHttpContext();
|
|
24
|
+
const { init } = store("request");
|
|
25
|
+
const { rawBody } = useRequest();
|
|
26
|
+
const { "content-type": contentType } = useHeaders();
|
|
27
|
+
function contentIs(type) {
|
|
28
|
+
return (contentType || "").includes(type);
|
|
29
|
+
}
|
|
30
|
+
const isJson = () => init("isJson", () => contentIs("application/json"));
|
|
31
|
+
const isHtml = () => init("isHtml", () => contentIs("text/html"));
|
|
32
|
+
const isXml = () => init("isXml", () => contentIs("text/xml"));
|
|
33
|
+
const isText = () => init("isText", () => contentIs("text/plain"));
|
|
34
|
+
const isBinary = () => init("isBinary", () => contentIs("application/octet-stream"));
|
|
35
|
+
const isFormData = () => init("isFormData", () => contentIs("multipart/form-data"));
|
|
36
|
+
const isUrlencoded = () => init("isUrlencoded", () => contentIs("application/x-www-form-urlencoded"));
|
|
37
|
+
const parseBody = () => init("parsed", async () => {
|
|
38
|
+
const body = await rawBody();
|
|
39
|
+
const sBody = body.toString();
|
|
40
|
+
if (isJson()) return jsonParser(sBody);
|
|
41
|
+
else if (isFormData()) return formDataParser(sBody);
|
|
42
|
+
else if (isUrlencoded()) return urlEncodedParser(sBody);
|
|
43
|
+
else if (isBinary()) return textParser(sBody);
|
|
44
|
+
else return textParser(sBody);
|
|
45
|
+
});
|
|
46
|
+
function jsonParser(v) {
|
|
47
|
+
try {
|
|
48
|
+
return safeJsonParse(v);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new HttpError(400, error.message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function textParser(v) {
|
|
54
|
+
return v;
|
|
55
|
+
}
|
|
56
|
+
function formDataParser(v) {
|
|
57
|
+
const MAX_PARTS = 255;
|
|
58
|
+
const MAX_KEY_LENGTH = 100;
|
|
59
|
+
const MAX_VALUE_LENGTH = 102400;
|
|
60
|
+
const boundary = `--${(/boundary=([^;]+)(?:;|$)/u.exec(contentType || "") || [, ""])[1]}`;
|
|
61
|
+
if (!boundary) throw new HttpError(EHttpStatusCode.BadRequest, "form-data boundary not recognized");
|
|
62
|
+
const parts = v.trim().split(boundary);
|
|
63
|
+
const result = Object.create(null);
|
|
64
|
+
let key = "";
|
|
65
|
+
let partContentType = "text/plain";
|
|
66
|
+
let partCount = 0;
|
|
67
|
+
for (const part of parts) {
|
|
68
|
+
parsePart();
|
|
69
|
+
key = "";
|
|
70
|
+
partContentType = "text/plain";
|
|
71
|
+
if (!part.trim() || part.trim() === "--") continue;
|
|
72
|
+
partCount++;
|
|
73
|
+
if (partCount > MAX_PARTS) throw new HttpError(413, "Too many form fields");
|
|
74
|
+
let valueMode = false;
|
|
75
|
+
const lines = part.trim().split(/\n/u).map((l) => l.trim());
|
|
76
|
+
for (const line of lines) {
|
|
77
|
+
if (valueMode) {
|
|
78
|
+
if (line.length + String(result[key] ?? "").length > MAX_VALUE_LENGTH) throw new HttpError(413, `Field "${key}" is too large`);
|
|
79
|
+
result[key] = (result[key] ? `${result[key]}\n` : "") + line;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!line) {
|
|
83
|
+
valueMode = !!key;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (line.toLowerCase().startsWith("content-disposition: form-data;")) {
|
|
87
|
+
key = (/name=([^;]+)/.exec(line) || [])[1].replace(/^["']|["']$/g, "") ?? "";
|
|
88
|
+
if (!key) throw new HttpError(400, `Could not read multipart name: ${line}`);
|
|
89
|
+
if (key.length > MAX_KEY_LENGTH) throw new HttpError(413, "Field name too long");
|
|
90
|
+
if ([
|
|
91
|
+
"__proto__",
|
|
92
|
+
"constructor",
|
|
93
|
+
"prototype"
|
|
94
|
+
].includes(key)) throw new HttpError(400, `Illegal key name "${key}"`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (line.toLowerCase().startsWith("content-type:")) {
|
|
98
|
+
partContentType = (/content-type:\s?([^;]+)/i.exec(line) || [])[1] ?? "";
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
parsePart();
|
|
104
|
+
return result;
|
|
105
|
+
function parsePart() {
|
|
106
|
+
if (key && partContentType.includes("application/json") && typeof result[key] === "string") result[key] = safeJsonParse(result[key]);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function urlEncodedParser(v) {
|
|
110
|
+
return new WooksURLSearchParams(v.trim()).toJson();
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
isJson,
|
|
114
|
+
isHtml,
|
|
115
|
+
isXml,
|
|
116
|
+
isText,
|
|
117
|
+
isBinary,
|
|
118
|
+
isFormData,
|
|
119
|
+
isUrlencoded,
|
|
120
|
+
parseBody,
|
|
121
|
+
rawBody
|
|
122
|
+
};
|
|
160
123
|
}
|
|
161
124
|
|
|
162
|
-
|
|
125
|
+
//#endregion
|
|
126
|
+
export { useBody };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-body",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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
|
-
"
|
|
42
|
-
|
|
41
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-body#readme",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^22.10.6",
|
|
44
|
+
"typescript": "^5.7.3",
|
|
45
|
+
"vitest": "^2.1.8",
|
|
46
|
+
"@wooksjs/event-http": "^0.6.0"
|
|
43
47
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rolldown -c ../../rolldown.config.mjs"
|
|
50
|
+
}
|
|
51
|
+
}
|