@wooksjs/http-body 0.7.10 → 0.7.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.cjs +20 -43
- package/dist/index.mjs +1 -2
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,28 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
25
|
-
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _wooksjs_event_core = require("@wooksjs/event-core");
|
|
3
|
+
let _wooksjs_event_http = require("@wooksjs/event-http");
|
|
26
4
|
|
|
27
5
|
//#region packages/http-body/src/utils/safe-json.ts
|
|
28
6
|
const ILLEGAL_KEYS = [
|
|
@@ -43,7 +21,7 @@ function assertNoProtoKeys(obj) {
|
|
|
43
21
|
}
|
|
44
22
|
const record = obj;
|
|
45
23
|
for (const key of Object.keys(record)) {
|
|
46
|
-
if (key === ILLEGAL_KEYS[0] || key === ILLEGAL_KEYS[1] || key === ILLEGAL_KEYS[2]) throw new
|
|
24
|
+
if (key === ILLEGAL_KEYS[0] || key === ILLEGAL_KEYS[1] || key === ILLEGAL_KEYS[2]) throw new _wooksjs_event_http.HttpError(400, `Illegal key name "${key}"`);
|
|
47
25
|
assertNoProtoKeys(record[key]);
|
|
48
26
|
}
|
|
49
27
|
}
|
|
@@ -59,17 +37,16 @@ const CONTENT_TYPE_MAP = {
|
|
|
59
37
|
"form-data": "multipart/form-data",
|
|
60
38
|
urlencoded: "application/x-www-form-urlencoded"
|
|
61
39
|
};
|
|
62
|
-
const contentIsSlot = (0,
|
|
63
|
-
const contentType = (0,
|
|
40
|
+
const contentIsSlot = (0, _wooksjs_event_core.cachedBy)((type, ctx) => {
|
|
41
|
+
const contentType = (0, _wooksjs_event_http.useHeaders)(ctx)["content-type"] || "";
|
|
64
42
|
const mime = CONTENT_TYPE_MAP[type] || type;
|
|
65
43
|
return contentType.includes(mime);
|
|
66
44
|
});
|
|
67
|
-
const parsedBodySlot = (0,
|
|
68
|
-
const { rawBody } = (0,
|
|
69
|
-
const contentType = (0,
|
|
45
|
+
const parsedBodySlot = (0, _wooksjs_event_core.cached)(async (ctx) => {
|
|
46
|
+
const { rawBody } = (0, _wooksjs_event_http.useRequest)(ctx);
|
|
47
|
+
const contentType = (0, _wooksjs_event_http.useHeaders)(ctx)["content-type"] || "";
|
|
70
48
|
const contentIs = (type) => contentType.includes(type);
|
|
71
|
-
const
|
|
72
|
-
const sBody = body.toString();
|
|
49
|
+
const sBody = (await rawBody()).toString();
|
|
73
50
|
if (contentIs("application/json")) return jsonParser(sBody);
|
|
74
51
|
else if (contentIs("multipart/form-data")) return formDataParser(sBody, contentType);
|
|
75
52
|
else if (contentIs("application/x-www-form-urlencoded")) return urlEncodedParser(sBody);
|
|
@@ -79,7 +56,7 @@ function jsonParser(v) {
|
|
|
79
56
|
try {
|
|
80
57
|
return safeJsonParse(v);
|
|
81
58
|
} catch (error) {
|
|
82
|
-
throw new
|
|
59
|
+
throw new _wooksjs_event_http.HttpError(400, error.message);
|
|
83
60
|
}
|
|
84
61
|
}
|
|
85
62
|
function formDataParser(v, contentType) {
|
|
@@ -87,7 +64,7 @@ function formDataParser(v, contentType) {
|
|
|
87
64
|
const MAX_KEY_LENGTH = 100;
|
|
88
65
|
const MAX_VALUE_LENGTH = 100 * 1024;
|
|
89
66
|
const boundary = `--${(/boundary=([^;]+)(?:;|$)/u.exec(contentType || "") || [, ""])[1]}`;
|
|
90
|
-
if (!boundary) throw new
|
|
67
|
+
if (!boundary) throw new _wooksjs_event_http.HttpError(_wooksjs_event_http.EHttpStatusCode.BadRequest, "form-data boundary not recognized");
|
|
91
68
|
const parts = v.trim().split(boundary);
|
|
92
69
|
const result = Object.create(null);
|
|
93
70
|
let key = "";
|
|
@@ -99,12 +76,12 @@ function formDataParser(v, contentType) {
|
|
|
99
76
|
partContentType = "text/plain";
|
|
100
77
|
if (!part.trim() || part.trim() === "--") continue;
|
|
101
78
|
partCount++;
|
|
102
|
-
if (partCount > MAX_PARTS) throw new
|
|
79
|
+
if (partCount > MAX_PARTS) throw new _wooksjs_event_http.HttpError(413, "Too many form fields");
|
|
103
80
|
let valueMode = false;
|
|
104
81
|
const lines = part.trim().split(/\n/u).map((l) => l.trim());
|
|
105
82
|
for (const line of lines) {
|
|
106
83
|
if (valueMode) {
|
|
107
|
-
if (line.length + String(result[key] ?? "").length > MAX_VALUE_LENGTH) throw new
|
|
84
|
+
if (line.length + String(result[key] ?? "").length > MAX_VALUE_LENGTH) throw new _wooksjs_event_http.HttpError(413, `Field "${key}" is too large`);
|
|
108
85
|
result[key] = (result[key] ? `${result[key]}\n` : "") + line;
|
|
109
86
|
continue;
|
|
110
87
|
}
|
|
@@ -114,13 +91,13 @@ function formDataParser(v, contentType) {
|
|
|
114
91
|
}
|
|
115
92
|
if (line.toLowerCase().startsWith("content-disposition: form-data;")) {
|
|
116
93
|
key = (/name=([^;]+)/.exec(line) || [])[1].replace(/^["']|["']$/g, "") ?? "";
|
|
117
|
-
if (!key) throw new
|
|
118
|
-
if (key.length > MAX_KEY_LENGTH) throw new
|
|
94
|
+
if (!key) throw new _wooksjs_event_http.HttpError(400, `Could not read multipart name: ${line}`);
|
|
95
|
+
if (key.length > MAX_KEY_LENGTH) throw new _wooksjs_event_http.HttpError(413, "Field name too long");
|
|
119
96
|
if ([
|
|
120
97
|
"__proto__",
|
|
121
98
|
"constructor",
|
|
122
99
|
"prototype"
|
|
123
|
-
].includes(key)) throw new
|
|
100
|
+
].includes(key)) throw new _wooksjs_event_http.HttpError(400, `Illegal key name "${key}"`);
|
|
124
101
|
continue;
|
|
125
102
|
}
|
|
126
103
|
if (line.toLowerCase().startsWith("content-type:")) {
|
|
@@ -136,7 +113,7 @@ function formDataParser(v, contentType) {
|
|
|
136
113
|
}
|
|
137
114
|
}
|
|
138
115
|
function urlEncodedParser(v) {
|
|
139
|
-
return new
|
|
116
|
+
return new _wooksjs_event_http.WooksURLSearchParams(v.trim()).toJson();
|
|
140
117
|
}
|
|
141
118
|
/**
|
|
142
119
|
* Composable that provides request body parsing utilities for various content types.
|
|
@@ -154,8 +131,8 @@ function urlEncodedParser(v) {
|
|
|
154
131
|
*
|
|
155
132
|
* @returns Object with `is(type)` checker, `parseBody` function, and `rawBody` accessor.
|
|
156
133
|
*/
|
|
157
|
-
const useBody = (0,
|
|
158
|
-
const { rawBody } = (0,
|
|
134
|
+
const useBody = (0, _wooksjs_event_core.defineWook)((ctx) => {
|
|
135
|
+
const { rawBody } = (0, _wooksjs_event_http.useRequest)(ctx);
|
|
159
136
|
return {
|
|
160
137
|
is: (type) => contentIsSlot(type, ctx),
|
|
161
138
|
parseBody: () => ctx.get(parsedBodySlot),
|
package/dist/index.mjs
CHANGED
|
@@ -45,8 +45,7 @@ const parsedBodySlot = cached(async (ctx) => {
|
|
|
45
45
|
const { rawBody } = useRequest(ctx);
|
|
46
46
|
const contentType = useHeaders(ctx)["content-type"] || "";
|
|
47
47
|
const contentIs = (type) => contentType.includes(type);
|
|
48
|
-
const
|
|
49
|
-
const sBody = body.toString();
|
|
48
|
+
const sBody = (await rawBody()).toString();
|
|
50
49
|
if (contentIs("application/json")) return jsonParser(sBody);
|
|
51
50
|
else if (contentIs("multipart/form-data")) return formDataParser(sBody, contentType);
|
|
52
51
|
else if (contentIs("application/x-www-form-urlencoded")) return urlEncodedParser(sBody);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-body",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.12",
|
|
4
4
|
"description": "@wooksjs/http-body",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"typescript": "^5.9.3",
|
|
44
44
|
"vitest": "^3.2.4",
|
|
45
|
-
"@wooksjs/event-core": "^0.7.
|
|
46
|
-
"@wooksjs/event-http": "^0.7.
|
|
45
|
+
"@wooksjs/event-core": "^0.7.12",
|
|
46
|
+
"@wooksjs/event-http": "^0.7.12"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@wooksjs/event-http": "^0.7.
|
|
50
|
-
"@wooksjs/event-core": "^0.7.
|
|
49
|
+
"@wooksjs/event-http": "^0.7.12",
|
|
50
|
+
"@wooksjs/event-core": "^0.7.12"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "rolldown -c ../../rolldown.config.mjs"
|